| William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 1 | #include "config.h" | 
|  | 2 |  | 
| Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 3 | #include "util.hpp" | 
| Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 4 |  | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 5 | #include "config_parser.hpp" | 
|  | 6 | #include "types.hpp" | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 7 |  | 
| William A. Kennington III | 1c77602 | 2022-01-05 14:12:16 -0800 | [diff] [blame] | 8 | #include <fmt/compile.h> | 
|  | 9 | #include <fmt/format.h> | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 10 | #include <sys/wait.h> | 
| Ratan Gupta | 3681a50 | 2017-06-17 19:20:04 +0530 | [diff] [blame] | 11 |  | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 12 | #include <cctype> | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 13 | #include <charconv> | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 14 | #include <fstream> | 
| William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 15 | #ifdef SYNC_MAC_FROM_INVENTORY | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 16 | #include <nlohmann/json.hpp> | 
| William A. Kennington III | de433b7 | 2021-05-17 22:59:28 -0700 | [diff] [blame] | 17 | #endif | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 18 | #include <phosphor-logging/elog-errors.hpp> | 
|  | 19 | #include <phosphor-logging/log.hpp> | 
|  | 20 | #include <string> | 
| William A. Kennington III | feb7aab | 2022-10-03 17:21:44 -0700 | [diff] [blame] | 21 | #include <string_view> | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 22 | #include <variant> | 
| Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 23 | #include <xyz/openbmc_project/Common/error.hpp> | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 24 |  | 
|  | 25 | namespace phosphor | 
|  | 26 | { | 
|  | 27 | namespace network | 
|  | 28 | { | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 29 |  | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 30 | using std::literals::string_view_literals::operator""sv; | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 31 | using namespace phosphor::logging; | 
| Ratan Gupta | 11cef80 | 2017-05-29 08:41:48 +0530 | [diff] [blame] | 32 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 33 |  | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 34 | namespace internal | 
|  | 35 | { | 
|  | 36 |  | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 37 | void executeCommandinChildProcess(stdplus::const_zstring path, char** args) | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 38 | { | 
|  | 39 | using namespace std::string_literals; | 
|  | 40 | pid_t pid = fork(); | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 41 |  | 
|  | 42 | if (pid == 0) | 
|  | 43 | { | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 44 | execv(path.c_str(), args); | 
|  | 45 | exit(255); | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 46 | } | 
|  | 47 | else if (pid < 0) | 
|  | 48 | { | 
|  | 49 | auto error = errno; | 
|  | 50 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); | 
|  | 51 | elog<InternalFailure>(); | 
|  | 52 | } | 
|  | 53 | else if (pid > 0) | 
|  | 54 | { | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 55 | int status; | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 56 | while (waitpid(pid, &status, 0) == -1) | 
|  | 57 | { | 
|  | 58 | if (errno != EINTR) | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 59 | { | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 60 | status = -1; | 
|  | 61 | break; | 
|  | 62 | } | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | if (status < 0) | 
|  | 66 | { | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 67 | fmt::memory_buffer buf; | 
|  | 68 | fmt::format_to(fmt::appender(buf), "`{}`", path); | 
|  | 69 | for (size_t i = 0; args[i] != nullptr; ++i) | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 70 | { | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 71 | fmt::format_to(fmt::appender(buf), " `{}`", args[i]); | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 72 | } | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 73 | buf.push_back('\0'); | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 74 | log<level::ERR>("Unable to execute the command", | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 75 | entry("CMD=%s", buf.data()), | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 76 | entry("STATUS=%d", status)); | 
|  | 77 | elog<InternalFailure>(); | 
|  | 78 | } | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 82 | /** @brief Get ignored interfaces from environment */ | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 83 | std::string_view getIgnoredInterfacesEnv() | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 84 | { | 
|  | 85 | auto r = std::getenv("IGNORED_INTERFACES"); | 
|  | 86 | if (r == nullptr) | 
|  | 87 | { | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 88 | return ""; | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 89 | } | 
|  | 90 | return r; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | /** @brief Parse the comma separated interface names */ | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 94 | std::unordered_set<std::string_view> | 
|  | 95 | parseInterfaces(std::string_view interfaces) | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 96 | { | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 97 | std::unordered_set<std::string_view> result; | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 98 | while (true) | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 99 | { | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 100 | auto sep = interfaces.find(','); | 
|  | 101 | auto interface = interfaces.substr(0, sep); | 
|  | 102 | while (!interface.empty() && std::isspace(interface.front())) | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 103 | { | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 104 | interface.remove_prefix(1); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 105 | } | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 106 | while (!interface.empty() && std::isspace(interface.back())) | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 107 | { | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 108 | interface.remove_suffix(1); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 109 | } | 
| William A. Kennington III | ee5b2c9 | 2021-04-28 02:31:28 -0700 | [diff] [blame] | 110 | if (!interface.empty()) | 
|  | 111 | { | 
|  | 112 | result.insert(interface); | 
|  | 113 | } | 
|  | 114 | if (sep == interfaces.npos) | 
|  | 115 | { | 
|  | 116 | break; | 
|  | 117 | } | 
|  | 118 | interfaces = interfaces.substr(sep + 1); | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 119 | } | 
|  | 120 | return result; | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | /** @brief Get the ignored interfaces */ | 
| William A. Kennington III | 95530ec | 2022-08-19 01:44:39 -0700 | [diff] [blame] | 124 | const std::unordered_set<std::string_view>& getIgnoredInterfaces() | 
| Lei YU | 307554e | 2021-03-18 14:56:50 +0800 | [diff] [blame] | 125 | { | 
|  | 126 | static auto ignoredInterfaces = parseInterfaces(getIgnoredInterfacesEnv()); | 
|  | 127 | return ignoredInterfaces; | 
|  | 128 | } | 
|  | 129 |  | 
| Lei YU | 3894ce7 | 2021-03-18 14:53:42 +0800 | [diff] [blame] | 130 | } // namespace internal | 
| Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 131 |  | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 132 | void deleteInterface(stdplus::const_zstring intf) | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 133 | { | 
|  | 134 | pid_t pid = fork(); | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 135 | int status{}; | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 136 |  | 
|  | 137 | if (pid == 0) | 
|  | 138 | { | 
|  | 139 |  | 
|  | 140 | execl("/sbin/ip", "ip", "link", "delete", "dev", intf.c_str(), nullptr); | 
|  | 141 | auto error = errno; | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 142 | log<level::ERR>("Couldn't delete the device", entry("ERRNO=%d", error), | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 143 | entry("INTF=%s", intf.c_str())); | 
|  | 144 | elog<InternalFailure>(); | 
|  | 145 | } | 
|  | 146 | else if (pid < 0) | 
|  | 147 | { | 
|  | 148 | auto error = errno; | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 149 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 150 | elog<InternalFailure>(); | 
|  | 151 | } | 
|  | 152 | else if (pid > 0) | 
|  | 153 | { | 
|  | 154 | while (waitpid(pid, &status, 0) == -1) | 
|  | 155 | { | 
|  | 156 | if (errno != EINTR) | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 157 | { /* Error other than EINTR */ | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 158 | status = -1; | 
|  | 159 | break; | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 163 | if (status < 0) | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 164 | { | 
|  | 165 | log<level::ERR>("Unable to delete the interface", | 
| Joseph Reynolds | 02653ca | 2018-05-10 15:55:09 -0500 | [diff] [blame] | 166 | entry("INTF=%s", intf.c_str()), | 
|  | 167 | entry("STATUS=%d", status)); | 
| Ratan Gupta | bc88629 | 2017-07-25 18:29:57 +0530 | [diff] [blame] | 168 | elog<InternalFailure>(); | 
|  | 169 | } | 
|  | 170 | } | 
|  | 171 | } | 
|  | 172 |  | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 173 | std::optional<std::string> interfaceToUbootEthAddr(std::string_view intf) | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 174 | { | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 175 | constexpr auto pfx = "eth"sv; | 
|  | 176 | if (!intf.starts_with(pfx)) | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 177 | { | 
|  | 178 | return std::nullopt; | 
|  | 179 | } | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 180 | intf.remove_prefix(pfx.size()); | 
|  | 181 | auto last = intf.data() + intf.size(); | 
|  | 182 | unsigned long idx; | 
|  | 183 | auto res = std::from_chars(intf.data(), last, idx); | 
|  | 184 | if (res.ec != std::errc() || res.ptr != last) | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 185 | { | 
|  | 186 | return std::nullopt; | 
|  | 187 | } | 
|  | 188 | if (idx == 0) | 
|  | 189 | { | 
|  | 190 | return "ethaddr"; | 
|  | 191 | } | 
| William A. Kennington III | 69f4554 | 2022-09-24 23:28:14 -0700 | [diff] [blame] | 192 | return fmt::format(FMT_COMPILE("eth{}addr"), idx); | 
| William A. Kennington III | 7b9e8bd | 2019-04-23 19:31:31 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 195 | static std::optional<DHCPVal> systemdParseDHCP(std::string_view str) | 
|  | 196 | { | 
|  | 197 | if (config::icaseeq(str, "ipv4")) | 
|  | 198 | { | 
|  | 199 | return DHCPVal{.v4 = true, .v6 = false}; | 
|  | 200 | } | 
|  | 201 | if (config::icaseeq(str, "ipv6")) | 
|  | 202 | { | 
|  | 203 | return DHCPVal{.v4 = false, .v6 = true}; | 
|  | 204 | } | 
|  | 205 | if (auto b = config::parseBool(str); b) | 
|  | 206 | { | 
|  | 207 | return DHCPVal{.v4 = *b, .v6 = *b}; | 
|  | 208 | } | 
|  | 209 | return std::nullopt; | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | inline auto systemdParseLast(const config::Parser& config, | 
|  | 213 | std::string_view section, std::string_view key, | 
|  | 214 | auto&& fun) | 
|  | 215 | { | 
| William A. Kennington III | 4a688fc | 2022-11-15 15:58:44 -0800 | [diff] [blame] | 216 | if (!config.getFileExists()) | 
|  | 217 | { | 
|  | 218 | } | 
|  | 219 | else if (auto str = config.map.getLastValueString(section, key); | 
|  | 220 | str == nullptr) | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 221 | { | 
|  | 222 | auto err = fmt::format("Unable to get the value of {}[{}] from {}", | 
|  | 223 | section, key, config.getFilename().native()); | 
|  | 224 | log<level::NOTICE>(err.c_str(), | 
|  | 225 | entry("FILE=%s", config.getFilename().c_str())); | 
|  | 226 | } | 
|  | 227 | else if (auto val = fun(*str); !val) | 
|  | 228 | { | 
|  | 229 | auto err = fmt::format("Invalid value of {}[{}] from {}: {}", section, | 
|  | 230 | key, config.getFilename().native(), *str); | 
|  | 231 | log<level::NOTICE>(err.c_str(), entry("VALUE=%s", str->c_str()), | 
|  | 232 | entry("FILE=%s", config.getFilename().c_str())); | 
|  | 233 | } | 
|  | 234 | else | 
|  | 235 | { | 
|  | 236 | return val; | 
|  | 237 | } | 
|  | 238 | return decltype(fun(std::string_view{}))(std::nullopt); | 
|  | 239 | } | 
|  | 240 |  | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 241 | bool getIPv6AcceptRA(const config::Parser& config) | 
| Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 242 | { | 
| William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 243 | #ifdef ENABLE_IPV6_ACCEPT_RA | 
|  | 244 | constexpr bool def = true; | 
|  | 245 | #else | 
|  | 246 | constexpr bool def = false; | 
|  | 247 | #endif | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 248 | return systemdParseLast(config, "Network", "IPv6AcceptRA", | 
|  | 249 | config::parseBool) | 
|  | 250 | .value_or(def); | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 251 | } | 
|  | 252 |  | 
| William A. Kennington III | 8060c0d | 2022-08-18 19:19:34 -0700 | [diff] [blame] | 253 | DHCPVal getDHCPValue(const config::Parser& config) | 
| William A. Kennington III | a520a39 | 2022-08-08 12:17:34 -0700 | [diff] [blame] | 254 | { | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 255 | return systemdParseLast(config, "Network", "DHCP", systemdParseDHCP) | 
|  | 256 | .value_or(DHCPVal{.v4 = true, .v6 = true}); | 
|  | 257 | } | 
| William A. Kennington III | 324d260 | 2022-08-18 18:32:56 -0700 | [diff] [blame] | 258 |  | 
| William A. Kennington III | e94c9ff | 2022-08-18 20:12:27 -0700 | [diff] [blame] | 259 | bool getDHCPProp(const config::Parser& config, std::string_view key) | 
|  | 260 | { | 
|  | 261 | return systemdParseLast(config, "DHCP", key, config::parseBool) | 
|  | 262 | .value_or(true); | 
| Ratan Gupta | 56187e7 | 2017-08-13 09:40:14 +0530 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 265 | namespace mac_address | 
|  | 266 | { | 
|  | 267 |  | 
|  | 268 | constexpr auto mapperBus = "xyz.openbmc_project.ObjectMapper"; | 
|  | 269 | constexpr auto mapperObj = "/xyz/openbmc_project/object_mapper"; | 
|  | 270 | constexpr auto mapperIntf = "xyz.openbmc_project.ObjectMapper"; | 
|  | 271 | constexpr auto propIntf = "org.freedesktop.DBus.Properties"; | 
|  | 272 | constexpr auto methodGet = "Get"; | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 273 | constexpr auto configFile = "/usr/share/network/config.json"; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 274 |  | 
|  | 275 | using DbusObjectPath = std::string; | 
|  | 276 | using DbusService = std::string; | 
|  | 277 | using DbusInterface = std::string; | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 278 | using ObjectTree = | 
|  | 279 | std::map<DbusObjectPath, std::map<DbusService, std::vector<DbusInterface>>>; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 280 |  | 
|  | 281 | constexpr auto invBus = "xyz.openbmc_project.Inventory.Manager"; | 
|  | 282 | constexpr auto invNetworkIntf = | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 283 | "xyz.openbmc_project.Inventory.Item.NetworkInterface"; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 284 | constexpr auto invRoot = "/xyz/openbmc_project/inventory"; | 
|  | 285 |  | 
| Patrick Williams | c38b071 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 286 | ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName) | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 287 | { | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 288 |  | 
|  | 289 | std::string interfaceName = intfName; | 
|  | 290 |  | 
| William A. Kennington III | 6f39c5e | 2021-05-13 18:39:23 -0700 | [diff] [blame] | 291 | #ifdef SYNC_MAC_FROM_INVENTORY | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 292 | // load the config JSON from the Read Only Path | 
|  | 293 | std::ifstream in(configFile); | 
|  | 294 | nlohmann::json configJson; | 
|  | 295 | in >> configJson; | 
|  | 296 | interfaceName = configJson[intfName]; | 
|  | 297 | #endif | 
|  | 298 |  | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 299 | std::vector<DbusInterface> interfaces; | 
|  | 300 | interfaces.emplace_back(invNetworkIntf); | 
|  | 301 |  | 
|  | 302 | auto depth = 0; | 
|  | 303 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 304 | auto mapperCall = | 
|  | 305 | bus.new_method_call(mapperBus, mapperObj, mapperIntf, "GetSubTree"); | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 306 |  | 
|  | 307 | mapperCall.append(invRoot, depth, interfaces); | 
|  | 308 |  | 
|  | 309 | auto mapperReply = bus.call(mapperCall); | 
|  | 310 | if (mapperReply.is_method_error()) | 
|  | 311 | { | 
|  | 312 | log<level::ERR>("Error in mapper call"); | 
|  | 313 | elog<InternalFailure>(); | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | ObjectTree objectTree; | 
|  | 317 | mapperReply.read(objectTree); | 
|  | 318 |  | 
|  | 319 | if (objectTree.empty()) | 
|  | 320 | { | 
|  | 321 | log<level::ERR>("No Object has implemented the interface", | 
|  | 322 | entry("INTERFACE=%s", invNetworkIntf)); | 
|  | 323 | elog<InternalFailure>(); | 
|  | 324 | } | 
|  | 325 |  | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 326 | DbusObjectPath objPath; | 
|  | 327 | DbusService service; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 328 |  | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 329 | if (1 == objectTree.size()) | 
|  | 330 | { | 
|  | 331 | objPath = objectTree.begin()->first; | 
|  | 332 | service = objectTree.begin()->second.begin()->first; | 
|  | 333 | } | 
|  | 334 | else | 
|  | 335 | { | 
|  | 336 | // If there are more than 2 objects, object path must contain the | 
|  | 337 | // interface name | 
|  | 338 | for (auto const& object : objectTree) | 
|  | 339 | { | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 340 | log<level::INFO>("interface", | 
|  | 341 | entry("INT=%s", interfaceName.c_str())); | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 342 | log<level::INFO>("object", entry("OBJ=%s", object.first.c_str())); | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 343 |  | 
|  | 344 | if (std::string::npos != object.first.find(interfaceName.c_str())) | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 345 | { | 
|  | 346 | objPath = object.first; | 
|  | 347 | service = object.second.begin()->first; | 
|  | 348 | break; | 
|  | 349 | } | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | if (objPath.empty()) | 
|  | 353 | { | 
|  | 354 | log<level::ERR>("Can't find the object for the interface", | 
| Manojkiran Eda | cc099a8 | 2020-05-11 14:25:16 +0530 | [diff] [blame] | 355 | entry("intfName=%s", interfaceName.c_str())); | 
| Alvin Wang | 38a63c3 | 2019-08-29 22:56:46 +0800 | [diff] [blame] | 356 | elog<InternalFailure>(); | 
|  | 357 | } | 
|  | 358 | } | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 359 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 360 | auto method = bus.new_method_call(service.c_str(), objPath.c_str(), | 
|  | 361 | propIntf, methodGet); | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 362 |  | 
|  | 363 | method.append(invNetworkIntf, "MACAddress"); | 
|  | 364 |  | 
|  | 365 | auto reply = bus.call(method); | 
|  | 366 | if (reply.is_method_error()) | 
|  | 367 | { | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 368 | log<level::ERR>("Failed to get MACAddress", | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 369 | entry("PATH=%s", objPath.c_str()), | 
|  | 370 | entry("INTERFACE=%s", invNetworkIntf)); | 
|  | 371 | elog<InternalFailure>(); | 
|  | 372 | } | 
|  | 373 |  | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 374 | std::variant<std::string> value; | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 375 | reply.read(value); | 
| William A. Kennington III | b01d08f | 2022-11-03 12:50:00 -0700 | [diff] [blame] | 376 | return ToAddr<ether_addr>{}(std::get<std::string>(value)); | 
| Ratan Gupta | bd303b1 | 2017-08-18 17:10:07 +0530 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 379 | bool isEmpty(const ether_addr& mac) | 
|  | 380 | { | 
| William A. Kennington III | bb0eacc | 2022-10-21 15:22:06 -0700 | [diff] [blame] | 381 | return mac == ether_addr{}; | 
| William A. Kennington III | 1137a97 | 2019-04-20 20:49:58 -0700 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
|  | 384 | bool isMulticast(const ether_addr& mac) | 
|  | 385 | { | 
|  | 386 | return mac.ether_addr_octet[0] & 0b1; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | bool isUnicast(const ether_addr& mac) | 
|  | 390 | { | 
|  | 391 | return !isEmpty(mac) && !isMulticast(mac); | 
|  | 392 | } | 
|  | 393 |  | 
| Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 394 | } // namespace mac_address | 
|  | 395 | } // namespace network | 
|  | 396 | } // namespace phosphor |