blob: 64dc204225263ca604637dae33894df511654dd7 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
19#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080020#include "dbus_utility.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "error_messages.hpp"
22#include "health.hpp"
23#include "query.hpp"
24#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070025#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080026#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070027
Ed Tanous11ba3972022-07-11 09:50:41 -070028#include <boost/algorithm/string/classification.hpp>
29#include <boost/algorithm/string/split.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070030#include <boost/container/flat_set.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050031
George Liu7a1dbc42022-12-07 16:03:22 +080032#include <array>
Ed Tanousa24526d2018-12-10 15:17:59 -080033#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053034#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080035#include <string_view>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010036
Ed Tanous1abe55e2018-09-05 08:30:59 -070037namespace redfish
38{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010039
Ed Tanous4a0cb852018-10-15 07:55:04 -070040enum class LinkType
41{
42 Local,
43 Global
44};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010045
46/**
47 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010048 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070049struct IPv4AddressData
50{
51 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070052 std::string address;
53 std::string domain;
54 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 std::string netmask;
56 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070057 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050058 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070059
Gunnar Mills1214b7e2020-06-04 10:11:30 -050060 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010064};
65
66/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050067 * Structure for keeping IPv6 data required by Redfish
68 */
69struct IPv6AddressData
70{
71 std::string id;
72 std::string address;
73 std::string origin;
74 uint8_t prefixLength;
75
Gunnar Mills1214b7e2020-06-04 10:11:30 -050076 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050077 {
78 return id < obj.id;
79 }
80};
81/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010082 * Structure for keeping basic single Ethernet Interface information
83 * available from DBus
84 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070085struct EthernetInterfaceData
86{
Ed Tanous4a0cb852018-10-15 07:55:04 -070087 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053088 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080089 bool autoNeg;
90 bool dnsEnabled;
91 bool ntpEnabled;
92 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080093 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070094 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080095 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070096 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080097 std::string hostName;
98 std::string defaultGateway;
99 std::string ipv6DefaultGateway;
100 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800101 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500102 std::vector<std::string> nameServers;
103 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800104 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100105};
106
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700107struct DHCPParameters
108{
109 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800110 std::optional<bool> useDnsServers;
111 std::optional<bool> useNtpServers;
112 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700113 std::optional<std::string> dhcpv6OperatingMode;
114};
115
Ed Tanous4a0cb852018-10-15 07:55:04 -0700116// Helper function that changes bits netmask notation (i.e. /24)
117// into full dot notation
118inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700120 uint32_t value = 0xffffffff << (32 - bits);
121 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
122 std::to_string((value >> 16) & 0xff) + "." +
123 std::to_string((value >> 8) & 0xff) + "." +
124 std::to_string(value & 0xff);
125 return netmask;
126}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100127
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800128inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700129 bool isIPv4)
130{
131 if (isIPv4)
132 {
133 return (
134 (inputDHCP ==
135 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
136 (inputDHCP ==
137 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
138 }
139 return ((inputDHCP ==
140 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
141 (inputDHCP ==
142 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
143}
144
Ed Tanous2c70f802020-09-28 14:29:23 -0700145inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700146{
147 if (isIPv4 && isIPv6)
148 {
149 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
150 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700151 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700152 {
153 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
154 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700155 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700156 {
157 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
158 }
159 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
160}
161
Ed Tanous4a0cb852018-10-15 07:55:04 -0700162inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500163 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700164 bool isIPv4)
165{
166 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700168 return "Static";
169 }
170 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
171 {
172 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700173 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700174 return "IPv4LinkLocal";
175 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700176 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700177 }
178 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
179 {
180 if (isIPv4)
181 {
182 return "DHCP";
183 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700184 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700185 }
186 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
187 {
188 return "SLAAC";
189 }
190 return "";
191}
192
Ed Tanous02cad962022-06-30 16:50:15 -0700193inline bool extractEthernetInterfaceData(
194 const std::string& ethifaceId,
195 const dbus::utility::ManagedObjectType& dbusData,
196 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700198 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700199 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700200 {
Ed Tanous02cad962022-06-30 16:50:15 -0700201 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700202 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000203 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700205 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700206 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500208 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700210 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500212 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800213 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 if (mac != nullptr)
215 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800216 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 }
220 }
221 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
222 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 {
225 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500227 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800228 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 if (id != nullptr)
230 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800231 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700232 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700234 }
235 }
236 else if (ifacePair.first ==
237 "xyz.openbmc_project.Network.EthernetInterface")
238 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500239 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700240 {
241 if (propertyPair.first == "AutoNeg")
242 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700243 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800244 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700245 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800247 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 }
249 }
250 else if (propertyPair.first == "Speed")
251 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500252 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800253 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254 if (speed != nullptr)
255 {
256 ethData.speed = *speed;
257 }
258 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530259 else if (propertyPair.first == "MTU")
260 {
261 const uint32_t* mtuSize =
262 std::get_if<uint32_t>(&propertyPair.second);
263 if (mtuSize != nullptr)
264 {
265 ethData.mtuSize = *mtuSize;
266 }
267 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800268 else if (propertyPair.first == "LinkUp")
269 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800271 std::get_if<bool>(&propertyPair.second);
272 if (linkUp != nullptr)
273 {
274 ethData.linkUp = *linkUp;
275 }
276 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700277 else if (propertyPair.first == "NICEnabled")
278 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500279 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700280 std::get_if<bool>(&propertyPair.second);
281 if (nicEnabled != nullptr)
282 {
283 ethData.nicEnabled = *nicEnabled;
284 }
285 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500286 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500288 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500289 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800290 &propertyPair.second);
291 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700292 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700293 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500294 }
295 }
296 else if (propertyPair.first == "StaticNameServers")
297 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500298 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500299 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500300 &propertyPair.second);
301 if (staticNameServers != nullptr)
302 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700303 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700304 }
305 }
manojkiraneda2a133282019-02-19 13:09:43 +0530306 else if (propertyPair.first == "DHCPEnabled")
307 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700308 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700309 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700310 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530311 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800312 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530313 }
314 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800315 else if (propertyPair.first == "DomainName")
316 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500318 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800319 &propertyPair.second);
320 if (domainNames != nullptr)
321 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700322 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800323 }
324 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500325 else if (propertyPair.first == "DefaultGateway")
326 {
327 const std::string* defaultGateway =
328 std::get_if<std::string>(&propertyPair.second);
329 if (defaultGateway != nullptr)
330 {
331 std::string defaultGatewayStr = *defaultGateway;
332 if (defaultGatewayStr.empty())
333 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800334 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500335 }
336 else
337 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800338 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500339 }
340 }
341 }
342 else if (propertyPair.first == "DefaultGateway6")
343 {
344 const std::string* defaultGateway6 =
345 std::get_if<std::string>(&propertyPair.second);
346 if (defaultGateway6 != nullptr)
347 {
348 std::string defaultGateway6Str =
349 *defaultGateway6;
350 if (defaultGateway6Str.empty())
351 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800352 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500353 "0:0:0:0:0:0:0:0";
354 }
355 else
356 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800357 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500358 defaultGateway6Str;
359 }
360 }
361 }
Ed Tanous029573d2019-02-01 10:57:49 -0800362 }
363 }
364 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700365
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800366 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700367 {
368 if (ifacePair.first ==
369 "xyz.openbmc_project.Network.DHCPConfiguration")
370 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 {
373 if (propertyPair.first == "DNSEnabled")
374 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700375 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700376 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800379 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 }
381 }
382 else if (propertyPair.first == "NTPEnabled")
383 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700384 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700385 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800388 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 }
390 }
391 else if (propertyPair.first == "HostNameEnabled")
392 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700393 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700394 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700395 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700396 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800397 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700398 }
399 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700400 }
401 }
402 }
Ed Tanous029573d2019-02-01 10:57:49 -0800403 // System configuration shows up in the global namespace, so no need
404 // to check eth number
405 if (ifacePair.first ==
406 "xyz.openbmc_project.Network.SystemConfiguration")
407 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500408 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800409 {
410 if (propertyPair.first == "HostName")
411 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500412 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500413 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800414 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700415 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800416 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800417 }
418 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 }
420 }
421 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700422 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700423 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700424}
425
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500426// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700427inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800429 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000430 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500431{
Tony Lee353163e2022-11-23 11:06:10 +0800432 const std::string ipPathStart =
433 "/xyz/openbmc_project/network/" + ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500434
435 // Since there might be several IPv6 configurations aligned with
436 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000437 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500438 {
439 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800440 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800442 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500443 {
444 if (interface.first == "xyz.openbmc_project.Network.IP")
445 {
Tony Lee353163e2022-11-23 11:06:10 +0800446 auto type = std::find_if(interface.second.begin(),
447 interface.second.end(),
448 [](const auto& property) {
449 return property.first == "Type";
450 });
451 if (type == interface.second.end())
452 {
453 continue;
454 }
455
456 const std::string* typeStr =
457 std::get_if<std::string>(&type->second);
458
459 if (typeStr == nullptr ||
460 (*typeStr !=
461 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
462 {
463 continue;
464 }
465
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 // Instance IPv6AddressData structure, and set as
467 // appropriate
468 std::pair<
469 boost::container::flat_set<IPv6AddressData>::iterator,
470 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000471 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700472 IPv6AddressData& ipv6Address = *it.first;
473 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800474 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800475 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500476 {
477 if (property.first == "Address")
478 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500479 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500480 std::get_if<std::string>(&property.second);
481 if (address != nullptr)
482 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700483 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500484 }
485 }
486 else if (property.first == "Origin")
487 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500488 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500489 std::get_if<std::string>(&property.second);
490 if (origin != nullptr)
491 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700492 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500493 translateAddressOriginDbusToRedfish(*origin,
494 false);
495 }
496 }
497 else if (property.first == "PrefixLength")
498 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500499 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500500 std::get_if<uint8_t>(&property.second);
501 if (prefix != nullptr)
502 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700503 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500504 }
505 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600506 else if (property.first == "Type" ||
507 property.first == "Gateway")
508 {
509 // Type & Gateway is not used
510 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500511 else
512 {
513 BMCWEB_LOG_ERROR
514 << "Got extra property: " << property.first
515 << " on the " << objpath.first.str << " object";
516 }
517 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500518 }
519 }
520 }
521 }
522}
523
Ed Tanous4a0cb852018-10-15 07:55:04 -0700524// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700525inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000526 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800527 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000528 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700529{
Tony Lee353163e2022-11-23 11:06:10 +0800530 const std::string ipPathStart =
531 "/xyz/openbmc_project/network/" + ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700532
533 // Since there might be several IPv4 configurations aligned with
534 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000535 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700536 {
537 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800538 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700539 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800540 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 {
542 if (interface.first == "xyz.openbmc_project.Network.IP")
543 {
Tony Lee353163e2022-11-23 11:06:10 +0800544 auto type = std::find_if(interface.second.begin(),
545 interface.second.end(),
546 [](const auto& property) {
547 return property.first == "Type";
548 });
549 if (type == interface.second.end())
550 {
551 continue;
552 }
553
554 const std::string* typeStr =
555 std::get_if<std::string>(&type->second);
556
557 if (typeStr == nullptr ||
558 (*typeStr !=
559 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
560 {
561 continue;
562 }
563
Ed Tanous4a0cb852018-10-15 07:55:04 -0700564 // Instance IPv4AddressData structure, and set as
565 // appropriate
566 std::pair<
567 boost::container::flat_set<IPv4AddressData>::iterator,
568 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000569 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700570 IPv4AddressData& ipv4Address = *it.first;
571 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800572 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800573 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700574 {
575 if (property.first == "Address")
576 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500577 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800578 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700579 if (address != nullptr)
580 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700581 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700582 }
583 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700584 else if (property.first == "Origin")
585 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500586 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800587 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700588 if (origin != nullptr)
589 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700590 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700591 translateAddressOriginDbusToRedfish(*origin,
592 true);
593 }
594 }
595 else if (property.first == "PrefixLength")
596 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500597 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800598 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700599 if (mask != nullptr)
600 {
601 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700602 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603 }
604 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600605 else if (property.first == "Type" ||
606 property.first == "Gateway")
607 {
608 // Type & Gateway is not used
609 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700610 else
611 {
612 BMCWEB_LOG_ERROR
613 << "Got extra property: " << property.first
614 << " on the " << objpath.first.str << " object";
615 }
616 }
617 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700618 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700619 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700620 ? LinkType::Local
621 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622 }
623 }
624 }
625 }
626}
627
628/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700629 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 *
631 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 * @param[in] ipHash DBus Hash id of IP that should be deleted
633 * @param[io] asyncResp Response object that will be returned to client
634 *
635 * @return None
636 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500637inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800638 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700639{
640 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800641 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700642 if (ec)
643 {
644 messages::internalError(asyncResp->res);
645 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700646 },
647 "xyz.openbmc_project.Network",
648 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
649 "xyz.openbmc_project.Object.Delete", "Delete");
650}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700651
Gunnar Mills244b6d52021-04-12 15:44:23 -0500652inline void updateIPv4DefaultGateway(
653 const std::string& ifaceId, const std::string& gateway,
654 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500655{
656 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800657 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700658 if (ec)
659 {
660 messages::internalError(asyncResp->res);
661 return;
662 }
663 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500664 },
665 "xyz.openbmc_project.Network",
666 "/xyz/openbmc_project/network/" + ifaceId,
667 "org.freedesktop.DBus.Properties", "Set",
668 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800669 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500670}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700672 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700674 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
675 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
676 * @param[in] gateway IPv4 address of this interfaces gateway
677 * @param[in] address IPv4 address to assign to this interface
678 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 *
680 * @return None
681 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000682inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
683 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800684 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700685{
Ed Tanous002d39b2022-05-31 08:59:27 -0700686 auto createIpHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800687 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500688 if (ec)
689 {
690 messages::internalError(asyncResp->res);
691 return;
692 }
693 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
694 };
695
Ed Tanous4a0cb852018-10-15 07:55:04 -0700696 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500697 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700698 "/xyz/openbmc_project/network/" + ifaceId,
699 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700700 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 gateway);
702}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500703
704/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700705 * @brief Deletes the IPv4 entry for this interface and creates a replacement
706 * static IPv4 entry
707 *
708 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
709 * @param[in] id The unique hash entry identifying the DBus entry
710 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
711 * @param[in] gateway IPv4 address of this interfaces gateway
712 * @param[in] address IPv4 address to assign to this interface
713 * @param[io] asyncResp Response object that will be returned to client
714 *
715 * @return None
716 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800717inline void
718 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
719 uint8_t prefixLength, const std::string& gateway,
720 const std::string& address,
721 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700722{
723 crow::connections::systemBus->async_method_call(
724 [asyncResp, ifaceId, address, prefixLength,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800725 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 if (ec)
727 {
728 messages::internalError(asyncResp->res);
729 return;
730 }
731
732 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800733 [asyncResp, ifaceId,
734 gateway](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700735 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700736 {
737 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500738 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700739 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700740 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
741 },
742 "xyz.openbmc_project.Network",
743 "/xyz/openbmc_project/network/" + ifaceId,
744 "xyz.openbmc_project.Network.IP.Create", "IP",
745 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
746 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700747 },
748 "xyz.openbmc_project.Network",
749 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
750 "xyz.openbmc_project.Object.Delete", "Delete");
751}
752
753/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500754 * @brief Deletes given IPv6
755 *
756 * @param[in] ifaceId Id of interface whose IP should be deleted
757 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500758 * @param[io] asyncResp Response object that will be returned to client
759 *
760 * @return None
761 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500762inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800763 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500764{
765 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800766 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700767 if (ec)
768 {
769 messages::internalError(asyncResp->res);
770 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500771 },
772 "xyz.openbmc_project.Network",
773 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
774 "xyz.openbmc_project.Object.Delete", "Delete");
775}
776
777/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700778 * @brief Deletes the IPv6 entry for this interface and creates a replacement
779 * static IPv6 entry
780 *
781 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
782 * @param[in] id The unique hash entry identifying the DBus entry
783 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
784 * @param[in] address IPv6 address to assign to this interface
785 * @param[io] asyncResp Response object that will be returned to client
786 *
787 * @return None
788 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800789inline void
790 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
791 uint8_t prefixLength, const std::string& address,
792 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700793{
794 crow::connections::systemBus->async_method_call(
795 [asyncResp, ifaceId, address,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800796 prefixLength](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700797 if (ec)
798 {
799 messages::internalError(asyncResp->res);
800 }
801 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800802 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700803 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700804 {
805 messages::internalError(asyncResp->res);
806 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700807 },
808 "xyz.openbmc_project.Network",
809 "/xyz/openbmc_project/network/" + ifaceId,
810 "xyz.openbmc_project.Network.IP.Create", "IP",
811 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
812 prefixLength, "");
Johnathan Mantey01784822019-06-18 12:44:21 -0700813 },
814 "xyz.openbmc_project.Network",
815 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
816 "xyz.openbmc_project.Object.Delete", "Delete");
817}
818
819/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500820 * @brief Creates IPv6 with given data
821 *
822 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500823 * @param[in] prefixLength Prefix length that needs to be added
824 * @param[in] address IP address that needs to be added
825 * @param[io] asyncResp Response object that will be returned to client
826 *
827 * @return None
828 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500829inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
830 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800831 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500832{
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800833 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500834 if (ec)
835 {
836 messages::internalError(asyncResp->res);
837 }
838 };
839 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500840 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500841 crow::connections::systemBus->async_method_call(
842 std::move(createIpHandler), "xyz.openbmc_project.Network",
843 "/xyz/openbmc_project/network/" + ifaceId,
844 "xyz.openbmc_project.Network.IP.Create", "IP",
845 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
846 "");
847}
848
Ed Tanous4a0cb852018-10-15 07:55:04 -0700849/**
850 * Function that retrieves all properties for given Ethernet Interface
851 * Object
852 * from EntityManager Network Manager
853 * @param ethiface_id a eth interface id to query on DBus
854 * @param callback a function that shall be called to convert Dbus output
855 * into JSON
856 */
857template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000858void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500859 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700860{
861 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800862 [ethifaceId{std::string{ethifaceId}},
863 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800864 const boost::system::error_code& errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700865 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700866 EthernetInterfaceData ethData{};
867 boost::container::flat_set<IPv4AddressData> ipv4Data;
868 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700869
Ed Tanous002d39b2022-05-31 08:59:27 -0700870 if (errorCode)
871 {
872 callback(false, ethData, ipv4Data, ipv6Data);
873 return;
874 }
875
876 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
877 if (!found)
878 {
879 callback(false, ethData, ipv4Data, ipv6Data);
880 return;
881 }
882
883 extractIPData(ethifaceId, resp, ipv4Data);
884 // Fix global GW
885 for (IPv4AddressData& ipv4 : ipv4Data)
886 {
887 if (((ipv4.linktype == LinkType::Global) &&
888 (ipv4.gateway == "0.0.0.0")) ||
889 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700890 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700891 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700892 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700893 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700894
Ed Tanous002d39b2022-05-31 08:59:27 -0700895 extractIPV6Data(ethifaceId, resp, ipv6Data);
896 // Finally make a callback with useful data
897 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700898 },
899 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
900 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700901}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700902
903/**
904 * Function that retrieves all Ethernet Interfaces available through Network
905 * Manager
906 * @param callback a function that shall be called to convert Dbus output
907 * into JSON.
908 */
909template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500910void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700911{
912 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800913 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800914 const boost::system::error_code& errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800915 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700916 // Callback requires vector<string> to retrieve all available
917 // ethernet interfaces
918 boost::container::flat_set<std::string> ifaceList;
919 ifaceList.reserve(resp.size());
920 if (errorCode)
921 {
922 callback(false, ifaceList);
923 return;
924 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700925
Ed Tanous002d39b2022-05-31 08:59:27 -0700926 // Iterate over all retrieved ObjectPaths.
927 for (const auto& objpath : resp)
928 {
929 // And all interfaces available for certain ObjectPath.
930 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700932 // If interface is
933 // xyz.openbmc_project.Network.EthernetInterface, this is
934 // what we're looking for.
935 if (interface.first ==
936 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700937 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700938 std::string ifaceId = objpath.first.filename();
939 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700940 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700941 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700943 // and put it into output vector.
944 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700947 }
948 // Finally make a callback with useful data
949 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700950 },
951 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
952 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700953}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100954
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700955inline void
956 handleHostnamePatch(const std::string& hostname,
957 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958{
Ed Tanousbf648f72021-06-03 15:00:14 -0700959 // SHOULD handle host names of up to 255 characters(RFC 1123)
960 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700962 messages::propertyValueFormatError(asyncResp->res, hostname,
963 "HostName");
964 return;
965 }
966 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800967 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700968 if (ec)
969 {
970 messages::internalError(asyncResp->res);
971 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700972 },
973 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
974 "org.freedesktop.DBus.Properties", "Set",
975 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800976 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700977}
978
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700979inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530980 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
981 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
982{
983 sdbusplus::message::object_path objPath =
984 "/xyz/openbmc_project/network/" + ifaceId;
985 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800986 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700987 if (ec)
988 {
989 messages::internalError(asyncResp->res);
990 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530991 },
992 "xyz.openbmc_project.Network", objPath,
993 "org.freedesktop.DBus.Properties", "Set",
994 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
995 std::variant<size_t>(mtuSize));
996}
997
998inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700999 handleDomainnamePatch(const std::string& ifaceId,
1000 const std::string& domainname,
1001 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001002{
1003 std::vector<std::string> vectorDomainname = {domainname};
1004 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001005 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001006 if (ec)
1007 {
1008 messages::internalError(asyncResp->res);
1009 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001010 },
1011 "xyz.openbmc_project.Network",
1012 "/xyz/openbmc_project/network/" + ifaceId,
1013 "org.freedesktop.DBus.Properties", "Set",
1014 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001015 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001016}
1017
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001018inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001019{
1020 // A valid host name can never have the dotted-decimal form (RFC 1123)
1021 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1022 {
1023 return false;
1024 }
1025 // Each label(hostname/subdomains) within a valid FQDN
1026 // MUST handle host names of up to 63 characters (RFC 1123)
1027 // labels cannot start or end with hyphens (RFC 952)
1028 // labels can start with numbers (RFC 1123)
1029 const std::regex pattern(
1030 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1031
1032 return std::regex_match(hostname, pattern);
1033}
1034
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001035inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001036{
1037 // Can have multiple subdomains
1038 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001039 const std::regex pattern(
1040 "^([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 -07001041
1042 return std::regex_match(domainname, pattern);
1043}
1044
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001045inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1046 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001047{
1048 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1049 if (fqdn.length() > 255)
1050 {
1051 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1052 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 }
1054
Ed Tanousbf648f72021-06-03 15:00:14 -07001055 size_t pos = fqdn.find('.');
1056 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001058 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1059 return;
1060 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001061
Ed Tanousbf648f72021-06-03 15:00:14 -07001062 std::string hostname;
1063 std::string domainname;
1064 domainname = (fqdn).substr(pos + 1);
1065 hostname = (fqdn).substr(0, pos);
1066
1067 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1068 {
1069 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1070 return;
1071 }
1072
1073 handleHostnamePatch(hostname, asyncResp);
1074 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1075}
1076
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001077inline void
1078 handleMACAddressPatch(const std::string& ifaceId,
1079 const std::string& macAddress,
1080 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001081{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001082 static constexpr std::string_view dbusNotAllowedError =
1083 "xyz.openbmc_project.Common.Error.NotAllowed";
1084
Ed Tanousbf648f72021-06-03 15:00:14 -07001085 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001086 [asyncResp, macAddress](const boost::system::error_code& ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001087 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001088 if (ec)
1089 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001090 const sd_bus_error* err = msg.get_error();
1091 if (err == nullptr)
1092 {
1093 messages::internalError(asyncResp->res);
1094 return;
1095 }
1096 if (err->name == dbusNotAllowedError)
1097 {
1098 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1099 return;
1100 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001101 messages::internalError(asyncResp->res);
1102 return;
1103 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001104 },
1105 "xyz.openbmc_project.Network",
1106 "/xyz/openbmc_project/network/" + ifaceId,
1107 "org.freedesktop.DBus.Properties", "Set",
1108 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001109 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001110}
1111
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001112inline void setDHCPEnabled(const std::string& ifaceId,
1113 const std::string& propertyName, const bool v4Value,
1114 const bool v6Value,
1115 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001116{
1117 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1118 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001119 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001120 if (ec)
1121 {
1122 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1123 messages::internalError(asyncResp->res);
1124 return;
1125 }
1126 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001127 },
1128 "xyz.openbmc_project.Network",
1129 "/xyz/openbmc_project/network/" + ifaceId,
1130 "org.freedesktop.DBus.Properties", "Set",
1131 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001132 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001133}
1134
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001135inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001136 const std::string& ifaceId, const std::string& propertyName,
1137 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1138{
1139 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001140 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001141 if (ec)
1142 {
1143 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1144 messages::internalError(asyncResp->res);
1145 return;
1146 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001147 },
1148 "xyz.openbmc_project.Network",
1149 "/xyz/openbmc_project/network/" + ifaceId,
1150 "org.freedesktop.DBus.Properties", "Set",
1151 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001152 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001153}
1154
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001155inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1156 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001157{
1158 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1159 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001160 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001161 if (ec)
1162 {
1163 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1164 messages::internalError(asyncResp->res);
1165 return;
1166 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001167 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001168 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001169 "org.freedesktop.DBus.Properties", "Set",
1170 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001171 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001172}
1173
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001174inline void handleDHCPPatch(const std::string& ifaceId,
1175 const EthernetInterfaceData& ethData,
1176 const DHCPParameters& v4dhcpParms,
1177 const DHCPParameters& v6dhcpParms,
1178 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001179{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001180 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1181 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001182
1183 bool nextv4DHCPState =
1184 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1185
1186 bool nextv6DHCPState{};
1187 if (v6dhcpParms.dhcpv6OperatingMode)
1188 {
1189 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1190 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1191 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1192 {
1193 messages::propertyValueFormatError(asyncResp->res,
1194 *v6dhcpParms.dhcpv6OperatingMode,
1195 "OperatingMode");
1196 return;
1197 }
1198 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1199 }
1200 else
1201 {
1202 nextv6DHCPState = ipv6Active;
1203 }
1204
1205 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001206 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001207 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001208 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001209 {
1210 messages::generalError(asyncResp->res);
1211 return;
1212 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001213 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001214 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001215 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001216 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001217 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001218 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001219 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001220 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001221 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001222 }
1223 else
1224 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001225 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001226 }
1227
1228 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001229 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001230 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001231 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001232 {
1233 messages::generalError(asyncResp->res);
1234 return;
1235 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001236 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001237 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001238 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001239 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001240 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001241 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001242 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001243 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001244 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001245 }
1246 else
1247 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001248 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001249 }
1250
1251 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001252 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001253 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001254 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001255 {
1256 messages::generalError(asyncResp->res);
1257 return;
1258 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001259 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001260 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001261 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001262 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001263 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001264 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001265 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001266 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001267 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001268 }
1269 else
1270 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001271 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001272 }
1273
1274 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1275 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1276 asyncResp);
1277 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1278 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1279 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1280 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1281 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1282 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1283}
1284
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001285inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001286 getNextStaticIpEntry(
1287 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1288 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1289{
1290 return std::find_if(head, end, [](const IPv4AddressData& value) {
1291 return value.origin == "Static";
1292 });
1293}
1294
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001295inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001296 getNextStaticIpEntry(
1297 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1298 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1299{
1300 return std::find_if(head, end, [](const IPv6AddressData& value) {
1301 return value.origin == "Static";
1302 });
1303}
1304
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001305inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001306 const std::string& ifaceId, nlohmann::json& input,
1307 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1308 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1309{
1310 if ((!input.is_array()) || input.empty())
1311 {
1312 messages::propertyValueTypeError(
1313 asyncResp->res,
1314 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1315 "IPv4StaticAddresses");
1316 return;
1317 }
1318
1319 unsigned entryIdx = 1;
1320 // Find the first static IP address currently active on the NIC and
1321 // match it to the first JSON element in the IPv4StaticAddresses array.
1322 // Match each subsequent JSON element to the next static IP programmed
1323 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001324 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001325 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1326
1327 for (nlohmann::json& thisJson : input)
1328 {
1329 std::string pathString =
1330 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1331
1332 if (!thisJson.is_null() && !thisJson.empty())
1333 {
1334 std::optional<std::string> address;
1335 std::optional<std::string> subnetMask;
1336 std::optional<std::string> gateway;
1337
1338 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1339 address, "SubnetMask", subnetMask,
1340 "Gateway", gateway))
1341 {
1342 messages::propertyValueFormatError(
1343 asyncResp->res,
1344 thisJson.dump(2, ' ', true,
1345 nlohmann::json::error_handler_t::replace),
1346 pathString);
1347 return;
1348 }
1349
1350 // Find the address/subnet/gateway values. Any values that are
1351 // not explicitly provided are assumed to be unmodified from the
1352 // current state of the interface. Merge existing state into the
1353 // current request.
1354 const std::string* addr = nullptr;
1355 const std::string* gw = nullptr;
1356 uint8_t prefixLength = 0;
1357 bool errorInEntry = false;
1358 if (address)
1359 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001360 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001361 {
1362 addr = &(*address);
1363 }
1364 else
1365 {
1366 messages::propertyValueFormatError(asyncResp->res, *address,
1367 pathString + "/Address");
1368 errorInEntry = true;
1369 }
1370 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001371 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001372 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001373 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001374 }
1375 else
1376 {
1377 messages::propertyMissing(asyncResp->res,
1378 pathString + "/Address");
1379 errorInEntry = true;
1380 }
1381
1382 if (subnetMask)
1383 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001384 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1385 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001386 {
1387 messages::propertyValueFormatError(
1388 asyncResp->res, *subnetMask,
1389 pathString + "/SubnetMask");
1390 errorInEntry = true;
1391 }
1392 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001393 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001394 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001395 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1396 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001397 {
1398 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001399 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001400 pathString + "/SubnetMask");
1401 errorInEntry = true;
1402 }
1403 }
1404 else
1405 {
1406 messages::propertyMissing(asyncResp->res,
1407 pathString + "/SubnetMask");
1408 errorInEntry = true;
1409 }
1410
1411 if (gateway)
1412 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001413 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001414 {
1415 gw = &(*gateway);
1416 }
1417 else
1418 {
1419 messages::propertyValueFormatError(asyncResp->res, *gateway,
1420 pathString + "/Gateway");
1421 errorInEntry = true;
1422 }
1423 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001424 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001425 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001426 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001427 }
1428 else
1429 {
1430 messages::propertyMissing(asyncResp->res,
1431 pathString + "/Gateway");
1432 errorInEntry = true;
1433 }
1434
1435 if (errorInEntry)
1436 {
1437 return;
1438 }
1439
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001440 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001441 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001442 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001443 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001444 nicIpEntry =
1445 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001446 }
1447 else
1448 {
1449 createIPv4(ifaceId, prefixLength, *gateway, *address,
1450 asyncResp);
1451 }
1452 entryIdx++;
1453 }
1454 else
1455 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001456 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001457 {
1458 // Requesting a DELETE/DO NOT MODIFY action for an item
1459 // that isn't present on the eth(n) interface. Input JSON is
1460 // in error, so bail out.
1461 if (thisJson.is_null())
1462 {
1463 messages::resourceCannotBeDeleted(asyncResp->res);
1464 return;
1465 }
1466 messages::propertyValueFormatError(
1467 asyncResp->res,
1468 thisJson.dump(2, ' ', true,
1469 nlohmann::json::error_handler_t::replace),
1470 pathString);
1471 return;
1472 }
1473
1474 if (thisJson.is_null())
1475 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001476 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001477 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001478 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001479 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001480 nicIpEntry =
1481 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001482 }
1483 entryIdx++;
1484 }
1485 }
1486}
1487
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001488inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001489 const std::string& ifaceId,
1490 const std::vector<std::string>& updatedStaticNameServers,
1491 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1492{
1493 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001494 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001495 if (ec)
1496 {
1497 messages::internalError(asyncResp->res);
1498 return;
1499 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001500 },
1501 "xyz.openbmc_project.Network",
1502 "/xyz/openbmc_project/network/" + ifaceId,
1503 "org.freedesktop.DBus.Properties", "Set",
1504 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001505 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001506}
1507
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001508inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001509 const std::string& ifaceId, const nlohmann::json& input,
1510 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1511 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1512{
1513 if (!input.is_array() || input.empty())
1514 {
1515 messages::propertyValueTypeError(
1516 asyncResp->res,
1517 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1518 "IPv6StaticAddresses");
1519 return;
1520 }
1521 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001522 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001523 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1524 for (const nlohmann::json& thisJson : input)
1525 {
1526 std::string pathString =
1527 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1528
1529 if (!thisJson.is_null() && !thisJson.empty())
1530 {
1531 std::optional<std::string> address;
1532 std::optional<uint8_t> prefixLength;
1533 nlohmann::json thisJsonCopy = thisJson;
1534 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1535 address, "PrefixLength", prefixLength))
1536 {
1537 messages::propertyValueFormatError(
1538 asyncResp->res,
1539 thisJson.dump(2, ' ', true,
1540 nlohmann::json::error_handler_t::replace),
1541 pathString);
1542 return;
1543 }
1544
Ed Tanous543f4402022-01-06 13:12:53 -08001545 const std::string* addr = nullptr;
1546 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001547
1548 // Find the address and prefixLength values. Any values that are
1549 // not explicitly provided are assumed to be unmodified from the
1550 // current state of the interface. Merge existing state into the
1551 // current request.
1552 if (address)
1553 {
1554 addr = &(*address);
1555 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001556 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001557 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001558 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001559 }
1560 else
1561 {
1562 messages::propertyMissing(asyncResp->res,
1563 pathString + "/Address");
1564 return;
1565 }
1566
1567 if (prefixLength)
1568 {
1569 prefix = *prefixLength;
1570 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001571 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001572 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001573 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001574 }
1575 else
1576 {
1577 messages::propertyMissing(asyncResp->res,
1578 pathString + "/PrefixLength");
1579 return;
1580 }
1581
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001582 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001583 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001584 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001585 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001586 nicIpEntry =
1587 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001588 }
1589 else
1590 {
1591 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1592 }
1593 entryIdx++;
1594 }
1595 else
1596 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001597 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001598 {
1599 // Requesting a DELETE/DO NOT MODIFY action for an item
1600 // that isn't present on the eth(n) interface. Input JSON is
1601 // in error, so bail out.
1602 if (thisJson.is_null())
1603 {
1604 messages::resourceCannotBeDeleted(asyncResp->res);
1605 return;
1606 }
1607 messages::propertyValueFormatError(
1608 asyncResp->res,
1609 thisJson.dump(2, ' ', true,
1610 nlohmann::json::error_handler_t::replace),
1611 pathString);
1612 return;
1613 }
1614
1615 if (thisJson.is_null())
1616 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001617 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001618 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001619 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001620 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001621 nicIpEntry =
1622 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001623 }
1624 entryIdx++;
1625 }
1626 }
1627}
1628
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001629inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001630 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1631 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1632 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1633 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1634{
George Liu7a1dbc42022-12-07 16:03:22 +08001635 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001636 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1637
1638 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1639 jsonResponse["Id"] = ifaceId;
Willy Tueddfc432022-09-26 16:46:38 +00001640 jsonResponse["@odata.id"] = crow::utility::urlFromPieces(
1641 "redfish", "v1", "Managers", "bmc", "EthernetInterfaces", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001642 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1643
1644 auto health = std::make_shared<HealthPopulate>(asyncResp);
1645
George Liu7a1dbc42022-12-07 16:03:22 +08001646 dbus::utility::getSubTreePaths(
1647 "/", 0, inventoryForEthernet,
1648 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001649 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001650 if (ec)
1651 {
1652 return;
1653 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001654
Ed Tanous002d39b2022-05-31 08:59:27 -07001655 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001656 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001657
1658 health->populate();
1659
1660 if (ethData.nicEnabled)
1661 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001662 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001663 jsonResponse["Status"]["State"] = "Enabled";
1664 }
1665 else
1666 {
1667 jsonResponse["LinkStatus"] = "NoLink";
1668 jsonResponse["Status"]["State"] = "Disabled";
1669 }
1670
Ed Tanousbf648f72021-06-03 15:00:14 -07001671 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301672 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001673 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001674 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001675 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1676 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1677 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1678 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001679
1680 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001681 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001682 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001683 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1684 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1685 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001686
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001687 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001688 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001689 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001690
1691 // When domain name is empty then it means, that it is a network
1692 // without domain names, and the host name itself must be treated as
1693 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001694 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001695 if (!ethData.domainnames.empty())
1696 {
1697 fqdn += "." + ethData.domainnames[0];
1698 }
1699 jsonResponse["FQDN"] = fqdn;
1700 }
1701
Ed Tanous613dabe2022-07-09 11:17:36 -07001702 jsonResponse["VLANs"]["@odata.id"] =
1703 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1704 "EthernetInterfaces", ifaceId, "VLANs");
Ed Tanousbf648f72021-06-03 15:00:14 -07001705
1706 jsonResponse["NameServers"] = ethData.nameServers;
1707 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1708
1709 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1710 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1711 ipv4Array = nlohmann::json::array();
1712 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001713 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001714 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001715 std::string gatewayStr = ipv4Config.gateway;
1716 if (gatewayStr.empty())
1717 {
1718 gatewayStr = "0.0.0.0";
1719 }
Ed Tanous14766872022-03-15 10:44:42 -07001720 nlohmann::json::object_t ipv4;
1721 ipv4["AddressOrigin"] = ipv4Config.origin;
1722 ipv4["SubnetMask"] = ipv4Config.netmask;
1723 ipv4["Address"] = ipv4Config.address;
1724 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001725
Ed Tanousbf648f72021-06-03 15:00:14 -07001726 if (ipv4Config.origin == "Static")
1727 {
Ed Tanous14766872022-03-15 10:44:42 -07001728 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001729 }
Ed Tanous14766872022-03-15 10:44:42 -07001730
1731 ipv4Array.push_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001732 }
1733
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001734 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001735 if (ipv6GatewayStr.empty())
1736 {
1737 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1738 }
1739
1740 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1741
1742 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1743 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1744 ipv6Array = nlohmann::json::array();
1745 ipv6StaticArray = nlohmann::json::array();
1746 nlohmann::json& ipv6AddrPolicyTable =
1747 jsonResponse["IPv6AddressPolicyTable"];
1748 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001749 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001750 {
Ed Tanous14766872022-03-15 10:44:42 -07001751 nlohmann::json::object_t ipv6;
1752 ipv6["Address"] = ipv6Config.address;
1753 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1754 ipv6["AddressOrigin"] = ipv6Config.origin;
1755 ipv6["AddressState"] = nullptr;
1756 ipv6Array.push_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001757 if (ipv6Config.origin == "Static")
1758 {
Ed Tanous14766872022-03-15 10:44:42 -07001759 nlohmann::json::object_t ipv6Static;
1760 ipv6Static["Address"] = ipv6Config.address;
1761 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1762 ipv6StaticArray.push_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001763 }
1764 }
1765}
1766
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001767inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001768{
Ed Tanous11ba3972022-07-11 09:50:41 -07001769 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001770}
1771
1772inline void requestEthernetInterfacesRoutes(App& app)
1773{
1774 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001775 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001776 .methods(boost::beast::http::verb::get)(
1777 [&app](const crow::Request& req,
1778 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001779 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001780 {
1781 return;
1782 }
1783
1784 asyncResp->res.jsonValue["@odata.type"] =
1785 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1786 asyncResp->res.jsonValue["@odata.id"] =
1787 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1788 asyncResp->res.jsonValue["Name"] =
1789 "Ethernet Network Interface Collection";
1790 asyncResp->res.jsonValue["Description"] =
1791 "Collection of EthernetInterfaces for this Manager";
1792
1793 // Get eth interface list, and call the below callback for JSON
1794 // preparation
1795 getEthernetIfaceList(
1796 [asyncResp](
1797 const bool& success,
1798 const boost::container::flat_set<std::string>& ifaceList) {
1799 if (!success)
1800 {
1801 messages::internalError(asyncResp->res);
1802 return;
1803 }
1804
1805 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1806 ifaceArray = nlohmann::json::array();
1807 std::string tag = "_";
1808 for (const std::string& ifaceItem : ifaceList)
1809 {
1810 std::size_t found = ifaceItem.find(tag);
1811 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001812 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 nlohmann::json::object_t iface;
Willy Tueddfc432022-09-26 16:46:38 +00001814 iface["@odata.id"] = crow::utility::urlFromPieces(
1815 "redfish", "v1", "Managers", "bmc",
1816 "EthernetInterfaces", ifaceItem);
Ed Tanous002d39b2022-05-31 08:59:27 -07001817 ifaceArray.push_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001818 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001820
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1822 asyncResp->res.jsonValue["@odata.id"] =
1823 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1824 });
1825 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001826
Ed Tanousbf648f72021-06-03 15:00:14 -07001827 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001828 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001829 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001830 [&app](const crow::Request& req,
1831 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1832 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001833 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001834 {
1835 return;
1836 }
1837 getEthernetIfaceData(
1838 ifaceId,
1839 [asyncResp, ifaceId](
1840 const bool& success, const EthernetInterfaceData& ethData,
1841 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1842 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1843 if (!success)
1844 {
1845 // TODO(Pawel)consider distinguish between non
1846 // existing object, and other errors
1847 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1848 ifaceId);
1849 return;
1850 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001851
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001852 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1853 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001854 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001855 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001856 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1857 asyncResp->res.jsonValue["Description"] =
1858 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301859
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001861 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001862 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001863
Ed Tanousbf648f72021-06-03 15:00:14 -07001864 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001865 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001866 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001867 [&app](const crow::Request& req,
1868 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1869 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001870 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001871 {
1872 return;
1873 }
1874 std::optional<std::string> hostname;
1875 std::optional<std::string> fqdn;
1876 std::optional<std::string> macAddress;
1877 std::optional<std::string> ipv6DefaultGateway;
1878 std::optional<nlohmann::json> ipv4StaticAddresses;
1879 std::optional<nlohmann::json> ipv6StaticAddresses;
1880 std::optional<std::vector<std::string>> staticNameServers;
1881 std::optional<nlohmann::json> dhcpv4;
1882 std::optional<nlohmann::json> dhcpv6;
1883 std::optional<bool> interfaceEnabled;
1884 std::optional<size_t> mtuSize;
1885 DHCPParameters v4dhcpParms;
1886 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001887
Ed Tanous002d39b2022-05-31 08:59:27 -07001888 if (!json_util::readJsonPatch(
1889 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1890 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1891 macAddress, "StaticNameServers", staticNameServers,
1892 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1893 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1894 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1895 {
1896 return;
1897 }
1898 if (dhcpv4)
1899 {
1900 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1901 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1902 v4dhcpParms.useDnsServers, "UseNTPServers",
1903 v4dhcpParms.useNtpServers, "UseDomainName",
1904 v4dhcpParms.useDomainName))
1905 {
1906 return;
1907 }
1908 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001909
Ed Tanous002d39b2022-05-31 08:59:27 -07001910 if (dhcpv6)
1911 {
1912 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1913 v6dhcpParms.dhcpv6OperatingMode,
1914 "UseDNSServers", v6dhcpParms.useDnsServers,
1915 "UseNTPServers", v6dhcpParms.useNtpServers,
1916 "UseDomainName",
1917 v6dhcpParms.useDomainName))
1918 {
1919 return;
1920 }
1921 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001922
Ed Tanous002d39b2022-05-31 08:59:27 -07001923 // Get single eth interface data, and call the below callback
1924 // for JSON preparation
1925 getEthernetIfaceData(
1926 ifaceId,
1927 [asyncResp, ifaceId, hostname = std::move(hostname),
1928 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1929 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1930 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1931 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1932 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001933 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1934 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001935 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1936 const bool& success, const EthernetInterfaceData& ethData,
1937 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1938 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1939 if (!success)
1940 {
1941 // ... otherwise return error
1942 // TODO(Pawel)consider distinguish between non
1943 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001944 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001945 ifaceId);
1946 return;
1947 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001948
Ed Tanous002d39b2022-05-31 08:59:27 -07001949 if (dhcpv4 || dhcpv6)
1950 {
1951 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001952 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001953 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 if (hostname)
1956 {
1957 handleHostnamePatch(*hostname, asyncResp);
1958 }
1959
1960 if (fqdn)
1961 {
1962 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1963 }
1964
1965 if (macAddress)
1966 {
1967 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1968 }
1969
1970 if (ipv4StaticAddresses)
1971 {
1972 // TODO(ed) for some reason the capture of
1973 // ipv4Addresses above is returning a const value,
1974 // not a non-const value. This doesn't really work
1975 // for us, as we need to be able to efficiently move
1976 // out the intermedia nlohmann::json objects. This
1977 // makes a copy of the structure, and operates on
1978 // that, but could be done more efficiently
1979 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1980 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1981 }
1982
1983 if (staticNameServers)
1984 {
1985 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1986 asyncResp);
1987 }
1988
1989 if (ipv6DefaultGateway)
1990 {
1991 messages::propertyNotWritable(asyncResp->res,
1992 "IPv6DefaultGateway");
1993 }
1994
1995 if (ipv6StaticAddresses)
1996 {
1997 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1998 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1999 asyncResp);
2000 }
2001
2002 if (interfaceEnabled)
2003 {
2004 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2005 *interfaceEnabled, asyncResp);
2006 }
2007
2008 if (mtuSize)
2009 {
2010 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2011 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002012 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002013 });
manojkiraneda2a133282019-02-19 13:09:43 +05302014
Ed Tanousbf648f72021-06-03 15:00:14 -07002015 BMCWEB_ROUTE(
2016 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002017 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002018 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002019 [&app](const crow::Request& req,
2020 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2021 const std::string& parentIfaceId,
2022 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002023 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002024 {
2025 return;
2026 }
2027 asyncResp->res.jsonValue["@odata.type"] =
2028 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2029 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002030
Ed Tanous002d39b2022-05-31 08:59:27 -07002031 if (!verifyNames(parentIfaceId, ifaceId))
2032 {
2033 return;
2034 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002035
Ed Tanous002d39b2022-05-31 08:59:27 -07002036 // Get single eth interface data, and call the below callback
2037 // for JSON preparation
2038 getEthernetIfaceData(
2039 ifaceId,
2040 [asyncResp, parentIfaceId,
2041 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2042 const boost::container::flat_set<IPv4AddressData>&,
2043 const boost::container::flat_set<IPv6AddressData>&) {
2044 if (success && ethData.vlanId)
2045 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002046 asyncResp->res.jsonValue["Id"] = ifaceId;
2047 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002048 crow::utility::urlFromPieces(
2049 "redfish", "v1", "Managers", "bmc",
2050 "EthernetInterfaces", parentIfaceId, "VLANs", ifaceId);
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002051
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002052 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002053 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 }
2055 else
2056 {
2057 // ... otherwise return error
2058 // TODO(Pawel)consider distinguish between non
2059 // existing object, and other errors
2060 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002061 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002062 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002063 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002064 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002065
Ed Tanousbf648f72021-06-03 15:00:14 -07002066 BMCWEB_ROUTE(
2067 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002068 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002069 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002070 [&app](const crow::Request& req,
2071 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2072 const std::string& parentIfaceId,
2073 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002074 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 {
2076 return;
2077 }
2078 if (!verifyNames(parentIfaceId, ifaceId))
2079 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002080 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002081 ifaceId);
2082 return;
2083 }
2084
2085 std::optional<bool> vlanEnable;
2086 std::optional<uint32_t> vlanId;
2087
2088 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2089 vlanEnable, "VLANId", vlanId))
2090 {
2091 return;
2092 }
2093
2094 if (vlanId)
2095 {
2096 messages::propertyNotWritable(asyncResp->res, "VLANId");
2097 return;
2098 }
2099
2100 // Get single eth interface data, and call the below callback
2101 // for JSON preparation
2102 getEthernetIfaceData(
2103 ifaceId,
2104 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2105 const bool& success, const EthernetInterfaceData& ethData,
2106 const boost::container::flat_set<IPv4AddressData>&,
2107 const boost::container::flat_set<IPv6AddressData>&) {
2108 if (success && ethData.vlanId)
2109 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002110 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002111 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002112 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002113 [asyncResp](const boost::system::error_code& ec) {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002114 if (ec)
2115 {
2116 messages::internalError(asyncResp->res);
2117 return;
2118 }
2119 },
2120 "xyz.openbmc_project.Network",
2121 "/xyz/openbmc_project/network/" + ifaceId,
2122 "org.freedesktop.DBus.Properties", "Set",
2123 "xyz.openbmc_project.Network.EthernetInterface",
2124 "NICEnabled",
2125 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002126 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002127 }
2128 else
2129 {
2130 // TODO(Pawel)consider distinguish between non
2131 // existing object, and other errors
2132 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002133 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002134 return;
2135 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002136 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002137 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002138
2139 BMCWEB_ROUTE(
2140 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002141 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002142 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002143 [&app](const crow::Request& req,
2144 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2145 const std::string& parentIfaceId,
2146 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002147 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002148 {
2149 return;
2150 }
2151 if (!verifyNames(parentIfaceId, ifaceId))
2152 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002153 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002154 ifaceId);
2155 return;
2156 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002157
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 // Get single eth interface data, and call the below callback
2159 // for JSON preparation
2160 getEthernetIfaceData(
2161 ifaceId,
2162 [asyncResp, parentIfaceId,
2163 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2164 const boost::container::flat_set<IPv4AddressData>&,
2165 const boost::container::flat_set<IPv6AddressData>&) {
2166 if (success && ethData.vlanId)
2167 {
2168 auto callback =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002169 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002170 if (ec)
2171 {
2172 messages::internalError(asyncResp->res);
2173 }
2174 };
2175 crow::connections::systemBus->async_method_call(
2176 std::move(callback), "xyz.openbmc_project.Network",
2177 std::string("/xyz/openbmc_project/network/") + ifaceId,
2178 "xyz.openbmc_project.Object.Delete", "Delete");
2179 }
2180 else
2181 {
2182 // ... otherwise return error
2183 // TODO(Pawel)consider distinguish between non
2184 // existing object, and other errors
2185 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002186 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002187 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002188 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002189 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002190
Ed Tanousbf648f72021-06-03 15:00:14 -07002191 BMCWEB_ROUTE(app,
2192 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002193
2194 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002195 .methods(boost::beast::http::verb::get)(
2196 [&app](const crow::Request& req,
2197 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2198 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002199 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002200 {
2201 return;
2202 }
2203 // Get eth interface list, and call the below callback for JSON
2204 // preparation
2205 getEthernetIfaceList(
2206 [asyncResp, rootInterfaceName](
2207 const bool& success,
2208 const boost::container::flat_set<std::string>& ifaceList) {
2209 if (!success)
2210 {
2211 messages::internalError(asyncResp->res);
2212 return;
2213 }
2214
2215 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2216 {
2217 messages::resourceNotFound(asyncResp->res,
2218 "VLanNetworkInterfaceCollection",
2219 rootInterfaceName);
2220 return;
2221 }
2222
2223 asyncResp->res.jsonValue["@odata.type"] =
2224 "#VLanNetworkInterfaceCollection."
2225 "VLanNetworkInterfaceCollection";
2226 asyncResp->res.jsonValue["Name"] =
2227 "VLAN Network Interface Collection";
2228
2229 nlohmann::json ifaceArray = nlohmann::json::array();
2230
2231 for (const std::string& ifaceItem : ifaceList)
2232 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002233 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002234 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002235 nlohmann::json::object_t iface;
Willy Tueddfc432022-09-26 16:46:38 +00002236 iface["@odata.id"] = crow::utility::urlFromPieces(
2237 "redfish", "v1", "Managers", "bmc",
2238 "EthernetInterfaces", rootInterfaceName, "VLANs",
2239 ifaceItem);
Ed Tanous002d39b2022-05-31 08:59:27 -07002240 ifaceArray.push_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002241 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002242 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002243
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2245 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2246 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002247 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
2248 "EthernetInterfaces",
2249 rootInterfaceName, "VLANs");
Ed Tanous002d39b2022-05-31 08:59:27 -07002250 });
2251 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002252
Ed Tanousbf648f72021-06-03 15:00:14 -07002253 BMCWEB_ROUTE(app,
2254 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002255 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002256 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002257 [&app](const crow::Request& req,
2258 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2259 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002260 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002261 {
2262 return;
2263 }
2264 bool vlanEnable = false;
2265 uint32_t vlanId = 0;
2266 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2267 "VLANEnable", vlanEnable))
2268 {
2269 return;
2270 }
2271 // Need both vlanId and vlanEnable to service this request
2272 if (vlanId == 0U)
2273 {
2274 messages::propertyMissing(asyncResp->res, "VLANId");
2275 }
2276 if (!vlanEnable)
2277 {
2278 messages::propertyMissing(asyncResp->res, "VLANEnable");
2279 }
2280 if (static_cast<bool>(vlanId) ^ vlanEnable)
2281 {
2282 return;
2283 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002284
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002285 auto callback = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002286 if (ec)
2287 {
2288 // TODO(ed) make more consistent error messages
2289 // based on phosphor-network responses
2290 messages::internalError(asyncResp->res);
2291 return;
2292 }
2293 messages::created(asyncResp->res);
2294 };
2295 crow::connections::systemBus->async_method_call(
2296 std::move(callback), "xyz.openbmc_project.Network",
2297 "/xyz/openbmc_project/network",
2298 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2299 rootInterfaceName, vlanId);
2300 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002301}
2302
Ed Tanous1abe55e2018-09-05 08:30:59 -07002303} // namespace redfish