Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Willy Tu | 13451e3 | 2023-05-24 16:08:18 -0700 | [diff] [blame] | 18 | #include "bmcweb_config.h" |
| 19 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 20 | #include "app.hpp" |
| 21 | #include "dbus_singleton.hpp" |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 22 | #include "dbus_utility.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 23 | #include "error_messages.hpp" |
| 24 | #include "health.hpp" |
Ed Tanous | 2c5875a | 2023-05-15 09:56:06 -0700 | [diff] [blame] | 25 | #include "human_sort.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 26 | #include "query.hpp" |
| 27 | #include "registries/privilege_registry.hpp" |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 28 | #include "utils/ip_utils.hpp" |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 29 | #include "utils/json_utils.hpp" |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 30 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 31 | #include <boost/algorithm/string/classification.hpp> |
| 32 | #include <boost/algorithm/string/split.hpp> |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 33 | #include <boost/url/format.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 34 | |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 35 | #include <array> |
Ed Tanous | a24526d | 2018-12-10 15:17:59 -0800 | [diff] [blame] | 36 | #include <optional> |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 37 | #include <regex> |
George Liu | 7a1dbc4 | 2022-12-07 16:03:22 +0800 | [diff] [blame] | 38 | #include <string_view> |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 39 | #include <vector> |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 40 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 41 | namespace redfish |
| 42 | { |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 43 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 44 | enum class LinkType |
| 45 | { |
| 46 | Local, |
| 47 | Global |
| 48 | }; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Structure for keeping IPv4 data required by Redfish |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 52 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 53 | struct IPv4AddressData |
| 54 | { |
| 55 | std::string id; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 56 | std::string address; |
| 57 | std::string domain; |
| 58 | std::string gateway; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 59 | std::string netmask; |
| 60 | std::string origin; |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 61 | LinkType linktype{}; |
| 62 | bool isActive{}; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 66 | * Structure for keeping IPv6 data required by Redfish |
| 67 | */ |
| 68 | struct IPv6AddressData |
| 69 | { |
| 70 | std::string id; |
| 71 | std::string address; |
| 72 | std::string origin; |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 73 | uint8_t prefixLength = 0; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 74 | }; |
| 75 | /** |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 76 | * Structure for keeping basic single Ethernet Interface information |
| 77 | * available from DBus |
| 78 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | struct EthernetInterfaceData |
| 80 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 81 | uint32_t speed; |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 82 | size_t mtuSize; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 83 | bool autoNeg; |
| 84 | bool dnsEnabled; |
| 85 | bool ntpEnabled; |
| 86 | bool hostNameEnabled; |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 87 | bool linkUp; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 88 | bool nicEnabled; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 89 | std::string dhcpEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 90 | std::string operatingMode; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 91 | std::string hostName; |
| 92 | std::string defaultGateway; |
| 93 | std::string ipv6DefaultGateway; |
| 94 | std::string macAddress; |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 95 | std::optional<uint32_t> vlanId; |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 96 | std::vector<std::string> nameServers; |
| 97 | std::vector<std::string> staticNameServers; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 98 | std::vector<std::string> domainnames; |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 101 | struct DHCPParameters |
| 102 | { |
| 103 | std::optional<bool> dhcpv4Enabled; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 104 | std::optional<bool> useDnsServers; |
| 105 | std::optional<bool> useNtpServers; |
| 106 | std::optional<bool> useDomainName; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 107 | std::optional<std::string> dhcpv6OperatingMode; |
| 108 | }; |
| 109 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 110 | // Helper function that changes bits netmask notation (i.e. /24) |
| 111 | // into full dot notation |
| 112 | inline std::string getNetmask(unsigned int bits) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 114 | uint32_t value = 0xffffffff << (32 - bits); |
| 115 | std::string netmask = std::to_string((value >> 24) & 0xff) + "." + |
| 116 | std::to_string((value >> 16) & 0xff) + "." + |
| 117 | std::to_string((value >> 8) & 0xff) + "." + |
| 118 | std::to_string(value & 0xff); |
| 119 | return netmask; |
| 120 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 121 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 122 | inline bool translateDhcpEnabledToBool(const std::string& inputDHCP, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 123 | bool isIPv4) |
| 124 | { |
| 125 | if (isIPv4) |
| 126 | { |
| 127 | return ( |
| 128 | (inputDHCP == |
| 129 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || |
| 130 | (inputDHCP == |
| 131 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); |
| 132 | } |
| 133 | return ((inputDHCP == |
| 134 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || |
| 135 | (inputDHCP == |
| 136 | "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); |
| 137 | } |
| 138 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 139 | inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 140 | { |
| 141 | if (isIPv4 && isIPv6) |
| 142 | { |
| 143 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; |
| 144 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 145 | if (isIPv4) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 146 | { |
| 147 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; |
| 148 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 149 | if (isIPv6) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 150 | { |
| 151 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; |
| 152 | } |
| 153 | return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; |
| 154 | } |
| 155 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 156 | inline std::string |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 157 | translateAddressOriginDbusToRedfish(const std::string& inputOrigin, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 158 | bool isIPv4) |
| 159 | { |
| 160 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 161 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 162 | return "Static"; |
| 163 | } |
| 164 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") |
| 165 | { |
| 166 | if (isIPv4) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 167 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 168 | return "IPv4LinkLocal"; |
| 169 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 170 | return "LinkLocal"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 171 | } |
| 172 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") |
| 173 | { |
| 174 | if (isIPv4) |
| 175 | { |
| 176 | return "DHCP"; |
| 177 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 178 | return "DHCPv6"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 179 | } |
| 180 | if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") |
| 181 | { |
| 182 | return "SLAAC"; |
| 183 | } |
| 184 | return ""; |
| 185 | } |
| 186 | |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 187 | inline bool extractEthernetInterfaceData( |
| 188 | const std::string& ethifaceId, |
| 189 | const dbus::utility::ManagedObjectType& dbusData, |
| 190 | EthernetInterfaceData& ethData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 191 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 192 | bool idFound = false; |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 193 | for (const auto& objpath : dbusData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 194 | { |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 195 | for (const auto& ifacePair : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 196 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 197 | if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 199 | idFound = true; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 200 | if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 201 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 202 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 204 | if (propertyPair.first == "MACAddress") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 206 | const std::string* mac = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 207 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 208 | if (mac != nullptr) |
| 209 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 210 | ethData.macAddress = *mac; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 211 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 212 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") |
| 216 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 217 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 218 | { |
| 219 | if (propertyPair.first == "Id") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 220 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 221 | const uint32_t* id = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 222 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 223 | if (id != nullptr) |
| 224 | { |
Jiaqing Zhao | 17e2202 | 2022-04-14 18:58:06 +0800 | [diff] [blame] | 225 | ethData.vlanId = *id; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 226 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | else if (ifacePair.first == |
| 231 | "xyz.openbmc_project.Network.EthernetInterface") |
| 232 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 233 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 234 | { |
| 235 | if (propertyPair.first == "AutoNeg") |
| 236 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 237 | const bool* autoNeg = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 238 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 239 | if (autoNeg != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 240 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 241 | ethData.autoNeg = *autoNeg; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | else if (propertyPair.first == "Speed") |
| 245 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 246 | const uint32_t* speed = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 247 | std::get_if<uint32_t>(&propertyPair.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 248 | if (speed != nullptr) |
| 249 | { |
| 250 | ethData.speed = *speed; |
| 251 | } |
| 252 | } |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 253 | else if (propertyPair.first == "MTU") |
| 254 | { |
| 255 | const uint32_t* mtuSize = |
| 256 | std::get_if<uint32_t>(&propertyPair.second); |
| 257 | if (mtuSize != nullptr) |
| 258 | { |
| 259 | ethData.mtuSize = *mtuSize; |
| 260 | } |
| 261 | } |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 262 | else if (propertyPair.first == "LinkUp") |
| 263 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 264 | const bool* linkUp = |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 265 | std::get_if<bool>(&propertyPair.second); |
| 266 | if (linkUp != nullptr) |
| 267 | { |
| 268 | ethData.linkUp = *linkUp; |
| 269 | } |
| 270 | } |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 271 | else if (propertyPair.first == "NICEnabled") |
| 272 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 273 | const bool* nicEnabled = |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 274 | std::get_if<bool>(&propertyPair.second); |
| 275 | if (nicEnabled != nullptr) |
| 276 | { |
| 277 | ethData.nicEnabled = *nicEnabled; |
| 278 | } |
| 279 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 280 | else if (propertyPair.first == "Nameservers") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 281 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 282 | const std::vector<std::string>* nameservers = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 283 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 284 | &propertyPair.second); |
| 285 | if (nameservers != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 286 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 287 | ethData.nameServers = *nameservers; |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | else if (propertyPair.first == "StaticNameServers") |
| 291 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 292 | const std::vector<std::string>* staticNameServers = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 293 | std::get_if<std::vector<std::string>>( |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 294 | &propertyPair.second); |
| 295 | if (staticNameServers != nullptr) |
| 296 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 297 | ethData.staticNameServers = *staticNameServers; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 300 | else if (propertyPair.first == "DHCPEnabled") |
| 301 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 302 | const std::string* dhcpEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 303 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 304 | if (dhcpEnabled != nullptr) |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 305 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 306 | ethData.dhcpEnabled = *dhcpEnabled; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 307 | } |
| 308 | } |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 309 | else if (propertyPair.first == "DomainName") |
| 310 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 311 | const std::vector<std::string>* domainNames = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 312 | std::get_if<std::vector<std::string>>( |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 313 | &propertyPair.second); |
| 314 | if (domainNames != nullptr) |
| 315 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 316 | ethData.domainnames = *domainNames; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 319 | else if (propertyPair.first == "DefaultGateway") |
| 320 | { |
| 321 | const std::string* defaultGateway = |
| 322 | std::get_if<std::string>(&propertyPair.second); |
| 323 | if (defaultGateway != nullptr) |
| 324 | { |
| 325 | std::string defaultGatewayStr = *defaultGateway; |
| 326 | if (defaultGatewayStr.empty()) |
| 327 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 328 | ethData.defaultGateway = "0.0.0.0"; |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 332 | ethData.defaultGateway = defaultGatewayStr; |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | } |
| 336 | else if (propertyPair.first == "DefaultGateway6") |
| 337 | { |
| 338 | const std::string* defaultGateway6 = |
| 339 | std::get_if<std::string>(&propertyPair.second); |
| 340 | if (defaultGateway6 != nullptr) |
| 341 | { |
| 342 | std::string defaultGateway6Str = |
| 343 | *defaultGateway6; |
| 344 | if (defaultGateway6Str.empty()) |
| 345 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 346 | ethData.ipv6DefaultGateway = |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 347 | "0:0:0:0:0:0:0:0"; |
| 348 | } |
| 349 | else |
| 350 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 351 | ethData.ipv6DefaultGateway = |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 352 | defaultGateway6Str; |
| 353 | } |
| 354 | } |
| 355 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 359 | |
Jian Zhang | 1e3f85e | 2022-12-13 13:50:35 +0800 | [diff] [blame] | 360 | if (objpath.first == "/xyz/openbmc_project/network/dhcp") |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 361 | { |
| 362 | if (ifacePair.first == |
| 363 | "xyz.openbmc_project.Network.DHCPConfiguration") |
| 364 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 365 | for (const auto& propertyPair : ifacePair.second) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 366 | { |
| 367 | if (propertyPair.first == "DNSEnabled") |
| 368 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 369 | const bool* dnsEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 370 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 371 | if (dnsEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 372 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 373 | ethData.dnsEnabled = *dnsEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | else if (propertyPair.first == "NTPEnabled") |
| 377 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 378 | const bool* ntpEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 379 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 380 | if (ntpEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 381 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 382 | ethData.ntpEnabled = *ntpEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | else if (propertyPair.first == "HostNameEnabled") |
| 386 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 387 | const bool* hostNameEnabled = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 388 | std::get_if<bool>(&propertyPair.second); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 389 | if (hostNameEnabled != nullptr) |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 390 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 391 | ethData.hostNameEnabled = *hostNameEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 394 | } |
| 395 | } |
| 396 | } |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 397 | // System configuration shows up in the global namespace, so no need |
| 398 | // to check eth number |
| 399 | if (ifacePair.first == |
| 400 | "xyz.openbmc_project.Network.SystemConfiguration") |
| 401 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 402 | for (const auto& propertyPair : ifacePair.second) |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 403 | { |
| 404 | if (propertyPair.first == "HostName") |
| 405 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 406 | const std::string* hostname = |
Patrick Williams | 8d78b7a | 2020-05-13 11:24:20 -0500 | [diff] [blame] | 407 | std::get_if<std::string>(&propertyPair.second); |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 408 | if (hostname != nullptr) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 409 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 410 | ethData.hostName = *hostname; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 411 | } |
| 412 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 416 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 417 | return idFound; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 420 | // Helper function that extracts data for single ethernet ipv6 address |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 421 | inline void extractIPV6Data(const std::string& ethifaceId, |
| 422 | const dbus::utility::ManagedObjectType& dbusData, |
| 423 | std::vector<IPv6AddressData>& ipv6Config) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 424 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 425 | const std::string ipPathStart = "/xyz/openbmc_project/network/" + |
| 426 | ethifaceId; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 427 | |
| 428 | // Since there might be several IPv6 configurations aligned with |
| 429 | // single ethernet interface, loop over all of them |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 430 | for (const auto& objpath : dbusData) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 431 | { |
| 432 | // Check if proper pattern for object path appears |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 433 | if (objpath.first.str.starts_with(ipPathStart + "/")) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 434 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 435 | for (const auto& interface : objpath.second) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 436 | { |
| 437 | if (interface.first == "xyz.openbmc_project.Network.IP") |
| 438 | { |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 439 | auto type = std::find_if(interface.second.begin(), |
| 440 | interface.second.end(), |
| 441 | [](const auto& property) { |
| 442 | return property.first == "Type"; |
| 443 | }); |
| 444 | if (type == interface.second.end()) |
| 445 | { |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | const std::string* typeStr = |
| 450 | std::get_if<std::string>(&type->second); |
| 451 | |
| 452 | if (typeStr == nullptr || |
| 453 | (*typeStr != |
| 454 | "xyz.openbmc_project.Network.IP.Protocol.IPv6")) |
| 455 | { |
| 456 | continue; |
| 457 | } |
| 458 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 459 | // Instance IPv6AddressData structure, and set as |
| 460 | // appropriate |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 461 | IPv6AddressData& ipv6Address = ipv6Config.emplace_back(); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 462 | ipv6Address.id = |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 463 | objpath.first.str.substr(ipPathStart.size()); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 464 | for (const auto& property : interface.second) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 465 | { |
| 466 | if (property.first == "Address") |
| 467 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 468 | const std::string* address = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 469 | std::get_if<std::string>(&property.second); |
| 470 | if (address != nullptr) |
| 471 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 472 | ipv6Address.address = *address; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | else if (property.first == "Origin") |
| 476 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 477 | const std::string* origin = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 478 | std::get_if<std::string>(&property.second); |
| 479 | if (origin != nullptr) |
| 480 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 481 | ipv6Address.origin = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 482 | translateAddressOriginDbusToRedfish(*origin, |
| 483 | false); |
| 484 | } |
| 485 | } |
| 486 | else if (property.first == "PrefixLength") |
| 487 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 488 | const uint8_t* prefix = |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 489 | std::get_if<uint8_t>(&property.second); |
| 490 | if (prefix != nullptr) |
| 491 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 492 | ipv6Address.prefixLength = *prefix; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 493 | } |
| 494 | } |
Asmitha Karunanithi | 889ff69 | 2021-11-29 08:43:30 -0600 | [diff] [blame] | 495 | else if (property.first == "Type" || |
| 496 | property.first == "Gateway") |
| 497 | { |
| 498 | // Type & Gateway is not used |
| 499 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 500 | else |
| 501 | { |
| 502 | BMCWEB_LOG_ERROR |
| 503 | << "Got extra property: " << property.first |
| 504 | << " on the " << objpath.first.str << " object"; |
| 505 | } |
| 506 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 507 | } |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 513 | // Helper function that extracts data for single ethernet ipv4 address |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 514 | inline void extractIPData(const std::string& ethifaceId, |
| 515 | const dbus::utility::ManagedObjectType& dbusData, |
| 516 | std::vector<IPv4AddressData>& ipv4Config) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 517 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 518 | const std::string ipPathStart = "/xyz/openbmc_project/network/" + |
| 519 | ethifaceId; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 520 | |
| 521 | // Since there might be several IPv4 configurations aligned with |
| 522 | // single ethernet interface, loop over all of them |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 523 | for (const auto& objpath : dbusData) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 524 | { |
| 525 | // Check if proper pattern for object path appears |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 526 | if (objpath.first.str.starts_with(ipPathStart + "/")) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 527 | { |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 528 | for (const auto& interface : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 529 | { |
| 530 | if (interface.first == "xyz.openbmc_project.Network.IP") |
| 531 | { |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 532 | auto type = std::find_if(interface.second.begin(), |
| 533 | interface.second.end(), |
| 534 | [](const auto& property) { |
| 535 | return property.first == "Type"; |
| 536 | }); |
| 537 | if (type == interface.second.end()) |
| 538 | { |
| 539 | continue; |
| 540 | } |
| 541 | |
| 542 | const std::string* typeStr = |
| 543 | std::get_if<std::string>(&type->second); |
| 544 | |
| 545 | if (typeStr == nullptr || |
| 546 | (*typeStr != |
| 547 | "xyz.openbmc_project.Network.IP.Protocol.IPv4")) |
| 548 | { |
| 549 | continue; |
| 550 | } |
| 551 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 552 | // Instance IPv4AddressData structure, and set as |
| 553 | // appropriate |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 554 | IPv4AddressData& ipv4Address = ipv4Config.emplace_back(); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 555 | ipv4Address.id = |
Tony Lee | 353163e | 2022-11-23 11:06:10 +0800 | [diff] [blame] | 556 | objpath.first.str.substr(ipPathStart.size()); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 557 | for (const auto& property : interface.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 558 | { |
| 559 | if (property.first == "Address") |
| 560 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 561 | const std::string* address = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 562 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 563 | if (address != nullptr) |
| 564 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 565 | ipv4Address.address = *address; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 568 | else if (property.first == "Origin") |
| 569 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 570 | const std::string* origin = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 571 | std::get_if<std::string>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 572 | if (origin != nullptr) |
| 573 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 574 | ipv4Address.origin = |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 575 | translateAddressOriginDbusToRedfish(*origin, |
| 576 | true); |
| 577 | } |
| 578 | } |
| 579 | else if (property.first == "PrefixLength") |
| 580 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 581 | const uint8_t* mask = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 582 | std::get_if<uint8_t>(&property.second); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 583 | if (mask != nullptr) |
| 584 | { |
| 585 | // convert it to the string |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 586 | ipv4Address.netmask = getNetmask(*mask); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
Asmitha Karunanithi | 889ff69 | 2021-11-29 08:43:30 -0600 | [diff] [blame] | 589 | else if (property.first == "Type" || |
| 590 | property.first == "Gateway") |
| 591 | { |
| 592 | // Type & Gateway is not used |
| 593 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 594 | else |
| 595 | { |
| 596 | BMCWEB_LOG_ERROR |
| 597 | << "Got extra property: " << property.first |
| 598 | << " on the " << objpath.first.str << " object"; |
| 599 | } |
| 600 | } |
| 601 | // Check if given address is local, or global |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 602 | ipv4Address.linktype = |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 603 | ipv4Address.address.starts_with("169.254.") |
Johnathan Mantey | 18659d1 | 2019-06-07 10:26:29 -0700 | [diff] [blame] | 604 | ? LinkType::Local |
| 605 | : LinkType::Global; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 613 | * @brief Deletes given IPv4 interface |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 614 | * |
| 615 | * @param[in] ifaceId Id of interface whose IP should be deleted |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 616 | * @param[in] ipHash DBus Hash id of IP that should be deleted |
| 617 | * @param[io] asyncResp Response object that will be returned to client |
| 618 | * |
| 619 | * @return None |
| 620 | */ |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 621 | inline void deleteIPAddress(const std::string& ifaceId, |
| 622 | const std::string& ipHash, |
| 623 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 624 | { |
| 625 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 626 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 627 | if (ec) |
| 628 | { |
| 629 | messages::internalError(asyncResp->res); |
| 630 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 631 | }, |
| 632 | "xyz.openbmc_project.Network", |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 633 | "/xyz/openbmc_project/network/" + ifaceId + ipHash, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 634 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 635 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | |
Gunnar Mills | 244b6d5 | 2021-04-12 15:44:23 -0500 | [diff] [blame] | 637 | inline void updateIPv4DefaultGateway( |
| 638 | const std::string& ifaceId, const std::string& gateway, |
| 639 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 640 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 641 | sdbusplus::asio::setProperty( |
| 642 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 643 | "/xyz/openbmc_project/network/" + ifaceId, |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 644 | "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 645 | gateway, [asyncResp](const boost::system::error_code& ec) { |
| 646 | if (ec) |
| 647 | { |
| 648 | messages::internalError(asyncResp->res); |
| 649 | return; |
| 650 | } |
| 651 | asyncResp->res.result(boost::beast::http::status::no_content); |
| 652 | }); |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 653 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 654 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 655 | * @brief Creates a static IPv4 entry |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 656 | * |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 657 | * @param[in] ifaceId Id of interface upon which to create the IPv4 entry |
| 658 | * @param[in] prefixLength IPv4 prefix syntax for the subnet mask |
| 659 | * @param[in] gateway IPv4 address of this interfaces gateway |
| 660 | * @param[in] address IPv4 address to assign to this interface |
| 661 | * @param[io] asyncResp Response object that will be returned to client |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 662 | * |
| 663 | * @return None |
| 664 | */ |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 665 | inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, |
| 666 | const std::string& gateway, const std::string& address, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 667 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 668 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 669 | auto createIpHandler = |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 670 | [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) { |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 671 | if (ec) |
| 672 | { |
| 673 | messages::internalError(asyncResp->res); |
| 674 | return; |
| 675 | } |
| 676 | updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); |
| 677 | }; |
| 678 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 679 | crow::connections::systemBus->async_method_call( |
Ravi Teja | 9010ec2 | 2019-08-01 23:30:25 -0500 | [diff] [blame] | 680 | std::move(createIpHandler), "xyz.openbmc_project.Network", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 681 | "/xyz/openbmc_project/network/" + ifaceId, |
| 682 | "xyz.openbmc_project.Network.IP.Create", "IP", |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 683 | "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 684 | gateway); |
| 685 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 686 | |
| 687 | /** |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 688 | * @brief Deletes the IPv6 entry for this interface and creates a replacement |
| 689 | * static IPv6 entry |
| 690 | * |
| 691 | * @param[in] ifaceId Id of interface upon which to create the IPv6 entry |
| 692 | * @param[in] id The unique hash entry identifying the DBus entry |
| 693 | * @param[in] prefixLength IPv6 prefix syntax for the subnet mask |
| 694 | * @param[in] address IPv6 address to assign to this interface |
| 695 | * @param[io] asyncResp Response object that will be returned to client |
| 696 | * |
| 697 | * @return None |
| 698 | */ |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 699 | |
| 700 | enum class IpVersion |
| 701 | { |
| 702 | IpV4, |
| 703 | IpV6 |
| 704 | }; |
| 705 | |
| 706 | inline void deleteAndCreateIPAddress( |
| 707 | IpVersion version, const std::string& ifaceId, const std::string& id, |
| 708 | uint8_t prefixLength, const std::string& address, |
| 709 | const std::string& gateway, |
| 710 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 711 | { |
| 712 | crow::connections::systemBus->async_method_call( |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 713 | [asyncResp, version, ifaceId, address, prefixLength, |
| 714 | gateway](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 715 | if (ec) |
| 716 | { |
| 717 | messages::internalError(asyncResp->res); |
| 718 | } |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 719 | std::string protocol = "xyz.openbmc_project.Network.IP.Protocol."; |
| 720 | protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 721 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 722 | [asyncResp](const boost::system::error_code& ec2) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 723 | if (ec2) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 724 | { |
| 725 | messages::internalError(asyncResp->res); |
| 726 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 727 | }, |
| 728 | "xyz.openbmc_project.Network", |
| 729 | "/xyz/openbmc_project/network/" + ifaceId, |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 730 | "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address, |
| 731 | prefixLength, gateway); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 732 | }, |
| 733 | "xyz.openbmc_project.Network", |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 734 | "/xyz/openbmc_project/network/" + ifaceId + id, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 735 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 736 | } |
| 737 | |
| 738 | /** |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 739 | * @brief Creates IPv6 with given data |
| 740 | * |
| 741 | * @param[in] ifaceId Id of interface whose IP should be added |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 742 | * @param[in] prefixLength Prefix length that needs to be added |
| 743 | * @param[in] address IP address that needs to be added |
| 744 | * @param[io] asyncResp Response object that will be returned to client |
| 745 | * |
| 746 | * @return None |
| 747 | */ |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 748 | inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, |
| 749 | const std::string& address, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 750 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 751 | { |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 752 | auto createIpHandler = [asyncResp](const boost::system::error_code& ec) { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 753 | if (ec) |
| 754 | { |
| 755 | messages::internalError(asyncResp->res); |
| 756 | } |
| 757 | }; |
| 758 | // Passing null for gateway, as per redfish spec IPv6StaticAddresses object |
Gunnar Mills | 4e0453b | 2020-07-08 14:00:30 -0500 | [diff] [blame] | 759 | // does not have associated gateway property |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 760 | crow::connections::systemBus->async_method_call( |
| 761 | std::move(createIpHandler), "xyz.openbmc_project.Network", |
| 762 | "/xyz/openbmc_project/network/" + ifaceId, |
| 763 | "xyz.openbmc_project.Network.IP.Create", "IP", |
| 764 | "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, |
| 765 | ""); |
| 766 | } |
| 767 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 768 | /** |
| 769 | * Function that retrieves all properties for given Ethernet Interface |
| 770 | * Object |
| 771 | * from EntityManager Network Manager |
| 772 | * @param ethiface_id a eth interface id to query on DBus |
| 773 | * @param callback a function that shall be called to convert Dbus output |
| 774 | * into JSON |
| 775 | */ |
| 776 | template <typename CallbackFunc> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 777 | void getEthernetIfaceData(const std::string& ethifaceId, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 778 | CallbackFunc&& callback) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 779 | { |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 780 | sdbusplus::message::object_path path("/xyz/openbmc_project/network"); |
| 781 | dbus::utility::getManagedObjects( |
| 782 | "xyz.openbmc_project.Network", path, |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 783 | [ethifaceId{std::string{ethifaceId}}, |
| 784 | callback{std::forward<CallbackFunc>(callback)}]( |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 785 | const boost::system::error_code& ec, |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 786 | const dbus::utility::ManagedObjectType& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 787 | EthernetInterfaceData ethData{}; |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 788 | std::vector<IPv4AddressData> ipv4Data; |
| 789 | std::vector<IPv6AddressData> ipv6Data; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 790 | |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 791 | if (ec) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 792 | { |
| 793 | callback(false, ethData, ipv4Data, ipv6Data); |
| 794 | return; |
| 795 | } |
| 796 | |
| 797 | bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData); |
| 798 | if (!found) |
| 799 | { |
| 800 | callback(false, ethData, ipv4Data, ipv6Data); |
| 801 | return; |
| 802 | } |
| 803 | |
| 804 | extractIPData(ethifaceId, resp, ipv4Data); |
| 805 | // Fix global GW |
| 806 | for (IPv4AddressData& ipv4 : ipv4Data) |
| 807 | { |
| 808 | if (((ipv4.linktype == LinkType::Global) && |
| 809 | (ipv4.gateway == "0.0.0.0")) || |
| 810 | (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 811 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 812 | ipv4.gateway = ethData.defaultGateway; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 813 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 814 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 815 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 816 | extractIPV6Data(ethifaceId, resp, ipv6Data); |
| 817 | // Finally make a callback with useful data |
| 818 | callback(true, ethData, ipv4Data, ipv6Data); |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 819 | }); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 820 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 821 | |
| 822 | /** |
| 823 | * Function that retrieves all Ethernet Interfaces available through Network |
| 824 | * Manager |
| 825 | * @param callback a function that shall be called to convert Dbus output |
| 826 | * into JSON. |
| 827 | */ |
| 828 | template <typename CallbackFunc> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 829 | void getEthernetIfaceList(CallbackFunc&& callback) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 830 | { |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 831 | sdbusplus::message::object_path path("/xyz/openbmc_project/network"); |
| 832 | dbus::utility::getManagedObjects( |
| 833 | "xyz.openbmc_project.Network", path, |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 834 | [callback{std::forward<CallbackFunc>(callback)}]( |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 835 | const boost::system::error_code& ec, |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 836 | const dbus::utility::ManagedObjectType& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 837 | // Callback requires vector<string> to retrieve all available |
| 838 | // ethernet interfaces |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 839 | std::vector<std::string> ifaceList; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 840 | ifaceList.reserve(resp.size()); |
Ed Tanous | 8b24275 | 2023-06-27 17:17:13 -0700 | [diff] [blame] | 841 | if (ec) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 842 | { |
| 843 | callback(false, ifaceList); |
| 844 | return; |
| 845 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 846 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 847 | // Iterate over all retrieved ObjectPaths. |
| 848 | for (const auto& objpath : resp) |
| 849 | { |
| 850 | // And all interfaces available for certain ObjectPath. |
| 851 | for (const auto& interface : objpath.second) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 852 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 853 | // If interface is |
| 854 | // xyz.openbmc_project.Network.EthernetInterface, this is |
| 855 | // what we're looking for. |
| 856 | if (interface.first == |
| 857 | "xyz.openbmc_project.Network.EthernetInterface") |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 858 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 859 | std::string ifaceId = objpath.first.filename(); |
| 860 | if (ifaceId.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 861 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 862 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 863 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 864 | // and put it into output vector. |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 865 | ifaceList.emplace_back(ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 866 | } |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 867 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 868 | } |
Ed Tanous | 2c5875a | 2023-05-15 09:56:06 -0700 | [diff] [blame] | 869 | |
| 870 | std::sort(ifaceList.begin(), ifaceList.end(), |
| 871 | AlphanumLess<std::string>()); |
| 872 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 873 | // Finally make a callback with useful data |
| 874 | callback(true, ifaceList); |
George Liu | f5892d0 | 2023-03-01 10:37:08 +0800 | [diff] [blame] | 875 | }); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 876 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 877 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 878 | inline void |
| 879 | handleHostnamePatch(const std::string& hostname, |
| 880 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 881 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 882 | // SHOULD handle host names of up to 255 characters(RFC 1123) |
| 883 | if (hostname.length() > 255) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 884 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 885 | messages::propertyValueFormatError(asyncResp->res, hostname, |
| 886 | "HostName"); |
| 887 | return; |
| 888 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 889 | sdbusplus::asio::setProperty( |
| 890 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 891 | "/xyz/openbmc_project/network/config", |
| 892 | "xyz.openbmc_project.Network.SystemConfiguration", "HostName", hostname, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 893 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 894 | if (ec) |
| 895 | { |
| 896 | messages::internalError(asyncResp->res); |
| 897 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 898 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 901 | inline void |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 902 | handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, |
| 903 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 904 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 905 | sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" + |
| 906 | ifaceId; |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 907 | sdbusplus::asio::setProperty( |
| 908 | *crow::connections::systemBus, "xyz.openbmc_project.Network", objPath, |
| 909 | "xyz.openbmc_project.Network.EthernetInterface", "MTU", mtuSize, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 910 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 911 | if (ec) |
| 912 | { |
| 913 | messages::internalError(asyncResp->res); |
| 914 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 915 | }); |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | inline void |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 919 | handleDomainnamePatch(const std::string& ifaceId, |
| 920 | const std::string& domainname, |
| 921 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 922 | { |
| 923 | std::vector<std::string> vectorDomainname = {domainname}; |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 924 | sdbusplus::asio::setProperty( |
| 925 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 926 | "/xyz/openbmc_project/network/" + ifaceId, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 927 | "xyz.openbmc_project.Network.EthernetInterface", "DomainName", |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 928 | vectorDomainname, [asyncResp](const boost::system::error_code& ec) { |
| 929 | if (ec) |
| 930 | { |
| 931 | messages::internalError(asyncResp->res); |
| 932 | } |
| 933 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 934 | } |
| 935 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 936 | inline bool isHostnameValid(const std::string& hostname) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 937 | { |
| 938 | // A valid host name can never have the dotted-decimal form (RFC 1123) |
| 939 | if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) |
| 940 | { |
| 941 | return false; |
| 942 | } |
| 943 | // Each label(hostname/subdomains) within a valid FQDN |
| 944 | // MUST handle host names of up to 63 characters (RFC 1123) |
| 945 | // labels cannot start or end with hyphens (RFC 952) |
| 946 | // labels can start with numbers (RFC 1123) |
Ed Tanous | 4b24274 | 2023-05-11 09:51:51 -0700 | [diff] [blame] | 947 | const static std::regex pattern( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 948 | "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); |
| 949 | |
| 950 | return std::regex_match(hostname, pattern); |
| 951 | } |
| 952 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 953 | inline bool isDomainnameValid(const std::string& domainname) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 954 | { |
| 955 | // Can have multiple subdomains |
| 956 | // Top Level Domain's min length is 2 character |
Ed Tanous | 4b24274 | 2023-05-11 09:51:51 -0700 | [diff] [blame] | 957 | const static std::regex pattern( |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 958 | "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 959 | |
| 960 | return std::regex_match(domainname, pattern); |
| 961 | } |
| 962 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 963 | inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, |
| 964 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 965 | { |
| 966 | // Total length of FQDN must not exceed 255 characters(RFC 1035) |
| 967 | if (fqdn.length() > 255) |
| 968 | { |
| 969 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 970 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 973 | size_t pos = fqdn.find('.'); |
| 974 | if (pos == std::string::npos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 975 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 976 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 977 | return; |
| 978 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 979 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 980 | std::string hostname; |
| 981 | std::string domainname; |
| 982 | domainname = (fqdn).substr(pos + 1); |
| 983 | hostname = (fqdn).substr(0, pos); |
| 984 | |
| 985 | if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) |
| 986 | { |
| 987 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 988 | return; |
| 989 | } |
| 990 | |
| 991 | handleHostnamePatch(hostname, asyncResp); |
| 992 | handleDomainnamePatch(ifaceId, domainname, asyncResp); |
| 993 | } |
| 994 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 995 | inline void |
| 996 | handleMACAddressPatch(const std::string& ifaceId, |
| 997 | const std::string& macAddress, |
| 998 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 999 | { |
Johnathan Mantey | 58283f4 | 2022-08-15 14:38:36 -0700 | [diff] [blame] | 1000 | static constexpr std::string_view dbusNotAllowedError = |
| 1001 | "xyz.openbmc_project.Common.Error.NotAllowed"; |
| 1002 | |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1003 | sdbusplus::asio::setProperty( |
| 1004 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 1005 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1006 | "xyz.openbmc_project.Network.MACAddress", "MACAddress", macAddress, |
| 1007 | [asyncResp](const boost::system::error_code& ec, |
| 1008 | const sdbusplus::message_t& msg) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1009 | if (ec) |
| 1010 | { |
Johnathan Mantey | 58283f4 | 2022-08-15 14:38:36 -0700 | [diff] [blame] | 1011 | const sd_bus_error* err = msg.get_error(); |
| 1012 | if (err == nullptr) |
| 1013 | { |
| 1014 | messages::internalError(asyncResp->res); |
| 1015 | return; |
| 1016 | } |
| 1017 | if (err->name == dbusNotAllowedError) |
| 1018 | { |
| 1019 | messages::propertyNotWritable(asyncResp->res, "MACAddress"); |
| 1020 | return; |
| 1021 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1022 | messages::internalError(asyncResp->res); |
| 1023 | return; |
| 1024 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1025 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1026 | } |
| 1027 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1028 | inline void setDHCPEnabled(const std::string& ifaceId, |
| 1029 | const std::string& propertyName, const bool v4Value, |
| 1030 | const bool v6Value, |
| 1031 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1032 | { |
| 1033 | const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1034 | sdbusplus::asio::setProperty( |
| 1035 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 1036 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1037 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, dhcp, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1038 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1039 | if (ec) |
| 1040 | { |
| 1041 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1042 | messages::internalError(asyncResp->res); |
| 1043 | return; |
| 1044 | } |
| 1045 | messages::success(asyncResp->res); |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1046 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1047 | } |
| 1048 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1049 | inline void setEthernetInterfaceBoolProperty( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1050 | const std::string& ifaceId, const std::string& propertyName, |
| 1051 | const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1052 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1053 | sdbusplus::asio::setProperty( |
| 1054 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 1055 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1056 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, value, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1057 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1058 | if (ec) |
| 1059 | { |
| 1060 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1061 | messages::internalError(asyncResp->res); |
| 1062 | return; |
| 1063 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1064 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1067 | inline void setDHCPv4Config(const std::string& propertyName, const bool& value, |
| 1068 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1069 | { |
| 1070 | BMCWEB_LOG_DEBUG << propertyName << " = " << value; |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1071 | sdbusplus::asio::setProperty( |
| 1072 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 1073 | "/xyz/openbmc_project/network/dhcp", |
| 1074 | "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, value, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1075 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1076 | if (ec) |
| 1077 | { |
| 1078 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1079 | messages::internalError(asyncResp->res); |
| 1080 | return; |
| 1081 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1082 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1085 | inline void handleDHCPPatch(const std::string& ifaceId, |
| 1086 | const EthernetInterfaceData& ethData, |
| 1087 | const DHCPParameters& v4dhcpParms, |
| 1088 | const DHCPParameters& v6dhcpParms, |
| 1089 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1090 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1091 | bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); |
| 1092 | bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1093 | |
| 1094 | bool nextv4DHCPState = |
| 1095 | v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; |
| 1096 | |
| 1097 | bool nextv6DHCPState{}; |
| 1098 | if (v6dhcpParms.dhcpv6OperatingMode) |
| 1099 | { |
| 1100 | if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && |
| 1101 | (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && |
| 1102 | (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) |
| 1103 | { |
| 1104 | messages::propertyValueFormatError(asyncResp->res, |
| 1105 | *v6dhcpParms.dhcpv6OperatingMode, |
| 1106 | "OperatingMode"); |
| 1107 | return; |
| 1108 | } |
| 1109 | nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); |
| 1110 | } |
| 1111 | else |
| 1112 | { |
| 1113 | nextv6DHCPState = ipv6Active; |
| 1114 | } |
| 1115 | |
| 1116 | bool nextDNS{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1117 | if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1118 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1119 | if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1120 | { |
| 1121 | messages::generalError(asyncResp->res); |
| 1122 | return; |
| 1123 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1124 | nextDNS = *v4dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1125 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1126 | else if (v4dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1127 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1128 | nextDNS = *v4dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1129 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1130 | else if (v6dhcpParms.useDnsServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1131 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1132 | nextDNS = *v6dhcpParms.useDnsServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1133 | } |
| 1134 | else |
| 1135 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1136 | nextDNS = ethData.dnsEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | bool nextNTP{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1140 | if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1141 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1142 | if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1143 | { |
| 1144 | messages::generalError(asyncResp->res); |
| 1145 | return; |
| 1146 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1147 | nextNTP = *v4dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1148 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1149 | else if (v4dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1150 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1151 | nextNTP = *v4dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1152 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1153 | else if (v6dhcpParms.useNtpServers) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1154 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1155 | nextNTP = *v6dhcpParms.useNtpServers; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1156 | } |
| 1157 | else |
| 1158 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1159 | nextNTP = ethData.ntpEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | bool nextUseDomain{}; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1163 | if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1164 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1165 | if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1166 | { |
| 1167 | messages::generalError(asyncResp->res); |
| 1168 | return; |
| 1169 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1170 | nextUseDomain = *v4dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1171 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1172 | else if (v4dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1173 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1174 | nextUseDomain = *v4dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1175 | } |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1176 | else if (v6dhcpParms.useDomainName) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1177 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1178 | nextUseDomain = *v6dhcpParms.useDomainName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1179 | } |
| 1180 | else |
| 1181 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1182 | nextUseDomain = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
| 1185 | BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; |
| 1186 | setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, |
| 1187 | asyncResp); |
| 1188 | BMCWEB_LOG_DEBUG << "set DNSEnabled..."; |
| 1189 | setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); |
| 1190 | BMCWEB_LOG_DEBUG << "set NTPEnabled..."; |
| 1191 | setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); |
| 1192 | BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; |
| 1193 | setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); |
| 1194 | } |
| 1195 | |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1196 | inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry( |
| 1197 | const std::vector<IPv4AddressData>::const_iterator& head, |
| 1198 | const std::vector<IPv4AddressData>::const_iterator& end) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1199 | { |
| 1200 | return std::find_if(head, end, [](const IPv4AddressData& value) { |
| 1201 | return value.origin == "Static"; |
| 1202 | }); |
| 1203 | } |
| 1204 | |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1205 | inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry( |
| 1206 | const std::vector<IPv6AddressData>::const_iterator& head, |
| 1207 | const std::vector<IPv6AddressData>::const_iterator& end) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1208 | { |
| 1209 | return std::find_if(head, end, [](const IPv6AddressData& value) { |
| 1210 | return value.origin == "Static"; |
| 1211 | }); |
| 1212 | } |
| 1213 | |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1214 | inline void |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 1215 | handleIPv4StaticPatch(const std::string& ifaceId, |
| 1216 | nlohmann::json::array_t& input, |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1217 | const std::vector<IPv4AddressData>& ipv4Data, |
| 1218 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1219 | { |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 1220 | if (input.empty()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1221 | { |
Ed Tanous | 2e8c4bd | 2022-06-27 12:59:12 -0700 | [diff] [blame] | 1222 | messages::propertyValueTypeError(asyncResp->res, input, |
| 1223 | "IPv4StaticAddresses"); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1224 | return; |
| 1225 | } |
| 1226 | |
| 1227 | unsigned entryIdx = 1; |
| 1228 | // Find the first static IP address currently active on the NIC and |
| 1229 | // match it to the first JSON element in the IPv4StaticAddresses array. |
| 1230 | // Match each subsequent JSON element to the next static IP programmed |
| 1231 | // into the NIC. |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1232 | std::vector<IPv4AddressData>::const_iterator nicIpEntry = |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1233 | getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); |
| 1234 | |
| 1235 | for (nlohmann::json& thisJson : input) |
| 1236 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1237 | std::string pathString = "IPv4StaticAddresses/" + |
| 1238 | std::to_string(entryIdx); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1239 | |
| 1240 | if (!thisJson.is_null() && !thisJson.empty()) |
| 1241 | { |
| 1242 | std::optional<std::string> address; |
| 1243 | std::optional<std::string> subnetMask; |
| 1244 | std::optional<std::string> gateway; |
| 1245 | |
| 1246 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1247 | address, "SubnetMask", subnetMask, |
| 1248 | "Gateway", gateway)) |
| 1249 | { |
Ed Tanous | f818b04 | 2022-06-27 13:17:35 -0700 | [diff] [blame] | 1250 | messages::propertyValueFormatError(asyncResp->res, thisJson, |
| 1251 | pathString); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | // Find the address/subnet/gateway values. Any values that are |
| 1256 | // not explicitly provided are assumed to be unmodified from the |
| 1257 | // current state of the interface. Merge existing state into the |
| 1258 | // current request. |
| 1259 | const std::string* addr = nullptr; |
| 1260 | const std::string* gw = nullptr; |
| 1261 | uint8_t prefixLength = 0; |
| 1262 | bool errorInEntry = false; |
| 1263 | if (address) |
| 1264 | { |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 1265 | if (ip_util::ipv4VerifyIpAndGetBitcount(*address)) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1266 | { |
| 1267 | addr = &(*address); |
| 1268 | } |
| 1269 | else |
| 1270 | { |
| 1271 | messages::propertyValueFormatError(asyncResp->res, *address, |
| 1272 | pathString + "/Address"); |
| 1273 | errorInEntry = true; |
| 1274 | } |
| 1275 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1276 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1277 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1278 | addr = &(nicIpEntry->address); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1279 | } |
| 1280 | else |
| 1281 | { |
| 1282 | messages::propertyMissing(asyncResp->res, |
| 1283 | pathString + "/Address"); |
| 1284 | errorInEntry = true; |
| 1285 | } |
| 1286 | |
| 1287 | if (subnetMask) |
| 1288 | { |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 1289 | if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask, |
| 1290 | &prefixLength)) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1291 | { |
| 1292 | messages::propertyValueFormatError( |
| 1293 | asyncResp->res, *subnetMask, |
| 1294 | pathString + "/SubnetMask"); |
| 1295 | errorInEntry = true; |
| 1296 | } |
| 1297 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1298 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1299 | { |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 1300 | if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, |
| 1301 | &prefixLength)) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1302 | { |
| 1303 | messages::propertyValueFormatError( |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1304 | asyncResp->res, nicIpEntry->netmask, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1305 | pathString + "/SubnetMask"); |
| 1306 | errorInEntry = true; |
| 1307 | } |
| 1308 | } |
| 1309 | else |
| 1310 | { |
| 1311 | messages::propertyMissing(asyncResp->res, |
| 1312 | pathString + "/SubnetMask"); |
| 1313 | errorInEntry = true; |
| 1314 | } |
| 1315 | |
| 1316 | if (gateway) |
| 1317 | { |
Ed Tanous | 033f1e4 | 2022-08-15 09:47:37 -0700 | [diff] [blame] | 1318 | if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway)) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1319 | { |
| 1320 | gw = &(*gateway); |
| 1321 | } |
| 1322 | else |
| 1323 | { |
| 1324 | messages::propertyValueFormatError(asyncResp->res, *gateway, |
| 1325 | pathString + "/Gateway"); |
| 1326 | errorInEntry = true; |
| 1327 | } |
| 1328 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1329 | else if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1330 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1331 | gw = &nicIpEntry->gateway; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1332 | } |
| 1333 | else |
| 1334 | { |
| 1335 | messages::propertyMissing(asyncResp->res, |
| 1336 | pathString + "/Gateway"); |
| 1337 | errorInEntry = true; |
| 1338 | } |
| 1339 | |
| 1340 | if (errorInEntry) |
| 1341 | { |
| 1342 | return; |
| 1343 | } |
| 1344 | |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1345 | if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1346 | { |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 1347 | deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId, |
| 1348 | nicIpEntry->id, prefixLength, *gw, |
| 1349 | *addr, asyncResp); |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1350 | nicIpEntry = getNextStaticIpEntry(++nicIpEntry, |
| 1351 | ipv4Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | createIPv4(ifaceId, prefixLength, *gateway, *address, |
| 1356 | asyncResp); |
| 1357 | } |
| 1358 | entryIdx++; |
| 1359 | } |
| 1360 | else |
| 1361 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1362 | if (nicIpEntry == ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1363 | { |
| 1364 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1365 | // that isn't present on the eth(n) interface. Input JSON is |
| 1366 | // in error, so bail out. |
| 1367 | if (thisJson.is_null()) |
| 1368 | { |
| 1369 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1370 | return; |
| 1371 | } |
Ed Tanous | f818b04 | 2022-06-27 13:17:35 -0700 | [diff] [blame] | 1372 | messages::propertyValueFormatError(asyncResp->res, thisJson, |
| 1373 | pathString); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1374 | return; |
| 1375 | } |
| 1376 | |
| 1377 | if (thisJson.is_null()) |
| 1378 | { |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 1379 | deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1380 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1381 | if (nicIpEntry != ipv4Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1382 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1383 | nicIpEntry = getNextStaticIpEntry(++nicIpEntry, |
| 1384 | ipv4Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1385 | } |
| 1386 | entryIdx++; |
| 1387 | } |
| 1388 | } |
| 1389 | } |
| 1390 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1391 | inline void handleStaticNameServersPatch( |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1392 | const std::string& ifaceId, |
| 1393 | const std::vector<std::string>& updatedStaticNameServers, |
| 1394 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1395 | { |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1396 | sdbusplus::asio::setProperty( |
| 1397 | *crow::connections::systemBus, "xyz.openbmc_project.Network", |
| 1398 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1399 | "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", |
| 1400 | updatedStaticNameServers, |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1401 | [asyncResp](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1402 | if (ec) |
| 1403 | { |
| 1404 | messages::internalError(asyncResp->res); |
| 1405 | return; |
| 1406 | } |
George Liu | 9ae226f | 2023-06-21 17:56:46 +0800 | [diff] [blame] | 1407 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1408 | } |
| 1409 | |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 1410 | inline void handleIPv6StaticAddressesPatch( |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 1411 | const std::string& ifaceId, const nlohmann::json::array_t& input, |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1412 | const std::vector<IPv6AddressData>& ipv6Data, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1413 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
| 1414 | { |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 1415 | if (input.empty()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1416 | { |
Ed Tanous | 2e8c4bd | 2022-06-27 12:59:12 -0700 | [diff] [blame] | 1417 | messages::propertyValueTypeError(asyncResp->res, input, |
| 1418 | "IPv6StaticAddresses"); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1419 | return; |
| 1420 | } |
| 1421 | size_t entryIdx = 1; |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1422 | std::vector<IPv6AddressData>::const_iterator nicIpEntry = |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1423 | getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); |
| 1424 | for (const nlohmann::json& thisJson : input) |
| 1425 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1426 | std::string pathString = "IPv6StaticAddresses/" + |
| 1427 | std::to_string(entryIdx); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1428 | |
| 1429 | if (!thisJson.is_null() && !thisJson.empty()) |
| 1430 | { |
| 1431 | std::optional<std::string> address; |
| 1432 | std::optional<uint8_t> prefixLength; |
| 1433 | nlohmann::json thisJsonCopy = thisJson; |
| 1434 | if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", |
| 1435 | address, "PrefixLength", prefixLength)) |
| 1436 | { |
Ed Tanous | f818b04 | 2022-06-27 13:17:35 -0700 | [diff] [blame] | 1437 | messages::propertyValueFormatError(asyncResp->res, thisJson, |
| 1438 | pathString); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1439 | return; |
| 1440 | } |
| 1441 | |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 1442 | const std::string* addr = nullptr; |
| 1443 | uint8_t prefix = 0; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1444 | |
| 1445 | // Find the address and prefixLength values. Any values that are |
| 1446 | // not explicitly provided are assumed to be unmodified from the |
| 1447 | // current state of the interface. Merge existing state into the |
| 1448 | // current request. |
| 1449 | if (address) |
| 1450 | { |
| 1451 | addr = &(*address); |
| 1452 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1453 | else if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1454 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1455 | addr = &(nicIpEntry->address); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1456 | } |
| 1457 | else |
| 1458 | { |
| 1459 | messages::propertyMissing(asyncResp->res, |
| 1460 | pathString + "/Address"); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | if (prefixLength) |
| 1465 | { |
| 1466 | prefix = *prefixLength; |
| 1467 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1468 | else if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1469 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1470 | prefix = nicIpEntry->prefixLength; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1471 | } |
| 1472 | else |
| 1473 | { |
| 1474 | messages::propertyMissing(asyncResp->res, |
| 1475 | pathString + "/PrefixLength"); |
| 1476 | return; |
| 1477 | } |
| 1478 | |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1479 | if (nicIpEntry != ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1480 | { |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 1481 | deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId, |
| 1482 | nicIpEntry->id, prefix, "", *addr, |
| 1483 | asyncResp); |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1484 | nicIpEntry = getNextStaticIpEntry(++nicIpEntry, |
| 1485 | ipv6Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | createIPv6(ifaceId, *prefixLength, *addr, asyncResp); |
| 1490 | } |
| 1491 | entryIdx++; |
| 1492 | } |
| 1493 | else |
| 1494 | { |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1495 | if (nicIpEntry == ipv6Data.end()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1496 | { |
| 1497 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1498 | // that isn't present on the eth(n) interface. Input JSON is |
| 1499 | // in error, so bail out. |
| 1500 | if (thisJson.is_null()) |
| 1501 | { |
| 1502 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1503 | return; |
| 1504 | } |
Ed Tanous | f818b04 | 2022-06-27 13:17:35 -0700 | [diff] [blame] | 1505 | messages::propertyValueFormatError(asyncResp->res, thisJson, |
| 1506 | pathString); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | if (thisJson.is_null()) |
| 1511 | { |
Ravi Teja | 9c5e585 | 2023-02-26 21:33:52 -0600 | [diff] [blame] | 1512 | deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1513 | } |
Jiaqing Zhao | 85ffe86 | 2021-12-31 15:41:59 +0800 | [diff] [blame] | 1514 | if (nicIpEntry != ipv6Data.cend()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1515 | { |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 1516 | nicIpEntry = getNextStaticIpEntry(++nicIpEntry, |
| 1517 | ipv6Data.cend()); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1518 | } |
| 1519 | entryIdx++; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |
Jiaqing Zhao | 7857cb8 | 2023-03-03 11:23:08 +0800 | [diff] [blame] | 1524 | inline std::string extractParentInterfaceName(const std::string& ifaceId) |
| 1525 | { |
| 1526 | std::size_t pos = ifaceId.find('_'); |
| 1527 | return ifaceId.substr(0, pos); |
| 1528 | } |
| 1529 | |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1530 | inline void |
| 1531 | parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1532 | const std::string& ifaceId, |
| 1533 | const EthernetInterfaceData& ethData, |
| 1534 | const std::vector<IPv4AddressData>& ipv4Data, |
| 1535 | const std::vector<IPv6AddressData>& ipv6Data) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1536 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1537 | nlohmann::json& jsonResponse = asyncResp->res.jsonValue; |
| 1538 | jsonResponse["Id"] = ifaceId; |
Ed Tanous | ef4c65b | 2023-04-24 15:28:50 -0700 | [diff] [blame] | 1539 | jsonResponse["@odata.id"] = boost::urls::format( |
| 1540 | "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1541 | jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; |
| 1542 | |
Willy Tu | 13451e3 | 2023-05-24 16:08:18 -0700 | [diff] [blame] | 1543 | if constexpr (bmcwebEnableHealthPopulate) |
| 1544 | { |
| 1545 | constexpr std::array<std::string_view, 1> inventoryForEthernet = { |
| 1546 | "xyz.openbmc_project.Inventory.Item.Ethernet"}; |
| 1547 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 1548 | dbus::utility::getSubTreePaths( |
| 1549 | "/", 0, inventoryForEthernet, |
| 1550 | [health](const boost::system::error_code& ec, |
| 1551 | const dbus::utility::MapperGetSubTreePathsResponse& resp) { |
| 1552 | if (ec) |
| 1553 | { |
| 1554 | return; |
| 1555 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1556 | |
Willy Tu | 13451e3 | 2023-05-24 16:08:18 -0700 | [diff] [blame] | 1557 | health->inventory = resp; |
| 1558 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1559 | |
Willy Tu | 13451e3 | 2023-05-24 16:08:18 -0700 | [diff] [blame] | 1560 | health->populate(); |
| 1561 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1562 | |
| 1563 | if (ethData.nicEnabled) |
| 1564 | { |
Johnathan Mantey | 0ef0e28 | 2022-11-15 12:15:02 -0800 | [diff] [blame] | 1565 | jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1566 | jsonResponse["Status"]["State"] = "Enabled"; |
| 1567 | } |
| 1568 | else |
| 1569 | { |
| 1570 | jsonResponse["LinkStatus"] = "NoLink"; |
| 1571 | jsonResponse["Status"]["State"] = "Disabled"; |
| 1572 | } |
| 1573 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1574 | jsonResponse["SpeedMbps"] = ethData.speed; |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 1575 | jsonResponse["MTUSize"] = ethData.mtuSize; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1576 | jsonResponse["MACAddress"] = ethData.macAddress; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1577 | jsonResponse["DHCPv4"]["DHCPEnabled"] = |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1578 | translateDhcpEnabledToBool(ethData.dhcpEnabled, true); |
| 1579 | jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; |
| 1580 | jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; |
| 1581 | jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1582 | |
| 1583 | jsonResponse["DHCPv6"]["OperatingMode"] = |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1584 | translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1585 | : "Disabled"; |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1586 | jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; |
| 1587 | jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; |
| 1588 | jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1589 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1590 | if (!ethData.hostName.empty()) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1591 | { |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1592 | jsonResponse["HostName"] = ethData.hostName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1593 | |
| 1594 | // When domain name is empty then it means, that it is a network |
| 1595 | // without domain names, and the host name itself must be treated as |
| 1596 | // FQDN |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1597 | std::string fqdn = ethData.hostName; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1598 | if (!ethData.domainnames.empty()) |
| 1599 | { |
| 1600 | fqdn += "." + ethData.domainnames[0]; |
| 1601 | } |
| 1602 | jsonResponse["FQDN"] = fqdn; |
| 1603 | } |
| 1604 | |
Jiaqing Zhao | 7857cb8 | 2023-03-03 11:23:08 +0800 | [diff] [blame] | 1605 | if (ethData.vlanId) |
| 1606 | { |
| 1607 | jsonResponse["EthernetInterfaceType"] = "Virtual"; |
| 1608 | jsonResponse["VLAN"]["VLANEnable"] = true; |
| 1609 | jsonResponse["VLAN"]["VLANId"] = *ethData.vlanId; |
| 1610 | jsonResponse["VLAN"]["Tagged"] = true; |
| 1611 | |
| 1612 | nlohmann::json::array_t relatedInterfaces; |
| 1613 | nlohmann::json& parentInterface = relatedInterfaces.emplace_back(); |
| 1614 | parentInterface["@odata.id"] = |
| 1615 | boost::urls::format("/redfish/v1/Managers/bmc/EthernetInterfaces", |
| 1616 | extractParentInterfaceName(ifaceId)); |
| 1617 | jsonResponse["Links"]["RelatedInterfaces"] = |
| 1618 | std::move(relatedInterfaces); |
| 1619 | } |
| 1620 | else |
| 1621 | { |
| 1622 | jsonResponse["EthernetInterfaceType"] = "Physical"; |
| 1623 | } |
| 1624 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1625 | jsonResponse["NameServers"] = ethData.nameServers; |
| 1626 | jsonResponse["StaticNameServers"] = ethData.staticNameServers; |
| 1627 | |
| 1628 | nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; |
| 1629 | nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; |
| 1630 | ipv4Array = nlohmann::json::array(); |
| 1631 | ipv4StaticArray = nlohmann::json::array(); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 1632 | for (const auto& ipv4Config : ipv4Data) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1633 | { |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1634 | std::string gatewayStr = ipv4Config.gateway; |
| 1635 | if (gatewayStr.empty()) |
| 1636 | { |
| 1637 | gatewayStr = "0.0.0.0"; |
| 1638 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1639 | nlohmann::json::object_t ipv4; |
| 1640 | ipv4["AddressOrigin"] = ipv4Config.origin; |
| 1641 | ipv4["SubnetMask"] = ipv4Config.netmask; |
| 1642 | ipv4["Address"] = ipv4Config.address; |
| 1643 | ipv4["Gateway"] = gatewayStr; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1644 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1645 | if (ipv4Config.origin == "Static") |
| 1646 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1647 | ipv4StaticArray.push_back(ipv4); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1648 | } |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1649 | |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 1650 | ipv4Array.emplace_back(std::move(ipv4)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Jiaqing Zhao | 82695a5 | 2022-04-14 15:15:59 +0800 | [diff] [blame] | 1653 | std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1654 | if (ipv6GatewayStr.empty()) |
| 1655 | { |
| 1656 | ipv6GatewayStr = "0:0:0:0:0:0:0:0"; |
| 1657 | } |
| 1658 | |
| 1659 | jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; |
| 1660 | |
| 1661 | nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; |
| 1662 | nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; |
| 1663 | ipv6Array = nlohmann::json::array(); |
| 1664 | ipv6StaticArray = nlohmann::json::array(); |
| 1665 | nlohmann::json& ipv6AddrPolicyTable = |
| 1666 | jsonResponse["IPv6AddressPolicyTable"]; |
| 1667 | ipv6AddrPolicyTable = nlohmann::json::array(); |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 1668 | for (const auto& ipv6Config : ipv6Data) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1669 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1670 | nlohmann::json::object_t ipv6; |
| 1671 | ipv6["Address"] = ipv6Config.address; |
| 1672 | ipv6["PrefixLength"] = ipv6Config.prefixLength; |
| 1673 | ipv6["AddressOrigin"] = ipv6Config.origin; |
Sunitha Harish | f836127 | 2023-03-16 03:23:59 -0500 | [diff] [blame] | 1674 | |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 1675 | ipv6Array.emplace_back(std::move(ipv6)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1676 | if (ipv6Config.origin == "Static") |
| 1677 | { |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1678 | nlohmann::json::object_t ipv6Static; |
| 1679 | ipv6Static["Address"] = ipv6Config.address; |
| 1680 | ipv6Static["PrefixLength"] = ipv6Config.prefixLength; |
Patrick Williams | b2ba307 | 2023-05-12 10:27:39 -0500 | [diff] [blame] | 1681 | ipv6StaticArray.emplace_back(std::move(ipv6Static)); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | } |
| 1685 | |
Jiaqing Zhao | e7caf25 | 2023-03-09 11:14:44 +0800 | [diff] [blame] | 1686 | inline void afterDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1687 | const std::string& ifaceId, |
| 1688 | const boost::system::error_code& ec, |
| 1689 | const sdbusplus::message_t& m) |
| 1690 | { |
| 1691 | if (!ec) |
| 1692 | { |
| 1693 | return; |
| 1694 | } |
| 1695 | const sd_bus_error* dbusError = m.get_error(); |
| 1696 | if (dbusError == nullptr) |
| 1697 | { |
| 1698 | messages::internalError(asyncResp->res); |
| 1699 | return; |
| 1700 | } |
| 1701 | BMCWEB_LOG_DEBUG << "DBus error: " << dbusError->name; |
| 1702 | |
| 1703 | if (std::string_view("org.freedesktop.DBus.Error.UnknownObject") == |
| 1704 | dbusError->name) |
| 1705 | { |
| 1706 | messages::resourceNotFound(asyncResp->res, "EthernetInterface", |
| 1707 | ifaceId); |
| 1708 | return; |
| 1709 | } |
| 1710 | if (std::string_view("org.freedesktop.DBus.Error.UnknownMethod") == |
| 1711 | dbusError->name) |
| 1712 | { |
| 1713 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1714 | return; |
| 1715 | } |
| 1716 | messages::internalError(asyncResp->res); |
| 1717 | } |
| 1718 | |
Jiaqing Zhao | b5ca3fd | 2023-03-08 15:14:58 +0800 | [diff] [blame] | 1719 | inline void afterVlanCreate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1720 | const std::string& parentInterfaceUri, |
| 1721 | const std::string& vlanInterface, |
| 1722 | const boost::system::error_code& ec, |
| 1723 | const sdbusplus::message_t& m |
| 1724 | |
| 1725 | ) |
| 1726 | { |
| 1727 | if (ec) |
| 1728 | { |
| 1729 | const sd_bus_error* dbusError = m.get_error(); |
| 1730 | if (dbusError == nullptr) |
| 1731 | { |
| 1732 | messages::internalError(asyncResp->res); |
| 1733 | return; |
| 1734 | } |
| 1735 | BMCWEB_LOG_DEBUG << "DBus error: " << dbusError->name; |
| 1736 | |
| 1737 | if (std::string_view( |
| 1738 | "xyz.openbmc_project.Common.Error.ResourceNotFound") == |
| 1739 | dbusError->name) |
| 1740 | { |
| 1741 | messages::propertyValueNotInList( |
| 1742 | asyncResp->res, parentInterfaceUri, |
| 1743 | "Links/RelatedInterfaces/0/@odata.id"); |
| 1744 | return; |
| 1745 | } |
| 1746 | if (std::string_view( |
| 1747 | "xyz.openbmc_project.Common.Error.InvalidArgument") == |
| 1748 | dbusError->name) |
| 1749 | { |
| 1750 | messages::resourceAlreadyExists(asyncResp->res, "EthernetInterface", |
| 1751 | "Id", vlanInterface); |
| 1752 | return; |
| 1753 | } |
| 1754 | messages::internalError(asyncResp->res); |
| 1755 | return; |
| 1756 | } |
| 1757 | |
| 1758 | const boost::urls::url vlanInterfaceUri = boost::urls::format( |
| 1759 | "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", vlanInterface); |
| 1760 | asyncResp->res.addHeader("Location", vlanInterfaceUri.buffer()); |
| 1761 | } |
| 1762 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1763 | inline void requestEthernetInterfacesRoutes(App& app) |
| 1764 | { |
| 1765 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1766 | .privileges(redfish::privileges::getEthernetInterfaceCollection) |
Ed Tanous | 1476687 | 2022-03-15 10:44:42 -0700 | [diff] [blame] | 1767 | .methods(boost::beast::http::verb::get)( |
| 1768 | [&app](const crow::Request& req, |
| 1769 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1770 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1771 | { |
| 1772 | return; |
| 1773 | } |
| 1774 | |
| 1775 | asyncResp->res.jsonValue["@odata.type"] = |
| 1776 | "#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
| 1777 | asyncResp->res.jsonValue["@odata.id"] = |
| 1778 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 1779 | asyncResp->res.jsonValue["Name"] = |
| 1780 | "Ethernet Network Interface Collection"; |
| 1781 | asyncResp->res.jsonValue["Description"] = |
| 1782 | "Collection of EthernetInterfaces for this Manager"; |
| 1783 | |
| 1784 | // Get eth interface list, and call the below callback for JSON |
| 1785 | // preparation |
| 1786 | getEthernetIfaceList( |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1787 | [asyncResp](const bool& success, |
| 1788 | const std::vector<std::string>& ifaceList) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1789 | if (!success) |
| 1790 | { |
| 1791 | messages::internalError(asyncResp->res); |
| 1792 | return; |
| 1793 | } |
| 1794 | |
| 1795 | nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; |
| 1796 | ifaceArray = nlohmann::json::array(); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1797 | for (const std::string& ifaceItem : ifaceList) |
| 1798 | { |
Jiaqing Zhao | 7857cb8 | 2023-03-03 11:23:08 +0800 | [diff] [blame] | 1799 | nlohmann::json::object_t iface; |
| 1800 | iface["@odata.id"] = boost::urls::format( |
| 1801 | "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", |
| 1802 | ifaceItem); |
| 1803 | ifaceArray.push_back(std::move(iface)); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1804 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1805 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1806 | asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); |
| 1807 | asyncResp->res.jsonValue["@odata.id"] = |
| 1808 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
| 1809 | }); |
| 1810 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1811 | |
Jiaqing Zhao | b5ca3fd | 2023-03-08 15:14:58 +0800 | [diff] [blame] | 1812 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
| 1813 | .privileges(redfish::privileges::postEthernetInterfaceCollection) |
| 1814 | .methods(boost::beast::http::verb::post)( |
| 1815 | [&app](const crow::Request& req, |
| 1816 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 1817 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 1818 | { |
| 1819 | return; |
| 1820 | } |
| 1821 | |
| 1822 | bool vlanEnable = false; |
| 1823 | uint32_t vlanId = 0; |
| 1824 | nlohmann::json::array_t relatedInterfaces; |
| 1825 | |
| 1826 | if (!json_util::readJsonPatch(req, asyncResp->res, "VLAN/VLANEnable", |
| 1827 | vlanEnable, "VLAN/VLANId", vlanId, |
| 1828 | "Links/RelatedInterfaces", |
| 1829 | relatedInterfaces)) |
| 1830 | { |
| 1831 | return; |
| 1832 | } |
| 1833 | |
| 1834 | if (relatedInterfaces.size() != 1) |
| 1835 | { |
| 1836 | messages::arraySizeTooLong(asyncResp->res, |
| 1837 | "Links/RelatedInterfaces", |
| 1838 | relatedInterfaces.size()); |
| 1839 | return; |
| 1840 | } |
| 1841 | |
| 1842 | std::string parentInterfaceUri; |
| 1843 | if (!json_util::readJson(relatedInterfaces[0], asyncResp->res, |
| 1844 | "@odata.id", parentInterfaceUri)) |
| 1845 | { |
| 1846 | messages::propertyMissing(asyncResp->res, |
| 1847 | "Links/RelatedInterfaces/0/@odata.id"); |
| 1848 | return; |
| 1849 | } |
| 1850 | BMCWEB_LOG_INFO << "Parent Interface URI: " << parentInterfaceUri; |
| 1851 | |
| 1852 | boost::urls::result<boost::urls::url_view> parsedUri = |
| 1853 | boost::urls::parse_relative_ref(parentInterfaceUri); |
| 1854 | if (!parsedUri) |
| 1855 | { |
| 1856 | messages::propertyValueFormatError( |
| 1857 | asyncResp->res, parentInterfaceUri, |
| 1858 | "Links/RelatedInterfaces/0/@odata.id"); |
| 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | std::string parentInterface; |
| 1863 | if (!crow::utility::readUrlSegments( |
| 1864 | *parsedUri, "redfish", "v1", "Managers", "bmc", |
| 1865 | "EthernetInterfaces", std::ref(parentInterface))) |
| 1866 | { |
| 1867 | messages::propertyValueNotInList( |
| 1868 | asyncResp->res, parentInterfaceUri, |
| 1869 | "Links/RelatedInterfaces/0/@odata.id"); |
| 1870 | return; |
| 1871 | } |
| 1872 | |
| 1873 | if (!vlanEnable) |
| 1874 | { |
| 1875 | // In OpenBMC implementation, VLANEnable cannot be false on |
| 1876 | // create |
| 1877 | messages::propertyValueIncorrect(asyncResp->res, "VLAN/VLANEnable", |
| 1878 | "false"); |
| 1879 | return; |
| 1880 | } |
| 1881 | |
| 1882 | std::string vlanInterface = parentInterface + "_" + |
| 1883 | std::to_string(vlanId); |
| 1884 | crow::connections::systemBus->async_method_call( |
| 1885 | [asyncResp, parentInterfaceUri, |
| 1886 | vlanInterface](const boost::system::error_code& ec, |
| 1887 | const sdbusplus::message_t& m) { |
| 1888 | afterVlanCreate(asyncResp, parentInterfaceUri, vlanInterface, ec, |
| 1889 | m); |
| 1890 | }, |
| 1891 | "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", |
| 1892 | "xyz.openbmc_project.Network.VLAN.Create", "VLAN", parentInterface, |
| 1893 | vlanId); |
| 1894 | }); |
| 1895 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1896 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1897 | .privileges(redfish::privileges::getEthernetInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1898 | .methods(boost::beast::http::verb::get)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1899 | [&app](const crow::Request& req, |
| 1900 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1901 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1902 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1903 | { |
| 1904 | return; |
| 1905 | } |
| 1906 | getEthernetIfaceData( |
| 1907 | ifaceId, |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 1908 | [asyncResp, ifaceId](const bool& success, |
| 1909 | const EthernetInterfaceData& ethData, |
| 1910 | const std::vector<IPv4AddressData>& ipv4Data, |
| 1911 | const std::vector<IPv6AddressData>& ipv6Data) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1912 | if (!success) |
| 1913 | { |
| 1914 | // TODO(Pawel)consider distinguish between non |
| 1915 | // existing object, and other errors |
| 1916 | messages::resourceNotFound(asyncResp->res, "EthernetInterface", |
| 1917 | ifaceId); |
| 1918 | return; |
| 1919 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1920 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1921 | asyncResp->res.jsonValue["@odata.type"] = |
Jiaqing Zhao | 93bbc95 | 2023-02-23 14:11:20 +0800 | [diff] [blame] | 1922 | "#EthernetInterface.v1_9_0.EthernetInterface"; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1923 | asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; |
| 1924 | asyncResp->res.jsonValue["Description"] = |
| 1925 | "Management Network Interface"; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1926 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1927 | parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1928 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1929 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1930 | |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1931 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1932 | .privileges(redfish::privileges::patchEthernetInterface) |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 1933 | .methods(boost::beast::http::verb::patch)( |
Ed Tanous | 45ca1b8 | 2022-03-25 13:07:27 -0700 | [diff] [blame] | 1934 | [&app](const crow::Request& req, |
| 1935 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1936 | const std::string& ifaceId) { |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 1937 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1938 | { |
| 1939 | return; |
| 1940 | } |
| 1941 | std::optional<std::string> hostname; |
| 1942 | std::optional<std::string> fqdn; |
| 1943 | std::optional<std::string> macAddress; |
| 1944 | std::optional<std::string> ipv6DefaultGateway; |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 1945 | std::optional<nlohmann::json::array_t> ipv4StaticAddresses; |
| 1946 | std::optional<nlohmann::json::array_t> ipv6StaticAddresses; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1947 | std::optional<std::vector<std::string>> staticNameServers; |
| 1948 | std::optional<nlohmann::json> dhcpv4; |
| 1949 | std::optional<nlohmann::json> dhcpv6; |
| 1950 | std::optional<bool> interfaceEnabled; |
| 1951 | std::optional<size_t> mtuSize; |
| 1952 | DHCPParameters v4dhcpParms; |
| 1953 | DHCPParameters v6dhcpParms; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1954 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1955 | if (!json_util::readJsonPatch( |
| 1956 | req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, |
| 1957 | "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", |
| 1958 | macAddress, "StaticNameServers", staticNameServers, |
| 1959 | "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", |
| 1960 | ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, |
| 1961 | "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled)) |
| 1962 | { |
| 1963 | return; |
| 1964 | } |
| 1965 | if (dhcpv4) |
| 1966 | { |
| 1967 | if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", |
| 1968 | v4dhcpParms.dhcpv4Enabled, "UseDNSServers", |
| 1969 | v4dhcpParms.useDnsServers, "UseNTPServers", |
| 1970 | v4dhcpParms.useNtpServers, "UseDomainName", |
| 1971 | v4dhcpParms.useDomainName)) |
| 1972 | { |
| 1973 | return; |
| 1974 | } |
| 1975 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1976 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1977 | if (dhcpv6) |
| 1978 | { |
| 1979 | if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", |
| 1980 | v6dhcpParms.dhcpv6OperatingMode, |
| 1981 | "UseDNSServers", v6dhcpParms.useDnsServers, |
| 1982 | "UseNTPServers", v6dhcpParms.useNtpServers, |
| 1983 | "UseDomainName", |
| 1984 | v6dhcpParms.useDomainName)) |
| 1985 | { |
| 1986 | return; |
| 1987 | } |
| 1988 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1989 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1990 | // Get single eth interface data, and call the below callback |
| 1991 | // for JSON preparation |
| 1992 | getEthernetIfaceData( |
| 1993 | ifaceId, |
| 1994 | [asyncResp, ifaceId, hostname = std::move(hostname), |
| 1995 | fqdn = std::move(fqdn), macAddress = std::move(macAddress), |
| 1996 | ipv4StaticAddresses = std::move(ipv4StaticAddresses), |
| 1997 | ipv6DefaultGateway = std::move(ipv6DefaultGateway), |
| 1998 | ipv6StaticAddresses = std::move(ipv6StaticAddresses), |
| 1999 | staticNameServers = std::move(staticNameServers), |
Ed Tanous | bc20089 | 2022-06-30 17:49:12 -0700 | [diff] [blame] | 2000 | dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize, |
| 2001 | v4dhcpParms = std::move(v4dhcpParms), |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2002 | v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( |
| 2003 | const bool& success, const EthernetInterfaceData& ethData, |
Ed Tanous | 7717953 | 2023-02-28 10:45:28 -0800 | [diff] [blame] | 2004 | const std::vector<IPv4AddressData>& ipv4Data, |
| 2005 | const std::vector<IPv6AddressData>& ipv6Data) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2006 | if (!success) |
| 2007 | { |
| 2008 | // ... otherwise return error |
| 2009 | // TODO(Pawel)consider distinguish between non |
| 2010 | // existing object, and other errors |
Jiaqing Zhao | d8a5d5d | 2022-08-05 16:21:51 +0800 | [diff] [blame] | 2011 | messages::resourceNotFound(asyncResp->res, "EthernetInterface", |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2012 | ifaceId); |
| 2013 | return; |
| 2014 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2015 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2016 | if (dhcpv4 || dhcpv6) |
| 2017 | { |
| 2018 | handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2019 | asyncResp); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2020 | } |
Tejas Patil | 35fb531 | 2021-09-20 15:35:20 +0530 | [diff] [blame] | 2021 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2022 | if (hostname) |
| 2023 | { |
| 2024 | handleHostnamePatch(*hostname, asyncResp); |
| 2025 | } |
| 2026 | |
| 2027 | if (fqdn) |
| 2028 | { |
| 2029 | handleFqdnPatch(ifaceId, *fqdn, asyncResp); |
| 2030 | } |
| 2031 | |
| 2032 | if (macAddress) |
| 2033 | { |
| 2034 | handleMACAddressPatch(ifaceId, *macAddress, asyncResp); |
| 2035 | } |
| 2036 | |
| 2037 | if (ipv4StaticAddresses) |
| 2038 | { |
| 2039 | // TODO(ed) for some reason the capture of |
| 2040 | // ipv4Addresses above is returning a const value, |
| 2041 | // not a non-const value. This doesn't really work |
| 2042 | // for us, as we need to be able to efficiently move |
| 2043 | // out the intermedia nlohmann::json objects. This |
| 2044 | // makes a copy of the structure, and operates on |
| 2045 | // that, but could be done more efficiently |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 2046 | nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2047 | handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp); |
| 2048 | } |
| 2049 | |
| 2050 | if (staticNameServers) |
| 2051 | { |
| 2052 | handleStaticNameServersPatch(ifaceId, *staticNameServers, |
| 2053 | asyncResp); |
| 2054 | } |
| 2055 | |
| 2056 | if (ipv6DefaultGateway) |
| 2057 | { |
| 2058 | messages::propertyNotWritable(asyncResp->res, |
| 2059 | "IPv6DefaultGateway"); |
| 2060 | } |
| 2061 | |
| 2062 | if (ipv6StaticAddresses) |
| 2063 | { |
Ed Tanous | ddd70dc | 2023-03-01 16:00:27 -0800 | [diff] [blame] | 2064 | handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses, |
| 2065 | ipv6Data, asyncResp); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | if (interfaceEnabled) |
| 2069 | { |
| 2070 | setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled", |
| 2071 | *interfaceEnabled, asyncResp); |
| 2072 | } |
| 2073 | |
| 2074 | if (mtuSize) |
| 2075 | { |
| 2076 | handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); |
| 2077 | } |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2078 | }); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 2079 | }); |
Jiaqing Zhao | e7caf25 | 2023-03-09 11:14:44 +0800 | [diff] [blame] | 2080 | |
| 2081 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") |
| 2082 | .privileges(redfish::privileges::deleteEthernetInterface) |
| 2083 | .methods(boost::beast::http::verb::delete_)( |
| 2084 | [&app](const crow::Request& req, |
| 2085 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2086 | const std::string& ifaceId) { |
| 2087 | if (!redfish::setUpRedfishRoute(app, req, asyncResp)) |
| 2088 | { |
| 2089 | return; |
| 2090 | } |
| 2091 | |
| 2092 | crow::connections::systemBus->async_method_call( |
| 2093 | [asyncResp, ifaceId](const boost::system::error_code& ec, |
| 2094 | const sdbusplus::message_t& m) { |
| 2095 | afterDelete(asyncResp, ifaceId, ec, m); |
| 2096 | }, |
| 2097 | "xyz.openbmc_project.Network", |
| 2098 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 2099 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2100 | }); |
Ed Tanous | bf648f7 | 2021-06-03 15:00:14 -0700 | [diff] [blame] | 2101 | } |
| 2102 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2103 | } // namespace redfish |