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