blob: 330e68e482c458eff9fbec38e4488fa41909522c [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"
Ed Tanous2c5875a2023-05-15 09:56:06 -070023#include "human_sort.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080024#include "query.hpp"
25#include "registries/privilege_registry.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070026#include "utils/ip_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "utils/json_utils.hpp"
Ed Tanous033f1e42022-08-15 09:47:37 -070028
Ed Tanous11ba3972022-07-11 09:50:41 -070029#include <boost/algorithm/string/classification.hpp>
30#include <boost/algorithm/string/split.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070031#include <boost/url/format.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050032
George Liu7a1dbc42022-12-07 16:03:22 +080033#include <array>
Ed Tanousa24526d2018-12-10 15:17:59 -080034#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053035#include <regex>
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <string_view>
Ed Tanous77179532023-02-28 10:45:28 -080037#include <vector>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010038
Ed Tanous1abe55e2018-09-05 08:30:59 -070039namespace redfish
40{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010041
Ed Tanous4a0cb852018-10-15 07:55:04 -070042enum class LinkType
43{
44 Local,
45 Global
46};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010047
48/**
49 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010050 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070051struct IPv4AddressData
52{
53 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070054 std::string address;
55 std::string domain;
56 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 std::string netmask;
58 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080059 LinkType linktype{};
60 bool isActive{};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010061};
62
63/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050064 * Structure for keeping IPv6 data required by Redfish
65 */
66struct IPv6AddressData
67{
68 std::string id;
69 std::string address;
70 std::string origin;
Ed Tanous77179532023-02-28 10:45:28 -080071 uint8_t prefixLength = 0;
Ravi Tejae48c0fc2019-04-16 08:37:20 -050072};
73/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010074 * Structure for keeping basic single Ethernet Interface information
75 * available from DBus
76 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070077struct EthernetInterfaceData
78{
Ed Tanous4a0cb852018-10-15 07:55:04 -070079 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053080 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080081 bool autoNeg;
82 bool dnsEnabled;
83 bool ntpEnabled;
84 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080085 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070086 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080087 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070088 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080089 std::string hostName;
90 std::string defaultGateway;
91 std::string ipv6DefaultGateway;
92 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +080093 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050094 std::vector<std::string> nameServers;
95 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -080096 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010097};
98
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070099struct DHCPParameters
100{
101 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800102 std::optional<bool> useDnsServers;
103 std::optional<bool> useNtpServers;
104 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700105 std::optional<std::string> dhcpv6OperatingMode;
106};
107
Ed Tanous4a0cb852018-10-15 07:55:04 -0700108// Helper function that changes bits netmask notation (i.e. /24)
109// into full dot notation
110inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700111{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700112 uint32_t value = 0xffffffff << (32 - bits);
113 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
114 std::to_string((value >> 16) & 0xff) + "." +
115 std::to_string((value >> 8) & 0xff) + "." +
116 std::to_string(value & 0xff);
117 return netmask;
118}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100119
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800120inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700121 bool isIPv4)
122{
123 if (isIPv4)
124 {
125 return (
126 (inputDHCP ==
127 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
128 (inputDHCP ==
129 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
130 }
131 return ((inputDHCP ==
132 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
133 (inputDHCP ==
134 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
135}
136
Ed Tanous2c70f802020-09-28 14:29:23 -0700137inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700138{
139 if (isIPv4 && isIPv6)
140 {
141 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
142 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700143 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700144 {
145 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
146 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700147 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700148 {
149 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
150 }
151 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
152}
153
Ed Tanous4a0cb852018-10-15 07:55:04 -0700154inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500155 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700156 bool isIPv4)
157{
158 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700160 return "Static";
161 }
162 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
163 {
164 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700166 return "IPv4LinkLocal";
167 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700168 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 }
170 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
171 {
172 if (isIPv4)
173 {
174 return "DHCP";
175 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700176 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700177 }
178 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
179 {
180 return "SLAAC";
181 }
182 return "";
183}
184
Ed Tanous02cad962022-06-30 16:50:15 -0700185inline bool extractEthernetInterfaceData(
186 const std::string& ethifaceId,
187 const dbus::utility::ManagedObjectType& dbusData,
188 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700189{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700190 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700191 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700192 {
Ed Tanous02cad962022-06-30 16:50:15 -0700193 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700194 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000195 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700197 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700198 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500200 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700202 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500204 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800205 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700206 if (mac != nullptr)
207 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800208 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700210 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 }
212 }
213 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
214 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 {
217 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500219 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800220 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 if (id != nullptr)
222 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800223 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700226 }
227 }
228 else if (ifacePair.first ==
229 "xyz.openbmc_project.Network.EthernetInterface")
230 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500231 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700232 {
233 if (propertyPair.first == "AutoNeg")
234 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700235 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800236 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700237 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800239 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700240 }
241 }
242 else if (propertyPair.first == "Speed")
243 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500244 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800245 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 if (speed != nullptr)
247 {
248 ethData.speed = *speed;
249 }
250 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530251 else if (propertyPair.first == "MTU")
252 {
253 const uint32_t* mtuSize =
254 std::get_if<uint32_t>(&propertyPair.second);
255 if (mtuSize != nullptr)
256 {
257 ethData.mtuSize = *mtuSize;
258 }
259 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800260 else if (propertyPair.first == "LinkUp")
261 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500262 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800263 std::get_if<bool>(&propertyPair.second);
264 if (linkUp != nullptr)
265 {
266 ethData.linkUp = *linkUp;
267 }
268 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700269 else if (propertyPair.first == "NICEnabled")
270 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500271 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700272 std::get_if<bool>(&propertyPair.second);
273 if (nicEnabled != nullptr)
274 {
275 ethData.nicEnabled = *nicEnabled;
276 }
277 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500278 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700279 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500280 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500281 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800282 &propertyPair.second);
283 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700284 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700285 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500286 }
287 }
288 else if (propertyPair.first == "StaticNameServers")
289 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500290 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500291 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500292 &propertyPair.second);
293 if (staticNameServers != nullptr)
294 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700295 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700296 }
297 }
manojkiraneda2a133282019-02-19 13:09:43 +0530298 else if (propertyPair.first == "DHCPEnabled")
299 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700300 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700301 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700302 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530303 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800304 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530305 }
306 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800307 else if (propertyPair.first == "DomainName")
308 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500309 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500310 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800311 &propertyPair.second);
312 if (domainNames != nullptr)
313 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700314 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800315 }
316 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500317 else if (propertyPair.first == "DefaultGateway")
318 {
319 const std::string* defaultGateway =
320 std::get_if<std::string>(&propertyPair.second);
321 if (defaultGateway != nullptr)
322 {
323 std::string defaultGatewayStr = *defaultGateway;
324 if (defaultGatewayStr.empty())
325 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800326 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500327 }
328 else
329 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800330 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500331 }
332 }
333 }
334 else if (propertyPair.first == "DefaultGateway6")
335 {
336 const std::string* defaultGateway6 =
337 std::get_if<std::string>(&propertyPair.second);
338 if (defaultGateway6 != nullptr)
339 {
340 std::string defaultGateway6Str =
341 *defaultGateway6;
342 if (defaultGateway6Str.empty())
343 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800344 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500345 "0:0:0:0:0:0:0:0";
346 }
347 else
348 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800349 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500350 defaultGateway6Str;
351 }
352 }
353 }
Ed Tanous029573d2019-02-01 10:57:49 -0800354 }
355 }
356 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700357
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800358 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700359 {
360 if (ifacePair.first ==
361 "xyz.openbmc_project.Network.DHCPConfiguration")
362 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500363 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700364 {
365 if (propertyPair.first == "DNSEnabled")
366 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700367 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700368 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700369 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700370 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800371 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 }
373 }
374 else if (propertyPair.first == "NTPEnabled")
375 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700376 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700377 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700378 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700379 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800380 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700381 }
382 }
383 else if (propertyPair.first == "HostNameEnabled")
384 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700385 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700386 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700387 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700388 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800389 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700390 }
391 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700392 }
393 }
394 }
Ed Tanous029573d2019-02-01 10:57:49 -0800395 // System configuration shows up in the global namespace, so no need
396 // to check eth number
397 if (ifacePair.first ==
398 "xyz.openbmc_project.Network.SystemConfiguration")
399 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500400 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800401 {
402 if (propertyPair.first == "HostName")
403 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500404 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500405 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800406 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700407 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800408 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800409 }
410 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700411 }
412 }
413 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700415 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700416}
417
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500418// Helper function that extracts data for single ethernet ipv6 address
Ed Tanous77179532023-02-28 10:45:28 -0800419inline void extractIPV6Data(const std::string& ethifaceId,
420 const dbus::utility::ManagedObjectType& dbusData,
421 std::vector<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500422{
Patrick Williams89492a12023-05-10 07:51:34 -0500423 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
424 ethifaceId;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500425
426 // Since there might be several IPv6 configurations aligned with
427 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500429 {
430 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800431 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500432 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800433 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500434 {
435 if (interface.first == "xyz.openbmc_project.Network.IP")
436 {
Tony Lee353163e2022-11-23 11:06:10 +0800437 auto type = std::find_if(interface.second.begin(),
438 interface.second.end(),
439 [](const auto& property) {
440 return property.first == "Type";
441 });
442 if (type == interface.second.end())
443 {
444 continue;
445 }
446
447 const std::string* typeStr =
448 std::get_if<std::string>(&type->second);
449
450 if (typeStr == nullptr ||
451 (*typeStr !=
452 "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
453 {
454 continue;
455 }
456
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500457 // Instance IPv6AddressData structure, and set as
458 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800459 IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700460 ipv6Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800461 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800462 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500463 {
464 if (property.first == "Address")
465 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500466 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500467 std::get_if<std::string>(&property.second);
468 if (address != nullptr)
469 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700470 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500471 }
472 }
473 else if (property.first == "Origin")
474 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500475 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500476 std::get_if<std::string>(&property.second);
477 if (origin != nullptr)
478 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700479 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500480 translateAddressOriginDbusToRedfish(*origin,
481 false);
482 }
483 }
484 else if (property.first == "PrefixLength")
485 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500486 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500487 std::get_if<uint8_t>(&property.second);
488 if (prefix != nullptr)
489 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700490 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500491 }
492 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600493 else if (property.first == "Type" ||
494 property.first == "Gateway")
495 {
496 // Type & Gateway is not used
497 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500498 else
499 {
500 BMCWEB_LOG_ERROR
501 << "Got extra property: " << property.first
502 << " on the " << objpath.first.str << " object";
503 }
504 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500505 }
506 }
507 }
508 }
509}
510
Ed Tanous4a0cb852018-10-15 07:55:04 -0700511// Helper function that extracts data for single ethernet ipv4 address
Ed Tanous77179532023-02-28 10:45:28 -0800512inline void extractIPData(const std::string& ethifaceId,
513 const dbus::utility::ManagedObjectType& dbusData,
514 std::vector<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700515{
Patrick Williams89492a12023-05-10 07:51:34 -0500516 const std::string ipPathStart = "/xyz/openbmc_project/network/" +
517 ethifaceId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700518
519 // Since there might be several IPv4 configurations aligned with
520 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000521 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700522 {
523 // Check if proper pattern for object path appears
Tony Lee353163e2022-11-23 11:06:10 +0800524 if (objpath.first.str.starts_with(ipPathStart + "/"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700525 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800526 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 {
528 if (interface.first == "xyz.openbmc_project.Network.IP")
529 {
Tony Lee353163e2022-11-23 11:06:10 +0800530 auto type = std::find_if(interface.second.begin(),
531 interface.second.end(),
532 [](const auto& property) {
533 return property.first == "Type";
534 });
535 if (type == interface.second.end())
536 {
537 continue;
538 }
539
540 const std::string* typeStr =
541 std::get_if<std::string>(&type->second);
542
543 if (typeStr == nullptr ||
544 (*typeStr !=
545 "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
546 {
547 continue;
548 }
549
Ed Tanous4a0cb852018-10-15 07:55:04 -0700550 // Instance IPv4AddressData structure, and set as
551 // appropriate
Ed Tanous77179532023-02-28 10:45:28 -0800552 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
Ed Tanous2c70f802020-09-28 14:29:23 -0700553 ipv4Address.id =
Tony Lee353163e2022-11-23 11:06:10 +0800554 objpath.first.str.substr(ipPathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800555 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700556 {
557 if (property.first == "Address")
558 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500559 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800560 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700561 if (address != nullptr)
562 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700563 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700564 }
565 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700566 else if (property.first == "Origin")
567 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500568 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800569 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700570 if (origin != nullptr)
571 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700572 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700573 translateAddressOriginDbusToRedfish(*origin,
574 true);
575 }
576 }
577 else if (property.first == "PrefixLength")
578 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500579 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800580 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700581 if (mask != nullptr)
582 {
583 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700584 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700585 }
586 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600587 else if (property.first == "Type" ||
588 property.first == "Gateway")
589 {
590 // Type & Gateway is not used
591 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700592 else
593 {
594 BMCWEB_LOG_ERROR
595 << "Got extra property: " << property.first
596 << " on the " << objpath.first.str << " object";
597 }
598 }
599 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700600 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700601 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700602 ? LinkType::Local
603 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604 }
605 }
606 }
607 }
608}
609
610/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700611 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700612 *
613 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700614 * @param[in] ipHash DBus Hash id of IP that should be deleted
615 * @param[io] asyncResp Response object that will be returned to client
616 *
617 * @return None
618 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600619inline void deleteIPAddress(const std::string& ifaceId,
620 const std::string& ipHash,
621 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622{
623 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800624 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700625 if (ec)
626 {
627 messages::internalError(asyncResp->res);
628 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629 },
630 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600631 "/xyz/openbmc_project/network/" + ifaceId + ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 "xyz.openbmc_project.Object.Delete", "Delete");
633}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634
Gunnar Mills244b6d52021-04-12 15:44:23 -0500635inline void updateIPv4DefaultGateway(
636 const std::string& ifaceId, const std::string& gateway,
637 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500638{
639 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800640 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700641 if (ec)
642 {
643 messages::internalError(asyncResp->res);
644 return;
645 }
646 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500647 },
648 "xyz.openbmc_project.Network",
649 "/xyz/openbmc_project/network/" + ifaceId,
650 "org.freedesktop.DBus.Properties", "Set",
651 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800652 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500653}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700654/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700655 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700656 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700657 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
658 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
659 * @param[in] gateway IPv4 address of this interfaces gateway
660 * @param[in] address IPv4 address to assign to this interface
661 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 *
663 * @return None
664 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000665inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
666 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800667 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668{
Ed Tanous002d39b2022-05-31 08:59:27 -0700669 auto createIpHandler =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800670 [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500671 if (ec)
672 {
673 messages::internalError(asyncResp->res);
674 return;
675 }
676 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
677 };
678
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500680 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 "/xyz/openbmc_project/network/" + ifaceId,
682 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700683 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700684 gateway);
685}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500686
687/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700688 * @brief Deletes the IPv6 entry for this interface and creates a replacement
689 * static IPv6 entry
690 *
691 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
692 * @param[in] id The unique hash entry identifying the DBus entry
693 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
694 * @param[in] address IPv6 address to assign to this interface
695 * @param[io] asyncResp Response object that will be returned to client
696 *
697 * @return None
698 */
Ravi Teja9c5e5852023-02-26 21:33:52 -0600699
700enum class IpVersion
701{
702 IpV4,
703 IpV6
704};
705
706inline void deleteAndCreateIPAddress(
707 IpVersion version, const std::string& ifaceId, const std::string& id,
708 uint8_t prefixLength, const std::string& address,
709 const std::string& gateway,
710 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700711{
712 crow::connections::systemBus->async_method_call(
Ravi Teja9c5e5852023-02-26 21:33:52 -0600713 [asyncResp, version, ifaceId, address, prefixLength,
714 gateway](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700715 if (ec)
716 {
717 messages::internalError(asyncResp->res);
718 }
Ravi Teja9c5e5852023-02-26 21:33:52 -0600719 std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
720 protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
Ed Tanous002d39b2022-05-31 08:59:27 -0700721 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800722 [asyncResp](const boost::system::error_code& ec2) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700723 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700724 {
725 messages::internalError(asyncResp->res);
726 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700727 },
728 "xyz.openbmc_project.Network",
729 "/xyz/openbmc_project/network/" + ifaceId,
Ravi Teja9c5e5852023-02-26 21:33:52 -0600730 "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
731 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700732 },
733 "xyz.openbmc_project.Network",
Ravi Teja9c5e5852023-02-26 21:33:52 -0600734 "/xyz/openbmc_project/network/" + ifaceId + id,
Johnathan Mantey01784822019-06-18 12:44:21 -0700735 "xyz.openbmc_project.Object.Delete", "Delete");
736}
737
738/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500739 * @brief Creates IPv6 with given data
740 *
741 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500742 * @param[in] prefixLength Prefix length that needs to be added
743 * @param[in] address IP address that needs to be added
744 * @param[io] asyncResp Response object that will be returned to client
745 *
746 * @return None
747 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500748inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
749 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800750 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500751{
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800752 auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500753 if (ec)
754 {
755 messages::internalError(asyncResp->res);
756 }
757 };
758 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500759 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500760 crow::connections::systemBus->async_method_call(
761 std::move(createIpHandler), "xyz.openbmc_project.Network",
762 "/xyz/openbmc_project/network/" + ifaceId,
763 "xyz.openbmc_project.Network.IP.Create", "IP",
764 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
765 "");
766}
767
Ed Tanous4a0cb852018-10-15 07:55:04 -0700768/**
769 * Function that retrieves all properties for given Ethernet Interface
770 * Object
771 * from EntityManager Network Manager
772 * @param ethiface_id a eth interface id to query on DBus
773 * @param callback a function that shall be called to convert Dbus output
774 * into JSON
775 */
776template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000777void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500778 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700779{
George Liuf5892d02023-03-01 10:37:08 +0800780 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
781 dbus::utility::getManagedObjects(
782 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800783 [ethifaceId{std::string{ethifaceId}},
784 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800785 const boost::system::error_code& errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700786 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700787 EthernetInterfaceData ethData{};
Ed Tanous77179532023-02-28 10:45:28 -0800788 std::vector<IPv4AddressData> ipv4Data;
789 std::vector<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700790
Ed Tanous002d39b2022-05-31 08:59:27 -0700791 if (errorCode)
792 {
793 callback(false, ethData, ipv4Data, ipv6Data);
794 return;
795 }
796
797 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
798 if (!found)
799 {
800 callback(false, ethData, ipv4Data, ipv6Data);
801 return;
802 }
803
804 extractIPData(ethifaceId, resp, ipv4Data);
805 // Fix global GW
806 for (IPv4AddressData& ipv4 : ipv4Data)
807 {
808 if (((ipv4.linktype == LinkType::Global) &&
809 (ipv4.gateway == "0.0.0.0")) ||
810 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700811 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700812 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700813 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700814 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700815
Ed Tanous002d39b2022-05-31 08:59:27 -0700816 extractIPV6Data(ethifaceId, resp, ipv6Data);
817 // Finally make a callback with useful data
818 callback(true, ethData, ipv4Data, ipv6Data);
George Liuf5892d02023-03-01 10:37:08 +0800819 });
Ed Tanous271584a2019-07-09 16:24:22 -0700820}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700821
822/**
823 * Function that retrieves all Ethernet Interfaces available through Network
824 * Manager
825 * @param callback a function that shall be called to convert Dbus output
826 * into JSON.
827 */
828template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500829void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700830{
George Liuf5892d02023-03-01 10:37:08 +0800831 sdbusplus::message::object_path path("/xyz/openbmc_project/network");
832 dbus::utility::getManagedObjects(
833 "xyz.openbmc_project.Network", path,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800834 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800835 const boost::system::error_code& errorCode,
George Liuf5892d02023-03-01 10:37:08 +0800836 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700837 // Callback requires vector<string> to retrieve all available
838 // ethernet interfaces
Ed Tanous77179532023-02-28 10:45:28 -0800839 std::vector<std::string> ifaceList;
Ed Tanous002d39b2022-05-31 08:59:27 -0700840 ifaceList.reserve(resp.size());
841 if (errorCode)
842 {
843 callback(false, ifaceList);
844 return;
845 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700846
Ed Tanous002d39b2022-05-31 08:59:27 -0700847 // Iterate over all retrieved ObjectPaths.
848 for (const auto& objpath : resp)
849 {
850 // And all interfaces available for certain ObjectPath.
851 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700852 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700853 // If interface is
854 // xyz.openbmc_project.Network.EthernetInterface, this is
855 // what we're looking for.
856 if (interface.first ==
857 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700858 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700859 std::string ifaceId = objpath.first.filename();
860 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700861 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700862 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700864 // and put it into output vector.
Ed Tanous77179532023-02-28 10:45:28 -0800865 ifaceList.emplace_back(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700867 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700868 }
Ed Tanous2c5875a2023-05-15 09:56:06 -0700869
870 std::sort(ifaceList.begin(), ifaceList.end(),
871 AlphanumLess<std::string>());
872
Ed Tanous002d39b2022-05-31 08:59:27 -0700873 // Finally make a callback with useful data
874 callback(true, ifaceList);
George Liuf5892d02023-03-01 10:37:08 +0800875 });
Ed Tanous271584a2019-07-09 16:24:22 -0700876}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100877
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700878inline void
879 handleHostnamePatch(const std::string& hostname,
880 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881{
Ed Tanousbf648f72021-06-03 15:00:14 -0700882 // SHOULD handle host names of up to 255 characters(RFC 1123)
883 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700885 messages::propertyValueFormatError(asyncResp->res, hostname,
886 "HostName");
887 return;
888 }
889 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800890 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700891 if (ec)
892 {
893 messages::internalError(asyncResp->res);
894 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700895 },
896 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
897 "org.freedesktop.DBus.Properties", "Set",
898 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800899 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700900}
901
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700902inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530903 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
904 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
905{
Patrick Williams89492a12023-05-10 07:51:34 -0500906 sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
907 ifaceId;
Tejas Patil35fb5312021-09-20 15:35:20 +0530908 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800909 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700910 if (ec)
911 {
912 messages::internalError(asyncResp->res);
913 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530914 },
915 "xyz.openbmc_project.Network", objPath,
916 "org.freedesktop.DBus.Properties", "Set",
917 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
918 std::variant<size_t>(mtuSize));
919}
920
921inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700922 handleDomainnamePatch(const std::string& ifaceId,
923 const std::string& domainname,
924 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700925{
926 std::vector<std::string> vectorDomainname = {domainname};
927 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800928 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700929 if (ec)
930 {
931 messages::internalError(asyncResp->res);
932 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700933 },
934 "xyz.openbmc_project.Network",
935 "/xyz/openbmc_project/network/" + ifaceId,
936 "org.freedesktop.DBus.Properties", "Set",
937 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800938 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700939}
940
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700941inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700942{
943 // A valid host name can never have the dotted-decimal form (RFC 1123)
944 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
945 {
946 return false;
947 }
948 // Each label(hostname/subdomains) within a valid FQDN
949 // MUST handle host names of up to 63 characters (RFC 1123)
950 // labels cannot start or end with hyphens (RFC 952)
951 // labels can start with numbers (RFC 1123)
Ed Tanous4b242742023-05-11 09:51:51 -0700952 const static std::regex pattern(
Ed Tanousbf648f72021-06-03 15:00:14 -0700953 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
954
955 return std::regex_match(hostname, pattern);
956}
957
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700958inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700959{
960 // Can have multiple subdomains
961 // Top Level Domain's min length is 2 character
Ed Tanous4b242742023-05-11 09:51:51 -0700962 const static std::regex pattern(
George Liu0fda0f12021-11-16 10:06:17 +0800963 "^([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 -0700964
965 return std::regex_match(domainname, pattern);
966}
967
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700968inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
969 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700970{
971 // Total length of FQDN must not exceed 255 characters(RFC 1035)
972 if (fqdn.length() > 255)
973 {
974 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
975 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 }
977
Ed Tanousbf648f72021-06-03 15:00:14 -0700978 size_t pos = fqdn.find('.');
979 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700981 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
982 return;
983 }
zhanghch058d1b46d2021-04-01 11:18:24 +0800984
Ed Tanousbf648f72021-06-03 15:00:14 -0700985 std::string hostname;
986 std::string domainname;
987 domainname = (fqdn).substr(pos + 1);
988 hostname = (fqdn).substr(0, pos);
989
990 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
991 {
992 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
993 return;
994 }
995
996 handleHostnamePatch(hostname, asyncResp);
997 handleDomainnamePatch(ifaceId, domainname, asyncResp);
998}
999
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001000inline void
1001 handleMACAddressPatch(const std::string& ifaceId,
1002 const std::string& macAddress,
1003 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001004{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001005 static constexpr std::string_view dbusNotAllowedError =
1006 "xyz.openbmc_project.Common.Error.NotAllowed";
1007
Ed Tanousbf648f72021-06-03 15:00:14 -07001008 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001009 [asyncResp, macAddress](const boost::system::error_code& ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001010 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001011 if (ec)
1012 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001013 const sd_bus_error* err = msg.get_error();
1014 if (err == nullptr)
1015 {
1016 messages::internalError(asyncResp->res);
1017 return;
1018 }
1019 if (err->name == dbusNotAllowedError)
1020 {
1021 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1022 return;
1023 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001024 messages::internalError(asyncResp->res);
1025 return;
1026 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001027 },
1028 "xyz.openbmc_project.Network",
1029 "/xyz/openbmc_project/network/" + ifaceId,
1030 "org.freedesktop.DBus.Properties", "Set",
1031 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001032 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001033}
1034
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001035inline void setDHCPEnabled(const std::string& ifaceId,
1036 const std::string& propertyName, const bool v4Value,
1037 const bool v6Value,
1038 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001039{
1040 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1041 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001042 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001043 if (ec)
1044 {
1045 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1046 messages::internalError(asyncResp->res);
1047 return;
1048 }
1049 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001050 },
1051 "xyz.openbmc_project.Network",
1052 "/xyz/openbmc_project/network/" + ifaceId,
1053 "org.freedesktop.DBus.Properties", "Set",
1054 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001055 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001056}
1057
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001058inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001059 const std::string& ifaceId, const std::string& propertyName,
1060 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1061{
1062 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001063 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001064 if (ec)
1065 {
1066 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1067 messages::internalError(asyncResp->res);
1068 return;
1069 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001070 },
1071 "xyz.openbmc_project.Network",
1072 "/xyz/openbmc_project/network/" + ifaceId,
1073 "org.freedesktop.DBus.Properties", "Set",
1074 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001075 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001076}
1077
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001078inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1079 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001080{
1081 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1082 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001083 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001084 if (ec)
1085 {
1086 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1087 messages::internalError(asyncResp->res);
1088 return;
1089 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001090 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001091 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001092 "org.freedesktop.DBus.Properties", "Set",
1093 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001094 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001095}
1096
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001097inline void handleDHCPPatch(const std::string& ifaceId,
1098 const EthernetInterfaceData& ethData,
1099 const DHCPParameters& v4dhcpParms,
1100 const DHCPParameters& v6dhcpParms,
1101 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001102{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001103 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1104 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001105
1106 bool nextv4DHCPState =
1107 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1108
1109 bool nextv6DHCPState{};
1110 if (v6dhcpParms.dhcpv6OperatingMode)
1111 {
1112 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1113 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1114 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1115 {
1116 messages::propertyValueFormatError(asyncResp->res,
1117 *v6dhcpParms.dhcpv6OperatingMode,
1118 "OperatingMode");
1119 return;
1120 }
1121 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1122 }
1123 else
1124 {
1125 nextv6DHCPState = ipv6Active;
1126 }
1127
1128 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001129 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001130 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001131 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001132 {
1133 messages::generalError(asyncResp->res);
1134 return;
1135 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001136 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001137 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001138 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001139 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001140 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001141 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001142 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001143 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001144 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001145 }
1146 else
1147 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001148 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001149 }
1150
1151 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001152 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001153 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001154 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001155 {
1156 messages::generalError(asyncResp->res);
1157 return;
1158 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001159 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001160 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001161 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001162 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001163 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001164 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001165 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001166 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001167 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001168 }
1169 else
1170 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001171 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001172 }
1173
1174 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001175 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001176 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001177 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001178 {
1179 messages::generalError(asyncResp->res);
1180 return;
1181 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001182 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001183 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001184 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001185 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001186 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001187 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001188 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001189 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001190 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001191 }
1192 else
1193 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001194 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001195 }
1196
1197 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1198 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1199 asyncResp);
1200 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1201 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1202 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1203 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1204 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1205 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1206}
1207
Ed Tanous77179532023-02-28 10:45:28 -08001208inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1209 const std::vector<IPv4AddressData>::const_iterator& head,
1210 const std::vector<IPv4AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001211{
1212 return std::find_if(head, end, [](const IPv4AddressData& value) {
1213 return value.origin == "Static";
1214 });
1215}
1216
Ed Tanous77179532023-02-28 10:45:28 -08001217inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1218 const std::vector<IPv6AddressData>::const_iterator& head,
1219 const std::vector<IPv6AddressData>::const_iterator& end)
Ed Tanousbf648f72021-06-03 15:00:14 -07001220{
1221 return std::find_if(head, end, [](const IPv6AddressData& value) {
1222 return value.origin == "Static";
1223 });
1224}
1225
Ed Tanous77179532023-02-28 10:45:28 -08001226inline void
Ed Tanousddd70dc2023-03-01 16:00:27 -08001227 handleIPv4StaticPatch(const std::string& ifaceId,
1228 nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001229 const std::vector<IPv4AddressData>& ipv4Data,
1230 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001231{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001232 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001233 {
1234 messages::propertyValueTypeError(
1235 asyncResp->res,
Ed Tanousddd70dc2023-03-01 16:00:27 -08001236 nlohmann::json(input).dump(
1237 2, ' ', true, nlohmann::json::error_handler_t::replace),
Ed Tanousbf648f72021-06-03 15:00:14 -07001238 "IPv4StaticAddresses");
1239 return;
1240 }
1241
1242 unsigned entryIdx = 1;
1243 // Find the first static IP address currently active on the NIC and
1244 // match it to the first JSON element in the IPv4StaticAddresses array.
1245 // Match each subsequent JSON element to the next static IP programmed
1246 // into the NIC.
Ed Tanous77179532023-02-28 10:45:28 -08001247 std::vector<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001248 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1249
1250 for (nlohmann::json& thisJson : input)
1251 {
Patrick Williams89492a12023-05-10 07:51:34 -05001252 std::string pathString = "IPv4StaticAddresses/" +
1253 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001254
1255 if (!thisJson.is_null() && !thisJson.empty())
1256 {
1257 std::optional<std::string> address;
1258 std::optional<std::string> subnetMask;
1259 std::optional<std::string> gateway;
1260
1261 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1262 address, "SubnetMask", subnetMask,
1263 "Gateway", gateway))
1264 {
1265 messages::propertyValueFormatError(
1266 asyncResp->res,
1267 thisJson.dump(2, ' ', true,
1268 nlohmann::json::error_handler_t::replace),
1269 pathString);
1270 return;
1271 }
1272
1273 // Find the address/subnet/gateway values. Any values that are
1274 // not explicitly provided are assumed to be unmodified from the
1275 // current state of the interface. Merge existing state into the
1276 // current request.
1277 const std::string* addr = nullptr;
1278 const std::string* gw = nullptr;
1279 uint8_t prefixLength = 0;
1280 bool errorInEntry = false;
1281 if (address)
1282 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001283 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001284 {
1285 addr = &(*address);
1286 }
1287 else
1288 {
1289 messages::propertyValueFormatError(asyncResp->res, *address,
1290 pathString + "/Address");
1291 errorInEntry = true;
1292 }
1293 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001294 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001295 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001296 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001297 }
1298 else
1299 {
1300 messages::propertyMissing(asyncResp->res,
1301 pathString + "/Address");
1302 errorInEntry = true;
1303 }
1304
1305 if (subnetMask)
1306 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001307 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1308 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001309 {
1310 messages::propertyValueFormatError(
1311 asyncResp->res, *subnetMask,
1312 pathString + "/SubnetMask");
1313 errorInEntry = true;
1314 }
1315 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001316 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001317 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001318 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1319 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001320 {
1321 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001322 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001323 pathString + "/SubnetMask");
1324 errorInEntry = true;
1325 }
1326 }
1327 else
1328 {
1329 messages::propertyMissing(asyncResp->res,
1330 pathString + "/SubnetMask");
1331 errorInEntry = true;
1332 }
1333
1334 if (gateway)
1335 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001336 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001337 {
1338 gw = &(*gateway);
1339 }
1340 else
1341 {
1342 messages::propertyValueFormatError(asyncResp->res, *gateway,
1343 pathString + "/Gateway");
1344 errorInEntry = true;
1345 }
1346 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001347 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001348 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001349 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001350 }
1351 else
1352 {
1353 messages::propertyMissing(asyncResp->res,
1354 pathString + "/Gateway");
1355 errorInEntry = true;
1356 }
1357
1358 if (errorInEntry)
1359 {
1360 return;
1361 }
1362
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001363 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001364 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001365 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
1366 nicIpEntry->id, prefixLength, *gw,
1367 *addr, asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001368 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1369 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001370 }
1371 else
1372 {
1373 createIPv4(ifaceId, prefixLength, *gateway, *address,
1374 asyncResp);
1375 }
1376 entryIdx++;
1377 }
1378 else
1379 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001380 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001381 {
1382 // Requesting a DELETE/DO NOT MODIFY action for an item
1383 // that isn't present on the eth(n) interface. Input JSON is
1384 // in error, so bail out.
1385 if (thisJson.is_null())
1386 {
1387 messages::resourceCannotBeDeleted(asyncResp->res);
1388 return;
1389 }
1390 messages::propertyValueFormatError(
1391 asyncResp->res,
1392 thisJson.dump(2, ' ', true,
1393 nlohmann::json::error_handler_t::replace),
1394 pathString);
1395 return;
1396 }
1397
1398 if (thisJson.is_null())
1399 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001400 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001401 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001402 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001403 {
Patrick Williams89492a12023-05-10 07:51:34 -05001404 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1405 ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001406 }
1407 entryIdx++;
1408 }
1409 }
1410}
1411
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001412inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001413 const std::string& ifaceId,
1414 const std::vector<std::string>& updatedStaticNameServers,
1415 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1416{
1417 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08001418 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001419 if (ec)
1420 {
1421 messages::internalError(asyncResp->res);
1422 return;
1423 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001424 },
1425 "xyz.openbmc_project.Network",
1426 "/xyz/openbmc_project/network/" + ifaceId,
1427 "org.freedesktop.DBus.Properties", "Set",
1428 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001429 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001430}
1431
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001432inline void handleIPv6StaticAddressesPatch(
Ed Tanousddd70dc2023-03-01 16:00:27 -08001433 const std::string& ifaceId, const nlohmann::json::array_t& input,
Ed Tanous77179532023-02-28 10:45:28 -08001434 const std::vector<IPv6AddressData>& ipv6Data,
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1436{
Ed Tanousddd70dc2023-03-01 16:00:27 -08001437 if (input.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001438 {
1439 messages::propertyValueTypeError(
1440 asyncResp->res,
Ed Tanousddd70dc2023-03-01 16:00:27 -08001441 nlohmann::json(input).dump(
1442 2, ' ', true, nlohmann::json::error_handler_t::replace),
Ed Tanousbf648f72021-06-03 15:00:14 -07001443 "IPv6StaticAddresses");
1444 return;
1445 }
1446 size_t entryIdx = 1;
Ed Tanous77179532023-02-28 10:45:28 -08001447 std::vector<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001448 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1449 for (const nlohmann::json& thisJson : input)
1450 {
Patrick Williams89492a12023-05-10 07:51:34 -05001451 std::string pathString = "IPv6StaticAddresses/" +
1452 std::to_string(entryIdx);
Ed Tanousbf648f72021-06-03 15:00:14 -07001453
1454 if (!thisJson.is_null() && !thisJson.empty())
1455 {
1456 std::optional<std::string> address;
1457 std::optional<uint8_t> prefixLength;
1458 nlohmann::json thisJsonCopy = thisJson;
1459 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1460 address, "PrefixLength", prefixLength))
1461 {
1462 messages::propertyValueFormatError(
1463 asyncResp->res,
1464 thisJson.dump(2, ' ', true,
1465 nlohmann::json::error_handler_t::replace),
1466 pathString);
1467 return;
1468 }
1469
Ed Tanous543f4402022-01-06 13:12:53 -08001470 const std::string* addr = nullptr;
1471 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001472
1473 // Find the address and prefixLength values. Any values that are
1474 // not explicitly provided are assumed to be unmodified from the
1475 // current state of the interface. Merge existing state into the
1476 // current request.
1477 if (address)
1478 {
1479 addr = &(*address);
1480 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001481 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001482 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001483 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001484 }
1485 else
1486 {
1487 messages::propertyMissing(asyncResp->res,
1488 pathString + "/Address");
1489 return;
1490 }
1491
1492 if (prefixLength)
1493 {
1494 prefix = *prefixLength;
1495 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001496 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001497 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001498 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001499 }
1500 else
1501 {
1502 messages::propertyMissing(asyncResp->res,
1503 pathString + "/PrefixLength");
1504 return;
1505 }
1506
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001507 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001508 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001509 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
1510 nicIpEntry->id, prefix, "", *addr,
1511 asyncResp);
Patrick Williams89492a12023-05-10 07:51:34 -05001512 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1513 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001514 }
1515 else
1516 {
1517 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1518 }
1519 entryIdx++;
1520 }
1521 else
1522 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001523 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001524 {
1525 // Requesting a DELETE/DO NOT MODIFY action for an item
1526 // that isn't present on the eth(n) interface. Input JSON is
1527 // in error, so bail out.
1528 if (thisJson.is_null())
1529 {
1530 messages::resourceCannotBeDeleted(asyncResp->res);
1531 return;
1532 }
1533 messages::propertyValueFormatError(
1534 asyncResp->res,
1535 thisJson.dump(2, ' ', true,
1536 nlohmann::json::error_handler_t::replace),
1537 pathString);
1538 return;
1539 }
1540
1541 if (thisJson.is_null())
1542 {
Ravi Teja9c5e5852023-02-26 21:33:52 -06001543 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001544 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001545 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001546 {
Patrick Williams89492a12023-05-10 07:51:34 -05001547 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1548 ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001549 }
1550 entryIdx++;
1551 }
1552 }
1553}
1554
Ed Tanous77179532023-02-28 10:45:28 -08001555inline void
1556 parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1557 const std::string& ifaceId,
1558 const EthernetInterfaceData& ethData,
1559 const std::vector<IPv4AddressData>& ipv4Data,
1560 const std::vector<IPv6AddressData>& ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001561{
George Liu7a1dbc42022-12-07 16:03:22 +08001562 constexpr std::array<std::string_view, 1> inventoryForEthernet = {
Ed Tanousbf648f72021-06-03 15:00:14 -07001563 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1564
1565 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1566 jsonResponse["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001567 jsonResponse["@odata.id"] = boost::urls::format(
1568 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001569 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1570
1571 auto health = std::make_shared<HealthPopulate>(asyncResp);
1572
George Liu7a1dbc42022-12-07 16:03:22 +08001573 dbus::utility::getSubTreePaths(
1574 "/", 0, inventoryForEthernet,
1575 [health](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001576 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001577 if (ec)
1578 {
1579 return;
1580 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001581
Ed Tanous002d39b2022-05-31 08:59:27 -07001582 health->inventory = resp;
George Liu7a1dbc42022-12-07 16:03:22 +08001583 });
Ed Tanousbf648f72021-06-03 15:00:14 -07001584
1585 health->populate();
1586
1587 if (ethData.nicEnabled)
1588 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001589 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001590 jsonResponse["Status"]["State"] = "Enabled";
1591 }
1592 else
1593 {
1594 jsonResponse["LinkStatus"] = "NoLink";
1595 jsonResponse["Status"]["State"] = "Disabled";
1596 }
1597
Ed Tanousbf648f72021-06-03 15:00:14 -07001598 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301599 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001600 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001601 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001602 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1603 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1604 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1605 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001606
1607 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001608 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001609 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001610 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1611 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1612 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001613
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001614 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001615 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001616 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001617
1618 // When domain name is empty then it means, that it is a network
1619 // without domain names, and the host name itself must be treated as
1620 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001621 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001622 if (!ethData.domainnames.empty())
1623 {
1624 fqdn += "." + ethData.domainnames[0];
1625 }
1626 jsonResponse["FQDN"] = fqdn;
1627 }
1628
Ed Tanousef4c65b2023-04-24 15:28:50 -07001629 jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1630 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
Ed Tanousbf648f72021-06-03 15:00:14 -07001631
1632 jsonResponse["NameServers"] = ethData.nameServers;
1633 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1634
1635 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1636 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1637 ipv4Array = nlohmann::json::array();
1638 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001639 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001640 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001641 std::string gatewayStr = ipv4Config.gateway;
1642 if (gatewayStr.empty())
1643 {
1644 gatewayStr = "0.0.0.0";
1645 }
Ed Tanous14766872022-03-15 10:44:42 -07001646 nlohmann::json::object_t ipv4;
1647 ipv4["AddressOrigin"] = ipv4Config.origin;
1648 ipv4["SubnetMask"] = ipv4Config.netmask;
1649 ipv4["Address"] = ipv4Config.address;
1650 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001651
Ed Tanousbf648f72021-06-03 15:00:14 -07001652 if (ipv4Config.origin == "Static")
1653 {
Ed Tanous14766872022-03-15 10:44:42 -07001654 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001655 }
Ed Tanous14766872022-03-15 10:44:42 -07001656
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001657 ipv4Array.emplace_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001658 }
1659
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001660 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001661 if (ipv6GatewayStr.empty())
1662 {
1663 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1664 }
1665
1666 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1667
1668 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1669 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1670 ipv6Array = nlohmann::json::array();
1671 ipv6StaticArray = nlohmann::json::array();
1672 nlohmann::json& ipv6AddrPolicyTable =
1673 jsonResponse["IPv6AddressPolicyTable"];
1674 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001675 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001676 {
Ed Tanous14766872022-03-15 10:44:42 -07001677 nlohmann::json::object_t ipv6;
1678 ipv6["Address"] = ipv6Config.address;
1679 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1680 ipv6["AddressOrigin"] = ipv6Config.origin;
Sunitha Harishf8361272023-03-16 03:23:59 -05001681
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001682 ipv6Array.emplace_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001683 if (ipv6Config.origin == "Static")
1684 {
Ed Tanous14766872022-03-15 10:44:42 -07001685 nlohmann::json::object_t ipv6Static;
1686 ipv6Static["Address"] = ipv6Config.address;
1687 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001688 ipv6StaticArray.emplace_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001689 }
1690 }
1691}
1692
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001693inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001694{
Ed Tanous11ba3972022-07-11 09:50:41 -07001695 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001696}
1697
1698inline void requestEthernetInterfacesRoutes(App& app)
1699{
1700 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001701 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001702 .methods(boost::beast::http::verb::get)(
1703 [&app](const crow::Request& req,
1704 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001705 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001706 {
1707 return;
1708 }
1709
1710 asyncResp->res.jsonValue["@odata.type"] =
1711 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1712 asyncResp->res.jsonValue["@odata.id"] =
1713 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1714 asyncResp->res.jsonValue["Name"] =
1715 "Ethernet Network Interface Collection";
1716 asyncResp->res.jsonValue["Description"] =
1717 "Collection of EthernetInterfaces for this Manager";
1718
1719 // Get eth interface list, and call the below callback for JSON
1720 // preparation
1721 getEthernetIfaceList(
Ed Tanous77179532023-02-28 10:45:28 -08001722 [asyncResp](const bool& success,
1723 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001724 if (!success)
1725 {
1726 messages::internalError(asyncResp->res);
1727 return;
1728 }
1729
1730 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1731 ifaceArray = nlohmann::json::array();
1732 std::string tag = "_";
1733 for (const std::string& ifaceItem : ifaceList)
1734 {
1735 std::size_t found = ifaceItem.find(tag);
1736 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001737 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001738 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001739 iface["@odata.id"] = boost::urls::format(
1740 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1741 ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05001742 ifaceArray.emplace_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001743 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001744 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001745
Ed Tanous002d39b2022-05-31 08:59:27 -07001746 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1747 asyncResp->res.jsonValue["@odata.id"] =
1748 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1749 });
1750 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001751
Ed Tanousbf648f72021-06-03 15:00:14 -07001752 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001753 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001754 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001755 [&app](const crow::Request& req,
1756 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1757 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001758 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001759 {
1760 return;
1761 }
1762 getEthernetIfaceData(
1763 ifaceId,
Ed Tanous77179532023-02-28 10:45:28 -08001764 [asyncResp, ifaceId](const bool& success,
1765 const EthernetInterfaceData& ethData,
1766 const std::vector<IPv4AddressData>& ipv4Data,
1767 const std::vector<IPv6AddressData>& ipv6Data) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001768 if (!success)
1769 {
1770 // TODO(Pawel)consider distinguish between non
1771 // existing object, and other errors
1772 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1773 ifaceId);
1774 return;
1775 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001776
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001777 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1778 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001779 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001780 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001781 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1782 asyncResp->res.jsonValue["Description"] =
1783 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301784
Ed Tanous002d39b2022-05-31 08:59:27 -07001785 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001786 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001787 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001788
Ed Tanousbf648f72021-06-03 15:00:14 -07001789 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001790 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001791 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001792 [&app](const crow::Request& req,
1793 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1794 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001795 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001796 {
1797 return;
1798 }
1799 std::optional<std::string> hostname;
1800 std::optional<std::string> fqdn;
1801 std::optional<std::string> macAddress;
1802 std::optional<std::string> ipv6DefaultGateway;
Ed Tanousddd70dc2023-03-01 16:00:27 -08001803 std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
1804 std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07001805 std::optional<std::vector<std::string>> staticNameServers;
1806 std::optional<nlohmann::json> dhcpv4;
1807 std::optional<nlohmann::json> dhcpv6;
1808 std::optional<bool> interfaceEnabled;
1809 std::optional<size_t> mtuSize;
1810 DHCPParameters v4dhcpParms;
1811 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001812
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 if (!json_util::readJsonPatch(
1814 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1815 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1816 macAddress, "StaticNameServers", staticNameServers,
1817 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1818 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1819 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1820 {
1821 return;
1822 }
1823 if (dhcpv4)
1824 {
1825 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1826 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1827 v4dhcpParms.useDnsServers, "UseNTPServers",
1828 v4dhcpParms.useNtpServers, "UseDomainName",
1829 v4dhcpParms.useDomainName))
1830 {
1831 return;
1832 }
1833 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001834
Ed Tanous002d39b2022-05-31 08:59:27 -07001835 if (dhcpv6)
1836 {
1837 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1838 v6dhcpParms.dhcpv6OperatingMode,
1839 "UseDNSServers", v6dhcpParms.useDnsServers,
1840 "UseNTPServers", v6dhcpParms.useNtpServers,
1841 "UseDomainName",
1842 v6dhcpParms.useDomainName))
1843 {
1844 return;
1845 }
1846 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001847
Ed Tanous002d39b2022-05-31 08:59:27 -07001848 // Get single eth interface data, and call the below callback
1849 // for JSON preparation
1850 getEthernetIfaceData(
1851 ifaceId,
1852 [asyncResp, ifaceId, hostname = std::move(hostname),
1853 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1854 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1855 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1856 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1857 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001858 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1859 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001860 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1861 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanous77179532023-02-28 10:45:28 -08001862 const std::vector<IPv4AddressData>& ipv4Data,
1863 const std::vector<IPv6AddressData>& ipv6Data) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001864 if (!success)
1865 {
1866 // ... otherwise return error
1867 // TODO(Pawel)consider distinguish between non
1868 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001869 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001870 ifaceId);
1871 return;
1872 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001873
Ed Tanous002d39b2022-05-31 08:59:27 -07001874 if (dhcpv4 || dhcpv6)
1875 {
1876 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001877 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001878 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301879
Ed Tanous002d39b2022-05-31 08:59:27 -07001880 if (hostname)
1881 {
1882 handleHostnamePatch(*hostname, asyncResp);
1883 }
1884
1885 if (fqdn)
1886 {
1887 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1888 }
1889
1890 if (macAddress)
1891 {
1892 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1893 }
1894
1895 if (ipv4StaticAddresses)
1896 {
1897 // TODO(ed) for some reason the capture of
1898 // ipv4Addresses above is returning a const value,
1899 // not a non-const value. This doesn't really work
1900 // for us, as we need to be able to efficiently move
1901 // out the intermedia nlohmann::json objects. This
1902 // makes a copy of the structure, and operates on
1903 // that, but could be done more efficiently
Ed Tanousddd70dc2023-03-01 16:00:27 -08001904 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
Ed Tanous002d39b2022-05-31 08:59:27 -07001905 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1906 }
1907
1908 if (staticNameServers)
1909 {
1910 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1911 asyncResp);
1912 }
1913
1914 if (ipv6DefaultGateway)
1915 {
1916 messages::propertyNotWritable(asyncResp->res,
1917 "IPv6DefaultGateway");
1918 }
1919
1920 if (ipv6StaticAddresses)
1921 {
Ed Tanousddd70dc2023-03-01 16:00:27 -08001922 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
1923 ipv6Data, asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001924 }
1925
1926 if (interfaceEnabled)
1927 {
1928 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1929 *interfaceEnabled, asyncResp);
1930 }
1931
1932 if (mtuSize)
1933 {
1934 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
1935 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001936 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001937 });
manojkiraneda2a133282019-02-19 13:09:43 +05301938
Ed Tanousbf648f72021-06-03 15:00:14 -07001939 BMCWEB_ROUTE(
1940 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001941 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001942 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001943 [&app](const crow::Request& req,
1944 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1945 const std::string& parentIfaceId,
1946 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001947 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001948 {
1949 return;
1950 }
1951 asyncResp->res.jsonValue["@odata.type"] =
1952 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1953 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001954
Ed Tanous002d39b2022-05-31 08:59:27 -07001955 if (!verifyNames(parentIfaceId, ifaceId))
1956 {
1957 return;
1958 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001959
Ed Tanous002d39b2022-05-31 08:59:27 -07001960 // Get single eth interface data, and call the below callback
1961 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08001962 getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
1963 const bool& success,
1964 const EthernetInterfaceData& ethData,
1965 const std::vector<IPv4AddressData>&,
1966 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001967 if (success && ethData.vlanId)
1968 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001969 asyncResp->res.jsonValue["Id"] = ifaceId;
Ed Tanousef4c65b2023-04-24 15:28:50 -07001970 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1971 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1972 parentIfaceId, ifaceId);
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001973
Jiaqing Zhao23a06312022-09-13 09:38:25 +08001974 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08001975 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07001976 }
1977 else
1978 {
1979 // ... otherwise return error
1980 // TODO(Pawel)consider distinguish between non
1981 // existing object, and other errors
1982 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001983 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07001984 }
Ed Tanous77179532023-02-28 10:45:28 -08001985 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001986 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001987
Ed Tanousbf648f72021-06-03 15:00:14 -07001988 BMCWEB_ROUTE(
1989 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05001990 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001991 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001992 [&app](const crow::Request& req,
1993 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1994 const std::string& parentIfaceId,
1995 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001996 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001997 {
1998 return;
1999 }
2000 if (!verifyNames(parentIfaceId, ifaceId))
2001 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002002 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002003 ifaceId);
2004 return;
2005 }
2006
2007 std::optional<bool> vlanEnable;
2008 std::optional<uint32_t> vlanId;
2009
2010 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2011 vlanEnable, "VLANId", vlanId))
2012 {
2013 return;
2014 }
2015
2016 if (vlanId)
2017 {
2018 messages::propertyNotWritable(asyncResp->res, "VLANId");
2019 return;
2020 }
2021
2022 // Get single eth interface data, and call the below callback
2023 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08002024 getEthernetIfaceData(ifaceId,
2025 [asyncResp, parentIfaceId, ifaceId,
2026 vlanEnable](const bool& success,
2027 const EthernetInterfaceData& ethData,
2028 const std::vector<IPv4AddressData>&,
2029 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002030 if (success && ethData.vlanId)
2031 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002032 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002033 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002034 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002035 [asyncResp](const boost::system::error_code& ec) {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002036 if (ec)
2037 {
2038 messages::internalError(asyncResp->res);
2039 return;
2040 }
2041 },
2042 "xyz.openbmc_project.Network",
2043 "/xyz/openbmc_project/network/" + ifaceId,
2044 "org.freedesktop.DBus.Properties", "Set",
2045 "xyz.openbmc_project.Network.EthernetInterface",
2046 "NICEnabled",
2047 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002048 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002049 }
2050 else
2051 {
2052 // TODO(Pawel)consider distinguish between non
2053 // existing object, and other errors
2054 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002055 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002056 return;
2057 }
Ed Tanous77179532023-02-28 10:45:28 -08002058 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002059 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002060
2061 BMCWEB_ROUTE(
2062 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002063 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002064 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002065 [&app](const crow::Request& req,
2066 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2067 const std::string& parentIfaceId,
2068 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002069 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002070 {
2071 return;
2072 }
2073 if (!verifyNames(parentIfaceId, ifaceId))
2074 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002075 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002076 ifaceId);
2077 return;
2078 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002079
Ed Tanous002d39b2022-05-31 08:59:27 -07002080 // Get single eth interface data, and call the below callback
2081 // for JSON preparation
Ed Tanous77179532023-02-28 10:45:28 -08002082 getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
2083 const bool& success,
2084 const EthernetInterfaceData& ethData,
2085 const std::vector<IPv4AddressData>&,
2086 const std::vector<IPv6AddressData>&) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002087 if (success && ethData.vlanId)
2088 {
2089 auto callback =
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002090 [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002091 if (ec)
2092 {
2093 messages::internalError(asyncResp->res);
2094 }
2095 };
2096 crow::connections::systemBus->async_method_call(
2097 std::move(callback), "xyz.openbmc_project.Network",
2098 std::string("/xyz/openbmc_project/network/") + ifaceId,
2099 "xyz.openbmc_project.Object.Delete", "Delete");
2100 }
2101 else
2102 {
2103 // ... otherwise return error
2104 // TODO(Pawel)consider distinguish between non
2105 // existing object, and other errors
2106 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002107 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002108 }
Ed Tanous77179532023-02-28 10:45:28 -08002109 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002110 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002111
Ed Tanousbf648f72021-06-03 15:00:14 -07002112 BMCWEB_ROUTE(app,
2113 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002114
2115 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002116 .methods(boost::beast::http::verb::get)(
2117 [&app](const crow::Request& req,
2118 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2119 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002120 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002121 {
2122 return;
2123 }
2124 // Get eth interface list, and call the below callback for JSON
2125 // preparation
Ed Tanous77179532023-02-28 10:45:28 -08002126 getEthernetIfaceList([asyncResp, rootInterfaceName](
2127 const bool& success,
2128 const std::vector<std::string>& ifaceList) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002129 if (!success)
2130 {
2131 messages::internalError(asyncResp->res);
2132 return;
2133 }
Ed Tanous77179532023-02-28 10:45:28 -08002134 if (std::find(ifaceList.begin(), ifaceList.end(),
2135 rootInterfaceName) == ifaceList.end())
Ed Tanous002d39b2022-05-31 08:59:27 -07002136 {
2137 messages::resourceNotFound(asyncResp->res,
2138 "VLanNetworkInterfaceCollection",
2139 rootInterfaceName);
2140 return;
2141 }
2142
2143 asyncResp->res.jsonValue["@odata.type"] =
2144 "#VLanNetworkInterfaceCollection."
2145 "VLanNetworkInterfaceCollection";
2146 asyncResp->res.jsonValue["Name"] =
2147 "VLAN Network Interface Collection";
2148
2149 nlohmann::json ifaceArray = nlohmann::json::array();
2150
2151 for (const std::string& ifaceItem : ifaceList)
2152 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002153 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002154 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002155 nlohmann::json::object_t iface;
Ed Tanousef4c65b2023-04-24 15:28:50 -07002156 iface["@odata.id"] = boost::urls::format(
2157 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2158 rootInterfaceName, ifaceItem);
Patrick Williamsb2ba3072023-05-12 10:27:39 -05002159 ifaceArray.emplace_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002160 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002161 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002162
Ed Tanous002d39b2022-05-31 08:59:27 -07002163 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2164 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
Ed Tanousef4c65b2023-04-24 15:28:50 -07002165 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2166 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2167 rootInterfaceName);
Ed Tanous002d39b2022-05-31 08:59:27 -07002168 });
2169 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002170
Ed Tanousbf648f72021-06-03 15:00:14 -07002171 BMCWEB_ROUTE(app,
2172 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002173 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002174 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002175 [&app](const crow::Request& req,
2176 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2177 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002178 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002179 {
2180 return;
2181 }
2182 bool vlanEnable = false;
2183 uint32_t vlanId = 0;
2184 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2185 "VLANEnable", vlanEnable))
2186 {
2187 return;
2188 }
2189 // Need both vlanId and vlanEnable to service this request
2190 if (vlanId == 0U)
2191 {
2192 messages::propertyMissing(asyncResp->res, "VLANId");
2193 }
2194 if (!vlanEnable)
2195 {
2196 messages::propertyMissing(asyncResp->res, "VLANEnable");
2197 }
2198 if (static_cast<bool>(vlanId) ^ vlanEnable)
2199 {
2200 return;
2201 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002202
Ed Tanous5e7e2dc2023-02-16 10:37:01 -08002203 auto callback = [asyncResp](const boost::system::error_code& ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07002204 if (ec)
2205 {
2206 // TODO(ed) make more consistent error messages
2207 // based on phosphor-network responses
2208 messages::internalError(asyncResp->res);
2209 return;
2210 }
2211 messages::created(asyncResp->res);
2212 };
2213 crow::connections::systemBus->async_method_call(
2214 std::move(callback), "xyz.openbmc_project.Network",
2215 "/xyz/openbmc_project/network",
2216 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2217 rootInterfaceName, vlanId);
2218 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002219}
2220
Ed Tanous1abe55e2018-09-05 08:30:59 -07002221} // namespace redfish