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 187e860f15SJohn Edward Broadbent #include <app.hpp> 194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp> 20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp> 21168e20c1SEd Tanous #include <dbus_utility.hpp> 22588c3f0dSKowalski, Kamil #include <error_messages.hpp> 2345ca1b86SEd Tanous #include <query.hpp> 24ed398213SEd Tanous #include <registries/privilege_registry.hpp> 251214b7e7SGunnar Mills #include <utils/json_utils.hpp> 261214b7e7SGunnar Mills 27a24526dcSEd Tanous #include <optional> 28ab6554f1SJoshi-Mansi #include <regex> 299391bb9cSRapkiewicz, Pawel 301abe55efSEd Tanous namespace redfish 311abe55efSEd Tanous { 329391bb9cSRapkiewicz, Pawel 334a0cb85cSEd Tanous enum class LinkType 344a0cb85cSEd Tanous { 354a0cb85cSEd Tanous Local, 364a0cb85cSEd Tanous Global 374a0cb85cSEd Tanous }; 389391bb9cSRapkiewicz, Pawel 399391bb9cSRapkiewicz, Pawel /** 409391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 419391bb9cSRapkiewicz, Pawel */ 421abe55efSEd Tanous struct IPv4AddressData 431abe55efSEd Tanous { 44179db1d7SKowalski, Kamil std::string id; 454a0cb85cSEd Tanous std::string address; 464a0cb85cSEd Tanous std::string domain; 474a0cb85cSEd Tanous std::string gateway; 489391bb9cSRapkiewicz, Pawel std::string netmask; 499391bb9cSRapkiewicz, Pawel std::string origin; 504a0cb85cSEd Tanous LinkType linktype; 5101c6e858SSunitha Harish bool isActive; 524a0cb85cSEd Tanous 531abe55efSEd Tanous bool operator<(const IPv4AddressData& obj) const 541abe55efSEd Tanous { 554a0cb85cSEd Tanous return id < obj.id; 561abe55efSEd Tanous } 579391bb9cSRapkiewicz, Pawel }; 589391bb9cSRapkiewicz, Pawel 599391bb9cSRapkiewicz, Pawel /** 60e48c0fc5SRavi Teja * Structure for keeping IPv6 data required by Redfish 61e48c0fc5SRavi Teja */ 62e48c0fc5SRavi Teja struct IPv6AddressData 63e48c0fc5SRavi Teja { 64e48c0fc5SRavi Teja std::string id; 65e48c0fc5SRavi Teja std::string address; 66e48c0fc5SRavi Teja std::string origin; 67e48c0fc5SRavi Teja uint8_t prefixLength; 68e48c0fc5SRavi Teja 69e48c0fc5SRavi Teja bool operator<(const IPv6AddressData& obj) const 70e48c0fc5SRavi Teja { 71e48c0fc5SRavi Teja return id < obj.id; 72e48c0fc5SRavi Teja } 73e48c0fc5SRavi Teja }; 74e48c0fc5SRavi Teja /** 759391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 769391bb9cSRapkiewicz, Pawel * available from DBus 779391bb9cSRapkiewicz, Pawel */ 781abe55efSEd Tanous struct EthernetInterfaceData 791abe55efSEd Tanous { 804a0cb85cSEd Tanous uint32_t speed; 8135fb5311STejas Patil size_t mtuSize; 82*82695a5bSJiaqing Zhao bool autoNeg; 83*82695a5bSJiaqing Zhao bool dnsEnabled; 84*82695a5bSJiaqing Zhao bool ntpEnabled; 85*82695a5bSJiaqing Zhao bool hostNameEnabled; 86aa05fb27SJohnathan Mantey bool linkUp; 87eeedda23SJohnathan Mantey bool nicEnabled; 88*82695a5bSJiaqing Zhao std::string dhcpEnabled; 891f8c7b5dSJohnathan Mantey std::string operatingMode; 90*82695a5bSJiaqing Zhao std::string hostName; 91*82695a5bSJiaqing Zhao std::string defaultGateway; 92*82695a5bSJiaqing Zhao std::string ipv6DefaultGateway; 93*82695a5bSJiaqing Zhao std::string macAddress; 94*82695a5bSJiaqing Zhao std::vector<std::uint32_t> vlanId; 950f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 960f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 97d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 989391bb9cSRapkiewicz, Pawel }; 999391bb9cSRapkiewicz, Pawel 1001f8c7b5dSJohnathan Mantey struct DHCPParameters 1011f8c7b5dSJohnathan Mantey { 1021f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 103*82695a5bSJiaqing Zhao std::optional<bool> useDnsServers; 104*82695a5bSJiaqing Zhao std::optional<bool> useNtpServers; 105*82695a5bSJiaqing Zhao std::optional<bool> useDomainName; 1061f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1071f8c7b5dSJohnathan Mantey }; 1081f8c7b5dSJohnathan Mantey 1099391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1109391bb9cSRapkiewicz, Pawel // into full dot notation 1111abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1121abe55efSEd Tanous { 1139391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1149391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1159391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1169391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1179391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1189391bb9cSRapkiewicz, Pawel return netmask; 1199391bb9cSRapkiewicz, Pawel } 1209391bb9cSRapkiewicz, Pawel 121*82695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP, 1221f8c7b5dSJohnathan Mantey bool isIPv4) 1231f8c7b5dSJohnathan Mantey { 1241f8c7b5dSJohnathan Mantey if (isIPv4) 1251f8c7b5dSJohnathan Mantey { 1261f8c7b5dSJohnathan Mantey return ( 1271f8c7b5dSJohnathan Mantey (inputDHCP == 1281f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1291f8c7b5dSJohnathan Mantey (inputDHCP == 1301f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1311f8c7b5dSJohnathan Mantey } 1321f8c7b5dSJohnathan Mantey return ((inputDHCP == 1331f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1341f8c7b5dSJohnathan Mantey (inputDHCP == 1351f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1361f8c7b5dSJohnathan Mantey } 1371f8c7b5dSJohnathan Mantey 1382c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1391f8c7b5dSJohnathan Mantey { 1401f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1411f8c7b5dSJohnathan Mantey { 1421f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1431f8c7b5dSJohnathan Mantey } 1443174e4dfSEd Tanous if (isIPv4) 1451f8c7b5dSJohnathan Mantey { 1461f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1471f8c7b5dSJohnathan Mantey } 1483174e4dfSEd Tanous if (isIPv6) 1491f8c7b5dSJohnathan Mantey { 1501f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1511f8c7b5dSJohnathan Mantey } 1521f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1531f8c7b5dSJohnathan Mantey } 1541f8c7b5dSJohnathan Mantey 1554a0cb85cSEd Tanous inline std::string 1564a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1574a0cb85cSEd Tanous bool isIPv4) 1581abe55efSEd Tanous { 1594a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1601abe55efSEd Tanous { 1614a0cb85cSEd Tanous return "Static"; 1629391bb9cSRapkiewicz, Pawel } 1634a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1641abe55efSEd Tanous { 1654a0cb85cSEd Tanous if (isIPv4) 1661abe55efSEd Tanous { 1674a0cb85cSEd Tanous return "IPv4LinkLocal"; 1681abe55efSEd Tanous } 1694a0cb85cSEd Tanous return "LinkLocal"; 1709391bb9cSRapkiewicz, Pawel } 1714a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1721abe55efSEd Tanous { 1734a0cb85cSEd Tanous if (isIPv4) 1744a0cb85cSEd Tanous { 1754a0cb85cSEd Tanous return "DHCP"; 1764a0cb85cSEd Tanous } 1774a0cb85cSEd Tanous return "DHCPv6"; 1784a0cb85cSEd Tanous } 1794a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1804a0cb85cSEd Tanous { 1814a0cb85cSEd Tanous return "SLAAC"; 1824a0cb85cSEd Tanous } 1834a0cb85cSEd Tanous return ""; 1844a0cb85cSEd Tanous } 1854a0cb85cSEd Tanous 186711ac7a9SEd Tanous inline bool 187711ac7a9SEd Tanous extractEthernetInterfaceData(const std::string& ethifaceId, 188711ac7a9SEd Tanous dbus::utility::ManagedObjectType& dbusData, 1894a0cb85cSEd Tanous EthernetInterfaceData& ethData) 1904a0cb85cSEd Tanous { 1914c9afe43SEd Tanous bool idFound = false; 19281ce609eSEd Tanous for (auto& objpath : dbusData) 1934a0cb85cSEd Tanous { 194f23b7296SEd Tanous for (auto& ifacePair : objpath.second) 1954a0cb85cSEd Tanous { 19681ce609eSEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 197029573d4SEd Tanous { 1984c9afe43SEd Tanous idFound = true; 1994a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2004a0cb85cSEd Tanous { 2014a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2024a0cb85cSEd Tanous { 2034a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2044a0cb85cSEd Tanous { 2054a0cb85cSEd Tanous const std::string* mac = 206abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2074a0cb85cSEd Tanous if (mac != nullptr) 2084a0cb85cSEd Tanous { 209*82695a5bSJiaqing Zhao ethData.macAddress = *mac; 2104a0cb85cSEd Tanous } 2114a0cb85cSEd Tanous } 2124a0cb85cSEd Tanous } 2134a0cb85cSEd Tanous } 2144a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2154a0cb85cSEd Tanous { 2164a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2174a0cb85cSEd Tanous { 2184a0cb85cSEd Tanous if (propertyPair.first == "Id") 2194a0cb85cSEd Tanous { 2201b6b96c5SEd Tanous const uint32_t* id = 221abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2224a0cb85cSEd Tanous if (id != nullptr) 2234a0cb85cSEd Tanous { 224*82695a5bSJiaqing Zhao ethData.vlanId.push_back(*id); 2254a0cb85cSEd Tanous } 2264a0cb85cSEd Tanous } 2274a0cb85cSEd Tanous } 2284a0cb85cSEd Tanous } 2294a0cb85cSEd Tanous else if (ifacePair.first == 2304a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2314a0cb85cSEd Tanous { 2324a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2334a0cb85cSEd Tanous { 2344a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2354a0cb85cSEd Tanous { 2362c70f800SEd Tanous const bool* autoNeg = 237abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2382c70f800SEd Tanous if (autoNeg != nullptr) 2394a0cb85cSEd Tanous { 240*82695a5bSJiaqing Zhao ethData.autoNeg = *autoNeg; 2414a0cb85cSEd Tanous } 2424a0cb85cSEd Tanous } 2434a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2444a0cb85cSEd Tanous { 2454a0cb85cSEd Tanous const uint32_t* speed = 246abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2474a0cb85cSEd Tanous if (speed != nullptr) 2484a0cb85cSEd Tanous { 2494a0cb85cSEd Tanous ethData.speed = *speed; 2504a0cb85cSEd Tanous } 2514a0cb85cSEd Tanous } 25235fb5311STejas Patil else if (propertyPair.first == "MTU") 25335fb5311STejas Patil { 25435fb5311STejas Patil const uint32_t* mtuSize = 25535fb5311STejas Patil std::get_if<uint32_t>(&propertyPair.second); 25635fb5311STejas Patil if (mtuSize != nullptr) 25735fb5311STejas Patil { 25835fb5311STejas Patil ethData.mtuSize = *mtuSize; 25935fb5311STejas Patil } 26035fb5311STejas Patil } 261aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 262aa05fb27SJohnathan Mantey { 263aa05fb27SJohnathan Mantey const bool* linkUp = 264aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 265aa05fb27SJohnathan Mantey if (linkUp != nullptr) 266aa05fb27SJohnathan Mantey { 267aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 268aa05fb27SJohnathan Mantey } 269aa05fb27SJohnathan Mantey } 270eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 271eeedda23SJohnathan Mantey { 272eeedda23SJohnathan Mantey const bool* nicEnabled = 273eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 274eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 275eeedda23SJohnathan Mantey { 276eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 277eeedda23SJohnathan Mantey } 278eeedda23SJohnathan Mantey } 279f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 280029573d4SEd Tanous { 281029573d4SEd Tanous const std::vector<std::string>* nameservers = 2828d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 283029573d4SEd Tanous &propertyPair.second); 284029573d4SEd Tanous if (nameservers != nullptr) 285029573d4SEd Tanous { 286f23b7296SEd Tanous ethData.nameServers = *nameservers; 2870f6efdc1Smanojkiran.eda@gmail.com } 2880f6efdc1Smanojkiran.eda@gmail.com } 2890f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 2900f6efdc1Smanojkiran.eda@gmail.com { 2910f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 2928d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 2930f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 2940f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 2950f6efdc1Smanojkiran.eda@gmail.com { 296f23b7296SEd Tanous ethData.staticNameServers = *staticNameServers; 2974a0cb85cSEd Tanous } 2984a0cb85cSEd Tanous } 2992a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3002a133282Smanojkiraneda { 3012c70f800SEd Tanous const std::string* dhcpEnabled = 3021f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 3032c70f800SEd Tanous if (dhcpEnabled != nullptr) 3042a133282Smanojkiraneda { 305*82695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcpEnabled; 3062a133282Smanojkiraneda } 3072a133282Smanojkiraneda } 308d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 309d24bfc7aSJennifer Lee { 310d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3118d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 312d24bfc7aSJennifer Lee &propertyPair.second); 313d24bfc7aSJennifer Lee if (domainNames != nullptr) 314d24bfc7aSJennifer Lee { 315f23b7296SEd Tanous ethData.domainnames = *domainNames; 316d24bfc7aSJennifer Lee } 317d24bfc7aSJennifer Lee } 3189010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway") 3199010ec2eSRavi Teja { 3209010ec2eSRavi Teja const std::string* defaultGateway = 3219010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3229010ec2eSRavi Teja if (defaultGateway != nullptr) 3239010ec2eSRavi Teja { 3249010ec2eSRavi Teja std::string defaultGatewayStr = *defaultGateway; 3259010ec2eSRavi Teja if (defaultGatewayStr.empty()) 3269010ec2eSRavi Teja { 327*82695a5bSJiaqing Zhao ethData.defaultGateway = "0.0.0.0"; 3289010ec2eSRavi Teja } 3299010ec2eSRavi Teja else 3309010ec2eSRavi Teja { 331*82695a5bSJiaqing Zhao ethData.defaultGateway = defaultGatewayStr; 3329010ec2eSRavi Teja } 3339010ec2eSRavi Teja } 3349010ec2eSRavi Teja } 3359010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway6") 3369010ec2eSRavi Teja { 3379010ec2eSRavi Teja const std::string* defaultGateway6 = 3389010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3399010ec2eSRavi Teja if (defaultGateway6 != nullptr) 3409010ec2eSRavi Teja { 3419010ec2eSRavi Teja std::string defaultGateway6Str = 3429010ec2eSRavi Teja *defaultGateway6; 3439010ec2eSRavi Teja if (defaultGateway6Str.empty()) 3449010ec2eSRavi Teja { 345*82695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3469010ec2eSRavi Teja "0:0:0:0:0:0:0:0"; 3479010ec2eSRavi Teja } 3489010ec2eSRavi Teja else 3499010ec2eSRavi Teja { 350*82695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3519010ec2eSRavi Teja defaultGateway6Str; 3529010ec2eSRavi Teja } 3539010ec2eSRavi Teja } 3549010ec2eSRavi Teja } 355029573d4SEd Tanous } 356029573d4SEd Tanous } 357029573d4SEd Tanous } 3581f8c7b5dSJohnathan Mantey 3591f8c7b5dSJohnathan Mantey if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 3601f8c7b5dSJohnathan Mantey { 3611f8c7b5dSJohnathan Mantey if (ifacePair.first == 3621f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3631f8c7b5dSJohnathan Mantey { 3641f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3651f8c7b5dSJohnathan Mantey { 3661f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3671f8c7b5dSJohnathan Mantey { 3682c70f800SEd Tanous const bool* dnsEnabled = 3691f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3702c70f800SEd Tanous if (dnsEnabled != nullptr) 3711f8c7b5dSJohnathan Mantey { 372*82695a5bSJiaqing Zhao ethData.dnsEnabled = *dnsEnabled; 3731f8c7b5dSJohnathan Mantey } 3741f8c7b5dSJohnathan Mantey } 3751f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3761f8c7b5dSJohnathan Mantey { 3772c70f800SEd Tanous const bool* ntpEnabled = 3781f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3792c70f800SEd Tanous if (ntpEnabled != nullptr) 3801f8c7b5dSJohnathan Mantey { 381*82695a5bSJiaqing Zhao ethData.ntpEnabled = *ntpEnabled; 3821f8c7b5dSJohnathan Mantey } 3831f8c7b5dSJohnathan Mantey } 3841f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3851f8c7b5dSJohnathan Mantey { 3862c70f800SEd Tanous const bool* hostNameEnabled = 3871f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3882c70f800SEd Tanous if (hostNameEnabled != nullptr) 3891f8c7b5dSJohnathan Mantey { 390*82695a5bSJiaqing Zhao ethData.hostNameEnabled = *hostNameEnabled; 3911f8c7b5dSJohnathan Mantey } 3921f8c7b5dSJohnathan Mantey } 3931f8c7b5dSJohnathan Mantey } 3941f8c7b5dSJohnathan Mantey } 3951f8c7b5dSJohnathan Mantey } 396029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 397029573d4SEd Tanous // to check eth number 398029573d4SEd Tanous if (ifacePair.first == 3994a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 4004a0cb85cSEd Tanous { 4014a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 4024a0cb85cSEd Tanous { 4034a0cb85cSEd Tanous if (propertyPair.first == "HostName") 4044a0cb85cSEd Tanous { 4054a0cb85cSEd Tanous const std::string* hostname = 4068d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4074a0cb85cSEd Tanous if (hostname != nullptr) 4084a0cb85cSEd Tanous { 409*82695a5bSJiaqing Zhao ethData.hostName = *hostname; 4104a0cb85cSEd Tanous } 4114a0cb85cSEd Tanous } 4124a0cb85cSEd Tanous } 4134a0cb85cSEd Tanous } 4144a0cb85cSEd Tanous } 4154a0cb85cSEd Tanous } 4164c9afe43SEd Tanous return idFound; 4174a0cb85cSEd Tanous } 4184a0cb85cSEd Tanous 419e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 42001784826SJohnathan Mantey inline void 42181ce609eSEd Tanous extractIPV6Data(const std::string& ethifaceId, 422711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 42381ce609eSEd Tanous boost::container::flat_set<IPv6AddressData>& ipv6Config) 424e48c0fc5SRavi Teja { 425e48c0fc5SRavi Teja const std::string ipv6PathStart = 42681ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 427e48c0fc5SRavi Teja 428e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 429e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 43081ce609eSEd Tanous for (const auto& objpath : dbusData) 431e48c0fc5SRavi Teja { 432e48c0fc5SRavi Teja // Check if proper pattern for object path appears 433e48c0fc5SRavi Teja if (boost::starts_with(objpath.first.str, ipv6PathStart)) 434e48c0fc5SRavi Teja { 4359eb808c1SEd Tanous for (const auto& interface : objpath.second) 436e48c0fc5SRavi Teja { 437e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 438e48c0fc5SRavi Teja { 439e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 440e48c0fc5SRavi Teja // appropriate 441e48c0fc5SRavi Teja std::pair< 442e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 443e48c0fc5SRavi Teja bool> 44481ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4452c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4462c70f800SEd Tanous ipv6Address.id = 447271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 4489eb808c1SEd Tanous for (const auto& property : interface.second) 449e48c0fc5SRavi Teja { 450e48c0fc5SRavi Teja if (property.first == "Address") 451e48c0fc5SRavi Teja { 452e48c0fc5SRavi Teja const std::string* address = 453e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 454e48c0fc5SRavi Teja if (address != nullptr) 455e48c0fc5SRavi Teja { 4562c70f800SEd Tanous ipv6Address.address = *address; 457e48c0fc5SRavi Teja } 458e48c0fc5SRavi Teja } 459e48c0fc5SRavi Teja else if (property.first == "Origin") 460e48c0fc5SRavi Teja { 461e48c0fc5SRavi Teja const std::string* origin = 462e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 463e48c0fc5SRavi Teja if (origin != nullptr) 464e48c0fc5SRavi Teja { 4652c70f800SEd Tanous ipv6Address.origin = 466e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 467e48c0fc5SRavi Teja false); 468e48c0fc5SRavi Teja } 469e48c0fc5SRavi Teja } 470e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 471e48c0fc5SRavi Teja { 472e48c0fc5SRavi Teja const uint8_t* prefix = 473e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 474e48c0fc5SRavi Teja if (prefix != nullptr) 475e48c0fc5SRavi Teja { 4762c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 477e48c0fc5SRavi Teja } 478e48c0fc5SRavi Teja } 479889ff694SAsmitha Karunanithi else if (property.first == "Type" || 480889ff694SAsmitha Karunanithi property.first == "Gateway") 481889ff694SAsmitha Karunanithi { 482889ff694SAsmitha Karunanithi // Type & Gateway is not used 483889ff694SAsmitha Karunanithi } 484e48c0fc5SRavi Teja else 485e48c0fc5SRavi Teja { 486e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 487e48c0fc5SRavi Teja << "Got extra property: " << property.first 488e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 489e48c0fc5SRavi Teja } 490e48c0fc5SRavi Teja } 491e48c0fc5SRavi Teja } 492e48c0fc5SRavi Teja } 493e48c0fc5SRavi Teja } 494e48c0fc5SRavi Teja } 495e48c0fc5SRavi Teja } 496e48c0fc5SRavi Teja 4974a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 49801784826SJohnathan Mantey inline void 49981ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 500711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 50181ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5024a0cb85cSEd Tanous { 5034a0cb85cSEd Tanous const std::string ipv4PathStart = 50481ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 5054a0cb85cSEd Tanous 5064a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5074a0cb85cSEd Tanous // single ethernet interface, loop over all of them 50881ce609eSEd Tanous for (const auto& objpath : dbusData) 5094a0cb85cSEd Tanous { 5104a0cb85cSEd Tanous // Check if proper pattern for object path appears 5114a0cb85cSEd Tanous if (boost::starts_with(objpath.first.str, ipv4PathStart)) 5124a0cb85cSEd Tanous { 5139eb808c1SEd Tanous for (const auto& interface : objpath.second) 5144a0cb85cSEd Tanous { 5154a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5164a0cb85cSEd Tanous { 5174a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5184a0cb85cSEd Tanous // appropriate 5194a0cb85cSEd Tanous std::pair< 5204a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5214a0cb85cSEd Tanous bool> 52281ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5232c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5242c70f800SEd Tanous ipv4Address.id = 525271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5269eb808c1SEd Tanous for (const auto& property : interface.second) 5274a0cb85cSEd Tanous { 5284a0cb85cSEd Tanous if (property.first == "Address") 5294a0cb85cSEd Tanous { 5304a0cb85cSEd Tanous const std::string* address = 531abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5324a0cb85cSEd Tanous if (address != nullptr) 5334a0cb85cSEd Tanous { 5342c70f800SEd Tanous ipv4Address.address = *address; 5354a0cb85cSEd Tanous } 5364a0cb85cSEd Tanous } 5374a0cb85cSEd Tanous else if (property.first == "Origin") 5384a0cb85cSEd Tanous { 5394a0cb85cSEd Tanous const std::string* origin = 540abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5414a0cb85cSEd Tanous if (origin != nullptr) 5424a0cb85cSEd Tanous { 5432c70f800SEd Tanous ipv4Address.origin = 5444a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5454a0cb85cSEd Tanous true); 5464a0cb85cSEd Tanous } 5474a0cb85cSEd Tanous } 5484a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5494a0cb85cSEd Tanous { 5504a0cb85cSEd Tanous const uint8_t* mask = 551abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5524a0cb85cSEd Tanous if (mask != nullptr) 5534a0cb85cSEd Tanous { 5544a0cb85cSEd Tanous // convert it to the string 5552c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5564a0cb85cSEd Tanous } 5574a0cb85cSEd Tanous } 558889ff694SAsmitha Karunanithi else if (property.first == "Type" || 559889ff694SAsmitha Karunanithi property.first == "Gateway") 560889ff694SAsmitha Karunanithi { 561889ff694SAsmitha Karunanithi // Type & Gateway is not used 562889ff694SAsmitha Karunanithi } 5634a0cb85cSEd Tanous else 5644a0cb85cSEd Tanous { 5654a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5664a0cb85cSEd Tanous << "Got extra property: " << property.first 5674a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5684a0cb85cSEd Tanous } 5694a0cb85cSEd Tanous } 5704a0cb85cSEd Tanous // Check if given address is local, or global 5712c70f800SEd Tanous ipv4Address.linktype = 5722c70f800SEd Tanous boost::starts_with(ipv4Address.address, "169.254.") 57318659d10SJohnathan Mantey ? LinkType::Local 57418659d10SJohnathan Mantey : LinkType::Global; 5754a0cb85cSEd Tanous } 5764a0cb85cSEd Tanous } 5774a0cb85cSEd Tanous } 5784a0cb85cSEd Tanous } 5794a0cb85cSEd Tanous } 580588c3f0dSKowalski, Kamil 581588c3f0dSKowalski, Kamil /** 582179db1d7SKowalski, Kamil * @brief Helper function that verifies IP address to check if it is in 583179db1d7SKowalski, Kamil * proper format. If bits pointer is provided, also calculates active 584179db1d7SKowalski, Kamil * bit count for Subnet Mask. 585179db1d7SKowalski, Kamil * 586179db1d7SKowalski, Kamil * @param[in] ip IP that will be verified 587179db1d7SKowalski, Kamil * @param[out] bits Calculated mask in bits notation 588179db1d7SKowalski, Kamil * 589179db1d7SKowalski, Kamil * @return true in case of success, false otherwise 590179db1d7SKowalski, Kamil */ 5914a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, 5921abe55efSEd Tanous uint8_t* bits = nullptr) 5931abe55efSEd Tanous { 594179db1d7SKowalski, Kamil std::vector<std::string> bytesInMask; 595179db1d7SKowalski, Kamil 596179db1d7SKowalski, Kamil boost::split(bytesInMask, ip, boost::is_any_of(".")); 597179db1d7SKowalski, Kamil 5984a0cb85cSEd Tanous static const constexpr int ipV4AddressSectionsCount = 4; 5991abe55efSEd Tanous if (bytesInMask.size() != ipV4AddressSectionsCount) 6001abe55efSEd Tanous { 601179db1d7SKowalski, Kamil return false; 602179db1d7SKowalski, Kamil } 603179db1d7SKowalski, Kamil 6041abe55efSEd Tanous if (bits != nullptr) 6051abe55efSEd Tanous { 606179db1d7SKowalski, Kamil *bits = 0; 607179db1d7SKowalski, Kamil } 608179db1d7SKowalski, Kamil 609543f4400SEd Tanous char* endPtr = nullptr; 610179db1d7SKowalski, Kamil long previousValue = 255; 611543f4400SEd Tanous bool firstZeroInByteHit = false; 6121abe55efSEd Tanous for (const std::string& byte : bytesInMask) 6131abe55efSEd Tanous { 6141abe55efSEd Tanous if (byte.empty()) 6151abe55efSEd Tanous { 6161db9ca37SKowalski, Kamil return false; 6171db9ca37SKowalski, Kamil } 6181db9ca37SKowalski, Kamil 619179db1d7SKowalski, Kamil // Use strtol instead of stroi to avoid exceptions 6201db9ca37SKowalski, Kamil long value = std::strtol(byte.c_str(), &endPtr, 10); 621179db1d7SKowalski, Kamil 6224a0cb85cSEd Tanous // endPtr should point to the end of the string, otherwise given string 6234a0cb85cSEd Tanous // is not 100% number 6241abe55efSEd Tanous if (*endPtr != '\0') 6251abe55efSEd Tanous { 626179db1d7SKowalski, Kamil return false; 627179db1d7SKowalski, Kamil } 628179db1d7SKowalski, Kamil 629179db1d7SKowalski, Kamil // Value should be contained in byte 6301abe55efSEd Tanous if (value < 0 || value > 255) 6311abe55efSEd Tanous { 632179db1d7SKowalski, Kamil return false; 633179db1d7SKowalski, Kamil } 634179db1d7SKowalski, Kamil 6351abe55efSEd Tanous if (bits != nullptr) 6361abe55efSEd Tanous { 637179db1d7SKowalski, Kamil // Mask has to be continuous between bytes 6381abe55efSEd Tanous if (previousValue != 255 && value != 0) 6391abe55efSEd Tanous { 640179db1d7SKowalski, Kamil return false; 641179db1d7SKowalski, Kamil } 642179db1d7SKowalski, Kamil 643179db1d7SKowalski, Kamil // Mask has to be continuous inside bytes 644179db1d7SKowalski, Kamil firstZeroInByteHit = false; 645179db1d7SKowalski, Kamil 646179db1d7SKowalski, Kamil // Count bits 64723a21a1cSEd Tanous for (long bitIdx = 7; bitIdx >= 0; bitIdx--) 6481abe55efSEd Tanous { 649e662eae8SEd Tanous if ((value & (1L << bitIdx)) != 0) 6501abe55efSEd Tanous { 6511abe55efSEd Tanous if (firstZeroInByteHit) 6521abe55efSEd Tanous { 653179db1d7SKowalski, Kamil // Continuity not preserved 654179db1d7SKowalski, Kamil return false; 6551abe55efSEd Tanous } 656179db1d7SKowalski, Kamil (*bits)++; 657179db1d7SKowalski, Kamil } 6581abe55efSEd Tanous else 6591abe55efSEd Tanous { 660179db1d7SKowalski, Kamil firstZeroInByteHit = true; 661179db1d7SKowalski, Kamil } 662179db1d7SKowalski, Kamil } 663179db1d7SKowalski, Kamil } 664179db1d7SKowalski, Kamil 665179db1d7SKowalski, Kamil previousValue = value; 666179db1d7SKowalski, Kamil } 667179db1d7SKowalski, Kamil 668179db1d7SKowalski, Kamil return true; 669179db1d7SKowalski, Kamil } 670179db1d7SKowalski, Kamil 671179db1d7SKowalski, Kamil /** 67201784826SJohnathan Mantey * @brief Deletes given IPv4 interface 673179db1d7SKowalski, Kamil * 674179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 675179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 676179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 677179db1d7SKowalski, Kamil * 678179db1d7SKowalski, Kamil * @return None 679179db1d7SKowalski, Kamil */ 6804a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 6818d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6821abe55efSEd Tanous { 68355c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 684286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 6851abe55efSEd Tanous if (ec) 6861abe55efSEd Tanous { 687a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6881abe55efSEd Tanous } 689179db1d7SKowalski, Kamil }, 690179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 691179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 692179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 693179db1d7SKowalski, Kamil } 694179db1d7SKowalski, Kamil 695244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 696244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 697244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6989010ec2eSRavi Teja { 6999010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7009010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 7019010ec2eSRavi Teja if (ec) 7029010ec2eSRavi Teja { 7039010ec2eSRavi Teja messages::internalError(asyncResp->res); 7049010ec2eSRavi Teja return; 7059010ec2eSRavi Teja } 7069010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 7079010ec2eSRavi Teja }, 7089010ec2eSRavi Teja "xyz.openbmc_project.Network", 7099010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 7109010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 7119010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 712168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 7139010ec2eSRavi Teja } 714179db1d7SKowalski, Kamil /** 71501784826SJohnathan Mantey * @brief Creates a static IPv4 entry 716179db1d7SKowalski, Kamil * 71701784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 71801784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 71901784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 72001784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 721179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 722179db1d7SKowalski, Kamil * 723179db1d7SKowalski, Kamil * @return None 724179db1d7SKowalski, Kamil */ 725cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 726cb13a392SEd Tanous const std::string& gateway, const std::string& address, 7278d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7281abe55efSEd Tanous { 7299010ec2eSRavi Teja auto createIpHandler = [asyncResp, ifaceId, 7309010ec2eSRavi Teja gateway](const boost::system::error_code ec) { 7311abe55efSEd Tanous if (ec) 7321abe55efSEd Tanous { 733a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7349010ec2eSRavi Teja return; 735179db1d7SKowalski, Kamil } 7369010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 7379010ec2eSRavi Teja }; 7389010ec2eSRavi Teja 7399010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7409010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 741179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 742179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 74301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 744179db1d7SKowalski, Kamil gateway); 745179db1d7SKowalski, Kamil } 746e48c0fc5SRavi Teja 747e48c0fc5SRavi Teja /** 74801784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 74901784826SJohnathan Mantey * static IPv4 entry 75001784826SJohnathan Mantey * 75101784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 75201784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 75301784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 75401784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 75501784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 75601784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 75701784826SJohnathan Mantey * 75801784826SJohnathan Mantey * @return None 75901784826SJohnathan Mantey */ 7608d1b46d7Szhanghch05 inline void 7618d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 7628d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 76301784826SJohnathan Mantey const std::string& address, 7648d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 76501784826SJohnathan Mantey { 76601784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 76701784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 76801784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 76901784826SJohnathan Mantey if (ec) 77001784826SJohnathan Mantey { 77101784826SJohnathan Mantey messages::internalError(asyncResp->res); 7729010ec2eSRavi Teja return; 77301784826SJohnathan Mantey } 7749010ec2eSRavi Teja 77501784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 7769010ec2eSRavi Teja [asyncResp, ifaceId, 7779010ec2eSRavi Teja gateway](const boost::system::error_code ec2) { 77823a21a1cSEd Tanous if (ec2) 77901784826SJohnathan Mantey { 78001784826SJohnathan Mantey messages::internalError(asyncResp->res); 7819010ec2eSRavi Teja return; 78201784826SJohnathan Mantey } 7839010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 78401784826SJohnathan Mantey }, 78501784826SJohnathan Mantey "xyz.openbmc_project.Network", 78601784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 78701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 78801784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 78901784826SJohnathan Mantey prefixLength, gateway); 79001784826SJohnathan Mantey }, 79101784826SJohnathan Mantey "xyz.openbmc_project.Network", 79201784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 79301784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 79401784826SJohnathan Mantey } 79501784826SJohnathan Mantey 79601784826SJohnathan Mantey /** 797e48c0fc5SRavi Teja * @brief Deletes given IPv6 798e48c0fc5SRavi Teja * 799e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 800e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 801e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 802e48c0fc5SRavi Teja * 803e48c0fc5SRavi Teja * @return None 804e48c0fc5SRavi Teja */ 805e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 8068d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 807e48c0fc5SRavi Teja { 808e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 809286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 810e48c0fc5SRavi Teja if (ec) 811e48c0fc5SRavi Teja { 812e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 813e48c0fc5SRavi Teja } 814e48c0fc5SRavi Teja }, 815e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 816e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 817e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 818e48c0fc5SRavi Teja } 819e48c0fc5SRavi Teja 820e48c0fc5SRavi Teja /** 82101784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 82201784826SJohnathan Mantey * static IPv6 entry 82301784826SJohnathan Mantey * 82401784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 82501784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 82601784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 82701784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 82801784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 82901784826SJohnathan Mantey * 83001784826SJohnathan Mantey * @return None 83101784826SJohnathan Mantey */ 8328d1b46d7Szhanghch05 inline void 8338d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 8348d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 8358d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 83601784826SJohnathan Mantey { 83701784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 83801784826SJohnathan Mantey [asyncResp, ifaceId, address, 83901784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 84001784826SJohnathan Mantey if (ec) 84101784826SJohnathan Mantey { 84201784826SJohnathan Mantey messages::internalError(asyncResp->res); 84301784826SJohnathan Mantey } 84401784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 84523a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 84623a21a1cSEd Tanous if (ec2) 84701784826SJohnathan Mantey { 84801784826SJohnathan Mantey messages::internalError(asyncResp->res); 84901784826SJohnathan Mantey } 85001784826SJohnathan Mantey }, 85101784826SJohnathan Mantey "xyz.openbmc_project.Network", 85201784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 85301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 85401784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 85501784826SJohnathan Mantey prefixLength, ""); 85601784826SJohnathan Mantey }, 85701784826SJohnathan Mantey "xyz.openbmc_project.Network", 85801784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 85901784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 86001784826SJohnathan Mantey } 86101784826SJohnathan Mantey 86201784826SJohnathan Mantey /** 863e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 864e48c0fc5SRavi Teja * 865e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 866e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 867e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 868e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 869e48c0fc5SRavi Teja * 870e48c0fc5SRavi Teja * @return None 871e48c0fc5SRavi Teja */ 87201784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 87301784826SJohnathan Mantey const std::string& address, 8748d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 875e48c0fc5SRavi Teja { 876e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 877e48c0fc5SRavi Teja if (ec) 878e48c0fc5SRavi Teja { 879e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 880e48c0fc5SRavi Teja } 881e48c0fc5SRavi Teja }; 882e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 8834e0453b1SGunnar Mills // does not have associated gateway property 884e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 885e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 886e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 887e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 888e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 889e48c0fc5SRavi Teja ""); 890e48c0fc5SRavi Teja } 891e48c0fc5SRavi Teja 892179db1d7SKowalski, Kamil /** 893179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 894179db1d7SKowalski, Kamil * Object 895179db1d7SKowalski, Kamil * from EntityManager Network Manager 8964a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 897179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 898179db1d7SKowalski, Kamil * into JSON 899179db1d7SKowalski, Kamil */ 900179db1d7SKowalski, Kamil template <typename CallbackFunc> 90181ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 9021abe55efSEd Tanous CallbackFunc&& callback) 9031abe55efSEd Tanous { 90455c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 905f94c4ecfSEd Tanous [ethifaceId{std::string{ethifaceId}}, 906f94c4ecfSEd Tanous callback{std::forward<CallbackFunc>(callback)}]( 90781ce609eSEd Tanous const boost::system::error_code errorCode, 908711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 90955c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 9104a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 911e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 912179db1d7SKowalski, Kamil 91381ce609eSEd Tanous if (errorCode) 9141abe55efSEd Tanous { 91501784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 916179db1d7SKowalski, Kamil return; 917179db1d7SKowalski, Kamil } 918179db1d7SKowalski, Kamil 9194c9afe43SEd Tanous bool found = 9202c70f800SEd Tanous extractEthernetInterfaceData(ethifaceId, resp, ethData); 9214c9afe43SEd Tanous if (!found) 9224c9afe43SEd Tanous { 92301784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 9244c9afe43SEd Tanous return; 9254c9afe43SEd Tanous } 9264c9afe43SEd Tanous 9272c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 928179db1d7SKowalski, Kamil // Fix global GW 9291abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 9301abe55efSEd Tanous { 931c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 932c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 9339010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 9341abe55efSEd Tanous { 935*82695a5bSJiaqing Zhao ipv4.gateway = ethData.defaultGateway; 936179db1d7SKowalski, Kamil } 937179db1d7SKowalski, Kamil } 938179db1d7SKowalski, Kamil 9392c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 9404e0453b1SGunnar Mills // Finally make a callback with useful data 94101784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 942179db1d7SKowalski, Kamil }, 943179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 944179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 945271584abSEd Tanous } 946179db1d7SKowalski, Kamil 947179db1d7SKowalski, Kamil /** 9489391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 9499391bb9cSRapkiewicz, Pawel * Manager 9501abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 9511abe55efSEd Tanous * into JSON. 9529391bb9cSRapkiewicz, Pawel */ 9539391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 9541abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 9551abe55efSEd Tanous { 95655c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 957f94c4ecfSEd Tanous [callback{std::forward<CallbackFunc>(callback)}]( 95881ce609eSEd Tanous const boost::system::error_code errorCode, 959711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 9601abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 9611abe55efSEd Tanous // ethernet interfaces 9622c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 9632c70f800SEd Tanous ifaceList.reserve(resp.size()); 96481ce609eSEd Tanous if (errorCode) 9651abe55efSEd Tanous { 9662c70f800SEd Tanous callback(false, ifaceList); 9679391bb9cSRapkiewicz, Pawel return; 9689391bb9cSRapkiewicz, Pawel } 9699391bb9cSRapkiewicz, Pawel 9709391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 9714a0cb85cSEd Tanous for (const auto& objpath : resp) 9721abe55efSEd Tanous { 9739391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 9744a0cb85cSEd Tanous for (const auto& interface : objpath.second) 9751abe55efSEd Tanous { 9761abe55efSEd Tanous // If interface is 9774a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 9784a0cb85cSEd Tanous // what we're looking for. 9799391bb9cSRapkiewicz, Pawel if (interface.first == 9801abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 9811abe55efSEd Tanous { 9822dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 9832dfd18efSEd Tanous if (ifaceId.empty()) 9841abe55efSEd Tanous { 9852dfd18efSEd Tanous continue; 9869391bb9cSRapkiewicz, Pawel } 9872dfd18efSEd Tanous // and put it into output vector. 9882dfd18efSEd Tanous ifaceList.emplace(ifaceId); 9899391bb9cSRapkiewicz, Pawel } 9909391bb9cSRapkiewicz, Pawel } 9919391bb9cSRapkiewicz, Pawel } 992a434f2bdSEd Tanous // Finally make a callback with useful data 9932c70f800SEd Tanous callback(true, ifaceList); 9949391bb9cSRapkiewicz, Pawel }, 995aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 996aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 997271584abSEd Tanous } 9989391bb9cSRapkiewicz, Pawel 9994f48d5f6SEd Tanous inline void 10004f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 10018d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 10021abe55efSEd Tanous { 1003ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 1004ab6554f1SJoshi-Mansi if (hostname.length() > 255) 1005ab6554f1SJoshi-Mansi { 1006ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 1007ab6554f1SJoshi-Mansi "HostName"); 1008ab6554f1SJoshi-Mansi return; 1009ab6554f1SJoshi-Mansi } 1010bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 1011bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 10124a0cb85cSEd Tanous if (ec) 10134a0cb85cSEd Tanous { 1014a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 10151abe55efSEd Tanous } 1016bc0bd6e0SEd Tanous }, 1017bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 1018bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 1019bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1020168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 1021588c3f0dSKowalski, Kamil } 1022588c3f0dSKowalski, Kamil 10234f48d5f6SEd Tanous inline void 102435fb5311STejas Patil handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, 102535fb5311STejas Patil const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 102635fb5311STejas Patil { 102735fb5311STejas Patil sdbusplus::message::object_path objPath = 102835fb5311STejas Patil "/xyz/openbmc_project/network/" + ifaceId; 102935fb5311STejas Patil crow::connections::systemBus->async_method_call( 103035fb5311STejas Patil [asyncResp](const boost::system::error_code ec) { 103135fb5311STejas Patil if (ec) 103235fb5311STejas Patil { 103335fb5311STejas Patil messages::internalError(asyncResp->res); 103435fb5311STejas Patil } 103535fb5311STejas Patil }, 103635fb5311STejas Patil "xyz.openbmc_project.Network", objPath, 103735fb5311STejas Patil "org.freedesktop.DBus.Properties", "Set", 103835fb5311STejas Patil "xyz.openbmc_project.Network.EthernetInterface", "MTU", 103935fb5311STejas Patil std::variant<size_t>(mtuSize)); 104035fb5311STejas Patil } 104135fb5311STejas Patil 104235fb5311STejas Patil inline void 10434f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 1044bf648f77SEd Tanous const std::string& domainname, 10458d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1046ab6554f1SJoshi-Mansi { 1047ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 1048ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 1049ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 1050ab6554f1SJoshi-Mansi if (ec) 1051ab6554f1SJoshi-Mansi { 1052ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 1053ab6554f1SJoshi-Mansi } 1054ab6554f1SJoshi-Mansi }, 1055ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 1056ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 1057ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 1058ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1059168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 1060ab6554f1SJoshi-Mansi } 1061ab6554f1SJoshi-Mansi 10624f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 1063bf648f77SEd Tanous { 1064bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 1065bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1066bf648f77SEd Tanous { 1067bf648f77SEd Tanous return false; 1068bf648f77SEd Tanous } 1069bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 1070bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 1071bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 1072bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 1073bf648f77SEd Tanous const std::regex pattern( 1074bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1075bf648f77SEd Tanous 1076bf648f77SEd Tanous return std::regex_match(hostname, pattern); 1077bf648f77SEd Tanous } 1078bf648f77SEd Tanous 10794f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 1080bf648f77SEd Tanous { 1081bf648f77SEd Tanous // Can have multiple subdomains 1082bf648f77SEd Tanous // Top Level Domain's min length is 2 character 10830fda0f12SGeorge Liu const std::regex pattern( 10840fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 1085bf648f77SEd Tanous 1086bf648f77SEd Tanous return std::regex_match(domainname, pattern); 1087bf648f77SEd Tanous } 1088bf648f77SEd Tanous 10894f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 10908d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1091ab6554f1SJoshi-Mansi { 1092ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1093ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1094ab6554f1SJoshi-Mansi { 1095ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1096ab6554f1SJoshi-Mansi return; 1097ab6554f1SJoshi-Mansi } 1098ab6554f1SJoshi-Mansi 1099ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1100ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1101ab6554f1SJoshi-Mansi { 1102ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1103ab6554f1SJoshi-Mansi return; 1104ab6554f1SJoshi-Mansi } 1105ab6554f1SJoshi-Mansi 1106ab6554f1SJoshi-Mansi std::string hostname; 1107ab6554f1SJoshi-Mansi std::string domainname; 1108ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1109ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1110ab6554f1SJoshi-Mansi 1111ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1112ab6554f1SJoshi-Mansi { 1113ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1114ab6554f1SJoshi-Mansi return; 1115ab6554f1SJoshi-Mansi } 1116ab6554f1SJoshi-Mansi 1117ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1118ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1119ab6554f1SJoshi-Mansi } 1120ab6554f1SJoshi-Mansi 11214f48d5f6SEd Tanous inline void 11224f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1123bf648f77SEd Tanous const std::string& macAddress, 11248d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1125d577665bSRatan Gupta { 1126d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 1127d577665bSRatan Gupta [asyncResp, macAddress](const boost::system::error_code ec) { 1128d577665bSRatan Gupta if (ec) 1129d577665bSRatan Gupta { 1130d577665bSRatan Gupta messages::internalError(asyncResp->res); 1131d577665bSRatan Gupta return; 1132d577665bSRatan Gupta } 1133d577665bSRatan Gupta }, 1134d577665bSRatan Gupta "xyz.openbmc_project.Network", 1135d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1136d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1137d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1138168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1139d577665bSRatan Gupta } 1140286b9118SJohnathan Mantey 11414f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 11424f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 11434f48d5f6SEd Tanous const bool v6Value, 11448d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1145da131a9aSJennifer Lee { 11462c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1147da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1148da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1149da131a9aSJennifer Lee if (ec) 1150da131a9aSJennifer Lee { 1151da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1152da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1153da131a9aSJennifer Lee return; 1154da131a9aSJennifer Lee } 11558f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1156da131a9aSJennifer Lee }, 1157da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1158da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1159da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1160da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1161168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1162da131a9aSJennifer Lee } 11631f8c7b5dSJohnathan Mantey 11644f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1165eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 11668d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1167eeedda23SJohnathan Mantey { 1168eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1169eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1170eeedda23SJohnathan Mantey if (ec) 1171eeedda23SJohnathan Mantey { 1172eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1173eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1174eeedda23SJohnathan Mantey return; 1175eeedda23SJohnathan Mantey } 1176eeedda23SJohnathan Mantey }, 1177eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1178eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1179eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1180eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1181168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1182eeedda23SJohnathan Mantey } 1183eeedda23SJohnathan Mantey 11844f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 11858d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1186da131a9aSJennifer Lee { 1187da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1188da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1189da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1190da131a9aSJennifer Lee if (ec) 1191da131a9aSJennifer Lee { 1192da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1193da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1194da131a9aSJennifer Lee return; 1195da131a9aSJennifer Lee } 1196da131a9aSJennifer Lee }, 1197da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1198da131a9aSJennifer Lee "/xyz/openbmc_project/network/config/dhcp", 1199da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1200da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1201168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1202da131a9aSJennifer Lee } 1203d577665bSRatan Gupta 12044f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 12051f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1206f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1207f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 12088d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1209da131a9aSJennifer Lee { 1210*82695a5bSJiaqing Zhao bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 1211*82695a5bSJiaqing Zhao bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); 1212da131a9aSJennifer Lee 12131f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 12141f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 12151f8c7b5dSJohnathan Mantey 12161f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 12171f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1218da131a9aSJennifer Lee { 12191f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 12201f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 12211f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 12221f8c7b5dSJohnathan Mantey { 1223bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1224bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 12251f8c7b5dSJohnathan Mantey "OperatingMode"); 1226da131a9aSJennifer Lee return; 1227da131a9aSJennifer Lee } 12281f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 12291f8c7b5dSJohnathan Mantey } 12301f8c7b5dSJohnathan Mantey else 1231da131a9aSJennifer Lee { 12321f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 12331f8c7b5dSJohnathan Mantey } 12341f8c7b5dSJohnathan Mantey 12351f8c7b5dSJohnathan Mantey bool nextDNS{}; 1236*82695a5bSJiaqing Zhao if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) 12371f8c7b5dSJohnathan Mantey { 1238*82695a5bSJiaqing Zhao if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) 12391f8c7b5dSJohnathan Mantey { 12401f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12411f8c7b5dSJohnathan Mantey return; 12421f8c7b5dSJohnathan Mantey } 1243*82695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 12441f8c7b5dSJohnathan Mantey } 1245*82695a5bSJiaqing Zhao else if (v4dhcpParms.useDnsServers) 12461f8c7b5dSJohnathan Mantey { 1247*82695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 12481f8c7b5dSJohnathan Mantey } 1249*82695a5bSJiaqing Zhao else if (v6dhcpParms.useDnsServers) 12501f8c7b5dSJohnathan Mantey { 1251*82695a5bSJiaqing Zhao nextDNS = *v6dhcpParms.useDnsServers; 12521f8c7b5dSJohnathan Mantey } 12531f8c7b5dSJohnathan Mantey else 12541f8c7b5dSJohnathan Mantey { 1255*82695a5bSJiaqing Zhao nextDNS = ethData.dnsEnabled; 12561f8c7b5dSJohnathan Mantey } 12571f8c7b5dSJohnathan Mantey 12581f8c7b5dSJohnathan Mantey bool nextNTP{}; 1259*82695a5bSJiaqing Zhao if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) 12601f8c7b5dSJohnathan Mantey { 1261*82695a5bSJiaqing Zhao if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) 12621f8c7b5dSJohnathan Mantey { 12631f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12641f8c7b5dSJohnathan Mantey return; 12651f8c7b5dSJohnathan Mantey } 1266*82695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 12671f8c7b5dSJohnathan Mantey } 1268*82695a5bSJiaqing Zhao else if (v4dhcpParms.useNtpServers) 12691f8c7b5dSJohnathan Mantey { 1270*82695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 12711f8c7b5dSJohnathan Mantey } 1272*82695a5bSJiaqing Zhao else if (v6dhcpParms.useNtpServers) 12731f8c7b5dSJohnathan Mantey { 1274*82695a5bSJiaqing Zhao nextNTP = *v6dhcpParms.useNtpServers; 12751f8c7b5dSJohnathan Mantey } 12761f8c7b5dSJohnathan Mantey else 12771f8c7b5dSJohnathan Mantey { 1278*82695a5bSJiaqing Zhao nextNTP = ethData.ntpEnabled; 12791f8c7b5dSJohnathan Mantey } 12801f8c7b5dSJohnathan Mantey 12811f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 1282*82695a5bSJiaqing Zhao if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) 12831f8c7b5dSJohnathan Mantey { 1284*82695a5bSJiaqing Zhao if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) 12851f8c7b5dSJohnathan Mantey { 12861f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12871f8c7b5dSJohnathan Mantey return; 12881f8c7b5dSJohnathan Mantey } 1289*82695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12901f8c7b5dSJohnathan Mantey } 1291*82695a5bSJiaqing Zhao else if (v4dhcpParms.useDomainName) 12921f8c7b5dSJohnathan Mantey { 1293*82695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12941f8c7b5dSJohnathan Mantey } 1295*82695a5bSJiaqing Zhao else if (v6dhcpParms.useDomainName) 12961f8c7b5dSJohnathan Mantey { 1297*82695a5bSJiaqing Zhao nextUseDomain = *v6dhcpParms.useDomainName; 12981f8c7b5dSJohnathan Mantey } 12991f8c7b5dSJohnathan Mantey else 13001f8c7b5dSJohnathan Mantey { 1301*82695a5bSJiaqing Zhao nextUseDomain = ethData.hostNameEnabled; 13021f8c7b5dSJohnathan Mantey } 13031f8c7b5dSJohnathan Mantey 1304da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 13051f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 13061f8c7b5dSJohnathan Mantey asyncResp); 1307da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 13081f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1309da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 13101f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 13111f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 13121f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1313da131a9aSJennifer Lee } 131401784826SJohnathan Mantey 13154f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 13162c70f800SEd Tanous getNextStaticIpEntry( 1317bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1318bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 131901784826SJohnathan Mantey { 132017a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 132117a897dfSManojkiran Eda return value.origin == "Static"; 132217a897dfSManojkiran Eda }); 132301784826SJohnathan Mantey } 132401784826SJohnathan Mantey 13254f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 13262c70f800SEd Tanous getNextStaticIpEntry( 1327bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1328bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 132901784826SJohnathan Mantey { 133017a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 133117a897dfSManojkiran Eda return value.origin == "Static"; 133217a897dfSManojkiran Eda }); 133301784826SJohnathan Mantey } 133401784826SJohnathan Mantey 13354f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1336f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 133701784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 13388d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 13391abe55efSEd Tanous { 134001784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1341f476acbfSRatan Gupta { 134271f52d96SEd Tanous messages::propertyValueTypeError( 134371f52d96SEd Tanous asyncResp->res, 1344bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1345d1d50814SRavi Teja "IPv4StaticAddresses"); 1346f476acbfSRatan Gupta return; 1347f476acbfSRatan Gupta } 1348f476acbfSRatan Gupta 1349271584abSEd Tanous unsigned entryIdx = 1; 135001784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 135101784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 135201784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 135301784826SJohnathan Mantey // into the NIC. 135485ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 13552c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 135601784826SJohnathan Mantey 1357537174c4SEd Tanous for (nlohmann::json& thisJson : input) 13581abe55efSEd Tanous { 13594a0cb85cSEd Tanous std::string pathString = 1360d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1361179db1d7SKowalski, Kamil 136201784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1363f476acbfSRatan Gupta { 1364537174c4SEd Tanous std::optional<std::string> address; 1365537174c4SEd Tanous std::optional<std::string> subnetMask; 1366537174c4SEd Tanous std::optional<std::string> gateway; 1367537174c4SEd Tanous 1368537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 13697e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 13707e27d832SJohnathan Mantey "Gateway", gateway)) 1371537174c4SEd Tanous { 137201784826SJohnathan Mantey messages::propertyValueFormatError( 137371f52d96SEd Tanous asyncResp->res, 137471f52d96SEd Tanous thisJson.dump(2, ' ', true, 137571f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 137671f52d96SEd Tanous pathString); 1377537174c4SEd Tanous return; 1378179db1d7SKowalski, Kamil } 1379179db1d7SKowalski, Kamil 138001784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 138101784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 138201784826SJohnathan Mantey // current state of the interface. Merge existing state into the 138301784826SJohnathan Mantey // current request. 1384271584abSEd Tanous const std::string* addr = nullptr; 1385271584abSEd Tanous const std::string* gw = nullptr; 138601784826SJohnathan Mantey uint8_t prefixLength = 0; 138701784826SJohnathan Mantey bool errorInEntry = false; 1388537174c4SEd Tanous if (address) 13891abe55efSEd Tanous { 139001784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*address)) 13911abe55efSEd Tanous { 139201784826SJohnathan Mantey addr = &(*address); 13934a0cb85cSEd Tanous } 139401784826SJohnathan Mantey else 139501784826SJohnathan Mantey { 1396bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1397bf648f77SEd Tanous pathString + "/Address"); 139801784826SJohnathan Mantey errorInEntry = true; 139901784826SJohnathan Mantey } 140001784826SJohnathan Mantey } 140185ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 140201784826SJohnathan Mantey { 140385ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 140401784826SJohnathan Mantey } 140501784826SJohnathan Mantey else 140601784826SJohnathan Mantey { 140701784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 140801784826SJohnathan Mantey pathString + "/Address"); 140901784826SJohnathan Mantey errorInEntry = true; 14104a0cb85cSEd Tanous } 14114a0cb85cSEd Tanous 1412537174c4SEd Tanous if (subnetMask) 14134a0cb85cSEd Tanous { 1414537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 14154a0cb85cSEd Tanous { 1416f12894f8SJason M. Bills messages::propertyValueFormatError( 1417537174c4SEd Tanous asyncResp->res, *subnetMask, 14184a0cb85cSEd Tanous pathString + "/SubnetMask"); 141901784826SJohnathan Mantey errorInEntry = true; 14204a0cb85cSEd Tanous } 14214a0cb85cSEd Tanous } 142285ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 14234a0cb85cSEd Tanous { 142485ffe86aSJiaqing Zhao if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 142501784826SJohnathan Mantey &prefixLength)) 14264a0cb85cSEd Tanous { 142701784826SJohnathan Mantey messages::propertyValueFormatError( 142885ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 142901784826SJohnathan Mantey pathString + "/SubnetMask"); 143001784826SJohnathan Mantey errorInEntry = true; 14314a0cb85cSEd Tanous } 14324a0cb85cSEd Tanous } 14331abe55efSEd Tanous else 14341abe55efSEd Tanous { 143501784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 143601784826SJohnathan Mantey pathString + "/SubnetMask"); 143701784826SJohnathan Mantey errorInEntry = true; 143801784826SJohnathan Mantey } 143901784826SJohnathan Mantey 144001784826SJohnathan Mantey if (gateway) 144101784826SJohnathan Mantey { 144201784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*gateway)) 144301784826SJohnathan Mantey { 144401784826SJohnathan Mantey gw = &(*gateway); 144501784826SJohnathan Mantey } 144601784826SJohnathan Mantey else 144701784826SJohnathan Mantey { 1448bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1449bf648f77SEd Tanous pathString + "/Gateway"); 145001784826SJohnathan Mantey errorInEntry = true; 145101784826SJohnathan Mantey } 145201784826SJohnathan Mantey } 145385ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 145401784826SJohnathan Mantey { 145585ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 145601784826SJohnathan Mantey } 145701784826SJohnathan Mantey else 14581abe55efSEd Tanous { 1459a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 14604a0cb85cSEd Tanous pathString + "/Gateway"); 146101784826SJohnathan Mantey errorInEntry = true; 14624a0cb85cSEd Tanous } 14634a0cb85cSEd Tanous 146401784826SJohnathan Mantey if (errorInEntry) 14651abe55efSEd Tanous { 146601784826SJohnathan Mantey return; 14674a0cb85cSEd Tanous } 14684a0cb85cSEd Tanous 146985ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 14701abe55efSEd Tanous { 147185ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1472bf648f77SEd Tanous *addr, asyncResp); 147385ffe86aSJiaqing Zhao nicIpEntry = 147485ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1475588c3f0dSKowalski, Kamil } 147601784826SJohnathan Mantey else 147701784826SJohnathan Mantey { 1478cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1479cb13a392SEd Tanous asyncResp); 14804a0cb85cSEd Tanous } 14814a0cb85cSEd Tanous entryIdx++; 14824a0cb85cSEd Tanous } 148301784826SJohnathan Mantey else 148401784826SJohnathan Mantey { 148585ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 148601784826SJohnathan Mantey { 148701784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 148801784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 148901784826SJohnathan Mantey // in error, so bail out. 149001784826SJohnathan Mantey if (thisJson.is_null()) 149101784826SJohnathan Mantey { 149201784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 149301784826SJohnathan Mantey return; 149401784826SJohnathan Mantey } 149501784826SJohnathan Mantey messages::propertyValueFormatError( 149671f52d96SEd Tanous asyncResp->res, 149771f52d96SEd Tanous thisJson.dump(2, ' ', true, 149871f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 149971f52d96SEd Tanous pathString); 150001784826SJohnathan Mantey return; 150101784826SJohnathan Mantey } 150201784826SJohnathan Mantey 150301784826SJohnathan Mantey if (thisJson.is_null()) 150401784826SJohnathan Mantey { 150585ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 150601784826SJohnathan Mantey } 150785ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 150801784826SJohnathan Mantey { 150985ffe86aSJiaqing Zhao nicIpEntry = 151085ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 151101784826SJohnathan Mantey } 151201784826SJohnathan Mantey entryIdx++; 151301784826SJohnathan Mantey } 151401784826SJohnathan Mantey } 15154a0cb85cSEd Tanous } 15164a0cb85cSEd Tanous 15174f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1518f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1519f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 15208d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1521f85837bfSRAJESWARAN THILLAIGOVINDAN { 1522f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1523286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1524f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1525f85837bfSRAJESWARAN THILLAIGOVINDAN { 1526f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1527f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1528f85837bfSRAJESWARAN THILLAIGOVINDAN } 1529f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1530f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1531f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1532f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1533bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1534168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1535f85837bfSRAJESWARAN THILLAIGOVINDAN } 1536f85837bfSRAJESWARAN THILLAIGOVINDAN 15374f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1538f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 153901784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 15408d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1541e48c0fc5SRavi Teja { 154201784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1543e48c0fc5SRavi Teja { 154471f52d96SEd Tanous messages::propertyValueTypeError( 154571f52d96SEd Tanous asyncResp->res, 1546bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1547e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1548e48c0fc5SRavi Teja return; 1549e48c0fc5SRavi Teja } 1550271584abSEd Tanous size_t entryIdx = 1; 155185ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 15522c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1553f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1554e48c0fc5SRavi Teja { 1555e48c0fc5SRavi Teja std::string pathString = 1556e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1557e48c0fc5SRavi Teja 155801784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1559e48c0fc5SRavi Teja { 1560e48c0fc5SRavi Teja std::optional<std::string> address; 1561e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1562f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1563bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1564bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1565e48c0fc5SRavi Teja { 156601784826SJohnathan Mantey messages::propertyValueFormatError( 156771f52d96SEd Tanous asyncResp->res, 156871f52d96SEd Tanous thisJson.dump(2, ' ', true, 156971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 157071f52d96SEd Tanous pathString); 1571e48c0fc5SRavi Teja return; 1572e48c0fc5SRavi Teja } 1573e48c0fc5SRavi Teja 1574543f4400SEd Tanous const std::string* addr = nullptr; 1575543f4400SEd Tanous uint8_t prefix = 0; 157601784826SJohnathan Mantey 157701784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 157801784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 157901784826SJohnathan Mantey // current state of the interface. Merge existing state into the 158001784826SJohnathan Mantey // current request. 1581e48c0fc5SRavi Teja if (address) 1582e48c0fc5SRavi Teja { 158301784826SJohnathan Mantey addr = &(*address); 1584e48c0fc5SRavi Teja } 158585ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 158601784826SJohnathan Mantey { 158785ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 158801784826SJohnathan Mantey } 158901784826SJohnathan Mantey else 159001784826SJohnathan Mantey { 159101784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 159201784826SJohnathan Mantey pathString + "/Address"); 159301784826SJohnathan Mantey return; 1594e48c0fc5SRavi Teja } 1595e48c0fc5SRavi Teja 1596e48c0fc5SRavi Teja if (prefixLength) 1597e48c0fc5SRavi Teja { 159801784826SJohnathan Mantey prefix = *prefixLength; 159901784826SJohnathan Mantey } 160085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1601e48c0fc5SRavi Teja { 160285ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1603e48c0fc5SRavi Teja } 1604e48c0fc5SRavi Teja else 1605e48c0fc5SRavi Teja { 1606e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1607e48c0fc5SRavi Teja pathString + "/PrefixLength"); 160801784826SJohnathan Mantey return; 1609e48c0fc5SRavi Teja } 1610e48c0fc5SRavi Teja 161185ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1612e48c0fc5SRavi Teja { 161385ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1614e48c0fc5SRavi Teja asyncResp); 161585ffe86aSJiaqing Zhao nicIpEntry = 161685ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 161701784826SJohnathan Mantey } 161801784826SJohnathan Mantey else 161901784826SJohnathan Mantey { 162001784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1621e48c0fc5SRavi Teja } 1622e48c0fc5SRavi Teja entryIdx++; 1623e48c0fc5SRavi Teja } 162401784826SJohnathan Mantey else 162501784826SJohnathan Mantey { 162685ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 162701784826SJohnathan Mantey { 162801784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 162901784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 163001784826SJohnathan Mantey // in error, so bail out. 163101784826SJohnathan Mantey if (thisJson.is_null()) 163201784826SJohnathan Mantey { 163301784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 163401784826SJohnathan Mantey return; 163501784826SJohnathan Mantey } 163601784826SJohnathan Mantey messages::propertyValueFormatError( 163771f52d96SEd Tanous asyncResp->res, 163871f52d96SEd Tanous thisJson.dump(2, ' ', true, 163971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 164071f52d96SEd Tanous pathString); 164101784826SJohnathan Mantey return; 164201784826SJohnathan Mantey } 164301784826SJohnathan Mantey 164401784826SJohnathan Mantey if (thisJson.is_null()) 164501784826SJohnathan Mantey { 164685ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 164701784826SJohnathan Mantey } 164885ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 164901784826SJohnathan Mantey { 165085ffe86aSJiaqing Zhao nicIpEntry = 165185ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 165201784826SJohnathan Mantey } 165301784826SJohnathan Mantey entryIdx++; 165401784826SJohnathan Mantey } 165501784826SJohnathan Mantey } 1656e48c0fc5SRavi Teja } 1657e48c0fc5SRavi Teja 16584f48d5f6SEd Tanous inline void parseInterfaceData( 16598d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16608d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1661e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 166201784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 16634a0cb85cSEd Tanous { 1664eeedda23SJohnathan Mantey constexpr const std::array<const char*, 1> inventoryForEthernet = { 1665eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1666eeedda23SJohnathan Mantey 16672c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 166881ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 16692c70f800SEd Tanous jsonResponse["@odata.id"] = 167081ce609eSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 16712c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1672eeedda23SJohnathan Mantey 1673eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1674eeedda23SJohnathan Mantey 1675eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1676eeedda23SJohnathan Mantey [health](const boost::system::error_code ec, 1677b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& resp) { 1678eeedda23SJohnathan Mantey if (ec) 1679029573d4SEd Tanous { 1680eeedda23SJohnathan Mantey return; 1681eeedda23SJohnathan Mantey } 1682eeedda23SJohnathan Mantey 1683914e2d5dSEd Tanous health->inventory = resp; 1684eeedda23SJohnathan Mantey }, 1685eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", 1686eeedda23SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 1687bf648f77SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), 1688bf648f77SEd Tanous inventoryForEthernet); 1689eeedda23SJohnathan Mantey 1690eeedda23SJohnathan Mantey health->populate(); 1691eeedda23SJohnathan Mantey 1692eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1693eeedda23SJohnathan Mantey { 16942c70f800SEd Tanous jsonResponse["LinkStatus"] = "LinkUp"; 16952c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1696029573d4SEd Tanous } 1697029573d4SEd Tanous else 1698029573d4SEd Tanous { 16992c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 17002c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1701029573d4SEd Tanous } 1702aa05fb27SJohnathan Mantey 17032c70f800SEd Tanous jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 17042c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 170535fb5311STejas Patil jsonResponse["MTUSize"] = ethData.mtuSize; 1706*82695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 17072c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 1708*82695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 1709*82695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; 1710*82695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; 1711*82695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; 17121f8c7b5dSJohnathan Mantey 17132c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 1714*82695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" 17151f8c7b5dSJohnathan Mantey : "Disabled"; 1716*82695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; 1717*82695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; 1718*82695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; 17192a133282Smanojkiraneda 1720*82695a5bSJiaqing Zhao if (!ethData.hostName.empty()) 17214a0cb85cSEd Tanous { 1722*82695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 1723ab6554f1SJoshi-Mansi 1724ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1725ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1726ab6554f1SJoshi-Mansi // FQDN 1727*82695a5bSJiaqing Zhao std::string fqdn = ethData.hostName; 1728d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1729d24bfc7aSJennifer Lee { 17302c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1731d24bfc7aSJennifer Lee } 17322c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 17334a0cb85cSEd Tanous } 17344a0cb85cSEd Tanous 17352c70f800SEd Tanous jsonResponse["VLANs"] = { 1736bf648f77SEd Tanous {"@odata.id", 1737bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; 1738fda13ad2SSunitha Harish 17392c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 17402c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 17414a0cb85cSEd Tanous 17422c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 17432c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 17442c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 17452c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 17469eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 17474a0cb85cSEd Tanous { 1748fa5053a6SGunnar Mills 17492c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1750fa5053a6SGunnar Mills if (gatewayStr.empty()) 1751fa5053a6SGunnar Mills { 1752fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1753fa5053a6SGunnar Mills } 1754fa5053a6SGunnar Mills 17552c70f800SEd Tanous ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 17562c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 17572c70f800SEd Tanous {"Address", ipv4Config.address}, 1758fa5053a6SGunnar Mills {"Gateway", gatewayStr}}); 17592c70f800SEd Tanous if (ipv4Config.origin == "Static") 1760d1d50814SRavi Teja { 17612c70f800SEd Tanous ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 17622c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 17632c70f800SEd Tanous {"Address", ipv4Config.address}, 1764d1d50814SRavi Teja {"Gateway", gatewayStr}}); 1765d1d50814SRavi Teja } 176601784826SJohnathan Mantey } 1767d1d50814SRavi Teja 1768*82695a5bSJiaqing Zhao std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; 17697ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 17707ea79e5eSRavi Teja { 17717ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 17727ea79e5eSRavi Teja } 17737ea79e5eSRavi Teja 17747ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1775e48c0fc5SRavi Teja 17762c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 17772c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 17782c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 17792c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 17807f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 17812c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 17827f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 17839eb808c1SEd Tanous for (const auto& ipv6Config : ipv6Data) 1784e48c0fc5SRavi Teja { 17852c70f800SEd Tanous ipv6Array.push_back({{"Address", ipv6Config.address}, 17862c70f800SEd Tanous {"PrefixLength", ipv6Config.prefixLength}, 17872c70f800SEd Tanous {"AddressOrigin", ipv6Config.origin}, 17885fd16e4bSJohnathan Mantey {"AddressState", nullptr}}); 17892c70f800SEd Tanous if (ipv6Config.origin == "Static") 1790e48c0fc5SRavi Teja { 17912c70f800SEd Tanous ipv6StaticArray.push_back( 17922c70f800SEd Tanous {{"Address", ipv6Config.address}, 17937a474a5fSJiaqing Zhao {"PrefixLength", ipv6Config.prefixLength}}); 179401784826SJohnathan Mantey } 1795e48c0fc5SRavi Teja } 1796588c3f0dSKowalski, Kamil } 1797588c3f0dSKowalski, Kamil 17984f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 1799bf648f77SEd Tanous const std::string& parentIfaceId, 1800bf648f77SEd Tanous const std::string& ifaceId, 1801bf648f77SEd Tanous const EthernetInterfaceData& ethData) 18021abe55efSEd Tanous { 1803bf648f77SEd Tanous // Fill out obvious data... 1804bf648f77SEd Tanous jsonResponse["Id"] = ifaceId; 1805bf648f77SEd Tanous jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1806bf648f77SEd Tanous parentIfaceId + "/VLANs/" + ifaceId; 1807bf648f77SEd Tanous 1808bf648f77SEd Tanous jsonResponse["VLANEnable"] = true; 1809*82695a5bSJiaqing Zhao if (!ethData.vlanId.empty()) 1810bf648f77SEd Tanous { 1811*82695a5bSJiaqing Zhao jsonResponse["VLANId"] = ethData.vlanId.back(); 1812bf648f77SEd Tanous } 1813bf648f77SEd Tanous } 1814bf648f77SEd Tanous 18154f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1816bf648f77SEd Tanous { 1817dcf2ebc0SEd Tanous return boost::starts_with(iface, parent + "_"); 1818bf648f77SEd Tanous } 1819bf648f77SEd Tanous 1820bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1821bf648f77SEd Tanous { 1822bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1823ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 182445ca1b86SEd Tanous .methods(boost::beast::http::verb::get)([&app](const crow::Request& req, 182545ca1b86SEd Tanous const std::shared_ptr< 182645ca1b86SEd Tanous bmcweb::AsyncResp>& 182745ca1b86SEd Tanous asyncResp) { 182845ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 182945ca1b86SEd Tanous { 183045ca1b86SEd Tanous return; 183145ca1b86SEd Tanous } 183245ca1b86SEd Tanous 1833bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1834bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1835bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1836bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1837bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1838bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1839bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1840bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1841bf648f77SEd Tanous 1842bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1843bf648f77SEd Tanous // preparation 1844bf648f77SEd Tanous getEthernetIfaceList([asyncResp](const bool& success, 1845bf648f77SEd Tanous const boost::container::flat_set< 1846bf648f77SEd Tanous std::string>& ifaceList) { 1847bf648f77SEd Tanous if (!success) 18481abe55efSEd Tanous { 1849f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18509391bb9cSRapkiewicz, Pawel return; 18519391bb9cSRapkiewicz, Pawel } 18529391bb9cSRapkiewicz, Pawel 1853bf648f77SEd Tanous nlohmann::json& ifaceArray = 1854bf648f77SEd Tanous asyncResp->res.jsonValue["Members"]; 1855bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1856bf648f77SEd Tanous std::string tag = "_"; 1857bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1858bf648f77SEd Tanous { 1859bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1860bf648f77SEd Tanous if (found == std::string::npos) 1861bf648f77SEd Tanous { 1862bf648f77SEd Tanous ifaceArray.push_back( 1863bf648f77SEd Tanous {{"@odata.id", 1864bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1865bf648f77SEd Tanous ifaceItem}}); 1866bf648f77SEd Tanous } 1867bf648f77SEd Tanous } 1868bf648f77SEd Tanous 1869bf648f77SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 1870bf648f77SEd Tanous ifaceArray.size(); 1871bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1872bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1873bf648f77SEd Tanous }); 1874bf648f77SEd Tanous }); 1875bf648f77SEd Tanous 1876bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1877ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1878bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 187945ca1b86SEd Tanous [&app](const crow::Request& req, 1880bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1881bf648f77SEd Tanous const std::string& ifaceId) { 188245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 188345ca1b86SEd Tanous { 188445ca1b86SEd Tanous return; 188545ca1b86SEd Tanous } 18864a0cb85cSEd Tanous getEthernetIfaceData( 1887bf648f77SEd Tanous ifaceId, 1888bf648f77SEd Tanous [asyncResp, 1889bf648f77SEd Tanous ifaceId](const bool& success, 1890bf648f77SEd Tanous const EthernetInterfaceData& ethData, 1891bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 1892bf648f77SEd Tanous ipv4Data, 1893bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 1894bf648f77SEd Tanous ipv6Data) { 18954a0cb85cSEd Tanous if (!success) 18961abe55efSEd Tanous { 1897bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1898bf648f77SEd Tanous // existing object, and other errors 1899bf648f77SEd Tanous messages::resourceNotFound( 1900bf648f77SEd Tanous asyncResp->res, "EthernetInterface", ifaceId); 19014a0cb85cSEd Tanous return; 19029391bb9cSRapkiewicz, Pawel } 19034c9afe43SEd Tanous 19040f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1905fda13ad2SSunitha Harish "#EthernetInterface.v1_4_1.EthernetInterface"; 1906bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1907bf648f77SEd Tanous "Manager Ethernet Interface"; 19080f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 19090f74e643SEd Tanous "Management Network Interface"; 19100f74e643SEd Tanous 1911bf648f77SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, 1912bf648f77SEd Tanous ipv4Data, ipv6Data); 19139391bb9cSRapkiewicz, Pawel }); 1914bf648f77SEd Tanous }); 19159391bb9cSRapkiewicz, Pawel 1916bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1917ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1918bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 191945ca1b86SEd Tanous [&app](const crow::Request& req, 1920bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1921bf648f77SEd Tanous const std::string& ifaceId) { 192245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 192345ca1b86SEd Tanous { 192445ca1b86SEd Tanous return; 192545ca1b86SEd Tanous } 1926bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1927ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1928d577665bSRatan Gupta std::optional<std::string> macAddress; 19299a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1930d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1931e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1932f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1933da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 19341f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1935eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 193635fb5311STejas Patil std::optional<size_t> mtuSize; 19371f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 19381f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 19390627a2c7SEd Tanous 194015ed6780SWilly Tu if (!json_util::readJsonPatch( 19418d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1942bf648f77SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, 1943bf648f77SEd Tanous "MACAddress", macAddress, "StaticNameServers", 1944bf648f77SEd Tanous staticNameServers, "IPv6DefaultGateway", 1945bf648f77SEd Tanous ipv6DefaultGateway, "IPv6StaticAddresses", 1946ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 194735fb5311STejas Patil "MTUSize", mtuSize, "InterfaceEnabled", 194835fb5311STejas Patil interfaceEnabled)) 19491abe55efSEd Tanous { 1950588c3f0dSKowalski, Kamil return; 1951588c3f0dSKowalski, Kamil } 1952da131a9aSJennifer Lee if (dhcpv4) 1953da131a9aSJennifer Lee { 1954bf648f77SEd Tanous if (!json_util::readJson( 1955bf648f77SEd Tanous *dhcpv4, asyncResp->res, "DHCPEnabled", 19561f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 1957*82695a5bSJiaqing Zhao v4dhcpParms.useDnsServers, "UseNTPServers", 1958*82695a5bSJiaqing Zhao v4dhcpParms.useNtpServers, "UseDomainName", 1959*82695a5bSJiaqing Zhao v4dhcpParms.useDomainName)) 19601f8c7b5dSJohnathan Mantey { 19611f8c7b5dSJohnathan Mantey return; 19621f8c7b5dSJohnathan Mantey } 19631f8c7b5dSJohnathan Mantey } 19641f8c7b5dSJohnathan Mantey 19651f8c7b5dSJohnathan Mantey if (dhcpv6) 19661f8c7b5dSJohnathan Mantey { 1967bf648f77SEd Tanous if (!json_util::readJson( 1968bf648f77SEd Tanous *dhcpv6, asyncResp->res, "OperatingMode", 1969bf648f77SEd Tanous v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers", 1970*82695a5bSJiaqing Zhao v6dhcpParms.useDnsServers, "UseNTPServers", 1971*82695a5bSJiaqing Zhao v6dhcpParms.useNtpServers, "UseDomainName", 1972*82695a5bSJiaqing Zhao v6dhcpParms.useDomainName)) 19731f8c7b5dSJohnathan Mantey { 19741f8c7b5dSJohnathan Mantey return; 19751f8c7b5dSJohnathan Mantey } 1976da131a9aSJennifer Lee } 1977da131a9aSJennifer Lee 1978bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1979bf648f77SEd Tanous // for JSON preparation 19804a0cb85cSEd Tanous getEthernetIfaceData( 19812c70f800SEd Tanous ifaceId, 1982bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 1983ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1984d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 19859a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1986e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 19871f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 19881f8c7b5dSJohnathan Mantey dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 198935fb5311STejas Patil mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms), 1990f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1991bf648f77SEd Tanous const bool& success, 1992bf648f77SEd Tanous const EthernetInterfaceData& ethData, 1993bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 1994bf648f77SEd Tanous ipv4Data, 1995bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 1996bf648f77SEd Tanous ipv6Data) { 19971abe55efSEd Tanous if (!success) 19981abe55efSEd Tanous { 1999588c3f0dSKowalski, Kamil // ... otherwise return error 2000bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2001bf648f77SEd Tanous // existing object, and other errors 2002bf648f77SEd Tanous messages::resourceNotFound( 2003bf648f77SEd Tanous asyncResp->res, "Ethernet Interface", ifaceId); 2004588c3f0dSKowalski, Kamil return; 2005588c3f0dSKowalski, Kamil } 2006588c3f0dSKowalski, Kamil 20071f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 20081f8c7b5dSJohnathan Mantey { 2009bf648f77SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, 2010bf648f77SEd Tanous v6dhcpParms, asyncResp); 20111f8c7b5dSJohnathan Mantey } 20121f8c7b5dSJohnathan Mantey 20130627a2c7SEd Tanous if (hostname) 20141abe55efSEd Tanous { 20150627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 20161abe55efSEd Tanous } 20170627a2c7SEd Tanous 2018ab6554f1SJoshi-Mansi if (fqdn) 2019ab6554f1SJoshi-Mansi { 20202c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2021ab6554f1SJoshi-Mansi } 2022ab6554f1SJoshi-Mansi 2023d577665bSRatan Gupta if (macAddress) 2024d577665bSRatan Gupta { 2025bf648f77SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, 2026bf648f77SEd Tanous asyncResp); 2027d577665bSRatan Gupta } 2028d577665bSRatan Gupta 2029d1d50814SRavi Teja if (ipv4StaticAddresses) 2030d1d50814SRavi Teja { 2031bf648f77SEd Tanous // TODO(ed) for some reason the capture of 2032bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 2033bf648f77SEd Tanous // not a non-const value. This doesn't really work 2034bf648f77SEd Tanous // for us, as we need to be able to efficiently move 2035bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 2036bf648f77SEd Tanous // makes a copy of the structure, and operates on 2037bf648f77SEd Tanous // that, but could be done more efficiently 2038f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 20392c70f800SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2040d1d50814SRavi Teja asyncResp); 20411abe55efSEd Tanous } 20420627a2c7SEd Tanous 2043f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 2044f85837bfSRAJESWARAN THILLAIGOVINDAN { 2045bf648f77SEd Tanous handleStaticNameServersPatch( 2046bf648f77SEd Tanous ifaceId, *staticNameServers, asyncResp); 2047f85837bfSRAJESWARAN THILLAIGOVINDAN } 20489a6fc6feSRavi Teja 20499a6fc6feSRavi Teja if (ipv6DefaultGateway) 20509a6fc6feSRavi Teja { 20519a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 20529a6fc6feSRavi Teja "IPv6DefaultGateway"); 20539a6fc6feSRavi Teja } 2054e48c0fc5SRavi Teja 2055e48c0fc5SRavi Teja if (ipv6StaticAddresses) 2056e48c0fc5SRavi Teja { 2057f5b191a6SEd Tanous const nlohmann::json& ipv6Static = 2058f5b191a6SEd Tanous *ipv6StaticAddresses; 20592c70f800SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 206001784826SJohnathan Mantey ipv6Data, asyncResp); 2061e48c0fc5SRavi Teja } 2062eeedda23SJohnathan Mantey 2063eeedda23SJohnathan Mantey if (interfaceEnabled) 2064eeedda23SJohnathan Mantey { 2065eeedda23SJohnathan Mantey setEthernetInterfaceBoolProperty( 2066bf648f77SEd Tanous ifaceId, "NICEnabled", *interfaceEnabled, 2067bf648f77SEd Tanous asyncResp); 2068eeedda23SJohnathan Mantey } 206935fb5311STejas Patil 207035fb5311STejas Patil if (mtuSize) 207135fb5311STejas Patil { 207235fb5311STejas Patil handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); 207335fb5311STejas Patil } 2074588c3f0dSKowalski, Kamil }); 2075bf648f77SEd Tanous }); 20769391bb9cSRapkiewicz, Pawel 2077bf648f77SEd Tanous BMCWEB_ROUTE( 2078bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2079ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 2080bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 208145ca1b86SEd Tanous [&app](const crow::Request& req, 2082bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 208345ca1b86SEd Tanous const std::string& parentIfaceId, 208445ca1b86SEd Tanous const std::string& ifaceId) { 208545ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 208645ca1b86SEd Tanous { 208745ca1b86SEd Tanous return; 208845ca1b86SEd Tanous } 20898d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 20900f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 20918d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2092e439f0f8SKowalski, Kamil 20932c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 20941abe55efSEd Tanous { 2095a434f2bdSEd Tanous return; 2096a434f2bdSEd Tanous } 2097a434f2bdSEd Tanous 2098bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2099bf648f77SEd Tanous // for JSON preparation 21004a0cb85cSEd Tanous getEthernetIfaceData( 2101bf648f77SEd Tanous ifaceId, 2102bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2103bf648f77SEd Tanous const bool& success, 2104bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2105cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2106cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2107*82695a5bSJiaqing Zhao if (success && !ethData.vlanId.empty()) 21081abe55efSEd Tanous { 2109bf648f77SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, 2110bf648f77SEd Tanous parentIfaceId, ifaceId, ethData); 21111abe55efSEd Tanous } 21121abe55efSEd Tanous else 21131abe55efSEd Tanous { 2114e439f0f8SKowalski, Kamil // ... otherwise return error 2115bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2116bf648f77SEd Tanous // existing object, and other errors 2117bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2118bf648f77SEd Tanous "VLAN Network Interface", 2119bf648f77SEd Tanous ifaceId); 2120e439f0f8SKowalski, Kamil } 2121e439f0f8SKowalski, Kamil }); 2122bf648f77SEd Tanous }); 2123e439f0f8SKowalski, Kamil 2124bf648f77SEd Tanous BMCWEB_ROUTE( 2125bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2126ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2127ed398213SEd Tanous //.privileges(redfish::privileges::patchVLanNetworkInterface) 2128432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2129bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 213045ca1b86SEd Tanous [&app](const crow::Request& req, 2131bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 213245ca1b86SEd Tanous const std::string& parentIfaceId, 213345ca1b86SEd Tanous const std::string& ifaceId) { 213445ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 213545ca1b86SEd Tanous { 213645ca1b86SEd Tanous return; 213745ca1b86SEd Tanous } 2138fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 21391abe55efSEd Tanous { 2140bf648f77SEd Tanous messages::resourceNotFound( 2141bf648f77SEd Tanous asyncResp->res, "VLAN Network Interface", ifaceId); 2142927a505aSKowalski, Kamil return; 2143927a505aSKowalski, Kamil } 2144927a505aSKowalski, Kamil 21450627a2c7SEd Tanous bool vlanEnable = false; 214638268fa8SAndrew Geissler uint32_t vlanId = 0; 21470627a2c7SEd Tanous 214815ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable", 2149bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 21501abe55efSEd Tanous { 2151927a505aSKowalski, Kamil return; 2152927a505aSKowalski, Kamil } 2153927a505aSKowalski, Kamil 2154bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2155bf648f77SEd Tanous // for JSON preparation 2156e48c0fc5SRavi Teja getEthernetIfaceData( 2157bf648f77SEd Tanous ifaceId, 2158bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId]( 2159bf648f77SEd Tanous const bool& success, 2160bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2161cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2162cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2163*82695a5bSJiaqing Zhao if (success && !ethData.vlanId.empty()) 216408244d02SSunitha Harish { 216508244d02SSunitha Harish auto callback = 2166bf648f77SEd Tanous [asyncResp]( 2167bf648f77SEd Tanous const boost::system::error_code ec) { 216808244d02SSunitha Harish if (ec) 216908244d02SSunitha Harish { 217008244d02SSunitha Harish messages::internalError(asyncResp->res); 217108244d02SSunitha Harish } 217208244d02SSunitha Harish }; 217308244d02SSunitha Harish 2174e05aec50SEd Tanous if (vlanEnable) 217508244d02SSunitha Harish { 217608244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2177bf648f77SEd Tanous std::move(callback), 2178bf648f77SEd Tanous "xyz.openbmc_project.Network", 217908244d02SSunitha Harish "/xyz/openbmc_project/network/" + ifaceId, 218008244d02SSunitha Harish "org.freedesktop.DBus.Properties", "Set", 218108244d02SSunitha Harish "xyz.openbmc_project.Network.VLAN", "Id", 2182168e20c1SEd Tanous dbus::utility::DbusVariantType(vlanId)); 218308244d02SSunitha Harish } 218408244d02SSunitha Harish else 218508244d02SSunitha Harish { 2186bf648f77SEd Tanous BMCWEB_LOG_DEBUG 2187bf648f77SEd Tanous << "vlanEnable is false. Deleting the " 2188e48c0fc5SRavi Teja "vlan interface"; 218908244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2190bf648f77SEd Tanous std::move(callback), 2191bf648f77SEd Tanous "xyz.openbmc_project.Network", 2192bf648f77SEd Tanous std::string( 2193bf648f77SEd Tanous "/xyz/openbmc_project/network/") + 2194e48c0fc5SRavi Teja ifaceId, 2195bf648f77SEd Tanous "xyz.openbmc_project.Object.Delete", 2196bf648f77SEd Tanous "Delete"); 219708244d02SSunitha Harish } 219808244d02SSunitha Harish } 219908244d02SSunitha Harish else 22001abe55efSEd Tanous { 2201bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2202bf648f77SEd Tanous // existing object, and other errors 2203bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2204bf648f77SEd Tanous "VLAN Network Interface", 2205bf648f77SEd Tanous ifaceId); 2206bf648f77SEd Tanous return; 2207bf648f77SEd Tanous } 2208bf648f77SEd Tanous }); 2209bf648f77SEd Tanous }); 2210bf648f77SEd Tanous 2211bf648f77SEd Tanous BMCWEB_ROUTE( 2212bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2213ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2214ed398213SEd Tanous //.privileges(redfish::privileges::deleteVLanNetworkInterface) 2215432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2216bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 221745ca1b86SEd Tanous [&app](const crow::Request& req, 2218bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 221945ca1b86SEd Tanous const std::string& parentIfaceId, 222045ca1b86SEd Tanous const std::string& ifaceId) { 222145ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 222245ca1b86SEd Tanous { 222345ca1b86SEd Tanous return; 222445ca1b86SEd Tanous } 2225bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2226bf648f77SEd Tanous { 2227e48c0fc5SRavi Teja messages::resourceNotFound( 2228e48c0fc5SRavi Teja asyncResp->res, "VLAN Network Interface", ifaceId); 2229927a505aSKowalski, Kamil return; 2230927a505aSKowalski, Kamil } 2231e439f0f8SKowalski, Kamil 2232bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2233bf648f77SEd Tanous // for JSON preparation 2234f12894f8SJason M. Bills getEthernetIfaceData( 2235bf648f77SEd Tanous ifaceId, 2236bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2237bf648f77SEd Tanous const bool& success, 2238bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2239cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2240cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2241*82695a5bSJiaqing Zhao if (success && !ethData.vlanId.empty()) 22421abe55efSEd Tanous { 2243f12894f8SJason M. Bills auto callback = 2244bf648f77SEd Tanous [asyncResp]( 2245bf648f77SEd Tanous const boost::system::error_code ec) { 22461abe55efSEd Tanous if (ec) 22471abe55efSEd Tanous { 2248f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2249927a505aSKowalski, Kamil } 22504a0cb85cSEd Tanous }; 22514a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2252bf648f77SEd Tanous std::move(callback), 2253bf648f77SEd Tanous "xyz.openbmc_project.Network", 2254bf648f77SEd Tanous std::string("/xyz/openbmc_project/network/") + 2255bf648f77SEd Tanous ifaceId, 22564a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 22571abe55efSEd Tanous } 22581abe55efSEd Tanous else 22591abe55efSEd Tanous { 2260927a505aSKowalski, Kamil // ... otherwise return error 2261bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2262bf648f77SEd Tanous // existing object, and other errors 2263bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2264bf648f77SEd Tanous "VLAN Network Interface", 2265bf648f77SEd Tanous ifaceId); 2266927a505aSKowalski, Kamil } 2267927a505aSKowalski, Kamil }); 2268bf648f77SEd Tanous }); 2269e439f0f8SKowalski, Kamil 2270bf648f77SEd Tanous BMCWEB_ROUTE(app, 2271bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2272ed398213SEd Tanous 2273ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 2274bf648f77SEd Tanous .methods( 2275bf648f77SEd Tanous boost::beast::http::verb:: 227645ca1b86SEd Tanous get)([&app](const crow::Request& req, 2277bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2278bf648f77SEd Tanous const std::string& rootInterfaceName) { 227945ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 228045ca1b86SEd Tanous { 228145ca1b86SEd Tanous return; 228245ca1b86SEd Tanous } 22834a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 22841abe55efSEd Tanous // preparation 2285bf648f77SEd Tanous getEthernetIfaceList([asyncResp, rootInterfaceName]( 22861abe55efSEd Tanous const bool& success, 2287bf648f77SEd Tanous const boost::container::flat_set< 2288bf648f77SEd Tanous std::string>& ifaceList) { 22894a0cb85cSEd Tanous if (!success) 22901abe55efSEd Tanous { 2291f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22924a0cb85cSEd Tanous return; 22931abe55efSEd Tanous } 22944c9afe43SEd Tanous 229581ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 22964c9afe43SEd Tanous { 22974c9afe43SEd Tanous messages::resourceNotFound(asyncResp->res, 22984c9afe43SEd Tanous "VLanNetworkInterfaceCollection", 22994c9afe43SEd Tanous rootInterfaceName); 23004c9afe43SEd Tanous return; 23014c9afe43SEd Tanous } 23024c9afe43SEd Tanous 23030f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 23040f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 23050f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 23060f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 23070f74e643SEd Tanous "VLAN Network Interface Collection"; 23084a0cb85cSEd Tanous 23092c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 23104a0cb85cSEd Tanous 231181ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 23121abe55efSEd Tanous { 23132c70f800SEd Tanous if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 23144a0cb85cSEd Tanous { 2315f23b7296SEd Tanous std::string path = 2316f23b7296SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2317f23b7296SEd Tanous path += rootInterfaceName; 2318f23b7296SEd Tanous path += "/VLANs/"; 2319f23b7296SEd Tanous path += ifaceItem; 2320f23b7296SEd Tanous ifaceArray.push_back({{"@odata.id", std::move(path)}}); 2321e439f0f8SKowalski, Kamil } 2322e439f0f8SKowalski, Kamil } 2323e439f0f8SKowalski, Kamil 23244a0cb85cSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 23252c70f800SEd Tanous ifaceArray.size(); 23262c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 23274a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 23284a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 23294a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2330e439f0f8SKowalski, Kamil }); 2331bf648f77SEd Tanous }); 2332e439f0f8SKowalski, Kamil 2333bf648f77SEd Tanous BMCWEB_ROUTE(app, 2334bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2335ed398213SEd Tanous // This privilege is wrong, it should be ConfigureManager 2336ed398213SEd Tanous //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2337432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2338bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 233945ca1b86SEd Tanous [&app](const crow::Request& req, 2340bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2341bf648f77SEd Tanous const std::string& rootInterfaceName) { 234245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 234345ca1b86SEd Tanous { 234445ca1b86SEd Tanous return; 234545ca1b86SEd Tanous } 2346fda13ad2SSunitha Harish bool vlanEnable = false; 23470627a2c7SEd Tanous uint32_t vlanId = 0; 234815ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", 234915ed6780SWilly Tu vlanId, "VLANEnable", vlanEnable)) 23501abe55efSEd Tanous { 23514a0cb85cSEd Tanous return; 2352e439f0f8SKowalski, Kamil } 2353fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2354dbb59d4dSEd Tanous if (vlanId == 0U) 2355fda13ad2SSunitha Harish { 2356fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2357fda13ad2SSunitha Harish } 2358fda13ad2SSunitha Harish if (!vlanEnable) 2359fda13ad2SSunitha Harish { 2360fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2361fda13ad2SSunitha Harish } 2362271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2363fda13ad2SSunitha Harish { 2364fda13ad2SSunitha Harish return; 2365fda13ad2SSunitha Harish } 2366fda13ad2SSunitha Harish 2367bf648f77SEd Tanous auto callback = 2368bf648f77SEd Tanous [asyncResp](const boost::system::error_code ec) { 23691abe55efSEd Tanous if (ec) 23701abe55efSEd Tanous { 2371bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2372bf648f77SEd Tanous // based on phosphor-network responses 2373f12894f8SJason M. Bills messages::internalError(asyncResp->res); 23744a0cb85cSEd Tanous return; 23751abe55efSEd Tanous } 2376f12894f8SJason M. Bills messages::created(asyncResp->res); 2377e439f0f8SKowalski, Kamil }; 23784a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 23794a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 23804a0cb85cSEd Tanous "/xyz/openbmc_project/network", 23814a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 23820627a2c7SEd Tanous rootInterfaceName, vlanId); 2383bf648f77SEd Tanous }); 23844a0cb85cSEd Tanous } 2385bf648f77SEd Tanous 23869391bb9cSRapkiewicz, Pawel } // namespace redfish 2387