blob: b1a9f695bbcf0b775d0ee026cf11b3ecee0939c8 [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;
68
Gunnar Mills1214b7e2020-06-04 10:11:30 -050069 bool operator<(const IPv4AddressData& obj) const
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070071 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010073};
74
75/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050076 * Structure for keeping IPv6 data required by Redfish
77 */
78struct IPv6AddressData
79{
80 std::string id;
81 std::string address;
82 std::string origin;
83 uint8_t prefixLength;
84
Gunnar Mills1214b7e2020-06-04 10:11:30 -050085 bool operator<(const IPv6AddressData& obj) const
Ravi Tejae48c0fc2019-04-16 08:37:20 -050086 {
87 return id < obj.id;
88 }
89};
90/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010091 * Structure for keeping basic single Ethernet Interface information
92 * available from DBus
93 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070094struct EthernetInterfaceData
95{
Ed Tanous4a0cb852018-10-15 07:55:04 -070096 uint32_t speed;
97 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070098 bool DNSEnabled;
99 bool NTPEnabled;
100 bool HostNameEnabled;
101 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800102 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700103 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700104 std::string DHCPEnabled;
105 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700106 std::string hostname;
107 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500108 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700109 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500110 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500111 std::vector<std::string> nameServers;
112 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800113 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100114};
115
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700116struct DHCPParameters
117{
118 std::optional<bool> dhcpv4Enabled;
119 std::optional<bool> useDNSServers;
120 std::optional<bool> useNTPServers;
121 std::optional<bool> useUseDomainName;
122 std::optional<std::string> dhcpv6OperatingMode;
123};
124
Ed Tanous4a0cb852018-10-15 07:55:04 -0700125// Helper function that changes bits netmask notation (i.e. /24)
126// into full dot notation
127inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700129 uint32_t value = 0xffffffff << (32 - bits);
130 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
131 std::to_string((value >> 16) & 0xff) + "." +
132 std::to_string((value >> 8) & 0xff) + "." +
133 std::to_string(value & 0xff);
134 return netmask;
135}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100136
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500137inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700138 bool isIPv4)
139{
140 if (isIPv4)
141 {
142 return (
143 (inputDHCP ==
144 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
145 (inputDHCP ==
146 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
147 }
148 return ((inputDHCP ==
149 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
150 (inputDHCP ==
151 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
152}
153
154inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
155{
156 if (isIPv4 && isIPv6)
157 {
158 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
159 }
160 else if (isIPv4)
161 {
162 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
163 }
164 else if (isIPv6)
165 {
166 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
167 }
168 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
169}
170
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171inline std::string
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500172 translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 bool isIPv4)
174{
175 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700177 return "Static";
178 }
179 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
180 {
181 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700183 return "IPv4LinkLocal";
184 }
185 else
186 {
187 return "LinkLocal";
188 }
189 }
190 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
191 {
192 if (isIPv4)
193 {
194 return "DHCP";
195 }
196 else
197 {
198 return "DHCPv6";
199 }
200 }
201 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
202 {
203 return "SLAAC";
204 }
205 return "";
206}
207
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500208inline bool extractEthernetInterfaceData(const std::string& ethiface_id,
209 const GetManagedObjects& dbus_data,
210 EthernetInterfaceData& ethData)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700212 bool idFound = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213 for (const auto& objpath : dbus_data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500215 for (const auto& ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 {
Ed Tanous029573d2019-02-01 10:57:49 -0800217 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700219 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 for (const auto& propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500226 const std::string* mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800227 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 if (mac != nullptr)
229 {
230 ethData.mac_address = *mac;
231 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700233 }
234 }
235 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
236 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500237 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 {
239 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500241 const uint32_t* id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800242 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 if (id != nullptr)
244 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500245 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700247 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700248 }
249 }
250 else if (ifacePair.first ==
251 "xyz.openbmc_project.Network.EthernetInterface")
252 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500253 for (const auto& propertyPair : ifacePair.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254 {
255 if (propertyPair.first == "AutoNeg")
256 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500257 const bool* auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800258 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700259 if (auto_neg != nullptr)
260 {
261 ethData.auto_neg = *auto_neg;
262 }
263 }
264 else if (propertyPair.first == "Speed")
265 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500266 const uint32_t* speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800267 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700268 if (speed != nullptr)
269 {
270 ethData.speed = *speed;
271 }
272 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800273 else if (propertyPair.first == "LinkUp")
274 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500275 const bool* linkUp =
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800276 std::get_if<bool>(&propertyPair.second);
277 if (linkUp != nullptr)
278 {
279 ethData.linkUp = *linkUp;
280 }
281 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700282 else if (propertyPair.first == "NICEnabled")
283 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500284 const bool* nicEnabled =
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700285 std::get_if<bool>(&propertyPair.second);
286 if (nicEnabled != nullptr)
287 {
288 ethData.nicEnabled = *nicEnabled;
289 }
290 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500291 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700292 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500293 const std::vector<std::string>* nameservers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500294 std::get_if<std::vector<std::string>>(
Ed Tanous029573d2019-02-01 10:57:49 -0800295 &propertyPair.second);
296 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700297 {
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500298 ethData.nameServers = std::move(*nameservers);
299 }
300 }
301 else if (propertyPair.first == "StaticNameServers")
302 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500303 const std::vector<std::string>* staticNameServers =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500304 std::get_if<std::vector<std::string>>(
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500305 &propertyPair.second);
306 if (staticNameServers != nullptr)
307 {
308 ethData.staticNameServers =
309 std::move(*staticNameServers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700310 }
311 }
manojkiraneda2a133282019-02-19 13:09:43 +0530312 else if (propertyPair.first == "DHCPEnabled")
313 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500314 const std::string* DHCPEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700315 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530316 if (DHCPEnabled != nullptr)
317 {
318 ethData.DHCPEnabled = *DHCPEnabled;
319 }
320 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800321 else if (propertyPair.first == "DomainName")
322 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500323 const std::vector<std::string>* domainNames =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500324 std::get_if<std::vector<std::string>>(
Jennifer Leed24bfc72019-03-05 13:03:37 -0800325 &propertyPair.second);
326 if (domainNames != nullptr)
327 {
328 ethData.domainnames = std::move(*domainNames);
329 }
330 }
Ed Tanous029573d2019-02-01 10:57:49 -0800331 }
332 }
333 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700334
335 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
336 {
337 if (ifacePair.first ==
338 "xyz.openbmc_project.Network.DHCPConfiguration")
339 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 for (const auto& propertyPair : ifacePair.second)
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700341 {
342 if (propertyPair.first == "DNSEnabled")
343 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500344 const bool* DNSEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700345 std::get_if<bool>(&propertyPair.second);
346 if (DNSEnabled != nullptr)
347 {
348 ethData.DNSEnabled = *DNSEnabled;
349 }
350 }
351 else if (propertyPair.first == "NTPEnabled")
352 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500353 const bool* NTPEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700354 std::get_if<bool>(&propertyPair.second);
355 if (NTPEnabled != nullptr)
356 {
357 ethData.NTPEnabled = *NTPEnabled;
358 }
359 }
360 else if (propertyPair.first == "HostNameEnabled")
361 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500362 const bool* HostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700363 std::get_if<bool>(&propertyPair.second);
364 if (HostNameEnabled != nullptr)
365 {
366 ethData.HostNameEnabled = *HostNameEnabled;
367 }
368 }
369 else if (propertyPair.first == "SendHostNameEnabled")
370 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 const bool* SendHostNameEnabled =
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700372 std::get_if<bool>(&propertyPair.second);
373 if (SendHostNameEnabled != nullptr)
374 {
375 ethData.SendHostNameEnabled =
376 *SendHostNameEnabled;
377 }
378 }
379 }
380 }
381 }
Ed Tanous029573d2019-02-01 10:57:49 -0800382 // System configuration shows up in the global namespace, so no need
383 // to check eth number
384 if (ifacePair.first ==
385 "xyz.openbmc_project.Network.SystemConfiguration")
386 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500387 for (const auto& propertyPair : ifacePair.second)
Ed Tanous029573d2019-02-01 10:57:49 -0800388 {
389 if (propertyPair.first == "HostName")
390 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500391 const std::string* hostname =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500392 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800393 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700394 {
Ed Tanous029573d2019-02-01 10:57:49 -0800395 ethData.hostname = *hostname;
396 }
397 }
398 else if (propertyPair.first == "DefaultGateway")
399 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500400 const std::string* defaultGateway =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500401 std::get_if<std::string>(&propertyPair.second);
Ed Tanous029573d2019-02-01 10:57:49 -0800402 if (defaultGateway != nullptr)
403 {
404 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700405 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500407 else if (propertyPair.first == "DefaultGateway6")
408 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500409 const std::string* defaultGateway6 =
Patrick Williams8d78b7a2020-05-13 11:24:20 -0500410 std::get_if<std::string>(&propertyPair.second);
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500411 if (defaultGateway6 != nullptr)
412 {
413 ethData.ipv6_default_gateway = *defaultGateway6;
414 }
415 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700416 }
417 }
418 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700420 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700421}
422
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500423// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700424inline void
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500425 extractIPV6Data(const std::string& ethiface_id,
426 const GetManagedObjects& dbus_data,
427 boost::container::flat_set<IPv6AddressData>& ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500428{
429 const std::string ipv6PathStart =
430 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
431
432 // Since there might be several IPv6 configurations aligned with
433 // single ethernet interface, loop over all of them
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500434 for (const auto& objpath : dbus_data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500435 {
436 // Check if proper pattern for object path appears
437 if (boost::starts_with(objpath.first.str, ipv6PathStart))
438 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500439 for (auto& interface : objpath.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500440 {
441 if (interface.first == "xyz.openbmc_project.Network.IP")
442 {
443 // Instance IPv6AddressData structure, and set as
444 // appropriate
445 std::pair<
446 boost::container::flat_set<IPv6AddressData>::iterator,
447 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700448 it = ipv6_config.insert(IPv6AddressData{});
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500449 IPv6AddressData& ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700450 ipv6_address.id =
451 objpath.first.str.substr(ipv6PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 for (auto& property : interface.second)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500453 {
454 if (property.first == "Address")
455 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500456 const std::string* address =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500457 std::get_if<std::string>(&property.second);
458 if (address != nullptr)
459 {
460 ipv6_address.address = *address;
461 }
462 }
463 else if (property.first == "Origin")
464 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500465 const std::string* origin =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500466 std::get_if<std::string>(&property.second);
467 if (origin != nullptr)
468 {
469 ipv6_address.origin =
470 translateAddressOriginDbusToRedfish(*origin,
471 false);
472 }
473 }
474 else if (property.first == "PrefixLength")
475 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500476 const uint8_t* prefix =
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500477 std::get_if<uint8_t>(&property.second);
478 if (prefix != nullptr)
479 {
480 ipv6_address.prefixLength = *prefix;
481 }
482 }
483 else
484 {
485 BMCWEB_LOG_ERROR
486 << "Got extra property: " << property.first
487 << " on the " << objpath.first.str << " object";
488 }
489 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500490 }
491 }
492 }
493 }
494}
495
Ed Tanous4a0cb852018-10-15 07:55:04 -0700496// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700497inline void
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500498 extractIPData(const std::string& ethiface_id,
499 const GetManagedObjects& dbus_data,
500 boost::container::flat_set<IPv4AddressData>& ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700501{
502 const std::string ipv4PathStart =
503 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
504
505 // Since there might be several IPv4 configurations aligned with
506 // single ethernet interface, loop over all of them
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500507 for (const auto& objpath : dbus_data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700508 {
509 // Check if proper pattern for object path appears
510 if (boost::starts_with(objpath.first.str, ipv4PathStart))
511 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500512 for (auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700513 {
514 if (interface.first == "xyz.openbmc_project.Network.IP")
515 {
516 // Instance IPv4AddressData structure, and set as
517 // appropriate
518 std::pair<
519 boost::container::flat_set<IPv4AddressData>::iterator,
520 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700521 it = ipv4_config.insert(IPv4AddressData{});
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500522 IPv4AddressData& ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700523 ipv4_address.id =
524 objpath.first.str.substr(ipv4PathStart.size());
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 for (auto& property : interface.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700526 {
527 if (property.first == "Address")
528 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500529 const std::string* address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800530 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700531 if (address != nullptr)
532 {
533 ipv4_address.address = *address;
534 }
535 }
536 else if (property.first == "Gateway")
537 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500538 const std::string* gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800539 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700540 if (gateway != nullptr)
541 {
542 ipv4_address.gateway = *gateway;
543 }
544 }
545 else if (property.first == "Origin")
546 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500547 const std::string* origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800548 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700549 if (origin != nullptr)
550 {
551 ipv4_address.origin =
552 translateAddressOriginDbusToRedfish(*origin,
553 true);
554 }
555 }
556 else if (property.first == "PrefixLength")
557 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500558 const uint8_t* mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800559 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700560 if (mask != nullptr)
561 {
562 // convert it to the string
563 ipv4_address.netmask = getNetmask(*mask);
564 }
565 }
566 else
567 {
568 BMCWEB_LOG_ERROR
569 << "Got extra property: " << property.first
570 << " on the " << objpath.first.str << " object";
571 }
572 }
573 // Check if given address is local, or global
574 ipv4_address.linktype =
575 boost::starts_with(ipv4_address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700576 ? LinkType::Local
577 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700578 }
579 }
580 }
581 }
582}
583
584/**
585 * @brief Sets given Id on the given VLAN interface through D-Bus
586 *
587 * @param[in] ifaceId Id of VLAN interface that should be modified
588 * @param[in] inputVlanId New ID of the VLAN
589 * @param[in] callback Function that will be called after the operation
590 *
591 * @return None.
592 */
593template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500594void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
595 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700596{
597 crow::connections::systemBus->async_method_call(
598 callback, "xyz.openbmc_project.Network",
599 std::string("/xyz/openbmc_project/network/") + ifaceId,
600 "org.freedesktop.DBus.Properties", "Set",
601 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800602 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700603}
604
605/**
606 * @brief Helper function that verifies IP address to check if it is in
607 * proper format. If bits pointer is provided, also calculates active
608 * bit count for Subnet Mask.
609 *
610 * @param[in] ip IP that will be verified
611 * @param[out] bits Calculated mask in bits notation
612 *
613 * @return true in case of success, false otherwise
614 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500615inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
616 uint8_t* bits = nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700617{
618 std::vector<std::string> bytesInMask;
619
620 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621
622 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700623 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700624 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700625 return false;
626 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700627
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 *bits = 0;
631 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500633 char* endPtr;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700634 long previousValue = 255;
635 bool firstZeroInByteHit;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500636 for (const std::string& byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 if (byte.empty())
639 {
640 return false;
641 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 // Use strtol instead of stroi to avoid exceptions
644 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645
Ed Tanous4a0cb852018-10-15 07:55:04 -0700646 // endPtr should point to the end of the string, otherwise given string
647 // is not 100% number
648 if (*endPtr != '\0')
649 {
650 return false;
651 }
652
653 // Value should be contained in byte
654 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
656 return false;
657 }
658
659 if (bits != nullptr)
660 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700661 // Mask has to be continuous between bytes
662 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700663 {
664 return false;
665 }
666
Ed Tanous4a0cb852018-10-15 07:55:04 -0700667 // Mask has to be continuous inside bytes
668 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669
Ed Tanous4a0cb852018-10-15 07:55:04 -0700670 // Count bits
671 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700672 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700677 // Continuity not preserved
678 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 }
680 else
681 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
684 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 else
686 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700687 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700689 }
690 }
691
692 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 }
694
Ed Tanous4a0cb852018-10-15 07:55:04 -0700695 return true;
696}
697
698/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700699 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700 *
701 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700702 * @param[in] ipHash DBus Hash id of IP that should be deleted
703 * @param[io] asyncResp Response object that will be returned to client
704 *
705 * @return None
706 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500707inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700708 const std::shared_ptr<AsyncResp> asyncResp)
709{
710 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700711 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712 if (ec)
713 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800714 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100715 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700716 },
717 "xyz.openbmc_project.Network",
718 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
719 "xyz.openbmc_project.Object.Delete", "Delete");
720}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700721
Ed Tanous4a0cb852018-10-15 07:55:04 -0700722/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700723 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700724 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700725 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
726 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
727 * @param[in] gateway IPv4 address of this interfaces gateway
728 * @param[in] address IPv4 address to assign to this interface
729 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700730 *
731 * @return None
732 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500733inline void createIPv4(const std::string& ifaceId, unsigned int ipIdx,
734 uint8_t prefixLength, const std::string& gateway,
735 const std::string& address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700736 std::shared_ptr<AsyncResp> asyncResp)
737{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700738 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700739 [asyncResp](const boost::system::error_code ec) {
740 if (ec)
741 {
742 messages::internalError(asyncResp->res);
743 }
744 },
745 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700746 "/xyz/openbmc_project/network/" + ifaceId,
747 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700748 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700749 gateway);
750}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500751
752/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700753 * @brief Deletes the IPv4 entry for this interface and creates a replacement
754 * static IPv4 entry
755 *
756 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
757 * @param[in] id The unique hash entry identifying the DBus entry
758 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
759 * @param[in] gateway IPv4 address of this interfaces gateway
760 * @param[in] address IPv4 address to assign to this interface
761 * @param[io] asyncResp Response object that will be returned to client
762 *
763 * @return None
764 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500765inline void deleteAndCreateIPv4(const std::string& ifaceId,
766 const std::string& id, uint8_t prefixLength,
767 const std::string& gateway,
768 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700769 std::shared_ptr<AsyncResp> asyncResp)
770{
771 crow::connections::systemBus->async_method_call(
772 [asyncResp, ifaceId, address, prefixLength,
773 gateway](const boost::system::error_code ec) {
774 if (ec)
775 {
776 messages::internalError(asyncResp->res);
777 }
778 crow::connections::systemBus->async_method_call(
779 [asyncResp](const boost::system::error_code ec) {
780 if (ec)
781 {
782 messages::internalError(asyncResp->res);
783 }
784 },
785 "xyz.openbmc_project.Network",
786 "/xyz/openbmc_project/network/" + ifaceId,
787 "xyz.openbmc_project.Network.IP.Create", "IP",
788 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
789 prefixLength, gateway);
790 },
791 "xyz.openbmc_project.Network",
792 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
793 "xyz.openbmc_project.Object.Delete", "Delete");
794}
795
796/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500797 * @brief Deletes given IPv6
798 *
799 * @param[in] ifaceId Id of interface whose IP should be deleted
800 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500801 * @param[io] asyncResp Response object that will be returned to client
802 *
803 * @return None
804 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500805inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500806 const std::shared_ptr<AsyncResp> asyncResp)
807{
808 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700809 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500810 if (ec)
811 {
812 messages::internalError(asyncResp->res);
813 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500814 },
815 "xyz.openbmc_project.Network",
816 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
817 "xyz.openbmc_project.Object.Delete", "Delete");
818}
819
820/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700821 * @brief Deletes the IPv6 entry for this interface and creates a replacement
822 * static IPv6 entry
823 *
824 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
825 * @param[in] id The unique hash entry identifying the DBus entry
826 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
827 * @param[in] address IPv6 address to assign to this interface
828 * @param[io] asyncResp Response object that will be returned to client
829 *
830 * @return None
831 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500832inline void deleteAndCreateIPv6(const std::string& ifaceId,
833 const std::string& id, uint8_t prefixLength,
834 const std::string& address,
Johnathan Mantey01784822019-06-18 12:44:21 -0700835 std::shared_ptr<AsyncResp> asyncResp)
836{
837 crow::connections::systemBus->async_method_call(
838 [asyncResp, ifaceId, address,
839 prefixLength](const boost::system::error_code ec) {
840 if (ec)
841 {
842 messages::internalError(asyncResp->res);
843 }
844 crow::connections::systemBus->async_method_call(
845 [asyncResp](const boost::system::error_code ec) {
846 if (ec)
847 {
848 messages::internalError(asyncResp->res);
849 }
850 },
851 "xyz.openbmc_project.Network",
852 "/xyz/openbmc_project/network/" + ifaceId,
853 "xyz.openbmc_project.Network.IP.Create", "IP",
854 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
855 prefixLength, "");
856 },
857 "xyz.openbmc_project.Network",
858 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
859 "xyz.openbmc_project.Object.Delete", "Delete");
860}
861
862/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500863 * @brief Creates IPv6 with given data
864 *
865 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500866 * @param[in] prefixLength Prefix length that needs to be added
867 * @param[in] address IP address that needs to be added
868 * @param[io] asyncResp Response object that will be returned to client
869 *
870 * @return None
871 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500872inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
873 const std::string& address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500874 std::shared_ptr<AsyncResp> asyncResp)
875{
876 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
877 if (ec)
878 {
879 messages::internalError(asyncResp->res);
880 }
881 };
882 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500883 // does not have associated gateway property
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500884 crow::connections::systemBus->async_method_call(
885 std::move(createIpHandler), "xyz.openbmc_project.Network",
886 "/xyz/openbmc_project/network/" + ifaceId,
887 "xyz.openbmc_project.Network.IP.Create", "IP",
888 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
889 "");
890}
891
Ed Tanous4a0cb852018-10-15 07:55:04 -0700892/**
893 * Function that retrieves all properties for given Ethernet Interface
894 * Object
895 * from EntityManager Network Manager
896 * @param ethiface_id a eth interface id to query on DBus
897 * @param callback a function that shall be called to convert Dbus output
898 * into JSON
899 */
900template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500901void getEthernetIfaceData(const std::string& ethiface_id,
902 CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903{
904 crow::connections::systemBus->async_method_call(
905 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
906 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500907 const GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700908 EthernetInterfaceData ethData{};
909 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500910 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700911
912 if (error_code)
913 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700914 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700915 return;
916 }
917
Ed Tanous4c9afe42019-05-03 16:59:57 -0700918 bool found =
919 extractEthernetInterfaceData(ethiface_id, resp, ethData);
920 if (!found)
921 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700922 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700923 return;
924 }
925
Johnathan Mantey01784822019-06-18 12:44:21 -0700926 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700927 // Fix global GW
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500928 for (IPv4AddressData& ipv4 : ipv4Data)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700929 {
Ravi Tejac6191412019-07-30 00:53:50 -0500930 if (((ipv4.linktype == LinkType::Global) &&
931 (ipv4.gateway == "0.0.0.0")) ||
932 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934 ipv4.gateway = ethData.default_gateway;
935 }
936 }
937
Johnathan Mantey01784822019-06-18 12:44:21 -0700938 extractIPV6Data(ethiface_id, resp, ipv6Data);
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500939 // Finally make a callback with useful data
Johnathan Mantey01784822019-06-18 12:44:21 -0700940 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 },
942 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
943 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700944}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700945
946/**
947 * Function that retrieves all Ethernet Interfaces available through Network
948 * Manager
949 * @param callback a function that shall be called to convert Dbus output
950 * into JSON.
951 */
952template <typename CallbackFunc>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500953void getEthernetIfaceList(CallbackFunc&& callback)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954{
955 crow::connections::systemBus->async_method_call(
956 [callback{std::move(callback)}](
957 const boost::system::error_code error_code,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500958 GetManagedObjects& resp) {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700959 // Callback requires vector<string> to retrieve all available
960 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700961 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 iface_list.reserve(resp.size());
963 if (error_code)
964 {
965 callback(false, iface_list);
966 return;
967 }
968
969 // Iterate over all retrieved ObjectPaths.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500970 for (const auto& objpath : resp)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700971 {
972 // And all interfaces available for certain ObjectPath.
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500973 for (const auto& interface : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700974 {
975 // If interface is
976 // xyz.openbmc_project.Network.EthernetInterface, this is
977 // what we're looking for.
978 if (interface.first ==
979 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
Gunnar Mills4e0453b2020-07-08 14:00:30 -0500981 // Cut out everything until last "/", ...
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500982 const std::string& iface_id = objpath.first.str;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700983 std::size_t last_pos = iface_id.rfind("/");
984 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700987 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 }
989 }
990 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 }
992 // Finally make a callback with useful data
993 callback(true, iface_list);
994 },
995 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700997}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100998
999/**
1000 * EthernetCollection derived class for delivering Ethernet Collection Schema
1001 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002class EthernetCollection : public Node
1003{
1004 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -07001005 template <typename CrowApp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001006 EthernetCollection(CrowApp& 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 Tanous4a0cb852018-10-15 07:55:04 -07001078 template <typename CrowApp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001079 EthernetInterface(CrowApp& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001080 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001081 std::string())
1082 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001083 entityPrivileges = {
1084 {boost::beast::http::verb::get, {{"Login"}}},
1085 {boost::beast::http::verb::head, {{"Login"}}},
1086 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1087 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1088 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1089 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001090 }
1091
Ed Tanous1abe55e2018-09-05 08:30:59 -07001092 private:
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001093 void handleHostnamePatch(const std::string& hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001094 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001095 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301096 // SHOULD handle host names of up to 255 characters(RFC 1123)
1097 if (hostname.length() > 255)
1098 {
1099 messages::propertyValueFormatError(asyncResp->res, hostname,
1100 "HostName");
1101 return;
1102 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001103 crow::connections::systemBus->async_method_call(
1104 [asyncResp](const boost::system::error_code ec) {
1105 if (ec)
1106 {
1107 messages::internalError(asyncResp->res);
1108 }
1109 },
1110 "xyz.openbmc_project.Network",
1111 "/xyz/openbmc_project/network/config",
1112 "org.freedesktop.DBus.Properties", "Set",
1113 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001114 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001115 }
1116
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001117 void handleDomainnamePatch(const std::string& ifaceId,
1118 const std::string& domainname,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301119 const std::shared_ptr<AsyncResp> asyncResp)
1120 {
1121 std::vector<std::string> vectorDomainname = {domainname};
1122 crow::connections::systemBus->async_method_call(
1123 [asyncResp](const boost::system::error_code ec) {
1124 if (ec)
1125 {
1126 messages::internalError(asyncResp->res);
1127 }
1128 },
1129 "xyz.openbmc_project.Network",
1130 "/xyz/openbmc_project/network/" + ifaceId,
1131 "org.freedesktop.DBus.Properties", "Set",
1132 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1133 std::variant<std::vector<std::string>>(vectorDomainname));
1134 }
1135
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001136 void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301137 const std::shared_ptr<AsyncResp> asyncResp)
1138 {
1139 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1140 if (fqdn.length() > 255)
1141 {
1142 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1143 return;
1144 }
1145
1146 size_t pos = fqdn.find('.');
1147 if (pos == std::string::npos)
1148 {
1149 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1150 return;
1151 }
1152
1153 std::string hostname;
1154 std::string domainname;
1155 domainname = (fqdn).substr(pos + 1);
1156 hostname = (fqdn).substr(0, pos);
1157
1158 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1159 {
1160 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1161 return;
1162 }
1163
1164 handleHostnamePatch(hostname, asyncResp);
1165 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1166 }
1167
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001168 bool isHostnameValid(const std::string& hostname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301169 {
1170 // A valid host name can never have the dotted-decimal form (RFC 1123)
1171 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1172 {
1173 return false;
1174 }
1175 // Each label(hostname/subdomains) within a valid FQDN
1176 // MUST handle host names of up to 63 characters (RFC 1123)
1177 // labels cannot start or end with hyphens (RFC 952)
1178 // labels can start with numbers (RFC 1123)
1179 const std::regex pattern(
1180 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1181
1182 return std::regex_match(hostname, pattern);
1183 }
1184
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001185 bool isDomainnameValid(const std::string& domainname)
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301186 {
1187 // Can have multiple subdomains
1188 // Top Level Domain's min length is 2 character
1189 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1190 "{1,30}\\.)*[a-zA-Z]{2,}$");
1191
1192 return std::regex_match(domainname, pattern);
1193 }
1194
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001195 void handleMACAddressPatch(const std::string& ifaceId,
1196 const std::string& macAddress,
1197 const std::shared_ptr<AsyncResp>& asyncResp)
Ratan Guptad5776652019-03-03 08:47:22 +05301198 {
1199 crow::connections::systemBus->async_method_call(
1200 [asyncResp, macAddress](const boost::system::error_code ec) {
1201 if (ec)
1202 {
1203 messages::internalError(asyncResp->res);
1204 return;
1205 }
Ratan Guptad5776652019-03-03 08:47:22 +05301206 },
1207 "xyz.openbmc_project.Network",
1208 "/xyz/openbmc_project/network/" + ifaceId,
1209 "org.freedesktop.DBus.Properties", "Set",
1210 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1211 std::variant<std::string>(macAddress));
1212 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001213
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001214 void setDHCPEnabled(const std::string& ifaceId,
1215 const std::string& propertyName, const bool v4Value,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001216 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001217 const std::shared_ptr<AsyncResp> asyncResp)
1218 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001219 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001220 crow::connections::systemBus->async_method_call(
1221 [asyncResp](const boost::system::error_code ec) {
1222 if (ec)
1223 {
1224 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1225 messages::internalError(asyncResp->res);
1226 return;
1227 }
1228 },
1229 "xyz.openbmc_project.Network",
1230 "/xyz/openbmc_project/network/" + ifaceId,
1231 "org.freedesktop.DBus.Properties", "Set",
1232 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001233 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001234 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001235
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001236 void setEthernetInterfaceBoolProperty(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001237 const std::string& ifaceId, const std::string& propertyName,
1238 const bool& value, const std::shared_ptr<AsyncResp> asyncResp)
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001239 {
1240 crow::connections::systemBus->async_method_call(
1241 [asyncResp](const boost::system::error_code ec) {
1242 if (ec)
1243 {
1244 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1245 messages::internalError(asyncResp->res);
1246 return;
1247 }
1248 },
1249 "xyz.openbmc_project.Network",
1250 "/xyz/openbmc_project/network/" + ifaceId,
1251 "org.freedesktop.DBus.Properties", "Set",
1252 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1253 std::variant<bool>{value});
1254 }
1255
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001256 void setDHCPv4Config(const std::string& propertyName, const bool& value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001257 const std::shared_ptr<AsyncResp> asyncResp)
1258 {
1259 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1260 crow::connections::systemBus->async_method_call(
1261 [asyncResp](const boost::system::error_code ec) {
1262 if (ec)
1263 {
1264 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1265 messages::internalError(asyncResp->res);
1266 return;
1267 }
1268 },
1269 "xyz.openbmc_project.Network",
1270 "/xyz/openbmc_project/network/config/dhcp",
1271 "org.freedesktop.DBus.Properties", "Set",
1272 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1273 std::variant<bool>{value});
1274 }
Ratan Guptad5776652019-03-03 08:47:22 +05301275
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001276 void handleDHCPPatch(const std::string& ifaceId,
1277 const EthernetInterfaceData& ethData,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001278 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1279 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001280 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001281 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1282 bool ipv6Active =
1283 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001284
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001285 bool nextv4DHCPState =
1286 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1287
1288 bool nextv6DHCPState{};
1289 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001290 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001291 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1292 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1293 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1294 {
1295 messages::propertyValueFormatError(
1296 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1297 "OperatingMode");
1298 return;
1299 }
1300 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1301 }
1302 else
1303 {
1304 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001305 }
1306
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001307 bool nextDNS{};
1308 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001309 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001310 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1311 {
1312 messages::generalError(asyncResp->res);
1313 return;
1314 }
1315 nextDNS = *v4dhcpParms.useDNSServers;
1316 }
1317 else if (v4dhcpParms.useDNSServers)
1318 {
1319 nextDNS = *v4dhcpParms.useDNSServers;
1320 }
1321 else if (v6dhcpParms.useDNSServers)
1322 {
1323 nextDNS = *v6dhcpParms.useDNSServers;
1324 }
1325 else
1326 {
1327 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001328 }
1329
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001330 bool nextNTP{};
1331 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001332 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001333 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1334 {
1335 messages::generalError(asyncResp->res);
1336 return;
1337 }
1338 nextNTP = *v4dhcpParms.useNTPServers;
1339 }
1340 else if (v4dhcpParms.useNTPServers)
1341 {
1342 nextNTP = *v4dhcpParms.useNTPServers;
1343 }
1344 else if (v6dhcpParms.useNTPServers)
1345 {
1346 nextNTP = *v6dhcpParms.useNTPServers;
1347 }
1348 else
1349 {
1350 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001351 }
1352
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001353 bool nextUseDomain{};
1354 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001355 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001356 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1357 {
1358 messages::generalError(asyncResp->res);
1359 return;
1360 }
1361 nextUseDomain = *v4dhcpParms.useUseDomainName;
1362 }
1363 else if (v4dhcpParms.useUseDomainName)
1364 {
1365 nextUseDomain = *v4dhcpParms.useUseDomainName;
1366 }
1367 else if (v6dhcpParms.useUseDomainName)
1368 {
1369 nextUseDomain = *v6dhcpParms.useUseDomainName;
1370 }
1371 else
1372 {
1373 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001374 }
1375
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001376 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1377 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1378 asyncResp);
1379 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1380 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1381 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1382 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1383 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1384 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001385 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001386
1387 boost::container::flat_set<IPv4AddressData>::const_iterator
1388 GetNextStaticIPEntry(
1389 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1390 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1391 {
1392 for (; head != end; head++)
1393 {
1394 if (head->origin == "Static")
1395 {
1396 return head;
1397 }
1398 }
1399 return end;
1400 }
1401
1402 boost::container::flat_set<IPv6AddressData>::const_iterator
1403 GetNextStaticIPEntry(
1404 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1405 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1406 {
1407 for (; head != end; head++)
1408 {
1409 if (head->origin == "Static")
1410 {
1411 return head;
1412 }
1413 }
1414 return end;
1415 }
1416
Ravi Tejad1d50812019-06-23 16:20:27 -05001417 void handleIPv4StaticPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001418 const std::string& ifaceId, nlohmann::json& input,
1419 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001420 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001421 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001422 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301423 {
1424 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001425 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301426 return;
1427 }
1428
Ed Tanous271584a2019-07-09 16:24:22 -07001429 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001430 // Find the first static IP address currently active on the NIC and
1431 // match it to the first JSON element in the IPv4StaticAddresses array.
1432 // Match each subsequent JSON element to the next static IP programmed
1433 // into the NIC.
1434 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1435 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1436
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001437 for (nlohmann::json& thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001438 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001439 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001440 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001441
Johnathan Mantey01784822019-06-18 12:44:21 -07001442 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301443 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001444 std::optional<std::string> address;
1445 std::optional<std::string> subnetMask;
1446 std::optional<std::string> gateway;
1447
1448 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1449 address, "SubnetMask", subnetMask,
1450 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301451 {
1452 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001453 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301454 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301455 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301456
Johnathan Mantey01784822019-06-18 12:44:21 -07001457 // Find the address/subnet/gateway values. Any values that are
1458 // not explicitly provided are assumed to be unmodified from the
1459 // current state of the interface. Merge existing state into the
1460 // current request.
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001461 const std::string* addr = nullptr;
1462 const std::string* gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001463 uint8_t prefixLength = 0;
1464 bool errorInEntry = false;
1465 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301466 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001467 if (ipv4VerifyIpAndGetBitcount(*address))
1468 {
1469 addr = &(*address);
1470 }
1471 else
1472 {
1473 messages::propertyValueFormatError(
1474 asyncResp->res, *address, pathString + "/Address");
1475 errorInEntry = true;
1476 }
1477 }
1478 else if (NICIPentry != ipv4Data.cend())
1479 {
1480 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301481 }
1482 else
1483 {
1484 messages::propertyMissing(asyncResp->res,
1485 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001486 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001487 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301488
1489 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001490 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001491 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1492 {
1493 messages::propertyValueFormatError(
1494 asyncResp->res, *subnetMask,
1495 pathString + "/SubnetMask");
1496 errorInEntry = true;
1497 }
1498 }
1499 else if (NICIPentry != ipv4Data.cend())
1500 {
1501 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1502 &prefixLength))
1503 {
1504 messages::propertyValueFormatError(
1505 asyncResp->res, NICIPentry->netmask,
1506 pathString + "/SubnetMask");
1507 errorInEntry = true;
1508 }
1509 }
1510 else
1511 {
1512 messages::propertyMissing(asyncResp->res,
1513 pathString + "/SubnetMask");
1514 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001515 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301516
Ratan Guptaf476acb2019-03-02 16:46:57 +05301517 if (gateway)
1518 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001519 if (ipv4VerifyIpAndGetBitcount(*gateway))
1520 {
1521 gw = &(*gateway);
1522 }
1523 else
1524 {
1525 messages::propertyValueFormatError(
1526 asyncResp->res, *gateway, pathString + "/Gateway");
1527 errorInEntry = true;
1528 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301529 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001530 else if (NICIPentry != ipv4Data.cend())
1531 {
1532 gw = &NICIPentry->gateway;
1533 }
1534 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001535 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001536 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001537 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001538 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001539 }
1540
Johnathan Mantey01784822019-06-18 12:44:21 -07001541 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001542 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001543 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001544 }
1545
Johnathan Mantey01784822019-06-18 12:44:21 -07001546 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001547 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001548 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1549 *gw, *addr, asyncResp);
1550 NICIPentry =
1551 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001552 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001553 else
1554 {
1555 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1556 *address, asyncResp);
1557 }
1558 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001559 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001560 else
1561 {
1562 if (NICIPentry == ipv4Data.cend())
1563 {
1564 // Requesting a DELETE/DO NOT MODIFY action for an item
1565 // that isn't present on the eth(n) interface. Input JSON is
1566 // in error, so bail out.
1567 if (thisJson.is_null())
1568 {
1569 messages::resourceCannotBeDeleted(asyncResp->res);
1570 return;
1571 }
1572 else
1573 {
1574 messages::propertyValueFormatError(
1575 asyncResp->res, thisJson.dump(), pathString);
1576 return;
1577 }
1578 }
1579
1580 if (thisJson.is_null())
1581 {
1582 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1583 }
1584 if (NICIPentry != ipv4Data.cend())
1585 {
1586 NICIPentry =
1587 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1588 }
1589 entryIdx++;
1590 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001591 }
1592 }
1593
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001594 void handleStaticNameServersPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001595 const std::string& ifaceId,
1596 const std::vector<std::string>& updatedStaticNameServers,
1597 const std::shared_ptr<AsyncResp>& asyncResp)
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001598 {
1599 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001600 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001601 if (ec)
1602 {
1603 messages::internalError(asyncResp->res);
1604 return;
1605 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001606 },
1607 "xyz.openbmc_project.Network",
1608 "/xyz/openbmc_project/network/" + ifaceId,
1609 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001610 "xyz.openbmc_project.Network.EthernetInterface",
1611 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001612 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1613 }
1614
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001615 void handleIPv6StaticAddressesPatch(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001616 const std::string& ifaceId, nlohmann::json& input,
1617 const boost::container::flat_set<IPv6AddressData>& ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001618 const std::shared_ptr<AsyncResp> asyncResp)
1619 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001620 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001621 {
1622 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1623 "IPv6StaticAddresses");
1624 return;
1625 }
Ed Tanous271584a2019-07-09 16:24:22 -07001626 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001627 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1628 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001629 for (nlohmann::json& thisJson : input)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001630 {
1631 std::string pathString =
1632 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1633
Johnathan Mantey01784822019-06-18 12:44:21 -07001634 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001635 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001636 std::optional<std::string> address;
1637 std::optional<uint8_t> prefixLength;
1638
1639 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1640 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001641 {
1642 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001643 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001644 return;
1645 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001646
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001647 const std::string* addr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001648 uint8_t prefix;
1649
1650 // Find the address and prefixLength values. Any values that are
1651 // not explicitly provided are assumed to be unmodified from the
1652 // current state of the interface. Merge existing state into the
1653 // current request.
1654 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001655 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001656 addr = &(*address);
1657 }
1658 else if (NICIPentry != ipv6Data.end())
1659 {
1660 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001661 }
1662 else
1663 {
1664 messages::propertyMissing(asyncResp->res,
1665 pathString + "/Address");
1666 return;
1667 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001668
1669 if (prefixLength)
1670 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001671 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001672 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001673 else if (NICIPentry != ipv6Data.end())
1674 {
1675 prefix = NICIPentry->prefixLength;
1676 }
1677 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001678 {
1679 messages::propertyMissing(asyncResp->res,
1680 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001681 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001682 }
1683
Johnathan Mantey01784822019-06-18 12:44:21 -07001684 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001685 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001686 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1687 asyncResp);
1688 NICIPentry =
1689 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1690 }
1691 else
1692 {
1693 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1694 }
1695 entryIdx++;
1696 }
1697 else
1698 {
1699 if (NICIPentry == ipv6Data.end())
1700 {
1701 // Requesting a DELETE/DO NOT MODIFY action for an item
1702 // that isn't present on the eth(n) interface. Input JSON is
1703 // in error, so bail out.
1704 if (thisJson.is_null())
1705 {
1706 messages::resourceCannotBeDeleted(asyncResp->res);
1707 return;
1708 }
1709 else
1710 {
1711 messages::propertyValueFormatError(
1712 asyncResp->res, thisJson.dump(), pathString);
1713 return;
1714 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001715 }
1716
Johnathan Mantey01784822019-06-18 12:44:21 -07001717 if (thisJson.is_null())
1718 {
1719 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1720 }
1721 if (NICIPentry != ipv6Data.cend())
1722 {
1723 NICIPentry =
1724 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1725 }
1726 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001727 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001728 }
1729 }
1730
Ed Tanous0f74e642018-11-12 15:17:05 -08001731 void parseInterfaceData(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001732 std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id,
1733 const EthernetInterfaceData& ethData,
1734 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1735 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001736 {
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001737 constexpr const std::array<const char*, 1> inventoryForEthernet = {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001738 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1739
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001740 nlohmann::json& json_response = asyncResp->res.jsonValue;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001741 json_response["Id"] = iface_id;
1742 json_response["@odata.id"] =
1743 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001744 json_response["InterfaceEnabled"] = ethData.nicEnabled;
1745
1746 auto health = std::make_shared<HealthPopulate>(asyncResp);
1747
1748 crow::connections::systemBus->async_method_call(
1749 [health](const boost::system::error_code ec,
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001750 std::vector<std::string>& resp) {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001751 if (ec)
1752 {
1753 return;
1754 }
1755
1756 health->inventory = std::move(resp);
1757 },
1758 "xyz.openbmc_project.ObjectMapper",
1759 "/xyz/openbmc_project/object_mapper",
1760 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1761 int32_t(0), inventoryForEthernet);
1762
1763 health->populate();
1764
1765 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001766 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001767 json_response["LinkStatus"] = "LinkUp";
1768 json_response["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001769 }
1770 else
1771 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001772 json_response["LinkStatus"] = "NoLink";
1773 json_response["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001774 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001775
1776 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001777 json_response["SpeedMbps"] = ethData.speed;
1778 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001779 json_response["DHCPv4"]["DHCPEnabled"] =
1780 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1781 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1782 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1783 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1784
1785 json_response["DHCPv6"]["OperatingMode"] =
1786 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1787 : "Disabled";
1788 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1789 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1790 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301791
Ed Tanous4a0cb852018-10-15 07:55:04 -07001792 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001793 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001794 json_response["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301795
1796 // When domain name is empty then it means, that it is a network
1797 // without domain names, and the host name itself must be treated as
1798 // FQDN
1799 std::string FQDN = std::move(ethData.hostname);
Jennifer Leed24bfc72019-03-05 13:03:37 -08001800 if (!ethData.domainnames.empty())
1801 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301802 FQDN += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001803 }
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301804 json_response["FQDN"] = FQDN;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001805 }
1806
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001807 json_response["VLANs"] = {
1808 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1809 iface_id + "/VLANs"}};
1810
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001811 json_response["NameServers"] = ethData.nameServers;
1812 json_response["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001813
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001814 nlohmann::json& ipv4_array = json_response["IPv4Addresses"];
1815 nlohmann::json& ipv4_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001816 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001817 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001818 ipv4_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001819 for (auto& ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001820 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001821
1822 std::string gatewayStr = ipv4_config.gateway;
1823 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001824 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001825 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001826 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001827
1828 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1829 {"SubnetMask", ipv4_config.netmask},
1830 {"Address", ipv4_config.address},
1831 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001832 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001833 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001834 ipv4_static_array.push_back(
1835 {{"AddressOrigin", ipv4_config.origin},
1836 {"SubnetMask", ipv4_config.netmask},
1837 {"Address", ipv4_config.address},
1838 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001839 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001840 }
1841
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001842 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001843
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001844 nlohmann::json& ipv6_array = json_response["IPv6Addresses"];
1845 nlohmann::json& ipv6_static_array =
Johnathan Mantey01784822019-06-18 12:44:21 -07001846 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001847 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001848 ipv6_static_array = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001849 nlohmann::json& ipv6AddrPolicyTable =
Johnathan Mantey7f2e23e2020-05-14 13:36:52 -07001850 json_response["IPv6AddressPolicyTable"];
1851 ipv6AddrPolicyTable = nlohmann::json::array();
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001852 for (auto& ipv6_config : ipv6Data)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001853 {
1854 ipv6_array.push_back({{"Address", ipv6_config.address},
1855 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001856 {"AddressOrigin", ipv6_config.origin},
1857 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001858 if (ipv6_config.origin == "Static")
1859 {
1860 ipv6_static_array.push_back(
1861 {{"Address", ipv6_config.address},
1862 {"PrefixLength", ipv6_config.prefixLength},
Johnathan Mantey5fd16e42020-05-13 15:14:47 -07001863 {"AddressOrigin", ipv6_config.origin},
1864 {"AddressState", nullptr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001865 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001866 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001867 }
1868
1869 /**
1870 * Functions triggers appropriate requests on DBus
1871 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001872 void doGet(crow::Response& res, const crow::Request& req,
1873 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001874 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001875 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001876 if (params.size() != 1)
1877 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001878 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001879 return;
1880 }
1881
Ed Tanous4a0cb852018-10-15 07:55:04 -07001882 getEthernetIfaceData(
1883 params[0],
1884 [this, asyncResp, iface_id{std::string(params[0])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001885 const bool& success, const EthernetInterfaceData& ethData,
1886 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1887 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001888 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001889 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001890 // TODO(Pawel)consider distinguish between non existing
1891 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001892 messages::resourceNotFound(asyncResp->res,
1893 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001894 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001895 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001896
Ed Tanous0f74e642018-11-12 15:17:05 -08001897 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001898 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001899 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1900 asyncResp->res.jsonValue["Description"] =
1901 "Management Network Interface";
1902
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001903 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1904 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001905 });
1906 }
1907
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001908 void doPatch(crow::Response& res, const crow::Request& req,
1909 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001910 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001911 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001912 if (params.size() != 1)
1913 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001914 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001915 return;
1916 }
1917
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001918 const std::string& iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001919
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001920 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301921 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301922 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001923 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001924 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001925 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001926 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001927 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001928 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001929 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001930 DHCPParameters v4dhcpParms;
1931 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001932
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001933 if (!json_util::readJson(
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301934 req, res, "HostName", hostname, "FQDN", fqdn,
1935 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1936 macAddress, "StaticNameServers", staticNameServers,
1937 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1938 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1939 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940 {
1941 return;
1942 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001943 if (dhcpv4)
1944 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001945 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1946 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1947 v4dhcpParms.useDNSServers, "UseNTPServers",
1948 v4dhcpParms.useNTPServers, "UseDomainName",
1949 v4dhcpParms.useUseDomainName))
1950 {
1951 return;
1952 }
1953 }
1954
1955 if (dhcpv6)
1956 {
1957 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1958 v6dhcpParms.dhcpv6OperatingMode,
1959 "UseDNSServers", v6dhcpParms.useDNSServers,
1960 "UseNTPServers", v6dhcpParms.useNTPServers,
1961 "UseDomainName",
1962 v6dhcpParms.useUseDomainName))
1963 {
1964 return;
1965 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001966 }
1967
Johnathan Mantey01784822019-06-18 12:44:21 -07001968 // Get single eth interface data, and call the below callback for
1969 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001970 getEthernetIfaceData(
1971 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001972 [this, asyncResp, iface_id, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301973 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001974 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001975 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001976 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001977 staticNameServers = std::move(staticNameServers),
1978 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1979 v4dhcpParms = std::move(v4dhcpParms),
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001980 v6dhcpParms = std::move(v6dhcpParms),
1981 interfaceEnabled = std::move(interfaceEnabled)](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001982 const bool& success, const EthernetInterfaceData& ethData,
1983 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1984 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001985 if (!success)
1986 {
1987 // ... otherwise return error
1988 // TODO(Pawel)consider distinguish between non existing
1989 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001990 messages::resourceNotFound(asyncResp->res,
1991 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001992 return;
1993 }
1994
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001995 if (dhcpv4 || dhcpv6)
1996 {
1997 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1998 std::move(v6dhcpParms), asyncResp);
1999 }
2000
Ed Tanous0627a2c2018-11-29 17:09:23 -08002001 if (hostname)
2002 {
2003 handleHostnamePatch(*hostname, asyncResp);
2004 }
2005
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302006 if (fqdn)
2007 {
2008 handleFqdnPatch(iface_id, *fqdn, asyncResp);
2009 }
2010
Ratan Guptad5776652019-03-03 08:47:22 +05302011 if (macAddress)
2012 {
2013 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2014 }
2015
Ravi Tejad1d50812019-06-23 16:20:27 -05002016 if (ipv4StaticAddresses)
2017 {
Ed Tanous537174c2018-12-10 15:09:31 -08002018 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002019 // above is returning a const value, not a non-const
2020 // value. This doesn't really work for us, as we need to
2021 // be able to efficiently move out the intermedia
2022 // nlohmann::json objects. This makes a copy of the
2023 // structure, and operates on that, but could be done
2024 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05002025 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07002026 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002027 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002028 }
2029
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002030 if (staticNameServers)
2031 {
2032 handleStaticNameServersPatch(iface_id, *staticNameServers,
2033 asyncResp);
2034 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002035
2036 if (ipv6DefaultGateway)
2037 {
2038 messages::propertyNotWritable(asyncResp->res,
2039 "IPv6DefaultGateway");
2040 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002041
2042 if (ipv6StaticAddresses)
2043 {
2044 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2045 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002046 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002047 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002048
2049 if (interfaceEnabled)
2050 {
2051 setEthernetInterfaceBoolProperty(
2052 iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2053 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002054 });
2055 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002056};
2057
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002058/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002059 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2060 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002061 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002062class VlanNetworkInterface : public Node
2063{
2064 public:
2065 /*
2066 * Default Constructor
2067 */
2068 template <typename CrowApp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002069 VlanNetworkInterface(CrowApp& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002070 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002071 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002072 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002073 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002074 entityPrivileges = {
2075 {boost::beast::http::verb::get, {{"Login"}}},
2076 {boost::beast::http::verb::head, {{"Login"}}},
2077 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2078 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2079 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002081 }
2082
Ed Tanous1abe55e2018-09-05 08:30:59 -07002083 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08002084 void parseInterfaceData(
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002085 nlohmann::json& json_response, const std::string& parent_iface_id,
2086 const std::string& iface_id, const EthernetInterfaceData& ethData,
2087 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2088 const boost::container::flat_set<IPv6AddressData>& ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002089 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002090 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07002091 json_response["Id"] = iface_id;
2092 json_response["@odata.id"] =
2093 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2094 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002095
Ed Tanous4a0cb852018-10-15 07:55:04 -07002096 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002097 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002098 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002099 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002100 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002101 }
2102
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002103 bool verifyNames(const std::string& parent, const std::string& iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002104 {
2105 if (!boost::starts_with(iface, parent + "_"))
2106 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002107 return false;
2108 }
2109 else
2110 {
2111 return true;
2112 }
2113 }
2114
2115 /**
2116 * Functions triggers appropriate requests on DBus
2117 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002118 void doGet(crow::Response& res, const crow::Request& req,
2119 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002120 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002121 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2122 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002123 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2124 // Check if there is required param, truly entering this shall be
2125 // impossible.
2126 if (params.size() != 2)
2127 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002128 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002129 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002130 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002131 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002132
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002133 const std::string& parent_iface_id = params[0];
2134 const std::string& iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08002135 res.jsonValue["@odata.type"] =
2136 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002137 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002138
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002139 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002140 {
2141 return;
2142 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002143
Johnathan Mantey01784822019-06-18 12:44:21 -07002144 // Get single eth interface data, and call the below callback for
2145 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002146 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002147 params[1],
2148 [this, asyncResp, parent_iface_id{std::string(params[0])},
2149 iface_id{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002150 const bool& success, const EthernetInterfaceData& ethData,
2151 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2152 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002153 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002154 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002155 parseInterfaceData(asyncResp->res.jsonValue,
2156 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002157 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002158 }
2159 else
2160 {
2161 // ... otherwise return error
2162 // TODO(Pawel)consider distinguish between non existing
2163 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002164 messages::resourceNotFound(
2165 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002166 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002167 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002168 }
2169
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002170 void doPatch(crow::Response& res, const crow::Request& req,
2171 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002173 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002174 if (params.size() != 2)
2175 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002176 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002177 return;
2178 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002179
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002180 const std::string& parentIfaceId = params[0];
2181 const std::string& ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002182
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002183 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002184 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002185 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2186 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 return;
2188 }
2189
Ed Tanous0627a2c2018-11-29 17:09:23 -08002190 bool vlanEnable = false;
Andrew Geissler38268fa2020-05-16 14:22:18 -05002191 uint32_t vlanId = 0;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002192
2193 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2194 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002195 {
2196 return;
2197 }
2198
Johnathan Mantey01784822019-06-18 12:44:21 -07002199 // Get single eth interface data, and call the below callback for
2200 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002201 getEthernetIfaceData(
2202 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002203 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002204 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002205 const bool& success, const EthernetInterfaceData& ethData,
2206 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2207 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002208 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002209 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002210 auto callback =
2211 [asyncResp](const boost::system::error_code ec) {
2212 if (ec)
2213 {
2214 messages::internalError(asyncResp->res);
2215 }
2216 };
2217
2218 if (vlanEnable == true)
2219 {
2220 crow::connections::systemBus->async_method_call(
2221 std::move(callback), "xyz.openbmc_project.Network",
2222 "/xyz/openbmc_project/network/" + ifaceId,
2223 "org.freedesktop.DBus.Properties", "Set",
2224 "xyz.openbmc_project.Network.VLAN", "Id",
2225 std::variant<uint32_t>(vlanId));
2226 }
2227 else
2228 {
2229 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2230 "vlan interface";
2231 crow::connections::systemBus->async_method_call(
2232 std::move(callback), "xyz.openbmc_project.Network",
2233 std::string("/xyz/openbmc_project/network/") +
2234 ifaceId,
2235 "xyz.openbmc_project.Object.Delete", "Delete");
2236 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002237 }
2238 else
2239 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002240 // TODO(Pawel)consider distinguish between non existing
2241 // object, and other errors
2242 messages::resourceNotFound(
2243 asyncResp->res, "VLAN Network Interface", ifaceId);
2244 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002245 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002246 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002247 }
2248
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002249 void doDelete(crow::Response& res, const crow::Request& req,
2250 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002251 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002252 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002253 if (params.size() != 2)
2254 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002255 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002256 return;
2257 }
2258
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002259 const std::string& parentIfaceId = params[0];
2260 const std::string& ifaceId = params[1];
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002262 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002263 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002264 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2265 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002266 return;
2267 }
2268
Johnathan Mantey01784822019-06-18 12:44:21 -07002269 // Get single eth interface data, and call the below callback for
2270 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002271 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002272 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002273 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002274 ifaceId{std::string(params[1])}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002275 const bool& success, const EthernetInterfaceData& ethData,
2276 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2277 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002278 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002279 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002280 auto callback =
2281 [asyncResp](const boost::system::error_code ec) {
2282 if (ec)
2283 {
2284 messages::internalError(asyncResp->res);
2285 }
2286 };
2287 crow::connections::systemBus->async_method_call(
2288 std::move(callback), "xyz.openbmc_project.Network",
2289 std::string("/xyz/openbmc_project/network/") + ifaceId,
2290 "xyz.openbmc_project.Object.Delete", "Delete");
2291 }
2292 else
2293 {
2294 // ... otherwise return error
2295 // TODO(Pawel)consider distinguish between non existing
2296 // object, and other errors
2297 messages::resourceNotFound(
2298 asyncResp->res, "VLAN Network Interface", ifaceId);
2299 }
2300 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002301 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002302};
2303
2304/**
2305 * VlanNetworkInterfaceCollection derived class for delivering
2306 * VLANNetworkInterface Collection Schema
2307 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002308class VlanNetworkInterfaceCollection : public Node
2309{
2310 public:
2311 template <typename CrowApp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002312 VlanNetworkInterfaceCollection(CrowApp& app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002313 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2314 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002315 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002316 entityPrivileges = {
2317 {boost::beast::http::verb::get, {{"Login"}}},
2318 {boost::beast::http::verb::head, {{"Login"}}},
2319 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2320 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2321 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2322 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002323 }
2324
Ed Tanous1abe55e2018-09-05 08:30:59 -07002325 private:
2326 /**
2327 * Functions triggers appropriate requests on DBus
2328 */
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002329 void doGet(crow::Response& res, const crow::Request& req,
2330 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002331 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002332 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002333 if (params.size() != 1)
2334 {
2335 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002336 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002337 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002338 }
2339
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002340 const std::string& rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002341
Ed Tanous4a0cb852018-10-15 07:55:04 -07002342 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002343 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002344 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002345 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002346 const bool& success,
2347 const boost::container::flat_set<std::string>& iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002348 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002349 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002350 messages::internalError(asyncResp->res);
2351 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002352 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002353
2354 if (iface_list.find(rootInterfaceName) == iface_list.end())
2355 {
2356 messages::resourceNotFound(asyncResp->res,
2357 "VLanNetworkInterfaceCollection",
2358 rootInterfaceName);
2359 return;
2360 }
2361
Ed Tanous0f74e642018-11-12 15:17:05 -08002362 asyncResp->res.jsonValue["@odata.type"] =
2363 "#VLanNetworkInterfaceCollection."
2364 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002365 asyncResp->res.jsonValue["Name"] =
2366 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002367
Jason M. Billsf12894f2018-10-09 12:45:45 -07002368 nlohmann::json iface_array = nlohmann::json::array();
2369
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002370 for (const std::string& iface_item : iface_list)
Jason M. Billsf12894f2018-10-09 12:45:45 -07002371 {
2372 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2373 {
2374 iface_array.push_back(
2375 {{"@odata.id",
2376 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2377 rootInterfaceName + "/VLANs/" + iface_item}});
2378 }
2379 }
2380
Jason M. Billsf12894f2018-10-09 12:45:45 -07002381 asyncResp->res.jsonValue["Members@odata.count"] =
2382 iface_array.size();
2383 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2384 asyncResp->res.jsonValue["@odata.id"] =
2385 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2386 rootInterfaceName + "/VLANs";
2387 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002388 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002389
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002390 void doPost(crow::Response& res, const crow::Request& req,
2391 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07002392 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002393 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002394 if (params.size() != 1)
2395 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002396 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002397 return;
2398 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002399 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002400 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002401 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2402 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002403 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002404 return;
2405 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002406 // Need both vlanId and vlanEnable to service this request
2407 if (!vlanId)
2408 {
2409 messages::propertyMissing(asyncResp->res, "VLANId");
2410 }
2411 if (!vlanEnable)
2412 {
2413 messages::propertyMissing(asyncResp->res, "VLANEnable");
2414 }
Ed Tanous271584a2019-07-09 16:24:22 -07002415 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002416 {
2417 return;
2418 }
2419
Gunnar Mills1214b7e2020-06-04 10:11:30 -05002420 const std::string& rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002421 auto callback = [asyncResp](const boost::system::error_code ec) {
2422 if (ec)
2423 {
2424 // TODO(ed) make more consistent error messages based on
2425 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002426 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002427 return;
2428 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002429 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002430 };
2431 crow::connections::systemBus->async_method_call(
2432 std::move(callback), "xyz.openbmc_project.Network",
2433 "/xyz/openbmc_project/network",
2434 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002435 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002436 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002437};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002438} // namespace redfish