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 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 19 | #include <boost/container/flat_set.hpp> |
Kowalski, Kamil | 179db1d | 2018-04-23 11:12:41 +0200 | [diff] [blame] | 20 | #include <dbus_singleton.hpp> |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 21 | #include <error_messages.hpp> |
Kowalski, Kamil | 179db1d | 2018-04-23 11:12:41 +0200 | [diff] [blame] | 22 | #include <node.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 | |
| 1026 | /** |
| 1027 | * EthernetCollection derived class for delivering Ethernet Collection Schema |
| 1028 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1029 | class EthernetCollection : public Node |
| 1030 | { |
| 1031 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1032 | EthernetCollection(App& app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1033 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1034 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1035 | entityPrivileges = { |
| 1036 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1037 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1038 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1039 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1040 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1041 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
| 1042 | } |
| 1043 | |
| 1044 | private: |
| 1045 | /** |
| 1046 | * Functions triggers appropriate requests on DBus |
| 1047 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1048 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1049 | const crow::Request&, const std::vector<std::string>&) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1050 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1051 | asyncResp->res.jsonValue["@odata.type"] = |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1052 | "#EthernetInterfaceCollection.EthernetInterfaceCollection"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1053 | asyncResp->res.jsonValue["@odata.id"] = |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1054 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1055 | asyncResp->res.jsonValue["Name"] = |
| 1056 | "Ethernet Network Interface Collection"; |
| 1057 | asyncResp->res.jsonValue["Description"] = |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1058 | "Collection of EthernetInterfaces for this Manager"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1059 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1060 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1061 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1062 | getEthernetIfaceList( |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1063 | [asyncResp]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1064 | const bool& success, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1065 | const boost::container::flat_set<std::string>& ifaceList) { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1066 | if (!success) |
| 1067 | { |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1068 | messages::internalError(asyncResp->res); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1069 | return; |
| 1070 | } |
| 1071 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1072 | nlohmann::json& ifaceArray = |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1073 | asyncResp->res.jsonValue["Members"]; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1074 | ifaceArray = nlohmann::json::array(); |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1075 | std::string tag = "_"; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1076 | for (const std::string& ifaceItem : ifaceList) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1077 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1078 | std::size_t found = ifaceItem.find(tag); |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1079 | if (found == std::string::npos) |
| 1080 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1081 | ifaceArray.push_back( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1082 | {{"@odata.id", |
| 1083 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1084 | ifaceItem}}); |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1085 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1088 | asyncResp->res.jsonValue["Members@odata.count"] = |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1089 | ifaceArray.size(); |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1090 | asyncResp->res.jsonValue["@odata.id"] = |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1091 | "/redfish/v1/Managers/bmc/EthernetInterfaces"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1092 | }); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1093 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 1094 | }; |
| 1095 | |
| 1096 | /** |
| 1097 | * EthernetInterface derived class for delivering Ethernet Schema |
| 1098 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1099 | class EthernetInterface : public Node |
| 1100 | { |
| 1101 | public: |
| 1102 | /* |
| 1103 | * Default Constructor |
| 1104 | */ |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 1105 | EthernetInterface(App& app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1106 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1107 | std::string()) |
| 1108 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1109 | entityPrivileges = { |
| 1110 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1111 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1112 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 1113 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 1114 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 1115 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | 588c3f0 | 2018-04-03 14:55:27 +0200 | [diff] [blame] | 1116 | } |
| 1117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1118 | private: |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1119 | void |
| 1120 | handleHostnamePatch(const std::string& hostname, |
| 1121 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1122 | { |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1123 | // SHOULD handle host names of up to 255 characters(RFC 1123) |
| 1124 | if (hostname.length() > 255) |
| 1125 | { |
| 1126 | messages::propertyValueFormatError(asyncResp->res, hostname, |
| 1127 | "HostName"); |
| 1128 | return; |
| 1129 | } |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1130 | crow::connections::systemBus->async_method_call( |
| 1131 | [asyncResp](const boost::system::error_code ec) { |
| 1132 | if (ec) |
| 1133 | { |
| 1134 | messages::internalError(asyncResp->res); |
| 1135 | } |
| 1136 | }, |
| 1137 | "xyz.openbmc_project.Network", |
| 1138 | "/xyz/openbmc_project/network/config", |
| 1139 | "org.freedesktop.DBus.Properties", "Set", |
| 1140 | "xyz.openbmc_project.Network.SystemConfiguration", "HostName", |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 1141 | std::variant<std::string>(hostname)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1144 | void handleDomainnamePatch( |
| 1145 | const std::string& ifaceId, const std::string& domainname, |
| 1146 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1147 | { |
| 1148 | std::vector<std::string> vectorDomainname = {domainname}; |
| 1149 | crow::connections::systemBus->async_method_call( |
| 1150 | [asyncResp](const boost::system::error_code ec) { |
| 1151 | if (ec) |
| 1152 | { |
| 1153 | messages::internalError(asyncResp->res); |
| 1154 | } |
| 1155 | }, |
| 1156 | "xyz.openbmc_project.Network", |
| 1157 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1158 | "org.freedesktop.DBus.Properties", "Set", |
| 1159 | "xyz.openbmc_project.Network.EthernetInterface", "DomainName", |
| 1160 | std::variant<std::vector<std::string>>(vectorDomainname)); |
| 1161 | } |
| 1162 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1163 | void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1164 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1165 | { |
| 1166 | // Total length of FQDN must not exceed 255 characters(RFC 1035) |
| 1167 | if (fqdn.length() > 255) |
| 1168 | { |
| 1169 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | size_t pos = fqdn.find('.'); |
| 1174 | if (pos == std::string::npos) |
| 1175 | { |
| 1176 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1177 | return; |
| 1178 | } |
| 1179 | |
| 1180 | std::string hostname; |
| 1181 | std::string domainname; |
| 1182 | domainname = (fqdn).substr(pos + 1); |
| 1183 | hostname = (fqdn).substr(0, pos); |
| 1184 | |
| 1185 | if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) |
| 1186 | { |
| 1187 | messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); |
| 1188 | return; |
| 1189 | } |
| 1190 | |
| 1191 | handleHostnamePatch(hostname, asyncResp); |
| 1192 | handleDomainnamePatch(ifaceId, domainname, asyncResp); |
| 1193 | } |
| 1194 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1195 | bool isHostnameValid(const std::string& hostname) |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1196 | { |
| 1197 | // A valid host name can never have the dotted-decimal form (RFC 1123) |
| 1198 | if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) |
| 1199 | { |
| 1200 | return false; |
| 1201 | } |
| 1202 | // Each label(hostname/subdomains) within a valid FQDN |
| 1203 | // MUST handle host names of up to 63 characters (RFC 1123) |
| 1204 | // labels cannot start or end with hyphens (RFC 952) |
| 1205 | // labels can start with numbers (RFC 1123) |
| 1206 | const std::regex pattern( |
| 1207 | "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); |
| 1208 | |
| 1209 | return std::regex_match(hostname, pattern); |
| 1210 | } |
| 1211 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1212 | bool isDomainnameValid(const std::string& domainname) |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1213 | { |
| 1214 | // Can have multiple subdomains |
| 1215 | // Top Level Domain's min length is 2 character |
| 1216 | const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]" |
| 1217 | "{1,30}\\.)*[a-zA-Z]{2,}$"); |
| 1218 | |
| 1219 | return std::regex_match(domainname, pattern); |
| 1220 | } |
| 1221 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1222 | void handleMACAddressPatch( |
| 1223 | const std::string& ifaceId, const std::string& macAddress, |
| 1224 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1225 | { |
| 1226 | crow::connections::systemBus->async_method_call( |
| 1227 | [asyncResp, macAddress](const boost::system::error_code ec) { |
| 1228 | if (ec) |
| 1229 | { |
| 1230 | messages::internalError(asyncResp->res); |
| 1231 | return; |
| 1232 | } |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1233 | }, |
| 1234 | "xyz.openbmc_project.Network", |
| 1235 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1236 | "org.freedesktop.DBus.Properties", "Set", |
| 1237 | "xyz.openbmc_project.Network.MACAddress", "MACAddress", |
| 1238 | std::variant<std::string>(macAddress)); |
| 1239 | } |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 1240 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1241 | void setDHCPEnabled(const std::string& ifaceId, |
| 1242 | const std::string& propertyName, const bool v4Value, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1243 | const bool v6Value, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1244 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1245 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1246 | const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1247 | crow::connections::systemBus->async_method_call( |
| 1248 | [asyncResp](const boost::system::error_code ec) { |
| 1249 | if (ec) |
| 1250 | { |
| 1251 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1252 | messages::internalError(asyncResp->res); |
| 1253 | return; |
| 1254 | } |
Jayaprakash Mutyala | 8f7e9c1 | 2021-03-22 10:08:00 +0000 | [diff] [blame] | 1255 | messages::success(asyncResp->res); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1256 | }, |
| 1257 | "xyz.openbmc_project.Network", |
| 1258 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1259 | "org.freedesktop.DBus.Properties", "Set", |
| 1260 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1261 | std::variant<std::string>{dhcp}); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1262 | } |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1263 | |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1264 | void setEthernetInterfaceBoolProperty( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1265 | const std::string& ifaceId, const std::string& propertyName, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1266 | const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1267 | { |
| 1268 | crow::connections::systemBus->async_method_call( |
| 1269 | [asyncResp](const boost::system::error_code ec) { |
| 1270 | if (ec) |
| 1271 | { |
| 1272 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1273 | messages::internalError(asyncResp->res); |
| 1274 | return; |
| 1275 | } |
| 1276 | }, |
| 1277 | "xyz.openbmc_project.Network", |
| 1278 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1279 | "org.freedesktop.DBus.Properties", "Set", |
| 1280 | "xyz.openbmc_project.Network.EthernetInterface", propertyName, |
| 1281 | std::variant<bool>{value}); |
| 1282 | } |
| 1283 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1284 | void setDHCPv4Config(const std::string& propertyName, const bool& value, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1285 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1286 | { |
| 1287 | BMCWEB_LOG_DEBUG << propertyName << " = " << value; |
| 1288 | crow::connections::systemBus->async_method_call( |
| 1289 | [asyncResp](const boost::system::error_code ec) { |
| 1290 | if (ec) |
| 1291 | { |
| 1292 | BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; |
| 1293 | messages::internalError(asyncResp->res); |
| 1294 | return; |
| 1295 | } |
| 1296 | }, |
| 1297 | "xyz.openbmc_project.Network", |
| 1298 | "/xyz/openbmc_project/network/config/dhcp", |
| 1299 | "org.freedesktop.DBus.Properties", "Set", |
| 1300 | "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, |
| 1301 | std::variant<bool>{value}); |
| 1302 | } |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1303 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1304 | void handleDHCPPatch(const std::string& ifaceId, |
| 1305 | const EthernetInterfaceData& ethData, |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 1306 | const DHCPParameters& v4dhcpParms, |
| 1307 | const DHCPParameters& v6dhcpParms, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1308 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1309 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1310 | bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); |
| 1311 | bool ipv6Active = |
| 1312 | translateDHCPEnabledToBool(ethData.DHCPEnabled, false); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1313 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1314 | bool nextv4DHCPState = |
| 1315 | v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; |
| 1316 | |
| 1317 | bool nextv6DHCPState{}; |
| 1318 | if (v6dhcpParms.dhcpv6OperatingMode) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1319 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1320 | if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && |
| 1321 | (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && |
| 1322 | (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) |
| 1323 | { |
| 1324 | messages::propertyValueFormatError( |
| 1325 | asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode, |
| 1326 | "OperatingMode"); |
| 1327 | return; |
| 1328 | } |
| 1329 | nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); |
| 1330 | } |
| 1331 | else |
| 1332 | { |
| 1333 | nextv6DHCPState = ipv6Active; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1336 | bool nextDNS{}; |
| 1337 | if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1338 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1339 | if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) |
| 1340 | { |
| 1341 | messages::generalError(asyncResp->res); |
| 1342 | return; |
| 1343 | } |
| 1344 | nextDNS = *v4dhcpParms.useDNSServers; |
| 1345 | } |
| 1346 | else if (v4dhcpParms.useDNSServers) |
| 1347 | { |
| 1348 | nextDNS = *v4dhcpParms.useDNSServers; |
| 1349 | } |
| 1350 | else if (v6dhcpParms.useDNSServers) |
| 1351 | { |
| 1352 | nextDNS = *v6dhcpParms.useDNSServers; |
| 1353 | } |
| 1354 | else |
| 1355 | { |
| 1356 | nextDNS = ethData.DNSEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1357 | } |
| 1358 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1359 | bool nextNTP{}; |
| 1360 | if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1361 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1362 | if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) |
| 1363 | { |
| 1364 | messages::generalError(asyncResp->res); |
| 1365 | return; |
| 1366 | } |
| 1367 | nextNTP = *v4dhcpParms.useNTPServers; |
| 1368 | } |
| 1369 | else if (v4dhcpParms.useNTPServers) |
| 1370 | { |
| 1371 | nextNTP = *v4dhcpParms.useNTPServers; |
| 1372 | } |
| 1373 | else if (v6dhcpParms.useNTPServers) |
| 1374 | { |
| 1375 | nextNTP = *v6dhcpParms.useNTPServers; |
| 1376 | } |
| 1377 | else |
| 1378 | { |
| 1379 | nextNTP = ethData.NTPEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1382 | bool nextUseDomain{}; |
| 1383 | if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1384 | { |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1385 | if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) |
| 1386 | { |
| 1387 | messages::generalError(asyncResp->res); |
| 1388 | return; |
| 1389 | } |
| 1390 | nextUseDomain = *v4dhcpParms.useUseDomainName; |
| 1391 | } |
| 1392 | else if (v4dhcpParms.useUseDomainName) |
| 1393 | { |
| 1394 | nextUseDomain = *v4dhcpParms.useUseDomainName; |
| 1395 | } |
| 1396 | else if (v6dhcpParms.useUseDomainName) |
| 1397 | { |
| 1398 | nextUseDomain = *v6dhcpParms.useUseDomainName; |
| 1399 | } |
| 1400 | else |
| 1401 | { |
| 1402 | nextUseDomain = ethData.HostNameEnabled; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1405 | BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; |
| 1406 | setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, |
| 1407 | asyncResp); |
| 1408 | BMCWEB_LOG_DEBUG << "set DNSEnabled..."; |
| 1409 | setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); |
| 1410 | BMCWEB_LOG_DEBUG << "set NTPEnabled..."; |
| 1411 | setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); |
| 1412 | BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; |
| 1413 | setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1414 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1415 | |
| 1416 | boost::container::flat_set<IPv4AddressData>::const_iterator |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1417 | getNextStaticIpEntry( |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 1418 | const boost::container::flat_set<IPv4AddressData>::const_iterator& |
| 1419 | head, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1420 | const boost::container::flat_set<IPv4AddressData>::const_iterator& |
| 1421 | end) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1422 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 1423 | return std::find_if(head, end, [](const IPv4AddressData& value) { |
| 1424 | return value.origin == "Static"; |
| 1425 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | boost::container::flat_set<IPv6AddressData>::const_iterator |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1429 | getNextStaticIpEntry( |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 1430 | const boost::container::flat_set<IPv6AddressData>::const_iterator& |
| 1431 | head, |
Ed Tanous | b5a7693 | 2020-09-29 16:16:58 -0700 | [diff] [blame] | 1432 | const boost::container::flat_set<IPv6AddressData>::const_iterator& |
| 1433 | end) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1434 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 1435 | return std::find_if(head, end, [](const IPv6AddressData& value) { |
| 1436 | return value.origin == "Static"; |
| 1437 | }); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1440 | void handleIPv4StaticPatch( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1441 | const std::string& ifaceId, nlohmann::json& input, |
| 1442 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1443 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1444 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1445 | if ((!input.is_array()) || input.empty()) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1446 | { |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1447 | messages::propertyValueTypeError( |
| 1448 | asyncResp->res, |
| 1449 | input.dump(2, ' ', true, |
| 1450 | nlohmann::json::error_handler_t::replace), |
| 1451 | "IPv4StaticAddresses"); |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1452 | return; |
| 1453 | } |
| 1454 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1455 | unsigned entryIdx = 1; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1456 | // Find the first static IP address currently active on the NIC and |
| 1457 | // match it to the first JSON element in the IPv4StaticAddresses array. |
| 1458 | // Match each subsequent JSON element to the next static IP programmed |
| 1459 | // into the NIC. |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1460 | boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry = |
| 1461 | getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1462 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1463 | for (nlohmann::json& thisJson : input) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1464 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1465 | std::string pathString = |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1466 | "IPv4StaticAddresses/" + std::to_string(entryIdx); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1467 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1468 | if (!thisJson.is_null() && !thisJson.empty()) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1469 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1470 | std::optional<std::string> address; |
| 1471 | std::optional<std::string> subnetMask; |
| 1472 | std::optional<std::string> gateway; |
| 1473 | |
| 1474 | if (!json_util::readJson(thisJson, asyncResp->res, "Address", |
| 1475 | address, "SubnetMask", subnetMask, |
| 1476 | "Gateway", gateway)) |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1477 | { |
| 1478 | messages::propertyValueFormatError( |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1479 | asyncResp->res, |
| 1480 | thisJson.dump(2, ' ', true, |
| 1481 | nlohmann::json::error_handler_t::replace), |
| 1482 | pathString); |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1483 | return; |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1484 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1485 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1486 | // Find the address/subnet/gateway values. Any values that are |
| 1487 | // not explicitly provided are assumed to be unmodified from the |
| 1488 | // current state of the interface. Merge existing state into the |
| 1489 | // current request. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1490 | const std::string* addr = nullptr; |
| 1491 | const std::string* gw = nullptr; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1492 | uint8_t prefixLength = 0; |
| 1493 | bool errorInEntry = false; |
| 1494 | if (address) |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 1495 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1496 | if (ipv4VerifyIpAndGetBitcount(*address)) |
| 1497 | { |
| 1498 | addr = &(*address); |
| 1499 | } |
| 1500 | else |
| 1501 | { |
| 1502 | messages::propertyValueFormatError( |
| 1503 | asyncResp->res, *address, pathString + "/Address"); |
| 1504 | errorInEntry = true; |
| 1505 | } |
| 1506 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1507 | else if (niciPentry != ipv4Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1508 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1509 | addr = &(niciPentry->address); |
Ratan Gupta | 9474b37 | 2019-03-01 15:13:37 +0530 | [diff] [blame] | 1510 | } |
| 1511 | else |
| 1512 | { |
| 1513 | messages::propertyMissing(asyncResp->res, |
| 1514 | pathString + "/Address"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1515 | errorInEntry = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1516 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1517 | |
| 1518 | if (subnetMask) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1519 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1520 | if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) |
| 1521 | { |
| 1522 | messages::propertyValueFormatError( |
| 1523 | asyncResp->res, *subnetMask, |
| 1524 | pathString + "/SubnetMask"); |
| 1525 | errorInEntry = true; |
| 1526 | } |
| 1527 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1528 | else if (niciPentry != ipv4Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1529 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1530 | if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1531 | &prefixLength)) |
| 1532 | { |
| 1533 | messages::propertyValueFormatError( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1534 | asyncResp->res, niciPentry->netmask, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1535 | pathString + "/SubnetMask"); |
| 1536 | errorInEntry = true; |
| 1537 | } |
| 1538 | } |
| 1539 | else |
| 1540 | { |
| 1541 | messages::propertyMissing(asyncResp->res, |
| 1542 | pathString + "/SubnetMask"); |
| 1543 | errorInEntry = true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1544 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1545 | |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1546 | if (gateway) |
| 1547 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1548 | if (ipv4VerifyIpAndGetBitcount(*gateway)) |
| 1549 | { |
| 1550 | gw = &(*gateway); |
| 1551 | } |
| 1552 | else |
| 1553 | { |
| 1554 | messages::propertyValueFormatError( |
| 1555 | asyncResp->res, *gateway, pathString + "/Gateway"); |
| 1556 | errorInEntry = true; |
| 1557 | } |
Ratan Gupta | f476acb | 2019-03-02 16:46:57 +0530 | [diff] [blame] | 1558 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1559 | else if (niciPentry != ipv4Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1560 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1561 | gw = &niciPentry->gateway; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1562 | } |
| 1563 | else |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1564 | { |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 1565 | messages::propertyMissing(asyncResp->res, |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1566 | pathString + "/Gateway"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1567 | errorInEntry = true; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1568 | } |
| 1569 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1570 | if (errorInEntry) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1571 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1572 | return; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1573 | } |
| 1574 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1575 | if (niciPentry != ipv4Data.cend()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1576 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1577 | deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1578 | *gw, *addr, asyncResp); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1579 | niciPentry = |
| 1580 | getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1581 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1582 | else |
| 1583 | { |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 1584 | createIPv4(ifaceId, prefixLength, *gateway, *address, |
| 1585 | asyncResp); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1586 | } |
| 1587 | entryIdx++; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1588 | } |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1589 | else |
| 1590 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1591 | if (niciPentry == ipv4Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1592 | { |
| 1593 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1594 | // that isn't present on the eth(n) interface. Input JSON is |
| 1595 | // in error, so bail out. |
| 1596 | if (thisJson.is_null()) |
| 1597 | { |
| 1598 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1599 | return; |
| 1600 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1601 | messages::propertyValueFormatError( |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1602 | asyncResp->res, |
| 1603 | thisJson.dump(2, ' ', true, |
| 1604 | nlohmann::json::error_handler_t::replace), |
| 1605 | pathString); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1606 | return; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | if (thisJson.is_null()) |
| 1610 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1611 | deleteIPv4(ifaceId, niciPentry->id, asyncResp); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1612 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1613 | if (niciPentry != ipv4Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1614 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1615 | niciPentry = |
| 1616 | getNextStaticIpEntry(++niciPentry, ipv4Data.cend()); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1617 | } |
| 1618 | entryIdx++; |
| 1619 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1620 | } |
| 1621 | } |
| 1622 | |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1623 | void handleStaticNameServersPatch( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1624 | const std::string& ifaceId, |
| 1625 | const std::vector<std::string>& updatedStaticNameServers, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1626 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1627 | { |
| 1628 | crow::connections::systemBus->async_method_call( |
Johnathan Mantey | 286b911 | 2019-06-10 13:38:04 -0700 | [diff] [blame] | 1629 | [asyncResp](const boost::system::error_code ec) { |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1630 | if (ec) |
| 1631 | { |
| 1632 | messages::internalError(asyncResp->res); |
| 1633 | return; |
| 1634 | } |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1635 | }, |
| 1636 | "xyz.openbmc_project.Network", |
| 1637 | "/xyz/openbmc_project/network/" + ifaceId, |
| 1638 | "org.freedesktop.DBus.Properties", "Set", |
manojkiran.eda@gmail.com | 0f6efdc | 2019-10-03 04:53:44 -0500 | [diff] [blame] | 1639 | "xyz.openbmc_project.Network.EthernetInterface", |
| 1640 | "StaticNameServers", |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1641 | std::variant<std::vector<std::string>>{updatedStaticNameServers}); |
| 1642 | } |
| 1643 | |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1644 | void handleIPv6StaticAddressesPatch( |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 1645 | const std::string& ifaceId, const nlohmann::json& input, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1646 | const boost::container::flat_set<IPv6AddressData>& ipv6Data, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1647 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1648 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1649 | if (!input.is_array() || input.empty()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1650 | { |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1651 | messages::propertyValueTypeError( |
| 1652 | asyncResp->res, |
| 1653 | input.dump(2, ' ', true, |
| 1654 | nlohmann::json::error_handler_t::replace), |
| 1655 | "IPv6StaticAddresses"); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1656 | return; |
| 1657 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1658 | size_t entryIdx = 1; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1659 | boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry = |
| 1660 | getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 1661 | for (const nlohmann::json& thisJson : input) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1662 | { |
| 1663 | std::string pathString = |
| 1664 | "IPv6StaticAddresses/" + std::to_string(entryIdx); |
| 1665 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1666 | if (!thisJson.is_null() && !thisJson.empty()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1667 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1668 | std::optional<std::string> address; |
| 1669 | std::optional<uint8_t> prefixLength; |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 1670 | nlohmann::json thisJsonCopy = thisJson; |
| 1671 | if (!json_util::readJson(thisJsonCopy, asyncResp->res, |
| 1672 | "Address", address, "PrefixLength", |
| 1673 | prefixLength)) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1674 | { |
| 1675 | messages::propertyValueFormatError( |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1676 | asyncResp->res, |
| 1677 | thisJson.dump(2, ' ', true, |
| 1678 | nlohmann::json::error_handler_t::replace), |
| 1679 | pathString); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1680 | return; |
| 1681 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1682 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1683 | const std::string* addr; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1684 | uint8_t prefix; |
| 1685 | |
| 1686 | // Find the address and prefixLength values. Any values that are |
| 1687 | // not explicitly provided are assumed to be unmodified from the |
| 1688 | // current state of the interface. Merge existing state into the |
| 1689 | // current request. |
| 1690 | if (address) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1691 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1692 | addr = &(*address); |
| 1693 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1694 | else if (niciPentry != ipv6Data.end()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1695 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1696 | addr = &(niciPentry->address); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1697 | } |
| 1698 | else |
| 1699 | { |
| 1700 | messages::propertyMissing(asyncResp->res, |
| 1701 | pathString + "/Address"); |
| 1702 | return; |
| 1703 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1704 | |
| 1705 | if (prefixLength) |
| 1706 | { |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1707 | prefix = *prefixLength; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1708 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1709 | else if (niciPentry != ipv6Data.end()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1710 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1711 | prefix = niciPentry->prefixLength; |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1712 | } |
| 1713 | else |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1714 | { |
| 1715 | messages::propertyMissing(asyncResp->res, |
| 1716 | pathString + "/PrefixLength"); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1717 | return; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1718 | } |
| 1719 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1720 | if (niciPentry != ipv6Data.end()) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1721 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1722 | deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1723 | asyncResp); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1724 | niciPentry = |
| 1725 | getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1726 | } |
| 1727 | else |
| 1728 | { |
| 1729 | createIPv6(ifaceId, *prefixLength, *addr, asyncResp); |
| 1730 | } |
| 1731 | entryIdx++; |
| 1732 | } |
| 1733 | else |
| 1734 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1735 | if (niciPentry == ipv6Data.end()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1736 | { |
| 1737 | // Requesting a DELETE/DO NOT MODIFY action for an item |
| 1738 | // that isn't present on the eth(n) interface. Input JSON is |
| 1739 | // in error, so bail out. |
| 1740 | if (thisJson.is_null()) |
| 1741 | { |
| 1742 | messages::resourceCannotBeDeleted(asyncResp->res); |
| 1743 | return; |
| 1744 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1745 | messages::propertyValueFormatError( |
Ed Tanous | 71f52d9 | 2021-02-19 08:51:17 -0800 | [diff] [blame] | 1746 | asyncResp->res, |
| 1747 | thisJson.dump(2, ' ', true, |
| 1748 | nlohmann::json::error_handler_t::replace), |
| 1749 | pathString); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1750 | return; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1751 | } |
| 1752 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1753 | if (thisJson.is_null()) |
| 1754 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1755 | deleteIPv6(ifaceId, niciPentry->id, asyncResp); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1756 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1757 | if (niciPentry != ipv6Data.cend()) |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1758 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1759 | niciPentry = |
| 1760 | getNextStaticIpEntry(++niciPentry, ipv6Data.cend()); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1761 | } |
| 1762 | entryIdx++; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1763 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1767 | void parseInterfaceData( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1768 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1769 | const std::string& ifaceId, const EthernetInterfaceData& ethData, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1770 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 1771 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1772 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1773 | constexpr const std::array<const char*, 1> inventoryForEthernet = { |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1774 | "xyz.openbmc_project.Inventory.Item.Ethernet"}; |
| 1775 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1776 | nlohmann::json& jsonResponse = asyncResp->res.jsonValue; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1777 | jsonResponse["Id"] = ifaceId; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1778 | jsonResponse["@odata.id"] = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1779 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1780 | jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1781 | |
| 1782 | auto health = std::make_shared<HealthPopulate>(asyncResp); |
| 1783 | |
| 1784 | crow::connections::systemBus->async_method_call( |
| 1785 | [health](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1786 | std::vector<std::string>& resp) { |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1787 | if (ec) |
| 1788 | { |
| 1789 | return; |
| 1790 | } |
| 1791 | |
| 1792 | health->inventory = std::move(resp); |
| 1793 | }, |
| 1794 | "xyz.openbmc_project.ObjectMapper", |
| 1795 | "/xyz/openbmc_project/object_mapper", |
| 1796 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", |
| 1797 | int32_t(0), inventoryForEthernet); |
| 1798 | |
| 1799 | health->populate(); |
| 1800 | |
| 1801 | if (ethData.nicEnabled) |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1802 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1803 | jsonResponse["LinkStatus"] = "LinkUp"; |
| 1804 | jsonResponse["Status"]["State"] = "Enabled"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1805 | } |
| 1806 | else |
| 1807 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1808 | jsonResponse["LinkStatus"] = "NoLink"; |
| 1809 | jsonResponse["Status"]["State"] = "Disabled"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 1810 | } |
Johnathan Mantey | aa05fb2 | 2020-01-08 12:08:44 -0800 | [diff] [blame] | 1811 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1812 | jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; |
| 1813 | jsonResponse["SpeedMbps"] = ethData.speed; |
| 1814 | jsonResponse["MACAddress"] = ethData.mac_address; |
| 1815 | jsonResponse["DHCPv4"]["DHCPEnabled"] = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1816 | translateDHCPEnabledToBool(ethData.DHCPEnabled, true); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1817 | jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; |
| 1818 | jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; |
| 1819 | jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1820 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1821 | jsonResponse["DHCPv6"]["OperatingMode"] = |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1822 | translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" |
| 1823 | : "Disabled"; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1824 | jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; |
| 1825 | jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; |
| 1826 | jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; |
manojkiraneda | 2a13328 | 2019-02-19 13:09:43 +0530 | [diff] [blame] | 1827 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1828 | if (!ethData.hostname.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1829 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1830 | jsonResponse["HostName"] = ethData.hostname; |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1831 | |
| 1832 | // When domain name is empty then it means, that it is a network |
| 1833 | // without domain names, and the host name itself must be treated as |
| 1834 | // FQDN |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 1835 | std::string fqdn = ethData.hostname; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 1836 | if (!ethData.domainnames.empty()) |
| 1837 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1838 | fqdn += "." + ethData.domainnames[0]; |
Jennifer Lee | d24bfc7 | 2019-03-05 13:03:37 -0800 | [diff] [blame] | 1839 | } |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1840 | jsonResponse["FQDN"] = fqdn; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1843 | jsonResponse["VLANs"] = { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1844 | {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 1845 | ifaceId + "/VLANs"}}; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1846 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1847 | jsonResponse["NameServers"] = ethData.nameServers; |
| 1848 | jsonResponse["StaticNameServers"] = ethData.staticNameServers; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1849 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1850 | nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; |
| 1851 | nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; |
| 1852 | ipv4Array = nlohmann::json::array(); |
| 1853 | ipv4StaticArray = nlohmann::json::array(); |
| 1854 | for (auto& ipv4Config : ipv4Data) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1855 | { |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1856 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1857 | std::string gatewayStr = ipv4Config.gateway; |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1858 | if (gatewayStr.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1859 | { |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1860 | gatewayStr = "0.0.0.0"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1861 | } |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1862 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1863 | ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, |
| 1864 | {"SubnetMask", ipv4Config.netmask}, |
| 1865 | {"Address", ipv4Config.address}, |
| 1866 | {"Gateway", gatewayStr}}); |
| 1867 | if (ipv4Config.origin == "Static") |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1868 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1869 | ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, |
| 1870 | {"SubnetMask", ipv4Config.netmask}, |
| 1871 | {"Address", ipv4Config.address}, |
| 1872 | {"Gateway", gatewayStr}}); |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1873 | } |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1874 | } |
| 1875 | |
Ravi Teja | 7ea79e5 | 2021-03-02 08:38:05 +0530 | [diff] [blame] | 1876 | std::string ipv6GatewayStr = ethData.ipv6_default_gateway; |
| 1877 | if (ipv6GatewayStr.empty()) |
| 1878 | { |
| 1879 | ipv6GatewayStr = "0:0:0:0:0:0:0:0"; |
| 1880 | } |
| 1881 | |
| 1882 | jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1883 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1884 | nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; |
| 1885 | nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; |
| 1886 | ipv6Array = nlohmann::json::array(); |
| 1887 | ipv6StaticArray = nlohmann::json::array(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1888 | nlohmann::json& ipv6AddrPolicyTable = |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1889 | jsonResponse["IPv6AddressPolicyTable"]; |
Johnathan Mantey | 7f2e23e | 2020-05-14 13:36:52 -0700 | [diff] [blame] | 1890 | ipv6AddrPolicyTable = nlohmann::json::array(); |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1891 | for (auto& ipv6Config : ipv6Data) |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1892 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1893 | ipv6Array.push_back({{"Address", ipv6Config.address}, |
| 1894 | {"PrefixLength", ipv6Config.prefixLength}, |
| 1895 | {"AddressOrigin", ipv6Config.origin}, |
| 1896 | {"AddressState", nullptr}}); |
| 1897 | if (ipv6Config.origin == "Static") |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1898 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1899 | ipv6StaticArray.push_back( |
| 1900 | {{"Address", ipv6Config.address}, |
| 1901 | {"PrefixLength", ipv6Config.prefixLength}, |
| 1902 | {"AddressOrigin", ipv6Config.origin}, |
Johnathan Mantey | 5fd16e4 | 2020-05-13 15:14:47 -0700 | [diff] [blame] | 1903 | {"AddressState", nullptr}}); |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1904 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1905 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1906 | } |
| 1907 | |
| 1908 | /** |
| 1909 | * Functions triggers appropriate requests on DBus |
| 1910 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1911 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1912 | const crow::Request&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1913 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1914 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1915 | if (params.size() != 1) |
| 1916 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1917 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1918 | return; |
| 1919 | } |
| 1920 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1921 | getEthernetIfaceData( |
| 1922 | params[0], |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1923 | [this, asyncResp, ifaceId{std::string(params[0])}]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1924 | const bool& success, const EthernetInterfaceData& ethData, |
| 1925 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 1926 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1927 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1928 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1929 | // TODO(Pawel)consider distinguish between non existing |
| 1930 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1931 | messages::resourceNotFound(asyncResp->res, |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1932 | "EthernetInterface", ifaceId); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 1933 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1934 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 1935 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1936 | asyncResp->res.jsonValue["@odata.type"] = |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 1937 | "#EthernetInterface.v1_4_1.EthernetInterface"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1938 | asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; |
| 1939 | asyncResp->res.jsonValue["Description"] = |
| 1940 | "Management Network Interface"; |
| 1941 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1942 | parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1943 | ipv6Data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1944 | }); |
| 1945 | } |
| 1946 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1947 | void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1948 | const crow::Request& req, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1949 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1950 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1951 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1952 | if (params.size() != 1) |
| 1953 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1954 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1955 | return; |
| 1956 | } |
| 1957 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1958 | const std::string& ifaceId = params[0]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1959 | |
Ed Tanous | bc0bd6e | 2018-12-10 14:07:55 -0800 | [diff] [blame] | 1960 | std::optional<std::string> hostname; |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1961 | std::optional<std::string> fqdn; |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 1962 | std::optional<std::string> macAddress; |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 1963 | std::optional<std::string> ipv6DefaultGateway; |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 1964 | std::optional<nlohmann::json> ipv4StaticAddresses; |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 1965 | std::optional<nlohmann::json> ipv6StaticAddresses; |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 1966 | std::optional<std::vector<std::string>> staticNameServers; |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1967 | std::optional<nlohmann::json> dhcpv4; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1968 | std::optional<nlohmann::json> dhcpv6; |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 1969 | std::optional<bool> interfaceEnabled; |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1970 | DHCPParameters v4dhcpParms; |
| 1971 | DHCPParameters v6dhcpParms; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1972 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1973 | if (!json_util::readJson( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1974 | req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 1975 | "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", |
| 1976 | macAddress, "StaticNameServers", staticNameServers, |
| 1977 | "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", |
| 1978 | ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, |
| 1979 | "InterfaceEnabled", interfaceEnabled)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1980 | { |
| 1981 | return; |
| 1982 | } |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 1983 | if (dhcpv4) |
| 1984 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1985 | if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1986 | v4dhcpParms.dhcpv4Enabled, "UseDNSServers", |
| 1987 | v4dhcpParms.useDNSServers, "UseNTPServers", |
| 1988 | v4dhcpParms.useNTPServers, "UseDomainName", |
| 1989 | v4dhcpParms.useUseDomainName)) |
| 1990 | { |
| 1991 | return; |
| 1992 | } |
| 1993 | } |
| 1994 | |
| 1995 | if (dhcpv6) |
| 1996 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1997 | if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 1998 | v6dhcpParms.dhcpv6OperatingMode, |
| 1999 | "UseDNSServers", v6dhcpParms.useDNSServers, |
| 2000 | "UseNTPServers", v6dhcpParms.useNTPServers, |
| 2001 | "UseDomainName", |
| 2002 | v6dhcpParms.useUseDomainName)) |
| 2003 | { |
| 2004 | return; |
| 2005 | } |
Jennifer Lee | da131a9 | 2019-04-24 15:13:55 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2008 | // Get single eth interface data, and call the below callback for |
| 2009 | // JSON preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2010 | getEthernetIfaceData( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2011 | ifaceId, |
| 2012 | [this, asyncResp, ifaceId, hostname = std::move(hostname), |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 2013 | fqdn = std::move(fqdn), macAddress = std::move(macAddress), |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2014 | ipv4StaticAddresses = std::move(ipv4StaticAddresses), |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 2015 | ipv6DefaultGateway = std::move(ipv6DefaultGateway), |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2016 | ipv6StaticAddresses = std::move(ipv6StaticAddresses), |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2017 | staticNameServers = std::move(staticNameServers), |
| 2018 | dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), |
| 2019 | v4dhcpParms = std::move(v4dhcpParms), |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 2020 | v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2021 | const bool& success, const EthernetInterfaceData& ethData, |
| 2022 | const boost::container::flat_set<IPv4AddressData>& ipv4Data, |
| 2023 | const boost::container::flat_set<IPv6AddressData>& ipv6Data) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2024 | if (!success) |
| 2025 | { |
| 2026 | // ... otherwise return error |
| 2027 | // TODO(Pawel)consider distinguish between non existing |
| 2028 | // object, and other errors |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2029 | messages::resourceNotFound(asyncResp->res, |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2030 | "Ethernet Interface", ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2031 | return; |
| 2032 | } |
| 2033 | |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2034 | if (dhcpv4 || dhcpv6) |
| 2035 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 2036 | handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, |
| 2037 | asyncResp); |
Johnathan Mantey | 1f8c7b5 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2038 | } |
| 2039 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2040 | if (hostname) |
| 2041 | { |
| 2042 | handleHostnamePatch(*hostname, asyncResp); |
| 2043 | } |
| 2044 | |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 2045 | if (fqdn) |
| 2046 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2047 | handleFqdnPatch(ifaceId, *fqdn, asyncResp); |
Joshi-Mansi | ab6554f | 2020-03-10 18:33:36 +0530 | [diff] [blame] | 2048 | } |
| 2049 | |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 2050 | if (macAddress) |
| 2051 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2052 | handleMACAddressPatch(ifaceId, *macAddress, asyncResp); |
Ratan Gupta | d577665 | 2019-03-03 08:47:22 +0530 | [diff] [blame] | 2053 | } |
| 2054 | |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2055 | if (ipv4StaticAddresses) |
| 2056 | { |
Ed Tanous | 537174c | 2018-12-10 15:09:31 -0800 | [diff] [blame] | 2057 | // TODO(ed) for some reason the capture of ipv4Addresses |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2058 | // above is returning a const value, not a non-const |
| 2059 | // value. This doesn't really work for us, as we need to |
| 2060 | // be able to efficiently move out the intermedia |
| 2061 | // nlohmann::json objects. This makes a copy of the |
| 2062 | // structure, and operates on that, but could be done |
| 2063 | // more efficiently |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 2064 | nlohmann::json ipv4Static = *ipv4StaticAddresses; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2065 | handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, |
Ravi Teja | d1d5081 | 2019-06-23 16:20:27 -0500 | [diff] [blame] | 2066 | asyncResp); |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 2069 | if (staticNameServers) |
| 2070 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2071 | handleStaticNameServersPatch(ifaceId, *staticNameServers, |
RAJESWARAN THILLAIGOVINDAN | f85837b | 2019-04-04 05:18:53 -0500 | [diff] [blame] | 2072 | asyncResp); |
| 2073 | } |
Ravi Teja | 9a6fc6f | 2019-04-16 02:43:13 -0500 | [diff] [blame] | 2074 | |
| 2075 | if (ipv6DefaultGateway) |
| 2076 | { |
| 2077 | messages::propertyNotWritable(asyncResp->res, |
| 2078 | "IPv6DefaultGateway"); |
| 2079 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2080 | |
| 2081 | if (ipv6StaticAddresses) |
| 2082 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 2083 | nlohmann::json ipv6Static = *ipv6StaticAddresses; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2084 | handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2085 | ipv6Data, asyncResp); |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2086 | } |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 2087 | |
| 2088 | if (interfaceEnabled) |
| 2089 | { |
| 2090 | setEthernetInterfaceBoolProperty( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2091 | ifaceId, "NICEnabled", *interfaceEnabled, asyncResp); |
Johnathan Mantey | eeedda2 | 2019-10-29 16:09:52 -0700 | [diff] [blame] | 2092 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2093 | }); |
| 2094 | } |
Rapkiewicz, Pawel | 9391bb9 | 2018-03-20 03:12:18 +0100 | [diff] [blame] | 2095 | }; |
| 2096 | |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2097 | /** |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2098 | * VlanNetworkInterface derived class for delivering VLANNetworkInterface |
| 2099 | * Schema |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2100 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2101 | class VlanNetworkInterface : public Node |
| 2102 | { |
| 2103 | public: |
| 2104 | /* |
| 2105 | * Default Constructor |
| 2106 | */ |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 2107 | VlanNetworkInterface(App& app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2108 | Node(app, |
Gunnar Mills | 7af9151 | 2020-04-14 22:16:57 -0500 | [diff] [blame] | 2109 | "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/", |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2110 | std::string(), std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2111 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2112 | entityPrivileges = { |
| 2113 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 2114 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 2115 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 2116 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 2117 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 2118 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2119 | } |
| 2120 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2121 | private: |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2122 | void parseInterfaceData(nlohmann::json& jsonResponse, |
| 2123 | const std::string& parentIfaceId, |
| 2124 | const std::string& ifaceId, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 2125 | const EthernetInterfaceData& ethData) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2126 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2127 | // Fill out obvious data... |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2128 | jsonResponse["Id"] = ifaceId; |
| 2129 | jsonResponse["@odata.id"] = |
| 2130 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId + |
| 2131 | "/VLANs/" + ifaceId; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2132 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2133 | jsonResponse["VLANEnable"] = true; |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2134 | if (!ethData.vlan_id.empty()) |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2135 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2136 | jsonResponse["VLANId"] = ethData.vlan_id.back(); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2137 | } |
Ed Tanous | a434f2b | 2018-07-27 13:04:22 -0700 | [diff] [blame] | 2138 | } |
| 2139 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2140 | bool verifyNames(const std::string& parent, const std::string& iface) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2141 | { |
| 2142 | if (!boost::starts_with(iface, parent + "_")) |
| 2143 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2144 | return false; |
| 2145 | } |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 2146 | return true; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
| 2149 | /** |
| 2150 | * Functions triggers appropriate requests on DBus |
| 2151 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2152 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2153 | const crow::Request&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2154 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2155 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2156 | // TODO(Pawel) this shall be parameterized call (two params) to get |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2157 | // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. |
| 2158 | // Check if there is required param, truly entering this shall be |
| 2159 | // impossible. |
| 2160 | if (params.size() != 2) |
| 2161 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2162 | messages::internalError(asyncResp->res); |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2163 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2164 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2165 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2166 | const std::string& parentIfaceId = params[0]; |
| 2167 | const std::string& ifaceId = params[1]; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2168 | asyncResp->res.jsonValue["@odata.type"] = |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2169 | "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2170 | asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2171 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2172 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2173 | { |
| 2174 | return; |
| 2175 | } |
Kowalski, Kamil | 927a505 | 2018-07-03 14:16:46 +0200 | [diff] [blame] | 2176 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2177 | // Get single eth interface data, and call the below callback for |
| 2178 | // JSON preparation |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2179 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2180 | params[1], |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2181 | [this, asyncResp, parentIfaceId{std::string(params[0])}, |
| 2182 | ifaceId{std::string(params[1])}]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2183 | const bool& success, const EthernetInterfaceData& ethData, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 2184 | const boost::container::flat_set<IPv4AddressData>&, |
| 2185 | const boost::container::flat_set<IPv6AddressData>&) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2186 | if (success && ethData.vlan_id.size() != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2187 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2188 | parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, |
| 2189 | ifaceId, ethData); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2190 | } |
| 2191 | else |
| 2192 | { |
| 2193 | // ... otherwise return error |
| 2194 | // TODO(Pawel)consider distinguish between non existing |
| 2195 | // object, and other errors |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2196 | messages::resourceNotFound( |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2197 | asyncResp->res, "VLAN Network Interface", ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2198 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2199 | }); |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2200 | } |
| 2201 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2202 | void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2203 | const crow::Request& req, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2204 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2205 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2206 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2207 | if (params.size() != 2) |
| 2208 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2209 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2210 | return; |
| 2211 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2212 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2213 | const std::string& parentIfaceId = params[0]; |
| 2214 | const std::string& ifaceId = params[1]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2215 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2216 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2217 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2218 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2219 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2220 | return; |
| 2221 | } |
| 2222 | |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2223 | bool vlanEnable = false; |
Andrew Geissler | 38268fa | 2020-05-16 14:22:18 -0500 | [diff] [blame] | 2224 | uint32_t vlanId = 0; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2225 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2226 | if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable, |
| 2227 | "VLANId", vlanId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2228 | { |
| 2229 | return; |
| 2230 | } |
| 2231 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2232 | // Get single eth interface data, and call the below callback for |
| 2233 | // JSON preparation |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2234 | getEthernetIfaceData( |
| 2235 | params[1], |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2236 | [asyncResp, parentIfaceId{std::string(params[0])}, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 2237 | ifaceId{std::string(params[1])}, &vlanEnable, |
| 2238 | &vlanId](const bool& success, const EthernetInterfaceData& ethData, |
| 2239 | const boost::container::flat_set<IPv4AddressData>&, |
| 2240 | const boost::container::flat_set<IPv6AddressData>&) { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2241 | if (success && !ethData.vlan_id.empty()) |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2242 | { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2243 | auto callback = |
| 2244 | [asyncResp](const boost::system::error_code ec) { |
| 2245 | if (ec) |
| 2246 | { |
| 2247 | messages::internalError(asyncResp->res); |
| 2248 | } |
| 2249 | }; |
| 2250 | |
| 2251 | if (vlanEnable == true) |
| 2252 | { |
| 2253 | crow::connections::systemBus->async_method_call( |
| 2254 | std::move(callback), "xyz.openbmc_project.Network", |
| 2255 | "/xyz/openbmc_project/network/" + ifaceId, |
| 2256 | "org.freedesktop.DBus.Properties", "Set", |
| 2257 | "xyz.openbmc_project.Network.VLAN", "Id", |
| 2258 | std::variant<uint32_t>(vlanId)); |
| 2259 | } |
| 2260 | else |
| 2261 | { |
| 2262 | BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " |
| 2263 | "vlan interface"; |
| 2264 | crow::connections::systemBus->async_method_call( |
| 2265 | std::move(callback), "xyz.openbmc_project.Network", |
| 2266 | std::string("/xyz/openbmc_project/network/") + |
| 2267 | ifaceId, |
| 2268 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2269 | } |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2270 | } |
| 2271 | else |
| 2272 | { |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2273 | // TODO(Pawel)consider distinguish between non existing |
| 2274 | // object, and other errors |
| 2275 | messages::resourceNotFound( |
| 2276 | asyncResp->res, "VLAN Network Interface", ifaceId); |
| 2277 | return; |
Sunitha Harish | 08244d0 | 2019-04-01 03:57:25 -0500 | [diff] [blame] | 2278 | } |
Ravi Teja | e48c0fc | 2019-04-16 08:37:20 -0500 | [diff] [blame] | 2279 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2282 | void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2283 | const crow::Request&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2284 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2285 | { |
| 2286 | if (params.size() != 2) |
| 2287 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2288 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2289 | return; |
| 2290 | } |
| 2291 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2292 | const std::string& parentIfaceId = params[0]; |
| 2293 | const std::string& ifaceId = params[1]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2294 | |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2295 | if (!verifyNames(parentIfaceId, ifaceId)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2296 | { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2297 | messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", |
| 2298 | ifaceId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2299 | return; |
| 2300 | } |
| 2301 | |
Johnathan Mantey | 0178482 | 2019-06-18 12:44:21 -0700 | [diff] [blame] | 2302 | // Get single eth interface data, and call the below callback for |
| 2303 | // JSON preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2304 | getEthernetIfaceData( |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2305 | params[1], |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2306 | [asyncResp, parentIfaceId{std::string(params[0])}, |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2307 | ifaceId{std::string(params[1])}]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2308 | const bool& success, const EthernetInterfaceData& ethData, |
Ed Tanous | cb13a39 | 2020-07-25 19:02:03 +0000 | [diff] [blame] | 2309 | const boost::container::flat_set<IPv4AddressData>&, |
| 2310 | const boost::container::flat_set<IPv6AddressData>&) { |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2311 | if (success && !ethData.vlan_id.empty()) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2312 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2313 | auto callback = |
| 2314 | [asyncResp](const boost::system::error_code ec) { |
| 2315 | if (ec) |
| 2316 | { |
| 2317 | messages::internalError(asyncResp->res); |
| 2318 | } |
| 2319 | }; |
| 2320 | crow::connections::systemBus->async_method_call( |
| 2321 | std::move(callback), "xyz.openbmc_project.Network", |
| 2322 | std::string("/xyz/openbmc_project/network/") + ifaceId, |
| 2323 | "xyz.openbmc_project.Object.Delete", "Delete"); |
| 2324 | } |
| 2325 | else |
| 2326 | { |
| 2327 | // ... otherwise return error |
| 2328 | // TODO(Pawel)consider distinguish between non existing |
| 2329 | // object, and other errors |
| 2330 | messages::resourceNotFound( |
| 2331 | asyncResp->res, "VLAN Network Interface", ifaceId); |
| 2332 | } |
| 2333 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2334 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2335 | }; |
| 2336 | |
| 2337 | /** |
| 2338 | * VlanNetworkInterfaceCollection derived class for delivering |
| 2339 | * VLANNetworkInterface Collection Schema |
| 2340 | */ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2341 | class VlanNetworkInterfaceCollection : public Node |
| 2342 | { |
| 2343 | public: |
Ed Tanous | 52cc112 | 2020-07-18 13:51:21 -0700 | [diff] [blame] | 2344 | VlanNetworkInterfaceCollection(App& app) : |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2345 | Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", |
| 2346 | std::string()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2347 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2348 | entityPrivileges = { |
| 2349 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 2350 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 2351 | {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, |
| 2352 | {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, |
| 2353 | {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, |
| 2354 | {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2355 | } |
| 2356 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2357 | private: |
| 2358 | /** |
| 2359 | * Functions triggers appropriate requests on DBus |
| 2360 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2361 | void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2362 | const crow::Request&, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2363 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2364 | { |
| 2365 | if (params.size() != 1) |
| 2366 | { |
| 2367 | // This means there is a problem with the router |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2368 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2369 | return; |
Ed Tanous | 8ceb2ec | 2018-08-13 11:11:56 -0700 | [diff] [blame] | 2370 | } |
| 2371 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2372 | const std::string& rootInterfaceName = params[0]; |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2373 | |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2374 | // Get eth interface list, and call the below callback for JSON |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2375 | // preparation |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2376 | getEthernetIfaceList( |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 2377 | [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2378 | const bool& success, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2379 | const boost::container::flat_set<std::string>& ifaceList) { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2380 | if (!success) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2381 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2382 | messages::internalError(asyncResp->res); |
| 2383 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2384 | } |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 2385 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2386 | if (ifaceList.find(rootInterfaceName) == ifaceList.end()) |
Ed Tanous | 4c9afe4 | 2019-05-03 16:59:57 -0700 | [diff] [blame] | 2387 | { |
| 2388 | messages::resourceNotFound(asyncResp->res, |
| 2389 | "VLanNetworkInterfaceCollection", |
| 2390 | rootInterfaceName); |
| 2391 | return; |
| 2392 | } |
| 2393 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2394 | asyncResp->res.jsonValue["@odata.type"] = |
| 2395 | "#VLanNetworkInterfaceCollection." |
| 2396 | "VLanNetworkInterfaceCollection"; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 2397 | asyncResp->res.jsonValue["Name"] = |
| 2398 | "VLAN Network Interface Collection"; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2399 | |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2400 | nlohmann::json ifaceArray = nlohmann::json::array(); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2401 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 2402 | for (const std::string& ifaceItem : ifaceList) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2403 | { |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2404 | if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2405 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 2406 | std::string path = |
| 2407 | "/redfish/v1/Managers/bmc/EthernetInterfaces/"; |
| 2408 | path += rootInterfaceName; |
| 2409 | path += "/VLANs/"; |
| 2410 | path += ifaceItem; |
| 2411 | ifaceArray.push_back({{"@odata.id", std::move(path)}}); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2412 | } |
| 2413 | } |
| 2414 | |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2415 | asyncResp->res.jsonValue["Members@odata.count"] = |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 2416 | ifaceArray.size(); |
| 2417 | asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2418 | asyncResp->res.jsonValue["@odata.id"] = |
| 2419 | "/redfish/v1/Managers/bmc/EthernetInterfaces/" + |
| 2420 | rootInterfaceName + "/VLANs"; |
| 2421 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2422 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2423 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2424 | void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 2425 | const crow::Request& req, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2426 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2427 | { |
| 2428 | if (params.size() != 1) |
| 2429 | { |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2430 | messages::internalError(asyncResp->res); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2431 | return; |
| 2432 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2433 | bool vlanEnable = false; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2434 | uint32_t vlanId = 0; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 2435 | if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId, |
| 2436 | "VLANEnable", vlanEnable)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2437 | { |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2438 | return; |
| 2439 | } |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2440 | // Need both vlanId and vlanEnable to service this request |
| 2441 | if (!vlanId) |
| 2442 | { |
| 2443 | messages::propertyMissing(asyncResp->res, "VLANId"); |
| 2444 | } |
| 2445 | if (!vlanEnable) |
| 2446 | { |
| 2447 | messages::propertyMissing(asyncResp->res, "VLANEnable"); |
| 2448 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2449 | if (static_cast<bool>(vlanId) ^ vlanEnable) |
Sunitha Harish | fda13ad | 2019-03-21 11:01:24 -0500 | [diff] [blame] | 2450 | { |
| 2451 | return; |
| 2452 | } |
| 2453 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2454 | const std::string& rootInterfaceName = params[0]; |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2455 | auto callback = [asyncResp](const boost::system::error_code ec) { |
| 2456 | if (ec) |
| 2457 | { |
| 2458 | // TODO(ed) make more consistent error messages based on |
| 2459 | // phosphor-network responses |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2460 | messages::internalError(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2461 | return; |
| 2462 | } |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 2463 | messages::created(asyncResp->res); |
Ed Tanous | 4a0cb85 | 2018-10-15 07:55:04 -0700 | [diff] [blame] | 2464 | }; |
| 2465 | crow::connections::systemBus->async_method_call( |
| 2466 | std::move(callback), "xyz.openbmc_project.Network", |
| 2467 | "/xyz/openbmc_project/network", |
| 2468 | "xyz.openbmc_project.Network.VLAN.Create", "VLAN", |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 2469 | rootInterfaceName, vlanId); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2470 | } |
Kowalski, Kamil | e439f0f | 2018-05-21 08:13:57 +0200 | [diff] [blame] | 2471 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2472 | } // namespace redfish |