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