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