blob: 8cdb7e026049b4862ba7014d8dbd25c95e7965aa [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(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700641 [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(
657 [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 =
687 [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,
725 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(
733 [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
734 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700735 {
736 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500737 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700738 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700739 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
740 },
741 "xyz.openbmc_project.Network",
742 "/xyz/openbmc_project/network/" + ifaceId,
743 "xyz.openbmc_project.Network.IP.Create", "IP",
744 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
745 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700746 },
747 "xyz.openbmc_project.Network",
748 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
749 "xyz.openbmc_project.Object.Delete", "Delete");
750}
751
752/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500753 * @brief Deletes given IPv6
754 *
755 * @param[in] ifaceId Id of interface whose IP should be deleted
756 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500757 * @param[io] asyncResp Response object that will be returned to client
758 *
759 * @return None
760 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500761inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800762 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500763{
764 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700765 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700766 if (ec)
767 {
768 messages::internalError(asyncResp->res);
769 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500770 },
771 "xyz.openbmc_project.Network",
772 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
773 "xyz.openbmc_project.Object.Delete", "Delete");
774}
775
776/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700777 * @brief Deletes the IPv6 entry for this interface and creates a replacement
778 * static IPv6 entry
779 *
780 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
781 * @param[in] id The unique hash entry identifying the DBus entry
782 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
783 * @param[in] address IPv6 address to assign to this interface
784 * @param[io] asyncResp Response object that will be returned to client
785 *
786 * @return None
787 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800788inline void
789 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
790 uint8_t prefixLength, const std::string& address,
791 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700792{
793 crow::connections::systemBus->async_method_call(
794 [asyncResp, ifaceId, address,
795 prefixLength](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700796 if (ec)
797 {
798 messages::internalError(asyncResp->res);
799 }
800 crow::connections::systemBus->async_method_call(
801 [asyncResp](const boost::system::error_code ec2) {
802 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700803 {
804 messages::internalError(asyncResp->res);
805 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700806 },
807 "xyz.openbmc_project.Network",
808 "/xyz/openbmc_project/network/" + ifaceId,
809 "xyz.openbmc_project.Network.IP.Create", "IP",
810 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
811 prefixLength, "");
Johnathan Mantey01784822019-06-18 12:44:21 -0700812 },
813 "xyz.openbmc_project.Network",
814 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
815 "xyz.openbmc_project.Object.Delete", "Delete");
816}
817
818/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500819 * @brief Creates IPv6 with given data
820 *
821 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500822 * @param[in] prefixLength Prefix length that needs to be added
823 * @param[in] address IP address that needs to be added
824 * @param[io] asyncResp Response object that will be returned to client
825 *
826 * @return None
827 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500828inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
829 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800830 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500831{
832 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
833 if (ec)
834 {
835 messages::internalError(asyncResp->res);
836 }
837 };
838 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500839 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500840 crow::connections::systemBus->async_method_call(
841 std::move(createIpHandler), "xyz.openbmc_project.Network",
842 "/xyz/openbmc_project/network/" + ifaceId,
843 "xyz.openbmc_project.Network.IP.Create", "IP",
844 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
845 "");
846}
847
Ed Tanous4a0cb852018-10-15 07:55:04 -0700848/**
849 * Function that retrieves all properties for given Ethernet Interface
850 * Object
851 * from EntityManager Network Manager
852 * @param ethiface_id a eth interface id to query on DBus
853 * @param callback a function that shall be called to convert Dbus output
854 * into JSON
855 */
856template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000857void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500858 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700859{
860 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800861 [ethifaceId{std::string{ethifaceId}},
862 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000863 const boost::system::error_code errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700864 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700865 EthernetInterfaceData ethData{};
866 boost::container::flat_set<IPv4AddressData> ipv4Data;
867 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700868
Ed Tanous002d39b2022-05-31 08:59:27 -0700869 if (errorCode)
870 {
871 callback(false, ethData, ipv4Data, ipv6Data);
872 return;
873 }
874
875 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
876 if (!found)
877 {
878 callback(false, ethData, ipv4Data, ipv6Data);
879 return;
880 }
881
882 extractIPData(ethifaceId, resp, ipv4Data);
883 // Fix global GW
884 for (IPv4AddressData& ipv4 : ipv4Data)
885 {
886 if (((ipv4.linktype == LinkType::Global) &&
887 (ipv4.gateway == "0.0.0.0")) ||
888 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700889 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700890 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700891 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700892 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700893
Ed Tanous002d39b2022-05-31 08:59:27 -0700894 extractIPV6Data(ethifaceId, resp, ipv6Data);
895 // Finally make a callback with useful data
896 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700897 },
898 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
899 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700900}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700901
902/**
903 * Function that retrieves all Ethernet Interfaces available through Network
904 * Manager
905 * @param callback a function that shall be called to convert Dbus output
906 * into JSON.
907 */
908template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500909void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700910{
911 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800912 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000913 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800914 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700915 // Callback requires vector<string> to retrieve all available
916 // ethernet interfaces
917 boost::container::flat_set<std::string> ifaceList;
918 ifaceList.reserve(resp.size());
919 if (errorCode)
920 {
921 callback(false, ifaceList);
922 return;
923 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700924
Ed Tanous002d39b2022-05-31 08:59:27 -0700925 // Iterate over all retrieved ObjectPaths.
926 for (const auto& objpath : resp)
927 {
928 // And all interfaces available for certain ObjectPath.
929 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700930 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700931 // If interface is
932 // xyz.openbmc_project.Network.EthernetInterface, this is
933 // what we're looking for.
934 if (interface.first ==
935 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700936 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700937 std::string ifaceId = objpath.first.filename();
938 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700940 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700942 // and put it into output vector.
943 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700944 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700945 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700946 }
947 // Finally make a callback with useful data
948 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700949 },
950 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
951 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700952}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100953
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700954inline void
955 handleHostnamePatch(const std::string& hostname,
956 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957{
Ed Tanousbf648f72021-06-03 15:00:14 -0700958 // SHOULD handle host names of up to 255 characters(RFC 1123)
959 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700961 messages::propertyValueFormatError(asyncResp->res, hostname,
962 "HostName");
963 return;
964 }
965 crow::connections::systemBus->async_method_call(
966 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700967 if (ec)
968 {
969 messages::internalError(asyncResp->res);
970 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700971 },
972 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
973 "org.freedesktop.DBus.Properties", "Set",
974 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800975 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700976}
977
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700978inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530979 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
980 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
981{
982 sdbusplus::message::object_path objPath =
983 "/xyz/openbmc_project/network/" + ifaceId;
984 crow::connections::systemBus->async_method_call(
985 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700986 if (ec)
987 {
988 messages::internalError(asyncResp->res);
989 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530990 },
991 "xyz.openbmc_project.Network", objPath,
992 "org.freedesktop.DBus.Properties", "Set",
993 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
994 std::variant<size_t>(mtuSize));
995}
996
997inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700998 handleDomainnamePatch(const std::string& ifaceId,
999 const std::string& domainname,
1000 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001001{
1002 std::vector<std::string> vectorDomainname = {domainname};
1003 crow::connections::systemBus->async_method_call(
1004 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001005 if (ec)
1006 {
1007 messages::internalError(asyncResp->res);
1008 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001009 },
1010 "xyz.openbmc_project.Network",
1011 "/xyz/openbmc_project/network/" + ifaceId,
1012 "org.freedesktop.DBus.Properties", "Set",
1013 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -08001014 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -07001015}
1016
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001017inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001018{
1019 // A valid host name can never have the dotted-decimal form (RFC 1123)
1020 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1021 {
1022 return false;
1023 }
1024 // Each label(hostname/subdomains) within a valid FQDN
1025 // MUST handle host names of up to 63 characters (RFC 1123)
1026 // labels cannot start or end with hyphens (RFC 952)
1027 // labels can start with numbers (RFC 1123)
1028 const std::regex pattern(
1029 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1030
1031 return std::regex_match(hostname, pattern);
1032}
1033
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001034inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -07001035{
1036 // Can have multiple subdomains
1037 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +08001038 const std::regex pattern(
1039 "^([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 -07001040
1041 return std::regex_match(domainname, pattern);
1042}
1043
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001044inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1045 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001046{
1047 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1048 if (fqdn.length() > 255)
1049 {
1050 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1051 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001052 }
1053
Ed Tanousbf648f72021-06-03 15:00:14 -07001054 size_t pos = fqdn.find('.');
1055 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001056 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001057 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1058 return;
1059 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001060
Ed Tanousbf648f72021-06-03 15:00:14 -07001061 std::string hostname;
1062 std::string domainname;
1063 domainname = (fqdn).substr(pos + 1);
1064 hostname = (fqdn).substr(0, pos);
1065
1066 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1067 {
1068 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1069 return;
1070 }
1071
1072 handleHostnamePatch(hostname, asyncResp);
1073 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1074}
1075
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001076inline void
1077 handleMACAddressPatch(const std::string& ifaceId,
1078 const std::string& macAddress,
1079 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001080{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001081 static constexpr std::string_view dbusNotAllowedError =
1082 "xyz.openbmc_project.Common.Error.NotAllowed";
1083
Ed Tanousbf648f72021-06-03 15:00:14 -07001084 crow::connections::systemBus->async_method_call(
Johnathan Mantey58283f42022-08-15 14:38:36 -07001085 [asyncResp, macAddress](const boost::system::error_code ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001086 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001087 if (ec)
1088 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001089 const sd_bus_error* err = msg.get_error();
1090 if (err == nullptr)
1091 {
1092 messages::internalError(asyncResp->res);
1093 return;
1094 }
1095 if (err->name == dbusNotAllowedError)
1096 {
1097 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1098 return;
1099 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001100 messages::internalError(asyncResp->res);
1101 return;
1102 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001103 },
1104 "xyz.openbmc_project.Network",
1105 "/xyz/openbmc_project/network/" + ifaceId,
1106 "org.freedesktop.DBus.Properties", "Set",
1107 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001108 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001109}
1110
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001111inline void setDHCPEnabled(const std::string& ifaceId,
1112 const std::string& propertyName, const bool v4Value,
1113 const bool v6Value,
1114 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001115{
1116 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1117 crow::connections::systemBus->async_method_call(
1118 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001119 if (ec)
1120 {
1121 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1122 messages::internalError(asyncResp->res);
1123 return;
1124 }
1125 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001126 },
1127 "xyz.openbmc_project.Network",
1128 "/xyz/openbmc_project/network/" + ifaceId,
1129 "org.freedesktop.DBus.Properties", "Set",
1130 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001131 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001132}
1133
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001134inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001135 const std::string& ifaceId, const std::string& propertyName,
1136 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1137{
1138 crow::connections::systemBus->async_method_call(
1139 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001140 if (ec)
1141 {
1142 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1143 messages::internalError(asyncResp->res);
1144 return;
1145 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001146 },
1147 "xyz.openbmc_project.Network",
1148 "/xyz/openbmc_project/network/" + ifaceId,
1149 "org.freedesktop.DBus.Properties", "Set",
1150 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001151 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001152}
1153
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001154inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1155 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001156{
1157 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1158 crow::connections::systemBus->async_method_call(
1159 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001160 if (ec)
1161 {
1162 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1163 messages::internalError(asyncResp->res);
1164 return;
1165 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001166 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001167 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001168 "org.freedesktop.DBus.Properties", "Set",
1169 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001170 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001171}
1172
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001173inline void handleDHCPPatch(const std::string& ifaceId,
1174 const EthernetInterfaceData& ethData,
1175 const DHCPParameters& v4dhcpParms,
1176 const DHCPParameters& v6dhcpParms,
1177 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001178{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001179 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1180 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001181
1182 bool nextv4DHCPState =
1183 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1184
1185 bool nextv6DHCPState{};
1186 if (v6dhcpParms.dhcpv6OperatingMode)
1187 {
1188 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1189 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1190 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1191 {
1192 messages::propertyValueFormatError(asyncResp->res,
1193 *v6dhcpParms.dhcpv6OperatingMode,
1194 "OperatingMode");
1195 return;
1196 }
1197 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1198 }
1199 else
1200 {
1201 nextv6DHCPState = ipv6Active;
1202 }
1203
1204 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001205 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001206 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001207 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001208 {
1209 messages::generalError(asyncResp->res);
1210 return;
1211 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001212 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001213 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001214 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001215 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001216 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001217 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001218 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001219 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001220 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001221 }
1222 else
1223 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001224 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001225 }
1226
1227 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001228 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001229 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001230 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001231 {
1232 messages::generalError(asyncResp->res);
1233 return;
1234 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001235 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001236 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001237 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001238 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001239 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001240 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001241 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001242 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001243 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001244 }
1245 else
1246 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001247 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001248 }
1249
1250 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001251 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001252 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001253 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001254 {
1255 messages::generalError(asyncResp->res);
1256 return;
1257 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001258 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001259 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001260 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001261 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001262 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001263 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001264 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001265 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001266 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001267 }
1268 else
1269 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001270 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001271 }
1272
1273 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1274 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1275 asyncResp);
1276 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1277 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1278 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1279 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1280 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1281 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1282}
1283
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001284inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001285 getNextStaticIpEntry(
1286 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1287 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1288{
1289 return std::find_if(head, end, [](const IPv4AddressData& value) {
1290 return value.origin == "Static";
1291 });
1292}
1293
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001294inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001295 getNextStaticIpEntry(
1296 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1297 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1298{
1299 return std::find_if(head, end, [](const IPv6AddressData& value) {
1300 return value.origin == "Static";
1301 });
1302}
1303
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001304inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001305 const std::string& ifaceId, nlohmann::json& input,
1306 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1307 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1308{
1309 if ((!input.is_array()) || input.empty())
1310 {
1311 messages::propertyValueTypeError(
1312 asyncResp->res,
1313 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1314 "IPv4StaticAddresses");
1315 return;
1316 }
1317
1318 unsigned entryIdx = 1;
1319 // Find the first static IP address currently active on the NIC and
1320 // match it to the first JSON element in the IPv4StaticAddresses array.
1321 // Match each subsequent JSON element to the next static IP programmed
1322 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001323 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001324 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1325
1326 for (nlohmann::json& thisJson : input)
1327 {
1328 std::string pathString =
1329 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1330
1331 if (!thisJson.is_null() && !thisJson.empty())
1332 {
1333 std::optional<std::string> address;
1334 std::optional<std::string> subnetMask;
1335 std::optional<std::string> gateway;
1336
1337 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1338 address, "SubnetMask", subnetMask,
1339 "Gateway", gateway))
1340 {
1341 messages::propertyValueFormatError(
1342 asyncResp->res,
1343 thisJson.dump(2, ' ', true,
1344 nlohmann::json::error_handler_t::replace),
1345 pathString);
1346 return;
1347 }
1348
1349 // Find the address/subnet/gateway values. Any values that are
1350 // not explicitly provided are assumed to be unmodified from the
1351 // current state of the interface. Merge existing state into the
1352 // current request.
1353 const std::string* addr = nullptr;
1354 const std::string* gw = nullptr;
1355 uint8_t prefixLength = 0;
1356 bool errorInEntry = false;
1357 if (address)
1358 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001359 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001360 {
1361 addr = &(*address);
1362 }
1363 else
1364 {
1365 messages::propertyValueFormatError(asyncResp->res, *address,
1366 pathString + "/Address");
1367 errorInEntry = true;
1368 }
1369 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001370 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001371 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001372 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001373 }
1374 else
1375 {
1376 messages::propertyMissing(asyncResp->res,
1377 pathString + "/Address");
1378 errorInEntry = true;
1379 }
1380
1381 if (subnetMask)
1382 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001383 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1384 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001385 {
1386 messages::propertyValueFormatError(
1387 asyncResp->res, *subnetMask,
1388 pathString + "/SubnetMask");
1389 errorInEntry = true;
1390 }
1391 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001392 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001393 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001394 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1395 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001396 {
1397 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001398 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001399 pathString + "/SubnetMask");
1400 errorInEntry = true;
1401 }
1402 }
1403 else
1404 {
1405 messages::propertyMissing(asyncResp->res,
1406 pathString + "/SubnetMask");
1407 errorInEntry = true;
1408 }
1409
1410 if (gateway)
1411 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001412 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001413 {
1414 gw = &(*gateway);
1415 }
1416 else
1417 {
1418 messages::propertyValueFormatError(asyncResp->res, *gateway,
1419 pathString + "/Gateway");
1420 errorInEntry = true;
1421 }
1422 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001423 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001424 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001425 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001426 }
1427 else
1428 {
1429 messages::propertyMissing(asyncResp->res,
1430 pathString + "/Gateway");
1431 errorInEntry = true;
1432 }
1433
1434 if (errorInEntry)
1435 {
1436 return;
1437 }
1438
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001439 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001440 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001441 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001442 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001443 nicIpEntry =
1444 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001445 }
1446 else
1447 {
1448 createIPv4(ifaceId, prefixLength, *gateway, *address,
1449 asyncResp);
1450 }
1451 entryIdx++;
1452 }
1453 else
1454 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001455 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 {
1457 // Requesting a DELETE/DO NOT MODIFY action for an item
1458 // that isn't present on the eth(n) interface. Input JSON is
1459 // in error, so bail out.
1460 if (thisJson.is_null())
1461 {
1462 messages::resourceCannotBeDeleted(asyncResp->res);
1463 return;
1464 }
1465 messages::propertyValueFormatError(
1466 asyncResp->res,
1467 thisJson.dump(2, ' ', true,
1468 nlohmann::json::error_handler_t::replace),
1469 pathString);
1470 return;
1471 }
1472
1473 if (thisJson.is_null())
1474 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001475 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001476 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001477 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001478 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001479 nicIpEntry =
1480 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001481 }
1482 entryIdx++;
1483 }
1484 }
1485}
1486
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001487inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001488 const std::string& ifaceId,
1489 const std::vector<std::string>& updatedStaticNameServers,
1490 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1491{
1492 crow::connections::systemBus->async_method_call(
1493 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001494 if (ec)
1495 {
1496 messages::internalError(asyncResp->res);
1497 return;
1498 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001499 },
1500 "xyz.openbmc_project.Network",
1501 "/xyz/openbmc_project/network/" + ifaceId,
1502 "org.freedesktop.DBus.Properties", "Set",
1503 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001504 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001505}
1506
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001507inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001508 const std::string& ifaceId, const nlohmann::json& input,
1509 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1510 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1511{
1512 if (!input.is_array() || input.empty())
1513 {
1514 messages::propertyValueTypeError(
1515 asyncResp->res,
1516 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1517 "IPv6StaticAddresses");
1518 return;
1519 }
1520 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001521 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001522 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1523 for (const nlohmann::json& thisJson : input)
1524 {
1525 std::string pathString =
1526 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1527
1528 if (!thisJson.is_null() && !thisJson.empty())
1529 {
1530 std::optional<std::string> address;
1531 std::optional<uint8_t> prefixLength;
1532 nlohmann::json thisJsonCopy = thisJson;
1533 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1534 address, "PrefixLength", prefixLength))
1535 {
1536 messages::propertyValueFormatError(
1537 asyncResp->res,
1538 thisJson.dump(2, ' ', true,
1539 nlohmann::json::error_handler_t::replace),
1540 pathString);
1541 return;
1542 }
1543
Ed Tanous543f4402022-01-06 13:12:53 -08001544 const std::string* addr = nullptr;
1545 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001546
1547 // Find the address and prefixLength values. Any values that are
1548 // not explicitly provided are assumed to be unmodified from the
1549 // current state of the interface. Merge existing state into the
1550 // current request.
1551 if (address)
1552 {
1553 addr = &(*address);
1554 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001555 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001556 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001557 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001558 }
1559 else
1560 {
1561 messages::propertyMissing(asyncResp->res,
1562 pathString + "/Address");
1563 return;
1564 }
1565
1566 if (prefixLength)
1567 {
1568 prefix = *prefixLength;
1569 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001570 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001571 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001572 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001573 }
1574 else
1575 {
1576 messages::propertyMissing(asyncResp->res,
1577 pathString + "/PrefixLength");
1578 return;
1579 }
1580
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001581 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001582 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001583 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001584 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001585 nicIpEntry =
1586 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001587 }
1588 else
1589 {
1590 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1591 }
1592 entryIdx++;
1593 }
1594 else
1595 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001596 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001597 {
1598 // Requesting a DELETE/DO NOT MODIFY action for an item
1599 // that isn't present on the eth(n) interface. Input JSON is
1600 // in error, so bail out.
1601 if (thisJson.is_null())
1602 {
1603 messages::resourceCannotBeDeleted(asyncResp->res);
1604 return;
1605 }
1606 messages::propertyValueFormatError(
1607 asyncResp->res,
1608 thisJson.dump(2, ' ', true,
1609 nlohmann::json::error_handler_t::replace),
1610 pathString);
1611 return;
1612 }
1613
1614 if (thisJson.is_null())
1615 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001616 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001618 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001619 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001620 nicIpEntry =
1621 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001622 }
1623 entryIdx++;
1624 }
1625 }
1626}
1627
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001628inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001629 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1630 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1631 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1632 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1633{
George Liu7a1dbc42022-12-07 16:03:22 +08001634 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1636
1637 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1638 jsonResponse["Id"] = ifaceId;
Willy Tueddfc432022-09-26 16:46:38 +00001639 jsonResponse["@odata.id"] = crow::utility::urlFromPieces(
1640 "redfish", "v1", "Managers", "bmc", "EthernetInterfaces", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001641 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1642
1643 auto health = std::make_shared<HealthPopulate>(asyncResp);
1644
George Liu7a1dbc42022-12-07 16:03:22 +08001645 dbus::utility::getSubTreePaths(
1646 "/", 0, inventoryForEthernet,
1647 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001648 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001649 if (ec)
1650 {
1651 return;
1652 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001653
Ed Tanous002d39b2022-05-31 08:59:27 -07001654 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001655 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001656
1657 health->populate();
1658
1659 if (ethData.nicEnabled)
1660 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001661 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001662 jsonResponse["Status"]["State"] = "Enabled";
1663 }
1664 else
1665 {
1666 jsonResponse["LinkStatus"] = "NoLink";
1667 jsonResponse["Status"]["State"] = "Disabled";
1668 }
1669
Ed Tanousbf648f72021-06-03 15:00:14 -07001670 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301671 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001672 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001673 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001674 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1675 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1676 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1677 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001678
1679 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001680 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001681 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001682 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1683 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1684 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001685
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001686 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001687 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001688 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001689
1690 // When domain name is empty then it means, that it is a network
1691 // without domain names, and the host name itself must be treated as
1692 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001693 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001694 if (!ethData.domainnames.empty())
1695 {
1696 fqdn += "." + ethData.domainnames[0];
1697 }
1698 jsonResponse["FQDN"] = fqdn;
1699 }
1700
Ed Tanous613dabe2022-07-09 11:17:36 -07001701 jsonResponse["VLANs"]["@odata.id"] =
1702 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1703 "EthernetInterfaces", ifaceId, "VLANs");
Ed Tanousbf648f72021-06-03 15:00:14 -07001704
1705 jsonResponse["NameServers"] = ethData.nameServers;
1706 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1707
1708 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1709 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1710 ipv4Array = nlohmann::json::array();
1711 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001712 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001713 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001714 std::string gatewayStr = ipv4Config.gateway;
1715 if (gatewayStr.empty())
1716 {
1717 gatewayStr = "0.0.0.0";
1718 }
Ed Tanous14766872022-03-15 10:44:42 -07001719 nlohmann::json::object_t ipv4;
1720 ipv4["AddressOrigin"] = ipv4Config.origin;
1721 ipv4["SubnetMask"] = ipv4Config.netmask;
1722 ipv4["Address"] = ipv4Config.address;
1723 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001724
Ed Tanousbf648f72021-06-03 15:00:14 -07001725 if (ipv4Config.origin == "Static")
1726 {
Ed Tanous14766872022-03-15 10:44:42 -07001727 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001728 }
Ed Tanous14766872022-03-15 10:44:42 -07001729
1730 ipv4Array.push_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001731 }
1732
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001733 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001734 if (ipv6GatewayStr.empty())
1735 {
1736 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1737 }
1738
1739 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1740
1741 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1742 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1743 ipv6Array = nlohmann::json::array();
1744 ipv6StaticArray = nlohmann::json::array();
1745 nlohmann::json& ipv6AddrPolicyTable =
1746 jsonResponse["IPv6AddressPolicyTable"];
1747 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001748 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001749 {
Ed Tanous14766872022-03-15 10:44:42 -07001750 nlohmann::json::object_t ipv6;
1751 ipv6["Address"] = ipv6Config.address;
1752 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1753 ipv6["AddressOrigin"] = ipv6Config.origin;
1754 ipv6["AddressState"] = nullptr;
1755 ipv6Array.push_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001756 if (ipv6Config.origin == "Static")
1757 {
Ed Tanous14766872022-03-15 10:44:42 -07001758 nlohmann::json::object_t ipv6Static;
1759 ipv6Static["Address"] = ipv6Config.address;
1760 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1761 ipv6StaticArray.push_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001762 }
1763 }
1764}
1765
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001766inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001767{
Ed Tanous11ba3972022-07-11 09:50:41 -07001768 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001769}
1770
1771inline void requestEthernetInterfacesRoutes(App& app)
1772{
1773 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001774 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001775 .methods(boost::beast::http::verb::get)(
1776 [&app](const crow::Request& req,
1777 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001778 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 {
1780 return;
1781 }
1782
1783 asyncResp->res.jsonValue["@odata.type"] =
1784 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1785 asyncResp->res.jsonValue["@odata.id"] =
1786 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1787 asyncResp->res.jsonValue["Name"] =
1788 "Ethernet Network Interface Collection";
1789 asyncResp->res.jsonValue["Description"] =
1790 "Collection of EthernetInterfaces for this Manager";
1791
1792 // Get eth interface list, and call the below callback for JSON
1793 // preparation
1794 getEthernetIfaceList(
1795 [asyncResp](
1796 const bool& success,
1797 const boost::container::flat_set<std::string>& ifaceList) {
1798 if (!success)
1799 {
1800 messages::internalError(asyncResp->res);
1801 return;
1802 }
1803
1804 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1805 ifaceArray = nlohmann::json::array();
1806 std::string tag = "_";
1807 for (const std::string& ifaceItem : ifaceList)
1808 {
1809 std::size_t found = ifaceItem.find(tag);
1810 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001811 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001812 nlohmann::json::object_t iface;
Willy Tueddfc432022-09-26 16:46:38 +00001813 iface["@odata.id"] = crow::utility::urlFromPieces(
1814 "redfish", "v1", "Managers", "bmc",
1815 "EthernetInterfaces", ifaceItem);
Ed Tanous002d39b2022-05-31 08:59:27 -07001816 ifaceArray.push_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001817 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001818 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001819
Ed Tanous002d39b2022-05-31 08:59:27 -07001820 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1821 asyncResp->res.jsonValue["@odata.id"] =
1822 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1823 });
1824 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001825
Ed Tanousbf648f72021-06-03 15:00:14 -07001826 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001827 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001828 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001829 [&app](const crow::Request& req,
1830 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1831 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001832 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001833 {
1834 return;
1835 }
1836 getEthernetIfaceData(
1837 ifaceId,
1838 [asyncResp, ifaceId](
1839 const bool& success, const EthernetInterfaceData& ethData,
1840 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1841 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1842 if (!success)
1843 {
1844 // TODO(Pawel)consider distinguish between non
1845 // existing object, and other errors
1846 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1847 ifaceId);
1848 return;
1849 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001850
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001851 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1852 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001853 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001854 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1856 asyncResp->res.jsonValue["Description"] =
1857 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301858
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001860 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001861 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001862
Ed Tanousbf648f72021-06-03 15:00:14 -07001863 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001864 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001865 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001866 [&app](const crow::Request& req,
1867 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1868 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001869 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 {
1871 return;
1872 }
1873 std::optional<std::string> hostname;
1874 std::optional<std::string> fqdn;
1875 std::optional<std::string> macAddress;
1876 std::optional<std::string> ipv6DefaultGateway;
1877 std::optional<nlohmann::json> ipv4StaticAddresses;
1878 std::optional<nlohmann::json> ipv6StaticAddresses;
1879 std::optional<std::vector<std::string>> staticNameServers;
1880 std::optional<nlohmann::json> dhcpv4;
1881 std::optional<nlohmann::json> dhcpv6;
1882 std::optional<bool> interfaceEnabled;
1883 std::optional<size_t> mtuSize;
1884 DHCPParameters v4dhcpParms;
1885 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001886
Ed Tanous002d39b2022-05-31 08:59:27 -07001887 if (!json_util::readJsonPatch(
1888 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1889 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1890 macAddress, "StaticNameServers", staticNameServers,
1891 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1892 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1893 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1894 {
1895 return;
1896 }
1897 if (dhcpv4)
1898 {
1899 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1900 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1901 v4dhcpParms.useDnsServers, "UseNTPServers",
1902 v4dhcpParms.useNtpServers, "UseDomainName",
1903 v4dhcpParms.useDomainName))
1904 {
1905 return;
1906 }
1907 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001908
Ed Tanous002d39b2022-05-31 08:59:27 -07001909 if (dhcpv6)
1910 {
1911 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1912 v6dhcpParms.dhcpv6OperatingMode,
1913 "UseDNSServers", v6dhcpParms.useDnsServers,
1914 "UseNTPServers", v6dhcpParms.useNtpServers,
1915 "UseDomainName",
1916 v6dhcpParms.useDomainName))
1917 {
1918 return;
1919 }
1920 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001921
Ed Tanous002d39b2022-05-31 08:59:27 -07001922 // Get single eth interface data, and call the below callback
1923 // for JSON preparation
1924 getEthernetIfaceData(
1925 ifaceId,
1926 [asyncResp, ifaceId, hostname = std::move(hostname),
1927 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1928 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1929 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1930 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1931 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001932 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1933 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001934 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1935 const bool& success, const EthernetInterfaceData& ethData,
1936 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1937 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1938 if (!success)
1939 {
1940 // ... otherwise return error
1941 // TODO(Pawel)consider distinguish between non
1942 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001943 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001944 ifaceId);
1945 return;
1946 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001947
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 if (dhcpv4 || dhcpv6)
1949 {
1950 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001951 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001952 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301953
Ed Tanous002d39b2022-05-31 08:59:27 -07001954 if (hostname)
1955 {
1956 handleHostnamePatch(*hostname, asyncResp);
1957 }
1958
1959 if (fqdn)
1960 {
1961 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1962 }
1963
1964 if (macAddress)
1965 {
1966 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1967 }
1968
1969 if (ipv4StaticAddresses)
1970 {
1971 // TODO(ed) for some reason the capture of
1972 // ipv4Addresses above is returning a const value,
1973 // not a non-const value. This doesn't really work
1974 // for us, as we need to be able to efficiently move
1975 // out the intermedia nlohmann::json objects. This
1976 // makes a copy of the structure, and operates on
1977 // that, but could be done more efficiently
1978 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1979 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1980 }
1981
1982 if (staticNameServers)
1983 {
1984 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1985 asyncResp);
1986 }
1987
1988 if (ipv6DefaultGateway)
1989 {
1990 messages::propertyNotWritable(asyncResp->res,
1991 "IPv6DefaultGateway");
1992 }
1993
1994 if (ipv6StaticAddresses)
1995 {
1996 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1997 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1998 asyncResp);
1999 }
2000
2001 if (interfaceEnabled)
2002 {
2003 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2004 *interfaceEnabled, asyncResp);
2005 }
2006
2007 if (mtuSize)
2008 {
2009 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
2010 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002011 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002012 });
manojkiraneda2a133282019-02-19 13:09:43 +05302013
Ed Tanousbf648f72021-06-03 15:00:14 -07002014 BMCWEB_ROUTE(
2015 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07002016 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002017 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002018 [&app](const crow::Request& req,
2019 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2020 const std::string& parentIfaceId,
2021 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002022 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002023 {
2024 return;
2025 }
2026 asyncResp->res.jsonValue["@odata.type"] =
2027 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2028 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002029
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 if (!verifyNames(parentIfaceId, ifaceId))
2031 {
2032 return;
2033 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002034
Ed Tanous002d39b2022-05-31 08:59:27 -07002035 // Get single eth interface data, and call the below callback
2036 // for JSON preparation
2037 getEthernetIfaceData(
2038 ifaceId,
2039 [asyncResp, parentIfaceId,
2040 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2041 const boost::container::flat_set<IPv4AddressData>&,
2042 const boost::container::flat_set<IPv6AddressData>&) {
2043 if (success && ethData.vlanId)
2044 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002045 asyncResp->res.jsonValue["Id"] = ifaceId;
2046 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002047 crow::utility::urlFromPieces(
2048 "redfish", "v1", "Managers", "bmc",
2049 "EthernetInterfaces", parentIfaceId, "VLANs", ifaceId);
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002050
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002051 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002052 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002053 }
2054 else
2055 {
2056 // ... otherwise return error
2057 // TODO(Pawel)consider distinguish between non
2058 // existing object, and other errors
2059 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002060 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002061 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002062 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002063 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002064
Ed Tanousbf648f72021-06-03 15:00:14 -07002065 BMCWEB_ROUTE(
2066 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002067 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002068 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002069 [&app](const crow::Request& req,
2070 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2071 const std::string& parentIfaceId,
2072 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002073 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002074 {
2075 return;
2076 }
2077 if (!verifyNames(parentIfaceId, ifaceId))
2078 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002079 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 ifaceId);
2081 return;
2082 }
2083
2084 std::optional<bool> vlanEnable;
2085 std::optional<uint32_t> vlanId;
2086
2087 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2088 vlanEnable, "VLANId", vlanId))
2089 {
2090 return;
2091 }
2092
2093 if (vlanId)
2094 {
2095 messages::propertyNotWritable(asyncResp->res, "VLANId");
2096 return;
2097 }
2098
2099 // Get single eth interface data, and call the below callback
2100 // for JSON preparation
2101 getEthernetIfaceData(
2102 ifaceId,
2103 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2104 const bool& success, const EthernetInterfaceData& ethData,
2105 const boost::container::flat_set<IPv4AddressData>&,
2106 const boost::container::flat_set<IPv6AddressData>&) {
2107 if (success && ethData.vlanId)
2108 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002109 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002110 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002111 crow::connections::systemBus->async_method_call(
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002112 [asyncResp](const boost::system::error_code ec) {
2113 if (ec)
2114 {
2115 messages::internalError(asyncResp->res);
2116 return;
2117 }
2118 },
2119 "xyz.openbmc_project.Network",
2120 "/xyz/openbmc_project/network/" + ifaceId,
2121 "org.freedesktop.DBus.Properties", "Set",
2122 "xyz.openbmc_project.Network.EthernetInterface",
2123 "NICEnabled",
2124 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002125 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002126 }
2127 else
2128 {
2129 // TODO(Pawel)consider distinguish between non
2130 // existing object, and other errors
2131 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002132 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002133 return;
2134 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002135 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002137
2138 BMCWEB_ROUTE(
2139 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002140 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002141 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002142 [&app](const crow::Request& req,
2143 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2144 const std::string& parentIfaceId,
2145 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002146 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 {
2148 return;
2149 }
2150 if (!verifyNames(parentIfaceId, ifaceId))
2151 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002152 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002153 ifaceId);
2154 return;
2155 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002156
Ed Tanous002d39b2022-05-31 08:59:27 -07002157 // Get single eth interface data, and call the below callback
2158 // for JSON preparation
2159 getEthernetIfaceData(
2160 ifaceId,
2161 [asyncResp, parentIfaceId,
2162 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2163 const boost::container::flat_set<IPv4AddressData>&,
2164 const boost::container::flat_set<IPv6AddressData>&) {
2165 if (success && ethData.vlanId)
2166 {
2167 auto callback =
2168 [asyncResp](const boost::system::error_code ec) {
2169 if (ec)
2170 {
2171 messages::internalError(asyncResp->res);
2172 }
2173 };
2174 crow::connections::systemBus->async_method_call(
2175 std::move(callback), "xyz.openbmc_project.Network",
2176 std::string("/xyz/openbmc_project/network/") + ifaceId,
2177 "xyz.openbmc_project.Object.Delete", "Delete");
2178 }
2179 else
2180 {
2181 // ... otherwise return error
2182 // TODO(Pawel)consider distinguish between non
2183 // existing object, and other errors
2184 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002185 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002186 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002187 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002188 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002189
Ed Tanousbf648f72021-06-03 15:00:14 -07002190 BMCWEB_ROUTE(app,
2191 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002192
2193 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002194 .methods(boost::beast::http::verb::get)(
2195 [&app](const crow::Request& req,
2196 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2197 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002198 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002199 {
2200 return;
2201 }
2202 // Get eth interface list, and call the below callback for JSON
2203 // preparation
2204 getEthernetIfaceList(
2205 [asyncResp, rootInterfaceName](
2206 const bool& success,
2207 const boost::container::flat_set<std::string>& ifaceList) {
2208 if (!success)
2209 {
2210 messages::internalError(asyncResp->res);
2211 return;
2212 }
2213
2214 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2215 {
2216 messages::resourceNotFound(asyncResp->res,
2217 "VLanNetworkInterfaceCollection",
2218 rootInterfaceName);
2219 return;
2220 }
2221
2222 asyncResp->res.jsonValue["@odata.type"] =
2223 "#VLanNetworkInterfaceCollection."
2224 "VLanNetworkInterfaceCollection";
2225 asyncResp->res.jsonValue["Name"] =
2226 "VLAN Network Interface Collection";
2227
2228 nlohmann::json ifaceArray = nlohmann::json::array();
2229
2230 for (const std::string& ifaceItem : ifaceList)
2231 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002232 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002233 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002234 nlohmann::json::object_t iface;
Willy Tueddfc432022-09-26 16:46:38 +00002235 iface["@odata.id"] = crow::utility::urlFromPieces(
2236 "redfish", "v1", "Managers", "bmc",
2237 "EthernetInterfaces", rootInterfaceName, "VLANs",
2238 ifaceItem);
Ed Tanous002d39b2022-05-31 08:59:27 -07002239 ifaceArray.push_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002240 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002241 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002242
Ed Tanous002d39b2022-05-31 08:59:27 -07002243 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2244 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2245 asyncResp->res.jsonValue["@odata.id"] =
Willy Tueddfc432022-09-26 16:46:38 +00002246 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
2247 "EthernetInterfaces",
2248 rootInterfaceName, "VLANs");
Ed Tanous002d39b2022-05-31 08:59:27 -07002249 });
2250 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002251
Ed Tanousbf648f72021-06-03 15:00:14 -07002252 BMCWEB_ROUTE(app,
2253 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002254 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002255 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002256 [&app](const crow::Request& req,
2257 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2258 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002259 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002260 {
2261 return;
2262 }
2263 bool vlanEnable = false;
2264 uint32_t vlanId = 0;
2265 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2266 "VLANEnable", vlanEnable))
2267 {
2268 return;
2269 }
2270 // Need both vlanId and vlanEnable to service this request
2271 if (vlanId == 0U)
2272 {
2273 messages::propertyMissing(asyncResp->res, "VLANId");
2274 }
2275 if (!vlanEnable)
2276 {
2277 messages::propertyMissing(asyncResp->res, "VLANEnable");
2278 }
2279 if (static_cast<bool>(vlanId) ^ vlanEnable)
2280 {
2281 return;
2282 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002283
Ed Tanous002d39b2022-05-31 08:59:27 -07002284 auto callback = [asyncResp](const boost::system::error_code ec) {
2285 if (ec)
2286 {
2287 // TODO(ed) make more consistent error messages
2288 // based on phosphor-network responses
2289 messages::internalError(asyncResp->res);
2290 return;
2291 }
2292 messages::created(asyncResp->res);
2293 };
2294 crow::connections::systemBus->async_method_call(
2295 std::move(callback), "xyz.openbmc_project.Network",
2296 "/xyz/openbmc_project/network",
2297 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2298 rootInterfaceName, vlanId);
2299 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002300}
2301
Ed Tanous1abe55e2018-09-05 08:30:59 -07002302} // namespace redfish