blob: f7fbb9575b3993ac2439e5351c0ef31a7049ec17 [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 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600497 else if (property.first == "Type" ||
498 property.first == "Gateway")
499 {
500 // Type & Gateway is not used
501 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500502 else
503 {
504 BMCWEB_LOG_ERROR
505 << "Got extra property: " << property.first
506 << " on the " << objpath.first.str << " object";
507 }
508 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500509 }
510 }
511 }
512 }
513}
514
Ed Tanous4a0cb852018-10-15 07:55:04 -0700515// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700516inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000517 extractIPData(const std::string& ethifaceId,
518 const GetManagedObjects& dbusData,
519 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700520{
521 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000522 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700523
524 // Since there might be several IPv4 configurations aligned with
525 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000526 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 {
528 // Check if proper pattern for object path appears
529 if (boost::starts_with(objpath.first.str, ipv4PathStart))
530 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500531 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700532 {
533 if (interface.first == "xyz.openbmc_project.Network.IP")
534 {
535 // Instance IPv4AddressData structure, and set as
536 // appropriate
537 std::pair<
538 boost::container::flat_set<IPv4AddressData>::iterator,
539 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000540 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700541 IPv4AddressData& ipv4Address = *it.first;
542 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700543 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500544 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700545 {
546 if (property.first == "Address")
547 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500548 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800549 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700550 if (address != nullptr)
551 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700552 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700553 }
554 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700555 else if (property.first == "Origin")
556 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500557 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800558 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700559 if (origin != nullptr)
560 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700561 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700562 translateAddressOriginDbusToRedfish(*origin,
563 true);
564 }
565 }
566 else if (property.first == "PrefixLength")
567 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500568 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800569 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700570 if (mask != nullptr)
571 {
572 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700573 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700574 }
575 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600576 else if (property.first == "Type" ||
577 property.first == "Gateway")
578 {
579 // Type & Gateway is not used
580 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700581 else
582 {
583 BMCWEB_LOG_ERROR
584 << "Got extra property: " << property.first
585 << " on the " << objpath.first.str << " object";
586 }
587 }
588 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700589 ipv4Address.linktype =
590 boost::starts_with(ipv4Address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700591 ? LinkType::Local
592 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700593 }
594 }
595 }
596 }
597}
598
599/**
600 * @brief Sets given Id on the given VLAN interface through D-Bus
601 *
602 * @param[in] ifaceId Id of VLAN interface that should be modified
603 * @param[in] inputVlanId New ID of the VLAN
604 * @param[in] callback Function that will be called after the operation
605 *
606 * @return None.
607 */
608template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500609void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
610 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611{
612 crow::connections::systemBus->async_method_call(
613 callback, "xyz.openbmc_project.Network",
614 std::string("/xyz/openbmc_project/network/") + ifaceId,
615 "org.freedesktop.DBus.Properties", "Set",
616 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800617 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700618}
619
620/**
621 * @brief Helper function that verifies IP address to check if it is in
622 * proper format. If bits pointer is provided, also calculates active
623 * bit count for Subnet Mask.
624 *
625 * @param[in] ip IP that will be verified
626 * @param[out] bits Calculated mask in bits notation
627 *
628 * @return true in case of success, false otherwise
629 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500630inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
631 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632{
633 std::vector<std::string> bytesInMask;
634
635 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636
637 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700640 return false;
641 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700645 *bits = 0;
646 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500648 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700649 long previousValue = 255;
650 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500651 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700652 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 if (byte.empty())
654 {
655 return false;
656 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657
Ed Tanous4a0cb852018-10-15 07:55:04 -0700658 // Use strtol instead of stroi to avoid exceptions
659 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660
Ed Tanous4a0cb852018-10-15 07:55:04 -0700661 // endPtr should point to the end of the string, otherwise given string
662 // is not 100% number
663 if (*endPtr != '\0')
664 {
665 return false;
666 }
667
668 // Value should be contained in byte
669 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 {
671 return false;
672 }
673
674 if (bits != nullptr)
675 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700676 // Mask has to be continuous between bytes
677 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678 {
679 return false;
680 }
681
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 // Mask has to be continuous inside bytes
683 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000686 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000688 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700691 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700692 // Continuity not preserved
693 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700695 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 else
698 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700699 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700700 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 }
702 }
703
704 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705 }
706
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 return true;
708}
709
710/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700711 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700712 *
713 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700714 * @param[in] ipHash DBus Hash id of IP that should be deleted
715 * @param[io] asyncResp Response object that will be returned to client
716 *
717 * @return None
718 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500719inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800720 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700721{
722 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700723 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700724 if (ec)
725 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800726 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100727 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700728 },
729 "xyz.openbmc_project.Network",
730 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
731 "xyz.openbmc_project.Object.Delete", "Delete");
732}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733
Gunnar Mills244b6d52021-04-12 15:44:23 -0500734inline void updateIPv4DefaultGateway(
735 const std::string& ifaceId, const std::string& gateway,
736 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500737{
738 crow::connections::systemBus->async_method_call(
739 [asyncResp](const boost::system::error_code ec) {
740 if (ec)
741 {
742 messages::internalError(asyncResp->res);
743 return;
744 }
745 asyncResp->res.result(boost::beast::http::status::no_content);
746 },
747 "xyz.openbmc_project.Network",
748 "/xyz/openbmc_project/network/" + ifaceId,
749 "org.freedesktop.DBus.Properties", "Set",
750 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
751 std::variant<std::string>(gateway));
752}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700753/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700754 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700755 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700756 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
757 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
758 * @param[in] gateway IPv4 address of this interfaces gateway
759 * @param[in] address IPv4 address to assign to this interface
760 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700761 *
762 * @return None
763 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000764inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
765 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800766 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700767{
Ravi Teja9010ec22019-08-01 23:30:25 -0500768 auto createIpHandler = [asyncResp, ifaceId,
769 gateway](const boost::system::error_code ec) {
770 if (ec)
771 {
772 messages::internalError(asyncResp->res);
773 return;
774 }
775 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
776 };
777
Ed Tanous4a0cb852018-10-15 07:55:04 -0700778 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500779 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700780 "/xyz/openbmc_project/network/" + ifaceId,
781 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700782 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700783 gateway);
784}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500785
786/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700787 * @brief Deletes the IPv4 entry for this interface and creates a replacement
788 * static IPv4 entry
789 *
790 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
791 * @param[in] id The unique hash entry identifying the DBus entry
792 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
793 * @param[in] gateway IPv4 address of this interfaces gateway
794 * @param[in] address IPv4 address to assign to this interface
795 * @param[io] asyncResp Response object that will be returned to client
796 *
797 * @return None
798 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800799inline void
800 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
801 uint8_t prefixLength, const std::string& gateway,
802 const std::string& address,
803 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700804{
805 crow::connections::systemBus->async_method_call(
806 [asyncResp, ifaceId, address, prefixLength,
807 gateway](const boost::system::error_code ec) {
808 if (ec)
809 {
810 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500811 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700812 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500813
Johnathan Mantey01784822019-06-18 12:44:21 -0700814 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500815 [asyncResp, ifaceId,
816 gateway](const boost::system::error_code ec2) {
Ed Tanous23a21a12020-07-25 04:45:05 +0000817 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700818 {
819 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500820 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700821 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500822 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -0700823 },
824 "xyz.openbmc_project.Network",
825 "/xyz/openbmc_project/network/" + ifaceId,
826 "xyz.openbmc_project.Network.IP.Create", "IP",
827 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
828 prefixLength, gateway);
829 },
830 "xyz.openbmc_project.Network",
831 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
832 "xyz.openbmc_project.Object.Delete", "Delete");
833}
834
835/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500836 * @brief Deletes given IPv6
837 *
838 * @param[in] ifaceId Id of interface whose IP should be deleted
839 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500840 * @param[io] asyncResp Response object that will be returned to client
841 *
842 * @return None
843 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500844inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800845 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500846{
847 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700848 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500849 if (ec)
850 {
851 messages::internalError(asyncResp->res);
852 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500853 },
854 "xyz.openbmc_project.Network",
855 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
856 "xyz.openbmc_project.Object.Delete", "Delete");
857}
858
859/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700860 * @brief Deletes the IPv6 entry for this interface and creates a replacement
861 * static IPv6 entry
862 *
863 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
864 * @param[in] id The unique hash entry identifying the DBus entry
865 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
866 * @param[in] address IPv6 address to assign to this interface
867 * @param[io] asyncResp Response object that will be returned to client
868 *
869 * @return None
870 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800871inline void
872 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
873 uint8_t prefixLength, const std::string& address,
874 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700875{
876 crow::connections::systemBus->async_method_call(
877 [asyncResp, ifaceId, address,
878 prefixLength](const boost::system::error_code ec) {
879 if (ec)
880 {
881 messages::internalError(asyncResp->res);
882 }
883 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000884 [asyncResp](const boost::system::error_code ec2) {
885 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700886 {
887 messages::internalError(asyncResp->res);
888 }
889 },
890 "xyz.openbmc_project.Network",
891 "/xyz/openbmc_project/network/" + ifaceId,
892 "xyz.openbmc_project.Network.IP.Create", "IP",
893 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
894 prefixLength, "");
895 },
896 "xyz.openbmc_project.Network",
897 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
898 "xyz.openbmc_project.Object.Delete", "Delete");
899}
900
901/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500902 * @brief Creates IPv6 with given data
903 *
904 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500905 * @param[in] prefixLength Prefix length that needs to be added
906 * @param[in] address IP address that needs to be added
907 * @param[io] asyncResp Response object that will be returned to client
908 *
909 * @return None
910 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500911inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
912 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800913 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500914{
915 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
916 if (ec)
917 {
918 messages::internalError(asyncResp->res);
919 }
920 };
921 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500922 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500923 crow::connections::systemBus->async_method_call(
924 std::move(createIpHandler), "xyz.openbmc_project.Network",
925 "/xyz/openbmc_project/network/" + ifaceId,
926 "xyz.openbmc_project.Network.IP.Create", "IP",
927 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
928 "");
929}
930
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931/**
932 * Function that retrieves all properties for given Ethernet Interface
933 * Object
934 * from EntityManager Network Manager
935 * @param ethiface_id a eth interface id to query on DBus
936 * @param callback a function that shall be called to convert Dbus output
937 * into JSON
938 */
939template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000940void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500941 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942{
943 crow::connections::systemBus->async_method_call(
Ed Tanous81ce6092020-12-17 16:54:55 +0000944 [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
945 const boost::system::error_code errorCode,
Ed Tanousf23b7292020-10-15 09:41:17 -0700946 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700947 EthernetInterfaceData ethData{};
948 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500949 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700950
Ed Tanous81ce6092020-12-17 16:54:55 +0000951 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700952 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700953 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954 return;
955 }
956
Ed Tanous4c9afe42019-05-03 16:59:57 -0700957 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700958 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700959 if (!found)
960 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700961 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700962 return;
963 }
964
Ed Tanous2c70f802020-09-28 14:29:23 -0700965 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700966 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500967 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700968 {
Ravi Tejac6191412019-07-30 00:53:50 -0500969 if (((ipv4.linktype == LinkType::Global) &&
970 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500971 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973 ipv4.gateway = ethData.default_gateway;
974 }
975 }
976
Ed Tanous2c70f802020-09-28 14:29:23 -0700977 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500978 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700979 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700980 },
981 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
982 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700983}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700984
985/**
986 * Function that retrieves all Ethernet Interfaces available through Network
987 * Manager
988 * @param callback a function that shall be called to convert Dbus output
989 * into JSON.
990 */
991template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500992void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700993{
994 crow::connections::systemBus->async_method_call(
995 [callback{std::move(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000996 const boost::system::error_code errorCode,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500997 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700998 // Callback requires vector<string> to retrieve all available
999 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -07001000 boost::container::flat_set<std::string> ifaceList;
1001 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +00001002 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001003 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001004 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001005 return;
1006 }
1007
1008 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001009 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001010 {
1011 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001012 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001013 {
1014 // If interface is
1015 // xyz.openbmc_project.Network.EthernetInterface, this is
1016 // what we're looking for.
1017 if (interface.first ==
1018 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001019 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001020 std::string ifaceId = objpath.first.filename();
1021 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001022 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001023 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001024 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001025 // and put it into output vector.
1026 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 }
1028 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001029 }
1030 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001031 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001032 },
1033 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1034 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001035}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001036
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001037inline void
1038 handleHostnamePatch(const std::string& hostname,
1039 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040{
Ed Tanousbf648f72021-06-03 15:00:14 -07001041 // SHOULD handle host names of up to 255 characters(RFC 1123)
1042 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001043 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001044 messages::propertyValueFormatError(asyncResp->res, hostname,
1045 "HostName");
1046 return;
1047 }
1048 crow::connections::systemBus->async_method_call(
1049 [asyncResp](const boost::system::error_code ec) {
1050 if (ec)
1051 {
1052 messages::internalError(asyncResp->res);
1053 }
1054 },
1055 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1056 "org.freedesktop.DBus.Properties", "Set",
1057 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1058 std::variant<std::string>(hostname));
1059}
1060
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001061inline void
1062 handleDomainnamePatch(const std::string& ifaceId,
1063 const std::string& domainname,
1064 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001065{
1066 std::vector<std::string> vectorDomainname = {domainname};
1067 crow::connections::systemBus->async_method_call(
1068 [asyncResp](const boost::system::error_code ec) {
1069 if (ec)
1070 {
1071 messages::internalError(asyncResp->res);
1072 }
1073 },
1074 "xyz.openbmc_project.Network",
1075 "/xyz/openbmc_project/network/" + ifaceId,
1076 "org.freedesktop.DBus.Properties", "Set",
1077 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1078 std::variant<std::vector<std::string>>(vectorDomainname));
1079}
1080
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001081inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001082{
1083 // A valid host name can never have the dotted-decimal form (RFC 1123)
1084 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1085 {
1086 return false;
1087 }
1088 // Each label(hostname/subdomains) within a valid FQDN
1089 // MUST handle host names of up to 63 characters (RFC 1123)
1090 // labels cannot start or end with hyphens (RFC 952)
1091 // labels can start with numbers (RFC 1123)
1092 const std::regex pattern(
1093 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1094
1095 return std::regex_match(hostname, pattern);
1096}
1097
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001098inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001099{
1100 // Can have multiple subdomains
1101 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001102 const std::regex pattern(
1103 "^([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 -07001104
1105 return std::regex_match(domainname, pattern);
1106}
1107
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001108inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001110{
1111 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1112 if (fqdn.length() > 255)
1113 {
1114 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1115 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001116 }
1117
Ed Tanousbf648f72021-06-03 15:00:14 -07001118 size_t pos = fqdn.find('.');
1119 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001120 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001121 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1122 return;
1123 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001124
Ed Tanousbf648f72021-06-03 15:00:14 -07001125 std::string hostname;
1126 std::string domainname;
1127 domainname = (fqdn).substr(pos + 1);
1128 hostname = (fqdn).substr(0, pos);
1129
1130 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1131 {
1132 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1133 return;
1134 }
1135
1136 handleHostnamePatch(hostname, asyncResp);
1137 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1138}
1139
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001140inline void
1141 handleMACAddressPatch(const std::string& ifaceId,
1142 const std::string& macAddress,
1143 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001144{
1145 crow::connections::systemBus->async_method_call(
1146 [asyncResp, macAddress](const boost::system::error_code ec) {
1147 if (ec)
1148 {
1149 messages::internalError(asyncResp->res);
1150 return;
1151 }
1152 },
1153 "xyz.openbmc_project.Network",
1154 "/xyz/openbmc_project/network/" + ifaceId,
1155 "org.freedesktop.DBus.Properties", "Set",
1156 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1157 std::variant<std::string>(macAddress));
1158}
1159
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001160inline void setDHCPEnabled(const std::string& ifaceId,
1161 const std::string& propertyName, const bool v4Value,
1162 const bool v6Value,
1163 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001164{
1165 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1166 crow::connections::systemBus->async_method_call(
1167 [asyncResp](const boost::system::error_code ec) {
1168 if (ec)
1169 {
1170 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1171 messages::internalError(asyncResp->res);
1172 return;
1173 }
1174 messages::success(asyncResp->res);
1175 },
1176 "xyz.openbmc_project.Network",
1177 "/xyz/openbmc_project/network/" + ifaceId,
1178 "org.freedesktop.DBus.Properties", "Set",
1179 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1180 std::variant<std::string>{dhcp});
1181}
1182
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001183inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001184 const std::string& ifaceId, const std::string& propertyName,
1185 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1186{
1187 crow::connections::systemBus->async_method_call(
1188 [asyncResp](const boost::system::error_code ec) {
1189 if (ec)
1190 {
1191 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1192 messages::internalError(asyncResp->res);
1193 return;
1194 }
1195 },
1196 "xyz.openbmc_project.Network",
1197 "/xyz/openbmc_project/network/" + ifaceId,
1198 "org.freedesktop.DBus.Properties", "Set",
1199 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1200 std::variant<bool>{value});
1201}
1202
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001203inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1204 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001205{
1206 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1207 crow::connections::systemBus->async_method_call(
1208 [asyncResp](const boost::system::error_code ec) {
1209 if (ec)
1210 {
1211 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1212 messages::internalError(asyncResp->res);
1213 return;
1214 }
1215 },
1216 "xyz.openbmc_project.Network",
1217 "/xyz/openbmc_project/network/config/dhcp",
1218 "org.freedesktop.DBus.Properties", "Set",
1219 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1220 std::variant<bool>{value});
1221}
1222
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001223inline void handleDHCPPatch(const std::string& ifaceId,
1224 const EthernetInterfaceData& ethData,
1225 const DHCPParameters& v4dhcpParms,
1226 const DHCPParameters& v6dhcpParms,
1227 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001228{
1229 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1230 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1231
1232 bool nextv4DHCPState =
1233 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1234
1235 bool nextv6DHCPState{};
1236 if (v6dhcpParms.dhcpv6OperatingMode)
1237 {
1238 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1239 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1240 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1241 {
1242 messages::propertyValueFormatError(asyncResp->res,
1243 *v6dhcpParms.dhcpv6OperatingMode,
1244 "OperatingMode");
1245 return;
1246 }
1247 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1248 }
1249 else
1250 {
1251 nextv6DHCPState = ipv6Active;
1252 }
1253
1254 bool nextDNS{};
1255 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1256 {
1257 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1258 {
1259 messages::generalError(asyncResp->res);
1260 return;
1261 }
1262 nextDNS = *v4dhcpParms.useDNSServers;
1263 }
1264 else if (v4dhcpParms.useDNSServers)
1265 {
1266 nextDNS = *v4dhcpParms.useDNSServers;
1267 }
1268 else if (v6dhcpParms.useDNSServers)
1269 {
1270 nextDNS = *v6dhcpParms.useDNSServers;
1271 }
1272 else
1273 {
1274 nextDNS = ethData.DNSEnabled;
1275 }
1276
1277 bool nextNTP{};
1278 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1279 {
1280 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1281 {
1282 messages::generalError(asyncResp->res);
1283 return;
1284 }
1285 nextNTP = *v4dhcpParms.useNTPServers;
1286 }
1287 else if (v4dhcpParms.useNTPServers)
1288 {
1289 nextNTP = *v4dhcpParms.useNTPServers;
1290 }
1291 else if (v6dhcpParms.useNTPServers)
1292 {
1293 nextNTP = *v6dhcpParms.useNTPServers;
1294 }
1295 else
1296 {
1297 nextNTP = ethData.NTPEnabled;
1298 }
1299
1300 bool nextUseDomain{};
1301 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1302 {
1303 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1304 {
1305 messages::generalError(asyncResp->res);
1306 return;
1307 }
1308 nextUseDomain = *v4dhcpParms.useUseDomainName;
1309 }
1310 else if (v4dhcpParms.useUseDomainName)
1311 {
1312 nextUseDomain = *v4dhcpParms.useUseDomainName;
1313 }
1314 else if (v6dhcpParms.useUseDomainName)
1315 {
1316 nextUseDomain = *v6dhcpParms.useUseDomainName;
1317 }
1318 else
1319 {
1320 nextUseDomain = ethData.HostNameEnabled;
1321 }
1322
1323 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1324 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1325 asyncResp);
1326 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1327 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1328 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1329 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1330 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1331 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1332}
1333
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001334inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001335 getNextStaticIpEntry(
1336 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1337 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1338{
1339 return std::find_if(head, end, [](const IPv4AddressData& value) {
1340 return value.origin == "Static";
1341 });
1342}
1343
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001344inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001345 getNextStaticIpEntry(
1346 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1347 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1348{
1349 return std::find_if(head, end, [](const IPv6AddressData& value) {
1350 return value.origin == "Static";
1351 });
1352}
1353
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001354inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001355 const std::string& ifaceId, nlohmann::json& input,
1356 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1357 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1358{
1359 if ((!input.is_array()) || input.empty())
1360 {
1361 messages::propertyValueTypeError(
1362 asyncResp->res,
1363 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1364 "IPv4StaticAddresses");
1365 return;
1366 }
1367
1368 unsigned entryIdx = 1;
1369 // Find the first static IP address currently active on the NIC and
1370 // match it to the first JSON element in the IPv4StaticAddresses array.
1371 // Match each subsequent JSON element to the next static IP programmed
1372 // into the NIC.
1373 boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1374 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1375
1376 for (nlohmann::json& thisJson : input)
1377 {
1378 std::string pathString =
1379 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1380
1381 if (!thisJson.is_null() && !thisJson.empty())
1382 {
1383 std::optional<std::string> address;
1384 std::optional<std::string> subnetMask;
1385 std::optional<std::string> gateway;
1386
1387 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1388 address, "SubnetMask", subnetMask,
1389 "Gateway", gateway))
1390 {
1391 messages::propertyValueFormatError(
1392 asyncResp->res,
1393 thisJson.dump(2, ' ', true,
1394 nlohmann::json::error_handler_t::replace),
1395 pathString);
1396 return;
1397 }
1398
1399 // Find the address/subnet/gateway values. Any values that are
1400 // not explicitly provided are assumed to be unmodified from the
1401 // current state of the interface. Merge existing state into the
1402 // current request.
1403 const std::string* addr = nullptr;
1404 const std::string* gw = nullptr;
1405 uint8_t prefixLength = 0;
1406 bool errorInEntry = false;
1407 if (address)
1408 {
1409 if (ipv4VerifyIpAndGetBitcount(*address))
1410 {
1411 addr = &(*address);
1412 }
1413 else
1414 {
1415 messages::propertyValueFormatError(asyncResp->res, *address,
1416 pathString + "/Address");
1417 errorInEntry = true;
1418 }
1419 }
1420 else if (niciPentry != ipv4Data.cend())
1421 {
1422 addr = &(niciPentry->address);
1423 }
1424 else
1425 {
1426 messages::propertyMissing(asyncResp->res,
1427 pathString + "/Address");
1428 errorInEntry = true;
1429 }
1430
1431 if (subnetMask)
1432 {
1433 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1434 {
1435 messages::propertyValueFormatError(
1436 asyncResp->res, *subnetMask,
1437 pathString + "/SubnetMask");
1438 errorInEntry = true;
1439 }
1440 }
1441 else if (niciPentry != ipv4Data.cend())
1442 {
1443 if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
1444 &prefixLength))
1445 {
1446 messages::propertyValueFormatError(
1447 asyncResp->res, niciPentry->netmask,
1448 pathString + "/SubnetMask");
1449 errorInEntry = true;
1450 }
1451 }
1452 else
1453 {
1454 messages::propertyMissing(asyncResp->res,
1455 pathString + "/SubnetMask");
1456 errorInEntry = true;
1457 }
1458
1459 if (gateway)
1460 {
1461 if (ipv4VerifyIpAndGetBitcount(*gateway))
1462 {
1463 gw = &(*gateway);
1464 }
1465 else
1466 {
1467 messages::propertyValueFormatError(asyncResp->res, *gateway,
1468 pathString + "/Gateway");
1469 errorInEntry = true;
1470 }
1471 }
1472 else if (niciPentry != ipv4Data.cend())
1473 {
1474 gw = &niciPentry->gateway;
1475 }
1476 else
1477 {
1478 messages::propertyMissing(asyncResp->res,
1479 pathString + "/Gateway");
1480 errorInEntry = true;
1481 }
1482
1483 if (errorInEntry)
1484 {
1485 return;
1486 }
1487
1488 if (niciPentry != ipv4Data.cend())
1489 {
1490 deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength, *gw,
1491 *addr, asyncResp);
1492 niciPentry =
1493 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1494 }
1495 else
1496 {
1497 createIPv4(ifaceId, prefixLength, *gateway, *address,
1498 asyncResp);
1499 }
1500 entryIdx++;
1501 }
1502 else
1503 {
1504 if (niciPentry == ipv4Data.cend())
1505 {
1506 // Requesting a DELETE/DO NOT MODIFY action for an item
1507 // that isn't present on the eth(n) interface. Input JSON is
1508 // in error, so bail out.
1509 if (thisJson.is_null())
1510 {
1511 messages::resourceCannotBeDeleted(asyncResp->res);
1512 return;
1513 }
1514 messages::propertyValueFormatError(
1515 asyncResp->res,
1516 thisJson.dump(2, ' ', true,
1517 nlohmann::json::error_handler_t::replace),
1518 pathString);
1519 return;
1520 }
1521
1522 if (thisJson.is_null())
1523 {
1524 deleteIPv4(ifaceId, niciPentry->id, asyncResp);
1525 }
1526 if (niciPentry != ipv4Data.cend())
1527 {
1528 niciPentry =
1529 getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1530 }
1531 entryIdx++;
1532 }
1533 }
1534}
1535
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001536inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001537 const std::string& ifaceId,
1538 const std::vector<std::string>& updatedStaticNameServers,
1539 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1540{
1541 crow::connections::systemBus->async_method_call(
1542 [asyncResp](const boost::system::error_code ec) {
1543 if (ec)
1544 {
1545 messages::internalError(asyncResp->res);
1546 return;
1547 }
1548 },
1549 "xyz.openbmc_project.Network",
1550 "/xyz/openbmc_project/network/" + ifaceId,
1551 "org.freedesktop.DBus.Properties", "Set",
1552 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1553 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1554}
1555
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001556inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001557 const std::string& ifaceId, const nlohmann::json& input,
1558 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1559 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1560{
1561 if (!input.is_array() || input.empty())
1562 {
1563 messages::propertyValueTypeError(
1564 asyncResp->res,
1565 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1566 "IPv6StaticAddresses");
1567 return;
1568 }
1569 size_t entryIdx = 1;
1570 boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1571 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1572 for (const nlohmann::json& thisJson : input)
1573 {
1574 std::string pathString =
1575 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1576
1577 if (!thisJson.is_null() && !thisJson.empty())
1578 {
1579 std::optional<std::string> address;
1580 std::optional<uint8_t> prefixLength;
1581 nlohmann::json thisJsonCopy = thisJson;
1582 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1583 address, "PrefixLength", prefixLength))
1584 {
1585 messages::propertyValueFormatError(
1586 asyncResp->res,
1587 thisJson.dump(2, ' ', true,
1588 nlohmann::json::error_handler_t::replace),
1589 pathString);
1590 return;
1591 }
1592
1593 const std::string* addr;
1594 uint8_t prefix;
1595
1596 // Find the address and prefixLength values. Any values that are
1597 // not explicitly provided are assumed to be unmodified from the
1598 // current state of the interface. Merge existing state into the
1599 // current request.
1600 if (address)
1601 {
1602 addr = &(*address);
1603 }
1604 else if (niciPentry != ipv6Data.end())
1605 {
1606 addr = &(niciPentry->address);
1607 }
1608 else
1609 {
1610 messages::propertyMissing(asyncResp->res,
1611 pathString + "/Address");
1612 return;
1613 }
1614
1615 if (prefixLength)
1616 {
1617 prefix = *prefixLength;
1618 }
1619 else if (niciPentry != ipv6Data.end())
1620 {
1621 prefix = niciPentry->prefixLength;
1622 }
1623 else
1624 {
1625 messages::propertyMissing(asyncResp->res,
1626 pathString + "/PrefixLength");
1627 return;
1628 }
1629
1630 if (niciPentry != ipv6Data.end())
1631 {
1632 deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1633 asyncResp);
1634 niciPentry =
1635 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1636 }
1637 else
1638 {
1639 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1640 }
1641 entryIdx++;
1642 }
1643 else
1644 {
1645 if (niciPentry == ipv6Data.end())
1646 {
1647 // Requesting a DELETE/DO NOT MODIFY action for an item
1648 // that isn't present on the eth(n) interface. Input JSON is
1649 // in error, so bail out.
1650 if (thisJson.is_null())
1651 {
1652 messages::resourceCannotBeDeleted(asyncResp->res);
1653 return;
1654 }
1655 messages::propertyValueFormatError(
1656 asyncResp->res,
1657 thisJson.dump(2, ' ', true,
1658 nlohmann::json::error_handler_t::replace),
1659 pathString);
1660 return;
1661 }
1662
1663 if (thisJson.is_null())
1664 {
1665 deleteIPv6(ifaceId, niciPentry->id, asyncResp);
1666 }
1667 if (niciPentry != ipv6Data.cend())
1668 {
1669 niciPentry =
1670 getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1671 }
1672 entryIdx++;
1673 }
1674 }
1675}
1676
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001677inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001678 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1679 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1680 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1681 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1682{
1683 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1684 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1685
1686 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1687 jsonResponse["Id"] = ifaceId;
1688 jsonResponse["@odata.id"] =
1689 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1690 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1691
1692 auto health = std::make_shared<HealthPopulate>(asyncResp);
1693
1694 crow::connections::systemBus->async_method_call(
1695 [health](const boost::system::error_code ec,
1696 std::vector<std::string>& resp) {
1697 if (ec)
1698 {
1699 return;
1700 }
1701
1702 health->inventory = std::move(resp);
1703 },
1704 "xyz.openbmc_project.ObjectMapper",
1705 "/xyz/openbmc_project/object_mapper",
1706 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1707 inventoryForEthernet);
1708
1709 health->populate();
1710
1711 if (ethData.nicEnabled)
1712 {
1713 jsonResponse["LinkStatus"] = "LinkUp";
1714 jsonResponse["Status"]["State"] = "Enabled";
1715 }
1716 else
1717 {
1718 jsonResponse["LinkStatus"] = "NoLink";
1719 jsonResponse["Status"]["State"] = "Disabled";
1720 }
1721
1722 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1723 jsonResponse["SpeedMbps"] = ethData.speed;
1724 jsonResponse["MACAddress"] = ethData.mac_address;
1725 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1726 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1727 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1728 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1729 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1730
1731 jsonResponse["DHCPv6"]["OperatingMode"] =
1732 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1733 : "Disabled";
1734 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1735 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1736 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1737
1738 if (!ethData.hostname.empty())
1739 {
1740 jsonResponse["HostName"] = ethData.hostname;
1741
1742 // When domain name is empty then it means, that it is a network
1743 // without domain names, and the host name itself must be treated as
1744 // FQDN
1745 std::string fqdn = ethData.hostname;
1746 if (!ethData.domainnames.empty())
1747 {
1748 fqdn += "." + ethData.domainnames[0];
1749 }
1750 jsonResponse["FQDN"] = fqdn;
1751 }
1752
1753 jsonResponse["VLANs"] = {
1754 {"@odata.id",
1755 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1756
1757 jsonResponse["NameServers"] = ethData.nameServers;
1758 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1759
1760 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1761 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1762 ipv4Array = nlohmann::json::array();
1763 ipv4StaticArray = nlohmann::json::array();
1764 for (auto& ipv4Config : ipv4Data)
1765 {
1766
1767 std::string gatewayStr = ipv4Config.gateway;
1768 if (gatewayStr.empty())
1769 {
1770 gatewayStr = "0.0.0.0";
1771 }
1772
1773 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1774 {"SubnetMask", ipv4Config.netmask},
1775 {"Address", ipv4Config.address},
1776 {"Gateway", gatewayStr}});
1777 if (ipv4Config.origin == "Static")
1778 {
1779 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1780 {"SubnetMask", ipv4Config.netmask},
1781 {"Address", ipv4Config.address},
1782 {"Gateway", gatewayStr}});
1783 }
1784 }
1785
1786 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1787 if (ipv6GatewayStr.empty())
1788 {
1789 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1790 }
1791
1792 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1793
1794 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1795 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1796 ipv6Array = nlohmann::json::array();
1797 ipv6StaticArray = nlohmann::json::array();
1798 nlohmann::json& ipv6AddrPolicyTable =
1799 jsonResponse["IPv6AddressPolicyTable"];
1800 ipv6AddrPolicyTable = nlohmann::json::array();
1801 for (auto& ipv6Config : ipv6Data)
1802 {
1803 ipv6Array.push_back({{"Address", ipv6Config.address},
1804 {"PrefixLength", ipv6Config.prefixLength},
1805 {"AddressOrigin", ipv6Config.origin},
1806 {"AddressState", nullptr}});
1807 if (ipv6Config.origin == "Static")
1808 {
1809 ipv6StaticArray.push_back(
1810 {{"Address", ipv6Config.address},
1811 {"PrefixLength", ipv6Config.prefixLength},
1812 {"AddressOrigin", ipv6Config.origin},
1813 {"AddressState", nullptr}});
1814 }
1815 }
1816}
1817
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001818inline void parseInterfaceData(nlohmann::json& jsonResponse,
1819 const std::string& parentIfaceId,
1820 const std::string& ifaceId,
1821 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001822{
1823 // Fill out obvious data...
1824 jsonResponse["Id"] = ifaceId;
1825 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1826 parentIfaceId + "/VLANs/" + ifaceId;
1827
1828 jsonResponse["VLANEnable"] = true;
1829 if (!ethData.vlan_id.empty())
1830 {
1831 jsonResponse["VLANId"] = ethData.vlan_id.back();
1832 }
1833}
1834
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001835inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001836{
1837 if (!boost::starts_with(iface, parent + "_"))
1838 {
1839 return false;
1840 }
1841 return true;
1842}
1843
1844inline void requestEthernetInterfacesRoutes(App& app)
1845{
1846 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001847 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07001848 .methods(
1849 boost::beast::http::verb::
1850 get)([](const crow::Request&,
1851 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1852 asyncResp->res.jsonValue["@odata.type"] =
1853 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1854 asyncResp->res.jsonValue["@odata.id"] =
1855 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1856 asyncResp->res.jsonValue["Name"] =
1857 "Ethernet Network Interface Collection";
1858 asyncResp->res.jsonValue["Description"] =
1859 "Collection of EthernetInterfaces for this Manager";
1860
1861 // Get eth interface list, and call the below callback for JSON
1862 // preparation
1863 getEthernetIfaceList([asyncResp](const bool& success,
1864 const boost::container::flat_set<
1865 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001866 if (!success)
1867 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001868 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001869 return;
1870 }
1871
Ed Tanous2c70f802020-09-28 14:29:23 -07001872 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001873 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001874 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001875 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001876 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001877 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001878 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001879 if (found == std::string::npos)
1880 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001881 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001882 {{"@odata.id",
1883 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001884 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001885 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001886 }
1887
Ed Tanous4c9afe42019-05-03 16:59:57 -07001888 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001889 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001890 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001891 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001892 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301893 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001894
Ed Tanousbf648f72021-06-03 15:00:14 -07001895 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001896 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001897 .methods(boost::beast::http::verb::get)(
1898 [](const crow::Request&,
1899 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1900 const std::string& ifaceId) {
1901 getEthernetIfaceData(
1902 ifaceId,
1903 [asyncResp,
1904 ifaceId](const bool& success,
1905 const EthernetInterfaceData& ethData,
1906 const boost::container::flat_set<IPv4AddressData>&
1907 ipv4Data,
1908 const boost::container::flat_set<IPv6AddressData>&
1909 ipv6Data) {
1910 if (!success)
1911 {
1912 // TODO(Pawel)consider distinguish between non
1913 // existing object, and other errors
1914 messages::resourceNotFound(
1915 asyncResp->res, "EthernetInterface", ifaceId);
1916 return;
1917 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001918
Ed Tanousbf648f72021-06-03 15:00:14 -07001919 asyncResp->res.jsonValue["@odata.type"] =
1920 "#EthernetInterface.v1_4_1.EthernetInterface";
1921 asyncResp->res.jsonValue["Name"] =
1922 "Manager Ethernet Interface";
1923 asyncResp->res.jsonValue["Description"] =
1924 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301925
Ed Tanousbf648f72021-06-03 15:00:14 -07001926 parseInterfaceData(asyncResp, ifaceId, ethData,
1927 ipv4Data, ipv6Data);
1928 });
1929 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001930
Ed Tanousbf648f72021-06-03 15:00:14 -07001931 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001932 .privileges(redfish::privileges::patchEthernetInterface)
1933
Ed Tanousbf648f72021-06-03 15:00:14 -07001934 .methods(boost::beast::http::verb::patch)(
1935 [](const crow::Request& req,
1936 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1937 const std::string& ifaceId) {
1938 std::optional<std::string> hostname;
1939 std::optional<std::string> fqdn;
1940 std::optional<std::string> macAddress;
1941 std::optional<std::string> ipv6DefaultGateway;
1942 std::optional<nlohmann::json> ipv4StaticAddresses;
1943 std::optional<nlohmann::json> ipv6StaticAddresses;
1944 std::optional<std::vector<std::string>> staticNameServers;
1945 std::optional<nlohmann::json> dhcpv4;
1946 std::optional<nlohmann::json> dhcpv6;
1947 std::optional<bool> interfaceEnabled;
1948 DHCPParameters v4dhcpParms;
1949 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001950
Ed Tanousbf648f72021-06-03 15:00:14 -07001951 if (!json_util::readJson(
1952 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1953 "IPv4StaticAddresses", ipv4StaticAddresses,
1954 "MACAddress", macAddress, "StaticNameServers",
1955 staticNameServers, "IPv6DefaultGateway",
1956 ipv6DefaultGateway, "IPv6StaticAddresses",
1957 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1958 "InterfaceEnabled", interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001959 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001960 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001961 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001962 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001963 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001964 if (!json_util::readJson(
1965 *dhcpv4, asyncResp->res, "DHCPEnabled",
1966 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1967 v4dhcpParms.useDNSServers, "UseNTPServers",
1968 v4dhcpParms.useNTPServers, "UseDomainName",
1969 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001970 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001971 return;
1972 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001973 }
1974
Ed Tanousbf648f72021-06-03 15:00:14 -07001975 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001976 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001977 if (!json_util::readJson(
1978 *dhcpv6, asyncResp->res, "OperatingMode",
1979 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1980 v6dhcpParms.useDNSServers, "UseNTPServers",
1981 v6dhcpParms.useNTPServers, "UseDomainName",
1982 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001983 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001984 return;
1985 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001986 }
1987
Ed Tanousbf648f72021-06-03 15:00:14 -07001988 // Get single eth interface data, and call the below callback
1989 // for JSON preparation
1990 getEthernetIfaceData(
1991 ifaceId,
1992 [asyncResp, ifaceId, hostname = std::move(hostname),
1993 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1994 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1995 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1996 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1997 staticNameServers = std::move(staticNameServers),
1998 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1999 v4dhcpParms = std::move(v4dhcpParms),
2000 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2001 const bool& success,
2002 const EthernetInterfaceData& ethData,
2003 const boost::container::flat_set<IPv4AddressData>&
2004 ipv4Data,
2005 const boost::container::flat_set<IPv6AddressData>&
2006 ipv6Data) {
2007 if (!success)
2008 {
2009 // ... otherwise return error
2010 // TODO(Pawel)consider distinguish between non
2011 // existing object, and other errors
2012 messages::resourceNotFound(
2013 asyncResp->res, "Ethernet Interface", ifaceId);
2014 return;
2015 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002016
Ed Tanousbf648f72021-06-03 15:00:14 -07002017 if (dhcpv4 || dhcpv6)
2018 {
2019 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2020 v6dhcpParms, asyncResp);
2021 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002022
Ed Tanousbf648f72021-06-03 15:00:14 -07002023 if (hostname)
2024 {
2025 handleHostnamePatch(*hostname, asyncResp);
2026 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002027
Ed Tanousbf648f72021-06-03 15:00:14 -07002028 if (fqdn)
2029 {
2030 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2031 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002032
Ed Tanousbf648f72021-06-03 15:00:14 -07002033 if (macAddress)
2034 {
2035 handleMACAddressPatch(ifaceId, *macAddress,
2036 asyncResp);
2037 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002038
Ed Tanousbf648f72021-06-03 15:00:14 -07002039 if (ipv4StaticAddresses)
2040 {
2041 // TODO(ed) for some reason the capture of
2042 // ipv4Addresses above is returning a const value,
2043 // not a non-const value. This doesn't really work
2044 // for us, as we need to be able to efficiently move
2045 // out the intermedia nlohmann::json objects. This
2046 // makes a copy of the structure, and operates on
2047 // that, but could be done more efficiently
2048 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2049 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2050 asyncResp);
2051 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002052
Ed Tanousbf648f72021-06-03 15:00:14 -07002053 if (staticNameServers)
2054 {
2055 handleStaticNameServersPatch(
2056 ifaceId, *staticNameServers, asyncResp);
2057 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002058
Ed Tanousbf648f72021-06-03 15:00:14 -07002059 if (ipv6DefaultGateway)
2060 {
2061 messages::propertyNotWritable(asyncResp->res,
2062 "IPv6DefaultGateway");
2063 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002064
Ed Tanousbf648f72021-06-03 15:00:14 -07002065 if (ipv6StaticAddresses)
2066 {
2067 nlohmann::json ipv6Static = *ipv6StaticAddresses;
2068 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2069 ipv6Data, asyncResp);
2070 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002071
Ed Tanousbf648f72021-06-03 15:00:14 -07002072 if (interfaceEnabled)
2073 {
2074 setEthernetInterfaceBoolProperty(
2075 ifaceId, "NICEnabled", *interfaceEnabled,
2076 asyncResp);
2077 }
2078 });
2079 });
manojkiraneda2a133282019-02-19 13:09:43 +05302080
Ed Tanousbf648f72021-06-03 15:00:14 -07002081 BMCWEB_ROUTE(
2082 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002083 .privileges(redfish::privileges::getVLanNetworkInterface)
2084
Ed Tanousbf648f72021-06-03 15:00:14 -07002085 .methods(boost::beast::http::verb::get)(
2086 [](const crow::Request& /* req */,
2087 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2088 const std::string& parentIfaceId, const std::string& ifaceId) {
Ed Tanous0f74e642018-11-12 15:17:05 -08002089 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002090 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2091 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002092
Ed Tanousbf648f72021-06-03 15:00:14 -07002093 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002094 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002095 return;
2096 }
2097
Ed Tanousbf648f72021-06-03 15:00:14 -07002098 // Get single eth interface data, and call the below callback
2099 // for JSON preparation
2100 getEthernetIfaceData(
2101 ifaceId,
2102 [asyncResp, parentIfaceId, ifaceId](
2103 const bool& success,
2104 const EthernetInterfaceData& ethData,
2105 const boost::container::flat_set<IPv4AddressData>&,
2106 const boost::container::flat_set<IPv6AddressData>&) {
2107 if (success && ethData.vlan_id.size() != 0)
2108 {
2109 parseInterfaceData(asyncResp->res.jsonValue,
2110 parentIfaceId, ifaceId, ethData);
2111 }
2112 else
2113 {
2114 // ... otherwise return error
2115 // TODO(Pawel)consider distinguish between non
2116 // existing object, and other errors
2117 messages::resourceNotFound(asyncResp->res,
2118 "VLAN Network Interface",
2119 ifaceId);
2120 }
2121 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002122 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002123
Ed Tanousbf648f72021-06-03 15:00:14 -07002124 BMCWEB_ROUTE(
2125 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002126 // This privilege is incorrect, it should be ConfigureManager
2127 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002128 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002129 .methods(boost::beast::http::verb::patch)(
2130 [](const crow::Request& req,
2131 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2132 const std::string& parentIfaceId, const std::string& ifaceId) {
2133 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002134 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002135 messages::resourceNotFound(
2136 asyncResp->res, "VLAN Network Interface", ifaceId);
2137 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002138 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002139
Ed Tanousbf648f72021-06-03 15:00:14 -07002140 bool vlanEnable = false;
2141 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002142
Ed Tanousbf648f72021-06-03 15:00:14 -07002143 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2144 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002145 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002146 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002147 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002148
2149 // Get single eth interface data, and call the below callback
2150 // for JSON preparation
2151 getEthernetIfaceData(
2152 ifaceId,
2153 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2154 const bool& success,
2155 const EthernetInterfaceData& ethData,
2156 const boost::container::flat_set<IPv4AddressData>&,
2157 const boost::container::flat_set<IPv6AddressData>&) {
2158 if (success && !ethData.vlan_id.empty())
2159 {
2160 auto callback =
2161 [asyncResp](
2162 const boost::system::error_code ec) {
2163 if (ec)
2164 {
2165 messages::internalError(asyncResp->res);
2166 }
2167 };
2168
2169 if (vlanEnable == true)
2170 {
2171 crow::connections::systemBus->async_method_call(
2172 std::move(callback),
2173 "xyz.openbmc_project.Network",
2174 "/xyz/openbmc_project/network/" + ifaceId,
2175 "org.freedesktop.DBus.Properties", "Set",
2176 "xyz.openbmc_project.Network.VLAN", "Id",
2177 std::variant<uint32_t>(vlanId));
2178 }
2179 else
2180 {
2181 BMCWEB_LOG_DEBUG
2182 << "vlanEnable is false. Deleting the "
2183 "vlan interface";
2184 crow::connections::systemBus->async_method_call(
2185 std::move(callback),
2186 "xyz.openbmc_project.Network",
2187 std::string(
2188 "/xyz/openbmc_project/network/") +
2189 ifaceId,
2190 "xyz.openbmc_project.Object.Delete",
2191 "Delete");
2192 }
2193 }
2194 else
2195 {
2196 // TODO(Pawel)consider distinguish between non
2197 // existing object, and other errors
2198 messages::resourceNotFound(asyncResp->res,
2199 "VLAN Network Interface",
2200 ifaceId);
2201 return;
2202 }
2203 });
2204 });
2205
2206 BMCWEB_ROUTE(
2207 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002208 // This privilege is incorrect, it should be ConfigureManager
2209 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002210 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002211 .methods(boost::beast::http::verb::delete_)(
2212 [](const crow::Request& /* req */,
2213 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2214 const std::string& parentIfaceId, const std::string& ifaceId) {
2215 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002216 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002217 messages::resourceNotFound(
2218 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002219 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002220 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002221
2222 // Get single eth interface data, and call the below callback
2223 // for JSON preparation
2224 getEthernetIfaceData(
2225 ifaceId,
2226 [asyncResp, parentIfaceId, ifaceId](
2227 const bool& success,
2228 const EthernetInterfaceData& ethData,
2229 const boost::container::flat_set<IPv4AddressData>&,
2230 const boost::container::flat_set<IPv6AddressData>&) {
2231 if (success && !ethData.vlan_id.empty())
2232 {
2233 auto callback =
2234 [asyncResp](
2235 const boost::system::error_code ec) {
2236 if (ec)
2237 {
2238 messages::internalError(asyncResp->res);
2239 }
2240 };
2241 crow::connections::systemBus->async_method_call(
2242 std::move(callback),
2243 "xyz.openbmc_project.Network",
2244 std::string("/xyz/openbmc_project/network/") +
2245 ifaceId,
2246 "xyz.openbmc_project.Object.Delete", "Delete");
2247 }
2248 else
2249 {
2250 // ... otherwise return error
2251 // TODO(Pawel)consider distinguish between non
2252 // existing object, and other errors
2253 messages::resourceNotFound(asyncResp->res,
2254 "VLAN Network Interface",
2255 ifaceId);
2256 }
2257 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002258 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002259
Ed Tanousbf648f72021-06-03 15:00:14 -07002260 BMCWEB_ROUTE(app,
2261 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002262
2263 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002264 .methods(
2265 boost::beast::http::verb::
2266 get)([](const crow::Request& /* req */,
2267 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2268 const std::string& rootInterfaceName) {
2269 // Get eth interface list, and call the below callback for JSON
2270 // preparation
2271 getEthernetIfaceList([asyncResp, rootInterfaceName](
2272 const bool& success,
2273 const boost::container::flat_set<
2274 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002275 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002276 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002277 messages::internalError(asyncResp->res);
2278 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002279 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002280
Ed Tanous81ce6092020-12-17 16:54:55 +00002281 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002282 {
2283 messages::resourceNotFound(asyncResp->res,
2284 "VLanNetworkInterfaceCollection",
2285 rootInterfaceName);
2286 return;
2287 }
2288
Ed Tanous0f74e642018-11-12 15:17:05 -08002289 asyncResp->res.jsonValue["@odata.type"] =
2290 "#VLanNetworkInterfaceCollection."
2291 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002292 asyncResp->res.jsonValue["Name"] =
2293 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002294
Ed Tanous2c70f802020-09-28 14:29:23 -07002295 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002296
Ed Tanous81ce6092020-12-17 16:54:55 +00002297 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002298 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002299 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002300 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002301 std::string path =
2302 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2303 path += rootInterfaceName;
2304 path += "/VLANs/";
2305 path += ifaceItem;
2306 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002307 }
2308 }
2309
Jason M. Billsf12894f2018-10-09 12:45:45 -07002310 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002311 ifaceArray.size();
2312 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002313 asyncResp->res.jsonValue["@odata.id"] =
2314 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2315 rootInterfaceName + "/VLANs";
2316 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002317 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002318
Ed Tanousbf648f72021-06-03 15:00:14 -07002319 BMCWEB_ROUTE(app,
2320 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002321 // This privilege is wrong, it should be ConfigureManager
2322 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002323 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002324 .methods(boost::beast::http::verb::post)(
2325 [](const crow::Request& req,
2326 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2327 const std::string& rootInterfaceName) {
2328 bool vlanEnable = false;
2329 uint32_t vlanId = 0;
2330 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2331 "VLANEnable", vlanEnable))
2332 {
2333 return;
2334 }
2335 // Need both vlanId and vlanEnable to service this request
2336 if (!vlanId)
2337 {
2338 messages::propertyMissing(asyncResp->res, "VLANId");
2339 }
2340 if (!vlanEnable)
2341 {
2342 messages::propertyMissing(asyncResp->res, "VLANEnable");
2343 }
2344 if (static_cast<bool>(vlanId) ^ vlanEnable)
2345 {
2346 return;
2347 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002348
Ed Tanousbf648f72021-06-03 15:00:14 -07002349 auto callback =
2350 [asyncResp](const boost::system::error_code ec) {
2351 if (ec)
2352 {
2353 // TODO(ed) make more consistent error messages
2354 // based on phosphor-network responses
2355 messages::internalError(asyncResp->res);
2356 return;
2357 }
2358 messages::created(asyncResp->res);
2359 };
2360 crow::connections::systemBus->async_method_call(
2361 std::move(callback), "xyz.openbmc_project.Network",
2362 "/xyz/openbmc_project/network",
2363 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2364 rootInterfaceName, vlanId);
2365 });
2366}
2367
Ed Tanous1abe55e2018-09-05 08:30:59 -07002368} // namespace redfish