blob: 5c928d61acebf9add1c91c7d6005f7b66143026c [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 Tanousef4c65b2023-04-24 15:28:50 -070030#include <boost/url/format.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>
Ed Tanous77179532023-02-28 10:45:28 -080036#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace redfish
39{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010040
Ed Tanous4a0cb852018-10-15 07:55:04 -070041enum class LinkType
42{
43 Local,
44 Global
45};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010046
47/**
48 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010049 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070050struct IPv4AddressData
51{
52 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070053 std::string address;
54 std::string domain;
55 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070056 std::string netmask;
57 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080058 LinkType linktype{};
59 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010060};
61
62/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050063 * Structure for keeping IPv6 data required by Redfish
64 */
65struct IPv6AddressData
66{
67 std::string id;
68 std::string address;
69 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080070 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050071};
72/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010073 * Structure for keeping basic single Ethernet Interface information
74 * available from DBus
75 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070076struct EthernetInterfaceData
77{
Ed Tanous4a0cb852018-10-15 07:55:04 -070078 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053079 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080080 bool autoNeg;
81 bool dnsEnabled;
82 bool ntpEnabled;
83 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080084 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070085 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080086 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070087 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080088 std::string hostName;
89 std::string defaultGateway;
90 std::string ipv6DefaultGateway;
91 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +080092 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050093 std::vector<std::string> nameServers;
94 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080095 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010096};
97
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070098struct DHCPParameters
99{
100 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800101 std::optional<bool> useDnsServers;
102 std::optional<bool> useNtpServers;
103 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700104 std::optional<std::string> dhcpv6OperatingMode;
105};
106
Ed Tanous4a0cb852018-10-15 07:55:04 -0700107// Helper function that changes bits netmask notation (i.e. /24)
108// into full dot notation
109inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700111 uint32_t value = 0xffffffff << (32 - bits);
112 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
113 std::to_string((value >> 16) & 0xff) + "." +
114 std::to_string((value >> 8) & 0xff) + "." +
115 std::to_string(value & 0xff);
116 return netmask;
117}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100118
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800119inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700120 bool isIPv4)
121{
122 if (isIPv4)
123 {
124 return (
125 (inputDHCP ==
126 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
127 (inputDHCP ==
128 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
129 }
130 return ((inputDHCP ==
131 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
132 (inputDHCP ==
133 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
134}
135
Ed Tanous2c70f802020-09-28 14:29:23 -0700136inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700137{
138 if (isIPv4 && isIPv6)
139 {
140 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
141 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700142 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700143 {
144 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
145 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700146 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700147 {
148 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
149 }
150 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
151}
152
Ed Tanous4a0cb852018-10-15 07:55:04 -0700153inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500154 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700155 bool isIPv4)
156{
157 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700159 return "Static";
160 }
161 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
162 {
163 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700165 return "IPv4LinkLocal";
166 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700167 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700168 }
169 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
170 {
171 if (isIPv4)
172 {
173 return "DHCP";
174 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700175 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700176 }
177 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
178 {
179 return "SLAAC";
180 }
181 return "";
182}
183
Ed Tanous02cad962022-06-30 16:50:15 -0700184inline bool extractEthernetInterfaceData(
185 const std::string& ethifaceId,
186 const dbus::utility::ManagedObjectType& dbusData,
187 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700188{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700189 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700190 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700191 {
Ed Tanous02cad962022-06-30 16:50:15 -0700192 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700193 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000194 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700196 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500199 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700201 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500203 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800204 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700205 if (mac != nullptr)
206 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800207 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700210 }
211 }
212 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
213 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500214 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 {
216 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500218 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800219 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 if (id != nullptr)
221 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800222 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 }
226 }
227 else if (ifacePair.first ==
228 "xyz.openbmc_project.Network.EthernetInterface")
229 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500230 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 {
232 if (propertyPair.first == "AutoNeg")
233 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700234 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800235 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700236 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700237 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800238 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 }
240 }
241 else if (propertyPair.first == "Speed")
242 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500243 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800244 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700245 if (speed != nullptr)
246 {
247 ethData.speed = *speed;
248 }
249 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530250 else if (propertyPair.first == "MTU")
251 {
252 const uint32_t* mtuSize =
253 std::get_if<uint32_t>(&propertyPair.second);
254 if (mtuSize != nullptr)
255 {
256 ethData.mtuSize = *mtuSize;
257 }
258 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800259 else if (propertyPair.first == "LinkUp")
260 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500261 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800262 std::get_if<bool>(&propertyPair.second);
263 if (linkUp != nullptr)
264 {
265 ethData.linkUp = *linkUp;
266 }
267 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700268 else if (propertyPair.first == "NICEnabled")
269 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700271 std::get_if<bool>(&propertyPair.second);
272 if (nicEnabled != nullptr)
273 {
274 ethData.nicEnabled = *nicEnabled;
275 }
276 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500277 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700278 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500279 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500280 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800281 &propertyPair.second);
282 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700283 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700284 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500285 }
286 }
287 else if (propertyPair.first == "StaticNameServers")
288 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500289 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500290 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500291 &propertyPair.second);
292 if (staticNameServers != nullptr)
293 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700294 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700295 }
296 }
manojkiraneda2a133282019-02-19 13:09:43 +0530297 else if (propertyPair.first == "DHCPEnabled")
298 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700299 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700300 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700301 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530302 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800303 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530304 }
305 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800306 else if (propertyPair.first == "DomainName")
307 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500308 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500309 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800310 &propertyPair.second);
311 if (domainNames != nullptr)
312 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700313 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800314 }
315 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500316 else if (propertyPair.first == "DefaultGateway")
317 {
318 const std::string* defaultGateway =
319 std::get_if<std::string>(&propertyPair.second);
320 if (defaultGateway != nullptr)
321 {
322 std::string defaultGatewayStr = *defaultGateway;
323 if (defaultGatewayStr.empty())
324 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800325 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500326 }
327 else
328 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800329 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500330 }
331 }
332 }
333 else if (propertyPair.first == "DefaultGateway6")
334 {
335 const std::string* defaultGateway6 =
336 std::get_if<std::string>(&propertyPair.second);
337 if (defaultGateway6 != nullptr)
338 {
339 std::string defaultGateway6Str =
340 *defaultGateway6;
341 if (defaultGateway6Str.empty())
342 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800343 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500344 "0:0:0:0:0:0:0:0";
345 }
346 else
347 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800348 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500349 defaultGateway6Str;
350 }
351 }
352 }
Ed Tanous029573d2019-02-01 10:57:49 -0800353 }
354 }
355 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700356
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800357 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700358 {
359 if (ifacePair.first ==
360 "xyz.openbmc_project.Network.DHCPConfiguration")
361 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500362 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700363 {
364 if (propertyPair.first == "DNSEnabled")
365 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700366 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700367 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700368 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700369 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800370 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700371 }
372 }
373 else if (propertyPair.first == "NTPEnabled")
374 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700375 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700376 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700377 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700378 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800379 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700380 }
381 }
382 else if (propertyPair.first == "HostNameEnabled")
383 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700384 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700385 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700386 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700387 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800388 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700389 }
390 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700391 }
392 }
393 }
Ed Tanous029573d2019-02-01 10:57:49 -0800394 // System configuration shows up in the global namespace, so no need
395 // to check eth number
396 if (ifacePair.first ==
397 "xyz.openbmc_project.Network.SystemConfiguration")
398 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500399 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800400 {
401 if (propertyPair.first == "HostName")
402 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500403 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500404 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800405 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700406 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800407 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800408 }
409 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
411 }
412 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700413 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700414 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700415}
416
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500417// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800418inline void extractIPV6Data(const std::string& ethifaceId,
419 const dbus::utility::ManagedObjectType& dbusData,
420 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500421{
Patrick Williams89492a12023-05-10 07:51:34 -0500422 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
423 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500424
425 // Since there might be several IPv6 configurations aligned with
426 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500428 {
429 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800430 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500431 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800432 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500433 {
434 if (interface.first == "xyz.openbmc_project.Network.IP")
435 {
Tony Lee353163e2022-11-23 11:06:10 +0800436 auto type = std::find_if(interface.second.begin(),
437 interface.second.end(),
438 [](const auto& property) {
439 return property.first == "Type";
440 });
441 if (type == interface.second.end())
442 {
443 continue;
444 }
445
446 const std::string* typeStr =
447 std::get_if<std::string>(&type->second);
448
449 if (typeStr == nullptr ||
450 (*typeStr !=
451 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
452 {
453 continue;
454 }
455
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500456 // Instance IPv6AddressData structure, and set as
457 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800458 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700459 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800460 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800461 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500462 {
463 if (property.first == "Address")
464 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500465 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 std::get_if<std::string>(&property.second);
467 if (address != nullptr)
468 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700469 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500470 }
471 }
472 else if (property.first == "Origin")
473 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500474 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500475 std::get_if<std::string>(&property.second);
476 if (origin != nullptr)
477 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700478 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500479 translateAddressOriginDbusToRedfish(*origin,
480 false);
481 }
482 }
483 else if (property.first == "PrefixLength")
484 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500485 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500486 std::get_if<uint8_t>(&property.second);
487 if (prefix != nullptr)
488 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700489 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 }
491 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600492 else if (property.first == "Type" ||
493 property.first == "Gateway")
494 {
495 // Type & Gateway is not used
496 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500497 else
498 {
499 BMCWEB_LOG_ERROR
500 << "Got extra property: " << property.first
501 << " on the " << objpath.first.str << " object";
502 }
503 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500504 }
505 }
506 }
507 }
508}
509
Ed Tanous4a0cb852018-10-15 07:55:04 -0700510// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800511inline void extractIPData(const std::string& ethifaceId,
512 const dbus::utility::ManagedObjectType& dbusData,
513 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514{
Patrick Williams89492a12023-05-10 07:51:34 -0500515 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
516 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700517
518 // Since there might be several IPv4 configurations aligned with
519 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000520 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700521 {
522 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800523 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700524 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800525 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700526 {
527 if (interface.first == "xyz.openbmc_project.Network.IP")
528 {
Tony Lee353163e2022-11-23 11:06:10 +0800529 auto type = std::find_if(interface.second.begin(),
530 interface.second.end(),
531 [](const auto& property) {
532 return property.first == "Type";
533 });
534 if (type == interface.second.end())
535 {
536 continue;
537 }
538
539 const std::string* typeStr =
540 std::get_if<std::string>(&type->second);
541
542 if (typeStr == nullptr ||
543 (*typeStr !=
544 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
545 {
546 continue;
547 }
548
Ed Tanous4a0cb852018-10-15 07:55:04 -0700549 // Instance IPv4AddressData structure, and set as
550 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800551 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700552 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800553 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800554 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700555 {
556 if (property.first == "Address")
557 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500558 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800559 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700560 if (address != nullptr)
561 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700562 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700563 }
564 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700565 else if (property.first == "Origin")
566 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500567 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800568 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700569 if (origin != nullptr)
570 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700571 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700572 translateAddressOriginDbusToRedfish(*origin,
573 true);
574 }
575 }
576 else if (property.first == "PrefixLength")
577 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500578 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800579 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700580 if (mask != nullptr)
581 {
582 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700583 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700584 }
585 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600586 else if (property.first == "Type" ||
587 property.first == "Gateway")
588 {
589 // Type & Gateway is not used
590 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700591 else
592 {
593 BMCWEB_LOG_ERROR
594 << "Got extra property: " << property.first
595 << " on the " << objpath.first.str << " object";
596 }
597 }
598 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700599 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700600 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700601 ? LinkType::Local
602 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603 }
604 }
605 }
606 }
607}
608
609/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700610 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611 *
612 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700613 * @param[in] ipHash DBus Hash id of IP that should be deleted
614 * @param[io] asyncResp Response object that will be returned to client
615 *
616 * @return None
617 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600618inline void deleteIPAddress(const std::string& ifaceId,
619 const std::string& ipHash,
620 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700621{
622 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800623 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700624 if (ec)
625 {
626 messages::internalError(asyncResp->res);
627 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628 },
629 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600630 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700631 "xyz.openbmc_project.Object.Delete", "Delete");
632}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633
Gunnar Mills244b6d52021-04-12 15:44:23 -0500634inline void updateIPv4DefaultGateway(
635 const std::string& ifaceId, const std::string& gateway,
636 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500637{
638 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800639 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700640 if (ec)
641 {
642 messages::internalError(asyncResp->res);
643 return;
644 }
645 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500646 },
647 "xyz.openbmc_project.Network",
648 "/xyz/openbmc_project/network/" + ifaceId,
649 "org.freedesktop.DBus.Properties", "Set",
650 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800651 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500652}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700654 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700656 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
657 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
658 * @param[in] gateway IPv4 address of this interfaces gateway
659 * @param[in] address IPv4 address to assign to this interface
660 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700661 *
662 * @return None
663 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000664inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
665 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800666 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700667{
Ed Tanous002d39b2022-05-31 08:59:27 -0700668 auto createIpHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800669 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500670 if (ec)
671 {
672 messages::internalError(asyncResp->res);
673 return;
674 }
675 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
676 };
677
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500679 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700680 "/xyz/openbmc_project/network/" + ifaceId,
681 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700682 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 gateway);
684}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500685
686/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700687 * @brief Deletes the IPv6 entry for this interface and creates a replacement
688 * static IPv6 entry
689 *
690 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
691 * @param[in] id The unique hash entry identifying the DBus entry
692 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
693 * @param[in] address IPv6 address to assign to this interface
694 * @param[io] asyncResp Response object that will be returned to client
695 *
696 * @return None
697 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600698
699enum class IpVersion
700{
701 IpV4,
702 IpV6
703};
704
705inline void deleteAndCreateIPAddress(
706 IpVersion version, const std::string& ifaceId, const std::string& id,
707 uint8_t prefixLength, const std::string& address,
708 const std::string& gateway,
709 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700710{
711 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600712 [asyncResp, version, ifaceId, address, prefixLength,
713 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700714 if (ec)
715 {
716 messages::internalError(asyncResp->res);
717 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600718 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
719 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700720 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800721 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700722 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700723 {
724 messages::internalError(asyncResp->res);
725 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700726 },
727 "xyz.openbmc_project.Network",
728 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600729 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
730 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700731 },
732 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600733 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700734 "xyz.openbmc_project.Object.Delete", "Delete");
735}
736
737/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500738 * @brief Creates IPv6 with given data
739 *
740 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500741 * @param[in] prefixLength Prefix length that needs to be added
742 * @param[in] address IP address that needs to be added
743 * @param[io] asyncResp Response object that will be returned to client
744 *
745 * @return None
746 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500747inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
748 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800749 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500750{
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800751 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500752 if (ec)
753 {
754 messages::internalError(asyncResp->res);
755 }
756 };
757 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500758 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500759 crow::connections::systemBus->async_method_call(
760 std::move(createIpHandler), "xyz.openbmc_project.Network",
761 "/xyz/openbmc_project/network/" + ifaceId,
762 "xyz.openbmc_project.Network.IP.Create", "IP",
763 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
764 "");
765}
766
Ed Tanous4a0cb852018-10-15 07:55:04 -0700767/**
768 * Function that retrieves all properties for given Ethernet Interface
769 * Object
770 * from EntityManager Network Manager
771 * @param ethiface_id a eth interface id to query on DBus
772 * @param callback a function that shall be called to convert Dbus output
773 * into JSON
774 */
775template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000776void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500777 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700778{
George Liuf5892d02023-03-01 10:37:08 +0800779 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
780 dbus::utility::getManagedObjects(
781 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800782 [ethifaceId{std::string{ethifaceId}},
783 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800784 const boost::system::error_code& errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700785 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700786 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -0800787 std::vector<IPv4AddressData> ipv4Data;
788 std::vector<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700789
Ed Tanous002d39b2022-05-31 08:59:27 -0700790 if (errorCode)
791 {
792 callback(false, ethData, ipv4Data, ipv6Data);
793 return;
794 }
795
796 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
797 if (!found)
798 {
799 callback(false, ethData, ipv4Data, ipv6Data);
800 return;
801 }
802
803 extractIPData(ethifaceId, resp, ipv4Data);
804 // Fix global GW
805 for (IPv4AddressData& ipv4 : ipv4Data)
806 {
807 if (((ipv4.linktype == LinkType::Global) &&
808 (ipv4.gateway == "0.0.0.0")) ||
809 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700810 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700811 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700812 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700814
Ed Tanous002d39b2022-05-31 08:59:27 -0700815 extractIPV6Data(ethifaceId, resp, ipv6Data);
816 // Finally make a callback with useful data
817 callback(true, ethData, ipv4Data, ipv6Data);
George Liuf5892d02023-03-01 10:37:08 +0800818 });
Ed Tanous271584a2019-07-09 16:24:22 -0700819}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700820
821/**
822 * Function that retrieves all Ethernet Interfaces available through Network
823 * Manager
824 * @param callback a function that shall be called to convert Dbus output
825 * into JSON.
826 */
827template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500828void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700829{
George Liuf5892d02023-03-01 10:37:08 +0800830 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
831 dbus::utility::getManagedObjects(
832 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800833 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800834 const boost::system::error_code& errorCode,
George Liuf5892d02023-03-01 10:37:08 +0800835 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700836 // Callback requires vector<string> to retrieve all available
837 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -0800838 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -0700839 ifaceList.reserve(resp.size());
840 if (errorCode)
841 {
842 callback(false, ifaceList);
843 return;
844 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700845
Ed Tanous002d39b2022-05-31 08:59:27 -0700846 // Iterate over all retrieved ObjectPaths.
847 for (const auto& objpath : resp)
848 {
849 // And all interfaces available for certain ObjectPath.
850 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700851 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700852 // If interface is
853 // xyz.openbmc_project.Network.EthernetInterface, this is
854 // what we're looking for.
855 if (interface.first ==
856 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700857 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700858 std::string ifaceId = objpath.first.filename();
859 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700861 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700863 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -0800864 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700865 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700866 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700867 }
868 // Finally make a callback with useful data
869 callback(true, ifaceList);
George Liuf5892d02023-03-01 10:37:08 +0800870 });
Ed Tanous271584a2019-07-09 16:24:22 -0700871}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100872
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700873inline void
874 handleHostnamePatch(const std::string& hostname,
875 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876{
Ed Tanousbf648f72021-06-03 15:00:14 -0700877 // SHOULD handle host names of up to 255 characters(RFC 1123)
878 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700879 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700880 messages::propertyValueFormatError(asyncResp->res, hostname,
881 "HostName");
882 return;
883 }
884 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800885 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700886 if (ec)
887 {
888 messages::internalError(asyncResp->res);
889 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700890 },
891 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
892 "org.freedesktop.DBus.Properties", "Set",
893 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800894 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700895}
896
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700897inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530898 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
899 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
900{
Patrick Williams89492a12023-05-10 07:51:34 -0500901 sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
902 ifaceId;
Tejas Patil35fb5312021-09-20 15:35:20 +0530903 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800904 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700905 if (ec)
906 {
907 messages::internalError(asyncResp->res);
908 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530909 },
910 "xyz.openbmc_project.Network", objPath,
911 "org.freedesktop.DBus.Properties", "Set",
912 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
913 std::variant<size_t>(mtuSize));
914}
915
916inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700917 handleDomainnamePatch(const std::string& ifaceId,
918 const std::string& domainname,
919 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700920{
921 std::vector<std::string> vectorDomainname = {domainname};
922 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800923 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700924 if (ec)
925 {
926 messages::internalError(asyncResp->res);
927 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700928 },
929 "xyz.openbmc_project.Network",
930 "/xyz/openbmc_project/network/" + ifaceId,
931 "org.freedesktop.DBus.Properties", "Set",
932 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800933 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700934}
935
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700936inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700937{
938 // A valid host name can never have the dotted-decimal form (RFC 1123)
939 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
940 {
941 return false;
942 }
943 // Each label(hostname/subdomains) within a valid FQDN
944 // MUST handle host names of up to 63 characters (RFC 1123)
945 // labels cannot start or end with hyphens (RFC 952)
946 // labels can start with numbers (RFC 1123)
947 const std::regex pattern(
948 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
949
950 return std::regex_match(hostname, pattern);
951}
952
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700953inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700954{
955 // Can have multiple subdomains
956 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +0800957 const std::regex pattern(
958 "^([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 -0700959
960 return std::regex_match(domainname, pattern);
961}
962
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700963inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
964 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700965{
966 // Total length of FQDN must not exceed 255 characters(RFC 1035)
967 if (fqdn.length() > 255)
968 {
969 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
970 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 }
972
Ed Tanousbf648f72021-06-03 15:00:14 -0700973 size_t pos = fqdn.find('.');
974 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700976 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
977 return;
978 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800979
Ed Tanousbf648f72021-06-03 15:00:14 -0700980 std::string hostname;
981 std::string domainname;
982 domainname = (fqdn).substr(pos + 1);
983 hostname = (fqdn).substr(0, pos);
984
985 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
986 {
987 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
988 return;
989 }
990
991 handleHostnamePatch(hostname, asyncResp);
992 handleDomainnamePatch(ifaceId, domainname, asyncResp);
993}
994
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700995inline void
996 handleMACAddressPatch(const std::string& ifaceId,
997 const std::string& macAddress,
998 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700999{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001000 static constexpr std::string_view dbusNotAllowedError =
1001 "xyz.openbmc_project.Common.Error.NotAllowed";
1002
Ed Tanousbf648f72021-06-03 15:00:14 -07001003 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001004 [asyncResp, macAddress](const boost::system::error_code& ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001005 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001006 if (ec)
1007 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001008 const sd_bus_error* err = msg.get_error();
1009 if (err == nullptr)
1010 {
1011 messages::internalError(asyncResp->res);
1012 return;
1013 }
1014 if (err->name == dbusNotAllowedError)
1015 {
1016 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1017 return;
1018 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001019 messages::internalError(asyncResp->res);
1020 return;
1021 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001022 },
1023 "xyz.openbmc_project.Network",
1024 "/xyz/openbmc_project/network/" + ifaceId,
1025 "org.freedesktop.DBus.Properties", "Set",
1026 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001027 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001028}
1029
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001030inline void setDHCPEnabled(const std::string& ifaceId,
1031 const std::string& propertyName, const bool v4Value,
1032 const bool v6Value,
1033 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001034{
1035 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1036 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001037 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001038 if (ec)
1039 {
1040 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1041 messages::internalError(asyncResp->res);
1042 return;
1043 }
1044 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001045 },
1046 "xyz.openbmc_project.Network",
1047 "/xyz/openbmc_project/network/" + ifaceId,
1048 "org.freedesktop.DBus.Properties", "Set",
1049 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001050 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001051}
1052
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001053inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001054 const std::string& ifaceId, const std::string& propertyName,
1055 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1056{
1057 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001058 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001059 if (ec)
1060 {
1061 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1062 messages::internalError(asyncResp->res);
1063 return;
1064 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001065 },
1066 "xyz.openbmc_project.Network",
1067 "/xyz/openbmc_project/network/" + ifaceId,
1068 "org.freedesktop.DBus.Properties", "Set",
1069 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001070 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001071}
1072
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001073inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1074 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001075{
1076 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1077 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001078 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001079 if (ec)
1080 {
1081 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1082 messages::internalError(asyncResp->res);
1083 return;
1084 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001085 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001086 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001087 "org.freedesktop.DBus.Properties", "Set",
1088 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001089 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001090}
1091
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001092inline void handleDHCPPatch(const std::string& ifaceId,
1093 const EthernetInterfaceData& ethData,
1094 const DHCPParameters& v4dhcpParms,
1095 const DHCPParameters& v6dhcpParms,
1096 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001097{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001098 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1099 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001100
1101 bool nextv4DHCPState =
1102 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1103
1104 bool nextv6DHCPState{};
1105 if (v6dhcpParms.dhcpv6OperatingMode)
1106 {
1107 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1108 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1109 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1110 {
1111 messages::propertyValueFormatError(asyncResp->res,
1112 *v6dhcpParms.dhcpv6OperatingMode,
1113 "OperatingMode");
1114 return;
1115 }
1116 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1117 }
1118 else
1119 {
1120 nextv6DHCPState = ipv6Active;
1121 }
1122
1123 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001124 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001125 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001126 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001127 {
1128 messages::generalError(asyncResp->res);
1129 return;
1130 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001131 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001132 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001133 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001134 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001135 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001136 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001137 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001138 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001139 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001140 }
1141 else
1142 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001143 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001144 }
1145
1146 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001147 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001148 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001149 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001150 {
1151 messages::generalError(asyncResp->res);
1152 return;
1153 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001154 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001155 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001156 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001157 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001158 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001159 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001160 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001161 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001162 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001163 }
1164 else
1165 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001166 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001167 }
1168
1169 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001170 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001171 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001172 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001173 {
1174 messages::generalError(asyncResp->res);
1175 return;
1176 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001177 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001178 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001179 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001180 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001181 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001182 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001183 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001184 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001185 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001186 }
1187 else
1188 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001189 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001190 }
1191
1192 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1193 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1194 asyncResp);
1195 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1196 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1197 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1198 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1199 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1200 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1201}
1202
Ed Tanous77179532023-02-28 10:45:28 -08001203inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1204 const std::vector<IPv4AddressData>::const_iterator& head,
1205 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001206{
1207 return std::find_if(head, end, [](const IPv4AddressData& value) {
1208 return value.origin == "Static";
1209 });
1210}
1211
Ed Tanous77179532023-02-28 10:45:28 -08001212inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1213 const std::vector<IPv6AddressData>::const_iterator& head,
1214 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001215{
1216 return std::find_if(head, end, [](const IPv6AddressData& value) {
1217 return value.origin == "Static";
1218 });
1219}
1220
Ed Tanous77179532023-02-28 10:45:28 -08001221inline void
Ed Tanousddd70dc2023-03-01 16:00:27 -08001222 handleIPv4StaticPatch(const std::string& ifaceId,
1223 nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001224 const std::vector<IPv4AddressData>& ipv4Data,
1225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001226{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001227 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001228 {
1229 messages::propertyValueTypeError(
1230 asyncResp->res,
Ed Tanousddd70dc2023-03-01 16:00:27 -08001231 nlohmann::json(input).dump(
1232 2, ' ', true, nlohmann::json::error_handler_t::replace),
Ed Tanousbf648f72021-06-03 15:00:14 -07001233 "IPv4StaticAddresses");
1234 return;
1235 }
1236
1237 unsigned entryIdx = 1;
1238 // Find the first static IP address currently active on the NIC and
1239 // match it to the first JSON element in the IPv4StaticAddresses array.
1240 // Match each subsequent JSON element to the next static IP programmed
1241 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001242 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001243 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1244
1245 for (nlohmann::json& thisJson : input)
1246 {
Patrick Williams89492a12023-05-10 07:51:34 -05001247 std::string pathString = "IPv4StaticAddresses/" +
1248 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001249
1250 if (!thisJson.is_null() && !thisJson.empty())
1251 {
1252 std::optional<std::string> address;
1253 std::optional<std::string> subnetMask;
1254 std::optional<std::string> gateway;
1255
1256 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1257 address, "SubnetMask", subnetMask,
1258 "Gateway", gateway))
1259 {
1260 messages::propertyValueFormatError(
1261 asyncResp->res,
1262 thisJson.dump(2, ' ', true,
1263 nlohmann::json::error_handler_t::replace),
1264 pathString);
1265 return;
1266 }
1267
1268 // Find the address/subnet/gateway values. Any values that are
1269 // not explicitly provided are assumed to be unmodified from the
1270 // current state of the interface. Merge existing state into the
1271 // current request.
1272 const std::string* addr = nullptr;
1273 const std::string* gw = nullptr;
1274 uint8_t prefixLength = 0;
1275 bool errorInEntry = false;
1276 if (address)
1277 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001278 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001279 {
1280 addr = &(*address);
1281 }
1282 else
1283 {
1284 messages::propertyValueFormatError(asyncResp->res, *address,
1285 pathString + "/Address");
1286 errorInEntry = true;
1287 }
1288 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001289 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001290 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001291 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001292 }
1293 else
1294 {
1295 messages::propertyMissing(asyncResp->res,
1296 pathString + "/Address");
1297 errorInEntry = true;
1298 }
1299
1300 if (subnetMask)
1301 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001302 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1303 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001304 {
1305 messages::propertyValueFormatError(
1306 asyncResp->res, *subnetMask,
1307 pathString + "/SubnetMask");
1308 errorInEntry = true;
1309 }
1310 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001311 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001312 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001313 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1314 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001315 {
1316 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001317 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001318 pathString + "/SubnetMask");
1319 errorInEntry = true;
1320 }
1321 }
1322 else
1323 {
1324 messages::propertyMissing(asyncResp->res,
1325 pathString + "/SubnetMask");
1326 errorInEntry = true;
1327 }
1328
1329 if (gateway)
1330 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001331 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001332 {
1333 gw = &(*gateway);
1334 }
1335 else
1336 {
1337 messages::propertyValueFormatError(asyncResp->res, *gateway,
1338 pathString + "/Gateway");
1339 errorInEntry = true;
1340 }
1341 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001342 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001343 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001344 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001345 }
1346 else
1347 {
1348 messages::propertyMissing(asyncResp->res,
1349 pathString + "/Gateway");
1350 errorInEntry = true;
1351 }
1352
1353 if (errorInEntry)
1354 {
1355 return;
1356 }
1357
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001358 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001359 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001360 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
1361 nicIpEntry->id, prefixLength, *gw,
1362 *addr, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001363 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1364 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001365 }
1366 else
1367 {
1368 createIPv4(ifaceId, prefixLength, *gateway, *address,
1369 asyncResp);
1370 }
1371 entryIdx++;
1372 }
1373 else
1374 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001375 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001376 {
1377 // Requesting a DELETE/DO NOT MODIFY action for an item
1378 // that isn't present on the eth(n) interface. Input JSON is
1379 // in error, so bail out.
1380 if (thisJson.is_null())
1381 {
1382 messages::resourceCannotBeDeleted(asyncResp->res);
1383 return;
1384 }
1385 messages::propertyValueFormatError(
1386 asyncResp->res,
1387 thisJson.dump(2, ' ', true,
1388 nlohmann::json::error_handler_t::replace),
1389 pathString);
1390 return;
1391 }
1392
1393 if (thisJson.is_null())
1394 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001395 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001396 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001397 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001398 {
Patrick Williams89492a12023-05-10 07:51:34 -05001399 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1400 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001401 }
1402 entryIdx++;
1403 }
1404 }
1405}
1406
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001407inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001408 const std::string& ifaceId,
1409 const std::vector<std::string>& updatedStaticNameServers,
1410 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1411{
1412 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001413 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001414 if (ec)
1415 {
1416 messages::internalError(asyncResp->res);
1417 return;
1418 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001419 },
1420 "xyz.openbmc_project.Network",
1421 "/xyz/openbmc_project/network/" + ifaceId,
1422 "org.freedesktop.DBus.Properties", "Set",
1423 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001424 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001425}
1426
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001427inline void handleIPv6StaticAddressesPatch(
Ed Tanousddd70dc2023-03-01 16:00:27 -08001428 const std::string& ifaceId, const nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001429 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001430 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1431{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001432 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 {
1434 messages::propertyValueTypeError(
1435 asyncResp->res,
Ed Tanousddd70dc2023-03-01 16:00:27 -08001436 nlohmann::json(input).dump(
1437 2, ' ', true, nlohmann::json::error_handler_t::replace),
Ed Tanousbf648f72021-06-03 15:00:14 -07001438 "IPv6StaticAddresses");
1439 return;
1440 }
1441 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001442 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001443 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1444 for (const nlohmann::json& thisJson : input)
1445 {
Patrick Williams89492a12023-05-10 07:51:34 -05001446 std::string pathString = "IPv6StaticAddresses/" +
1447 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001448
1449 if (!thisJson.is_null() && !thisJson.empty())
1450 {
1451 std::optional<std::string> address;
1452 std::optional<uint8_t> prefixLength;
1453 nlohmann::json thisJsonCopy = thisJson;
1454 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1455 address, "PrefixLength", prefixLength))
1456 {
1457 messages::propertyValueFormatError(
1458 asyncResp->res,
1459 thisJson.dump(2, ' ', true,
1460 nlohmann::json::error_handler_t::replace),
1461 pathString);
1462 return;
1463 }
1464
Ed Tanous543f4402022-01-06 13:12:53 -08001465 const std::string* addr = nullptr;
1466 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001467
1468 // Find the address and prefixLength values. Any values that are
1469 // not explicitly provided are assumed to be unmodified from the
1470 // current state of the interface. Merge existing state into the
1471 // current request.
1472 if (address)
1473 {
1474 addr = &(*address);
1475 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001476 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001477 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001478 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001479 }
1480 else
1481 {
1482 messages::propertyMissing(asyncResp->res,
1483 pathString + "/Address");
1484 return;
1485 }
1486
1487 if (prefixLength)
1488 {
1489 prefix = *prefixLength;
1490 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001491 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001492 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001493 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001494 }
1495 else
1496 {
1497 messages::propertyMissing(asyncResp->res,
1498 pathString + "/PrefixLength");
1499 return;
1500 }
1501
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001502 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001503 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001504 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
1505 nicIpEntry->id, prefix, "", *addr,
1506 asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001507 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1508 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001509 }
1510 else
1511 {
1512 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1513 }
1514 entryIdx++;
1515 }
1516 else
1517 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001518 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001519 {
1520 // Requesting a DELETE/DO NOT MODIFY action for an item
1521 // that isn't present on the eth(n) interface. Input JSON is
1522 // in error, so bail out.
1523 if (thisJson.is_null())
1524 {
1525 messages::resourceCannotBeDeleted(asyncResp->res);
1526 return;
1527 }
1528 messages::propertyValueFormatError(
1529 asyncResp->res,
1530 thisJson.dump(2, ' ', true,
1531 nlohmann::json::error_handler_t::replace),
1532 pathString);
1533 return;
1534 }
1535
1536 if (thisJson.is_null())
1537 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001538 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001539 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001540 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001541 {
Patrick Williams89492a12023-05-10 07:51:34 -05001542 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1543 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001544 }
1545 entryIdx++;
1546 }
1547 }
1548}
1549
Ed Tanous77179532023-02-28 10:45:28 -08001550inline void
1551 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1552 const std::string& ifaceId,
1553 const EthernetInterfaceData& ethData,
1554 const std::vector<IPv4AddressData>& ipv4Data,
1555 const std::vector<IPv6AddressData>& ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001556{
George Liu7a1dbc42022-12-07 16:03:22 +08001557 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001558 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1559
1560 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1561 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001562 jsonResponse["@odata.id"] = boost::urls::format(
1563 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001564 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1565
1566 auto health = std::make_shared<HealthPopulate>(asyncResp);
1567
George Liu7a1dbc42022-12-07 16:03:22 +08001568 dbus::utility::getSubTreePaths(
1569 "/", 0, inventoryForEthernet,
1570 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001571 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001572 if (ec)
1573 {
1574 return;
1575 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001576
Ed Tanous002d39b2022-05-31 08:59:27 -07001577 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001578 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001579
1580 health->populate();
1581
1582 if (ethData.nicEnabled)
1583 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001584 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001585 jsonResponse["Status"]["State"] = "Enabled";
1586 }
1587 else
1588 {
1589 jsonResponse["LinkStatus"] = "NoLink";
1590 jsonResponse["Status"]["State"] = "Disabled";
1591 }
1592
Ed Tanousbf648f72021-06-03 15:00:14 -07001593 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301594 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001595 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001596 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001597 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1598 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1599 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1600 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001601
1602 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001603 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001604 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001605 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1606 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1607 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001608
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001609 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001610 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001611 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001612
1613 // When domain name is empty then it means, that it is a network
1614 // without domain names, and the host name itself must be treated as
1615 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001616 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001617 if (!ethData.domainnames.empty())
1618 {
1619 fqdn += "." + ethData.domainnames[0];
1620 }
1621 jsonResponse["FQDN"] = fqdn;
1622 }
1623
Ed Tanousef4c65b2023-04-24 15:28:50 -07001624 jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1625 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001626
1627 jsonResponse["NameServers"] = ethData.nameServers;
1628 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1629
1630 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1631 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1632 ipv4Array = nlohmann::json::array();
1633 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001634 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001635 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001636 std::string gatewayStr = ipv4Config.gateway;
1637 if (gatewayStr.empty())
1638 {
1639 gatewayStr = "0.0.0.0";
1640 }
Ed Tanous14766872022-03-15 10:44:42 -07001641 nlohmann::json::object_t ipv4;
1642 ipv4["AddressOrigin"] = ipv4Config.origin;
1643 ipv4["SubnetMask"] = ipv4Config.netmask;
1644 ipv4["Address"] = ipv4Config.address;
1645 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001646
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 if (ipv4Config.origin == "Static")
1648 {
Ed Tanous14766872022-03-15 10:44:42 -07001649 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001650 }
Ed Tanous14766872022-03-15 10:44:42 -07001651
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001652 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001653 }
1654
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001655 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001656 if (ipv6GatewayStr.empty())
1657 {
1658 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1659 }
1660
1661 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1662
1663 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1664 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1665 ipv6Array = nlohmann::json::array();
1666 ipv6StaticArray = nlohmann::json::array();
1667 nlohmann::json& ipv6AddrPolicyTable =
1668 jsonResponse["IPv6AddressPolicyTable"];
1669 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001670 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001671 {
Ed Tanous14766872022-03-15 10:44:42 -07001672 nlohmann::json::object_t ipv6;
1673 ipv6["Address"] = ipv6Config.address;
1674 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1675 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001676
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001677 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001678 if (ipv6Config.origin == "Static")
1679 {
Ed Tanous14766872022-03-15 10:44:42 -07001680 nlohmann::json::object_t ipv6Static;
1681 ipv6Static["Address"] = ipv6Config.address;
1682 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001683 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001684 }
1685 }
1686}
1687
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001688inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001689{
Ed Tanous11ba3972022-07-11 09:50:41 -07001690 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001691}
1692
1693inline void requestEthernetInterfacesRoutes(App& app)
1694{
1695 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001696 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001697 .methods(boost::beast::http::verb::get)(
1698 [&app](const crow::Request& req,
1699 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001700 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001701 {
1702 return;
1703 }
1704
1705 asyncResp->res.jsonValue["@odata.type"] =
1706 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1707 asyncResp->res.jsonValue["@odata.id"] =
1708 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1709 asyncResp->res.jsonValue["Name"] =
1710 "Ethernet Network Interface Collection";
1711 asyncResp->res.jsonValue["Description"] =
1712 "Collection of EthernetInterfaces for this Manager";
1713
1714 // Get eth interface list, and call the below callback for JSON
1715 // preparation
1716 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08001717 [asyncResp](const bool& success,
1718 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001719 if (!success)
1720 {
1721 messages::internalError(asyncResp->res);
1722 return;
1723 }
1724
1725 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1726 ifaceArray = nlohmann::json::array();
1727 std::string tag = "_";
1728 for (const std::string& ifaceItem : ifaceList)
1729 {
1730 std::size_t found = ifaceItem.find(tag);
1731 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001732 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001733 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001734 iface["@odata.id"] = boost::urls::format(
1735 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1736 ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001737 ifaceArray.emplace_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001738 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001740
Ed Tanous002d39b2022-05-31 08:59:27 -07001741 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1742 asyncResp->res.jsonValue["@odata.id"] =
1743 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1744 });
1745 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001746
Ed Tanousbf648f72021-06-03 15:00:14 -07001747 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001748 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001749 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001750 [&app](const crow::Request& req,
1751 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1752 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001753 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001754 {
1755 return;
1756 }
1757 getEthernetIfaceData(
1758 ifaceId,
Ed Tanous77179532023-02-28 10:45:28 -08001759 [asyncResp, ifaceId](const bool& success,
1760 const EthernetInterfaceData& ethData,
1761 const std::vector<IPv4AddressData>& ipv4Data,
1762 const std::vector<IPv6AddressData>& ipv6Data) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001763 if (!success)
1764 {
1765 // TODO(Pawel)consider distinguish between non
1766 // existing object, and other errors
1767 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1768 ifaceId);
1769 return;
1770 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001771
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001772 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1773 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001774 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001775 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001776 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1777 asyncResp->res.jsonValue["Description"] =
1778 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301779
Ed Tanous002d39b2022-05-31 08:59:27 -07001780 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001781 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001782 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001783
Ed Tanousbf648f72021-06-03 15:00:14 -07001784 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001785 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001786 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001787 [&app](const crow::Request& req,
1788 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1789 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001790 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001791 {
1792 return;
1793 }
1794 std::optional<std::string> hostname;
1795 std::optional<std::string> fqdn;
1796 std::optional<std::string> macAddress;
1797 std::optional<std::string> ipv6DefaultGateway;
Ed Tanousddd70dc2023-03-01 16:00:27 -08001798 std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
1799 std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07001800 std::optional<std::vector<std::string>> staticNameServers;
1801 std::optional<nlohmann::json> dhcpv4;
1802 std::optional<nlohmann::json> dhcpv6;
1803 std::optional<bool> interfaceEnabled;
1804 std::optional<size_t> mtuSize;
1805 DHCPParameters v4dhcpParms;
1806 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001807
Ed Tanous002d39b2022-05-31 08:59:27 -07001808 if (!json_util::readJsonPatch(
1809 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1810 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1811 macAddress, "StaticNameServers", staticNameServers,
1812 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1813 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1814 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1815 {
1816 return;
1817 }
1818 if (dhcpv4)
1819 {
1820 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1821 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1822 v4dhcpParms.useDnsServers, "UseNTPServers",
1823 v4dhcpParms.useNtpServers, "UseDomainName",
1824 v4dhcpParms.useDomainName))
1825 {
1826 return;
1827 }
1828 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001829
Ed Tanous002d39b2022-05-31 08:59:27 -07001830 if (dhcpv6)
1831 {
1832 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1833 v6dhcpParms.dhcpv6OperatingMode,
1834 "UseDNSServers", v6dhcpParms.useDnsServers,
1835 "UseNTPServers", v6dhcpParms.useNtpServers,
1836 "UseDomainName",
1837 v6dhcpParms.useDomainName))
1838 {
1839 return;
1840 }
1841 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001842
Ed Tanous002d39b2022-05-31 08:59:27 -07001843 // Get single eth interface data, and call the below callback
1844 // for JSON preparation
1845 getEthernetIfaceData(
1846 ifaceId,
1847 [asyncResp, ifaceId, hostname = std::move(hostname),
1848 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1849 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1850 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1851 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1852 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001853 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1854 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001855 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1856 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08001857 const std::vector<IPv4AddressData>& ipv4Data,
1858 const std::vector<IPv6AddressData>& ipv6Data) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001859 if (!success)
1860 {
1861 // ... otherwise return error
1862 // TODO(Pawel)consider distinguish between non
1863 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001864 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001865 ifaceId);
1866 return;
1867 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001868
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 if (dhcpv4 || dhcpv6)
1870 {
1871 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001872 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001873 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301874
Ed Tanous002d39b2022-05-31 08:59:27 -07001875 if (hostname)
1876 {
1877 handleHostnamePatch(*hostname, asyncResp);
1878 }
1879
1880 if (fqdn)
1881 {
1882 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1883 }
1884
1885 if (macAddress)
1886 {
1887 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1888 }
1889
1890 if (ipv4StaticAddresses)
1891 {
1892 // TODO(ed) for some reason the capture of
1893 // ipv4Addresses above is returning a const value,
1894 // not a non-const value. This doesn't really work
1895 // for us, as we need to be able to efficiently move
1896 // out the intermedia nlohmann::json objects. This
1897 // makes a copy of the structure, and operates on
1898 // that, but could be done more efficiently
Ed Tanousddd70dc2023-03-01 16:00:27 -08001899 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07001900 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1901 }
1902
1903 if (staticNameServers)
1904 {
1905 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1906 asyncResp);
1907 }
1908
1909 if (ipv6DefaultGateway)
1910 {
1911 messages::propertyNotWritable(asyncResp->res,
1912 "IPv6DefaultGateway");
1913 }
1914
1915 if (ipv6StaticAddresses)
1916 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08001917 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
1918 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001919 }
1920
1921 if (interfaceEnabled)
1922 {
1923 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1924 *interfaceEnabled, asyncResp);
1925 }
1926
1927 if (mtuSize)
1928 {
1929 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
1930 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001931 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001932 });
manojkiraneda2a133282019-02-19 13:09:43 +05301933
Ed Tanousbf648f72021-06-03 15:00:14 -07001934 BMCWEB_ROUTE(
1935 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001936 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001937 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001938 [&app](const crow::Request& req,
1939 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1940 const std::string& parentIfaceId,
1941 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001942 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001943 {
1944 return;
1945 }
1946 asyncResp->res.jsonValue["@odata.type"] =
1947 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1948 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001949
Ed Tanous002d39b2022-05-31 08:59:27 -07001950 if (!verifyNames(parentIfaceId, ifaceId))
1951 {
1952 return;
1953 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 // Get single eth interface data, and call the below callback
1956 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08001957 getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
1958 const bool& success,
1959 const EthernetInterfaceData& ethData,
1960 const std::vector<IPv4AddressData>&,
1961 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001962 if (success && ethData.vlanId)
1963 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001964 asyncResp->res.jsonValue["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001965 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1966 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1967 parentIfaceId, ifaceId);
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001968
Jiaqing Zhao23a06312022-09-13 09:38:25 +08001969 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001970 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07001971 }
1972 else
1973 {
1974 // ... otherwise return error
1975 // TODO(Pawel)consider distinguish between non
1976 // existing object, and other errors
1977 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001978 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001979 }
Ed Tanous77179532023-02-28 10:45:28 -08001980 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001981 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001982
Ed Tanousbf648f72021-06-03 15:00:14 -07001983 BMCWEB_ROUTE(
1984 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05001985 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001986 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001987 [&app](const crow::Request& req,
1988 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1989 const std::string& parentIfaceId,
1990 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001991 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001992 {
1993 return;
1994 }
1995 if (!verifyNames(parentIfaceId, ifaceId))
1996 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001997 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001998 ifaceId);
1999 return;
2000 }
2001
2002 std::optional<bool> vlanEnable;
2003 std::optional<uint32_t> vlanId;
2004
2005 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2006 vlanEnable, "VLANId", vlanId))
2007 {
2008 return;
2009 }
2010
2011 if (vlanId)
2012 {
2013 messages::propertyNotWritable(asyncResp->res, "VLANId");
2014 return;
2015 }
2016
2017 // Get single eth interface data, and call the below callback
2018 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08002019 getEthernetIfaceData(ifaceId,
2020 [asyncResp, parentIfaceId, ifaceId,
2021 vlanEnable](const bool& success,
2022 const EthernetInterfaceData& ethData,
2023 const std::vector<IPv4AddressData>&,
2024 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002025 if (success && ethData.vlanId)
2026 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002027 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002028 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002029 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002030 [asyncResp](const boost::system::error_code& ec) {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002031 if (ec)
2032 {
2033 messages::internalError(asyncResp->res);
2034 return;
2035 }
2036 },
2037 "xyz.openbmc_project.Network",
2038 "/xyz/openbmc_project/network/" + ifaceId,
2039 "org.freedesktop.DBus.Properties", "Set",
2040 "xyz.openbmc_project.Network.EthernetInterface",
2041 "NICEnabled",
2042 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002043 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002044 }
2045 else
2046 {
2047 // TODO(Pawel)consider distinguish between non
2048 // existing object, and other errors
2049 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002050 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002051 return;
2052 }
Ed Tanous77179532023-02-28 10:45:28 -08002053 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002054 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002055
2056 BMCWEB_ROUTE(
2057 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002058 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002059 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002060 [&app](const crow::Request& req,
2061 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2062 const std::string& parentIfaceId,
2063 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002064 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002065 {
2066 return;
2067 }
2068 if (!verifyNames(parentIfaceId, ifaceId))
2069 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002070 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002071 ifaceId);
2072 return;
2073 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002074
Ed Tanous002d39b2022-05-31 08:59:27 -07002075 // Get single eth interface data, and call the below callback
2076 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08002077 getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
2078 const bool& success,
2079 const EthernetInterfaceData& ethData,
2080 const std::vector<IPv4AddressData>&,
2081 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002082 if (success && ethData.vlanId)
2083 {
2084 auto callback =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002085 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002086 if (ec)
2087 {
2088 messages::internalError(asyncResp->res);
2089 }
2090 };
2091 crow::connections::systemBus->async_method_call(
2092 std::move(callback), "xyz.openbmc_project.Network",
2093 std::string("/xyz/openbmc_project/network/") + ifaceId,
2094 "xyz.openbmc_project.Object.Delete", "Delete");
2095 }
2096 else
2097 {
2098 // ... otherwise return error
2099 // TODO(Pawel)consider distinguish between non
2100 // existing object, and other errors
2101 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002102 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002103 }
Ed Tanous77179532023-02-28 10:45:28 -08002104 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002105 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002106
Ed Tanousbf648f72021-06-03 15:00:14 -07002107 BMCWEB_ROUTE(app,
2108 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002109
2110 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002111 .methods(boost::beast::http::verb::get)(
2112 [&app](const crow::Request& req,
2113 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2114 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002115 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002116 {
2117 return;
2118 }
2119 // Get eth interface list, and call the below callback for JSON
2120 // preparation
Ed Tanous77179532023-02-28 10:45:28 -08002121 getEthernetIfaceList([asyncResp, rootInterfaceName](
2122 const bool& success,
2123 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002124 if (!success)
2125 {
2126 messages::internalError(asyncResp->res);
2127 return;
2128 }
Ed Tanous77179532023-02-28 10:45:28 -08002129 if (std::find(ifaceList.begin(), ifaceList.end(),
2130 rootInterfaceName) == ifaceList.end())
Ed Tanous002d39b2022-05-31 08:59:27 -07002131 {
2132 messages::resourceNotFound(asyncResp->res,
2133 "VLanNetworkInterfaceCollection",
2134 rootInterfaceName);
2135 return;
2136 }
2137
2138 asyncResp->res.jsonValue["@odata.type"] =
2139 "#VLanNetworkInterfaceCollection."
2140 "VLanNetworkInterfaceCollection";
2141 asyncResp->res.jsonValue["Name"] =
2142 "VLAN Network Interface Collection";
2143
2144 nlohmann::json ifaceArray = nlohmann::json::array();
2145
2146 for (const std::string& ifaceItem : ifaceList)
2147 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002148 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002149 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002150 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002151 iface["@odata.id"] = boost::urls::format(
2152 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2153 rootInterfaceName, ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002154 ifaceArray.emplace_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002155 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002156 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002157
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2159 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Ed Tanousef4c65b2023-04-24 15:28:50 -07002160 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2161 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2162 rootInterfaceName);
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 });
2164 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002165
Ed Tanousbf648f72021-06-03 15:00:14 -07002166 BMCWEB_ROUTE(app,
2167 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002168 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002169 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002170 [&app](const crow::Request& req,
2171 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2172 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002173 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002174 {
2175 return;
2176 }
2177 bool vlanEnable = false;
2178 uint32_t vlanId = 0;
2179 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2180 "VLANEnable", vlanEnable))
2181 {
2182 return;
2183 }
2184 // Need both vlanId and vlanEnable to service this request
2185 if (vlanId == 0U)
2186 {
2187 messages::propertyMissing(asyncResp->res, "VLANId");
2188 }
2189 if (!vlanEnable)
2190 {
2191 messages::propertyMissing(asyncResp->res, "VLANEnable");
2192 }
2193 if (static_cast<bool>(vlanId) ^ vlanEnable)
2194 {
2195 return;
2196 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002197
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002198 auto callback = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002199 if (ec)
2200 {
2201 // TODO(ed) make more consistent error messages
2202 // based on phosphor-network responses
2203 messages::internalError(asyncResp->res);
2204 return;
2205 }
2206 messages::created(asyncResp->res);
2207 };
2208 crow::connections::systemBus->async_method_call(
2209 std::move(callback), "xyz.openbmc_project.Network",
2210 "/xyz/openbmc_project/network",
2211 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2212 rootInterfaceName, vlanId);
2213 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002214}
2215
Ed Tanous1abe55e2018-09-05 08:30:59 -07002216} // namespace redfish