Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 1 | #include "config_parser.hpp" |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 2 | #include "util.hpp" |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 3 | #include "types.hpp" |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 4 | #include "xyz/openbmc_project/Common/error.hpp" |
| 5 | |
| 6 | #include <phosphor-logging/log.hpp> |
| 7 | #include <phosphor-logging/elog-errors.hpp> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 8 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 9 | #include <arpa/inet.h> |
| 10 | #include <dirent.h> |
| 11 | #include <net/if.h> |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 12 | #include <sys/wait.h> |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 13 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 14 | #include <iostream> |
| 15 | #include <list> |
| 16 | #include <string> |
| 17 | #include <algorithm> |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 18 | #include <experimental/filesystem> |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 19 | |
| 20 | namespace phosphor |
| 21 | { |
| 22 | namespace network |
| 23 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 24 | |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 25 | namespace |
| 26 | { |
| 27 | |
| 28 | using namespace phosphor::logging; |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 29 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 30 | namespace fs = std::experimental::filesystem; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 31 | |
| 32 | uint8_t toV6Cidr(const std::string& subnetMask) |
| 33 | { |
| 34 | uint8_t pos = 0; |
| 35 | uint8_t prevPos = 0; |
| 36 | uint8_t cidr = 0; |
| 37 | uint16_t buff {}; |
| 38 | do |
| 39 | { |
| 40 | //subnet mask look like ffff:ffff:: |
| 41 | // or ffff:c000:: |
| 42 | pos = subnetMask.find(":", prevPos); |
| 43 | if (pos == std::string::npos) |
| 44 | { |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | auto str = subnetMask.substr(prevPos, (pos - prevPos)); |
| 49 | prevPos = pos + 1; |
| 50 | |
| 51 | // String length is 0 |
| 52 | if (!str.length()) |
| 53 | { |
| 54 | return cidr; |
| 55 | } |
| 56 | //converts it into number. |
| 57 | if (sscanf(str.c_str(), "%hx", &buff) <= 0) |
| 58 | { |
| 59 | log<level::ERR>("Invalid Mask", |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 60 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | // convert the number into bitset |
| 66 | // and check for how many ones are there. |
| 67 | // if we don't have all the ones then make |
| 68 | // sure that all the ones should be left justify. |
| 69 | |
| 70 | if (__builtin_popcount(buff) != 16) |
| 71 | { |
| 72 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != __builtin_popcount(buff)) |
| 73 | { |
| 74 | log<level::ERR>("Invalid Mask", |
| 75 | entry("SUBNETMASK=%s", subnetMask)); |
| 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | cidr += __builtin_popcount(buff); |
| 80 | return cidr; |
| 81 | } |
| 82 | |
| 83 | cidr += 16; |
| 84 | } |
| 85 | while (1); |
| 86 | |
| 87 | return cidr; |
| 88 | } |
| 89 | }// anonymous namespace |
| 90 | |
| 91 | uint8_t toCidr(int addressFamily, const std::string& subnetMask) |
| 92 | { |
| 93 | if (addressFamily == AF_INET6) |
| 94 | { |
| 95 | return toV6Cidr(subnetMask); |
| 96 | } |
| 97 | |
| 98 | uint32_t buff; |
| 99 | |
| 100 | auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff); |
| 101 | if (rc <= 0) |
| 102 | { |
| 103 | log<level::ERR>("inet_pton failed:", |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 104 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | buff = be32toh(buff); |
| 109 | // total no of bits - total no of leading zero == total no of ones |
| 110 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == __builtin_popcount(buff)) |
| 111 | { |
| 112 | return __builtin_popcount(buff); |
| 113 | } |
| 114 | else |
| 115 | { |
| 116 | log<level::ERR>("Invalid Mask", |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 117 | entry("SUBNETMASK=%s", subnetMask)); |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | std::string toMask(int addressFamily, uint8_t prefix) |
| 123 | { |
| 124 | if (addressFamily == AF_INET6) |
| 125 | { |
| 126 | //TODO:- conversion for v6 |
| 127 | return ""; |
| 128 | } |
| 129 | |
| 130 | if (prefix < 1 || prefix > 30) |
| 131 | { |
| 132 | log<level::ERR>("Invalid Prefix", |
| 133 | entry("PREFIX=%d", prefix)); |
| 134 | return ""; |
| 135 | } |
| 136 | /* Create the netmask from the number of bits */ |
| 137 | unsigned long mask = 0; |
| 138 | for (auto i = 0 ; i < prefix ; i++) |
| 139 | { |
| 140 | mask |= 1 << (31 - i); |
| 141 | } |
| 142 | struct in_addr netmask; |
| 143 | netmask.s_addr = htonl(mask); |
| 144 | return inet_ntoa(netmask); |
| 145 | } |
| 146 | |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 147 | std::string getNetworkID(int addressFamily, const std::string& ipaddress, |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 148 | uint8_t prefix) |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 149 | { |
| 150 | unsigned char* pntMask = nullptr; |
| 151 | unsigned char* pntNetwork = nullptr; |
| 152 | int bit {}; |
| 153 | int offset {}; |
| 154 | struct in6_addr netmask {}; |
| 155 | const u_char maskbit[] = {0x00, 0x80, 0xc0, 0xe0, 0xf0, |
| 156 | 0xf8, 0xfc, 0xfe, 0xff |
| 157 | }; |
| 158 | |
| 159 | pntMask = reinterpret_cast<unsigned char*>(&netmask); |
| 160 | |
| 161 | offset = prefix / 8; |
| 162 | bit = prefix % 8; |
| 163 | |
| 164 | while (offset--) |
| 165 | { |
| 166 | *pntMask++ = 0xff; |
| 167 | } |
| 168 | |
| 169 | if (bit) |
| 170 | { |
| 171 | *pntMask = maskbit[bit]; |
| 172 | } |
| 173 | |
| 174 | // convert ipaddres string into network address |
| 175 | struct in6_addr ipaddressNetwork; |
| 176 | if (inet_pton(addressFamily, ipaddress.c_str(), &ipaddressNetwork) <= 0) |
| 177 | { |
| 178 | log<level::ERR>("inet_pton failure", |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 179 | entry("IPADDRESS=%s", ipaddress.c_str())); |
Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 180 | report<InternalFailure>(); |
| 181 | |
| 182 | return ""; |
| 183 | } |
| 184 | |
| 185 | // Now bit wise and gets you the network address |
| 186 | pntMask = reinterpret_cast<unsigned char*>(&netmask); |
| 187 | pntNetwork = reinterpret_cast<unsigned char*>(&ipaddressNetwork); |
| 188 | |
| 189 | for (int i = 0; i < 16 ; i++) |
| 190 | { |
| 191 | pntNetwork[i] = pntNetwork[i] & pntMask[i]; |
| 192 | } |
| 193 | |
| 194 | //convert the network address into string fomat. |
| 195 | char networkString[INET6_ADDRSTRLEN] = { 0 }; |
| 196 | if (inet_ntop(addressFamily, &ipaddressNetwork, networkString, |
| 197 | INET6_ADDRSTRLEN) == NULL) |
| 198 | { |
| 199 | log<level::ERR>("inet_ntop failure"); |
| 200 | report<InternalFailure>(); |
| 201 | } |
| 202 | return networkString; |
| 203 | } |
| 204 | |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 205 | bool isLinkLocalIP(const std::string& address) |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 206 | { |
Nagaraju Goruganti | 66b974d | 2017-10-03 08:43:08 -0500 | [diff] [blame] | 207 | return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0; |
| 208 | } |
| 209 | |
| 210 | bool isValidIP(int addressFamily, const std::string& address) |
| 211 | { |
| 212 | unsigned char buf[sizeof(struct in6_addr)]; |
| 213 | |
| 214 | return inet_pton(addressFamily, address.c_str(), buf) > 0; |
| 215 | } |
| 216 | |
| 217 | bool isValidPrefix(int addressFamily, uint8_t prefixLength) |
| 218 | { |
| 219 | if (addressFamily == AF_INET) |
| 220 | { |
| 221 | if (prefixLength < IPV4_MIN_PREFIX_LENGTH || |
| 222 | prefixLength > IPV4_MAX_PREFIX_LENGTH) |
| 223 | { |
| 224 | return false; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (addressFamily == AF_INET6) |
| 229 | { |
| 230 | if (prefixLength < IPV4_MIN_PREFIX_LENGTH || |
| 231 | prefixLength > IPV6_MAX_PREFIX_LENGTH) |
| 232 | { |
| 233 | return false; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | return true; |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 238 | } |
| 239 | |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 240 | IntfAddrMap getInterfaceAddrs() |
| 241 | { |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 242 | IntfAddrMap intfMap {}; |
| 243 | AddrList addrList {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 244 | struct ifaddrs* ifaddr = nullptr; |
| 245 | |
| 246 | // attempt to fill struct with ifaddrs |
| 247 | if (getifaddrs(&ifaddr) == -1) |
| 248 | { |
| 249 | auto error = errno; |
| 250 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 251 | entry("ERRNO=%s", strerror(error))); |
| 252 | elog<InternalFailure>(); |
| 253 | } |
| 254 | |
| 255 | AddrPtr ifaddrPtr(ifaddr); |
| 256 | ifaddr = nullptr; |
| 257 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 258 | std::string intfName {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 259 | |
| 260 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 261 | { |
| 262 | // walk interfaces |
| 263 | if (ifa->ifa_addr == nullptr) |
| 264 | { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | // get only INET interfaces not ipv6 |
| 269 | if (ifa->ifa_addr->sa_family == AF_INET || |
| 270 | ifa->ifa_addr->sa_family == AF_INET6) |
| 271 | { |
| 272 | // if loopback, or not running ignore |
| 273 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 274 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 275 | { |
| 276 | continue; |
| 277 | } |
| 278 | // if the interface name is not same as the previous |
| 279 | // iteration then add the addr list into |
| 280 | // the map. |
| 281 | if (intfName != "" && intfName != std::string(ifa->ifa_name)) |
| 282 | { |
| 283 | intfMap.emplace(intfName, addrList); |
| 284 | addrList.clear(); |
| 285 | } |
| 286 | intfName = ifa->ifa_name; |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 287 | AddrInfo info {}; |
Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 288 | char ip[INET6_ADDRSTRLEN] = { 0 }; |
| 289 | char subnetMask[INET6_ADDRSTRLEN] = { 0 }; |
| 290 | |
| 291 | if (ifa->ifa_addr->sa_family == AF_INET) |
| 292 | { |
| 293 | |
| 294 | inet_ntop(ifa->ifa_addr->sa_family, |
| 295 | &(((struct sockaddr_in*)(ifa->ifa_addr))->sin_addr), |
| 296 | ip, |
| 297 | sizeof(ip)); |
| 298 | |
| 299 | inet_ntop(ifa->ifa_addr->sa_family, |
| 300 | &(((struct sockaddr_in*)(ifa->ifa_netmask))->sin_addr), |
| 301 | subnetMask, |
| 302 | sizeof(subnetMask)); |
| 303 | |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | inet_ntop(ifa->ifa_addr->sa_family, |
| 308 | &(((struct sockaddr_in6*)(ifa->ifa_addr))->sin6_addr), |
| 309 | ip, |
| 310 | sizeof(ip)); |
| 311 | |
| 312 | inet_ntop(ifa->ifa_addr->sa_family, |
| 313 | &(((struct sockaddr_in6*)(ifa->ifa_netmask))->sin6_addr), |
| 314 | subnetMask, |
| 315 | sizeof(subnetMask)); |
| 316 | |
| 317 | } |
| 318 | |
| 319 | info.addrType = ifa->ifa_addr->sa_family; |
| 320 | info.ipaddress = ip; |
| 321 | info.prefix = toCidr(info.addrType, std::string(subnetMask)); |
| 322 | addrList.emplace_back(info); |
| 323 | } |
| 324 | } |
| 325 | intfMap.emplace(intfName, addrList); |
| 326 | return intfMap; |
| 327 | } |
| 328 | |
Ratan Gupta | fd4b0f0 | 2017-09-16 06:01:24 +0530 | [diff] [blame] | 329 | InterfaceList getInterfaces() |
| 330 | { |
| 331 | InterfaceList interfaces {}; |
| 332 | struct ifaddrs* ifaddr = nullptr; |
| 333 | |
| 334 | // attempt to fill struct with ifaddrs |
| 335 | if (getifaddrs(&ifaddr) == -1) |
| 336 | { |
| 337 | auto error = errno; |
| 338 | log<level::ERR>("Error occurred during the getifaddrs call", |
| 339 | entry("ERRNO=%d", error)); |
| 340 | elog<InternalFailure>(); |
| 341 | } |
| 342 | |
| 343 | AddrPtr ifaddrPtr(ifaddr); |
| 344 | ifaddr = nullptr; |
| 345 | |
| 346 | for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next) |
| 347 | { |
| 348 | // walk interfaces |
| 349 | // if loopback, or not running ignore |
| 350 | if ((ifa->ifa_flags & IFF_LOOPBACK) || |
| 351 | !(ifa->ifa_flags & IFF_RUNNING)) |
| 352 | { |
| 353 | continue; |
| 354 | } |
| 355 | interfaces.emplace(ifa->ifa_name); |
| 356 | } |
| 357 | return interfaces; |
| 358 | } |
| 359 | |
| 360 | |
Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 361 | void deleteInterface(const std::string& intf) |
| 362 | { |
| 363 | pid_t pid = fork(); |
| 364 | int status {}; |
| 365 | |
| 366 | if (pid == 0) |
| 367 | { |
| 368 | |
| 369 | execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); |
| 370 | auto error = errno; |
| 371 | log<level::ERR>("Couldn't delete the device", |
| 372 | entry("ERRNO=%d", error), |
| 373 | entry("INTF=%s", intf.c_str())); |
| 374 | elog<InternalFailure>(); |
| 375 | } |
| 376 | else if (pid < 0) |
| 377 | { |
| 378 | auto error = errno; |
| 379 | log<level::ERR>("Error occurred during fork", |
| 380 | entry("ERRNO=%d", error)); |
| 381 | elog<InternalFailure>(); |
| 382 | } |
| 383 | else if (pid > 0) |
| 384 | { |
| 385 | while (waitpid(pid, &status, 0) == -1) |
| 386 | { |
| 387 | if (errno != EINTR) |
| 388 | { /* Error other than EINTR */ |
| 389 | status = -1; |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if(status < 0) |
| 395 | { |
| 396 | log<level::ERR>("Unable to delete the interface", |
| 397 | entry("INTF=%s", intf.c_str(), |
| 398 | entry("STATUS=%d", status))); |
| 399 | elog<InternalFailure>(); |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 404 | bool getDHCPValue(const std::string& confDir, const std::string& intf) |
| 405 | { |
| 406 | bool dhcp = false; |
| 407 | // Get the interface mode value from systemd conf |
| 408 | //using namespace std::string_literals; |
| 409 | fs::path confPath = confDir; |
| 410 | std::string fileName = systemd::config::networkFilePrefix + intf + |
| 411 | systemd::config::networkFileSuffix; |
| 412 | confPath /= fileName; |
| 413 | |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 414 | auto rc = config::ReturnCode::SUCCESS; |
| 415 | config::ValueList values; |
| 416 | config::Parser parser(confPath.string()); |
| 417 | |
| 418 | std::tie(rc, values) = parser.getValues("Network", "DHCP"); |
| 419 | if (rc != config::ReturnCode::SUCCESS) |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 420 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 421 | log<level::DEBUG>("Unable to get the value for Network[DHCP]", |
| 422 | entry("RC=%d", rc)); |
| 423 | return dhcp; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 424 | } |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 425 | // There will be only single value for DHCP key. |
| 426 | if (values[0] == "true") |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 427 | { |
Ratan Gupta | c27170a | 2017-11-22 15:44:42 +0530 | [diff] [blame] | 428 | dhcp = true; |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 429 | } |
| 430 | return dhcp; |
| 431 | } |
| 432 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 433 | namespace internal |
| 434 | { |
Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 435 | |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 436 | void executeCommandinChildProcess(const char* path, char** args) |
| 437 | { |
| 438 | using namespace std::string_literals; |
| 439 | pid_t pid = fork(); |
| 440 | int status {}; |
| 441 | |
| 442 | if (pid == 0) |
| 443 | { |
| 444 | execv(path, args); |
| 445 | auto error = errno; |
| 446 | // create the command from var args. |
| 447 | std::string command = path + " "s; |
| 448 | |
| 449 | for(int i = 0; args[i]; i++) |
| 450 | { |
| 451 | command += args[i] + " "s; |
| 452 | } |
| 453 | |
| 454 | log<level::ERR>("Couldn't exceute the command", |
| 455 | entry("ERRNO=%d", error), |
| 456 | entry("CMD=%s", command.c_str())); |
| 457 | elog<InternalFailure>(); |
| 458 | } |
| 459 | else if (pid < 0) |
| 460 | { |
| 461 | auto error = errno; |
| 462 | log<level::ERR>("Error occurred during fork", |
| 463 | entry("ERRNO=%d", error)); |
| 464 | elog<InternalFailure>(); |
| 465 | } |
| 466 | else if (pid > 0) |
| 467 | { |
| 468 | while (waitpid(pid, &status, 0) == -1) |
| 469 | { |
| 470 | if (errno != EINTR) |
| 471 | { //Error other than EINTR |
| 472 | status = -1; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if(status < 0) |
| 478 | { |
| 479 | std::string command = path + " "s; |
| 480 | for(int i = 0; args[i]; i++) |
| 481 | { |
| 482 | command += args[i] + " "s; |
| 483 | } |
| 484 | |
| 485 | log<level::ERR>("Unable to execute the command", |
| 486 | entry("CMD=%s", command.c_str(), |
| 487 | entry("STATUS=%d", status))); |
| 488 | elog<InternalFailure>(); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | } |
| 493 | } //namespace internal |
| 494 | |
| 495 | namespace mac_address |
| 496 | { |
| 497 | |
| 498 | constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; |
| 499 | constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; |
| 500 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 501 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; |
| 502 | constexpr auto methodGet = "Get"; |
| 503 | |
| 504 | using DbusObjectPath = std::string; |
| 505 | using DbusService = std::string; |
| 506 | using DbusInterface = std::string; |
| 507 | using ObjectTree = std::map<DbusObjectPath, |
| 508 | std::map<DbusService, std::vector<DbusInterface>>>; |
| 509 | |
| 510 | constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager"; |
| 511 | constexpr auto invNetworkIntf = |
| 512 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; |
| 513 | constexpr auto invRoot = "/xyz/openbmc_project/inventory"; |
| 514 | |
| 515 | std::string getfromInventory(sdbusplus::bus::bus& bus) |
| 516 | { |
| 517 | std::vector<DbusInterface> interfaces; |
| 518 | interfaces.emplace_back(invNetworkIntf); |
| 519 | |
| 520 | auto depth = 0; |
| 521 | |
| 522 | auto mapperCall = bus.new_method_call(mapperBus, |
| 523 | mapperObj, |
| 524 | mapperIntf, |
| 525 | "GetSubTree"); |
| 526 | |
| 527 | mapperCall.append(invRoot, depth, interfaces); |
| 528 | |
| 529 | auto mapperReply = bus.call(mapperCall); |
| 530 | if (mapperReply.is_method_error()) |
| 531 | { |
| 532 | log<level::ERR>("Error in mapper call"); |
| 533 | elog<InternalFailure>(); |
| 534 | } |
| 535 | |
| 536 | ObjectTree objectTree; |
| 537 | mapperReply.read(objectTree); |
| 538 | |
| 539 | if (objectTree.empty()) |
| 540 | { |
| 541 | log<level::ERR>("No Object has implemented the interface", |
| 542 | entry("INTERFACE=%s", invNetworkIntf)); |
| 543 | elog<InternalFailure>(); |
| 544 | } |
| 545 | |
Gunnar Mills | a251a78 | 2017-09-26 16:49:08 -0500 | [diff] [blame] | 546 | // It is expected that only one object has implemented this interface. |
Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 547 | |
| 548 | auto objPath = objectTree.begin()->first; |
| 549 | auto service = objectTree.begin()->second.begin()->first; |
| 550 | |
| 551 | sdbusplus::message::variant<std::string> value; |
| 552 | |
| 553 | auto method = bus.new_method_call( |
| 554 | service.c_str(), |
| 555 | objPath.c_str(), |
| 556 | propIntf, |
| 557 | methodGet); |
| 558 | |
| 559 | method.append(invNetworkIntf, "MACAddress"); |
| 560 | |
| 561 | auto reply = bus.call(method); |
| 562 | if (reply.is_method_error()) |
| 563 | { |
| 564 | log<level::ERR>("Failed to get MACAddress", |
| 565 | entry("PATH=%s", objPath.c_str()), |
| 566 | entry("INTERFACE=%s", invNetworkIntf)); |
| 567 | elog<InternalFailure>(); |
| 568 | } |
| 569 | |
| 570 | reply.read(value); |
| 571 | return value.get<std::string>(); |
| 572 | } |
| 573 | |
| 574 | }//namespace mac_address |
Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 575 | }//namespace network |
| 576 | }//namespace phosphor |