blob: cccae8bff308eb38b470c68495fdacb9b4da58a3 [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
George Liu7a1dbc42022-12-07 16:03:22 +080018#include "dbus_utility.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070019#include "utils/ip_utils.hpp"
20
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070022#include <boost/algorithm/string/classification.hpp>
23#include <boost/algorithm/string/split.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070024#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020025#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020026#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070027#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070028#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029#include <utils/json_utils.hpp>
30
George Liu7a1dbc42022-12-07 16:03:22 +080031#include <array>
Ed Tanousa24526d2018-12-10 15:17:59 -080032#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053033#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080034#include <string_view>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010035
Ed Tanous1abe55e2018-09-05 08:30:59 -070036namespace redfish
37{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010038
Ed Tanous4a0cb852018-10-15 07:55:04 -070039enum class LinkType
40{
41 Local,
42 Global
43};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010044
45/**
46 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010047 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070048struct IPv4AddressData
49{
50 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070051 std::string address;
52 std::string domain;
53 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 std::string netmask;
55 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070056 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050057 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070058
Gunnar Mills1214b7e2020-06-04 10:11:30 -050059 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070061 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010063};
64
65/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050066 * Structure for keeping IPv6 data required by Redfish
67 */
68struct IPv6AddressData
69{
70 std::string id;
71 std::string address;
72 std::string origin;
73 uint8_t prefixLength;
74
Gunnar Mills1214b7e2020-06-04 10:11:30 -050075 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050076 {
77 return id < obj.id;
78 }
79};
80/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010081 * Structure for keeping basic single Ethernet Interface information
82 * available from DBus
83 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070084struct EthernetInterfaceData
85{
Ed Tanous4a0cb852018-10-15 07:55:04 -070086 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053087 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080088 bool autoNeg;
89 bool dnsEnabled;
90 bool ntpEnabled;
91 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080092 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070093 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080094 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070095 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080096 std::string hostName;
97 std::string defaultGateway;
98 std::string ipv6DefaultGateway;
99 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800100 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500101 std::vector<std::string> nameServers;
102 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800103 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100104};
105
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700106struct DHCPParameters
107{
108 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800109 std::optional<bool> useDnsServers;
110 std::optional<bool> useNtpServers;
111 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700112 std::optional<std::string> dhcpv6OperatingMode;
113};
114
Ed Tanous4a0cb852018-10-15 07:55:04 -0700115// Helper function that changes bits netmask notation (i.e. /24)
116// into full dot notation
117inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700119 uint32_t value = 0xffffffff << (32 - bits);
120 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
121 std::to_string((value >> 16) & 0xff) + "." +
122 std::to_string((value >> 8) & 0xff) + "." +
123 std::to_string(value & 0xff);
124 return netmask;
125}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100126
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800127inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700128 bool isIPv4)
129{
130 if (isIPv4)
131 {
132 return (
133 (inputDHCP ==
134 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
135 (inputDHCP ==
136 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
137 }
138 return ((inputDHCP ==
139 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
140 (inputDHCP ==
141 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
142}
143
Ed Tanous2c70f802020-09-28 14:29:23 -0700144inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700145{
146 if (isIPv4 && isIPv6)
147 {
148 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
149 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700150 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700151 {
152 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
153 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700154 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700155 {
156 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
157 }
158 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
159}
160
Ed Tanous4a0cb852018-10-15 07:55:04 -0700161inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500162 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700163 bool isIPv4)
164{
165 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167 return "Static";
168 }
169 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
170 {
171 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 return "IPv4LinkLocal";
174 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700175 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700176 }
177 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
178 {
179 if (isIPv4)
180 {
181 return "DHCP";
182 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700183 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700184 }
185 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
186 {
187 return "SLAAC";
188 }
189 return "";
190}
191
Ed Tanous02cad962022-06-30 16:50:15 -0700192inline bool extractEthernetInterfaceData(
193 const std::string& ethifaceId,
194 const dbus::utility::ManagedObjectType& dbusData,
195 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700197 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700198 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199 {
Ed Tanous02cad962022-06-30 16:50:15 -0700200 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000202 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700204 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700205 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500207 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500211 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800212 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700213 if (mac != nullptr)
214 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800215 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 }
219 }
220 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
221 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 {
224 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500226 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800227 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 if (id != nullptr)
229 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800230 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 }
234 }
235 else if (ifacePair.first ==
236 "xyz.openbmc_project.Network.EthernetInterface")
237 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500238 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 {
240 if (propertyPair.first == "AutoNeg")
241 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700242 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800243 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700244 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700245 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800246 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 }
248 }
249 else if (propertyPair.first == "Speed")
250 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500251 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800252 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700253 if (speed != nullptr)
254 {
255 ethData.speed = *speed;
256 }
257 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530258 else if (propertyPair.first == "MTU")
259 {
260 const uint32_t* mtuSize =
261 std::get_if<uint32_t>(&propertyPair.second);
262 if (mtuSize != nullptr)
263 {
264 ethData.mtuSize = *mtuSize;
265 }
266 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800267 else if (propertyPair.first == "LinkUp")
268 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500269 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800270 std::get_if<bool>(&propertyPair.second);
271 if (linkUp != nullptr)
272 {
273 ethData.linkUp = *linkUp;
274 }
275 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700276 else if (propertyPair.first == "NICEnabled")
277 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500278 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700279 std::get_if<bool>(&propertyPair.second);
280 if (nicEnabled != nullptr)
281 {
282 ethData.nicEnabled = *nicEnabled;
283 }
284 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500285 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700286 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500287 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500288 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800289 &propertyPair.second);
290 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700291 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700292 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500293 }
294 }
295 else if (propertyPair.first == "StaticNameServers")
296 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500297 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500298 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500299 &propertyPair.second);
300 if (staticNameServers != nullptr)
301 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700302 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700303 }
304 }
manojkiraneda2a133282019-02-19 13:09:43 +0530305 else if (propertyPair.first == "DHCPEnabled")
306 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700307 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700308 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700309 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530310 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800311 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530312 }
313 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800314 else if (propertyPair.first == "DomainName")
315 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500316 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500317 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800318 &propertyPair.second);
319 if (domainNames != nullptr)
320 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700321 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800322 }
323 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500324 else if (propertyPair.first == "DefaultGateway")
325 {
326 const std::string* defaultGateway =
327 std::get_if<std::string>(&propertyPair.second);
328 if (defaultGateway != nullptr)
329 {
330 std::string defaultGatewayStr = *defaultGateway;
331 if (defaultGatewayStr.empty())
332 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800333 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500334 }
335 else
336 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800337 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500338 }
339 }
340 }
341 else if (propertyPair.first == "DefaultGateway6")
342 {
343 const std::string* defaultGateway6 =
344 std::get_if<std::string>(&propertyPair.second);
345 if (defaultGateway6 != nullptr)
346 {
347 std::string defaultGateway6Str =
348 *defaultGateway6;
349 if (defaultGateway6Str.empty())
350 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800351 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500352 "0:0:0:0:0:0:0:0";
353 }
354 else
355 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800356 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500357 defaultGateway6Str;
358 }
359 }
360 }
Ed Tanous029573d2019-02-01 10:57:49 -0800361 }
362 }
363 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700364
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800365 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700366 {
367 if (ifacePair.first ==
368 "xyz.openbmc_project.Network.DHCPConfiguration")
369 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500370 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 {
372 if (propertyPair.first == "DNSEnabled")
373 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700374 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700375 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700376 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700377 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800378 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700379 }
380 }
381 else if (propertyPair.first == "NTPEnabled")
382 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700383 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700384 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700385 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700386 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800387 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700388 }
389 }
390 else if (propertyPair.first == "HostNameEnabled")
391 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700392 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700393 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700394 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700395 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800396 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700397 }
398 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700399 }
400 }
401 }
Ed Tanous029573d2019-02-01 10:57:49 -0800402 // System configuration shows up in the global namespace, so no need
403 // to check eth number
404 if (ifacePair.first ==
405 "xyz.openbmc_project.Network.SystemConfiguration")
406 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500407 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800408 {
409 if (propertyPair.first == "HostName")
410 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500411 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500412 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800413 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700414 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800415 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800416 }
417 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418 }
419 }
420 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700421 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700422 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700423}
424
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500425// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700426inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800428 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000429 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500430{
431 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000432 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500433
434 // Since there might be several IPv6 configurations aligned with
435 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000436 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500437 {
438 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700439 if (objpath.first.str.starts_with(ipv6PathStart))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500440 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800441 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500442 {
443 if (interface.first == "xyz.openbmc_project.Network.IP")
444 {
445 // Instance IPv6AddressData structure, and set as
446 // appropriate
447 std::pair<
448 boost::container::flat_set<IPv6AddressData>::iterator,
449 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000450 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700451 IPv6AddressData& ipv6Address = *it.first;
452 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700453 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800454 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500455 {
456 if (property.first == "Address")
457 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500458 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500459 std::get_if<std::string>(&property.second);
460 if (address != nullptr)
461 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700462 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500463 }
464 }
465 else if (property.first == "Origin")
466 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500467 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500468 std::get_if<std::string>(&property.second);
469 if (origin != nullptr)
470 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700471 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500472 translateAddressOriginDbusToRedfish(*origin,
473 false);
474 }
475 }
476 else if (property.first == "PrefixLength")
477 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500478 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500479 std::get_if<uint8_t>(&property.second);
480 if (prefix != nullptr)
481 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700482 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500483 }
484 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600485 else if (property.first == "Type" ||
486 property.first == "Gateway")
487 {
488 // Type & Gateway is not used
489 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 else
491 {
492 BMCWEB_LOG_ERROR
493 << "Got extra property: " << property.first
494 << " on the " << objpath.first.str << " object";
495 }
496 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500497 }
498 }
499 }
500 }
501}
502
Ed Tanous4a0cb852018-10-15 07:55:04 -0700503// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700504inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000505 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800506 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000507 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700508{
509 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000510 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700511
512 // Since there might be several IPv4 configurations aligned with
513 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000514 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700515 {
516 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700517 if (objpath.first.str.starts_with(ipv4PathStart))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700518 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800519 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700520 {
521 if (interface.first == "xyz.openbmc_project.Network.IP")
522 {
523 // Instance IPv4AddressData structure, and set as
524 // appropriate
525 std::pair<
526 boost::container::flat_set<IPv4AddressData>::iterator,
527 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000528 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700529 IPv4AddressData& ipv4Address = *it.first;
530 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700531 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800532 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700533 {
534 if (property.first == "Address")
535 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500536 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800537 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700538 if (address != nullptr)
539 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700540 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 }
542 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700543 else if (property.first == "Origin")
544 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500545 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800546 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700547 if (origin != nullptr)
548 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700549 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700550 translateAddressOriginDbusToRedfish(*origin,
551 true);
552 }
553 }
554 else if (property.first == "PrefixLength")
555 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500556 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800557 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700558 if (mask != nullptr)
559 {
560 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700561 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700562 }
563 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600564 else if (property.first == "Type" ||
565 property.first == "Gateway")
566 {
567 // Type & Gateway is not used
568 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700569 else
570 {
571 BMCWEB_LOG_ERROR
572 << "Got extra property: " << property.first
573 << " on the " << objpath.first.str << " object";
574 }
575 }
576 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700577 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700578 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700579 ? LinkType::Local
580 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700581 }
582 }
583 }
584 }
585}
586
587/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700588 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700589 *
590 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700591 * @param[in] ipHash DBus Hash id of IP that should be deleted
592 * @param[io] asyncResp Response object that will be returned to client
593 *
594 * @return None
595 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500596inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800597 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700598{
599 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700600 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700601 if (ec)
602 {
603 messages::internalError(asyncResp->res);
604 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700605 },
606 "xyz.openbmc_project.Network",
607 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
608 "xyz.openbmc_project.Object.Delete", "Delete");
609}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610
Gunnar Mills244b6d52021-04-12 15:44:23 -0500611inline void updateIPv4DefaultGateway(
612 const std::string& ifaceId, const std::string& gateway,
613 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500614{
615 crow::connections::systemBus->async_method_call(
616 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700617 if (ec)
618 {
619 messages::internalError(asyncResp->res);
620 return;
621 }
622 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500623 },
624 "xyz.openbmc_project.Network",
625 "/xyz/openbmc_project/network/" + ifaceId,
626 "org.freedesktop.DBus.Properties", "Set",
627 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800628 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500629}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700631 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700633 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
634 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
635 * @param[in] gateway IPv4 address of this interfaces gateway
636 * @param[in] address IPv4 address to assign to this interface
637 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 *
639 * @return None
640 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000641inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
642 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800643 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700644{
Ed Tanous002d39b2022-05-31 08:59:27 -0700645 auto createIpHandler =
646 [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500647 if (ec)
648 {
649 messages::internalError(asyncResp->res);
650 return;
651 }
652 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
653 };
654
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500656 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700657 "/xyz/openbmc_project/network/" + ifaceId,
658 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700659 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700660 gateway);
661}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500662
663/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700664 * @brief Deletes the IPv4 entry for this interface and creates a replacement
665 * static IPv4 entry
666 *
667 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
668 * @param[in] id The unique hash entry identifying the DBus entry
669 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
670 * @param[in] gateway IPv4 address of this interfaces gateway
671 * @param[in] address IPv4 address to assign to this interface
672 * @param[io] asyncResp Response object that will be returned to client
673 *
674 * @return None
675 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800676inline void
677 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
678 uint8_t prefixLength, const std::string& gateway,
679 const std::string& address,
680 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700681{
682 crow::connections::systemBus->async_method_call(
683 [asyncResp, ifaceId, address, prefixLength,
684 gateway](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700685 if (ec)
686 {
687 messages::internalError(asyncResp->res);
688 return;
689 }
690
691 crow::connections::systemBus->async_method_call(
692 [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
693 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700694 {
695 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500696 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700697 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700698 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
699 },
700 "xyz.openbmc_project.Network",
701 "/xyz/openbmc_project/network/" + ifaceId,
702 "xyz.openbmc_project.Network.IP.Create", "IP",
703 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
704 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700705 },
706 "xyz.openbmc_project.Network",
707 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
708 "xyz.openbmc_project.Object.Delete", "Delete");
709}
710
711/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500712 * @brief Deletes given IPv6
713 *
714 * @param[in] ifaceId Id of interface whose IP should be deleted
715 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500716 * @param[io] asyncResp Response object that will be returned to client
717 *
718 * @return None
719 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500720inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500722{
723 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700724 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700725 if (ec)
726 {
727 messages::internalError(asyncResp->res);
728 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500729 },
730 "xyz.openbmc_project.Network",
731 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
732 "xyz.openbmc_project.Object.Delete", "Delete");
733}
734
735/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700736 * @brief Deletes the IPv6 entry for this interface and creates a replacement
737 * static IPv6 entry
738 *
739 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
740 * @param[in] id The unique hash entry identifying the DBus entry
741 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
742 * @param[in] address IPv6 address to assign to this interface
743 * @param[io] asyncResp Response object that will be returned to client
744 *
745 * @return None
746 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800747inline void
748 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
749 uint8_t prefixLength, const std::string& address,
750 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700751{
752 crow::connections::systemBus->async_method_call(
753 [asyncResp, ifaceId, address,
754 prefixLength](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700755 if (ec)
756 {
757 messages::internalError(asyncResp->res);
758 }
759 crow::connections::systemBus->async_method_call(
760 [asyncResp](const boost::system::error_code ec2) {
761 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700762 {
763 messages::internalError(asyncResp->res);
764 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700765 },
766 "xyz.openbmc_project.Network",
767 "/xyz/openbmc_project/network/" + ifaceId,
768 "xyz.openbmc_project.Network.IP.Create", "IP",
769 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
770 prefixLength, "");
Johnathan Mantey01784822019-06-18 12:44:21 -0700771 },
772 "xyz.openbmc_project.Network",
773 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
774 "xyz.openbmc_project.Object.Delete", "Delete");
775}
776
777/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500778 * @brief Creates IPv6 with given data
779 *
780 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500781 * @param[in] prefixLength Prefix length that needs to be added
782 * @param[in] address IP address that needs to be added
783 * @param[io] asyncResp Response object that will be returned to client
784 *
785 * @return None
786 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500787inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
788 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800789 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500790{
791 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
792 if (ec)
793 {
794 messages::internalError(asyncResp->res);
795 }
796 };
797 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500798 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500799 crow::connections::systemBus->async_method_call(
800 std::move(createIpHandler), "xyz.openbmc_project.Network",
801 "/xyz/openbmc_project/network/" + ifaceId,
802 "xyz.openbmc_project.Network.IP.Create", "IP",
803 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
804 "");
805}
806
Ed Tanous4a0cb852018-10-15 07:55:04 -0700807/**
808 * Function that retrieves all properties for given Ethernet Interface
809 * Object
810 * from EntityManager Network Manager
811 * @param ethiface_id a eth interface id to query on DBus
812 * @param callback a function that shall be called to convert Dbus output
813 * into JSON
814 */
815template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000816void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500817 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700818{
819 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800820 [ethifaceId{std::string{ethifaceId}},
821 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000822 const boost::system::error_code errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700823 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700824 EthernetInterfaceData ethData{};
825 boost::container::flat_set<IPv4AddressData> ipv4Data;
826 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700827
Ed Tanous002d39b2022-05-31 08:59:27 -0700828 if (errorCode)
829 {
830 callback(false, ethData, ipv4Data, ipv6Data);
831 return;
832 }
833
834 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
835 if (!found)
836 {
837 callback(false, ethData, ipv4Data, ipv6Data);
838 return;
839 }
840
841 extractIPData(ethifaceId, resp, ipv4Data);
842 // Fix global GW
843 for (IPv4AddressData& ipv4 : ipv4Data)
844 {
845 if (((ipv4.linktype == LinkType::Global) &&
846 (ipv4.gateway == "0.0.0.0")) ||
847 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700848 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700849 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700850 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700852
Ed Tanous002d39b2022-05-31 08:59:27 -0700853 extractIPV6Data(ethifaceId, resp, ipv6Data);
854 // Finally make a callback with useful data
855 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700856 },
857 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
858 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700859}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700860
861/**
862 * Function that retrieves all Ethernet Interfaces available through Network
863 * Manager
864 * @param callback a function that shall be called to convert Dbus output
865 * into JSON.
866 */
867template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500868void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700869{
870 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800871 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000872 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800873 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700874 // Callback requires vector<string> to retrieve all available
875 // ethernet interfaces
876 boost::container::flat_set<std::string> ifaceList;
877 ifaceList.reserve(resp.size());
878 if (errorCode)
879 {
880 callback(false, ifaceList);
881 return;
882 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700883
Ed Tanous002d39b2022-05-31 08:59:27 -0700884 // Iterate over all retrieved ObjectPaths.
885 for (const auto& objpath : resp)
886 {
887 // And all interfaces available for certain ObjectPath.
888 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700889 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700890 // If interface is
891 // xyz.openbmc_project.Network.EthernetInterface, this is
892 // what we're looking for.
893 if (interface.first ==
894 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700895 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700896 std::string ifaceId = objpath.first.filename();
897 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700899 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700901 // and put it into output vector.
902 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700904 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700905 }
906 // Finally make a callback with useful data
907 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700908 },
909 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
910 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700911}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100912
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700913inline void
914 handleHostnamePatch(const std::string& hostname,
915 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916{
Ed Tanousbf648f72021-06-03 15:00:14 -0700917 // SHOULD handle host names of up to 255 characters(RFC 1123)
918 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700920 messages::propertyValueFormatError(asyncResp->res, hostname,
921 "HostName");
922 return;
923 }
924 crow::connections::systemBus->async_method_call(
925 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700926 if (ec)
927 {
928 messages::internalError(asyncResp->res);
929 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700930 },
931 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
932 "org.freedesktop.DBus.Properties", "Set",
933 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800934 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700935}
936
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700937inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530938 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
939 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
940{
941 sdbusplus::message::object_path objPath =
942 "/xyz/openbmc_project/network/" + ifaceId;
943 crow::connections::systemBus->async_method_call(
944 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700945 if (ec)
946 {
947 messages::internalError(asyncResp->res);
948 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530949 },
950 "xyz.openbmc_project.Network", objPath,
951 "org.freedesktop.DBus.Properties", "Set",
952 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
953 std::variant<size_t>(mtuSize));
954}
955
956inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700957 handleDomainnamePatch(const std::string& ifaceId,
958 const std::string& domainname,
959 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700960{
961 std::vector<std::string> vectorDomainname = {domainname};
962 crow::connections::systemBus->async_method_call(
963 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700964 if (ec)
965 {
966 messages::internalError(asyncResp->res);
967 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700968 },
969 "xyz.openbmc_project.Network",
970 "/xyz/openbmc_project/network/" + ifaceId,
971 "org.freedesktop.DBus.Properties", "Set",
972 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800973 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700974}
975
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700976inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700977{
978 // A valid host name can never have the dotted-decimal form (RFC 1123)
979 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
980 {
981 return false;
982 }
983 // Each label(hostname/subdomains) within a valid FQDN
984 // MUST handle host names of up to 63 characters (RFC 1123)
985 // labels cannot start or end with hyphens (RFC 952)
986 // labels can start with numbers (RFC 1123)
987 const std::regex pattern(
988 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
989
990 return std::regex_match(hostname, pattern);
991}
992
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700993inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700994{
995 // Can have multiple subdomains
996 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +0800997 const std::regex pattern(
998 "^([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 -0700999
1000 return std::regex_match(domainname, pattern);
1001}
1002
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001003inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1004 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001005{
1006 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1007 if (fqdn.length() > 255)
1008 {
1009 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1010 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 }
1012
Ed Tanousbf648f72021-06-03 15:00:14 -07001013 size_t pos = fqdn.find('.');
1014 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001016 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1017 return;
1018 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001019
Ed Tanousbf648f72021-06-03 15:00:14 -07001020 std::string hostname;
1021 std::string domainname;
1022 domainname = (fqdn).substr(pos + 1);
1023 hostname = (fqdn).substr(0, pos);
1024
1025 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1026 {
1027 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1028 return;
1029 }
1030
1031 handleHostnamePatch(hostname, asyncResp);
1032 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1033}
1034
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001035inline void
1036 handleMACAddressPatch(const std::string& ifaceId,
1037 const std::string& macAddress,
1038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001039{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001040 static constexpr std::string_view dbusNotAllowedError =
1041 "xyz.openbmc_project.Common.Error.NotAllowed";
1042
Ed Tanousbf648f72021-06-03 15:00:14 -07001043 crow::connections::systemBus->async_method_call(
Johnathan Mantey58283f42022-08-15 14:38:36 -07001044 [asyncResp, macAddress](const boost::system::error_code ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001045 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001046 if (ec)
1047 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001048 const sd_bus_error* err = msg.get_error();
1049 if (err == nullptr)
1050 {
1051 messages::internalError(asyncResp->res);
1052 return;
1053 }
1054 if (err->name == dbusNotAllowedError)
1055 {
1056 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1057 return;
1058 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 messages::internalError(asyncResp->res);
1060 return;
1061 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001062 },
1063 "xyz.openbmc_project.Network",
1064 "/xyz/openbmc_project/network/" + ifaceId,
1065 "org.freedesktop.DBus.Properties", "Set",
1066 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001067 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001068}
1069
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001070inline void setDHCPEnabled(const std::string& ifaceId,
1071 const std::string& propertyName, const bool v4Value,
1072 const bool v6Value,
1073 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001074{
1075 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1076 crow::connections::systemBus->async_method_call(
1077 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001078 if (ec)
1079 {
1080 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1081 messages::internalError(asyncResp->res);
1082 return;
1083 }
1084 messages::success(asyncResp->res);
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{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001091}
1092
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001093inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001094 const std::string& ifaceId, const std::string& propertyName,
1095 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1096{
1097 crow::connections::systemBus->async_method_call(
1098 [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 },
1106 "xyz.openbmc_project.Network",
1107 "/xyz/openbmc_project/network/" + ifaceId,
1108 "org.freedesktop.DBus.Properties", "Set",
1109 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001110 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001111}
1112
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001113inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001115{
1116 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1117 crow::connections::systemBus->async_method_call(
1118 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 if (ec)
1120 {
1121 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1122 messages::internalError(asyncResp->res);
1123 return;
1124 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001125 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001126 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001127 "org.freedesktop.DBus.Properties", "Set",
1128 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001129 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001130}
1131
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001132inline void handleDHCPPatch(const std::string& ifaceId,
1133 const EthernetInterfaceData& ethData,
1134 const DHCPParameters& v4dhcpParms,
1135 const DHCPParameters& v6dhcpParms,
1136 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001137{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001138 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1139 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001140
1141 bool nextv4DHCPState =
1142 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1143
1144 bool nextv6DHCPState{};
1145 if (v6dhcpParms.dhcpv6OperatingMode)
1146 {
1147 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1148 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1149 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1150 {
1151 messages::propertyValueFormatError(asyncResp->res,
1152 *v6dhcpParms.dhcpv6OperatingMode,
1153 "OperatingMode");
1154 return;
1155 }
1156 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1157 }
1158 else
1159 {
1160 nextv6DHCPState = ipv6Active;
1161 }
1162
1163 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001164 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001165 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001166 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001167 {
1168 messages::generalError(asyncResp->res);
1169 return;
1170 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001171 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001172 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001173 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001174 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001175 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001176 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001177 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001178 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001179 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001180 }
1181 else
1182 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001183 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001184 }
1185
1186 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001187 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001188 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001189 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001190 {
1191 messages::generalError(asyncResp->res);
1192 return;
1193 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001194 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001195 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001196 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001197 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001198 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001199 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001200 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001201 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001202 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001203 }
1204 else
1205 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001206 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001207 }
1208
1209 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001210 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001211 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001212 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001213 {
1214 messages::generalError(asyncResp->res);
1215 return;
1216 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001217 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001218 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001219 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001220 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001221 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001222 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001223 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001224 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001225 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001226 }
1227 else
1228 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001229 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001230 }
1231
1232 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1233 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1234 asyncResp);
1235 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1236 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1237 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1238 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1239 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1240 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1241}
1242
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001243inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001244 getNextStaticIpEntry(
1245 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1246 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1247{
1248 return std::find_if(head, end, [](const IPv4AddressData& value) {
1249 return value.origin == "Static";
1250 });
1251}
1252
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001253inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001254 getNextStaticIpEntry(
1255 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1256 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1257{
1258 return std::find_if(head, end, [](const IPv6AddressData& value) {
1259 return value.origin == "Static";
1260 });
1261}
1262
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001263inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001264 const std::string& ifaceId, nlohmann::json& input,
1265 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1266 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1267{
1268 if ((!input.is_array()) || input.empty())
1269 {
1270 messages::propertyValueTypeError(
1271 asyncResp->res,
1272 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1273 "IPv4StaticAddresses");
1274 return;
1275 }
1276
1277 unsigned entryIdx = 1;
1278 // Find the first static IP address currently active on the NIC and
1279 // match it to the first JSON element in the IPv4StaticAddresses array.
1280 // Match each subsequent JSON element to the next static IP programmed
1281 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001282 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001283 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1284
1285 for (nlohmann::json& thisJson : input)
1286 {
1287 std::string pathString =
1288 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1289
1290 if (!thisJson.is_null() && !thisJson.empty())
1291 {
1292 std::optional<std::string> address;
1293 std::optional<std::string> subnetMask;
1294 std::optional<std::string> gateway;
1295
1296 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1297 address, "SubnetMask", subnetMask,
1298 "Gateway", gateway))
1299 {
1300 messages::propertyValueFormatError(
1301 asyncResp->res,
1302 thisJson.dump(2, ' ', true,
1303 nlohmann::json::error_handler_t::replace),
1304 pathString);
1305 return;
1306 }
1307
1308 // Find the address/subnet/gateway values. Any values that are
1309 // not explicitly provided are assumed to be unmodified from the
1310 // current state of the interface. Merge existing state into the
1311 // current request.
1312 const std::string* addr = nullptr;
1313 const std::string* gw = nullptr;
1314 uint8_t prefixLength = 0;
1315 bool errorInEntry = false;
1316 if (address)
1317 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001318 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001319 {
1320 addr = &(*address);
1321 }
1322 else
1323 {
1324 messages::propertyValueFormatError(asyncResp->res, *address,
1325 pathString + "/Address");
1326 errorInEntry = true;
1327 }
1328 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001329 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001330 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001331 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001332 }
1333 else
1334 {
1335 messages::propertyMissing(asyncResp->res,
1336 pathString + "/Address");
1337 errorInEntry = true;
1338 }
1339
1340 if (subnetMask)
1341 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001342 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1343 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001344 {
1345 messages::propertyValueFormatError(
1346 asyncResp->res, *subnetMask,
1347 pathString + "/SubnetMask");
1348 errorInEntry = true;
1349 }
1350 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001351 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001352 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001353 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1354 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001355 {
1356 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001357 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001358 pathString + "/SubnetMask");
1359 errorInEntry = true;
1360 }
1361 }
1362 else
1363 {
1364 messages::propertyMissing(asyncResp->res,
1365 pathString + "/SubnetMask");
1366 errorInEntry = true;
1367 }
1368
1369 if (gateway)
1370 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001371 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001372 {
1373 gw = &(*gateway);
1374 }
1375 else
1376 {
1377 messages::propertyValueFormatError(asyncResp->res, *gateway,
1378 pathString + "/Gateway");
1379 errorInEntry = true;
1380 }
1381 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001382 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001383 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001384 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001385 }
1386 else
1387 {
1388 messages::propertyMissing(asyncResp->res,
1389 pathString + "/Gateway");
1390 errorInEntry = true;
1391 }
1392
1393 if (errorInEntry)
1394 {
1395 return;
1396 }
1397
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001398 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001399 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001400 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001401 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001402 nicIpEntry =
1403 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001404 }
1405 else
1406 {
1407 createIPv4(ifaceId, prefixLength, *gateway, *address,
1408 asyncResp);
1409 }
1410 entryIdx++;
1411 }
1412 else
1413 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001414 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001415 {
1416 // Requesting a DELETE/DO NOT MODIFY action for an item
1417 // that isn't present on the eth(n) interface. Input JSON is
1418 // in error, so bail out.
1419 if (thisJson.is_null())
1420 {
1421 messages::resourceCannotBeDeleted(asyncResp->res);
1422 return;
1423 }
1424 messages::propertyValueFormatError(
1425 asyncResp->res,
1426 thisJson.dump(2, ' ', true,
1427 nlohmann::json::error_handler_t::replace),
1428 pathString);
1429 return;
1430 }
1431
1432 if (thisJson.is_null())
1433 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001434 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001436 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001437 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001438 nicIpEntry =
1439 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001440 }
1441 entryIdx++;
1442 }
1443 }
1444}
1445
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001446inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001447 const std::string& ifaceId,
1448 const std::vector<std::string>& updatedStaticNameServers,
1449 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1450{
1451 crow::connections::systemBus->async_method_call(
1452 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001453 if (ec)
1454 {
1455 messages::internalError(asyncResp->res);
1456 return;
1457 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001458 },
1459 "xyz.openbmc_project.Network",
1460 "/xyz/openbmc_project/network/" + ifaceId,
1461 "org.freedesktop.DBus.Properties", "Set",
1462 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001463 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001464}
1465
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001466inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001467 const std::string& ifaceId, const nlohmann::json& input,
1468 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1469 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1470{
1471 if (!input.is_array() || input.empty())
1472 {
1473 messages::propertyValueTypeError(
1474 asyncResp->res,
1475 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1476 "IPv6StaticAddresses");
1477 return;
1478 }
1479 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001480 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001481 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1482 for (const nlohmann::json& thisJson : input)
1483 {
1484 std::string pathString =
1485 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1486
1487 if (!thisJson.is_null() && !thisJson.empty())
1488 {
1489 std::optional<std::string> address;
1490 std::optional<uint8_t> prefixLength;
1491 nlohmann::json thisJsonCopy = thisJson;
1492 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1493 address, "PrefixLength", prefixLength))
1494 {
1495 messages::propertyValueFormatError(
1496 asyncResp->res,
1497 thisJson.dump(2, ' ', true,
1498 nlohmann::json::error_handler_t::replace),
1499 pathString);
1500 return;
1501 }
1502
Ed Tanous543f4402022-01-06 13:12:53 -08001503 const std::string* addr = nullptr;
1504 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001505
1506 // Find the address and prefixLength values. Any values that are
1507 // not explicitly provided are assumed to be unmodified from the
1508 // current state of the interface. Merge existing state into the
1509 // current request.
1510 if (address)
1511 {
1512 addr = &(*address);
1513 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001514 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001515 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001516 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001517 }
1518 else
1519 {
1520 messages::propertyMissing(asyncResp->res,
1521 pathString + "/Address");
1522 return;
1523 }
1524
1525 if (prefixLength)
1526 {
1527 prefix = *prefixLength;
1528 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001529 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001530 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001531 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001532 }
1533 else
1534 {
1535 messages::propertyMissing(asyncResp->res,
1536 pathString + "/PrefixLength");
1537 return;
1538 }
1539
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001540 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001541 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001542 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001543 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001544 nicIpEntry =
1545 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001546 }
1547 else
1548 {
1549 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1550 }
1551 entryIdx++;
1552 }
1553 else
1554 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001555 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001556 {
1557 // Requesting a DELETE/DO NOT MODIFY action for an item
1558 // that isn't present on the eth(n) interface. Input JSON is
1559 // in error, so bail out.
1560 if (thisJson.is_null())
1561 {
1562 messages::resourceCannotBeDeleted(asyncResp->res);
1563 return;
1564 }
1565 messages::propertyValueFormatError(
1566 asyncResp->res,
1567 thisJson.dump(2, ' ', true,
1568 nlohmann::json::error_handler_t::replace),
1569 pathString);
1570 return;
1571 }
1572
1573 if (thisJson.is_null())
1574 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001575 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001577 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001578 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001579 nicIpEntry =
1580 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001581 }
1582 entryIdx++;
1583 }
1584 }
1585}
1586
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001587inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1589 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1590 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1591 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1592{
George Liu7a1dbc42022-12-07 16:03:22 +08001593 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001594 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1595
1596 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1597 jsonResponse["Id"] = ifaceId;
1598 jsonResponse["@odata.id"] =
1599 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1600 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1601
1602 auto health = std::make_shared<HealthPopulate>(asyncResp);
1603
George Liu7a1dbc42022-12-07 16:03:22 +08001604 dbus::utility::getSubTreePaths(
1605 "/", 0, inventoryForEthernet,
1606 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001607 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001608 if (ec)
1609 {
1610 return;
1611 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001612
Ed Tanous002d39b2022-05-31 08:59:27 -07001613 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001614 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001615
1616 health->populate();
1617
1618 if (ethData.nicEnabled)
1619 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001620 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001621 jsonResponse["Status"]["State"] = "Enabled";
1622 }
1623 else
1624 {
1625 jsonResponse["LinkStatus"] = "NoLink";
1626 jsonResponse["Status"]["State"] = "Disabled";
1627 }
1628
Ed Tanousbf648f72021-06-03 15:00:14 -07001629 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301630 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001631 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001632 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001633 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1634 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1635 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1636 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001637
1638 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001639 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001640 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001641 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1642 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1643 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001644
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001645 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001646 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001647 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001648
1649 // When domain name is empty then it means, that it is a network
1650 // without domain names, and the host name itself must be treated as
1651 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001652 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001653 if (!ethData.domainnames.empty())
1654 {
1655 fqdn += "." + ethData.domainnames[0];
1656 }
1657 jsonResponse["FQDN"] = fqdn;
1658 }
1659
Ed Tanous613dabe2022-07-09 11:17:36 -07001660 jsonResponse["VLANs"]["@odata.id"] =
1661 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1662 "EthernetInterfaces", ifaceId, "VLANs");
Ed Tanousbf648f72021-06-03 15:00:14 -07001663
1664 jsonResponse["NameServers"] = ethData.nameServers;
1665 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1666
1667 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1668 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1669 ipv4Array = nlohmann::json::array();
1670 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001671 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001672 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001673 std::string gatewayStr = ipv4Config.gateway;
1674 if (gatewayStr.empty())
1675 {
1676 gatewayStr = "0.0.0.0";
1677 }
Ed Tanous14766872022-03-15 10:44:42 -07001678 nlohmann::json::object_t ipv4;
1679 ipv4["AddressOrigin"] = ipv4Config.origin;
1680 ipv4["SubnetMask"] = ipv4Config.netmask;
1681 ipv4["Address"] = ipv4Config.address;
1682 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001683
Ed Tanousbf648f72021-06-03 15:00:14 -07001684 if (ipv4Config.origin == "Static")
1685 {
Ed Tanous14766872022-03-15 10:44:42 -07001686 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001687 }
Ed Tanous14766872022-03-15 10:44:42 -07001688
1689 ipv4Array.push_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001690 }
1691
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001692 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001693 if (ipv6GatewayStr.empty())
1694 {
1695 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1696 }
1697
1698 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1699
1700 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1701 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1702 ipv6Array = nlohmann::json::array();
1703 ipv6StaticArray = nlohmann::json::array();
1704 nlohmann::json& ipv6AddrPolicyTable =
1705 jsonResponse["IPv6AddressPolicyTable"];
1706 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001707 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001708 {
Ed Tanous14766872022-03-15 10:44:42 -07001709 nlohmann::json::object_t ipv6;
1710 ipv6["Address"] = ipv6Config.address;
1711 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1712 ipv6["AddressOrigin"] = ipv6Config.origin;
1713 ipv6["AddressState"] = nullptr;
1714 ipv6Array.push_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001715 if (ipv6Config.origin == "Static")
1716 {
Ed Tanous14766872022-03-15 10:44:42 -07001717 nlohmann::json::object_t ipv6Static;
1718 ipv6Static["Address"] = ipv6Config.address;
1719 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1720 ipv6StaticArray.push_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001721 }
1722 }
1723}
1724
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001725inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001726{
Ed Tanous11ba3972022-07-11 09:50:41 -07001727 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001728}
1729
1730inline void requestEthernetInterfacesRoutes(App& app)
1731{
1732 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001733 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001734 .methods(boost::beast::http::verb::get)(
1735 [&app](const crow::Request& req,
1736 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001737 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 {
1739 return;
1740 }
1741
1742 asyncResp->res.jsonValue["@odata.type"] =
1743 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1744 asyncResp->res.jsonValue["@odata.id"] =
1745 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1746 asyncResp->res.jsonValue["Name"] =
1747 "Ethernet Network Interface Collection";
1748 asyncResp->res.jsonValue["Description"] =
1749 "Collection of EthernetInterfaces for this Manager";
1750
1751 // Get eth interface list, and call the below callback for JSON
1752 // preparation
1753 getEthernetIfaceList(
1754 [asyncResp](
1755 const bool& success,
1756 const boost::container::flat_set<std::string>& ifaceList) {
1757 if (!success)
1758 {
1759 messages::internalError(asyncResp->res);
1760 return;
1761 }
1762
1763 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1764 ifaceArray = nlohmann::json::array();
1765 std::string tag = "_";
1766 for (const std::string& ifaceItem : ifaceList)
1767 {
1768 std::size_t found = ifaceItem.find(tag);
1769 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001770 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001771 nlohmann::json::object_t iface;
1772 iface["@odata.id"] =
1773 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1774 ifaceItem;
1775 ifaceArray.push_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001776 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001777 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001778
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1780 asyncResp->res.jsonValue["@odata.id"] =
1781 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1782 });
1783 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001784
Ed Tanousbf648f72021-06-03 15:00:14 -07001785 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001786 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001787 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001788 [&app](const crow::Request& req,
1789 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1790 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001791 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001792 {
1793 return;
1794 }
1795 getEthernetIfaceData(
1796 ifaceId,
1797 [asyncResp, ifaceId](
1798 const bool& success, const EthernetInterfaceData& ethData,
1799 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1800 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1801 if (!success)
1802 {
1803 // TODO(Pawel)consider distinguish between non
1804 // existing object, and other errors
1805 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1806 ifaceId);
1807 return;
1808 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001809
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001810 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1811 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001813 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001814 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1815 asyncResp->res.jsonValue["Description"] =
1816 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301817
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001819 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001821
Ed Tanousbf648f72021-06-03 15:00:14 -07001822 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001823 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001824 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001825 [&app](const crow::Request& req,
1826 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1827 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001828 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001829 {
1830 return;
1831 }
1832 std::optional<std::string> hostname;
1833 std::optional<std::string> fqdn;
1834 std::optional<std::string> macAddress;
1835 std::optional<std::string> ipv6DefaultGateway;
1836 std::optional<nlohmann::json> ipv4StaticAddresses;
1837 std::optional<nlohmann::json> ipv6StaticAddresses;
1838 std::optional<std::vector<std::string>> staticNameServers;
1839 std::optional<nlohmann::json> dhcpv4;
1840 std::optional<nlohmann::json> dhcpv6;
1841 std::optional<bool> interfaceEnabled;
1842 std::optional<size_t> mtuSize;
1843 DHCPParameters v4dhcpParms;
1844 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001845
Ed Tanous002d39b2022-05-31 08:59:27 -07001846 if (!json_util::readJsonPatch(
1847 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1848 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1849 macAddress, "StaticNameServers", staticNameServers,
1850 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1851 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1852 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1853 {
1854 return;
1855 }
1856 if (dhcpv4)
1857 {
1858 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1859 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1860 v4dhcpParms.useDnsServers, "UseNTPServers",
1861 v4dhcpParms.useNtpServers, "UseDomainName",
1862 v4dhcpParms.useDomainName))
1863 {
1864 return;
1865 }
1866 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001867
Ed Tanous002d39b2022-05-31 08:59:27 -07001868 if (dhcpv6)
1869 {
1870 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1871 v6dhcpParms.dhcpv6OperatingMode,
1872 "UseDNSServers", v6dhcpParms.useDnsServers,
1873 "UseNTPServers", v6dhcpParms.useNtpServers,
1874 "UseDomainName",
1875 v6dhcpParms.useDomainName))
1876 {
1877 return;
1878 }
1879 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001880
Ed Tanous002d39b2022-05-31 08:59:27 -07001881 // Get single eth interface data, and call the below callback
1882 // for JSON preparation
1883 getEthernetIfaceData(
1884 ifaceId,
1885 [asyncResp, ifaceId, hostname = std::move(hostname),
1886 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1887 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1888 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1889 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1890 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001891 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1892 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001893 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1894 const bool& success, const EthernetInterfaceData& ethData,
1895 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1896 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1897 if (!success)
1898 {
1899 // ... otherwise return error
1900 // TODO(Pawel)consider distinguish between non
1901 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001902 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001903 ifaceId);
1904 return;
1905 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001906
Ed Tanous002d39b2022-05-31 08:59:27 -07001907 if (dhcpv4 || dhcpv6)
1908 {
1909 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001910 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001911 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301912
Ed Tanous002d39b2022-05-31 08:59:27 -07001913 if (hostname)
1914 {
1915 handleHostnamePatch(*hostname, asyncResp);
1916 }
1917
1918 if (fqdn)
1919 {
1920 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1921 }
1922
1923 if (macAddress)
1924 {
1925 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1926 }
1927
1928 if (ipv4StaticAddresses)
1929 {
1930 // TODO(ed) for some reason the capture of
1931 // ipv4Addresses above is returning a const value,
1932 // not a non-const value. This doesn't really work
1933 // for us, as we need to be able to efficiently move
1934 // out the intermedia nlohmann::json objects. This
1935 // makes a copy of the structure, and operates on
1936 // that, but could be done more efficiently
1937 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1938 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1939 }
1940
1941 if (staticNameServers)
1942 {
1943 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1944 asyncResp);
1945 }
1946
1947 if (ipv6DefaultGateway)
1948 {
1949 messages::propertyNotWritable(asyncResp->res,
1950 "IPv6DefaultGateway");
1951 }
1952
1953 if (ipv6StaticAddresses)
1954 {
1955 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1956 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1957 asyncResp);
1958 }
1959
1960 if (interfaceEnabled)
1961 {
1962 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1963 *interfaceEnabled, asyncResp);
1964 }
1965
1966 if (mtuSize)
1967 {
1968 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
1969 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001970 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001971 });
manojkiraneda2a133282019-02-19 13:09:43 +05301972
Ed Tanousbf648f72021-06-03 15:00:14 -07001973 BMCWEB_ROUTE(
1974 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001975 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001976 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001977 [&app](const crow::Request& req,
1978 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1979 const std::string& parentIfaceId,
1980 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001981 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001982 {
1983 return;
1984 }
1985 asyncResp->res.jsonValue["@odata.type"] =
1986 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1987 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001988
Ed Tanous002d39b2022-05-31 08:59:27 -07001989 if (!verifyNames(parentIfaceId, ifaceId))
1990 {
1991 return;
1992 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001993
Ed Tanous002d39b2022-05-31 08:59:27 -07001994 // Get single eth interface data, and call the below callback
1995 // for JSON preparation
1996 getEthernetIfaceData(
1997 ifaceId,
1998 [asyncResp, parentIfaceId,
1999 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2000 const boost::container::flat_set<IPv4AddressData>&,
2001 const boost::container::flat_set<IPv6AddressData>&) {
2002 if (success && ethData.vlanId)
2003 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002004 asyncResp->res.jsonValue["Id"] = ifaceId;
2005 asyncResp->res.jsonValue["@odata.id"] =
2006 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2007 parentIfaceId + "/VLANs/" + ifaceId;
2008
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002009 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002010 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002011 }
2012 else
2013 {
2014 // ... otherwise return error
2015 // TODO(Pawel)consider distinguish between non
2016 // existing object, and other errors
2017 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002018 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002019 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002020 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002021 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002022
Ed Tanousbf648f72021-06-03 15:00:14 -07002023 BMCWEB_ROUTE(
2024 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002025 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002026 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002027 [&app](const crow::Request& req,
2028 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2029 const std::string& parentIfaceId,
2030 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002031 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002032 {
2033 return;
2034 }
2035 if (!verifyNames(parentIfaceId, ifaceId))
2036 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002037 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002038 ifaceId);
2039 return;
2040 }
2041
2042 std::optional<bool> vlanEnable;
2043 std::optional<uint32_t> vlanId;
2044
2045 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2046 vlanEnable, "VLANId", vlanId))
2047 {
2048 return;
2049 }
2050
2051 if (vlanId)
2052 {
2053 messages::propertyNotWritable(asyncResp->res, "VLANId");
2054 return;
2055 }
2056
2057 // Get single eth interface data, and call the below callback
2058 // for JSON preparation
2059 getEthernetIfaceData(
2060 ifaceId,
2061 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2062 const bool& success, const EthernetInterfaceData& ethData,
2063 const boost::container::flat_set<IPv4AddressData>&,
2064 const boost::container::flat_set<IPv6AddressData>&) {
2065 if (success && ethData.vlanId)
2066 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002067 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002068 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002069 crow::connections::systemBus->async_method_call(
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002070 [asyncResp](const boost::system::error_code ec) {
2071 if (ec)
2072 {
2073 messages::internalError(asyncResp->res);
2074 return;
2075 }
2076 },
2077 "xyz.openbmc_project.Network",
2078 "/xyz/openbmc_project/network/" + ifaceId,
2079 "org.freedesktop.DBus.Properties", "Set",
2080 "xyz.openbmc_project.Network.EthernetInterface",
2081 "NICEnabled",
2082 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002083 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002084 }
2085 else
2086 {
2087 // TODO(Pawel)consider distinguish between non
2088 // existing object, and other errors
2089 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002090 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 return;
2092 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002093 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002094 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002095
2096 BMCWEB_ROUTE(
2097 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002098 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002099 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002100 [&app](const crow::Request& req,
2101 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2102 const std::string& parentIfaceId,
2103 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002104 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002105 {
2106 return;
2107 }
2108 if (!verifyNames(parentIfaceId, ifaceId))
2109 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002110 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 ifaceId);
2112 return;
2113 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002114
Ed Tanous002d39b2022-05-31 08:59:27 -07002115 // Get single eth interface data, and call the below callback
2116 // for JSON preparation
2117 getEthernetIfaceData(
2118 ifaceId,
2119 [asyncResp, parentIfaceId,
2120 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2121 const boost::container::flat_set<IPv4AddressData>&,
2122 const boost::container::flat_set<IPv6AddressData>&) {
2123 if (success && ethData.vlanId)
2124 {
2125 auto callback =
2126 [asyncResp](const boost::system::error_code ec) {
2127 if (ec)
2128 {
2129 messages::internalError(asyncResp->res);
2130 }
2131 };
2132 crow::connections::systemBus->async_method_call(
2133 std::move(callback), "xyz.openbmc_project.Network",
2134 std::string("/xyz/openbmc_project/network/") + ifaceId,
2135 "xyz.openbmc_project.Object.Delete", "Delete");
2136 }
2137 else
2138 {
2139 // ... otherwise return error
2140 // TODO(Pawel)consider distinguish between non
2141 // existing object, and other errors
2142 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002143 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002144 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002145 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002146 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002147
Ed Tanousbf648f72021-06-03 15:00:14 -07002148 BMCWEB_ROUTE(app,
2149 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002150
2151 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002152 .methods(boost::beast::http::verb::get)(
2153 [&app](const crow::Request& req,
2154 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2155 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002156 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002157 {
2158 return;
2159 }
2160 // Get eth interface list, and call the below callback for JSON
2161 // preparation
2162 getEthernetIfaceList(
2163 [asyncResp, rootInterfaceName](
2164 const bool& success,
2165 const boost::container::flat_set<std::string>& ifaceList) {
2166 if (!success)
2167 {
2168 messages::internalError(asyncResp->res);
2169 return;
2170 }
2171
2172 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2173 {
2174 messages::resourceNotFound(asyncResp->res,
2175 "VLanNetworkInterfaceCollection",
2176 rootInterfaceName);
2177 return;
2178 }
2179
2180 asyncResp->res.jsonValue["@odata.type"] =
2181 "#VLanNetworkInterfaceCollection."
2182 "VLanNetworkInterfaceCollection";
2183 asyncResp->res.jsonValue["Name"] =
2184 "VLAN Network Interface Collection";
2185
2186 nlohmann::json ifaceArray = nlohmann::json::array();
2187
2188 for (const std::string& ifaceItem : ifaceList)
2189 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002190 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002191 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002192 std::string path =
2193 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2194 path += rootInterfaceName;
2195 path += "/VLANs/";
2196 path += ifaceItem;
2197 nlohmann::json::object_t iface;
2198 iface["@odata.id"] = std::move(path);
2199 ifaceArray.push_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002200 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002201 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002202
Ed Tanous002d39b2022-05-31 08:59:27 -07002203 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2204 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2205 asyncResp->res.jsonValue["@odata.id"] =
2206 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2207 rootInterfaceName + "/VLANs";
2208 });
2209 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002210
Ed Tanousbf648f72021-06-03 15:00:14 -07002211 BMCWEB_ROUTE(app,
2212 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002213 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002214 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002215 [&app](const crow::Request& req,
2216 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2217 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002218 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002219 {
2220 return;
2221 }
2222 bool vlanEnable = false;
2223 uint32_t vlanId = 0;
2224 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2225 "VLANEnable", vlanEnable))
2226 {
2227 return;
2228 }
2229 // Need both vlanId and vlanEnable to service this request
2230 if (vlanId == 0U)
2231 {
2232 messages::propertyMissing(asyncResp->res, "VLANId");
2233 }
2234 if (!vlanEnable)
2235 {
2236 messages::propertyMissing(asyncResp->res, "VLANEnable");
2237 }
2238 if (static_cast<bool>(vlanId) ^ vlanEnable)
2239 {
2240 return;
2241 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002242
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 auto callback = [asyncResp](const boost::system::error_code ec) {
2244 if (ec)
2245 {
2246 // TODO(ed) make more consistent error messages
2247 // based on phosphor-network responses
2248 messages::internalError(asyncResp->res);
2249 return;
2250 }
2251 messages::created(asyncResp->res);
2252 };
2253 crow::connections::systemBus->async_method_call(
2254 std::move(callback), "xyz.openbmc_project.Network",
2255 "/xyz/openbmc_project/network",
2256 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2257 rootInterfaceName, vlanId);
2258 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002259}
2260
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261} // namespace redfish