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