blob: 852b3b72c4b70a251ef0f4a4bb9b712033a39f36 [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 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500734inline void createIPv4(const std::string& ifaceId, unsigned int ipIdx,
735 uint8_t prefixLength, const std::string& gateway,
736 const std::string& address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700737 std::shared_ptr<AsyncResp> asyncResp)
738{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700739 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700740 [asyncResp](const boost::system::error_code ec) {
741 if (ec)
742 {
743 messages::internalError(asyncResp->res);
744 }
745 },
746 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700747 "/xyz/openbmc_project/network/" + ifaceId,
748 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700749 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700750 gateway);
751}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500752
753/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700754 * @brief Deletes the IPv4 entry for this interface and creates a replacement
755 * static IPv4 entry
756 *
757 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
758 * @param[in] id The unique hash entry identifying the DBus entry
759 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
760 * @param[in] gateway IPv4 address of this interfaces gateway
761 * @param[in] address IPv4 address to assign to this interface
762 * @param[io] asyncResp Response object that will be returned to client
763 *
764 * @return None
765 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500766inline void deleteAndCreateIPv4(const std::string& ifaceId,
767 const std::string& id, uint8_t prefixLength,
768 const std::string& gateway,
769 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700770 std::shared_ptr<AsyncResp> asyncResp)
771{
772 crow::connections::systemBus->async_method_call(
773 [asyncResp, ifaceId, address, prefixLength,
774 gateway](const boost::system::error_code ec) {
775 if (ec)
776 {
777 messages::internalError(asyncResp->res);
778 }
779 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000780 [asyncResp](const boost::system::error_code ec2) {
781 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700782 {
783 messages::internalError(asyncResp->res);
784 }
785 },
786 "xyz.openbmc_project.Network",
787 "/xyz/openbmc_project/network/" + ifaceId,
788 "xyz.openbmc_project.Network.IP.Create", "IP",
789 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
790 prefixLength, gateway);
791 },
792 "xyz.openbmc_project.Network",
793 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
794 "xyz.openbmc_project.Object.Delete", "Delete");
795}
796
797/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500798 * @brief Deletes given IPv6
799 *
800 * @param[in] ifaceId Id of interface whose IP should be deleted
801 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500802 * @param[io] asyncResp Response object that will be returned to client
803 *
804 * @return None
805 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500806inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500807 const std::shared_ptr<AsyncResp> asyncResp)
808{
809 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700810 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500811 if (ec)
812 {
813 messages::internalError(asyncResp->res);
814 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500815 },
816 "xyz.openbmc_project.Network",
817 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
818 "xyz.openbmc_project.Object.Delete", "Delete");
819}
820
821/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700822 * @brief Deletes the IPv6 entry for this interface and creates a replacement
823 * static IPv6 entry
824 *
825 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
826 * @param[in] id The unique hash entry identifying the DBus entry
827 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
828 * @param[in] address IPv6 address to assign to this interface
829 * @param[io] asyncResp Response object that will be returned to client
830 *
831 * @return None
832 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500833inline void deleteAndCreateIPv6(const std::string& ifaceId,
834 const std::string& id, uint8_t prefixLength,
835 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700836 std::shared_ptr<AsyncResp> asyncResp)
837{
838 crow::connections::systemBus->async_method_call(
839 [asyncResp, ifaceId, address,
840 prefixLength](const boost::system::error_code ec) {
841 if (ec)
842 {
843 messages::internalError(asyncResp->res);
844 }
845 crow::connections::systemBus->async_method_call(
Ed Tanous23a21a12020-07-25 04:45:05 +0000846 [asyncResp](const boost::system::error_code ec2) {
847 if (ec2)
Johnathan Mantey01784822019-06-18 12:44:21 -0700848 {
849 messages::internalError(asyncResp->res);
850 }
851 },
852 "xyz.openbmc_project.Network",
853 "/xyz/openbmc_project/network/" + ifaceId,
854 "xyz.openbmc_project.Network.IP.Create", "IP",
855 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
856 prefixLength, "");
857 },
858 "xyz.openbmc_project.Network",
859 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
860 "xyz.openbmc_project.Object.Delete", "Delete");
861}
862
863/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500864 * @brief Creates IPv6 with given data
865 *
866 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500867 * @param[in] prefixLength Prefix length that needs to be added
868 * @param[in] address IP address that needs to be added
869 * @param[io] asyncResp Response object that will be returned to client
870 *
871 * @return None
872 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500873inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
874 const std::string& address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500875 std::shared_ptr<AsyncResp> asyncResp)
876{
877 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
878 if (ec)
879 {
880 messages::internalError(asyncResp->res);
881 }
882 };
883 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500884 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500885 crow::connections::systemBus->async_method_call(
886 std::move(createIpHandler), "xyz.openbmc_project.Network",
887 "/xyz/openbmc_project/network/" + ifaceId,
888 "xyz.openbmc_project.Network.IP.Create", "IP",
889 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
890 "");
891}
892
Ed Tanous4a0cb852018-10-15 07:55:04 -0700893/**
894 * Function that retrieves all properties for given Ethernet Interface
895 * Object
896 * from EntityManager Network Manager
897 * @param ethiface_id a eth interface id to query on DBus
898 * @param callback a function that shall be called to convert Dbus output
899 * into JSON
900 */
901template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500902void getEthernetIfaceData(const std::string& ethiface_id,
903 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700904{
905 crow::connections::systemBus->async_method_call(
906 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
907 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500908 const GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700909 EthernetInterfaceData ethData{};
910 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500911 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700912
913 if (error_code)
914 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700915 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700916 return;
917 }
918
Ed Tanous4c9afe42019-05-03 16:59:57 -0700919 bool found =
920 extractEthernetInterfaceData(ethiface_id, resp, ethData);
921 if (!found)
922 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700923 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700924 return;
925 }
926
Johnathan Mantey01784822019-06-18 12:44:21 -0700927 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700928 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500929 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700930 {
Ravi Tejac6191412019-07-30 00:53:50 -0500931 if (((ipv4.linktype == LinkType::Global) &&
932 (ipv4.gateway == "0.0.0.0")) ||
933 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700934 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700935 ipv4.gateway = ethData.default_gateway;
936 }
937 }
938
Johnathan Mantey01784822019-06-18 12:44:21 -0700939 extractIPV6Data(ethiface_id, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500940 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700941 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700942 },
943 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
944 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700945}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946
947/**
948 * Function that retrieves all Ethernet Interfaces available through Network
949 * Manager
950 * @param callback a function that shall be called to convert Dbus output
951 * into JSON.
952 */
953template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500954void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700955{
956 crow::connections::systemBus->async_method_call(
957 [callback{std::move(callback)}](
958 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500959 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700960 // Callback requires vector<string> to retrieve all available
961 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700962 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700963 iface_list.reserve(resp.size());
964 if (error_code)
965 {
966 callback(false, iface_list);
967 return;
968 }
969
970 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500971 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700972 {
973 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500974 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700975 {
976 // If interface is
977 // xyz.openbmc_project.Network.EthernetInterface, this is
978 // what we're looking for.
979 if (interface.first ==
980 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500982 // Cut out everything until last "/", ...
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500983 const std::string& iface_id = objpath.first.str;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700984 std::size_t last_pos = iface_id.rfind("/");
985 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700987 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700988 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 }
990 }
991 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700992 }
993 // Finally make a callback with useful data
994 callback(true, iface_list);
995 },
996 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
997 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700998}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100999
1000/**
1001 * EthernetCollection derived class for delivering Ethernet Collection Schema
1002 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003class EthernetCollection : public Node
1004{
1005 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07001006 EthernetCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001007 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001009 entityPrivileges = {
1010 {boost::beast::http::verb::get, {{"Login"}}},
1011 {boost::beast::http::verb::head, {{"Login"}}},
1012 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1013 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1014 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1015 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1016 }
1017
1018 private:
1019 /**
1020 * Functions triggers appropriate requests on DBus
1021 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001022 void doGet(crow::Response& res, const crow::Request& req,
1023 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001024 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001025 res.jsonValue["@odata.type"] =
1026 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001027 res.jsonValue["@odata.id"] =
1028 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1029 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1030 res.jsonValue["Description"] =
1031 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001032 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001033 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001035 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001036 [asyncResp](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001037 const bool& success,
1038 const boost::container::flat_set<std::string>& iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001039 if (!success)
1040 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001041 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001042 return;
1043 }
1044
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001045 nlohmann::json& iface_array =
Ed Tanous4c9afe42019-05-03 16:59:57 -07001046 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001047 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001048 std::string tag = "_";
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001049 for (const std::string& iface_item : iface_list)
Jason M. Billsf12894f2018-10-09 12:45:45 -07001050 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001051 std::size_t found = iface_item.find(tag);
1052 if (found == std::string::npos)
1053 {
1054 iface_array.push_back(
1055 {{"@odata.id",
1056 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1057 iface_item}});
1058 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001059 }
1060
Ed Tanous4c9afe42019-05-03 16:59:57 -07001061 asyncResp->res.jsonValue["Members@odata.count"] =
1062 iface_array.size();
1063 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001064 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001065 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001066 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001067};
1068
1069/**
1070 * EthernetInterface derived class for delivering Ethernet Schema
1071 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072class EthernetInterface : public Node
1073{
1074 public:
1075 /*
1076 * Default Constructor
1077 */
Ed Tanous52cc1122020-07-18 13:51:21 -07001078 EthernetInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001079 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001080 std::string())
1081 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001082 entityPrivileges = {
1083 {boost::beast::http::verb::get, {{"Login"}}},
1084 {boost::beast::http::verb::head, {{"Login"}}},
1085 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1086 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1087 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1088 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001089 }
1090
Ed Tanous1abe55e2018-09-05 08:30:59 -07001091 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001092 void handleHostnamePatch(const std::string& hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001093 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301095 // SHOULD handle host names of up to 255 characters(RFC 1123)
1096 if (hostname.length() > 255)
1097 {
1098 messages::propertyValueFormatError(asyncResp->res, hostname,
1099 "HostName");
1100 return;
1101 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001102 crow::connections::systemBus->async_method_call(
1103 [asyncResp](const boost::system::error_code ec) {
1104 if (ec)
1105 {
1106 messages::internalError(asyncResp->res);
1107 }
1108 },
1109 "xyz.openbmc_project.Network",
1110 "/xyz/openbmc_project/network/config",
1111 "org.freedesktop.DBus.Properties", "Set",
1112 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001113 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001114 }
1115
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001116 void handleDomainnamePatch(const std::string& ifaceId,
1117 const std::string& domainname,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301118 const std::shared_ptr<AsyncResp> asyncResp)
1119 {
1120 std::vector<std::string> vectorDomainname = {domainname};
1121 crow::connections::systemBus->async_method_call(
1122 [asyncResp](const boost::system::error_code ec) {
1123 if (ec)
1124 {
1125 messages::internalError(asyncResp->res);
1126 }
1127 },
1128 "xyz.openbmc_project.Network",
1129 "/xyz/openbmc_project/network/" + ifaceId,
1130 "org.freedesktop.DBus.Properties", "Set",
1131 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1132 std::variant<std::vector<std::string>>(vectorDomainname));
1133 }
1134
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001135 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301136 const std::shared_ptr<AsyncResp> asyncResp)
1137 {
1138 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1139 if (fqdn.length() > 255)
1140 {
1141 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1142 return;
1143 }
1144
1145 size_t pos = fqdn.find('.');
1146 if (pos == std::string::npos)
1147 {
1148 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1149 return;
1150 }
1151
1152 std::string hostname;
1153 std::string domainname;
1154 domainname = (fqdn).substr(pos + 1);
1155 hostname = (fqdn).substr(0, pos);
1156
1157 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1158 {
1159 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1160 return;
1161 }
1162
1163 handleHostnamePatch(hostname, asyncResp);
1164 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1165 }
1166
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001167 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301168 {
1169 // A valid host name can never have the dotted-decimal form (RFC 1123)
1170 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1171 {
1172 return false;
1173 }
1174 // Each label(hostname/subdomains) within a valid FQDN
1175 // MUST handle host names of up to 63 characters (RFC 1123)
1176 // labels cannot start or end with hyphens (RFC 952)
1177 // labels can start with numbers (RFC 1123)
1178 const std::regex pattern(
1179 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1180
1181 return std::regex_match(hostname, pattern);
1182 }
1183
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001184 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301185 {
1186 // Can have multiple subdomains
1187 // Top Level Domain's min length is 2 character
1188 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1189 "{1,30}\\.)*[a-zA-Z]{2,}$");
1190
1191 return std::regex_match(domainname, pattern);
1192 }
1193
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001194 void handleMACAddressPatch(const std::string& ifaceId,
1195 const std::string& macAddress,
1196 const std::shared_ptr<AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301197 {
1198 crow::connections::systemBus->async_method_call(
1199 [asyncResp, macAddress](const boost::system::error_code ec) {
1200 if (ec)
1201 {
1202 messages::internalError(asyncResp->res);
1203 return;
1204 }
Ratan Guptad5776652019-03-03 08:47:22 +05301205 },
1206 "xyz.openbmc_project.Network",
1207 "/xyz/openbmc_project/network/" + ifaceId,
1208 "org.freedesktop.DBus.Properties", "Set",
1209 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1210 std::variant<std::string>(macAddress));
1211 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001212
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001213 void setDHCPEnabled(const std::string& ifaceId,
1214 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001215 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001216 const std::shared_ptr<AsyncResp> asyncResp)
1217 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001218 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001219 crow::connections::systemBus->async_method_call(
1220 [asyncResp](const boost::system::error_code ec) {
1221 if (ec)
1222 {
1223 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1224 messages::internalError(asyncResp->res);
1225 return;
1226 }
1227 },
1228 "xyz.openbmc_project.Network",
1229 "/xyz/openbmc_project/network/" + ifaceId,
1230 "org.freedesktop.DBus.Properties", "Set",
1231 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001232 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001233 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001234
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001235 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001236 const std::string& ifaceId, const std::string& propertyName,
1237 const bool& value, const std::shared_ptr<AsyncResp> asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001238 {
1239 crow::connections::systemBus->async_method_call(
1240 [asyncResp](const boost::system::error_code ec) {
1241 if (ec)
1242 {
1243 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1244 messages::internalError(asyncResp->res);
1245 return;
1246 }
1247 },
1248 "xyz.openbmc_project.Network",
1249 "/xyz/openbmc_project/network/" + ifaceId,
1250 "org.freedesktop.DBus.Properties", "Set",
1251 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1252 std::variant<bool>{value});
1253 }
1254
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001255 void setDHCPv4Config(const std::string& propertyName, const bool& value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001256 const std::shared_ptr<AsyncResp> asyncResp)
1257 {
1258 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1259 crow::connections::systemBus->async_method_call(
1260 [asyncResp](const boost::system::error_code ec) {
1261 if (ec)
1262 {
1263 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1264 messages::internalError(asyncResp->res);
1265 return;
1266 }
1267 },
1268 "xyz.openbmc_project.Network",
1269 "/xyz/openbmc_project/network/config/dhcp",
1270 "org.freedesktop.DBus.Properties", "Set",
1271 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1272 std::variant<bool>{value});
1273 }
Ratan Guptad5776652019-03-03 08:47:22 +05301274
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001275 void handleDHCPPatch(const std::string& ifaceId,
1276 const EthernetInterfaceData& ethData,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001277 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1278 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001279 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001280 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1281 bool ipv6Active =
1282 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001283
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001284 bool nextv4DHCPState =
1285 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1286
1287 bool nextv6DHCPState{};
1288 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001289 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001290 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1291 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1292 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1293 {
1294 messages::propertyValueFormatError(
1295 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1296 "OperatingMode");
1297 return;
1298 }
1299 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1300 }
1301 else
1302 {
1303 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001304 }
1305
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001306 bool nextDNS{};
1307 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001308 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001309 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1310 {
1311 messages::generalError(asyncResp->res);
1312 return;
1313 }
1314 nextDNS = *v4dhcpParms.useDNSServers;
1315 }
1316 else if (v4dhcpParms.useDNSServers)
1317 {
1318 nextDNS = *v4dhcpParms.useDNSServers;
1319 }
1320 else if (v6dhcpParms.useDNSServers)
1321 {
1322 nextDNS = *v6dhcpParms.useDNSServers;
1323 }
1324 else
1325 {
1326 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001327 }
1328
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001329 bool nextNTP{};
1330 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001331 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001332 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1333 {
1334 messages::generalError(asyncResp->res);
1335 return;
1336 }
1337 nextNTP = *v4dhcpParms.useNTPServers;
1338 }
1339 else if (v4dhcpParms.useNTPServers)
1340 {
1341 nextNTP = *v4dhcpParms.useNTPServers;
1342 }
1343 else if (v6dhcpParms.useNTPServers)
1344 {
1345 nextNTP = *v6dhcpParms.useNTPServers;
1346 }
1347 else
1348 {
1349 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001350 }
1351
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001352 bool nextUseDomain{};
1353 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001354 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001355 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1356 {
1357 messages::generalError(asyncResp->res);
1358 return;
1359 }
1360 nextUseDomain = *v4dhcpParms.useUseDomainName;
1361 }
1362 else if (v4dhcpParms.useUseDomainName)
1363 {
1364 nextUseDomain = *v4dhcpParms.useUseDomainName;
1365 }
1366 else if (v6dhcpParms.useUseDomainName)
1367 {
1368 nextUseDomain = *v6dhcpParms.useUseDomainName;
1369 }
1370 else
1371 {
1372 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001373 }
1374
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001375 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1376 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1377 asyncResp);
1378 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1379 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1380 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1381 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1382 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1383 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001384 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001385
1386 boost::container::flat_set<IPv4AddressData>::const_iterator
1387 GetNextStaticIPEntry(
1388 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1389 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1390 {
1391 for (; head != end; head++)
1392 {
1393 if (head->origin == "Static")
1394 {
1395 return head;
1396 }
1397 }
1398 return end;
1399 }
1400
1401 boost::container::flat_set<IPv6AddressData>::const_iterator
1402 GetNextStaticIPEntry(
1403 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1404 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1405 {
1406 for (; head != end; head++)
1407 {
1408 if (head->origin == "Static")
1409 {
1410 return head;
1411 }
1412 }
1413 return end;
1414 }
1415
Ravi Tejad1d50812019-06-23 16:20:27 -05001416 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001417 const std::string& ifaceId, nlohmann::json& input,
1418 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001419 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001420 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001421 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301422 {
1423 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001424 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301425 return;
1426 }
1427
Ed Tanous271584a2019-07-09 16:24:22 -07001428 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001429 // Find the first static IP address currently active on the NIC and
1430 // match it to the first JSON element in the IPv4StaticAddresses array.
1431 // Match each subsequent JSON element to the next static IP programmed
1432 // into the NIC.
1433 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1434 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1435
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001436 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001437 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001438 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001439 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001440
Johnathan Mantey01784822019-06-18 12:44:21 -07001441 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301442 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001443 std::optional<std::string> address;
1444 std::optional<std::string> subnetMask;
1445 std::optional<std::string> gateway;
1446
1447 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1448 address, "SubnetMask", subnetMask,
1449 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301450 {
1451 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001452 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301453 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301454 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301455
Johnathan Mantey01784822019-06-18 12:44:21 -07001456 // Find the address/subnet/gateway values. Any values that are
1457 // not explicitly provided are assumed to be unmodified from the
1458 // current state of the interface. Merge existing state into the
1459 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001460 const std::string* addr = nullptr;
1461 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001462 uint8_t prefixLength = 0;
1463 bool errorInEntry = false;
1464 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301465 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001466 if (ipv4VerifyIpAndGetBitcount(*address))
1467 {
1468 addr = &(*address);
1469 }
1470 else
1471 {
1472 messages::propertyValueFormatError(
1473 asyncResp->res, *address, pathString + "/Address");
1474 errorInEntry = true;
1475 }
1476 }
1477 else if (NICIPentry != ipv4Data.cend())
1478 {
1479 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301480 }
1481 else
1482 {
1483 messages::propertyMissing(asyncResp->res,
1484 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001485 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001486 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301487
1488 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001489 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001490 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1491 {
1492 messages::propertyValueFormatError(
1493 asyncResp->res, *subnetMask,
1494 pathString + "/SubnetMask");
1495 errorInEntry = true;
1496 }
1497 }
1498 else if (NICIPentry != ipv4Data.cend())
1499 {
1500 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1501 &prefixLength))
1502 {
1503 messages::propertyValueFormatError(
1504 asyncResp->res, NICIPentry->netmask,
1505 pathString + "/SubnetMask");
1506 errorInEntry = true;
1507 }
1508 }
1509 else
1510 {
1511 messages::propertyMissing(asyncResp->res,
1512 pathString + "/SubnetMask");
1513 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001514 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301515
Ratan Guptaf476acb2019-03-02 16:46:57 +05301516 if (gateway)
1517 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001518 if (ipv4VerifyIpAndGetBitcount(*gateway))
1519 {
1520 gw = &(*gateway);
1521 }
1522 else
1523 {
1524 messages::propertyValueFormatError(
1525 asyncResp->res, *gateway, pathString + "/Gateway");
1526 errorInEntry = true;
1527 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301528 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001529 else if (NICIPentry != ipv4Data.cend())
1530 {
1531 gw = &NICIPentry->gateway;
1532 }
1533 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001534 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001535 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001536 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001537 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001538 }
1539
Johnathan Mantey01784822019-06-18 12:44:21 -07001540 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001541 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001542 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001543 }
1544
Johnathan Mantey01784822019-06-18 12:44:21 -07001545 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001546 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001547 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1548 *gw, *addr, asyncResp);
1549 NICIPentry =
1550 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001551 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001552 else
1553 {
1554 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1555 *address, asyncResp);
1556 }
1557 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001558 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001559 else
1560 {
1561 if (NICIPentry == ipv4Data.cend())
1562 {
1563 // Requesting a DELETE/DO NOT MODIFY action for an item
1564 // that isn't present on the eth(n) interface. Input JSON is
1565 // in error, so bail out.
1566 if (thisJson.is_null())
1567 {
1568 messages::resourceCannotBeDeleted(asyncResp->res);
1569 return;
1570 }
1571 else
1572 {
1573 messages::propertyValueFormatError(
1574 asyncResp->res, thisJson.dump(), pathString);
1575 return;
1576 }
1577 }
1578
1579 if (thisJson.is_null())
1580 {
1581 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1582 }
1583 if (NICIPentry != ipv4Data.cend())
1584 {
1585 NICIPentry =
1586 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1587 }
1588 entryIdx++;
1589 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001590 }
1591 }
1592
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001593 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001594 const std::string& ifaceId,
1595 const std::vector<std::string>& updatedStaticNameServers,
1596 const std::shared_ptr<AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001597 {
1598 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001599 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001600 if (ec)
1601 {
1602 messages::internalError(asyncResp->res);
1603 return;
1604 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001605 },
1606 "xyz.openbmc_project.Network",
1607 "/xyz/openbmc_project/network/" + ifaceId,
1608 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001609 "xyz.openbmc_project.Network.EthernetInterface",
1610 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001611 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1612 }
1613
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001614 void handleIPv6StaticAddressesPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001615 const std::string& ifaceId, nlohmann::json& input,
1616 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001617 const std::shared_ptr<AsyncResp> asyncResp)
1618 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001619 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001620 {
1621 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1622 "IPv6StaticAddresses");
1623 return;
1624 }
Ed Tanous271584a2019-07-09 16:24:22 -07001625 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001626 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1627 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001628 for (nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001629 {
1630 std::string pathString =
1631 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1632
Johnathan Mantey01784822019-06-18 12:44:21 -07001633 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001634 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001635 std::optional<std::string> address;
1636 std::optional<uint8_t> prefixLength;
1637
1638 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1639 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001640 {
1641 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001642 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001643 return;
1644 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001645
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001646 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001647 uint8_t prefix;
1648
1649 // Find the address and prefixLength values. Any values that are
1650 // not explicitly provided are assumed to be unmodified from the
1651 // current state of the interface. Merge existing state into the
1652 // current request.
1653 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001654 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001655 addr = &(*address);
1656 }
1657 else if (NICIPentry != ipv6Data.end())
1658 {
1659 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001660 }
1661 else
1662 {
1663 messages::propertyMissing(asyncResp->res,
1664 pathString + "/Address");
1665 return;
1666 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001667
1668 if (prefixLength)
1669 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001670 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001671 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001672 else if (NICIPentry != ipv6Data.end())
1673 {
1674 prefix = NICIPentry->prefixLength;
1675 }
1676 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001677 {
1678 messages::propertyMissing(asyncResp->res,
1679 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001680 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001681 }
1682
Johnathan Mantey01784822019-06-18 12:44:21 -07001683 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001684 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001685 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1686 asyncResp);
1687 NICIPentry =
1688 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1689 }
1690 else
1691 {
1692 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1693 }
1694 entryIdx++;
1695 }
1696 else
1697 {
1698 if (NICIPentry == ipv6Data.end())
1699 {
1700 // Requesting a DELETE/DO NOT MODIFY action for an item
1701 // that isn't present on the eth(n) interface. Input JSON is
1702 // in error, so bail out.
1703 if (thisJson.is_null())
1704 {
1705 messages::resourceCannotBeDeleted(asyncResp->res);
1706 return;
1707 }
1708 else
1709 {
1710 messages::propertyValueFormatError(
1711 asyncResp->res, thisJson.dump(), pathString);
1712 return;
1713 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001714 }
1715
Johnathan Mantey01784822019-06-18 12:44:21 -07001716 if (thisJson.is_null())
1717 {
1718 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1719 }
1720 if (NICIPentry != ipv6Data.cend())
1721 {
1722 NICIPentry =
1723 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1724 }
1725 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001726 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001727 }
1728 }
1729
Ed Tanous0f74e642018-11-12 15:17:05 -08001730 void parseInterfaceData(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001731 std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id,
1732 const EthernetInterfaceData& ethData,
1733 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1734 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001735 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001736 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001737 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1738
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001739 nlohmann::json& json_response = asyncResp->res.jsonValue;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001740 json_response["Id"] = iface_id;
1741 json_response["@odata.id"] =
1742 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001743 json_response["InterfaceEnabled"] = ethData.nicEnabled;
1744
1745 auto health = std::make_shared<HealthPopulate>(asyncResp);
1746
1747 crow::connections::systemBus->async_method_call(
1748 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001749 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001750 if (ec)
1751 {
1752 return;
1753 }
1754
1755 health->inventory = std::move(resp);
1756 },
1757 "xyz.openbmc_project.ObjectMapper",
1758 "/xyz/openbmc_project/object_mapper",
1759 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1760 int32_t(0), inventoryForEthernet);
1761
1762 health->populate();
1763
1764 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001765 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001766 json_response["LinkStatus"] = "LinkUp";
1767 json_response["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001768 }
1769 else
1770 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001771 json_response["LinkStatus"] = "NoLink";
1772 json_response["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001773 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001774
1775 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001776 json_response["SpeedMbps"] = ethData.speed;
1777 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001778 json_response["DHCPv4"]["DHCPEnabled"] =
1779 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1780 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1781 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1782 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1783
1784 json_response["DHCPv6"]["OperatingMode"] =
1785 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1786 : "Disabled";
1787 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1788 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1789 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301790
Ed Tanous4a0cb852018-10-15 07:55:04 -07001791 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001792 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001793 json_response["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301794
1795 // When domain name is empty then it means, that it is a network
1796 // without domain names, and the host name itself must be treated as
1797 // FQDN
1798 std::string FQDN = std::move(ethData.hostname);
Jennifer Leed24bfc72019-03-05 13:03:37 -08001799 if (!ethData.domainnames.empty())
1800 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301801 FQDN += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001802 }
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301803 json_response["FQDN"] = FQDN;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001804 }
1805
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001806 json_response["VLANs"] = {
1807 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1808 iface_id + "/VLANs"}};
1809
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001810 json_response["NameServers"] = ethData.nameServers;
1811 json_response["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001812
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001813 nlohmann::json& ipv4_array = json_response["IPv4Addresses"];
1814 nlohmann::json& ipv4_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001815 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001816 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001817 ipv4_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001818 for (auto& ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001819 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001820
1821 std::string gatewayStr = ipv4_config.gateway;
1822 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001823 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001824 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001825 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001826
1827 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1828 {"SubnetMask", ipv4_config.netmask},
1829 {"Address", ipv4_config.address},
1830 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001831 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001832 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001833 ipv4_static_array.push_back(
1834 {{"AddressOrigin", ipv4_config.origin},
1835 {"SubnetMask", ipv4_config.netmask},
1836 {"Address", ipv4_config.address},
1837 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001838 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001839 }
1840
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001841 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001842
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001843 nlohmann::json& ipv6_array = json_response["IPv6Addresses"];
1844 nlohmann::json& ipv6_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001845 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001846 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001847 ipv6_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001848 nlohmann::json& ipv6AddrPolicyTable =
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001849 json_response["IPv6AddressPolicyTable"];
1850 ipv6AddrPolicyTable = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001851 for (auto& ipv6_config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001852 {
1853 ipv6_array.push_back({{"Address", ipv6_config.address},
1854 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001855 {"AddressOrigin", ipv6_config.origin},
1856 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001857 if (ipv6_config.origin == "Static")
1858 {
1859 ipv6_static_array.push_back(
1860 {{"Address", ipv6_config.address},
1861 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001862 {"AddressOrigin", ipv6_config.origin},
1863 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001864 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001865 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001866 }
1867
1868 /**
1869 * Functions triggers appropriate requests on DBus
1870 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001871 void doGet(crow::Response& res, const crow::Request& req,
1872 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001873 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001874 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001875 if (params.size() != 1)
1876 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001877 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001878 return;
1879 }
1880
Ed Tanous4a0cb852018-10-15 07:55:04 -07001881 getEthernetIfaceData(
1882 params[0],
1883 [this, asyncResp, iface_id{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001884 const bool& success, const EthernetInterfaceData& ethData,
1885 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1886 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001887 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001888 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001889 // TODO(Pawel)consider distinguish between non existing
1890 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001891 messages::resourceNotFound(asyncResp->res,
1892 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001893 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001894 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001895
Ed Tanous0f74e642018-11-12 15:17:05 -08001896 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001897 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001898 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1899 asyncResp->res.jsonValue["Description"] =
1900 "Management Network Interface";
1901
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001902 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1903 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001904 });
1905 }
1906
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001907 void doPatch(crow::Response& res, const crow::Request& req,
1908 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001909 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001910 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001911 if (params.size() != 1)
1912 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001913 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 return;
1915 }
1916
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001917 const std::string& iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001918
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001919 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301920 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301921 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001922 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001923 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001924 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001925 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001926 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001927 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001928 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001929 DHCPParameters v4dhcpParms;
1930 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001931
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001932 if (!json_util::readJson(
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301933 req, res, "HostName", hostname, "FQDN", fqdn,
1934 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1935 macAddress, "StaticNameServers", staticNameServers,
1936 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1937 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1938 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001939 {
1940 return;
1941 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001942 if (dhcpv4)
1943 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001944 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1945 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1946 v4dhcpParms.useDNSServers, "UseNTPServers",
1947 v4dhcpParms.useNTPServers, "UseDomainName",
1948 v4dhcpParms.useUseDomainName))
1949 {
1950 return;
1951 }
1952 }
1953
1954 if (dhcpv6)
1955 {
1956 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1957 v6dhcpParms.dhcpv6OperatingMode,
1958 "UseDNSServers", v6dhcpParms.useDNSServers,
1959 "UseNTPServers", v6dhcpParms.useNTPServers,
1960 "UseDomainName",
1961 v6dhcpParms.useUseDomainName))
1962 {
1963 return;
1964 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001965 }
1966
Johnathan Mantey01784822019-06-18 12:44:21 -07001967 // Get single eth interface data, and call the below callback for
1968 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001969 getEthernetIfaceData(
1970 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001971 [this, asyncResp, iface_id, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301972 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001973 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001974 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001975 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001976 staticNameServers = std::move(staticNameServers),
1977 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1978 v4dhcpParms = std::move(v4dhcpParms),
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001979 v6dhcpParms = std::move(v6dhcpParms),
1980 interfaceEnabled = std::move(interfaceEnabled)](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001981 const bool& success, const EthernetInterfaceData& ethData,
1982 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1983 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001984 if (!success)
1985 {
1986 // ... otherwise return error
1987 // TODO(Pawel)consider distinguish between non existing
1988 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001989 messages::resourceNotFound(asyncResp->res,
1990 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001991 return;
1992 }
1993
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001994 if (dhcpv4 || dhcpv6)
1995 {
1996 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1997 std::move(v6dhcpParms), asyncResp);
1998 }
1999
Ed Tanous0627a2c2018-11-29 17:09:23 -08002000 if (hostname)
2001 {
2002 handleHostnamePatch(*hostname, asyncResp);
2003 }
2004
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302005 if (fqdn)
2006 {
2007 handleFqdnPatch(iface_id, *fqdn, asyncResp);
2008 }
2009
Ratan Guptad5776652019-03-03 08:47:22 +05302010 if (macAddress)
2011 {
2012 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2013 }
2014
Ravi Tejad1d50812019-06-23 16:20:27 -05002015 if (ipv4StaticAddresses)
2016 {
Ed Tanous537174c2018-12-10 15:09:31 -08002017 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002018 // above is returning a const value, not a non-const
2019 // value. This doesn't really work for us, as we need to
2020 // be able to efficiently move out the intermedia
2021 // nlohmann::json objects. This makes a copy of the
2022 // structure, and operates on that, but could be done
2023 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05002024 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07002025 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002026 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002027 }
2028
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002029 if (staticNameServers)
2030 {
2031 handleStaticNameServersPatch(iface_id, *staticNameServers,
2032 asyncResp);
2033 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002034
2035 if (ipv6DefaultGateway)
2036 {
2037 messages::propertyNotWritable(asyncResp->res,
2038 "IPv6DefaultGateway");
2039 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002040
2041 if (ipv6StaticAddresses)
2042 {
2043 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2044 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002045 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002046 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002047
2048 if (interfaceEnabled)
2049 {
2050 setEthernetInterfaceBoolProperty(
2051 iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2052 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002053 });
2054 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002055};
2056
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002057/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002058 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2059 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002060 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002061class VlanNetworkInterface : public Node
2062{
2063 public:
2064 /*
2065 * Default Constructor
2066 */
Ed Tanous52cc1122020-07-18 13:51:21 -07002067 VlanNetworkInterface(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002068 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002069 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002070 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002071 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002072 entityPrivileges = {
2073 {boost::beast::http::verb::get, {{"Login"}}},
2074 {boost::beast::http::verb::head, {{"Login"}}},
2075 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2076 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2077 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2078 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002079 }
2080
Ed Tanous1abe55e2018-09-05 08:30:59 -07002081 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08002082 void parseInterfaceData(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002083 nlohmann::json& json_response, const std::string& parent_iface_id,
2084 const std::string& iface_id, const EthernetInterfaceData& ethData,
2085 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2086 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002087 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002088 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07002089 json_response["Id"] = iface_id;
2090 json_response["@odata.id"] =
2091 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2092 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002093
Ed Tanous4a0cb852018-10-15 07:55:04 -07002094 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002095 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002096 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002097 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002098 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002099 }
2100
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002101 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002102 {
2103 if (!boost::starts_with(iface, parent + "_"))
2104 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002105 return false;
2106 }
2107 else
2108 {
2109 return true;
2110 }
2111 }
2112
2113 /**
2114 * Functions triggers appropriate requests on DBus
2115 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002116 void doGet(crow::Response& res, const crow::Request& req,
2117 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002118 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002119 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2120 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002121 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2122 // Check if there is required param, truly entering this shall be
2123 // impossible.
2124 if (params.size() != 2)
2125 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002126 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002127 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002128 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002129 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002130
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002131 const std::string& parent_iface_id = params[0];
2132 const std::string& iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08002133 res.jsonValue["@odata.type"] =
2134 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002135 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002136
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002137 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002138 {
2139 return;
2140 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002141
Johnathan Mantey01784822019-06-18 12:44:21 -07002142 // Get single eth interface data, and call the below callback for
2143 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002144 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002145 params[1],
2146 [this, asyncResp, parent_iface_id{std::string(params[0])},
2147 iface_id{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002148 const bool& success, const EthernetInterfaceData& ethData,
2149 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2150 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002151 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002152 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002153 parseInterfaceData(asyncResp->res.jsonValue,
2154 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002155 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002156 }
2157 else
2158 {
2159 // ... otherwise return error
2160 // TODO(Pawel)consider distinguish between non existing
2161 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002162 messages::resourceNotFound(
2163 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002164 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002165 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002166 }
2167
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002168 void doPatch(crow::Response& res, const crow::Request& req,
2169 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002170 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002171 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 if (params.size() != 2)
2173 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002174 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002175 return;
2176 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002177
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002178 const std::string& parentIfaceId = params[0];
2179 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002180
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002181 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002182 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002183 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2184 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002185 return;
2186 }
2187
Ed Tanous0627a2c2018-11-29 17:09:23 -08002188 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002189 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002190
2191 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2192 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002193 {
2194 return;
2195 }
2196
Johnathan Mantey01784822019-06-18 12:44:21 -07002197 // Get single eth interface data, and call the below callback for
2198 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002199 getEthernetIfaceData(
2200 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002201 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002202 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002203 const bool& success, const EthernetInterfaceData& ethData,
2204 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2205 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002206 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002207 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002208 auto callback =
2209 [asyncResp](const boost::system::error_code ec) {
2210 if (ec)
2211 {
2212 messages::internalError(asyncResp->res);
2213 }
2214 };
2215
2216 if (vlanEnable == true)
2217 {
2218 crow::connections::systemBus->async_method_call(
2219 std::move(callback), "xyz.openbmc_project.Network",
2220 "/xyz/openbmc_project/network/" + ifaceId,
2221 "org.freedesktop.DBus.Properties", "Set",
2222 "xyz.openbmc_project.Network.VLAN", "Id",
2223 std::variant<uint32_t>(vlanId));
2224 }
2225 else
2226 {
2227 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2228 "vlan interface";
2229 crow::connections::systemBus->async_method_call(
2230 std::move(callback), "xyz.openbmc_project.Network",
2231 std::string("/xyz/openbmc_project/network/") +
2232 ifaceId,
2233 "xyz.openbmc_project.Object.Delete", "Delete");
2234 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002235 }
2236 else
2237 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002238 // TODO(Pawel)consider distinguish between non existing
2239 // object, and other errors
2240 messages::resourceNotFound(
2241 asyncResp->res, "VLAN Network Interface", ifaceId);
2242 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002243 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002244 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002245 }
2246
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002247 void doDelete(crow::Response& res, const crow::Request& req,
2248 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002249 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002250 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002251 if (params.size() != 2)
2252 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002253 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002254 return;
2255 }
2256
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002257 const std::string& parentIfaceId = params[0];
2258 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002259
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002260 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002262 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2263 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002264 return;
2265 }
2266
Johnathan Mantey01784822019-06-18 12:44:21 -07002267 // Get single eth interface data, and call the below callback for
2268 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002269 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002270 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002271 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002272 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002273 const bool& success, const EthernetInterfaceData& ethData,
2274 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2275 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002276 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002277 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002278 auto callback =
2279 [asyncResp](const boost::system::error_code ec) {
2280 if (ec)
2281 {
2282 messages::internalError(asyncResp->res);
2283 }
2284 };
2285 crow::connections::systemBus->async_method_call(
2286 std::move(callback), "xyz.openbmc_project.Network",
2287 std::string("/xyz/openbmc_project/network/") + ifaceId,
2288 "xyz.openbmc_project.Object.Delete", "Delete");
2289 }
2290 else
2291 {
2292 // ... otherwise return error
2293 // TODO(Pawel)consider distinguish between non existing
2294 // object, and other errors
2295 messages::resourceNotFound(
2296 asyncResp->res, "VLAN Network Interface", ifaceId);
2297 }
2298 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002299 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002300};
2301
2302/**
2303 * VlanNetworkInterfaceCollection derived class for delivering
2304 * VLANNetworkInterface Collection Schema
2305 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002306class VlanNetworkInterfaceCollection : public Node
2307{
2308 public:
Ed Tanous52cc1122020-07-18 13:51:21 -07002309 VlanNetworkInterfaceCollection(App& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002310 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2311 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002312 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002313 entityPrivileges = {
2314 {boost::beast::http::verb::get, {{"Login"}}},
2315 {boost::beast::http::verb::head, {{"Login"}}},
2316 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2317 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2318 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2319 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002320 }
2321
Ed Tanous1abe55e2018-09-05 08:30:59 -07002322 private:
2323 /**
2324 * Functions triggers appropriate requests on DBus
2325 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002326 void doGet(crow::Response& res, const crow::Request& req,
2327 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002328 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002329 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002330 if (params.size() != 1)
2331 {
2332 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002333 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002334 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002335 }
2336
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002337 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002338
Ed Tanous4a0cb852018-10-15 07:55:04 -07002339 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002340 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002341 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002342 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002343 const bool& success,
2344 const boost::container::flat_set<std::string>& iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002345 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002346 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002347 messages::internalError(asyncResp->res);
2348 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002349 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002350
2351 if (iface_list.find(rootInterfaceName) == iface_list.end())
2352 {
2353 messages::resourceNotFound(asyncResp->res,
2354 "VLanNetworkInterfaceCollection",
2355 rootInterfaceName);
2356 return;
2357 }
2358
Ed Tanous0f74e642018-11-12 15:17:05 -08002359 asyncResp->res.jsonValue["@odata.type"] =
2360 "#VLanNetworkInterfaceCollection."
2361 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002362 asyncResp->res.jsonValue["Name"] =
2363 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002364
Jason M. Billsf12894f2018-10-09 12:45:45 -07002365 nlohmann::json iface_array = nlohmann::json::array();
2366
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002367 for (const std::string& iface_item : iface_list)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002368 {
2369 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2370 {
2371 iface_array.push_back(
2372 {{"@odata.id",
2373 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2374 rootInterfaceName + "/VLANs/" + iface_item}});
2375 }
2376 }
2377
Jason M. Billsf12894f2018-10-09 12:45:45 -07002378 asyncResp->res.jsonValue["Members@odata.count"] =
2379 iface_array.size();
2380 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2381 asyncResp->res.jsonValue["@odata.id"] =
2382 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2383 rootInterfaceName + "/VLANs";
2384 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002385 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002386
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002387 void doPost(crow::Response& res, const crow::Request& req,
2388 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002389 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002390 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002391 if (params.size() != 1)
2392 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002393 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002394 return;
2395 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002396 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002397 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002398 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2399 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002400 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002401 return;
2402 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002403 // Need both vlanId and vlanEnable to service this request
2404 if (!vlanId)
2405 {
2406 messages::propertyMissing(asyncResp->res, "VLANId");
2407 }
2408 if (!vlanEnable)
2409 {
2410 messages::propertyMissing(asyncResp->res, "VLANEnable");
2411 }
Ed Tanous271584a2019-07-09 16:24:22 -07002412 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002413 {
2414 return;
2415 }
2416
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002417 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002418 auto callback = [asyncResp](const boost::system::error_code ec) {
2419 if (ec)
2420 {
2421 // TODO(ed) make more consistent error messages based on
2422 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002423 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002424 return;
2425 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002426 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002427 };
2428 crow::connections::systemBus->async_method_call(
2429 std::move(callback), "xyz.openbmc_project.Network",
2430 "/xyz/openbmc_project/network",
2431 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002432 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002433 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002434};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002435} // namespace redfish