19391bb9cSRapkiewicz, Pawel /* 29391bb9cSRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation 39391bb9cSRapkiewicz, Pawel // 49391bb9cSRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License"); 59391bb9cSRapkiewicz, Pawel // you may not use this file except in compliance with the License. 69391bb9cSRapkiewicz, Pawel // You may obtain a copy of the License at 79391bb9cSRapkiewicz, Pawel // 89391bb9cSRapkiewicz, Pawel // http://www.apache.org/licenses/LICENSE-2.0 99391bb9cSRapkiewicz, Pawel // 109391bb9cSRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software 119391bb9cSRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS, 129391bb9cSRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139391bb9cSRapkiewicz, Pawel // See the License for the specific language governing permissions and 149391bb9cSRapkiewicz, Pawel // limitations under the License. 159391bb9cSRapkiewicz, Pawel */ 169391bb9cSRapkiewicz, Pawel #pragma once 179391bb9cSRapkiewicz, Pawel 18033f1e4dSEd Tanous #include "utils/ip_utils.hpp" 19033f1e4dSEd Tanous 207e860f15SJohn Edward Broadbent #include <app.hpp> 2111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 2211ba3979SEd Tanous #include <boost/algorithm/string/split.hpp> 234a0cb85cSEd Tanous #include <boost/container/flat_set.hpp> 24179db1d7SKowalski, Kamil #include <dbus_singleton.hpp> 25168e20c1SEd Tanous #include <dbus_utility.hpp> 26588c3f0dSKowalski, Kamil #include <error_messages.hpp> 2745ca1b86SEd Tanous #include <query.hpp> 28ed398213SEd Tanous #include <registries/privilege_registry.hpp> 291214b7e7SGunnar Mills #include <utils/json_utils.hpp> 301214b7e7SGunnar Mills 31a24526dcSEd Tanous #include <optional> 32ab6554f1SJoshi-Mansi #include <regex> 339391bb9cSRapkiewicz, Pawel 341abe55efSEd Tanous namespace redfish 351abe55efSEd Tanous { 369391bb9cSRapkiewicz, Pawel 374a0cb85cSEd Tanous enum class LinkType 384a0cb85cSEd Tanous { 394a0cb85cSEd Tanous Local, 404a0cb85cSEd Tanous Global 414a0cb85cSEd Tanous }; 429391bb9cSRapkiewicz, Pawel 439391bb9cSRapkiewicz, Pawel /** 449391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 459391bb9cSRapkiewicz, Pawel */ 461abe55efSEd Tanous struct IPv4AddressData 471abe55efSEd Tanous { 48179db1d7SKowalski, Kamil std::string id; 494a0cb85cSEd Tanous std::string address; 504a0cb85cSEd Tanous std::string domain; 514a0cb85cSEd Tanous std::string gateway; 529391bb9cSRapkiewicz, Pawel std::string netmask; 539391bb9cSRapkiewicz, Pawel std::string origin; 544a0cb85cSEd Tanous LinkType linktype; 5501c6e858SSunitha Harish bool isActive; 564a0cb85cSEd Tanous 571abe55efSEd Tanous bool operator<(const IPv4AddressData& obj) const 581abe55efSEd Tanous { 594a0cb85cSEd Tanous return id < obj.id; 601abe55efSEd Tanous } 619391bb9cSRapkiewicz, Pawel }; 629391bb9cSRapkiewicz, Pawel 639391bb9cSRapkiewicz, Pawel /** 64e48c0fc5SRavi Teja * Structure for keeping IPv6 data required by Redfish 65e48c0fc5SRavi Teja */ 66e48c0fc5SRavi Teja struct IPv6AddressData 67e48c0fc5SRavi Teja { 68e48c0fc5SRavi Teja std::string id; 69e48c0fc5SRavi Teja std::string address; 70e48c0fc5SRavi Teja std::string origin; 71e48c0fc5SRavi Teja uint8_t prefixLength; 72e48c0fc5SRavi Teja 73e48c0fc5SRavi Teja bool operator<(const IPv6AddressData& obj) const 74e48c0fc5SRavi Teja { 75e48c0fc5SRavi Teja return id < obj.id; 76e48c0fc5SRavi Teja } 77e48c0fc5SRavi Teja }; 78e48c0fc5SRavi Teja /** 799391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 809391bb9cSRapkiewicz, Pawel * available from DBus 819391bb9cSRapkiewicz, Pawel */ 821abe55efSEd Tanous struct EthernetInterfaceData 831abe55efSEd Tanous { 844a0cb85cSEd Tanous uint32_t speed; 8535fb5311STejas Patil size_t mtuSize; 8682695a5bSJiaqing Zhao bool autoNeg; 8782695a5bSJiaqing Zhao bool dnsEnabled; 8882695a5bSJiaqing Zhao bool ntpEnabled; 8982695a5bSJiaqing Zhao bool hostNameEnabled; 90aa05fb27SJohnathan Mantey bool linkUp; 91eeedda23SJohnathan Mantey bool nicEnabled; 9282695a5bSJiaqing Zhao std::string dhcpEnabled; 931f8c7b5dSJohnathan Mantey std::string operatingMode; 9482695a5bSJiaqing Zhao std::string hostName; 9582695a5bSJiaqing Zhao std::string defaultGateway; 9682695a5bSJiaqing Zhao std::string ipv6DefaultGateway; 9782695a5bSJiaqing Zhao std::string macAddress; 9817e22024SJiaqing Zhao std::optional<uint32_t> vlanId; 990f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 1000f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 101d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 1029391bb9cSRapkiewicz, Pawel }; 1039391bb9cSRapkiewicz, Pawel 1041f8c7b5dSJohnathan Mantey struct DHCPParameters 1051f8c7b5dSJohnathan Mantey { 1061f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 10782695a5bSJiaqing Zhao std::optional<bool> useDnsServers; 10882695a5bSJiaqing Zhao std::optional<bool> useNtpServers; 10982695a5bSJiaqing Zhao std::optional<bool> useDomainName; 1101f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1111f8c7b5dSJohnathan Mantey }; 1121f8c7b5dSJohnathan Mantey 1139391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1149391bb9cSRapkiewicz, Pawel // into full dot notation 1151abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1161abe55efSEd Tanous { 1179391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1189391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1199391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1209391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1219391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1229391bb9cSRapkiewicz, Pawel return netmask; 1239391bb9cSRapkiewicz, Pawel } 1249391bb9cSRapkiewicz, Pawel 12582695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP, 1261f8c7b5dSJohnathan Mantey bool isIPv4) 1271f8c7b5dSJohnathan Mantey { 1281f8c7b5dSJohnathan Mantey if (isIPv4) 1291f8c7b5dSJohnathan Mantey { 1301f8c7b5dSJohnathan Mantey return ( 1311f8c7b5dSJohnathan Mantey (inputDHCP == 1321f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1331f8c7b5dSJohnathan Mantey (inputDHCP == 1341f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1351f8c7b5dSJohnathan Mantey } 1361f8c7b5dSJohnathan Mantey return ((inputDHCP == 1371f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1381f8c7b5dSJohnathan Mantey (inputDHCP == 1391f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1401f8c7b5dSJohnathan Mantey } 1411f8c7b5dSJohnathan Mantey 1422c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1431f8c7b5dSJohnathan Mantey { 1441f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1451f8c7b5dSJohnathan Mantey { 1461f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1471f8c7b5dSJohnathan Mantey } 1483174e4dfSEd Tanous if (isIPv4) 1491f8c7b5dSJohnathan Mantey { 1501f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1511f8c7b5dSJohnathan Mantey } 1523174e4dfSEd Tanous if (isIPv6) 1531f8c7b5dSJohnathan Mantey { 1541f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1551f8c7b5dSJohnathan Mantey } 1561f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1571f8c7b5dSJohnathan Mantey } 1581f8c7b5dSJohnathan Mantey 1594a0cb85cSEd Tanous inline std::string 1604a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1614a0cb85cSEd Tanous bool isIPv4) 1621abe55efSEd Tanous { 1634a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1641abe55efSEd Tanous { 1654a0cb85cSEd Tanous return "Static"; 1669391bb9cSRapkiewicz, Pawel } 1674a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1681abe55efSEd Tanous { 1694a0cb85cSEd Tanous if (isIPv4) 1701abe55efSEd Tanous { 1714a0cb85cSEd Tanous return "IPv4LinkLocal"; 1721abe55efSEd Tanous } 1734a0cb85cSEd Tanous return "LinkLocal"; 1749391bb9cSRapkiewicz, Pawel } 1754a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1761abe55efSEd Tanous { 1774a0cb85cSEd Tanous if (isIPv4) 1784a0cb85cSEd Tanous { 1794a0cb85cSEd Tanous return "DHCP"; 1804a0cb85cSEd Tanous } 1814a0cb85cSEd Tanous return "DHCPv6"; 1824a0cb85cSEd Tanous } 1834a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1844a0cb85cSEd Tanous { 1854a0cb85cSEd Tanous return "SLAAC"; 1864a0cb85cSEd Tanous } 1874a0cb85cSEd Tanous return ""; 1884a0cb85cSEd Tanous } 1894a0cb85cSEd Tanous 19002cad96eSEd Tanous inline bool extractEthernetInterfaceData( 19102cad96eSEd Tanous const std::string& ethifaceId, 19202cad96eSEd Tanous const dbus::utility::ManagedObjectType& dbusData, 1934a0cb85cSEd Tanous EthernetInterfaceData& ethData) 1944a0cb85cSEd Tanous { 1954c9afe43SEd Tanous bool idFound = false; 19602cad96eSEd Tanous for (const auto& objpath : dbusData) 1974a0cb85cSEd Tanous { 19802cad96eSEd Tanous for (const auto& ifacePair : objpath.second) 1994a0cb85cSEd Tanous { 20081ce609eSEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 201029573d4SEd Tanous { 2024c9afe43SEd Tanous idFound = true; 2034a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2044a0cb85cSEd Tanous { 2054a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2064a0cb85cSEd Tanous { 2074a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2084a0cb85cSEd Tanous { 2094a0cb85cSEd Tanous const std::string* mac = 210abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2114a0cb85cSEd Tanous if (mac != nullptr) 2124a0cb85cSEd Tanous { 21382695a5bSJiaqing Zhao ethData.macAddress = *mac; 2144a0cb85cSEd Tanous } 2154a0cb85cSEd Tanous } 2164a0cb85cSEd Tanous } 2174a0cb85cSEd Tanous } 2184a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2194a0cb85cSEd Tanous { 2204a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2214a0cb85cSEd Tanous { 2224a0cb85cSEd Tanous if (propertyPair.first == "Id") 2234a0cb85cSEd Tanous { 2241b6b96c5SEd Tanous const uint32_t* id = 225abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2264a0cb85cSEd Tanous if (id != nullptr) 2274a0cb85cSEd Tanous { 22817e22024SJiaqing Zhao ethData.vlanId = *id; 2294a0cb85cSEd Tanous } 2304a0cb85cSEd Tanous } 2314a0cb85cSEd Tanous } 2324a0cb85cSEd Tanous } 2334a0cb85cSEd Tanous else if (ifacePair.first == 2344a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2354a0cb85cSEd Tanous { 2364a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2374a0cb85cSEd Tanous { 2384a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2394a0cb85cSEd Tanous { 2402c70f800SEd Tanous const bool* autoNeg = 241abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2422c70f800SEd Tanous if (autoNeg != nullptr) 2434a0cb85cSEd Tanous { 24482695a5bSJiaqing Zhao ethData.autoNeg = *autoNeg; 2454a0cb85cSEd Tanous } 2464a0cb85cSEd Tanous } 2474a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2484a0cb85cSEd Tanous { 2494a0cb85cSEd Tanous const uint32_t* speed = 250abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2514a0cb85cSEd Tanous if (speed != nullptr) 2524a0cb85cSEd Tanous { 2534a0cb85cSEd Tanous ethData.speed = *speed; 2544a0cb85cSEd Tanous } 2554a0cb85cSEd Tanous } 25635fb5311STejas Patil else if (propertyPair.first == "MTU") 25735fb5311STejas Patil { 25835fb5311STejas Patil const uint32_t* mtuSize = 25935fb5311STejas Patil std::get_if<uint32_t>(&propertyPair.second); 26035fb5311STejas Patil if (mtuSize != nullptr) 26135fb5311STejas Patil { 26235fb5311STejas Patil ethData.mtuSize = *mtuSize; 26335fb5311STejas Patil } 26435fb5311STejas Patil } 265aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 266aa05fb27SJohnathan Mantey { 267aa05fb27SJohnathan Mantey const bool* linkUp = 268aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 269aa05fb27SJohnathan Mantey if (linkUp != nullptr) 270aa05fb27SJohnathan Mantey { 271aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 272aa05fb27SJohnathan Mantey } 273aa05fb27SJohnathan Mantey } 274eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 275eeedda23SJohnathan Mantey { 276eeedda23SJohnathan Mantey const bool* nicEnabled = 277eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 278eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 279eeedda23SJohnathan Mantey { 280eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 281eeedda23SJohnathan Mantey } 282eeedda23SJohnathan Mantey } 283f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 284029573d4SEd Tanous { 285029573d4SEd Tanous const std::vector<std::string>* nameservers = 2868d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 287029573d4SEd Tanous &propertyPair.second); 288029573d4SEd Tanous if (nameservers != nullptr) 289029573d4SEd Tanous { 290f23b7296SEd Tanous ethData.nameServers = *nameservers; 2910f6efdc1Smanojkiran.eda@gmail.com } 2920f6efdc1Smanojkiran.eda@gmail.com } 2930f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 2940f6efdc1Smanojkiran.eda@gmail.com { 2950f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 2968d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 2970f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 2980f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 2990f6efdc1Smanojkiran.eda@gmail.com { 300f23b7296SEd Tanous ethData.staticNameServers = *staticNameServers; 3014a0cb85cSEd Tanous } 3024a0cb85cSEd Tanous } 3032a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3042a133282Smanojkiraneda { 3052c70f800SEd Tanous const std::string* dhcpEnabled = 3061f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 3072c70f800SEd Tanous if (dhcpEnabled != nullptr) 3082a133282Smanojkiraneda { 30982695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcpEnabled; 3102a133282Smanojkiraneda } 3112a133282Smanojkiraneda } 312d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 313d24bfc7aSJennifer Lee { 314d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3158d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 316d24bfc7aSJennifer Lee &propertyPair.second); 317d24bfc7aSJennifer Lee if (domainNames != nullptr) 318d24bfc7aSJennifer Lee { 319f23b7296SEd Tanous ethData.domainnames = *domainNames; 320d24bfc7aSJennifer Lee } 321d24bfc7aSJennifer Lee } 3229010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway") 3239010ec2eSRavi Teja { 3249010ec2eSRavi Teja const std::string* defaultGateway = 3259010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3269010ec2eSRavi Teja if (defaultGateway != nullptr) 3279010ec2eSRavi Teja { 3289010ec2eSRavi Teja std::string defaultGatewayStr = *defaultGateway; 3299010ec2eSRavi Teja if (defaultGatewayStr.empty()) 3309010ec2eSRavi Teja { 33182695a5bSJiaqing Zhao ethData.defaultGateway = "0.0.0.0"; 3329010ec2eSRavi Teja } 3339010ec2eSRavi Teja else 3349010ec2eSRavi Teja { 33582695a5bSJiaqing Zhao ethData.defaultGateway = defaultGatewayStr; 3369010ec2eSRavi Teja } 3379010ec2eSRavi Teja } 3389010ec2eSRavi Teja } 3399010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway6") 3409010ec2eSRavi Teja { 3419010ec2eSRavi Teja const std::string* defaultGateway6 = 3429010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3439010ec2eSRavi Teja if (defaultGateway6 != nullptr) 3449010ec2eSRavi Teja { 3459010ec2eSRavi Teja std::string defaultGateway6Str = 3469010ec2eSRavi Teja *defaultGateway6; 3479010ec2eSRavi Teja if (defaultGateway6Str.empty()) 3489010ec2eSRavi Teja { 34982695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3509010ec2eSRavi Teja "0:0:0:0:0:0:0:0"; 3519010ec2eSRavi Teja } 3529010ec2eSRavi Teja else 3539010ec2eSRavi Teja { 35482695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3559010ec2eSRavi Teja defaultGateway6Str; 3569010ec2eSRavi Teja } 3579010ec2eSRavi Teja } 3589010ec2eSRavi Teja } 359029573d4SEd Tanous } 360029573d4SEd Tanous } 361029573d4SEd Tanous } 3621f8c7b5dSJohnathan Mantey 3631f8c7b5dSJohnathan Mantey if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 3641f8c7b5dSJohnathan Mantey { 3651f8c7b5dSJohnathan Mantey if (ifacePair.first == 3661f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3671f8c7b5dSJohnathan Mantey { 3681f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3691f8c7b5dSJohnathan Mantey { 3701f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3711f8c7b5dSJohnathan Mantey { 3722c70f800SEd Tanous const bool* dnsEnabled = 3731f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3742c70f800SEd Tanous if (dnsEnabled != nullptr) 3751f8c7b5dSJohnathan Mantey { 37682695a5bSJiaqing Zhao ethData.dnsEnabled = *dnsEnabled; 3771f8c7b5dSJohnathan Mantey } 3781f8c7b5dSJohnathan Mantey } 3791f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3801f8c7b5dSJohnathan Mantey { 3812c70f800SEd Tanous const bool* ntpEnabled = 3821f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3832c70f800SEd Tanous if (ntpEnabled != nullptr) 3841f8c7b5dSJohnathan Mantey { 38582695a5bSJiaqing Zhao ethData.ntpEnabled = *ntpEnabled; 3861f8c7b5dSJohnathan Mantey } 3871f8c7b5dSJohnathan Mantey } 3881f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3891f8c7b5dSJohnathan Mantey { 3902c70f800SEd Tanous const bool* hostNameEnabled = 3911f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3922c70f800SEd Tanous if (hostNameEnabled != nullptr) 3931f8c7b5dSJohnathan Mantey { 39482695a5bSJiaqing Zhao ethData.hostNameEnabled = *hostNameEnabled; 3951f8c7b5dSJohnathan Mantey } 3961f8c7b5dSJohnathan Mantey } 3971f8c7b5dSJohnathan Mantey } 3981f8c7b5dSJohnathan Mantey } 3991f8c7b5dSJohnathan Mantey } 400029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 401029573d4SEd Tanous // to check eth number 402029573d4SEd Tanous if (ifacePair.first == 4034a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 4044a0cb85cSEd Tanous { 4054a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 4064a0cb85cSEd Tanous { 4074a0cb85cSEd Tanous if (propertyPair.first == "HostName") 4084a0cb85cSEd Tanous { 4094a0cb85cSEd Tanous const std::string* hostname = 4108d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4114a0cb85cSEd Tanous if (hostname != nullptr) 4124a0cb85cSEd Tanous { 41382695a5bSJiaqing Zhao ethData.hostName = *hostname; 4144a0cb85cSEd Tanous } 4154a0cb85cSEd Tanous } 4164a0cb85cSEd Tanous } 4174a0cb85cSEd Tanous } 4184a0cb85cSEd Tanous } 4194a0cb85cSEd Tanous } 4204c9afe43SEd Tanous return idFound; 4214a0cb85cSEd Tanous } 4224a0cb85cSEd Tanous 423e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 42401784826SJohnathan Mantey inline void 42581ce609eSEd Tanous extractIPV6Data(const std::string& ethifaceId, 426711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 42781ce609eSEd Tanous boost::container::flat_set<IPv6AddressData>& ipv6Config) 428e48c0fc5SRavi Teja { 429e48c0fc5SRavi Teja const std::string ipv6PathStart = 43081ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 431e48c0fc5SRavi Teja 432e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 433e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 43481ce609eSEd Tanous for (const auto& objpath : dbusData) 435e48c0fc5SRavi Teja { 436e48c0fc5SRavi Teja // Check if proper pattern for object path appears 43711ba3979SEd Tanous if (objpath.first.str.starts_with(ipv6PathStart)) 438e48c0fc5SRavi Teja { 4399eb808c1SEd Tanous for (const auto& interface : objpath.second) 440e48c0fc5SRavi Teja { 441e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 442e48c0fc5SRavi Teja { 443e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 444e48c0fc5SRavi Teja // appropriate 445e48c0fc5SRavi Teja std::pair< 446e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 447e48c0fc5SRavi Teja bool> 44881ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4492c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4502c70f800SEd Tanous ipv6Address.id = 451271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 4529eb808c1SEd Tanous for (const auto& property : interface.second) 453e48c0fc5SRavi Teja { 454e48c0fc5SRavi Teja if (property.first == "Address") 455e48c0fc5SRavi Teja { 456e48c0fc5SRavi Teja const std::string* address = 457e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 458e48c0fc5SRavi Teja if (address != nullptr) 459e48c0fc5SRavi Teja { 4602c70f800SEd Tanous ipv6Address.address = *address; 461e48c0fc5SRavi Teja } 462e48c0fc5SRavi Teja } 463e48c0fc5SRavi Teja else if (property.first == "Origin") 464e48c0fc5SRavi Teja { 465e48c0fc5SRavi Teja const std::string* origin = 466e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 467e48c0fc5SRavi Teja if (origin != nullptr) 468e48c0fc5SRavi Teja { 4692c70f800SEd Tanous ipv6Address.origin = 470e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 471e48c0fc5SRavi Teja false); 472e48c0fc5SRavi Teja } 473e48c0fc5SRavi Teja } 474e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 475e48c0fc5SRavi Teja { 476e48c0fc5SRavi Teja const uint8_t* prefix = 477e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 478e48c0fc5SRavi Teja if (prefix != nullptr) 479e48c0fc5SRavi Teja { 4802c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 481e48c0fc5SRavi Teja } 482e48c0fc5SRavi Teja } 483889ff694SAsmitha Karunanithi else if (property.first == "Type" || 484889ff694SAsmitha Karunanithi property.first == "Gateway") 485889ff694SAsmitha Karunanithi { 486889ff694SAsmitha Karunanithi // Type & Gateway is not used 487889ff694SAsmitha Karunanithi } 488e48c0fc5SRavi Teja else 489e48c0fc5SRavi Teja { 490e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 491e48c0fc5SRavi Teja << "Got extra property: " << property.first 492e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 493e48c0fc5SRavi Teja } 494e48c0fc5SRavi Teja } 495e48c0fc5SRavi Teja } 496e48c0fc5SRavi Teja } 497e48c0fc5SRavi Teja } 498e48c0fc5SRavi Teja } 499e48c0fc5SRavi Teja } 500e48c0fc5SRavi Teja 5014a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 50201784826SJohnathan Mantey inline void 50381ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 504711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 50581ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5064a0cb85cSEd Tanous { 5074a0cb85cSEd Tanous const std::string ipv4PathStart = 50881ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 5094a0cb85cSEd Tanous 5104a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5114a0cb85cSEd Tanous // single ethernet interface, loop over all of them 51281ce609eSEd Tanous for (const auto& objpath : dbusData) 5134a0cb85cSEd Tanous { 5144a0cb85cSEd Tanous // Check if proper pattern for object path appears 51511ba3979SEd Tanous if (objpath.first.str.starts_with(ipv4PathStart)) 5164a0cb85cSEd Tanous { 5179eb808c1SEd Tanous for (const auto& interface : objpath.second) 5184a0cb85cSEd Tanous { 5194a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5204a0cb85cSEd Tanous { 5214a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5224a0cb85cSEd Tanous // appropriate 5234a0cb85cSEd Tanous std::pair< 5244a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5254a0cb85cSEd Tanous bool> 52681ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5272c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5282c70f800SEd Tanous ipv4Address.id = 529271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5309eb808c1SEd Tanous for (const auto& property : interface.second) 5314a0cb85cSEd Tanous { 5324a0cb85cSEd Tanous if (property.first == "Address") 5334a0cb85cSEd Tanous { 5344a0cb85cSEd Tanous const std::string* address = 535abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5364a0cb85cSEd Tanous if (address != nullptr) 5374a0cb85cSEd Tanous { 5382c70f800SEd Tanous ipv4Address.address = *address; 5394a0cb85cSEd Tanous } 5404a0cb85cSEd Tanous } 5414a0cb85cSEd Tanous else if (property.first == "Origin") 5424a0cb85cSEd Tanous { 5434a0cb85cSEd Tanous const std::string* origin = 544abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5454a0cb85cSEd Tanous if (origin != nullptr) 5464a0cb85cSEd Tanous { 5472c70f800SEd Tanous ipv4Address.origin = 5484a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5494a0cb85cSEd Tanous true); 5504a0cb85cSEd Tanous } 5514a0cb85cSEd Tanous } 5524a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5534a0cb85cSEd Tanous { 5544a0cb85cSEd Tanous const uint8_t* mask = 555abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5564a0cb85cSEd Tanous if (mask != nullptr) 5574a0cb85cSEd Tanous { 5584a0cb85cSEd Tanous // convert it to the string 5592c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5604a0cb85cSEd Tanous } 5614a0cb85cSEd Tanous } 562889ff694SAsmitha Karunanithi else if (property.first == "Type" || 563889ff694SAsmitha Karunanithi property.first == "Gateway") 564889ff694SAsmitha Karunanithi { 565889ff694SAsmitha Karunanithi // Type & Gateway is not used 566889ff694SAsmitha Karunanithi } 5674a0cb85cSEd Tanous else 5684a0cb85cSEd Tanous { 5694a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5704a0cb85cSEd Tanous << "Got extra property: " << property.first 5714a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5724a0cb85cSEd Tanous } 5734a0cb85cSEd Tanous } 5744a0cb85cSEd Tanous // Check if given address is local, or global 5752c70f800SEd Tanous ipv4Address.linktype = 57611ba3979SEd Tanous ipv4Address.address.starts_with("169.254.") 57718659d10SJohnathan Mantey ? LinkType::Local 57818659d10SJohnathan Mantey : LinkType::Global; 5794a0cb85cSEd Tanous } 5804a0cb85cSEd Tanous } 5814a0cb85cSEd Tanous } 5824a0cb85cSEd Tanous } 5834a0cb85cSEd Tanous } 584588c3f0dSKowalski, Kamil 585588c3f0dSKowalski, Kamil /** 58601784826SJohnathan Mantey * @brief Deletes given IPv4 interface 587179db1d7SKowalski, Kamil * 588179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 589179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 590179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 591179db1d7SKowalski, Kamil * 592179db1d7SKowalski, Kamil * @return None 593179db1d7SKowalski, Kamil */ 5944a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 5958d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 5961abe55efSEd Tanous { 59755c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 598286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 5991abe55efSEd Tanous if (ec) 6001abe55efSEd Tanous { 601a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6021abe55efSEd Tanous } 603179db1d7SKowalski, Kamil }, 604179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 605179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 606179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 607179db1d7SKowalski, Kamil } 608179db1d7SKowalski, Kamil 609244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 610244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 611244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6129010ec2eSRavi Teja { 6139010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6149010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 6159010ec2eSRavi Teja if (ec) 6169010ec2eSRavi Teja { 6179010ec2eSRavi Teja messages::internalError(asyncResp->res); 6189010ec2eSRavi Teja return; 6199010ec2eSRavi Teja } 6209010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 6219010ec2eSRavi Teja }, 6229010ec2eSRavi Teja "xyz.openbmc_project.Network", 6239010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 6249010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 6259010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 626168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 6279010ec2eSRavi Teja } 628179db1d7SKowalski, Kamil /** 62901784826SJohnathan Mantey * @brief Creates a static IPv4 entry 630179db1d7SKowalski, Kamil * 63101784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 63201784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 63301784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 63401784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 635179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 636179db1d7SKowalski, Kamil * 637179db1d7SKowalski, Kamil * @return None 638179db1d7SKowalski, Kamil */ 639cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 640cb13a392SEd Tanous const std::string& gateway, const std::string& address, 6418d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6421abe55efSEd Tanous { 643002d39b4SEd Tanous auto createIpHandler = 644002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec) { 6451abe55efSEd Tanous if (ec) 6461abe55efSEd Tanous { 647a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6489010ec2eSRavi Teja return; 649179db1d7SKowalski, Kamil } 6509010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 6519010ec2eSRavi Teja }; 6529010ec2eSRavi Teja 6539010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6549010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 655179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 656179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 65701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 658179db1d7SKowalski, Kamil gateway); 659179db1d7SKowalski, Kamil } 660e48c0fc5SRavi Teja 661e48c0fc5SRavi Teja /** 66201784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 66301784826SJohnathan Mantey * static IPv4 entry 66401784826SJohnathan Mantey * 66501784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 66601784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 66701784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 66801784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 66901784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 67001784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 67101784826SJohnathan Mantey * 67201784826SJohnathan Mantey * @return None 67301784826SJohnathan Mantey */ 6748d1b46d7Szhanghch05 inline void 6758d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 6768d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 67701784826SJohnathan Mantey const std::string& address, 6788d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 67901784826SJohnathan Mantey { 68001784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 68101784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 68201784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 68301784826SJohnathan Mantey if (ec) 68401784826SJohnathan Mantey { 68501784826SJohnathan Mantey messages::internalError(asyncResp->res); 6869010ec2eSRavi Teja return; 68701784826SJohnathan Mantey } 6889010ec2eSRavi Teja 68901784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 690002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) { 69123a21a1cSEd Tanous if (ec2) 69201784826SJohnathan Mantey { 69301784826SJohnathan Mantey messages::internalError(asyncResp->res); 6949010ec2eSRavi Teja return; 69501784826SJohnathan Mantey } 6969010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 69701784826SJohnathan Mantey }, 69801784826SJohnathan Mantey "xyz.openbmc_project.Network", 69901784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 70001784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 70101784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 70201784826SJohnathan Mantey prefixLength, gateway); 70301784826SJohnathan Mantey }, 70401784826SJohnathan Mantey "xyz.openbmc_project.Network", 70501784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 70601784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 70701784826SJohnathan Mantey } 70801784826SJohnathan Mantey 70901784826SJohnathan Mantey /** 710e48c0fc5SRavi Teja * @brief Deletes given IPv6 711e48c0fc5SRavi Teja * 712e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 713e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 714e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 715e48c0fc5SRavi Teja * 716e48c0fc5SRavi Teja * @return None 717e48c0fc5SRavi Teja */ 718e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 7198d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 720e48c0fc5SRavi Teja { 721e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 722286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 723e48c0fc5SRavi Teja if (ec) 724e48c0fc5SRavi Teja { 725e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 726e48c0fc5SRavi Teja } 727e48c0fc5SRavi Teja }, 728e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 729e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 730e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 731e48c0fc5SRavi Teja } 732e48c0fc5SRavi Teja 733e48c0fc5SRavi Teja /** 73401784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 73501784826SJohnathan Mantey * static IPv6 entry 73601784826SJohnathan Mantey * 73701784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 73801784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 73901784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 74001784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 74101784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 74201784826SJohnathan Mantey * 74301784826SJohnathan Mantey * @return None 74401784826SJohnathan Mantey */ 7458d1b46d7Szhanghch05 inline void 7468d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 7478d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 7488d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 74901784826SJohnathan Mantey { 75001784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 75101784826SJohnathan Mantey [asyncResp, ifaceId, address, 75201784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 75301784826SJohnathan Mantey if (ec) 75401784826SJohnathan Mantey { 75501784826SJohnathan Mantey messages::internalError(asyncResp->res); 75601784826SJohnathan Mantey } 75701784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 75823a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 75923a21a1cSEd Tanous if (ec2) 76001784826SJohnathan Mantey { 76101784826SJohnathan Mantey messages::internalError(asyncResp->res); 76201784826SJohnathan Mantey } 76301784826SJohnathan Mantey }, 76401784826SJohnathan Mantey "xyz.openbmc_project.Network", 76501784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 76601784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 76701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 76801784826SJohnathan Mantey prefixLength, ""); 76901784826SJohnathan Mantey }, 77001784826SJohnathan Mantey "xyz.openbmc_project.Network", 77101784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 77201784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 77301784826SJohnathan Mantey } 77401784826SJohnathan Mantey 77501784826SJohnathan Mantey /** 776e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 777e48c0fc5SRavi Teja * 778e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 779e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 780e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 781e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 782e48c0fc5SRavi Teja * 783e48c0fc5SRavi Teja * @return None 784e48c0fc5SRavi Teja */ 78501784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 78601784826SJohnathan Mantey const std::string& address, 7878d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 788e48c0fc5SRavi Teja { 789e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 790e48c0fc5SRavi Teja if (ec) 791e48c0fc5SRavi Teja { 792e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 793e48c0fc5SRavi Teja } 794e48c0fc5SRavi Teja }; 795e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 7964e0453b1SGunnar Mills // does not have associated gateway property 797e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 798e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 799e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 800e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 801e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 802e48c0fc5SRavi Teja ""); 803e48c0fc5SRavi Teja } 804e48c0fc5SRavi Teja 805179db1d7SKowalski, Kamil /** 806179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 807179db1d7SKowalski, Kamil * Object 808179db1d7SKowalski, Kamil * from EntityManager Network Manager 8094a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 810179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 811179db1d7SKowalski, Kamil * into JSON 812179db1d7SKowalski, Kamil */ 813179db1d7SKowalski, Kamil template <typename CallbackFunc> 81481ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 8151abe55efSEd Tanous CallbackFunc&& callback) 8161abe55efSEd Tanous { 81755c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 818f94c4ecfSEd Tanous [ethifaceId{std::string{ethifaceId}}, 819f94c4ecfSEd Tanous callback{std::forward<CallbackFunc>(callback)}]( 82081ce609eSEd Tanous const boost::system::error_code errorCode, 82102cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 82255c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 8234a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 824e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 825179db1d7SKowalski, Kamil 82681ce609eSEd Tanous if (errorCode) 8271abe55efSEd Tanous { 82801784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 829179db1d7SKowalski, Kamil return; 830179db1d7SKowalski, Kamil } 831179db1d7SKowalski, Kamil 832002d39b4SEd Tanous bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData); 8334c9afe43SEd Tanous if (!found) 8344c9afe43SEd Tanous { 83501784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 8364c9afe43SEd Tanous return; 8374c9afe43SEd Tanous } 8384c9afe43SEd Tanous 8392c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 840179db1d7SKowalski, Kamil // Fix global GW 8411abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 8421abe55efSEd Tanous { 843c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 844c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 8459010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 8461abe55efSEd Tanous { 84782695a5bSJiaqing Zhao ipv4.gateway = ethData.defaultGateway; 848179db1d7SKowalski, Kamil } 849179db1d7SKowalski, Kamil } 850179db1d7SKowalski, Kamil 8512c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 8524e0453b1SGunnar Mills // Finally make a callback with useful data 85301784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 854179db1d7SKowalski, Kamil }, 855179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 856179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 857271584abSEd Tanous } 858179db1d7SKowalski, Kamil 859179db1d7SKowalski, Kamil /** 8609391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 8619391bb9cSRapkiewicz, Pawel * Manager 8621abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 8631abe55efSEd Tanous * into JSON. 8649391bb9cSRapkiewicz, Pawel */ 8659391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 8661abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 8671abe55efSEd Tanous { 86855c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 869f94c4ecfSEd Tanous [callback{std::forward<CallbackFunc>(callback)}]( 87081ce609eSEd Tanous const boost::system::error_code errorCode, 871711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 8721abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 8731abe55efSEd Tanous // ethernet interfaces 8742c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 8752c70f800SEd Tanous ifaceList.reserve(resp.size()); 87681ce609eSEd Tanous if (errorCode) 8771abe55efSEd Tanous { 8782c70f800SEd Tanous callback(false, ifaceList); 8799391bb9cSRapkiewicz, Pawel return; 8809391bb9cSRapkiewicz, Pawel } 8819391bb9cSRapkiewicz, Pawel 8829391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 8834a0cb85cSEd Tanous for (const auto& objpath : resp) 8841abe55efSEd Tanous { 8859391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 8864a0cb85cSEd Tanous for (const auto& interface : objpath.second) 8871abe55efSEd Tanous { 8881abe55efSEd Tanous // If interface is 8894a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 8904a0cb85cSEd Tanous // what we're looking for. 8919391bb9cSRapkiewicz, Pawel if (interface.first == 8921abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 8931abe55efSEd Tanous { 8942dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 8952dfd18efSEd Tanous if (ifaceId.empty()) 8961abe55efSEd Tanous { 8972dfd18efSEd Tanous continue; 8989391bb9cSRapkiewicz, Pawel } 8992dfd18efSEd Tanous // and put it into output vector. 9002dfd18efSEd Tanous ifaceList.emplace(ifaceId); 9019391bb9cSRapkiewicz, Pawel } 9029391bb9cSRapkiewicz, Pawel } 9039391bb9cSRapkiewicz, Pawel } 904a434f2bdSEd Tanous // Finally make a callback with useful data 9052c70f800SEd Tanous callback(true, ifaceList); 9069391bb9cSRapkiewicz, Pawel }, 907aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 908aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 909271584abSEd Tanous } 9109391bb9cSRapkiewicz, Pawel 9114f48d5f6SEd Tanous inline void 9124f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 9138d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 9141abe55efSEd Tanous { 915ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 916ab6554f1SJoshi-Mansi if (hostname.length() > 255) 917ab6554f1SJoshi-Mansi { 918ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 919ab6554f1SJoshi-Mansi "HostName"); 920ab6554f1SJoshi-Mansi return; 921ab6554f1SJoshi-Mansi } 922bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 923bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 9244a0cb85cSEd Tanous if (ec) 9254a0cb85cSEd Tanous { 926a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 9271abe55efSEd Tanous } 928bc0bd6e0SEd Tanous }, 929bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 930bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 931bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 932168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 933588c3f0dSKowalski, Kamil } 934588c3f0dSKowalski, Kamil 9354f48d5f6SEd Tanous inline void 93635fb5311STejas Patil handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, 93735fb5311STejas Patil const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 93835fb5311STejas Patil { 93935fb5311STejas Patil sdbusplus::message::object_path objPath = 94035fb5311STejas Patil "/xyz/openbmc_project/network/" + ifaceId; 94135fb5311STejas Patil crow::connections::systemBus->async_method_call( 94235fb5311STejas Patil [asyncResp](const boost::system::error_code ec) { 94335fb5311STejas Patil if (ec) 94435fb5311STejas Patil { 94535fb5311STejas Patil messages::internalError(asyncResp->res); 94635fb5311STejas Patil } 94735fb5311STejas Patil }, 94835fb5311STejas Patil "xyz.openbmc_project.Network", objPath, 94935fb5311STejas Patil "org.freedesktop.DBus.Properties", "Set", 95035fb5311STejas Patil "xyz.openbmc_project.Network.EthernetInterface", "MTU", 95135fb5311STejas Patil std::variant<size_t>(mtuSize)); 95235fb5311STejas Patil } 95335fb5311STejas Patil 95435fb5311STejas Patil inline void 9554f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 956bf648f77SEd Tanous const std::string& domainname, 9578d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 958ab6554f1SJoshi-Mansi { 959ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 960ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 961ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 962ab6554f1SJoshi-Mansi if (ec) 963ab6554f1SJoshi-Mansi { 964ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 965ab6554f1SJoshi-Mansi } 966ab6554f1SJoshi-Mansi }, 967ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 968ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 969ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 970ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 971168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 972ab6554f1SJoshi-Mansi } 973ab6554f1SJoshi-Mansi 9744f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 975bf648f77SEd Tanous { 976bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 977bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 978bf648f77SEd Tanous { 979bf648f77SEd Tanous return false; 980bf648f77SEd Tanous } 981bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 982bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 983bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 984bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 985bf648f77SEd Tanous const std::regex pattern( 986bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 987bf648f77SEd Tanous 988bf648f77SEd Tanous return std::regex_match(hostname, pattern); 989bf648f77SEd Tanous } 990bf648f77SEd Tanous 9914f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 992bf648f77SEd Tanous { 993bf648f77SEd Tanous // Can have multiple subdomains 994bf648f77SEd Tanous // Top Level Domain's min length is 2 character 9950fda0f12SGeorge Liu const std::regex pattern( 9960fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 997bf648f77SEd Tanous 998bf648f77SEd Tanous return std::regex_match(domainname, pattern); 999bf648f77SEd Tanous } 1000bf648f77SEd Tanous 10014f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 10028d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1003ab6554f1SJoshi-Mansi { 1004ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1005ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1006ab6554f1SJoshi-Mansi { 1007ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1008ab6554f1SJoshi-Mansi return; 1009ab6554f1SJoshi-Mansi } 1010ab6554f1SJoshi-Mansi 1011ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1012ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1013ab6554f1SJoshi-Mansi { 1014ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1015ab6554f1SJoshi-Mansi return; 1016ab6554f1SJoshi-Mansi } 1017ab6554f1SJoshi-Mansi 1018ab6554f1SJoshi-Mansi std::string hostname; 1019ab6554f1SJoshi-Mansi std::string domainname; 1020ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1021ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1022ab6554f1SJoshi-Mansi 1023ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1024ab6554f1SJoshi-Mansi { 1025ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1026ab6554f1SJoshi-Mansi return; 1027ab6554f1SJoshi-Mansi } 1028ab6554f1SJoshi-Mansi 1029ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1030ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1031ab6554f1SJoshi-Mansi } 1032ab6554f1SJoshi-Mansi 10334f48d5f6SEd Tanous inline void 10344f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1035bf648f77SEd Tanous const std::string& macAddress, 10368d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1037d577665bSRatan Gupta { 103858283f41SJohnathan Mantey static constexpr std::string_view dbusNotAllowedError = 103958283f41SJohnathan Mantey "xyz.openbmc_project.Common.Error.NotAllowed"; 104058283f41SJohnathan Mantey 1041d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 104258283f41SJohnathan Mantey [asyncResp, macAddress](const boost::system::error_code ec, 104358283f41SJohnathan Mantey const sdbusplus::message::message& msg) { 1044d577665bSRatan Gupta if (ec) 1045d577665bSRatan Gupta { 104658283f41SJohnathan Mantey const sd_bus_error* err = msg.get_error(); 104758283f41SJohnathan Mantey if (err == nullptr) 104858283f41SJohnathan Mantey { 104958283f41SJohnathan Mantey messages::internalError(asyncResp->res); 105058283f41SJohnathan Mantey return; 105158283f41SJohnathan Mantey } 105258283f41SJohnathan Mantey if (err->name == dbusNotAllowedError) 105358283f41SJohnathan Mantey { 105458283f41SJohnathan Mantey messages::propertyNotWritable(asyncResp->res, "MACAddress"); 105558283f41SJohnathan Mantey return; 105658283f41SJohnathan Mantey } 1057d577665bSRatan Gupta messages::internalError(asyncResp->res); 1058d577665bSRatan Gupta return; 1059d577665bSRatan Gupta } 1060d577665bSRatan Gupta }, 1061d577665bSRatan Gupta "xyz.openbmc_project.Network", 1062d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1063d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1064d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1065168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1066d577665bSRatan Gupta } 1067286b9118SJohnathan Mantey 10684f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 10694f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 10704f48d5f6SEd Tanous const bool v6Value, 10718d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1072da131a9aSJennifer Lee { 10732c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1074da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1075da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1076da131a9aSJennifer Lee if (ec) 1077da131a9aSJennifer Lee { 1078da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1079da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1080da131a9aSJennifer Lee return; 1081da131a9aSJennifer Lee } 10828f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1083da131a9aSJennifer Lee }, 1084da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1085da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1086da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1087da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1088168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1089da131a9aSJennifer Lee } 10901f8c7b5dSJohnathan Mantey 10914f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1092eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 10938d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1094eeedda23SJohnathan Mantey { 1095eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1096eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1097eeedda23SJohnathan Mantey if (ec) 1098eeedda23SJohnathan Mantey { 1099eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1100eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1101eeedda23SJohnathan Mantey return; 1102eeedda23SJohnathan Mantey } 1103eeedda23SJohnathan Mantey }, 1104eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1105eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1106eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1107eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1108168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1109eeedda23SJohnathan Mantey } 1110eeedda23SJohnathan Mantey 11114f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 11128d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1113da131a9aSJennifer Lee { 1114da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1115da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1116da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1117da131a9aSJennifer Lee if (ec) 1118da131a9aSJennifer Lee { 1119da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1120da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1121da131a9aSJennifer Lee return; 1122da131a9aSJennifer Lee } 1123da131a9aSJennifer Lee }, 1124da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1125da131a9aSJennifer Lee "/xyz/openbmc_project/network/config/dhcp", 1126da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1127da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1128168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1129da131a9aSJennifer Lee } 1130d577665bSRatan Gupta 11314f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 11321f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1133f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1134f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 11358d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1136da131a9aSJennifer Lee { 113782695a5bSJiaqing Zhao bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 113882695a5bSJiaqing Zhao bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); 1139da131a9aSJennifer Lee 11401f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 11411f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 11421f8c7b5dSJohnathan Mantey 11431f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 11441f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1145da131a9aSJennifer Lee { 11461f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 11471f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 11481f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 11491f8c7b5dSJohnathan Mantey { 1150bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1151bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 11521f8c7b5dSJohnathan Mantey "OperatingMode"); 1153da131a9aSJennifer Lee return; 1154da131a9aSJennifer Lee } 11551f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 11561f8c7b5dSJohnathan Mantey } 11571f8c7b5dSJohnathan Mantey else 1158da131a9aSJennifer Lee { 11591f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 11601f8c7b5dSJohnathan Mantey } 11611f8c7b5dSJohnathan Mantey 11621f8c7b5dSJohnathan Mantey bool nextDNS{}; 116382695a5bSJiaqing Zhao if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) 11641f8c7b5dSJohnathan Mantey { 116582695a5bSJiaqing Zhao if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) 11661f8c7b5dSJohnathan Mantey { 11671f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 11681f8c7b5dSJohnathan Mantey return; 11691f8c7b5dSJohnathan Mantey } 117082695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 11711f8c7b5dSJohnathan Mantey } 117282695a5bSJiaqing Zhao else if (v4dhcpParms.useDnsServers) 11731f8c7b5dSJohnathan Mantey { 117482695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 11751f8c7b5dSJohnathan Mantey } 117682695a5bSJiaqing Zhao else if (v6dhcpParms.useDnsServers) 11771f8c7b5dSJohnathan Mantey { 117882695a5bSJiaqing Zhao nextDNS = *v6dhcpParms.useDnsServers; 11791f8c7b5dSJohnathan Mantey } 11801f8c7b5dSJohnathan Mantey else 11811f8c7b5dSJohnathan Mantey { 118282695a5bSJiaqing Zhao nextDNS = ethData.dnsEnabled; 11831f8c7b5dSJohnathan Mantey } 11841f8c7b5dSJohnathan Mantey 11851f8c7b5dSJohnathan Mantey bool nextNTP{}; 118682695a5bSJiaqing Zhao if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) 11871f8c7b5dSJohnathan Mantey { 118882695a5bSJiaqing Zhao if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) 11891f8c7b5dSJohnathan Mantey { 11901f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 11911f8c7b5dSJohnathan Mantey return; 11921f8c7b5dSJohnathan Mantey } 119382695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 11941f8c7b5dSJohnathan Mantey } 119582695a5bSJiaqing Zhao else if (v4dhcpParms.useNtpServers) 11961f8c7b5dSJohnathan Mantey { 119782695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 11981f8c7b5dSJohnathan Mantey } 119982695a5bSJiaqing Zhao else if (v6dhcpParms.useNtpServers) 12001f8c7b5dSJohnathan Mantey { 120182695a5bSJiaqing Zhao nextNTP = *v6dhcpParms.useNtpServers; 12021f8c7b5dSJohnathan Mantey } 12031f8c7b5dSJohnathan Mantey else 12041f8c7b5dSJohnathan Mantey { 120582695a5bSJiaqing Zhao nextNTP = ethData.ntpEnabled; 12061f8c7b5dSJohnathan Mantey } 12071f8c7b5dSJohnathan Mantey 12081f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 120982695a5bSJiaqing Zhao if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) 12101f8c7b5dSJohnathan Mantey { 121182695a5bSJiaqing Zhao if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) 12121f8c7b5dSJohnathan Mantey { 12131f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12141f8c7b5dSJohnathan Mantey return; 12151f8c7b5dSJohnathan Mantey } 121682695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12171f8c7b5dSJohnathan Mantey } 121882695a5bSJiaqing Zhao else if (v4dhcpParms.useDomainName) 12191f8c7b5dSJohnathan Mantey { 122082695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12211f8c7b5dSJohnathan Mantey } 122282695a5bSJiaqing Zhao else if (v6dhcpParms.useDomainName) 12231f8c7b5dSJohnathan Mantey { 122482695a5bSJiaqing Zhao nextUseDomain = *v6dhcpParms.useDomainName; 12251f8c7b5dSJohnathan Mantey } 12261f8c7b5dSJohnathan Mantey else 12271f8c7b5dSJohnathan Mantey { 122882695a5bSJiaqing Zhao nextUseDomain = ethData.hostNameEnabled; 12291f8c7b5dSJohnathan Mantey } 12301f8c7b5dSJohnathan Mantey 1231da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 12321f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 12331f8c7b5dSJohnathan Mantey asyncResp); 1234da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 12351f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1236da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 12371f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 12381f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 12391f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1240da131a9aSJennifer Lee } 124101784826SJohnathan Mantey 12424f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 12432c70f800SEd Tanous getNextStaticIpEntry( 1244bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1245bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 124601784826SJohnathan Mantey { 124717a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 124817a897dfSManojkiran Eda return value.origin == "Static"; 124917a897dfSManojkiran Eda }); 125001784826SJohnathan Mantey } 125101784826SJohnathan Mantey 12524f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 12532c70f800SEd Tanous getNextStaticIpEntry( 1254bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1255bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 125601784826SJohnathan Mantey { 125717a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 125817a897dfSManojkiran Eda return value.origin == "Static"; 125917a897dfSManojkiran Eda }); 126001784826SJohnathan Mantey } 126101784826SJohnathan Mantey 12624f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1263f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 126401784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 12658d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 12661abe55efSEd Tanous { 126701784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1268f476acbfSRatan Gupta { 126971f52d96SEd Tanous messages::propertyValueTypeError( 127071f52d96SEd Tanous asyncResp->res, 1271bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1272d1d50814SRavi Teja "IPv4StaticAddresses"); 1273f476acbfSRatan Gupta return; 1274f476acbfSRatan Gupta } 1275f476acbfSRatan Gupta 1276271584abSEd Tanous unsigned entryIdx = 1; 127701784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 127801784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 127901784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 128001784826SJohnathan Mantey // into the NIC. 128185ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 12822c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 128301784826SJohnathan Mantey 1284537174c4SEd Tanous for (nlohmann::json& thisJson : input) 12851abe55efSEd Tanous { 12864a0cb85cSEd Tanous std::string pathString = 1287d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1288179db1d7SKowalski, Kamil 128901784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1290f476acbfSRatan Gupta { 1291537174c4SEd Tanous std::optional<std::string> address; 1292537174c4SEd Tanous std::optional<std::string> subnetMask; 1293537174c4SEd Tanous std::optional<std::string> gateway; 1294537174c4SEd Tanous 1295537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 12967e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 12977e27d832SJohnathan Mantey "Gateway", gateway)) 1298537174c4SEd Tanous { 129901784826SJohnathan Mantey messages::propertyValueFormatError( 130071f52d96SEd Tanous asyncResp->res, 130171f52d96SEd Tanous thisJson.dump(2, ' ', true, 130271f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 130371f52d96SEd Tanous pathString); 1304537174c4SEd Tanous return; 1305179db1d7SKowalski, Kamil } 1306179db1d7SKowalski, Kamil 130701784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 130801784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 130901784826SJohnathan Mantey // current state of the interface. Merge existing state into the 131001784826SJohnathan Mantey // current request. 1311271584abSEd Tanous const std::string* addr = nullptr; 1312271584abSEd Tanous const std::string* gw = nullptr; 131301784826SJohnathan Mantey uint8_t prefixLength = 0; 131401784826SJohnathan Mantey bool errorInEntry = false; 1315537174c4SEd Tanous if (address) 13161abe55efSEd Tanous { 1317033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*address)) 13181abe55efSEd Tanous { 131901784826SJohnathan Mantey addr = &(*address); 13204a0cb85cSEd Tanous } 132101784826SJohnathan Mantey else 132201784826SJohnathan Mantey { 1323bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1324bf648f77SEd Tanous pathString + "/Address"); 132501784826SJohnathan Mantey errorInEntry = true; 132601784826SJohnathan Mantey } 132701784826SJohnathan Mantey } 132885ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 132901784826SJohnathan Mantey { 133085ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 133101784826SJohnathan Mantey } 133201784826SJohnathan Mantey else 133301784826SJohnathan Mantey { 133401784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 133501784826SJohnathan Mantey pathString + "/Address"); 133601784826SJohnathan Mantey errorInEntry = true; 13374a0cb85cSEd Tanous } 13384a0cb85cSEd Tanous 1339537174c4SEd Tanous if (subnetMask) 13404a0cb85cSEd Tanous { 1341033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask, 1342033f1e4dSEd Tanous &prefixLength)) 13434a0cb85cSEd Tanous { 1344f12894f8SJason M. Bills messages::propertyValueFormatError( 1345537174c4SEd Tanous asyncResp->res, *subnetMask, 13464a0cb85cSEd Tanous pathString + "/SubnetMask"); 134701784826SJohnathan Mantey errorInEntry = true; 13484a0cb85cSEd Tanous } 13494a0cb85cSEd Tanous } 135085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 13514a0cb85cSEd Tanous { 1352033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 135301784826SJohnathan Mantey &prefixLength)) 13544a0cb85cSEd Tanous { 135501784826SJohnathan Mantey messages::propertyValueFormatError( 135685ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 135701784826SJohnathan Mantey pathString + "/SubnetMask"); 135801784826SJohnathan Mantey errorInEntry = true; 13594a0cb85cSEd Tanous } 13604a0cb85cSEd Tanous } 13611abe55efSEd Tanous else 13621abe55efSEd Tanous { 136301784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 136401784826SJohnathan Mantey pathString + "/SubnetMask"); 136501784826SJohnathan Mantey errorInEntry = true; 136601784826SJohnathan Mantey } 136701784826SJohnathan Mantey 136801784826SJohnathan Mantey if (gateway) 136901784826SJohnathan Mantey { 1370033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway)) 137101784826SJohnathan Mantey { 137201784826SJohnathan Mantey gw = &(*gateway); 137301784826SJohnathan Mantey } 137401784826SJohnathan Mantey else 137501784826SJohnathan Mantey { 1376bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1377bf648f77SEd Tanous pathString + "/Gateway"); 137801784826SJohnathan Mantey errorInEntry = true; 137901784826SJohnathan Mantey } 138001784826SJohnathan Mantey } 138185ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 138201784826SJohnathan Mantey { 138385ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 138401784826SJohnathan Mantey } 138501784826SJohnathan Mantey else 13861abe55efSEd Tanous { 1387a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 13884a0cb85cSEd Tanous pathString + "/Gateway"); 138901784826SJohnathan Mantey errorInEntry = true; 13904a0cb85cSEd Tanous } 13914a0cb85cSEd Tanous 139201784826SJohnathan Mantey if (errorInEntry) 13931abe55efSEd Tanous { 139401784826SJohnathan Mantey return; 13954a0cb85cSEd Tanous } 13964a0cb85cSEd Tanous 139785ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 13981abe55efSEd Tanous { 139985ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1400bf648f77SEd Tanous *addr, asyncResp); 140185ffe86aSJiaqing Zhao nicIpEntry = 140285ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1403588c3f0dSKowalski, Kamil } 140401784826SJohnathan Mantey else 140501784826SJohnathan Mantey { 1406cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1407cb13a392SEd Tanous asyncResp); 14084a0cb85cSEd Tanous } 14094a0cb85cSEd Tanous entryIdx++; 14104a0cb85cSEd Tanous } 141101784826SJohnathan Mantey else 141201784826SJohnathan Mantey { 141385ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 141401784826SJohnathan Mantey { 141501784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 141601784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 141701784826SJohnathan Mantey // in error, so bail out. 141801784826SJohnathan Mantey if (thisJson.is_null()) 141901784826SJohnathan Mantey { 142001784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 142101784826SJohnathan Mantey return; 142201784826SJohnathan Mantey } 142301784826SJohnathan Mantey messages::propertyValueFormatError( 142471f52d96SEd Tanous asyncResp->res, 142571f52d96SEd Tanous thisJson.dump(2, ' ', true, 142671f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 142771f52d96SEd Tanous pathString); 142801784826SJohnathan Mantey return; 142901784826SJohnathan Mantey } 143001784826SJohnathan Mantey 143101784826SJohnathan Mantey if (thisJson.is_null()) 143201784826SJohnathan Mantey { 143385ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 143401784826SJohnathan Mantey } 143585ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 143601784826SJohnathan Mantey { 143785ffe86aSJiaqing Zhao nicIpEntry = 143885ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 143901784826SJohnathan Mantey } 144001784826SJohnathan Mantey entryIdx++; 144101784826SJohnathan Mantey } 144201784826SJohnathan Mantey } 14434a0cb85cSEd Tanous } 14444a0cb85cSEd Tanous 14454f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1446f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1447f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 14488d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1449f85837bfSRAJESWARAN THILLAIGOVINDAN { 1450f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1451286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1452f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1453f85837bfSRAJESWARAN THILLAIGOVINDAN { 1454f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1455f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1456f85837bfSRAJESWARAN THILLAIGOVINDAN } 1457f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1458f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1459f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1460f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1461bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1462168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1463f85837bfSRAJESWARAN THILLAIGOVINDAN } 1464f85837bfSRAJESWARAN THILLAIGOVINDAN 14654f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1466f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 146701784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 14688d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1469e48c0fc5SRavi Teja { 147001784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1471e48c0fc5SRavi Teja { 147271f52d96SEd Tanous messages::propertyValueTypeError( 147371f52d96SEd Tanous asyncResp->res, 1474bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1475e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1476e48c0fc5SRavi Teja return; 1477e48c0fc5SRavi Teja } 1478271584abSEd Tanous size_t entryIdx = 1; 147985ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 14802c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1481f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1482e48c0fc5SRavi Teja { 1483e48c0fc5SRavi Teja std::string pathString = 1484e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1485e48c0fc5SRavi Teja 148601784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1487e48c0fc5SRavi Teja { 1488e48c0fc5SRavi Teja std::optional<std::string> address; 1489e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1490f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1491bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1492bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1493e48c0fc5SRavi Teja { 149401784826SJohnathan Mantey messages::propertyValueFormatError( 149571f52d96SEd Tanous asyncResp->res, 149671f52d96SEd Tanous thisJson.dump(2, ' ', true, 149771f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 149871f52d96SEd Tanous pathString); 1499e48c0fc5SRavi Teja return; 1500e48c0fc5SRavi Teja } 1501e48c0fc5SRavi Teja 1502543f4400SEd Tanous const std::string* addr = nullptr; 1503543f4400SEd Tanous uint8_t prefix = 0; 150401784826SJohnathan Mantey 150501784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 150601784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 150701784826SJohnathan Mantey // current state of the interface. Merge existing state into the 150801784826SJohnathan Mantey // current request. 1509e48c0fc5SRavi Teja if (address) 1510e48c0fc5SRavi Teja { 151101784826SJohnathan Mantey addr = &(*address); 1512e48c0fc5SRavi Teja } 151385ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 151401784826SJohnathan Mantey { 151585ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 151601784826SJohnathan Mantey } 151701784826SJohnathan Mantey else 151801784826SJohnathan Mantey { 151901784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 152001784826SJohnathan Mantey pathString + "/Address"); 152101784826SJohnathan Mantey return; 1522e48c0fc5SRavi Teja } 1523e48c0fc5SRavi Teja 1524e48c0fc5SRavi Teja if (prefixLength) 1525e48c0fc5SRavi Teja { 152601784826SJohnathan Mantey prefix = *prefixLength; 152701784826SJohnathan Mantey } 152885ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1529e48c0fc5SRavi Teja { 153085ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1531e48c0fc5SRavi Teja } 1532e48c0fc5SRavi Teja else 1533e48c0fc5SRavi Teja { 1534e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1535e48c0fc5SRavi Teja pathString + "/PrefixLength"); 153601784826SJohnathan Mantey return; 1537e48c0fc5SRavi Teja } 1538e48c0fc5SRavi Teja 153985ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1540e48c0fc5SRavi Teja { 154185ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1542e48c0fc5SRavi Teja asyncResp); 154385ffe86aSJiaqing Zhao nicIpEntry = 154485ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 154501784826SJohnathan Mantey } 154601784826SJohnathan Mantey else 154701784826SJohnathan Mantey { 154801784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1549e48c0fc5SRavi Teja } 1550e48c0fc5SRavi Teja entryIdx++; 1551e48c0fc5SRavi Teja } 155201784826SJohnathan Mantey else 155301784826SJohnathan Mantey { 155485ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 155501784826SJohnathan Mantey { 155601784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 155701784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 155801784826SJohnathan Mantey // in error, so bail out. 155901784826SJohnathan Mantey if (thisJson.is_null()) 156001784826SJohnathan Mantey { 156101784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 156201784826SJohnathan Mantey return; 156301784826SJohnathan Mantey } 156401784826SJohnathan Mantey messages::propertyValueFormatError( 156571f52d96SEd Tanous asyncResp->res, 156671f52d96SEd Tanous thisJson.dump(2, ' ', true, 156771f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 156871f52d96SEd Tanous pathString); 156901784826SJohnathan Mantey return; 157001784826SJohnathan Mantey } 157101784826SJohnathan Mantey 157201784826SJohnathan Mantey if (thisJson.is_null()) 157301784826SJohnathan Mantey { 157485ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 157501784826SJohnathan Mantey } 157685ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 157701784826SJohnathan Mantey { 157885ffe86aSJiaqing Zhao nicIpEntry = 157985ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 158001784826SJohnathan Mantey } 158101784826SJohnathan Mantey entryIdx++; 158201784826SJohnathan Mantey } 158301784826SJohnathan Mantey } 1584e48c0fc5SRavi Teja } 1585e48c0fc5SRavi Teja 15864f48d5f6SEd Tanous inline void parseInterfaceData( 15878d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 15888d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1589e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 159001784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 15914a0cb85cSEd Tanous { 1592eeedda23SJohnathan Mantey constexpr const std::array<const char*, 1> inventoryForEthernet = { 1593eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1594eeedda23SJohnathan Mantey 15952c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 159681ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 15972c70f800SEd Tanous jsonResponse["@odata.id"] = 159881ce609eSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 15992c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1600eeedda23SJohnathan Mantey 1601eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1602eeedda23SJohnathan Mantey 1603eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1604eeedda23SJohnathan Mantey [health](const boost::system::error_code ec, 1605b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& resp) { 1606eeedda23SJohnathan Mantey if (ec) 1607029573d4SEd Tanous { 1608eeedda23SJohnathan Mantey return; 1609eeedda23SJohnathan Mantey } 1610eeedda23SJohnathan Mantey 1611914e2d5dSEd Tanous health->inventory = resp; 1612eeedda23SJohnathan Mantey }, 1613eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", 1614eeedda23SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 1615bf648f77SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), 1616bf648f77SEd Tanous inventoryForEthernet); 1617eeedda23SJohnathan Mantey 1618eeedda23SJohnathan Mantey health->populate(); 1619eeedda23SJohnathan Mantey 1620eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1621eeedda23SJohnathan Mantey { 16222c70f800SEd Tanous jsonResponse["LinkStatus"] = "LinkUp"; 16232c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1624029573d4SEd Tanous } 1625029573d4SEd Tanous else 1626029573d4SEd Tanous { 16272c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 16282c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1629029573d4SEd Tanous } 1630aa05fb27SJohnathan Mantey 16312c70f800SEd Tanous jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 16322c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 163335fb5311STejas Patil jsonResponse["MTUSize"] = ethData.mtuSize; 163482695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 16352c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 163682695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 163782695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; 163882695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; 163982695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; 16401f8c7b5dSJohnathan Mantey 16412c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 164282695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" 16431f8c7b5dSJohnathan Mantey : "Disabled"; 164482695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; 164582695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; 164682695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; 16472a133282Smanojkiraneda 164882695a5bSJiaqing Zhao if (!ethData.hostName.empty()) 16494a0cb85cSEd Tanous { 165082695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 1651ab6554f1SJoshi-Mansi 1652ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1653ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1654ab6554f1SJoshi-Mansi // FQDN 165582695a5bSJiaqing Zhao std::string fqdn = ethData.hostName; 1656d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1657d24bfc7aSJennifer Lee { 16582c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1659d24bfc7aSJennifer Lee } 16602c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 16614a0cb85cSEd Tanous } 16624a0cb85cSEd Tanous 16632c70f800SEd Tanous jsonResponse["VLANs"] = { 1664bf648f77SEd Tanous {"@odata.id", 1665bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; 1666fda13ad2SSunitha Harish 16672c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 16682c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 16694a0cb85cSEd Tanous 16702c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 16712c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 16722c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 16732c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 16749eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 16754a0cb85cSEd Tanous { 1676fa5053a6SGunnar Mills 16772c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1678fa5053a6SGunnar Mills if (gatewayStr.empty()) 1679fa5053a6SGunnar Mills { 1680fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1681fa5053a6SGunnar Mills } 16821476687dSEd Tanous nlohmann::json::object_t ipv4; 16831476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 16841476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 16851476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 16861476687dSEd Tanous ipv4["Gateway"] = gatewayStr; 1687fa5053a6SGunnar Mills 16882c70f800SEd Tanous if (ipv4Config.origin == "Static") 1689d1d50814SRavi Teja { 16901476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 1691d1d50814SRavi Teja } 16921476687dSEd Tanous 16931476687dSEd Tanous ipv4Array.push_back(std::move(ipv4)); 169401784826SJohnathan Mantey } 1695d1d50814SRavi Teja 169682695a5bSJiaqing Zhao std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; 16977ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 16987ea79e5eSRavi Teja { 16997ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 17007ea79e5eSRavi Teja } 17017ea79e5eSRavi Teja 17027ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1703e48c0fc5SRavi Teja 17042c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 17052c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 17062c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 17072c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 17087f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 17092c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 17107f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 17119eb808c1SEd Tanous for (const auto& ipv6Config : ipv6Data) 1712e48c0fc5SRavi Teja { 17131476687dSEd Tanous nlohmann::json::object_t ipv6; 17141476687dSEd Tanous ipv6["Address"] = ipv6Config.address; 17151476687dSEd Tanous ipv6["PrefixLength"] = ipv6Config.prefixLength; 17161476687dSEd Tanous ipv6["AddressOrigin"] = ipv6Config.origin; 17171476687dSEd Tanous ipv6["AddressState"] = nullptr; 17181476687dSEd Tanous ipv6Array.push_back(std::move(ipv6)); 17192c70f800SEd Tanous if (ipv6Config.origin == "Static") 1720e48c0fc5SRavi Teja { 17211476687dSEd Tanous nlohmann::json::object_t ipv6Static; 17221476687dSEd Tanous ipv6Static["Address"] = ipv6Config.address; 17231476687dSEd Tanous ipv6Static["PrefixLength"] = ipv6Config.prefixLength; 17241476687dSEd Tanous ipv6StaticArray.push_back(std::move(ipv6Static)); 172501784826SJohnathan Mantey } 1726e48c0fc5SRavi Teja } 1727588c3f0dSKowalski, Kamil } 1728588c3f0dSKowalski, Kamil 17294f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 1730bf648f77SEd Tanous const std::string& parentIfaceId, 1731bf648f77SEd Tanous const std::string& ifaceId, 1732bf648f77SEd Tanous const EthernetInterfaceData& ethData) 17331abe55efSEd Tanous { 1734bf648f77SEd Tanous // Fill out obvious data... 1735bf648f77SEd Tanous jsonResponse["Id"] = ifaceId; 1736bf648f77SEd Tanous jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1737bf648f77SEd Tanous parentIfaceId + "/VLANs/" + ifaceId; 1738bf648f77SEd Tanous 1739bf648f77SEd Tanous jsonResponse["VLANEnable"] = true; 174017e22024SJiaqing Zhao if (ethData.vlanId) 1741bf648f77SEd Tanous { 174217e22024SJiaqing Zhao jsonResponse["VLANId"] = *ethData.vlanId; 1743bf648f77SEd Tanous } 1744bf648f77SEd Tanous } 1745bf648f77SEd Tanous 17464f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1747bf648f77SEd Tanous { 174811ba3979SEd Tanous return iface.starts_with(parent + "_"); 1749bf648f77SEd Tanous } 1750bf648f77SEd Tanous 1751bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1752bf648f77SEd Tanous { 1753bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1754ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 17551476687dSEd Tanous .methods(boost::beast::http::verb::get)( 17561476687dSEd Tanous [&app](const crow::Request& req, 17571476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 17583ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 175945ca1b86SEd Tanous { 176045ca1b86SEd Tanous return; 176145ca1b86SEd Tanous } 176245ca1b86SEd Tanous 1763bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1764bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1765bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1766bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1767bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1768bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1769bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1770bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1771bf648f77SEd Tanous 1772bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1773bf648f77SEd Tanous // preparation 1774002d39b4SEd Tanous getEthernetIfaceList( 1775002d39b4SEd Tanous [asyncResp]( 17761476687dSEd Tanous const bool& success, 1777002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 1778bf648f77SEd Tanous if (!success) 17791abe55efSEd Tanous { 1780f12894f8SJason M. Bills messages::internalError(asyncResp->res); 17819391bb9cSRapkiewicz, Pawel return; 17829391bb9cSRapkiewicz, Pawel } 17839391bb9cSRapkiewicz, Pawel 1784002d39b4SEd Tanous nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 1785bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1786bf648f77SEd Tanous std::string tag = "_"; 1787bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1788bf648f77SEd Tanous { 1789bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1790bf648f77SEd Tanous if (found == std::string::npos) 1791bf648f77SEd Tanous { 17921476687dSEd Tanous nlohmann::json::object_t iface; 17931476687dSEd Tanous iface["@odata.id"] = 1794bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 17951476687dSEd Tanous ifaceItem; 17961476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 1797bf648f77SEd Tanous } 1798bf648f77SEd Tanous } 1799bf648f77SEd Tanous 1800002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 1801bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1802bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1803bf648f77SEd Tanous }); 1804bf648f77SEd Tanous }); 1805bf648f77SEd Tanous 1806bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1807ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1808bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 180945ca1b86SEd Tanous [&app](const crow::Request& req, 1810bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1811bf648f77SEd Tanous const std::string& ifaceId) { 18123ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 181345ca1b86SEd Tanous { 181445ca1b86SEd Tanous return; 181545ca1b86SEd Tanous } 18164a0cb85cSEd Tanous getEthernetIfaceData( 1817bf648f77SEd Tanous ifaceId, 1818002d39b4SEd Tanous [asyncResp, ifaceId]( 1819002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1820002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1821002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 18224a0cb85cSEd Tanous if (!success) 18231abe55efSEd Tanous { 1824bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1825bf648f77SEd Tanous // existing object, and other errors 1826002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1827002d39b4SEd Tanous ifaceId); 18284a0cb85cSEd Tanous return; 18299391bb9cSRapkiewicz, Pawel } 18304c9afe43SEd Tanous 1831*188cb629SJiaqing Zhao // Keep using the v1.6.0 schema here as currently bmcweb have to use 1832*188cb629SJiaqing Zhao // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion. 18330f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1834*188cb629SJiaqing Zhao "#EthernetInterface.v1_6_0.EthernetInterface"; 1835002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 18360f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 18370f74e643SEd Tanous "Management Network Interface"; 18380f74e643SEd Tanous 1839002d39b4SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data); 18409391bb9cSRapkiewicz, Pawel }); 1841bf648f77SEd Tanous }); 18429391bb9cSRapkiewicz, Pawel 1843bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1844ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1845bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 184645ca1b86SEd Tanous [&app](const crow::Request& req, 1847bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1848bf648f77SEd Tanous const std::string& ifaceId) { 18493ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 185045ca1b86SEd Tanous { 185145ca1b86SEd Tanous return; 185245ca1b86SEd Tanous } 1853bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1854ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1855d577665bSRatan Gupta std::optional<std::string> macAddress; 18569a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1857d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1858e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1859f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1860da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 18611f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1862eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 186335fb5311STejas Patil std::optional<size_t> mtuSize; 18641f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 18651f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 18660627a2c7SEd Tanous 186715ed6780SWilly Tu if (!json_util::readJsonPatch( 18688d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1869002d39b4SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", 1870002d39b4SEd Tanous macAddress, "StaticNameServers", staticNameServers, 1871002d39b4SEd Tanous "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", 1872ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1873002d39b4SEd Tanous "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled)) 18741abe55efSEd Tanous { 1875588c3f0dSKowalski, Kamil return; 1876588c3f0dSKowalski, Kamil } 1877da131a9aSJennifer Lee if (dhcpv4) 1878da131a9aSJennifer Lee { 1879002d39b4SEd Tanous if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", 18801f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 188182695a5bSJiaqing Zhao v4dhcpParms.useDnsServers, "UseNTPServers", 188282695a5bSJiaqing Zhao v4dhcpParms.useNtpServers, "UseDomainName", 188382695a5bSJiaqing Zhao v4dhcpParms.useDomainName)) 18841f8c7b5dSJohnathan Mantey { 18851f8c7b5dSJohnathan Mantey return; 18861f8c7b5dSJohnathan Mantey } 18871f8c7b5dSJohnathan Mantey } 18881f8c7b5dSJohnathan Mantey 18891f8c7b5dSJohnathan Mantey if (dhcpv6) 18901f8c7b5dSJohnathan Mantey { 1891002d39b4SEd Tanous if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", 1892002d39b4SEd Tanous v6dhcpParms.dhcpv6OperatingMode, 1893002d39b4SEd Tanous "UseDNSServers", v6dhcpParms.useDnsServers, 1894002d39b4SEd Tanous "UseNTPServers", v6dhcpParms.useNtpServers, 1895002d39b4SEd Tanous "UseDomainName", 189682695a5bSJiaqing Zhao v6dhcpParms.useDomainName)) 18971f8c7b5dSJohnathan Mantey { 18981f8c7b5dSJohnathan Mantey return; 18991f8c7b5dSJohnathan Mantey } 1900da131a9aSJennifer Lee } 1901da131a9aSJennifer Lee 1902bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1903bf648f77SEd Tanous // for JSON preparation 19044a0cb85cSEd Tanous getEthernetIfaceData( 19052c70f800SEd Tanous ifaceId, 1906bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 1907ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1908d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 19099a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1910e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 19111f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 1912bc20089aSEd Tanous dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize, 1913bc20089aSEd Tanous v4dhcpParms = std::move(v4dhcpParms), 1914f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1915002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1916002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1917002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 19181abe55efSEd Tanous if (!success) 19191abe55efSEd Tanous { 1920588c3f0dSKowalski, Kamil // ... otherwise return error 1921bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1922bf648f77SEd Tanous // existing object, and other errors 1923002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1924002d39b4SEd Tanous ifaceId); 1925588c3f0dSKowalski, Kamil return; 1926588c3f0dSKowalski, Kamil } 1927588c3f0dSKowalski, Kamil 19281f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 19291f8c7b5dSJohnathan Mantey { 1930002d39b4SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, 1931002d39b4SEd Tanous asyncResp); 19321f8c7b5dSJohnathan Mantey } 19331f8c7b5dSJohnathan Mantey 19340627a2c7SEd Tanous if (hostname) 19351abe55efSEd Tanous { 19360627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 19371abe55efSEd Tanous } 19380627a2c7SEd Tanous 1939ab6554f1SJoshi-Mansi if (fqdn) 1940ab6554f1SJoshi-Mansi { 19412c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 1942ab6554f1SJoshi-Mansi } 1943ab6554f1SJoshi-Mansi 1944d577665bSRatan Gupta if (macAddress) 1945d577665bSRatan Gupta { 1946002d39b4SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, asyncResp); 1947d577665bSRatan Gupta } 1948d577665bSRatan Gupta 1949d1d50814SRavi Teja if (ipv4StaticAddresses) 1950d1d50814SRavi Teja { 1951bf648f77SEd Tanous // TODO(ed) for some reason the capture of 1952bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 1953bf648f77SEd Tanous // not a non-const value. This doesn't really work 1954bf648f77SEd Tanous // for us, as we need to be able to efficiently move 1955bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 1956bf648f77SEd Tanous // makes a copy of the structure, and operates on 1957bf648f77SEd Tanous // that, but could be done more efficiently 1958f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 1959002d39b4SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp); 19601abe55efSEd Tanous } 19610627a2c7SEd Tanous 1962f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 1963f85837bfSRAJESWARAN THILLAIGOVINDAN { 1964002d39b4SEd Tanous handleStaticNameServersPatch(ifaceId, *staticNameServers, 1965002d39b4SEd Tanous asyncResp); 1966f85837bfSRAJESWARAN THILLAIGOVINDAN } 19679a6fc6feSRavi Teja 19689a6fc6feSRavi Teja if (ipv6DefaultGateway) 19699a6fc6feSRavi Teja { 19709a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 19719a6fc6feSRavi Teja "IPv6DefaultGateway"); 19729a6fc6feSRavi Teja } 1973e48c0fc5SRavi Teja 1974e48c0fc5SRavi Teja if (ipv6StaticAddresses) 1975e48c0fc5SRavi Teja { 1976002d39b4SEd Tanous const nlohmann::json& ipv6Static = *ipv6StaticAddresses; 1977002d39b4SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data, 1978002d39b4SEd Tanous asyncResp); 1979e48c0fc5SRavi Teja } 1980eeedda23SJohnathan Mantey 1981eeedda23SJohnathan Mantey if (interfaceEnabled) 1982eeedda23SJohnathan Mantey { 1983002d39b4SEd Tanous setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled", 1984002d39b4SEd Tanous *interfaceEnabled, asyncResp); 1985eeedda23SJohnathan Mantey } 198635fb5311STejas Patil 198735fb5311STejas Patil if (mtuSize) 198835fb5311STejas Patil { 198935fb5311STejas Patil handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); 199035fb5311STejas Patil } 1991588c3f0dSKowalski, Kamil }); 1992bf648f77SEd Tanous }); 19939391bb9cSRapkiewicz, Pawel 1994bf648f77SEd Tanous BMCWEB_ROUTE( 1995bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 1996ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 1997bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 199845ca1b86SEd Tanous [&app](const crow::Request& req, 1999bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 200045ca1b86SEd Tanous const std::string& parentIfaceId, 200145ca1b86SEd Tanous const std::string& ifaceId) { 20023ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 200345ca1b86SEd Tanous { 200445ca1b86SEd Tanous return; 200545ca1b86SEd Tanous } 20068d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 20070f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 20088d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2009e439f0f8SKowalski, Kamil 20102c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 20111abe55efSEd Tanous { 2012a434f2bdSEd Tanous return; 2013a434f2bdSEd Tanous } 2014a434f2bdSEd Tanous 2015bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2016bf648f77SEd Tanous // for JSON preparation 20174a0cb85cSEd Tanous getEthernetIfaceData( 2018bf648f77SEd Tanous ifaceId, 2019002d39b4SEd Tanous [asyncResp, parentIfaceId, 2020002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2021cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2022cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 202317e22024SJiaqing Zhao if (success && ethData.vlanId) 20241abe55efSEd Tanous { 2025002d39b4SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 2026002d39b4SEd Tanous ifaceId, ethData); 20271abe55efSEd Tanous } 20281abe55efSEd Tanous else 20291abe55efSEd Tanous { 2030e439f0f8SKowalski, Kamil // ... otherwise return error 2031bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2032bf648f77SEd Tanous // existing object, and other errors 2033bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2034d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2035e439f0f8SKowalski, Kamil } 2036e439f0f8SKowalski, Kamil }); 2037bf648f77SEd Tanous }); 2038e439f0f8SKowalski, Kamil 2039bf648f77SEd Tanous BMCWEB_ROUTE( 2040bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 20413d768a16SAbhishek Patel .privileges(redfish::privileges::patchVLanNetworkInterface) 2042bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 204345ca1b86SEd Tanous [&app](const crow::Request& req, 2044bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 204545ca1b86SEd Tanous const std::string& parentIfaceId, 204645ca1b86SEd Tanous const std::string& ifaceId) { 20473ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 204845ca1b86SEd Tanous { 204945ca1b86SEd Tanous return; 205045ca1b86SEd Tanous } 2051fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 20521abe55efSEd Tanous { 2053d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2054002d39b4SEd Tanous ifaceId); 2055927a505aSKowalski, Kamil return; 2056927a505aSKowalski, Kamil } 2057927a505aSKowalski, Kamil 20583927e13eSJiaqing Zhao std::optional<bool> vlanEnable; 20593927e13eSJiaqing Zhao std::optional<uint32_t> vlanId; 20600627a2c7SEd Tanous 206115ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable", 2062bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 20631abe55efSEd Tanous { 2064927a505aSKowalski, Kamil return; 2065927a505aSKowalski, Kamil } 2066927a505aSKowalski, Kamil 20673927e13eSJiaqing Zhao if (vlanId) 20683927e13eSJiaqing Zhao { 20693927e13eSJiaqing Zhao messages::propertyNotWritable(asyncResp->res, "VLANId"); 20703927e13eSJiaqing Zhao return; 20713927e13eSJiaqing Zhao } 20723927e13eSJiaqing Zhao 2073bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2074bf648f77SEd Tanous // for JSON preparation 2075e48c0fc5SRavi Teja getEthernetIfaceData( 2076bf648f77SEd Tanous ifaceId, 20773927e13eSJiaqing Zhao [asyncResp, parentIfaceId, ifaceId, vlanEnable]( 2078002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 2079cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2080cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 208117e22024SJiaqing Zhao if (success && ethData.vlanId) 208208244d02SSunitha Harish { 208308244d02SSunitha Harish auto callback = 2084002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 208508244d02SSunitha Harish if (ec) 208608244d02SSunitha Harish { 208708244d02SSunitha Harish messages::internalError(asyncResp->res); 20883927e13eSJiaqing Zhao return; 208908244d02SSunitha Harish } 209008244d02SSunitha Harish }; 209108244d02SSunitha Harish 20923927e13eSJiaqing Zhao if (vlanEnable && !(*vlanEnable)) 209308244d02SSunitha Harish { 2094002d39b4SEd Tanous BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the " 2095e48c0fc5SRavi Teja "vlan interface"; 209608244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2097002d39b4SEd Tanous std::move(callback), "xyz.openbmc_project.Network", 2098002d39b4SEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 2099002d39b4SEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 210008244d02SSunitha Harish } 210108244d02SSunitha Harish } 210208244d02SSunitha Harish else 21031abe55efSEd Tanous { 2104bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2105bf648f77SEd Tanous // existing object, and other errors 2106bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2107d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2108bf648f77SEd Tanous return; 2109bf648f77SEd Tanous } 2110bf648f77SEd Tanous }); 2111bf648f77SEd Tanous }); 2112bf648f77SEd Tanous 2113bf648f77SEd Tanous BMCWEB_ROUTE( 2114bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 21153d768a16SAbhishek Patel .privileges(redfish::privileges::deleteVLanNetworkInterface) 2116bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 211745ca1b86SEd Tanous [&app](const crow::Request& req, 2118bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 211945ca1b86SEd Tanous const std::string& parentIfaceId, 212045ca1b86SEd Tanous const std::string& ifaceId) { 21213ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 212245ca1b86SEd Tanous { 212345ca1b86SEd Tanous return; 212445ca1b86SEd Tanous } 2125bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2126bf648f77SEd Tanous { 2127d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2128002d39b4SEd Tanous ifaceId); 2129927a505aSKowalski, Kamil return; 2130927a505aSKowalski, Kamil } 2131e439f0f8SKowalski, Kamil 2132bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2133bf648f77SEd Tanous // for JSON preparation 2134f12894f8SJason M. Bills getEthernetIfaceData( 2135bf648f77SEd Tanous ifaceId, 2136002d39b4SEd Tanous [asyncResp, parentIfaceId, 2137002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2138cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2139cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 214017e22024SJiaqing Zhao if (success && ethData.vlanId) 21411abe55efSEd Tanous { 2142f12894f8SJason M. Bills auto callback = 2143002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 21441abe55efSEd Tanous if (ec) 21451abe55efSEd Tanous { 2146f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2147927a505aSKowalski, Kamil } 21484a0cb85cSEd Tanous }; 21494a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2150002d39b4SEd Tanous std::move(callback), "xyz.openbmc_project.Network", 2151002d39b4SEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 21524a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 21531abe55efSEd Tanous } 21541abe55efSEd Tanous else 21551abe55efSEd Tanous { 2156927a505aSKowalski, Kamil // ... otherwise return error 2157bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2158bf648f77SEd Tanous // existing object, and other errors 2159bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2160d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2161927a505aSKowalski, Kamil } 2162927a505aSKowalski, Kamil }); 2163bf648f77SEd Tanous }); 2164e439f0f8SKowalski, Kamil 2165bf648f77SEd Tanous BMCWEB_ROUTE(app, 2166bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2167ed398213SEd Tanous 2168ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 21691476687dSEd Tanous .methods(boost::beast::http::verb::get)( 21701476687dSEd Tanous [&app](const crow::Request& req, 2171bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2172bf648f77SEd Tanous const std::string& rootInterfaceName) { 21733ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 217445ca1b86SEd Tanous { 217545ca1b86SEd Tanous return; 217645ca1b86SEd Tanous } 21774a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 21781abe55efSEd Tanous // preparation 2179002d39b4SEd Tanous getEthernetIfaceList( 2180002d39b4SEd Tanous [asyncResp, rootInterfaceName]( 21811abe55efSEd Tanous const bool& success, 2182002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 21834a0cb85cSEd Tanous if (!success) 21841abe55efSEd Tanous { 2185f12894f8SJason M. Bills messages::internalError(asyncResp->res); 21864a0cb85cSEd Tanous return; 21871abe55efSEd Tanous } 21884c9afe43SEd Tanous 218981ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 21904c9afe43SEd Tanous { 2191002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, 2192002d39b4SEd Tanous "VLanNetworkInterfaceCollection", 21934c9afe43SEd Tanous rootInterfaceName); 21944c9afe43SEd Tanous return; 21954c9afe43SEd Tanous } 21964c9afe43SEd Tanous 21970f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 21980f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 21990f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 22000f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 22010f74e643SEd Tanous "VLAN Network Interface Collection"; 22024a0cb85cSEd Tanous 22032c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 22044a0cb85cSEd Tanous 220581ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 22061abe55efSEd Tanous { 220711ba3979SEd Tanous if (ifaceItem.starts_with(rootInterfaceName + "_")) 22084a0cb85cSEd Tanous { 2209f23b7296SEd Tanous std::string path = 2210f23b7296SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2211f23b7296SEd Tanous path += rootInterfaceName; 2212f23b7296SEd Tanous path += "/VLANs/"; 2213f23b7296SEd Tanous path += ifaceItem; 22141476687dSEd Tanous nlohmann::json::object_t iface; 22151476687dSEd Tanous iface["@odata.id"] = std::move(path); 22161476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 2217e439f0f8SKowalski, Kamil } 2218e439f0f8SKowalski, Kamil } 2219e439f0f8SKowalski, Kamil 2220002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 22212c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 22224a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 22234a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 22244a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2225e439f0f8SKowalski, Kamil }); 2226bf648f77SEd Tanous }); 2227e439f0f8SKowalski, Kamil 2228bf648f77SEd Tanous BMCWEB_ROUTE(app, 2229bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 22303d768a16SAbhishek Patel .privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2231bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 223245ca1b86SEd Tanous [&app](const crow::Request& req, 2233bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2234bf648f77SEd Tanous const std::string& rootInterfaceName) { 22353ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 223645ca1b86SEd Tanous { 223745ca1b86SEd Tanous return; 223845ca1b86SEd Tanous } 2239fda13ad2SSunitha Harish bool vlanEnable = false; 22400627a2c7SEd Tanous uint32_t vlanId = 0; 2241002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId, 2242002d39b4SEd Tanous "VLANEnable", vlanEnable)) 22431abe55efSEd Tanous { 22444a0cb85cSEd Tanous return; 2245e439f0f8SKowalski, Kamil } 2246fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2247dbb59d4dSEd Tanous if (vlanId == 0U) 2248fda13ad2SSunitha Harish { 2249fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2250fda13ad2SSunitha Harish } 2251fda13ad2SSunitha Harish if (!vlanEnable) 2252fda13ad2SSunitha Harish { 2253fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2254fda13ad2SSunitha Harish } 2255271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2256fda13ad2SSunitha Harish { 2257fda13ad2SSunitha Harish return; 2258fda13ad2SSunitha Harish } 2259fda13ad2SSunitha Harish 2260002d39b4SEd Tanous auto callback = [asyncResp](const boost::system::error_code ec) { 22611abe55efSEd Tanous if (ec) 22621abe55efSEd Tanous { 2263bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2264bf648f77SEd Tanous // based on phosphor-network responses 2265f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22664a0cb85cSEd Tanous return; 22671abe55efSEd Tanous } 2268f12894f8SJason M. Bills messages::created(asyncResp->res); 2269e439f0f8SKowalski, Kamil }; 22704a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 22714a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 22724a0cb85cSEd Tanous "/xyz/openbmc_project/network", 22734a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 22740627a2c7SEd Tanous rootInterfaceName, vlanId); 2275bf648f77SEd Tanous }); 22764a0cb85cSEd Tanous } 2277bf648f77SEd Tanous 22789391bb9cSRapkiewicz, Pawel } // namespace redfish 2279