blob: 9d58bcbd55d23487cb1b267c8201740656d00c38 [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
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700141constexpr auto familyVisit(auto&& visitor, int family)
142{
143 if (family == AF_INET)
144 {
145 return visitor.template operator()<AF_INET>();
146 }
147 else if (family == AF_INET6)
148 {
149 return visitor.template operator()<AF_INET6>();
150 }
151 throw std::invalid_argument("Invalid addr family");
152}
153
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700154template <int family>
155typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf)
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800156{
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700157 return stdplus::raw::copyFromStrict<typename FamilyTraits<family>::addr>(
158 buf);
159}
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800160
William A. Kennington III97b5dc62022-10-07 14:01:29 -0700161InAddrAny addrFromBuf(int family, std::string_view buf)
162{
163 return familyVisit(
164 [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family);
William A. Kennington IIIa00b1c32019-02-01 18:57:17 -0800165}
166
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700167template <typename Addr>
168std::string toString(const Addr& addr)
Alexander Filippov983da552021-02-08 15:26:54 +0300169{
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700170 static constexpr int family = AddrToFamily<Addr>::value;
171 std::string ret(FamilyTraits<family>::strlen, '\0');
172 if (inet_ntop(family, &addr, ret.data(), ret.size()) == nullptr)
Alexander Filippov983da552021-02-08 15:26:54 +0300173 {
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700174 throw std::runtime_error("Failed to convert IP to string");
Alexander Filippov983da552021-02-08 15:26:54 +0300175 }
176
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700177 ret.resize(strlen(ret.c_str()));
178 return ret;
Alexander Filippov983da552021-02-08 15:26:54 +0300179}
180
William A. Kennington III5058f572019-01-30 17:18:14 -0800181std::string toString(const InAddrAny& addr)
182{
William A. Kennington IIId07c0862022-10-07 14:17:05 -0700183 return std::visit([](auto&& a) { return toString(a); }, addr);
William A. Kennington III5058f572019-01-30 17:18:14 -0800184}
185
William A. Kennington III9f228302022-10-07 14:02:49 -0700186bool isValidIP(int family, stdplus::const_zstring address) noexcept
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500187{
188 unsigned char buf[sizeof(struct in6_addr)];
William A. Kennington III9f228302022-10-07 14:02:49 -0700189 return inet_pton(family, address.c_str(), buf) > 0;
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500190}
191
William A. Kennington IIIff12acb2022-10-07 19:06:56 -0700192bool isValidIP(stdplus::const_zstring address) noexcept
193{
194 return isValidIP(AF_INET, address) || isValidIP(AF_INET6, address);
195}
196
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700197bool isValidPrefix(int family, uint8_t prefix)
Nagaraju Goruganti66b974d2017-10-03 08:43:08 -0500198{
William A. Kennington IIIe5a48ab2019-04-22 03:55:23 -0700199 return familyVisit(
200 [=]<int f>() noexcept { return isValidPrefix<f>(prefix); }, family);
Ratan Gupta8804feb2017-05-25 10:49:57 +0530201}
202
William A. Kennington III96444792022-10-05 15:16:22 -0700203string_uset getSystemInterfaces()
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530204{
William A. Kennington III96444792022-10-05 15:16:22 -0700205 string_uset interfaces;
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530206 struct ifaddrs* ifaddr = nullptr;
207
208 // attempt to fill struct with ifaddrs
209 if (getifaddrs(&ifaddr) == -1)
210 {
211 auto error = errno;
212 log<level::ERR>("Error occurred during the getifaddrs call",
213 entry("ERRNO=%d", error));
214 elog<InternalFailure>();
215 }
216
217 AddrPtr ifaddrPtr(ifaddr);
218 ifaddr = nullptr;
Lei YUefda98b2021-03-18 15:52:19 +0800219 const auto& ignoredInterfaces = internal::getIgnoredInterfaces();
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530220
221 for (ifaddrs* ifa = ifaddrPtr.get(); ifa != nullptr; ifa = ifa->ifa_next)
222 {
223 // walk interfaces
William A. Kennington IIIf273d2b2019-03-21 14:38:36 -0700224 // if loopback ignore
Lei YUefda98b2021-03-18 15:52:19 +0800225 if (ifa->ifa_flags & IFF_LOOPBACK ||
226 ignoredInterfaces.find(ifa->ifa_name) != ignoredInterfaces.end())
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530227 {
228 continue;
229 }
Willy Tuf7dce2e2022-10-07 05:48:08 +0000230 interfaces.emplace(ifa->ifa_name);
Ratan Guptafd4b0f02017-09-16 06:01:24 +0530231 }
232 return interfaces;
233}
234
William A. Kennington III69f45542022-09-24 23:28:14 -0700235void deleteInterface(stdplus::const_zstring intf)
Ratan Guptabc886292017-07-25 18:29:57 +0530236{
237 pid_t pid = fork();
Gunnar Mills57d9c502018-09-14 14:42:34 -0500238 int status{};
Ratan Guptabc886292017-07-25 18:29:57 +0530239
240 if (pid == 0)
241 {
242
243 execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr);
244 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500245 log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error),
Ratan Guptabc886292017-07-25 18:29:57 +0530246 entry("INTF=%s", intf.c_str()));
247 elog<InternalFailure>();
248 }
249 else if (pid < 0)
250 {
251 auto error = errno;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500252 log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error));
Ratan Guptabc886292017-07-25 18:29:57 +0530253 elog<InternalFailure>();
254 }
255 else if (pid > 0)
256 {
257 while (waitpid(pid, &status, 0) == -1)
258 {
259 if (errno != EINTR)
Gunnar Mills57d9c502018-09-14 14:42:34 -0500260 { /* Error other than EINTR */
Ratan Guptabc886292017-07-25 18:29:57 +0530261 status = -1;
262 break;
263 }
264 }
265
Gunnar Mills57d9c502018-09-14 14:42:34 -0500266 if (status < 0)
Ratan Guptabc886292017-07-25 18:29:57 +0530267 {
268 log<level::ERR>("Unable to delete the interface",
Joseph Reynolds02653ca2018-05-10 15:55:09 -0500269 entry("INTF=%s", intf.c_str()),
270 entry("STATUS=%d", status));
Ratan Guptabc886292017-07-25 18:29:57 +0530271 elog<InternalFailure>();
272 }
273 }
274}
275
William A. Kennington III69f45542022-09-24 23:28:14 -0700276std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700277{
William A. Kennington III69f45542022-09-24 23:28:14 -0700278 constexpr auto pfx = "eth"sv;
279 if (!intf.starts_with(pfx))
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700280 {
281 return std::nullopt;
282 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700283 intf.remove_prefix(pfx.size());
284 auto last = intf.data() + intf.size();
285 unsigned long idx;
286 auto res = std::from_chars(intf.data(), last, idx);
287 if (res.ec != std::errc() || res.ptr != last)
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700288 {
289 return std::nullopt;
290 }
291 if (idx == 0)
292 {
293 return "ethaddr";
294 }
William A. Kennington III69f45542022-09-24 23:28:14 -0700295 return fmt::format(FMT_COMPILE("eth{}addr"), idx);
William A. Kennington III7b9e8bd2019-04-23 19:31:31 -0700296}
297
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700298static std::optional<DHCPVal> systemdParseDHCP(std::string_view str)
299{
300 if (config::icaseeq(str, "ipv4"))
301 {
302 return DHCPVal{.v4 = true, .v6 = false};
303 }
304 if (config::icaseeq(str, "ipv6"))
305 {
306 return DHCPVal{.v4 = false, .v6 = true};
307 }
308 if (auto b = config::parseBool(str); b)
309 {
310 return DHCPVal{.v4 = *b, .v6 = *b};
311 }
312 return std::nullopt;
313}
314
315inline auto systemdParseLast(const config::Parser& config,
316 std::string_view section, std::string_view key,
317 auto&& fun)
318{
319 if (auto str = config.map.getLastValueString(section, key); str == nullptr)
320 {
321 auto err = fmt::format("Unable to get the value of {}[{}] from {}",
322 section, key, config.getFilename().native());
323 log<level::NOTICE>(err.c_str(),
324 entry("FILE=%s", config.getFilename().c_str()));
325 }
326 else if (auto val = fun(*str); !val)
327 {
328 auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section,
329 key, config.getFilename().native(), *str);
330 log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()),
331 entry("FILE=%s", config.getFilename().c_str()));
332 }
333 else
334 {
335 return val;
336 }
337 return decltype(fun(std::string_view{}))(std::nullopt);
338}
339
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700340bool getIPv6AcceptRA(const config::Parser& config)
Ratan Gupta56187e72017-08-13 09:40:14 +0530341{
William A. Kennington III324d2602022-08-18 18:32:56 -0700342#ifdef ENABLE_IPV6_ACCEPT_RA
343 constexpr bool def = true;
344#else
345 constexpr bool def = false;
346#endif
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700347 return systemdParseLast(config, "Network", "IPv6AcceptRA",
348 config::parseBool)
349 .value_or(def);
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700350}
351
William A. Kennington III8060c0d2022-08-18 19:19:34 -0700352DHCPVal getDHCPValue(const config::Parser& config)
William A. Kennington IIIa520a392022-08-08 12:17:34 -0700353{
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700354 return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP)
355 .value_or(DHCPVal{.v4 = true, .v6 = true});
356}
William A. Kennington III324d2602022-08-18 18:32:56 -0700357
William A. Kennington IIIe94c9ff2022-08-18 20:12:27 -0700358bool getDHCPProp(const config::Parser& config, std::string_view key)
359{
360 return systemdParseLast(config, "DHCP", key, config::parseBool)
361 .value_or(true);
Ratan Gupta56187e72017-08-13 09:40:14 +0530362}
363
Ratan Guptabd303b12017-08-18 17:10:07 +0530364namespace mac_address
365{
366
367constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper";
368constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper";
369constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper";
370constexpr auto propIntf = "org.freedesktop.DBus.Properties";
371constexpr auto methodGet = "Get";
Manojkiran Edacc099a82020-05-11 14:25:16 +0530372constexpr auto configFile = "/usr/share/network/config.json";
Ratan Guptabd303b12017-08-18 17:10:07 +0530373
374using DbusObjectPath = std::string;
375using DbusService = std::string;
376using DbusInterface = std::string;
Gunnar Mills57d9c502018-09-14 14:42:34 -0500377using ObjectTree =
378 std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>;
Ratan Guptabd303b12017-08-18 17:10:07 +0530379
380constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager";
381constexpr auto invNetworkIntf =
Gunnar Mills57d9c502018-09-14 14:42:34 -0500382 "xyz.openbmc_project.Inventory.Item.NetworkInterface";
Ratan Guptabd303b12017-08-18 17:10:07 +0530383constexpr auto invRoot = "/xyz/openbmc_project/inventory";
384
Patrick Williamsc38b0712022-07-22 19:26:54 -0500385ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName)
Ratan Guptabd303b12017-08-18 17:10:07 +0530386{
Manojkiran Edacc099a82020-05-11 14:25:16 +0530387
388 std::string interfaceName = intfName;
389
William A. Kennington III6f39c5e2021-05-13 18:39:23 -0700390#ifdef SYNC_MAC_FROM_INVENTORY
Manojkiran Edacc099a82020-05-11 14:25:16 +0530391 // load the config JSON from the Read Only Path
392 std::ifstream in(configFile);
393 nlohmann::json configJson;
394 in >> configJson;
395 interfaceName = configJson[intfName];
396#endif
397
Ratan Guptabd303b12017-08-18 17:10:07 +0530398 std::vector<DbusInterface> interfaces;
399 interfaces.emplace_back(invNetworkIntf);
400
401 auto depth = 0;
402
Gunnar Mills57d9c502018-09-14 14:42:34 -0500403 auto mapperCall =
404 bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree");
Ratan Guptabd303b12017-08-18 17:10:07 +0530405
406 mapperCall.append(invRoot, depth, interfaces);
407
408 auto mapperReply = bus.call(mapperCall);
409 if (mapperReply.is_method_error())
410 {
411 log<level::ERR>("Error in mapper call");
412 elog<InternalFailure>();
413 }
414
415 ObjectTree objectTree;
416 mapperReply.read(objectTree);
417
418 if (objectTree.empty())
419 {
420 log<level::ERR>("No Object has implemented the interface",
421 entry("INTERFACE=%s", invNetworkIntf));
422 elog<InternalFailure>();
423 }
424
Alvin Wang38a63c32019-08-29 22:56:46 +0800425 DbusObjectPath objPath;
426 DbusService service;
Ratan Guptabd303b12017-08-18 17:10:07 +0530427
Alvin Wang38a63c32019-08-29 22:56:46 +0800428 if (1 == objectTree.size())
429 {
430 objPath = objectTree.begin()->first;
431 service = objectTree.begin()->second.begin()->first;
432 }
433 else
434 {
435 // If there are more than 2 objects, object path must contain the
436 // interface name
437 for (auto const& object : objectTree)
438 {
Manojkiran Edacc099a82020-05-11 14:25:16 +0530439 log<level::INFO>("interface",
440 entry("INT=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800441 log<level::INFO>("object", entry("OBJ=%s", object.first.c_str()));
Manojkiran Edacc099a82020-05-11 14:25:16 +0530442
443 if (std::string::npos != object.first.find(interfaceName.c_str()))
Alvin Wang38a63c32019-08-29 22:56:46 +0800444 {
445 objPath = object.first;
446 service = object.second.begin()->first;
447 break;
448 }
449 }
450
451 if (objPath.empty())
452 {
453 log<level::ERR>("Can't find the object for the interface",
Manojkiran Edacc099a82020-05-11 14:25:16 +0530454 entry("intfName=%s", interfaceName.c_str()));
Alvin Wang38a63c32019-08-29 22:56:46 +0800455 elog<InternalFailure>();
456 }
457 }
Ratan Guptabd303b12017-08-18 17:10:07 +0530458
Gunnar Mills57d9c502018-09-14 14:42:34 -0500459 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
460 propIntf, methodGet);
Ratan Guptabd303b12017-08-18 17:10:07 +0530461
462 method.append(invNetworkIntf, "MACAddress");
463
464 auto reply = bus.call(method);
465 if (reply.is_method_error())
466 {
Gunnar Mills57d9c502018-09-14 14:42:34 -0500467 log<level::ERR>("Failed to get MACAddress",
Ratan Guptabd303b12017-08-18 17:10:07 +0530468 entry("PATH=%s", objPath.c_str()),
469 entry("INTERFACE=%s", invNetworkIntf));
470 elog<InternalFailure>();
471 }
472
William A. Kennington III1137a972019-04-20 20:49:58 -0700473 std::variant<std::string> value;
Ratan Guptabd303b12017-08-18 17:10:07 +0530474 reply.read(value);
William A. Kennington III1137a972019-04-20 20:49:58 -0700475 return fromString(std::get<std::string>(value));
476}
477
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700478static uint8_t decodeHex(std::string_view str)
William A. Kennington III1137a972019-04-20 20:49:58 -0700479{
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700480 uint8_t ret;
481 auto res = std::from_chars(str.begin(), str.end(), ret, 16);
482 if (res.ptr != str.end() || res.ec != std::errc())
483 {
484 throw std::invalid_argument("Not hex");
485 }
486 return ret;
487}
Potin Laida0b1d42021-12-26 20:08:20 +0800488
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700489ether_addr fromString(std::string_view str)
490{
491 ether_addr ret;
William A. Kennington III69f45542022-09-24 23:28:14 -0700492 if (str.size() == 12 && str.find(":") == str.npos)
William A. Kennington III1137a972019-04-20 20:49:58 -0700493 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700494 for (size_t i = 0; i < 6; ++i)
495 {
496 ret.ether_addr_octet[i] = decodeHex(str.substr(i * 2, 2));
497 }
William A. Kennington III1137a972019-04-20 20:49:58 -0700498 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700499 else
Potin Laida0b1d42021-12-26 20:08:20 +0800500 {
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700501 for (size_t i = 0; i < 5; ++i)
502 {
503 auto loc = str.find(":");
504 ret.ether_addr_octet[i] = decodeHex(str.substr(0, loc));
505 str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
506 if (str.empty())
507 {
508 throw std::invalid_argument("Missing mac data");
509 }
510 }
511 ret.ether_addr_octet[5] = decodeHex(str);
Potin Laida0b1d42021-12-26 20:08:20 +0800512 }
William A. Kennington IIIfeb7aab2022-10-03 17:21:44 -0700513 return ret;
Ratan Guptabd303b12017-08-18 17:10:07 +0530514}
515
William A. Kennington III6ca08d82019-04-20 16:04:18 -0700516std::string toString(const ether_addr& mac)
William A. Kennington IIIa14879e2019-02-01 21:43:11 -0800517{
William A. Kennington III69f45542022-09-24 23:28:14 -0700518 return fmt::format(FMT_COMPILE("{:02x}"),
519 fmt::join(mac.ether_addr_octet, ":"));
William A. Kennington IIId27410f2019-01-30 17:15:43 -0800520}
521
William A. Kennington III1137a972019-04-20 20:49:58 -0700522bool isEmpty(const ether_addr& mac)
523{
William A. Kennington III12beaad2020-06-13 19:30:41 -0700524 return stdplus::raw::equal(mac, ether_addr{});
William A. Kennington III1137a972019-04-20 20:49:58 -0700525}
526
527bool isMulticast(const ether_addr& mac)
528{
529 return mac.ether_addr_octet[0] & 0b1;
530}
531
532bool isUnicast(const ether_addr& mac)
533{
534 return !isEmpty(mac) && !isMulticast(mac);
535}
536
Gunnar Mills57d9c502018-09-14 14:42:34 -0500537} // namespace mac_address
538} // namespace network
539} // namespace phosphor