blob: d957ad846148c709de5567a67d1cddfbc285a0aa [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 Tanous033f1e42022-08-15 09:47:37 -070018#include "utils/ip_utils.hpp"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
Ed Tanous11ba3972022-07-11 09:50:41 -070021#include <boost/algorithm/string/classification.hpp>
22#include <boost/algorithm/string/split.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070023#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020024#include <dbus_singleton.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080025#include <dbus_utility.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020026#include <error_messages.hpp>
Ed Tanous45ca1b82022-03-25 13:07:27 -070027#include <query.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070028#include <registries/privilege_registry.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029#include <utils/json_utils.hpp>
30
Ed Tanousa24526d2018-12-10 15:17:59 -080031#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053032#include <regex>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034namespace redfish
35{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010036
Ed Tanous4a0cb852018-10-15 07:55:04 -070037enum class LinkType
38{
39 Local,
40 Global
41};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010042
43/**
44 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010045 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070046struct IPv4AddressData
47{
48 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070049 std::string address;
50 std::string domain;
51 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 std::string netmask;
53 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070054 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050055 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070056
Gunnar Mills1214b7e2020-06-04 10:11:30 -050057 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070058 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070059 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070060 }
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;
71 uint8_t prefixLength;
72
Gunnar Mills1214b7e2020-06-04 10:11:30 -050073 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050074 {
75 return id < obj.id;
76 }
77};
78/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010079 * Structure for keeping basic single Ethernet Interface information
80 * available from DBus
81 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070082struct EthernetInterfaceData
83{
Ed Tanous4a0cb852018-10-15 07:55:04 -070084 uint32_t speed;
Tejas Patil35fb5312021-09-20 15:35:20 +053085 size_t mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080086 bool autoNeg;
87 bool dnsEnabled;
88 bool ntpEnabled;
89 bool hostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -080090 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -070091 bool nicEnabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080092 std::string dhcpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070093 std::string operatingMode;
Jiaqing Zhao82695a52022-04-14 15:15:59 +080094 std::string hostName;
95 std::string defaultGateway;
96 std::string ipv6DefaultGateway;
97 std::string macAddress;
Jiaqing Zhao17e22022022-04-14 18:58:06 +080098 std::optional<uint32_t> vlanId;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -050099 std::vector<std::string> nameServers;
100 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800101 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100102};
103
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700104struct DHCPParameters
105{
106 std::optional<bool> dhcpv4Enabled;
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800107 std::optional<bool> useDnsServers;
108 std::optional<bool> useNtpServers;
109 std::optional<bool> useDomainName;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700110 std::optional<std::string> dhcpv6OperatingMode;
111};
112
Ed Tanous4a0cb852018-10-15 07:55:04 -0700113// Helper function that changes bits netmask notation (i.e. /24)
114// into full dot notation
115inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700117 uint32_t value = 0xffffffff << (32 - bits);
118 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
119 std::to_string((value >> 16) & 0xff) + "." +
120 std::to_string((value >> 8) & 0xff) + "." +
121 std::to_string(value & 0xff);
122 return netmask;
123}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100124
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800125inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700126 bool isIPv4)
127{
128 if (isIPv4)
129 {
130 return (
131 (inputDHCP ==
132 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
133 (inputDHCP ==
134 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
135 }
136 return ((inputDHCP ==
137 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
138 (inputDHCP ==
139 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
140}
141
Ed Tanous2c70f802020-09-28 14:29:23 -0700142inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700143{
144 if (isIPv4 && isIPv6)
145 {
146 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
147 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700148 if (isIPv4)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700149 {
150 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
151 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700152 if (isIPv6)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700153 {
154 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
155 }
156 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
157}
158
Ed Tanous4a0cb852018-10-15 07:55:04 -0700159inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500160 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700161 bool isIPv4)
162{
163 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700164 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700165 return "Static";
166 }
167 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
168 {
169 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171 return "IPv4LinkLocal";
172 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700173 return "LinkLocal";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700174 }
175 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
176 {
177 if (isIPv4)
178 {
179 return "DHCP";
180 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700181 return "DHCPv6";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700182 }
183 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
184 {
185 return "SLAAC";
186 }
187 return "";
188}
189
Ed Tanous02cad962022-06-30 16:50:15 -0700190inline bool extractEthernetInterfaceData(
191 const std::string& ethifaceId,
192 const dbus::utility::ManagedObjectType& dbusData,
193 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700194{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700195 bool idFound = false;
Ed Tanous02cad962022-06-30 16:50:15 -0700196 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197 {
Ed Tanous02cad962022-06-30 16:50:15 -0700198 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700199 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000200 if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700202 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700203 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700204 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500205 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700208 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500209 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800210 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 if (mac != nullptr)
212 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800213 ethData.macAddress = *mac;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700215 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 }
217 }
218 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
219 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500220 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 {
222 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500224 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800225 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700226 if (id != nullptr)
227 {
Jiaqing Zhao17e22022022-04-14 18:58:06 +0800228 ethData.vlanId = *id;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 }
232 }
233 else if (ifacePair.first ==
234 "xyz.openbmc_project.Network.EthernetInterface")
235 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500236 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700237 {
238 if (propertyPair.first == "AutoNeg")
239 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700240 const bool* autoNeg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800241 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700242 if (autoNeg != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800244 ethData.autoNeg = *autoNeg;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700245 }
246 }
247 else if (propertyPair.first == "Speed")
248 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500249 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800250 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700251 if (speed != nullptr)
252 {
253 ethData.speed = *speed;
254 }
255 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530256 else if (propertyPair.first == "MTU")
257 {
258 const uint32_t* mtuSize =
259 std::get_if<uint32_t>(&propertyPair.second);
260 if (mtuSize != nullptr)
261 {
262 ethData.mtuSize = *mtuSize;
263 }
264 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800265 else if (propertyPair.first == "LinkUp")
266 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500267 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800268 std::get_if<bool>(&propertyPair.second);
269 if (linkUp != nullptr)
270 {
271 ethData.linkUp = *linkUp;
272 }
273 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700274 else if (propertyPair.first == "NICEnabled")
275 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500276 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700277 std::get_if<bool>(&propertyPair.second);
278 if (nicEnabled != nullptr)
279 {
280 ethData.nicEnabled = *nicEnabled;
281 }
282 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500283 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700284 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500285 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500286 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800287 &propertyPair.second);
288 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700289 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700290 ethData.nameServers = *nameservers;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500291 }
292 }
293 else if (propertyPair.first == "StaticNameServers")
294 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500295 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500296 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500297 &propertyPair.second);
298 if (staticNameServers != nullptr)
299 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700300 ethData.staticNameServers = *staticNameServers;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700301 }
302 }
manojkiraneda2a133282019-02-19 13:09:43 +0530303 else if (propertyPair.first == "DHCPEnabled")
304 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700305 const std::string* dhcpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700306 std::get_if<std::string>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700307 if (dhcpEnabled != nullptr)
manojkiraneda2a133282019-02-19 13:09:43 +0530308 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800309 ethData.dhcpEnabled = *dhcpEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +0530310 }
311 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800312 else if (propertyPair.first == "DomainName")
313 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500314 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500315 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800316 &propertyPair.second);
317 if (domainNames != nullptr)
318 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700319 ethData.domainnames = *domainNames;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800320 }
321 }
Ravi Teja9010ec22019-08-01 23:30:25 -0500322 else if (propertyPair.first == "DefaultGateway")
323 {
324 const std::string* defaultGateway =
325 std::get_if<std::string>(&propertyPair.second);
326 if (defaultGateway != nullptr)
327 {
328 std::string defaultGatewayStr = *defaultGateway;
329 if (defaultGatewayStr.empty())
330 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800331 ethData.defaultGateway = "0.0.0.0";
Ravi Teja9010ec22019-08-01 23:30:25 -0500332 }
333 else
334 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800335 ethData.defaultGateway = defaultGatewayStr;
Ravi Teja9010ec22019-08-01 23:30:25 -0500336 }
337 }
338 }
339 else if (propertyPair.first == "DefaultGateway6")
340 {
341 const std::string* defaultGateway6 =
342 std::get_if<std::string>(&propertyPair.second);
343 if (defaultGateway6 != nullptr)
344 {
345 std::string defaultGateway6Str =
346 *defaultGateway6;
347 if (defaultGateway6Str.empty())
348 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800349 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500350 "0:0:0:0:0:0:0:0";
351 }
352 else
353 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800354 ethData.ipv6DefaultGateway =
Ravi Teja9010ec22019-08-01 23:30:25 -0500355 defaultGateway6Str;
356 }
357 }
358 }
Ed Tanous029573d2019-02-01 10:57:49 -0800359 }
360 }
361 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700362
Jian Zhang1e3f85e2022-12-13 13:50:35 +0800363 if (objpath.first == "/xyz/openbmc_project/network/dhcp")
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700364 {
365 if (ifacePair.first ==
366 "xyz.openbmc_project.Network.DHCPConfiguration")
367 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500368 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700369 {
370 if (propertyPair.first == "DNSEnabled")
371 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 const bool* dnsEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700374 if (dnsEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700375 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800376 ethData.dnsEnabled = *dnsEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700377 }
378 }
379 else if (propertyPair.first == "NTPEnabled")
380 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700381 const bool* ntpEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700382 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700383 if (ntpEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700384 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800385 ethData.ntpEnabled = *ntpEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700386 }
387 }
388 else if (propertyPair.first == "HostNameEnabled")
389 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700390 const bool* hostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700391 std::get_if<bool>(&propertyPair.second);
Ed Tanous2c70f802020-09-28 14:29:23 -0700392 if (hostNameEnabled != nullptr)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700393 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800394 ethData.hostNameEnabled = *hostNameEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700395 }
396 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700397 }
398 }
399 }
Ed Tanous029573d2019-02-01 10:57:49 -0800400 // System configuration shows up in the global namespace, so no need
401 // to check eth number
402 if (ifacePair.first ==
403 "xyz.openbmc_project.Network.SystemConfiguration")
404 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500405 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800406 {
407 if (propertyPair.first == "HostName")
408 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500409 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500410 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800411 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700412 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +0800413 ethData.hostName = *hostname;
Ed Tanous029573d2019-02-01 10:57:49 -0800414 }
415 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700416 }
417 }
418 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700420 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700421}
422
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500423// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700424inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000425 extractIPV6Data(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800426 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000427 boost::container::flat_set<IPv6AddressData>& ipv6Config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500428{
429 const std::string ipv6PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000430 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500431
432 // Since there might be several IPv6 configurations aligned with
433 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000434 for (const auto& objpath : dbusData)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500435 {
436 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700437 if (objpath.first.str.starts_with(ipv6PathStart))
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500438 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800439 for (const auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500440 {
441 if (interface.first == "xyz.openbmc_project.Network.IP")
442 {
443 // Instance IPv6AddressData structure, and set as
444 // appropriate
445 std::pair<
446 boost::container::flat_set<IPv6AddressData>::iterator,
447 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000448 it = ipv6Config.insert(IPv6AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700449 IPv6AddressData& ipv6Address = *it.first;
450 ipv6Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700451 objpath.first.str.substr(ipv6PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800452 for (const auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500453 {
454 if (property.first == "Address")
455 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500456 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500457 std::get_if<std::string>(&property.second);
458 if (address != nullptr)
459 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700460 ipv6Address.address = *address;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500461 }
462 }
463 else if (property.first == "Origin")
464 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500465 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 std::get_if<std::string>(&property.second);
467 if (origin != nullptr)
468 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700469 ipv6Address.origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500470 translateAddressOriginDbusToRedfish(*origin,
471 false);
472 }
473 }
474 else if (property.first == "PrefixLength")
475 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500476 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500477 std::get_if<uint8_t>(&property.second);
478 if (prefix != nullptr)
479 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700480 ipv6Address.prefixLength = *prefix;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500481 }
482 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600483 else if (property.first == "Type" ||
484 property.first == "Gateway")
485 {
486 // Type & Gateway is not used
487 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500488 else
489 {
490 BMCWEB_LOG_ERROR
491 << "Got extra property: " << property.first
492 << " on the " << objpath.first.str << " object";
493 }
494 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500495 }
496 }
497 }
498 }
499}
500
Ed Tanous4a0cb852018-10-15 07:55:04 -0700501// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700502inline void
Ed Tanous81ce6092020-12-17 16:54:55 +0000503 extractIPData(const std::string& ethifaceId,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800504 const dbus::utility::ManagedObjectType& dbusData,
Ed Tanous81ce6092020-12-17 16:54:55 +0000505 boost::container::flat_set<IPv4AddressData>& ipv4Config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700506{
507 const std::string ipv4PathStart =
Ed Tanous81ce6092020-12-17 16:54:55 +0000508 "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
Ed Tanous4a0cb852018-10-15 07:55:04 -0700509
510 // Since there might be several IPv4 configurations aligned with
511 // single ethernet interface, loop over all of them
Ed Tanous81ce6092020-12-17 16:54:55 +0000512 for (const auto& objpath : dbusData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700513 {
514 // Check if proper pattern for object path appears
Ed Tanous11ba3972022-07-11 09:50:41 -0700515 if (objpath.first.str.starts_with(ipv4PathStart))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700516 {
Ed Tanous9eb808c2022-01-25 10:19:23 -0800517 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700518 {
519 if (interface.first == "xyz.openbmc_project.Network.IP")
520 {
521 // Instance IPv4AddressData structure, and set as
522 // appropriate
523 std::pair<
524 boost::container::flat_set<IPv4AddressData>::iterator,
525 bool>
Ed Tanous81ce6092020-12-17 16:54:55 +0000526 it = ipv4Config.insert(IPv4AddressData{});
Ed Tanous2c70f802020-09-28 14:29:23 -0700527 IPv4AddressData& ipv4Address = *it.first;
528 ipv4Address.id =
Ed Tanous271584a2019-07-09 16:24:22 -0700529 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous9eb808c2022-01-25 10:19:23 -0800530 for (const auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700531 {
532 if (property.first == "Address")
533 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500534 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800535 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700536 if (address != nullptr)
537 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700538 ipv4Address.address = *address;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700539 }
540 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 else if (property.first == "Origin")
542 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500543 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800544 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700545 if (origin != nullptr)
546 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700547 ipv4Address.origin =
Ed Tanous4a0cb852018-10-15 07:55:04 -0700548 translateAddressOriginDbusToRedfish(*origin,
549 true);
550 }
551 }
552 else if (property.first == "PrefixLength")
553 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500554 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800555 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700556 if (mask != nullptr)
557 {
558 // convert it to the string
Ed Tanous2c70f802020-09-28 14:29:23 -0700559 ipv4Address.netmask = getNetmask(*mask);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700560 }
561 }
Asmitha Karunanithi889ff692021-11-29 08:43:30 -0600562 else if (property.first == "Type" ||
563 property.first == "Gateway")
564 {
565 // Type & Gateway is not used
566 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700567 else
568 {
569 BMCWEB_LOG_ERROR
570 << "Got extra property: " << property.first
571 << " on the " << objpath.first.str << " object";
572 }
573 }
574 // Check if given address is local, or global
Ed Tanous2c70f802020-09-28 14:29:23 -0700575 ipv4Address.linktype =
Ed Tanous11ba3972022-07-11 09:50:41 -0700576 ipv4Address.address.starts_with("169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700577 ? LinkType::Local
578 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700579 }
580 }
581 }
582 }
583}
584
585/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700586 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700587 *
588 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700589 * @param[in] ipHash DBus Hash id of IP that should be deleted
590 * @param[io] asyncResp Response object that will be returned to client
591 *
592 * @return None
593 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500594inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800595 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700596{
597 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700598 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700599 if (ec)
600 {
601 messages::internalError(asyncResp->res);
602 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603 },
604 "xyz.openbmc_project.Network",
605 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
606 "xyz.openbmc_project.Object.Delete", "Delete");
607}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608
Gunnar Mills244b6d52021-04-12 15:44:23 -0500609inline void updateIPv4DefaultGateway(
610 const std::string& ifaceId, const std::string& gateway,
611 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Teja9010ec22019-08-01 23:30:25 -0500612{
613 crow::connections::systemBus->async_method_call(
614 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700615 if (ec)
616 {
617 messages::internalError(asyncResp->res);
618 return;
619 }
620 asyncResp->res.result(boost::beast::http::status::no_content);
Ravi Teja9010ec22019-08-01 23:30:25 -0500621 },
622 "xyz.openbmc_project.Network",
623 "/xyz/openbmc_project/network/" + ifaceId,
624 "org.freedesktop.DBus.Properties", "Set",
625 "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
Ed Tanous168e20c2021-12-13 14:39:53 -0800626 dbus::utility::DbusVariantType(gateway));
Ravi Teja9010ec22019-08-01 23:30:25 -0500627}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700629 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700631 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
632 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
633 * @param[in] gateway IPv4 address of this interfaces gateway
634 * @param[in] address IPv4 address to assign to this interface
635 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700636 *
637 * @return None
638 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000639inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
640 const std::string& gateway, const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800641 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642{
Ed Tanous002d39b2022-05-31 08:59:27 -0700643 auto createIpHandler =
644 [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
Ravi Teja9010ec22019-08-01 23:30:25 -0500645 if (ec)
646 {
647 messages::internalError(asyncResp->res);
648 return;
649 }
650 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
651 };
652
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 crow::connections::systemBus->async_method_call(
Ravi Teja9010ec22019-08-01 23:30:25 -0500654 std::move(createIpHandler), "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 "/xyz/openbmc_project/network/" + ifaceId,
656 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700657 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700658 gateway);
659}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500660
661/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700662 * @brief Deletes the IPv4 entry for this interface and creates a replacement
663 * static IPv4 entry
664 *
665 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
666 * @param[in] id The unique hash entry identifying the DBus entry
667 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
668 * @param[in] gateway IPv4 address of this interfaces gateway
669 * @param[in] address IPv4 address to assign to this interface
670 * @param[io] asyncResp Response object that will be returned to client
671 *
672 * @return None
673 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800674inline void
675 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
676 uint8_t prefixLength, const std::string& gateway,
677 const std::string& address,
678 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700679{
680 crow::connections::systemBus->async_method_call(
681 [asyncResp, ifaceId, address, prefixLength,
682 gateway](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700683 if (ec)
684 {
685 messages::internalError(asyncResp->res);
686 return;
687 }
688
689 crow::connections::systemBus->async_method_call(
690 [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
691 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700692 {
693 messages::internalError(asyncResp->res);
Ravi Teja9010ec22019-08-01 23:30:25 -0500694 return;
Johnathan Mantey01784822019-06-18 12:44:21 -0700695 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700696 updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
697 },
698 "xyz.openbmc_project.Network",
699 "/xyz/openbmc_project/network/" + ifaceId,
700 "xyz.openbmc_project.Network.IP.Create", "IP",
701 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
702 prefixLength, gateway);
Johnathan Mantey01784822019-06-18 12:44:21 -0700703 },
704 "xyz.openbmc_project.Network",
705 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
706 "xyz.openbmc_project.Object.Delete", "Delete");
707}
708
709/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500710 * @brief Deletes given IPv6
711 *
712 * @param[in] ifaceId Id of interface whose IP should be deleted
713 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500714 * @param[io] asyncResp Response object that will be returned to client
715 *
716 * @return None
717 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500718inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
zhanghch058d1b46d2021-04-01 11:18:24 +0800719 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500720{
721 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700722 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700723 if (ec)
724 {
725 messages::internalError(asyncResp->res);
726 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500727 },
728 "xyz.openbmc_project.Network",
729 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
730 "xyz.openbmc_project.Object.Delete", "Delete");
731}
732
733/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700734 * @brief Deletes the IPv6 entry for this interface and creates a replacement
735 * static IPv6 entry
736 *
737 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
738 * @param[in] id The unique hash entry identifying the DBus entry
739 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
740 * @param[in] address IPv6 address to assign to this interface
741 * @param[io] asyncResp Response object that will be returned to client
742 *
743 * @return None
744 */
zhanghch058d1b46d2021-04-01 11:18:24 +0800745inline void
746 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
747 uint8_t prefixLength, const std::string& address,
748 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Johnathan Mantey01784822019-06-18 12:44:21 -0700749{
750 crow::connections::systemBus->async_method_call(
751 [asyncResp, ifaceId, address,
752 prefixLength](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700753 if (ec)
754 {
755 messages::internalError(asyncResp->res);
756 }
757 crow::connections::systemBus->async_method_call(
758 [asyncResp](const boost::system::error_code ec2) {
759 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700760 {
761 messages::internalError(asyncResp->res);
762 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700763 },
764 "xyz.openbmc_project.Network",
765 "/xyz/openbmc_project/network/" + ifaceId,
766 "xyz.openbmc_project.Network.IP.Create", "IP",
767 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
768 prefixLength, "");
Johnathan Mantey01784822019-06-18 12:44:21 -0700769 },
770 "xyz.openbmc_project.Network",
771 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
772 "xyz.openbmc_project.Object.Delete", "Delete");
773}
774
775/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500776 * @brief Creates IPv6 with given data
777 *
778 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500779 * @param[in] prefixLength Prefix length that needs to be added
780 * @param[in] address IP address that needs to be added
781 * @param[io] asyncResp Response object that will be returned to client
782 *
783 * @return None
784 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500785inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
786 const std::string& address,
zhanghch058d1b46d2021-04-01 11:18:24 +0800787 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500788{
789 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
790 if (ec)
791 {
792 messages::internalError(asyncResp->res);
793 }
794 };
795 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500796 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500797 crow::connections::systemBus->async_method_call(
798 std::move(createIpHandler), "xyz.openbmc_project.Network",
799 "/xyz/openbmc_project/network/" + ifaceId,
800 "xyz.openbmc_project.Network.IP.Create", "IP",
801 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
802 "");
803}
804
Ed Tanous4a0cb852018-10-15 07:55:04 -0700805/**
806 * Function that retrieves all properties for given Ethernet Interface
807 * Object
808 * from EntityManager Network Manager
809 * @param ethiface_id a eth interface id to query on DBus
810 * @param callback a function that shall be called to convert Dbus output
811 * into JSON
812 */
813template <typename CallbackFunc>
Ed Tanous81ce6092020-12-17 16:54:55 +0000814void getEthernetIfaceData(const std::string& ethifaceId,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500815 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700816{
817 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800818 [ethifaceId{std::string{ethifaceId}},
819 callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000820 const boost::system::error_code errorCode,
Ed Tanous02cad962022-06-30 16:50:15 -0700821 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700822 EthernetInterfaceData ethData{};
823 boost::container::flat_set<IPv4AddressData> ipv4Data;
824 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700825
Ed Tanous002d39b2022-05-31 08:59:27 -0700826 if (errorCode)
827 {
828 callback(false, ethData, ipv4Data, ipv6Data);
829 return;
830 }
831
832 bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
833 if (!found)
834 {
835 callback(false, ethData, ipv4Data, ipv6Data);
836 return;
837 }
838
839 extractIPData(ethifaceId, resp, ipv4Data);
840 // Fix global GW
841 for (IPv4AddressData& ipv4 : ipv4Data)
842 {
843 if (((ipv4.linktype == LinkType::Global) &&
844 (ipv4.gateway == "0.0.0.0")) ||
845 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
Ed Tanous4a0cb852018-10-15 07:55:04 -0700846 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700847 ipv4.gateway = ethData.defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700848 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700849 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700850
Ed Tanous002d39b2022-05-31 08:59:27 -0700851 extractIPV6Data(ethifaceId, resp, ipv6Data);
852 // Finally make a callback with useful data
853 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700854 },
855 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
856 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700857}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700858
859/**
860 * Function that retrieves all Ethernet Interfaces available through Network
861 * Manager
862 * @param callback a function that shall be called to convert Dbus output
863 * into JSON.
864 */
865template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500866void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700867{
868 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800869 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous81ce6092020-12-17 16:54:55 +0000870 const boost::system::error_code errorCode,
Ed Tanous711ac7a2021-12-20 09:34:41 -0800871 dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700872 // Callback requires vector<string> to retrieve all available
873 // ethernet interfaces
874 boost::container::flat_set<std::string> ifaceList;
875 ifaceList.reserve(resp.size());
876 if (errorCode)
877 {
878 callback(false, ifaceList);
879 return;
880 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700881
Ed Tanous002d39b2022-05-31 08:59:27 -0700882 // Iterate over all retrieved ObjectPaths.
883 for (const auto& objpath : resp)
884 {
885 // And all interfaces available for certain ObjectPath.
886 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700887 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700888 // If interface is
889 // xyz.openbmc_project.Network.EthernetInterface, this is
890 // what we're looking for.
891 if (interface.first ==
892 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700893 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700894 std::string ifaceId = objpath.first.filename();
895 if (ifaceId.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700897 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700898 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700899 // and put it into output vector.
900 ifaceList.emplace(ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700902 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700903 }
904 // Finally make a callback with useful data
905 callback(true, ifaceList);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700906 },
907 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
908 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700909}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100910
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700911inline void
912 handleHostnamePatch(const std::string& hostname,
913 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914{
Ed Tanousbf648f72021-06-03 15:00:14 -0700915 // SHOULD handle host names of up to 255 characters(RFC 1123)
916 if (hostname.length() > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 {
Ed Tanousbf648f72021-06-03 15:00:14 -0700918 messages::propertyValueFormatError(asyncResp->res, hostname,
919 "HostName");
920 return;
921 }
922 crow::connections::systemBus->async_method_call(
923 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700924 if (ec)
925 {
926 messages::internalError(asyncResp->res);
927 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700928 },
929 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
930 "org.freedesktop.DBus.Properties", "Set",
931 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800932 dbus::utility::DbusVariantType(hostname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700933}
934
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700935inline void
Tejas Patil35fb5312021-09-20 15:35:20 +0530936 handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
937 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
938{
939 sdbusplus::message::object_path objPath =
940 "/xyz/openbmc_project/network/" + ifaceId;
941 crow::connections::systemBus->async_method_call(
942 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700943 if (ec)
944 {
945 messages::internalError(asyncResp->res);
946 }
Tejas Patil35fb5312021-09-20 15:35:20 +0530947 },
948 "xyz.openbmc_project.Network", objPath,
949 "org.freedesktop.DBus.Properties", "Set",
950 "xyz.openbmc_project.Network.EthernetInterface", "MTU",
951 std::variant<size_t>(mtuSize));
952}
953
954inline void
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700955 handleDomainnamePatch(const std::string& ifaceId,
956 const std::string& domainname,
957 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -0700958{
959 std::vector<std::string> vectorDomainname = {domainname};
960 crow::connections::systemBus->async_method_call(
961 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700962 if (ec)
963 {
964 messages::internalError(asyncResp->res);
965 }
Ed Tanousbf648f72021-06-03 15:00:14 -0700966 },
967 "xyz.openbmc_project.Network",
968 "/xyz/openbmc_project/network/" + ifaceId,
969 "org.freedesktop.DBus.Properties", "Set",
970 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
Ed Tanous168e20c2021-12-13 14:39:53 -0800971 dbus::utility::DbusVariantType(vectorDomainname));
Ed Tanousbf648f72021-06-03 15:00:14 -0700972}
973
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700974inline bool isHostnameValid(const std::string& hostname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700975{
976 // A valid host name can never have the dotted-decimal form (RFC 1123)
977 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
978 {
979 return false;
980 }
981 // Each label(hostname/subdomains) within a valid FQDN
982 // MUST handle host names of up to 63 characters (RFC 1123)
983 // labels cannot start or end with hyphens (RFC 952)
984 // labels can start with numbers (RFC 1123)
985 const std::regex pattern(
986 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
987
988 return std::regex_match(hostname, pattern);
989}
990
Ed Tanous4f48d5f2021-06-21 08:27:45 -0700991inline bool isDomainnameValid(const std::string& domainname)
Ed Tanousbf648f72021-06-03 15:00:14 -0700992{
993 // Can have multiple subdomains
994 // Top Level Domain's min length is 2 character
George Liu0fda0f12021-11-16 10:06:17 +0800995 const std::regex pattern(
996 "^([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 -0700997
998 return std::regex_match(domainname, pattern);
999}
1000
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001001inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1002 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001003{
1004 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1005 if (fqdn.length() > 255)
1006 {
1007 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1008 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001009 }
1010
Ed Tanousbf648f72021-06-03 15:00:14 -07001011 size_t pos = fqdn.find('.');
1012 if (pos == std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001014 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1015 return;
1016 }
zhanghch058d1b46d2021-04-01 11:18:24 +08001017
Ed Tanousbf648f72021-06-03 15:00:14 -07001018 std::string hostname;
1019 std::string domainname;
1020 domainname = (fqdn).substr(pos + 1);
1021 hostname = (fqdn).substr(0, pos);
1022
1023 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1024 {
1025 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1026 return;
1027 }
1028
1029 handleHostnamePatch(hostname, asyncResp);
1030 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1031}
1032
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001033inline void
1034 handleMACAddressPatch(const std::string& ifaceId,
1035 const std::string& macAddress,
1036 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001037{
Johnathan Mantey58283f42022-08-15 14:38:36 -07001038 static constexpr std::string_view dbusNotAllowedError =
1039 "xyz.openbmc_project.Common.Error.NotAllowed";
1040
Ed Tanousbf648f72021-06-03 15:00:14 -07001041 crow::connections::systemBus->async_method_call(
Johnathan Mantey58283f42022-08-15 14:38:36 -07001042 [asyncResp, macAddress](const boost::system::error_code ec,
Patrick Williams5b378542022-11-26 09:41:59 -06001043 const sdbusplus::message_t& msg) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001044 if (ec)
1045 {
Johnathan Mantey58283f42022-08-15 14:38:36 -07001046 const sd_bus_error* err = msg.get_error();
1047 if (err == nullptr)
1048 {
1049 messages::internalError(asyncResp->res);
1050 return;
1051 }
1052 if (err->name == dbusNotAllowedError)
1053 {
1054 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1055 return;
1056 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001057 messages::internalError(asyncResp->res);
1058 return;
1059 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001060 },
1061 "xyz.openbmc_project.Network",
1062 "/xyz/openbmc_project/network/" + ifaceId,
1063 "org.freedesktop.DBus.Properties", "Set",
1064 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
Ed Tanous168e20c2021-12-13 14:39:53 -08001065 dbus::utility::DbusVariantType(macAddress));
Ed Tanousbf648f72021-06-03 15:00:14 -07001066}
1067
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001068inline void setDHCPEnabled(const std::string& ifaceId,
1069 const std::string& propertyName, const bool v4Value,
1070 const bool v6Value,
1071 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001072{
1073 const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1074 crow::connections::systemBus->async_method_call(
1075 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001076 if (ec)
1077 {
1078 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1079 messages::internalError(asyncResp->res);
1080 return;
1081 }
1082 messages::success(asyncResp->res);
Ed Tanousbf648f72021-06-03 15:00:14 -07001083 },
1084 "xyz.openbmc_project.Network",
1085 "/xyz/openbmc_project/network/" + ifaceId,
1086 "org.freedesktop.DBus.Properties", "Set",
1087 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001088 dbus::utility::DbusVariantType{dhcp});
Ed Tanousbf648f72021-06-03 15:00:14 -07001089}
1090
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001091inline void setEthernetInterfaceBoolProperty(
Ed Tanousbf648f72021-06-03 15:00:14 -07001092 const std::string& ifaceId, const std::string& propertyName,
1093 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1094{
1095 crow::connections::systemBus->async_method_call(
1096 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001097 if (ec)
1098 {
1099 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1100 messages::internalError(asyncResp->res);
1101 return;
1102 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001103 },
1104 "xyz.openbmc_project.Network",
1105 "/xyz/openbmc_project/network/" + ifaceId,
1106 "org.freedesktop.DBus.Properties", "Set",
1107 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001108 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001109}
1110
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001111inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
1112 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001113{
1114 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1115 crow::connections::systemBus->async_method_call(
1116 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001117 if (ec)
1118 {
1119 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1120 messages::internalError(asyncResp->res);
1121 return;
1122 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001123 },
Jian Zhang1e3f85e2022-12-13 13:50:35 +08001124 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
Ed Tanousbf648f72021-06-03 15:00:14 -07001125 "org.freedesktop.DBus.Properties", "Set",
1126 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
Ed Tanous168e20c2021-12-13 14:39:53 -08001127 dbus::utility::DbusVariantType{value});
Ed Tanousbf648f72021-06-03 15:00:14 -07001128}
1129
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001130inline void handleDHCPPatch(const std::string& ifaceId,
1131 const EthernetInterfaceData& ethData,
1132 const DHCPParameters& v4dhcpParms,
1133 const DHCPParameters& v6dhcpParms,
1134 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanousbf648f72021-06-03 15:00:14 -07001135{
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001136 bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1137 bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
Ed Tanousbf648f72021-06-03 15:00:14 -07001138
1139 bool nextv4DHCPState =
1140 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1141
1142 bool nextv6DHCPState{};
1143 if (v6dhcpParms.dhcpv6OperatingMode)
1144 {
1145 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1146 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1147 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1148 {
1149 messages::propertyValueFormatError(asyncResp->res,
1150 *v6dhcpParms.dhcpv6OperatingMode,
1151 "OperatingMode");
1152 return;
1153 }
1154 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1155 }
1156 else
1157 {
1158 nextv6DHCPState = ipv6Active;
1159 }
1160
1161 bool nextDNS{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001162 if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001163 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001164 if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001165 {
1166 messages::generalError(asyncResp->res);
1167 return;
1168 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001169 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001170 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001171 else if (v4dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001172 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001173 nextDNS = *v4dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001174 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001175 else if (v6dhcpParms.useDnsServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001176 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001177 nextDNS = *v6dhcpParms.useDnsServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001178 }
1179 else
1180 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001181 nextDNS = ethData.dnsEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001182 }
1183
1184 bool nextNTP{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001185 if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001186 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001187 if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001188 {
1189 messages::generalError(asyncResp->res);
1190 return;
1191 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001192 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001193 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001194 else if (v4dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001195 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001196 nextNTP = *v4dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001197 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001198 else if (v6dhcpParms.useNtpServers)
Ed Tanousbf648f72021-06-03 15:00:14 -07001199 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001200 nextNTP = *v6dhcpParms.useNtpServers;
Ed Tanousbf648f72021-06-03 15:00:14 -07001201 }
1202 else
1203 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001204 nextNTP = ethData.ntpEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001205 }
1206
1207 bool nextUseDomain{};
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001208 if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001209 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001210 if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001211 {
1212 messages::generalError(asyncResp->res);
1213 return;
1214 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001215 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001216 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001217 else if (v4dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001218 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001219 nextUseDomain = *v4dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001220 }
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001221 else if (v6dhcpParms.useDomainName)
Ed Tanousbf648f72021-06-03 15:00:14 -07001222 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001223 nextUseDomain = *v6dhcpParms.useDomainName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001224 }
1225 else
1226 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001227 nextUseDomain = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001228 }
1229
1230 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1231 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1232 asyncResp);
1233 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1234 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1235 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1236 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1237 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1238 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1239}
1240
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001241inline boost::container::flat_set<IPv4AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001242 getNextStaticIpEntry(
1243 const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1244 const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
1245{
1246 return std::find_if(head, end, [](const IPv4AddressData& value) {
1247 return value.origin == "Static";
1248 });
1249}
1250
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001251inline boost::container::flat_set<IPv6AddressData>::const_iterator
Ed Tanousbf648f72021-06-03 15:00:14 -07001252 getNextStaticIpEntry(
1253 const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1254 const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
1255{
1256 return std::find_if(head, end, [](const IPv6AddressData& value) {
1257 return value.origin == "Static";
1258 });
1259}
1260
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001261inline void handleIPv4StaticPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001262 const std::string& ifaceId, nlohmann::json& input,
1263 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1264 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1265{
1266 if ((!input.is_array()) || input.empty())
1267 {
1268 messages::propertyValueTypeError(
1269 asyncResp->res,
1270 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1271 "IPv4StaticAddresses");
1272 return;
1273 }
1274
1275 unsigned entryIdx = 1;
1276 // Find the first static IP address currently active on the NIC and
1277 // match it to the first JSON element in the IPv4StaticAddresses array.
1278 // Match each subsequent JSON element to the next static IP programmed
1279 // into the NIC.
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001280 boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001281 getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1282
1283 for (nlohmann::json& thisJson : input)
1284 {
1285 std::string pathString =
1286 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1287
1288 if (!thisJson.is_null() && !thisJson.empty())
1289 {
1290 std::optional<std::string> address;
1291 std::optional<std::string> subnetMask;
1292 std::optional<std::string> gateway;
1293
1294 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1295 address, "SubnetMask", subnetMask,
1296 "Gateway", gateway))
1297 {
1298 messages::propertyValueFormatError(
1299 asyncResp->res,
1300 thisJson.dump(2, ' ', true,
1301 nlohmann::json::error_handler_t::replace),
1302 pathString);
1303 return;
1304 }
1305
1306 // Find the address/subnet/gateway values. Any values that are
1307 // not explicitly provided are assumed to be unmodified from the
1308 // current state of the interface. Merge existing state into the
1309 // current request.
1310 const std::string* addr = nullptr;
1311 const std::string* gw = nullptr;
1312 uint8_t prefixLength = 0;
1313 bool errorInEntry = false;
1314 if (address)
1315 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001316 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
Ed Tanousbf648f72021-06-03 15:00:14 -07001317 {
1318 addr = &(*address);
1319 }
1320 else
1321 {
1322 messages::propertyValueFormatError(asyncResp->res, *address,
1323 pathString + "/Address");
1324 errorInEntry = true;
1325 }
1326 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001327 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001328 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001329 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001330 }
1331 else
1332 {
1333 messages::propertyMissing(asyncResp->res,
1334 pathString + "/Address");
1335 errorInEntry = true;
1336 }
1337
1338 if (subnetMask)
1339 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001340 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1341 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001342 {
1343 messages::propertyValueFormatError(
1344 asyncResp->res, *subnetMask,
1345 pathString + "/SubnetMask");
1346 errorInEntry = true;
1347 }
1348 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001349 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001350 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001351 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
1352 &prefixLength))
Ed Tanousbf648f72021-06-03 15:00:14 -07001353 {
1354 messages::propertyValueFormatError(
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001355 asyncResp->res, nicIpEntry->netmask,
Ed Tanousbf648f72021-06-03 15:00:14 -07001356 pathString + "/SubnetMask");
1357 errorInEntry = true;
1358 }
1359 }
1360 else
1361 {
1362 messages::propertyMissing(asyncResp->res,
1363 pathString + "/SubnetMask");
1364 errorInEntry = true;
1365 }
1366
1367 if (gateway)
1368 {
Ed Tanous033f1e42022-08-15 09:47:37 -07001369 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
Ed Tanousbf648f72021-06-03 15:00:14 -07001370 {
1371 gw = &(*gateway);
1372 }
1373 else
1374 {
1375 messages::propertyValueFormatError(asyncResp->res, *gateway,
1376 pathString + "/Gateway");
1377 errorInEntry = true;
1378 }
1379 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001380 else if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001381 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001382 gw = &nicIpEntry->gateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001383 }
1384 else
1385 {
1386 messages::propertyMissing(asyncResp->res,
1387 pathString + "/Gateway");
1388 errorInEntry = true;
1389 }
1390
1391 if (errorInEntry)
1392 {
1393 return;
1394 }
1395
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001396 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001397 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001398 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
Ed Tanousbf648f72021-06-03 15:00:14 -07001399 *addr, asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001400 nicIpEntry =
1401 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001402 }
1403 else
1404 {
1405 createIPv4(ifaceId, prefixLength, *gateway, *address,
1406 asyncResp);
1407 }
1408 entryIdx++;
1409 }
1410 else
1411 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001412 if (nicIpEntry == ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001413 {
1414 // Requesting a DELETE/DO NOT MODIFY action for an item
1415 // that isn't present on the eth(n) interface. Input JSON is
1416 // in error, so bail out.
1417 if (thisJson.is_null())
1418 {
1419 messages::resourceCannotBeDeleted(asyncResp->res);
1420 return;
1421 }
1422 messages::propertyValueFormatError(
1423 asyncResp->res,
1424 thisJson.dump(2, ' ', true,
1425 nlohmann::json::error_handler_t::replace),
1426 pathString);
1427 return;
1428 }
1429
1430 if (thisJson.is_null())
1431 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001432 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001433 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001434 if (nicIpEntry != ipv4Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001435 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001436 nicIpEntry =
1437 getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001438 }
1439 entryIdx++;
1440 }
1441 }
1442}
1443
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001444inline void handleStaticNameServersPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001445 const std::string& ifaceId,
1446 const std::vector<std::string>& updatedStaticNameServers,
1447 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1448{
1449 crow::connections::systemBus->async_method_call(
1450 [asyncResp](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001451 if (ec)
1452 {
1453 messages::internalError(asyncResp->res);
1454 return;
1455 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001456 },
1457 "xyz.openbmc_project.Network",
1458 "/xyz/openbmc_project/network/" + ifaceId,
1459 "org.freedesktop.DBus.Properties", "Set",
1460 "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
Ed Tanous168e20c2021-12-13 14:39:53 -08001461 dbus::utility::DbusVariantType{updatedStaticNameServers});
Ed Tanousbf648f72021-06-03 15:00:14 -07001462}
1463
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001464inline void handleIPv6StaticAddressesPatch(
Ed Tanousbf648f72021-06-03 15:00:14 -07001465 const std::string& ifaceId, const nlohmann::json& input,
1466 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1467 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1468{
1469 if (!input.is_array() || input.empty())
1470 {
1471 messages::propertyValueTypeError(
1472 asyncResp->res,
1473 input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1474 "IPv6StaticAddresses");
1475 return;
1476 }
1477 size_t entryIdx = 1;
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001478 boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
Ed Tanousbf648f72021-06-03 15:00:14 -07001479 getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1480 for (const nlohmann::json& thisJson : input)
1481 {
1482 std::string pathString =
1483 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1484
1485 if (!thisJson.is_null() && !thisJson.empty())
1486 {
1487 std::optional<std::string> address;
1488 std::optional<uint8_t> prefixLength;
1489 nlohmann::json thisJsonCopy = thisJson;
1490 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1491 address, "PrefixLength", prefixLength))
1492 {
1493 messages::propertyValueFormatError(
1494 asyncResp->res,
1495 thisJson.dump(2, ' ', true,
1496 nlohmann::json::error_handler_t::replace),
1497 pathString);
1498 return;
1499 }
1500
Ed Tanous543f4402022-01-06 13:12:53 -08001501 const std::string* addr = nullptr;
1502 uint8_t prefix = 0;
Ed Tanousbf648f72021-06-03 15:00:14 -07001503
1504 // Find the address and prefixLength values. Any values that are
1505 // not explicitly provided are assumed to be unmodified from the
1506 // current state of the interface. Merge existing state into the
1507 // current request.
1508 if (address)
1509 {
1510 addr = &(*address);
1511 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001512 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001513 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001514 addr = &(nicIpEntry->address);
Ed Tanousbf648f72021-06-03 15:00:14 -07001515 }
1516 else
1517 {
1518 messages::propertyMissing(asyncResp->res,
1519 pathString + "/Address");
1520 return;
1521 }
1522
1523 if (prefixLength)
1524 {
1525 prefix = *prefixLength;
1526 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001527 else if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001528 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001529 prefix = nicIpEntry->prefixLength;
Ed Tanousbf648f72021-06-03 15:00:14 -07001530 }
1531 else
1532 {
1533 messages::propertyMissing(asyncResp->res,
1534 pathString + "/PrefixLength");
1535 return;
1536 }
1537
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001538 if (nicIpEntry != ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001539 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001540 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
Ed Tanousbf648f72021-06-03 15:00:14 -07001541 asyncResp);
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001542 nicIpEntry =
1543 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001544 }
1545 else
1546 {
1547 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1548 }
1549 entryIdx++;
1550 }
1551 else
1552 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001553 if (nicIpEntry == ipv6Data.end())
Ed Tanousbf648f72021-06-03 15:00:14 -07001554 {
1555 // Requesting a DELETE/DO NOT MODIFY action for an item
1556 // that isn't present on the eth(n) interface. Input JSON is
1557 // in error, so bail out.
1558 if (thisJson.is_null())
1559 {
1560 messages::resourceCannotBeDeleted(asyncResp->res);
1561 return;
1562 }
1563 messages::propertyValueFormatError(
1564 asyncResp->res,
1565 thisJson.dump(2, ' ', true,
1566 nlohmann::json::error_handler_t::replace),
1567 pathString);
1568 return;
1569 }
1570
1571 if (thisJson.is_null())
1572 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001573 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
Ed Tanousbf648f72021-06-03 15:00:14 -07001574 }
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001575 if (nicIpEntry != ipv6Data.cend())
Ed Tanousbf648f72021-06-03 15:00:14 -07001576 {
Jiaqing Zhao85ffe862021-12-31 15:41:59 +08001577 nicIpEntry =
1578 getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
Ed Tanousbf648f72021-06-03 15:00:14 -07001579 }
1580 entryIdx++;
1581 }
1582 }
1583}
1584
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001585inline void parseInterfaceData(
Ed Tanousbf648f72021-06-03 15:00:14 -07001586 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1587 const std::string& ifaceId, const EthernetInterfaceData& ethData,
1588 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1589 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1590{
1591 constexpr const std::array<const char*, 1> inventoryForEthernet = {
1592 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1593
1594 nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1595 jsonResponse["Id"] = ifaceId;
1596 jsonResponse["@odata.id"] =
1597 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
1598 jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1599
1600 auto health = std::make_shared<HealthPopulate>(asyncResp);
1601
1602 crow::connections::systemBus->async_method_call(
1603 [health](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001604 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -07001605 if (ec)
1606 {
1607 return;
1608 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001609
Ed Tanous002d39b2022-05-31 08:59:27 -07001610 health->inventory = resp;
Ed Tanousbf648f72021-06-03 15:00:14 -07001611 },
1612 "xyz.openbmc_project.ObjectMapper",
1613 "/xyz/openbmc_project/object_mapper",
1614 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1615 inventoryForEthernet);
1616
1617 health->populate();
1618
1619 if (ethData.nicEnabled)
1620 {
Johnathan Mantey0ef0e282022-11-15 12:15:02 -08001621 jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanousbf648f72021-06-03 15:00:14 -07001622 jsonResponse["Status"]["State"] = "Enabled";
1623 }
1624 else
1625 {
1626 jsonResponse["LinkStatus"] = "NoLink";
1627 jsonResponse["Status"]["State"] = "Disabled";
1628 }
1629
Ed Tanousbf648f72021-06-03 15:00:14 -07001630 jsonResponse["SpeedMbps"] = ethData.speed;
Tejas Patil35fb5312021-09-20 15:35:20 +05301631 jsonResponse["MTUSize"] = ethData.mtuSize;
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001632 jsonResponse["MACAddress"] = ethData.macAddress;
Ed Tanousbf648f72021-06-03 15:00:14 -07001633 jsonResponse["DHCPv4"]["DHCPEnabled"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001634 translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
1635 jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
1636 jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
1637 jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001638
1639 jsonResponse["DHCPv6"]["OperatingMode"] =
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001640 translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
Ed Tanousbf648f72021-06-03 15:00:14 -07001641 : "Disabled";
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001642 jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
1643 jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
1644 jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
Ed Tanousbf648f72021-06-03 15:00:14 -07001645
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001646 if (!ethData.hostName.empty())
Ed Tanousbf648f72021-06-03 15:00:14 -07001647 {
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001648 jsonResponse["HostName"] = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001649
1650 // When domain name is empty then it means, that it is a network
1651 // without domain names, and the host name itself must be treated as
1652 // FQDN
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001653 std::string fqdn = ethData.hostName;
Ed Tanousbf648f72021-06-03 15:00:14 -07001654 if (!ethData.domainnames.empty())
1655 {
1656 fqdn += "." + ethData.domainnames[0];
1657 }
1658 jsonResponse["FQDN"] = fqdn;
1659 }
1660
Ed Tanous613dabe2022-07-09 11:17:36 -07001661 jsonResponse["VLANs"]["@odata.id"] =
1662 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1663 "EthernetInterfaces", ifaceId, "VLANs");
Ed Tanousbf648f72021-06-03 15:00:14 -07001664
1665 jsonResponse["NameServers"] = ethData.nameServers;
1666 jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1667
1668 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1669 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1670 ipv4Array = nlohmann::json::array();
1671 ipv4StaticArray = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001672 for (const auto& ipv4Config : ipv4Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001673 {
Ed Tanousbf648f72021-06-03 15:00:14 -07001674 std::string gatewayStr = ipv4Config.gateway;
1675 if (gatewayStr.empty())
1676 {
1677 gatewayStr = "0.0.0.0";
1678 }
Ed Tanous14766872022-03-15 10:44:42 -07001679 nlohmann::json::object_t ipv4;
1680 ipv4["AddressOrigin"] = ipv4Config.origin;
1681 ipv4["SubnetMask"] = ipv4Config.netmask;
1682 ipv4["Address"] = ipv4Config.address;
1683 ipv4["Gateway"] = gatewayStr;
Ed Tanousbf648f72021-06-03 15:00:14 -07001684
Ed Tanousbf648f72021-06-03 15:00:14 -07001685 if (ipv4Config.origin == "Static")
1686 {
Ed Tanous14766872022-03-15 10:44:42 -07001687 ipv4StaticArray.push_back(ipv4);
Ed Tanousbf648f72021-06-03 15:00:14 -07001688 }
Ed Tanous14766872022-03-15 10:44:42 -07001689
1690 ipv4Array.push_back(std::move(ipv4));
Ed Tanousbf648f72021-06-03 15:00:14 -07001691 }
1692
Jiaqing Zhao82695a52022-04-14 15:15:59 +08001693 std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
Ed Tanousbf648f72021-06-03 15:00:14 -07001694 if (ipv6GatewayStr.empty())
1695 {
1696 ipv6GatewayStr = "0:0:0:0:0:0:0:0";
1697 }
1698
1699 jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1700
1701 nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1702 nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1703 ipv6Array = nlohmann::json::array();
1704 ipv6StaticArray = nlohmann::json::array();
1705 nlohmann::json& ipv6AddrPolicyTable =
1706 jsonResponse["IPv6AddressPolicyTable"];
1707 ipv6AddrPolicyTable = nlohmann::json::array();
Ed Tanous9eb808c2022-01-25 10:19:23 -08001708 for (const auto& ipv6Config : ipv6Data)
Ed Tanousbf648f72021-06-03 15:00:14 -07001709 {
Ed Tanous14766872022-03-15 10:44:42 -07001710 nlohmann::json::object_t ipv6;
1711 ipv6["Address"] = ipv6Config.address;
1712 ipv6["PrefixLength"] = ipv6Config.prefixLength;
1713 ipv6["AddressOrigin"] = ipv6Config.origin;
1714 ipv6["AddressState"] = nullptr;
1715 ipv6Array.push_back(std::move(ipv6));
Ed Tanousbf648f72021-06-03 15:00:14 -07001716 if (ipv6Config.origin == "Static")
1717 {
Ed Tanous14766872022-03-15 10:44:42 -07001718 nlohmann::json::object_t ipv6Static;
1719 ipv6Static["Address"] = ipv6Config.address;
1720 ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1721 ipv6StaticArray.push_back(std::move(ipv6Static));
Ed Tanousbf648f72021-06-03 15:00:14 -07001722 }
1723 }
1724}
1725
Ed Tanous4f48d5f2021-06-21 08:27:45 -07001726inline bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001727{
Ed Tanous11ba3972022-07-11 09:50:41 -07001728 return iface.starts_with(parent + "_");
Ed Tanousbf648f72021-06-03 15:00:14 -07001729}
1730
1731inline void requestEthernetInterfacesRoutes(App& app)
1732{
1733 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanoused398212021-06-09 17:05:54 -07001734 .privileges(redfish::privileges::getEthernetInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07001735 .methods(boost::beast::http::verb::get)(
1736 [&app](const crow::Request& req,
1737 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001738 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001739 {
1740 return;
1741 }
1742
1743 asyncResp->res.jsonValue["@odata.type"] =
1744 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1745 asyncResp->res.jsonValue["@odata.id"] =
1746 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1747 asyncResp->res.jsonValue["Name"] =
1748 "Ethernet Network Interface Collection";
1749 asyncResp->res.jsonValue["Description"] =
1750 "Collection of EthernetInterfaces for this Manager";
1751
1752 // Get eth interface list, and call the below callback for JSON
1753 // preparation
1754 getEthernetIfaceList(
1755 [asyncResp](
1756 const bool& success,
1757 const boost::container::flat_set<std::string>& ifaceList) {
1758 if (!success)
1759 {
1760 messages::internalError(asyncResp->res);
1761 return;
1762 }
1763
1764 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1765 ifaceArray = nlohmann::json::array();
1766 std::string tag = "_";
1767 for (const std::string& ifaceItem : ifaceList)
1768 {
1769 std::size_t found = ifaceItem.find(tag);
1770 if (found == std::string::npos)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001771 {
Ed Tanous002d39b2022-05-31 08:59:27 -07001772 nlohmann::json::object_t iface;
1773 iface["@odata.id"] =
1774 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1775 ifaceItem;
1776 ifaceArray.push_back(std::move(iface));
Jason M. Billsf12894f2018-10-09 12:45:45 -07001777 }
Ed Tanous002d39b2022-05-31 08:59:27 -07001778 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001779
Ed Tanous002d39b2022-05-31 08:59:27 -07001780 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1781 asyncResp->res.jsonValue["@odata.id"] =
1782 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1783 });
1784 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001785
Ed Tanousbf648f72021-06-03 15:00:14 -07001786 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001787 .privileges(redfish::privileges::getEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001788 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001789 [&app](const crow::Request& req,
1790 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1791 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001792 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001793 {
1794 return;
1795 }
1796 getEthernetIfaceData(
1797 ifaceId,
1798 [asyncResp, ifaceId](
1799 const bool& success, const EthernetInterfaceData& ethData,
1800 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1801 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1802 if (!success)
1803 {
1804 // TODO(Pawel)consider distinguish between non
1805 // existing object, and other errors
1806 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1807 ifaceId);
1808 return;
1809 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001810
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001811 // Keep using the v1.6.0 schema here as currently bmcweb have to use
1812 // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
Ed Tanous002d39b2022-05-31 08:59:27 -07001813 asyncResp->res.jsonValue["@odata.type"] =
Jiaqing Zhao188cb622022-05-26 02:14:28 +08001814 "#EthernetInterface.v1_6_0.EthernetInterface";
Ed Tanous002d39b2022-05-31 08:59:27 -07001815 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1816 asyncResp->res.jsonValue["Description"] =
1817 "Management Network Interface";
Ratan Guptaf476acb2019-03-02 16:46:57 +05301818
Ed Tanous002d39b2022-05-31 08:59:27 -07001819 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
Ed Tanousbf648f72021-06-03 15:00:14 -07001820 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001821 });
Johnathan Mantey01784822019-06-18 12:44:21 -07001822
Ed Tanousbf648f72021-06-03 15:00:14 -07001823 BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001824 .privileges(redfish::privileges::patchEthernetInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001825 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001826 [&app](const crow::Request& req,
1827 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1828 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001829 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001830 {
1831 return;
1832 }
1833 std::optional<std::string> hostname;
1834 std::optional<std::string> fqdn;
1835 std::optional<std::string> macAddress;
1836 std::optional<std::string> ipv6DefaultGateway;
1837 std::optional<nlohmann::json> ipv4StaticAddresses;
1838 std::optional<nlohmann::json> ipv6StaticAddresses;
1839 std::optional<std::vector<std::string>> staticNameServers;
1840 std::optional<nlohmann::json> dhcpv4;
1841 std::optional<nlohmann::json> dhcpv6;
1842 std::optional<bool> interfaceEnabled;
1843 std::optional<size_t> mtuSize;
1844 DHCPParameters v4dhcpParms;
1845 DHCPParameters v6dhcpParms;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001846
Ed Tanous002d39b2022-05-31 08:59:27 -07001847 if (!json_util::readJsonPatch(
1848 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1849 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1850 macAddress, "StaticNameServers", staticNameServers,
1851 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1852 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1853 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
1854 {
1855 return;
1856 }
1857 if (dhcpv4)
1858 {
1859 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
1860 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1861 v4dhcpParms.useDnsServers, "UseNTPServers",
1862 v4dhcpParms.useNtpServers, "UseDomainName",
1863 v4dhcpParms.useDomainName))
1864 {
1865 return;
1866 }
1867 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001868
Ed Tanous002d39b2022-05-31 08:59:27 -07001869 if (dhcpv6)
1870 {
1871 if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1872 v6dhcpParms.dhcpv6OperatingMode,
1873 "UseDNSServers", v6dhcpParms.useDnsServers,
1874 "UseNTPServers", v6dhcpParms.useNtpServers,
1875 "UseDomainName",
1876 v6dhcpParms.useDomainName))
1877 {
1878 return;
1879 }
1880 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001881
Ed Tanous002d39b2022-05-31 08:59:27 -07001882 // Get single eth interface data, and call the below callback
1883 // for JSON preparation
1884 getEthernetIfaceData(
1885 ifaceId,
1886 [asyncResp, ifaceId, hostname = std::move(hostname),
1887 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1888 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1889 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1890 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1891 staticNameServers = std::move(staticNameServers),
Ed Tanousbc200892022-06-30 17:49:12 -07001892 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1893 v4dhcpParms = std::move(v4dhcpParms),
Ed Tanous002d39b2022-05-31 08:59:27 -07001894 v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1895 const bool& success, const EthernetInterfaceData& ethData,
1896 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1897 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1898 if (!success)
1899 {
1900 // ... otherwise return error
1901 // TODO(Pawel)consider distinguish between non
1902 // existing object, and other errors
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08001903 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07001904 ifaceId);
1905 return;
1906 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001907
Ed Tanous002d39b2022-05-31 08:59:27 -07001908 if (dhcpv4 || dhcpv6)
1909 {
1910 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
Ed Tanousbf648f72021-06-03 15:00:14 -07001911 asyncResp);
Ed Tanous002d39b2022-05-31 08:59:27 -07001912 }
Tejas Patil35fb5312021-09-20 15:35:20 +05301913
Ed Tanous002d39b2022-05-31 08:59:27 -07001914 if (hostname)
1915 {
1916 handleHostnamePatch(*hostname, asyncResp);
1917 }
1918
1919 if (fqdn)
1920 {
1921 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1922 }
1923
1924 if (macAddress)
1925 {
1926 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1927 }
1928
1929 if (ipv4StaticAddresses)
1930 {
1931 // TODO(ed) for some reason the capture of
1932 // ipv4Addresses above is returning a const value,
1933 // not a non-const value. This doesn't really work
1934 // for us, as we need to be able to efficiently move
1935 // out the intermedia nlohmann::json objects. This
1936 // makes a copy of the structure, and operates on
1937 // that, but could be done more efficiently
1938 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1939 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
1940 }
1941
1942 if (staticNameServers)
1943 {
1944 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1945 asyncResp);
1946 }
1947
1948 if (ipv6DefaultGateway)
1949 {
1950 messages::propertyNotWritable(asyncResp->res,
1951 "IPv6DefaultGateway");
1952 }
1953
1954 if (ipv6StaticAddresses)
1955 {
1956 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1957 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1958 asyncResp);
1959 }
1960
1961 if (interfaceEnabled)
1962 {
1963 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1964 *interfaceEnabled, asyncResp);
1965 }
1966
1967 if (mtuSize)
1968 {
1969 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
1970 }
Ed Tanousbf648f72021-06-03 15:00:14 -07001971 });
Ed Tanous002d39b2022-05-31 08:59:27 -07001972 });
manojkiraneda2a133282019-02-19 13:09:43 +05301973
Ed Tanousbf648f72021-06-03 15:00:14 -07001974 BMCWEB_ROUTE(
1975 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -07001976 .privileges(redfish::privileges::getVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07001977 .methods(boost::beast::http::verb::get)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07001978 [&app](const crow::Request& req,
1979 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1980 const std::string& parentIfaceId,
1981 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00001982 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07001983 {
1984 return;
1985 }
1986 asyncResp->res.jsonValue["@odata.type"] =
1987 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1988 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001989
Ed Tanous002d39b2022-05-31 08:59:27 -07001990 if (!verifyNames(parentIfaceId, ifaceId))
1991 {
1992 return;
1993 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001994
Ed Tanous002d39b2022-05-31 08:59:27 -07001995 // Get single eth interface data, and call the below callback
1996 // for JSON preparation
1997 getEthernetIfaceData(
1998 ifaceId,
1999 [asyncResp, parentIfaceId,
2000 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2001 const boost::container::flat_set<IPv4AddressData>&,
2002 const boost::container::flat_set<IPv6AddressData>&) {
2003 if (success && ethData.vlanId)
2004 {
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002005 asyncResp->res.jsonValue["Id"] = ifaceId;
2006 asyncResp->res.jsonValue["@odata.id"] =
2007 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2008 parentIfaceId + "/VLANs/" + ifaceId;
2009
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002010 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
Jiaqing Zhao22872ff2022-09-13 09:38:20 +08002011 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
Ed Tanous002d39b2022-05-31 08:59:27 -07002012 }
2013 else
2014 {
2015 // ... otherwise return error
2016 // TODO(Pawel)consider distinguish between non
2017 // existing object, and other errors
2018 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002019 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002020 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002021 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002022 });
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002023
Ed Tanousbf648f72021-06-03 15:00:14 -07002024 BMCWEB_ROUTE(
2025 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002026 .privileges(redfish::privileges::patchVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002027 .methods(boost::beast::http::verb::patch)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002028 [&app](const crow::Request& req,
2029 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2030 const std::string& parentIfaceId,
2031 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002032 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002033 {
2034 return;
2035 }
2036 if (!verifyNames(parentIfaceId, ifaceId))
2037 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002038 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002039 ifaceId);
2040 return;
2041 }
2042
2043 std::optional<bool> vlanEnable;
2044 std::optional<uint32_t> vlanId;
2045
2046 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2047 vlanEnable, "VLANId", vlanId))
2048 {
2049 return;
2050 }
2051
2052 if (vlanId)
2053 {
2054 messages::propertyNotWritable(asyncResp->res, "VLANId");
2055 return;
2056 }
2057
2058 // Get single eth interface data, and call the below callback
2059 // for JSON preparation
2060 getEthernetIfaceData(
2061 ifaceId,
2062 [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2063 const bool& success, const EthernetInterfaceData& ethData,
2064 const boost::container::flat_set<IPv4AddressData>&,
2065 const boost::container::flat_set<IPv6AddressData>&) {
2066 if (success && ethData.vlanId)
2067 {
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002068 if (vlanEnable)
Ed Tanous45ca1b82022-03-25 13:07:27 -07002069 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002070 crow::connections::systemBus->async_method_call(
Jiaqing Zhao23a06312022-09-13 09:38:25 +08002071 [asyncResp](const boost::system::error_code ec) {
2072 if (ec)
2073 {
2074 messages::internalError(asyncResp->res);
2075 return;
2076 }
2077 },
2078 "xyz.openbmc_project.Network",
2079 "/xyz/openbmc_project/network/" + ifaceId,
2080 "org.freedesktop.DBus.Properties", "Set",
2081 "xyz.openbmc_project.Network.EthernetInterface",
2082 "NICEnabled",
2083 dbus::utility::DbusVariantType(*vlanEnable));
Ed Tanous45ca1b82022-03-25 13:07:27 -07002084 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002085 }
2086 else
2087 {
2088 // TODO(Pawel)consider distinguish between non
2089 // existing object, and other errors
2090 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002091 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002092 return;
2093 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002094 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002095 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002096
2097 BMCWEB_ROUTE(
2098 app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002099 .privileges(redfish::privileges::deleteVLanNetworkInterface)
Ed Tanousbf648f72021-06-03 15:00:14 -07002100 .methods(boost::beast::http::verb::delete_)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002101 [&app](const crow::Request& req,
2102 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2103 const std::string& parentIfaceId,
2104 const std::string& ifaceId) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002105 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002106 {
2107 return;
2108 }
2109 if (!verifyNames(parentIfaceId, ifaceId))
2110 {
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002111 messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
Ed Tanous002d39b2022-05-31 08:59:27 -07002112 ifaceId);
2113 return;
2114 }
Ed Tanousbf648f72021-06-03 15:00:14 -07002115
Ed Tanous002d39b2022-05-31 08:59:27 -07002116 // Get single eth interface data, and call the below callback
2117 // for JSON preparation
2118 getEthernetIfaceData(
2119 ifaceId,
2120 [asyncResp, parentIfaceId,
2121 ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2122 const boost::container::flat_set<IPv4AddressData>&,
2123 const boost::container::flat_set<IPv6AddressData>&) {
2124 if (success && ethData.vlanId)
2125 {
2126 auto callback =
2127 [asyncResp](const boost::system::error_code ec) {
2128 if (ec)
2129 {
2130 messages::internalError(asyncResp->res);
2131 }
2132 };
2133 crow::connections::systemBus->async_method_call(
2134 std::move(callback), "xyz.openbmc_project.Network",
2135 std::string("/xyz/openbmc_project/network/") + ifaceId,
2136 "xyz.openbmc_project.Object.Delete", "Delete");
2137 }
2138 else
2139 {
2140 // ... otherwise return error
2141 // TODO(Pawel)consider distinguish between non
2142 // existing object, and other errors
2143 messages::resourceNotFound(asyncResp->res,
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +08002144 "VLanNetworkInterface", ifaceId);
Ed Tanous002d39b2022-05-31 08:59:27 -07002145 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002146 });
Ed Tanous002d39b2022-05-31 08:59:27 -07002147 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002148
Ed Tanousbf648f72021-06-03 15:00:14 -07002149 BMCWEB_ROUTE(app,
2150 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Ed Tanoused398212021-06-09 17:05:54 -07002151
2152 .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
Ed Tanous14766872022-03-15 10:44:42 -07002153 .methods(boost::beast::http::verb::get)(
2154 [&app](const crow::Request& req,
2155 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2156 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002157 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002158 {
2159 return;
2160 }
2161 // Get eth interface list, and call the below callback for JSON
2162 // preparation
2163 getEthernetIfaceList(
2164 [asyncResp, rootInterfaceName](
2165 const bool& success,
2166 const boost::container::flat_set<std::string>& ifaceList) {
2167 if (!success)
2168 {
2169 messages::internalError(asyncResp->res);
2170 return;
2171 }
2172
2173 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
2174 {
2175 messages::resourceNotFound(asyncResp->res,
2176 "VLanNetworkInterfaceCollection",
2177 rootInterfaceName);
2178 return;
2179 }
2180
2181 asyncResp->res.jsonValue["@odata.type"] =
2182 "#VLanNetworkInterfaceCollection."
2183 "VLanNetworkInterfaceCollection";
2184 asyncResp->res.jsonValue["Name"] =
2185 "VLAN Network Interface Collection";
2186
2187 nlohmann::json ifaceArray = nlohmann::json::array();
2188
2189 for (const std::string& ifaceItem : ifaceList)
2190 {
Ed Tanous11ba3972022-07-11 09:50:41 -07002191 if (ifaceItem.starts_with(rootInterfaceName + "_"))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002192 {
Ed Tanous002d39b2022-05-31 08:59:27 -07002193 std::string path =
2194 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2195 path += rootInterfaceName;
2196 path += "/VLANs/";
2197 path += ifaceItem;
2198 nlohmann::json::object_t iface;
2199 iface["@odata.id"] = std::move(path);
2200 ifaceArray.push_back(std::move(iface));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002201 }
Ed Tanous002d39b2022-05-31 08:59:27 -07002202 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002203
Ed Tanous002d39b2022-05-31 08:59:27 -07002204 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
2205 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2206 asyncResp->res.jsonValue["@odata.id"] =
2207 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2208 rootInterfaceName + "/VLANs";
2209 });
2210 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002211
Ed Tanousbf648f72021-06-03 15:00:14 -07002212 BMCWEB_ROUTE(app,
2213 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
Abhishek Patel3d768a12021-07-31 16:44:51 -05002214 .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
Ed Tanousbf648f72021-06-03 15:00:14 -07002215 .methods(boost::beast::http::verb::post)(
Ed Tanous45ca1b82022-03-25 13:07:27 -07002216 [&app](const crow::Request& req,
2217 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2218 const std::string& rootInterfaceName) {
Carson Labrado3ba00072022-06-06 19:40:56 +00002219 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
Ed Tanous002d39b2022-05-31 08:59:27 -07002220 {
2221 return;
2222 }
2223 bool vlanEnable = false;
2224 uint32_t vlanId = 0;
2225 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2226 "VLANEnable", vlanEnable))
2227 {
2228 return;
2229 }
2230 // Need both vlanId and vlanEnable to service this request
2231 if (vlanId == 0U)
2232 {
2233 messages::propertyMissing(asyncResp->res, "VLANId");
2234 }
2235 if (!vlanEnable)
2236 {
2237 messages::propertyMissing(asyncResp->res, "VLANEnable");
2238 }
2239 if (static_cast<bool>(vlanId) ^ vlanEnable)
2240 {
2241 return;
2242 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002243
Ed Tanous002d39b2022-05-31 08:59:27 -07002244 auto callback = [asyncResp](const boost::system::error_code ec) {
2245 if (ec)
2246 {
2247 // TODO(ed) make more consistent error messages
2248 // based on phosphor-network responses
2249 messages::internalError(asyncResp->res);
2250 return;
2251 }
2252 messages::created(asyncResp->res);
2253 };
2254 crow::connections::systemBus->async_method_call(
2255 std::move(callback), "xyz.openbmc_project.Network",
2256 "/xyz/openbmc_project/network",
2257 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2258 rootInterfaceName, vlanId);
2259 });
Ed Tanousbf648f72021-06-03 15:00:14 -07002260}
2261
Ed Tanous1abe55e2018-09-05 08:30:59 -07002262} // namespace redfish