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