blob: babfd4a41228941ea42111369fbca0ff35219461 [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 Tanous1abe55e2018-09-05 08:30:59 -070018#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020021#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <node.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023#include <utils/json_utils.hpp>
24
Ed Tanousa24526d2018-12-10 15:17:59 -080025#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053026#include <regex>
Ed Tanousabf2add2019-01-22 16:40:12 -080027#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace redfish
30{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010031
32/**
33 * DBus types primitives for several generic DBus interfaces
34 * TODO(Pawel) consider move this to separate file into boost::dbus
35 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070036using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080037 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010039
Ed Tanous4a0cb852018-10-15 07:55:04 -070040using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070042 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070043 std::string,
44 boost::container::flat_map<
Patrick Williams19bd78d2020-05-13 17:38:24 -050045 std::string,
46 std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
47 uint32_t, int64_t, uint64_t, double,
48 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070049
50enum class LinkType
51{
52 Local,
53 Global
54};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010055
56/**
57 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010058 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070059struct IPv4AddressData
60{
61 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070062 std::string address;
63 std::string domain;
64 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 std::string netmask;
66 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070067 LinkType linktype;
Sunitha Harish01c6e852020-03-20 05:04:09 -050068 bool isActive;
Ed Tanous4a0cb852018-10-15 07:55:04 -070069
Gunnar Mills1214b7e2020-06-04 10:11:30 -050070 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070072 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010074};
75
76/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050077 * Structure for keeping IPv6 data required by Redfish
78 */
79struct IPv6AddressData
80{
81 std::string id;
82 std::string address;
83 std::string origin;
84 uint8_t prefixLength;
85
Gunnar Mills1214b7e2020-06-04 10:11:30 -050086 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050087 {
88 return id < obj.id;
89 }
90};
91/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010092 * Structure for keeping basic single Ethernet Interface information
93 * available from DBus
94 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070095struct EthernetInterfaceData
96{
Ed Tanous4a0cb852018-10-15 07:55:04 -070097 uint32_t speed;
98 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070099 bool DNSEnabled;
100 bool NTPEnabled;
101 bool HostNameEnabled;
102 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800103 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700104 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700105 std::string DHCPEnabled;
106 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700107 std::string hostname;
108 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500109 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500111 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500112 std::vector<std::string> nameServers;
113 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800114 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100115};
116
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700117struct DHCPParameters
118{
119 std::optional<bool> dhcpv4Enabled;
120 std::optional<bool> useDNSServers;
121 std::optional<bool> useNTPServers;
122 std::optional<bool> useUseDomainName;
123 std::optional<std::string> dhcpv6OperatingMode;
124};
125
Ed Tanous4a0cb852018-10-15 07:55:04 -0700126// Helper function that changes bits netmask notation (i.e. /24)
127// into full dot notation
128inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700130 uint32_t value = 0xffffffff << (32 - bits);
131 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
132 std::to_string((value >> 16) & 0xff) + "." +
133 std::to_string((value >> 8) & 0xff) + "." +
134 std::to_string(value & 0xff);
135 return netmask;
136}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100137
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500138inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700139 bool isIPv4)
140{
141 if (isIPv4)
142 {
143 return (
144 (inputDHCP ==
145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
146 (inputDHCP ==
147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
148 }
149 return ((inputDHCP ==
150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
151 (inputDHCP ==
152 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
153}
154
155inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
156{
157 if (isIPv4 && isIPv6)
158 {
159 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
160 }
161 else if (isIPv4)
162 {
163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
164 }
165 else if (isIPv6)
166 {
167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
168 }
169 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
170}
171
Ed Tanous4a0cb852018-10-15 07:55:04 -0700172inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500173 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700174 bool isIPv4)
175{
176 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 return "Static";
179 }
180 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
181 {
182 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700184 return "IPv4LinkLocal";
185 }
186 else
187 {
188 return "LinkLocal";
189 }
190 }
191 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
192 {
193 if (isIPv4)
194 {
195 return "DHCP";
196 }
197 else
198 {
199 return "DHCPv6";
200 }
201 }
202 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
203 {
204 return "SLAAC";
205 }
206 return "";
207}
208
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500209inline bool extractEthernetInterfaceData(const std::string& ethiface_id,
210 const GetManagedObjects& dbus_data,
211 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700213 bool idFound = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500214 for (const auto& objpath : dbus_data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500216 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 {
Ed Tanous029573d2019-02-01 10:57:49 -0800218 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700220 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500227 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800228 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 if (mac != nullptr)
230 {
231 ethData.mac_address = *mac;
232 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700233 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700234 }
235 }
236 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
237 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500238 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 {
240 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500242 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800243 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700244 if (id != nullptr)
245 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500246 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700249 }
250 }
251 else if (ifacePair.first ==
252 "xyz.openbmc_project.Network.EthernetInterface")
253 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500254 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700255 {
256 if (propertyPair.first == "AutoNeg")
257 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500258 const bool* auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800259 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700260 if (auto_neg != nullptr)
261 {
262 ethData.auto_neg = *auto_neg;
263 }
264 }
265 else if (propertyPair.first == "Speed")
266 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500267 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800268 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700269 if (speed != nullptr)
270 {
271 ethData.speed = *speed;
272 }
273 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800274 else if (propertyPair.first == "LinkUp")
275 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500276 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800277 std::get_if<bool>(&propertyPair.second);
278 if (linkUp != nullptr)
279 {
280 ethData.linkUp = *linkUp;
281 }
282 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700283 else if (propertyPair.first == "NICEnabled")
284 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500285 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700286 std::get_if<bool>(&propertyPair.second);
287 if (nicEnabled != nullptr)
288 {
289 ethData.nicEnabled = *nicEnabled;
290 }
291 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500292 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700293 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500294 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500295 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800296 &propertyPair.second);
297 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700298 {
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500299 ethData.nameServers = std::move(*nameservers);
300 }
301 }
302 else if (propertyPair.first == "StaticNameServers")
303 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500304 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500305 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500306 &propertyPair.second);
307 if (staticNameServers != nullptr)
308 {
309 ethData.staticNameServers =
310 std::move(*staticNameServers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700311 }
312 }
manojkiraneda2a133282019-02-19 13:09:43 +0530313 else if (propertyPair.first == "DHCPEnabled")
314 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500315 const std::string* DHCPEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700316 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530317 if (DHCPEnabled != nullptr)
318 {
319 ethData.DHCPEnabled = *DHCPEnabled;
320 }
321 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800322 else if (propertyPair.first == "DomainName")
323 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500324 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500325 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800326 &propertyPair.second);
327 if (domainNames != nullptr)
328 {
329 ethData.domainnames = std::move(*domainNames);
330 }
331 }
Ed Tanous029573d2019-02-01 10:57:49 -0800332 }
333 }
334 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700335
336 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
337 {
338 if (ifacePair.first ==
339 "xyz.openbmc_project.Network.DHCPConfiguration")
340 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500341 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700342 {
343 if (propertyPair.first == "DNSEnabled")
344 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500345 const bool* DNSEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700346 std::get_if<bool>(&propertyPair.second);
347 if (DNSEnabled != nullptr)
348 {
349 ethData.DNSEnabled = *DNSEnabled;
350 }
351 }
352 else if (propertyPair.first == "NTPEnabled")
353 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500354 const bool* NTPEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700355 std::get_if<bool>(&propertyPair.second);
356 if (NTPEnabled != nullptr)
357 {
358 ethData.NTPEnabled = *NTPEnabled;
359 }
360 }
361 else if (propertyPair.first == "HostNameEnabled")
362 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500363 const bool* HostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700364 std::get_if<bool>(&propertyPair.second);
365 if (HostNameEnabled != nullptr)
366 {
367 ethData.HostNameEnabled = *HostNameEnabled;
368 }
369 }
370 else if (propertyPair.first == "SendHostNameEnabled")
371 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500372 const bool* SendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700373 std::get_if<bool>(&propertyPair.second);
374 if (SendHostNameEnabled != nullptr)
375 {
376 ethData.SendHostNameEnabled =
377 *SendHostNameEnabled;
378 }
379 }
380 }
381 }
382 }
Ed Tanous029573d2019-02-01 10:57:49 -0800383 // System configuration shows up in the global namespace, so no need
384 // to check eth number
385 if (ifacePair.first ==
386 "xyz.openbmc_project.Network.SystemConfiguration")
387 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500388 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800389 {
390 if (propertyPair.first == "HostName")
391 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500392 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500393 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800394 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700395 {
Ed Tanous029573d2019-02-01 10:57:49 -0800396 ethData.hostname = *hostname;
397 }
398 }
399 else if (propertyPair.first == "DefaultGateway")
400 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500401 const std::string* defaultGateway =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500402 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800403 if (defaultGateway != nullptr)
404 {
405 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700406 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500408 else if (propertyPair.first == "DefaultGateway6")
409 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500410 const std::string* defaultGateway6 =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500411 std::get_if<std::string>(&propertyPair.second);
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500412 if (defaultGateway6 != nullptr)
413 {
414 ethData.ipv6_default_gateway = *defaultGateway6;
415 }
416 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 }
418 }
419 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700421 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700422}
423
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500424// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700425inline void
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500426 extractIPV6Data(const std::string& ethiface_id,
427 const GetManagedObjects& dbus_data,
428 boost::container::flat_set<IPv6AddressData>& ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500429{
430 const std::string ipv6PathStart =
431 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
432
433 // Since there might be several IPv6 configurations aligned with
434 // single ethernet interface, loop over all of them
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500435 for (const auto& objpath : dbus_data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500436 {
437 // Check if proper pattern for object path appears
438 if (boost::starts_with(objpath.first.str, ipv6PathStart))
439 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500440 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441 {
442 if (interface.first == "xyz.openbmc_project.Network.IP")
443 {
444 // Instance IPv6AddressData structure, and set as
445 // appropriate
446 std::pair<
447 boost::container::flat_set<IPv6AddressData>::iterator,
448 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700449 it = ipv6_config.insert(IPv6AddressData{});
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500450 IPv6AddressData& ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700451 ipv6_address.id =
452 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500453 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500454 {
455 if (property.first == "Address")
456 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500457 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500458 std::get_if<std::string>(&property.second);
459 if (address != nullptr)
460 {
461 ipv6_address.address = *address;
462 }
463 }
464 else if (property.first == "Origin")
465 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500466 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500467 std::get_if<std::string>(&property.second);
468 if (origin != nullptr)
469 {
470 ipv6_address.origin =
471 translateAddressOriginDbusToRedfish(*origin,
472 false);
473 }
474 }
475 else if (property.first == "PrefixLength")
476 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500477 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500478 std::get_if<uint8_t>(&property.second);
479 if (prefix != nullptr)
480 {
481 ipv6_address.prefixLength = *prefix;
482 }
483 }
484 else
485 {
486 BMCWEB_LOG_ERROR
487 << "Got extra property: " << property.first
488 << " on the " << objpath.first.str << " object";
489 }
490 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500491 }
492 }
493 }
494 }
495}
496
Ed Tanous4a0cb852018-10-15 07:55:04 -0700497// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700498inline void
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500499 extractIPData(const std::string& ethiface_id,
500 const GetManagedObjects& dbus_data,
501 boost::container::flat_set<IPv4AddressData>& ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700502{
503 const std::string ipv4PathStart =
504 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
505
506 // Since there might be several IPv4 configurations aligned with
507 // single ethernet interface, loop over all of them
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500508 for (const auto& objpath : dbus_data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700509 {
510 // Check if proper pattern for object path appears
511 if (boost::starts_with(objpath.first.str, ipv4PathStart))
512 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500513 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514 {
515 if (interface.first == "xyz.openbmc_project.Network.IP")
516 {
517 // Instance IPv4AddressData structure, and set as
518 // appropriate
519 std::pair<
520 boost::container::flat_set<IPv4AddressData>::iterator,
521 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700522 it = ipv4_config.insert(IPv4AddressData{});
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500523 IPv4AddressData& ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700524 ipv4_address.id =
525 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500526 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 {
528 if (property.first == "Address")
529 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500530 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800531 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700532 if (address != nullptr)
533 {
534 ipv4_address.address = *address;
535 }
536 }
537 else if (property.first == "Gateway")
538 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500539 const std::string* gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800540 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 if (gateway != nullptr)
542 {
543 ipv4_address.gateway = *gateway;
544 }
545 }
546 else if (property.first == "Origin")
547 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500548 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800549 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700550 if (origin != nullptr)
551 {
552 ipv4_address.origin =
553 translateAddressOriginDbusToRedfish(*origin,
554 true);
555 }
556 }
557 else if (property.first == "PrefixLength")
558 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500559 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800560 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700561 if (mask != nullptr)
562 {
563 // convert it to the string
564 ipv4_address.netmask = getNetmask(*mask);
565 }
566 }
567 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
575 ipv4_address.linktype =
576 boost::starts_with(ipv4_address.address, "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/**
586 * @brief Sets given Id on the given VLAN interface through D-Bus
587 *
588 * @param[in] ifaceId Id of VLAN interface that should be modified
589 * @param[in] inputVlanId New ID of the VLAN
590 * @param[in] callback Function that will be called after the operation
591 *
592 * @return None.
593 */
594template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500595void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
596 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700597{
598 crow::connections::systemBus->async_method_call(
599 callback, "xyz.openbmc_project.Network",
600 std::string("/xyz/openbmc_project/network/") + ifaceId,
601 "org.freedesktop.DBus.Properties", "Set",
602 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800603 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604}
605
606/**
607 * @brief Helper function that verifies IP address to check if it is in
608 * proper format. If bits pointer is provided, also calculates active
609 * bit count for Subnet Mask.
610 *
611 * @param[in] ip IP that will be verified
612 * @param[out] bits Calculated mask in bits notation
613 *
614 * @return true in case of success, false otherwise
615 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500616inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
617 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700618{
619 std::vector<std::string> bytesInMask;
620
621 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700622
623 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700626 return false;
627 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700631 *bits = 0;
632 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500634 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 long previousValue = 255;
636 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500637 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700639 if (byte.empty())
640 {
641 return false;
642 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700643
Ed Tanous4a0cb852018-10-15 07:55:04 -0700644 // Use strtol instead of stroi to avoid exceptions
645 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646
Ed Tanous4a0cb852018-10-15 07:55:04 -0700647 // endPtr should point to the end of the string, otherwise given string
648 // is not 100% number
649 if (*endPtr != '\0')
650 {
651 return false;
652 }
653
654 // Value should be contained in byte
655 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 {
657 return false;
658 }
659
660 if (bits != nullptr)
661 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 // Mask has to be continuous between bytes
663 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 {
665 return false;
666 }
667
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 // Mask has to be continuous inside bytes
669 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 // Count bits
Ed Tanous23a21a12020-07-25 04:45:05 +0000672 for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000674 if (value & (1L << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700676 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 // Continuity not preserved
679 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700680 }
681 else
682 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 }
685 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 else
687 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700688 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700690 }
691 }
692
693 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 }
695
Ed Tanous4a0cb852018-10-15 07:55:04 -0700696 return true;
697}
698
699/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700700 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 *
702 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700703 * @param[in] ipHash DBus Hash id of IP that should be deleted
704 * @param[io] asyncResp Response object that will be returned to client
705 *
706 * @return None
707 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500708inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700709 const std::shared_ptr<AsyncResp> asyncResp)
710{
711 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700712 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700713 if (ec)
714 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800715 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100716 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 },
718 "xyz.openbmc_project.Network",
719 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
720 "xyz.openbmc_project.Object.Delete", "Delete");
721}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722
Ed Tanous4a0cb852018-10-15 07:55:04 -0700723/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700724 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700725 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700726 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
727 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
728 * @param[in] gateway IPv4 address of this interfaces gateway
729 * @param[in] address IPv4 address to assign to this interface
730 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700731 *
732 * @return None
733 */
Ed Tanouscb13a392020-07-25 19:02:03 +0000734inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
735 const std::string& gateway, const std::string& address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700736 std::shared_ptr<AsyncResp> asyncResp)
737{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700738 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700739 [asyncResp](const boost::system::error_code ec) {
740 if (ec)
741 {
742 messages::internalError(asyncResp->res);
743 }
744 },
745 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700746 "/xyz/openbmc_project/network/" + ifaceId,
747 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700748 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700749 gateway);
750}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500751
752/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700753 * @brief Deletes the IPv4 entry for this interface and creates a replacement
754 * static IPv4 entry
755 *
756 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
757 * @param[in] id The unique hash entry identifying the DBus entry
758 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
759 * @param[in] gateway IPv4 address of this interfaces gateway
760 * @param[in] address IPv4 address to assign to this interface
761 * @param[io] asyncResp Response object that will be returned to client
762 *
763 * @return None
764 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500765inline void deleteAndCreateIPv4(const std::string& ifaceId,
766 const std::string& id, uint8_t prefixLength,
767 const std::string& gateway,
768 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700769 std::shared_ptr<AsyncResp> asyncResp)
770{
771 crow::connections::systemBus->async_method_call(
772 [asyncResp, ifaceId, address, prefixLength,
773 gateway](const boost::system::error_code ec) {
774 if (ec)
775 {
776 messages::internalError(asyncResp->res);
777 }
778 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000779 [asyncResp](const boost::system::error_code ec2) {
780 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700781 {
782 messages::internalError(asyncResp->res);
783 }
784 },
785 "xyz.openbmc_project.Network",
786 "/xyz/openbmc_project/network/" + ifaceId,
787 "xyz.openbmc_project.Network.IP.Create", "IP",
788 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
789 prefixLength, gateway);
790 },
791 "xyz.openbmc_project.Network",
792 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
793 "xyz.openbmc_project.Object.Delete", "Delete");
794}
795
796/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500797 * @brief Deletes given IPv6
798 *
799 * @param[in] ifaceId Id of interface whose IP should be deleted
800 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500801 * @param[io] asyncResp Response object that will be returned to client
802 *
803 * @return None
804 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500805inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500806 const std::shared_ptr<AsyncResp> asyncResp)
807{
808 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700809 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500810 if (ec)
811 {
812 messages::internalError(asyncResp->res);
813 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500814 },
815 "xyz.openbmc_project.Network",
816 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
817 "xyz.openbmc_project.Object.Delete", "Delete");
818}
819
820/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700821 * @brief Deletes the IPv6 entry for this interface and creates a replacement
822 * static IPv6 entry
823 *
824 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
825 * @param[in] id The unique hash entry identifying the DBus entry
826 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
827 * @param[in] address IPv6 address to assign to this interface
828 * @param[io] asyncResp Response object that will be returned to client
829 *
830 * @return None
831 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500832inline void deleteAndCreateIPv6(const std::string& ifaceId,
833 const std::string& id, uint8_t prefixLength,
834 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700835 std::shared_ptr<AsyncResp> asyncResp)
836{
837 crow::connections::systemBus->async_method_call(
838 [asyncResp, ifaceId, address,
839 prefixLength](const boost::system::error_code ec) {
840 if (ec)
841 {
842 messages::internalError(asyncResp->res);
843 }
844 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000845 [asyncResp](const boost::system::error_code ec2) {
846 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700847 {
848 messages::internalError(asyncResp->res);
849 }
850 },
851 "xyz.openbmc_project.Network",
852 "/xyz/openbmc_project/network/" + ifaceId,
853 "xyz.openbmc_project.Network.IP.Create", "IP",
854 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
855 prefixLength, "");
856 },
857 "xyz.openbmc_project.Network",
858 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
859 "xyz.openbmc_project.Object.Delete", "Delete");
860}
861
862/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500863 * @brief Creates IPv6 with given data
864 *
865 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500866 * @param[in] prefixLength Prefix length that needs to be added
867 * @param[in] address IP address that needs to be added
868 * @param[io] asyncResp Response object that will be returned to client
869 *
870 * @return None
871 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500872inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
873 const std::string& address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500874 std::shared_ptr<AsyncResp> asyncResp)
875{
876 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
877 if (ec)
878 {
879 messages::internalError(asyncResp->res);
880 }
881 };
882 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500883 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500884 crow::connections::systemBus->async_method_call(
885 std::move(createIpHandler), "xyz.openbmc_project.Network",
886 "/xyz/openbmc_project/network/" + ifaceId,
887 "xyz.openbmc_project.Network.IP.Create", "IP",
888 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
889 "");
890}
891
Ed Tanous4a0cb852018-10-15 07:55:04 -0700892/**
893 * Function that retrieves all properties for given Ethernet Interface
894 * Object
895 * from EntityManager Network Manager
896 * @param ethiface_id a eth interface id to query on DBus
897 * @param callback a function that shall be called to convert Dbus output
898 * into JSON
899 */
900template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500901void getEthernetIfaceData(const std::string& ethiface_id,
902 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903{
904 crow::connections::systemBus->async_method_call(
905 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
906 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500907 const GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700908 EthernetInterfaceData ethData{};
909 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500910 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700911
912 if (error_code)
913 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700914 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700915 return;
916 }
917
Ed Tanous4c9afe42019-05-03 16:59:57 -0700918 bool found =
919 extractEthernetInterfaceData(ethiface_id, resp, ethData);
920 if (!found)
921 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700922 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700923 return;
924 }
925
Johnathan Mantey01784822019-06-18 12:44:21 -0700926 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700927 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500928 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700929 {
Ravi Tejac6191412019-07-30 00:53:50 -0500930 if (((ipv4.linktype == LinkType::Global) &&
931 (ipv4.gateway == "0.0.0.0")) ||
932 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934 ipv4.gateway = ethData.default_gateway;
935 }
936 }
937
Johnathan Mantey01784822019-06-18 12:44:21 -0700938 extractIPV6Data(ethiface_id, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500939 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700940 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 },
942 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
943 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700944}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700945
946/**
947 * Function that retrieves all Ethernet Interfaces available through Network
948 * Manager
949 * @param callback a function that shall be called to convert Dbus output
950 * into JSON.
951 */
952template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500953void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954{
955 crow::connections::systemBus->async_method_call(
956 [callback{std::move(callback)}](
957 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500958 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700959 // Callback requires vector<string> to retrieve all available
960 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700961 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 iface_list.reserve(resp.size());
963 if (error_code)
964 {
965 callback(false, iface_list);
966 return;
967 }
968
969 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500970 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700971 {
972 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500973 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700974 {
975 // If interface is
976 // xyz.openbmc_project.Network.EthernetInterface, this is
977 // what we're looking for.
978 if (interface.first ==
979 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500981 // Cut out everything until last "/", ...
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500982 const std::string& iface_id = objpath.first.str;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700983 std::size_t last_pos = iface_id.rfind("/");
984 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700987 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 }
989 }
990 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 }
992 // Finally make a callback with useful data
993 callback(true, iface_list);
994 },
995 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700997}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100998
999/**
1000 * EthernetCollection derived class for delivering Ethernet Collection Schema
1001 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002class EthernetCollection : public Node
1003{
1004 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001005 EthernetCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001006 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 entityPrivileges = {
1009 {boost::beast::http::verb::get, {{"Login"}}},
1010 {boost::beast::http::verb::head, {{"Login"}}},
1011 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1012 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1013 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1014 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1015 }
1016
1017 private:
1018 /**
1019 * Functions triggers appropriate requests on DBus
1020 */
Ed Tanouscb13a392020-07-25 19:02:03 +00001021 void doGet(crow::Response& res, const crow::Request&,
1022 const std::vector<std::string>&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001024 res.jsonValue["@odata.type"] =
1025 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001026 res.jsonValue["@odata.id"] =
1027 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1028 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1029 res.jsonValue["Description"] =
1030 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001031 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001032 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001034 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001035 [asyncResp](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001036 const bool& success,
1037 const boost::container::flat_set<std::string>& iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001038 if (!success)
1039 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001040 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001041 return;
1042 }
1043
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001044 nlohmann::json& iface_array =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001045 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001046 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001047 std::string tag = "_";
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001048 for (const std::string& iface_item : iface_list)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001049 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001050 std::size_t found = iface_item.find(tag);
1051 if (found == std::string::npos)
1052 {
1053 iface_array.push_back(
1054 {{"@odata.id",
1055 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1056 iface_item}});
1057 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001058 }
1059
Ed Tanous4c9afe42019-05-03 16:59:57 -07001060 asyncResp->res.jsonValue["Members@odata.count"] =
1061 iface_array.size();
1062 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001063 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001064 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001065 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001066};
1067
1068/**
1069 * EthernetInterface derived class for delivering Ethernet Schema
1070 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071class EthernetInterface : public Node
1072{
1073 public:
1074 /*
1075 * Default Constructor
1076 */
Ed Tanous52cc1122020-07-18 13:51:21 -07001077 EthernetInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001078 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 std::string())
1080 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 entityPrivileges = {
1082 {boost::beast::http::verb::get, {{"Login"}}},
1083 {boost::beast::http::verb::head, {{"Login"}}},
1084 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1085 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1086 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1087 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001088 }
1089
Ed Tanous1abe55e2018-09-05 08:30:59 -07001090 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001091 void handleHostnamePatch(const std::string& hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001092 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001093 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301094 // SHOULD handle host names of up to 255 characters(RFC 1123)
1095 if (hostname.length() > 255)
1096 {
1097 messages::propertyValueFormatError(asyncResp->res, hostname,
1098 "HostName");
1099 return;
1100 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001101 crow::connections::systemBus->async_method_call(
1102 [asyncResp](const boost::system::error_code ec) {
1103 if (ec)
1104 {
1105 messages::internalError(asyncResp->res);
1106 }
1107 },
1108 "xyz.openbmc_project.Network",
1109 "/xyz/openbmc_project/network/config",
1110 "org.freedesktop.DBus.Properties", "Set",
1111 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001112 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001113 }
1114
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001115 void handleDomainnamePatch(const std::string& ifaceId,
1116 const std::string& domainname,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301117 const std::shared_ptr<AsyncResp> asyncResp)
1118 {
1119 std::vector<std::string> vectorDomainname = {domainname};
1120 crow::connections::systemBus->async_method_call(
1121 [asyncResp](const boost::system::error_code ec) {
1122 if (ec)
1123 {
1124 messages::internalError(asyncResp->res);
1125 }
1126 },
1127 "xyz.openbmc_project.Network",
1128 "/xyz/openbmc_project/network/" + ifaceId,
1129 "org.freedesktop.DBus.Properties", "Set",
1130 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1131 std::variant<std::vector<std::string>>(vectorDomainname));
1132 }
1133
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001134 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301135 const std::shared_ptr<AsyncResp> asyncResp)
1136 {
1137 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1138 if (fqdn.length() > 255)
1139 {
1140 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1141 return;
1142 }
1143
1144 size_t pos = fqdn.find('.');
1145 if (pos == std::string::npos)
1146 {
1147 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1148 return;
1149 }
1150
1151 std::string hostname;
1152 std::string domainname;
1153 domainname = (fqdn).substr(pos + 1);
1154 hostname = (fqdn).substr(0, pos);
1155
1156 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1157 {
1158 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1159 return;
1160 }
1161
1162 handleHostnamePatch(hostname, asyncResp);
1163 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1164 }
1165
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001166 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301167 {
1168 // A valid host name can never have the dotted-decimal form (RFC 1123)
1169 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1170 {
1171 return false;
1172 }
1173 // Each label(hostname/subdomains) within a valid FQDN
1174 // MUST handle host names of up to 63 characters (RFC 1123)
1175 // labels cannot start or end with hyphens (RFC 952)
1176 // labels can start with numbers (RFC 1123)
1177 const std::regex pattern(
1178 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1179
1180 return std::regex_match(hostname, pattern);
1181 }
1182
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001183 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301184 {
1185 // Can have multiple subdomains
1186 // Top Level Domain's min length is 2 character
1187 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1188 "{1,30}\\.)*[a-zA-Z]{2,}$");
1189
1190 return std::regex_match(domainname, pattern);
1191 }
1192
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001193 void handleMACAddressPatch(const std::string& ifaceId,
1194 const std::string& macAddress,
1195 const std::shared_ptr<AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301196 {
1197 crow::connections::systemBus->async_method_call(
1198 [asyncResp, macAddress](const boost::system::error_code ec) {
1199 if (ec)
1200 {
1201 messages::internalError(asyncResp->res);
1202 return;
1203 }
Ratan Guptad5776652019-03-03 08:47:22 +05301204 },
1205 "xyz.openbmc_project.Network",
1206 "/xyz/openbmc_project/network/" + ifaceId,
1207 "org.freedesktop.DBus.Properties", "Set",
1208 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1209 std::variant<std::string>(macAddress));
1210 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001211
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001212 void setDHCPEnabled(const std::string& ifaceId,
1213 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001214 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001215 const std::shared_ptr<AsyncResp> asyncResp)
1216 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001217 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001218 crow::connections::systemBus->async_method_call(
1219 [asyncResp](const boost::system::error_code ec) {
1220 if (ec)
1221 {
1222 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1223 messages::internalError(asyncResp->res);
1224 return;
1225 }
1226 },
1227 "xyz.openbmc_project.Network",
1228 "/xyz/openbmc_project/network/" + ifaceId,
1229 "org.freedesktop.DBus.Properties", "Set",
1230 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001231 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001232 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001233
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001234 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001235 const std::string& ifaceId, const std::string& propertyName,
1236 const bool& value, const std::shared_ptr<AsyncResp> asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001237 {
1238 crow::connections::systemBus->async_method_call(
1239 [asyncResp](const boost::system::error_code ec) {
1240 if (ec)
1241 {
1242 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1243 messages::internalError(asyncResp->res);
1244 return;
1245 }
1246 },
1247 "xyz.openbmc_project.Network",
1248 "/xyz/openbmc_project/network/" + ifaceId,
1249 "org.freedesktop.DBus.Properties", "Set",
1250 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1251 std::variant<bool>{value});
1252 }
1253
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001254 void setDHCPv4Config(const std::string& propertyName, const bool& value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001255 const std::shared_ptr<AsyncResp> asyncResp)
1256 {
1257 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1258 crow::connections::systemBus->async_method_call(
1259 [asyncResp](const boost::system::error_code ec) {
1260 if (ec)
1261 {
1262 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1263 messages::internalError(asyncResp->res);
1264 return;
1265 }
1266 },
1267 "xyz.openbmc_project.Network",
1268 "/xyz/openbmc_project/network/config/dhcp",
1269 "org.freedesktop.DBus.Properties", "Set",
1270 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1271 std::variant<bool>{value});
1272 }
Ratan Guptad5776652019-03-03 08:47:22 +05301273
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001274 void handleDHCPPatch(const std::string& ifaceId,
1275 const EthernetInterfaceData& ethData,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001276 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1277 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001278 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001279 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1280 bool ipv6Active =
1281 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001282
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001283 bool nextv4DHCPState =
1284 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1285
1286 bool nextv6DHCPState{};
1287 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001288 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001289 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1290 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1291 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1292 {
1293 messages::propertyValueFormatError(
1294 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1295 "OperatingMode");
1296 return;
1297 }
1298 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1299 }
1300 else
1301 {
1302 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001303 }
1304
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001305 bool nextDNS{};
1306 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001307 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001308 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1309 {
1310 messages::generalError(asyncResp->res);
1311 return;
1312 }
1313 nextDNS = *v4dhcpParms.useDNSServers;
1314 }
1315 else if (v4dhcpParms.useDNSServers)
1316 {
1317 nextDNS = *v4dhcpParms.useDNSServers;
1318 }
1319 else if (v6dhcpParms.useDNSServers)
1320 {
1321 nextDNS = *v6dhcpParms.useDNSServers;
1322 }
1323 else
1324 {
1325 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001326 }
1327
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001328 bool nextNTP{};
1329 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001330 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001331 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1332 {
1333 messages::generalError(asyncResp->res);
1334 return;
1335 }
1336 nextNTP = *v4dhcpParms.useNTPServers;
1337 }
1338 else if (v4dhcpParms.useNTPServers)
1339 {
1340 nextNTP = *v4dhcpParms.useNTPServers;
1341 }
1342 else if (v6dhcpParms.useNTPServers)
1343 {
1344 nextNTP = *v6dhcpParms.useNTPServers;
1345 }
1346 else
1347 {
1348 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001349 }
1350
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001351 bool nextUseDomain{};
1352 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001353 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001354 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1355 {
1356 messages::generalError(asyncResp->res);
1357 return;
1358 }
1359 nextUseDomain = *v4dhcpParms.useUseDomainName;
1360 }
1361 else if (v4dhcpParms.useUseDomainName)
1362 {
1363 nextUseDomain = *v4dhcpParms.useUseDomainName;
1364 }
1365 else if (v6dhcpParms.useUseDomainName)
1366 {
1367 nextUseDomain = *v6dhcpParms.useUseDomainName;
1368 }
1369 else
1370 {
1371 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001372 }
1373
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001374 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1375 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1376 asyncResp);
1377 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1378 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1379 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1380 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1381 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1382 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001383 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001384
1385 boost::container::flat_set<IPv4AddressData>::const_iterator
1386 GetNextStaticIPEntry(
1387 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1388 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1389 {
1390 for (; head != end; head++)
1391 {
1392 if (head->origin == "Static")
1393 {
1394 return head;
1395 }
1396 }
1397 return end;
1398 }
1399
1400 boost::container::flat_set<IPv6AddressData>::const_iterator
1401 GetNextStaticIPEntry(
1402 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1403 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1404 {
1405 for (; head != end; head++)
1406 {
1407 if (head->origin == "Static")
1408 {
1409 return head;
1410 }
1411 }
1412 return end;
1413 }
1414
Ravi Tejad1d50812019-06-23 16:20:27 -05001415 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001416 const std::string& ifaceId, nlohmann::json& input,
1417 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001418 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001419 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001420 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301421 {
1422 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001423 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301424 return;
1425 }
1426
Ed Tanous271584a2019-07-09 16:24:22 -07001427 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001428 // Find the first static IP address currently active on the NIC and
1429 // match it to the first JSON element in the IPv4StaticAddresses array.
1430 // Match each subsequent JSON element to the next static IP programmed
1431 // into the NIC.
1432 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1433 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1434
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001435 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001436 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001437 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001438 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001439
Johnathan Mantey01784822019-06-18 12:44:21 -07001440 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301441 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001442 std::optional<std::string> address;
1443 std::optional<std::string> subnetMask;
1444 std::optional<std::string> gateway;
1445
1446 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1447 address, "SubnetMask", subnetMask,
1448 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301449 {
1450 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001451 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301452 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301453 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301454
Johnathan Mantey01784822019-06-18 12:44:21 -07001455 // Find the address/subnet/gateway values. Any values that are
1456 // not explicitly provided are assumed to be unmodified from the
1457 // current state of the interface. Merge existing state into the
1458 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001459 const std::string* addr = nullptr;
1460 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001461 uint8_t prefixLength = 0;
1462 bool errorInEntry = false;
1463 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301464 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001465 if (ipv4VerifyIpAndGetBitcount(*address))
1466 {
1467 addr = &(*address);
1468 }
1469 else
1470 {
1471 messages::propertyValueFormatError(
1472 asyncResp->res, *address, pathString + "/Address");
1473 errorInEntry = true;
1474 }
1475 }
1476 else if (NICIPentry != ipv4Data.cend())
1477 {
1478 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301479 }
1480 else
1481 {
1482 messages::propertyMissing(asyncResp->res,
1483 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001484 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001485 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301486
1487 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001488 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001489 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1490 {
1491 messages::propertyValueFormatError(
1492 asyncResp->res, *subnetMask,
1493 pathString + "/SubnetMask");
1494 errorInEntry = true;
1495 }
1496 }
1497 else if (NICIPentry != ipv4Data.cend())
1498 {
1499 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1500 &prefixLength))
1501 {
1502 messages::propertyValueFormatError(
1503 asyncResp->res, NICIPentry->netmask,
1504 pathString + "/SubnetMask");
1505 errorInEntry = true;
1506 }
1507 }
1508 else
1509 {
1510 messages::propertyMissing(asyncResp->res,
1511 pathString + "/SubnetMask");
1512 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001513 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301514
Ratan Guptaf476acb2019-03-02 16:46:57 +05301515 if (gateway)
1516 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001517 if (ipv4VerifyIpAndGetBitcount(*gateway))
1518 {
1519 gw = &(*gateway);
1520 }
1521 else
1522 {
1523 messages::propertyValueFormatError(
1524 asyncResp->res, *gateway, pathString + "/Gateway");
1525 errorInEntry = true;
1526 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301527 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001528 else if (NICIPentry != ipv4Data.cend())
1529 {
1530 gw = &NICIPentry->gateway;
1531 }
1532 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001533 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001534 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001535 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001536 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001537 }
1538
Johnathan Mantey01784822019-06-18 12:44:21 -07001539 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001540 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001541 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001542 }
1543
Johnathan Mantey01784822019-06-18 12:44:21 -07001544 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001545 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001546 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1547 *gw, *addr, asyncResp);
1548 NICIPentry =
1549 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001550 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001551 else
1552 {
Ed Tanouscb13a392020-07-25 19:02:03 +00001553 createIPv4(ifaceId, prefixLength, *gateway, *address,
1554 asyncResp);
Johnathan Mantey01784822019-06-18 12:44:21 -07001555 }
1556 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001557 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001558 else
1559 {
1560 if (NICIPentry == ipv4Data.cend())
1561 {
1562 // Requesting a DELETE/DO NOT MODIFY action for an item
1563 // that isn't present on the eth(n) interface. Input JSON is
1564 // in error, so bail out.
1565 if (thisJson.is_null())
1566 {
1567 messages::resourceCannotBeDeleted(asyncResp->res);
1568 return;
1569 }
1570 else
1571 {
1572 messages::propertyValueFormatError(
1573 asyncResp->res, thisJson.dump(), pathString);
1574 return;
1575 }
1576 }
1577
1578 if (thisJson.is_null())
1579 {
1580 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1581 }
1582 if (NICIPentry != ipv4Data.cend())
1583 {
1584 NICIPentry =
1585 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1586 }
1587 entryIdx++;
1588 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001589 }
1590 }
1591
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001592 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001593 const std::string& ifaceId,
1594 const std::vector<std::string>& updatedStaticNameServers,
1595 const std::shared_ptr<AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001596 {
1597 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001598 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001599 if (ec)
1600 {
1601 messages::internalError(asyncResp->res);
1602 return;
1603 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001604 },
1605 "xyz.openbmc_project.Network",
1606 "/xyz/openbmc_project/network/" + ifaceId,
1607 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001608 "xyz.openbmc_project.Network.EthernetInterface",
1609 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001610 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1611 }
1612
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001613 void handleIPv6StaticAddressesPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001614 const std::string& ifaceId, nlohmann::json& input,
1615 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001616 const std::shared_ptr<AsyncResp> asyncResp)
1617 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001618 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001619 {
1620 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1621 "IPv6StaticAddresses");
1622 return;
1623 }
Ed Tanous271584a2019-07-09 16:24:22 -07001624 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001625 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1626 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001627 for (nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001628 {
1629 std::string pathString =
1630 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1631
Johnathan Mantey01784822019-06-18 12:44:21 -07001632 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001633 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001634 std::optional<std::string> address;
1635 std::optional<uint8_t> prefixLength;
1636
1637 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1638 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001639 {
1640 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001641 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001642 return;
1643 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001644
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001645 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001646 uint8_t prefix;
1647
1648 // Find the address and prefixLength values. Any values that are
1649 // not explicitly provided are assumed to be unmodified from the
1650 // current state of the interface. Merge existing state into the
1651 // current request.
1652 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001653 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001654 addr = &(*address);
1655 }
1656 else if (NICIPentry != ipv6Data.end())
1657 {
1658 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001659 }
1660 else
1661 {
1662 messages::propertyMissing(asyncResp->res,
1663 pathString + "/Address");
1664 return;
1665 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001666
1667 if (prefixLength)
1668 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001669 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001670 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001671 else if (NICIPentry != ipv6Data.end())
1672 {
1673 prefix = NICIPentry->prefixLength;
1674 }
1675 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001676 {
1677 messages::propertyMissing(asyncResp->res,
1678 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001679 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001680 }
1681
Johnathan Mantey01784822019-06-18 12:44:21 -07001682 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001683 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001684 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1685 asyncResp);
1686 NICIPentry =
1687 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1688 }
1689 else
1690 {
1691 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1692 }
1693 entryIdx++;
1694 }
1695 else
1696 {
1697 if (NICIPentry == ipv6Data.end())
1698 {
1699 // Requesting a DELETE/DO NOT MODIFY action for an item
1700 // that isn't present on the eth(n) interface. Input JSON is
1701 // in error, so bail out.
1702 if (thisJson.is_null())
1703 {
1704 messages::resourceCannotBeDeleted(asyncResp->res);
1705 return;
1706 }
1707 else
1708 {
1709 messages::propertyValueFormatError(
1710 asyncResp->res, thisJson.dump(), pathString);
1711 return;
1712 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001713 }
1714
Johnathan Mantey01784822019-06-18 12:44:21 -07001715 if (thisJson.is_null())
1716 {
1717 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1718 }
1719 if (NICIPentry != ipv6Data.cend())
1720 {
1721 NICIPentry =
1722 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1723 }
1724 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001725 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001726 }
1727 }
1728
Ed Tanous0f74e642018-11-12 15:17:05 -08001729 void parseInterfaceData(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001730 std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id,
1731 const EthernetInterfaceData& ethData,
1732 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1733 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001734 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001735 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001736 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1737
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001738 nlohmann::json& json_response = asyncResp->res.jsonValue;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001739 json_response["Id"] = iface_id;
1740 json_response["@odata.id"] =
1741 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001742 json_response["InterfaceEnabled"] = ethData.nicEnabled;
1743
1744 auto health = std::make_shared<HealthPopulate>(asyncResp);
1745
1746 crow::connections::systemBus->async_method_call(
1747 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001748 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001749 if (ec)
1750 {
1751 return;
1752 }
1753
1754 health->inventory = std::move(resp);
1755 },
1756 "xyz.openbmc_project.ObjectMapper",
1757 "/xyz/openbmc_project/object_mapper",
1758 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1759 int32_t(0), inventoryForEthernet);
1760
1761 health->populate();
1762
1763 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001764 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001765 json_response["LinkStatus"] = "LinkUp";
1766 json_response["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001767 }
1768 else
1769 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001770 json_response["LinkStatus"] = "NoLink";
1771 json_response["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001772 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001773
1774 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001775 json_response["SpeedMbps"] = ethData.speed;
1776 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001777 json_response["DHCPv4"]["DHCPEnabled"] =
1778 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1779 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1780 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1781 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1782
1783 json_response["DHCPv6"]["OperatingMode"] =
1784 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1785 : "Disabled";
1786 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1787 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1788 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301789
Ed Tanous4a0cb852018-10-15 07:55:04 -07001790 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001791 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001792 json_response["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301793
1794 // When domain name is empty then it means, that it is a network
1795 // without domain names, and the host name itself must be treated as
1796 // FQDN
1797 std::string FQDN = std::move(ethData.hostname);
Jennifer Leed24bfc72019-03-05 13:03:37 -08001798 if (!ethData.domainnames.empty())
1799 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301800 FQDN += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001801 }
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301802 json_response["FQDN"] = FQDN;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001803 }
1804
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001805 json_response["VLANs"] = {
1806 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1807 iface_id + "/VLANs"}};
1808
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001809 json_response["NameServers"] = ethData.nameServers;
1810 json_response["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001811
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001812 nlohmann::json& ipv4_array = json_response["IPv4Addresses"];
1813 nlohmann::json& ipv4_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001814 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001815 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001816 ipv4_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001817 for (auto& ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001818 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001819
1820 std::string gatewayStr = ipv4_config.gateway;
1821 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001822 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001823 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001824 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001825
1826 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1827 {"SubnetMask", ipv4_config.netmask},
1828 {"Address", ipv4_config.address},
1829 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001830 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001831 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001832 ipv4_static_array.push_back(
1833 {{"AddressOrigin", ipv4_config.origin},
1834 {"SubnetMask", ipv4_config.netmask},
1835 {"Address", ipv4_config.address},
1836 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001837 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001838 }
1839
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001840 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001841
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001842 nlohmann::json& ipv6_array = json_response["IPv6Addresses"];
1843 nlohmann::json& ipv6_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001844 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001845 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001846 ipv6_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001847 nlohmann::json& ipv6AddrPolicyTable =
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001848 json_response["IPv6AddressPolicyTable"];
1849 ipv6AddrPolicyTable = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001850 for (auto& ipv6_config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001851 {
1852 ipv6_array.push_back({{"Address", ipv6_config.address},
1853 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001854 {"AddressOrigin", ipv6_config.origin},
1855 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001856 if (ipv6_config.origin == "Static")
1857 {
1858 ipv6_static_array.push_back(
1859 {{"Address", ipv6_config.address},
1860 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001861 {"AddressOrigin", ipv6_config.origin},
1862 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001863 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001864 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001865 }
1866
1867 /**
1868 * Functions triggers appropriate requests on DBus
1869 */
Ed Tanouscb13a392020-07-25 19:02:03 +00001870 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001871 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001872 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001873 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001874 if (params.size() != 1)
1875 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001876 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001877 return;
1878 }
1879
Ed Tanous4a0cb852018-10-15 07:55:04 -07001880 getEthernetIfaceData(
1881 params[0],
1882 [this, asyncResp, iface_id{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001883 const bool& success, const EthernetInterfaceData& ethData,
1884 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1885 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001886 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001887 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001888 // TODO(Pawel)consider distinguish between non existing
1889 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001890 messages::resourceNotFound(asyncResp->res,
1891 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001892 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001893 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001894
Ed Tanous0f74e642018-11-12 15:17:05 -08001895 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001896 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001897 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1898 asyncResp->res.jsonValue["Description"] =
1899 "Management Network Interface";
1900
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001901 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1902 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001903 });
1904 }
1905
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001906 void doPatch(crow::Response& res, const crow::Request& req,
1907 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001908 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001909 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001910 if (params.size() != 1)
1911 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001912 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001913 return;
1914 }
1915
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001916 const std::string& iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001917
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001918 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301919 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301920 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001921 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001922 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001923 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001924 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001925 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001926 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001927 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001928 DHCPParameters v4dhcpParms;
1929 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001930
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001931 if (!json_util::readJson(
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301932 req, res, "HostName", hostname, "FQDN", fqdn,
1933 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1934 macAddress, "StaticNameServers", staticNameServers,
1935 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1936 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1937 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001938 {
1939 return;
1940 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001941 if (dhcpv4)
1942 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001943 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1944 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1945 v4dhcpParms.useDNSServers, "UseNTPServers",
1946 v4dhcpParms.useNTPServers, "UseDomainName",
1947 v4dhcpParms.useUseDomainName))
1948 {
1949 return;
1950 }
1951 }
1952
1953 if (dhcpv6)
1954 {
1955 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1956 v6dhcpParms.dhcpv6OperatingMode,
1957 "UseDNSServers", v6dhcpParms.useDNSServers,
1958 "UseNTPServers", v6dhcpParms.useNTPServers,
1959 "UseDomainName",
1960 v6dhcpParms.useUseDomainName))
1961 {
1962 return;
1963 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001964 }
1965
Johnathan Mantey01784822019-06-18 12:44:21 -07001966 // Get single eth interface data, and call the below callback for
1967 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001968 getEthernetIfaceData(
1969 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001970 [this, asyncResp, iface_id, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301971 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001972 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001973 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001974 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001975 staticNameServers = std::move(staticNameServers),
1976 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1977 v4dhcpParms = std::move(v4dhcpParms),
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001978 v6dhcpParms = std::move(v6dhcpParms),
1979 interfaceEnabled = std::move(interfaceEnabled)](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001980 const bool& success, const EthernetInterfaceData& ethData,
1981 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1982 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001983 if (!success)
1984 {
1985 // ... otherwise return error
1986 // TODO(Pawel)consider distinguish between non existing
1987 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001988 messages::resourceNotFound(asyncResp->res,
1989 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001990 return;
1991 }
1992
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001993 if (dhcpv4 || dhcpv6)
1994 {
1995 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1996 std::move(v6dhcpParms), asyncResp);
1997 }
1998
Ed Tanous0627a2c2018-11-29 17:09:23 -08001999 if (hostname)
2000 {
2001 handleHostnamePatch(*hostname, asyncResp);
2002 }
2003
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302004 if (fqdn)
2005 {
2006 handleFqdnPatch(iface_id, *fqdn, asyncResp);
2007 }
2008
Ratan Guptad5776652019-03-03 08:47:22 +05302009 if (macAddress)
2010 {
2011 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2012 }
2013
Ravi Tejad1d50812019-06-23 16:20:27 -05002014 if (ipv4StaticAddresses)
2015 {
Ed Tanous537174c2018-12-10 15:09:31 -08002016 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002017 // above is returning a const value, not a non-const
2018 // value. This doesn't really work for us, as we need to
2019 // be able to efficiently move out the intermedia
2020 // nlohmann::json objects. This makes a copy of the
2021 // structure, and operates on that, but could be done
2022 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05002023 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07002024 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002025 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002026 }
2027
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002028 if (staticNameServers)
2029 {
2030 handleStaticNameServersPatch(iface_id, *staticNameServers,
2031 asyncResp);
2032 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002033
2034 if (ipv6DefaultGateway)
2035 {
2036 messages::propertyNotWritable(asyncResp->res,
2037 "IPv6DefaultGateway");
2038 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002039
2040 if (ipv6StaticAddresses)
2041 {
2042 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2043 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002044 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002045 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002046
2047 if (interfaceEnabled)
2048 {
2049 setEthernetInterfaceBoolProperty(
2050 iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2051 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002052 });
2053 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002054};
2055
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002056/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002057 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2058 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002059 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002060class VlanNetworkInterface : public Node
2061{
2062 public:
2063 /*
2064 * Default Constructor
2065 */
Ed Tanous52cc1122020-07-18 13:51:21 -07002066 VlanNetworkInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002067 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002068 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002069 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002070 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002071 entityPrivileges = {
2072 {boost::beast::http::verb::get, {{"Login"}}},
2073 {boost::beast::http::verb::head, {{"Login"}}},
2074 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2075 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2076 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2077 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002078 }
2079
Ed Tanous1abe55e2018-09-05 08:30:59 -07002080 private:
Ed Tanouscb13a392020-07-25 19:02:03 +00002081 void parseInterfaceData(nlohmann::json& json_response,
2082 const std::string& parent_iface_id,
2083 const std::string& iface_id,
2084 const EthernetInterfaceData& ethData)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002085 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002086 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07002087 json_response["Id"] = iface_id;
2088 json_response["@odata.id"] =
2089 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2090 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002091
Ed Tanous4a0cb852018-10-15 07:55:04 -07002092 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002093 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002094 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002095 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002096 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002097 }
2098
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002099 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002100 {
2101 if (!boost::starts_with(iface, parent + "_"))
2102 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002103 return false;
2104 }
2105 else
2106 {
2107 return true;
2108 }
2109 }
2110
2111 /**
2112 * Functions triggers appropriate requests on DBus
2113 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002114 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002115 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002116 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002117 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2118 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002119 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2120 // Check if there is required param, truly entering this shall be
2121 // impossible.
2122 if (params.size() != 2)
2123 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002124 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002125 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002126 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002127 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002128
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002129 const std::string& parent_iface_id = params[0];
2130 const std::string& iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08002131 res.jsonValue["@odata.type"] =
2132 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002133 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002134
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002135 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002136 {
2137 return;
2138 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002139
Johnathan Mantey01784822019-06-18 12:44:21 -07002140 // Get single eth interface data, and call the below callback for
2141 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002142 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002143 params[1],
2144 [this, asyncResp, parent_iface_id{std::string(params[0])},
2145 iface_id{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002146 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002147 const boost::container::flat_set<IPv4AddressData>&,
2148 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002149 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002150 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002151 parseInterfaceData(asyncResp->res.jsonValue,
Ed Tanouscb13a392020-07-25 19:02:03 +00002152 parent_iface_id, iface_id, ethData);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002153 }
2154 else
2155 {
2156 // ... otherwise return error
2157 // TODO(Pawel)consider distinguish between non existing
2158 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002159 messages::resourceNotFound(
2160 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002161 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002162 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002163 }
2164
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002165 void doPatch(crow::Response& res, const crow::Request& req,
2166 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002167 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002168 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002169 if (params.size() != 2)
2170 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002171 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 return;
2173 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002174
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002175 const std::string& parentIfaceId = params[0];
2176 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002177
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002178 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002179 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002180 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2181 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002182 return;
2183 }
2184
Ed Tanous0627a2c2018-11-29 17:09:23 -08002185 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002186 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002187
2188 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2189 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002190 {
2191 return;
2192 }
2193
Johnathan Mantey01784822019-06-18 12:44:21 -07002194 // Get single eth interface data, and call the below callback for
2195 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002196 getEthernetIfaceData(
2197 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002198 [asyncResp, parentIfaceId{std::string(params[0])},
Ed Tanouscb13a392020-07-25 19:02:03 +00002199 ifaceId{std::string(params[1])}, &vlanEnable,
2200 &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2201 const boost::container::flat_set<IPv4AddressData>&,
2202 const boost::container::flat_set<IPv6AddressData>&) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002203 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002204 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002205 auto callback =
2206 [asyncResp](const boost::system::error_code ec) {
2207 if (ec)
2208 {
2209 messages::internalError(asyncResp->res);
2210 }
2211 };
2212
2213 if (vlanEnable == true)
2214 {
2215 crow::connections::systemBus->async_method_call(
2216 std::move(callback), "xyz.openbmc_project.Network",
2217 "/xyz/openbmc_project/network/" + ifaceId,
2218 "org.freedesktop.DBus.Properties", "Set",
2219 "xyz.openbmc_project.Network.VLAN", "Id",
2220 std::variant<uint32_t>(vlanId));
2221 }
2222 else
2223 {
2224 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2225 "vlan interface";
2226 crow::connections::systemBus->async_method_call(
2227 std::move(callback), "xyz.openbmc_project.Network",
2228 std::string("/xyz/openbmc_project/network/") +
2229 ifaceId,
2230 "xyz.openbmc_project.Object.Delete", "Delete");
2231 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002232 }
2233 else
2234 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002235 // TODO(Pawel)consider distinguish between non existing
2236 // object, and other errors
2237 messages::resourceNotFound(
2238 asyncResp->res, "VLAN Network Interface", ifaceId);
2239 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002240 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002241 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002242 }
2243
Ed Tanouscb13a392020-07-25 19:02:03 +00002244 void doDelete(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002245 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002246 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002247 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002248 if (params.size() != 2)
2249 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002250 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002251 return;
2252 }
2253
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002254 const std::string& parentIfaceId = params[0];
2255 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002256
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002257 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002258 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002259 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2260 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261 return;
2262 }
2263
Johnathan Mantey01784822019-06-18 12:44:21 -07002264 // Get single eth interface data, and call the below callback for
2265 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002266 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002267 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002268 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002269 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002270 const bool& success, const EthernetInterfaceData& ethData,
Ed Tanouscb13a392020-07-25 19:02:03 +00002271 const boost::container::flat_set<IPv4AddressData>&,
2272 const boost::container::flat_set<IPv6AddressData>&) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002273 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002274 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002275 auto callback =
2276 [asyncResp](const boost::system::error_code ec) {
2277 if (ec)
2278 {
2279 messages::internalError(asyncResp->res);
2280 }
2281 };
2282 crow::connections::systemBus->async_method_call(
2283 std::move(callback), "xyz.openbmc_project.Network",
2284 std::string("/xyz/openbmc_project/network/") + ifaceId,
2285 "xyz.openbmc_project.Object.Delete", "Delete");
2286 }
2287 else
2288 {
2289 // ... otherwise return error
2290 // TODO(Pawel)consider distinguish between non existing
2291 // object, and other errors
2292 messages::resourceNotFound(
2293 asyncResp->res, "VLAN Network Interface", ifaceId);
2294 }
2295 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002296 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002297};
2298
2299/**
2300 * VlanNetworkInterfaceCollection derived class for delivering
2301 * VLANNetworkInterface Collection Schema
2302 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002303class VlanNetworkInterfaceCollection : public Node
2304{
2305 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002306 VlanNetworkInterfaceCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002307 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2308 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002309 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002310 entityPrivileges = {
2311 {boost::beast::http::verb::get, {{"Login"}}},
2312 {boost::beast::http::verb::head, {{"Login"}}},
2313 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2314 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2315 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2316 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002317 }
2318
Ed Tanous1abe55e2018-09-05 08:30:59 -07002319 private:
2320 /**
2321 * Functions triggers appropriate requests on DBus
2322 */
Ed Tanouscb13a392020-07-25 19:02:03 +00002323 void doGet(crow::Response& res, const crow::Request&,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002324 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002325 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002326 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002327 if (params.size() != 1)
2328 {
2329 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002330 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002331 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002332 }
2333
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002334 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002335
Ed Tanous4a0cb852018-10-15 07:55:04 -07002336 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002337 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002338 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002339 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002340 const bool& success,
2341 const boost::container::flat_set<std::string>& iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002342 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002343 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002344 messages::internalError(asyncResp->res);
2345 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002346 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002347
2348 if (iface_list.find(rootInterfaceName) == iface_list.end())
2349 {
2350 messages::resourceNotFound(asyncResp->res,
2351 "VLanNetworkInterfaceCollection",
2352 rootInterfaceName);
2353 return;
2354 }
2355
Ed Tanous0f74e642018-11-12 15:17:05 -08002356 asyncResp->res.jsonValue["@odata.type"] =
2357 "#VLanNetworkInterfaceCollection."
2358 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002359 asyncResp->res.jsonValue["Name"] =
2360 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002361
Jason M. Billsf12894f2018-10-09 12:45:45 -07002362 nlohmann::json iface_array = nlohmann::json::array();
2363
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002364 for (const std::string& iface_item : iface_list)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002365 {
2366 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2367 {
2368 iface_array.push_back(
2369 {{"@odata.id",
2370 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2371 rootInterfaceName + "/VLANs/" + iface_item}});
2372 }
2373 }
2374
Jason M. Billsf12894f2018-10-09 12:45:45 -07002375 asyncResp->res.jsonValue["Members@odata.count"] =
2376 iface_array.size();
2377 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2378 asyncResp->res.jsonValue["@odata.id"] =
2379 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2380 rootInterfaceName + "/VLANs";
2381 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002382 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002383
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002384 void doPost(crow::Response& res, const crow::Request& req,
2385 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002386 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002387 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002388 if (params.size() != 1)
2389 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002390 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002391 return;
2392 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002393 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002394 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002395 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2396 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002397 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002398 return;
2399 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002400 // Need both vlanId and vlanEnable to service this request
2401 if (!vlanId)
2402 {
2403 messages::propertyMissing(asyncResp->res, "VLANId");
2404 }
2405 if (!vlanEnable)
2406 {
2407 messages::propertyMissing(asyncResp->res, "VLANEnable");
2408 }
Ed Tanous271584a2019-07-09 16:24:22 -07002409 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002410 {
2411 return;
2412 }
2413
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002414 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002415 auto callback = [asyncResp](const boost::system::error_code ec) {
2416 if (ec)
2417 {
2418 // TODO(ed) make more consistent error messages based on
2419 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002420 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002421 return;
2422 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002423 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002424 };
2425 crow::connections::systemBus->async_method_call(
2426 std::move(callback), "xyz.openbmc_project.Network",
2427 "/xyz/openbmc_project/network",
2428 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002429 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002430 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002431};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002432} // namespace redfish