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