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 enum class LinkType 414a0cb85cSEd Tanous { 424a0cb85cSEd Tanous Local, 434a0cb85cSEd Tanous Global 444a0cb85cSEd Tanous }; 459391bb9cSRapkiewicz, Pawel 469391bb9cSRapkiewicz, Pawel /** 479391bb9cSRapkiewicz, Pawel * Structure for keeping IPv4 data required by Redfish 489391bb9cSRapkiewicz, Pawel */ 491abe55efSEd Tanous struct IPv4AddressData 501abe55efSEd Tanous { 51179db1d7SKowalski, Kamil std::string id; 524a0cb85cSEd Tanous std::string address; 534a0cb85cSEd Tanous std::string domain; 544a0cb85cSEd Tanous std::string gateway; 559391bb9cSRapkiewicz, Pawel std::string netmask; 569391bb9cSRapkiewicz, Pawel std::string origin; 574a0cb85cSEd Tanous LinkType linktype; 5801c6e858SSunitha Harish bool isActive; 594a0cb85cSEd Tanous 601abe55efSEd Tanous bool operator<(const IPv4AddressData& obj) const 611abe55efSEd Tanous { 624a0cb85cSEd Tanous return id < obj.id; 631abe55efSEd Tanous } 649391bb9cSRapkiewicz, Pawel }; 659391bb9cSRapkiewicz, Pawel 669391bb9cSRapkiewicz, Pawel /** 67e48c0fc5SRavi Teja * Structure for keeping IPv6 data required by Redfish 68e48c0fc5SRavi Teja */ 69e48c0fc5SRavi Teja struct IPv6AddressData 70e48c0fc5SRavi Teja { 71e48c0fc5SRavi Teja std::string id; 72e48c0fc5SRavi Teja std::string address; 73e48c0fc5SRavi Teja std::string origin; 74e48c0fc5SRavi Teja uint8_t prefixLength; 75e48c0fc5SRavi Teja 76e48c0fc5SRavi Teja bool operator<(const IPv6AddressData& obj) const 77e48c0fc5SRavi Teja { 78e48c0fc5SRavi Teja return id < obj.id; 79e48c0fc5SRavi Teja } 80e48c0fc5SRavi Teja }; 81e48c0fc5SRavi Teja /** 829391bb9cSRapkiewicz, Pawel * Structure for keeping basic single Ethernet Interface information 839391bb9cSRapkiewicz, Pawel * available from DBus 849391bb9cSRapkiewicz, Pawel */ 851abe55efSEd Tanous struct EthernetInterfaceData 861abe55efSEd Tanous { 874a0cb85cSEd Tanous uint32_t speed; 88*35fb5311STejas Patil size_t mtuSize; 894a0cb85cSEd Tanous bool auto_neg; 901f8c7b5dSJohnathan Mantey bool DNSEnabled; 911f8c7b5dSJohnathan Mantey bool NTPEnabled; 921f8c7b5dSJohnathan Mantey bool HostNameEnabled; 931f8c7b5dSJohnathan Mantey bool SendHostNameEnabled; 94aa05fb27SJohnathan Mantey bool linkUp; 95eeedda23SJohnathan Mantey bool nicEnabled; 961f8c7b5dSJohnathan Mantey std::string DHCPEnabled; 971f8c7b5dSJohnathan Mantey std::string operatingMode; 984a0cb85cSEd Tanous std::string hostname; 994a0cb85cSEd Tanous std::string default_gateway; 1009a6fc6feSRavi Teja std::string ipv6_default_gateway; 1014a0cb85cSEd Tanous std::string mac_address; 102fda13ad2SSunitha Harish std::vector<std::uint32_t> vlan_id; 1030f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 1040f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 105d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 1069391bb9cSRapkiewicz, Pawel }; 1079391bb9cSRapkiewicz, Pawel 1081f8c7b5dSJohnathan Mantey struct DHCPParameters 1091f8c7b5dSJohnathan Mantey { 1101f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 1111f8c7b5dSJohnathan Mantey std::optional<bool> useDNSServers; 1121f8c7b5dSJohnathan Mantey std::optional<bool> useNTPServers; 1131f8c7b5dSJohnathan Mantey std::optional<bool> useUseDomainName; 1141f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1151f8c7b5dSJohnathan Mantey }; 1161f8c7b5dSJohnathan Mantey 1179391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1189391bb9cSRapkiewicz, Pawel // into full dot notation 1191abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1201abe55efSEd Tanous { 1219391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1229391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1239391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1249391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1259391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1269391bb9cSRapkiewicz, Pawel return netmask; 1279391bb9cSRapkiewicz, Pawel } 1289391bb9cSRapkiewicz, Pawel 1291f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP, 1301f8c7b5dSJohnathan Mantey bool isIPv4) 1311f8c7b5dSJohnathan Mantey { 1321f8c7b5dSJohnathan Mantey if (isIPv4) 1331f8c7b5dSJohnathan Mantey { 1341f8c7b5dSJohnathan Mantey return ( 1351f8c7b5dSJohnathan Mantey (inputDHCP == 1361f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1371f8c7b5dSJohnathan Mantey (inputDHCP == 1381f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1391f8c7b5dSJohnathan Mantey } 1401f8c7b5dSJohnathan Mantey return ((inputDHCP == 1411f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1421f8c7b5dSJohnathan Mantey (inputDHCP == 1431f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1441f8c7b5dSJohnathan Mantey } 1451f8c7b5dSJohnathan Mantey 1462c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1471f8c7b5dSJohnathan Mantey { 1481f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1491f8c7b5dSJohnathan Mantey { 1501f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1511f8c7b5dSJohnathan Mantey } 1523174e4dfSEd Tanous if (isIPv4) 1531f8c7b5dSJohnathan Mantey { 1541f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1551f8c7b5dSJohnathan Mantey } 1563174e4dfSEd Tanous if (isIPv6) 1571f8c7b5dSJohnathan Mantey { 1581f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1591f8c7b5dSJohnathan Mantey } 1601f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1611f8c7b5dSJohnathan Mantey } 1621f8c7b5dSJohnathan Mantey 1634a0cb85cSEd Tanous inline std::string 1644a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1654a0cb85cSEd Tanous bool isIPv4) 1661abe55efSEd Tanous { 1674a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1681abe55efSEd Tanous { 1694a0cb85cSEd Tanous return "Static"; 1709391bb9cSRapkiewicz, Pawel } 1714a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1721abe55efSEd Tanous { 1734a0cb85cSEd Tanous if (isIPv4) 1741abe55efSEd Tanous { 1754a0cb85cSEd Tanous return "IPv4LinkLocal"; 1761abe55efSEd Tanous } 1774a0cb85cSEd Tanous return "LinkLocal"; 1789391bb9cSRapkiewicz, Pawel } 1794a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1801abe55efSEd Tanous { 1814a0cb85cSEd Tanous if (isIPv4) 1824a0cb85cSEd Tanous { 1834a0cb85cSEd Tanous return "DHCP"; 1844a0cb85cSEd Tanous } 1854a0cb85cSEd Tanous return "DHCPv6"; 1864a0cb85cSEd Tanous } 1874a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1884a0cb85cSEd Tanous { 1894a0cb85cSEd Tanous return "SLAAC"; 1904a0cb85cSEd Tanous } 1914a0cb85cSEd Tanous return ""; 1924a0cb85cSEd Tanous } 1934a0cb85cSEd Tanous 194711ac7a9SEd Tanous inline bool 195711ac7a9SEd Tanous extractEthernetInterfaceData(const std::string& ethifaceId, 196711ac7a9SEd Tanous dbus::utility::ManagedObjectType& dbusData, 1974a0cb85cSEd Tanous EthernetInterfaceData& ethData) 1984a0cb85cSEd Tanous { 1994c9afe43SEd Tanous bool idFound = false; 20081ce609eSEd Tanous for (auto& objpath : dbusData) 2014a0cb85cSEd Tanous { 202f23b7296SEd Tanous for (auto& ifacePair : objpath.second) 2034a0cb85cSEd Tanous { 20481ce609eSEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 205029573d4SEd Tanous { 2064c9afe43SEd Tanous idFound = true; 2074a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2084a0cb85cSEd Tanous { 2094a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2104a0cb85cSEd Tanous { 2114a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2124a0cb85cSEd Tanous { 2134a0cb85cSEd Tanous const std::string* mac = 214abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2154a0cb85cSEd Tanous if (mac != nullptr) 2164a0cb85cSEd Tanous { 2174a0cb85cSEd Tanous ethData.mac_address = *mac; 2184a0cb85cSEd Tanous } 2194a0cb85cSEd Tanous } 2204a0cb85cSEd Tanous } 2214a0cb85cSEd Tanous } 2224a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2234a0cb85cSEd Tanous { 2244a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2254a0cb85cSEd Tanous { 2264a0cb85cSEd Tanous if (propertyPair.first == "Id") 2274a0cb85cSEd Tanous { 2281b6b96c5SEd Tanous const uint32_t* id = 229abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2304a0cb85cSEd Tanous if (id != nullptr) 2314a0cb85cSEd Tanous { 232fda13ad2SSunitha Harish ethData.vlan_id.push_back(*id); 2334a0cb85cSEd Tanous } 2344a0cb85cSEd Tanous } 2354a0cb85cSEd Tanous } 2364a0cb85cSEd Tanous } 2374a0cb85cSEd Tanous else if (ifacePair.first == 2384a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2394a0cb85cSEd Tanous { 2404a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2414a0cb85cSEd Tanous { 2424a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2434a0cb85cSEd Tanous { 2442c70f800SEd Tanous const bool* autoNeg = 245abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2462c70f800SEd Tanous if (autoNeg != nullptr) 2474a0cb85cSEd Tanous { 2482c70f800SEd Tanous ethData.auto_neg = *autoNeg; 2494a0cb85cSEd Tanous } 2504a0cb85cSEd Tanous } 2514a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2524a0cb85cSEd Tanous { 2534a0cb85cSEd Tanous const uint32_t* speed = 254abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2554a0cb85cSEd Tanous if (speed != nullptr) 2564a0cb85cSEd Tanous { 2574a0cb85cSEd Tanous ethData.speed = *speed; 2584a0cb85cSEd Tanous } 2594a0cb85cSEd Tanous } 260*35fb5311STejas Patil else if (propertyPair.first == "MTU") 261*35fb5311STejas Patil { 262*35fb5311STejas Patil const uint32_t* mtuSize = 263*35fb5311STejas Patil std::get_if<uint32_t>(&propertyPair.second); 264*35fb5311STejas Patil if (mtuSize != nullptr) 265*35fb5311STejas Patil { 266*35fb5311STejas Patil ethData.mtuSize = *mtuSize; 267*35fb5311STejas Patil } 268*35fb5311STejas Patil } 269aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 270aa05fb27SJohnathan Mantey { 271aa05fb27SJohnathan Mantey const bool* linkUp = 272aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 273aa05fb27SJohnathan Mantey if (linkUp != nullptr) 274aa05fb27SJohnathan Mantey { 275aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 276aa05fb27SJohnathan Mantey } 277aa05fb27SJohnathan Mantey } 278eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 279eeedda23SJohnathan Mantey { 280eeedda23SJohnathan Mantey const bool* nicEnabled = 281eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 282eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 283eeedda23SJohnathan Mantey { 284eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 285eeedda23SJohnathan Mantey } 286eeedda23SJohnathan Mantey } 287f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 288029573d4SEd Tanous { 289029573d4SEd Tanous const std::vector<std::string>* nameservers = 2908d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 291029573d4SEd Tanous &propertyPair.second); 292029573d4SEd Tanous if (nameservers != nullptr) 293029573d4SEd Tanous { 294f23b7296SEd Tanous ethData.nameServers = *nameservers; 2950f6efdc1Smanojkiran.eda@gmail.com } 2960f6efdc1Smanojkiran.eda@gmail.com } 2970f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 2980f6efdc1Smanojkiran.eda@gmail.com { 2990f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 3008d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 3010f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 3020f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 3030f6efdc1Smanojkiran.eda@gmail.com { 304f23b7296SEd Tanous ethData.staticNameServers = *staticNameServers; 3054a0cb85cSEd Tanous } 3064a0cb85cSEd Tanous } 3072a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3082a133282Smanojkiraneda { 3092c70f800SEd Tanous const std::string* dhcpEnabled = 3101f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 3112c70f800SEd Tanous if (dhcpEnabled != nullptr) 3122a133282Smanojkiraneda { 3132c70f800SEd Tanous ethData.DHCPEnabled = *dhcpEnabled; 3142a133282Smanojkiraneda } 3152a133282Smanojkiraneda } 316d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 317d24bfc7aSJennifer Lee { 318d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3198d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 320d24bfc7aSJennifer Lee &propertyPair.second); 321d24bfc7aSJennifer Lee if (domainNames != nullptr) 322d24bfc7aSJennifer Lee { 323f23b7296SEd Tanous ethData.domainnames = *domainNames; 324d24bfc7aSJennifer Lee } 325d24bfc7aSJennifer Lee } 3269010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway") 3279010ec2eSRavi Teja { 3289010ec2eSRavi Teja const std::string* defaultGateway = 3299010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3309010ec2eSRavi Teja if (defaultGateway != nullptr) 3319010ec2eSRavi Teja { 3329010ec2eSRavi Teja std::string defaultGatewayStr = *defaultGateway; 3339010ec2eSRavi Teja if (defaultGatewayStr.empty()) 3349010ec2eSRavi Teja { 3359010ec2eSRavi Teja ethData.default_gateway = "0.0.0.0"; 3369010ec2eSRavi Teja } 3379010ec2eSRavi Teja else 3389010ec2eSRavi Teja { 3399010ec2eSRavi Teja ethData.default_gateway = defaultGatewayStr; 3409010ec2eSRavi Teja } 3419010ec2eSRavi Teja } 3429010ec2eSRavi Teja } 3439010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway6") 3449010ec2eSRavi Teja { 3459010ec2eSRavi Teja const std::string* defaultGateway6 = 3469010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3479010ec2eSRavi Teja if (defaultGateway6 != nullptr) 3489010ec2eSRavi Teja { 3499010ec2eSRavi Teja std::string defaultGateway6Str = 3509010ec2eSRavi Teja *defaultGateway6; 3519010ec2eSRavi Teja if (defaultGateway6Str.empty()) 3529010ec2eSRavi Teja { 3539010ec2eSRavi Teja ethData.ipv6_default_gateway = 3549010ec2eSRavi Teja "0:0:0:0:0:0:0:0"; 3559010ec2eSRavi Teja } 3569010ec2eSRavi Teja else 3579010ec2eSRavi Teja { 3589010ec2eSRavi Teja ethData.ipv6_default_gateway = 3599010ec2eSRavi Teja defaultGateway6Str; 3609010ec2eSRavi Teja } 3619010ec2eSRavi Teja } 3629010ec2eSRavi Teja } 363029573d4SEd Tanous } 364029573d4SEd Tanous } 365029573d4SEd Tanous } 3661f8c7b5dSJohnathan Mantey 3671f8c7b5dSJohnathan Mantey if (objpath.first == "/xyz/openbmc_project/network/config/dhcp") 3681f8c7b5dSJohnathan Mantey { 3691f8c7b5dSJohnathan Mantey if (ifacePair.first == 3701f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3711f8c7b5dSJohnathan Mantey { 3721f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3731f8c7b5dSJohnathan Mantey { 3741f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3751f8c7b5dSJohnathan Mantey { 3762c70f800SEd Tanous const bool* dnsEnabled = 3771f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3782c70f800SEd Tanous if (dnsEnabled != nullptr) 3791f8c7b5dSJohnathan Mantey { 3802c70f800SEd Tanous ethData.DNSEnabled = *dnsEnabled; 3811f8c7b5dSJohnathan Mantey } 3821f8c7b5dSJohnathan Mantey } 3831f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3841f8c7b5dSJohnathan Mantey { 3852c70f800SEd Tanous const bool* ntpEnabled = 3861f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3872c70f800SEd Tanous if (ntpEnabled != nullptr) 3881f8c7b5dSJohnathan Mantey { 3892c70f800SEd Tanous ethData.NTPEnabled = *ntpEnabled; 3901f8c7b5dSJohnathan Mantey } 3911f8c7b5dSJohnathan Mantey } 3921f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3931f8c7b5dSJohnathan Mantey { 3942c70f800SEd Tanous const bool* hostNameEnabled = 3951f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3962c70f800SEd Tanous if (hostNameEnabled != nullptr) 3971f8c7b5dSJohnathan Mantey { 3982c70f800SEd Tanous ethData.HostNameEnabled = *hostNameEnabled; 3991f8c7b5dSJohnathan Mantey } 4001f8c7b5dSJohnathan Mantey } 4011f8c7b5dSJohnathan Mantey else if (propertyPair.first == "SendHostNameEnabled") 4021f8c7b5dSJohnathan Mantey { 4032c70f800SEd Tanous const bool* sendHostNameEnabled = 4041f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 4052c70f800SEd Tanous if (sendHostNameEnabled != nullptr) 4061f8c7b5dSJohnathan Mantey { 4071f8c7b5dSJohnathan Mantey ethData.SendHostNameEnabled = 4082c70f800SEd Tanous *sendHostNameEnabled; 4091f8c7b5dSJohnathan Mantey } 4101f8c7b5dSJohnathan Mantey } 4111f8c7b5dSJohnathan Mantey } 4121f8c7b5dSJohnathan Mantey } 4131f8c7b5dSJohnathan Mantey } 414029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 415029573d4SEd Tanous // to check eth number 416029573d4SEd Tanous if (ifacePair.first == 4174a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 4184a0cb85cSEd Tanous { 4194a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 4204a0cb85cSEd Tanous { 4214a0cb85cSEd Tanous if (propertyPair.first == "HostName") 4224a0cb85cSEd Tanous { 4234a0cb85cSEd Tanous const std::string* hostname = 4248d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4254a0cb85cSEd Tanous if (hostname != nullptr) 4264a0cb85cSEd Tanous { 4274a0cb85cSEd Tanous ethData.hostname = *hostname; 4284a0cb85cSEd Tanous } 4294a0cb85cSEd Tanous } 4304a0cb85cSEd Tanous } 4314a0cb85cSEd Tanous } 4324a0cb85cSEd Tanous } 4334a0cb85cSEd Tanous } 4344c9afe43SEd Tanous return idFound; 4354a0cb85cSEd Tanous } 4364a0cb85cSEd Tanous 437e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 43801784826SJohnathan Mantey inline void 43981ce609eSEd Tanous extractIPV6Data(const std::string& ethifaceId, 440711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 44181ce609eSEd Tanous boost::container::flat_set<IPv6AddressData>& ipv6Config) 442e48c0fc5SRavi Teja { 443e48c0fc5SRavi Teja const std::string ipv6PathStart = 44481ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 445e48c0fc5SRavi Teja 446e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 447e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 44881ce609eSEd Tanous for (const auto& objpath : dbusData) 449e48c0fc5SRavi Teja { 450e48c0fc5SRavi Teja // Check if proper pattern for object path appears 451e48c0fc5SRavi Teja if (boost::starts_with(objpath.first.str, ipv6PathStart)) 452e48c0fc5SRavi Teja { 453e48c0fc5SRavi Teja for (auto& interface : objpath.second) 454e48c0fc5SRavi Teja { 455e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 456e48c0fc5SRavi Teja { 457e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 458e48c0fc5SRavi Teja // appropriate 459e48c0fc5SRavi Teja std::pair< 460e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 461e48c0fc5SRavi Teja bool> 46281ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4632c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4642c70f800SEd Tanous ipv6Address.id = 465271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 466e48c0fc5SRavi Teja for (auto& property : interface.second) 467e48c0fc5SRavi Teja { 468e48c0fc5SRavi Teja if (property.first == "Address") 469e48c0fc5SRavi Teja { 470e48c0fc5SRavi Teja const std::string* address = 471e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 472e48c0fc5SRavi Teja if (address != nullptr) 473e48c0fc5SRavi Teja { 4742c70f800SEd Tanous ipv6Address.address = *address; 475e48c0fc5SRavi Teja } 476e48c0fc5SRavi Teja } 477e48c0fc5SRavi Teja else if (property.first == "Origin") 478e48c0fc5SRavi Teja { 479e48c0fc5SRavi Teja const std::string* origin = 480e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 481e48c0fc5SRavi Teja if (origin != nullptr) 482e48c0fc5SRavi Teja { 4832c70f800SEd Tanous ipv6Address.origin = 484e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 485e48c0fc5SRavi Teja false); 486e48c0fc5SRavi Teja } 487e48c0fc5SRavi Teja } 488e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 489e48c0fc5SRavi Teja { 490e48c0fc5SRavi Teja const uint8_t* prefix = 491e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 492e48c0fc5SRavi Teja if (prefix != nullptr) 493e48c0fc5SRavi Teja { 4942c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 495e48c0fc5SRavi Teja } 496e48c0fc5SRavi Teja } 497889ff694SAsmitha Karunanithi else if (property.first == "Type" || 498889ff694SAsmitha Karunanithi property.first == "Gateway") 499889ff694SAsmitha Karunanithi { 500889ff694SAsmitha Karunanithi // Type & Gateway is not used 501889ff694SAsmitha Karunanithi } 502e48c0fc5SRavi Teja else 503e48c0fc5SRavi Teja { 504e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 505e48c0fc5SRavi Teja << "Got extra property: " << property.first 506e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 507e48c0fc5SRavi Teja } 508e48c0fc5SRavi Teja } 509e48c0fc5SRavi Teja } 510e48c0fc5SRavi Teja } 511e48c0fc5SRavi Teja } 512e48c0fc5SRavi Teja } 513e48c0fc5SRavi Teja } 514e48c0fc5SRavi Teja 5154a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 51601784826SJohnathan Mantey inline void 51781ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 518711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 51981ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5204a0cb85cSEd Tanous { 5214a0cb85cSEd Tanous const std::string ipv4PathStart = 52281ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 5234a0cb85cSEd Tanous 5244a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5254a0cb85cSEd Tanous // single ethernet interface, loop over all of them 52681ce609eSEd Tanous for (const auto& objpath : dbusData) 5274a0cb85cSEd Tanous { 5284a0cb85cSEd Tanous // Check if proper pattern for object path appears 5294a0cb85cSEd Tanous if (boost::starts_with(objpath.first.str, ipv4PathStart)) 5304a0cb85cSEd Tanous { 5314a0cb85cSEd Tanous for (auto& interface : objpath.second) 5324a0cb85cSEd Tanous { 5334a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5344a0cb85cSEd Tanous { 5354a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5364a0cb85cSEd Tanous // appropriate 5374a0cb85cSEd Tanous std::pair< 5384a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5394a0cb85cSEd Tanous bool> 54081ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5412c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5422c70f800SEd Tanous ipv4Address.id = 543271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5444a0cb85cSEd Tanous for (auto& property : interface.second) 5454a0cb85cSEd Tanous { 5464a0cb85cSEd Tanous if (property.first == "Address") 5474a0cb85cSEd Tanous { 5484a0cb85cSEd Tanous const std::string* address = 549abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5504a0cb85cSEd Tanous if (address != nullptr) 5514a0cb85cSEd Tanous { 5522c70f800SEd Tanous ipv4Address.address = *address; 5534a0cb85cSEd Tanous } 5544a0cb85cSEd Tanous } 5554a0cb85cSEd Tanous else if (property.first == "Origin") 5564a0cb85cSEd Tanous { 5574a0cb85cSEd Tanous const std::string* origin = 558abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5594a0cb85cSEd Tanous if (origin != nullptr) 5604a0cb85cSEd Tanous { 5612c70f800SEd Tanous ipv4Address.origin = 5624a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5634a0cb85cSEd Tanous true); 5644a0cb85cSEd Tanous } 5654a0cb85cSEd Tanous } 5664a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5674a0cb85cSEd Tanous { 5684a0cb85cSEd Tanous const uint8_t* mask = 569abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5704a0cb85cSEd Tanous if (mask != nullptr) 5714a0cb85cSEd Tanous { 5724a0cb85cSEd Tanous // convert it to the string 5732c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5744a0cb85cSEd Tanous } 5754a0cb85cSEd Tanous } 576889ff694SAsmitha Karunanithi else if (property.first == "Type" || 577889ff694SAsmitha Karunanithi property.first == "Gateway") 578889ff694SAsmitha Karunanithi { 579889ff694SAsmitha Karunanithi // Type & Gateway is not used 580889ff694SAsmitha Karunanithi } 5814a0cb85cSEd Tanous else 5824a0cb85cSEd Tanous { 5834a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5844a0cb85cSEd Tanous << "Got extra property: " << property.first 5854a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5864a0cb85cSEd Tanous } 5874a0cb85cSEd Tanous } 5884a0cb85cSEd Tanous // Check if given address is local, or global 5892c70f800SEd Tanous ipv4Address.linktype = 5902c70f800SEd Tanous boost::starts_with(ipv4Address.address, "169.254.") 59118659d10SJohnathan Mantey ? LinkType::Local 59218659d10SJohnathan Mantey : LinkType::Global; 5934a0cb85cSEd Tanous } 5944a0cb85cSEd Tanous } 5954a0cb85cSEd Tanous } 5964a0cb85cSEd Tanous } 5974a0cb85cSEd Tanous } 598588c3f0dSKowalski, Kamil 599588c3f0dSKowalski, Kamil /** 600588c3f0dSKowalski, Kamil * @brief Sets given Id on the given VLAN interface through D-Bus 601588c3f0dSKowalski, Kamil * 602588c3f0dSKowalski, Kamil * @param[in] ifaceId Id of VLAN interface that should be modified 603588c3f0dSKowalski, Kamil * @param[in] inputVlanId New ID of the VLAN 604588c3f0dSKowalski, Kamil * @param[in] callback Function that will be called after the operation 605588c3f0dSKowalski, Kamil * 606588c3f0dSKowalski, Kamil * @return None. 607588c3f0dSKowalski, Kamil */ 608588c3f0dSKowalski, Kamil template <typename CallbackFunc> 6094a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId, 6101abe55efSEd Tanous CallbackFunc&& callback) 6111abe55efSEd Tanous { 61255c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 613588c3f0dSKowalski, Kamil callback, "xyz.openbmc_project.Network", 614588c3f0dSKowalski, Kamil std::string("/xyz/openbmc_project/network/") + ifaceId, 615588c3f0dSKowalski, Kamil "org.freedesktop.DBus.Properties", "Set", 616588c3f0dSKowalski, Kamil "xyz.openbmc_project.Network.VLAN", "Id", 617168e20c1SEd Tanous dbus::utility::DbusVariantType(inputVlanId)); 6184a0cb85cSEd Tanous } 619588c3f0dSKowalski, Kamil 620588c3f0dSKowalski, Kamil /** 621179db1d7SKowalski, Kamil * @brief Helper function that verifies IP address to check if it is in 622179db1d7SKowalski, Kamil * proper format. If bits pointer is provided, also calculates active 623179db1d7SKowalski, Kamil * bit count for Subnet Mask. 624179db1d7SKowalski, Kamil * 625179db1d7SKowalski, Kamil * @param[in] ip IP that will be verified 626179db1d7SKowalski, Kamil * @param[out] bits Calculated mask in bits notation 627179db1d7SKowalski, Kamil * 628179db1d7SKowalski, Kamil * @return true in case of success, false otherwise 629179db1d7SKowalski, Kamil */ 6304a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip, 6311abe55efSEd Tanous uint8_t* bits = nullptr) 6321abe55efSEd Tanous { 633179db1d7SKowalski, Kamil std::vector<std::string> bytesInMask; 634179db1d7SKowalski, Kamil 635179db1d7SKowalski, Kamil boost::split(bytesInMask, ip, boost::is_any_of(".")); 636179db1d7SKowalski, Kamil 6374a0cb85cSEd Tanous static const constexpr int ipV4AddressSectionsCount = 4; 6381abe55efSEd Tanous if (bytesInMask.size() != ipV4AddressSectionsCount) 6391abe55efSEd Tanous { 640179db1d7SKowalski, Kamil return false; 641179db1d7SKowalski, Kamil } 642179db1d7SKowalski, Kamil 6431abe55efSEd Tanous if (bits != nullptr) 6441abe55efSEd Tanous { 645179db1d7SKowalski, Kamil *bits = 0; 646179db1d7SKowalski, Kamil } 647179db1d7SKowalski, Kamil 648543f4400SEd Tanous char* endPtr = nullptr; 649179db1d7SKowalski, Kamil long previousValue = 255; 650543f4400SEd Tanous bool firstZeroInByteHit = false; 6511abe55efSEd Tanous for (const std::string& byte : bytesInMask) 6521abe55efSEd Tanous { 6531abe55efSEd Tanous if (byte.empty()) 6541abe55efSEd Tanous { 6551db9ca37SKowalski, Kamil return false; 6561db9ca37SKowalski, Kamil } 6571db9ca37SKowalski, Kamil 658179db1d7SKowalski, Kamil // Use strtol instead of stroi to avoid exceptions 6591db9ca37SKowalski, Kamil long value = std::strtol(byte.c_str(), &endPtr, 10); 660179db1d7SKowalski, Kamil 6614a0cb85cSEd Tanous // endPtr should point to the end of the string, otherwise given string 6624a0cb85cSEd Tanous // is not 100% number 6631abe55efSEd Tanous if (*endPtr != '\0') 6641abe55efSEd Tanous { 665179db1d7SKowalski, Kamil return false; 666179db1d7SKowalski, Kamil } 667179db1d7SKowalski, Kamil 668179db1d7SKowalski, Kamil // Value should be contained in byte 6691abe55efSEd Tanous if (value < 0 || value > 255) 6701abe55efSEd Tanous { 671179db1d7SKowalski, Kamil return false; 672179db1d7SKowalski, Kamil } 673179db1d7SKowalski, Kamil 6741abe55efSEd Tanous if (bits != nullptr) 6751abe55efSEd Tanous { 676179db1d7SKowalski, Kamil // Mask has to be continuous between bytes 6771abe55efSEd Tanous if (previousValue != 255 && value != 0) 6781abe55efSEd Tanous { 679179db1d7SKowalski, Kamil return false; 680179db1d7SKowalski, Kamil } 681179db1d7SKowalski, Kamil 682179db1d7SKowalski, Kamil // Mask has to be continuous inside bytes 683179db1d7SKowalski, Kamil firstZeroInByteHit = false; 684179db1d7SKowalski, Kamil 685179db1d7SKowalski, Kamil // Count bits 68623a21a1cSEd Tanous for (long bitIdx = 7; bitIdx >= 0; bitIdx--) 6871abe55efSEd Tanous { 68823a21a1cSEd Tanous if (value & (1L << bitIdx)) 6891abe55efSEd Tanous { 6901abe55efSEd Tanous if (firstZeroInByteHit) 6911abe55efSEd Tanous { 692179db1d7SKowalski, Kamil // Continuity not preserved 693179db1d7SKowalski, Kamil return false; 6941abe55efSEd Tanous } 695179db1d7SKowalski, Kamil (*bits)++; 696179db1d7SKowalski, Kamil } 6971abe55efSEd Tanous else 6981abe55efSEd Tanous { 699179db1d7SKowalski, Kamil firstZeroInByteHit = true; 700179db1d7SKowalski, Kamil } 701179db1d7SKowalski, Kamil } 702179db1d7SKowalski, Kamil } 703179db1d7SKowalski, Kamil 704179db1d7SKowalski, Kamil previousValue = value; 705179db1d7SKowalski, Kamil } 706179db1d7SKowalski, Kamil 707179db1d7SKowalski, Kamil return true; 708179db1d7SKowalski, Kamil } 709179db1d7SKowalski, Kamil 710179db1d7SKowalski, Kamil /** 71101784826SJohnathan Mantey * @brief Deletes given IPv4 interface 712179db1d7SKowalski, Kamil * 713179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 714179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 715179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 716179db1d7SKowalski, Kamil * 717179db1d7SKowalski, Kamil * @return None 718179db1d7SKowalski, Kamil */ 7194a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 7208d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7211abe55efSEd Tanous { 72255c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 723286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 7241abe55efSEd Tanous if (ec) 7251abe55efSEd Tanous { 726a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7271abe55efSEd Tanous } 728179db1d7SKowalski, Kamil }, 729179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 730179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 731179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 732179db1d7SKowalski, Kamil } 733179db1d7SKowalski, Kamil 734244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 735244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 736244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7379010ec2eSRavi Teja { 7389010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7399010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 7409010ec2eSRavi Teja if (ec) 7419010ec2eSRavi Teja { 7429010ec2eSRavi Teja messages::internalError(asyncResp->res); 7439010ec2eSRavi Teja return; 7449010ec2eSRavi Teja } 7459010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 7469010ec2eSRavi Teja }, 7479010ec2eSRavi Teja "xyz.openbmc_project.Network", 7489010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 7499010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 7509010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 751168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 7529010ec2eSRavi Teja } 753179db1d7SKowalski, Kamil /** 75401784826SJohnathan Mantey * @brief Creates a static IPv4 entry 755179db1d7SKowalski, Kamil * 75601784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 75701784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 75801784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 75901784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 760179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 761179db1d7SKowalski, Kamil * 762179db1d7SKowalski, Kamil * @return None 763179db1d7SKowalski, Kamil */ 764cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 765cb13a392SEd Tanous const std::string& gateway, const std::string& address, 7668d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7671abe55efSEd Tanous { 7689010ec2eSRavi Teja auto createIpHandler = [asyncResp, ifaceId, 7699010ec2eSRavi Teja gateway](const boost::system::error_code ec) { 7701abe55efSEd Tanous if (ec) 7711abe55efSEd Tanous { 772a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 7739010ec2eSRavi Teja return; 774179db1d7SKowalski, Kamil } 7759010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 7769010ec2eSRavi Teja }; 7779010ec2eSRavi Teja 7789010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 7799010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 780179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 781179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 78201784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 783179db1d7SKowalski, Kamil gateway); 784179db1d7SKowalski, Kamil } 785e48c0fc5SRavi Teja 786e48c0fc5SRavi Teja /** 78701784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 78801784826SJohnathan Mantey * static IPv4 entry 78901784826SJohnathan Mantey * 79001784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 79101784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 79201784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 79301784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 79401784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 79501784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 79601784826SJohnathan Mantey * 79701784826SJohnathan Mantey * @return None 79801784826SJohnathan Mantey */ 7998d1b46d7Szhanghch05 inline void 8008d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 8018d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 80201784826SJohnathan Mantey const std::string& address, 8038d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 80401784826SJohnathan Mantey { 80501784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 80601784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 80701784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 80801784826SJohnathan Mantey if (ec) 80901784826SJohnathan Mantey { 81001784826SJohnathan Mantey messages::internalError(asyncResp->res); 8119010ec2eSRavi Teja return; 81201784826SJohnathan Mantey } 8139010ec2eSRavi Teja 81401784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 8159010ec2eSRavi Teja [asyncResp, ifaceId, 8169010ec2eSRavi Teja gateway](const boost::system::error_code ec2) { 81723a21a1cSEd Tanous if (ec2) 81801784826SJohnathan Mantey { 81901784826SJohnathan Mantey messages::internalError(asyncResp->res); 8209010ec2eSRavi Teja return; 82101784826SJohnathan Mantey } 8229010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 82301784826SJohnathan Mantey }, 82401784826SJohnathan Mantey "xyz.openbmc_project.Network", 82501784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 82601784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 82701784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 82801784826SJohnathan Mantey prefixLength, gateway); 82901784826SJohnathan Mantey }, 83001784826SJohnathan Mantey "xyz.openbmc_project.Network", 83101784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 83201784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 83301784826SJohnathan Mantey } 83401784826SJohnathan Mantey 83501784826SJohnathan Mantey /** 836e48c0fc5SRavi Teja * @brief Deletes given IPv6 837e48c0fc5SRavi Teja * 838e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 839e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 840e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 841e48c0fc5SRavi Teja * 842e48c0fc5SRavi Teja * @return None 843e48c0fc5SRavi Teja */ 844e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 8458d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 846e48c0fc5SRavi Teja { 847e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 848286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 849e48c0fc5SRavi Teja if (ec) 850e48c0fc5SRavi Teja { 851e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 852e48c0fc5SRavi Teja } 853e48c0fc5SRavi Teja }, 854e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 855e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 856e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 857e48c0fc5SRavi Teja } 858e48c0fc5SRavi Teja 859e48c0fc5SRavi Teja /** 86001784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 86101784826SJohnathan Mantey * static IPv6 entry 86201784826SJohnathan Mantey * 86301784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 86401784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 86501784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 86601784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 86701784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 86801784826SJohnathan Mantey * 86901784826SJohnathan Mantey * @return None 87001784826SJohnathan Mantey */ 8718d1b46d7Szhanghch05 inline void 8728d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 8738d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 8748d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 87501784826SJohnathan Mantey { 87601784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 87701784826SJohnathan Mantey [asyncResp, ifaceId, address, 87801784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 87901784826SJohnathan Mantey if (ec) 88001784826SJohnathan Mantey { 88101784826SJohnathan Mantey messages::internalError(asyncResp->res); 88201784826SJohnathan Mantey } 88301784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 88423a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 88523a21a1cSEd Tanous if (ec2) 88601784826SJohnathan Mantey { 88701784826SJohnathan Mantey messages::internalError(asyncResp->res); 88801784826SJohnathan Mantey } 88901784826SJohnathan Mantey }, 89001784826SJohnathan Mantey "xyz.openbmc_project.Network", 89101784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 89201784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 89301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 89401784826SJohnathan Mantey prefixLength, ""); 89501784826SJohnathan Mantey }, 89601784826SJohnathan Mantey "xyz.openbmc_project.Network", 89701784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 89801784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 89901784826SJohnathan Mantey } 90001784826SJohnathan Mantey 90101784826SJohnathan Mantey /** 902e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 903e48c0fc5SRavi Teja * 904e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 905e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 906e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 907e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 908e48c0fc5SRavi Teja * 909e48c0fc5SRavi Teja * @return None 910e48c0fc5SRavi Teja */ 91101784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 91201784826SJohnathan Mantey const std::string& address, 9138d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 914e48c0fc5SRavi Teja { 915e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 916e48c0fc5SRavi Teja if (ec) 917e48c0fc5SRavi Teja { 918e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 919e48c0fc5SRavi Teja } 920e48c0fc5SRavi Teja }; 921e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 9224e0453b1SGunnar Mills // does not have associated gateway property 923e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 924e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 925e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 926e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 927e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 928e48c0fc5SRavi Teja ""); 929e48c0fc5SRavi Teja } 930e48c0fc5SRavi Teja 931179db1d7SKowalski, Kamil /** 932179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 933179db1d7SKowalski, Kamil * Object 934179db1d7SKowalski, Kamil * from EntityManager Network Manager 9354a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 936179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 937179db1d7SKowalski, Kamil * into JSON 938179db1d7SKowalski, Kamil */ 939179db1d7SKowalski, Kamil template <typename CallbackFunc> 94081ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 9411abe55efSEd Tanous CallbackFunc&& callback) 9421abe55efSEd Tanous { 94355c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 944f94c4ecfSEd Tanous [ethifaceId{std::string{ethifaceId}}, 945f94c4ecfSEd Tanous callback{std::forward<CallbackFunc>(callback)}]( 94681ce609eSEd Tanous const boost::system::error_code errorCode, 947711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 94855c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 9494a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 950e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 951179db1d7SKowalski, Kamil 95281ce609eSEd Tanous if (errorCode) 9531abe55efSEd Tanous { 95401784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 955179db1d7SKowalski, Kamil return; 956179db1d7SKowalski, Kamil } 957179db1d7SKowalski, Kamil 9584c9afe43SEd Tanous bool found = 9592c70f800SEd Tanous extractEthernetInterfaceData(ethifaceId, resp, ethData); 9604c9afe43SEd Tanous if (!found) 9614c9afe43SEd Tanous { 96201784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 9634c9afe43SEd Tanous return; 9644c9afe43SEd Tanous } 9654c9afe43SEd Tanous 9662c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 967179db1d7SKowalski, Kamil // Fix global GW 9681abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 9691abe55efSEd Tanous { 970c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 971c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 9729010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 9731abe55efSEd Tanous { 9744a0cb85cSEd Tanous ipv4.gateway = ethData.default_gateway; 975179db1d7SKowalski, Kamil } 976179db1d7SKowalski, Kamil } 977179db1d7SKowalski, Kamil 9782c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 9794e0453b1SGunnar Mills // Finally make a callback with useful data 98001784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 981179db1d7SKowalski, Kamil }, 982179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 983179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 984271584abSEd Tanous } 985179db1d7SKowalski, Kamil 986179db1d7SKowalski, Kamil /** 9879391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 9889391bb9cSRapkiewicz, Pawel * Manager 9891abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 9901abe55efSEd Tanous * into JSON. 9919391bb9cSRapkiewicz, Pawel */ 9929391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 9931abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 9941abe55efSEd Tanous { 99555c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 996f94c4ecfSEd Tanous [callback{std::forward<CallbackFunc>(callback)}]( 99781ce609eSEd Tanous const boost::system::error_code errorCode, 998711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 9991abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 10001abe55efSEd Tanous // ethernet interfaces 10012c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 10022c70f800SEd Tanous ifaceList.reserve(resp.size()); 100381ce609eSEd Tanous if (errorCode) 10041abe55efSEd Tanous { 10052c70f800SEd Tanous callback(false, ifaceList); 10069391bb9cSRapkiewicz, Pawel return; 10079391bb9cSRapkiewicz, Pawel } 10089391bb9cSRapkiewicz, Pawel 10099391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 10104a0cb85cSEd Tanous for (const auto& objpath : resp) 10111abe55efSEd Tanous { 10129391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 10134a0cb85cSEd Tanous for (const auto& interface : objpath.second) 10141abe55efSEd Tanous { 10151abe55efSEd Tanous // If interface is 10164a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 10174a0cb85cSEd Tanous // what we're looking for. 10189391bb9cSRapkiewicz, Pawel if (interface.first == 10191abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 10201abe55efSEd Tanous { 10212dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 10222dfd18efSEd Tanous if (ifaceId.empty()) 10231abe55efSEd Tanous { 10242dfd18efSEd Tanous continue; 10259391bb9cSRapkiewicz, Pawel } 10262dfd18efSEd Tanous // and put it into output vector. 10272dfd18efSEd Tanous ifaceList.emplace(ifaceId); 10289391bb9cSRapkiewicz, Pawel } 10299391bb9cSRapkiewicz, Pawel } 10309391bb9cSRapkiewicz, Pawel } 1031a434f2bdSEd Tanous // Finally make a callback with useful data 10322c70f800SEd Tanous callback(true, ifaceList); 10339391bb9cSRapkiewicz, Pawel }, 1034aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 1035aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 1036271584abSEd Tanous } 10379391bb9cSRapkiewicz, Pawel 10384f48d5f6SEd Tanous inline void 10394f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 10408d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 10411abe55efSEd Tanous { 1042ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 1043ab6554f1SJoshi-Mansi if (hostname.length() > 255) 1044ab6554f1SJoshi-Mansi { 1045ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 1046ab6554f1SJoshi-Mansi "HostName"); 1047ab6554f1SJoshi-Mansi return; 1048ab6554f1SJoshi-Mansi } 1049bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 1050bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 10514a0cb85cSEd Tanous if (ec) 10524a0cb85cSEd Tanous { 1053a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 10541abe55efSEd Tanous } 1055bc0bd6e0SEd Tanous }, 1056bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 1057bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 1058bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 1059168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 1060588c3f0dSKowalski, Kamil } 1061588c3f0dSKowalski, Kamil 10624f48d5f6SEd Tanous inline void 1063*35fb5311STejas Patil handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, 1064*35fb5311STejas Patil const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1065*35fb5311STejas Patil { 1066*35fb5311STejas Patil sdbusplus::message::object_path objPath = 1067*35fb5311STejas Patil "/xyz/openbmc_project/network/" + ifaceId; 1068*35fb5311STejas Patil crow::connections::systemBus->async_method_call( 1069*35fb5311STejas Patil [asyncResp](const boost::system::error_code ec) { 1070*35fb5311STejas Patil if (ec) 1071*35fb5311STejas Patil { 1072*35fb5311STejas Patil messages::internalError(asyncResp->res); 1073*35fb5311STejas Patil } 1074*35fb5311STejas Patil }, 1075*35fb5311STejas Patil "xyz.openbmc_project.Network", objPath, 1076*35fb5311STejas Patil "org.freedesktop.DBus.Properties", "Set", 1077*35fb5311STejas Patil "xyz.openbmc_project.Network.EthernetInterface", "MTU", 1078*35fb5311STejas Patil std::variant<size_t>(mtuSize)); 1079*35fb5311STejas Patil } 1080*35fb5311STejas Patil 1081*35fb5311STejas Patil inline void 10824f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 1083bf648f77SEd Tanous const std::string& domainname, 10848d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1085ab6554f1SJoshi-Mansi { 1086ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 1087ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 1088ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 1089ab6554f1SJoshi-Mansi if (ec) 1090ab6554f1SJoshi-Mansi { 1091ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 1092ab6554f1SJoshi-Mansi } 1093ab6554f1SJoshi-Mansi }, 1094ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 1095ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 1096ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 1097ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1098168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 1099ab6554f1SJoshi-Mansi } 1100ab6554f1SJoshi-Mansi 11014f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 1102bf648f77SEd Tanous { 1103bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 1104bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1105bf648f77SEd Tanous { 1106bf648f77SEd Tanous return false; 1107bf648f77SEd Tanous } 1108bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 1109bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 1110bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 1111bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 1112bf648f77SEd Tanous const std::regex pattern( 1113bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1114bf648f77SEd Tanous 1115bf648f77SEd Tanous return std::regex_match(hostname, pattern); 1116bf648f77SEd Tanous } 1117bf648f77SEd Tanous 11184f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 1119bf648f77SEd Tanous { 1120bf648f77SEd Tanous // Can have multiple subdomains 1121bf648f77SEd Tanous // Top Level Domain's min length is 2 character 11220fda0f12SGeorge Liu const std::regex pattern( 11230fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 1124bf648f77SEd Tanous 1125bf648f77SEd Tanous return std::regex_match(domainname, pattern); 1126bf648f77SEd Tanous } 1127bf648f77SEd Tanous 11284f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 11298d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1130ab6554f1SJoshi-Mansi { 1131ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1132ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1133ab6554f1SJoshi-Mansi { 1134ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1135ab6554f1SJoshi-Mansi return; 1136ab6554f1SJoshi-Mansi } 1137ab6554f1SJoshi-Mansi 1138ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1139ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1140ab6554f1SJoshi-Mansi { 1141ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1142ab6554f1SJoshi-Mansi return; 1143ab6554f1SJoshi-Mansi } 1144ab6554f1SJoshi-Mansi 1145ab6554f1SJoshi-Mansi std::string hostname; 1146ab6554f1SJoshi-Mansi std::string domainname; 1147ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1148ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1149ab6554f1SJoshi-Mansi 1150ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1151ab6554f1SJoshi-Mansi { 1152ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1153ab6554f1SJoshi-Mansi return; 1154ab6554f1SJoshi-Mansi } 1155ab6554f1SJoshi-Mansi 1156ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1157ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1158ab6554f1SJoshi-Mansi } 1159ab6554f1SJoshi-Mansi 11604f48d5f6SEd Tanous inline void 11614f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1162bf648f77SEd Tanous const std::string& macAddress, 11638d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1164d577665bSRatan Gupta { 1165d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 1166d577665bSRatan Gupta [asyncResp, macAddress](const boost::system::error_code ec) { 1167d577665bSRatan Gupta if (ec) 1168d577665bSRatan Gupta { 1169d577665bSRatan Gupta messages::internalError(asyncResp->res); 1170d577665bSRatan Gupta return; 1171d577665bSRatan Gupta } 1172d577665bSRatan Gupta }, 1173d577665bSRatan Gupta "xyz.openbmc_project.Network", 1174d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1175d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1176d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1177168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1178d577665bSRatan Gupta } 1179286b9118SJohnathan Mantey 11804f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 11814f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 11824f48d5f6SEd Tanous const bool v6Value, 11838d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1184da131a9aSJennifer Lee { 11852c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1186da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1187da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1188da131a9aSJennifer Lee if (ec) 1189da131a9aSJennifer Lee { 1190da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1191da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1192da131a9aSJennifer Lee return; 1193da131a9aSJennifer Lee } 11948f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1195da131a9aSJennifer Lee }, 1196da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1197da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1198da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1199da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1200168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1201da131a9aSJennifer Lee } 12021f8c7b5dSJohnathan Mantey 12034f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1204eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 12058d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1206eeedda23SJohnathan Mantey { 1207eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1208eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1209eeedda23SJohnathan Mantey if (ec) 1210eeedda23SJohnathan Mantey { 1211eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1212eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1213eeedda23SJohnathan Mantey return; 1214eeedda23SJohnathan Mantey } 1215eeedda23SJohnathan Mantey }, 1216eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1217eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1218eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1219eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1220168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1221eeedda23SJohnathan Mantey } 1222eeedda23SJohnathan Mantey 12234f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 12248d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1225da131a9aSJennifer Lee { 1226da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1227da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1228da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1229da131a9aSJennifer Lee if (ec) 1230da131a9aSJennifer Lee { 1231da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1232da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1233da131a9aSJennifer Lee return; 1234da131a9aSJennifer Lee } 1235da131a9aSJennifer Lee }, 1236da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1237da131a9aSJennifer Lee "/xyz/openbmc_project/network/config/dhcp", 1238da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1239da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1240168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1241da131a9aSJennifer Lee } 1242d577665bSRatan Gupta 12434f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 12441f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1245f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1246f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 12478d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1248da131a9aSJennifer Lee { 12491f8c7b5dSJohnathan Mantey bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 1250bf648f77SEd Tanous bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false); 1251da131a9aSJennifer Lee 12521f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 12531f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 12541f8c7b5dSJohnathan Mantey 12551f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 12561f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1257da131a9aSJennifer Lee { 12581f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 12591f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 12601f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 12611f8c7b5dSJohnathan Mantey { 1262bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1263bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 12641f8c7b5dSJohnathan Mantey "OperatingMode"); 1265da131a9aSJennifer Lee return; 1266da131a9aSJennifer Lee } 12671f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 12681f8c7b5dSJohnathan Mantey } 12691f8c7b5dSJohnathan Mantey else 1270da131a9aSJennifer Lee { 12711f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 12721f8c7b5dSJohnathan Mantey } 12731f8c7b5dSJohnathan Mantey 12741f8c7b5dSJohnathan Mantey bool nextDNS{}; 12751f8c7b5dSJohnathan Mantey if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers) 12761f8c7b5dSJohnathan Mantey { 12771f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers) 12781f8c7b5dSJohnathan Mantey { 12791f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12801f8c7b5dSJohnathan Mantey return; 12811f8c7b5dSJohnathan Mantey } 12821f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 12831f8c7b5dSJohnathan Mantey } 12841f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useDNSServers) 12851f8c7b5dSJohnathan Mantey { 12861f8c7b5dSJohnathan Mantey nextDNS = *v4dhcpParms.useDNSServers; 12871f8c7b5dSJohnathan Mantey } 12881f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useDNSServers) 12891f8c7b5dSJohnathan Mantey { 12901f8c7b5dSJohnathan Mantey nextDNS = *v6dhcpParms.useDNSServers; 12911f8c7b5dSJohnathan Mantey } 12921f8c7b5dSJohnathan Mantey else 12931f8c7b5dSJohnathan Mantey { 12941f8c7b5dSJohnathan Mantey nextDNS = ethData.DNSEnabled; 12951f8c7b5dSJohnathan Mantey } 12961f8c7b5dSJohnathan Mantey 12971f8c7b5dSJohnathan Mantey bool nextNTP{}; 12981f8c7b5dSJohnathan Mantey if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers) 12991f8c7b5dSJohnathan Mantey { 13001f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers) 13011f8c7b5dSJohnathan Mantey { 13021f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13031f8c7b5dSJohnathan Mantey return; 13041f8c7b5dSJohnathan Mantey } 13051f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 13061f8c7b5dSJohnathan Mantey } 13071f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useNTPServers) 13081f8c7b5dSJohnathan Mantey { 13091f8c7b5dSJohnathan Mantey nextNTP = *v4dhcpParms.useNTPServers; 13101f8c7b5dSJohnathan Mantey } 13111f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useNTPServers) 13121f8c7b5dSJohnathan Mantey { 13131f8c7b5dSJohnathan Mantey nextNTP = *v6dhcpParms.useNTPServers; 13141f8c7b5dSJohnathan Mantey } 13151f8c7b5dSJohnathan Mantey else 13161f8c7b5dSJohnathan Mantey { 13171f8c7b5dSJohnathan Mantey nextNTP = ethData.NTPEnabled; 13181f8c7b5dSJohnathan Mantey } 13191f8c7b5dSJohnathan Mantey 13201f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 13211f8c7b5dSJohnathan Mantey if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName) 13221f8c7b5dSJohnathan Mantey { 13231f8c7b5dSJohnathan Mantey if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName) 13241f8c7b5dSJohnathan Mantey { 13251f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 13261f8c7b5dSJohnathan Mantey return; 13271f8c7b5dSJohnathan Mantey } 13281f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13291f8c7b5dSJohnathan Mantey } 13301f8c7b5dSJohnathan Mantey else if (v4dhcpParms.useUseDomainName) 13311f8c7b5dSJohnathan Mantey { 13321f8c7b5dSJohnathan Mantey nextUseDomain = *v4dhcpParms.useUseDomainName; 13331f8c7b5dSJohnathan Mantey } 13341f8c7b5dSJohnathan Mantey else if (v6dhcpParms.useUseDomainName) 13351f8c7b5dSJohnathan Mantey { 13361f8c7b5dSJohnathan Mantey nextUseDomain = *v6dhcpParms.useUseDomainName; 13371f8c7b5dSJohnathan Mantey } 13381f8c7b5dSJohnathan Mantey else 13391f8c7b5dSJohnathan Mantey { 13401f8c7b5dSJohnathan Mantey nextUseDomain = ethData.HostNameEnabled; 13411f8c7b5dSJohnathan Mantey } 13421f8c7b5dSJohnathan Mantey 1343da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 13441f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 13451f8c7b5dSJohnathan Mantey asyncResp); 1346da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 13471f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1348da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 13491f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 13501f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 13511f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1352da131a9aSJennifer Lee } 135301784826SJohnathan Mantey 13544f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 13552c70f800SEd Tanous getNextStaticIpEntry( 1356bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1357bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 135801784826SJohnathan Mantey { 135917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 136017a897dfSManojkiran Eda return value.origin == "Static"; 136117a897dfSManojkiran Eda }); 136201784826SJohnathan Mantey } 136301784826SJohnathan Mantey 13644f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 13652c70f800SEd Tanous getNextStaticIpEntry( 1366bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1367bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 136801784826SJohnathan Mantey { 136917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 137017a897dfSManojkiran Eda return value.origin == "Static"; 137117a897dfSManojkiran Eda }); 137201784826SJohnathan Mantey } 137301784826SJohnathan Mantey 13744f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1375f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 137601784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 13778d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 13781abe55efSEd Tanous { 137901784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1380f476acbfSRatan Gupta { 138171f52d96SEd Tanous messages::propertyValueTypeError( 138271f52d96SEd Tanous asyncResp->res, 1383bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1384d1d50814SRavi Teja "IPv4StaticAddresses"); 1385f476acbfSRatan Gupta return; 1386f476acbfSRatan Gupta } 1387f476acbfSRatan Gupta 1388271584abSEd Tanous unsigned entryIdx = 1; 138901784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 139001784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 139101784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 139201784826SJohnathan Mantey // into the NIC. 139385ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 13942c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 139501784826SJohnathan Mantey 1396537174c4SEd Tanous for (nlohmann::json& thisJson : input) 13971abe55efSEd Tanous { 13984a0cb85cSEd Tanous std::string pathString = 1399d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1400179db1d7SKowalski, Kamil 140101784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1402f476acbfSRatan Gupta { 1403537174c4SEd Tanous std::optional<std::string> address; 1404537174c4SEd Tanous std::optional<std::string> subnetMask; 1405537174c4SEd Tanous std::optional<std::string> gateway; 1406537174c4SEd Tanous 1407537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 14087e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 14097e27d832SJohnathan Mantey "Gateway", gateway)) 1410537174c4SEd Tanous { 141101784826SJohnathan Mantey messages::propertyValueFormatError( 141271f52d96SEd Tanous asyncResp->res, 141371f52d96SEd Tanous thisJson.dump(2, ' ', true, 141471f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 141571f52d96SEd Tanous pathString); 1416537174c4SEd Tanous return; 1417179db1d7SKowalski, Kamil } 1418179db1d7SKowalski, Kamil 141901784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 142001784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 142101784826SJohnathan Mantey // current state of the interface. Merge existing state into the 142201784826SJohnathan Mantey // current request. 1423271584abSEd Tanous const std::string* addr = nullptr; 1424271584abSEd Tanous const std::string* gw = nullptr; 142501784826SJohnathan Mantey uint8_t prefixLength = 0; 142601784826SJohnathan Mantey bool errorInEntry = false; 1427537174c4SEd Tanous if (address) 14281abe55efSEd Tanous { 142901784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*address)) 14301abe55efSEd Tanous { 143101784826SJohnathan Mantey addr = &(*address); 14324a0cb85cSEd Tanous } 143301784826SJohnathan Mantey else 143401784826SJohnathan Mantey { 1435bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1436bf648f77SEd Tanous pathString + "/Address"); 143701784826SJohnathan Mantey errorInEntry = true; 143801784826SJohnathan Mantey } 143901784826SJohnathan Mantey } 144085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 144101784826SJohnathan Mantey { 144285ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 144301784826SJohnathan Mantey } 144401784826SJohnathan Mantey else 144501784826SJohnathan Mantey { 144601784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 144701784826SJohnathan Mantey pathString + "/Address"); 144801784826SJohnathan Mantey errorInEntry = true; 14494a0cb85cSEd Tanous } 14504a0cb85cSEd Tanous 1451537174c4SEd Tanous if (subnetMask) 14524a0cb85cSEd Tanous { 1453537174c4SEd Tanous if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength)) 14544a0cb85cSEd Tanous { 1455f12894f8SJason M. Bills messages::propertyValueFormatError( 1456537174c4SEd Tanous asyncResp->res, *subnetMask, 14574a0cb85cSEd Tanous pathString + "/SubnetMask"); 145801784826SJohnathan Mantey errorInEntry = true; 14594a0cb85cSEd Tanous } 14604a0cb85cSEd Tanous } 146185ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 14624a0cb85cSEd Tanous { 146385ffe86aSJiaqing Zhao if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 146401784826SJohnathan Mantey &prefixLength)) 14654a0cb85cSEd Tanous { 146601784826SJohnathan Mantey messages::propertyValueFormatError( 146785ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 146801784826SJohnathan Mantey pathString + "/SubnetMask"); 146901784826SJohnathan Mantey errorInEntry = true; 14704a0cb85cSEd Tanous } 14714a0cb85cSEd Tanous } 14721abe55efSEd Tanous else 14731abe55efSEd Tanous { 147401784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 147501784826SJohnathan Mantey pathString + "/SubnetMask"); 147601784826SJohnathan Mantey errorInEntry = true; 147701784826SJohnathan Mantey } 147801784826SJohnathan Mantey 147901784826SJohnathan Mantey if (gateway) 148001784826SJohnathan Mantey { 148101784826SJohnathan Mantey if (ipv4VerifyIpAndGetBitcount(*gateway)) 148201784826SJohnathan Mantey { 148301784826SJohnathan Mantey gw = &(*gateway); 148401784826SJohnathan Mantey } 148501784826SJohnathan Mantey else 148601784826SJohnathan Mantey { 1487bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1488bf648f77SEd Tanous pathString + "/Gateway"); 148901784826SJohnathan Mantey errorInEntry = true; 149001784826SJohnathan Mantey } 149101784826SJohnathan Mantey } 149285ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 149301784826SJohnathan Mantey { 149485ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 149501784826SJohnathan Mantey } 149601784826SJohnathan Mantey else 14971abe55efSEd Tanous { 1498a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 14994a0cb85cSEd Tanous pathString + "/Gateway"); 150001784826SJohnathan Mantey errorInEntry = true; 15014a0cb85cSEd Tanous } 15024a0cb85cSEd Tanous 150301784826SJohnathan Mantey if (errorInEntry) 15041abe55efSEd Tanous { 150501784826SJohnathan Mantey return; 15064a0cb85cSEd Tanous } 15074a0cb85cSEd Tanous 150885ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 15091abe55efSEd Tanous { 151085ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1511bf648f77SEd Tanous *addr, asyncResp); 151285ffe86aSJiaqing Zhao nicIpEntry = 151385ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1514588c3f0dSKowalski, Kamil } 151501784826SJohnathan Mantey else 151601784826SJohnathan Mantey { 1517cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1518cb13a392SEd Tanous asyncResp); 15194a0cb85cSEd Tanous } 15204a0cb85cSEd Tanous entryIdx++; 15214a0cb85cSEd Tanous } 152201784826SJohnathan Mantey else 152301784826SJohnathan Mantey { 152485ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 152501784826SJohnathan Mantey { 152601784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 152701784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 152801784826SJohnathan Mantey // in error, so bail out. 152901784826SJohnathan Mantey if (thisJson.is_null()) 153001784826SJohnathan Mantey { 153101784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 153201784826SJohnathan Mantey return; 153301784826SJohnathan Mantey } 153401784826SJohnathan Mantey messages::propertyValueFormatError( 153571f52d96SEd Tanous asyncResp->res, 153671f52d96SEd Tanous thisJson.dump(2, ' ', true, 153771f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 153871f52d96SEd Tanous pathString); 153901784826SJohnathan Mantey return; 154001784826SJohnathan Mantey } 154101784826SJohnathan Mantey 154201784826SJohnathan Mantey if (thisJson.is_null()) 154301784826SJohnathan Mantey { 154485ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 154501784826SJohnathan Mantey } 154685ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 154701784826SJohnathan Mantey { 154885ffe86aSJiaqing Zhao nicIpEntry = 154985ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 155001784826SJohnathan Mantey } 155101784826SJohnathan Mantey entryIdx++; 155201784826SJohnathan Mantey } 155301784826SJohnathan Mantey } 15544a0cb85cSEd Tanous } 15554a0cb85cSEd Tanous 15564f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1557f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1558f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 15598d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1560f85837bfSRAJESWARAN THILLAIGOVINDAN { 1561f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1562286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1563f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1564f85837bfSRAJESWARAN THILLAIGOVINDAN { 1565f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1566f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1567f85837bfSRAJESWARAN THILLAIGOVINDAN } 1568f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1569f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1570f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1571f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1572bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1573168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1574f85837bfSRAJESWARAN THILLAIGOVINDAN } 1575f85837bfSRAJESWARAN THILLAIGOVINDAN 15764f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1577f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 157801784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 15798d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1580e48c0fc5SRavi Teja { 158101784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1582e48c0fc5SRavi Teja { 158371f52d96SEd Tanous messages::propertyValueTypeError( 158471f52d96SEd Tanous asyncResp->res, 1585bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1586e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1587e48c0fc5SRavi Teja return; 1588e48c0fc5SRavi Teja } 1589271584abSEd Tanous size_t entryIdx = 1; 159085ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 15912c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1592f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1593e48c0fc5SRavi Teja { 1594e48c0fc5SRavi Teja std::string pathString = 1595e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1596e48c0fc5SRavi Teja 159701784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1598e48c0fc5SRavi Teja { 1599e48c0fc5SRavi Teja std::optional<std::string> address; 1600e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1601f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1602bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1603bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1604e48c0fc5SRavi Teja { 160501784826SJohnathan Mantey messages::propertyValueFormatError( 160671f52d96SEd Tanous asyncResp->res, 160771f52d96SEd Tanous thisJson.dump(2, ' ', true, 160871f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 160971f52d96SEd Tanous pathString); 1610e48c0fc5SRavi Teja return; 1611e48c0fc5SRavi Teja } 1612e48c0fc5SRavi Teja 1613543f4400SEd Tanous const std::string* addr = nullptr; 1614543f4400SEd Tanous uint8_t prefix = 0; 161501784826SJohnathan Mantey 161601784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 161701784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 161801784826SJohnathan Mantey // current state of the interface. Merge existing state into the 161901784826SJohnathan Mantey // current request. 1620e48c0fc5SRavi Teja if (address) 1621e48c0fc5SRavi Teja { 162201784826SJohnathan Mantey addr = &(*address); 1623e48c0fc5SRavi Teja } 162485ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 162501784826SJohnathan Mantey { 162685ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 162701784826SJohnathan Mantey } 162801784826SJohnathan Mantey else 162901784826SJohnathan Mantey { 163001784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 163101784826SJohnathan Mantey pathString + "/Address"); 163201784826SJohnathan Mantey return; 1633e48c0fc5SRavi Teja } 1634e48c0fc5SRavi Teja 1635e48c0fc5SRavi Teja if (prefixLength) 1636e48c0fc5SRavi Teja { 163701784826SJohnathan Mantey prefix = *prefixLength; 163801784826SJohnathan Mantey } 163985ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1640e48c0fc5SRavi Teja { 164185ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1642e48c0fc5SRavi Teja } 1643e48c0fc5SRavi Teja else 1644e48c0fc5SRavi Teja { 1645e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1646e48c0fc5SRavi Teja pathString + "/PrefixLength"); 164701784826SJohnathan Mantey return; 1648e48c0fc5SRavi Teja } 1649e48c0fc5SRavi Teja 165085ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1651e48c0fc5SRavi Teja { 165285ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1653e48c0fc5SRavi Teja asyncResp); 165485ffe86aSJiaqing Zhao nicIpEntry = 165585ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 165601784826SJohnathan Mantey } 165701784826SJohnathan Mantey else 165801784826SJohnathan Mantey { 165901784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1660e48c0fc5SRavi Teja } 1661e48c0fc5SRavi Teja entryIdx++; 1662e48c0fc5SRavi Teja } 166301784826SJohnathan Mantey else 166401784826SJohnathan Mantey { 166585ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 166601784826SJohnathan Mantey { 166701784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 166801784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 166901784826SJohnathan Mantey // in error, so bail out. 167001784826SJohnathan Mantey if (thisJson.is_null()) 167101784826SJohnathan Mantey { 167201784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 167301784826SJohnathan Mantey return; 167401784826SJohnathan Mantey } 167501784826SJohnathan Mantey messages::propertyValueFormatError( 167671f52d96SEd Tanous asyncResp->res, 167771f52d96SEd Tanous thisJson.dump(2, ' ', true, 167871f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 167971f52d96SEd Tanous pathString); 168001784826SJohnathan Mantey return; 168101784826SJohnathan Mantey } 168201784826SJohnathan Mantey 168301784826SJohnathan Mantey if (thisJson.is_null()) 168401784826SJohnathan Mantey { 168585ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 168601784826SJohnathan Mantey } 168785ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 168801784826SJohnathan Mantey { 168985ffe86aSJiaqing Zhao nicIpEntry = 169085ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 169101784826SJohnathan Mantey } 169201784826SJohnathan Mantey entryIdx++; 169301784826SJohnathan Mantey } 169401784826SJohnathan Mantey } 1695e48c0fc5SRavi Teja } 1696e48c0fc5SRavi Teja 16974f48d5f6SEd Tanous inline void parseInterfaceData( 16988d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16998d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1700e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 170101784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 17024a0cb85cSEd Tanous { 1703eeedda23SJohnathan Mantey constexpr const std::array<const char*, 1> inventoryForEthernet = { 1704eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1705eeedda23SJohnathan Mantey 17062c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 170781ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 17082c70f800SEd Tanous jsonResponse["@odata.id"] = 170981ce609eSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 17102c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1711eeedda23SJohnathan Mantey 1712eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1713eeedda23SJohnathan Mantey 1714eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1715eeedda23SJohnathan Mantey [health](const boost::system::error_code ec, 1716914e2d5dSEd Tanous const std::vector<std::string>& resp) { 1717eeedda23SJohnathan Mantey if (ec) 1718029573d4SEd Tanous { 1719eeedda23SJohnathan Mantey return; 1720eeedda23SJohnathan Mantey } 1721eeedda23SJohnathan Mantey 1722914e2d5dSEd Tanous health->inventory = resp; 1723eeedda23SJohnathan Mantey }, 1724eeedda23SJohnathan Mantey "xyz.openbmc_project.ObjectMapper", 1725eeedda23SJohnathan Mantey "/xyz/openbmc_project/object_mapper", 1726bf648f77SEd Tanous "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0), 1727bf648f77SEd Tanous inventoryForEthernet); 1728eeedda23SJohnathan Mantey 1729eeedda23SJohnathan Mantey health->populate(); 1730eeedda23SJohnathan Mantey 1731eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1732eeedda23SJohnathan Mantey { 17332c70f800SEd Tanous jsonResponse["LinkStatus"] = "LinkUp"; 17342c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1735029573d4SEd Tanous } 1736029573d4SEd Tanous else 1737029573d4SEd Tanous { 17382c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 17392c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1740029573d4SEd Tanous } 1741aa05fb27SJohnathan Mantey 17422c70f800SEd Tanous jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 17432c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 1744*35fb5311STejas Patil jsonResponse["MTUSize"] = ethData.mtuSize; 17452c70f800SEd Tanous jsonResponse["MACAddress"] = ethData.mac_address; 17462c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 17471f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, true); 17482c70f800SEd Tanous jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled; 17492c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled; 17502c70f800SEd Tanous jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled; 17511f8c7b5dSJohnathan Mantey 17522c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 17531f8c7b5dSJohnathan Mantey translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful" 17541f8c7b5dSJohnathan Mantey : "Disabled"; 17552c70f800SEd Tanous jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled; 17562c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled; 17572c70f800SEd Tanous jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled; 17582a133282Smanojkiraneda 17594a0cb85cSEd Tanous if (!ethData.hostname.empty()) 17604a0cb85cSEd Tanous { 17612c70f800SEd Tanous jsonResponse["HostName"] = ethData.hostname; 1762ab6554f1SJoshi-Mansi 1763ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1764ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1765ab6554f1SJoshi-Mansi // FQDN 1766f23b7296SEd Tanous std::string fqdn = ethData.hostname; 1767d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1768d24bfc7aSJennifer Lee { 17692c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1770d24bfc7aSJennifer Lee } 17712c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 17724a0cb85cSEd Tanous } 17734a0cb85cSEd Tanous 17742c70f800SEd Tanous jsonResponse["VLANs"] = { 1775bf648f77SEd Tanous {"@odata.id", 1776bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}}; 1777fda13ad2SSunitha Harish 17782c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 17792c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 17804a0cb85cSEd Tanous 17812c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 17822c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 17832c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 17842c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 17852c70f800SEd Tanous for (auto& ipv4Config : ipv4Data) 17864a0cb85cSEd Tanous { 1787fa5053a6SGunnar Mills 17882c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1789fa5053a6SGunnar Mills if (gatewayStr.empty()) 1790fa5053a6SGunnar Mills { 1791fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1792fa5053a6SGunnar Mills } 1793fa5053a6SGunnar Mills 17942c70f800SEd Tanous ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin}, 17952c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 17962c70f800SEd Tanous {"Address", ipv4Config.address}, 1797fa5053a6SGunnar Mills {"Gateway", gatewayStr}}); 17982c70f800SEd Tanous if (ipv4Config.origin == "Static") 1799d1d50814SRavi Teja { 18002c70f800SEd Tanous ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin}, 18012c70f800SEd Tanous {"SubnetMask", ipv4Config.netmask}, 18022c70f800SEd Tanous {"Address", ipv4Config.address}, 1803d1d50814SRavi Teja {"Gateway", gatewayStr}}); 1804d1d50814SRavi Teja } 180501784826SJohnathan Mantey } 1806d1d50814SRavi Teja 18077ea79e5eSRavi Teja std::string ipv6GatewayStr = ethData.ipv6_default_gateway; 18087ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 18097ea79e5eSRavi Teja { 18107ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 18117ea79e5eSRavi Teja } 18127ea79e5eSRavi Teja 18137ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1814e48c0fc5SRavi Teja 18152c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 18162c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 18172c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 18182c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 18197f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 18202c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 18217f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 18222c70f800SEd Tanous for (auto& ipv6Config : ipv6Data) 1823e48c0fc5SRavi Teja { 18242c70f800SEd Tanous ipv6Array.push_back({{"Address", ipv6Config.address}, 18252c70f800SEd Tanous {"PrefixLength", ipv6Config.prefixLength}, 18262c70f800SEd Tanous {"AddressOrigin", ipv6Config.origin}, 18275fd16e4bSJohnathan Mantey {"AddressState", nullptr}}); 18282c70f800SEd Tanous if (ipv6Config.origin == "Static") 1829e48c0fc5SRavi Teja { 18302c70f800SEd Tanous ipv6StaticArray.push_back( 18312c70f800SEd Tanous {{"Address", ipv6Config.address}, 18327a474a5fSJiaqing Zhao {"PrefixLength", ipv6Config.prefixLength}}); 183301784826SJohnathan Mantey } 1834e48c0fc5SRavi Teja } 1835588c3f0dSKowalski, Kamil } 1836588c3f0dSKowalski, Kamil 18374f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 1838bf648f77SEd Tanous const std::string& parentIfaceId, 1839bf648f77SEd Tanous const std::string& ifaceId, 1840bf648f77SEd Tanous const EthernetInterfaceData& ethData) 18411abe55efSEd Tanous { 1842bf648f77SEd Tanous // Fill out obvious data... 1843bf648f77SEd Tanous jsonResponse["Id"] = ifaceId; 1844bf648f77SEd Tanous jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1845bf648f77SEd Tanous parentIfaceId + "/VLANs/" + ifaceId; 1846bf648f77SEd Tanous 1847bf648f77SEd Tanous jsonResponse["VLANEnable"] = true; 1848bf648f77SEd Tanous if (!ethData.vlan_id.empty()) 1849bf648f77SEd Tanous { 1850bf648f77SEd Tanous jsonResponse["VLANId"] = ethData.vlan_id.back(); 1851bf648f77SEd Tanous } 1852bf648f77SEd Tanous } 1853bf648f77SEd Tanous 18544f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1855bf648f77SEd Tanous { 1856bf648f77SEd Tanous if (!boost::starts_with(iface, parent + "_")) 1857bf648f77SEd Tanous { 1858bf648f77SEd Tanous return false; 1859bf648f77SEd Tanous } 1860bf648f77SEd Tanous return true; 1861bf648f77SEd Tanous } 1862bf648f77SEd Tanous 1863bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1864bf648f77SEd Tanous { 1865bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1866ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 1867bf648f77SEd Tanous .methods( 1868bf648f77SEd Tanous boost::beast::http::verb:: 1869bf648f77SEd Tanous get)([](const crow::Request&, 1870bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 1871bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1872bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1873bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1874bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1875bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1876bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1877bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1878bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1879bf648f77SEd Tanous 1880bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1881bf648f77SEd Tanous // preparation 1882bf648f77SEd Tanous getEthernetIfaceList([asyncResp](const bool& success, 1883bf648f77SEd Tanous const boost::container::flat_set< 1884bf648f77SEd Tanous std::string>& ifaceList) { 1885bf648f77SEd Tanous if (!success) 18861abe55efSEd Tanous { 1887f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18889391bb9cSRapkiewicz, Pawel return; 18899391bb9cSRapkiewicz, Pawel } 18909391bb9cSRapkiewicz, Pawel 1891bf648f77SEd Tanous nlohmann::json& ifaceArray = 1892bf648f77SEd Tanous asyncResp->res.jsonValue["Members"]; 1893bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1894bf648f77SEd Tanous std::string tag = "_"; 1895bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1896bf648f77SEd Tanous { 1897bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1898bf648f77SEd Tanous if (found == std::string::npos) 1899bf648f77SEd Tanous { 1900bf648f77SEd Tanous ifaceArray.push_back( 1901bf648f77SEd Tanous {{"@odata.id", 1902bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1903bf648f77SEd Tanous ifaceItem}}); 1904bf648f77SEd Tanous } 1905bf648f77SEd Tanous } 1906bf648f77SEd Tanous 1907bf648f77SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 1908bf648f77SEd Tanous ifaceArray.size(); 1909bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1910bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1911bf648f77SEd Tanous }); 1912bf648f77SEd Tanous }); 1913bf648f77SEd Tanous 1914bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1915ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1916bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 1917bf648f77SEd Tanous [](const crow::Request&, 1918bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1919bf648f77SEd Tanous const std::string& ifaceId) { 19204a0cb85cSEd Tanous getEthernetIfaceData( 1921bf648f77SEd Tanous ifaceId, 1922bf648f77SEd Tanous [asyncResp, 1923bf648f77SEd Tanous ifaceId](const bool& success, 1924bf648f77SEd Tanous const EthernetInterfaceData& ethData, 1925bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 1926bf648f77SEd Tanous ipv4Data, 1927bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 1928bf648f77SEd Tanous ipv6Data) { 19294a0cb85cSEd Tanous if (!success) 19301abe55efSEd Tanous { 1931bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1932bf648f77SEd Tanous // existing object, and other errors 1933bf648f77SEd Tanous messages::resourceNotFound( 1934bf648f77SEd Tanous asyncResp->res, "EthernetInterface", ifaceId); 19354a0cb85cSEd Tanous return; 19369391bb9cSRapkiewicz, Pawel } 19374c9afe43SEd Tanous 19380f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1939fda13ad2SSunitha Harish "#EthernetInterface.v1_4_1.EthernetInterface"; 1940bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1941bf648f77SEd Tanous "Manager Ethernet Interface"; 19420f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 19430f74e643SEd Tanous "Management Network Interface"; 19440f74e643SEd Tanous 1945bf648f77SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, 1946bf648f77SEd Tanous ipv4Data, ipv6Data); 19479391bb9cSRapkiewicz, Pawel }); 1948bf648f77SEd Tanous }); 19499391bb9cSRapkiewicz, Pawel 1950bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1951ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1952ed398213SEd Tanous 1953bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 1954bf648f77SEd Tanous [](const crow::Request& req, 1955bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1956bf648f77SEd Tanous const std::string& ifaceId) { 1957bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1958ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1959d577665bSRatan Gupta std::optional<std::string> macAddress; 19609a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1961d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1962e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1963f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1964da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 19651f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1966eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 1967*35fb5311STejas Patil std::optional<size_t> mtuSize; 19681f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 19691f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 19700627a2c7SEd Tanous 19711f8c7b5dSJohnathan Mantey if (!json_util::readJson( 19728d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1973bf648f77SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, 1974bf648f77SEd Tanous "MACAddress", macAddress, "StaticNameServers", 1975bf648f77SEd Tanous staticNameServers, "IPv6DefaultGateway", 1976bf648f77SEd Tanous ipv6DefaultGateway, "IPv6StaticAddresses", 1977ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1978*35fb5311STejas Patil "MTUSize", mtuSize, "InterfaceEnabled", 1979*35fb5311STejas Patil interfaceEnabled)) 19801abe55efSEd Tanous { 1981588c3f0dSKowalski, Kamil return; 1982588c3f0dSKowalski, Kamil } 1983da131a9aSJennifer Lee if (dhcpv4) 1984da131a9aSJennifer Lee { 1985bf648f77SEd Tanous if (!json_util::readJson( 1986bf648f77SEd Tanous *dhcpv4, asyncResp->res, "DHCPEnabled", 19871f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 19881f8c7b5dSJohnathan Mantey v4dhcpParms.useDNSServers, "UseNTPServers", 19891f8c7b5dSJohnathan Mantey v4dhcpParms.useNTPServers, "UseDomainName", 19901f8c7b5dSJohnathan Mantey v4dhcpParms.useUseDomainName)) 19911f8c7b5dSJohnathan Mantey { 19921f8c7b5dSJohnathan Mantey return; 19931f8c7b5dSJohnathan Mantey } 19941f8c7b5dSJohnathan Mantey } 19951f8c7b5dSJohnathan Mantey 19961f8c7b5dSJohnathan Mantey if (dhcpv6) 19971f8c7b5dSJohnathan Mantey { 1998bf648f77SEd Tanous if (!json_util::readJson( 1999bf648f77SEd Tanous *dhcpv6, asyncResp->res, "OperatingMode", 2000bf648f77SEd Tanous v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers", 2001bf648f77SEd Tanous v6dhcpParms.useDNSServers, "UseNTPServers", 2002bf648f77SEd Tanous v6dhcpParms.useNTPServers, "UseDomainName", 20031f8c7b5dSJohnathan Mantey v6dhcpParms.useUseDomainName)) 20041f8c7b5dSJohnathan Mantey { 20051f8c7b5dSJohnathan Mantey return; 20061f8c7b5dSJohnathan Mantey } 2007da131a9aSJennifer Lee } 2008da131a9aSJennifer Lee 2009bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2010bf648f77SEd Tanous // for JSON preparation 20114a0cb85cSEd Tanous getEthernetIfaceData( 20122c70f800SEd Tanous ifaceId, 2013bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 2014ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 2015d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 20169a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 2017e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 20181f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 20191f8c7b5dSJohnathan Mantey dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), 2020*35fb5311STejas Patil mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms), 2021f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 2022bf648f77SEd Tanous const bool& success, 2023bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2024bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>& 2025bf648f77SEd Tanous ipv4Data, 2026bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>& 2027bf648f77SEd Tanous ipv6Data) { 20281abe55efSEd Tanous if (!success) 20291abe55efSEd Tanous { 2030588c3f0dSKowalski, Kamil // ... otherwise return error 2031bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2032bf648f77SEd Tanous // existing object, and other errors 2033bf648f77SEd Tanous messages::resourceNotFound( 2034bf648f77SEd Tanous asyncResp->res, "Ethernet Interface", ifaceId); 2035588c3f0dSKowalski, Kamil return; 2036588c3f0dSKowalski, Kamil } 2037588c3f0dSKowalski, Kamil 20381f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 20391f8c7b5dSJohnathan Mantey { 2040bf648f77SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, 2041bf648f77SEd Tanous v6dhcpParms, asyncResp); 20421f8c7b5dSJohnathan Mantey } 20431f8c7b5dSJohnathan Mantey 20440627a2c7SEd Tanous if (hostname) 20451abe55efSEd Tanous { 20460627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 20471abe55efSEd Tanous } 20480627a2c7SEd Tanous 2049ab6554f1SJoshi-Mansi if (fqdn) 2050ab6554f1SJoshi-Mansi { 20512c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 2052ab6554f1SJoshi-Mansi } 2053ab6554f1SJoshi-Mansi 2054d577665bSRatan Gupta if (macAddress) 2055d577665bSRatan Gupta { 2056bf648f77SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, 2057bf648f77SEd Tanous asyncResp); 2058d577665bSRatan Gupta } 2059d577665bSRatan Gupta 2060d1d50814SRavi Teja if (ipv4StaticAddresses) 2061d1d50814SRavi Teja { 2062bf648f77SEd Tanous // TODO(ed) for some reason the capture of 2063bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 2064bf648f77SEd Tanous // not a non-const value. This doesn't really work 2065bf648f77SEd Tanous // for us, as we need to be able to efficiently move 2066bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 2067bf648f77SEd Tanous // makes a copy of the structure, and operates on 2068bf648f77SEd Tanous // that, but could be done more efficiently 2069f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 20702c70f800SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, 2071d1d50814SRavi Teja asyncResp); 20721abe55efSEd Tanous } 20730627a2c7SEd Tanous 2074f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 2075f85837bfSRAJESWARAN THILLAIGOVINDAN { 2076bf648f77SEd Tanous handleStaticNameServersPatch( 2077bf648f77SEd Tanous ifaceId, *staticNameServers, asyncResp); 2078f85837bfSRAJESWARAN THILLAIGOVINDAN } 20799a6fc6feSRavi Teja 20809a6fc6feSRavi Teja if (ipv6DefaultGateway) 20819a6fc6feSRavi Teja { 20829a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 20839a6fc6feSRavi Teja "IPv6DefaultGateway"); 20849a6fc6feSRavi Teja } 2085e48c0fc5SRavi Teja 2086e48c0fc5SRavi Teja if (ipv6StaticAddresses) 2087e48c0fc5SRavi Teja { 2088f23b7296SEd Tanous nlohmann::json ipv6Static = *ipv6StaticAddresses; 20892c70f800SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, 209001784826SJohnathan Mantey ipv6Data, asyncResp); 2091e48c0fc5SRavi Teja } 2092eeedda23SJohnathan Mantey 2093eeedda23SJohnathan Mantey if (interfaceEnabled) 2094eeedda23SJohnathan Mantey { 2095eeedda23SJohnathan Mantey setEthernetInterfaceBoolProperty( 2096bf648f77SEd Tanous ifaceId, "NICEnabled", *interfaceEnabled, 2097bf648f77SEd Tanous asyncResp); 2098eeedda23SJohnathan Mantey } 2099*35fb5311STejas Patil 2100*35fb5311STejas Patil if (mtuSize) 2101*35fb5311STejas Patil { 2102*35fb5311STejas Patil handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); 2103*35fb5311STejas Patil } 2104588c3f0dSKowalski, Kamil }); 2105bf648f77SEd Tanous }); 21069391bb9cSRapkiewicz, Pawel 2107bf648f77SEd Tanous BMCWEB_ROUTE( 2108bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2109ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 2110ed398213SEd Tanous 2111bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 2112bf648f77SEd Tanous [](const crow::Request& /* req */, 2113bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2114bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 21158d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 21160f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 21178d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2118e439f0f8SKowalski, Kamil 21192c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 21201abe55efSEd Tanous { 2121a434f2bdSEd Tanous return; 2122a434f2bdSEd Tanous } 2123a434f2bdSEd Tanous 2124bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2125bf648f77SEd Tanous // for JSON preparation 21264a0cb85cSEd Tanous getEthernetIfaceData( 2127bf648f77SEd Tanous ifaceId, 2128bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2129bf648f77SEd Tanous const bool& success, 2130bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2131cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2132cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2133fda13ad2SSunitha Harish if (success && ethData.vlan_id.size() != 0) 21341abe55efSEd Tanous { 2135bf648f77SEd Tanous parseInterfaceData(asyncResp->res.jsonValue, 2136bf648f77SEd Tanous parentIfaceId, ifaceId, ethData); 21371abe55efSEd Tanous } 21381abe55efSEd Tanous else 21391abe55efSEd Tanous { 2140e439f0f8SKowalski, Kamil // ... otherwise return error 2141bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2142bf648f77SEd Tanous // existing object, and other errors 2143bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2144bf648f77SEd Tanous "VLAN Network Interface", 2145bf648f77SEd Tanous ifaceId); 2146e439f0f8SKowalski, Kamil } 2147e439f0f8SKowalski, Kamil }); 2148bf648f77SEd Tanous }); 2149e439f0f8SKowalski, Kamil 2150bf648f77SEd Tanous BMCWEB_ROUTE( 2151bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2152ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2153ed398213SEd Tanous //.privileges(redfish::privileges::patchVLanNetworkInterface) 2154432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2155bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 2156bf648f77SEd Tanous [](const crow::Request& req, 2157bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2158bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 2159fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 21601abe55efSEd Tanous { 2161bf648f77SEd Tanous messages::resourceNotFound( 2162bf648f77SEd Tanous asyncResp->res, "VLAN Network Interface", ifaceId); 2163927a505aSKowalski, Kamil return; 2164927a505aSKowalski, Kamil } 2165927a505aSKowalski, Kamil 21660627a2c7SEd Tanous bool vlanEnable = false; 216738268fa8SAndrew Geissler uint32_t vlanId = 0; 21680627a2c7SEd Tanous 2169bf648f77SEd Tanous if (!json_util::readJson(req, asyncResp->res, "VLANEnable", 2170bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 21711abe55efSEd Tanous { 2172927a505aSKowalski, Kamil return; 2173927a505aSKowalski, Kamil } 2174927a505aSKowalski, Kamil 2175bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2176bf648f77SEd Tanous // for JSON preparation 2177e48c0fc5SRavi Teja getEthernetIfaceData( 2178bf648f77SEd Tanous ifaceId, 2179bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId]( 2180bf648f77SEd Tanous const bool& success, 2181bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2182cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2183cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 218408244d02SSunitha Harish if (success && !ethData.vlan_id.empty()) 218508244d02SSunitha Harish { 218608244d02SSunitha Harish auto callback = 2187bf648f77SEd Tanous [asyncResp]( 2188bf648f77SEd Tanous const boost::system::error_code ec) { 218908244d02SSunitha Harish if (ec) 219008244d02SSunitha Harish { 219108244d02SSunitha Harish messages::internalError(asyncResp->res); 219208244d02SSunitha Harish } 219308244d02SSunitha Harish }; 219408244d02SSunitha Harish 219508244d02SSunitha Harish if (vlanEnable == true) 219608244d02SSunitha Harish { 219708244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2198bf648f77SEd Tanous std::move(callback), 2199bf648f77SEd Tanous "xyz.openbmc_project.Network", 220008244d02SSunitha Harish "/xyz/openbmc_project/network/" + ifaceId, 220108244d02SSunitha Harish "org.freedesktop.DBus.Properties", "Set", 220208244d02SSunitha Harish "xyz.openbmc_project.Network.VLAN", "Id", 2203168e20c1SEd Tanous dbus::utility::DbusVariantType(vlanId)); 220408244d02SSunitha Harish } 220508244d02SSunitha Harish else 220608244d02SSunitha Harish { 2207bf648f77SEd Tanous BMCWEB_LOG_DEBUG 2208bf648f77SEd Tanous << "vlanEnable is false. Deleting the " 2209e48c0fc5SRavi Teja "vlan interface"; 221008244d02SSunitha Harish crow::connections::systemBus->async_method_call( 2211bf648f77SEd Tanous std::move(callback), 2212bf648f77SEd Tanous "xyz.openbmc_project.Network", 2213bf648f77SEd Tanous std::string( 2214bf648f77SEd Tanous "/xyz/openbmc_project/network/") + 2215e48c0fc5SRavi Teja ifaceId, 2216bf648f77SEd Tanous "xyz.openbmc_project.Object.Delete", 2217bf648f77SEd Tanous "Delete"); 221808244d02SSunitha Harish } 221908244d02SSunitha Harish } 222008244d02SSunitha Harish else 22211abe55efSEd Tanous { 2222bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2223bf648f77SEd Tanous // existing object, and other errors 2224bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2225bf648f77SEd Tanous "VLAN Network Interface", 2226bf648f77SEd Tanous ifaceId); 2227bf648f77SEd Tanous return; 2228bf648f77SEd Tanous } 2229bf648f77SEd Tanous }); 2230bf648f77SEd Tanous }); 2231bf648f77SEd Tanous 2232bf648f77SEd Tanous BMCWEB_ROUTE( 2233bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2234ed398213SEd Tanous // This privilege is incorrect, it should be ConfigureManager 2235ed398213SEd Tanous //.privileges(redfish::privileges::deleteVLanNetworkInterface) 2236432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2237bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 2238bf648f77SEd Tanous [](const crow::Request& /* req */, 2239bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2240bf648f77SEd Tanous const std::string& parentIfaceId, const std::string& ifaceId) { 2241bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2242bf648f77SEd Tanous { 2243e48c0fc5SRavi Teja messages::resourceNotFound( 2244e48c0fc5SRavi Teja asyncResp->res, "VLAN Network Interface", ifaceId); 2245927a505aSKowalski, Kamil return; 2246927a505aSKowalski, Kamil } 2247e439f0f8SKowalski, Kamil 2248bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2249bf648f77SEd Tanous // for JSON preparation 2250f12894f8SJason M. Bills getEthernetIfaceData( 2251bf648f77SEd Tanous ifaceId, 2252bf648f77SEd Tanous [asyncResp, parentIfaceId, ifaceId]( 2253bf648f77SEd Tanous const bool& success, 2254bf648f77SEd Tanous const EthernetInterfaceData& ethData, 2255cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2256cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 2257fda13ad2SSunitha Harish if (success && !ethData.vlan_id.empty()) 22581abe55efSEd Tanous { 2259f12894f8SJason M. Bills auto callback = 2260bf648f77SEd Tanous [asyncResp]( 2261bf648f77SEd Tanous const boost::system::error_code ec) { 22621abe55efSEd Tanous if (ec) 22631abe55efSEd Tanous { 2264f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2265927a505aSKowalski, Kamil } 22664a0cb85cSEd Tanous }; 22674a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2268bf648f77SEd Tanous std::move(callback), 2269bf648f77SEd Tanous "xyz.openbmc_project.Network", 2270bf648f77SEd Tanous std::string("/xyz/openbmc_project/network/") + 2271bf648f77SEd Tanous ifaceId, 22724a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 22731abe55efSEd Tanous } 22741abe55efSEd Tanous else 22751abe55efSEd Tanous { 2276927a505aSKowalski, Kamil // ... otherwise return error 2277bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2278bf648f77SEd Tanous // existing object, and other errors 2279bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2280bf648f77SEd Tanous "VLAN Network Interface", 2281bf648f77SEd Tanous ifaceId); 2282927a505aSKowalski, Kamil } 2283927a505aSKowalski, Kamil }); 2284bf648f77SEd Tanous }); 2285e439f0f8SKowalski, Kamil 2286bf648f77SEd Tanous BMCWEB_ROUTE(app, 2287bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2288ed398213SEd Tanous 2289ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 2290bf648f77SEd Tanous .methods( 2291bf648f77SEd Tanous boost::beast::http::verb:: 2292bf648f77SEd Tanous get)([](const crow::Request& /* req */, 2293bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2294bf648f77SEd Tanous const std::string& rootInterfaceName) { 22954a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 22961abe55efSEd Tanous // preparation 2297bf648f77SEd Tanous getEthernetIfaceList([asyncResp, rootInterfaceName]( 22981abe55efSEd Tanous const bool& success, 2299bf648f77SEd Tanous const boost::container::flat_set< 2300bf648f77SEd Tanous std::string>& ifaceList) { 23014a0cb85cSEd Tanous if (!success) 23021abe55efSEd Tanous { 2303f12894f8SJason M. Bills messages::internalError(asyncResp->res); 23044a0cb85cSEd Tanous return; 23051abe55efSEd Tanous } 23064c9afe43SEd Tanous 230781ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 23084c9afe43SEd Tanous { 23094c9afe43SEd Tanous messages::resourceNotFound(asyncResp->res, 23104c9afe43SEd Tanous "VLanNetworkInterfaceCollection", 23114c9afe43SEd Tanous rootInterfaceName); 23124c9afe43SEd Tanous return; 23134c9afe43SEd Tanous } 23144c9afe43SEd Tanous 23150f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 23160f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 23170f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 23180f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 23190f74e643SEd Tanous "VLAN Network Interface Collection"; 23204a0cb85cSEd Tanous 23212c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 23224a0cb85cSEd Tanous 232381ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 23241abe55efSEd Tanous { 23252c70f800SEd Tanous if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) 23264a0cb85cSEd Tanous { 2327f23b7296SEd Tanous std::string path = 2328f23b7296SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2329f23b7296SEd Tanous path += rootInterfaceName; 2330f23b7296SEd Tanous path += "/VLANs/"; 2331f23b7296SEd Tanous path += ifaceItem; 2332f23b7296SEd Tanous ifaceArray.push_back({{"@odata.id", std::move(path)}}); 2333e439f0f8SKowalski, Kamil } 2334e439f0f8SKowalski, Kamil } 2335e439f0f8SKowalski, Kamil 23364a0cb85cSEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = 23372c70f800SEd Tanous ifaceArray.size(); 23382c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 23394a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 23404a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 23414a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2342e439f0f8SKowalski, Kamil }); 2343bf648f77SEd Tanous }); 2344e439f0f8SKowalski, Kamil 2345bf648f77SEd Tanous BMCWEB_ROUTE(app, 2346bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2347ed398213SEd Tanous // This privilege is wrong, it should be ConfigureManager 2348ed398213SEd Tanous //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2349432a890cSEd Tanous .privileges({{"ConfigureComponents"}}) 2350bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 2351bf648f77SEd Tanous [](const crow::Request& req, 2352bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2353bf648f77SEd Tanous const std::string& rootInterfaceName) { 2354fda13ad2SSunitha Harish bool vlanEnable = false; 23550627a2c7SEd Tanous uint32_t vlanId = 0; 23568d1b46d7Szhanghch05 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId, 23578d1b46d7Szhanghch05 "VLANEnable", vlanEnable)) 23581abe55efSEd Tanous { 23594a0cb85cSEd Tanous return; 2360e439f0f8SKowalski, Kamil } 2361fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2362fda13ad2SSunitha Harish if (!vlanId) 2363fda13ad2SSunitha Harish { 2364fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2365fda13ad2SSunitha Harish } 2366fda13ad2SSunitha Harish if (!vlanEnable) 2367fda13ad2SSunitha Harish { 2368fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2369fda13ad2SSunitha Harish } 2370271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2371fda13ad2SSunitha Harish { 2372fda13ad2SSunitha Harish return; 2373fda13ad2SSunitha Harish } 2374fda13ad2SSunitha Harish 2375bf648f77SEd Tanous auto callback = 2376bf648f77SEd Tanous [asyncResp](const boost::system::error_code ec) { 23771abe55efSEd Tanous if (ec) 23781abe55efSEd Tanous { 2379bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2380bf648f77SEd Tanous // based on phosphor-network responses 2381f12894f8SJason M. Bills messages::internalError(asyncResp->res); 23824a0cb85cSEd Tanous return; 23831abe55efSEd Tanous } 2384f12894f8SJason M. Bills messages::created(asyncResp->res); 2385e439f0f8SKowalski, Kamil }; 23864a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 23874a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 23884a0cb85cSEd Tanous "/xyz/openbmc_project/network", 23894a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 23900627a2c7SEd Tanous rootInterfaceName, vlanId); 2391bf648f77SEd Tanous }); 23924a0cb85cSEd Tanous } 2393bf648f77SEd Tanous 23949391bb9cSRapkiewicz, Pawel } // namespace redfish 2395