Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 1 | #include <arpa/inet.h> |
| 2 | #include <dirent.h> |
| 3 | #include <net/if.h> |
| 4 | |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 5 | #include <algorithm> |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 6 | #include <chrono> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 7 | #include <ipmid/utils.hpp> |
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; |
| 18 | |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 19 | namespace network |
| 20 | { |
| 21 | |
| 22 | /** @brief checks if the given ip is Link Local Ip or not. |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 23 | * @param[in] ipaddress - IPAddress. |
| 24 | */ |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 25 | bool isLinkLocalIP(const std::string& ipaddress); |
| 26 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 27 | } // namespace network |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 28 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 29 | // TODO There may be cases where an interface is implemented by multiple |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 30 | // objects,to handle such cases we are interested on that object |
| 31 | // which are on interested busname. |
| 32 | // Currently mapper doesn't give the readable busname(gives busid) so we can't |
| 33 | // use busname to find the object,will do later once the support is there. |
| 34 | |
Ratan Gupta | 01d4bd1 | 2017-08-07 15:53:25 +0530 | [diff] [blame] | 35 | DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus, |
| 36 | const std::string& interface, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 37 | const std::string& serviceRoot, |
| 38 | const std::string& match) |
| 39 | { |
| 40 | std::vector<DbusInterface> interfaces; |
| 41 | interfaces.emplace_back(interface); |
| 42 | |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 43 | auto depth = 0; |
| 44 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 45 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, |
| 46 | MAPPER_INTF, "GetSubTree"); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 47 | |
| 48 | mapperCall.append(serviceRoot, depth, interfaces); |
| 49 | |
| 50 | auto mapperReply = bus.call(mapperCall); |
| 51 | if (mapperReply.is_method_error()) |
| 52 | { |
| 53 | log<level::ERR>("Error in mapper call"); |
| 54 | elog<InternalFailure>(); |
| 55 | } |
| 56 | |
| 57 | ObjectTree objectTree; |
| 58 | mapperReply.read(objectTree); |
| 59 | |
| 60 | if (objectTree.empty()) |
| 61 | { |
Patrick Venture | f0c4878 | 2017-09-21 18:48:51 -0700 | [diff] [blame] | 62 | log<level::ERR>("No Object has implemented the interface", |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 63 | entry("INTERFACE=%s", interface.c_str())); |
| 64 | elog<InternalFailure>(); |
| 65 | } |
| 66 | |
| 67 | DbusObjectInfo objectInfo; |
| 68 | |
| 69 | // if match is empty then return the first object |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 70 | if (match == "") |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 71 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 72 | objectInfo = std::make_pair( |
| 73 | objectTree.begin()->first, |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 74 | std::move(objectTree.begin()->second.begin()->first)); |
| 75 | return objectInfo; |
| 76 | } |
| 77 | |
| 78 | // else search the match string in the object path |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 79 | auto found = std::find_if( |
| 80 | objectTree.begin(), objectTree.end(), [&match](const auto& object) { |
| 81 | return (object.first.find(match) != std::string::npos); |
| 82 | }); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 83 | |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 84 | if (found == objectTree.end()) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 85 | { |
| 86 | log<level::ERR>("Failed to find object which matches", |
| 87 | entry("MATCH=%s", match.c_str())); |
| 88 | elog<InternalFailure>(); |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 89 | // elog<> throws an exception. |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 90 | } |
Patrick Venture | 2e63352 | 2018-10-13 13:51:06 -0700 | [diff] [blame] | 91 | |
| 92 | return make_pair(found->first, std::move(found->second.begin()->first)); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 93 | } |
| 94 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 95 | DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus, |
| 96 | const std::string& interface, |
| 97 | const std::string& serviceRoot, |
| 98 | const std::string& match) |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 99 | { |
| 100 | auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match); |
| 101 | |
| 102 | if (objectTree.empty()) |
| 103 | { |
| 104 | log<level::ERR>("No Object has implemented the IP interface", |
| 105 | entry("INTERFACE=%s", interface.c_str())); |
| 106 | elog<InternalFailure>(); |
| 107 | } |
| 108 | |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 109 | DbusObjectInfo objectInfo; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 110 | |
| 111 | for (auto& object : objectTree) |
| 112 | { |
| 113 | auto variant = ipmi::getDbusProperty( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 114 | bus, object.second.begin()->first, object.first, |
| 115 | ipmi::network::IP_INTERFACE, "Address"); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 116 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 117 | objectInfo = std::make_pair(object.first, object.second.begin()->first); |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 118 | |
| 119 | // if LinkLocalIP found look for Non-LinkLocalIP |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 120 | if (ipmi::network::isLinkLocalIP(std::get<std::string>(variant))) |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 121 | { |
| 122 | continue; |
| 123 | } |
| 124 | else |
| 125 | { |
| 126 | break; |
| 127 | } |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 128 | } |
Ratan Gupta | dd64620 | 2017-11-21 17:46:59 +0530 | [diff] [blame] | 129 | return objectInfo; |
Nagaraju Goruganti | 1fe5c83 | 2017-09-21 07:44:17 -0500 | [diff] [blame] | 130 | } |
| 131 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 132 | Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 133 | const std::string& objPath, const std::string& interface, |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 134 | const std::string& property, |
| 135 | std::chrono::microseconds timeout) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 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 | |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 145 | auto reply = bus.call(method, timeout.count()); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 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, |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 164 | const std::string& interface, |
| 165 | std::chrono::microseconds timeout) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 166 | { |
| 167 | PropertyMap properties; |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 168 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 169 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 170 | PROP_INTF, METHOD_GET_ALL); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 171 | |
| 172 | method.append(interface); |
| 173 | |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 174 | auto reply = bus.call(method, timeout.count()); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 175 | |
| 176 | if (reply.is_method_error()) |
| 177 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 178 | log<level::ERR>("Failed to get all properties", |
| 179 | entry("PATH=%s", objPath.c_str()), |
| 180 | entry("INTERFACE=%s", interface.c_str())); |
| 181 | elog<InternalFailure>(); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | reply.read(properties); |
| 185 | return properties; |
| 186 | } |
| 187 | |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 188 | ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 189 | const std::string& service, |
| 190 | const std::string& objPath) |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 191 | { |
| 192 | ipmi::ObjectValueTree interfaces; |
| 193 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 194 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), |
| 195 | "org.freedesktop.DBus.ObjectManager", |
| 196 | "GetManagedObjects"); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 197 | |
| 198 | auto reply = bus.call(method); |
| 199 | |
| 200 | if (reply.is_method_error()) |
| 201 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 202 | log<level::ERR>("Failed to get managed objects", |
| 203 | entry("PATH=%s", objPath.c_str())); |
| 204 | elog<InternalFailure>(); |
Dhruvaraj Subhashchandran | 5c0beec | 2018-01-23 04:47:06 -0600 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | reply.read(interfaces); |
| 208 | return interfaces; |
| 209 | } |
| 210 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 211 | void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 212 | const std::string& objPath, const std::string& interface, |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 213 | const std::string& property, const Value& value, |
| 214 | std::chrono::microseconds timeout) |
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 | |
Kun Yi | 5dcf41e | 2019-03-05 14:02:51 -0800 | [diff] [blame] | 221 | if (!bus.call(method, timeout.count())) |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 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) : |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 232 | intf(intf), path(path), cachedService(std::nullopt), |
| 233 | cachedBusName(std::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) : |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 238 | intf(std::move(intf)), path(std::move(path)), cachedService(std::nullopt), |
| 239 | cachedBusName(std::nullopt) |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 240 | { |
| 241 | } |
| 242 | |
| 243 | const std::string& ServiceCache::getService(sdbusplus::bus::bus& bus) |
| 244 | { |
| 245 | if (!isValid(bus)) |
| 246 | { |
| 247 | cachedBusName = bus.get_unique_name(); |
| 248 | cachedService = ::ipmi::getService(bus, intf, path); |
| 249 | } |
| 250 | return cachedService.value(); |
| 251 | } |
| 252 | |
| 253 | void ServiceCache::invalidate() |
| 254 | { |
Vernon Mauery | 5401250 | 2018-11-07 10:17:31 -0800 | [diff] [blame] | 255 | cachedBusName = std::nullopt; |
| 256 | cachedService = std::nullopt; |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 257 | } |
| 258 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 259 | sdbusplus::message::message |
| 260 | ServiceCache::newMethodCall(sdbusplus::bus::bus& bus, const char* intf, |
| 261 | const char* method) |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 262 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 263 | return bus.new_method_call(getService(bus).c_str(), path.c_str(), intf, |
| 264 | method); |
William A. Kennington III | e47fdfb | 2018-03-15 17:09:28 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | bool ServiceCache::isValid(sdbusplus::bus::bus& bus) const |
| 268 | { |
| 269 | return cachedService && cachedBusName == bus.get_unique_name(); |
| 270 | } |
| 271 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 272 | std::string getService(sdbusplus::bus::bus& bus, const std::string& intf, |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 273 | const std::string& path) |
| 274 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 275 | auto mapperCall = |
| 276 | bus.new_method_call("xyz.openbmc_project.ObjectMapper", |
| 277 | "/xyz/openbmc_project/object_mapper", |
| 278 | "xyz.openbmc_project.ObjectMapper", "GetObject"); |
Tom Joseph | be703f7 | 2017-03-09 12:34:35 +0530 | [diff] [blame] | 279 | |
| 280 | mapperCall.append(path); |
| 281 | mapperCall.append(std::vector<std::string>({intf})); |
| 282 | |
| 283 | auto mapperResponseMsg = bus.call(mapperCall); |
| 284 | |
| 285 | if (mapperResponseMsg.is_method_error()) |
| 286 | { |
| 287 | throw std::runtime_error("ERROR in mapper call"); |
| 288 | } |
| 289 | |
| 290 | std::map<std::string, std::vector<std::string>> mapperResponse; |
| 291 | mapperResponseMsg.read(mapperResponse); |
| 292 | |
| 293 | if (mapperResponse.begin() == mapperResponse.end()) |
| 294 | { |
| 295 | throw std::runtime_error("ERROR in reading the mapper response"); |
| 296 | } |
| 297 | |
| 298 | return mapperResponse.begin()->first; |
| 299 | } |
| 300 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 301 | ipmi::ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus, |
| 302 | const std::string& serviceRoot, |
| 303 | const std::string& interface, |
| 304 | const std::string& match) |
| 305 | { |
| 306 | std::vector<std::string> interfaces; |
| 307 | interfaces.emplace_back(interface); |
Ratan Gupta | cc8feb4 | 2017-07-25 21:52:10 +0530 | [diff] [blame] | 308 | |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 309 | auto depth = 0; |
| 310 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 311 | auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ, |
| 312 | MAPPER_INTF, "GetSubTree"); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 313 | |
| 314 | mapperCall.append(serviceRoot, depth, interfaces); |
| 315 | |
| 316 | auto mapperReply = bus.call(mapperCall); |
| 317 | if (mapperReply.is_method_error()) |
| 318 | { |
| 319 | log<level::ERR>("Error in mapper call", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 320 | entry("SERVICEROOT=%s", serviceRoot.c_str()), |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 321 | entry("INTERFACE=%s", interface.c_str())); |
| 322 | |
| 323 | elog<InternalFailure>(); |
| 324 | } |
| 325 | |
| 326 | ObjectTree objectTree; |
| 327 | mapperReply.read(objectTree); |
| 328 | |
| 329 | for (auto it = objectTree.begin(); it != objectTree.end();) |
| 330 | { |
| 331 | if (it->first.find(match) == std::string::npos) |
| 332 | { |
| 333 | it = objectTree.erase(it); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | ++it; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | return objectTree; |
| 342 | } |
| 343 | |
| 344 | void deleteAllDbusObjects(sdbusplus::bus::bus& bus, |
| 345 | const std::string& serviceRoot, |
| 346 | const std::string& interface, |
| 347 | const std::string& match) |
| 348 | { |
| 349 | try |
| 350 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 351 | auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 352 | |
| 353 | for (auto& object : objectTree) |
| 354 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 355 | method_no_args::callDbusMethod(bus, object.second.begin()->first, |
| 356 | object.first, DELETE_INTERFACE, |
| 357 | "Delete"); |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 358 | } |
| 359 | } |
Vernon Mauery | 7a61418 | 2018-11-27 12:54:52 -0800 | [diff] [blame] | 360 | catch (sdbusplus::exception::exception& e) |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 361 | { |
Vernon Mauery | 7a61418 | 2018-11-27 12:54:52 -0800 | [diff] [blame] | 362 | log<level::INFO>("sdbusplus exception - Unable to delete the objects", |
| 363 | entry("ERROR=%s", e.what()), |
Ratan Gupta | b8e9955 | 2017-07-27 07:07:48 +0530 | [diff] [blame] | 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 |