blob: 93c475ee5950112d1db3d2e4814b00e098bcd44c [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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "error_messages.hpp"
22#include "health.hpp"
23#include "query.hpp"
24#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070025#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070027
Ed Tanous11ba3972022-07-11 09:50:41 -070028#include <boost/algorithm/string/classification.hpp>
29#include <boost/algorithm/string/split.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070030#include <boost/container/flat_set.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070031#include <boost/url/format.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050032
George Liu7a1dbc42022-12-07 16:03:22 +080033#include <array>
Ed Tanousa24526d2018-12-10 15:17:59 -080034#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053035#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <string_view>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace redfish
39{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
Ed Tanous4a0cb852018-10-15 07:55:04 -070041enum class LinkType
42{
43 Local,
44 Global
45};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010046
47/**
48 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010049 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070050struct IPv4AddressData
51{
52 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070053 std::string address;
54 std::string domain;
55 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 std::string netmask;
57 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070058 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050059 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070060
Gunnar Mills1214b7e2020-06-04 10:11:30 -050061 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070063 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010065};
66
67/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050068 * Structure for keeping IPv6 data required by Redfish
69 */
70struct IPv6AddressData
71{
72 std::string id;
73 std::string address;
74 std::string origin;
75 uint8_t prefixLength;
76
Gunnar Mills1214b7e2020-06-04 10:11:30 -050077 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050078 {
79 return id < obj.id;
80 }
81};
82/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010083 * Structure for keeping basic single Ethernet Interface information
84 * available from DBus
85 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070086struct EthernetInterfaceData
87{
Ed Tanous4a0cb852018-10-15 07:55:04 -070088 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053089 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080090 bool autoNeg;
91 bool dnsEnabled;
92 bool ntpEnabled;
93 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080094 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070095 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080096 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070097 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080098 std::string hostName;
99 std::string defaultGateway;
100 std::string ipv6DefaultGateway;
101 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800102 std::optional<uint32_t> vlanId;
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;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800111 std::optional<bool> useDnsServers;
112 std::optional<bool> useNtpServers;
113 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700114 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
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800129inline 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 Tanous02cad962022-06-30 16:50:15 -0700194inline bool extractEthernetInterfaceData(
195 const std::string& ethifaceId,
196 const 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 Tanous02cad962022-06-30 16:50:15 -0700200 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 {
Ed Tanous02cad962022-06-30 16:50:15 -0700202 for (const 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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800217 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 }
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 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800232 ethData.vlanId = *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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800248 ethData.autoNeg = *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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800313 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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800335 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500336 }
337 else
338 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800339 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500340 }
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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800353 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500354 "0:0:0:0:0:0:0:0";
355 }
356 else
357 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800358 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500359 defaultGateway6Str;
360 }
361 }
362 }
Ed Tanous029573d2019-02-01 10:57:49 -0800363 }
364 }
365 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700366
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800367 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700368 {
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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800380 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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800389 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 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800398 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700399 }
400 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700401 }
402 }
403 }
Ed Tanous029573d2019-02-01 10:57:49 -0800404 // System configuration shows up in the global namespace, so no need
405 // to check eth number
406 if (ifacePair.first ==
407 "xyz.openbmc_project.Network.SystemConfiguration")
408 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500409 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800410 {
411 if (propertyPair.first == "HostName")
412 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500413 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500414 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800415 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700416 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800417 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800418 }
419 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 }
421 }
422 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700424 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700425}
426
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500427// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700428inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000429 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800430 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000431 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500432{
Patrick Williams89492a12023-05-10 07:51:34 -0500433 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
434 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500435
436 // Since there might be several IPv6 configurations aligned with
437 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000438 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500439 {
440 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800441 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500442 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800443 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500444 {
445 if (interface.first == "xyz.openbmc_project.Network.IP")
446 {
Tony Lee353163e2022-11-23 11:06:10 +0800447 auto type = std::find_if(interface.second.begin(),
448 interface.second.end(),
449 [](const auto& property) {
450 return property.first == "Type";
451 });
452 if (type == interface.second.end())
453 {
454 continue;
455 }
456
457 const std::string* typeStr =
458 std::get_if<std::string>(&type->second);
459
460 if (typeStr == nullptr ||
461 (*typeStr !=
462 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
463 {
464 continue;
465 }
466
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500467 // Instance IPv6AddressData structure, and set as
468 // appropriate
469 std::pair<
470 boost::container::flat_set<IPv6AddressData>::iterator,
471 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000472 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700473 IPv6AddressData& ipv6Address = *it.first;
474 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800475 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800476 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500477 {
478 if (property.first == "Address")
479 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500480 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500481 std::get_if<std::string>(&property.second);
482 if (address != nullptr)
483 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700484 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500485 }
486 }
487 else if (property.first == "Origin")
488 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500489 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 std::get_if<std::string>(&property.second);
491 if (origin != nullptr)
492 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700493 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500494 translateAddressOriginDbusToRedfish(*origin,
495 false);
496 }
497 }
498 else if (property.first == "PrefixLength")
499 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500500 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500501 std::get_if<uint8_t>(&property.second);
502 if (prefix != nullptr)
503 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700504 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500505 }
506 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600507 else if (property.first == "Type" ||
508 property.first == "Gateway")
509 {
510 // Type & Gateway is not used
511 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500512 else
513 {
514 BMCWEB_LOG_ERROR
515 << "Got extra property: " << property.first
516 << " on the " << objpath.first.str << " object";
517 }
518 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500519 }
520 }
521 }
522 }
523}
524
Ed Tanous4a0cb852018-10-15 07:55:04 -0700525// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700526inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000527 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800528 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000529 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700530{
Patrick Williams89492a12023-05-10 07:51:34 -0500531 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
532 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700533
534 // Since there might be several IPv4 configurations aligned with
535 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000536 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700537 {
538 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800539 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700540 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800541 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700542 {
543 if (interface.first == "xyz.openbmc_project.Network.IP")
544 {
Tony Lee353163e2022-11-23 11:06:10 +0800545 auto type = std::find_if(interface.second.begin(),
546 interface.second.end(),
547 [](const auto& property) {
548 return property.first == "Type";
549 });
550 if (type == interface.second.end())
551 {
552 continue;
553 }
554
555 const std::string* typeStr =
556 std::get_if<std::string>(&type->second);
557
558 if (typeStr == nullptr ||
559 (*typeStr !=
560 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
561 {
562 continue;
563 }
564
Ed Tanous4a0cb852018-10-15 07:55:04 -0700565 // Instance IPv4AddressData structure, and set as
566 // appropriate
567 std::pair<
568 boost::container::flat_set<IPv4AddressData>::iterator,
569 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000570 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700571 IPv4AddressData& ipv4Address = *it.first;
572 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800573 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800574 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700575 {
576 if (property.first == "Address")
577 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500578 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800579 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700580 if (address != nullptr)
581 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700582 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700583 }
584 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700585 else if (property.first == "Origin")
586 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500587 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800588 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700589 if (origin != nullptr)
590 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700591 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700592 translateAddressOriginDbusToRedfish(*origin,
593 true);
594 }
595 }
596 else if (property.first == "PrefixLength")
597 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500598 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800599 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700600 if (mask != nullptr)
601 {
602 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700603 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604 }
605 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600606 else if (property.first == "Type" ||
607 property.first == "Gateway")
608 {
609 // Type & Gateway is not used
610 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611 else
612 {
613 BMCWEB_LOG_ERROR
614 << "Got extra property: " << property.first
615 << " on the " << objpath.first.str << " object";
616 }
617 }
618 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700619 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700620 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700621 ? LinkType::Local
622 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700623 }
624 }
625 }
626 }
627}
628
629/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700630 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700631 *
632 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700633 * @param[in] ipHash DBus Hash id of IP that should be deleted
634 * @param[io] asyncResp Response object that will be returned to client
635 *
636 * @return None
637 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600638inline void deleteIPAddress(const std::string& ifaceId,
639 const std::string& ipHash,
640 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700641{
642 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800643 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700644 if (ec)
645 {
646 messages::internalError(asyncResp->res);
647 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 },
649 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600650 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 "xyz.openbmc_project.Object.Delete", "Delete");
652}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653
Gunnar Mills244b6d52021-04-12 15:44:23 -0500654inline void updateIPv4DefaultGateway(
655 const std::string& ifaceId, const std::string& gateway,
656 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500657{
658 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800659 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700660 if (ec)
661 {
662 messages::internalError(asyncResp->res);
663 return;
664 }
665 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500666 },
667 "xyz.openbmc_project.Network",
668 "/xyz/openbmc_project/network/" + ifaceId,
669 "org.freedesktop.DBus.Properties", "Set",
670 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800671 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500672}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700674 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700676 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
677 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
678 * @param[in] gateway IPv4 address of this interfaces gateway
679 * @param[in] address IPv4 address to assign to this interface
680 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 *
682 * @return None
683 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000684inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
685 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800686 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700687{
Ed Tanous002d39b2022-05-31 08:59:27 -0700688 auto createIpHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800689 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500690 if (ec)
691 {
692 messages::internalError(asyncResp->res);
693 return;
694 }
695 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
696 };
697
Ed Tanous4a0cb852018-10-15 07:55:04 -0700698 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500699 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700 "/xyz/openbmc_project/network/" + ifaceId,
701 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700702 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700703 gateway);
704}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500705
706/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700707 * @brief Deletes the IPv6 entry for this interface and creates a replacement
708 * static IPv6 entry
709 *
710 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
711 * @param[in] id The unique hash entry identifying the DBus entry
712 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
713 * @param[in] address IPv6 address to assign to this interface
714 * @param[io] asyncResp Response object that will be returned to client
715 *
716 * @return None
717 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600718
719enum class IpVersion
720{
721 IpV4,
722 IpV6
723};
724
725inline void deleteAndCreateIPAddress(
726 IpVersion version, const std::string& ifaceId, const std::string& id,
727 uint8_t prefixLength, const std::string& address,
728 const std::string& gateway,
729 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700730{
731 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600732 [asyncResp, version, ifaceId, address, prefixLength,
733 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700734 if (ec)
735 {
736 messages::internalError(asyncResp->res);
737 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600738 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
739 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700740 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800741 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700742 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700743 {
744 messages::internalError(asyncResp->res);
745 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700746 },
747 "xyz.openbmc_project.Network",
748 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600749 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
750 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700751 },
752 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600753 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700754 "xyz.openbmc_project.Object.Delete", "Delete");
755}
756
757/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500758 * @brief Creates IPv6 with given data
759 *
760 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500761 * @param[in] prefixLength Prefix length that needs to be added
762 * @param[in] address IP address that needs to be added
763 * @param[io] asyncResp Response object that will be returned to client
764 *
765 * @return None
766 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500767inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
768 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800769 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500770{
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800771 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500772 if (ec)
773 {
774 messages::internalError(asyncResp->res);
775 }
776 };
777 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500778 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500779 crow::connections::systemBus->async_method_call(
780 std::move(createIpHandler), "xyz.openbmc_project.Network",
781 "/xyz/openbmc_project/network/" + ifaceId,
782 "xyz.openbmc_project.Network.IP.Create", "IP",
783 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
784 "");
785}
786
Ed Tanous4a0cb852018-10-15 07:55:04 -0700787/**
788 * Function that retrieves all properties for given Ethernet Interface
789 * Object
790 * from EntityManager Network Manager
791 * @param ethiface_id a eth interface id to query on DBus
792 * @param callback a function that shall be called to convert Dbus output
793 * into JSON
794 */
795template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000796void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500797 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700798{
George Liuf5892d02023-03-01 10:37:08 +0800799 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
800 dbus::utility::getManagedObjects(
801 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800802 [ethifaceId{std::string{ethifaceId}},
803 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800804 const boost::system::error_code& errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700805 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700806 EthernetInterfaceData ethData{};
807 boost::container::flat_set<IPv4AddressData> ipv4Data;
808 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700809
Ed Tanous002d39b2022-05-31 08:59:27 -0700810 if (errorCode)
811 {
812 callback(false, ethData, ipv4Data, ipv6Data);
813 return;
814 }
815
816 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
817 if (!found)
818 {
819 callback(false, ethData, ipv4Data, ipv6Data);
820 return;
821 }
822
823 extractIPData(ethifaceId, resp, ipv4Data);
824 // Fix global GW
825 for (IPv4AddressData& ipv4 : ipv4Data)
826 {
827 if (((ipv4.linktype == LinkType::Global) &&
828 (ipv4.gateway == "0.0.0.0")) ||
829 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700830 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700831 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700832 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700833 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700834
Ed Tanous002d39b2022-05-31 08:59:27 -0700835 extractIPV6Data(ethifaceId, resp, ipv6Data);
836 // Finally make a callback with useful data
837 callback(true, ethData, ipv4Data, ipv6Data);
George Liuf5892d02023-03-01 10:37:08 +0800838 });
Ed Tanous271584a2019-07-09 16:24:22 -0700839}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700840
841/**
842 * Function that retrieves all Ethernet Interfaces available through Network
843 * Manager
844 * @param callback a function that shall be called to convert Dbus output
845 * into JSON.
846 */
847template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500848void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700849{
George Liuf5892d02023-03-01 10:37:08 +0800850 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
851 dbus::utility::getManagedObjects(
852 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800853 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800854 const boost::system::error_code& errorCode,
George Liuf5892d02023-03-01 10:37:08 +0800855 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700856 // Callback requires vector<string> to retrieve all available
857 // ethernet interfaces
858 boost::container::flat_set<std::string> ifaceList;
859 ifaceList.reserve(resp.size());
860 if (errorCode)
861 {
862 callback(false, ifaceList);
863 return;
864 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700865
Ed Tanous002d39b2022-05-31 08:59:27 -0700866 // Iterate over all retrieved ObjectPaths.
867 for (const auto& objpath : resp)
868 {
869 // And all interfaces available for certain ObjectPath.
870 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700871 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700872 // If interface is
873 // xyz.openbmc_project.Network.EthernetInterface, this is
874 // what we're looking for.
875 if (interface.first ==
876 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700877 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700878 std::string ifaceId = objpath.first.filename();
879 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700881 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700883 // and put it into output vector.
884 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700886 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700887 }
888 // Finally make a callback with useful data
889 callback(true, ifaceList);
George Liuf5892d02023-03-01 10:37:08 +0800890 });
Ed Tanous271584a2019-07-09 16:24:22 -0700891}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100892
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700893inline void
894 handleHostnamePatch(const std::string& hostname,
895 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896{
Ed Tanousbf648f72021-06-03 15:00:14 -0700897 // SHOULD handle host names of up to 255 characters(RFC 1123)
898 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700900 messages::propertyValueFormatError(asyncResp->res, hostname,
901 "HostName");
902 return;
903 }
904 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800905 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700906 if (ec)
907 {
908 messages::internalError(asyncResp->res);
909 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700910 },
911 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
912 "org.freedesktop.DBus.Properties", "Set",
913 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800914 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700915}
916
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700917inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530918 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
919 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
920{
Patrick Williams89492a12023-05-10 07:51:34 -0500921 sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
922 ifaceId;
Tejas Patil35fb5312021-09-20 15:35:20 +0530923 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800924 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700925 if (ec)
926 {
927 messages::internalError(asyncResp->res);
928 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530929 },
930 "xyz.openbmc_project.Network", objPath,
931 "org.freedesktop.DBus.Properties", "Set",
932 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
933 std::variant<size_t>(mtuSize));
934}
935
936inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700937 handleDomainnamePatch(const std::string& ifaceId,
938 const std::string& domainname,
939 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700940{
941 std::vector<std::string> vectorDomainname = {domainname};
942 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800943 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700944 if (ec)
945 {
946 messages::internalError(asyncResp->res);
947 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700948 },
949 "xyz.openbmc_project.Network",
950 "/xyz/openbmc_project/network/" + ifaceId,
951 "org.freedesktop.DBus.Properties", "Set",
952 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800953 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700954}
955
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700956inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700957{
958 // A valid host name can never have the dotted-decimal form (RFC 1123)
959 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
960 {
961 return false;
962 }
963 // Each label(hostname/subdomains) within a valid FQDN
964 // MUST handle host names of up to 63 characters (RFC 1123)
965 // labels cannot start or end with hyphens (RFC 952)
966 // labels can start with numbers (RFC 1123)
967 const std::regex pattern(
968 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
969
970 return std::regex_match(hostname, pattern);
971}
972
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700973inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700974{
975 // Can have multiple subdomains
976 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +0800977 const std::regex pattern(
978 "^([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 -0700979
980 return std::regex_match(domainname, pattern);
981}
982
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700983inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
984 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700985{
986 // Total length of FQDN must not exceed 255 characters(RFC 1035)
987 if (fqdn.length() > 255)
988 {
989 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
990 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 }
992
Ed Tanousbf648f72021-06-03 15:00:14 -0700993 size_t pos = fqdn.find('.');
994 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700996 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
997 return;
998 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800999
Ed Tanousbf648f72021-06-03 15:00:14 -07001000 std::string hostname;
1001 std::string domainname;
1002 domainname = (fqdn).substr(pos + 1);
1003 hostname = (fqdn).substr(0, pos);
1004
1005 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1006 {
1007 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1008 return;
1009 }
1010
1011 handleHostnamePatch(hostname, asyncResp);
1012 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1013}
1014
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001015inline void
1016 handleMACAddressPatch(const std::string& ifaceId,
1017 const std::string& macAddress,
1018 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001019{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001020 static constexpr std::string_view dbusNotAllowedError =
1021 "xyz.openbmc_project.Common.Error.NotAllowed";
1022
Ed Tanousbf648f72021-06-03 15:00:14 -07001023 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001024 [asyncResp, macAddress](const boost::system::error_code& ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001025 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001026 if (ec)
1027 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001028 const sd_bus_error* err = msg.get_error();
1029 if (err == nullptr)
1030 {
1031 messages::internalError(asyncResp->res);
1032 return;
1033 }
1034 if (err->name == dbusNotAllowedError)
1035 {
1036 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1037 return;
1038 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001039 messages::internalError(asyncResp->res);
1040 return;
1041 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001042 },
1043 "xyz.openbmc_project.Network",
1044 "/xyz/openbmc_project/network/" + ifaceId,
1045 "org.freedesktop.DBus.Properties", "Set",
1046 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001047 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001048}
1049
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001050inline void setDHCPEnabled(const std::string& ifaceId,
1051 const std::string& propertyName, const bool v4Value,
1052 const bool v6Value,
1053 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001054{
1055 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1056 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001057 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001058 if (ec)
1059 {
1060 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1061 messages::internalError(asyncResp->res);
1062 return;
1063 }
1064 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001065 },
1066 "xyz.openbmc_project.Network",
1067 "/xyz/openbmc_project/network/" + ifaceId,
1068 "org.freedesktop.DBus.Properties", "Set",
1069 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001070 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001071}
1072
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001073inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001074 const std::string& ifaceId, const std::string& propertyName,
1075 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1076{
1077 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001078 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 if (ec)
1080 {
1081 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1082 messages::internalError(asyncResp->res);
1083 return;
1084 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001085 },
1086 "xyz.openbmc_project.Network",
1087 "/xyz/openbmc_project/network/" + ifaceId,
1088 "org.freedesktop.DBus.Properties", "Set",
1089 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001090 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001091}
1092
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001093inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1094 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001095{
1096 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1097 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001098 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001099 if (ec)
1100 {
1101 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1102 messages::internalError(asyncResp->res);
1103 return;
1104 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001105 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001106 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001107 "org.freedesktop.DBus.Properties", "Set",
1108 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001109 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001110}
1111
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001112inline void handleDHCPPatch(const std::string& ifaceId,
1113 const EthernetInterfaceData& ethData,
1114 const DHCPParameters& v4dhcpParms,
1115 const DHCPParameters& v6dhcpParms,
1116 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001117{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001118 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1119 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001120
1121 bool nextv4DHCPState =
1122 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1123
1124 bool nextv6DHCPState{};
1125 if (v6dhcpParms.dhcpv6OperatingMode)
1126 {
1127 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1128 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1129 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1130 {
1131 messages::propertyValueFormatError(asyncResp->res,
1132 *v6dhcpParms.dhcpv6OperatingMode,
1133 "OperatingMode");
1134 return;
1135 }
1136 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1137 }
1138 else
1139 {
1140 nextv6DHCPState = ipv6Active;
1141 }
1142
1143 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001144 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001145 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001146 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001147 {
1148 messages::generalError(asyncResp->res);
1149 return;
1150 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001151 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001152 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001153 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001154 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001155 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001156 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001157 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001158 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001159 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001160 }
1161 else
1162 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001163 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001164 }
1165
1166 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001167 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001168 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001169 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001170 {
1171 messages::generalError(asyncResp->res);
1172 return;
1173 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001174 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001175 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001176 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001177 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001178 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001179 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001180 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001181 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001182 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001183 }
1184 else
1185 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001186 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001187 }
1188
1189 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001190 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001191 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001192 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001193 {
1194 messages::generalError(asyncResp->res);
1195 return;
1196 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001197 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001198 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001199 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001200 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001201 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001202 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001203 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001204 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001205 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001206 }
1207 else
1208 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001209 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001210 }
1211
1212 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1213 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1214 asyncResp);
1215 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1216 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1217 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1218 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1219 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1220 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1221}
1222
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001223inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001224 getNextStaticIpEntry(
1225 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1226 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1227{
1228 return std::find_if(head, end, [](const IPv4AddressData& value) {
1229 return value.origin == "Static";
1230 });
1231}
1232
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001233inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001234 getNextStaticIpEntry(
1235 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1236 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1237{
1238 return std::find_if(head, end, [](const IPv6AddressData& value) {
1239 return value.origin == "Static";
1240 });
1241}
1242
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001243inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001244 const std::string& ifaceId, nlohmann::json& input,
1245 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1246 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1247{
1248 if ((!input.is_array()) || input.empty())
1249 {
1250 messages::propertyValueTypeError(
1251 asyncResp->res,
1252 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1253 "IPv4StaticAddresses");
1254 return;
1255 }
1256
1257 unsigned entryIdx = 1;
1258 // Find the first static IP address currently active on the NIC and
1259 // match it to the first JSON element in the IPv4StaticAddresses array.
1260 // Match each subsequent JSON element to the next static IP programmed
1261 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001262 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001263 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1264
1265 for (nlohmann::json& thisJson : input)
1266 {
Patrick Williams89492a12023-05-10 07:51:34 -05001267 std::string pathString = "IPv4StaticAddresses/" +
1268 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001269
1270 if (!thisJson.is_null() && !thisJson.empty())
1271 {
1272 std::optional<std::string> address;
1273 std::optional<std::string> subnetMask;
1274 std::optional<std::string> gateway;
1275
1276 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1277 address, "SubnetMask", subnetMask,
1278 "Gateway", gateway))
1279 {
1280 messages::propertyValueFormatError(
1281 asyncResp->res,
1282 thisJson.dump(2, ' ', true,
1283 nlohmann::json::error_handler_t::replace),
1284 pathString);
1285 return;
1286 }
1287
1288 // Find the address/subnet/gateway values. Any values that are
1289 // not explicitly provided are assumed to be unmodified from the
1290 // current state of the interface. Merge existing state into the
1291 // current request.
1292 const std::string* addr = nullptr;
1293 const std::string* gw = nullptr;
1294 uint8_t prefixLength = 0;
1295 bool errorInEntry = false;
1296 if (address)
1297 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001298 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001299 {
1300 addr = &(*address);
1301 }
1302 else
1303 {
1304 messages::propertyValueFormatError(asyncResp->res, *address,
1305 pathString + "/Address");
1306 errorInEntry = true;
1307 }
1308 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001309 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001310 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001311 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001312 }
1313 else
1314 {
1315 messages::propertyMissing(asyncResp->res,
1316 pathString + "/Address");
1317 errorInEntry = true;
1318 }
1319
1320 if (subnetMask)
1321 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001322 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1323 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001324 {
1325 messages::propertyValueFormatError(
1326 asyncResp->res, *subnetMask,
1327 pathString + "/SubnetMask");
1328 errorInEntry = true;
1329 }
1330 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001331 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001332 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001333 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1334 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001335 {
1336 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001337 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001338 pathString + "/SubnetMask");
1339 errorInEntry = true;
1340 }
1341 }
1342 else
1343 {
1344 messages::propertyMissing(asyncResp->res,
1345 pathString + "/SubnetMask");
1346 errorInEntry = true;
1347 }
1348
1349 if (gateway)
1350 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001351 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001352 {
1353 gw = &(*gateway);
1354 }
1355 else
1356 {
1357 messages::propertyValueFormatError(asyncResp->res, *gateway,
1358 pathString + "/Gateway");
1359 errorInEntry = true;
1360 }
1361 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001362 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001363 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001364 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001365 }
1366 else
1367 {
1368 messages::propertyMissing(asyncResp->res,
1369 pathString + "/Gateway");
1370 errorInEntry = true;
1371 }
1372
1373 if (errorInEntry)
1374 {
1375 return;
1376 }
1377
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001378 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001379 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001380 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
1381 nicIpEntry->id, prefixLength, *gw,
1382 *addr, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001383 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1384 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001385 }
1386 else
1387 {
1388 createIPv4(ifaceId, prefixLength, *gateway, *address,
1389 asyncResp);
1390 }
1391 entryIdx++;
1392 }
1393 else
1394 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001395 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001396 {
1397 // Requesting a DELETE/DO NOT MODIFY action for an item
1398 // that isn't present on the eth(n) interface. Input JSON is
1399 // in error, so bail out.
1400 if (thisJson.is_null())
1401 {
1402 messages::resourceCannotBeDeleted(asyncResp->res);
1403 return;
1404 }
1405 messages::propertyValueFormatError(
1406 asyncResp->res,
1407 thisJson.dump(2, ' ', true,
1408 nlohmann::json::error_handler_t::replace),
1409 pathString);
1410 return;
1411 }
1412
1413 if (thisJson.is_null())
1414 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001415 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001416 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001417 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001418 {
Patrick Williams89492a12023-05-10 07:51:34 -05001419 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1420 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001421 }
1422 entryIdx++;
1423 }
1424 }
1425}
1426
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001427inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001428 const std::string& ifaceId,
1429 const std::vector<std::string>& updatedStaticNameServers,
1430 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1431{
1432 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001433 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001434 if (ec)
1435 {
1436 messages::internalError(asyncResp->res);
1437 return;
1438 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001439 },
1440 "xyz.openbmc_project.Network",
1441 "/xyz/openbmc_project/network/" + ifaceId,
1442 "org.freedesktop.DBus.Properties", "Set",
1443 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001444 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001445}
1446
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001447inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001448 const std::string& ifaceId, const nlohmann::json& input,
1449 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1450 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1451{
1452 if (!input.is_array() || input.empty())
1453 {
1454 messages::propertyValueTypeError(
1455 asyncResp->res,
1456 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1457 "IPv6StaticAddresses");
1458 return;
1459 }
1460 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001461 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001462 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1463 for (const nlohmann::json& thisJson : input)
1464 {
Patrick Williams89492a12023-05-10 07:51:34 -05001465 std::string pathString = "IPv6StaticAddresses/" +
1466 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001467
1468 if (!thisJson.is_null() && !thisJson.empty())
1469 {
1470 std::optional<std::string> address;
1471 std::optional<uint8_t> prefixLength;
1472 nlohmann::json thisJsonCopy = thisJson;
1473 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1474 address, "PrefixLength", prefixLength))
1475 {
1476 messages::propertyValueFormatError(
1477 asyncResp->res,
1478 thisJson.dump(2, ' ', true,
1479 nlohmann::json::error_handler_t::replace),
1480 pathString);
1481 return;
1482 }
1483
Ed Tanous543f4402022-01-06 13:12:53 -08001484 const std::string* addr = nullptr;
1485 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001486
1487 // Find the address and prefixLength values. Any values that are
1488 // not explicitly provided are assumed to be unmodified from the
1489 // current state of the interface. Merge existing state into the
1490 // current request.
1491 if (address)
1492 {
1493 addr = &(*address);
1494 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001495 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001496 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001497 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001498 }
1499 else
1500 {
1501 messages::propertyMissing(asyncResp->res,
1502 pathString + "/Address");
1503 return;
1504 }
1505
1506 if (prefixLength)
1507 {
1508 prefix = *prefixLength;
1509 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001510 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001511 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001512 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001513 }
1514 else
1515 {
1516 messages::propertyMissing(asyncResp->res,
1517 pathString + "/PrefixLength");
1518 return;
1519 }
1520
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001521 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001522 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001523 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
1524 nicIpEntry->id, prefix, "", *addr,
1525 asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001526 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1527 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001528 }
1529 else
1530 {
1531 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1532 }
1533 entryIdx++;
1534 }
1535 else
1536 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001537 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001538 {
1539 // Requesting a DELETE/DO NOT MODIFY action for an item
1540 // that isn't present on the eth(n) interface. Input JSON is
1541 // in error, so bail out.
1542 if (thisJson.is_null())
1543 {
1544 messages::resourceCannotBeDeleted(asyncResp->res);
1545 return;
1546 }
1547 messages::propertyValueFormatError(
1548 asyncResp->res,
1549 thisJson.dump(2, ' ', true,
1550 nlohmann::json::error_handler_t::replace),
1551 pathString);
1552 return;
1553 }
1554
1555 if (thisJson.is_null())
1556 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001557 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001558 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001559 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001560 {
Patrick Williams89492a12023-05-10 07:51:34 -05001561 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1562 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001563 }
1564 entryIdx++;
1565 }
1566 }
1567}
1568
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001569inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001570 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1571 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1572 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1573 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1574{
George Liu7a1dbc42022-12-07 16:03:22 +08001575 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1577
1578 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1579 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001580 jsonResponse["@odata.id"] = boost::urls::format(
1581 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001582 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1583
1584 auto health = std::make_shared<HealthPopulate>(asyncResp);
1585
George Liu7a1dbc42022-12-07 16:03:22 +08001586 dbus::utility::getSubTreePaths(
1587 "/", 0, inventoryForEthernet,
1588 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001589 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001590 if (ec)
1591 {
1592 return;
1593 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001594
Ed Tanous002d39b2022-05-31 08:59:27 -07001595 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001596 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001597
1598 health->populate();
1599
1600 if (ethData.nicEnabled)
1601 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001602 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001603 jsonResponse["Status"]["State"] = "Enabled";
1604 }
1605 else
1606 {
1607 jsonResponse["LinkStatus"] = "NoLink";
1608 jsonResponse["Status"]["State"] = "Disabled";
1609 }
1610
Ed Tanousbf648f72021-06-03 15:00:14 -07001611 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301612 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001613 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001614 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001615 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1616 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1617 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1618 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001619
1620 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001621 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001622 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001623 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1624 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1625 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001626
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001627 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001628 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001629 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001630
1631 // When domain name is empty then it means, that it is a network
1632 // without domain names, and the host name itself must be treated as
1633 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001634 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 if (!ethData.domainnames.empty())
1636 {
1637 fqdn += "." + ethData.domainnames[0];
1638 }
1639 jsonResponse["FQDN"] = fqdn;
1640 }
1641
Ed Tanousef4c65b2023-04-24 15:28:50 -07001642 jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1643 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001644
1645 jsonResponse["NameServers"] = ethData.nameServers;
1646 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1647
1648 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1649 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1650 ipv4Array = nlohmann::json::array();
1651 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001652 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001653 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001654 std::string gatewayStr = ipv4Config.gateway;
1655 if (gatewayStr.empty())
1656 {
1657 gatewayStr = "0.0.0.0";
1658 }
Ed Tanous14766872022-03-15 10:44:42 -07001659 nlohmann::json::object_t ipv4;
1660 ipv4["AddressOrigin"] = ipv4Config.origin;
1661 ipv4["SubnetMask"] = ipv4Config.netmask;
1662 ipv4["Address"] = ipv4Config.address;
1663 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001664
Ed Tanousbf648f72021-06-03 15:00:14 -07001665 if (ipv4Config.origin == "Static")
1666 {
Ed Tanous14766872022-03-15 10:44:42 -07001667 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001668 }
Ed Tanous14766872022-03-15 10:44:42 -07001669
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001670 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001671 }
1672
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001673 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001674 if (ipv6GatewayStr.empty())
1675 {
1676 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1677 }
1678
1679 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1680
1681 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1682 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1683 ipv6Array = nlohmann::json::array();
1684 ipv6StaticArray = nlohmann::json::array();
1685 nlohmann::json& ipv6AddrPolicyTable =
1686 jsonResponse["IPv6AddressPolicyTable"];
1687 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001688 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001689 {
Ed Tanous14766872022-03-15 10:44:42 -07001690 nlohmann::json::object_t ipv6;
1691 ipv6["Address"] = ipv6Config.address;
1692 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1693 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001694
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001695 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001696 if (ipv6Config.origin == "Static")
1697 {
Ed Tanous14766872022-03-15 10:44:42 -07001698 nlohmann::json::object_t ipv6Static;
1699 ipv6Static["Address"] = ipv6Config.address;
1700 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001701 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001702 }
1703 }
1704}
1705
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001706inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001707{
Ed Tanous11ba3972022-07-11 09:50:41 -07001708 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001709}
1710
1711inline void requestEthernetInterfacesRoutes(App& app)
1712{
1713 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001714 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001715 .methods(boost::beast::http::verb::get)(
1716 [&app](const crow::Request& req,
1717 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001718 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 {
1720 return;
1721 }
1722
1723 asyncResp->res.jsonValue["@odata.type"] =
1724 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1725 asyncResp->res.jsonValue["@odata.id"] =
1726 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1727 asyncResp->res.jsonValue["Name"] =
1728 "Ethernet Network Interface Collection";
1729 asyncResp->res.jsonValue["Description"] =
1730 "Collection of EthernetInterfaces for this Manager";
1731
1732 // Get eth interface list, and call the below callback for JSON
1733 // preparation
1734 getEthernetIfaceList(
1735 [asyncResp](
1736 const bool& success,
1737 const boost::container::flat_set<std::string>& ifaceList) {
1738 if (!success)
1739 {
1740 messages::internalError(asyncResp->res);
1741 return;
1742 }
1743
1744 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1745 ifaceArray = nlohmann::json::array();
1746 std::string tag = "_";
1747 for (const std::string& ifaceItem : ifaceList)
1748 {
1749 std::size_t found = ifaceItem.find(tag);
1750 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001751 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001752 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001753 iface["@odata.id"] = boost::urls::format(
1754 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1755 ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001756 ifaceArray.emplace_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001757 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001758 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001759
Ed Tanous002d39b2022-05-31 08:59:27 -07001760 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1761 asyncResp->res.jsonValue["@odata.id"] =
1762 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1763 });
1764 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001765
Ed Tanousbf648f72021-06-03 15:00:14 -07001766 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001767 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001768 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001769 [&app](const crow::Request& req,
1770 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1771 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001772 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001773 {
1774 return;
1775 }
1776 getEthernetIfaceData(
1777 ifaceId,
1778 [asyncResp, ifaceId](
1779 const bool& success, const EthernetInterfaceData& ethData,
1780 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1781 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1782 if (!success)
1783 {
1784 // TODO(Pawel)consider distinguish between non
1785 // existing object, and other errors
1786 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1787 ifaceId);
1788 return;
1789 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001790
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001791 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1792 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001794 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001795 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1796 asyncResp->res.jsonValue["Description"] =
1797 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301798
Ed Tanous002d39b2022-05-31 08:59:27 -07001799 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001800 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001801 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001802
Ed Tanousbf648f72021-06-03 15:00:14 -07001803 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001804 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001805 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001806 [&app](const crow::Request& req,
1807 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1808 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001809 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001810 {
1811 return;
1812 }
1813 std::optional<std::string> hostname;
1814 std::optional<std::string> fqdn;
1815 std::optional<std::string> macAddress;
1816 std::optional<std::string> ipv6DefaultGateway;
1817 std::optional<nlohmann::json> ipv4StaticAddresses;
1818 std::optional<nlohmann::json> ipv6StaticAddresses;
1819 std::optional<std::vector<std::string>> staticNameServers;
1820 std::optional<nlohmann::json> dhcpv4;
1821 std::optional<nlohmann::json> dhcpv6;
1822 std::optional<bool> interfaceEnabled;
1823 std::optional<size_t> mtuSize;
1824 DHCPParameters v4dhcpParms;
1825 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001826
Ed Tanous002d39b2022-05-31 08:59:27 -07001827 if (!json_util::readJsonPatch(
1828 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1829 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1830 macAddress, "StaticNameServers", staticNameServers,
1831 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1832 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1833 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1834 {
1835 return;
1836 }
1837 if (dhcpv4)
1838 {
1839 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1840 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1841 v4dhcpParms.useDnsServers, "UseNTPServers",
1842 v4dhcpParms.useNtpServers, "UseDomainName",
1843 v4dhcpParms.useDomainName))
1844 {
1845 return;
1846 }
1847 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001848
Ed Tanous002d39b2022-05-31 08:59:27 -07001849 if (dhcpv6)
1850 {
1851 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1852 v6dhcpParms.dhcpv6OperatingMode,
1853 "UseDNSServers", v6dhcpParms.useDnsServers,
1854 "UseNTPServers", v6dhcpParms.useNtpServers,
1855 "UseDomainName",
1856 v6dhcpParms.useDomainName))
1857 {
1858 return;
1859 }
1860 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001861
Ed Tanous002d39b2022-05-31 08:59:27 -07001862 // Get single eth interface data, and call the below callback
1863 // for JSON preparation
1864 getEthernetIfaceData(
1865 ifaceId,
1866 [asyncResp, ifaceId, hostname = std::move(hostname),
1867 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1868 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1869 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1870 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1871 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001872 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1873 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001874 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1875 const bool& success, const EthernetInterfaceData& ethData,
1876 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1877 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1878 if (!success)
1879 {
1880 // ... otherwise return error
1881 // TODO(Pawel)consider distinguish between non
1882 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001883 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001884 ifaceId);
1885 return;
1886 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001887
Ed Tanous002d39b2022-05-31 08:59:27 -07001888 if (dhcpv4 || dhcpv6)
1889 {
1890 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001891 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001892 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301893
Ed Tanous002d39b2022-05-31 08:59:27 -07001894 if (hostname)
1895 {
1896 handleHostnamePatch(*hostname, asyncResp);
1897 }
1898
1899 if (fqdn)
1900 {
1901 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1902 }
1903
1904 if (macAddress)
1905 {
1906 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1907 }
1908
1909 if (ipv4StaticAddresses)
1910 {
1911 // TODO(ed) for some reason the capture of
1912 // ipv4Addresses above is returning a const value,
1913 // not a non-const value. This doesn't really work
1914 // for us, as we need to be able to efficiently move
1915 // out the intermedia nlohmann::json objects. This
1916 // makes a copy of the structure, and operates on
1917 // that, but could be done more efficiently
1918 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1919 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1920 }
1921
1922 if (staticNameServers)
1923 {
1924 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1925 asyncResp);
1926 }
1927
1928 if (ipv6DefaultGateway)
1929 {
1930 messages::propertyNotWritable(asyncResp->res,
1931 "IPv6DefaultGateway");
1932 }
1933
1934 if (ipv6StaticAddresses)
1935 {
1936 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1937 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1938 asyncResp);
1939 }
1940
1941 if (interfaceEnabled)
1942 {
1943 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1944 *interfaceEnabled, asyncResp);
1945 }
1946
1947 if (mtuSize)
1948 {
1949 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
1950 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001951 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 });
manojkiraneda2a133282019-02-19 13:09:43 +05301953
Ed Tanousbf648f72021-06-03 15:00:14 -07001954 BMCWEB_ROUTE(
1955 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001956 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001957 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001958 [&app](const crow::Request& req,
1959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1960 const std::string& parentIfaceId,
1961 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001962 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001963 {
1964 return;
1965 }
1966 asyncResp->res.jsonValue["@odata.type"] =
1967 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1968 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001969
Ed Tanous002d39b2022-05-31 08:59:27 -07001970 if (!verifyNames(parentIfaceId, ifaceId))
1971 {
1972 return;
1973 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001974
Ed Tanous002d39b2022-05-31 08:59:27 -07001975 // Get single eth interface data, and call the below callback
1976 // for JSON preparation
1977 getEthernetIfaceData(
1978 ifaceId,
1979 [asyncResp, parentIfaceId,
1980 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
1981 const boost::container::flat_set<IPv4AddressData>&,
1982 const boost::container::flat_set<IPv6AddressData>&) {
1983 if (success && ethData.vlanId)
1984 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001985 asyncResp->res.jsonValue["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001986 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1987 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1988 parentIfaceId, ifaceId);
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001989
Jiaqing Zhao23a06312022-09-13 09:38:25 +08001990 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001991 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07001992 }
1993 else
1994 {
1995 // ... otherwise return error
1996 // TODO(Pawel)consider distinguish between non
1997 // existing object, and other errors
1998 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001999 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002000 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002001 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002002 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002003
Ed Tanousbf648f72021-06-03 15:00:14 -07002004 BMCWEB_ROUTE(
2005 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002006 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002007 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002008 [&app](const crow::Request& req,
2009 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2010 const std::string& parentIfaceId,
2011 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002012 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 {
2014 return;
2015 }
2016 if (!verifyNames(parentIfaceId, ifaceId))
2017 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002018 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 ifaceId);
2020 return;
2021 }
2022
2023 std::optional<bool> vlanEnable;
2024 std::optional<uint32_t> vlanId;
2025
2026 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2027 vlanEnable, "VLANId", vlanId))
2028 {
2029 return;
2030 }
2031
2032 if (vlanId)
2033 {
2034 messages::propertyNotWritable(asyncResp->res, "VLANId");
2035 return;
2036 }
2037
2038 // Get single eth interface data, and call the below callback
2039 // for JSON preparation
2040 getEthernetIfaceData(
2041 ifaceId,
2042 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2043 const bool& success, const EthernetInterfaceData& ethData,
2044 const boost::container::flat_set<IPv4AddressData>&,
2045 const boost::container::flat_set<IPv6AddressData>&) {
2046 if (success && ethData.vlanId)
2047 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002048 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002049 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002050 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002051 [asyncResp](const boost::system::error_code& ec) {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002052 if (ec)
2053 {
2054 messages::internalError(asyncResp->res);
2055 return;
2056 }
2057 },
2058 "xyz.openbmc_project.Network",
2059 "/xyz/openbmc_project/network/" + ifaceId,
2060 "org.freedesktop.DBus.Properties", "Set",
2061 "xyz.openbmc_project.Network.EthernetInterface",
2062 "NICEnabled",
2063 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002064 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 }
2066 else
2067 {
2068 // TODO(Pawel)consider distinguish between non
2069 // existing object, and other errors
2070 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002071 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002072 return;
2073 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002074 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002076
2077 BMCWEB_ROUTE(
2078 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002079 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002080 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002081 [&app](const crow::Request& req,
2082 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2083 const std::string& parentIfaceId,
2084 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002085 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 {
2087 return;
2088 }
2089 if (!verifyNames(parentIfaceId, ifaceId))
2090 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002091 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 ifaceId);
2093 return;
2094 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002095
Ed Tanous002d39b2022-05-31 08:59:27 -07002096 // Get single eth interface data, and call the below callback
2097 // for JSON preparation
2098 getEthernetIfaceData(
2099 ifaceId,
2100 [asyncResp, parentIfaceId,
2101 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2102 const boost::container::flat_set<IPv4AddressData>&,
2103 const boost::container::flat_set<IPv6AddressData>&) {
2104 if (success && ethData.vlanId)
2105 {
2106 auto callback =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002107 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 if (ec)
2109 {
2110 messages::internalError(asyncResp->res);
2111 }
2112 };
2113 crow::connections::systemBus->async_method_call(
2114 std::move(callback), "xyz.openbmc_project.Network",
2115 std::string("/xyz/openbmc_project/network/") + ifaceId,
2116 "xyz.openbmc_project.Object.Delete", "Delete");
2117 }
2118 else
2119 {
2120 // ... otherwise return error
2121 // TODO(Pawel)consider distinguish between non
2122 // existing object, and other errors
2123 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002124 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002125 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002126 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002128
Ed Tanousbf648f72021-06-03 15:00:14 -07002129 BMCWEB_ROUTE(app,
2130 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002131
2132 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002133 .methods(boost::beast::http::verb::get)(
2134 [&app](const crow::Request& req,
2135 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2136 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002137 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002138 {
2139 return;
2140 }
2141 // Get eth interface list, and call the below callback for JSON
2142 // preparation
2143 getEthernetIfaceList(
2144 [asyncResp, rootInterfaceName](
2145 const bool& success,
2146 const boost::container::flat_set<std::string>& ifaceList) {
2147 if (!success)
2148 {
2149 messages::internalError(asyncResp->res);
2150 return;
2151 }
2152
2153 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2154 {
2155 messages::resourceNotFound(asyncResp->res,
2156 "VLanNetworkInterfaceCollection",
2157 rootInterfaceName);
2158 return;
2159 }
2160
2161 asyncResp->res.jsonValue["@odata.type"] =
2162 "#VLanNetworkInterfaceCollection."
2163 "VLanNetworkInterfaceCollection";
2164 asyncResp->res.jsonValue["Name"] =
2165 "VLAN Network Interface Collection";
2166
2167 nlohmann::json ifaceArray = nlohmann::json::array();
2168
2169 for (const std::string& ifaceItem : ifaceList)
2170 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002171 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002173 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002174 iface["@odata.id"] = boost::urls::format(
2175 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2176 rootInterfaceName, ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002177 ifaceArray.emplace_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002178 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002179 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002180
Ed Tanous002d39b2022-05-31 08:59:27 -07002181 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2182 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Ed Tanousef4c65b2023-04-24 15:28:50 -07002183 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2184 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2185 rootInterfaceName);
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 });
2187 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002188
Ed Tanousbf648f72021-06-03 15:00:14 -07002189 BMCWEB_ROUTE(app,
2190 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002191 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002192 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002193 [&app](const crow::Request& req,
2194 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2195 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002196 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002197 {
2198 return;
2199 }
2200 bool vlanEnable = false;
2201 uint32_t vlanId = 0;
2202 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2203 "VLANEnable", vlanEnable))
2204 {
2205 return;
2206 }
2207 // Need both vlanId and vlanEnable to service this request
2208 if (vlanId == 0U)
2209 {
2210 messages::propertyMissing(asyncResp->res, "VLANId");
2211 }
2212 if (!vlanEnable)
2213 {
2214 messages::propertyMissing(asyncResp->res, "VLANEnable");
2215 }
2216 if (static_cast<bool>(vlanId) ^ vlanEnable)
2217 {
2218 return;
2219 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002220
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002221 auto callback = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002222 if (ec)
2223 {
2224 // TODO(ed) make more consistent error messages
2225 // based on phosphor-network responses
2226 messages::internalError(asyncResp->res);
2227 return;
2228 }
2229 messages::created(asyncResp->res);
2230 };
2231 crow::connections::systemBus->async_method_call(
2232 std::move(callback), "xyz.openbmc_project.Network",
2233 "/xyz/openbmc_project/network",
2234 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2235 rootInterfaceName, vlanId);
2236 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002237}
2238
Ed Tanous1abe55e2018-09-05 08:30:59 -07002239} // namespace redfish