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