Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 3 | #include <arpa/inet.h> |
| 4 | #include <dirent.h> |
| 5 | #include <net/if.h> |
| 6 | |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 7 | #include <algorithm> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | #include <xyz/openbmc_project/Common/error.hpp> |
| 11 | |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 12 | namespace ipmi |
| 13 | { |
| 14 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 15 | using namespace phosphor::logging; |
| 16 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 17 | |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 18 | namespace network |
| 19 | { |
| 20 | |
| 21 | /** @brief checks if the given ip is Link Local Ip or not. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 22 | * @param[in] ipaddress - IPAddress. |
| 23 | */ |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 24 | bool isLinkLocalIP(const std::string& ipaddress); |
| 25 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 26 | } // namespace network |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 27 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 28 | // TODO There may be cases where an interface is implemented by multiple |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 29 | // objects,to handle such cases we are interested on that object |
| 30 | // which are on interested busname. |
| 31 | // Currently mapper doesn't give the readable busname(gives busid) so we can't |
| 32 | // use busname to find the object,will do later once the support is there. |
| 33 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 34 | DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, |
| 35 | const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 36 | const std::string& serviceRoot, |
| 37 | const std::string& match) |
| 38 | { |
| 39 | std::vector<DbusInterface> interfaces; |
| 40 | interfaces.emplace_back(interface); |
| 41 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 42 | auto depth = 0; |
| 43 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 44 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, |
| 45 | MAPPER_INTF, "GetSubTree"); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 46 | |
| 47 | mapperCall.append(serviceRoot, depth, interfaces); |
| 48 | |
| 49 | auto mapperReply = bus.call(mapperCall); |
| 50 | if (mapperReply.is_method_error()) |
| 51 | { |
| 52 | log<level::ERR>("Error in mapper call"); |
| 53 | elog<InternalFailure>(); |
| 54 | } |
| 55 | |
| 56 | ObjectTree objectTree; |
| 57 | mapperReply.read(objectTree); |
| 58 | |
| 59 | if (objectTree.empty()) |
| 60 | { |
Patrick Venture | f0c4878 | 2017-09-21 18:48:51 -0700 | [diff] [blame] | 61 | log<level::ERR>("No Object has implemented the interface", |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 62 | entry("INTERFACE=%s", interface.c_str())); |
| 63 | elog<InternalFailure>(); |
| 64 | } |
| 65 | |
| 66 | DbusObjectInfo objectInfo; |
| 67 | |
| 68 | // if match is empty then return the first object |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 69 | if (match == "") |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 70 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 71 | objectInfo = std::make_pair( |
| 72 | objectTree.begin()->first, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 73 | std::move(objectTree.begin()->second.begin()->first)); |
| 74 | return objectInfo; |
| 75 | } |
| 76 | |
| 77 | // else search the match string in the object path |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 78 | auto found = std::find_if( |
| 79 | objectTree.begin(), objectTree.end(), [&match](const auto& object) { |
| 80 | return (object.first.find(match) != std::string::npos); |
| 81 | }); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 82 | |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 83 | if (found == objectTree.end()) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 84 | { |
| 85 | log<level::ERR>("Failed to find object which matches", |
| 86 | entry("MATCH=%s", match.c_str())); |
| 87 | elog<InternalFailure>(); |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 88 | // elog<> throws an exception. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 89 | } |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 90 | |
| 91 | return make_pair(found->first, std::move(found->second.begin()->first)); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 92 | } |
| 93 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 94 | DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus, |
| 95 | const std::string& interface, |
| 96 | const std::string& serviceRoot, |
| 97 | const std::string& match) |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 98 | { |
| 99 | auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match); |
| 100 | |
| 101 | if (objectTree.empty()) |
| 102 | { |
| 103 | log<level::ERR>("No Object has implemented the IP interface", |
| 104 | entry("INTERFACE=%s", interface.c_str())); |
| 105 | elog<InternalFailure>(); |
| 106 | } |
| 107 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 108 | DbusObjectInfo objectInfo; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 109 | |
| 110 | for (auto& object : objectTree) |
| 111 | { |
| 112 | auto variant = ipmi::getDbusProperty( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 113 | bus, object.second.begin()->first, object.first, |
| 114 | ipmi::network::IP_INTERFACE, "Address"); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 115 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 116 | objectInfo = std::make_pair(object.first, object.second.begin()->first); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 117 | |
| 118 | // if LinkLocalIP found look for Non-LinkLocalIP |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 119 | if (ipmi::network::isLinkLocalIP(variant.get<std::string>())) |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 120 | { |
| 121 | continue; |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | break; |
| 126 | } |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 127 | } |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 128 | return objectInfo; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 129 | } |
| 130 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 131 | Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 132 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 133 | const std::string& property) |
| 134 | { |
| 135 | |
| 136 | Value value; |
| 137 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 138 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 139 | PROP_INTF, METHOD_GET); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 140 | |
| 141 | method.append(interface, property); |
| 142 | |
| 143 | auto reply = bus.call(method); |
| 144 | |
| 145 | if (reply.is_method_error()) |
| 146 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 147 | log<level::ERR>("Failed to get property", |
| 148 | entry("PROPERTY=%s", property.c_str()), |
| 149 | entry("PATH=%s", objPath.c_str()), |
| 150 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 151 | elog<InternalFailure>(); |
| 152 | } |
| 153 | |
| 154 | reply.read(value); |
| 155 | |
| 156 | return value; |
| 157 | } |
| 158 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 159 | PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus, |
| 160 | const std::string& service, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 161 | const std::string& objPath, |
| 162 | const std::string& interface) |
| 163 | { |
| 164 | PropertyMap properties; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 165 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 166 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 167 | PROP_INTF, METHOD_GET_ALL); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 168 | |
| 169 | method.append(interface); |
| 170 | |
| 171 | auto reply = bus.call(method); |
| 172 | |
| 173 | if (reply.is_method_error()) |
| 174 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 175 | log<level::ERR>("Failed to get all properties", |
| 176 | entry("PATH=%s", objPath.c_str()), |
| 177 | entry("INTERFACE=%s", interface.c_str())); |
| 178 | elog<InternalFailure>(); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | reply.read(properties); |
| 182 | return properties; |
| 183 | } |
| 184 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 185 | ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 186 | const std::string& service, |
| 187 | const std::string& objPath) |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 188 | { |
| 189 | ipmi::ObjectValueTree interfaces; |
| 190 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 191 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 192 | "org.freedesktop.DBus.ObjectManager", |
| 193 | "GetManagedObjects"); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 194 | |
| 195 | auto reply = bus.call(method); |
| 196 | |
| 197 | if (reply.is_method_error()) |
| 198 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 199 | log<level::ERR>("Failed to get managed objects", |
| 200 | entry("PATH=%s", objPath.c_str())); |
| 201 | elog<InternalFailure>(); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | reply.read(interfaces); |
| 205 | return interfaces; |
| 206 | } |
| 207 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 208 | void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 209 | const std::string& objPath, const std::string& interface, |
| 210 | const std::string& property, const Value& value) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 211 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 212 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 213 | PROP_INTF, METHOD_SET); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 214 | |
| 215 | method.append(interface, property, value); |
| 216 | |
| 217 | if (!bus.call(method)) |
| 218 | { |
| 219 | log<level::ERR>("Failed to set property", |
| 220 | entry("PROPERTY=%s", property.c_str()), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 221 | entry("PATH=%s", objPath.c_str()), |
| 222 | entry("INTERFACE=%s", interface.c_str())); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 223 | elog<InternalFailure>(); |
| 224 | } |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 225 | } |
| 226 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 227 | ServiceCache::ServiceCache(const std::string& intf, const std::string& path) : |
| 228 | intf(intf), path(path), cachedService(std::experimental::nullopt), |
| 229 | cachedBusName(std::experimental::nullopt) |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 230 | { |
| 231 | } |
| 232 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 233 | ServiceCache::ServiceCache(std::string&& intf, std::string&& path) : |
| 234 | intf(std::move(intf)), path(std::move(path)), |
| 235 | cachedService(std::experimental::nullopt), |
| 236 | cachedBusName(std::experimental::nullopt) |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 237 | { |
| 238 | } |
| 239 | |
| 240 | const std::string& ServiceCache::getService(sdbusplus::bus::bus& bus) |
| 241 | { |
| 242 | if (!isValid(bus)) |
| 243 | { |
| 244 | cachedBusName = bus.get_unique_name(); |
| 245 | cachedService = ::ipmi::getService(bus, intf, path); |
| 246 | } |
| 247 | return cachedService.value(); |
| 248 | } |
| 249 | |
| 250 | void ServiceCache::invalidate() |
| 251 | { |
| 252 | cachedBusName = std::experimental::nullopt; |
| 253 | cachedService = std::experimental::nullopt; |
| 254 | } |
| 255 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 256 | sdbusplus::message::message |
| 257 | ServiceCache::newMethodCall(sdbusplus::bus::bus& bus, const char* intf, |
| 258 | const char* method) |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 259 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 260 | return bus.new_method_call(getService(bus).c_str(), path.c_str(), intf, |
| 261 | method); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | bool ServiceCache::isValid(sdbusplus::bus::bus& bus) const |
| 265 | { |
| 266 | return cachedService && cachedBusName == bus.get_unique_name(); |
| 267 | } |
| 268 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 269 | std::string getService(sdbusplus::bus::bus& bus, const std::string& intf, |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 270 | const std::string& path) |
| 271 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 272 | auto mapperCall = |
| 273 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 274 | "/xyz/openbmc_project/object_mapper", |
| 275 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 276 | |
| 277 | mapperCall.append(path); |
| 278 | mapperCall.append(std::vector<std::string>({intf})); |
| 279 | |
| 280 | auto mapperResponseMsg = bus.call(mapperCall); |
| 281 | |
| 282 | if (mapperResponseMsg.is_method_error()) |
| 283 | { |
| 284 | throw std::runtime_error("ERROR in mapper call"); |
| 285 | } |
| 286 | |
| 287 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 288 | mapperResponseMsg.read(mapperResponse); |
| 289 | |
| 290 | if (mapperResponse.begin() == mapperResponse.end()) |
| 291 | { |
| 292 | throw std::runtime_error("ERROR in reading the mapper response"); |
| 293 | } |
| 294 | |
| 295 | return mapperResponse.begin()->first; |
| 296 | } |
| 297 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 298 | ipmi::ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus, |
| 299 | const std::string& serviceRoot, |
| 300 | const std::string& interface, |
| 301 | const std::string& match) |
| 302 | { |
| 303 | std::vector<std::string> interfaces; |
| 304 | interfaces.emplace_back(interface); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 305 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 306 | auto depth = 0; |
| 307 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 308 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, |
| 309 | MAPPER_INTF, "GetSubTree"); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 310 | |
| 311 | mapperCall.append(serviceRoot, depth, interfaces); |
| 312 | |
| 313 | auto mapperReply = bus.call(mapperCall); |
| 314 | if (mapperReply.is_method_error()) |
| 315 | { |
| 316 | log<level::ERR>("Error in mapper call", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 317 | entry("SERVICEROOT=%s", serviceRoot.c_str()), |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 318 | entry("INTERFACE=%s", interface.c_str())); |
| 319 | |
| 320 | elog<InternalFailure>(); |
| 321 | } |
| 322 | |
| 323 | ObjectTree objectTree; |
| 324 | mapperReply.read(objectTree); |
| 325 | |
| 326 | for (auto it = objectTree.begin(); it != objectTree.end();) |
| 327 | { |
| 328 | if (it->first.find(match) == std::string::npos) |
| 329 | { |
| 330 | it = objectTree.erase(it); |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | ++it; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return objectTree; |
| 339 | } |
| 340 | |
| 341 | void deleteAllDbusObjects(sdbusplus::bus::bus& bus, |
| 342 | const std::string& serviceRoot, |
| 343 | const std::string& interface, |
| 344 | const std::string& match) |
| 345 | { |
| 346 | try |
| 347 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 348 | auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 349 | |
| 350 | for (auto& object : objectTree) |
| 351 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 352 | method_no_args::callDbusMethod(bus, object.second.begin()->first, |
| 353 | object.first, DELETE_INTERFACE, |
| 354 | "Delete"); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | catch (InternalFailure& e) |
| 358 | { |
| 359 | log<level::INFO>("Unable to delete the objects having", |
| 360 | entry("INTERFACE=%s", interface.c_str()), |
| 361 | entry("SERVICE=%s", serviceRoot.c_str())); |
| 362 | } |
| 363 | } |
| 364 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 365 | ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path, |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 366 | InterfaceList&& interfaces) |
| 367 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 368 | auto convertToString = [](InterfaceList& interfaces) -> std::string { |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 369 | std::string intfStr; |
| 370 | for (const auto& intf : interfaces) |
| 371 | { |
| 372 | intfStr += "," + intf; |
| 373 | } |
| 374 | return intfStr; |
| 375 | }; |
| 376 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 377 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, |
| 378 | MAPPER_INTF, "GetAncestors"); |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 379 | mapperCall.append(path, interfaces); |
| 380 | |
| 381 | auto mapperReply = bus.call(mapperCall); |
| 382 | if (mapperReply.is_method_error()) |
| 383 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 384 | log<level::ERR>( |
| 385 | "Error in mapper call", entry("PATH=%s", path.c_str()), |
| 386 | entry("INTERFACES=%s", convertToString(interfaces).c_str())); |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 387 | |
| 388 | elog<InternalFailure>(); |
| 389 | } |
| 390 | |
| 391 | ObjectTree objectTree; |
| 392 | mapperReply.read(objectTree); |
| 393 | |
| 394 | if (objectTree.empty()) |
| 395 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 396 | log<level::ERR>( |
| 397 | "No Object has implemented the interface", |
| 398 | entry("PATH=%s", path.c_str()), |
| 399 | entry("INTERFACES=%s", convertToString(interfaces).c_str())); |
Ratan Gupta | cc6cdbf | 2017-09-01 23:06:25 +0530 | [diff] [blame] | 400 | elog<InternalFailure>(); |
| 401 | } |
| 402 | |
| 403 | return objectTree; |
| 404 | } |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 405 | |
| 406 | namespace method_no_args |
| 407 | { |
| 408 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 409 | void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service, |
| 410 | const std::string& objPath, const std::string& interface, |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 411 | const std::string& method) |
| 412 | |
| 413 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 414 | auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 415 | interface.c_str(), method.c_str()); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 416 | |
| 417 | auto reply = bus.call(busMethod); |
| 418 | |
| 419 | if (reply.is_method_error()) |
| 420 | { |
| 421 | log<level::ERR>("Failed to execute method", |
| 422 | entry("METHOD=%s", method.c_str()), |
| 423 | entry("PATH=%s", objPath.c_str()), |
| 424 | entry("INTERFACE=%s", interface.c_str())); |
| 425 | elog<InternalFailure>(); |
| 426 | } |
| 427 | } |
| 428 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 429 | } // namespace method_no_args |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 430 | |
| 431 | namespace network |
| 432 | { |
| 433 | |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 434 | bool isLinkLocalIP(const std::string& address) |
| 435 | { |
| 436 | return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0; |
| 437 | } |
| 438 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 439 | void createIP(sdbusplus::bus::bus& bus, const std::string& service, |
| 440 | const std::string& objPath, const std::string& protocolType, |
| 441 | const std::string& ipaddress, uint8_t prefix) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 442 | { |
| 443 | std::string gateway = ""; |
| 444 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 445 | auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 446 | IP_CREATE_INTERFACE, "IP"); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 447 | |
| 448 | busMethod.append(protocolType, ipaddress, prefix, gateway); |
| 449 | |
| 450 | auto reply = bus.call(busMethod); |
| 451 | |
| 452 | if (reply.is_method_error()) |
| 453 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 454 | log<level::ERR>("Failed to execute method", entry("METHOD=%s", "IP"), |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 455 | entry("PATH=%s", objPath.c_str())); |
| 456 | elog<InternalFailure>(); |
| 457 | } |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 458 | } |
| 459 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 460 | void createVLAN(sdbusplus::bus::bus& bus, const std::string& service, |
| 461 | const std::string& objPath, const std::string& interfaceName, |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 462 | uint32_t vlanID) |
| 463 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 464 | auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 465 | VLAN_CREATE_INTERFACE, "VLAN"); |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 466 | |
| 467 | busMethod.append(interfaceName, vlanID); |
| 468 | |
| 469 | auto reply = bus.call(busMethod); |
| 470 | |
| 471 | if (reply.is_method_error()) |
| 472 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 473 | log<level::ERR>("Failed to execute method", entry("METHOD=%s", "VLAN"), |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 474 | entry("PATH=%s", objPath.c_str())); |
| 475 | elog<InternalFailure>(); |
| 476 | } |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 477 | } |
| 478 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 479 | uint8_t toPrefix(int addressFamily, const std::string& subnetMask) |
| 480 | { |
| 481 | if (addressFamily == AF_INET6) |
| 482 | { |
| 483 | return 0; |
| 484 | } |
| 485 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 486 | uint32_t buff{}; |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 487 | |
| 488 | auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff); |
| 489 | if (rc <= 0) |
| 490 | { |
| 491 | log<level::ERR>("inet_pton failed:", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 492 | entry("SUBNETMASK=%s", subnetMask.c_str())); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | buff = be32toh(buff); |
| 497 | // total no of bits - total no of leading zero == total no of ones |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 498 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == |
| 499 | __builtin_popcount(buff)) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 500 | { |
| 501 | return __builtin_popcount(buff); |
| 502 | } |
| 503 | else |
| 504 | { |
| 505 | log<level::ERR>("Invalid Mask", |
Joseph Reynolds | 510eb9c | 2018-05-30 11:51:28 -0500 | [diff] [blame] | 506 | entry("SUBNETMASK=%s", subnetMask.c_str())); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | } |
| 510 | |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 511 | uint32_t getVLAN(const std::string& path) |
| 512 | { |
| 513 | // Path would be look like |
| 514 | // /xyz/openbmc_project/network/eth0_443/ipv4 |
| 515 | |
| 516 | uint32_t vlanID = 0; |
| 517 | try |
| 518 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 519 | auto intfObjectPath = path.substr(0, path.find(IP_TYPE) - 1); |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 520 | |
| 521 | auto intfName = intfObjectPath.substr(intfObjectPath.rfind("/") + 1); |
| 522 | |
| 523 | auto index = intfName.find("_"); |
| 524 | if (index != std::string::npos) |
| 525 | { |
| 526 | auto str = intfName.substr(index + 1); |
| 527 | vlanID = std::stoul(str); |
| 528 | } |
| 529 | } |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 530 | catch (std::exception& e) |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 531 | { |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 532 | log<level::ERR>("Exception occurred during getVLAN", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 533 | entry("PATH=%s", path.c_str()), |
Gunnar Mills | 5b801e4 | 2017-10-19 17:11:42 -0500 | [diff] [blame] | 534 | entry("EXCEPTION=%s", e.what())); |
Ratan Gupta | 533d03b | 2017-07-30 10:39:22 +0530 | [diff] [blame] | 535 | } |
| 536 | return vlanID; |
| 537 | } |
| 538 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 539 | } // namespace network |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 540 | } // namespace ipmi |