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> 191abe55efSEd Tanous #include <boost/container/flat_map.hpp> 204a0cb85cSEd Tanous #include <boost/container/flat_set.hpp> 21179db1d7SKowalski, Kamil #include <dbus_singleton.hpp> 22168e20c1SEd Tanous #include <dbus_utility.hpp> 23588c3f0dSKowalski, Kamil #include <error_messages.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 339391bb9cSRapkiewicz, Pawel /** 349391bb9cSRapkiewicz, Pawel * DBus types primitives for several generic DBus interfaces 359391bb9cSRapkiewicz, Pawel * TODO(Pawel) consider move this to separate file into boost::dbus 369391bb9cSRapkiewicz, Pawel */ 37168e20c1SEd Tanous using PropertiesMapType = 38168e20c1SEd Tanous boost::container::flat_map<std::string, dbus::utility::DbusVariantType>; 399391bb9cSRapkiewicz, Pawel 404a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair< 41aa2e59c1SEd Tanous sdbusplus::message::object_path, 42168e20c1SEd Tanous std::vector<std::pair<std::string, 43aa2e59c1SEd Tanous boost::container::flat_map< 44168e20c1SEd Tanous std::string, dbus::utility::DbusVariantType>>>>>; 454a0cb85cSEd Tanous 464a0cb85cSEd Tanous enum class LinkType 474a0cb85cSEd Tanous { 484a0cb85cSEd Tanous Local, 494a0cb85cSEd Tanous Global 504a0cb85cSEd Tanous }; 519391bb9cSRapkiewicz, Pawel 529391bb9cSRapkiewicz, Pawel /** 539391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 549391bb9cSRapkiewicz, Pawel */ 551abe55efSEd Tanous struct IPv4AddressData 561abe55efSEd Tanous { 57179db1d7SKowalski, Kamil std::string id; 584a0cb85cSEd Tanous std::string address; 594a0cb85cSEd Tanous std::string domain; 604a0cb85cSEd Tanous std::string gateway; 619391bb9cSRapkiewicz, Pawel std::string netmask; 629391bb9cSRapkiewicz, Pawel std::string origin; 634a0cb85cSEd Tanous LinkType linktype; 6401c6e858SSunitha Harish bool isActive; 654a0cb85cSEd Tanous 661abe55efSEd Tanous bool operator<(const IPv4AddressData& obj) const 671abe55efSEd Tanous { 684a0cb85cSEd Tanous return id < obj.id; 691abe55efSEd Tanous } 709391bb9cSRapkiewicz, Pawel }; 719391bb9cSRapkiewicz, Pawel 729391bb9cSRapkiewicz, Pawel /** 73e48c0fc5SRavi Teja * Structure for keeping IPv6 data required by Redfish 74e48c0fc5SRavi Teja */ 75e48c0fc5SRavi Teja struct IPv6AddressData 76e48c0fc5SRavi Teja { 77e48c0fc5SRavi Teja std::string id; 78e48c0fc5SRavi Teja std::string address; 79e48c0fc5SRavi Teja std::string origin; 80e48c0fc5SRavi Teja uint8_t prefixLength; 81e48c0fc5SRavi Teja 82e48c0fc5SRavi Teja bool operator<(const IPv6AddressData& obj) const 83e48c0fc5SRavi Teja { 84e48c0fc5SRavi Teja return id < obj.id; 85e48c0fc5SRavi Teja } 86e48c0fc5SRavi Teja }; 87e48c0fc5SRavi Teja /** 889391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 899391bb9cSRapkiewicz, Pawel * available from DBus 909391bb9cSRapkiewicz, Pawel */ 911abe55efSEd Tanous struct EthernetInterfaceData 921abe55efSEd Tanous { 934a0cb85cSEd Tanous uint32_t speed; 944a0cb85cSEd Tanous bool auto_neg; 951f8c7b5dSJohnathan Mantey bool DNSEnabled; 961f8c7b5dSJohnathan Mantey bool NTPEnabled; 971f8c7b5dSJohnathan Mantey bool HostNameEnabled; 981f8c7b5dSJohnathan Mantey bool SendHostNameEnabled; 99aa05fb27SJohnathan Mantey bool linkUp; 100eeedda23SJohnathan Mantey bool nicEnabled; 1011f8c7b5dSJohnathan Mantey std::string DHCPEnabled; 1021f8c7b5dSJohnathan Mantey std::string operatingMode; 1034a0cb85cSEd Tanous std::string hostname; 1044a0cb85cSEd Tanous std::string default_gateway; 1059a6fc6feSRavi Teja std::string ipv6_default_gateway; 1064a0cb85cSEd Tanous std::string mac_address; 107fda13ad2SSunitha Harish std::vector<std::uint32_t> vlan_id; 1080f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 1090f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 110d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 1119391bb9cSRapkiewicz, Pawel }; 1129391bb9cSRapkiewicz, Pawel 1131f8c7b5dSJohnathan Mantey struct DHCPParameters 1141f8c7b5dSJohnathan Mantey { 1151f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 1161f8c7b5dSJohnathan Mantey std::optional<bool> useDNSServers; 1171f8c7b5dSJohnathan Mantey std::optional<bool> useNTPServers; 1181f8c7b5dSJohnathan Mantey std::optional<bool> useUseDomainName; 1191f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1201f8c7b5dSJohnathan Mantey }; 1211f8c7b5dSJohnathan Mantey 1229391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1239391bb9cSRapkiewicz, Pawel // into full dot notation 1241abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1251abe55efSEd Tanous { 1269391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1279391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1289391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1299391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1309391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1319391bb9cSRapkiewicz, Pawel return netmask; 1329391bb9cSRapkiewicz, Pawel } 1339391bb9cSRapkiewicz, Pawel 1341f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP, 1351f8c7b5dSJohnathan Mantey bool isIPv4) 1361f8c7b5dSJohnathan Mantey { 1371f8c7b5dSJohnathan Mantey if (isIPv4) 1381f8c7b5dSJohnathan Mantey { 1391f8c7b5dSJohnathan Mantey return ( 1401f8c7b5dSJohnathan Mantey (inputDHCP == 1411f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1421f8c7b5dSJohnathan Mantey (inputDHCP == 1431f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1441f8c7b5dSJohnathan Mantey } 1451f8c7b5dSJohnathan Mantey return ((inputDHCP == 1461f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1471f8c7b5dSJohnathan Mantey (inputDHCP == 1481f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1491f8c7b5dSJohnathan Mantey } 1501f8c7b5dSJohnathan Mantey 1512c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1521f8c7b5dSJohnathan Mantey { 1531f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1541f8c7b5dSJohnathan Mantey { 1551f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1561f8c7b5dSJohnathan Mantey } 1573174e4dfSEd Tanous if (isIPv4) 1581f8c7b5dSJohnathan Mantey { 1591f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1601f8c7b5dSJohnathan Mantey } 1613174e4dfSEd Tanous if (isIPv6) 1621f8c7b5dSJohnathan Mantey { 1631f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1641f8c7b5dSJohnathan Mantey } 1651f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1661f8c7b5dSJohnathan Mantey } 1671f8c7b5dSJohnathan Mantey 1684a0cb85cSEd Tanous inline std::string 1694a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1704a0cb85cSEd Tanous bool isIPv4) 1711abe55efSEd Tanous { 1724a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1731abe55efSEd Tanous { 1744a0cb85cSEd Tanous return "Static"; 1759391bb9cSRapkiewicz, Pawel } 1764a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1771abe55efSEd Tanous { 1784a0cb85cSEd Tanous if (isIPv4) 1791abe55efSEd Tanous { 1804a0cb85cSEd Tanous return "IPv4LinkLocal"; 1811abe55efSEd Tanous } 1824a0cb85cSEd Tanous return "LinkLocal"; 1839391bb9cSRapkiewicz, Pawel } 1844a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1851abe55efSEd Tanous { 1864a0cb85cSEd Tanous if (isIPv4) 1874a0cb85cSEd Tanous { 1884a0cb85cSEd Tanous return "DHCP"; 1894a0cb85cSEd Tanous } 1904a0cb85cSEd Tanous return "DHCPv6"; 1914a0cb85cSEd Tanous } 1924a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1934a0cb85cSEd Tanous { 1944a0cb85cSEd Tanous return "SLAAC"; 1954a0cb85cSEd Tanous } 1964a0cb85cSEd Tanous return ""; 1974a0cb85cSEd Tanous } 1984a0cb85cSEd Tanous 19981ce609eSEd Tanous inline bool extractEthernetInterfaceData(const std::string& ethifaceId, 20081ce609eSEd Tanous GetManagedObjects& dbusData, 2014a0cb85cSEd Tanous EthernetInterfaceData& ethData) 2024a0cb85cSEd Tanous { 2034c9afe43SEd Tanous bool idFound = false; 20481ce609eSEd Tanous for (auto& objpath : dbusData) 2054a0cb85cSEd Tanous { 206f23b7296SEd Tanous for (auto& ifacePair : objpath.second) 2074a0cb85cSEd Tanous { 20881ce609eSEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 209029573d4SEd Tanous { 2104c9afe43SEd Tanous idFound = true; 2114a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2124a0cb85cSEd Tanous { 2134a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2144a0cb85cSEd Tanous { 2154a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2164a0cb85cSEd Tanous { 2174a0cb85cSEd Tanous const std::string* mac = 218abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2194a0cb85cSEd Tanous if (mac != nullptr) 2204a0cb85cSEd Tanous { 2214a0cb85cSEd Tanous ethData.mac_address = *mac; 2224a0cb85cSEd Tanous } 2234a0cb85cSEd Tanous } 2244a0cb85cSEd Tanous } 2254a0cb85cSEd Tanous } 2264a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2274a0cb85cSEd Tanous { 2284a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2294a0cb85cSEd Tanous { 2304a0cb85cSEd Tanous if (propertyPair.first == "Id") 2314a0cb85cSEd Tanous { 2321b6b96c5SEd Tanous const uint32_t* id = 233abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2344a0cb85cSEd Tanous if (id != nullptr) 2354a0cb85cSEd Tanous { 236fda13ad2SSunitha Harish ethData.vlan_id.push_back(*id); 2374a0cb85cSEd Tanous } 2384a0cb85cSEd Tanous } 2394a0cb85cSEd Tanous } 2404a0cb85cSEd Tanous } 2414a0cb85cSEd Tanous else if (ifacePair.first == 2424a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2434a0cb85cSEd Tanous { 2444a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2454a0cb85cSEd Tanous { 2464a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2474a0cb85cSEd Tanous { 2482c70f800SEd Tanous const bool* autoNeg = 249abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2502c70f800SEd Tanous if (autoNeg != nullptr) 2514a0cb85cSEd Tanous { 2522c70f800SEd Tanous ethData.auto_neg = *autoNeg; 2534a0cb85cSEd Tanous } 2544a0cb85cSEd Tanous } 2554a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2564a0cb85cSEd Tanous { 2574a0cb85cSEd Tanous const uint32_t* speed = 258abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2594a0cb85cSEd Tanous if (speed != nullptr) 2604a0cb85cSEd Tanous { 2614a0cb85cSEd Tanous ethData.speed = *speed; 2624a0cb85cSEd Tanous } 2634a0cb85cSEd Tanous } 264aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 265aa05fb27SJohnathan Mantey { 266aa05fb27SJohnathan Mantey const bool* linkUp = 267aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 268aa05fb27SJohnathan Mantey if (linkUp != nullptr) 269aa05fb27SJohnathan Mantey { 270aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 271aa05fb27SJohnathan Mantey } 272aa05fb27SJohnathan Mantey } 273eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 274eeedda23SJohnathan Mantey { 275eeedda23SJohnathan Mantey const bool* nicEnabled = 276eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 277eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 278eeedda23SJohnathan Mantey { 279eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 280eeedda23SJohnathan Mantey } 281eeedda23SJohnathan Mantey } 282f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 283029573d4SEd Tanous { 284029573d4SEd Tanous const std::vector<std::string>* nameservers = 2858d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 286029573d4SEd Tanous &propertyPair.second); 287029573d4SEd Tanous if (nameservers != nullptr) 288029573d4SEd Tanous { 289f23b7296SEd Tanous ethData.nameServers = *nameservers; 2900f6efdc1Smanojkiran.eda@gmail.com } 2910f6efdc1Smanojkiran.eda@gmail.com } 2920f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 2930f6efdc1Smanojkiran.eda@gmail.com { 2940f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 2958d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 2960f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 2970f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 2980f6efdc1Smanojkiran.eda@gmail.com { 299f23b7296SEd Tanous ethData.staticNameServers = *staticNameServers; 3004a0cb85cSEd Tanous } 3014a0cb85cSEd Tanous } 3022a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3032a133282Smanojkiraneda { 3042c70f800SEd Tanous const std::string* dhcpEnabled = 3051f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 3062c70f800SEd Tanous if (dhcpEnabled != nullptr) 3072a133282Smanojkiraneda { 3082c70f800SEd Tanous ethData.DHCPEnabled = *dhcpEnabled; 3092a133282Smanojkiraneda } 3102a133282Smanojkiraneda } 311d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 312d24bfc7aSJennifer Lee { 313d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3148d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 315d24bfc7aSJennifer Lee &propertyPair.second); 316d24bfc7aSJennifer Lee if (domainNames != nullptr) 317d24bfc7aSJennifer Lee { 318f23b7296SEd Tanous ethData.domainnames = *domainNames; 319d24bfc7aSJennifer Lee } 320d24bfc7aSJennifer Lee } 3219010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway") 3229010ec2eSRavi Teja { 3239010ec2eSRavi Teja const std::string* defaultGateway = 3249010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3259010ec2eSRavi Teja if (defaultGateway != nullptr) 3269010ec2eSRavi Teja { 3279010ec2eSRavi Teja std::string defaultGatewayStr = *defaultGateway; 3289010ec2eSRavi Teja if (defaultGatewayStr.empty()) 3299010ec2eSRavi Teja { 3309010ec2eSRavi Teja ethData.default_gateway = "0.0.0.0"; 3319010ec2eSRavi Teja } 3329010ec2eSRavi Teja else 3339010ec2eSRavi Teja { 3349010ec2eSRavi Teja ethData.default_gateway = defaultGatewayStr; 3359010ec2eSRavi Teja } 3369010ec2eSRavi Teja } 3379010ec2eSRavi Teja } 3389010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway6") 3399010ec2eSRavi Teja { 3409010ec2eSRavi Teja const std::string* defaultGateway6 = 3419010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3429010ec2eSRavi Teja if (defaultGateway6 != nullptr) 3439010ec2eSRavi Teja { 3449010ec2eSRavi Teja std::string defaultGateway6Str = 3459010ec2eSRavi Teja *defaultGateway6; 3469010ec2eSRavi Teja if (defaultGateway6Str.empty()) 3479010ec2eSRavi Teja { 3489010ec2eSRavi Teja ethData.ipv6_default_gateway = 3499010ec2eSRavi Teja "0:0:0:0:0:0:0:0"; 3509010ec2eSRavi Teja } 3519010ec2eSRavi Teja else 3529010ec2eSRavi Teja { 3539010ec2eSRavi Teja ethData.ipv6_default_gateway = 3549010ec2eSRavi Teja defaultGateway6Str; 3559010ec2eSRavi Teja } 3569010ec2eSRavi Teja } 3579010ec2eSRavi Teja } 358029573d4SEd Tanous } 359029573d4SEd Tanous } 360029573d4SEd Tanous } 3611f8c7b5dSJohnathan Mantey 3621f8c7b5dSJohnathan Mantey if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 3631f8c7b5dSJohnathan Mantey { 3641f8c7b5dSJohnathan Mantey if (ifacePair.first == 3651f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3661f8c7b5dSJohnathan Mantey { 3671f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3681f8c7b5dSJohnathan Mantey { 3691f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3701f8c7b5dSJohnathan Mantey { 3712c70f800SEd Tanous const bool* dnsEnabled = 3721f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3732c70f800SEd Tanous if (dnsEnabled != nullptr) 3741f8c7b5dSJohnathan Mantey { 3752c70f800SEd Tanous ethData.DNSEnabled = *dnsEnabled; 3761f8c7b5dSJohnathan Mantey } 3771f8c7b5dSJohnathan Mantey } 3781f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3791f8c7b5dSJohnathan Mantey { 3802c70f800SEd Tanous const bool* ntpEnabled = 3811f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3822c70f800SEd Tanous if (ntpEnabled != nullptr) 3831f8c7b5dSJohnathan Mantey { 3842c70f800SEd Tanous ethData.NTPEnabled = *ntpEnabled; 3851f8c7b5dSJohnathan Mantey } 3861f8c7b5dSJohnathan Mantey } 3871f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3881f8c7b5dSJohnathan Mantey { 3892c70f800SEd Tanous const bool* hostNameEnabled = 3901f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3912c70f800SEd Tanous if (hostNameEnabled != nullptr) 3921f8c7b5dSJohnathan Mantey { 3932c70f800SEd Tanous ethData.HostNameEnabled = *hostNameEnabled; 3941f8c7b5dSJohnathan Mantey } 3951f8c7b5dSJohnathan Mantey } 3961f8c7b5dSJohnathan Mantey else if (propertyPair.first == "SendHostNameEnabled") 3971f8c7b5dSJohnathan Mantey { 3982c70f800SEd Tanous const bool* sendHostNameEnabled = 3991f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 4002c70f800SEd Tanous if (sendHostNameEnabled != nullptr) 4011f8c7b5dSJohnathan Mantey { 4021f8c7b5dSJohnathan Mantey ethData.SendHostNameEnabled = 4032c70f800SEd Tanous *sendHostNameEnabled; 4041f8c7b5dSJohnathan Mantey } 4051f8c7b5dSJohnathan Mantey } 4061f8c7b5dSJohnathan Mantey } 4071f8c7b5dSJohnathan Mantey } 4081f8c7b5dSJohnathan Mantey } 409029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 410029573d4SEd Tanous // to check eth number 411029573d4SEd Tanous if (ifacePair.first == 4124a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 4134a0cb85cSEd Tanous { 4144a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 4154a0cb85cSEd Tanous { 4164a0cb85cSEd Tanous if (propertyPair.first == "HostName") 4174a0cb85cSEd Tanous { 4184a0cb85cSEd Tanous const std::string* hostname = 4198d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4204a0cb85cSEd Tanous if (hostname != nullptr) 4214a0cb85cSEd Tanous { 4224a0cb85cSEd Tanous ethData.hostname = *hostname; 4234a0cb85cSEd Tanous } 4244a0cb85cSEd Tanous } 4254a0cb85cSEd Tanous } 4264a0cb85cSEd Tanous } 4274a0cb85cSEd Tanous } 4284a0cb85cSEd Tanous } 4294c9afe43SEd Tanous return idFound; 4304a0cb85cSEd Tanous } 4314a0cb85cSEd Tanous 432e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 43301784826SJohnathan Mantey inline void 43481ce609eSEd Tanous extractIPV6Data(const std::string& ethifaceId, 43581ce609eSEd Tanous const GetManagedObjects& dbusData, 43681ce609eSEd Tanous boost::container::flat_set<IPv6AddressData>& ipv6Config) 437e48c0fc5SRavi Teja { 438e48c0fc5SRavi Teja const std::string ipv6PathStart = 43981ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 440e48c0fc5SRavi Teja 441e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 442e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 44381ce609eSEd Tanous for (const auto& objpath : dbusData) 444e48c0fc5SRavi Teja { 445e48c0fc5SRavi Teja // Check if proper pattern for object path appears 446e48c0fc5SRavi Teja if (boost::starts_with(objpath.first.str, ipv6PathStart)) 447e48c0fc5SRavi Teja { 448e48c0fc5SRavi Teja for (auto& interface : objpath.second) 449e48c0fc5SRavi Teja { 450e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 451e48c0fc5SRavi Teja { 452e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 453e48c0fc5SRavi Teja // appropriate 454e48c0fc5SRavi Teja std::pair< 455e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 456e48c0fc5SRavi Teja bool> 45781ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4582c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4592c70f800SEd Tanous ipv6Address.id = 460271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 461e48c0fc5SRavi Teja for (auto& property : interface.second) 462e48c0fc5SRavi Teja { 463e48c0fc5SRavi Teja if (property.first == "Address") 464e48c0fc5SRavi Teja { 465e48c0fc5SRavi Teja const std::string* address = 466e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 467e48c0fc5SRavi Teja if (address != nullptr) 468e48c0fc5SRavi Teja { 4692c70f800SEd Tanous ipv6Address.address = *address; 470e48c0fc5SRavi Teja } 471e48c0fc5SRavi Teja } 472e48c0fc5SRavi Teja else if (property.first == "Origin") 473e48c0fc5SRavi Teja { 474e48c0fc5SRavi Teja const std::string* origin = 475e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 476e48c0fc5SRavi Teja if (origin != nullptr) 477e48c0fc5SRavi Teja { 4782c70f800SEd Tanous ipv6Address.origin = 479e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 480e48c0fc5SRavi Teja false); 481e48c0fc5SRavi Teja } 482e48c0fc5SRavi Teja } 483e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 484e48c0fc5SRavi Teja { 485e48c0fc5SRavi Teja const uint8_t* prefix = 486e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 487e48c0fc5SRavi Teja if (prefix != nullptr) 488e48c0fc5SRavi Teja { 4892c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 490e48c0fc5SRavi Teja } 491e48c0fc5SRavi Teja } 492889ff694SAsmitha Karunanithi else if (property.first == "Type" || 493889ff694SAsmitha Karunanithi property.first == "Gateway") 494889ff694SAsmitha Karunanithi { 495889ff694SAsmitha Karunanithi // Type & Gateway is not used 496889ff694SAsmitha Karunanithi } 497e48c0fc5SRavi Teja else 498e48c0fc5SRavi Teja { 499e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 500e48c0fc5SRavi Teja << "Got extra property: " << property.first 501e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 502e48c0fc5SRavi Teja } 503e48c0fc5SRavi Teja } 504e48c0fc5SRavi Teja } 505e48c0fc5SRavi Teja } 506e48c0fc5SRavi Teja } 507e48c0fc5SRavi Teja } 508e48c0fc5SRavi Teja } 509e48c0fc5SRavi Teja 5104a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 51101784826SJohnathan Mantey inline void 51281ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 51381ce609eSEd Tanous const GetManagedObjects& dbusData, 51481ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5154a0cb85cSEd Tanous { 5164a0cb85cSEd Tanous const std::string ipv4PathStart = 51781ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 5184a0cb85cSEd Tanous 5194a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5204a0cb85cSEd Tanous // single ethernet interface, loop over all of them 52181ce609eSEd Tanous for (const auto& objpath : dbusData) 5224a0cb85cSEd Tanous { 5234a0cb85cSEd Tanous // Check if proper pattern for object path appears 5244a0cb85cSEd Tanous if (boost::starts_with(objpath.first.str, ipv4PathStart)) 5254a0cb85cSEd Tanous { 5264a0cb85cSEd Tanous for (auto& interface : objpath.second) 5274a0cb85cSEd Tanous { 5284a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5294a0cb85cSEd Tanous { 5304a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5314a0cb85cSEd Tanous // appropriate 5324a0cb85cSEd Tanous std::pair< 5334a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5344a0cb85cSEd Tanous bool> 53581ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5362c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5372c70f800SEd Tanous ipv4Address.id = 538271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5394a0cb85cSEd Tanous for (auto& property : interface.second) 5404a0cb85cSEd Tanous { 5414a0cb85cSEd Tanous if (property.first == "Address") 5424a0cb85cSEd Tanous { 5434a0cb85cSEd Tanous const std::string* address = 544abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5454a0cb85cSEd Tanous if (address != nullptr) 5464a0cb85cSEd Tanous { 5472c70f800SEd Tanous ipv4Address.address = *address; 5484a0cb85cSEd Tanous } 5494a0cb85cSEd Tanous } 5504a0cb85cSEd Tanous else if (property.first == "Origin") 5514a0cb85cSEd Tanous { 5524a0cb85cSEd Tanous const std::string* origin = 553abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5544a0cb85cSEd Tanous if (origin != nullptr) 5554a0cb85cSEd Tanous { 5562c70f800SEd Tanous ipv4Address.origin = 5574a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5584a0cb85cSEd Tanous true); 5594a0cb85cSEd Tanous } 5604a0cb85cSEd Tanous } 5614a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5624a0cb85cSEd Tanous { 5634a0cb85cSEd Tanous const uint8_t* mask = 564abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5654a0cb85cSEd Tanous if (mask != nullptr) 5664a0cb85cSEd Tanous { 5674a0cb85cSEd Tanous // convert it to the string 5682c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5694a0cb85cSEd Tanous } 5704a0cb85cSEd Tanous } 571889ff694SAsmitha Karunanithi else if (property.first == "Type" || 572889ff694SAsmitha Karunanithi property.first == "Gateway") 573889ff694SAsmitha Karunanithi { 574889ff694SAsmitha Karunanithi // Type & Gateway is not used 575889ff694SAsmitha Karunanithi } 5764a0cb85cSEd Tanous else 5774a0cb85cSEd Tanous { 5784a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5794a0cb85cSEd Tanous << "Got extra property: " << property.first 5804a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5814a0cb85cSEd Tanous } 5824a0cb85cSEd Tanous } 5834a0cb85cSEd Tanous // Check if given address is local, or global 5842c70f800SEd Tanous ipv4Address.linktype = 5852c70f800SEd Tanous boost::starts_with(ipv4Address.address, "169.254.") 58618659d10SJohnathan Mantey ? LinkType::Local 58718659d10SJohnathan Mantey : LinkType::Global; 5884a0cb85cSEd Tanous } 5894a0cb85cSEd Tanous } 5904a0cb85cSEd Tanous } 5914a0cb85cSEd Tanous } 5924a0cb85cSEd Tanous } 593588c3f0dSKowalski, Kamil 594588c3f0dSKowalski, Kamil /** 595588c3f0dSKowalski, Kamil * @brief Sets given Id on the given VLAN interface through D-Bus 596588c3f0dSKowalski, Kamil * 597588c3f0dSKowalski, Kamil * @param[in] ifaceId Id of VLAN interface that should be modified 598588c3f0dSKowalski, Kamil * @param[in] inputVlanId New ID of the VLAN 599588c3f0dSKowalski, Kamil * @param[in] callback Function that will be called after the operation 600588c3f0dSKowalski, Kamil * 601588c3f0dSKowalski, Kamil * @return None. 602588c3f0dSKowalski, Kamil */ 603588c3f0dSKowalski, Kamil template <typename CallbackFunc> 6044a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId, 6051abe55efSEd Tanous CallbackFunc&& callback) 6061abe55efSEd Tanous { 60755c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 608588c3f0dSKowalski, Kamil callback, "xyz.openbmc_project.Network", 609588c3f0dSKowalski, Kamil std::string("/xyz/openbmc_project/network/") + ifaceId, 610588c3f0dSKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 611588c3f0dSKowalski, Kamil "xyz.openbmc_project.Network.VLAN", "Id", 612168e20c1SEd Tanous dbus::utility::DbusVariantType(inputVlanId)); 6134a0cb85cSEd Tanous } 614588c3f0dSKowalski, Kamil 615588c3f0dSKowalski, Kamil /** 616179db1d7SKowalski, Kamil * @brief Helper function that verifies IP address to check if it is in 617179db1d7SKowalski, Kamil * proper format. If bits pointer is provided, also calculates active 618179db1d7SKowalski, Kamil * bit count for Subnet Mask. 619179db1d7SKowalski, Kamil * 620179db1d7SKowalski, Kamil * @param[in] ip IP that will be verified 621179db1d7SKowalski, Kamil * @param[out] bits Calculated mask in bits notation 622179db1d7SKowalski, Kamil * 623179db1d7SKowalski, Kamil * @return true in case of success, false otherwise 624179db1d7SKowalski, Kamil */ 6254a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, 6261abe55efSEd Tanous uint8_t* bits = nullptr) 6271abe55efSEd Tanous { 628179db1d7SKowalski, Kamil std::vector<std::string> bytesInMask; 629179db1d7SKowalski, Kamil 630179db1d7SKowalski, Kamil boost::split(bytesInMask, ip, boost::is_any_of(".")); 631179db1d7SKowalski, Kamil 6324a0cb85cSEd Tanous static const constexpr int ipV4AddressSectionsCount = 4; 6331abe55efSEd Tanous if (bytesInMask.size() != ipV4AddressSectionsCount) 6341abe55efSEd Tanous { 635179db1d7SKowalski, Kamil return false; 636179db1d7SKowalski, Kamil } 637179db1d7SKowalski, Kamil 6381abe55efSEd Tanous if (bits != nullptr) 6391abe55efSEd Tanous { 640179db1d7SKowalski, Kamil *bits = 0; 641179db1d7SKowalski, Kamil } 642179db1d7SKowalski, Kamil 643179db1d7SKowalski, Kamil char* endPtr; 644179db1d7SKowalski, Kamil long previousValue = 255; 645179db1d7SKowalski, Kamil bool firstZeroInByteHit; 6461abe55efSEd Tanous for (const std::string& byte : bytesInMask) 6471abe55efSEd Tanous { 6481abe55efSEd Tanous if (byte.empty()) 6491abe55efSEd Tanous { 6501db9ca37SKowalski, Kamil return false; 6511db9ca37SKowalski, Kamil } 6521db9ca37SKowalski, Kamil 653179db1d7SKowalski, Kamil // Use strtol instead of stroi to avoid exceptions 6541db9ca37SKowalski, Kamil long value = std::strtol(byte.c_str(), &endPtr, 10); 655179db1d7SKowalski, Kamil 6564a0cb85cSEd Tanous // endPtr should point to the end of the string, otherwise given string 6574a0cb85cSEd Tanous // is not 100% number 6581abe55efSEd Tanous if (*endPtr != '\0') 6591abe55efSEd Tanous { 660179db1d7SKowalski, Kamil return false; 661179db1d7SKowalski, Kamil } 662179db1d7SKowalski, Kamil 663179db1d7SKowalski, Kamil // Value should be contained in byte 6641abe55efSEd Tanous if (value < 0 || value > 255) 6651abe55efSEd Tanous { 666179db1d7SKowalski, Kamil return false; 667179db1d7SKowalski, Kamil } 668179db1d7SKowalski, Kamil 6691abe55efSEd Tanous if (bits != nullptr) 6701abe55efSEd Tanous { 671179db1d7SKowalski, Kamil // Mask has to be continuous between bytes 6721abe55efSEd Tanous if (previousValue != 255 && value != 0) 6731abe55efSEd Tanous { 674179db1d7SKowalski, Kamil return false; 675179db1d7SKowalski, Kamil } 676179db1d7SKowalski, Kamil 677179db1d7SKowalski, Kamil // Mask has to be continuous inside bytes 678179db1d7SKowalski, Kamil firstZeroInByteHit = false; 679179db1d7SKowalski, Kamil 680179db1d7SKowalski, Kamil // Count bits 68123a21a1cSEd Tanous for (long bitIdx = 7; bitIdx >= 0; bitIdx--) 6821abe55efSEd Tanous { 68323a21a1cSEd Tanous if (value & (1L << bitIdx)) 6841abe55efSEd Tanous { 6851abe55efSEd Tanous if (firstZeroInByteHit) 6861abe55efSEd Tanous { 687179db1d7SKowalski, Kamil // Continuity not preserved 688179db1d7SKowalski, Kamil return false; 6891abe55efSEd Tanous } 690179db1d7SKowalski, Kamil (*bits)++; 691179db1d7SKowalski, Kamil } 6921abe55efSEd Tanous else 6931abe55efSEd Tanous { 694179db1d7SKowalski, Kamil firstZeroInByteHit = true; 695179db1d7SKowalski, Kamil } 696179db1d7SKowalski, Kamil } 697179db1d7SKowalski, Kamil } 698179db1d7SKowalski, Kamil 699179db1d7SKowalski, Kamil previousValue = value; 700179db1d7SKowalski, Kamil } 701179db1d7SKowalski, Kamil 702179db1d7SKowalski, Kamil return true; 703179db1d7SKowalski, Kamil } 704179db1d7SKowalski, Kamil 705179db1d7SKowalski, Kamil /** 70601784826SJohnathan Mantey * @brief Deletes given IPv4 interface 707179db1d7SKowalski, Kamil * 708179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 709179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 710179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 711179db1d7SKowalski, Kamil * 712179db1d7SKowalski, Kamil * @return None 713179db1d7SKowalski, Kamil */ 7144a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 7158d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7161abe55efSEd Tanous { 71755c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 718286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 7191abe55efSEd Tanous if (ec) 7201abe55efSEd Tanous { 721a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7221abe55efSEd Tanous } 723179db1d7SKowalski, Kamil }, 724179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 725179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 726179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 727179db1d7SKowalski, Kamil } 728179db1d7SKowalski, Kamil 729244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 730244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 731244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7329010ec2eSRavi Teja { 7339010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7349010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 7359010ec2eSRavi Teja if (ec) 7369010ec2eSRavi Teja { 7379010ec2eSRavi Teja messages::internalError(asyncResp->res); 7389010ec2eSRavi Teja return; 7399010ec2eSRavi Teja } 7409010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 7419010ec2eSRavi Teja }, 7429010ec2eSRavi Teja "xyz.openbmc_project.Network", 7439010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 7449010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 7459010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 746168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 7479010ec2eSRavi Teja } 748179db1d7SKowalski, Kamil /** 74901784826SJohnathan Mantey * @brief Creates a static IPv4 entry 750179db1d7SKowalski, Kamil * 75101784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 75201784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 75301784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 75401784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 755179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 756179db1d7SKowalski, Kamil * 757179db1d7SKowalski, Kamil * @return None 758179db1d7SKowalski, Kamil */ 759cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 760cb13a392SEd Tanous const std::string& gateway, const std::string& address, 7618d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7621abe55efSEd Tanous { 7639010ec2eSRavi Teja auto createIpHandler = [asyncResp, ifaceId, 7649010ec2eSRavi Teja gateway](const boost::system::error_code ec) { 7651abe55efSEd Tanous if (ec) 7661abe55efSEd Tanous { 767a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7689010ec2eSRavi Teja return; 769179db1d7SKowalski, Kamil } 7709010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 7719010ec2eSRavi Teja }; 7729010ec2eSRavi Teja 7739010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7749010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 775179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 776179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 77701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 778179db1d7SKowalski, Kamil gateway); 779179db1d7SKowalski, Kamil } 780e48c0fc5SRavi Teja 781e48c0fc5SRavi Teja /** 78201784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 78301784826SJohnathan Mantey * static IPv4 entry 78401784826SJohnathan Mantey * 78501784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 78601784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 78701784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 78801784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 78901784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 79001784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 79101784826SJohnathan Mantey * 79201784826SJohnathan Mantey * @return None 79301784826SJohnathan Mantey */ 7948d1b46d7Szhanghch05 inline void 7958d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 7968d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 79701784826SJohnathan Mantey const std::string& address, 7988d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 79901784826SJohnathan Mantey { 80001784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 80101784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 80201784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 80301784826SJohnathan Mantey if (ec) 80401784826SJohnathan Mantey { 80501784826SJohnathan Mantey messages::internalError(asyncResp->res); 8069010ec2eSRavi Teja return; 80701784826SJohnathan Mantey } 8089010ec2eSRavi Teja 80901784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 8109010ec2eSRavi Teja [asyncResp, ifaceId, 8119010ec2eSRavi Teja gateway](const boost::system::error_code ec2) { 81223a21a1cSEd Tanous if (ec2) 81301784826SJohnathan Mantey { 81401784826SJohnathan Mantey messages::internalError(asyncResp->res); 8159010ec2eSRavi Teja return; 81601784826SJohnathan Mantey } 8179010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 81801784826SJohnathan Mantey }, 81901784826SJohnathan Mantey "xyz.openbmc_project.Network", 82001784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 82101784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 82201784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 82301784826SJohnathan Mantey prefixLength, gateway); 82401784826SJohnathan Mantey }, 82501784826SJohnathan Mantey "xyz.openbmc_project.Network", 82601784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 82701784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 82801784826SJohnathan Mantey } 82901784826SJohnathan Mantey 83001784826SJohnathan Mantey /** 831e48c0fc5SRavi Teja * @brief Deletes given IPv6 832e48c0fc5SRavi Teja * 833e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 834e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 835e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 836e48c0fc5SRavi Teja * 837e48c0fc5SRavi Teja * @return None 838e48c0fc5SRavi Teja */ 839e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 8408d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 841e48c0fc5SRavi Teja { 842e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 843286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 844e48c0fc5SRavi Teja if (ec) 845e48c0fc5SRavi Teja { 846e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 847e48c0fc5SRavi Teja } 848e48c0fc5SRavi Teja }, 849e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 850e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 851e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 852e48c0fc5SRavi Teja } 853e48c0fc5SRavi Teja 854e48c0fc5SRavi Teja /** 85501784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 85601784826SJohnathan Mantey * static IPv6 entry 85701784826SJohnathan Mantey * 85801784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 85901784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 86001784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 86101784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 86201784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 86301784826SJohnathan Mantey * 86401784826SJohnathan Mantey * @return None 86501784826SJohnathan Mantey */ 8668d1b46d7Szhanghch05 inline void 8678d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 8688d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 8698d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 87001784826SJohnathan Mantey { 87101784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 87201784826SJohnathan Mantey [asyncResp, ifaceId, address, 87301784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 87401784826SJohnathan Mantey if (ec) 87501784826SJohnathan Mantey { 87601784826SJohnathan Mantey messages::internalError(asyncResp->res); 87701784826SJohnathan Mantey } 87801784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 87923a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 88023a21a1cSEd Tanous if (ec2) 88101784826SJohnathan Mantey { 88201784826SJohnathan Mantey messages::internalError(asyncResp->res); 88301784826SJohnathan Mantey } 88401784826SJohnathan Mantey }, 88501784826SJohnathan Mantey "xyz.openbmc_project.Network", 88601784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 88701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 88801784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 88901784826SJohnathan Mantey prefixLength, ""); 89001784826SJohnathan Mantey }, 89101784826SJohnathan Mantey "xyz.openbmc_project.Network", 89201784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 89301784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 89401784826SJohnathan Mantey } 89501784826SJohnathan Mantey 89601784826SJohnathan Mantey /** 897e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 898e48c0fc5SRavi Teja * 899e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 900e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 901e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 902e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 903e48c0fc5SRavi Teja * 904e48c0fc5SRavi Teja * @return None 905e48c0fc5SRavi Teja */ 90601784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 90701784826SJohnathan Mantey const std::string& address, 9088d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 909e48c0fc5SRavi Teja { 910e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 911e48c0fc5SRavi Teja if (ec) 912e48c0fc5SRavi Teja { 913e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 914e48c0fc5SRavi Teja } 915e48c0fc5SRavi Teja }; 916e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 9174e0453b1SGunnar Mills // does not have associated gateway property 918e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 919e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 920e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 921e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 922e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 923e48c0fc5SRavi Teja ""); 924e48c0fc5SRavi Teja } 925e48c0fc5SRavi Teja 926179db1d7SKowalski, Kamil /** 927179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 928179db1d7SKowalski, Kamil * Object 929179db1d7SKowalski, Kamil * from EntityManager Network Manager 9304a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 931179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 932179db1d7SKowalski, Kamil * into JSON 933179db1d7SKowalski, Kamil */ 934179db1d7SKowalski, Kamil template <typename CallbackFunc> 93581ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 9361abe55efSEd Tanous CallbackFunc&& callback) 9371abe55efSEd Tanous { 93855c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 93981ce609eSEd Tanous [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}]( 94081ce609eSEd Tanous const boost::system::error_code errorCode, 941f23b7296SEd Tanous GetManagedObjects& resp) { 94255c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 9434a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 944e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 945179db1d7SKowalski, Kamil 94681ce609eSEd Tanous if (errorCode) 9471abe55efSEd Tanous { 94801784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 949179db1d7SKowalski, Kamil return; 950179db1d7SKowalski, Kamil } 951179db1d7SKowalski, Kamil 9524c9afe43SEd Tanous bool found = 9532c70f800SEd Tanous extractEthernetInterfaceData(ethifaceId, resp, ethData); 9544c9afe43SEd Tanous if (!found) 9554c9afe43SEd Tanous { 95601784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 9574c9afe43SEd Tanous return; 9584c9afe43SEd Tanous } 9594c9afe43SEd Tanous 9602c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 961179db1d7SKowalski, Kamil // Fix global GW 9621abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 9631abe55efSEd Tanous { 964c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 965c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 9669010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 9671abe55efSEd Tanous { 9684a0cb85cSEd Tanous ipv4.gateway = ethData.default_gateway; 969179db1d7SKowalski, Kamil } 970179db1d7SKowalski, Kamil } 971179db1d7SKowalski, Kamil 9722c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 9734e0453b1SGunnar Mills // Finally make a callback with useful data 97401784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 975179db1d7SKowalski, Kamil }, 976179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 977179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 978271584abSEd Tanous } 979179db1d7SKowalski, Kamil 980179db1d7SKowalski, Kamil /** 9819391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 9829391bb9cSRapkiewicz, Pawel * Manager 9831abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 9841abe55efSEd Tanous * into JSON. 9859391bb9cSRapkiewicz, Pawel */ 9869391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 9871abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 9881abe55efSEd Tanous { 98955c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 9904a0cb85cSEd Tanous [callback{std::move(callback)}]( 99181ce609eSEd Tanous const boost::system::error_code errorCode, 9924a0cb85cSEd Tanous GetManagedObjects& resp) { 9931abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 9941abe55efSEd Tanous // ethernet interfaces 9952c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 9962c70f800SEd Tanous ifaceList.reserve(resp.size()); 99781ce609eSEd Tanous if (errorCode) 9981abe55efSEd Tanous { 9992c70f800SEd Tanous callback(false, ifaceList); 10009391bb9cSRapkiewicz, Pawel return; 10019391bb9cSRapkiewicz, Pawel } 10029391bb9cSRapkiewicz, Pawel 10039391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 10044a0cb85cSEd Tanous for (const auto& objpath : resp) 10051abe55efSEd Tanous { 10069391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 10074a0cb85cSEd Tanous for (const auto& interface : objpath.second) 10081abe55efSEd Tanous { 10091abe55efSEd Tanous // If interface is 10104a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 10114a0cb85cSEd Tanous // what we're looking for. 10129391bb9cSRapkiewicz, Pawel if (interface.first == 10131abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 10141abe55efSEd Tanous { 10152dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 10162dfd18efSEd Tanous if (ifaceId.empty()) 10171abe55efSEd Tanous { 10182dfd18efSEd Tanous continue; 10199391bb9cSRapkiewicz, Pawel } 10202dfd18efSEd Tanous // and put it into output vector. 10212dfd18efSEd Tanous ifaceList.emplace(ifaceId); 10229391bb9cSRapkiewicz, Pawel } 10239391bb9cSRapkiewicz, Pawel } 10249391bb9cSRapkiewicz, Pawel } 1025a434f2bdSEd Tanous // Finally make a callback with useful data 10262c70f800SEd Tanous callback(true, ifaceList); 10279391bb9cSRapkiewicz, Pawel }, 1028aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 1029aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1030271584abSEd Tanous } 10319391bb9cSRapkiewicz, Pawel 10324f48d5f6SEd Tanous inline void 10334f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 10348d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 10351abe55efSEd Tanous { 1036ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 1037ab6554f1SJoshi-Mansi if (hostname.length() > 255) 1038ab6554f1SJoshi-Mansi { 1039ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 1040ab6554f1SJoshi-Mansi "HostName"); 1041ab6554f1SJoshi-Mansi return; 1042ab6554f1SJoshi-Mansi } 1043bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 1044bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 10454a0cb85cSEd Tanous if (ec) 10464a0cb85cSEd Tanous { 1047a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 10481abe55efSEd Tanous } 1049bc0bd6e0SEd Tanous }, 1050bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 1051bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 1052bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1053168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 1054588c3f0dSKowalski, Kamil } 1055588c3f0dSKowalski, Kamil 10564f48d5f6SEd Tanous inline void 10574f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 1058bf648f77SEd Tanous const std::string& domainname, 10598d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1060ab6554f1SJoshi-Mansi { 1061ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 1062ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 1063ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 1064ab6554f1SJoshi-Mansi if (ec) 1065ab6554f1SJoshi-Mansi { 1066ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 1067ab6554f1SJoshi-Mansi } 1068ab6554f1SJoshi-Mansi }, 1069ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 1070ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 1071ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 1072ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1073168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 1074ab6554f1SJoshi-Mansi } 1075ab6554f1SJoshi-Mansi 10764f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 1077bf648f77SEd Tanous { 1078bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 1079bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1080bf648f77SEd Tanous { 1081bf648f77SEd Tanous return false; 1082bf648f77SEd Tanous } 1083bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 1084bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 1085bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 1086bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 1087bf648f77SEd Tanous const std::regex pattern( 1088bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1089bf648f77SEd Tanous 1090bf648f77SEd Tanous return std::regex_match(hostname, pattern); 1091bf648f77SEd Tanous } 1092bf648f77SEd Tanous 10934f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 1094bf648f77SEd Tanous { 1095bf648f77SEd Tanous // Can have multiple subdomains 1096bf648f77SEd Tanous // Top Level Domain's min length is 2 character 10970fda0f12SGeorge Liu const std::regex pattern( 10980fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 1099bf648f77SEd Tanous 1100bf648f77SEd Tanous return std::regex_match(domainname, pattern); 1101bf648f77SEd Tanous } 1102bf648f77SEd Tanous 11034f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 11048d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1105ab6554f1SJoshi-Mansi { 1106ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1107ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1108ab6554f1SJoshi-Mansi { 1109ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1110ab6554f1SJoshi-Mansi return; 1111ab6554f1SJoshi-Mansi } 1112ab6554f1SJoshi-Mansi 1113ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1114ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1115ab6554f1SJoshi-Mansi { 1116ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1117ab6554f1SJoshi-Mansi return; 1118ab6554f1SJoshi-Mansi } 1119ab6554f1SJoshi-Mansi 1120ab6554f1SJoshi-Mansi std::string hostname; 1121ab6554f1SJoshi-Mansi std::string domainname; 1122ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1123ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1124ab6554f1SJoshi-Mansi 1125ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1126ab6554f1SJoshi-Mansi { 1127ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1128ab6554f1SJoshi-Mansi return; 1129ab6554f1SJoshi-Mansi } 1130ab6554f1SJoshi-Mansi 1131ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1132ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1133ab6554f1SJoshi-Mansi } 1134ab6554f1SJoshi-Mansi 11354f48d5f6SEd Tanous inline void 11364f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1137bf648f77SEd Tanous const std::string& macAddress, 11388d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1139d577665bSRatan Gupta { 1140d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 1141d577665bSRatan Gupta [asyncResp, macAddress](const boost::system::error_code ec) { 1142d577665bSRatan Gupta if (ec) 1143d577665bSRatan Gupta { 1144d577665bSRatan Gupta messages::internalError(asyncResp->res); 1145d577665bSRatan Gupta return; 1146d577665bSRatan Gupta } 1147d577665bSRatan Gupta }, 1148d577665bSRatan Gupta "xyz.openbmc_project.Network", 1149d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1150d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1151d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1152168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1153d577665bSRatan Gupta } 1154286b9118SJohnathan Mantey 11554f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 11564f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 11574f48d5f6SEd Tanous const bool v6Value, 11588d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1159da131a9aSJennifer Lee { 11602c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1161da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1162da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1163da131a9aSJennifer Lee if (ec) 1164da131a9aSJennifer Lee { 1165da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1166da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1167da131a9aSJennifer Lee return; 1168da131a9aSJennifer Lee } 11698f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1170da131a9aSJennifer Lee }, 1171da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1172da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1173da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1174da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1175168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1176da131a9aSJennifer Lee } 11771f8c7b5dSJohnathan Mantey 11784f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1179eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 11808d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1181eeedda23SJohnathan Mantey { 1182eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1183eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1184eeedda23SJohnathan Mantey if (ec) 1185eeedda23SJohnathan Mantey { 1186eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1187eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1188eeedda23SJohnathan Mantey return; 1189eeedda23SJohnathan Mantey } 1190eeedda23SJohnathan Mantey }, 1191eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1192eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1193eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1194eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1195168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1196eeedda23SJohnathan Mantey } 1197eeedda23SJohnathan Mantey 11984f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 11998d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1200da131a9aSJennifer Lee { 1201da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1202da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1203da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1204da131a9aSJennifer Lee if (ec) 1205da131a9aSJennifer Lee { 1206da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1207da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1208da131a9aSJennifer Lee return; 1209da131a9aSJennifer Lee } 1210da131a9aSJennifer Lee }, 1211da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1212da131a9aSJennifer Lee "/xyz/openbmc_project/network/config/dhcp", 1213da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1214da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1215168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1216da131a9aSJennifer Lee } 1217d577665bSRatan Gupta 12184f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 12191f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1220f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1221f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 12228d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1223da131a9aSJennifer Lee { 12241f8c7b5dSJohnathan Mantey bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1225bf648f77SEd Tanous bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false); 1226da131a9aSJennifer Lee 12271f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 12281f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 12291f8c7b5dSJohnathan Mantey 12301f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 12311f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1232da131a9aSJennifer Lee { 12331f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 12341f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 12351f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 12361f8c7b5dSJohnathan Mantey { 1237bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1238bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 12391f8c7b5dSJohnathan Mantey "OperatingMode"); 1240da131a9aSJennifer Lee return; 1241da131a9aSJennifer Lee } 12421f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 12431f8c7b5dSJohnathan Mantey } 12441f8c7b5dSJohnathan Mantey else 1245da131a9aSJennifer Lee { 12461f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 12471f8c7b5dSJohnathan Mantey } 12481f8c7b5dSJohnathan Mantey 12491f8c7b5dSJohnathan Mantey bool nextDNS{}; 12501f8c7b5dSJohnathan Mantey if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) 12511f8c7b5dSJohnathan Mantey { 12521f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) 12531f8c7b5dSJohnathan Mantey { 12541f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12551f8c7b5dSJohnathan Mantey return; 12561f8c7b5dSJohnathan Mantey } 12571f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 12581f8c7b5dSJohnathan Mantey } 12591f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useDNSServers) 12601f8c7b5dSJohnathan Mantey { 12611f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 12621f8c7b5dSJohnathan Mantey } 12631f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useDNSServers) 12641f8c7b5dSJohnathan Mantey { 12651f8c7b5dSJohnathan Mantey nextDNS = *v6dhcpParms.useDNSServers; 12661f8c7b5dSJohnathan Mantey } 12671f8c7b5dSJohnathan Mantey else 12681f8c7b5dSJohnathan Mantey { 12691f8c7b5dSJohnathan Mantey nextDNS = ethData.DNSEnabled; 12701f8c7b5dSJohnathan Mantey } 12711f8c7b5dSJohnathan Mantey 12721f8c7b5dSJohnathan Mantey bool nextNTP{}; 12731f8c7b5dSJohnathan Mantey if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) 12741f8c7b5dSJohnathan Mantey { 12751f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) 12761f8c7b5dSJohnathan Mantey { 12771f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12781f8c7b5dSJohnathan Mantey return; 12791f8c7b5dSJohnathan Mantey } 12801f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 12811f8c7b5dSJohnathan Mantey } 12821f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useNTPServers) 12831f8c7b5dSJohnathan Mantey { 12841f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 12851f8c7b5dSJohnathan Mantey } 12861f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useNTPServers) 12871f8c7b5dSJohnathan Mantey { 12881f8c7b5dSJohnathan Mantey nextNTP = *v6dhcpParms.useNTPServers; 12891f8c7b5dSJohnathan Mantey } 12901f8c7b5dSJohnathan Mantey else 12911f8c7b5dSJohnathan Mantey { 12921f8c7b5dSJohnathan Mantey nextNTP = ethData.NTPEnabled; 12931f8c7b5dSJohnathan Mantey } 12941f8c7b5dSJohnathan Mantey 12951f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 12961f8c7b5dSJohnathan Mantey if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) 12971f8c7b5dSJohnathan Mantey { 12981f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) 12991f8c7b5dSJohnathan Mantey { 13001f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13011f8c7b5dSJohnathan Mantey return; 13021f8c7b5dSJohnathan Mantey } 13031f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13041f8c7b5dSJohnathan Mantey } 13051f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useUseDomainName) 13061f8c7b5dSJohnathan Mantey { 13071f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13081f8c7b5dSJohnathan Mantey } 13091f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useUseDomainName) 13101f8c7b5dSJohnathan Mantey { 13111f8c7b5dSJohnathan Mantey nextUseDomain = *v6dhcpParms.useUseDomainName; 13121f8c7b5dSJohnathan Mantey } 13131f8c7b5dSJohnathan Mantey else 13141f8c7b5dSJohnathan Mantey { 13151f8c7b5dSJohnathan Mantey nextUseDomain = ethData.HostNameEnabled; 13161f8c7b5dSJohnathan Mantey } 13171f8c7b5dSJohnathan Mantey 1318da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 13191f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 13201f8c7b5dSJohnathan Mantey asyncResp); 1321da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 13221f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1323da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 13241f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 13251f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 13261f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1327da131a9aSJennifer Lee } 132801784826SJohnathan Mantey 13294f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 13302c70f800SEd Tanous getNextStaticIpEntry( 1331bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1332bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 133301784826SJohnathan Mantey { 133417a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 133517a897dfSManojkiran Eda return value.origin == "Static"; 133617a897dfSManojkiran Eda }); 133701784826SJohnathan Mantey } 133801784826SJohnathan Mantey 13394f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 13402c70f800SEd Tanous getNextStaticIpEntry( 1341bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1342bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 134301784826SJohnathan Mantey { 134417a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 134517a897dfSManojkiran Eda return value.origin == "Static"; 134617a897dfSManojkiran Eda }); 134701784826SJohnathan Mantey } 134801784826SJohnathan Mantey 13494f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1350f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 135101784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 13528d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 13531abe55efSEd Tanous { 135401784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1355f476acbfSRatan Gupta { 135671f52d96SEd Tanous messages::propertyValueTypeError( 135771f52d96SEd Tanous asyncResp->res, 1358bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1359d1d50814SRavi Teja "IPv4StaticAddresses"); 1360f476acbfSRatan Gupta return; 1361f476acbfSRatan Gupta } 1362f476acbfSRatan Gupta 1363271584abSEd Tanous unsigned entryIdx = 1; 136401784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 136501784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 136601784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 136701784826SJohnathan Mantey // into the NIC. 1368*85ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 13692c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 137001784826SJohnathan Mantey 1371537174c4SEd Tanous for (nlohmann::json& thisJson : input) 13721abe55efSEd Tanous { 13734a0cb85cSEd Tanous std::string pathString = 1374d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1375179db1d7SKowalski, Kamil 137601784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1377f476acbfSRatan Gupta { 1378537174c4SEd Tanous std::optional<std::string> address; 1379537174c4SEd Tanous std::optional<std::string> subnetMask; 1380537174c4SEd Tanous std::optional<std::string> gateway; 1381537174c4SEd Tanous 1382537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 13837e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 13847e27d832SJohnathan Mantey "Gateway", gateway)) 1385537174c4SEd Tanous { 138601784826SJohnathan Mantey messages::propertyValueFormatError( 138771f52d96SEd Tanous asyncResp->res, 138871f52d96SEd Tanous thisJson.dump(2, ' ', true, 138971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 139071f52d96SEd Tanous pathString); 1391537174c4SEd Tanous return; 1392179db1d7SKowalski, Kamil } 1393179db1d7SKowalski, Kamil 139401784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 139501784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 139601784826SJohnathan Mantey // current state of the interface. Merge existing state into the 139701784826SJohnathan Mantey // current request. 1398271584abSEd Tanous const std::string* addr = nullptr; 1399271584abSEd Tanous const std::string* gw = nullptr; 140001784826SJohnathan Mantey uint8_t prefixLength = 0; 140101784826SJohnathan Mantey bool errorInEntry = false; 1402537174c4SEd Tanous if (address) 14031abe55efSEd Tanous { 140401784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*address)) 14051abe55efSEd Tanous { 140601784826SJohnathan Mantey addr = &(*address); 14074a0cb85cSEd Tanous } 140801784826SJohnathan Mantey else 140901784826SJohnathan Mantey { 1410bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1411bf648f77SEd Tanous pathString + "/Address"); 141201784826SJohnathan Mantey errorInEntry = true; 141301784826SJohnathan Mantey } 141401784826SJohnathan Mantey } 1415*85ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 141601784826SJohnathan Mantey { 1417*85ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 141801784826SJohnathan Mantey } 141901784826SJohnathan Mantey else 142001784826SJohnathan Mantey { 142101784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 142201784826SJohnathan Mantey pathString + "/Address"); 142301784826SJohnathan Mantey errorInEntry = true; 14244a0cb85cSEd Tanous } 14254a0cb85cSEd Tanous 1426537174c4SEd Tanous if (subnetMask) 14274a0cb85cSEd Tanous { 1428537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 14294a0cb85cSEd Tanous { 1430f12894f8SJason M. Bills messages::propertyValueFormatError( 1431537174c4SEd Tanous asyncResp->res, *subnetMask, 14324a0cb85cSEd Tanous pathString + "/SubnetMask"); 143301784826SJohnathan Mantey errorInEntry = true; 14344a0cb85cSEd Tanous } 14354a0cb85cSEd Tanous } 1436*85ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 14374a0cb85cSEd Tanous { 1438*85ffe86aSJiaqing Zhao if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 143901784826SJohnathan Mantey &prefixLength)) 14404a0cb85cSEd Tanous { 144101784826SJohnathan Mantey messages::propertyValueFormatError( 1442*85ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 144301784826SJohnathan Mantey pathString + "/SubnetMask"); 144401784826SJohnathan Mantey errorInEntry = true; 14454a0cb85cSEd Tanous } 14464a0cb85cSEd Tanous } 14471abe55efSEd Tanous else 14481abe55efSEd Tanous { 144901784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 145001784826SJohnathan Mantey pathString + "/SubnetMask"); 145101784826SJohnathan Mantey errorInEntry = true; 145201784826SJohnathan Mantey } 145301784826SJohnathan Mantey 145401784826SJohnathan Mantey if (gateway) 145501784826SJohnathan Mantey { 145601784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*gateway)) 145701784826SJohnathan Mantey { 145801784826SJohnathan Mantey gw = &(*gateway); 145901784826SJohnathan Mantey } 146001784826SJohnathan Mantey else 146101784826SJohnathan Mantey { 1462bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1463bf648f77SEd Tanous pathString + "/Gateway"); 146401784826SJohnathan Mantey errorInEntry = true; 146501784826SJohnathan Mantey } 146601784826SJohnathan Mantey } 1467*85ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 146801784826SJohnathan Mantey { 1469*85ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 147001784826SJohnathan Mantey } 147101784826SJohnathan Mantey else 14721abe55efSEd Tanous { 1473a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 14744a0cb85cSEd Tanous pathString + "/Gateway"); 147501784826SJohnathan Mantey errorInEntry = true; 14764a0cb85cSEd Tanous } 14774a0cb85cSEd Tanous 147801784826SJohnathan Mantey if (errorInEntry) 14791abe55efSEd Tanous { 148001784826SJohnathan Mantey return; 14814a0cb85cSEd Tanous } 14824a0cb85cSEd Tanous 1483*85ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 14841abe55efSEd Tanous { 1485*85ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1486bf648f77SEd Tanous *addr, asyncResp); 1487*85ffe86aSJiaqing Zhao nicIpEntry = 1488*85ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1489588c3f0dSKowalski, Kamil } 149001784826SJohnathan Mantey else 149101784826SJohnathan Mantey { 1492cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1493cb13a392SEd Tanous asyncResp); 14944a0cb85cSEd Tanous } 14954a0cb85cSEd Tanous entryIdx++; 14964a0cb85cSEd Tanous } 149701784826SJohnathan Mantey else 149801784826SJohnathan Mantey { 1499*85ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 150001784826SJohnathan Mantey { 150101784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 150201784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 150301784826SJohnathan Mantey // in error, so bail out. 150401784826SJohnathan Mantey if (thisJson.is_null()) 150501784826SJohnathan Mantey { 150601784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 150701784826SJohnathan Mantey return; 150801784826SJohnathan Mantey } 150901784826SJohnathan Mantey messages::propertyValueFormatError( 151071f52d96SEd Tanous asyncResp->res, 151171f52d96SEd Tanous thisJson.dump(2, ' ', true, 151271f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 151371f52d96SEd Tanous pathString); 151401784826SJohnathan Mantey return; 151501784826SJohnathan Mantey } 151601784826SJohnathan Mantey 151701784826SJohnathan Mantey if (thisJson.is_null()) 151801784826SJohnathan Mantey { 1519*85ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 152001784826SJohnathan Mantey } 1521*85ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 152201784826SJohnathan Mantey { 1523*85ffe86aSJiaqing Zhao nicIpEntry = 1524*85ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 152501784826SJohnathan Mantey } 152601784826SJohnathan Mantey entryIdx++; 152701784826SJohnathan Mantey } 152801784826SJohnathan Mantey } 15294a0cb85cSEd Tanous } 15304a0cb85cSEd Tanous 15314f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1532f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1533f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 15348d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1535f85837bfSRAJESWARAN THILLAIGOVINDAN { 1536f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1537286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1538f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1539f85837bfSRAJESWARAN THILLAIGOVINDAN { 1540f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1541f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1542f85837bfSRAJESWARAN THILLAIGOVINDAN } 1543f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1544f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1545f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1546f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1547bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1548168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1549f85837bfSRAJESWARAN THILLAIGOVINDAN } 1550f85837bfSRAJESWARAN THILLAIGOVINDAN 15514f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1552f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 155301784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 15548d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1555e48c0fc5SRavi Teja { 155601784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1557e48c0fc5SRavi Teja { 155871f52d96SEd Tanous messages::propertyValueTypeError( 155971f52d96SEd Tanous asyncResp->res, 1560bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1561e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1562e48c0fc5SRavi Teja return; 1563e48c0fc5SRavi Teja } 1564271584abSEd Tanous size_t entryIdx = 1; 1565*85ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 15662c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1567f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1568e48c0fc5SRavi Teja { 1569e48c0fc5SRavi Teja std::string pathString = 1570e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1571e48c0fc5SRavi Teja 157201784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1573e48c0fc5SRavi Teja { 1574e48c0fc5SRavi Teja std::optional<std::string> address; 1575e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1576f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1577bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1578bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1579e48c0fc5SRavi Teja { 158001784826SJohnathan Mantey messages::propertyValueFormatError( 158171f52d96SEd Tanous asyncResp->res, 158271f52d96SEd Tanous thisJson.dump(2, ' ', true, 158371f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 158471f52d96SEd Tanous pathString); 1585e48c0fc5SRavi Teja return; 1586e48c0fc5SRavi Teja } 1587e48c0fc5SRavi Teja 158801784826SJohnathan Mantey const std::string* addr; 158901784826SJohnathan Mantey uint8_t prefix; 159001784826SJohnathan Mantey 159101784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 159201784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 159301784826SJohnathan Mantey // current state of the interface. Merge existing state into the 159401784826SJohnathan Mantey // current request. 1595e48c0fc5SRavi Teja if (address) 1596e48c0fc5SRavi Teja { 159701784826SJohnathan Mantey addr = &(*address); 1598e48c0fc5SRavi Teja } 1599*85ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 160001784826SJohnathan Mantey { 1601*85ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 160201784826SJohnathan Mantey } 160301784826SJohnathan Mantey else 160401784826SJohnathan Mantey { 160501784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 160601784826SJohnathan Mantey pathString + "/Address"); 160701784826SJohnathan Mantey return; 1608e48c0fc5SRavi Teja } 1609e48c0fc5SRavi Teja 1610e48c0fc5SRavi Teja if (prefixLength) 1611e48c0fc5SRavi Teja { 161201784826SJohnathan Mantey prefix = *prefixLength; 161301784826SJohnathan Mantey } 1614*85ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1615e48c0fc5SRavi Teja { 1616*85ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1617e48c0fc5SRavi Teja } 1618e48c0fc5SRavi Teja else 1619e48c0fc5SRavi Teja { 1620e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1621e48c0fc5SRavi Teja pathString + "/PrefixLength"); 162201784826SJohnathan Mantey return; 1623e48c0fc5SRavi Teja } 1624e48c0fc5SRavi Teja 1625*85ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1626e48c0fc5SRavi Teja { 1627*85ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1628e48c0fc5SRavi Teja asyncResp); 1629*85ffe86aSJiaqing Zhao nicIpEntry = 1630*85ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 163101784826SJohnathan Mantey } 163201784826SJohnathan Mantey else 163301784826SJohnathan Mantey { 163401784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1635e48c0fc5SRavi Teja } 1636e48c0fc5SRavi Teja entryIdx++; 1637e48c0fc5SRavi Teja } 163801784826SJohnathan Mantey else 163901784826SJohnathan Mantey { 1640*85ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 164101784826SJohnathan Mantey { 164201784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 164301784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 164401784826SJohnathan Mantey // in error, so bail out. 164501784826SJohnathan Mantey if (thisJson.is_null()) 164601784826SJohnathan Mantey { 164701784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 164801784826SJohnathan Mantey return; 164901784826SJohnathan Mantey } 165001784826SJohnathan Mantey messages::propertyValueFormatError( 165171f52d96SEd Tanous asyncResp->res, 165271f52d96SEd Tanous thisJson.dump(2, ' ', true, 165371f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 165471f52d96SEd Tanous pathString); 165501784826SJohnathan Mantey return; 165601784826SJohnathan Mantey } 165701784826SJohnathan Mantey 165801784826SJohnathan Mantey if (thisJson.is_null()) 165901784826SJohnathan Mantey { 1660*85ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 166101784826SJohnathan Mantey } 1662*85ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 166301784826SJohnathan Mantey { 1664*85ffe86aSJiaqing Zhao nicIpEntry = 1665*85ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 166601784826SJohnathan Mantey } 166701784826SJohnathan Mantey entryIdx++; 166801784826SJohnathan Mantey } 166901784826SJohnathan Mantey } 1670e48c0fc5SRavi Teja } 1671e48c0fc5SRavi Teja 16724f48d5f6SEd Tanous inline void parseInterfaceData( 16738d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16748d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1675e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 167601784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 16774a0cb85cSEd Tanous { 1678eeedda23SJohnathan Mantey constexpr const std::array<const char*, 1> inventoryForEthernet = { 1679eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1680eeedda23SJohnathan Mantey 16812c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 168281ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 16832c70f800SEd Tanous jsonResponse["@odata.id"] = 168481ce609eSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 16852c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1686eeedda23SJohnathan Mantey 1687eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1688eeedda23SJohnathan Mantey 1689eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1690eeedda23SJohnathan Mantey [health](const boost::system::error_code ec, 1691eeedda23SJohnathan Mantey std::vector<std::string>& resp) { 1692eeedda23SJohnathan Mantey if (ec) 1693029573d4SEd Tanous { 1694eeedda23SJohnathan Mantey return; 1695eeedda23SJohnathan Mantey } 1696eeedda23SJohnathan Mantey 1697eeedda23SJohnathan Mantey health->inventory = std::move(resp); 1698eeedda23SJohnathan Mantey }, 1699eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", 1700eeedda23SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 1701bf648f77SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), 1702bf648f77SEd Tanous inventoryForEthernet); 1703eeedda23SJohnathan Mantey 1704eeedda23SJohnathan Mantey health->populate(); 1705eeedda23SJohnathan Mantey 1706eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1707eeedda23SJohnathan Mantey { 17082c70f800SEd Tanous jsonResponse["LinkStatus"] = "LinkUp"; 17092c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1710029573d4SEd Tanous } 1711029573d4SEd Tanous else 1712029573d4SEd Tanous { 17132c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 17142c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1715029573d4SEd Tanous } 1716aa05fb27SJohnathan Mantey 17172c70f800SEd Tanous jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 17182c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 17192c70f800SEd Tanous jsonResponse["MACAddress"] = ethData.mac_address; 17202c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 17211f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 17222c70f800SEd Tanous jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; 17232c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; 17242c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; 17251f8c7b5dSJohnathan Mantey 17262c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 17271f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" 17281f8c7b5dSJohnathan Mantey : "Disabled"; 17292c70f800SEd Tanous jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; 17302c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; 17312c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; 17322a133282Smanojkiraneda 17334a0cb85cSEd Tanous if (!ethData.hostname.empty()) 17344a0cb85cSEd Tanous { 17352c70f800SEd Tanous jsonResponse["HostName"] = ethData.hostname; 1736ab6554f1SJoshi-Mansi 1737ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1738ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1739ab6554f1SJoshi-Mansi // FQDN 1740f23b7296SEd Tanous std::string fqdn = ethData.hostname; 1741d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1742d24bfc7aSJennifer Lee { 17432c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1744d24bfc7aSJennifer Lee } 17452c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 17464a0cb85cSEd Tanous } 17474a0cb85cSEd Tanous 17482c70f800SEd Tanous jsonResponse["VLANs"] = { 1749bf648f77SEd Tanous {"@odata.id", 1750bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; 1751fda13ad2SSunitha Harish 17522c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 17532c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 17544a0cb85cSEd Tanous 17552c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 17562c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 17572c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 17582c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 17592c70f800SEd Tanous for (auto& ipv4Config : ipv4Data) 17604a0cb85cSEd Tanous { 1761fa5053a6SGunnar Mills 17622c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1763fa5053a6SGunnar Mills if (gatewayStr.empty()) 1764fa5053a6SGunnar Mills { 1765fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1766fa5053a6SGunnar Mills } 1767fa5053a6SGunnar Mills 17682c70f800SEd Tanous ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 17692c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 17702c70f800SEd Tanous {"Address", ipv4Config.address}, 1771fa5053a6SGunnar Mills {"Gateway", gatewayStr}}); 17722c70f800SEd Tanous if (ipv4Config.origin == "Static") 1773d1d50814SRavi Teja { 17742c70f800SEd Tanous ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 17752c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 17762c70f800SEd Tanous {"Address", ipv4Config.address}, 1777d1d50814SRavi Teja {"Gateway", gatewayStr}}); 1778d1d50814SRavi Teja } 177901784826SJohnathan Mantey } 1780d1d50814SRavi Teja 17817ea79e5eSRavi Teja std::string ipv6GatewayStr = ethData.ipv6_default_gateway; 17827ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 17837ea79e5eSRavi Teja { 17847ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 17857ea79e5eSRavi Teja } 17867ea79e5eSRavi Teja 17877ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1788e48c0fc5SRavi Teja 17892c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 17902c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 17912c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 17922c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 17937f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 17942c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 17957f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 17962c70f800SEd Tanous for (auto& ipv6Config : ipv6Data) 1797e48c0fc5SRavi Teja { 17982c70f800SEd Tanous ipv6Array.push_back({{"Address", ipv6Config.address}, 17992c70f800SEd Tanous {"PrefixLength", ipv6Config.prefixLength}, 18002c70f800SEd Tanous {"AddressOrigin", ipv6Config.origin}, 18015fd16e4bSJohnathan Mantey {"AddressState", nullptr}}); 18022c70f800SEd Tanous if (ipv6Config.origin == "Static") 1803e48c0fc5SRavi Teja { 18042c70f800SEd Tanous ipv6StaticArray.push_back( 18052c70f800SEd Tanous {{"Address", ipv6Config.address}, 18067a474a5fSJiaqing Zhao {"PrefixLength", ipv6Config.prefixLength}}); 180701784826SJohnathan Mantey } 1808e48c0fc5SRavi Teja } 1809588c3f0dSKowalski, Kamil } 1810588c3f0dSKowalski, Kamil 18114f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 1812bf648f77SEd Tanous const std::string& parentIfaceId, 1813bf648f77SEd Tanous const std::string& ifaceId, 1814bf648f77SEd Tanous const EthernetInterfaceData& ethData) 18151abe55efSEd Tanous { 1816bf648f77SEd Tanous // Fill out obvious data... 1817bf648f77SEd Tanous jsonResponse["Id"] = ifaceId; 1818bf648f77SEd Tanous jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1819bf648f77SEd Tanous parentIfaceId + "/VLANs/" + ifaceId; 1820bf648f77SEd Tanous 1821bf648f77SEd Tanous jsonResponse["VLANEnable"] = true; 1822bf648f77SEd Tanous if (!ethData.vlan_id.empty()) 1823bf648f77SEd Tanous { 1824bf648f77SEd Tanous jsonResponse["VLANId"] = ethData.vlan_id.back(); 1825bf648f77SEd Tanous } 1826bf648f77SEd Tanous } 1827bf648f77SEd Tanous 18284f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1829bf648f77SEd Tanous { 1830bf648f77SEd Tanous if (!boost::starts_with(iface, parent + "_")) 1831bf648f77SEd Tanous { 1832bf648f77SEd Tanous return false; 1833bf648f77SEd Tanous } 1834bf648f77SEd Tanous return true; 1835bf648f77SEd Tanous } 1836bf648f77SEd Tanous 1837bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1838bf648f77SEd Tanous { 1839bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1840ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 1841bf648f77SEd Tanous .methods( 1842bf648f77SEd Tanous boost::beast::http::verb:: 1843bf648f77SEd Tanous get)([](const crow::Request&, 1844bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1845bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1846bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1847bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1848bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1849bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1850bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1851bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1852bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1853bf648f77SEd Tanous 1854bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1855bf648f77SEd Tanous // preparation 1856bf648f77SEd Tanous getEthernetIfaceList([asyncResp](const bool& success, 1857bf648f77SEd Tanous const boost::container::flat_set< 1858bf648f77SEd Tanous std::string>& ifaceList) { 1859bf648f77SEd Tanous if (!success) 18601abe55efSEd Tanous { 1861f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18629391bb9cSRapkiewicz, Pawel return; 18639391bb9cSRapkiewicz, Pawel } 18649391bb9cSRapkiewicz, Pawel 1865bf648f77SEd Tanous nlohmann::json& ifaceArray = 1866bf648f77SEd Tanous asyncResp->res.jsonValue["Members"]; 1867bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1868bf648f77SEd Tanous std::string tag = "_"; 1869bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1870bf648f77SEd Tanous { 1871bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1872bf648f77SEd Tanous if (found == std::string::npos) 1873bf648f77SEd Tanous { 1874bf648f77SEd Tanous ifaceArray.push_back( 1875bf648f77SEd Tanous {{"@odata.id", 1876bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1877bf648f77SEd Tanous ifaceItem}}); 1878bf648f77SEd Tanous } 1879bf648f77SEd Tanous } 1880bf648f77SEd Tanous 1881bf648f77SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 1882bf648f77SEd Tanous ifaceArray.size(); 1883bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1884bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1885bf648f77SEd Tanous }); 1886bf648f77SEd Tanous }); 1887bf648f77SEd Tanous 1888bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1889ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1890bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 1891bf648f77SEd Tanous [](const crow::Request&, 1892bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1893bf648f77SEd Tanous const std::string& ifaceId) { 18944a0cb85cSEd Tanous getEthernetIfaceData( 1895bf648f77SEd Tanous ifaceId, 1896bf648f77SEd Tanous [asyncResp, 1897bf648f77SEd Tanous ifaceId](const bool& success, 1898bf648f77SEd Tanous const EthernetInterfaceData& ethData, 1899bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 1900bf648f77SEd Tanous ipv4Data, 1901bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 1902bf648f77SEd Tanous ipv6Data) { 19034a0cb85cSEd Tanous if (!success) 19041abe55efSEd Tanous { 1905bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1906bf648f77SEd Tanous // existing object, and other errors 1907bf648f77SEd Tanous messages::resourceNotFound( 1908bf648f77SEd Tanous asyncResp->res, "EthernetInterface", ifaceId); 19094a0cb85cSEd Tanous return; 19109391bb9cSRapkiewicz, Pawel } 19114c9afe43SEd Tanous 19120f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1913fda13ad2SSunitha Harish "#EthernetInterface.v1_4_1.EthernetInterface"; 1914bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1915bf648f77SEd Tanous "Manager Ethernet Interface"; 19160f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 19170f74e643SEd Tanous "Management Network Interface"; 19180f74e643SEd Tanous 1919bf648f77SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, 1920bf648f77SEd Tanous ipv4Data, ipv6Data); 19219391bb9cSRapkiewicz, Pawel }); 1922bf648f77SEd Tanous }); 19239391bb9cSRapkiewicz, Pawel 1924bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1925ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1926ed398213SEd Tanous 1927bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 1928bf648f77SEd Tanous [](const crow::Request& req, 1929bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1930bf648f77SEd Tanous const std::string& ifaceId) { 1931bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1932ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1933d577665bSRatan Gupta std::optional<std::string> macAddress; 19349a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1935d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1936e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1937f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1938da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 19391f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1940eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 19411f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 19421f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 19430627a2c7SEd Tanous 19441f8c7b5dSJohnathan Mantey if (!json_util::readJson( 19458d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1946bf648f77SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, 1947bf648f77SEd Tanous "MACAddress", macAddress, "StaticNameServers", 1948bf648f77SEd Tanous staticNameServers, "IPv6DefaultGateway", 1949bf648f77SEd Tanous ipv6DefaultGateway, "IPv6StaticAddresses", 1950ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1951ab6554f1SJoshi-Mansi "InterfaceEnabled", interfaceEnabled)) 19521abe55efSEd Tanous { 1953588c3f0dSKowalski, Kamil return; 1954588c3f0dSKowalski, Kamil } 1955da131a9aSJennifer Lee if (dhcpv4) 1956da131a9aSJennifer Lee { 1957bf648f77SEd Tanous if (!json_util::readJson( 1958bf648f77SEd Tanous *dhcpv4, asyncResp->res, "DHCPEnabled", 19591f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 19601f8c7b5dSJohnathan Mantey v4dhcpParms.useDNSServers, "UseNTPServers", 19611f8c7b5dSJohnathan Mantey v4dhcpParms.useNTPServers, "UseDomainName", 19621f8c7b5dSJohnathan Mantey v4dhcpParms.useUseDomainName)) 19631f8c7b5dSJohnathan Mantey { 19641f8c7b5dSJohnathan Mantey return; 19651f8c7b5dSJohnathan Mantey } 19661f8c7b5dSJohnathan Mantey } 19671f8c7b5dSJohnathan Mantey 19681f8c7b5dSJohnathan Mantey if (dhcpv6) 19691f8c7b5dSJohnathan Mantey { 1970bf648f77SEd Tanous if (!json_util::readJson( 1971bf648f77SEd Tanous *dhcpv6, asyncResp->res, "OperatingMode", 1972bf648f77SEd Tanous v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers", 1973bf648f77SEd Tanous v6dhcpParms.useDNSServers, "UseNTPServers", 1974bf648f77SEd Tanous v6dhcpParms.useNTPServers, "UseDomainName", 19751f8c7b5dSJohnathan Mantey v6dhcpParms.useUseDomainName)) 19761f8c7b5dSJohnathan Mantey { 19771f8c7b5dSJohnathan Mantey return; 19781f8c7b5dSJohnathan Mantey } 1979da131a9aSJennifer Lee } 1980da131a9aSJennifer Lee 1981bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1982bf648f77SEd Tanous // for JSON preparation 19834a0cb85cSEd Tanous getEthernetIfaceData( 19842c70f800SEd Tanous ifaceId, 1985bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 1986ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1987d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 19889a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1989e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 19901f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 19911f8c7b5dSJohnathan Mantey dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 19921f8c7b5dSJohnathan Mantey v4dhcpParms = std::move(v4dhcpParms), 1993f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1994bf648f77SEd Tanous const bool& success, 1995bf648f77SEd Tanous const EthernetInterfaceData& ethData, 1996bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 1997bf648f77SEd Tanous ipv4Data, 1998bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 1999bf648f77SEd Tanous ipv6Data) { 20001abe55efSEd Tanous if (!success) 20011abe55efSEd Tanous { 2002588c3f0dSKowalski, Kamil // ... otherwise return error 2003bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2004bf648f77SEd Tanous // existing object, and other errors 2005bf648f77SEd Tanous messages::resourceNotFound( 2006bf648f77SEd Tanous asyncResp->res, "Ethernet Interface", ifaceId); 2007588c3f0dSKowalski, Kamil return; 2008588c3f0dSKowalski, Kamil } 2009588c3f0dSKowalski, Kamil 20101f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 20111f8c7b5dSJohnathan Mantey { 2012bf648f77SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, 2013bf648f77SEd Tanous v6dhcpParms, asyncResp); 20141f8c7b5dSJohnathan Mantey } 20151f8c7b5dSJohnathan Mantey 20160627a2c7SEd Tanous if (hostname) 20171abe55efSEd Tanous { 20180627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 20191abe55efSEd Tanous } 20200627a2c7SEd Tanous 2021ab6554f1SJoshi-Mansi if (fqdn) 2022ab6554f1SJoshi-Mansi { 20232c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2024ab6554f1SJoshi-Mansi } 2025ab6554f1SJoshi-Mansi 2026d577665bSRatan Gupta if (macAddress) 2027d577665bSRatan Gupta { 2028bf648f77SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, 2029bf648f77SEd Tanous asyncResp); 2030d577665bSRatan Gupta } 2031d577665bSRatan Gupta 2032d1d50814SRavi Teja if (ipv4StaticAddresses) 2033d1d50814SRavi Teja { 2034bf648f77SEd Tanous // TODO(ed) for some reason the capture of 2035bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 2036bf648f77SEd Tanous // not a non-const value. This doesn't really work 2037bf648f77SEd Tanous // for us, as we need to be able to efficiently move 2038bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 2039bf648f77SEd Tanous // makes a copy of the structure, and operates on 2040bf648f77SEd Tanous // that, but could be done more efficiently 2041f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 20422c70f800SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2043d1d50814SRavi Teja asyncResp); 20441abe55efSEd Tanous } 20450627a2c7SEd Tanous 2046f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 2047f85837bfSRAJESWARAN THILLAIGOVINDAN { 2048bf648f77SEd Tanous handleStaticNameServersPatch( 2049bf648f77SEd Tanous ifaceId, *staticNameServers, asyncResp); 2050f85837bfSRAJESWARAN THILLAIGOVINDAN } 20519a6fc6feSRavi Teja 20529a6fc6feSRavi Teja if (ipv6DefaultGateway) 20539a6fc6feSRavi Teja { 20549a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 20559a6fc6feSRavi Teja "IPv6DefaultGateway"); 20569a6fc6feSRavi Teja } 2057e48c0fc5SRavi Teja 2058e48c0fc5SRavi Teja if (ipv6StaticAddresses) 2059e48c0fc5SRavi Teja { 2060f23b7296SEd Tanous nlohmann::json ipv6Static = *ipv6StaticAddresses; 20612c70f800SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 206201784826SJohnathan Mantey ipv6Data, asyncResp); 2063e48c0fc5SRavi Teja } 2064eeedda23SJohnathan Mantey 2065eeedda23SJohnathan Mantey if (interfaceEnabled) 2066eeedda23SJohnathan Mantey { 2067eeedda23SJohnathan Mantey setEthernetInterfaceBoolProperty( 2068bf648f77SEd Tanous ifaceId, "NICEnabled", *interfaceEnabled, 2069bf648f77SEd Tanous asyncResp); 2070eeedda23SJohnathan Mantey } 2071588c3f0dSKowalski, Kamil }); 2072bf648f77SEd Tanous }); 20739391bb9cSRapkiewicz, Pawel 2074bf648f77SEd Tanous BMCWEB_ROUTE( 2075bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2076ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 2077ed398213SEd Tanous 2078bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 2079bf648f77SEd Tanous [](const crow::Request& /* req */, 2080bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2081bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 20828d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 20830f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 20848d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2085e439f0f8SKowalski, Kamil 20862c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 20871abe55efSEd Tanous { 2088a434f2bdSEd Tanous return; 2089a434f2bdSEd Tanous } 2090a434f2bdSEd Tanous 2091bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2092bf648f77SEd Tanous // for JSON preparation 20934a0cb85cSEd Tanous getEthernetIfaceData( 2094bf648f77SEd Tanous ifaceId, 2095bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2096bf648f77SEd Tanous const bool& success, 2097bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2098cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2099cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2100fda13ad2SSunitha Harish if (success && ethData.vlan_id.size() != 0) 21011abe55efSEd Tanous { 2102bf648f77SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, 2103bf648f77SEd Tanous parentIfaceId, ifaceId, ethData); 21041abe55efSEd Tanous } 21051abe55efSEd Tanous else 21061abe55efSEd Tanous { 2107e439f0f8SKowalski, Kamil // ... otherwise return error 2108bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2109bf648f77SEd Tanous // existing object, and other errors 2110bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2111bf648f77SEd Tanous "VLAN Network Interface", 2112bf648f77SEd Tanous ifaceId); 2113e439f0f8SKowalski, Kamil } 2114e439f0f8SKowalski, Kamil }); 2115bf648f77SEd Tanous }); 2116e439f0f8SKowalski, Kamil 2117bf648f77SEd Tanous BMCWEB_ROUTE( 2118bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2119ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2120ed398213SEd Tanous //.privileges(redfish::privileges::patchVLanNetworkInterface) 2121432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2122bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 2123bf648f77SEd Tanous [](const crow::Request& req, 2124bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2125bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 2126fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 21271abe55efSEd Tanous { 2128bf648f77SEd Tanous messages::resourceNotFound( 2129bf648f77SEd Tanous asyncResp->res, "VLAN Network Interface", ifaceId); 2130927a505aSKowalski, Kamil return; 2131927a505aSKowalski, Kamil } 2132927a505aSKowalski, Kamil 21330627a2c7SEd Tanous bool vlanEnable = false; 213438268fa8SAndrew Geissler uint32_t vlanId = 0; 21350627a2c7SEd Tanous 2136bf648f77SEd Tanous if (!json_util::readJson(req, asyncResp->res, "VLANEnable", 2137bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 21381abe55efSEd Tanous { 2139927a505aSKowalski, Kamil return; 2140927a505aSKowalski, Kamil } 2141927a505aSKowalski, Kamil 2142bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2143bf648f77SEd Tanous // for JSON preparation 2144e48c0fc5SRavi Teja getEthernetIfaceData( 2145bf648f77SEd Tanous ifaceId, 2146bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId]( 2147bf648f77SEd Tanous const bool& success, 2148bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2149cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2150cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 215108244d02SSunitha Harish if (success && !ethData.vlan_id.empty()) 215208244d02SSunitha Harish { 215308244d02SSunitha Harish auto callback = 2154bf648f77SEd Tanous [asyncResp]( 2155bf648f77SEd Tanous const boost::system::error_code ec) { 215608244d02SSunitha Harish if (ec) 215708244d02SSunitha Harish { 215808244d02SSunitha Harish messages::internalError(asyncResp->res); 215908244d02SSunitha Harish } 216008244d02SSunitha Harish }; 216108244d02SSunitha Harish 216208244d02SSunitha Harish if (vlanEnable == true) 216308244d02SSunitha Harish { 216408244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2165bf648f77SEd Tanous std::move(callback), 2166bf648f77SEd Tanous "xyz.openbmc_project.Network", 216708244d02SSunitha Harish "/xyz/openbmc_project/network/" + ifaceId, 216808244d02SSunitha Harish "org.freedesktop.DBus.Properties", "Set", 216908244d02SSunitha Harish "xyz.openbmc_project.Network.VLAN", "Id", 2170168e20c1SEd Tanous dbus::utility::DbusVariantType(vlanId)); 217108244d02SSunitha Harish } 217208244d02SSunitha Harish else 217308244d02SSunitha Harish { 2174bf648f77SEd Tanous BMCWEB_LOG_DEBUG 2175bf648f77SEd Tanous << "vlanEnable is false. Deleting the " 2176e48c0fc5SRavi Teja "vlan interface"; 217708244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2178bf648f77SEd Tanous std::move(callback), 2179bf648f77SEd Tanous "xyz.openbmc_project.Network", 2180bf648f77SEd Tanous std::string( 2181bf648f77SEd Tanous "/xyz/openbmc_project/network/") + 2182e48c0fc5SRavi Teja ifaceId, 2183bf648f77SEd Tanous "xyz.openbmc_project.Object.Delete", 2184bf648f77SEd Tanous "Delete"); 218508244d02SSunitha Harish } 218608244d02SSunitha Harish } 218708244d02SSunitha Harish else 21881abe55efSEd Tanous { 2189bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2190bf648f77SEd Tanous // existing object, and other errors 2191bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2192bf648f77SEd Tanous "VLAN Network Interface", 2193bf648f77SEd Tanous ifaceId); 2194bf648f77SEd Tanous return; 2195bf648f77SEd Tanous } 2196bf648f77SEd Tanous }); 2197bf648f77SEd Tanous }); 2198bf648f77SEd Tanous 2199bf648f77SEd Tanous BMCWEB_ROUTE( 2200bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2201ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2202ed398213SEd Tanous //.privileges(redfish::privileges::deleteVLanNetworkInterface) 2203432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2204bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 2205bf648f77SEd Tanous [](const crow::Request& /* req */, 2206bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2207bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 2208bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2209bf648f77SEd Tanous { 2210e48c0fc5SRavi Teja messages::resourceNotFound( 2211e48c0fc5SRavi Teja asyncResp->res, "VLAN Network Interface", ifaceId); 2212927a505aSKowalski, Kamil return; 2213927a505aSKowalski, Kamil } 2214e439f0f8SKowalski, Kamil 2215bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2216bf648f77SEd Tanous // for JSON preparation 2217f12894f8SJason M. Bills getEthernetIfaceData( 2218bf648f77SEd Tanous ifaceId, 2219bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2220bf648f77SEd Tanous const bool& success, 2221bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2222cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2223cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2224fda13ad2SSunitha Harish if (success && !ethData.vlan_id.empty()) 22251abe55efSEd Tanous { 2226f12894f8SJason M. Bills auto callback = 2227bf648f77SEd Tanous [asyncResp]( 2228bf648f77SEd Tanous const boost::system::error_code ec) { 22291abe55efSEd Tanous if (ec) 22301abe55efSEd Tanous { 2231f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2232927a505aSKowalski, Kamil } 22334a0cb85cSEd Tanous }; 22344a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2235bf648f77SEd Tanous std::move(callback), 2236bf648f77SEd Tanous "xyz.openbmc_project.Network", 2237bf648f77SEd Tanous std::string("/xyz/openbmc_project/network/") + 2238bf648f77SEd Tanous ifaceId, 22394a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 22401abe55efSEd Tanous } 22411abe55efSEd Tanous else 22421abe55efSEd Tanous { 2243927a505aSKowalski, Kamil // ... otherwise return error 2244bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2245bf648f77SEd Tanous // existing object, and other errors 2246bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2247bf648f77SEd Tanous "VLAN Network Interface", 2248bf648f77SEd Tanous ifaceId); 2249927a505aSKowalski, Kamil } 2250927a505aSKowalski, Kamil }); 2251bf648f77SEd Tanous }); 2252e439f0f8SKowalski, Kamil 2253bf648f77SEd Tanous BMCWEB_ROUTE(app, 2254bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2255ed398213SEd Tanous 2256ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 2257bf648f77SEd Tanous .methods( 2258bf648f77SEd Tanous boost::beast::http::verb:: 2259bf648f77SEd Tanous get)([](const crow::Request& /* req */, 2260bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2261bf648f77SEd Tanous const std::string& rootInterfaceName) { 22624a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 22631abe55efSEd Tanous // preparation 2264bf648f77SEd Tanous getEthernetIfaceList([asyncResp, rootInterfaceName]( 22651abe55efSEd Tanous const bool& success, 2266bf648f77SEd Tanous const boost::container::flat_set< 2267bf648f77SEd Tanous std::string>& ifaceList) { 22684a0cb85cSEd Tanous if (!success) 22691abe55efSEd Tanous { 2270f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22714a0cb85cSEd Tanous return; 22721abe55efSEd Tanous } 22734c9afe43SEd Tanous 227481ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 22754c9afe43SEd Tanous { 22764c9afe43SEd Tanous messages::resourceNotFound(asyncResp->res, 22774c9afe43SEd Tanous "VLanNetworkInterfaceCollection", 22784c9afe43SEd Tanous rootInterfaceName); 22794c9afe43SEd Tanous return; 22804c9afe43SEd Tanous } 22814c9afe43SEd Tanous 22820f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 22830f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 22840f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 22850f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 22860f74e643SEd Tanous "VLAN Network Interface Collection"; 22874a0cb85cSEd Tanous 22882c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 22894a0cb85cSEd Tanous 229081ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 22911abe55efSEd Tanous { 22922c70f800SEd Tanous if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 22934a0cb85cSEd Tanous { 2294f23b7296SEd Tanous std::string path = 2295f23b7296SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2296f23b7296SEd Tanous path += rootInterfaceName; 2297f23b7296SEd Tanous path += "/VLANs/"; 2298f23b7296SEd Tanous path += ifaceItem; 2299f23b7296SEd Tanous ifaceArray.push_back({{"@odata.id", std::move(path)}}); 2300e439f0f8SKowalski, Kamil } 2301e439f0f8SKowalski, Kamil } 2302e439f0f8SKowalski, Kamil 23034a0cb85cSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 23042c70f800SEd Tanous ifaceArray.size(); 23052c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 23064a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 23074a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 23084a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2309e439f0f8SKowalski, Kamil }); 2310bf648f77SEd Tanous }); 2311e439f0f8SKowalski, Kamil 2312bf648f77SEd Tanous BMCWEB_ROUTE(app, 2313bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2314ed398213SEd Tanous // This privilege is wrong, it should be ConfigureManager 2315ed398213SEd Tanous //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2316432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2317bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 2318bf648f77SEd Tanous [](const crow::Request& req, 2319bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2320bf648f77SEd Tanous const std::string& rootInterfaceName) { 2321fda13ad2SSunitha Harish bool vlanEnable = false; 23220627a2c7SEd Tanous uint32_t vlanId = 0; 23238d1b46d7Szhanghch05 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId, 23248d1b46d7Szhanghch05 "VLANEnable", vlanEnable)) 23251abe55efSEd Tanous { 23264a0cb85cSEd Tanous return; 2327e439f0f8SKowalski, Kamil } 2328fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2329fda13ad2SSunitha Harish if (!vlanId) 2330fda13ad2SSunitha Harish { 2331fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2332fda13ad2SSunitha Harish } 2333fda13ad2SSunitha Harish if (!vlanEnable) 2334fda13ad2SSunitha Harish { 2335fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2336fda13ad2SSunitha Harish } 2337271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2338fda13ad2SSunitha Harish { 2339fda13ad2SSunitha Harish return; 2340fda13ad2SSunitha Harish } 2341fda13ad2SSunitha Harish 2342bf648f77SEd Tanous auto callback = 2343bf648f77SEd Tanous [asyncResp](const boost::system::error_code ec) { 23441abe55efSEd Tanous if (ec) 23451abe55efSEd Tanous { 2346bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2347bf648f77SEd Tanous // based on phosphor-network responses 2348f12894f8SJason M. Bills messages::internalError(asyncResp->res); 23494a0cb85cSEd Tanous return; 23501abe55efSEd Tanous } 2351f12894f8SJason M. Bills messages::created(asyncResp->res); 2352e439f0f8SKowalski, Kamil }; 23534a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 23544a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 23554a0cb85cSEd Tanous "/xyz/openbmc_project/network", 23564a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 23570627a2c7SEd Tanous rootInterfaceName, vlanId); 2358bf648f77SEd Tanous }); 23594a0cb85cSEd Tanous } 2360bf648f77SEd Tanous 23619391bb9cSRapkiewicz, Pawel } // namespace redfish 2362