blob: 6723d5bd186e51a1aab1c93e0dd4a2c17d4d21a5 [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>
Ed Tanoused398212021-06-09 17:05:54 -070023#include <registries/privilege_registry.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
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001027inline void
1028 handleHostnamePatch(const std::string& hostname,
1029 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001030{
Ed Tanousbf648f72021-06-03 15:00:14 -07001031 // SHOULD handle host names of up to 255 characters(RFC 1123)
1032 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001034 messages::propertyValueFormatError(asyncResp->res, hostname,
1035 "HostName");
1036 return;
1037 }
1038 crow::connections::systemBus->async_method_call(
1039 [asyncResp](const boost::system::error_code ec) {
1040 if (ec)
1041 {
1042 messages::internalError(asyncResp->res);
1043 }
1044 },
1045 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1046 "org.freedesktop.DBus.Properties", "Set",
1047 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1048 std::variant<std::string>(hostname));
1049}
1050
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001051inline void
1052 handleDomainnamePatch(const std::string& ifaceId,
1053 const std::string& domainname,
1054 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001055{
1056 std::vector<std::string> vectorDomainname = {domainname};
1057 crow::connections::systemBus->async_method_call(
1058 [asyncResp](const boost::system::error_code ec) {
1059 if (ec)
1060 {
1061 messages::internalError(asyncResp->res);
1062 }
1063 },
1064 "xyz.openbmc_project.Network",
1065 "/xyz/openbmc_project/network/" + ifaceId,
1066 "org.freedesktop.DBus.Properties", "Set",
1067 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1068 std::variant<std::vector<std::string>>(vectorDomainname));
1069}
1070
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001071inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001072{
1073 // A valid host name can never have the dotted-decimal form (RFC 1123)
1074 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1075 {
1076 return false;
1077 }
1078 // Each label(hostname/subdomains) within a valid FQDN
1079 // MUST handle host names of up to 63 characters (RFC 1123)
1080 // labels cannot start or end with hyphens (RFC 952)
1081 // labels can start with numbers (RFC 1123)
1082 const std::regex pattern(
1083 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1084
1085 return std::regex_match(hostname, pattern);
1086}
1087
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001088inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001089{
1090 // Can have multiple subdomains
1091 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001092 const std::regex pattern(
1093 "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
Ed Tanousbf648f72021-06-03 15:00:14 -07001094
1095 return std::regex_match(domainname, pattern);
1096}
1097
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001098inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1099 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001100{
1101 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1102 if (fqdn.length() > 255)
1103 {
1104 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1105 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 }
1107
Ed Tanousbf648f72021-06-03 15:00:14 -07001108 size_t pos = fqdn.find('.');
1109 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001111 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1112 return;
1113 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001114
Ed Tanousbf648f72021-06-03 15:00:14 -07001115 std::string hostname;
1116 std::string domainname;
1117 domainname = (fqdn).substr(pos + 1);
1118 hostname = (fqdn).substr(0, pos);
1119
1120 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1121 {
1122 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1123 return;
1124 }
1125
1126 handleHostnamePatch(hostname, asyncResp);
1127 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1128}
1129
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001130inline void
1131 handleMACAddressPatch(const std::string& ifaceId,
1132 const std::string& macAddress,
1133 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001134{
1135 crow::connections::systemBus->async_method_call(
1136 [asyncResp, macAddress](const boost::system::error_code ec) {
1137 if (ec)
1138 {
1139 messages::internalError(asyncResp->res);
1140 return;
1141 }
1142 },
1143 "xyz.openbmc_project.Network",
1144 "/xyz/openbmc_project/network/" + ifaceId,
1145 "org.freedesktop.DBus.Properties", "Set",
1146 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1147 std::variant<std::string>(macAddress));
1148}
1149
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001150inline void setDHCPEnabled(const std::string& ifaceId,
1151 const std::string& propertyName, const bool v4Value,
1152 const bool v6Value,
1153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001154{
1155 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1156 crow::connections::systemBus->async_method_call(
1157 [asyncResp](const boost::system::error_code ec) {
1158 if (ec)
1159 {
1160 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1161 messages::internalError(asyncResp->res);
1162 return;
1163 }
1164 messages::success(asyncResp->res);
1165 },
1166 "xyz.openbmc_project.Network",
1167 "/xyz/openbmc_project/network/" + ifaceId,
1168 "org.freedesktop.DBus.Properties", "Set",
1169 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1170 std::variant<std::string>{dhcp});
1171}
1172
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001173inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001174 const std::string& ifaceId, const std::string& propertyName,
1175 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1176{
1177 crow::connections::systemBus->async_method_call(
1178 [asyncResp](const boost::system::error_code ec) {
1179 if (ec)
1180 {
1181 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1182 messages::internalError(asyncResp->res);
1183 return;
1184 }
1185 },
1186 "xyz.openbmc_project.Network",
1187 "/xyz/openbmc_project/network/" + ifaceId,
1188 "org.freedesktop.DBus.Properties", "Set",
1189 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1190 std::variant<bool>{value});
1191}
1192
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001193inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001195{
1196 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1197 crow::connections::systemBus->async_method_call(
1198 [asyncResp](const boost::system::error_code ec) {
1199 if (ec)
1200 {
1201 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1202 messages::internalError(asyncResp->res);
1203 return;
1204 }
1205 },
1206 "xyz.openbmc_project.Network",
1207 "/xyz/openbmc_project/network/config/dhcp",
1208 "org.freedesktop.DBus.Properties", "Set",
1209 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1210 std::variant<bool>{value});
1211}
1212
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001213inline void handleDHCPPatch(const std::string& ifaceId,
1214 const EthernetInterfaceData& ethData,
1215 const DHCPParameters& v4dhcpParms,
1216 const DHCPParameters& v6dhcpParms,
1217 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001218{
1219 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1220 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1221
1222 bool nextv4DHCPState =
1223 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1224
1225 bool nextv6DHCPState{};
1226 if (v6dhcpParms.dhcpv6OperatingMode)
1227 {
1228 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1229 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1230 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1231 {
1232 messages::propertyValueFormatError(asyncResp->res,
1233 *v6dhcpParms.dhcpv6OperatingMode,
1234 "OperatingMode");
1235 return;
1236 }
1237 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1238 }
1239 else
1240 {
1241 nextv6DHCPState = ipv6Active;
1242 }
1243
1244 bool nextDNS{};
1245 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1246 {
1247 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1248 {
1249 messages::generalError(asyncResp->res);
1250 return;
1251 }
1252 nextDNS = *v4dhcpParms.useDNSServers;
1253 }
1254 else if (v4dhcpParms.useDNSServers)
1255 {
1256 nextDNS = *v4dhcpParms.useDNSServers;
1257 }
1258 else if (v6dhcpParms.useDNSServers)
1259 {
1260 nextDNS = *v6dhcpParms.useDNSServers;
1261 }
1262 else
1263 {
1264 nextDNS = ethData.DNSEnabled;
1265 }
1266
1267 bool nextNTP{};
1268 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1269 {
1270 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1271 {
1272 messages::generalError(asyncResp->res);
1273 return;
1274 }
1275 nextNTP = *v4dhcpParms.useNTPServers;
1276 }
1277 else if (v4dhcpParms.useNTPServers)
1278 {
1279 nextNTP = *v4dhcpParms.useNTPServers;
1280 }
1281 else if (v6dhcpParms.useNTPServers)
1282 {
1283 nextNTP = *v6dhcpParms.useNTPServers;
1284 }
1285 else
1286 {
1287 nextNTP = ethData.NTPEnabled;
1288 }
1289
1290 bool nextUseDomain{};
1291 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1292 {
1293 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1294 {
1295 messages::generalError(asyncResp->res);
1296 return;
1297 }
1298 nextUseDomain = *v4dhcpParms.useUseDomainName;
1299 }
1300 else if (v4dhcpParms.useUseDomainName)
1301 {
1302 nextUseDomain = *v4dhcpParms.useUseDomainName;
1303 }
1304 else if (v6dhcpParms.useUseDomainName)
1305 {
1306 nextUseDomain = *v6dhcpParms.useUseDomainName;
1307 }
1308 else
1309 {
1310 nextUseDomain = ethData.HostNameEnabled;
1311 }
1312
1313 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1314 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1315 asyncResp);
1316 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1317 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1318 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1319 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1320 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1321 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1322}
1323
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001324inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001325 getNextStaticIpEntry(
1326 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1327 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1328{
1329 return std::find_if(head, end, [](const IPv4AddressData& value) {
1330 return value.origin == "Static";
1331 });
1332}
1333
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001334inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001335 getNextStaticIpEntry(
1336 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1337 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1338{
1339 return std::find_if(head, end, [](const IPv6AddressData& value) {
1340 return value.origin == "Static";
1341 });
1342}
1343
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001344inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001345 const std::string& ifaceId, nlohmann::json& input,
1346 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1347 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1348{
1349 if ((!input.is_array()) || input.empty())
1350 {
1351 messages::propertyValueTypeError(
1352 asyncResp->res,
1353 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1354 "IPv4StaticAddresses");
1355 return;
1356 }
1357
1358 unsigned entryIdx = 1;
1359 // Find the first static IP address currently active on the NIC and
1360 // match it to the first JSON element in the IPv4StaticAddresses array.
1361 // Match each subsequent JSON element to the next static IP programmed
1362 // into the NIC.
1363 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1364 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1365
1366 for (nlohmann::json& thisJson : input)
1367 {
1368 std::string pathString =
1369 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1370
1371 if (!thisJson.is_null() && !thisJson.empty())
1372 {
1373 std::optional<std::string> address;
1374 std::optional<std::string> subnetMask;
1375 std::optional<std::string> gateway;
1376
1377 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1378 address, "SubnetMask", subnetMask,
1379 "Gateway", gateway))
1380 {
1381 messages::propertyValueFormatError(
1382 asyncResp->res,
1383 thisJson.dump(2, ' ', true,
1384 nlohmann::json::error_handler_t::replace),
1385 pathString);
1386 return;
1387 }
1388
1389 // Find the address/subnet/gateway values. Any values that are
1390 // not explicitly provided are assumed to be unmodified from the
1391 // current state of the interface. Merge existing state into the
1392 // current request.
1393 const std::string* addr = nullptr;
1394 const std::string* gw = nullptr;
1395 uint8_t prefixLength = 0;
1396 bool errorInEntry = false;
1397 if (address)
1398 {
1399 if (ipv4VerifyIpAndGetBitcount(*address))
1400 {
1401 addr = &(*address);
1402 }
1403 else
1404 {
1405 messages::propertyValueFormatError(asyncResp->res, *address,
1406 pathString + "/Address");
1407 errorInEntry = true;
1408 }
1409 }
1410 else if (niciPentry != ipv4Data.cend())
1411 {
1412 addr = &(niciPentry->address);
1413 }
1414 else
1415 {
1416 messages::propertyMissing(asyncResp->res,
1417 pathString + "/Address");
1418 errorInEntry = true;
1419 }
1420
1421 if (subnetMask)
1422 {
1423 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1424 {
1425 messages::propertyValueFormatError(
1426 asyncResp->res, *subnetMask,
1427 pathString + "/SubnetMask");
1428 errorInEntry = true;
1429 }
1430 }
1431 else if (niciPentry != ipv4Data.cend())
1432 {
1433 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
1434 &prefixLength))
1435 {
1436 messages::propertyValueFormatError(
1437 asyncResp->res, niciPentry->netmask,
1438 pathString + "/SubnetMask");
1439 errorInEntry = true;
1440 }
1441 }
1442 else
1443 {
1444 messages::propertyMissing(asyncResp->res,
1445 pathString + "/SubnetMask");
1446 errorInEntry = true;
1447 }
1448
1449 if (gateway)
1450 {
1451 if (ipv4VerifyIpAndGetBitcount(*gateway))
1452 {
1453 gw = &(*gateway);
1454 }
1455 else
1456 {
1457 messages::propertyValueFormatError(asyncResp->res, *gateway,
1458 pathString + "/Gateway");
1459 errorInEntry = true;
1460 }
1461 }
1462 else if (niciPentry != ipv4Data.cend())
1463 {
1464 gw = &niciPentry->gateway;
1465 }
1466 else
1467 {
1468 messages::propertyMissing(asyncResp->res,
1469 pathString + "/Gateway");
1470 errorInEntry = true;
1471 }
1472
1473 if (errorInEntry)
1474 {
1475 return;
1476 }
1477
1478 if (niciPentry != ipv4Data.cend())
1479 {
1480 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw,
1481 *addr, asyncResp);
1482 niciPentry =
1483 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1484 }
1485 else
1486 {
1487 createIPv4(ifaceId, prefixLength, *gateway, *address,
1488 asyncResp);
1489 }
1490 entryIdx++;
1491 }
1492 else
1493 {
1494 if (niciPentry == ipv4Data.cend())
1495 {
1496 // Requesting a DELETE/DO NOT MODIFY action for an item
1497 // that isn't present on the eth(n) interface. Input JSON is
1498 // in error, so bail out.
1499 if (thisJson.is_null())
1500 {
1501 messages::resourceCannotBeDeleted(asyncResp->res);
1502 return;
1503 }
1504 messages::propertyValueFormatError(
1505 asyncResp->res,
1506 thisJson.dump(2, ' ', true,
1507 nlohmann::json::error_handler_t::replace),
1508 pathString);
1509 return;
1510 }
1511
1512 if (thisJson.is_null())
1513 {
1514 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
1515 }
1516 if (niciPentry != ipv4Data.cend())
1517 {
1518 niciPentry =
1519 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1520 }
1521 entryIdx++;
1522 }
1523 }
1524}
1525
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001526inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001527 const std::string& ifaceId,
1528 const std::vector<std::string>& updatedStaticNameServers,
1529 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1530{
1531 crow::connections::systemBus->async_method_call(
1532 [asyncResp](const boost::system::error_code ec) {
1533 if (ec)
1534 {
1535 messages::internalError(asyncResp->res);
1536 return;
1537 }
1538 },
1539 "xyz.openbmc_project.Network",
1540 "/xyz/openbmc_project/network/" + ifaceId,
1541 "org.freedesktop.DBus.Properties", "Set",
1542 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1543 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1544}
1545
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001546inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001547 const std::string& ifaceId, const nlohmann::json& input,
1548 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1549 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1550{
1551 if (!input.is_array() || input.empty())
1552 {
1553 messages::propertyValueTypeError(
1554 asyncResp->res,
1555 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1556 "IPv6StaticAddresses");
1557 return;
1558 }
1559 size_t entryIdx = 1;
1560 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1561 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1562 for (const nlohmann::json& thisJson : input)
1563 {
1564 std::string pathString =
1565 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1566
1567 if (!thisJson.is_null() && !thisJson.empty())
1568 {
1569 std::optional<std::string> address;
1570 std::optional<uint8_t> prefixLength;
1571 nlohmann::json thisJsonCopy = thisJson;
1572 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1573 address, "PrefixLength", prefixLength))
1574 {
1575 messages::propertyValueFormatError(
1576 asyncResp->res,
1577 thisJson.dump(2, ' ', true,
1578 nlohmann::json::error_handler_t::replace),
1579 pathString);
1580 return;
1581 }
1582
1583 const std::string* addr;
1584 uint8_t prefix;
1585
1586 // Find the address and prefixLength values. Any values that are
1587 // not explicitly provided are assumed to be unmodified from the
1588 // current state of the interface. Merge existing state into the
1589 // current request.
1590 if (address)
1591 {
1592 addr = &(*address);
1593 }
1594 else if (niciPentry != ipv6Data.end())
1595 {
1596 addr = &(niciPentry->address);
1597 }
1598 else
1599 {
1600 messages::propertyMissing(asyncResp->res,
1601 pathString + "/Address");
1602 return;
1603 }
1604
1605 if (prefixLength)
1606 {
1607 prefix = *prefixLength;
1608 }
1609 else if (niciPentry != ipv6Data.end())
1610 {
1611 prefix = niciPentry->prefixLength;
1612 }
1613 else
1614 {
1615 messages::propertyMissing(asyncResp->res,
1616 pathString + "/PrefixLength");
1617 return;
1618 }
1619
1620 if (niciPentry != ipv6Data.end())
1621 {
1622 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1623 asyncResp);
1624 niciPentry =
1625 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1626 }
1627 else
1628 {
1629 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1630 }
1631 entryIdx++;
1632 }
1633 else
1634 {
1635 if (niciPentry == ipv6Data.end())
1636 {
1637 // Requesting a DELETE/DO NOT MODIFY action for an item
1638 // that isn't present on the eth(n) interface. Input JSON is
1639 // in error, so bail out.
1640 if (thisJson.is_null())
1641 {
1642 messages::resourceCannotBeDeleted(asyncResp->res);
1643 return;
1644 }
1645 messages::propertyValueFormatError(
1646 asyncResp->res,
1647 thisJson.dump(2, ' ', true,
1648 nlohmann::json::error_handler_t::replace),
1649 pathString);
1650 return;
1651 }
1652
1653 if (thisJson.is_null())
1654 {
1655 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
1656 }
1657 if (niciPentry != ipv6Data.cend())
1658 {
1659 niciPentry =
1660 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1661 }
1662 entryIdx++;
1663 }
1664 }
1665}
1666
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001667inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001668 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1669 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1670 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1671 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1672{
1673 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1674 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1675
1676 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1677 jsonResponse["Id"] = ifaceId;
1678 jsonResponse["@odata.id"] =
1679 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1680 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1681
1682 auto health = std::make_shared<HealthPopulate>(asyncResp);
1683
1684 crow::connections::systemBus->async_method_call(
1685 [health](const boost::system::error_code ec,
1686 std::vector<std::string>& resp) {
1687 if (ec)
1688 {
1689 return;
1690 }
1691
1692 health->inventory = std::move(resp);
1693 },
1694 "xyz.openbmc_project.ObjectMapper",
1695 "/xyz/openbmc_project/object_mapper",
1696 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1697 inventoryForEthernet);
1698
1699 health->populate();
1700
1701 if (ethData.nicEnabled)
1702 {
1703 jsonResponse["LinkStatus"] = "LinkUp";
1704 jsonResponse["Status"]["State"] = "Enabled";
1705 }
1706 else
1707 {
1708 jsonResponse["LinkStatus"] = "NoLink";
1709 jsonResponse["Status"]["State"] = "Disabled";
1710 }
1711
1712 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1713 jsonResponse["SpeedMbps"] = ethData.speed;
1714 jsonResponse["MACAddress"] = ethData.mac_address;
1715 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1716 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1717 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1718 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1719 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1720
1721 jsonResponse["DHCPv6"]["OperatingMode"] =
1722 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1723 : "Disabled";
1724 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1725 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1726 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1727
1728 if (!ethData.hostname.empty())
1729 {
1730 jsonResponse["HostName"] = ethData.hostname;
1731
1732 // When domain name is empty then it means, that it is a network
1733 // without domain names, and the host name itself must be treated as
1734 // FQDN
1735 std::string fqdn = ethData.hostname;
1736 if (!ethData.domainnames.empty())
1737 {
1738 fqdn += "." + ethData.domainnames[0];
1739 }
1740 jsonResponse["FQDN"] = fqdn;
1741 }
1742
1743 jsonResponse["VLANs"] = {
1744 {"@odata.id",
1745 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1746
1747 jsonResponse["NameServers"] = ethData.nameServers;
1748 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1749
1750 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1751 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1752 ipv4Array = nlohmann::json::array();
1753 ipv4StaticArray = nlohmann::json::array();
1754 for (auto& ipv4Config : ipv4Data)
1755 {
1756
1757 std::string gatewayStr = ipv4Config.gateway;
1758 if (gatewayStr.empty())
1759 {
1760 gatewayStr = "0.0.0.0";
1761 }
1762
1763 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1764 {"SubnetMask", ipv4Config.netmask},
1765 {"Address", ipv4Config.address},
1766 {"Gateway", gatewayStr}});
1767 if (ipv4Config.origin == "Static")
1768 {
1769 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1770 {"SubnetMask", ipv4Config.netmask},
1771 {"Address", ipv4Config.address},
1772 {"Gateway", gatewayStr}});
1773 }
1774 }
1775
1776 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1777 if (ipv6GatewayStr.empty())
1778 {
1779 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1780 }
1781
1782 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1783
1784 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1785 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1786 ipv6Array = nlohmann::json::array();
1787 ipv6StaticArray = nlohmann::json::array();
1788 nlohmann::json& ipv6AddrPolicyTable =
1789 jsonResponse["IPv6AddressPolicyTable"];
1790 ipv6AddrPolicyTable = nlohmann::json::array();
1791 for (auto& ipv6Config : ipv6Data)
1792 {
1793 ipv6Array.push_back({{"Address", ipv6Config.address},
1794 {"PrefixLength", ipv6Config.prefixLength},
1795 {"AddressOrigin", ipv6Config.origin},
1796 {"AddressState", nullptr}});
1797 if (ipv6Config.origin == "Static")
1798 {
1799 ipv6StaticArray.push_back(
1800 {{"Address", ipv6Config.address},
1801 {"PrefixLength", ipv6Config.prefixLength},
1802 {"AddressOrigin", ipv6Config.origin},
1803 {"AddressState", nullptr}});
1804 }
1805 }
1806}
1807
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001808inline void parseInterfaceData(nlohmann::json& jsonResponse,
1809 const std::string& parentIfaceId,
1810 const std::string& ifaceId,
1811 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001812{
1813 // Fill out obvious data...
1814 jsonResponse["Id"] = ifaceId;
1815 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1816 parentIfaceId + "/VLANs/" + ifaceId;
1817
1818 jsonResponse["VLANEnable"] = true;
1819 if (!ethData.vlan_id.empty())
1820 {
1821 jsonResponse["VLANId"] = ethData.vlan_id.back();
1822 }
1823}
1824
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001825inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001826{
1827 if (!boost::starts_with(iface, parent + "_"))
1828 {
1829 return false;
1830 }
1831 return true;
1832}
1833
1834inline void requestEthernetInterfacesRoutes(App& app)
1835{
1836 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001837 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07001838 .methods(
1839 boost::beast::http::verb::
1840 get)([](const crow::Request&,
1841 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1842 asyncResp->res.jsonValue["@odata.type"] =
1843 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1844 asyncResp->res.jsonValue["@odata.id"] =
1845 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1846 asyncResp->res.jsonValue["Name"] =
1847 "Ethernet Network Interface Collection";
1848 asyncResp->res.jsonValue["Description"] =
1849 "Collection of EthernetInterfaces for this Manager";
1850
1851 // Get eth interface list, and call the below callback for JSON
1852 // preparation
1853 getEthernetIfaceList([asyncResp](const bool& success,
1854 const boost::container::flat_set<
1855 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001856 if (!success)
1857 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001858 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001859 return;
1860 }
1861
Ed Tanous2c70f802020-09-28 14:29:23 -07001862 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001863 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001864 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001865 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001866 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001867 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001868 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001869 if (found == std::string::npos)
1870 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001871 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001872 {{"@odata.id",
1873 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001874 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001875 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001876 }
1877
Ed Tanous4c9afe42019-05-03 16:59:57 -07001878 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001879 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001880 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001881 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001882 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301883 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001884
Ed Tanousbf648f72021-06-03 15:00:14 -07001885 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001886 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001887 .methods(boost::beast::http::verb::get)(
1888 [](const crow::Request&,
1889 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1890 const std::string& ifaceId) {
1891 getEthernetIfaceData(
1892 ifaceId,
1893 [asyncResp,
1894 ifaceId](const bool& success,
1895 const EthernetInterfaceData& ethData,
1896 const boost::container::flat_set<IPv4AddressData>&
1897 ipv4Data,
1898 const boost::container::flat_set<IPv6AddressData>&
1899 ipv6Data) {
1900 if (!success)
1901 {
1902 // TODO(Pawel)consider distinguish between non
1903 // existing object, and other errors
1904 messages::resourceNotFound(
1905 asyncResp->res, "EthernetInterface", ifaceId);
1906 return;
1907 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001908
Ed Tanousbf648f72021-06-03 15:00:14 -07001909 asyncResp->res.jsonValue["@odata.type"] =
1910 "#EthernetInterface.v1_4_1.EthernetInterface";
1911 asyncResp->res.jsonValue["Name"] =
1912 "Manager Ethernet Interface";
1913 asyncResp->res.jsonValue["Description"] =
1914 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301915
Ed Tanousbf648f72021-06-03 15:00:14 -07001916 parseInterfaceData(asyncResp, ifaceId, ethData,
1917 ipv4Data, ipv6Data);
1918 });
1919 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001920
Ed Tanousbf648f72021-06-03 15:00:14 -07001921 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001922 .privileges(redfish::privileges::patchEthernetInterface)
1923
Ed Tanousbf648f72021-06-03 15:00:14 -07001924 .methods(boost::beast::http::verb::patch)(
1925 [](const crow::Request& req,
1926 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1927 const std::string& ifaceId) {
1928 std::optional<std::string> hostname;
1929 std::optional<std::string> fqdn;
1930 std::optional<std::string> macAddress;
1931 std::optional<std::string> ipv6DefaultGateway;
1932 std::optional<nlohmann::json> ipv4StaticAddresses;
1933 std::optional<nlohmann::json> ipv6StaticAddresses;
1934 std::optional<std::vector<std::string>> staticNameServers;
1935 std::optional<nlohmann::json> dhcpv4;
1936 std::optional<nlohmann::json> dhcpv6;
1937 std::optional<bool> interfaceEnabled;
1938 DHCPParameters v4dhcpParms;
1939 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940
Ed Tanousbf648f72021-06-03 15:00:14 -07001941 if (!json_util::readJson(
1942 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1943 "IPv4StaticAddresses", ipv4StaticAddresses,
1944 "MACAddress", macAddress, "StaticNameServers",
1945 staticNameServers, "IPv6DefaultGateway",
1946 ipv6DefaultGateway, "IPv6StaticAddresses",
1947 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1948 "InterfaceEnabled", interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001949 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001950 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001951 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001952 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001953 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001954 if (!json_util::readJson(
1955 *dhcpv4, asyncResp->res, "DHCPEnabled",
1956 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1957 v4dhcpParms.useDNSServers, "UseNTPServers",
1958 v4dhcpParms.useNTPServers, "UseDomainName",
1959 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001960 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001961 return;
1962 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001963 }
1964
Ed Tanousbf648f72021-06-03 15:00:14 -07001965 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001966 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001967 if (!json_util::readJson(
1968 *dhcpv6, asyncResp->res, "OperatingMode",
1969 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1970 v6dhcpParms.useDNSServers, "UseNTPServers",
1971 v6dhcpParms.useNTPServers, "UseDomainName",
1972 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001973 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001974 return;
1975 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001976 }
1977
Ed Tanousbf648f72021-06-03 15:00:14 -07001978 // Get single eth interface data, and call the below callback
1979 // for JSON preparation
1980 getEthernetIfaceData(
1981 ifaceId,
1982 [asyncResp, ifaceId, hostname = std::move(hostname),
1983 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1984 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1985 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1986 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1987 staticNameServers = std::move(staticNameServers),
1988 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1989 v4dhcpParms = std::move(v4dhcpParms),
1990 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1991 const bool& success,
1992 const EthernetInterfaceData& ethData,
1993 const boost::container::flat_set<IPv4AddressData>&
1994 ipv4Data,
1995 const boost::container::flat_set<IPv6AddressData>&
1996 ipv6Data) {
1997 if (!success)
1998 {
1999 // ... otherwise return error
2000 // TODO(Pawel)consider distinguish between non
2001 // existing object, and other errors
2002 messages::resourceNotFound(
2003 asyncResp->res, "Ethernet Interface", ifaceId);
2004 return;
2005 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002006
Ed Tanousbf648f72021-06-03 15:00:14 -07002007 if (dhcpv4 || dhcpv6)
2008 {
2009 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2010 v6dhcpParms, asyncResp);
2011 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002012
Ed Tanousbf648f72021-06-03 15:00:14 -07002013 if (hostname)
2014 {
2015 handleHostnamePatch(*hostname, asyncResp);
2016 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002017
Ed Tanousbf648f72021-06-03 15:00:14 -07002018 if (fqdn)
2019 {
2020 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2021 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002022
Ed Tanousbf648f72021-06-03 15:00:14 -07002023 if (macAddress)
2024 {
2025 handleMACAddressPatch(ifaceId, *macAddress,
2026 asyncResp);
2027 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002028
Ed Tanousbf648f72021-06-03 15:00:14 -07002029 if (ipv4StaticAddresses)
2030 {
2031 // TODO(ed) for some reason the capture of
2032 // ipv4Addresses above is returning a const value,
2033 // not a non-const value. This doesn't really work
2034 // for us, as we need to be able to efficiently move
2035 // out the intermedia nlohmann::json objects. This
2036 // makes a copy of the structure, and operates on
2037 // that, but could be done more efficiently
2038 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2039 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2040 asyncResp);
2041 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002042
Ed Tanousbf648f72021-06-03 15:00:14 -07002043 if (staticNameServers)
2044 {
2045 handleStaticNameServersPatch(
2046 ifaceId, *staticNameServers, asyncResp);
2047 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002048
Ed Tanousbf648f72021-06-03 15:00:14 -07002049 if (ipv6DefaultGateway)
2050 {
2051 messages::propertyNotWritable(asyncResp->res,
2052 "IPv6DefaultGateway");
2053 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002054
Ed Tanousbf648f72021-06-03 15:00:14 -07002055 if (ipv6StaticAddresses)
2056 {
2057 nlohmann::json ipv6Static = *ipv6StaticAddresses;
2058 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2059 ipv6Data, asyncResp);
2060 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002061
Ed Tanousbf648f72021-06-03 15:00:14 -07002062 if (interfaceEnabled)
2063 {
2064 setEthernetInterfaceBoolProperty(
2065 ifaceId, "NICEnabled", *interfaceEnabled,
2066 asyncResp);
2067 }
2068 });
2069 });
manojkiraneda2a133282019-02-19 13:09:43 +05302070
Ed Tanousbf648f72021-06-03 15:00:14 -07002071 BMCWEB_ROUTE(
2072 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002073 .privileges(redfish::privileges::getVLanNetworkInterface)
2074
Ed Tanousbf648f72021-06-03 15:00:14 -07002075 .methods(boost::beast::http::verb::get)(
2076 [](const crow::Request& /* req */,
2077 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2078 const std::string& parentIfaceId, const std::string& ifaceId) {
Ed Tanous0f74e642018-11-12 15:17:05 -08002079 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002080 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2081 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002082
Ed Tanousbf648f72021-06-03 15:00:14 -07002083 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002084 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002085 return;
2086 }
2087
Ed Tanousbf648f72021-06-03 15:00:14 -07002088 // Get single eth interface data, and call the below callback
2089 // for JSON preparation
2090 getEthernetIfaceData(
2091 ifaceId,
2092 [asyncResp, parentIfaceId, ifaceId](
2093 const bool& success,
2094 const EthernetInterfaceData& ethData,
2095 const boost::container::flat_set<IPv4AddressData>&,
2096 const boost::container::flat_set<IPv6AddressData>&) {
2097 if (success && ethData.vlan_id.size() != 0)
2098 {
2099 parseInterfaceData(asyncResp->res.jsonValue,
2100 parentIfaceId, ifaceId, ethData);
2101 }
2102 else
2103 {
2104 // ... otherwise return error
2105 // TODO(Pawel)consider distinguish between non
2106 // existing object, and other errors
2107 messages::resourceNotFound(asyncResp->res,
2108 "VLAN Network Interface",
2109 ifaceId);
2110 }
2111 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002112 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002113
Ed Tanousbf648f72021-06-03 15:00:14 -07002114 BMCWEB_ROUTE(
2115 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002116 // This privilege is incorrect, it should be ConfigureManager
2117 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002118 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002119 .methods(boost::beast::http::verb::patch)(
2120 [](const crow::Request& req,
2121 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2122 const std::string& parentIfaceId, const std::string& ifaceId) {
2123 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002124 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002125 messages::resourceNotFound(
2126 asyncResp->res, "VLAN Network Interface", ifaceId);
2127 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002128 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002129
Ed Tanousbf648f72021-06-03 15:00:14 -07002130 bool vlanEnable = false;
2131 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002132
Ed Tanousbf648f72021-06-03 15:00:14 -07002133 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2134 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002135 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002136 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002137 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002138
2139 // Get single eth interface data, and call the below callback
2140 // for JSON preparation
2141 getEthernetIfaceData(
2142 ifaceId,
2143 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2144 const bool& success,
2145 const EthernetInterfaceData& ethData,
2146 const boost::container::flat_set<IPv4AddressData>&,
2147 const boost::container::flat_set<IPv6AddressData>&) {
2148 if (success && !ethData.vlan_id.empty())
2149 {
2150 auto callback =
2151 [asyncResp](
2152 const boost::system::error_code ec) {
2153 if (ec)
2154 {
2155 messages::internalError(asyncResp->res);
2156 }
2157 };
2158
2159 if (vlanEnable == true)
2160 {
2161 crow::connections::systemBus->async_method_call(
2162 std::move(callback),
2163 "xyz.openbmc_project.Network",
2164 "/xyz/openbmc_project/network/" + ifaceId,
2165 "org.freedesktop.DBus.Properties", "Set",
2166 "xyz.openbmc_project.Network.VLAN", "Id",
2167 std::variant<uint32_t>(vlanId));
2168 }
2169 else
2170 {
2171 BMCWEB_LOG_DEBUG
2172 << "vlanEnable is false. Deleting the "
2173 "vlan interface";
2174 crow::connections::systemBus->async_method_call(
2175 std::move(callback),
2176 "xyz.openbmc_project.Network",
2177 std::string(
2178 "/xyz/openbmc_project/network/") +
2179 ifaceId,
2180 "xyz.openbmc_project.Object.Delete",
2181 "Delete");
2182 }
2183 }
2184 else
2185 {
2186 // TODO(Pawel)consider distinguish between non
2187 // existing object, and other errors
2188 messages::resourceNotFound(asyncResp->res,
2189 "VLAN Network Interface",
2190 ifaceId);
2191 return;
2192 }
2193 });
2194 });
2195
2196 BMCWEB_ROUTE(
2197 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002198 // This privilege is incorrect, it should be ConfigureManager
2199 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002200 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002201 .methods(boost::beast::http::verb::delete_)(
2202 [](const crow::Request& /* req */,
2203 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2204 const std::string& parentIfaceId, const std::string& ifaceId) {
2205 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002206 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002207 messages::resourceNotFound(
2208 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002209 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002210 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002211
2212 // Get single eth interface data, and call the below callback
2213 // for JSON preparation
2214 getEthernetIfaceData(
2215 ifaceId,
2216 [asyncResp, parentIfaceId, ifaceId](
2217 const bool& success,
2218 const EthernetInterfaceData& ethData,
2219 const boost::container::flat_set<IPv4AddressData>&,
2220 const boost::container::flat_set<IPv6AddressData>&) {
2221 if (success && !ethData.vlan_id.empty())
2222 {
2223 auto callback =
2224 [asyncResp](
2225 const boost::system::error_code ec) {
2226 if (ec)
2227 {
2228 messages::internalError(asyncResp->res);
2229 }
2230 };
2231 crow::connections::systemBus->async_method_call(
2232 std::move(callback),
2233 "xyz.openbmc_project.Network",
2234 std::string("/xyz/openbmc_project/network/") +
2235 ifaceId,
2236 "xyz.openbmc_project.Object.Delete", "Delete");
2237 }
2238 else
2239 {
2240 // ... otherwise return error
2241 // TODO(Pawel)consider distinguish between non
2242 // existing object, and other errors
2243 messages::resourceNotFound(asyncResp->res,
2244 "VLAN Network Interface",
2245 ifaceId);
2246 }
2247 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002248 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002249
Ed Tanousbf648f72021-06-03 15:00:14 -07002250 BMCWEB_ROUTE(app,
2251 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002252
2253 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002254 .methods(
2255 boost::beast::http::verb::
2256 get)([](const crow::Request& /* req */,
2257 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2258 const std::string& rootInterfaceName) {
2259 // Get eth interface list, and call the below callback for JSON
2260 // preparation
2261 getEthernetIfaceList([asyncResp, rootInterfaceName](
2262 const bool& success,
2263 const boost::container::flat_set<
2264 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002265 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002266 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002267 messages::internalError(asyncResp->res);
2268 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002269 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002270
Ed Tanous81ce6092020-12-17 16:54:55 +00002271 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002272 {
2273 messages::resourceNotFound(asyncResp->res,
2274 "VLanNetworkInterfaceCollection",
2275 rootInterfaceName);
2276 return;
2277 }
2278
Ed Tanous0f74e642018-11-12 15:17:05 -08002279 asyncResp->res.jsonValue["@odata.type"] =
2280 "#VLanNetworkInterfaceCollection."
2281 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002282 asyncResp->res.jsonValue["Name"] =
2283 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002284
Ed Tanous2c70f802020-09-28 14:29:23 -07002285 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002286
Ed Tanous81ce6092020-12-17 16:54:55 +00002287 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002288 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002289 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002290 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002291 std::string path =
2292 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2293 path += rootInterfaceName;
2294 path += "/VLANs/";
2295 path += ifaceItem;
2296 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002297 }
2298 }
2299
Jason M. Billsf12894f2018-10-09 12:45:45 -07002300 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002301 ifaceArray.size();
2302 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002303 asyncResp->res.jsonValue["@odata.id"] =
2304 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2305 rootInterfaceName + "/VLANs";
2306 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002307 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002308
Ed Tanousbf648f72021-06-03 15:00:14 -07002309 BMCWEB_ROUTE(app,
2310 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002311 // This privilege is wrong, it should be ConfigureManager
2312 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002313 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002314 .methods(boost::beast::http::verb::post)(
2315 [](const crow::Request& req,
2316 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2317 const std::string& rootInterfaceName) {
2318 bool vlanEnable = false;
2319 uint32_t vlanId = 0;
2320 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2321 "VLANEnable", vlanEnable))
2322 {
2323 return;
2324 }
2325 // Need both vlanId and vlanEnable to service this request
2326 if (!vlanId)
2327 {
2328 messages::propertyMissing(asyncResp->res, "VLANId");
2329 }
2330 if (!vlanEnable)
2331 {
2332 messages::propertyMissing(asyncResp->res, "VLANEnable");
2333 }
2334 if (static_cast<bool>(vlanId) ^ vlanEnable)
2335 {
2336 return;
2337 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002338
Ed Tanousbf648f72021-06-03 15:00:14 -07002339 auto callback =
2340 [asyncResp](const boost::system::error_code ec) {
2341 if (ec)
2342 {
2343 // TODO(ed) make more consistent error messages
2344 // based on phosphor-network responses
2345 messages::internalError(asyncResp->res);
2346 return;
2347 }
2348 messages::created(asyncResp->res);
2349 };
2350 crow::connections::systemBus->async_method_call(
2351 std::move(callback), "xyz.openbmc_project.Network",
2352 "/xyz/openbmc_project/network",
2353 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2354 rootInterfaceName, vlanId);
2355 });
2356}
2357
Ed Tanous1abe55e2018-09-05 08:30:59 -07002358} // namespace redfish