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