blob: 99f452d7273028b7d2e103ee5068a97018265503 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
John Edward Broadbent7e860f12021-04-08 15:57:16 -070018#include <app.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070019#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070020#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020021#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020023#include <node.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024#include <utils/json_utils.hpp>
25
Ed Tanousa24526d2018-12-10 15:17:59 -080026#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053027#include <regex>
Ed Tanousabf2add2019-01-22 16:40:12 -080028#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030namespace redfish
31{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010032
33/**
34 * DBus types primitives for several generic DBus interfaces
35 * TODO(Pawel) consider move this to separate file into boost::dbus
36 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070037using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080038 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
39 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
Ed Tanous4a0cb852018-10-15 07:55:04 -070041using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070043 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070044 std::string,
45 boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -050046 std::string,
47 std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
48 uint32_t, int64_t, uint64_t, double,
49 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070050
51enum class LinkType
52{
53 Local,
54 Global
55};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056
57/**
58 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010059 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070060struct IPv4AddressData
61{
62 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070063 std::string address;
64 std::string domain;
65 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 std::string netmask;
67 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070068 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050069 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070070
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070073 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010075};
76
77/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050078 * Structure for keeping IPv6 data required by Redfish
79 */
80struct IPv6AddressData
81{
82 std::string id;
83 std::string address;
84 std::string origin;
85 uint8_t prefixLength;
86
Gunnar Mills1214b7e2020-06-04 10:11:30 -050087 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050088 {
89 return id < obj.id;
90 }
91};
92/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010093 * Structure for keeping basic single Ethernet Interface information
94 * available from DBus
95 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070096struct EthernetInterfaceData
97{
Ed Tanous4a0cb852018-10-15 07:55:04 -070098 uint32_t speed;
99 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700100 bool DNSEnabled;
101 bool NTPEnabled;
102 bool HostNameEnabled;
103 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800104 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700105 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700106 std::string DHCPEnabled;
107 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700108 std::string hostname;
109 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500110 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700111 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500112 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500113 std::vector<std::string> nameServers;
114 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800115 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100116};
117
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700118struct DHCPParameters
119{
120 std::optional<bool> dhcpv4Enabled;
121 std::optional<bool> useDNSServers;
122 std::optional<bool> useNTPServers;
123 std::optional<bool> useUseDomainName;
124 std::optional<std::string> dhcpv6OperatingMode;
125};
126
Ed Tanous4a0cb852018-10-15 07:55:04 -0700127// Helper function that changes bits netmask notation (i.e. /24)
128// into full dot notation
129inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700131 uint32_t value = 0xffffffff << (32 - bits);
132 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
133 std::to_string((value >> 16) & 0xff) + "." +
134 std::to_string((value >> 8) & 0xff) + "." +
135 std::to_string(value & 0xff);
136 return netmask;
137}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100138
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500139inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700140 bool isIPv4)
141{
142 if (isIPv4)
143 {
144 return (
145 (inputDHCP ==
146 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
147 (inputDHCP ==
148 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
149 }
150 return ((inputDHCP ==
151 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
152 (inputDHCP ==
153 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
154}
155
Ed Tanous2c70f802020-09-28 14:29:23 -0700156inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700157{
158 if (isIPv4 && isIPv6)
159 {
160 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
161 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700162 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700163 {
164 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
165 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700166 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700167 {
168 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
169 }
170 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
171}
172
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500174 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700175 bool isIPv4)
176{
177 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700179 return "Static";
180 }
181 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
182 {
183 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700184 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700185 return "IPv4LinkLocal";
186 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700187 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700188 }
189 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
190 {
191 if (isIPv4)
192 {
193 return "DHCP";
194 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700195 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196 }
197 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
198 {
199 return "SLAAC";
200 }
201 return "";
202}
203
Ed Tanous81ce6092020-12-17 16:54:55 +0000204inline bool extractEthernetInterfaceData(const std::string& ethifaceId,
205 GetManagedObjects& dbusData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500206 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700208 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000209 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700210 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700211 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000213 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700214 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700215 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500218 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800223 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 if (mac != nullptr)
225 {
226 ethData.mac_address = *mac;
227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 }
230 }
231 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
232 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500233 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700234 {
235 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500237 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800238 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 if (id != nullptr)
240 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500241 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700242 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700244 }
245 }
246 else if (ifacePair.first ==
247 "xyz.openbmc_project.Network.EthernetInterface")
248 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500249 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700250 {
251 if (propertyPair.first == "AutoNeg")
252 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700253 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800254 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700255 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700256 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700257 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700258 }
259 }
260 else if (propertyPair.first == "Speed")
261 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500262 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800263 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700264 if (speed != nullptr)
265 {
266 ethData.speed = *speed;
267 }
268 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800269 else if (propertyPair.first == "LinkUp")
270 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500271 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800272 std::get_if<bool>(&propertyPair.second);
273 if (linkUp != nullptr)
274 {
275 ethData.linkUp = *linkUp;
276 }
277 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700278 else if (propertyPair.first == "NICEnabled")
279 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500280 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700281 std::get_if<bool>(&propertyPair.second);
282 if (nicEnabled != nullptr)
283 {
284 ethData.nicEnabled = *nicEnabled;
285 }
286 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500287 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700288 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500289 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500290 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800291 &propertyPair.second);
292 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700293 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700294 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500295 }
296 }
297 else if (propertyPair.first == "StaticNameServers")
298 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500299 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500300 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500301 &propertyPair.second);
302 if (staticNameServers != nullptr)
303 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700304 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700305 }
306 }
manojkiraneda2a133282019-02-19 13:09:43 +0530307 else if (propertyPair.first == "DHCPEnabled")
308 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700309 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700310 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700311 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530312 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700313 ethData.DHCPEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530314 }
315 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800316 else if (propertyPair.first == "DomainName")
317 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500318 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500319 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800320 &propertyPair.second);
321 if (domainNames != nullptr)
322 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700323 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800324 }
325 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500326 else if (propertyPair.first == "DefaultGateway")
327 {
328 const std::string* defaultGateway =
329 std::get_if<std::string>(&propertyPair.second);
330 if (defaultGateway != nullptr)
331 {
332 std::string defaultGatewayStr = *defaultGateway;
333 if (defaultGatewayStr.empty())
334 {
335 ethData.default_gateway = "0.0.0.0";
336 }
337 else
338 {
339 ethData.default_gateway = defaultGatewayStr;
340 }
341 }
342 }
343 else if (propertyPair.first == "DefaultGateway6")
344 {
345 const std::string* defaultGateway6 =
346 std::get_if<std::string>(&propertyPair.second);
347 if (defaultGateway6 != nullptr)
348 {
349 std::string defaultGateway6Str =
350 *defaultGateway6;
351 if (defaultGateway6Str.empty())
352 {
353 ethData.ipv6_default_gateway =
354 "0:0:0:0:0:0:0:0";
355 }
356 else
357 {
358 ethData.ipv6_default_gateway =
359 defaultGateway6Str;
360 }
361 }
362 }
Ed Tanous029573d2019-02-01 10:57:49 -0800363 }
364 }
365 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700366
367 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
368 {
369 if (ifacePair.first ==
370 "xyz.openbmc_project.Network.DHCPConfiguration")
371 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500372 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 {
374 if (propertyPair.first == "DNSEnabled")
375 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700376 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700377 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700378 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700379 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700380 ethData.DNSEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700381 }
382 }
383 else if (propertyPair.first == "NTPEnabled")
384 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700385 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700386 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700387 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700388 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700389 ethData.NTPEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700390 }
391 }
392 else if (propertyPair.first == "HostNameEnabled")
393 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700394 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700395 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700396 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700397 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700398 ethData.HostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700399 }
400 }
401 else if (propertyPair.first == "SendHostNameEnabled")
402 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700403 const bool* sendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700404 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700405 if (sendHostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700406 {
407 ethData.SendHostNameEnabled =
Ed Tanous2c70f802020-09-28 14:29:23 -0700408 *sendHostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700409 }
410 }
411 }
412 }
413 }
Ed Tanous029573d2019-02-01 10:57:49 -0800414 // System configuration shows up in the global namespace, so no need
415 // to check eth number
416 if (ifacePair.first ==
417 "xyz.openbmc_project.Network.SystemConfiguration")
418 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500419 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800420 {
421 if (propertyPair.first == "HostName")
422 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500423 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500424 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800425 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700426 {
Ed Tanous029573d2019-02-01 10:57:49 -0800427 ethData.hostname = *hostname;
428 }
429 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700430 }
431 }
432 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700433 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700434 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700435}
436
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500437// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700438inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000439 extractIPV6Data(const std::string& ethifaceId,
440 const GetManagedObjects& dbusData,
441 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500442{
443 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000444 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500445
446 // Since there might be several IPv6 configurations aligned with
447 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000448 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500449 {
450 // Check if proper pattern for object path appears
451 if (boost::starts_with(objpath.first.str, ipv6PathStart))
452 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500453 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500454 {
455 if (interface.first == "xyz.openbmc_project.Network.IP")
456 {
457 // Instance IPv6AddressData structure, and set as
458 // appropriate
459 std::pair<
460 boost::container::flat_set<IPv6AddressData>::iterator,
461 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000462 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700463 IPv6AddressData& ipv6Address = *it.first;
464 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700465 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500466 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500467 {
468 if (property.first == "Address")
469 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500470 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500471 std::get_if<std::string>(&property.second);
472 if (address != nullptr)
473 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700474 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500475 }
476 }
477 else if (property.first == "Origin")
478 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500479 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500480 std::get_if<std::string>(&property.second);
481 if (origin != nullptr)
482 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700483 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500484 translateAddressOriginDbusToRedfish(*origin,
485 false);
486 }
487 }
488 else if (property.first == "PrefixLength")
489 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500490 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500491 std::get_if<uint8_t>(&property.second);
492 if (prefix != nullptr)
493 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700494 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500495 }
496 }
497 else
498 {
499 BMCWEB_LOG_ERROR
500 << "Got extra property: " << property.first
501 << " on the " << objpath.first.str << " object";
502 }
503 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500504 }
505 }
506 }
507 }
508}
509
Ed Tanous4a0cb852018-10-15 07:55:04 -0700510// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700511inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000512 extractIPData(const std::string& ethifaceId,
513 const GetManagedObjects& dbusData,
514 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700515{
516 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000517 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700518
519 // Since there might be several IPv4 configurations aligned with
520 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000521 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700522 {
523 // Check if proper pattern for object path appears
524 if (boost::starts_with(objpath.first.str, ipv4PathStart))
525 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500526 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 {
528 if (interface.first == "xyz.openbmc_project.Network.IP")
529 {
530 // Instance IPv4AddressData structure, and set as
531 // appropriate
532 std::pair<
533 boost::container::flat_set<IPv4AddressData>::iterator,
534 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000535 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700536 IPv4AddressData& ipv4Address = *it.first;
537 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700538 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500539 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700540 {
541 if (property.first == "Address")
542 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500543 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800544 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700545 if (address != nullptr)
546 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700547 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700548 }
549 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700550 else if (property.first == "Origin")
551 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500552 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800553 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700554 if (origin != nullptr)
555 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700556 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700557 translateAddressOriginDbusToRedfish(*origin,
558 true);
559 }
560 }
561 else if (property.first == "PrefixLength")
562 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500563 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800564 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700565 if (mask != nullptr)
566 {
567 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700568 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700569 }
570 }
571 else
572 {
573 BMCWEB_LOG_ERROR
574 << "Got extra property: " << property.first
575 << " on the " << objpath.first.str << " object";
576 }
577 }
578 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700579 ipv4Address.linktype =
580 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700581 ? LinkType::Local
582 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700583 }
584 }
585 }
586 }
587}
588
589/**
590 * @brief Sets given Id on the given VLAN interface through D-Bus
591 *
592 * @param[in] ifaceId Id of VLAN interface that should be modified
593 * @param[in] inputVlanId New ID of the VLAN
594 * @param[in] callback Function that will be called after the operation
595 *
596 * @return None.
597 */
598template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500599void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
600 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700601{
602 crow::connections::systemBus->async_method_call(
603 callback, "xyz.openbmc_project.Network",
604 std::string("/xyz/openbmc_project/network/") + ifaceId,
605 "org.freedesktop.DBus.Properties", "Set",
606 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800607 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700608}
609
610/**
611 * @brief Helper function that verifies IP address to check if it is in
612 * proper format. If bits pointer is provided, also calculates active
613 * bit count for Subnet Mask.
614 *
615 * @param[in] ip IP that will be verified
616 * @param[out] bits Calculated mask in bits notation
617 *
618 * @return true in case of success, false otherwise
619 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500620inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
621 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622{
623 std::vector<std::string> bytesInMask;
624
625 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626
627 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 return false;
631 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632
Ed Tanous4a0cb852018-10-15 07:55:04 -0700633 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 *bits = 0;
636 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500638 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700639 long previousValue = 255;
640 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500641 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 if (byte.empty())
644 {
645 return false;
646 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Use strtol instead of stroi to avoid exceptions
649 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 // endPtr should point to the end of the string, otherwise given string
652 // is not 100% number
653 if (*endPtr != '\0')
654 {
655 return false;
656 }
657
658 // Value should be contained in byte
659 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 {
661 return false;
662 }
663
664 if (bits != nullptr)
665 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700666 // Mask has to be continuous between bytes
667 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700668 {
669 return false;
670 }
671
Ed Tanous4a0cb852018-10-15 07:55:04 -0700672 // Mask has to be continuous inside bytes
673 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000676 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000678 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700680 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 // Continuity not preserved
683 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700685 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 else
688 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700689 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700691 }
692 }
693
694 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700695 }
696
Ed Tanous4a0cb852018-10-15 07:55:04 -0700697 return true;
698}
699
700/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700701 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700702 *
703 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700704 * @param[in] ipHash DBus Hash id of IP that should be deleted
705 * @param[io] asyncResp Response object that will be returned to client
706 *
707 * @return None
708 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500709inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800710 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700711{
712 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700713 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700714 if (ec)
715 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800716 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100717 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700718 },
719 "xyz.openbmc_project.Network",
720 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
721 "xyz.openbmc_project.Object.Delete", "Delete");
722}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700723
Gunnar Mills244b6d52021-04-12 15:44:23 -0500724inline void updateIPv4DefaultGateway(
725 const std::string& ifaceId, const std::string& gateway,
726 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500727{
728 crow::connections::systemBus->async_method_call(
729 [asyncResp](const boost::system::error_code ec) {
730 if (ec)
731 {
732 messages::internalError(asyncResp->res);
733 return;
734 }
735 asyncResp->res.result(boost::beast::http::status::no_content);
736 },
737 "xyz.openbmc_project.Network",
738 "/xyz/openbmc_project/network/" + ifaceId,
739 "org.freedesktop.DBus.Properties", "Set",
740 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
741 std::variant<std::string>(gateway));
742}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700743/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700744 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700745 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700746 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
747 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
748 * @param[in] gateway IPv4 address of this interfaces gateway
749 * @param[in] address IPv4 address to assign to this interface
750 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700751 *
752 * @return None
753 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000754inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
755 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800756 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700757{
Ravi Teja9010ec22019-08-01 23:30:25 -0500758 auto createIpHandler = [asyncResp, ifaceId,
759 gateway](const boost::system::error_code ec) {
760 if (ec)
761 {
762 messages::internalError(asyncResp->res);
763 return;
764 }
765 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
766 };
767
Ed Tanous4a0cb852018-10-15 07:55:04 -0700768 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500769 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700770 "/xyz/openbmc_project/network/" + ifaceId,
771 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700772 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700773 gateway);
774}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500775
776/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700777 * @brief Deletes the IPv4 entry for this interface and creates a replacement
778 * static IPv4 entry
779 *
780 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
781 * @param[in] id The unique hash entry identifying the DBus entry
782 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
783 * @param[in] gateway IPv4 address of this interfaces gateway
784 * @param[in] address IPv4 address to assign to this interface
785 * @param[io] asyncResp Response object that will be returned to client
786 *
787 * @return None
788 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800789inline void
790 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
791 uint8_t prefixLength, const std::string& gateway,
792 const std::string& address,
793 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700794{
795 crow::connections::systemBus->async_method_call(
796 [asyncResp, ifaceId, address, prefixLength,
797 gateway](const boost::system::error_code ec) {
798 if (ec)
799 {
800 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500801 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700802 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500803
Johnathan Mantey01784822019-06-18 12:44:21 -0700804 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500805 [asyncResp, ifaceId,
806 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000807 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700808 {
809 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500810 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700811 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500812 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700813 },
814 "xyz.openbmc_project.Network",
815 "/xyz/openbmc_project/network/" + ifaceId,
816 "xyz.openbmc_project.Network.IP.Create", "IP",
817 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
818 prefixLength, gateway);
819 },
820 "xyz.openbmc_project.Network",
821 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
822 "xyz.openbmc_project.Object.Delete", "Delete");
823}
824
825/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500826 * @brief Deletes given IPv6
827 *
828 * @param[in] ifaceId Id of interface whose IP should be deleted
829 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500830 * @param[io] asyncResp Response object that will be returned to client
831 *
832 * @return None
833 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500834inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800835 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500836{
837 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700838 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500839 if (ec)
840 {
841 messages::internalError(asyncResp->res);
842 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500843 },
844 "xyz.openbmc_project.Network",
845 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
846 "xyz.openbmc_project.Object.Delete", "Delete");
847}
848
849/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700850 * @brief Deletes the IPv6 entry for this interface and creates a replacement
851 * static IPv6 entry
852 *
853 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
854 * @param[in] id The unique hash entry identifying the DBus entry
855 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
856 * @param[in] address IPv6 address to assign to this interface
857 * @param[io] asyncResp Response object that will be returned to client
858 *
859 * @return None
860 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800861inline void
862 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
863 uint8_t prefixLength, const std::string& address,
864 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700865{
866 crow::connections::systemBus->async_method_call(
867 [asyncResp, ifaceId, address,
868 prefixLength](const boost::system::error_code ec) {
869 if (ec)
870 {
871 messages::internalError(asyncResp->res);
872 }
873 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000874 [asyncResp](const boost::system::error_code ec2) {
875 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700876 {
877 messages::internalError(asyncResp->res);
878 }
879 },
880 "xyz.openbmc_project.Network",
881 "/xyz/openbmc_project/network/" + ifaceId,
882 "xyz.openbmc_project.Network.IP.Create", "IP",
883 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
884 prefixLength, "");
885 },
886 "xyz.openbmc_project.Network",
887 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
888 "xyz.openbmc_project.Object.Delete", "Delete");
889}
890
891/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500892 * @brief Creates IPv6 with given data
893 *
894 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500895 * @param[in] prefixLength Prefix length that needs to be added
896 * @param[in] address IP address that needs to be added
897 * @param[io] asyncResp Response object that will be returned to client
898 *
899 * @return None
900 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500901inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
902 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800903 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500904{
905 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
906 if (ec)
907 {
908 messages::internalError(asyncResp->res);
909 }
910 };
911 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500912 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500913 crow::connections::systemBus->async_method_call(
914 std::move(createIpHandler), "xyz.openbmc_project.Network",
915 "/xyz/openbmc_project/network/" + ifaceId,
916 "xyz.openbmc_project.Network.IP.Create", "IP",
917 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
918 "");
919}
920
Ed Tanous4a0cb852018-10-15 07:55:04 -0700921/**
922 * Function that retrieves all properties for given Ethernet Interface
923 * Object
924 * from EntityManager Network Manager
925 * @param ethiface_id a eth interface id to query on DBus
926 * @param callback a function that shall be called to convert Dbus output
927 * into JSON
928 */
929template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000930void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500931 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700932{
933 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000934 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
935 const boost::system::error_code errorCode,
Ed Tanousf23b7292020-10-15 09:41:17 -0700936 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700937 EthernetInterfaceData ethData{};
938 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500939 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700940
Ed Tanous81ce6092020-12-17 16:54:55 +0000941 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700943 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700944 return;
945 }
946
Ed Tanous4c9afe42019-05-03 16:59:57 -0700947 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700948 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700949 if (!found)
950 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700951 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700952 return;
953 }
954
Ed Tanous2c70f802020-09-28 14:29:23 -0700955 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700956 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500957 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700958 {
Ravi Tejac6191412019-07-30 00:53:50 -0500959 if (((ipv4.linktype == LinkType::Global) &&
960 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500961 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700963 ipv4.gateway = ethData.default_gateway;
964 }
965 }
966
Ed Tanous2c70f802020-09-28 14:29:23 -0700967 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500968 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700969 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700970 },
971 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
972 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700973}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700974
975/**
976 * Function that retrieves all Ethernet Interfaces available through Network
977 * Manager
978 * @param callback a function that shall be called to convert Dbus output
979 * into JSON.
980 */
981template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500982void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700983{
984 crow::connections::systemBus->async_method_call(
985 [callback{std::move(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000986 const boost::system::error_code errorCode,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500987 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700988 // Callback requires vector<string> to retrieve all available
989 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -0700990 boost::container::flat_set<std::string> ifaceList;
991 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +0000992 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700993 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700994 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700995 return;
996 }
997
998 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500999 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001000 {
1001 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001002 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001003 {
1004 // If interface is
1005 // xyz.openbmc_project.Network.EthernetInterface, this is
1006 // what we're looking for.
1007 if (interface.first ==
1008 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001009 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001010 std::string ifaceId = objpath.first.filename();
1011 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001012 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001013 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001014 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001015 // and put it into output vector.
1016 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001017 }
1018 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001019 }
1020 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001021 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001022 },
1023 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1024 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001025}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001026
1027/**
1028 * EthernetCollection derived class for delivering Ethernet Collection Schema
1029 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001030class EthernetCollection : public Node
1031{
1032 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001033 EthernetCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001034 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001035 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 entityPrivileges = {
1037 {boost::beast::http::verb::get, {{"Login"}}},
1038 {boost::beast::http::verb::head, {{"Login"}}},
1039 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1040 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1041 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1042 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1043 }
1044
1045 private:
1046 /**
1047 * Functions triggers appropriate requests on DBus
1048 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001049 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1050 const crow::Request&, const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001051 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001052 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001053 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
zhanghch058d1b46d2021-04-01 11:18:24 +08001054 asyncResp->res.jsonValue["@odata.id"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001055 "/redfish/v1/Managers/bmc/EthernetInterfaces";
zhanghch058d1b46d2021-04-01 11:18:24 +08001056 asyncResp->res.jsonValue["Name"] =
1057 "Ethernet Network Interface Collection";
1058 asyncResp->res.jsonValue["Description"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08001059 "Collection of EthernetInterfaces for this Manager";
zhanghch058d1b46d2021-04-01 11:18:24 +08001060
Ed Tanous4a0cb852018-10-15 07:55:04 -07001061 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001062 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001063 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001064 [asyncResp](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001065 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00001066 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001067 if (!success)
1068 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001069 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001070 return;
1071 }
1072
Ed Tanous2c70f802020-09-28 14:29:23 -07001073 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001074 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001075 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001076 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001077 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001078 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001079 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001080 if (found == std::string::npos)
1081 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001082 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001083 {{"@odata.id",
1084 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001085 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001086 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001087 }
1088
Ed Tanous4c9afe42019-05-03 16:59:57 -07001089 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001090 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001091 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001092 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001093 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001094 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001095};
1096
1097/**
1098 * EthernetInterface derived class for delivering Ethernet Schema
1099 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001100class EthernetInterface : public Node
1101{
1102 public:
1103 /*
1104 * Default Constructor
1105 */
Ed Tanous52cc1122020-07-18 13:51:21 -07001106 EthernetInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001107 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001108 std::string())
1109 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 entityPrivileges = {
1111 {boost::beast::http::verb::get, {{"Login"}}},
1112 {boost::beast::http::verb::head, {{"Login"}}},
1113 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1114 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1115 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1116 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001117 }
1118
Ed Tanous1abe55e2018-09-05 08:30:59 -07001119 private:
zhanghch058d1b46d2021-04-01 11:18:24 +08001120 void
1121 handleHostnamePatch(const std::string& hostname,
1122 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001123 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301124 // SHOULD handle host names of up to 255 characters(RFC 1123)
1125 if (hostname.length() > 255)
1126 {
1127 messages::propertyValueFormatError(asyncResp->res, hostname,
1128 "HostName");
1129 return;
1130 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001131 crow::connections::systemBus->async_method_call(
1132 [asyncResp](const boost::system::error_code ec) {
1133 if (ec)
1134 {
1135 messages::internalError(asyncResp->res);
1136 }
1137 },
1138 "xyz.openbmc_project.Network",
1139 "/xyz/openbmc_project/network/config",
1140 "org.freedesktop.DBus.Properties", "Set",
1141 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001142 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001143 }
1144
zhanghch058d1b46d2021-04-01 11:18:24 +08001145 void handleDomainnamePatch(
1146 const std::string& ifaceId, const std::string& domainname,
1147 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301148 {
1149 std::vector<std::string> vectorDomainname = {domainname};
1150 crow::connections::systemBus->async_method_call(
1151 [asyncResp](const boost::system::error_code ec) {
1152 if (ec)
1153 {
1154 messages::internalError(asyncResp->res);
1155 }
1156 },
1157 "xyz.openbmc_project.Network",
1158 "/xyz/openbmc_project/network/" + ifaceId,
1159 "org.freedesktop.DBus.Properties", "Set",
1160 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1161 std::variant<std::vector<std::string>>(vectorDomainname));
1162 }
1163
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001164 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
zhanghch058d1b46d2021-04-01 11:18:24 +08001165 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301166 {
1167 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1168 if (fqdn.length() > 255)
1169 {
1170 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1171 return;
1172 }
1173
1174 size_t pos = fqdn.find('.');
1175 if (pos == std::string::npos)
1176 {
1177 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1178 return;
1179 }
1180
1181 std::string hostname;
1182 std::string domainname;
1183 domainname = (fqdn).substr(pos + 1);
1184 hostname = (fqdn).substr(0, pos);
1185
1186 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1187 {
1188 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1189 return;
1190 }
1191
1192 handleHostnamePatch(hostname, asyncResp);
1193 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1194 }
1195
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001196 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301197 {
1198 // A valid host name can never have the dotted-decimal form (RFC 1123)
1199 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1200 {
1201 return false;
1202 }
1203 // Each label(hostname/subdomains) within a valid FQDN
1204 // MUST handle host names of up to 63 characters (RFC 1123)
1205 // labels cannot start or end with hyphens (RFC 952)
1206 // labels can start with numbers (RFC 1123)
1207 const std::regex pattern(
1208 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1209
1210 return std::regex_match(hostname, pattern);
1211 }
1212
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001213 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301214 {
1215 // Can have multiple subdomains
1216 // Top Level Domain's min length is 2 character
1217 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1218 "{1,30}\\.)*[a-zA-Z]{2,}$");
1219
1220 return std::regex_match(domainname, pattern);
1221 }
1222
zhanghch058d1b46d2021-04-01 11:18:24 +08001223 void handleMACAddressPatch(
1224 const std::string& ifaceId, const std::string& macAddress,
1225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301226 {
1227 crow::connections::systemBus->async_method_call(
1228 [asyncResp, macAddress](const boost::system::error_code ec) {
1229 if (ec)
1230 {
1231 messages::internalError(asyncResp->res);
1232 return;
1233 }
Ratan Guptad5776652019-03-03 08:47:22 +05301234 },
1235 "xyz.openbmc_project.Network",
1236 "/xyz/openbmc_project/network/" + ifaceId,
1237 "org.freedesktop.DBus.Properties", "Set",
1238 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1239 std::variant<std::string>(macAddress));
1240 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001241
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001242 void setDHCPEnabled(const std::string& ifaceId,
1243 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001244 const bool v6Value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001245 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001246 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001247 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001248 crow::connections::systemBus->async_method_call(
1249 [asyncResp](const boost::system::error_code ec) {
1250 if (ec)
1251 {
1252 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1253 messages::internalError(asyncResp->res);
1254 return;
1255 }
Jayaprakash Mutyala8f7e9c12021-03-22 10:08:00 +00001256 messages::success(asyncResp->res);
Jennifer Leeda131a92019-04-24 15:13:55 -07001257 },
1258 "xyz.openbmc_project.Network",
1259 "/xyz/openbmc_project/network/" + ifaceId,
1260 "org.freedesktop.DBus.Properties", "Set",
1261 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001262 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001263 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001264
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001265 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001266 const std::string& ifaceId, const std::string& propertyName,
zhanghch058d1b46d2021-04-01 11:18:24 +08001267 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001268 {
1269 crow::connections::systemBus->async_method_call(
1270 [asyncResp](const boost::system::error_code ec) {
1271 if (ec)
1272 {
1273 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1274 messages::internalError(asyncResp->res);
1275 return;
1276 }
1277 },
1278 "xyz.openbmc_project.Network",
1279 "/xyz/openbmc_project/network/" + ifaceId,
1280 "org.freedesktop.DBus.Properties", "Set",
1281 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1282 std::variant<bool>{value});
1283 }
1284
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001285 void setDHCPv4Config(const std::string& propertyName, const bool& value,
zhanghch058d1b46d2021-04-01 11:18:24 +08001286 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001287 {
1288 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1289 crow::connections::systemBus->async_method_call(
1290 [asyncResp](const boost::system::error_code ec) {
1291 if (ec)
1292 {
1293 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1294 messages::internalError(asyncResp->res);
1295 return;
1296 }
1297 },
1298 "xyz.openbmc_project.Network",
1299 "/xyz/openbmc_project/network/config/dhcp",
1300 "org.freedesktop.DBus.Properties", "Set",
1301 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1302 std::variant<bool>{value});
1303 }
Ratan Guptad5776652019-03-03 08:47:22 +05301304
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001305 void handleDHCPPatch(const std::string& ifaceId,
1306 const EthernetInterfaceData& ethData,
Ed Tanousf23b7292020-10-15 09:41:17 -07001307 const DHCPParameters& v4dhcpParms,
1308 const DHCPParameters& v6dhcpParms,
zhanghch058d1b46d2021-04-01 11:18:24 +08001309 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001310 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001311 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1312 bool ipv6Active =
1313 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001314
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001315 bool nextv4DHCPState =
1316 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1317
1318 bool nextv6DHCPState{};
1319 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001320 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001321 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1322 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1323 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1324 {
1325 messages::propertyValueFormatError(
1326 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1327 "OperatingMode");
1328 return;
1329 }
1330 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1331 }
1332 else
1333 {
1334 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001335 }
1336
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001337 bool nextDNS{};
1338 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001339 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001340 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1341 {
1342 messages::generalError(asyncResp->res);
1343 return;
1344 }
1345 nextDNS = *v4dhcpParms.useDNSServers;
1346 }
1347 else if (v4dhcpParms.useDNSServers)
1348 {
1349 nextDNS = *v4dhcpParms.useDNSServers;
1350 }
1351 else if (v6dhcpParms.useDNSServers)
1352 {
1353 nextDNS = *v6dhcpParms.useDNSServers;
1354 }
1355 else
1356 {
1357 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001358 }
1359
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001360 bool nextNTP{};
1361 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001362 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001363 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1364 {
1365 messages::generalError(asyncResp->res);
1366 return;
1367 }
1368 nextNTP = *v4dhcpParms.useNTPServers;
1369 }
1370 else if (v4dhcpParms.useNTPServers)
1371 {
1372 nextNTP = *v4dhcpParms.useNTPServers;
1373 }
1374 else if (v6dhcpParms.useNTPServers)
1375 {
1376 nextNTP = *v6dhcpParms.useNTPServers;
1377 }
1378 else
1379 {
1380 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001381 }
1382
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001383 bool nextUseDomain{};
1384 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001385 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001386 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1387 {
1388 messages::generalError(asyncResp->res);
1389 return;
1390 }
1391 nextUseDomain = *v4dhcpParms.useUseDomainName;
1392 }
1393 else if (v4dhcpParms.useUseDomainName)
1394 {
1395 nextUseDomain = *v4dhcpParms.useUseDomainName;
1396 }
1397 else if (v6dhcpParms.useUseDomainName)
1398 {
1399 nextUseDomain = *v6dhcpParms.useUseDomainName;
1400 }
1401 else
1402 {
1403 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001404 }
1405
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001406 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1407 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1408 asyncResp);
1409 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1410 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1411 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1412 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1413 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1414 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001415 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001416
1417 boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001418 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301419 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1420 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001421 const boost::container::flat_set<IPv4AddressData>::const_iterator&
1422 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001423 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301424 return std::find_if(head, end, [](const IPv4AddressData& value) {
1425 return value.origin == "Static";
1426 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001427 }
1428
1429 boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanous2c70f802020-09-28 14:29:23 -07001430 getNextStaticIpEntry(
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301431 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1432 head,
Ed Tanousb5a76932020-09-29 16:16:58 -07001433 const boost::container::flat_set<IPv6AddressData>::const_iterator&
1434 end)
Johnathan Mantey01784822019-06-18 12:44:21 -07001435 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301436 return std::find_if(head, end, [](const IPv6AddressData& value) {
1437 return value.origin == "Static";
1438 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001439 }
1440
Ravi Tejad1d50812019-06-23 16:20:27 -05001441 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001442 const std::string& ifaceId, nlohmann::json& input,
1443 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001444 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001445 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001446 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301447 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001448 messages::propertyValueTypeError(
1449 asyncResp->res,
1450 input.dump(2, ' ', true,
1451 nlohmann::json::error_handler_t::replace),
1452 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301453 return;
1454 }
1455
Ed Tanous271584a2019-07-09 16:24:22 -07001456 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001457 // Find the first static IP address currently active on the NIC and
1458 // match it to the first JSON element in the IPv4StaticAddresses array.
1459 // Match each subsequent JSON element to the next static IP programmed
1460 // into the NIC.
Ed Tanous2c70f802020-09-28 14:29:23 -07001461 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1462 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001463
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001464 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001465 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001466 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001467 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001468
Johnathan Mantey01784822019-06-18 12:44:21 -07001469 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301470 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001471 std::optional<std::string> address;
1472 std::optional<std::string> subnetMask;
1473 std::optional<std::string> gateway;
1474
1475 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1476 address, "SubnetMask", subnetMask,
1477 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301478 {
1479 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001480 asyncResp->res,
1481 thisJson.dump(2, ' ', true,
1482 nlohmann::json::error_handler_t::replace),
1483 pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301484 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301485 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301486
Johnathan Mantey01784822019-06-18 12:44:21 -07001487 // Find the address/subnet/gateway values. Any values that are
1488 // not explicitly provided are assumed to be unmodified from the
1489 // current state of the interface. Merge existing state into the
1490 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001491 const std::string* addr = nullptr;
1492 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001493 uint8_t prefixLength = 0;
1494 bool errorInEntry = false;
1495 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301496 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001497 if (ipv4VerifyIpAndGetBitcount(*address))
1498 {
1499 addr = &(*address);
1500 }
1501 else
1502 {
1503 messages::propertyValueFormatError(
1504 asyncResp->res, *address, pathString + "/Address");
1505 errorInEntry = true;
1506 }
1507 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001508 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001509 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001510 addr = &(niciPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301511 }
1512 else
1513 {
1514 messages::propertyMissing(asyncResp->res,
1515 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001516 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001517 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301518
1519 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001520 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001521 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1522 {
1523 messages::propertyValueFormatError(
1524 asyncResp->res, *subnetMask,
1525 pathString + "/SubnetMask");
1526 errorInEntry = true;
1527 }
1528 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001529 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001530 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001531 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001532 &prefixLength))
1533 {
1534 messages::propertyValueFormatError(
Ed Tanous2c70f802020-09-28 14:29:23 -07001535 asyncResp->res, niciPentry->netmask,
Johnathan Mantey01784822019-06-18 12:44:21 -07001536 pathString + "/SubnetMask");
1537 errorInEntry = true;
1538 }
1539 }
1540 else
1541 {
1542 messages::propertyMissing(asyncResp->res,
1543 pathString + "/SubnetMask");
1544 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001545 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301546
Ratan Guptaf476acb2019-03-02 16:46:57 +05301547 if (gateway)
1548 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001549 if (ipv4VerifyIpAndGetBitcount(*gateway))
1550 {
1551 gw = &(*gateway);
1552 }
1553 else
1554 {
1555 messages::propertyValueFormatError(
1556 asyncResp->res, *gateway, pathString + "/Gateway");
1557 errorInEntry = true;
1558 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301559 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001560 else if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001561 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001562 gw = &niciPentry->gateway;
Johnathan Mantey01784822019-06-18 12:44:21 -07001563 }
1564 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001565 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001566 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001567 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001568 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001569 }
1570
Johnathan Mantey01784822019-06-18 12:44:21 -07001571 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001572 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001573 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001574 }
1575
Ed Tanous2c70f802020-09-28 14:29:23 -07001576 if (niciPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001577 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001578 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
Johnathan Mantey01784822019-06-18 12:44:21 -07001579 *gw, *addr, asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001580 niciPentry =
1581 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001582 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001583 else
1584 {
Ed Tanouscb13a392020-07-25 19:02:03 +00001585 createIPv4(ifaceId, prefixLength, *gateway, *address,
1586 asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001587 }
1588 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001589 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001590 else
1591 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001592 if (niciPentry == ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001593 {
1594 // Requesting a DELETE/DO NOT MODIFY action for an item
1595 // that isn't present on the eth(n) interface. Input JSON is
1596 // in error, so bail out.
1597 if (thisJson.is_null())
1598 {
1599 messages::resourceCannotBeDeleted(asyncResp->res);
1600 return;
1601 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001602 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001603 asyncResp->res,
1604 thisJson.dump(2, ' ', true,
1605 nlohmann::json::error_handler_t::replace),
1606 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001607 return;
Johnathan Mantey01784822019-06-18 12:44:21 -07001608 }
1609
1610 if (thisJson.is_null())
1611 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001612 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001613 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001614 if (niciPentry != ipv4Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001615 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001616 niciPentry =
1617 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001618 }
1619 entryIdx++;
1620 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001621 }
1622 }
1623
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001624 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001625 const std::string& ifaceId,
1626 const std::vector<std::string>& updatedStaticNameServers,
zhanghch058d1b46d2021-04-01 11:18:24 +08001627 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001628 {
1629 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001630 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001631 if (ec)
1632 {
1633 messages::internalError(asyncResp->res);
1634 return;
1635 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001636 },
1637 "xyz.openbmc_project.Network",
1638 "/xyz/openbmc_project/network/" + ifaceId,
1639 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001640 "xyz.openbmc_project.Network.EthernetInterface",
1641 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001642 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1643 }
1644
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001645 void handleIPv6StaticAddressesPatch(
Ed Tanousf23b7292020-10-15 09:41:17 -07001646 const std::string& ifaceId, const nlohmann::json& input,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001647 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
zhanghch058d1b46d2021-04-01 11:18:24 +08001648 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001649 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001650 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001651 {
Ed Tanous71f52d92021-02-19 08:51:17 -08001652 messages::propertyValueTypeError(
1653 asyncResp->res,
1654 input.dump(2, ' ', true,
1655 nlohmann::json::error_handler_t::replace),
1656 "IPv6StaticAddresses");
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001657 return;
1658 }
Ed Tanous271584a2019-07-09 16:24:22 -07001659 size_t entryIdx = 1;
Ed Tanous2c70f802020-09-28 14:29:23 -07001660 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1661 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ed Tanousf23b7292020-10-15 09:41:17 -07001662 for (const nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001663 {
1664 std::string pathString =
1665 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1666
Johnathan Mantey01784822019-06-18 12:44:21 -07001667 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001668 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001669 std::optional<std::string> address;
1670 std::optional<uint8_t> prefixLength;
Ed Tanousf23b7292020-10-15 09:41:17 -07001671 nlohmann::json thisJsonCopy = thisJson;
1672 if (!json_util::readJson(thisJsonCopy, asyncResp->res,
1673 "Address", address, "PrefixLength",
1674 prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001675 {
1676 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001677 asyncResp->res,
1678 thisJson.dump(2, ' ', true,
1679 nlohmann::json::error_handler_t::replace),
1680 pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001681 return;
1682 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001683
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001684 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001685 uint8_t prefix;
1686
1687 // Find the address and prefixLength values. Any values that are
1688 // not explicitly provided are assumed to be unmodified from the
1689 // current state of the interface. Merge existing state into the
1690 // current request.
1691 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001692 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001693 addr = &(*address);
1694 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001695 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001696 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001697 addr = &(niciPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001698 }
1699 else
1700 {
1701 messages::propertyMissing(asyncResp->res,
1702 pathString + "/Address");
1703 return;
1704 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001705
1706 if (prefixLength)
1707 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001708 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001709 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001710 else if (niciPentry != ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001711 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001712 prefix = niciPentry->prefixLength;
Johnathan Mantey01784822019-06-18 12:44:21 -07001713 }
1714 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001715 {
1716 messages::propertyMissing(asyncResp->res,
1717 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001718 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001719 }
1720
Ed Tanous2c70f802020-09-28 14:29:23 -07001721 if (niciPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001722 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001723 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
Johnathan Mantey01784822019-06-18 12:44:21 -07001724 asyncResp);
Ed Tanous2c70f802020-09-28 14:29:23 -07001725 niciPentry =
1726 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001727 }
1728 else
1729 {
1730 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1731 }
1732 entryIdx++;
1733 }
1734 else
1735 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001736 if (niciPentry == ipv6Data.end())
Johnathan Mantey01784822019-06-18 12:44:21 -07001737 {
1738 // Requesting a DELETE/DO NOT MODIFY action for an item
1739 // that isn't present on the eth(n) interface. Input JSON is
1740 // in error, so bail out.
1741 if (thisJson.is_null())
1742 {
1743 messages::resourceCannotBeDeleted(asyncResp->res);
1744 return;
1745 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07001746 messages::propertyValueFormatError(
Ed Tanous71f52d92021-02-19 08:51:17 -08001747 asyncResp->res,
1748 thisJson.dump(2, ' ', true,
1749 nlohmann::json::error_handler_t::replace),
1750 pathString);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001751 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001752 }
1753
Johnathan Mantey01784822019-06-18 12:44:21 -07001754 if (thisJson.is_null())
1755 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001756 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001757 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001758 if (niciPentry != ipv6Data.cend())
Johnathan Mantey01784822019-06-18 12:44:21 -07001759 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001760 niciPentry =
1761 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
Johnathan Mantey01784822019-06-18 12:44:21 -07001762 }
1763 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001764 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001765 }
1766 }
1767
Ed Tanous0f74e642018-11-12 15:17:05 -08001768 void parseInterfaceData(
zhanghch058d1b46d2021-04-01 11:18:24 +08001769 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1770 const std::string& ifaceId, const EthernetInterfaceData& ethData,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001771 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1772 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001773 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001774 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001775 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1776
Ed Tanous2c70f802020-09-28 14:29:23 -07001777 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
Ed Tanous81ce6092020-12-17 16:54:55 +00001778 jsonResponse["Id"] = ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001779 jsonResponse["@odata.id"] =
Ed Tanous81ce6092020-12-17 16:54:55 +00001780 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
Ed Tanous2c70f802020-09-28 14:29:23 -07001781 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001782
1783 auto health = std::make_shared<HealthPopulate>(asyncResp);
1784
1785 crow::connections::systemBus->async_method_call(
1786 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001787 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001788 if (ec)
1789 {
1790 return;
1791 }
1792
1793 health->inventory = std::move(resp);
1794 },
1795 "xyz.openbmc_project.ObjectMapper",
1796 "/xyz/openbmc_project/object_mapper",
1797 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1798 int32_t(0), inventoryForEthernet);
1799
1800 health->populate();
1801
1802 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001803 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001804 jsonResponse["LinkStatus"] = "LinkUp";
1805 jsonResponse["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001806 }
1807 else
1808 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001809 jsonResponse["LinkStatus"] = "NoLink";
1810 jsonResponse["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001811 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001812
Ed Tanous2c70f802020-09-28 14:29:23 -07001813 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1814 jsonResponse["SpeedMbps"] = ethData.speed;
1815 jsonResponse["MACAddress"] = ethData.mac_address;
1816 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001817 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
Ed Tanous2c70f802020-09-28 14:29:23 -07001818 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1819 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1820 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001821
Ed Tanous2c70f802020-09-28 14:29:23 -07001822 jsonResponse["DHCPv6"]["OperatingMode"] =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001823 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1824 : "Disabled";
Ed Tanous2c70f802020-09-28 14:29:23 -07001825 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1826 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1827 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301828
Ed Tanous4a0cb852018-10-15 07:55:04 -07001829 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001830 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001831 jsonResponse["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301832
1833 // When domain name is empty then it means, that it is a network
1834 // without domain names, and the host name itself must be treated as
1835 // FQDN
Ed Tanousf23b7292020-10-15 09:41:17 -07001836 std::string fqdn = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001837 if (!ethData.domainnames.empty())
1838 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001839 fqdn += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001840 }
Ed Tanous2c70f802020-09-28 14:29:23 -07001841 jsonResponse["FQDN"] = fqdn;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001842 }
1843
Ed Tanous2c70f802020-09-28 14:29:23 -07001844 jsonResponse["VLANs"] = {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001845 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous81ce6092020-12-17 16:54:55 +00001846 ifaceId + "/VLANs"}};
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001847
Ed Tanous2c70f802020-09-28 14:29:23 -07001848 jsonResponse["NameServers"] = ethData.nameServers;
1849 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001850
Ed Tanous2c70f802020-09-28 14:29:23 -07001851 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1852 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1853 ipv4Array = nlohmann::json::array();
1854 ipv4StaticArray = nlohmann::json::array();
1855 for (auto& ipv4Config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001856 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001857
Ed Tanous2c70f802020-09-28 14:29:23 -07001858 std::string gatewayStr = ipv4Config.gateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001859 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001860 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001861 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001862 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001863
Ed Tanous2c70f802020-09-28 14:29:23 -07001864 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1865 {"SubnetMask", ipv4Config.netmask},
1866 {"Address", ipv4Config.address},
1867 {"Gateway", gatewayStr}});
1868 if (ipv4Config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001869 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001870 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1871 {"SubnetMask", ipv4Config.netmask},
1872 {"Address", ipv4Config.address},
1873 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001874 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001875 }
1876
Ravi Teja7ea79e52021-03-02 08:38:05 +05301877 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1878 if (ipv6GatewayStr.empty())
1879 {
1880 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1881 }
1882
1883 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001884
Ed Tanous2c70f802020-09-28 14:29:23 -07001885 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1886 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1887 ipv6Array = nlohmann::json::array();
1888 ipv6StaticArray = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001889 nlohmann::json& ipv6AddrPolicyTable =
Ed Tanous2c70f802020-09-28 14:29:23 -07001890 jsonResponse["IPv6AddressPolicyTable"];
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001891 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous2c70f802020-09-28 14:29:23 -07001892 for (auto& ipv6Config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001893 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001894 ipv6Array.push_back({{"Address", ipv6Config.address},
1895 {"PrefixLength", ipv6Config.prefixLength},
1896 {"AddressOrigin", ipv6Config.origin},
1897 {"AddressState", nullptr}});
1898 if (ipv6Config.origin == "Static")
Johnathan Mantey01784822019-06-18 12:44:21 -07001899 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001900 ipv6StaticArray.push_back(
1901 {{"Address", ipv6Config.address},
1902 {"PrefixLength", ipv6Config.prefixLength},
1903 {"AddressOrigin", ipv6Config.origin},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001904 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001905 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001906 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001907 }
1908
1909 /**
1910 * Functions triggers appropriate requests on DBus
1911 */
zhanghch058d1b46d2021-04-01 11:18:24 +08001912 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1913 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001914 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001915 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001916 if (params.size() != 1)
1917 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001918 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001919 return;
1920 }
1921
Ed Tanous4a0cb852018-10-15 07:55:04 -07001922 getEthernetIfaceData(
1923 params[0],
Ed Tanous2c70f802020-09-28 14:29:23 -07001924 [this, asyncResp, ifaceId{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001925 const bool& success, const EthernetInterfaceData& ethData,
1926 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1927 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001928 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001929 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001930 // TODO(Pawel)consider distinguish between non existing
1931 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001932 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07001933 "EthernetInterface", ifaceId);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001934 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001935 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001936
Ed Tanous0f74e642018-11-12 15:17:05 -08001937 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001938 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001939 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1940 asyncResp->res.jsonValue["Description"] =
1941 "Management Network Interface";
1942
Ed Tanous2c70f802020-09-28 14:29:23 -07001943 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001944 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001945 });
1946 }
1947
zhanghch058d1b46d2021-04-01 11:18:24 +08001948 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1949 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001950 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001951 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001952
Ed Tanous1abe55e2018-09-05 08:30:59 -07001953 if (params.size() != 1)
1954 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001955 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001956 return;
1957 }
1958
Ed Tanous2c70f802020-09-28 14:29:23 -07001959 const std::string& ifaceId = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001960
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001961 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301962 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301963 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001964 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001965 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001966 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001967 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001968 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001969 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001970 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001971 DHCPParameters v4dhcpParms;
1972 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001973
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001974 if (!json_util::readJson(
zhanghch058d1b46d2021-04-01 11:18:24 +08001975 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301976 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1977 macAddress, "StaticNameServers", staticNameServers,
1978 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1979 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1980 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001981 {
1982 return;
1983 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001984 if (dhcpv4)
1985 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001986 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001987 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1988 v4dhcpParms.useDNSServers, "UseNTPServers",
1989 v4dhcpParms.useNTPServers, "UseDomainName",
1990 v4dhcpParms.useUseDomainName))
1991 {
1992 return;
1993 }
1994 }
1995
1996 if (dhcpv6)
1997 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001998 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001999 v6dhcpParms.dhcpv6OperatingMode,
2000 "UseDNSServers", v6dhcpParms.useDNSServers,
2001 "UseNTPServers", v6dhcpParms.useNTPServers,
2002 "UseDomainName",
2003 v6dhcpParms.useUseDomainName))
2004 {
2005 return;
2006 }
Jennifer Leeda131a92019-04-24 15:13:55 -07002007 }
2008
Johnathan Mantey01784822019-06-18 12:44:21 -07002009 // Get single eth interface data, and call the below callback for
2010 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002011 getEthernetIfaceData(
Ed Tanous2c70f802020-09-28 14:29:23 -07002012 ifaceId,
2013 [this, asyncResp, ifaceId, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302014 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05002015 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002016 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002017 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002018 staticNameServers = std::move(staticNameServers),
2019 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
2020 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousf23b7292020-10-15 09:41:17 -07002021 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002022 const bool& success, const EthernetInterfaceData& ethData,
2023 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2024 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002025 if (!success)
2026 {
2027 // ... otherwise return error
2028 // TODO(Pawel)consider distinguish between non existing
2029 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002030 messages::resourceNotFound(asyncResp->res,
Ed Tanous2c70f802020-09-28 14:29:23 -07002031 "Ethernet Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002032 return;
2033 }
2034
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002035 if (dhcpv4 || dhcpv6)
2036 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002037 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2038 asyncResp);
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002039 }
2040
Ed Tanous0627a2c2018-11-29 17:09:23 -08002041 if (hostname)
2042 {
2043 handleHostnamePatch(*hostname, asyncResp);
2044 }
2045
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302046 if (fqdn)
2047 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002048 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302049 }
2050
Ratan Guptad5776652019-03-03 08:47:22 +05302051 if (macAddress)
2052 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002053 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
Ratan Guptad5776652019-03-03 08:47:22 +05302054 }
2055
Ravi Tejad1d50812019-06-23 16:20:27 -05002056 if (ipv4StaticAddresses)
2057 {
Ed Tanous537174c2018-12-10 15:09:31 -08002058 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002059 // above is returning a const value, not a non-const
2060 // value. This doesn't really work for us, as we need to
2061 // be able to efficiently move out the intermedia
2062 // nlohmann::json objects. This makes a copy of the
2063 // structure, and operates on that, but could be done
2064 // more efficiently
Ed Tanousf23b7292020-10-15 09:41:17 -07002065 nlohmann::json ipv4Static = *ipv4StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002066 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002067 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002068 }
2069
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002070 if (staticNameServers)
2071 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002072 handleStaticNameServersPatch(ifaceId, *staticNameServers,
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002073 asyncResp);
2074 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002075
2076 if (ipv6DefaultGateway)
2077 {
2078 messages::propertyNotWritable(asyncResp->res,
2079 "IPv6DefaultGateway");
2080 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002081
2082 if (ipv6StaticAddresses)
2083 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002084 nlohmann::json ipv6Static = *ipv6StaticAddresses;
Ed Tanous2c70f802020-09-28 14:29:23 -07002085 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002086 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002087 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002088
2089 if (interfaceEnabled)
2090 {
2091 setEthernetInterfaceBoolProperty(
Ed Tanous2c70f802020-09-28 14:29:23 -07002092 ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002093 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002094 });
2095 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002096};
2097
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002098/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002099 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2100 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002101 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002102class VlanNetworkInterface : public Node
2103{
2104 public:
2105 /*
2106 * Default Constructor
2107 */
Ed Tanous52cc1122020-07-18 13:51:21 -07002108 VlanNetworkInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002109 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002110 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002111 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002112 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002113 entityPrivileges = {
2114 {boost::beast::http::verb::get, {{"Login"}}},
2115 {boost::beast::http::verb::head, {{"Login"}}},
2116 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2117 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2118 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2119 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002120 }
2121
Ed Tanous1abe55e2018-09-05 08:30:59 -07002122 private:
Ed Tanous81ce6092020-12-17 16:54:55 +00002123 void parseInterfaceData(nlohmann::json& jsonResponse,
2124 const std::string& parentIfaceId,
2125 const std::string& ifaceId,
Ed Tanouscb13a392020-07-25 19:02:03 +00002126 const EthernetInterfaceData& ethData)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002127 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002128 // Fill out obvious data...
Ed Tanous81ce6092020-12-17 16:54:55 +00002129 jsonResponse["Id"] = ifaceId;
2130 jsonResponse["@odata.id"] =
2131 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parentIfaceId +
2132 "/VLANs/" + ifaceId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002133
Ed Tanous81ce6092020-12-17 16:54:55 +00002134 jsonResponse["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002135 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002136 {
Ed Tanous81ce6092020-12-17 16:54:55 +00002137 jsonResponse["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002138 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002139 }
2140
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002141 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002142 {
2143 if (!boost::starts_with(iface, parent + "_"))
2144 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002145 return false;
2146 }
Ed Tanous3174e4d2020-10-07 11:41:22 -07002147 return true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002148 }
2149
2150 /**
2151 * Functions triggers appropriate requests on DBus
2152 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002153 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2154 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002155 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002156 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002157 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002158 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2159 // Check if there is required param, truly entering this shall be
2160 // impossible.
2161 if (params.size() != 2)
2162 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002163 messages::internalError(asyncResp->res);
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002164 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002165 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002166
Ed Tanous2c70f802020-09-28 14:29:23 -07002167 const std::string& parentIfaceId = params[0];
2168 const std::string& ifaceId = params[1];
zhanghch058d1b46d2021-04-01 11:18:24 +08002169 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanous0f74e642018-11-12 15:17:05 -08002170 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
zhanghch058d1b46d2021-04-01 11:18:24 +08002171 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002172
Ed Tanous2c70f802020-09-28 14:29:23 -07002173 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002174 {
2175 return;
2176 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002177
Johnathan Mantey01784822019-06-18 12:44:21 -07002178 // Get single eth interface data, and call the below callback for
2179 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002180 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002181 params[1],
Ed Tanous2c70f802020-09-28 14:29:23 -07002182 [this, asyncResp, parentIfaceId{std::string(params[0])},
2183 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002184 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002185 const boost::container::flat_set<IPv4AddressData>&,
2186 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002187 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002188 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002189 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2190 ifaceId, ethData);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002191 }
2192 else
2193 {
2194 // ... otherwise return error
2195 // TODO(Pawel)consider distinguish between non existing
2196 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002197 messages::resourceNotFound(
Ed Tanous2c70f802020-09-28 14:29:23 -07002198 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002199 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002200 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002201 }
2202
zhanghch058d1b46d2021-04-01 11:18:24 +08002203 void doPatch(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2204 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002205 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002206 {
zhanghch058d1b46d2021-04-01 11:18:24 +08002207
Ed Tanous1abe55e2018-09-05 08:30:59 -07002208 if (params.size() != 2)
2209 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002210 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002211 return;
2212 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002213
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002214 const std::string& parentIfaceId = params[0];
2215 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002216
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002217 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002218 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002219 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2220 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002221 return;
2222 }
2223
Ed Tanous0627a2c2018-11-29 17:09:23 -08002224 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002225 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002226
zhanghch058d1b46d2021-04-01 11:18:24 +08002227 if (!json_util::readJson(req, asyncResp->res, "VLANEnable", vlanEnable,
2228 "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002229 {
2230 return;
2231 }
2232
Johnathan Mantey01784822019-06-18 12:44:21 -07002233 // Get single eth interface data, and call the below callback for
2234 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002235 getEthernetIfaceData(
2236 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002237 [asyncResp, parentIfaceId{std::string(params[0])},
Ed Tanouscb13a392020-07-25 19:02:03 +00002238 ifaceId{std::string(params[1])}, &vlanEnable,
2239 &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2240 const boost::container::flat_set<IPv4AddressData>&,
2241 const boost::container::flat_set<IPv6AddressData>&) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002242 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002243 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002244 auto callback =
2245 [asyncResp](const boost::system::error_code ec) {
2246 if (ec)
2247 {
2248 messages::internalError(asyncResp->res);
2249 }
2250 };
2251
2252 if (vlanEnable == true)
2253 {
2254 crow::connections::systemBus->async_method_call(
2255 std::move(callback), "xyz.openbmc_project.Network",
2256 "/xyz/openbmc_project/network/" + ifaceId,
2257 "org.freedesktop.DBus.Properties", "Set",
2258 "xyz.openbmc_project.Network.VLAN", "Id",
2259 std::variant<uint32_t>(vlanId));
2260 }
2261 else
2262 {
2263 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2264 "vlan interface";
2265 crow::connections::systemBus->async_method_call(
2266 std::move(callback), "xyz.openbmc_project.Network",
2267 std::string("/xyz/openbmc_project/network/") +
2268 ifaceId,
2269 "xyz.openbmc_project.Object.Delete", "Delete");
2270 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002271 }
2272 else
2273 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002274 // TODO(Pawel)consider distinguish between non existing
2275 // object, and other errors
2276 messages::resourceNotFound(
2277 asyncResp->res, "VLAN Network Interface", ifaceId);
2278 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002279 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002280 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002281 }
2282
zhanghch058d1b46d2021-04-01 11:18:24 +08002283 void doDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2284 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002285 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002286 {
2287 if (params.size() != 2)
2288 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002289 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002290 return;
2291 }
2292
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002293 const std::string& parentIfaceId = params[0];
2294 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002295
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002296 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002297 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002298 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2299 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002300 return;
2301 }
2302
Johnathan Mantey01784822019-06-18 12:44:21 -07002303 // Get single eth interface data, and call the below callback for
2304 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002305 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002306 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002307 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002308 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002309 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002310 const boost::container::flat_set<IPv4AddressData>&,
2311 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002312 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002313 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002314 auto callback =
2315 [asyncResp](const boost::system::error_code ec) {
2316 if (ec)
2317 {
2318 messages::internalError(asyncResp->res);
2319 }
2320 };
2321 crow::connections::systemBus->async_method_call(
2322 std::move(callback), "xyz.openbmc_project.Network",
2323 std::string("/xyz/openbmc_project/network/") + ifaceId,
2324 "xyz.openbmc_project.Object.Delete", "Delete");
2325 }
2326 else
2327 {
2328 // ... otherwise return error
2329 // TODO(Pawel)consider distinguish between non existing
2330 // object, and other errors
2331 messages::resourceNotFound(
2332 asyncResp->res, "VLAN Network Interface", ifaceId);
2333 }
2334 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002335 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002336};
2337
2338/**
2339 * VlanNetworkInterfaceCollection derived class for delivering
2340 * VLANNetworkInterface Collection Schema
2341 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002342class VlanNetworkInterfaceCollection : public Node
2343{
2344 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002345 VlanNetworkInterfaceCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002346 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2347 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002348 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002349 entityPrivileges = {
2350 {boost::beast::http::verb::get, {{"Login"}}},
2351 {boost::beast::http::verb::head, {{"Login"}}},
2352 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2353 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2354 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2355 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002356 }
2357
Ed Tanous1abe55e2018-09-05 08:30:59 -07002358 private:
2359 /**
2360 * Functions triggers appropriate requests on DBus
2361 */
zhanghch058d1b46d2021-04-01 11:18:24 +08002362 void doGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2363 const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002364 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002365 {
2366 if (params.size() != 1)
2367 {
2368 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002369 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002370 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002371 }
2372
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002373 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002374
Ed Tanous4a0cb852018-10-15 07:55:04 -07002375 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002376 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002377 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002378 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002379 const bool& success,
Ed Tanous81ce6092020-12-17 16:54:55 +00002380 const boost::container::flat_set<std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002381 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002382 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002383 messages::internalError(asyncResp->res);
2384 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002385 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002386
Ed Tanous81ce6092020-12-17 16:54:55 +00002387 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002388 {
2389 messages::resourceNotFound(asyncResp->res,
2390 "VLanNetworkInterfaceCollection",
2391 rootInterfaceName);
2392 return;
2393 }
2394
Ed Tanous0f74e642018-11-12 15:17:05 -08002395 asyncResp->res.jsonValue["@odata.type"] =
2396 "#VLanNetworkInterfaceCollection."
2397 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002398 asyncResp->res.jsonValue["Name"] =
2399 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002400
Ed Tanous2c70f802020-09-28 14:29:23 -07002401 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002402
Ed Tanous81ce6092020-12-17 16:54:55 +00002403 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002404 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002405 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002406 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002407 std::string path =
2408 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2409 path += rootInterfaceName;
2410 path += "/VLANs/";
2411 path += ifaceItem;
2412 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002413 }
2414 }
2415
Jason M. Billsf12894f2018-10-09 12:45:45 -07002416 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002417 ifaceArray.size();
2418 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002419 asyncResp->res.jsonValue["@odata.id"] =
2420 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2421 rootInterfaceName + "/VLANs";
2422 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002423 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002424
zhanghch058d1b46d2021-04-01 11:18:24 +08002425 void doPost(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2426 const crow::Request& req,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002427 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002428 {
2429 if (params.size() != 1)
2430 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002431 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002432 return;
2433 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002434 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002435 uint32_t vlanId = 0;
zhanghch058d1b46d2021-04-01 11:18:24 +08002436 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2437 "VLANEnable", vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002438 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002439 return;
2440 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002441 // Need both vlanId and vlanEnable to service this request
2442 if (!vlanId)
2443 {
2444 messages::propertyMissing(asyncResp->res, "VLANId");
2445 }
2446 if (!vlanEnable)
2447 {
2448 messages::propertyMissing(asyncResp->res, "VLANEnable");
2449 }
Ed Tanous271584a2019-07-09 16:24:22 -07002450 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002451 {
2452 return;
2453 }
2454
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002455 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002456 auto callback = [asyncResp](const boost::system::error_code ec) {
2457 if (ec)
2458 {
2459 // TODO(ed) make more consistent error messages based on
2460 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002461 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002462 return;
2463 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002464 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002465 };
2466 crow::connections::systemBus->async_method_call(
2467 std::move(callback), "xyz.openbmc_project.Network",
2468 "/xyz/openbmc_project/network",
2469 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002470 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002471 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002472};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002473} // namespace redfish