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