blob: 9b1aa438b55f901931f507bed1df0fd26496cc15 [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>
Ed Tanous168e20c2021-12-13 14:39:53 -080022#include <dbus_utility.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020023#include <error_messages.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070024#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050025#include <utils/json_utils.hpp>
26
Ed Tanousa24526d2018-12-10 15:17:59 -080027#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053028#include <regex>
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 Tanous168e20c2021-12-13 14:39:53 -080037using PropertiesMapType =
38 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010039
Ed Tanous4a0cb852018-10-15 07:55:04 -070040enum class LinkType
41{
42 Local,
43 Global
44};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010045
46/**
47 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010048 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070049struct IPv4AddressData
50{
51 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070052 std::string address;
53 std::string domain;
54 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 std::string netmask;
56 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070057 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050058 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070059
Gunnar Mills1214b7e2020-06-04 10:11:30 -050060 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010064};
65
66/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050067 * Structure for keeping IPv6 data required by Redfish
68 */
69struct IPv6AddressData
70{
71 std::string id;
72 std::string address;
73 std::string origin;
74 uint8_t prefixLength;
75
Gunnar Mills1214b7e2020-06-04 10:11:30 -050076 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050077 {
78 return id < obj.id;
79 }
80};
81/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010082 * Structure for keeping basic single Ethernet Interface information
83 * available from DBus
84 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070085struct EthernetInterfaceData
86{
Ed Tanous4a0cb852018-10-15 07:55:04 -070087 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053088 size_t mtuSize;
Ed Tanous4a0cb852018-10-15 07:55:04 -070089 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070090 bool DNSEnabled;
91 bool NTPEnabled;
92 bool HostNameEnabled;
93 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080094 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070095 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070096 std::string DHCPEnabled;
97 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -070098 std::string hostname;
99 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500100 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700101 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500102 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500103 std::vector<std::string> nameServers;
104 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800105 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100106};
107
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700108struct DHCPParameters
109{
110 std::optional<bool> dhcpv4Enabled;
111 std::optional<bool> useDNSServers;
112 std::optional<bool> useNTPServers;
113 std::optional<bool> useUseDomainName;
114 std::optional<std::string> dhcpv6OperatingMode;
115};
116
Ed Tanous4a0cb852018-10-15 07:55:04 -0700117// Helper function that changes bits netmask notation (i.e. /24)
118// into full dot notation
119inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700121 uint32_t value = 0xffffffff << (32 - bits);
122 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
123 std::to_string((value >> 16) & 0xff) + "." +
124 std::to_string((value >> 8) & 0xff) + "." +
125 std::to_string(value & 0xff);
126 return netmask;
127}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100128
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500129inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700130 bool isIPv4)
131{
132 if (isIPv4)
133 {
134 return (
135 (inputDHCP ==
136 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
137 (inputDHCP ==
138 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
139 }
140 return ((inputDHCP ==
141 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
142 (inputDHCP ==
143 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
144}
145
Ed Tanous2c70f802020-09-28 14:29:23 -0700146inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700147{
148 if (isIPv4 && isIPv6)
149 {
150 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
151 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700152 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700153 {
154 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
155 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700156 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700157 {
158 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
159 }
160 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
161}
162
Ed Tanous4a0cb852018-10-15 07:55:04 -0700163inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500164 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700165 bool isIPv4)
166{
167 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 return "Static";
170 }
171 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
172 {
173 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700175 return "IPv4LinkLocal";
176 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700177 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 }
179 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
180 {
181 if (isIPv4)
182 {
183 return "DHCP";
184 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700185 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700186 }
187 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
188 {
189 return "SLAAC";
190 }
191 return "";
192}
193
Ed Tanous711ac7a2021-12-20 09:34:41 -0800194inline bool
195 extractEthernetInterfaceData(const std::string& ethifaceId,
196 dbus::utility::ManagedObjectType& dbusData,
197 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700198{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700199 bool idFound = false;
Ed Tanous81ce6092020-12-17 16:54:55 +0000200 for (auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700202 for (auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700203 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000204 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700205 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700206 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500209 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700212 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800214 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 if (mac != nullptr)
216 {
217 ethData.mac_address = *mac;
218 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 }
221 }
222 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
223 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500224 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 {
226 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500228 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800229 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700230 if (id != nullptr)
231 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500232 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700235 }
236 }
237 else if (ifacePair.first ==
238 "xyz.openbmc_project.Network.EthernetInterface")
239 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500240 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 {
242 if (propertyPair.first == "AutoNeg")
243 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700244 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800245 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700246 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700248 ethData.auto_neg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 }
250 }
251 else if (propertyPair.first == "Speed")
252 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500253 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800254 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700255 if (speed != nullptr)
256 {
257 ethData.speed = *speed;
258 }
259 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530260 else if (propertyPair.first == "MTU")
261 {
262 const uint32_t* mtuSize =
263 std::get_if<uint32_t>(&propertyPair.second);
264 if (mtuSize != nullptr)
265 {
266 ethData.mtuSize = *mtuSize;
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,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800440 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000441 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 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800453 for (const 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());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800466 for (const 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,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800518 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000519 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 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800531 for (const 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());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800544 for (const 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 Tanous168e20c2021-12-13 14:39:53 -0800617 dbus::utility::DbusVariantType(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
Ed Tanous543f4402022-01-06 13:12:53 -0800648 char* endPtr = nullptr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700649 long previousValue = 255;
Ed Tanous543f4402022-01-06 13:12:53 -0800650 bool firstZeroInByteHit = false;
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 Tanouse662eae2022-01-25 10:39:19 -0800688 if ((value & (1L << bitIdx)) != 0)
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",
Ed Tanous168e20c2021-12-13 14:39:53 -0800751 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500752}
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 Tanousf94c4ec2022-01-06 12:44:41 -0800944 [ethifaceId{std::string{ethifaceId}},
945 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000946 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800947 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700948 EthernetInterfaceData ethData{};
949 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500950 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700951
Ed Tanous81ce6092020-12-17 16:54:55 +0000952 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700953 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700954 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955 return;
956 }
957
Ed Tanous4c9afe42019-05-03 16:59:57 -0700958 bool found =
Ed Tanous2c70f802020-09-28 14:29:23 -0700959 extractEthernetInterfaceData(ethifaceId, resp, ethData);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700960 if (!found)
961 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700962 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700963 return;
964 }
965
Ed Tanous2c70f802020-09-28 14:29:23 -0700966 extractIPData(ethifaceId, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500968 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700969 {
Ravi Tejac6191412019-07-30 00:53:50 -0500970 if (((ipv4.linktype == LinkType::Global) &&
971 (ipv4.gateway == "0.0.0.0")) ||
Ravi Teja9010ec22019-08-01 23:30:25 -0500972 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700974 ipv4.gateway = ethData.default_gateway;
975 }
976 }
977
Ed Tanous2c70f802020-09-28 14:29:23 -0700978 extractIPV6Data(ethifaceId, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500979 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700980 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700981 },
982 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
983 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700984}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700985
986/**
987 * Function that retrieves all Ethernet Interfaces available through Network
988 * Manager
989 * @param callback a function that shall be called to convert Dbus output
990 * into JSON.
991 */
992template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500993void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994{
995 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800996 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000997 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800998 dbus::utility::ManagedObjectType& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700999 // Callback requires vector<string> to retrieve all available
1000 // ethernet interfaces
Ed Tanous2c70f802020-09-28 14:29:23 -07001001 boost::container::flat_set<std::string> ifaceList;
1002 ifaceList.reserve(resp.size());
Ed Tanous81ce6092020-12-17 16:54:55 +00001003 if (errorCode)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001004 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001005 callback(false, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001006 return;
1007 }
1008
1009 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001010 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001011 {
1012 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001013 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001014 {
1015 // If interface is
1016 // xyz.openbmc_project.Network.EthernetInterface, this is
1017 // what we're looking for.
1018 if (interface.first ==
1019 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001020 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001021 std::string ifaceId = objpath.first.filename();
1022 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 {
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001024 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001025 }
Ed Tanous2dfd18e2020-12-18 00:41:31 +00001026 // and put it into output vector.
1027 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001028 }
1029 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001030 }
1031 // Finally make a callback with useful data
Ed Tanous2c70f802020-09-28 14:29:23 -07001032 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001033 },
1034 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1035 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001036}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001037
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001038inline void
1039 handleHostnamePatch(const std::string& hostname,
1040 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041{
Ed Tanousbf648f72021-06-03 15:00:14 -07001042 // SHOULD handle host names of up to 255 characters(RFC 1123)
1043 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001045 messages::propertyValueFormatError(asyncResp->res, hostname,
1046 "HostName");
1047 return;
1048 }
1049 crow::connections::systemBus->async_method_call(
1050 [asyncResp](const boost::system::error_code ec) {
1051 if (ec)
1052 {
1053 messages::internalError(asyncResp->res);
1054 }
1055 },
1056 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1057 "org.freedesktop.DBus.Properties", "Set",
1058 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001059 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001060}
1061
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001062inline void
Tejas Patil35fb5312021-09-20 15:35:20 +05301063 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
1064 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1065{
1066 sdbusplus::message::object_path objPath =
1067 "/xyz/openbmc_project/network/" + ifaceId;
1068 crow::connections::systemBus->async_method_call(
1069 [asyncResp](const boost::system::error_code ec) {
1070 if (ec)
1071 {
1072 messages::internalError(asyncResp->res);
1073 }
1074 },
1075 "xyz.openbmc_project.Network", objPath,
1076 "org.freedesktop.DBus.Properties", "Set",
1077 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
1078 std::variant<size_t>(mtuSize));
1079}
1080
1081inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001082 handleDomainnamePatch(const std::string& ifaceId,
1083 const std::string& domainname,
1084 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001085{
1086 std::vector<std::string> vectorDomainname = {domainname};
1087 crow::connections::systemBus->async_method_call(
1088 [asyncResp](const boost::system::error_code ec) {
1089 if (ec)
1090 {
1091 messages::internalError(asyncResp->res);
1092 }
1093 },
1094 "xyz.openbmc_project.Network",
1095 "/xyz/openbmc_project/network/" + ifaceId,
1096 "org.freedesktop.DBus.Properties", "Set",
1097 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001098 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001099}
1100
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001101inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001102{
1103 // A valid host name can never have the dotted-decimal form (RFC 1123)
1104 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1105 {
1106 return false;
1107 }
1108 // Each label(hostname/subdomains) within a valid FQDN
1109 // MUST handle host names of up to 63 characters (RFC 1123)
1110 // labels cannot start or end with hyphens (RFC 952)
1111 // labels can start with numbers (RFC 1123)
1112 const std::regex pattern(
1113 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1114
1115 return std::regex_match(hostname, pattern);
1116}
1117
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001118inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001119{
1120 // Can have multiple subdomains
1121 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001122 const std::regex pattern(
1123 "^([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 -07001124
1125 return std::regex_match(domainname, pattern);
1126}
1127
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001128inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1129 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001130{
1131 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1132 if (fqdn.length() > 255)
1133 {
1134 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001136 }
1137
Ed Tanousbf648f72021-06-03 15:00:14 -07001138 size_t pos = fqdn.find('.');
1139 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001140 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001141 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1142 return;
1143 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001144
Ed Tanousbf648f72021-06-03 15:00:14 -07001145 std::string hostname;
1146 std::string domainname;
1147 domainname = (fqdn).substr(pos + 1);
1148 hostname = (fqdn).substr(0, pos);
1149
1150 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1151 {
1152 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1153 return;
1154 }
1155
1156 handleHostnamePatch(hostname, asyncResp);
1157 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1158}
1159
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001160inline void
1161 handleMACAddressPatch(const std::string& ifaceId,
1162 const std::string& macAddress,
1163 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001164{
1165 crow::connections::systemBus->async_method_call(
1166 [asyncResp, macAddress](const boost::system::error_code ec) {
1167 if (ec)
1168 {
1169 messages::internalError(asyncResp->res);
1170 return;
1171 }
1172 },
1173 "xyz.openbmc_project.Network",
1174 "/xyz/openbmc_project/network/" + ifaceId,
1175 "org.freedesktop.DBus.Properties", "Set",
1176 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001177 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001178}
1179
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001180inline void setDHCPEnabled(const std::string& ifaceId,
1181 const std::string& propertyName, const bool v4Value,
1182 const bool v6Value,
1183 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001184{
1185 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1186 crow::connections::systemBus->async_method_call(
1187 [asyncResp](const boost::system::error_code ec) {
1188 if (ec)
1189 {
1190 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1191 messages::internalError(asyncResp->res);
1192 return;
1193 }
1194 messages::success(asyncResp->res);
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,
Ed Tanous168e20c2021-12-13 14:39:53 -08001200 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001201}
1202
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001203inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001204 const std::string& ifaceId, const std::string& propertyName,
1205 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1206{
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/" + ifaceId,
1218 "org.freedesktop.DBus.Properties", "Set",
1219 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001220 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001221}
1222
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001223inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1224 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001225{
1226 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1227 crow::connections::systemBus->async_method_call(
1228 [asyncResp](const boost::system::error_code ec) {
1229 if (ec)
1230 {
1231 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1232 messages::internalError(asyncResp->res);
1233 return;
1234 }
1235 },
1236 "xyz.openbmc_project.Network",
1237 "/xyz/openbmc_project/network/config/dhcp",
1238 "org.freedesktop.DBus.Properties", "Set",
1239 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001240 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001241}
1242
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001243inline void handleDHCPPatch(const std::string& ifaceId,
1244 const EthernetInterfaceData& ethData,
1245 const DHCPParameters& v4dhcpParms,
1246 const DHCPParameters& v6dhcpParms,
1247 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001248{
1249 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1250 bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1251
1252 bool nextv4DHCPState =
1253 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1254
1255 bool nextv6DHCPState{};
1256 if (v6dhcpParms.dhcpv6OperatingMode)
1257 {
1258 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1259 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1260 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1261 {
1262 messages::propertyValueFormatError(asyncResp->res,
1263 *v6dhcpParms.dhcpv6OperatingMode,
1264 "OperatingMode");
1265 return;
1266 }
1267 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1268 }
1269 else
1270 {
1271 nextv6DHCPState = ipv6Active;
1272 }
1273
1274 bool nextDNS{};
1275 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1276 {
1277 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1278 {
1279 messages::generalError(asyncResp->res);
1280 return;
1281 }
1282 nextDNS = *v4dhcpParms.useDNSServers;
1283 }
1284 else if (v4dhcpParms.useDNSServers)
1285 {
1286 nextDNS = *v4dhcpParms.useDNSServers;
1287 }
1288 else if (v6dhcpParms.useDNSServers)
1289 {
1290 nextDNS = *v6dhcpParms.useDNSServers;
1291 }
1292 else
1293 {
1294 nextDNS = ethData.DNSEnabled;
1295 }
1296
1297 bool nextNTP{};
1298 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1299 {
1300 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1301 {
1302 messages::generalError(asyncResp->res);
1303 return;
1304 }
1305 nextNTP = *v4dhcpParms.useNTPServers;
1306 }
1307 else if (v4dhcpParms.useNTPServers)
1308 {
1309 nextNTP = *v4dhcpParms.useNTPServers;
1310 }
1311 else if (v6dhcpParms.useNTPServers)
1312 {
1313 nextNTP = *v6dhcpParms.useNTPServers;
1314 }
1315 else
1316 {
1317 nextNTP = ethData.NTPEnabled;
1318 }
1319
1320 bool nextUseDomain{};
1321 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1322 {
1323 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1324 {
1325 messages::generalError(asyncResp->res);
1326 return;
1327 }
1328 nextUseDomain = *v4dhcpParms.useUseDomainName;
1329 }
1330 else if (v4dhcpParms.useUseDomainName)
1331 {
1332 nextUseDomain = *v4dhcpParms.useUseDomainName;
1333 }
1334 else if (v6dhcpParms.useUseDomainName)
1335 {
1336 nextUseDomain = *v6dhcpParms.useUseDomainName;
1337 }
1338 else
1339 {
1340 nextUseDomain = ethData.HostNameEnabled;
1341 }
1342
1343 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1344 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1345 asyncResp);
1346 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1347 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1348 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1349 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1350 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1351 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1352}
1353
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001354inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001355 getNextStaticIpEntry(
1356 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1357 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1358{
1359 return std::find_if(head, end, [](const IPv4AddressData& value) {
1360 return value.origin == "Static";
1361 });
1362}
1363
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001364inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001365 getNextStaticIpEntry(
1366 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1367 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1368{
1369 return std::find_if(head, end, [](const IPv6AddressData& value) {
1370 return value.origin == "Static";
1371 });
1372}
1373
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001374inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001375 const std::string& ifaceId, nlohmann::json& input,
1376 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1377 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1378{
1379 if ((!input.is_array()) || input.empty())
1380 {
1381 messages::propertyValueTypeError(
1382 asyncResp->res,
1383 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1384 "IPv4StaticAddresses");
1385 return;
1386 }
1387
1388 unsigned entryIdx = 1;
1389 // Find the first static IP address currently active on the NIC and
1390 // match it to the first JSON element in the IPv4StaticAddresses array.
1391 // Match each subsequent JSON element to the next static IP programmed
1392 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001393 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001394 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1395
1396 for (nlohmann::json& thisJson : input)
1397 {
1398 std::string pathString =
1399 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1400
1401 if (!thisJson.is_null() && !thisJson.empty())
1402 {
1403 std::optional<std::string> address;
1404 std::optional<std::string> subnetMask;
1405 std::optional<std::string> gateway;
1406
1407 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1408 address, "SubnetMask", subnetMask,
1409 "Gateway", gateway))
1410 {
1411 messages::propertyValueFormatError(
1412 asyncResp->res,
1413 thisJson.dump(2, ' ', true,
1414 nlohmann::json::error_handler_t::replace),
1415 pathString);
1416 return;
1417 }
1418
1419 // Find the address/subnet/gateway values. Any values that are
1420 // not explicitly provided are assumed to be unmodified from the
1421 // current state of the interface. Merge existing state into the
1422 // current request.
1423 const std::string* addr = nullptr;
1424 const std::string* gw = nullptr;
1425 uint8_t prefixLength = 0;
1426 bool errorInEntry = false;
1427 if (address)
1428 {
1429 if (ipv4VerifyIpAndGetBitcount(*address))
1430 {
1431 addr = &(*address);
1432 }
1433 else
1434 {
1435 messages::propertyValueFormatError(asyncResp->res, *address,
1436 pathString + "/Address");
1437 errorInEntry = true;
1438 }
1439 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001440 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001441 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001442 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001443 }
1444 else
1445 {
1446 messages::propertyMissing(asyncResp->res,
1447 pathString + "/Address");
1448 errorInEntry = true;
1449 }
1450
1451 if (subnetMask)
1452 {
1453 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1454 {
1455 messages::propertyValueFormatError(
1456 asyncResp->res, *subnetMask,
1457 pathString + "/SubnetMask");
1458 errorInEntry = true;
1459 }
1460 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001461 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001462 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001463 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001464 &prefixLength))
1465 {
1466 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001467 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001468 pathString + "/SubnetMask");
1469 errorInEntry = true;
1470 }
1471 }
1472 else
1473 {
1474 messages::propertyMissing(asyncResp->res,
1475 pathString + "/SubnetMask");
1476 errorInEntry = true;
1477 }
1478
1479 if (gateway)
1480 {
1481 if (ipv4VerifyIpAndGetBitcount(*gateway))
1482 {
1483 gw = &(*gateway);
1484 }
1485 else
1486 {
1487 messages::propertyValueFormatError(asyncResp->res, *gateway,
1488 pathString + "/Gateway");
1489 errorInEntry = true;
1490 }
1491 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001492 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001493 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001494 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001495 }
1496 else
1497 {
1498 messages::propertyMissing(asyncResp->res,
1499 pathString + "/Gateway");
1500 errorInEntry = true;
1501 }
1502
1503 if (errorInEntry)
1504 {
1505 return;
1506 }
1507
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001508 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001509 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001510 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001511 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001512 nicIpEntry =
1513 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001514 }
1515 else
1516 {
1517 createIPv4(ifaceId, prefixLength, *gateway, *address,
1518 asyncResp);
1519 }
1520 entryIdx++;
1521 }
1522 else
1523 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001524 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001525 {
1526 // Requesting a DELETE/DO NOT MODIFY action for an item
1527 // that isn't present on the eth(n) interface. Input JSON is
1528 // in error, so bail out.
1529 if (thisJson.is_null())
1530 {
1531 messages::resourceCannotBeDeleted(asyncResp->res);
1532 return;
1533 }
1534 messages::propertyValueFormatError(
1535 asyncResp->res,
1536 thisJson.dump(2, ' ', true,
1537 nlohmann::json::error_handler_t::replace),
1538 pathString);
1539 return;
1540 }
1541
1542 if (thisJson.is_null())
1543 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001544 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001545 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001546 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001547 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001548 nicIpEntry =
1549 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001550 }
1551 entryIdx++;
1552 }
1553 }
1554}
1555
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001556inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001557 const std::string& ifaceId,
1558 const std::vector<std::string>& updatedStaticNameServers,
1559 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1560{
1561 crow::connections::systemBus->async_method_call(
1562 [asyncResp](const boost::system::error_code ec) {
1563 if (ec)
1564 {
1565 messages::internalError(asyncResp->res);
1566 return;
1567 }
1568 },
1569 "xyz.openbmc_project.Network",
1570 "/xyz/openbmc_project/network/" + ifaceId,
1571 "org.freedesktop.DBus.Properties", "Set",
1572 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001573 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001574}
1575
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001576inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001577 const std::string& ifaceId, const nlohmann::json& input,
1578 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1579 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1580{
1581 if (!input.is_array() || input.empty())
1582 {
1583 messages::propertyValueTypeError(
1584 asyncResp->res,
1585 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1586 "IPv6StaticAddresses");
1587 return;
1588 }
1589 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001590 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001591 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1592 for (const nlohmann::json& thisJson : input)
1593 {
1594 std::string pathString =
1595 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1596
1597 if (!thisJson.is_null() && !thisJson.empty())
1598 {
1599 std::optional<std::string> address;
1600 std::optional<uint8_t> prefixLength;
1601 nlohmann::json thisJsonCopy = thisJson;
1602 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1603 address, "PrefixLength", prefixLength))
1604 {
1605 messages::propertyValueFormatError(
1606 asyncResp->res,
1607 thisJson.dump(2, ' ', true,
1608 nlohmann::json::error_handler_t::replace),
1609 pathString);
1610 return;
1611 }
1612
Ed Tanous543f4402022-01-06 13:12:53 -08001613 const std::string* addr = nullptr;
1614 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001615
1616 // Find the address and prefixLength values. Any values that are
1617 // not explicitly provided are assumed to be unmodified from the
1618 // current state of the interface. Merge existing state into the
1619 // current request.
1620 if (address)
1621 {
1622 addr = &(*address);
1623 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001624 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001625 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001626 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001627 }
1628 else
1629 {
1630 messages::propertyMissing(asyncResp->res,
1631 pathString + "/Address");
1632 return;
1633 }
1634
1635 if (prefixLength)
1636 {
1637 prefix = *prefixLength;
1638 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001639 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001640 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001641 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001642 }
1643 else
1644 {
1645 messages::propertyMissing(asyncResp->res,
1646 pathString + "/PrefixLength");
1647 return;
1648 }
1649
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001650 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001651 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001652 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001653 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001654 nicIpEntry =
1655 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001656 }
1657 else
1658 {
1659 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1660 }
1661 entryIdx++;
1662 }
1663 else
1664 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001665 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001666 {
1667 // Requesting a DELETE/DO NOT MODIFY action for an item
1668 // that isn't present on the eth(n) interface. Input JSON is
1669 // in error, so bail out.
1670 if (thisJson.is_null())
1671 {
1672 messages::resourceCannotBeDeleted(asyncResp->res);
1673 return;
1674 }
1675 messages::propertyValueFormatError(
1676 asyncResp->res,
1677 thisJson.dump(2, ' ', true,
1678 nlohmann::json::error_handler_t::replace),
1679 pathString);
1680 return;
1681 }
1682
1683 if (thisJson.is_null())
1684 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001685 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001686 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001687 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001688 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001689 nicIpEntry =
1690 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001691 }
1692 entryIdx++;
1693 }
1694 }
1695}
1696
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001697inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001698 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1699 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1700 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1701 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1702{
1703 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1704 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1705
1706 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1707 jsonResponse["Id"] = ifaceId;
1708 jsonResponse["@odata.id"] =
1709 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1710 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1711
1712 auto health = std::make_shared<HealthPopulate>(asyncResp);
1713
1714 crow::connections::systemBus->async_method_call(
1715 [health](const boost::system::error_code ec,
Ed Tanous914e2d52022-01-07 11:38:34 -08001716 const std::vector<std::string>& resp) {
Ed Tanousbf648f72021-06-03 15:00:14 -07001717 if (ec)
1718 {
1719 return;
1720 }
1721
Ed Tanous914e2d52022-01-07 11:38:34 -08001722 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001723 },
1724 "xyz.openbmc_project.ObjectMapper",
1725 "/xyz/openbmc_project/object_mapper",
1726 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1727 inventoryForEthernet);
1728
1729 health->populate();
1730
1731 if (ethData.nicEnabled)
1732 {
1733 jsonResponse["LinkStatus"] = "LinkUp";
1734 jsonResponse["Status"]["State"] = "Enabled";
1735 }
1736 else
1737 {
1738 jsonResponse["LinkStatus"] = "NoLink";
1739 jsonResponse["Status"]["State"] = "Disabled";
1740 }
1741
1742 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1743 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301744 jsonResponse["MTUSize"] = ethData.mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001745 jsonResponse["MACAddress"] = ethData.mac_address;
1746 jsonResponse["DHCPv4"]["DHCPEnabled"] =
1747 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1748 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1749 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1750 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1751
1752 jsonResponse["DHCPv6"]["OperatingMode"] =
1753 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1754 : "Disabled";
1755 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1756 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1757 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1758
1759 if (!ethData.hostname.empty())
1760 {
1761 jsonResponse["HostName"] = ethData.hostname;
1762
1763 // When domain name is empty then it means, that it is a network
1764 // without domain names, and the host name itself must be treated as
1765 // FQDN
1766 std::string fqdn = ethData.hostname;
1767 if (!ethData.domainnames.empty())
1768 {
1769 fqdn += "." + ethData.domainnames[0];
1770 }
1771 jsonResponse["FQDN"] = fqdn;
1772 }
1773
1774 jsonResponse["VLANs"] = {
1775 {"@odata.id",
1776 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1777
1778 jsonResponse["NameServers"] = ethData.nameServers;
1779 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1780
1781 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1782 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1783 ipv4Array = nlohmann::json::array();
1784 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001785 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001786 {
1787
1788 std::string gatewayStr = ipv4Config.gateway;
1789 if (gatewayStr.empty())
1790 {
1791 gatewayStr = "0.0.0.0";
1792 }
1793
1794 ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1795 {"SubnetMask", ipv4Config.netmask},
1796 {"Address", ipv4Config.address},
1797 {"Gateway", gatewayStr}});
1798 if (ipv4Config.origin == "Static")
1799 {
1800 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1801 {"SubnetMask", ipv4Config.netmask},
1802 {"Address", ipv4Config.address},
1803 {"Gateway", gatewayStr}});
1804 }
1805 }
1806
1807 std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
1808 if (ipv6GatewayStr.empty())
1809 {
1810 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1811 }
1812
1813 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1814
1815 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1816 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1817 ipv6Array = nlohmann::json::array();
1818 ipv6StaticArray = nlohmann::json::array();
1819 nlohmann::json& ipv6AddrPolicyTable =
1820 jsonResponse["IPv6AddressPolicyTable"];
1821 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001822 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001823 {
1824 ipv6Array.push_back({{"Address", ipv6Config.address},
1825 {"PrefixLength", ipv6Config.prefixLength},
1826 {"AddressOrigin", ipv6Config.origin},
1827 {"AddressState", nullptr}});
1828 if (ipv6Config.origin == "Static")
1829 {
1830 ipv6StaticArray.push_back(
1831 {{"Address", ipv6Config.address},
Jiaqing Zhao7a474a52021-12-03 17:44:36 +08001832 {"PrefixLength", ipv6Config.prefixLength}});
Ed Tanousbf648f72021-06-03 15:00:14 -07001833 }
1834 }
1835}
1836
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001837inline void parseInterfaceData(nlohmann::json& jsonResponse,
1838 const std::string& parentIfaceId,
1839 const std::string& ifaceId,
1840 const EthernetInterfaceData& ethData)
Ed Tanousbf648f72021-06-03 15:00:14 -07001841{
1842 // Fill out obvious data...
1843 jsonResponse["Id"] = ifaceId;
1844 jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1845 parentIfaceId + "/VLANs/" + ifaceId;
1846
1847 jsonResponse["VLANEnable"] = true;
1848 if (!ethData.vlan_id.empty())
1849 {
1850 jsonResponse["VLANId"] = ethData.vlan_id.back();
1851 }
1852}
1853
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001854inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001855{
Ed Tanousdcf2ebc2022-01-25 10:07:45 -08001856 return boost::starts_with(iface, parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001857}
1858
1859inline void requestEthernetInterfacesRoutes(App& app)
1860{
1861 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001862 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07001863 .methods(
1864 boost::beast::http::verb::
1865 get)([](const crow::Request&,
1866 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1867 asyncResp->res.jsonValue["@odata.type"] =
1868 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1869 asyncResp->res.jsonValue["@odata.id"] =
1870 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1871 asyncResp->res.jsonValue["Name"] =
1872 "Ethernet Network Interface Collection";
1873 asyncResp->res.jsonValue["Description"] =
1874 "Collection of EthernetInterfaces for this Manager";
1875
1876 // Get eth interface list, and call the below callback for JSON
1877 // preparation
1878 getEthernetIfaceList([asyncResp](const bool& success,
1879 const boost::container::flat_set<
1880 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001881 if (!success)
1882 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001883 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001884 return;
1885 }
1886
Ed Tanous2c70f802020-09-28 14:29:23 -07001887 nlohmann::json& ifaceArray =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001888 asyncResp->res.jsonValue["Members"];
Ed Tanous2c70f802020-09-28 14:29:23 -07001889 ifaceArray = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001890 std::string tag = "_";
Ed Tanous81ce6092020-12-17 16:54:55 +00001891 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001892 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001893 std::size_t found = ifaceItem.find(tag);
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001894 if (found == std::string::npos)
1895 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001896 ifaceArray.push_back(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001897 {{"@odata.id",
1898 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
Ed Tanous2c70f802020-09-28 14:29:23 -07001899 ifaceItem}});
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001900 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001901 }
1902
Ed Tanous4c9afe42019-05-03 16:59:57 -07001903 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07001904 ifaceArray.size();
Ed Tanous4c9afe42019-05-03 16:59:57 -07001905 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001906 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001907 });
Manojkiran Eda17a897d2020-09-12 15:31:58 +05301908 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001909
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001911 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001912 .methods(boost::beast::http::verb::get)(
1913 [](const crow::Request&,
1914 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1915 const std::string& ifaceId) {
1916 getEthernetIfaceData(
1917 ifaceId,
1918 [asyncResp,
1919 ifaceId](const bool& success,
1920 const EthernetInterfaceData& ethData,
1921 const boost::container::flat_set<IPv4AddressData>&
1922 ipv4Data,
1923 const boost::container::flat_set<IPv6AddressData>&
1924 ipv6Data) {
1925 if (!success)
1926 {
1927 // TODO(Pawel)consider distinguish between non
1928 // existing object, and other errors
1929 messages::resourceNotFound(
1930 asyncResp->res, "EthernetInterface", ifaceId);
1931 return;
1932 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001933
Ed Tanousbf648f72021-06-03 15:00:14 -07001934 asyncResp->res.jsonValue["@odata.type"] =
1935 "#EthernetInterface.v1_4_1.EthernetInterface";
1936 asyncResp->res.jsonValue["Name"] =
1937 "Manager Ethernet Interface";
1938 asyncResp->res.jsonValue["Description"] =
1939 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301940
Ed Tanousbf648f72021-06-03 15:00:14 -07001941 parseInterfaceData(asyncResp, ifaceId, ethData,
1942 ipv4Data, ipv6Data);
1943 });
1944 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001945
Ed Tanousbf648f72021-06-03 15:00:14 -07001946 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001947 .privileges(redfish::privileges::patchEthernetInterface)
1948
Ed Tanousbf648f72021-06-03 15:00:14 -07001949 .methods(boost::beast::http::verb::patch)(
1950 [](const crow::Request& req,
1951 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1952 const std::string& ifaceId) {
1953 std::optional<std::string> hostname;
1954 std::optional<std::string> fqdn;
1955 std::optional<std::string> macAddress;
1956 std::optional<std::string> ipv6DefaultGateway;
1957 std::optional<nlohmann::json> ipv4StaticAddresses;
1958 std::optional<nlohmann::json> ipv6StaticAddresses;
1959 std::optional<std::vector<std::string>> staticNameServers;
1960 std::optional<nlohmann::json> dhcpv4;
1961 std::optional<nlohmann::json> dhcpv6;
1962 std::optional<bool> interfaceEnabled;
Tejas Patil35fb5312021-09-20 15:35:20 +05301963 std::optional<size_t> mtuSize;
Ed Tanousbf648f72021-06-03 15:00:14 -07001964 DHCPParameters v4dhcpParms;
1965 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001966
Ed Tanousbf648f72021-06-03 15:00:14 -07001967 if (!json_util::readJson(
1968 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1969 "IPv4StaticAddresses", ipv4StaticAddresses,
1970 "MACAddress", macAddress, "StaticNameServers",
1971 staticNameServers, "IPv6DefaultGateway",
1972 ipv6DefaultGateway, "IPv6StaticAddresses",
1973 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
Tejas Patil35fb5312021-09-20 15:35:20 +05301974 "MTUSize", mtuSize, "InterfaceEnabled",
1975 interfaceEnabled))
Ed Tanous4a0cb852018-10-15 07:55:04 -07001976 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001977 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001978 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001979 if (dhcpv4)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001980 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001981 if (!json_util::readJson(
1982 *dhcpv4, asyncResp->res, "DHCPEnabled",
1983 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1984 v4dhcpParms.useDNSServers, "UseNTPServers",
1985 v4dhcpParms.useNTPServers, "UseDomainName",
1986 v4dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07001987 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001988 return;
1989 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001990 }
1991
Ed Tanousbf648f72021-06-03 15:00:14 -07001992 if (dhcpv6)
Johnathan Mantey01784822019-06-18 12:44:21 -07001993 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001994 if (!json_util::readJson(
1995 *dhcpv6, asyncResp->res, "OperatingMode",
1996 v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1997 v6dhcpParms.useDNSServers, "UseNTPServers",
1998 v6dhcpParms.useNTPServers, "UseDomainName",
1999 v6dhcpParms.useUseDomainName))
Johnathan Mantey01784822019-06-18 12:44:21 -07002000 {
Johnathan Mantey01784822019-06-18 12:44:21 -07002001 return;
2002 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002003 }
2004
Ed Tanousbf648f72021-06-03 15:00:14 -07002005 // Get single eth interface data, and call the below callback
2006 // for JSON preparation
2007 getEthernetIfaceData(
2008 ifaceId,
2009 [asyncResp, ifaceId, hostname = std::move(hostname),
2010 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2011 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
2012 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2013 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
2014 staticNameServers = std::move(staticNameServers),
2015 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
Tejas Patil35fb5312021-09-20 15:35:20 +05302016 mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
Ed Tanousbf648f72021-06-03 15:00:14 -07002017 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2018 const bool& success,
2019 const EthernetInterfaceData& ethData,
2020 const boost::container::flat_set<IPv4AddressData>&
2021 ipv4Data,
2022 const boost::container::flat_set<IPv6AddressData>&
2023 ipv6Data) {
2024 if (!success)
2025 {
2026 // ... otherwise return error
2027 // TODO(Pawel)consider distinguish between non
2028 // existing object, and other errors
2029 messages::resourceNotFound(
2030 asyncResp->res, "Ethernet Interface", ifaceId);
2031 return;
2032 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002033
Ed Tanousbf648f72021-06-03 15:00:14 -07002034 if (dhcpv4 || dhcpv6)
2035 {
2036 handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2037 v6dhcpParms, asyncResp);
2038 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002039
Ed Tanousbf648f72021-06-03 15:00:14 -07002040 if (hostname)
2041 {
2042 handleHostnamePatch(*hostname, asyncResp);
2043 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002044
Ed Tanousbf648f72021-06-03 15:00:14 -07002045 if (fqdn)
2046 {
2047 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2048 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002049
Ed Tanousbf648f72021-06-03 15:00:14 -07002050 if (macAddress)
2051 {
2052 handleMACAddressPatch(ifaceId, *macAddress,
2053 asyncResp);
2054 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002055
Ed Tanousbf648f72021-06-03 15:00:14 -07002056 if (ipv4StaticAddresses)
2057 {
2058 // TODO(ed) for some reason the capture of
2059 // ipv4Addresses above is returning a const value,
2060 // not a non-const value. This doesn't really work
2061 // for us, as we need to be able to efficiently move
2062 // out the intermedia nlohmann::json objects. This
2063 // makes a copy of the structure, and operates on
2064 // that, but could be done more efficiently
2065 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2066 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2067 asyncResp);
2068 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002069
Ed Tanousbf648f72021-06-03 15:00:14 -07002070 if (staticNameServers)
2071 {
2072 handleStaticNameServersPatch(
2073 ifaceId, *staticNameServers, asyncResp);
2074 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002075
Ed Tanousbf648f72021-06-03 15:00:14 -07002076 if (ipv6DefaultGateway)
2077 {
2078 messages::propertyNotWritable(asyncResp->res,
2079 "IPv6DefaultGateway");
2080 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08002081
Ed Tanousbf648f72021-06-03 15:00:14 -07002082 if (ipv6StaticAddresses)
2083 {
2084 nlohmann::json ipv6Static = *ipv6StaticAddresses;
2085 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2086 ipv6Data, asyncResp);
2087 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002088
Ed Tanousbf648f72021-06-03 15:00:14 -07002089 if (interfaceEnabled)
2090 {
2091 setEthernetInterfaceBoolProperty(
2092 ifaceId, "NICEnabled", *interfaceEnabled,
2093 asyncResp);
2094 }
Tejas Patil35fb5312021-09-20 15:35:20 +05302095
2096 if (mtuSize)
2097 {
2098 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2099 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002100 });
2101 });
manojkiraneda2a133282019-02-19 13:09:43 +05302102
Ed Tanousbf648f72021-06-03 15:00:14 -07002103 BMCWEB_ROUTE(
2104 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002105 .privileges(redfish::privileges::getVLanNetworkInterface)
2106
Ed Tanousbf648f72021-06-03 15:00:14 -07002107 .methods(boost::beast::http::verb::get)(
2108 [](const crow::Request& /* req */,
2109 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2110 const std::string& parentIfaceId, const std::string& ifaceId) {
Ed Tanous0f74e642018-11-12 15:17:05 -08002111 asyncResp->res.jsonValue["@odata.type"] =
Ed Tanousbf648f72021-06-03 15:00:14 -07002112 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2113 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002114
Ed Tanousbf648f72021-06-03 15:00:14 -07002115 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002116 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002117 return;
2118 }
2119
Ed Tanousbf648f72021-06-03 15:00:14 -07002120 // Get single eth interface data, and call the below callback
2121 // for JSON preparation
2122 getEthernetIfaceData(
2123 ifaceId,
2124 [asyncResp, parentIfaceId, ifaceId](
2125 const bool& success,
2126 const EthernetInterfaceData& ethData,
2127 const boost::container::flat_set<IPv4AddressData>&,
2128 const boost::container::flat_set<IPv6AddressData>&) {
Ed Tanous26f69762022-01-25 09:49:11 -08002129 if (success && !ethData.vlan_id.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07002130 {
2131 parseInterfaceData(asyncResp->res.jsonValue,
2132 parentIfaceId, ifaceId, ethData);
2133 }
2134 else
2135 {
2136 // ... otherwise return error
2137 // TODO(Pawel)consider distinguish between non
2138 // existing object, and other errors
2139 messages::resourceNotFound(asyncResp->res,
2140 "VLAN Network Interface",
2141 ifaceId);
2142 }
2143 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002144 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002145
Ed Tanousbf648f72021-06-03 15:00:14 -07002146 BMCWEB_ROUTE(
2147 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002148 // This privilege is incorrect, it should be ConfigureManager
2149 //.privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002150 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002151 .methods(boost::beast::http::verb::patch)(
2152 [](const crow::Request& req,
2153 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2154 const std::string& parentIfaceId, const std::string& ifaceId) {
2155 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002156 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002157 messages::resourceNotFound(
2158 asyncResp->res, "VLAN Network Interface", ifaceId);
2159 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002160 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002161
Ed Tanousbf648f72021-06-03 15:00:14 -07002162 bool vlanEnable = false;
2163 uint32_t vlanId = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002164
Ed Tanousbf648f72021-06-03 15:00:14 -07002165 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2166 vlanEnable, "VLANId", vlanId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002167 {
Ed Tanousbf648f72021-06-03 15:00:14 -07002168 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002169 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002170
2171 // Get single eth interface data, and call the below callback
2172 // for JSON preparation
2173 getEthernetIfaceData(
2174 ifaceId,
2175 [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2176 const bool& success,
2177 const EthernetInterfaceData& ethData,
2178 const boost::container::flat_set<IPv4AddressData>&,
2179 const boost::container::flat_set<IPv6AddressData>&) {
2180 if (success && !ethData.vlan_id.empty())
2181 {
2182 auto callback =
2183 [asyncResp](
2184 const boost::system::error_code ec) {
2185 if (ec)
2186 {
2187 messages::internalError(asyncResp->res);
2188 }
2189 };
2190
Ed Tanouse05aec52022-01-25 10:28:56 -08002191 if (vlanEnable)
Ed Tanousbf648f72021-06-03 15:00:14 -07002192 {
2193 crow::connections::systemBus->async_method_call(
2194 std::move(callback),
2195 "xyz.openbmc_project.Network",
2196 "/xyz/openbmc_project/network/" + ifaceId,
2197 "org.freedesktop.DBus.Properties", "Set",
2198 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous168e20c2021-12-13 14:39:53 -08002199 dbus::utility::DbusVariantType(vlanId));
Ed Tanousbf648f72021-06-03 15:00:14 -07002200 }
2201 else
2202 {
2203 BMCWEB_LOG_DEBUG
2204 << "vlanEnable is false. Deleting the "
2205 "vlan interface";
2206 crow::connections::systemBus->async_method_call(
2207 std::move(callback),
2208 "xyz.openbmc_project.Network",
2209 std::string(
2210 "/xyz/openbmc_project/network/") +
2211 ifaceId,
2212 "xyz.openbmc_project.Object.Delete",
2213 "Delete");
2214 }
2215 }
2216 else
2217 {
2218 // TODO(Pawel)consider distinguish between non
2219 // existing object, and other errors
2220 messages::resourceNotFound(asyncResp->res,
2221 "VLAN Network Interface",
2222 ifaceId);
2223 return;
2224 }
2225 });
2226 });
2227
2228 BMCWEB_ROUTE(
2229 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002230 // This privilege is incorrect, it should be ConfigureManager
2231 //.privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanous432a8902021-06-14 15:28:56 -07002232 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002233 .methods(boost::beast::http::verb::delete_)(
2234 [](const crow::Request& /* req */,
2235 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2236 const std::string& parentIfaceId, const std::string& ifaceId) {
2237 if (!verifyNames(parentIfaceId, ifaceId))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002238 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002239 messages::resourceNotFound(
2240 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07002241 return;
Jason M. Billsf12894f2018-10-09 12:45:45 -07002242 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002243
2244 // Get single eth interface data, and call the below callback
2245 // for JSON preparation
2246 getEthernetIfaceData(
2247 ifaceId,
2248 [asyncResp, parentIfaceId, ifaceId](
2249 const bool& success,
2250 const EthernetInterfaceData& ethData,
2251 const boost::container::flat_set<IPv4AddressData>&,
2252 const boost::container::flat_set<IPv6AddressData>&) {
2253 if (success && !ethData.vlan_id.empty())
2254 {
2255 auto callback =
2256 [asyncResp](
2257 const boost::system::error_code ec) {
2258 if (ec)
2259 {
2260 messages::internalError(asyncResp->res);
2261 }
2262 };
2263 crow::connections::systemBus->async_method_call(
2264 std::move(callback),
2265 "xyz.openbmc_project.Network",
2266 std::string("/xyz/openbmc_project/network/") +
2267 ifaceId,
2268 "xyz.openbmc_project.Object.Delete", "Delete");
2269 }
2270 else
2271 {
2272 // ... otherwise return error
2273 // TODO(Pawel)consider distinguish between non
2274 // existing object, and other errors
2275 messages::resourceNotFound(asyncResp->res,
2276 "VLAN Network Interface",
2277 ifaceId);
2278 }
2279 });
Jason M. Billsf12894f2018-10-09 12:45:45 -07002280 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002281
Ed Tanousbf648f72021-06-03 15:00:14 -07002282 BMCWEB_ROUTE(app,
2283 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002284
2285 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002286 .methods(
2287 boost::beast::http::verb::
2288 get)([](const crow::Request& /* req */,
2289 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2290 const std::string& rootInterfaceName) {
2291 // Get eth interface list, and call the below callback for JSON
2292 // preparation
2293 getEthernetIfaceList([asyncResp, rootInterfaceName](
2294 const bool& success,
2295 const boost::container::flat_set<
2296 std::string>& ifaceList) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002297 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002298 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002299 messages::internalError(asyncResp->res);
2300 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002301 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002302
Ed Tanous81ce6092020-12-17 16:54:55 +00002303 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
Ed Tanous4c9afe42019-05-03 16:59:57 -07002304 {
2305 messages::resourceNotFound(asyncResp->res,
2306 "VLanNetworkInterfaceCollection",
2307 rootInterfaceName);
2308 return;
2309 }
2310
Ed Tanous0f74e642018-11-12 15:17:05 -08002311 asyncResp->res.jsonValue["@odata.type"] =
2312 "#VLanNetworkInterfaceCollection."
2313 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002314 asyncResp->res.jsonValue["Name"] =
2315 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002316
Ed Tanous2c70f802020-09-28 14:29:23 -07002317 nlohmann::json ifaceArray = nlohmann::json::array();
Jason M. Billsf12894f2018-10-09 12:45:45 -07002318
Ed Tanous81ce6092020-12-17 16:54:55 +00002319 for (const std::string& ifaceItem : ifaceList)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002320 {
Ed Tanous2c70f802020-09-28 14:29:23 -07002321 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
Jason M. Billsf12894f2018-10-09 12:45:45 -07002322 {
Ed Tanousf23b7292020-10-15 09:41:17 -07002323 std::string path =
2324 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2325 path += rootInterfaceName;
2326 path += "/VLANs/";
2327 path += ifaceItem;
2328 ifaceArray.push_back({{"@odata.id", std::move(path)}});
Jason M. Billsf12894f2018-10-09 12:45:45 -07002329 }
2330 }
2331
Jason M. Billsf12894f2018-10-09 12:45:45 -07002332 asyncResp->res.jsonValue["Members@odata.count"] =
Ed Tanous2c70f802020-09-28 14:29:23 -07002333 ifaceArray.size();
2334 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Jason M. Billsf12894f2018-10-09 12:45:45 -07002335 asyncResp->res.jsonValue["@odata.id"] =
2336 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2337 rootInterfaceName + "/VLANs";
2338 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002339 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002340
Ed Tanousbf648f72021-06-03 15:00:14 -07002341 BMCWEB_ROUTE(app,
2342 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002343 // This privilege is wrong, it should be ConfigureManager
2344 //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanous432a8902021-06-14 15:28:56 -07002345 .privileges({{"ConfigureComponents"}})
Ed Tanousbf648f72021-06-03 15:00:14 -07002346 .methods(boost::beast::http::verb::post)(
2347 [](const crow::Request& req,
2348 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2349 const std::string& rootInterfaceName) {
2350 bool vlanEnable = false;
2351 uint32_t vlanId = 0;
2352 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
2353 "VLANEnable", vlanEnable))
2354 {
2355 return;
2356 }
2357 // Need both vlanId and vlanEnable to service this request
Ed Tanouse662eae2022-01-25 10:39:19 -08002358 if (vlanId == 0u)
Ed Tanousbf648f72021-06-03 15:00:14 -07002359 {
2360 messages::propertyMissing(asyncResp->res, "VLANId");
2361 }
2362 if (!vlanEnable)
2363 {
2364 messages::propertyMissing(asyncResp->res, "VLANEnable");
2365 }
2366 if (static_cast<bool>(vlanId) ^ vlanEnable)
2367 {
2368 return;
2369 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002370
Ed Tanousbf648f72021-06-03 15:00:14 -07002371 auto callback =
2372 [asyncResp](const boost::system::error_code ec) {
2373 if (ec)
2374 {
2375 // TODO(ed) make more consistent error messages
2376 // based on phosphor-network responses
2377 messages::internalError(asyncResp->res);
2378 return;
2379 }
2380 messages::created(asyncResp->res);
2381 };
2382 crow::connections::systemBus->async_method_call(
2383 std::move(callback), "xyz.openbmc_project.Network",
2384 "/xyz/openbmc_project/network",
2385 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2386 rootInterfaceName, vlanId);
2387 });
2388}
2389
Ed Tanous1abe55e2018-09-05 08:30:59 -07002390} // namespace redfish