blob: 4aaf9c053bf6e10ab490d326013b4715db9d91dd [file] [log] [blame]
Tom Josephbe703f72017-03-09 12:34:35 +05301#include "utils.hpp"
2
Ratan Guptab8e99552017-07-27 07:07:48 +05303#include <arpa/inet.h>
4#include <dirent.h>
5#include <net/if.h>
6
Patrick Venture2e633522018-10-13 13:51:06 -07007#include <algorithm>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070010#include <sdbusplus/message/types.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070011#include <xyz/openbmc_project/Common/error.hpp>
12
Tom Josephbe703f72017-03-09 12:34:35 +053013namespace ipmi
14{
15
Ratan Guptacc8feb42017-07-25 21:52:10 +053016using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
William A. Kennington III4c008022018-10-12 17:18:14 -070018namespace variant_ns = sdbusplus::message::variant_ns;
Ratan Guptacc8feb42017-07-25 21:52:10 +053019
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -050020namespace network
21{
22
23/** @brief checks if the given ip is Link Local Ip or not.
Patrick Venture0b02be92018-08-31 11:55:55 -070024 * @param[in] ipaddress - IPAddress.
25 */
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -050026bool isLinkLocalIP(const std::string& ipaddress);
27
Patrick Venture0b02be92018-08-31 11:55:55 -070028} // namespace network
Ratan Guptacc8feb42017-07-25 21:52:10 +053029
Patrick Venture0b02be92018-08-31 11:55:55 -070030// TODO There may be cases where an interface is implemented by multiple
Ratan Guptacc8feb42017-07-25 21:52:10 +053031// 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 Gupta01d4bd12017-08-07 15:53:25 +053036DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus,
37 const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +053038 const std::string& serviceRoot,
39 const std::string& match)
40{
41 std::vector<DbusInterface> interfaces;
42 interfaces.emplace_back(interface);
43
Ratan Guptacc8feb42017-07-25 21:52:10 +053044 auto depth = 0;
45
Patrick Venture0b02be92018-08-31 11:55:55 -070046 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
47 MAPPER_INTF, "GetSubTree");
Ratan Guptacc8feb42017-07-25 21:52:10 +053048
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 Venturef0c48782017-09-21 18:48:51 -070063 log<level::ERR>("No Object has implemented the interface",
Ratan Guptacc8feb42017-07-25 21:52:10 +053064 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 Venture0b02be92018-08-31 11:55:55 -070071 if (match == "")
Ratan Guptacc8feb42017-07-25 21:52:10 +053072 {
Patrick Venture0b02be92018-08-31 11:55:55 -070073 objectInfo = std::make_pair(
74 objectTree.begin()->first,
Ratan Guptacc8feb42017-07-25 21:52:10 +053075 std::move(objectTree.begin()->second.begin()->first));
76 return objectInfo;
77 }
78
79 // else search the match string in the object path
Patrick Venture2e633522018-10-13 13:51:06 -070080 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 Guptacc8feb42017-07-25 21:52:10 +053084
Patrick Venture2e633522018-10-13 13:51:06 -070085 if (found == objectTree.end())
Ratan Guptacc8feb42017-07-25 21:52:10 +053086 {
87 log<level::ERR>("Failed to find object which matches",
88 entry("MATCH=%s", match.c_str()));
89 elog<InternalFailure>();
Patrick Venture2e633522018-10-13 13:51:06 -070090 // elog<> throws an exception.
Ratan Guptacc8feb42017-07-25 21:52:10 +053091 }
Patrick Venture2e633522018-10-13 13:51:06 -070092
93 return make_pair(found->first, std::move(found->second.begin()->first));
Ratan Guptacc8feb42017-07-25 21:52:10 +053094}
95
Ratan Guptadd646202017-11-21 17:46:59 +053096DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus,
97 const std::string& interface,
98 const std::string& serviceRoot,
99 const std::string& match)
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500100{
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 Guptadd646202017-11-21 17:46:59 +0530110 DbusObjectInfo objectInfo;
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500111
112 for (auto& object : objectTree)
113 {
114 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700115 bus, object.second.begin()->first, object.first,
116 ipmi::network::IP_INTERFACE, "Address");
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500117
Patrick Venture0b02be92018-08-31 11:55:55 -0700118 objectInfo = std::make_pair(object.first, object.second.begin()->first);
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500119
120 // if LinkLocalIP found look for Non-LinkLocalIP
William A. Kennington III4c008022018-10-12 17:18:14 -0700121 if (ipmi::network::isLinkLocalIP(variant_ns::get<std::string>(variant)))
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500122 {
123 continue;
124 }
125 else
126 {
127 break;
128 }
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500129 }
Ratan Guptadd646202017-11-21 17:46:59 +0530130 return objectInfo;
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500131}
132
Patrick Venture0b02be92018-08-31 11:55:55 -0700133Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
134 const std::string& objPath, const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530135 const std::string& property)
136{
137
138 Value value;
139
Patrick Venture0b02be92018-08-31 11:55:55 -0700140 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
141 PROP_INTF, METHOD_GET);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530142
143 method.append(interface, property);
144
145 auto reply = bus.call(method);
146
147 if (reply.is_method_error())
148 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700149 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 Guptacc8feb42017-07-25 21:52:10 +0530153 elog<InternalFailure>();
154 }
155
156 reply.read(value);
157
158 return value;
159}
160
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530161PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus,
162 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530163 const std::string& objPath,
164 const std::string& interface)
165{
166 PropertyMap properties;
Ratan Guptacc8feb42017-07-25 21:52:10 +0530167
Patrick Venture0b02be92018-08-31 11:55:55 -0700168 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
169 PROP_INTF, METHOD_GET_ALL);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530170
171 method.append(interface);
172
173 auto reply = bus.call(method);
174
175 if (reply.is_method_error())
176 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700177 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 Guptacc8feb42017-07-25 21:52:10 +0530181 }
182
183 reply.read(properties);
184 return properties;
185}
186
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600187ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700188 const std::string& service,
189 const std::string& objPath)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600190{
191 ipmi::ObjectValueTree interfaces;
192
Patrick Venture0b02be92018-08-31 11:55:55 -0700193 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
194 "org.freedesktop.DBus.ObjectManager",
195 "GetManagedObjects");
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600196
197 auto reply = bus.call(method);
198
199 if (reply.is_method_error())
200 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700201 log<level::ERR>("Failed to get managed objects",
202 entry("PATH=%s", objPath.c_str()));
203 elog<InternalFailure>();
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600204 }
205
206 reply.read(interfaces);
207 return interfaces;
208}
209
Patrick Venture0b02be92018-08-31 11:55:55 -0700210void 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 Guptacc8feb42017-07-25 21:52:10 +0530213{
Patrick Venture0b02be92018-08-31 11:55:55 -0700214 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
215 PROP_INTF, METHOD_SET);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530216
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 Venture0b02be92018-08-31 11:55:55 -0700223 entry("PATH=%s", objPath.c_str()),
224 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptacc8feb42017-07-25 21:52:10 +0530225 elog<InternalFailure>();
226 }
Ratan Guptacc8feb42017-07-25 21:52:10 +0530227}
228
Patrick Venture0b02be92018-08-31 11:55:55 -0700229ServiceCache::ServiceCache(const std::string& intf, const std::string& path) :
Vernon Mauery54012502018-11-07 10:17:31 -0800230 intf(intf), path(path), cachedService(std::nullopt),
231 cachedBusName(std::nullopt)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700232{
233}
234
Patrick Venture0b02be92018-08-31 11:55:55 -0700235ServiceCache::ServiceCache(std::string&& intf, std::string&& path) :
Vernon Mauery54012502018-11-07 10:17:31 -0800236 intf(std::move(intf)), path(std::move(path)), cachedService(std::nullopt),
237 cachedBusName(std::nullopt)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700238{
239}
240
241const std::string& ServiceCache::getService(sdbusplus::bus::bus& bus)
242{
243 if (!isValid(bus))
244 {
245 cachedBusName = bus.get_unique_name();
246 cachedService = ::ipmi::getService(bus, intf, path);
247 }
248 return cachedService.value();
249}
250
251void ServiceCache::invalidate()
252{
Vernon Mauery54012502018-11-07 10:17:31 -0800253 cachedBusName = std::nullopt;
254 cachedService = std::nullopt;
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700255}
256
Patrick Venture0b02be92018-08-31 11:55:55 -0700257sdbusplus::message::message
258 ServiceCache::newMethodCall(sdbusplus::bus::bus& bus, const char* intf,
259 const char* method)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700260{
Patrick Venture0b02be92018-08-31 11:55:55 -0700261 return bus.new_method_call(getService(bus).c_str(), path.c_str(), intf,
262 method);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700263}
264
265bool ServiceCache::isValid(sdbusplus::bus::bus& bus) const
266{
267 return cachedService && cachedBusName == bus.get_unique_name();
268}
269
Patrick Venture0b02be92018-08-31 11:55:55 -0700270std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Tom Josephbe703f72017-03-09 12:34:35 +0530271 const std::string& path)
272{
Patrick Venture0b02be92018-08-31 11:55:55 -0700273 auto mapperCall =
274 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
275 "/xyz/openbmc_project/object_mapper",
276 "xyz.openbmc_project.ObjectMapper", "GetObject");
Tom Josephbe703f72017-03-09 12:34:35 +0530277
278 mapperCall.append(path);
279 mapperCall.append(std::vector<std::string>({intf}));
280
281 auto mapperResponseMsg = bus.call(mapperCall);
282
283 if (mapperResponseMsg.is_method_error())
284 {
285 throw std::runtime_error("ERROR in mapper call");
286 }
287
288 std::map<std::string, std::vector<std::string>> mapperResponse;
289 mapperResponseMsg.read(mapperResponse);
290
291 if (mapperResponse.begin() == mapperResponse.end())
292 {
293 throw std::runtime_error("ERROR in reading the mapper response");
294 }
295
296 return mapperResponse.begin()->first;
297}
298
Ratan Guptab8e99552017-07-27 07:07:48 +0530299ipmi::ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus,
300 const std::string& serviceRoot,
301 const std::string& interface,
302 const std::string& match)
303{
304 std::vector<std::string> interfaces;
305 interfaces.emplace_back(interface);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530306
Ratan Guptab8e99552017-07-27 07:07:48 +0530307 auto depth = 0;
308
Patrick Venture0b02be92018-08-31 11:55:55 -0700309 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
310 MAPPER_INTF, "GetSubTree");
Ratan Guptab8e99552017-07-27 07:07:48 +0530311
312 mapperCall.append(serviceRoot, depth, interfaces);
313
314 auto mapperReply = bus.call(mapperCall);
315 if (mapperReply.is_method_error())
316 {
317 log<level::ERR>("Error in mapper call",
Patrick Venture0b02be92018-08-31 11:55:55 -0700318 entry("SERVICEROOT=%s", serviceRoot.c_str()),
Ratan Guptab8e99552017-07-27 07:07:48 +0530319 entry("INTERFACE=%s", interface.c_str()));
320
321 elog<InternalFailure>();
322 }
323
324 ObjectTree objectTree;
325 mapperReply.read(objectTree);
326
327 for (auto it = objectTree.begin(); it != objectTree.end();)
328 {
329 if (it->first.find(match) == std::string::npos)
330 {
331 it = objectTree.erase(it);
332 }
333 else
334 {
335 ++it;
336 }
337 }
338
339 return objectTree;
340}
341
342void deleteAllDbusObjects(sdbusplus::bus::bus& bus,
343 const std::string& serviceRoot,
344 const std::string& interface,
345 const std::string& match)
346{
347 try
348 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700349 auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match);
Ratan Guptab8e99552017-07-27 07:07:48 +0530350
351 for (auto& object : objectTree)
352 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700353 method_no_args::callDbusMethod(bus, object.second.begin()->first,
354 object.first, DELETE_INTERFACE,
355 "Delete");
Ratan Guptab8e99552017-07-27 07:07:48 +0530356 }
357 }
358 catch (InternalFailure& e)
359 {
360 log<level::INFO>("Unable to delete the objects having",
361 entry("INTERFACE=%s", interface.c_str()),
362 entry("SERVICE=%s", serviceRoot.c_str()));
363 }
364}
365
Patrick Venture0b02be92018-08-31 11:55:55 -0700366ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path,
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530367 InterfaceList&& interfaces)
368{
Patrick Venture0b02be92018-08-31 11:55:55 -0700369 auto convertToString = [](InterfaceList& interfaces) -> std::string {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530370 std::string intfStr;
371 for (const auto& intf : interfaces)
372 {
373 intfStr += "," + intf;
374 }
375 return intfStr;
376 };
377
Patrick Venture0b02be92018-08-31 11:55:55 -0700378 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
379 MAPPER_INTF, "GetAncestors");
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530380 mapperCall.append(path, interfaces);
381
382 auto mapperReply = bus.call(mapperCall);
383 if (mapperReply.is_method_error())
384 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700385 log<level::ERR>(
386 "Error in mapper call", entry("PATH=%s", path.c_str()),
387 entry("INTERFACES=%s", convertToString(interfaces).c_str()));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530388
389 elog<InternalFailure>();
390 }
391
392 ObjectTree objectTree;
393 mapperReply.read(objectTree);
394
395 if (objectTree.empty())
396 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700397 log<level::ERR>(
398 "No Object has implemented the interface",
399 entry("PATH=%s", path.c_str()),
400 entry("INTERFACES=%s", convertToString(interfaces).c_str()));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530401 elog<InternalFailure>();
402 }
403
404 return objectTree;
405}
Ratan Guptab8e99552017-07-27 07:07:48 +0530406
407namespace method_no_args
408{
409
Patrick Venture0b02be92018-08-31 11:55:55 -0700410void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service,
411 const std::string& objPath, const std::string& interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530412 const std::string& method)
413
414{
Patrick Venture0b02be92018-08-31 11:55:55 -0700415 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
416 interface.c_str(), method.c_str());
Ratan Guptab8e99552017-07-27 07:07:48 +0530417
418 auto reply = bus.call(busMethod);
419
420 if (reply.is_method_error())
421 {
422 log<level::ERR>("Failed to execute method",
423 entry("METHOD=%s", method.c_str()),
424 entry("PATH=%s", objPath.c_str()),
425 entry("INTERFACE=%s", interface.c_str()));
426 elog<InternalFailure>();
427 }
428}
429
Patrick Venture0b02be92018-08-31 11:55:55 -0700430} // namespace method_no_args
Ratan Guptab8e99552017-07-27 07:07:48 +0530431
432namespace network
433{
434
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500435bool isLinkLocalIP(const std::string& address)
436{
437 return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0;
438}
439
Patrick Venture0b02be92018-08-31 11:55:55 -0700440void createIP(sdbusplus::bus::bus& bus, const std::string& service,
441 const std::string& objPath, const std::string& protocolType,
442 const std::string& ipaddress, uint8_t prefix)
Ratan Guptab8e99552017-07-27 07:07:48 +0530443{
444 std::string gateway = "";
445
Patrick Venture0b02be92018-08-31 11:55:55 -0700446 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
447 IP_CREATE_INTERFACE, "IP");
Ratan Guptab8e99552017-07-27 07:07:48 +0530448
449 busMethod.append(protocolType, ipaddress, prefix, gateway);
450
451 auto reply = bus.call(busMethod);
452
453 if (reply.is_method_error())
454 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700455 log<level::ERR>("Failed to execute method", entry("METHOD=%s", "IP"),
Ratan Guptab8e99552017-07-27 07:07:48 +0530456 entry("PATH=%s", objPath.c_str()));
457 elog<InternalFailure>();
458 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530459}
460
Patrick Venture0b02be92018-08-31 11:55:55 -0700461void createVLAN(sdbusplus::bus::bus& bus, const std::string& service,
462 const std::string& objPath, const std::string& interfaceName,
Ratan Gupta533d03b2017-07-30 10:39:22 +0530463 uint32_t vlanID)
464{
Patrick Venture0b02be92018-08-31 11:55:55 -0700465 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
466 VLAN_CREATE_INTERFACE, "VLAN");
Ratan Gupta533d03b2017-07-30 10:39:22 +0530467
468 busMethod.append(interfaceName, vlanID);
469
470 auto reply = bus.call(busMethod);
471
472 if (reply.is_method_error())
473 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700474 log<level::ERR>("Failed to execute method", entry("METHOD=%s", "VLAN"),
Ratan Gupta533d03b2017-07-30 10:39:22 +0530475 entry("PATH=%s", objPath.c_str()));
476 elog<InternalFailure>();
477 }
Ratan Gupta533d03b2017-07-30 10:39:22 +0530478}
479
Ratan Guptab8e99552017-07-27 07:07:48 +0530480uint8_t toPrefix(int addressFamily, const std::string& subnetMask)
481{
482 if (addressFamily == AF_INET6)
483 {
484 return 0;
485 }
486
Patrick Venture0b02be92018-08-31 11:55:55 -0700487 uint32_t buff{};
Ratan Guptab8e99552017-07-27 07:07:48 +0530488
489 auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff);
490 if (rc <= 0)
491 {
492 log<level::ERR>("inet_pton failed:",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -0500493 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Guptab8e99552017-07-27 07:07:48 +0530494 return 0;
495 }
496
497 buff = be32toh(buff);
498 // total no of bits - total no of leading zero == total no of ones
Patrick Venture0b02be92018-08-31 11:55:55 -0700499 if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) ==
500 __builtin_popcount(buff))
Ratan Guptab8e99552017-07-27 07:07:48 +0530501 {
502 return __builtin_popcount(buff);
503 }
504 else
505 {
506 log<level::ERR>("Invalid Mask",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -0500507 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Guptab8e99552017-07-27 07:07:48 +0530508 return 0;
509 }
510}
511
Ratan Gupta533d03b2017-07-30 10:39:22 +0530512uint32_t getVLAN(const std::string& path)
513{
514 // Path would be look like
515 // /xyz/openbmc_project/network/eth0_443/ipv4
516
517 uint32_t vlanID = 0;
518 try
519 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700520 auto intfObjectPath = path.substr(0, path.find(IP_TYPE) - 1);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530521
522 auto intfName = intfObjectPath.substr(intfObjectPath.rfind("/") + 1);
523
524 auto index = intfName.find("_");
525 if (index != std::string::npos)
526 {
527 auto str = intfName.substr(index + 1);
528 vlanID = std::stoul(str);
529 }
530 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700531 catch (std::exception& e)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530532 {
Gunnar Mills8991dd62017-10-25 17:11:29 -0500533 log<level::ERR>("Exception occurred during getVLAN",
Patrick Venture0b02be92018-08-31 11:55:55 -0700534 entry("PATH=%s", path.c_str()),
Gunnar Mills5b801e42017-10-19 17:11:42 -0500535 entry("EXCEPTION=%s", e.what()));
Ratan Gupta533d03b2017-07-30 10:39:22 +0530536 }
537 return vlanID;
538}
539
Ratan Guptab8e99552017-07-27 07:07:48 +0530540} // namespace network
Tom Josephbe703f72017-03-09 12:34:35 +0530541} // namespace ipmi