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 183ccb3adbSEd Tanous #include "app.hpp" 193ccb3adbSEd Tanous #include "dbus_singleton.hpp" 207a1dbc48SGeorge Liu #include "dbus_utility.hpp" 213ccb3adbSEd Tanous #include "error_messages.hpp" 223ccb3adbSEd Tanous #include "health.hpp" 233ccb3adbSEd Tanous #include "query.hpp" 243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 25033f1e4dSEd Tanous #include "utils/ip_utils.hpp" 263ccb3adbSEd Tanous #include "utils/json_utils.hpp" 27033f1e4dSEd Tanous 2811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp> 2911ba3979SEd Tanous #include <boost/algorithm/string/split.hpp> 304a0cb85cSEd Tanous #include <boost/container/flat_set.hpp> 311214b7e7SGunnar Mills 327a1dbc48SGeorge Liu #include <array> 33a24526dcSEd Tanous #include <optional> 34ab6554f1SJoshi-Mansi #include <regex> 357a1dbc48SGeorge Liu #include <string_view> 369391bb9cSRapkiewicz, Pawel 371abe55efSEd Tanous namespace redfish 381abe55efSEd Tanous { 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; 8835fb5311STejas Patil size_t mtuSize; 8982695a5bSJiaqing Zhao bool autoNeg; 9082695a5bSJiaqing Zhao bool dnsEnabled; 9182695a5bSJiaqing Zhao bool ntpEnabled; 9282695a5bSJiaqing Zhao bool hostNameEnabled; 93aa05fb27SJohnathan Mantey bool linkUp; 94eeedda23SJohnathan Mantey bool nicEnabled; 9582695a5bSJiaqing Zhao std::string dhcpEnabled; 961f8c7b5dSJohnathan Mantey std::string operatingMode; 9782695a5bSJiaqing Zhao std::string hostName; 9882695a5bSJiaqing Zhao std::string defaultGateway; 9982695a5bSJiaqing Zhao std::string ipv6DefaultGateway; 10082695a5bSJiaqing Zhao std::string macAddress; 10117e22024SJiaqing Zhao std::optional<uint32_t> vlanId; 1020f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> nameServers; 1030f6efdc1Smanojkiran.eda@gmail.com std::vector<std::string> staticNameServers; 104d24bfc7aSJennifer Lee std::vector<std::string> domainnames; 1059391bb9cSRapkiewicz, Pawel }; 1069391bb9cSRapkiewicz, Pawel 1071f8c7b5dSJohnathan Mantey struct DHCPParameters 1081f8c7b5dSJohnathan Mantey { 1091f8c7b5dSJohnathan Mantey std::optional<bool> dhcpv4Enabled; 11082695a5bSJiaqing Zhao std::optional<bool> useDnsServers; 11182695a5bSJiaqing Zhao std::optional<bool> useNtpServers; 11282695a5bSJiaqing Zhao std::optional<bool> useDomainName; 1131f8c7b5dSJohnathan Mantey std::optional<std::string> dhcpv6OperatingMode; 1141f8c7b5dSJohnathan Mantey }; 1151f8c7b5dSJohnathan Mantey 1169391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24) 1179391bb9cSRapkiewicz, Pawel // into full dot notation 1181abe55efSEd Tanous inline std::string getNetmask(unsigned int bits) 1191abe55efSEd Tanous { 1209391bb9cSRapkiewicz, Pawel uint32_t value = 0xffffffff << (32 - bits); 1219391bb9cSRapkiewicz, Pawel std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 1229391bb9cSRapkiewicz, Pawel std::to_string((value >> 16) & 0xff) + "." + 1239391bb9cSRapkiewicz, Pawel std::to_string((value >> 8) & 0xff) + "." + 1249391bb9cSRapkiewicz, Pawel std::to_string(value & 0xff); 1259391bb9cSRapkiewicz, Pawel return netmask; 1269391bb9cSRapkiewicz, Pawel } 1279391bb9cSRapkiewicz, Pawel 12882695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP, 1291f8c7b5dSJohnathan Mantey bool isIPv4) 1301f8c7b5dSJohnathan Mantey { 1311f8c7b5dSJohnathan Mantey if (isIPv4) 1321f8c7b5dSJohnathan Mantey { 1331f8c7b5dSJohnathan Mantey return ( 1341f8c7b5dSJohnathan Mantey (inputDHCP == 1351f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") || 1361f8c7b5dSJohnathan Mantey (inputDHCP == 1371f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1381f8c7b5dSJohnathan Mantey } 1391f8c7b5dSJohnathan Mantey return ((inputDHCP == 1401f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") || 1411f8c7b5dSJohnathan Mantey (inputDHCP == 1421f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both")); 1431f8c7b5dSJohnathan Mantey } 1441f8c7b5dSJohnathan Mantey 1452c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6) 1461f8c7b5dSJohnathan Mantey { 1471f8c7b5dSJohnathan Mantey if (isIPv4 && isIPv6) 1481f8c7b5dSJohnathan Mantey { 1491f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"; 1501f8c7b5dSJohnathan Mantey } 1513174e4dfSEd Tanous if (isIPv4) 1521f8c7b5dSJohnathan Mantey { 1531f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4"; 1541f8c7b5dSJohnathan Mantey } 1553174e4dfSEd Tanous if (isIPv6) 1561f8c7b5dSJohnathan Mantey { 1571f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6"; 1581f8c7b5dSJohnathan Mantey } 1591f8c7b5dSJohnathan Mantey return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none"; 1601f8c7b5dSJohnathan Mantey } 1611f8c7b5dSJohnathan Mantey 1624a0cb85cSEd Tanous inline std::string 1634a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(const std::string& inputOrigin, 1644a0cb85cSEd Tanous bool isIPv4) 1651abe55efSEd Tanous { 1664a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 1671abe55efSEd Tanous { 1684a0cb85cSEd Tanous return "Static"; 1699391bb9cSRapkiewicz, Pawel } 1704a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 1711abe55efSEd Tanous { 1724a0cb85cSEd Tanous if (isIPv4) 1731abe55efSEd Tanous { 1744a0cb85cSEd Tanous return "IPv4LinkLocal"; 1751abe55efSEd Tanous } 1764a0cb85cSEd Tanous return "LinkLocal"; 1779391bb9cSRapkiewicz, Pawel } 1784a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 1791abe55efSEd Tanous { 1804a0cb85cSEd Tanous if (isIPv4) 1814a0cb85cSEd Tanous { 1824a0cb85cSEd Tanous return "DHCP"; 1834a0cb85cSEd Tanous } 1844a0cb85cSEd Tanous return "DHCPv6"; 1854a0cb85cSEd Tanous } 1864a0cb85cSEd Tanous if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 1874a0cb85cSEd Tanous { 1884a0cb85cSEd Tanous return "SLAAC"; 1894a0cb85cSEd Tanous } 1904a0cb85cSEd Tanous return ""; 1914a0cb85cSEd Tanous } 1924a0cb85cSEd Tanous 19302cad96eSEd Tanous inline bool extractEthernetInterfaceData( 19402cad96eSEd Tanous const std::string& ethifaceId, 19502cad96eSEd Tanous const dbus::utility::ManagedObjectType& dbusData, 1964a0cb85cSEd Tanous EthernetInterfaceData& ethData) 1974a0cb85cSEd Tanous { 1984c9afe43SEd Tanous bool idFound = false; 19902cad96eSEd Tanous for (const auto& objpath : dbusData) 2004a0cb85cSEd Tanous { 20102cad96eSEd Tanous for (const auto& ifacePair : objpath.second) 2024a0cb85cSEd Tanous { 20381ce609eSEd Tanous if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId) 204029573d4SEd Tanous { 2054c9afe43SEd Tanous idFound = true; 2064a0cb85cSEd Tanous if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 2074a0cb85cSEd Tanous { 2084a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2094a0cb85cSEd Tanous { 2104a0cb85cSEd Tanous if (propertyPair.first == "MACAddress") 2114a0cb85cSEd Tanous { 2124a0cb85cSEd Tanous const std::string* mac = 213abf2add6SEd Tanous std::get_if<std::string>(&propertyPair.second); 2144a0cb85cSEd Tanous if (mac != nullptr) 2154a0cb85cSEd Tanous { 21682695a5bSJiaqing Zhao ethData.macAddress = *mac; 2174a0cb85cSEd Tanous } 2184a0cb85cSEd Tanous } 2194a0cb85cSEd Tanous } 2204a0cb85cSEd Tanous } 2214a0cb85cSEd Tanous else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 2224a0cb85cSEd Tanous { 2234a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2244a0cb85cSEd Tanous { 2254a0cb85cSEd Tanous if (propertyPair.first == "Id") 2264a0cb85cSEd Tanous { 2271b6b96c5SEd Tanous const uint32_t* id = 228abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2294a0cb85cSEd Tanous if (id != nullptr) 2304a0cb85cSEd Tanous { 23117e22024SJiaqing Zhao ethData.vlanId = *id; 2324a0cb85cSEd Tanous } 2334a0cb85cSEd Tanous } 2344a0cb85cSEd Tanous } 2354a0cb85cSEd Tanous } 2364a0cb85cSEd Tanous else if (ifacePair.first == 2374a0cb85cSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 2384a0cb85cSEd Tanous { 2394a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 2404a0cb85cSEd Tanous { 2414a0cb85cSEd Tanous if (propertyPair.first == "AutoNeg") 2424a0cb85cSEd Tanous { 2432c70f800SEd Tanous const bool* autoNeg = 244abf2add6SEd Tanous std::get_if<bool>(&propertyPair.second); 2452c70f800SEd Tanous if (autoNeg != nullptr) 2464a0cb85cSEd Tanous { 24782695a5bSJiaqing Zhao ethData.autoNeg = *autoNeg; 2484a0cb85cSEd Tanous } 2494a0cb85cSEd Tanous } 2504a0cb85cSEd Tanous else if (propertyPair.first == "Speed") 2514a0cb85cSEd Tanous { 2524a0cb85cSEd Tanous const uint32_t* speed = 253abf2add6SEd Tanous std::get_if<uint32_t>(&propertyPair.second); 2544a0cb85cSEd Tanous if (speed != nullptr) 2554a0cb85cSEd Tanous { 2564a0cb85cSEd Tanous ethData.speed = *speed; 2574a0cb85cSEd Tanous } 2584a0cb85cSEd Tanous } 25935fb5311STejas Patil else if (propertyPair.first == "MTU") 26035fb5311STejas Patil { 26135fb5311STejas Patil const uint32_t* mtuSize = 26235fb5311STejas Patil std::get_if<uint32_t>(&propertyPair.second); 26335fb5311STejas Patil if (mtuSize != nullptr) 26435fb5311STejas Patil { 26535fb5311STejas Patil ethData.mtuSize = *mtuSize; 26635fb5311STejas Patil } 26735fb5311STejas Patil } 268aa05fb27SJohnathan Mantey else if (propertyPair.first == "LinkUp") 269aa05fb27SJohnathan Mantey { 270aa05fb27SJohnathan Mantey const bool* linkUp = 271aa05fb27SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 272aa05fb27SJohnathan Mantey if (linkUp != nullptr) 273aa05fb27SJohnathan Mantey { 274aa05fb27SJohnathan Mantey ethData.linkUp = *linkUp; 275aa05fb27SJohnathan Mantey } 276aa05fb27SJohnathan Mantey } 277eeedda23SJohnathan Mantey else if (propertyPair.first == "NICEnabled") 278eeedda23SJohnathan Mantey { 279eeedda23SJohnathan Mantey const bool* nicEnabled = 280eeedda23SJohnathan Mantey std::get_if<bool>(&propertyPair.second); 281eeedda23SJohnathan Mantey if (nicEnabled != nullptr) 282eeedda23SJohnathan Mantey { 283eeedda23SJohnathan Mantey ethData.nicEnabled = *nicEnabled; 284eeedda23SJohnathan Mantey } 285eeedda23SJohnathan Mantey } 286f85837bfSRAJESWARAN THILLAIGOVINDAN else if (propertyPair.first == "Nameservers") 287029573d4SEd Tanous { 288029573d4SEd Tanous const std::vector<std::string>* nameservers = 2898d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 290029573d4SEd Tanous &propertyPair.second); 291029573d4SEd Tanous if (nameservers != nullptr) 292029573d4SEd Tanous { 293f23b7296SEd Tanous ethData.nameServers = *nameservers; 2940f6efdc1Smanojkiran.eda@gmail.com } 2950f6efdc1Smanojkiran.eda@gmail.com } 2960f6efdc1Smanojkiran.eda@gmail.com else if (propertyPair.first == "StaticNameServers") 2970f6efdc1Smanojkiran.eda@gmail.com { 2980f6efdc1Smanojkiran.eda@gmail.com const std::vector<std::string>* staticNameServers = 2998d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 3000f6efdc1Smanojkiran.eda@gmail.com &propertyPair.second); 3010f6efdc1Smanojkiran.eda@gmail.com if (staticNameServers != nullptr) 3020f6efdc1Smanojkiran.eda@gmail.com { 303f23b7296SEd Tanous ethData.staticNameServers = *staticNameServers; 3044a0cb85cSEd Tanous } 3054a0cb85cSEd Tanous } 3062a133282Smanojkiraneda else if (propertyPair.first == "DHCPEnabled") 3072a133282Smanojkiraneda { 3082c70f800SEd Tanous const std::string* dhcpEnabled = 3091f8c7b5dSJohnathan Mantey std::get_if<std::string>(&propertyPair.second); 3102c70f800SEd Tanous if (dhcpEnabled != nullptr) 3112a133282Smanojkiraneda { 31282695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcpEnabled; 3132a133282Smanojkiraneda } 3142a133282Smanojkiraneda } 315d24bfc7aSJennifer Lee else if (propertyPair.first == "DomainName") 316d24bfc7aSJennifer Lee { 317d24bfc7aSJennifer Lee const std::vector<std::string>* domainNames = 3188d78b7a9SPatrick Williams std::get_if<std::vector<std::string>>( 319d24bfc7aSJennifer Lee &propertyPair.second); 320d24bfc7aSJennifer Lee if (domainNames != nullptr) 321d24bfc7aSJennifer Lee { 322f23b7296SEd Tanous ethData.domainnames = *domainNames; 323d24bfc7aSJennifer Lee } 324d24bfc7aSJennifer Lee } 3259010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway") 3269010ec2eSRavi Teja { 3279010ec2eSRavi Teja const std::string* defaultGateway = 3289010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3299010ec2eSRavi Teja if (defaultGateway != nullptr) 3309010ec2eSRavi Teja { 3319010ec2eSRavi Teja std::string defaultGatewayStr = *defaultGateway; 3329010ec2eSRavi Teja if (defaultGatewayStr.empty()) 3339010ec2eSRavi Teja { 33482695a5bSJiaqing Zhao ethData.defaultGateway = "0.0.0.0"; 3359010ec2eSRavi Teja } 3369010ec2eSRavi Teja else 3379010ec2eSRavi Teja { 33882695a5bSJiaqing Zhao ethData.defaultGateway = defaultGatewayStr; 3399010ec2eSRavi Teja } 3409010ec2eSRavi Teja } 3419010ec2eSRavi Teja } 3429010ec2eSRavi Teja else if (propertyPair.first == "DefaultGateway6") 3439010ec2eSRavi Teja { 3449010ec2eSRavi Teja const std::string* defaultGateway6 = 3459010ec2eSRavi Teja std::get_if<std::string>(&propertyPair.second); 3469010ec2eSRavi Teja if (defaultGateway6 != nullptr) 3479010ec2eSRavi Teja { 3489010ec2eSRavi Teja std::string defaultGateway6Str = 3499010ec2eSRavi Teja *defaultGateway6; 3509010ec2eSRavi Teja if (defaultGateway6Str.empty()) 3519010ec2eSRavi Teja { 35282695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3539010ec2eSRavi Teja "0:0:0:0:0:0:0:0"; 3549010ec2eSRavi Teja } 3559010ec2eSRavi Teja else 3569010ec2eSRavi Teja { 35782695a5bSJiaqing Zhao ethData.ipv6DefaultGateway = 3589010ec2eSRavi Teja defaultGateway6Str; 3599010ec2eSRavi Teja } 3609010ec2eSRavi Teja } 3619010ec2eSRavi Teja } 362029573d4SEd Tanous } 363029573d4SEd Tanous } 364029573d4SEd Tanous } 3651f8c7b5dSJohnathan Mantey 3661e3f85e6SJian Zhang if (objpath.first == "/xyz/openbmc_project/network/dhcp") 3671f8c7b5dSJohnathan Mantey { 3681f8c7b5dSJohnathan Mantey if (ifacePair.first == 3691f8c7b5dSJohnathan Mantey "xyz.openbmc_project.Network.DHCPConfiguration") 3701f8c7b5dSJohnathan Mantey { 3711f8c7b5dSJohnathan Mantey for (const auto& propertyPair : ifacePair.second) 3721f8c7b5dSJohnathan Mantey { 3731f8c7b5dSJohnathan Mantey if (propertyPair.first == "DNSEnabled") 3741f8c7b5dSJohnathan Mantey { 3752c70f800SEd Tanous const bool* dnsEnabled = 3761f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3772c70f800SEd Tanous if (dnsEnabled != nullptr) 3781f8c7b5dSJohnathan Mantey { 37982695a5bSJiaqing Zhao ethData.dnsEnabled = *dnsEnabled; 3801f8c7b5dSJohnathan Mantey } 3811f8c7b5dSJohnathan Mantey } 3821f8c7b5dSJohnathan Mantey else if (propertyPair.first == "NTPEnabled") 3831f8c7b5dSJohnathan Mantey { 3842c70f800SEd Tanous const bool* ntpEnabled = 3851f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3862c70f800SEd Tanous if (ntpEnabled != nullptr) 3871f8c7b5dSJohnathan Mantey { 38882695a5bSJiaqing Zhao ethData.ntpEnabled = *ntpEnabled; 3891f8c7b5dSJohnathan Mantey } 3901f8c7b5dSJohnathan Mantey } 3911f8c7b5dSJohnathan Mantey else if (propertyPair.first == "HostNameEnabled") 3921f8c7b5dSJohnathan Mantey { 3932c70f800SEd Tanous const bool* hostNameEnabled = 3941f8c7b5dSJohnathan Mantey std::get_if<bool>(&propertyPair.second); 3952c70f800SEd Tanous if (hostNameEnabled != nullptr) 3961f8c7b5dSJohnathan Mantey { 39782695a5bSJiaqing Zhao ethData.hostNameEnabled = *hostNameEnabled; 3981f8c7b5dSJohnathan Mantey } 3991f8c7b5dSJohnathan Mantey } 4001f8c7b5dSJohnathan Mantey } 4011f8c7b5dSJohnathan Mantey } 4021f8c7b5dSJohnathan Mantey } 403029573d4SEd Tanous // System configuration shows up in the global namespace, so no need 404029573d4SEd Tanous // to check eth number 405029573d4SEd Tanous if (ifacePair.first == 4064a0cb85cSEd Tanous "xyz.openbmc_project.Network.SystemConfiguration") 4074a0cb85cSEd Tanous { 4084a0cb85cSEd Tanous for (const auto& propertyPair : ifacePair.second) 4094a0cb85cSEd Tanous { 4104a0cb85cSEd Tanous if (propertyPair.first == "HostName") 4114a0cb85cSEd Tanous { 4124a0cb85cSEd Tanous const std::string* hostname = 4138d78b7a9SPatrick Williams std::get_if<std::string>(&propertyPair.second); 4144a0cb85cSEd Tanous if (hostname != nullptr) 4154a0cb85cSEd Tanous { 41682695a5bSJiaqing Zhao ethData.hostName = *hostname; 4174a0cb85cSEd Tanous } 4184a0cb85cSEd Tanous } 4194a0cb85cSEd Tanous } 4204a0cb85cSEd Tanous } 4214a0cb85cSEd Tanous } 4224a0cb85cSEd Tanous } 4234c9afe43SEd Tanous return idFound; 4244a0cb85cSEd Tanous } 4254a0cb85cSEd Tanous 426e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address 42701784826SJohnathan Mantey inline void 42881ce609eSEd Tanous extractIPV6Data(const std::string& ethifaceId, 429711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 43081ce609eSEd Tanous boost::container::flat_set<IPv6AddressData>& ipv6Config) 431e48c0fc5SRavi Teja { 432353163e9STony Lee const std::string ipPathStart = 433353163e9STony Lee "/xyz/openbmc_project/network/" + ethifaceId; 434e48c0fc5SRavi Teja 435e48c0fc5SRavi Teja // Since there might be several IPv6 configurations aligned with 436e48c0fc5SRavi Teja // single ethernet interface, loop over all of them 43781ce609eSEd Tanous for (const auto& objpath : dbusData) 438e48c0fc5SRavi Teja { 439e48c0fc5SRavi Teja // Check if proper pattern for object path appears 440353163e9STony Lee if (objpath.first.str.starts_with(ipPathStart + "/")) 441e48c0fc5SRavi Teja { 4429eb808c1SEd Tanous for (const auto& interface : objpath.second) 443e48c0fc5SRavi Teja { 444e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 445e48c0fc5SRavi Teja { 446353163e9STony Lee auto type = std::find_if(interface.second.begin(), 447353163e9STony Lee interface.second.end(), 448353163e9STony Lee [](const auto& property) { 449353163e9STony Lee return property.first == "Type"; 450353163e9STony Lee }); 451353163e9STony Lee if (type == interface.second.end()) 452353163e9STony Lee { 453353163e9STony Lee continue; 454353163e9STony Lee } 455353163e9STony Lee 456353163e9STony Lee const std::string* typeStr = 457353163e9STony Lee std::get_if<std::string>(&type->second); 458353163e9STony Lee 459353163e9STony Lee if (typeStr == nullptr || 460353163e9STony Lee (*typeStr != 461353163e9STony Lee "xyz.openbmc_project.Network.IP.Protocol.IPv6")) 462353163e9STony Lee { 463353163e9STony Lee continue; 464353163e9STony Lee } 465353163e9STony Lee 466e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 467e48c0fc5SRavi Teja // appropriate 468e48c0fc5SRavi Teja std::pair< 469e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 470e48c0fc5SRavi Teja bool> 47181ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4722c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4732c70f800SEd Tanous ipv6Address.id = 474353163e9STony Lee objpath.first.str.substr(ipPathStart.size()); 4759eb808c1SEd Tanous for (const auto& property : interface.second) 476e48c0fc5SRavi Teja { 477e48c0fc5SRavi Teja if (property.first == "Address") 478e48c0fc5SRavi Teja { 479e48c0fc5SRavi Teja const std::string* address = 480e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 481e48c0fc5SRavi Teja if (address != nullptr) 482e48c0fc5SRavi Teja { 4832c70f800SEd Tanous ipv6Address.address = *address; 484e48c0fc5SRavi Teja } 485e48c0fc5SRavi Teja } 486e48c0fc5SRavi Teja else if (property.first == "Origin") 487e48c0fc5SRavi Teja { 488e48c0fc5SRavi Teja const std::string* origin = 489e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 490e48c0fc5SRavi Teja if (origin != nullptr) 491e48c0fc5SRavi Teja { 4922c70f800SEd Tanous ipv6Address.origin = 493e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 494e48c0fc5SRavi Teja false); 495e48c0fc5SRavi Teja } 496e48c0fc5SRavi Teja } 497e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 498e48c0fc5SRavi Teja { 499e48c0fc5SRavi Teja const uint8_t* prefix = 500e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 501e48c0fc5SRavi Teja if (prefix != nullptr) 502e48c0fc5SRavi Teja { 5032c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 504e48c0fc5SRavi Teja } 505e48c0fc5SRavi Teja } 506889ff694SAsmitha Karunanithi else if (property.first == "Type" || 507889ff694SAsmitha Karunanithi property.first == "Gateway") 508889ff694SAsmitha Karunanithi { 509889ff694SAsmitha Karunanithi // Type & Gateway is not used 510889ff694SAsmitha Karunanithi } 511e48c0fc5SRavi Teja else 512e48c0fc5SRavi Teja { 513e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 514e48c0fc5SRavi Teja << "Got extra property: " << property.first 515e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 516e48c0fc5SRavi Teja } 517e48c0fc5SRavi Teja } 518e48c0fc5SRavi Teja } 519e48c0fc5SRavi Teja } 520e48c0fc5SRavi Teja } 521e48c0fc5SRavi Teja } 522e48c0fc5SRavi Teja } 523e48c0fc5SRavi Teja 5244a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 52501784826SJohnathan Mantey inline void 52681ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 527711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 52881ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5294a0cb85cSEd Tanous { 530353163e9STony Lee const std::string ipPathStart = 531353163e9STony Lee "/xyz/openbmc_project/network/" + ethifaceId; 5324a0cb85cSEd Tanous 5334a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5344a0cb85cSEd Tanous // single ethernet interface, loop over all of them 53581ce609eSEd Tanous for (const auto& objpath : dbusData) 5364a0cb85cSEd Tanous { 5374a0cb85cSEd Tanous // Check if proper pattern for object path appears 538353163e9STony Lee if (objpath.first.str.starts_with(ipPathStart + "/")) 5394a0cb85cSEd Tanous { 5409eb808c1SEd Tanous for (const auto& interface : objpath.second) 5414a0cb85cSEd Tanous { 5424a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5434a0cb85cSEd Tanous { 544353163e9STony Lee auto type = std::find_if(interface.second.begin(), 545353163e9STony Lee interface.second.end(), 546353163e9STony Lee [](const auto& property) { 547353163e9STony Lee return property.first == "Type"; 548353163e9STony Lee }); 549353163e9STony Lee if (type == interface.second.end()) 550353163e9STony Lee { 551353163e9STony Lee continue; 552353163e9STony Lee } 553353163e9STony Lee 554353163e9STony Lee const std::string* typeStr = 555353163e9STony Lee std::get_if<std::string>(&type->second); 556353163e9STony Lee 557353163e9STony Lee if (typeStr == nullptr || 558353163e9STony Lee (*typeStr != 559353163e9STony Lee "xyz.openbmc_project.Network.IP.Protocol.IPv4")) 560353163e9STony Lee { 561353163e9STony Lee continue; 562353163e9STony Lee } 563353163e9STony Lee 5644a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5654a0cb85cSEd Tanous // appropriate 5664a0cb85cSEd Tanous std::pair< 5674a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5684a0cb85cSEd Tanous bool> 56981ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5702c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5712c70f800SEd Tanous ipv4Address.id = 572353163e9STony Lee objpath.first.str.substr(ipPathStart.size()); 5739eb808c1SEd Tanous for (const auto& property : interface.second) 5744a0cb85cSEd Tanous { 5754a0cb85cSEd Tanous if (property.first == "Address") 5764a0cb85cSEd Tanous { 5774a0cb85cSEd Tanous const std::string* address = 578abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5794a0cb85cSEd Tanous if (address != nullptr) 5804a0cb85cSEd Tanous { 5812c70f800SEd Tanous ipv4Address.address = *address; 5824a0cb85cSEd Tanous } 5834a0cb85cSEd Tanous } 5844a0cb85cSEd Tanous else if (property.first == "Origin") 5854a0cb85cSEd Tanous { 5864a0cb85cSEd Tanous const std::string* origin = 587abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5884a0cb85cSEd Tanous if (origin != nullptr) 5894a0cb85cSEd Tanous { 5902c70f800SEd Tanous ipv4Address.origin = 5914a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5924a0cb85cSEd Tanous true); 5934a0cb85cSEd Tanous } 5944a0cb85cSEd Tanous } 5954a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5964a0cb85cSEd Tanous { 5974a0cb85cSEd Tanous const uint8_t* mask = 598abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5994a0cb85cSEd Tanous if (mask != nullptr) 6004a0cb85cSEd Tanous { 6014a0cb85cSEd Tanous // convert it to the string 6022c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 6034a0cb85cSEd Tanous } 6044a0cb85cSEd Tanous } 605889ff694SAsmitha Karunanithi else if (property.first == "Type" || 606889ff694SAsmitha Karunanithi property.first == "Gateway") 607889ff694SAsmitha Karunanithi { 608889ff694SAsmitha Karunanithi // Type & Gateway is not used 609889ff694SAsmitha Karunanithi } 6104a0cb85cSEd Tanous else 6114a0cb85cSEd Tanous { 6124a0cb85cSEd Tanous BMCWEB_LOG_ERROR 6134a0cb85cSEd Tanous << "Got extra property: " << property.first 6144a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 6154a0cb85cSEd Tanous } 6164a0cb85cSEd Tanous } 6174a0cb85cSEd Tanous // Check if given address is local, or global 6182c70f800SEd Tanous ipv4Address.linktype = 61911ba3979SEd Tanous ipv4Address.address.starts_with("169.254.") 62018659d10SJohnathan Mantey ? LinkType::Local 62118659d10SJohnathan Mantey : LinkType::Global; 6224a0cb85cSEd Tanous } 6234a0cb85cSEd Tanous } 6244a0cb85cSEd Tanous } 6254a0cb85cSEd Tanous } 6264a0cb85cSEd Tanous } 627588c3f0dSKowalski, Kamil 628588c3f0dSKowalski, Kamil /** 62901784826SJohnathan Mantey * @brief Deletes given IPv4 interface 630179db1d7SKowalski, Kamil * 631179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 632179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 633179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 634179db1d7SKowalski, Kamil * 635179db1d7SKowalski, Kamil * @return None 636179db1d7SKowalski, Kamil */ 6374a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 6388d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6391abe55efSEd Tanous { 64055c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 641286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 6421abe55efSEd Tanous if (ec) 6431abe55efSEd Tanous { 644a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6451abe55efSEd Tanous } 646179db1d7SKowalski, Kamil }, 647179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 648179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 649179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 650179db1d7SKowalski, Kamil } 651179db1d7SKowalski, Kamil 652244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 653244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 654244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6559010ec2eSRavi Teja { 6569010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6579010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 6589010ec2eSRavi Teja if (ec) 6599010ec2eSRavi Teja { 6609010ec2eSRavi Teja messages::internalError(asyncResp->res); 6619010ec2eSRavi Teja return; 6629010ec2eSRavi Teja } 6639010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 6649010ec2eSRavi Teja }, 6659010ec2eSRavi Teja "xyz.openbmc_project.Network", 6669010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 6679010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 6689010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 669168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 6709010ec2eSRavi Teja } 671179db1d7SKowalski, Kamil /** 67201784826SJohnathan Mantey * @brief Creates a static IPv4 entry 673179db1d7SKowalski, Kamil * 67401784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 67501784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 67601784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 67701784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 678179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 679179db1d7SKowalski, Kamil * 680179db1d7SKowalski, Kamil * @return None 681179db1d7SKowalski, Kamil */ 682cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 683cb13a392SEd Tanous const std::string& gateway, const std::string& address, 6848d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6851abe55efSEd Tanous { 686002d39b4SEd Tanous auto createIpHandler = 687002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec) { 6881abe55efSEd Tanous if (ec) 6891abe55efSEd Tanous { 690a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6919010ec2eSRavi Teja return; 692179db1d7SKowalski, Kamil } 6939010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 6949010ec2eSRavi Teja }; 6959010ec2eSRavi Teja 6969010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6979010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 698179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 699179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 70001784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 701179db1d7SKowalski, Kamil gateway); 702179db1d7SKowalski, Kamil } 703e48c0fc5SRavi Teja 704e48c0fc5SRavi Teja /** 70501784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 70601784826SJohnathan Mantey * static IPv4 entry 70701784826SJohnathan Mantey * 70801784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 70901784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 71001784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 71101784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 71201784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 71301784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 71401784826SJohnathan Mantey * 71501784826SJohnathan Mantey * @return None 71601784826SJohnathan Mantey */ 7178d1b46d7Szhanghch05 inline void 7188d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 7198d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 72001784826SJohnathan Mantey const std::string& address, 7218d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 72201784826SJohnathan Mantey { 72301784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 72401784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 72501784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 72601784826SJohnathan Mantey if (ec) 72701784826SJohnathan Mantey { 72801784826SJohnathan Mantey messages::internalError(asyncResp->res); 7299010ec2eSRavi Teja return; 73001784826SJohnathan Mantey } 7319010ec2eSRavi Teja 73201784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 733002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) { 73423a21a1cSEd Tanous if (ec2) 73501784826SJohnathan Mantey { 73601784826SJohnathan Mantey messages::internalError(asyncResp->res); 7379010ec2eSRavi Teja return; 73801784826SJohnathan Mantey } 7399010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 74001784826SJohnathan Mantey }, 74101784826SJohnathan Mantey "xyz.openbmc_project.Network", 74201784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 74301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 74401784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 74501784826SJohnathan Mantey prefixLength, gateway); 74601784826SJohnathan Mantey }, 74701784826SJohnathan Mantey "xyz.openbmc_project.Network", 74801784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 74901784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 75001784826SJohnathan Mantey } 75101784826SJohnathan Mantey 75201784826SJohnathan Mantey /** 753e48c0fc5SRavi Teja * @brief Deletes given IPv6 754e48c0fc5SRavi Teja * 755e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 756e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 757e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 758e48c0fc5SRavi Teja * 759e48c0fc5SRavi Teja * @return None 760e48c0fc5SRavi Teja */ 761e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 7628d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 763e48c0fc5SRavi Teja { 764e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 765286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 766e48c0fc5SRavi Teja if (ec) 767e48c0fc5SRavi Teja { 768e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 769e48c0fc5SRavi Teja } 770e48c0fc5SRavi Teja }, 771e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 772e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 773e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 774e48c0fc5SRavi Teja } 775e48c0fc5SRavi Teja 776e48c0fc5SRavi Teja /** 77701784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 77801784826SJohnathan Mantey * static IPv6 entry 77901784826SJohnathan Mantey * 78001784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 78101784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 78201784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 78301784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 78401784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 78501784826SJohnathan Mantey * 78601784826SJohnathan Mantey * @return None 78701784826SJohnathan Mantey */ 7888d1b46d7Szhanghch05 inline void 7898d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 7908d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 7918d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 79201784826SJohnathan Mantey { 79301784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 79401784826SJohnathan Mantey [asyncResp, ifaceId, address, 79501784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 79601784826SJohnathan Mantey if (ec) 79701784826SJohnathan Mantey { 79801784826SJohnathan Mantey messages::internalError(asyncResp->res); 79901784826SJohnathan Mantey } 80001784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 80123a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 80223a21a1cSEd Tanous if (ec2) 80301784826SJohnathan Mantey { 80401784826SJohnathan Mantey messages::internalError(asyncResp->res); 80501784826SJohnathan Mantey } 80601784826SJohnathan Mantey }, 80701784826SJohnathan Mantey "xyz.openbmc_project.Network", 80801784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 80901784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 81001784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 81101784826SJohnathan Mantey prefixLength, ""); 81201784826SJohnathan Mantey }, 81301784826SJohnathan Mantey "xyz.openbmc_project.Network", 81401784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 81501784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 81601784826SJohnathan Mantey } 81701784826SJohnathan Mantey 81801784826SJohnathan Mantey /** 819e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 820e48c0fc5SRavi Teja * 821e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 822e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 823e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 824e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 825e48c0fc5SRavi Teja * 826e48c0fc5SRavi Teja * @return None 827e48c0fc5SRavi Teja */ 82801784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 82901784826SJohnathan Mantey const std::string& address, 8308d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 831e48c0fc5SRavi Teja { 832e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 833e48c0fc5SRavi Teja if (ec) 834e48c0fc5SRavi Teja { 835e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 836e48c0fc5SRavi Teja } 837e48c0fc5SRavi Teja }; 838e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 8394e0453b1SGunnar Mills // does not have associated gateway property 840e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 841e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 842e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 843e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 844e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 845e48c0fc5SRavi Teja ""); 846e48c0fc5SRavi Teja } 847e48c0fc5SRavi Teja 848179db1d7SKowalski, Kamil /** 849179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 850179db1d7SKowalski, Kamil * Object 851179db1d7SKowalski, Kamil * from EntityManager Network Manager 8524a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 853179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 854179db1d7SKowalski, Kamil * into JSON 855179db1d7SKowalski, Kamil */ 856179db1d7SKowalski, Kamil template <typename CallbackFunc> 85781ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 8581abe55efSEd Tanous CallbackFunc&& callback) 8591abe55efSEd Tanous { 86055c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 861f94c4ecfSEd Tanous [ethifaceId{std::string{ethifaceId}}, 862f94c4ecfSEd Tanous callback{std::forward<CallbackFunc>(callback)}]( 86381ce609eSEd Tanous const boost::system::error_code errorCode, 86402cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 86555c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 8664a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 867e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 868179db1d7SKowalski, Kamil 86981ce609eSEd Tanous if (errorCode) 8701abe55efSEd Tanous { 87101784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 872179db1d7SKowalski, Kamil return; 873179db1d7SKowalski, Kamil } 874179db1d7SKowalski, Kamil 875002d39b4SEd Tanous bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData); 8764c9afe43SEd Tanous if (!found) 8774c9afe43SEd Tanous { 87801784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 8794c9afe43SEd Tanous return; 8804c9afe43SEd Tanous } 8814c9afe43SEd Tanous 8822c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 883179db1d7SKowalski, Kamil // Fix global GW 8841abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 8851abe55efSEd Tanous { 886c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 887c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 8889010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 8891abe55efSEd Tanous { 89082695a5bSJiaqing Zhao ipv4.gateway = ethData.defaultGateway; 891179db1d7SKowalski, Kamil } 892179db1d7SKowalski, Kamil } 893179db1d7SKowalski, Kamil 8942c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 8954e0453b1SGunnar Mills // Finally make a callback with useful data 89601784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 897179db1d7SKowalski, Kamil }, 898179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 899179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 900271584abSEd Tanous } 901179db1d7SKowalski, Kamil 902179db1d7SKowalski, Kamil /** 9039391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 9049391bb9cSRapkiewicz, Pawel * Manager 9051abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 9061abe55efSEd Tanous * into JSON. 9079391bb9cSRapkiewicz, Pawel */ 9089391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 9091abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 9101abe55efSEd Tanous { 91155c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 912f94c4ecfSEd Tanous [callback{std::forward<CallbackFunc>(callback)}]( 91381ce609eSEd Tanous const boost::system::error_code errorCode, 914711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 9151abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 9161abe55efSEd Tanous // ethernet interfaces 9172c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 9182c70f800SEd Tanous ifaceList.reserve(resp.size()); 91981ce609eSEd Tanous if (errorCode) 9201abe55efSEd Tanous { 9212c70f800SEd Tanous callback(false, ifaceList); 9229391bb9cSRapkiewicz, Pawel return; 9239391bb9cSRapkiewicz, Pawel } 9249391bb9cSRapkiewicz, Pawel 9259391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 9264a0cb85cSEd Tanous for (const auto& objpath : resp) 9271abe55efSEd Tanous { 9289391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 9294a0cb85cSEd Tanous for (const auto& interface : objpath.second) 9301abe55efSEd Tanous { 9311abe55efSEd Tanous // If interface is 9324a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 9334a0cb85cSEd Tanous // what we're looking for. 9349391bb9cSRapkiewicz, Pawel if (interface.first == 9351abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 9361abe55efSEd Tanous { 9372dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 9382dfd18efSEd Tanous if (ifaceId.empty()) 9391abe55efSEd Tanous { 9402dfd18efSEd Tanous continue; 9419391bb9cSRapkiewicz, Pawel } 9422dfd18efSEd Tanous // and put it into output vector. 9432dfd18efSEd Tanous ifaceList.emplace(ifaceId); 9449391bb9cSRapkiewicz, Pawel } 9459391bb9cSRapkiewicz, Pawel } 9469391bb9cSRapkiewicz, Pawel } 947a434f2bdSEd Tanous // Finally make a callback with useful data 9482c70f800SEd Tanous callback(true, ifaceList); 9499391bb9cSRapkiewicz, Pawel }, 950aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 951aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 952271584abSEd Tanous } 9539391bb9cSRapkiewicz, Pawel 9544f48d5f6SEd Tanous inline void 9554f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 9568d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 9571abe55efSEd Tanous { 958ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 959ab6554f1SJoshi-Mansi if (hostname.length() > 255) 960ab6554f1SJoshi-Mansi { 961ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 962ab6554f1SJoshi-Mansi "HostName"); 963ab6554f1SJoshi-Mansi return; 964ab6554f1SJoshi-Mansi } 965bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 966bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 9674a0cb85cSEd Tanous if (ec) 9684a0cb85cSEd Tanous { 969a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 9701abe55efSEd Tanous } 971bc0bd6e0SEd Tanous }, 972bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 973bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 974bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 975168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 976588c3f0dSKowalski, Kamil } 977588c3f0dSKowalski, Kamil 9784f48d5f6SEd Tanous inline void 97935fb5311STejas Patil handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, 98035fb5311STejas Patil const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 98135fb5311STejas Patil { 98235fb5311STejas Patil sdbusplus::message::object_path objPath = 98335fb5311STejas Patil "/xyz/openbmc_project/network/" + ifaceId; 98435fb5311STejas Patil crow::connections::systemBus->async_method_call( 98535fb5311STejas Patil [asyncResp](const boost::system::error_code ec) { 98635fb5311STejas Patil if (ec) 98735fb5311STejas Patil { 98835fb5311STejas Patil messages::internalError(asyncResp->res); 98935fb5311STejas Patil } 99035fb5311STejas Patil }, 99135fb5311STejas Patil "xyz.openbmc_project.Network", objPath, 99235fb5311STejas Patil "org.freedesktop.DBus.Properties", "Set", 99335fb5311STejas Patil "xyz.openbmc_project.Network.EthernetInterface", "MTU", 99435fb5311STejas Patil std::variant<size_t>(mtuSize)); 99535fb5311STejas Patil } 99635fb5311STejas Patil 99735fb5311STejas Patil inline void 9984f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 999bf648f77SEd Tanous const std::string& domainname, 10008d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1001ab6554f1SJoshi-Mansi { 1002ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 1003ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 1004ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 1005ab6554f1SJoshi-Mansi if (ec) 1006ab6554f1SJoshi-Mansi { 1007ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 1008ab6554f1SJoshi-Mansi } 1009ab6554f1SJoshi-Mansi }, 1010ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 1011ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 1012ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 1013ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 1014168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 1015ab6554f1SJoshi-Mansi } 1016ab6554f1SJoshi-Mansi 10174f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 1018bf648f77SEd Tanous { 1019bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 1020bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 1021bf648f77SEd Tanous { 1022bf648f77SEd Tanous return false; 1023bf648f77SEd Tanous } 1024bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 1025bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 1026bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 1027bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 1028bf648f77SEd Tanous const std::regex pattern( 1029bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 1030bf648f77SEd Tanous 1031bf648f77SEd Tanous return std::regex_match(hostname, pattern); 1032bf648f77SEd Tanous } 1033bf648f77SEd Tanous 10344f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 1035bf648f77SEd Tanous { 1036bf648f77SEd Tanous // Can have multiple subdomains 1037bf648f77SEd Tanous // Top Level Domain's min length is 2 character 10380fda0f12SGeorge Liu const std::regex pattern( 10390fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 1040bf648f77SEd Tanous 1041bf648f77SEd Tanous return std::regex_match(domainname, pattern); 1042bf648f77SEd Tanous } 1043bf648f77SEd Tanous 10444f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 10458d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1046ab6554f1SJoshi-Mansi { 1047ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1048ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1049ab6554f1SJoshi-Mansi { 1050ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1051ab6554f1SJoshi-Mansi return; 1052ab6554f1SJoshi-Mansi } 1053ab6554f1SJoshi-Mansi 1054ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1055ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1056ab6554f1SJoshi-Mansi { 1057ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1058ab6554f1SJoshi-Mansi return; 1059ab6554f1SJoshi-Mansi } 1060ab6554f1SJoshi-Mansi 1061ab6554f1SJoshi-Mansi std::string hostname; 1062ab6554f1SJoshi-Mansi std::string domainname; 1063ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1064ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1065ab6554f1SJoshi-Mansi 1066ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1067ab6554f1SJoshi-Mansi { 1068ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1069ab6554f1SJoshi-Mansi return; 1070ab6554f1SJoshi-Mansi } 1071ab6554f1SJoshi-Mansi 1072ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1073ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1074ab6554f1SJoshi-Mansi } 1075ab6554f1SJoshi-Mansi 10764f48d5f6SEd Tanous inline void 10774f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1078bf648f77SEd Tanous const std::string& macAddress, 10798d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1080d577665bSRatan Gupta { 108158283f41SJohnathan Mantey static constexpr std::string_view dbusNotAllowedError = 108258283f41SJohnathan Mantey "xyz.openbmc_project.Common.Error.NotAllowed"; 108358283f41SJohnathan Mantey 1084d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 108558283f41SJohnathan Mantey [asyncResp, macAddress](const boost::system::error_code ec, 10865b378546SPatrick Williams const sdbusplus::message_t& msg) { 1087d577665bSRatan Gupta if (ec) 1088d577665bSRatan Gupta { 108958283f41SJohnathan Mantey const sd_bus_error* err = msg.get_error(); 109058283f41SJohnathan Mantey if (err == nullptr) 109158283f41SJohnathan Mantey { 109258283f41SJohnathan Mantey messages::internalError(asyncResp->res); 109358283f41SJohnathan Mantey return; 109458283f41SJohnathan Mantey } 109558283f41SJohnathan Mantey if (err->name == dbusNotAllowedError) 109658283f41SJohnathan Mantey { 109758283f41SJohnathan Mantey messages::propertyNotWritable(asyncResp->res, "MACAddress"); 109858283f41SJohnathan Mantey return; 109958283f41SJohnathan Mantey } 1100d577665bSRatan Gupta messages::internalError(asyncResp->res); 1101d577665bSRatan Gupta return; 1102d577665bSRatan Gupta } 1103d577665bSRatan Gupta }, 1104d577665bSRatan Gupta "xyz.openbmc_project.Network", 1105d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1106d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1107d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1108168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1109d577665bSRatan Gupta } 1110286b9118SJohnathan Mantey 11114f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 11124f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 11134f48d5f6SEd Tanous const bool v6Value, 11148d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1115da131a9aSJennifer Lee { 11162c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1117da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1118da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1119da131a9aSJennifer Lee if (ec) 1120da131a9aSJennifer Lee { 1121da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1122da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1123da131a9aSJennifer Lee return; 1124da131a9aSJennifer Lee } 11258f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1126da131a9aSJennifer Lee }, 1127da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1128da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1129da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1130da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1131168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1132da131a9aSJennifer Lee } 11331f8c7b5dSJohnathan Mantey 11344f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1135eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 11368d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1137eeedda23SJohnathan Mantey { 1138eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1139eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1140eeedda23SJohnathan Mantey if (ec) 1141eeedda23SJohnathan Mantey { 1142eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1143eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1144eeedda23SJohnathan Mantey return; 1145eeedda23SJohnathan Mantey } 1146eeedda23SJohnathan Mantey }, 1147eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1148eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1149eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1150eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1151168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1152eeedda23SJohnathan Mantey } 1153eeedda23SJohnathan Mantey 11544f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 11558d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1156da131a9aSJennifer Lee { 1157da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1158da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1159da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1160da131a9aSJennifer Lee if (ec) 1161da131a9aSJennifer Lee { 1162da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1163da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1164da131a9aSJennifer Lee return; 1165da131a9aSJennifer Lee } 1166da131a9aSJennifer Lee }, 11671e3f85e6SJian Zhang "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp", 1168da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1169da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1170168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1171da131a9aSJennifer Lee } 1172d577665bSRatan Gupta 11734f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 11741f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1175f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1176f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 11778d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1178da131a9aSJennifer Lee { 117982695a5bSJiaqing Zhao bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 118082695a5bSJiaqing Zhao bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); 1181da131a9aSJennifer Lee 11821f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 11831f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 11841f8c7b5dSJohnathan Mantey 11851f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 11861f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1187da131a9aSJennifer Lee { 11881f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 11891f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 11901f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 11911f8c7b5dSJohnathan Mantey { 1192bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1193bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 11941f8c7b5dSJohnathan Mantey "OperatingMode"); 1195da131a9aSJennifer Lee return; 1196da131a9aSJennifer Lee } 11971f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 11981f8c7b5dSJohnathan Mantey } 11991f8c7b5dSJohnathan Mantey else 1200da131a9aSJennifer Lee { 12011f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 12021f8c7b5dSJohnathan Mantey } 12031f8c7b5dSJohnathan Mantey 12041f8c7b5dSJohnathan Mantey bool nextDNS{}; 120582695a5bSJiaqing Zhao if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) 12061f8c7b5dSJohnathan Mantey { 120782695a5bSJiaqing Zhao if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) 12081f8c7b5dSJohnathan Mantey { 12091f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12101f8c7b5dSJohnathan Mantey return; 12111f8c7b5dSJohnathan Mantey } 121282695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 12131f8c7b5dSJohnathan Mantey } 121482695a5bSJiaqing Zhao else if (v4dhcpParms.useDnsServers) 12151f8c7b5dSJohnathan Mantey { 121682695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 12171f8c7b5dSJohnathan Mantey } 121882695a5bSJiaqing Zhao else if (v6dhcpParms.useDnsServers) 12191f8c7b5dSJohnathan Mantey { 122082695a5bSJiaqing Zhao nextDNS = *v6dhcpParms.useDnsServers; 12211f8c7b5dSJohnathan Mantey } 12221f8c7b5dSJohnathan Mantey else 12231f8c7b5dSJohnathan Mantey { 122482695a5bSJiaqing Zhao nextDNS = ethData.dnsEnabled; 12251f8c7b5dSJohnathan Mantey } 12261f8c7b5dSJohnathan Mantey 12271f8c7b5dSJohnathan Mantey bool nextNTP{}; 122882695a5bSJiaqing Zhao if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) 12291f8c7b5dSJohnathan Mantey { 123082695a5bSJiaqing Zhao if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) 12311f8c7b5dSJohnathan Mantey { 12321f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12331f8c7b5dSJohnathan Mantey return; 12341f8c7b5dSJohnathan Mantey } 123582695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 12361f8c7b5dSJohnathan Mantey } 123782695a5bSJiaqing Zhao else if (v4dhcpParms.useNtpServers) 12381f8c7b5dSJohnathan Mantey { 123982695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 12401f8c7b5dSJohnathan Mantey } 124182695a5bSJiaqing Zhao else if (v6dhcpParms.useNtpServers) 12421f8c7b5dSJohnathan Mantey { 124382695a5bSJiaqing Zhao nextNTP = *v6dhcpParms.useNtpServers; 12441f8c7b5dSJohnathan Mantey } 12451f8c7b5dSJohnathan Mantey else 12461f8c7b5dSJohnathan Mantey { 124782695a5bSJiaqing Zhao nextNTP = ethData.ntpEnabled; 12481f8c7b5dSJohnathan Mantey } 12491f8c7b5dSJohnathan Mantey 12501f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 125182695a5bSJiaqing Zhao if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) 12521f8c7b5dSJohnathan Mantey { 125382695a5bSJiaqing Zhao if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) 12541f8c7b5dSJohnathan Mantey { 12551f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12561f8c7b5dSJohnathan Mantey return; 12571f8c7b5dSJohnathan Mantey } 125882695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12591f8c7b5dSJohnathan Mantey } 126082695a5bSJiaqing Zhao else if (v4dhcpParms.useDomainName) 12611f8c7b5dSJohnathan Mantey { 126282695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12631f8c7b5dSJohnathan Mantey } 126482695a5bSJiaqing Zhao else if (v6dhcpParms.useDomainName) 12651f8c7b5dSJohnathan Mantey { 126682695a5bSJiaqing Zhao nextUseDomain = *v6dhcpParms.useDomainName; 12671f8c7b5dSJohnathan Mantey } 12681f8c7b5dSJohnathan Mantey else 12691f8c7b5dSJohnathan Mantey { 127082695a5bSJiaqing Zhao nextUseDomain = ethData.hostNameEnabled; 12711f8c7b5dSJohnathan Mantey } 12721f8c7b5dSJohnathan Mantey 1273da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 12741f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 12751f8c7b5dSJohnathan Mantey asyncResp); 1276da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 12771f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1278da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 12791f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 12801f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 12811f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1282da131a9aSJennifer Lee } 128301784826SJohnathan Mantey 12844f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 12852c70f800SEd Tanous getNextStaticIpEntry( 1286bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1287bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 128801784826SJohnathan Mantey { 128917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 129017a897dfSManojkiran Eda return value.origin == "Static"; 129117a897dfSManojkiran Eda }); 129201784826SJohnathan Mantey } 129301784826SJohnathan Mantey 12944f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 12952c70f800SEd Tanous getNextStaticIpEntry( 1296bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1297bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 129801784826SJohnathan Mantey { 129917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 130017a897dfSManojkiran Eda return value.origin == "Static"; 130117a897dfSManojkiran Eda }); 130201784826SJohnathan Mantey } 130301784826SJohnathan Mantey 13044f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1305f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 130601784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 13078d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 13081abe55efSEd Tanous { 130901784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1310f476acbfSRatan Gupta { 131171f52d96SEd Tanous messages::propertyValueTypeError( 131271f52d96SEd Tanous asyncResp->res, 1313bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1314d1d50814SRavi Teja "IPv4StaticAddresses"); 1315f476acbfSRatan Gupta return; 1316f476acbfSRatan Gupta } 1317f476acbfSRatan Gupta 1318271584abSEd Tanous unsigned entryIdx = 1; 131901784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 132001784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 132101784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 132201784826SJohnathan Mantey // into the NIC. 132385ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 13242c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 132501784826SJohnathan Mantey 1326537174c4SEd Tanous for (nlohmann::json& thisJson : input) 13271abe55efSEd Tanous { 13284a0cb85cSEd Tanous std::string pathString = 1329d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1330179db1d7SKowalski, Kamil 133101784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1332f476acbfSRatan Gupta { 1333537174c4SEd Tanous std::optional<std::string> address; 1334537174c4SEd Tanous std::optional<std::string> subnetMask; 1335537174c4SEd Tanous std::optional<std::string> gateway; 1336537174c4SEd Tanous 1337537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 13387e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 13397e27d832SJohnathan Mantey "Gateway", gateway)) 1340537174c4SEd Tanous { 134101784826SJohnathan Mantey messages::propertyValueFormatError( 134271f52d96SEd Tanous asyncResp->res, 134371f52d96SEd Tanous thisJson.dump(2, ' ', true, 134471f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 134571f52d96SEd Tanous pathString); 1346537174c4SEd Tanous return; 1347179db1d7SKowalski, Kamil } 1348179db1d7SKowalski, Kamil 134901784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 135001784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 135101784826SJohnathan Mantey // current state of the interface. Merge existing state into the 135201784826SJohnathan Mantey // current request. 1353271584abSEd Tanous const std::string* addr = nullptr; 1354271584abSEd Tanous const std::string* gw = nullptr; 135501784826SJohnathan Mantey uint8_t prefixLength = 0; 135601784826SJohnathan Mantey bool errorInEntry = false; 1357537174c4SEd Tanous if (address) 13581abe55efSEd Tanous { 1359033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*address)) 13601abe55efSEd Tanous { 136101784826SJohnathan Mantey addr = &(*address); 13624a0cb85cSEd Tanous } 136301784826SJohnathan Mantey else 136401784826SJohnathan Mantey { 1365bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1366bf648f77SEd Tanous pathString + "/Address"); 136701784826SJohnathan Mantey errorInEntry = true; 136801784826SJohnathan Mantey } 136901784826SJohnathan Mantey } 137085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 137101784826SJohnathan Mantey { 137285ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 137301784826SJohnathan Mantey } 137401784826SJohnathan Mantey else 137501784826SJohnathan Mantey { 137601784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 137701784826SJohnathan Mantey pathString + "/Address"); 137801784826SJohnathan Mantey errorInEntry = true; 13794a0cb85cSEd Tanous } 13804a0cb85cSEd Tanous 1381537174c4SEd Tanous if (subnetMask) 13824a0cb85cSEd Tanous { 1383033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask, 1384033f1e4dSEd Tanous &prefixLength)) 13854a0cb85cSEd Tanous { 1386f12894f8SJason M. Bills messages::propertyValueFormatError( 1387537174c4SEd Tanous asyncResp->res, *subnetMask, 13884a0cb85cSEd Tanous pathString + "/SubnetMask"); 138901784826SJohnathan Mantey errorInEntry = true; 13904a0cb85cSEd Tanous } 13914a0cb85cSEd Tanous } 139285ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 13934a0cb85cSEd Tanous { 1394033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 139501784826SJohnathan Mantey &prefixLength)) 13964a0cb85cSEd Tanous { 139701784826SJohnathan Mantey messages::propertyValueFormatError( 139885ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 139901784826SJohnathan Mantey pathString + "/SubnetMask"); 140001784826SJohnathan Mantey errorInEntry = true; 14014a0cb85cSEd Tanous } 14024a0cb85cSEd Tanous } 14031abe55efSEd Tanous else 14041abe55efSEd Tanous { 140501784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 140601784826SJohnathan Mantey pathString + "/SubnetMask"); 140701784826SJohnathan Mantey errorInEntry = true; 140801784826SJohnathan Mantey } 140901784826SJohnathan Mantey 141001784826SJohnathan Mantey if (gateway) 141101784826SJohnathan Mantey { 1412033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway)) 141301784826SJohnathan Mantey { 141401784826SJohnathan Mantey gw = &(*gateway); 141501784826SJohnathan Mantey } 141601784826SJohnathan Mantey else 141701784826SJohnathan Mantey { 1418bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1419bf648f77SEd Tanous pathString + "/Gateway"); 142001784826SJohnathan Mantey errorInEntry = true; 142101784826SJohnathan Mantey } 142201784826SJohnathan Mantey } 142385ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 142401784826SJohnathan Mantey { 142585ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 142601784826SJohnathan Mantey } 142701784826SJohnathan Mantey else 14281abe55efSEd Tanous { 1429a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 14304a0cb85cSEd Tanous pathString + "/Gateway"); 143101784826SJohnathan Mantey errorInEntry = true; 14324a0cb85cSEd Tanous } 14334a0cb85cSEd Tanous 143401784826SJohnathan Mantey if (errorInEntry) 14351abe55efSEd Tanous { 143601784826SJohnathan Mantey return; 14374a0cb85cSEd Tanous } 14384a0cb85cSEd Tanous 143985ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 14401abe55efSEd Tanous { 144185ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1442bf648f77SEd Tanous *addr, asyncResp); 144385ffe86aSJiaqing Zhao nicIpEntry = 144485ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1445588c3f0dSKowalski, Kamil } 144601784826SJohnathan Mantey else 144701784826SJohnathan Mantey { 1448cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1449cb13a392SEd Tanous asyncResp); 14504a0cb85cSEd Tanous } 14514a0cb85cSEd Tanous entryIdx++; 14524a0cb85cSEd Tanous } 145301784826SJohnathan Mantey else 145401784826SJohnathan Mantey { 145585ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 145601784826SJohnathan Mantey { 145701784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 145801784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 145901784826SJohnathan Mantey // in error, so bail out. 146001784826SJohnathan Mantey if (thisJson.is_null()) 146101784826SJohnathan Mantey { 146201784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 146301784826SJohnathan Mantey return; 146401784826SJohnathan Mantey } 146501784826SJohnathan Mantey messages::propertyValueFormatError( 146671f52d96SEd Tanous asyncResp->res, 146771f52d96SEd Tanous thisJson.dump(2, ' ', true, 146871f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 146971f52d96SEd Tanous pathString); 147001784826SJohnathan Mantey return; 147101784826SJohnathan Mantey } 147201784826SJohnathan Mantey 147301784826SJohnathan Mantey if (thisJson.is_null()) 147401784826SJohnathan Mantey { 147585ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 147601784826SJohnathan Mantey } 147785ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 147801784826SJohnathan Mantey { 147985ffe86aSJiaqing Zhao nicIpEntry = 148085ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 148101784826SJohnathan Mantey } 148201784826SJohnathan Mantey entryIdx++; 148301784826SJohnathan Mantey } 148401784826SJohnathan Mantey } 14854a0cb85cSEd Tanous } 14864a0cb85cSEd Tanous 14874f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1488f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1489f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 14908d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1491f85837bfSRAJESWARAN THILLAIGOVINDAN { 1492f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1493286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1494f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1495f85837bfSRAJESWARAN THILLAIGOVINDAN { 1496f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1497f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1498f85837bfSRAJESWARAN THILLAIGOVINDAN } 1499f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1500f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1501f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1502f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1503bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1504168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1505f85837bfSRAJESWARAN THILLAIGOVINDAN } 1506f85837bfSRAJESWARAN THILLAIGOVINDAN 15074f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1508f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 150901784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 15108d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1511e48c0fc5SRavi Teja { 151201784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1513e48c0fc5SRavi Teja { 151471f52d96SEd Tanous messages::propertyValueTypeError( 151571f52d96SEd Tanous asyncResp->res, 1516bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1517e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1518e48c0fc5SRavi Teja return; 1519e48c0fc5SRavi Teja } 1520271584abSEd Tanous size_t entryIdx = 1; 152185ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 15222c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1523f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1524e48c0fc5SRavi Teja { 1525e48c0fc5SRavi Teja std::string pathString = 1526e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1527e48c0fc5SRavi Teja 152801784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1529e48c0fc5SRavi Teja { 1530e48c0fc5SRavi Teja std::optional<std::string> address; 1531e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1532f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1533bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1534bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1535e48c0fc5SRavi Teja { 153601784826SJohnathan Mantey messages::propertyValueFormatError( 153771f52d96SEd Tanous asyncResp->res, 153871f52d96SEd Tanous thisJson.dump(2, ' ', true, 153971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 154071f52d96SEd Tanous pathString); 1541e48c0fc5SRavi Teja return; 1542e48c0fc5SRavi Teja } 1543e48c0fc5SRavi Teja 1544543f4400SEd Tanous const std::string* addr = nullptr; 1545543f4400SEd Tanous uint8_t prefix = 0; 154601784826SJohnathan Mantey 154701784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 154801784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 154901784826SJohnathan Mantey // current state of the interface. Merge existing state into the 155001784826SJohnathan Mantey // current request. 1551e48c0fc5SRavi Teja if (address) 1552e48c0fc5SRavi Teja { 155301784826SJohnathan Mantey addr = &(*address); 1554e48c0fc5SRavi Teja } 155585ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 155601784826SJohnathan Mantey { 155785ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 155801784826SJohnathan Mantey } 155901784826SJohnathan Mantey else 156001784826SJohnathan Mantey { 156101784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 156201784826SJohnathan Mantey pathString + "/Address"); 156301784826SJohnathan Mantey return; 1564e48c0fc5SRavi Teja } 1565e48c0fc5SRavi Teja 1566e48c0fc5SRavi Teja if (prefixLength) 1567e48c0fc5SRavi Teja { 156801784826SJohnathan Mantey prefix = *prefixLength; 156901784826SJohnathan Mantey } 157085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1571e48c0fc5SRavi Teja { 157285ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1573e48c0fc5SRavi Teja } 1574e48c0fc5SRavi Teja else 1575e48c0fc5SRavi Teja { 1576e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1577e48c0fc5SRavi Teja pathString + "/PrefixLength"); 157801784826SJohnathan Mantey return; 1579e48c0fc5SRavi Teja } 1580e48c0fc5SRavi Teja 158185ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1582e48c0fc5SRavi Teja { 158385ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1584e48c0fc5SRavi Teja asyncResp); 158585ffe86aSJiaqing Zhao nicIpEntry = 158685ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 158701784826SJohnathan Mantey } 158801784826SJohnathan Mantey else 158901784826SJohnathan Mantey { 159001784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1591e48c0fc5SRavi Teja } 1592e48c0fc5SRavi Teja entryIdx++; 1593e48c0fc5SRavi Teja } 159401784826SJohnathan Mantey else 159501784826SJohnathan Mantey { 159685ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 159701784826SJohnathan Mantey { 159801784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 159901784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 160001784826SJohnathan Mantey // in error, so bail out. 160101784826SJohnathan Mantey if (thisJson.is_null()) 160201784826SJohnathan Mantey { 160301784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 160401784826SJohnathan Mantey return; 160501784826SJohnathan Mantey } 160601784826SJohnathan Mantey messages::propertyValueFormatError( 160771f52d96SEd Tanous asyncResp->res, 160871f52d96SEd Tanous thisJson.dump(2, ' ', true, 160971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 161071f52d96SEd Tanous pathString); 161101784826SJohnathan Mantey return; 161201784826SJohnathan Mantey } 161301784826SJohnathan Mantey 161401784826SJohnathan Mantey if (thisJson.is_null()) 161501784826SJohnathan Mantey { 161685ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 161701784826SJohnathan Mantey } 161885ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 161901784826SJohnathan Mantey { 162085ffe86aSJiaqing Zhao nicIpEntry = 162185ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 162201784826SJohnathan Mantey } 162301784826SJohnathan Mantey entryIdx++; 162401784826SJohnathan Mantey } 162501784826SJohnathan Mantey } 1626e48c0fc5SRavi Teja } 1627e48c0fc5SRavi Teja 16284f48d5f6SEd Tanous inline void parseInterfaceData( 16298d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 16308d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1631e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 163201784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 16334a0cb85cSEd Tanous { 16347a1dbc48SGeorge Liu constexpr std::array<std::string_view, 1> inventoryForEthernet = { 1635eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1636eeedda23SJohnathan Mantey 16372c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 163881ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 1639*eddfc437SWilly Tu jsonResponse["@odata.id"] = crow::utility::urlFromPieces( 1640*eddfc437SWilly Tu "redfish", "v1", "Managers", "bmc", "EthernetInterfaces", ifaceId); 16412c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1642eeedda23SJohnathan Mantey 1643eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1644eeedda23SJohnathan Mantey 16457a1dbc48SGeorge Liu dbus::utility::getSubTreePaths( 16467a1dbc48SGeorge Liu "/", 0, inventoryForEthernet, 16477a1dbc48SGeorge Liu [health](const boost::system::error_code& ec, 1648b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& resp) { 1649eeedda23SJohnathan Mantey if (ec) 1650029573d4SEd Tanous { 1651eeedda23SJohnathan Mantey return; 1652eeedda23SJohnathan Mantey } 1653eeedda23SJohnathan Mantey 1654914e2d5dSEd Tanous health->inventory = resp; 16557a1dbc48SGeorge Liu }); 1656eeedda23SJohnathan Mantey 1657eeedda23SJohnathan Mantey health->populate(); 1658eeedda23SJohnathan Mantey 1659eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1660eeedda23SJohnathan Mantey { 16610ef0e289SJohnathan Mantey jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 16622c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1663029573d4SEd Tanous } 1664029573d4SEd Tanous else 1665029573d4SEd Tanous { 16662c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 16672c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1668029573d4SEd Tanous } 1669aa05fb27SJohnathan Mantey 16702c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 167135fb5311STejas Patil jsonResponse["MTUSize"] = ethData.mtuSize; 167282695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 16732c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 167482695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 167582695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; 167682695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; 167782695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; 16781f8c7b5dSJohnathan Mantey 16792c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 168082695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" 16811f8c7b5dSJohnathan Mantey : "Disabled"; 168282695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; 168382695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; 168482695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; 16852a133282Smanojkiraneda 168682695a5bSJiaqing Zhao if (!ethData.hostName.empty()) 16874a0cb85cSEd Tanous { 168882695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 1689ab6554f1SJoshi-Mansi 1690ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1691ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1692ab6554f1SJoshi-Mansi // FQDN 169382695a5bSJiaqing Zhao std::string fqdn = ethData.hostName; 1694d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1695d24bfc7aSJennifer Lee { 16962c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1697d24bfc7aSJennifer Lee } 16982c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 16994a0cb85cSEd Tanous } 17004a0cb85cSEd Tanous 1701613dabeaSEd Tanous jsonResponse["VLANs"]["@odata.id"] = 1702613dabeaSEd Tanous crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc", 1703613dabeaSEd Tanous "EthernetInterfaces", ifaceId, "VLANs"); 1704fda13ad2SSunitha Harish 17052c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 17062c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 17074a0cb85cSEd Tanous 17082c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 17092c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 17102c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 17112c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 17129eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 17134a0cb85cSEd Tanous { 17142c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1715fa5053a6SGunnar Mills if (gatewayStr.empty()) 1716fa5053a6SGunnar Mills { 1717fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1718fa5053a6SGunnar Mills } 17191476687dSEd Tanous nlohmann::json::object_t ipv4; 17201476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 17211476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 17221476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 17231476687dSEd Tanous ipv4["Gateway"] = gatewayStr; 1724fa5053a6SGunnar Mills 17252c70f800SEd Tanous if (ipv4Config.origin == "Static") 1726d1d50814SRavi Teja { 17271476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 1728d1d50814SRavi Teja } 17291476687dSEd Tanous 17301476687dSEd Tanous ipv4Array.push_back(std::move(ipv4)); 173101784826SJohnathan Mantey } 1732d1d50814SRavi Teja 173382695a5bSJiaqing Zhao std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; 17347ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 17357ea79e5eSRavi Teja { 17367ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 17377ea79e5eSRavi Teja } 17387ea79e5eSRavi Teja 17397ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1740e48c0fc5SRavi Teja 17412c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 17422c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 17432c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 17442c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 17457f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 17462c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 17477f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 17489eb808c1SEd Tanous for (const auto& ipv6Config : ipv6Data) 1749e48c0fc5SRavi Teja { 17501476687dSEd Tanous nlohmann::json::object_t ipv6; 17511476687dSEd Tanous ipv6["Address"] = ipv6Config.address; 17521476687dSEd Tanous ipv6["PrefixLength"] = ipv6Config.prefixLength; 17531476687dSEd Tanous ipv6["AddressOrigin"] = ipv6Config.origin; 17541476687dSEd Tanous ipv6["AddressState"] = nullptr; 17551476687dSEd Tanous ipv6Array.push_back(std::move(ipv6)); 17562c70f800SEd Tanous if (ipv6Config.origin == "Static") 1757e48c0fc5SRavi Teja { 17581476687dSEd Tanous nlohmann::json::object_t ipv6Static; 17591476687dSEd Tanous ipv6Static["Address"] = ipv6Config.address; 17601476687dSEd Tanous ipv6Static["PrefixLength"] = ipv6Config.prefixLength; 17611476687dSEd Tanous ipv6StaticArray.push_back(std::move(ipv6Static)); 176201784826SJohnathan Mantey } 1763e48c0fc5SRavi Teja } 1764588c3f0dSKowalski, Kamil } 1765588c3f0dSKowalski, Kamil 17664f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1767bf648f77SEd Tanous { 176811ba3979SEd Tanous return iface.starts_with(parent + "_"); 1769bf648f77SEd Tanous } 1770bf648f77SEd Tanous 1771bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1772bf648f77SEd Tanous { 1773bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1774ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 17751476687dSEd Tanous .methods(boost::beast::http::verb::get)( 17761476687dSEd Tanous [&app](const crow::Request& req, 17771476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 17783ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 177945ca1b86SEd Tanous { 178045ca1b86SEd Tanous return; 178145ca1b86SEd Tanous } 178245ca1b86SEd Tanous 1783bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1784bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1785bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1786bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1787bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1788bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1789bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1790bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1791bf648f77SEd Tanous 1792bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1793bf648f77SEd Tanous // preparation 1794002d39b4SEd Tanous getEthernetIfaceList( 1795002d39b4SEd Tanous [asyncResp]( 17961476687dSEd Tanous const bool& success, 1797002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 1798bf648f77SEd Tanous if (!success) 17991abe55efSEd Tanous { 1800f12894f8SJason M. Bills messages::internalError(asyncResp->res); 18019391bb9cSRapkiewicz, Pawel return; 18029391bb9cSRapkiewicz, Pawel } 18039391bb9cSRapkiewicz, Pawel 1804002d39b4SEd Tanous nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 1805bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1806bf648f77SEd Tanous std::string tag = "_"; 1807bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1808bf648f77SEd Tanous { 1809bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1810bf648f77SEd Tanous if (found == std::string::npos) 1811bf648f77SEd Tanous { 18121476687dSEd Tanous nlohmann::json::object_t iface; 1813*eddfc437SWilly Tu iface["@odata.id"] = crow::utility::urlFromPieces( 1814*eddfc437SWilly Tu "redfish", "v1", "Managers", "bmc", 1815*eddfc437SWilly Tu "EthernetInterfaces", ifaceItem); 18161476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 1817bf648f77SEd Tanous } 1818bf648f77SEd Tanous } 1819bf648f77SEd Tanous 1820002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 1821bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1822bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1823bf648f77SEd Tanous }); 1824bf648f77SEd Tanous }); 1825bf648f77SEd Tanous 1826bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1827ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1828bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 182945ca1b86SEd Tanous [&app](const crow::Request& req, 1830bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1831bf648f77SEd Tanous const std::string& ifaceId) { 18323ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 183345ca1b86SEd Tanous { 183445ca1b86SEd Tanous return; 183545ca1b86SEd Tanous } 18364a0cb85cSEd Tanous getEthernetIfaceData( 1837bf648f77SEd Tanous ifaceId, 1838002d39b4SEd Tanous [asyncResp, ifaceId]( 1839002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1840002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1841002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 18424a0cb85cSEd Tanous if (!success) 18431abe55efSEd Tanous { 1844bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1845bf648f77SEd Tanous // existing object, and other errors 1846002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1847002d39b4SEd Tanous ifaceId); 18484a0cb85cSEd Tanous return; 18499391bb9cSRapkiewicz, Pawel } 18504c9afe43SEd Tanous 1851188cb629SJiaqing Zhao // Keep using the v1.6.0 schema here as currently bmcweb have to use 1852188cb629SJiaqing Zhao // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion. 18530f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1854188cb629SJiaqing Zhao "#EthernetInterface.v1_6_0.EthernetInterface"; 1855002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 18560f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 18570f74e643SEd Tanous "Management Network Interface"; 18580f74e643SEd Tanous 1859002d39b4SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data); 18609391bb9cSRapkiewicz, Pawel }); 1861bf648f77SEd Tanous }); 18629391bb9cSRapkiewicz, Pawel 1863bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1864ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1865bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 186645ca1b86SEd Tanous [&app](const crow::Request& req, 1867bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1868bf648f77SEd Tanous const std::string& ifaceId) { 18693ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 187045ca1b86SEd Tanous { 187145ca1b86SEd Tanous return; 187245ca1b86SEd Tanous } 1873bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1874ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1875d577665bSRatan Gupta std::optional<std::string> macAddress; 18769a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1877d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1878e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1879f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1880da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 18811f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1882eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 188335fb5311STejas Patil std::optional<size_t> mtuSize; 18841f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 18851f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 18860627a2c7SEd Tanous 188715ed6780SWilly Tu if (!json_util::readJsonPatch( 18888d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1889002d39b4SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", 1890002d39b4SEd Tanous macAddress, "StaticNameServers", staticNameServers, 1891002d39b4SEd Tanous "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", 1892ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1893002d39b4SEd Tanous "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled)) 18941abe55efSEd Tanous { 1895588c3f0dSKowalski, Kamil return; 1896588c3f0dSKowalski, Kamil } 1897da131a9aSJennifer Lee if (dhcpv4) 1898da131a9aSJennifer Lee { 1899002d39b4SEd Tanous if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", 19001f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 190182695a5bSJiaqing Zhao v4dhcpParms.useDnsServers, "UseNTPServers", 190282695a5bSJiaqing Zhao v4dhcpParms.useNtpServers, "UseDomainName", 190382695a5bSJiaqing Zhao v4dhcpParms.useDomainName)) 19041f8c7b5dSJohnathan Mantey { 19051f8c7b5dSJohnathan Mantey return; 19061f8c7b5dSJohnathan Mantey } 19071f8c7b5dSJohnathan Mantey } 19081f8c7b5dSJohnathan Mantey 19091f8c7b5dSJohnathan Mantey if (dhcpv6) 19101f8c7b5dSJohnathan Mantey { 1911002d39b4SEd Tanous if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", 1912002d39b4SEd Tanous v6dhcpParms.dhcpv6OperatingMode, 1913002d39b4SEd Tanous "UseDNSServers", v6dhcpParms.useDnsServers, 1914002d39b4SEd Tanous "UseNTPServers", v6dhcpParms.useNtpServers, 1915002d39b4SEd Tanous "UseDomainName", 191682695a5bSJiaqing Zhao v6dhcpParms.useDomainName)) 19171f8c7b5dSJohnathan Mantey { 19181f8c7b5dSJohnathan Mantey return; 19191f8c7b5dSJohnathan Mantey } 1920da131a9aSJennifer Lee } 1921da131a9aSJennifer Lee 1922bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1923bf648f77SEd Tanous // for JSON preparation 19244a0cb85cSEd Tanous getEthernetIfaceData( 19252c70f800SEd Tanous ifaceId, 1926bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 1927ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1928d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 19299a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1930e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 19311f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 1932bc20089aSEd Tanous dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize, 1933bc20089aSEd Tanous v4dhcpParms = std::move(v4dhcpParms), 1934f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1935002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1936002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1937002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 19381abe55efSEd Tanous if (!success) 19391abe55efSEd Tanous { 1940588c3f0dSKowalski, Kamil // ... otherwise return error 1941bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1942bf648f77SEd Tanous // existing object, and other errors 1943002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1944002d39b4SEd Tanous ifaceId); 1945588c3f0dSKowalski, Kamil return; 1946588c3f0dSKowalski, Kamil } 1947588c3f0dSKowalski, Kamil 19481f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 19491f8c7b5dSJohnathan Mantey { 1950002d39b4SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, 1951002d39b4SEd Tanous asyncResp); 19521f8c7b5dSJohnathan Mantey } 19531f8c7b5dSJohnathan Mantey 19540627a2c7SEd Tanous if (hostname) 19551abe55efSEd Tanous { 19560627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 19571abe55efSEd Tanous } 19580627a2c7SEd Tanous 1959ab6554f1SJoshi-Mansi if (fqdn) 1960ab6554f1SJoshi-Mansi { 19612c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 1962ab6554f1SJoshi-Mansi } 1963ab6554f1SJoshi-Mansi 1964d577665bSRatan Gupta if (macAddress) 1965d577665bSRatan Gupta { 1966002d39b4SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, asyncResp); 1967d577665bSRatan Gupta } 1968d577665bSRatan Gupta 1969d1d50814SRavi Teja if (ipv4StaticAddresses) 1970d1d50814SRavi Teja { 1971bf648f77SEd Tanous // TODO(ed) for some reason the capture of 1972bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 1973bf648f77SEd Tanous // not a non-const value. This doesn't really work 1974bf648f77SEd Tanous // for us, as we need to be able to efficiently move 1975bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 1976bf648f77SEd Tanous // makes a copy of the structure, and operates on 1977bf648f77SEd Tanous // that, but could be done more efficiently 1978f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 1979002d39b4SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp); 19801abe55efSEd Tanous } 19810627a2c7SEd Tanous 1982f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 1983f85837bfSRAJESWARAN THILLAIGOVINDAN { 1984002d39b4SEd Tanous handleStaticNameServersPatch(ifaceId, *staticNameServers, 1985002d39b4SEd Tanous asyncResp); 1986f85837bfSRAJESWARAN THILLAIGOVINDAN } 19879a6fc6feSRavi Teja 19889a6fc6feSRavi Teja if (ipv6DefaultGateway) 19899a6fc6feSRavi Teja { 19909a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 19919a6fc6feSRavi Teja "IPv6DefaultGateway"); 19929a6fc6feSRavi Teja } 1993e48c0fc5SRavi Teja 1994e48c0fc5SRavi Teja if (ipv6StaticAddresses) 1995e48c0fc5SRavi Teja { 1996002d39b4SEd Tanous const nlohmann::json& ipv6Static = *ipv6StaticAddresses; 1997002d39b4SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data, 1998002d39b4SEd Tanous asyncResp); 1999e48c0fc5SRavi Teja } 2000eeedda23SJohnathan Mantey 2001eeedda23SJohnathan Mantey if (interfaceEnabled) 2002eeedda23SJohnathan Mantey { 2003002d39b4SEd Tanous setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled", 2004002d39b4SEd Tanous *interfaceEnabled, asyncResp); 2005eeedda23SJohnathan Mantey } 200635fb5311STejas Patil 200735fb5311STejas Patil if (mtuSize) 200835fb5311STejas Patil { 200935fb5311STejas Patil handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); 201035fb5311STejas Patil } 2011588c3f0dSKowalski, Kamil }); 2012bf648f77SEd Tanous }); 20139391bb9cSRapkiewicz, Pawel 2014bf648f77SEd Tanous BMCWEB_ROUTE( 2015bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 2016ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 2017bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 201845ca1b86SEd Tanous [&app](const crow::Request& req, 2019bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 202045ca1b86SEd Tanous const std::string& parentIfaceId, 202145ca1b86SEd Tanous const std::string& ifaceId) { 20223ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 202345ca1b86SEd Tanous { 202445ca1b86SEd Tanous return; 202545ca1b86SEd Tanous } 20268d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 20270f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 20288d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 2029e439f0f8SKowalski, Kamil 20302c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 20311abe55efSEd Tanous { 2032a434f2bdSEd Tanous return; 2033a434f2bdSEd Tanous } 2034a434f2bdSEd Tanous 2035bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2036bf648f77SEd Tanous // for JSON preparation 20374a0cb85cSEd Tanous getEthernetIfaceData( 2038bf648f77SEd Tanous ifaceId, 2039002d39b4SEd Tanous [asyncResp, parentIfaceId, 2040002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2041cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2042cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 204317e22024SJiaqing Zhao if (success && ethData.vlanId) 20441abe55efSEd Tanous { 204522872ff3SJiaqing Zhao asyncResp->res.jsonValue["Id"] = ifaceId; 204622872ff3SJiaqing Zhao asyncResp->res.jsonValue["@odata.id"] = 2047*eddfc437SWilly Tu crow::utility::urlFromPieces( 2048*eddfc437SWilly Tu "redfish", "v1", "Managers", "bmc", 2049*eddfc437SWilly Tu "EthernetInterfaces", parentIfaceId, "VLANs", ifaceId); 205022872ff3SJiaqing Zhao 205123a06317SJiaqing Zhao asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled; 205222872ff3SJiaqing Zhao asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId; 20531abe55efSEd Tanous } 20541abe55efSEd Tanous else 20551abe55efSEd Tanous { 2056e439f0f8SKowalski, Kamil // ... otherwise return error 2057bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2058bf648f77SEd Tanous // existing object, and other errors 2059bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2060d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2061e439f0f8SKowalski, Kamil } 2062e439f0f8SKowalski, Kamil }); 2063bf648f77SEd Tanous }); 2064e439f0f8SKowalski, Kamil 2065bf648f77SEd Tanous BMCWEB_ROUTE( 2066bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 20673d768a16SAbhishek Patel .privileges(redfish::privileges::patchVLanNetworkInterface) 2068bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 206945ca1b86SEd Tanous [&app](const crow::Request& req, 2070bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 207145ca1b86SEd Tanous const std::string& parentIfaceId, 207245ca1b86SEd Tanous const std::string& ifaceId) { 20733ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 207445ca1b86SEd Tanous { 207545ca1b86SEd Tanous return; 207645ca1b86SEd Tanous } 2077fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 20781abe55efSEd Tanous { 2079d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2080002d39b4SEd Tanous ifaceId); 2081927a505aSKowalski, Kamil return; 2082927a505aSKowalski, Kamil } 2083927a505aSKowalski, Kamil 20843927e13eSJiaqing Zhao std::optional<bool> vlanEnable; 20853927e13eSJiaqing Zhao std::optional<uint32_t> vlanId; 20860627a2c7SEd Tanous 208715ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable", 2088bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 20891abe55efSEd Tanous { 2090927a505aSKowalski, Kamil return; 2091927a505aSKowalski, Kamil } 2092927a505aSKowalski, Kamil 20933927e13eSJiaqing Zhao if (vlanId) 20943927e13eSJiaqing Zhao { 20953927e13eSJiaqing Zhao messages::propertyNotWritable(asyncResp->res, "VLANId"); 20963927e13eSJiaqing Zhao return; 20973927e13eSJiaqing Zhao } 20983927e13eSJiaqing Zhao 2099bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2100bf648f77SEd Tanous // for JSON preparation 2101e48c0fc5SRavi Teja getEthernetIfaceData( 2102bf648f77SEd Tanous ifaceId, 21033927e13eSJiaqing Zhao [asyncResp, parentIfaceId, ifaceId, vlanEnable]( 2104002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 2105cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2106cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 210717e22024SJiaqing Zhao if (success && ethData.vlanId) 210808244d02SSunitha Harish { 210923a06317SJiaqing Zhao if (vlanEnable) 211023a06317SJiaqing Zhao { 211123a06317SJiaqing Zhao crow::connections::systemBus->async_method_call( 2112002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 211308244d02SSunitha Harish if (ec) 211408244d02SSunitha Harish { 211508244d02SSunitha Harish messages::internalError(asyncResp->res); 21163927e13eSJiaqing Zhao return; 211708244d02SSunitha Harish } 211823a06317SJiaqing Zhao }, 211923a06317SJiaqing Zhao "xyz.openbmc_project.Network", 212023a06317SJiaqing Zhao "/xyz/openbmc_project/network/" + ifaceId, 212123a06317SJiaqing Zhao "org.freedesktop.DBus.Properties", "Set", 212223a06317SJiaqing Zhao "xyz.openbmc_project.Network.EthernetInterface", 212323a06317SJiaqing Zhao "NICEnabled", 212423a06317SJiaqing Zhao dbus::utility::DbusVariantType(*vlanEnable)); 212508244d02SSunitha Harish } 212608244d02SSunitha Harish } 212708244d02SSunitha Harish else 21281abe55efSEd Tanous { 2129bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2130bf648f77SEd Tanous // existing object, and other errors 2131bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2132d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2133bf648f77SEd Tanous return; 2134bf648f77SEd Tanous } 2135bf648f77SEd Tanous }); 2136bf648f77SEd Tanous }); 2137bf648f77SEd Tanous 2138bf648f77SEd Tanous BMCWEB_ROUTE( 2139bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 21403d768a16SAbhishek Patel .privileges(redfish::privileges::deleteVLanNetworkInterface) 2141bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 214245ca1b86SEd Tanous [&app](const crow::Request& req, 2143bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 214445ca1b86SEd Tanous const std::string& parentIfaceId, 214545ca1b86SEd Tanous const std::string& ifaceId) { 21463ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 214745ca1b86SEd Tanous { 214845ca1b86SEd Tanous return; 214945ca1b86SEd Tanous } 2150bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2151bf648f77SEd Tanous { 2152d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2153002d39b4SEd Tanous ifaceId); 2154927a505aSKowalski, Kamil return; 2155927a505aSKowalski, Kamil } 2156e439f0f8SKowalski, Kamil 2157bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2158bf648f77SEd Tanous // for JSON preparation 2159f12894f8SJason M. Bills getEthernetIfaceData( 2160bf648f77SEd Tanous ifaceId, 2161002d39b4SEd Tanous [asyncResp, parentIfaceId, 2162002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2163cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2164cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 216517e22024SJiaqing Zhao if (success && ethData.vlanId) 21661abe55efSEd Tanous { 2167f12894f8SJason M. Bills auto callback = 2168002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 21691abe55efSEd Tanous if (ec) 21701abe55efSEd Tanous { 2171f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2172927a505aSKowalski, Kamil } 21734a0cb85cSEd Tanous }; 21744a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2175002d39b4SEd Tanous std::move(callback), "xyz.openbmc_project.Network", 2176002d39b4SEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 21774a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 21781abe55efSEd Tanous } 21791abe55efSEd Tanous else 21801abe55efSEd Tanous { 2181927a505aSKowalski, Kamil // ... otherwise return error 2182bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2183bf648f77SEd Tanous // existing object, and other errors 2184bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2185d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2186927a505aSKowalski, Kamil } 2187927a505aSKowalski, Kamil }); 2188bf648f77SEd Tanous }); 2189e439f0f8SKowalski, Kamil 2190bf648f77SEd Tanous BMCWEB_ROUTE(app, 2191bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2192ed398213SEd Tanous 2193ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 21941476687dSEd Tanous .methods(boost::beast::http::verb::get)( 21951476687dSEd Tanous [&app](const crow::Request& req, 2196bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2197bf648f77SEd Tanous const std::string& rootInterfaceName) { 21983ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 219945ca1b86SEd Tanous { 220045ca1b86SEd Tanous return; 220145ca1b86SEd Tanous } 22024a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 22031abe55efSEd Tanous // preparation 2204002d39b4SEd Tanous getEthernetIfaceList( 2205002d39b4SEd Tanous [asyncResp, rootInterfaceName]( 22061abe55efSEd Tanous const bool& success, 2207002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 22084a0cb85cSEd Tanous if (!success) 22091abe55efSEd Tanous { 2210f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22114a0cb85cSEd Tanous return; 22121abe55efSEd Tanous } 22134c9afe43SEd Tanous 221481ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 22154c9afe43SEd Tanous { 2216002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, 2217002d39b4SEd Tanous "VLanNetworkInterfaceCollection", 22184c9afe43SEd Tanous rootInterfaceName); 22194c9afe43SEd Tanous return; 22204c9afe43SEd Tanous } 22214c9afe43SEd Tanous 22220f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 22230f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 22240f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 22250f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 22260f74e643SEd Tanous "VLAN Network Interface Collection"; 22274a0cb85cSEd Tanous 22282c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 22294a0cb85cSEd Tanous 223081ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 22311abe55efSEd Tanous { 223211ba3979SEd Tanous if (ifaceItem.starts_with(rootInterfaceName + "_")) 22334a0cb85cSEd Tanous { 22341476687dSEd Tanous nlohmann::json::object_t iface; 2235*eddfc437SWilly Tu iface["@odata.id"] = crow::utility::urlFromPieces( 2236*eddfc437SWilly Tu "redfish", "v1", "Managers", "bmc", 2237*eddfc437SWilly Tu "EthernetInterfaces", rootInterfaceName, "VLANs", 2238*eddfc437SWilly Tu ifaceItem); 22391476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 2240e439f0f8SKowalski, Kamil } 2241e439f0f8SKowalski, Kamil } 2242e439f0f8SKowalski, Kamil 2243002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 22442c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 22454a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 2246*eddfc437SWilly Tu crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc", 2247*eddfc437SWilly Tu "EthernetInterfaces", 2248*eddfc437SWilly Tu rootInterfaceName, "VLANs"); 2249e439f0f8SKowalski, Kamil }); 2250bf648f77SEd Tanous }); 2251e439f0f8SKowalski, Kamil 2252bf648f77SEd Tanous BMCWEB_ROUTE(app, 2253bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 22543d768a16SAbhishek Patel .privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2255bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 225645ca1b86SEd Tanous [&app](const crow::Request& req, 2257bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2258bf648f77SEd Tanous const std::string& rootInterfaceName) { 22593ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 226045ca1b86SEd Tanous { 226145ca1b86SEd Tanous return; 226245ca1b86SEd Tanous } 2263fda13ad2SSunitha Harish bool vlanEnable = false; 22640627a2c7SEd Tanous uint32_t vlanId = 0; 2265002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId, 2266002d39b4SEd Tanous "VLANEnable", vlanEnable)) 22671abe55efSEd Tanous { 22684a0cb85cSEd Tanous return; 2269e439f0f8SKowalski, Kamil } 2270fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2271dbb59d4dSEd Tanous if (vlanId == 0U) 2272fda13ad2SSunitha Harish { 2273fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2274fda13ad2SSunitha Harish } 2275fda13ad2SSunitha Harish if (!vlanEnable) 2276fda13ad2SSunitha Harish { 2277fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2278fda13ad2SSunitha Harish } 2279271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2280fda13ad2SSunitha Harish { 2281fda13ad2SSunitha Harish return; 2282fda13ad2SSunitha Harish } 2283fda13ad2SSunitha Harish 2284002d39b4SEd Tanous auto callback = [asyncResp](const boost::system::error_code ec) { 22851abe55efSEd Tanous if (ec) 22861abe55efSEd Tanous { 2287bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2288bf648f77SEd Tanous // based on phosphor-network responses 2289f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22904a0cb85cSEd Tanous return; 22911abe55efSEd Tanous } 2292f12894f8SJason M. Bills messages::created(asyncResp->res); 2293e439f0f8SKowalski, Kamil }; 22944a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 22954a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 22964a0cb85cSEd Tanous "/xyz/openbmc_project/network", 22974a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 22980627a2c7SEd Tanous rootInterfaceName, vlanId); 2299bf648f77SEd Tanous }); 23004a0cb85cSEd Tanous } 2301bf648f77SEd Tanous 23029391bb9cSRapkiewicz, Pawel } // namespace redfish 2303