blob: 7935ba6f31f348b8c699aed607cd6c424c017cc8 [file] [log] [blame]
William A. Kennington III324d2602022-08-18 18:32:56 -07001#include "config.h"
2
Ratan Gupta3681a502017-06-17 19:20:04 +05303#include "util.hpp"
Ratan Gupta11cef802017-05-29 08:41:48 +05304
Patrick Venture189d44e2018-07-09 12:30:59 -07005#include "config_parser.hpp"
6#include "types.hpp"
Ratan Gupta8804feb2017-05-25 10:49:57 +05307
Ratan Gupta3681a502017-06-17 19:20:04 +05308#include <arpa/inet.h>
9#include <dirent.h>
William A. Kennington III1c776022022-01-05 14:12:16 -080010#include <fmt/compile.h>
11#include <fmt/format.h>
Ratan Gupta3681a502017-06-17 19:20:04 +053012#include <net/if.h>
Ratan Guptabc886292017-07-25 18:29:57 +053013#include <sys/wait.h>
Ratan Gupta3681a502017-06-17 19:20:04 +053014
Ratan Gupta8804feb2017-05-25 10:49:57 +053015#include <algorithm>
Lei YU307554e2021-03-18 14:56:50 +080016#include <cctype>
William A. Kennington III69f45542022-09-24 23:28:14 -070017#include <charconv>
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -070018#include <cstdlib>
19#include <cstring>
Manojkiran Edacc099a82020-05-11 14:25:16 +053020#include <fstream>
Patrick Venture189d44e2018-07-09 12:30:59 -070021#include <list>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070022#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +053023#include <nlohmann/json.hpp>
William A. Kennington IIIde433b72021-05-17 22:59:28 -070024#endif
Patrick Venture189d44e2018-07-09 12:30:59 -070025#include <phosphor-logging/elog-errors.hpp>
26#include <phosphor-logging/log.hpp>
William A. Kennington III5058f572019-01-30 17:18:14 -080027#include <stdexcept>
William A. Kennington III12beaad2020-06-13 19:30:41 -070028#include <stdplus/raw.hpp>
Patrick Venture189d44e2018-07-09 12:30:59 -070029#include <string>
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -070030#include <string_view>
William A. Kennington III1137a972019-04-20 20:49:58 -070031#include <variant>
Patrick Venture189d44e2018-07-09 12:30:59 -070032#include <xyz/openbmc_project/Common/error.hpp>
Ratan Gupta8804feb2017-05-25 10:49:57 +053033
34namespace phosphor
35{
36namespace network
37{
Ratan Guptabc886292017-07-25 18:29:57 +053038
William A. Kennington III69f45542022-09-24 23:28:14 -070039using std::literals::string_view_literals::operator""sv;
Ratan Gupta8804feb2017-05-25 10:49:57 +053040using namespace phosphor::logging;
Ratan Gupta11cef802017-05-29 08:41:48 +053041using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Ratan Gupta8804feb2017-05-25 10:49:57 +053042
Lei YU3894ce72021-03-18 14:53:42 +080043namespace internal
44{
45
William A. Kennington III69f45542022-09-24 23:28:14 -070046void executeCommandinChildProcess(stdplus::const_zstring path, char** args)
Lei YU3894ce72021-03-18 14:53:42 +080047{
48 using namespace std::string_literals;
49 pid_t pid = fork();
Lei YU3894ce72021-03-18 14:53:42 +080050
51 if (pid == 0)
52 {
William A. Kennington III69f45542022-09-24 23:28:14 -070053 execv(path.c_str(), args);
54 exit(255);
Lei YU3894ce72021-03-18 14:53:42 +080055 }
56 else if (pid < 0)
57 {
58 auto error = errno;
59 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
60 elog<InternalFailure>();
61 }
62 else if (pid > 0)
63 {
William A. Kennington III69f45542022-09-24 23:28:14 -070064 int status;
Lei YU3894ce72021-03-18 14:53:42 +080065 while (waitpid(pid, &status, 0) == -1)
66 {
67 if (errno != EINTR)
William A. Kennington III69f45542022-09-24 23:28:14 -070068 {
Lei YU3894ce72021-03-18 14:53:42 +080069 status = -1;
70 break;
71 }
72 }
73
74 if (status < 0)
75 {
William A. Kennington III69f45542022-09-24 23:28:14 -070076 fmt::memory_buffer buf;
77 fmt::format_to(fmt::appender(buf), "`{}`", path);
78 for (size_t i = 0; args[i] != nullptr; ++i)
Lei YU3894ce72021-03-18 14:53:42 +080079 {
William A. Kennington III69f45542022-09-24 23:28:14 -070080 fmt::format_to(fmt::appender(buf), " `{}`", args[i]);
Lei YU3894ce72021-03-18 14:53:42 +080081 }
William A. Kennington III69f45542022-09-24 23:28:14 -070082 buf.push_back('\0');
Lei YU3894ce72021-03-18 14:53:42 +080083 log<level::ERR>("Unable to execute the command",
William A. Kennington III69f45542022-09-24 23:28:14 -070084 entry("CMD=%s", buf.data()),
Lei YU3894ce72021-03-18 14:53:42 +080085 entry("STATUS=%d", status));
86 elog<InternalFailure>();
87 }
88 }
89}
90
Lei YU307554e2021-03-18 14:56:50 +080091/** @brief Get ignored interfaces from environment */
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070092std::string_view getIgnoredInterfacesEnv()
Lei YU307554e2021-03-18 14:56:50 +080093{
94 auto r = std::getenv("IGNORED_INTERFACES");
95 if (r == nullptr)
96 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -070097 return "";
Lei YU307554e2021-03-18 14:56:50 +080098 }
99 return r;
100}
101
102/** @brief Parse the comma separated interface names */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700103std::unordered_set<std::string_view>
104 parseInterfaces(std::string_view interfaces)
Lei YU307554e2021-03-18 14:56:50 +0800105{
William A. Kennington III95530ec2022-08-19 01:44:39 -0700106 std::unordered_set<std::string_view> result;
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700107 while (true)
Lei YU307554e2021-03-18 14:56:50 +0800108 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700109 auto sep = interfaces.find(',');
110 auto interface = interfaces.substr(0, sep);
111 while (!interface.empty() && std::isspace(interface.front()))
Lei YU307554e2021-03-18 14:56:50 +0800112 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700113 interface.remove_prefix(1);
Lei YU307554e2021-03-18 14:56:50 +0800114 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700115 while (!interface.empty() && std::isspace(interface.back()))
Lei YU307554e2021-03-18 14:56:50 +0800116 {
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700117 interface.remove_suffix(1);
Lei YU307554e2021-03-18 14:56:50 +0800118 }
William A. Kennington IIIee5b2c92021-04-28 02:31:28 -0700119 if (!interface.empty())
120 {
121 result.insert(interface);
122 }
123 if (sep == interfaces.npos)
124 {
125 break;
126 }
127 interfaces = interfaces.substr(sep + 1);
Lei YU307554e2021-03-18 14:56:50 +0800128 }
129 return result;
130}
131
132/** @brief Get the ignored interfaces */
William A. Kennington III95530ec2022-08-19 01:44:39 -0700133const std::unordered_set<std::string_view>& getIgnoredInterfaces()
Lei YU307554e2021-03-18 14:56:50 +0800134{
135 static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv());
136 return ignoredInterfaces;
137}
138
Lei YU3894ce72021-03-18 14:53:42 +0800139} // namespace internal
Ratan Gupta8804feb2017-05-25 10:49:57 +0530140
Ratan Gupta8804feb2017-05-25 10:49:57 +0530141std::string toMask(int addressFamily, uint8_t prefix)
142{
143 if (addressFamily == AF_INET6)
144 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500145 // TODO:- conversion for v6
Ratan Gupta8804feb2017-05-25 10:49:57 +0530146 return "";
147 }
148
149 if (prefix < 1 || prefix > 30)
150 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500151 log<level::ERR>("Invalid Prefix", entry("PREFIX=%d", prefix));
Ratan Gupta8804feb2017-05-25 10:49:57 +0530152 return "";
153 }
154 /* Create the netmask from the number of bits */
155 unsigned long mask = 0;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500156 for (auto i = 0; i < prefix; i++)
Ratan Gupta8804feb2017-05-25 10:49:57 +0530157 {
158 mask |= 1 << (31 - i);
159 }
160 struct in_addr netmask;
161 netmask.s_addr = htonl(mask);
162 return inet_ntoa(netmask);
163}
164
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800165InAddrAny addrFromBuf(int addressFamily, std::string_view buf)
166{
167 if (addressFamily == AF_INET)
168 {
169 struct in_addr ret;
170 if (buf.size() != sizeof(ret))
171 {
172 throw std::runtime_error("Buf not in_addr sized");
173 }
174 memcpy(&ret, buf.data(), sizeof(ret));
175 return ret;
176 }
177 else if (addressFamily == AF_INET6)
178 {
179 struct in6_addr ret;
180 if (buf.size() != sizeof(ret))
181 {
182 throw std::runtime_error("Buf not in6_addr sized");
183 }
184 memcpy(&ret, buf.data(), sizeof(ret));
185 return ret;
186 }
187
188 throw std::runtime_error("Unsupported address family");
189}
190
Alexander Filippov983da552021-02-08 15:26:54 +0300191std::string toString(const struct in_addr& addr)
192{
193 std::string ip(INET_ADDRSTRLEN, '\0');
194 if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr)
195 {
196 throw std::runtime_error("Failed to convert IP4 to string");
197 }
198
199 ip.resize(strlen(ip.c_str()));
200 return ip;
201}
202
203std::string toString(const struct in6_addr& addr)
204{
205 std::string ip(INET6_ADDRSTRLEN, '\0');
206 if (inet_ntop(AF_INET6, &addr, ip.data(), ip.size()) == nullptr)
207 {
208 throw std::runtime_error("Failed to convert IP6 to string");
209 }
210
211 ip.resize(strlen(ip.c_str()));
212 return ip;
213}
214
William A. Kennington III5058f572019-01-30 17:18:14 -0800215std::string toString(const InAddrAny& addr)
216{
William A. Kennington III5058f572019-01-30 17:18:14 -0800217 if (std::holds_alternative<struct in_addr>(addr))
218 {
219 const auto& v = std::get<struct in_addr>(addr);
Alexander Filippov983da552021-02-08 15:26:54 +0300220 return toString(v);
William A. Kennington III5058f572019-01-30 17:18:14 -0800221 }
222 else if (std::holds_alternative<struct in6_addr>(addr))
223 {
224 const auto& v = std::get<struct in6_addr>(addr);
Alexander Filippov983da552021-02-08 15:26:54 +0300225 return toString(v);
William A. Kennington III5058f572019-01-30 17:18:14 -0800226 }
Alexander Filippov983da552021-02-08 15:26:54 +0300227
228 throw std::runtime_error("Invalid addr type");
William A. Kennington III5058f572019-01-30 17:18:14 -0800229}
230
William A. Kennington III69f45542022-09-24 23:28:14 -0700231bool isValidIP(int addressFamily, stdplus::const_zstring address)
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500232{
233 unsigned char buf[sizeof(struct in6_addr)];
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500234 return inet_pton(addressFamily, address.c_str(), buf) > 0;
235}
236
237bool isValidPrefix(int addressFamily, uint8_t prefixLength)
238{
239 if (addressFamily == AF_INET)
240 {
241 if (prefixLength < IPV4_MIN_PREFIX_LENGTH ||
242 prefixLength > IPV4_MAX_PREFIX_LENGTH)
243 {
244 return false;
245 }
246 }
247
248 if (addressFamily == AF_INET6)
249 {
250 if (prefixLength < IPV4_MIN_PREFIX_LENGTH ||
251 prefixLength > IPV6_MAX_PREFIX_LENGTH)
252 {
253 return false;
254 }
255 }
256
257 return true;
Ratan Gupta8804feb2017-05-25 10:49:57 +0530258}
259
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530260InterfaceList getInterfaces()
261{
Gunnar Mills57d9c502018-09-14 14:42:34 -0500262 InterfaceList interfaces{};
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530263 struct ifaddrs* ifaddr = nullptr;
264
265 // attempt to fill struct with ifaddrs
266 if (getifaddrs(&ifaddr) == -1)
267 {
268 auto error = errno;
269 log<level::ERR>("Error occurred during the getifaddrs call",
270 entry("ERRNO=%d", error));
271 elog<InternalFailure>();
272 }
273
274 AddrPtr ifaddrPtr(ifaddr);
275 ifaddr = nullptr;
Lei YUefda98b2021-03-18 15:52:19 +0800276 const auto& ignoredInterfaces = internal::getIgnoredInterfaces();
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530277
278 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
279 {
280 // walk interfaces
William A. Kennington IIIf273d2b2019-03-21 14:38:36 -0700281 // if loopback ignore
Lei YUefda98b2021-03-18 15:52:19 +0800282 if (ifa->ifa_flags & IFF_LOOPBACK ||
283 ignoredInterfaces.find(ifa->ifa_name) != ignoredInterfaces.end())
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530284 {
285 continue;
286 }
Willy Tuf7dce2e2022-10-07 05:48:08 +0000287 interfaces.emplace(ifa->ifa_name);
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530288 }
289 return interfaces;
290}
291
William A. Kennington III69f45542022-09-24 23:28:14 -0700292void deleteInterface(stdplus::const_zstring intf)
Ratan Guptabc886292017-07-25 18:29:57 +0530293{
294 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500295 int status{};
Ratan Guptabc886292017-07-25 18:29:57 +0530296
297 if (pid == 0)
298 {
299
300 execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr);
301 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500302 log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error),
Ratan Guptabc886292017-07-25 18:29:57 +0530303 entry("INTF=%s", intf.c_str()));
304 elog<InternalFailure>();
305 }
306 else if (pid < 0)
307 {
308 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500309 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabc886292017-07-25 18:29:57 +0530310 elog<InternalFailure>();
311 }
312 else if (pid > 0)
313 {
314 while (waitpid(pid, &status, 0) == -1)
315 {
316 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500317 { /* Error other than EINTR */
Ratan Guptabc886292017-07-25 18:29:57 +0530318 status = -1;
319 break;
320 }
321 }
322
Gunnar Mills57d9c502018-09-14 14:42:34 -0500323 if (status < 0)
Ratan Guptabc886292017-07-25 18:29:57 +0530324 {
325 log<level::ERR>("Unable to delete the interface",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500326 entry("INTF=%s", intf.c_str()),
327 entry("STATUS=%d", status));
Ratan Guptabc886292017-07-25 18:29:57 +0530328 elog<InternalFailure>();
329 }
330 }
331}
332
William A. Kennington III69f45542022-09-24 23:28:14 -0700333std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700334{
William A. Kennington III69f45542022-09-24 23:28:14 -0700335 constexpr auto pfx = "eth"sv;
336 if (!intf.starts_with(pfx))
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700337 {
338 return std::nullopt;
339 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700340 intf.remove_prefix(pfx.size());
341 auto last = intf.data() + intf.size();
342 unsigned long idx;
343 auto res = std::from_chars(intf.data(), last, idx);
344 if (res.ec != std::errc() || res.ptr != last)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700345 {
346 return std::nullopt;
347 }
348 if (idx == 0)
349 {
350 return "ethaddr";
351 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700352 return fmt::format(FMT_COMPILE("eth{}addr"), idx);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700353}
354
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700355static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
356{
357 if (config::icaseeq(str, "ipv4"))
358 {
359 return DHCPVal{.v4 = true, .v6 = false};
360 }
361 if (config::icaseeq(str, "ipv6"))
362 {
363 return DHCPVal{.v4 = false, .v6 = true};
364 }
365 if (auto b = config::parseBool(str); b)
366 {
367 return DHCPVal{.v4 = *b, .v6 = *b};
368 }
369 return std::nullopt;
370}
371
372inline auto systemdParseLast(const config::Parser& config,
373 std::string_view section, std::string_view key,
374 auto&& fun)
375{
376 if (auto str = config.map.getLastValueString(section, key); str == nullptr)
377 {
378 auto err = fmt::format("Unable to get the value of {}[{}] from {}",
379 section, key, config.getFilename().native());
380 log<level::NOTICE>(err.c_str(),
381 entry("FILE=%s", config.getFilename().c_str()));
382 }
383 else if (auto val = fun(*str); !val)
384 {
385 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section,
386 key, config.getFilename().native(), *str);
387 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()),
388 entry("FILE=%s", config.getFilename().c_str()));
389 }
390 else
391 {
392 return val;
393 }
394 return decltype(fun(std::string_view{}))(std::nullopt);
395}
396
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700397bool getIPv6AcceptRA(const config::Parser& config)
Ratan Gupta56187e72017-08-13 09:40:14 +0530398{
William A. Kennington III324d2602022-08-18 18:32:56 -0700399#ifdef ENABLE_IPV6_ACCEPT_RA
400 constexpr bool def = true;
401#else
402 constexpr bool def = false;
403#endif
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700404 return systemdParseLast(config, "Network", "IPv6AcceptRA",
405 config::parseBool)
406 .value_or(def);
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700407}
408
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700409DHCPVal getDHCPValue(const config::Parser& config)
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700410{
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700411 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP)
412 .value_or(DHCPVal{.v4 = true, .v6 = true});
413}
William A. Kennington III324d2602022-08-18 18:32:56 -0700414
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700415bool getDHCPProp(const config::Parser& config, std::string_view key)
416{
417 return systemdParseLast(config, "DHCP", key, config::parseBool)
418 .value_or(true);
Ratan Gupta56187e72017-08-13 09:40:14 +0530419}
420
Ratan Guptabd303b12017-08-18 17:10:07 +0530421namespace mac_address
422{
423
424constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
425constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
426constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
427constexpr auto propIntf = "org.freedesktop.DBus.Properties";
428constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530429constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530430
431using DbusObjectPath = std::string;
432using DbusService = std::string;
433using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500434using ObjectTree =
435 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530436
437constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
438constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500439 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530440constexpr auto invRoot = "/xyz/openbmc_project/inventory";
441
Patrick Williamsc38b0712022-07-22 19:26:54 -0500442ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530443{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530444
445 std::string interfaceName = intfName;
446
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700447#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530448 // load the config JSON from the Read Only Path
449 std::ifstream in(configFile);
450 nlohmann::json configJson;
451 in >> configJson;
452 interfaceName = configJson[intfName];
453#endif
454
Ratan Guptabd303b12017-08-18 17:10:07 +0530455 std::vector<DbusInterface> interfaces;
456 interfaces.emplace_back(invNetworkIntf);
457
458 auto depth = 0;
459
Gunnar Mills57d9c502018-09-14 14:42:34 -0500460 auto mapperCall =
461 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530462
463 mapperCall.append(invRoot, depth, interfaces);
464
465 auto mapperReply = bus.call(mapperCall);
466 if (mapperReply.is_method_error())
467 {
468 log<level::ERR>("Error in mapper call");
469 elog<InternalFailure>();
470 }
471
472 ObjectTree objectTree;
473 mapperReply.read(objectTree);
474
475 if (objectTree.empty())
476 {
477 log<level::ERR>("No Object has implemented the interface",
478 entry("INTERFACE=%s", invNetworkIntf));
479 elog<InternalFailure>();
480 }
481
Alvin Wang38a63c32019-08-29 22:56:46 +0800482 DbusObjectPath objPath;
483 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530484
Alvin Wang38a63c32019-08-29 22:56:46 +0800485 if (1 == objectTree.size())
486 {
487 objPath = objectTree.begin()->first;
488 service = objectTree.begin()->second.begin()->first;
489 }
490 else
491 {
492 // If there are more than 2 objects, object path must contain the
493 // interface name
494 for (auto const& object : objectTree)
495 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530496 log<level::INFO>("interface",
497 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800498 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530499
500 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800501 {
502 objPath = object.first;
503 service = object.second.begin()->first;
504 break;
505 }
506 }
507
508 if (objPath.empty())
509 {
510 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530511 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800512 elog<InternalFailure>();
513 }
514 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530515
Gunnar Mills57d9c502018-09-14 14:42:34 -0500516 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
517 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530518
519 method.append(invNetworkIntf, "MACAddress");
520
521 auto reply = bus.call(method);
522 if (reply.is_method_error())
523 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500524 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530525 entry("PATH=%s", objPath.c_str()),
526 entry("INTERFACE=%s", invNetworkIntf));
527 elog<InternalFailure>();
528 }
529
William A. Kennington III1137a972019-04-20 20:49:58 -0700530 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530531 reply.read(value);
William A. Kennington III1137a972019-04-20 20:49:58 -0700532 return fromString(std::get<std::string>(value));
533}
534
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700535static uint8_t decodeHex(std::string_view str)
William A. Kennington III1137a972019-04-20 20:49:58 -0700536{
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700537 uint8_t ret;
538 auto res = std::from_chars(str.begin(), str.end(), ret, 16);
539 if (res.ptr != str.end() || res.ec != std::errc())
540 {
541 throw std::invalid_argument("Not hex");
542 }
543 return ret;
544}
Potin Laida0b1d42021-12-26 20:08:20 +0800545
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700546ether_addr fromString(std::string_view str)
547{
548 ether_addr ret;
William A. Kennington III69f45542022-09-24 23:28:14 -0700549 if (str.size() == 12 && str.find(":") == str.npos)
William A. Kennington III1137a972019-04-20 20:49:58 -0700550 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700551 for (size_t i = 0; i < 6; ++i)
552 {
553 ret.ether_addr_octet[i] = decodeHex(str.substr(i * 2, 2));
554 }
William A. Kennington III1137a972019-04-20 20:49:58 -0700555 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700556 else
Potin Laida0b1d42021-12-26 20:08:20 +0800557 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700558 for (size_t i = 0; i < 5; ++i)
559 {
560 auto loc = str.find(":");
561 ret.ether_addr_octet[i] = decodeHex(str.substr(0, loc));
562 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
563 if (str.empty())
564 {
565 throw std::invalid_argument("Missing mac data");
566 }
567 }
568 ret.ether_addr_octet[5] = decodeHex(str);
Potin Laida0b1d42021-12-26 20:08:20 +0800569 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700570 return ret;
Ratan Guptabd303b12017-08-18 17:10:07 +0530571}
572
William A. Kennington III6ca08d82019-04-20 16:04:18 -0700573std::string toString(const ether_addr& mac)
William A. Kennington IIIa14879e2019-02-01 21:43:11 -0800574{
William A. Kennington III69f45542022-09-24 23:28:14 -0700575 return fmt::format(FMT_COMPILE("{:02x}"),
576 fmt::join(mac.ether_addr_octet, ":"));
William A. Kennington IIId27410f2019-01-30 17:15:43 -0800577}
578
William A. Kennington III1137a972019-04-20 20:49:58 -0700579bool isEmpty(const ether_addr& mac)
580{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700581 return stdplus::raw::equal(mac, ether_addr{});
William A. Kennington III1137a972019-04-20 20:49:58 -0700582}
583
584bool isMulticast(const ether_addr& mac)
585{
586 return mac.ether_addr_octet[0] & 0b1;
587}
588
589bool isUnicast(const ether_addr& mac)
590{
591 return !isEmpty(mac) && !isMulticast(mac);
592}
593
Gunnar Mills57d9c502018-09-14 14:42:34 -0500594} // namespace mac_address
595} // namespace network
596} // namespace phosphor