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 18*3ccb3adbSEd Tanous #include "app.hpp" 19*3ccb3adbSEd Tanous #include "dbus_singleton.hpp" 207a1dbc48SGeorge Liu #include "dbus_utility.hpp" 21*3ccb3adbSEd Tanous #include "error_messages.hpp" 22*3ccb3adbSEd Tanous #include "health.hpp" 23*3ccb3adbSEd Tanous #include "query.hpp" 24*3ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 25033f1e4dSEd Tanous #include "utils/ip_utils.hpp" 26*3ccb3adbSEd 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 { 432e48c0fc5SRavi Teja const std::string ipv6PathStart = 43381ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/"; 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 44011ba3979SEd Tanous if (objpath.first.str.starts_with(ipv6PathStart)) 441e48c0fc5SRavi Teja { 4429eb808c1SEd Tanous for (const auto& interface : objpath.second) 443e48c0fc5SRavi Teja { 444e48c0fc5SRavi Teja if (interface.first == "xyz.openbmc_project.Network.IP") 445e48c0fc5SRavi Teja { 446e48c0fc5SRavi Teja // Instance IPv6AddressData structure, and set as 447e48c0fc5SRavi Teja // appropriate 448e48c0fc5SRavi Teja std::pair< 449e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData>::iterator, 450e48c0fc5SRavi Teja bool> 45181ce609eSEd Tanous it = ipv6Config.insert(IPv6AddressData{}); 4522c70f800SEd Tanous IPv6AddressData& ipv6Address = *it.first; 4532c70f800SEd Tanous ipv6Address.id = 454271584abSEd Tanous objpath.first.str.substr(ipv6PathStart.size()); 4559eb808c1SEd Tanous for (const auto& property : interface.second) 456e48c0fc5SRavi Teja { 457e48c0fc5SRavi Teja if (property.first == "Address") 458e48c0fc5SRavi Teja { 459e48c0fc5SRavi Teja const std::string* address = 460e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 461e48c0fc5SRavi Teja if (address != nullptr) 462e48c0fc5SRavi Teja { 4632c70f800SEd Tanous ipv6Address.address = *address; 464e48c0fc5SRavi Teja } 465e48c0fc5SRavi Teja } 466e48c0fc5SRavi Teja else if (property.first == "Origin") 467e48c0fc5SRavi Teja { 468e48c0fc5SRavi Teja const std::string* origin = 469e48c0fc5SRavi Teja std::get_if<std::string>(&property.second); 470e48c0fc5SRavi Teja if (origin != nullptr) 471e48c0fc5SRavi Teja { 4722c70f800SEd Tanous ipv6Address.origin = 473e48c0fc5SRavi Teja translateAddressOriginDbusToRedfish(*origin, 474e48c0fc5SRavi Teja false); 475e48c0fc5SRavi Teja } 476e48c0fc5SRavi Teja } 477e48c0fc5SRavi Teja else if (property.first == "PrefixLength") 478e48c0fc5SRavi Teja { 479e48c0fc5SRavi Teja const uint8_t* prefix = 480e48c0fc5SRavi Teja std::get_if<uint8_t>(&property.second); 481e48c0fc5SRavi Teja if (prefix != nullptr) 482e48c0fc5SRavi Teja { 4832c70f800SEd Tanous ipv6Address.prefixLength = *prefix; 484e48c0fc5SRavi Teja } 485e48c0fc5SRavi Teja } 486889ff694SAsmitha Karunanithi else if (property.first == "Type" || 487889ff694SAsmitha Karunanithi property.first == "Gateway") 488889ff694SAsmitha Karunanithi { 489889ff694SAsmitha Karunanithi // Type & Gateway is not used 490889ff694SAsmitha Karunanithi } 491e48c0fc5SRavi Teja else 492e48c0fc5SRavi Teja { 493e48c0fc5SRavi Teja BMCWEB_LOG_ERROR 494e48c0fc5SRavi Teja << "Got extra property: " << property.first 495e48c0fc5SRavi Teja << " on the " << objpath.first.str << " object"; 496e48c0fc5SRavi Teja } 497e48c0fc5SRavi Teja } 498e48c0fc5SRavi Teja } 499e48c0fc5SRavi Teja } 500e48c0fc5SRavi Teja } 501e48c0fc5SRavi Teja } 502e48c0fc5SRavi Teja } 503e48c0fc5SRavi Teja 5044a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address 50501784826SJohnathan Mantey inline void 50681ce609eSEd Tanous extractIPData(const std::string& ethifaceId, 507711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 50881ce609eSEd Tanous boost::container::flat_set<IPv4AddressData>& ipv4Config) 5094a0cb85cSEd Tanous { 5104a0cb85cSEd Tanous const std::string ipv4PathStart = 51181ce609eSEd Tanous "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/"; 5124a0cb85cSEd Tanous 5134a0cb85cSEd Tanous // Since there might be several IPv4 configurations aligned with 5144a0cb85cSEd Tanous // single ethernet interface, loop over all of them 51581ce609eSEd Tanous for (const auto& objpath : dbusData) 5164a0cb85cSEd Tanous { 5174a0cb85cSEd Tanous // Check if proper pattern for object path appears 51811ba3979SEd Tanous if (objpath.first.str.starts_with(ipv4PathStart)) 5194a0cb85cSEd Tanous { 5209eb808c1SEd Tanous for (const auto& interface : objpath.second) 5214a0cb85cSEd Tanous { 5224a0cb85cSEd Tanous if (interface.first == "xyz.openbmc_project.Network.IP") 5234a0cb85cSEd Tanous { 5244a0cb85cSEd Tanous // Instance IPv4AddressData structure, and set as 5254a0cb85cSEd Tanous // appropriate 5264a0cb85cSEd Tanous std::pair< 5274a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData>::iterator, 5284a0cb85cSEd Tanous bool> 52981ce609eSEd Tanous it = ipv4Config.insert(IPv4AddressData{}); 5302c70f800SEd Tanous IPv4AddressData& ipv4Address = *it.first; 5312c70f800SEd Tanous ipv4Address.id = 532271584abSEd Tanous objpath.first.str.substr(ipv4PathStart.size()); 5339eb808c1SEd Tanous for (const auto& property : interface.second) 5344a0cb85cSEd Tanous { 5354a0cb85cSEd Tanous if (property.first == "Address") 5364a0cb85cSEd Tanous { 5374a0cb85cSEd Tanous const std::string* address = 538abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5394a0cb85cSEd Tanous if (address != nullptr) 5404a0cb85cSEd Tanous { 5412c70f800SEd Tanous ipv4Address.address = *address; 5424a0cb85cSEd Tanous } 5434a0cb85cSEd Tanous } 5444a0cb85cSEd Tanous else if (property.first == "Origin") 5454a0cb85cSEd Tanous { 5464a0cb85cSEd Tanous const std::string* origin = 547abf2add6SEd Tanous std::get_if<std::string>(&property.second); 5484a0cb85cSEd Tanous if (origin != nullptr) 5494a0cb85cSEd Tanous { 5502c70f800SEd Tanous ipv4Address.origin = 5514a0cb85cSEd Tanous translateAddressOriginDbusToRedfish(*origin, 5524a0cb85cSEd Tanous true); 5534a0cb85cSEd Tanous } 5544a0cb85cSEd Tanous } 5554a0cb85cSEd Tanous else if (property.first == "PrefixLength") 5564a0cb85cSEd Tanous { 5574a0cb85cSEd Tanous const uint8_t* mask = 558abf2add6SEd Tanous std::get_if<uint8_t>(&property.second); 5594a0cb85cSEd Tanous if (mask != nullptr) 5604a0cb85cSEd Tanous { 5614a0cb85cSEd Tanous // convert it to the string 5622c70f800SEd Tanous ipv4Address.netmask = getNetmask(*mask); 5634a0cb85cSEd Tanous } 5644a0cb85cSEd Tanous } 565889ff694SAsmitha Karunanithi else if (property.first == "Type" || 566889ff694SAsmitha Karunanithi property.first == "Gateway") 567889ff694SAsmitha Karunanithi { 568889ff694SAsmitha Karunanithi // Type & Gateway is not used 569889ff694SAsmitha Karunanithi } 5704a0cb85cSEd Tanous else 5714a0cb85cSEd Tanous { 5724a0cb85cSEd Tanous BMCWEB_LOG_ERROR 5734a0cb85cSEd Tanous << "Got extra property: " << property.first 5744a0cb85cSEd Tanous << " on the " << objpath.first.str << " object"; 5754a0cb85cSEd Tanous } 5764a0cb85cSEd Tanous } 5774a0cb85cSEd Tanous // Check if given address is local, or global 5782c70f800SEd Tanous ipv4Address.linktype = 57911ba3979SEd Tanous ipv4Address.address.starts_with("169.254.") 58018659d10SJohnathan Mantey ? LinkType::Local 58118659d10SJohnathan Mantey : LinkType::Global; 5824a0cb85cSEd Tanous } 5834a0cb85cSEd Tanous } 5844a0cb85cSEd Tanous } 5854a0cb85cSEd Tanous } 5864a0cb85cSEd Tanous } 587588c3f0dSKowalski, Kamil 588588c3f0dSKowalski, Kamil /** 58901784826SJohnathan Mantey * @brief Deletes given IPv4 interface 590179db1d7SKowalski, Kamil * 591179db1d7SKowalski, Kamil * @param[in] ifaceId Id of interface whose IP should be deleted 592179db1d7SKowalski, Kamil * @param[in] ipHash DBus Hash id of IP that should be deleted 593179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 594179db1d7SKowalski, Kamil * 595179db1d7SKowalski, Kamil * @return None 596179db1d7SKowalski, Kamil */ 5974a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash, 5988d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 5991abe55efSEd Tanous { 60055c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 601286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 6021abe55efSEd Tanous if (ec) 6031abe55efSEd Tanous { 604a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6051abe55efSEd Tanous } 606179db1d7SKowalski, Kamil }, 607179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", 608179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 609179db1d7SKowalski, Kamil "xyz.openbmc_project.Object.Delete", "Delete"); 610179db1d7SKowalski, Kamil } 611179db1d7SKowalski, Kamil 612244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway( 613244b6d5bSGunnar Mills const std::string& ifaceId, const std::string& gateway, 614244b6d5bSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6159010ec2eSRavi Teja { 6169010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6179010ec2eSRavi Teja [asyncResp](const boost::system::error_code ec) { 6189010ec2eSRavi Teja if (ec) 6199010ec2eSRavi Teja { 6209010ec2eSRavi Teja messages::internalError(asyncResp->res); 6219010ec2eSRavi Teja return; 6229010ec2eSRavi Teja } 6239010ec2eSRavi Teja asyncResp->res.result(boost::beast::http::status::no_content); 6249010ec2eSRavi Teja }, 6259010ec2eSRavi Teja "xyz.openbmc_project.Network", 6269010ec2eSRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 6279010ec2eSRavi Teja "org.freedesktop.DBus.Properties", "Set", 6289010ec2eSRavi Teja "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway", 629168e20c1SEd Tanous dbus::utility::DbusVariantType(gateway)); 6309010ec2eSRavi Teja } 631179db1d7SKowalski, Kamil /** 63201784826SJohnathan Mantey * @brief Creates a static IPv4 entry 633179db1d7SKowalski, Kamil * 63401784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 63501784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 63601784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 63701784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 638179db1d7SKowalski, Kamil * @param[io] asyncResp Response object that will be returned to client 639179db1d7SKowalski, Kamil * 640179db1d7SKowalski, Kamil * @return None 641179db1d7SKowalski, Kamil */ 642cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength, 643cb13a392SEd Tanous const std::string& gateway, const std::string& address, 6448d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6451abe55efSEd Tanous { 646002d39b4SEd Tanous auto createIpHandler = 647002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec) { 6481abe55efSEd Tanous if (ec) 6491abe55efSEd Tanous { 650a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 6519010ec2eSRavi Teja return; 652179db1d7SKowalski, Kamil } 6539010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 6549010ec2eSRavi Teja }; 6559010ec2eSRavi Teja 6569010ec2eSRavi Teja crow::connections::systemBus->async_method_call( 6579010ec2eSRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 658179db1d7SKowalski, Kamil "/xyz/openbmc_project/network/" + ifaceId, 659179db1d7SKowalski, Kamil "xyz.openbmc_project.Network.IP.Create", "IP", 66001784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength, 661179db1d7SKowalski, Kamil gateway); 662179db1d7SKowalski, Kamil } 663e48c0fc5SRavi Teja 664e48c0fc5SRavi Teja /** 66501784826SJohnathan Mantey * @brief Deletes the IPv4 entry for this interface and creates a replacement 66601784826SJohnathan Mantey * static IPv4 entry 66701784826SJohnathan Mantey * 66801784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 66901784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 67001784826SJohnathan Mantey * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 67101784826SJohnathan Mantey * @param[in] gateway IPv4 address of this interfaces gateway 67201784826SJohnathan Mantey * @param[in] address IPv4 address to assign to this interface 67301784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 67401784826SJohnathan Mantey * 67501784826SJohnathan Mantey * @return None 67601784826SJohnathan Mantey */ 6778d1b46d7Szhanghch05 inline void 6788d1b46d7Szhanghch05 deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id, 6798d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& gateway, 68001784826SJohnathan Mantey const std::string& address, 6818d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 68201784826SJohnathan Mantey { 68301784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 68401784826SJohnathan Mantey [asyncResp, ifaceId, address, prefixLength, 68501784826SJohnathan Mantey gateway](const boost::system::error_code ec) { 68601784826SJohnathan Mantey if (ec) 68701784826SJohnathan Mantey { 68801784826SJohnathan Mantey messages::internalError(asyncResp->res); 6899010ec2eSRavi Teja return; 69001784826SJohnathan Mantey } 6919010ec2eSRavi Teja 69201784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 693002d39b4SEd Tanous [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) { 69423a21a1cSEd Tanous if (ec2) 69501784826SJohnathan Mantey { 69601784826SJohnathan Mantey messages::internalError(asyncResp->res); 6979010ec2eSRavi Teja return; 69801784826SJohnathan Mantey } 6999010ec2eSRavi Teja updateIPv4DefaultGateway(ifaceId, gateway, asyncResp); 70001784826SJohnathan Mantey }, 70101784826SJohnathan Mantey "xyz.openbmc_project.Network", 70201784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 70301784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 70401784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, 70501784826SJohnathan Mantey prefixLength, gateway); 70601784826SJohnathan Mantey }, 70701784826SJohnathan Mantey "xyz.openbmc_project.Network", 70801784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id, 70901784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 71001784826SJohnathan Mantey } 71101784826SJohnathan Mantey 71201784826SJohnathan Mantey /** 713e48c0fc5SRavi Teja * @brief Deletes given IPv6 714e48c0fc5SRavi Teja * 715e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be deleted 716e48c0fc5SRavi Teja * @param[in] ipHash DBus Hash id of IP that should be deleted 717e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 718e48c0fc5SRavi Teja * 719e48c0fc5SRavi Teja * @return None 720e48c0fc5SRavi Teja */ 721e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash, 7228d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 723e48c0fc5SRavi Teja { 724e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 725286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 726e48c0fc5SRavi Teja if (ec) 727e48c0fc5SRavi Teja { 728e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 729e48c0fc5SRavi Teja } 730e48c0fc5SRavi Teja }, 731e48c0fc5SRavi Teja "xyz.openbmc_project.Network", 732e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash, 733e48c0fc5SRavi Teja "xyz.openbmc_project.Object.Delete", "Delete"); 734e48c0fc5SRavi Teja } 735e48c0fc5SRavi Teja 736e48c0fc5SRavi Teja /** 73701784826SJohnathan Mantey * @brief Deletes the IPv6 entry for this interface and creates a replacement 73801784826SJohnathan Mantey * static IPv6 entry 73901784826SJohnathan Mantey * 74001784826SJohnathan Mantey * @param[in] ifaceId Id of interface upon which to create the IPv6 entry 74101784826SJohnathan Mantey * @param[in] id The unique hash entry identifying the DBus entry 74201784826SJohnathan Mantey * @param[in] prefixLength IPv6 prefix syntax for the subnet mask 74301784826SJohnathan Mantey * @param[in] address IPv6 address to assign to this interface 74401784826SJohnathan Mantey * @param[io] asyncResp Response object that will be returned to client 74501784826SJohnathan Mantey * 74601784826SJohnathan Mantey * @return None 74701784826SJohnathan Mantey */ 7488d1b46d7Szhanghch05 inline void 7498d1b46d7Szhanghch05 deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id, 7508d1b46d7Szhanghch05 uint8_t prefixLength, const std::string& address, 7518d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 75201784826SJohnathan Mantey { 75301784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 75401784826SJohnathan Mantey [asyncResp, ifaceId, address, 75501784826SJohnathan Mantey prefixLength](const boost::system::error_code ec) { 75601784826SJohnathan Mantey if (ec) 75701784826SJohnathan Mantey { 75801784826SJohnathan Mantey messages::internalError(asyncResp->res); 75901784826SJohnathan Mantey } 76001784826SJohnathan Mantey crow::connections::systemBus->async_method_call( 76123a21a1cSEd Tanous [asyncResp](const boost::system::error_code ec2) { 76223a21a1cSEd Tanous if (ec2) 76301784826SJohnathan Mantey { 76401784826SJohnathan Mantey messages::internalError(asyncResp->res); 76501784826SJohnathan Mantey } 76601784826SJohnathan Mantey }, 76701784826SJohnathan Mantey "xyz.openbmc_project.Network", 76801784826SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 76901784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Create", "IP", 77001784826SJohnathan Mantey "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, 77101784826SJohnathan Mantey prefixLength, ""); 77201784826SJohnathan Mantey }, 77301784826SJohnathan Mantey "xyz.openbmc_project.Network", 77401784826SJohnathan Mantey +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id, 77501784826SJohnathan Mantey "xyz.openbmc_project.Object.Delete", "Delete"); 77601784826SJohnathan Mantey } 77701784826SJohnathan Mantey 77801784826SJohnathan Mantey /** 779e48c0fc5SRavi Teja * @brief Creates IPv6 with given data 780e48c0fc5SRavi Teja * 781e48c0fc5SRavi Teja * @param[in] ifaceId Id of interface whose IP should be added 782e48c0fc5SRavi Teja * @param[in] prefixLength Prefix length that needs to be added 783e48c0fc5SRavi Teja * @param[in] address IP address that needs to be added 784e48c0fc5SRavi Teja * @param[io] asyncResp Response object that will be returned to client 785e48c0fc5SRavi Teja * 786e48c0fc5SRavi Teja * @return None 787e48c0fc5SRavi Teja */ 78801784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength, 78901784826SJohnathan Mantey const std::string& address, 7908d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 791e48c0fc5SRavi Teja { 792e48c0fc5SRavi Teja auto createIpHandler = [asyncResp](const boost::system::error_code ec) { 793e48c0fc5SRavi Teja if (ec) 794e48c0fc5SRavi Teja { 795e48c0fc5SRavi Teja messages::internalError(asyncResp->res); 796e48c0fc5SRavi Teja } 797e48c0fc5SRavi Teja }; 798e48c0fc5SRavi Teja // Passing null for gateway, as per redfish spec IPv6StaticAddresses object 7994e0453b1SGunnar Mills // does not have associated gateway property 800e48c0fc5SRavi Teja crow::connections::systemBus->async_method_call( 801e48c0fc5SRavi Teja std::move(createIpHandler), "xyz.openbmc_project.Network", 802e48c0fc5SRavi Teja "/xyz/openbmc_project/network/" + ifaceId, 803e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Create", "IP", 804e48c0fc5SRavi Teja "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength, 805e48c0fc5SRavi Teja ""); 806e48c0fc5SRavi Teja } 807e48c0fc5SRavi Teja 808179db1d7SKowalski, Kamil /** 809179db1d7SKowalski, Kamil * Function that retrieves all properties for given Ethernet Interface 810179db1d7SKowalski, Kamil * Object 811179db1d7SKowalski, Kamil * from EntityManager Network Manager 8124a0cb85cSEd Tanous * @param ethiface_id a eth interface id to query on DBus 813179db1d7SKowalski, Kamil * @param callback a function that shall be called to convert Dbus output 814179db1d7SKowalski, Kamil * into JSON 815179db1d7SKowalski, Kamil */ 816179db1d7SKowalski, Kamil template <typename CallbackFunc> 81781ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId, 8181abe55efSEd Tanous CallbackFunc&& callback) 8191abe55efSEd Tanous { 82055c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 821f94c4ecfSEd Tanous [ethifaceId{std::string{ethifaceId}}, 822f94c4ecfSEd Tanous callback{std::forward<CallbackFunc>(callback)}]( 82381ce609eSEd Tanous const boost::system::error_code errorCode, 82402cad96eSEd Tanous const dbus::utility::ManagedObjectType& resp) { 82555c7b7a2SEd Tanous EthernetInterfaceData ethData{}; 8264a0cb85cSEd Tanous boost::container::flat_set<IPv4AddressData> ipv4Data; 827e48c0fc5SRavi Teja boost::container::flat_set<IPv6AddressData> ipv6Data; 828179db1d7SKowalski, Kamil 82981ce609eSEd Tanous if (errorCode) 8301abe55efSEd Tanous { 83101784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 832179db1d7SKowalski, Kamil return; 833179db1d7SKowalski, Kamil } 834179db1d7SKowalski, Kamil 835002d39b4SEd Tanous bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData); 8364c9afe43SEd Tanous if (!found) 8374c9afe43SEd Tanous { 83801784826SJohnathan Mantey callback(false, ethData, ipv4Data, ipv6Data); 8394c9afe43SEd Tanous return; 8404c9afe43SEd Tanous } 8414c9afe43SEd Tanous 8422c70f800SEd Tanous extractIPData(ethifaceId, resp, ipv4Data); 843179db1d7SKowalski, Kamil // Fix global GW 8441abe55efSEd Tanous for (IPv4AddressData& ipv4 : ipv4Data) 8451abe55efSEd Tanous { 846c619141bSRavi Teja if (((ipv4.linktype == LinkType::Global) && 847c619141bSRavi Teja (ipv4.gateway == "0.0.0.0")) || 8489010ec2eSRavi Teja (ipv4.origin == "DHCP") || (ipv4.origin == "Static")) 8491abe55efSEd Tanous { 85082695a5bSJiaqing Zhao ipv4.gateway = ethData.defaultGateway; 851179db1d7SKowalski, Kamil } 852179db1d7SKowalski, Kamil } 853179db1d7SKowalski, Kamil 8542c70f800SEd Tanous extractIPV6Data(ethifaceId, resp, ipv6Data); 8554e0453b1SGunnar Mills // Finally make a callback with useful data 85601784826SJohnathan Mantey callback(true, ethData, ipv4Data, ipv6Data); 857179db1d7SKowalski, Kamil }, 858179db1d7SKowalski, Kamil "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 859179db1d7SKowalski, Kamil "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 860271584abSEd Tanous } 861179db1d7SKowalski, Kamil 862179db1d7SKowalski, Kamil /** 8639391bb9cSRapkiewicz, Pawel * Function that retrieves all Ethernet Interfaces available through Network 8649391bb9cSRapkiewicz, Pawel * Manager 8651abe55efSEd Tanous * @param callback a function that shall be called to convert Dbus output 8661abe55efSEd Tanous * into JSON. 8679391bb9cSRapkiewicz, Pawel */ 8689391bb9cSRapkiewicz, Pawel template <typename CallbackFunc> 8691abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback) 8701abe55efSEd Tanous { 87155c7b7a2SEd Tanous crow::connections::systemBus->async_method_call( 872f94c4ecfSEd Tanous [callback{std::forward<CallbackFunc>(callback)}]( 87381ce609eSEd Tanous const boost::system::error_code errorCode, 874711ac7a9SEd Tanous dbus::utility::ManagedObjectType& resp) { 8751abe55efSEd Tanous // Callback requires vector<string> to retrieve all available 8761abe55efSEd Tanous // ethernet interfaces 8772c70f800SEd Tanous boost::container::flat_set<std::string> ifaceList; 8782c70f800SEd Tanous ifaceList.reserve(resp.size()); 87981ce609eSEd Tanous if (errorCode) 8801abe55efSEd Tanous { 8812c70f800SEd Tanous callback(false, ifaceList); 8829391bb9cSRapkiewicz, Pawel return; 8839391bb9cSRapkiewicz, Pawel } 8849391bb9cSRapkiewicz, Pawel 8859391bb9cSRapkiewicz, Pawel // Iterate over all retrieved ObjectPaths. 8864a0cb85cSEd Tanous for (const auto& objpath : resp) 8871abe55efSEd Tanous { 8889391bb9cSRapkiewicz, Pawel // And all interfaces available for certain ObjectPath. 8894a0cb85cSEd Tanous for (const auto& interface : objpath.second) 8901abe55efSEd Tanous { 8911abe55efSEd Tanous // If interface is 8924a0cb85cSEd Tanous // xyz.openbmc_project.Network.EthernetInterface, this is 8934a0cb85cSEd Tanous // what we're looking for. 8949391bb9cSRapkiewicz, Pawel if (interface.first == 8951abe55efSEd Tanous "xyz.openbmc_project.Network.EthernetInterface") 8961abe55efSEd Tanous { 8972dfd18efSEd Tanous std::string ifaceId = objpath.first.filename(); 8982dfd18efSEd Tanous if (ifaceId.empty()) 8991abe55efSEd Tanous { 9002dfd18efSEd Tanous continue; 9019391bb9cSRapkiewicz, Pawel } 9022dfd18efSEd Tanous // and put it into output vector. 9032dfd18efSEd Tanous ifaceList.emplace(ifaceId); 9049391bb9cSRapkiewicz, Pawel } 9059391bb9cSRapkiewicz, Pawel } 9069391bb9cSRapkiewicz, Pawel } 907a434f2bdSEd Tanous // Finally make a callback with useful data 9082c70f800SEd Tanous callback(true, ifaceList); 9099391bb9cSRapkiewicz, Pawel }, 910aa2e59c1SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 911aa2e59c1SEd Tanous "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 912271584abSEd Tanous } 9139391bb9cSRapkiewicz, Pawel 9144f48d5f6SEd Tanous inline void 9154f48d5f6SEd Tanous handleHostnamePatch(const std::string& hostname, 9168d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 9171abe55efSEd Tanous { 918ab6554f1SJoshi-Mansi // SHOULD handle host names of up to 255 characters(RFC 1123) 919ab6554f1SJoshi-Mansi if (hostname.length() > 255) 920ab6554f1SJoshi-Mansi { 921ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, hostname, 922ab6554f1SJoshi-Mansi "HostName"); 923ab6554f1SJoshi-Mansi return; 924ab6554f1SJoshi-Mansi } 925bc0bd6e0SEd Tanous crow::connections::systemBus->async_method_call( 926bc0bd6e0SEd Tanous [asyncResp](const boost::system::error_code ec) { 9274a0cb85cSEd Tanous if (ec) 9284a0cb85cSEd Tanous { 929a08b46ccSJason M. Bills messages::internalError(asyncResp->res); 9301abe55efSEd Tanous } 931bc0bd6e0SEd Tanous }, 932bf648f77SEd Tanous "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config", 933bc0bd6e0SEd Tanous "org.freedesktop.DBus.Properties", "Set", 934bc0bd6e0SEd Tanous "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 935168e20c1SEd Tanous dbus::utility::DbusVariantType(hostname)); 936588c3f0dSKowalski, Kamil } 937588c3f0dSKowalski, Kamil 9384f48d5f6SEd Tanous inline void 93935fb5311STejas Patil handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize, 94035fb5311STejas Patil const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 94135fb5311STejas Patil { 94235fb5311STejas Patil sdbusplus::message::object_path objPath = 94335fb5311STejas Patil "/xyz/openbmc_project/network/" + ifaceId; 94435fb5311STejas Patil crow::connections::systemBus->async_method_call( 94535fb5311STejas Patil [asyncResp](const boost::system::error_code ec) { 94635fb5311STejas Patil if (ec) 94735fb5311STejas Patil { 94835fb5311STejas Patil messages::internalError(asyncResp->res); 94935fb5311STejas Patil } 95035fb5311STejas Patil }, 95135fb5311STejas Patil "xyz.openbmc_project.Network", objPath, 95235fb5311STejas Patil "org.freedesktop.DBus.Properties", "Set", 95335fb5311STejas Patil "xyz.openbmc_project.Network.EthernetInterface", "MTU", 95435fb5311STejas Patil std::variant<size_t>(mtuSize)); 95535fb5311STejas Patil } 95635fb5311STejas Patil 95735fb5311STejas Patil inline void 9584f48d5f6SEd Tanous handleDomainnamePatch(const std::string& ifaceId, 959bf648f77SEd Tanous const std::string& domainname, 9608d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 961ab6554f1SJoshi-Mansi { 962ab6554f1SJoshi-Mansi std::vector<std::string> vectorDomainname = {domainname}; 963ab6554f1SJoshi-Mansi crow::connections::systemBus->async_method_call( 964ab6554f1SJoshi-Mansi [asyncResp](const boost::system::error_code ec) { 965ab6554f1SJoshi-Mansi if (ec) 966ab6554f1SJoshi-Mansi { 967ab6554f1SJoshi-Mansi messages::internalError(asyncResp->res); 968ab6554f1SJoshi-Mansi } 969ab6554f1SJoshi-Mansi }, 970ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network", 971ab6554f1SJoshi-Mansi "/xyz/openbmc_project/network/" + ifaceId, 972ab6554f1SJoshi-Mansi "org.freedesktop.DBus.Properties", "Set", 973ab6554f1SJoshi-Mansi "xyz.openbmc_project.Network.EthernetInterface", "DomainName", 974168e20c1SEd Tanous dbus::utility::DbusVariantType(vectorDomainname)); 975ab6554f1SJoshi-Mansi } 976ab6554f1SJoshi-Mansi 9774f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname) 978bf648f77SEd Tanous { 979bf648f77SEd Tanous // A valid host name can never have the dotted-decimal form (RFC 1123) 980bf648f77SEd Tanous if (std::all_of(hostname.begin(), hostname.end(), ::isdigit)) 981bf648f77SEd Tanous { 982bf648f77SEd Tanous return false; 983bf648f77SEd Tanous } 984bf648f77SEd Tanous // Each label(hostname/subdomains) within a valid FQDN 985bf648f77SEd Tanous // MUST handle host names of up to 63 characters (RFC 1123) 986bf648f77SEd Tanous // labels cannot start or end with hyphens (RFC 952) 987bf648f77SEd Tanous // labels can start with numbers (RFC 1123) 988bf648f77SEd Tanous const std::regex pattern( 989bf648f77SEd Tanous "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$"); 990bf648f77SEd Tanous 991bf648f77SEd Tanous return std::regex_match(hostname, pattern); 992bf648f77SEd Tanous } 993bf648f77SEd Tanous 9944f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname) 995bf648f77SEd Tanous { 996bf648f77SEd Tanous // Can have multiple subdomains 997bf648f77SEd Tanous // Top Level Domain's min length is 2 character 9980fda0f12SGeorge Liu const std::regex pattern( 9990fda0f12SGeorge Liu "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$"); 1000bf648f77SEd Tanous 1001bf648f77SEd Tanous return std::regex_match(domainname, pattern); 1002bf648f77SEd Tanous } 1003bf648f77SEd Tanous 10044f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn, 10058d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1006ab6554f1SJoshi-Mansi { 1007ab6554f1SJoshi-Mansi // Total length of FQDN must not exceed 255 characters(RFC 1035) 1008ab6554f1SJoshi-Mansi if (fqdn.length() > 255) 1009ab6554f1SJoshi-Mansi { 1010ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1011ab6554f1SJoshi-Mansi return; 1012ab6554f1SJoshi-Mansi } 1013ab6554f1SJoshi-Mansi 1014ab6554f1SJoshi-Mansi size_t pos = fqdn.find('.'); 1015ab6554f1SJoshi-Mansi if (pos == std::string::npos) 1016ab6554f1SJoshi-Mansi { 1017ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1018ab6554f1SJoshi-Mansi return; 1019ab6554f1SJoshi-Mansi } 1020ab6554f1SJoshi-Mansi 1021ab6554f1SJoshi-Mansi std::string hostname; 1022ab6554f1SJoshi-Mansi std::string domainname; 1023ab6554f1SJoshi-Mansi domainname = (fqdn).substr(pos + 1); 1024ab6554f1SJoshi-Mansi hostname = (fqdn).substr(0, pos); 1025ab6554f1SJoshi-Mansi 1026ab6554f1SJoshi-Mansi if (!isHostnameValid(hostname) || !isDomainnameValid(domainname)) 1027ab6554f1SJoshi-Mansi { 1028ab6554f1SJoshi-Mansi messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN"); 1029ab6554f1SJoshi-Mansi return; 1030ab6554f1SJoshi-Mansi } 1031ab6554f1SJoshi-Mansi 1032ab6554f1SJoshi-Mansi handleHostnamePatch(hostname, asyncResp); 1033ab6554f1SJoshi-Mansi handleDomainnamePatch(ifaceId, domainname, asyncResp); 1034ab6554f1SJoshi-Mansi } 1035ab6554f1SJoshi-Mansi 10364f48d5f6SEd Tanous inline void 10374f48d5f6SEd Tanous handleMACAddressPatch(const std::string& ifaceId, 1038bf648f77SEd Tanous const std::string& macAddress, 10398d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1040d577665bSRatan Gupta { 104158283f41SJohnathan Mantey static constexpr std::string_view dbusNotAllowedError = 104258283f41SJohnathan Mantey "xyz.openbmc_project.Common.Error.NotAllowed"; 104358283f41SJohnathan Mantey 1044d577665bSRatan Gupta crow::connections::systemBus->async_method_call( 104558283f41SJohnathan Mantey [asyncResp, macAddress](const boost::system::error_code ec, 10465b378546SPatrick Williams const sdbusplus::message_t& msg) { 1047d577665bSRatan Gupta if (ec) 1048d577665bSRatan Gupta { 104958283f41SJohnathan Mantey const sd_bus_error* err = msg.get_error(); 105058283f41SJohnathan Mantey if (err == nullptr) 105158283f41SJohnathan Mantey { 105258283f41SJohnathan Mantey messages::internalError(asyncResp->res); 105358283f41SJohnathan Mantey return; 105458283f41SJohnathan Mantey } 105558283f41SJohnathan Mantey if (err->name == dbusNotAllowedError) 105658283f41SJohnathan Mantey { 105758283f41SJohnathan Mantey messages::propertyNotWritable(asyncResp->res, "MACAddress"); 105858283f41SJohnathan Mantey return; 105958283f41SJohnathan Mantey } 1060d577665bSRatan Gupta messages::internalError(asyncResp->res); 1061d577665bSRatan Gupta return; 1062d577665bSRatan Gupta } 1063d577665bSRatan Gupta }, 1064d577665bSRatan Gupta "xyz.openbmc_project.Network", 1065d577665bSRatan Gupta "/xyz/openbmc_project/network/" + ifaceId, 1066d577665bSRatan Gupta "org.freedesktop.DBus.Properties", "Set", 1067d577665bSRatan Gupta "xyz.openbmc_project.Network.MACAddress", "MACAddress", 1068168e20c1SEd Tanous dbus::utility::DbusVariantType(macAddress)); 1069d577665bSRatan Gupta } 1070286b9118SJohnathan Mantey 10714f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, 10724f48d5f6SEd Tanous const std::string& propertyName, const bool v4Value, 10734f48d5f6SEd Tanous const bool v6Value, 10748d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1075da131a9aSJennifer Lee { 10762c70f800SEd Tanous const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value); 1077da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1078da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1079da131a9aSJennifer Lee if (ec) 1080da131a9aSJennifer Lee { 1081da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1082da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1083da131a9aSJennifer Lee return; 1084da131a9aSJennifer Lee } 10858f7e9c19SJayaprakash Mutyala messages::success(asyncResp->res); 1086da131a9aSJennifer Lee }, 1087da131a9aSJennifer Lee "xyz.openbmc_project.Network", 1088da131a9aSJennifer Lee "/xyz/openbmc_project/network/" + ifaceId, 1089da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1090da131a9aSJennifer Lee "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1091168e20c1SEd Tanous dbus::utility::DbusVariantType{dhcp}); 1092da131a9aSJennifer Lee } 10931f8c7b5dSJohnathan Mantey 10944f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty( 1095eeedda23SJohnathan Mantey const std::string& ifaceId, const std::string& propertyName, 10968d1b46d7Szhanghch05 const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1097eeedda23SJohnathan Mantey { 1098eeedda23SJohnathan Mantey crow::connections::systemBus->async_method_call( 1099eeedda23SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1100eeedda23SJohnathan Mantey if (ec) 1101eeedda23SJohnathan Mantey { 1102eeedda23SJohnathan Mantey BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1103eeedda23SJohnathan Mantey messages::internalError(asyncResp->res); 1104eeedda23SJohnathan Mantey return; 1105eeedda23SJohnathan Mantey } 1106eeedda23SJohnathan Mantey }, 1107eeedda23SJohnathan Mantey "xyz.openbmc_project.Network", 1108eeedda23SJohnathan Mantey "/xyz/openbmc_project/network/" + ifaceId, 1109eeedda23SJohnathan Mantey "org.freedesktop.DBus.Properties", "Set", 1110eeedda23SJohnathan Mantey "xyz.openbmc_project.Network.EthernetInterface", propertyName, 1111168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1112eeedda23SJohnathan Mantey } 1113eeedda23SJohnathan Mantey 11144f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value, 11158d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1116da131a9aSJennifer Lee { 1117da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << propertyName << " = " << value; 1118da131a9aSJennifer Lee crow::connections::systemBus->async_method_call( 1119da131a9aSJennifer Lee [asyncResp](const boost::system::error_code ec) { 1120da131a9aSJennifer Lee if (ec) 1121da131a9aSJennifer Lee { 1122da131a9aSJennifer Lee BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec; 1123da131a9aSJennifer Lee messages::internalError(asyncResp->res); 1124da131a9aSJennifer Lee return; 1125da131a9aSJennifer Lee } 1126da131a9aSJennifer Lee }, 11271e3f85e6SJian Zhang "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp", 1128da131a9aSJennifer Lee "org.freedesktop.DBus.Properties", "Set", 1129da131a9aSJennifer Lee "xyz.openbmc_project.Network.DHCPConfiguration", propertyName, 1130168e20c1SEd Tanous dbus::utility::DbusVariantType{value}); 1131da131a9aSJennifer Lee } 1132d577665bSRatan Gupta 11334f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId, 11341f8c7b5dSJohnathan Mantey const EthernetInterfaceData& ethData, 1135f23b7296SEd Tanous const DHCPParameters& v4dhcpParms, 1136f23b7296SEd Tanous const DHCPParameters& v6dhcpParms, 11378d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1138da131a9aSJennifer Lee { 113982695a5bSJiaqing Zhao bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 114082695a5bSJiaqing Zhao bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false); 1141da131a9aSJennifer Lee 11421f8c7b5dSJohnathan Mantey bool nextv4DHCPState = 11431f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active; 11441f8c7b5dSJohnathan Mantey 11451f8c7b5dSJohnathan Mantey bool nextv6DHCPState{}; 11461f8c7b5dSJohnathan Mantey if (v6dhcpParms.dhcpv6OperatingMode) 1147da131a9aSJennifer Lee { 11481f8c7b5dSJohnathan Mantey if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") && 11491f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") && 11501f8c7b5dSJohnathan Mantey (*v6dhcpParms.dhcpv6OperatingMode != "Disabled")) 11511f8c7b5dSJohnathan Mantey { 1152bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, 1153bf648f77SEd Tanous *v6dhcpParms.dhcpv6OperatingMode, 11541f8c7b5dSJohnathan Mantey "OperatingMode"); 1155da131a9aSJennifer Lee return; 1156da131a9aSJennifer Lee } 11571f8c7b5dSJohnathan Mantey nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful"); 11581f8c7b5dSJohnathan Mantey } 11591f8c7b5dSJohnathan Mantey else 1160da131a9aSJennifer Lee { 11611f8c7b5dSJohnathan Mantey nextv6DHCPState = ipv6Active; 11621f8c7b5dSJohnathan Mantey } 11631f8c7b5dSJohnathan Mantey 11641f8c7b5dSJohnathan Mantey bool nextDNS{}; 116582695a5bSJiaqing Zhao if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers) 11661f8c7b5dSJohnathan Mantey { 116782695a5bSJiaqing Zhao if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers) 11681f8c7b5dSJohnathan Mantey { 11691f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 11701f8c7b5dSJohnathan Mantey return; 11711f8c7b5dSJohnathan Mantey } 117282695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 11731f8c7b5dSJohnathan Mantey } 117482695a5bSJiaqing Zhao else if (v4dhcpParms.useDnsServers) 11751f8c7b5dSJohnathan Mantey { 117682695a5bSJiaqing Zhao nextDNS = *v4dhcpParms.useDnsServers; 11771f8c7b5dSJohnathan Mantey } 117882695a5bSJiaqing Zhao else if (v6dhcpParms.useDnsServers) 11791f8c7b5dSJohnathan Mantey { 118082695a5bSJiaqing Zhao nextDNS = *v6dhcpParms.useDnsServers; 11811f8c7b5dSJohnathan Mantey } 11821f8c7b5dSJohnathan Mantey else 11831f8c7b5dSJohnathan Mantey { 118482695a5bSJiaqing Zhao nextDNS = ethData.dnsEnabled; 11851f8c7b5dSJohnathan Mantey } 11861f8c7b5dSJohnathan Mantey 11871f8c7b5dSJohnathan Mantey bool nextNTP{}; 118882695a5bSJiaqing Zhao if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers) 11891f8c7b5dSJohnathan Mantey { 119082695a5bSJiaqing Zhao if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers) 11911f8c7b5dSJohnathan Mantey { 11921f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 11931f8c7b5dSJohnathan Mantey return; 11941f8c7b5dSJohnathan Mantey } 119582695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 11961f8c7b5dSJohnathan Mantey } 119782695a5bSJiaqing Zhao else if (v4dhcpParms.useNtpServers) 11981f8c7b5dSJohnathan Mantey { 119982695a5bSJiaqing Zhao nextNTP = *v4dhcpParms.useNtpServers; 12001f8c7b5dSJohnathan Mantey } 120182695a5bSJiaqing Zhao else if (v6dhcpParms.useNtpServers) 12021f8c7b5dSJohnathan Mantey { 120382695a5bSJiaqing Zhao nextNTP = *v6dhcpParms.useNtpServers; 12041f8c7b5dSJohnathan Mantey } 12051f8c7b5dSJohnathan Mantey else 12061f8c7b5dSJohnathan Mantey { 120782695a5bSJiaqing Zhao nextNTP = ethData.ntpEnabled; 12081f8c7b5dSJohnathan Mantey } 12091f8c7b5dSJohnathan Mantey 12101f8c7b5dSJohnathan Mantey bool nextUseDomain{}; 121182695a5bSJiaqing Zhao if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName) 12121f8c7b5dSJohnathan Mantey { 121382695a5bSJiaqing Zhao if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName) 12141f8c7b5dSJohnathan Mantey { 12151f8c7b5dSJohnathan Mantey messages::generalError(asyncResp->res); 12161f8c7b5dSJohnathan Mantey return; 12171f8c7b5dSJohnathan Mantey } 121882695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12191f8c7b5dSJohnathan Mantey } 122082695a5bSJiaqing Zhao else if (v4dhcpParms.useDomainName) 12211f8c7b5dSJohnathan Mantey { 122282695a5bSJiaqing Zhao nextUseDomain = *v4dhcpParms.useDomainName; 12231f8c7b5dSJohnathan Mantey } 122482695a5bSJiaqing Zhao else if (v6dhcpParms.useDomainName) 12251f8c7b5dSJohnathan Mantey { 122682695a5bSJiaqing Zhao nextUseDomain = *v6dhcpParms.useDomainName; 12271f8c7b5dSJohnathan Mantey } 12281f8c7b5dSJohnathan Mantey else 12291f8c7b5dSJohnathan Mantey { 123082695a5bSJiaqing Zhao nextUseDomain = ethData.hostNameEnabled; 12311f8c7b5dSJohnathan Mantey } 12321f8c7b5dSJohnathan Mantey 1233da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DHCPEnabled..."; 12341f8c7b5dSJohnathan Mantey setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState, 12351f8c7b5dSJohnathan Mantey asyncResp); 1236da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set DNSEnabled..."; 12371f8c7b5dSJohnathan Mantey setDHCPv4Config("DNSEnabled", nextDNS, asyncResp); 1238da131a9aSJennifer Lee BMCWEB_LOG_DEBUG << "set NTPEnabled..."; 12391f8c7b5dSJohnathan Mantey setDHCPv4Config("NTPEnabled", nextNTP, asyncResp); 12401f8c7b5dSJohnathan Mantey BMCWEB_LOG_DEBUG << "set HostNameEnabled..."; 12411f8c7b5dSJohnathan Mantey setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp); 1242da131a9aSJennifer Lee } 124301784826SJohnathan Mantey 12444f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator 12452c70f800SEd Tanous getNextStaticIpEntry( 1246bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& head, 1247bf648f77SEd Tanous const boost::container::flat_set<IPv4AddressData>::const_iterator& end) 124801784826SJohnathan Mantey { 124917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv4AddressData& value) { 125017a897dfSManojkiran Eda return value.origin == "Static"; 125117a897dfSManojkiran Eda }); 125201784826SJohnathan Mantey } 125301784826SJohnathan Mantey 12544f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator 12552c70f800SEd Tanous getNextStaticIpEntry( 1256bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& head, 1257bf648f77SEd Tanous const boost::container::flat_set<IPv6AddressData>::const_iterator& end) 125801784826SJohnathan Mantey { 125917a897dfSManojkiran Eda return std::find_if(head, end, [](const IPv6AddressData& value) { 126017a897dfSManojkiran Eda return value.origin == "Static"; 126117a897dfSManojkiran Eda }); 126201784826SJohnathan Mantey } 126301784826SJohnathan Mantey 12644f48d5f6SEd Tanous inline void handleIPv4StaticPatch( 1265f476acbfSRatan Gupta const std::string& ifaceId, nlohmann::json& input, 126601784826SJohnathan Mantey const boost::container::flat_set<IPv4AddressData>& ipv4Data, 12678d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 12681abe55efSEd Tanous { 126901784826SJohnathan Mantey if ((!input.is_array()) || input.empty()) 1270f476acbfSRatan Gupta { 127171f52d96SEd Tanous messages::propertyValueTypeError( 127271f52d96SEd Tanous asyncResp->res, 1273bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1274d1d50814SRavi Teja "IPv4StaticAddresses"); 1275f476acbfSRatan Gupta return; 1276f476acbfSRatan Gupta } 1277f476acbfSRatan Gupta 1278271584abSEd Tanous unsigned entryIdx = 1; 127901784826SJohnathan Mantey // Find the first static IP address currently active on the NIC and 128001784826SJohnathan Mantey // match it to the first JSON element in the IPv4StaticAddresses array. 128101784826SJohnathan Mantey // Match each subsequent JSON element to the next static IP programmed 128201784826SJohnathan Mantey // into the NIC. 128385ffe86aSJiaqing Zhao boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry = 12842c70f800SEd Tanous getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend()); 128501784826SJohnathan Mantey 1286537174c4SEd Tanous for (nlohmann::json& thisJson : input) 12871abe55efSEd Tanous { 12884a0cb85cSEd Tanous std::string pathString = 1289d1d50814SRavi Teja "IPv4StaticAddresses/" + std::to_string(entryIdx); 1290179db1d7SKowalski, Kamil 129101784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1292f476acbfSRatan Gupta { 1293537174c4SEd Tanous std::optional<std::string> address; 1294537174c4SEd Tanous std::optional<std::string> subnetMask; 1295537174c4SEd Tanous std::optional<std::string> gateway; 1296537174c4SEd Tanous 1297537174c4SEd Tanous if (!json_util::readJson(thisJson, asyncResp->res, "Address", 12987e27d832SJohnathan Mantey address, "SubnetMask", subnetMask, 12997e27d832SJohnathan Mantey "Gateway", gateway)) 1300537174c4SEd Tanous { 130101784826SJohnathan Mantey messages::propertyValueFormatError( 130271f52d96SEd Tanous asyncResp->res, 130371f52d96SEd Tanous thisJson.dump(2, ' ', true, 130471f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 130571f52d96SEd Tanous pathString); 1306537174c4SEd Tanous return; 1307179db1d7SKowalski, Kamil } 1308179db1d7SKowalski, Kamil 130901784826SJohnathan Mantey // Find the address/subnet/gateway values. Any values that are 131001784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 131101784826SJohnathan Mantey // current state of the interface. Merge existing state into the 131201784826SJohnathan Mantey // current request. 1313271584abSEd Tanous const std::string* addr = nullptr; 1314271584abSEd Tanous const std::string* gw = nullptr; 131501784826SJohnathan Mantey uint8_t prefixLength = 0; 131601784826SJohnathan Mantey bool errorInEntry = false; 1317537174c4SEd Tanous if (address) 13181abe55efSEd Tanous { 1319033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*address)) 13201abe55efSEd Tanous { 132101784826SJohnathan Mantey addr = &(*address); 13224a0cb85cSEd Tanous } 132301784826SJohnathan Mantey else 132401784826SJohnathan Mantey { 1325bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *address, 1326bf648f77SEd Tanous pathString + "/Address"); 132701784826SJohnathan Mantey errorInEntry = true; 132801784826SJohnathan Mantey } 132901784826SJohnathan Mantey } 133085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 133101784826SJohnathan Mantey { 133285ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 133301784826SJohnathan Mantey } 133401784826SJohnathan Mantey else 133501784826SJohnathan Mantey { 133601784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 133701784826SJohnathan Mantey pathString + "/Address"); 133801784826SJohnathan Mantey errorInEntry = true; 13394a0cb85cSEd Tanous } 13404a0cb85cSEd Tanous 1341537174c4SEd Tanous if (subnetMask) 13424a0cb85cSEd Tanous { 1343033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask, 1344033f1e4dSEd Tanous &prefixLength)) 13454a0cb85cSEd Tanous { 1346f12894f8SJason M. Bills messages::propertyValueFormatError( 1347537174c4SEd Tanous asyncResp->res, *subnetMask, 13484a0cb85cSEd Tanous pathString + "/SubnetMask"); 134901784826SJohnathan Mantey errorInEntry = true; 13504a0cb85cSEd Tanous } 13514a0cb85cSEd Tanous } 135285ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 13534a0cb85cSEd Tanous { 1354033f1e4dSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask, 135501784826SJohnathan Mantey &prefixLength)) 13564a0cb85cSEd Tanous { 135701784826SJohnathan Mantey messages::propertyValueFormatError( 135885ffe86aSJiaqing Zhao asyncResp->res, nicIpEntry->netmask, 135901784826SJohnathan Mantey pathString + "/SubnetMask"); 136001784826SJohnathan Mantey errorInEntry = true; 13614a0cb85cSEd Tanous } 13624a0cb85cSEd Tanous } 13631abe55efSEd Tanous else 13641abe55efSEd Tanous { 136501784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 136601784826SJohnathan Mantey pathString + "/SubnetMask"); 136701784826SJohnathan Mantey errorInEntry = true; 136801784826SJohnathan Mantey } 136901784826SJohnathan Mantey 137001784826SJohnathan Mantey if (gateway) 137101784826SJohnathan Mantey { 1372033f1e4dSEd Tanous if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway)) 137301784826SJohnathan Mantey { 137401784826SJohnathan Mantey gw = &(*gateway); 137501784826SJohnathan Mantey } 137601784826SJohnathan Mantey else 137701784826SJohnathan Mantey { 1378bf648f77SEd Tanous messages::propertyValueFormatError(asyncResp->res, *gateway, 1379bf648f77SEd Tanous pathString + "/Gateway"); 138001784826SJohnathan Mantey errorInEntry = true; 138101784826SJohnathan Mantey } 138201784826SJohnathan Mantey } 138385ffe86aSJiaqing Zhao else if (nicIpEntry != ipv4Data.cend()) 138401784826SJohnathan Mantey { 138585ffe86aSJiaqing Zhao gw = &nicIpEntry->gateway; 138601784826SJohnathan Mantey } 138701784826SJohnathan Mantey else 13881abe55efSEd Tanous { 1389a08b46ccSJason M. Bills messages::propertyMissing(asyncResp->res, 13904a0cb85cSEd Tanous pathString + "/Gateway"); 139101784826SJohnathan Mantey errorInEntry = true; 13924a0cb85cSEd Tanous } 13934a0cb85cSEd Tanous 139401784826SJohnathan Mantey if (errorInEntry) 13951abe55efSEd Tanous { 139601784826SJohnathan Mantey return; 13974a0cb85cSEd Tanous } 13984a0cb85cSEd Tanous 139985ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 14001abe55efSEd Tanous { 140185ffe86aSJiaqing Zhao deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw, 1402bf648f77SEd Tanous *addr, asyncResp); 140385ffe86aSJiaqing Zhao nicIpEntry = 140485ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 1405588c3f0dSKowalski, Kamil } 140601784826SJohnathan Mantey else 140701784826SJohnathan Mantey { 1408cb13a392SEd Tanous createIPv4(ifaceId, prefixLength, *gateway, *address, 1409cb13a392SEd Tanous asyncResp); 14104a0cb85cSEd Tanous } 14114a0cb85cSEd Tanous entryIdx++; 14124a0cb85cSEd Tanous } 141301784826SJohnathan Mantey else 141401784826SJohnathan Mantey { 141585ffe86aSJiaqing Zhao if (nicIpEntry == ipv4Data.cend()) 141601784826SJohnathan Mantey { 141701784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 141801784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 141901784826SJohnathan Mantey // in error, so bail out. 142001784826SJohnathan Mantey if (thisJson.is_null()) 142101784826SJohnathan Mantey { 142201784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 142301784826SJohnathan Mantey return; 142401784826SJohnathan Mantey } 142501784826SJohnathan Mantey messages::propertyValueFormatError( 142671f52d96SEd Tanous asyncResp->res, 142771f52d96SEd Tanous thisJson.dump(2, ' ', true, 142871f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 142971f52d96SEd Tanous pathString); 143001784826SJohnathan Mantey return; 143101784826SJohnathan Mantey } 143201784826SJohnathan Mantey 143301784826SJohnathan Mantey if (thisJson.is_null()) 143401784826SJohnathan Mantey { 143585ffe86aSJiaqing Zhao deleteIPv4(ifaceId, nicIpEntry->id, asyncResp); 143601784826SJohnathan Mantey } 143785ffe86aSJiaqing Zhao if (nicIpEntry != ipv4Data.cend()) 143801784826SJohnathan Mantey { 143985ffe86aSJiaqing Zhao nicIpEntry = 144085ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend()); 144101784826SJohnathan Mantey } 144201784826SJohnathan Mantey entryIdx++; 144301784826SJohnathan Mantey } 144401784826SJohnathan Mantey } 14454a0cb85cSEd Tanous } 14464a0cb85cSEd Tanous 14474f48d5f6SEd Tanous inline void handleStaticNameServersPatch( 1448f85837bfSRAJESWARAN THILLAIGOVINDAN const std::string& ifaceId, 1449f85837bfSRAJESWARAN THILLAIGOVINDAN const std::vector<std::string>& updatedStaticNameServers, 14508d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1451f85837bfSRAJESWARAN THILLAIGOVINDAN { 1452f85837bfSRAJESWARAN THILLAIGOVINDAN crow::connections::systemBus->async_method_call( 1453286b9118SJohnathan Mantey [asyncResp](const boost::system::error_code ec) { 1454f85837bfSRAJESWARAN THILLAIGOVINDAN if (ec) 1455f85837bfSRAJESWARAN THILLAIGOVINDAN { 1456f85837bfSRAJESWARAN THILLAIGOVINDAN messages::internalError(asyncResp->res); 1457f85837bfSRAJESWARAN THILLAIGOVINDAN return; 1458f85837bfSRAJESWARAN THILLAIGOVINDAN } 1459f85837bfSRAJESWARAN THILLAIGOVINDAN }, 1460f85837bfSRAJESWARAN THILLAIGOVINDAN "xyz.openbmc_project.Network", 1461f85837bfSRAJESWARAN THILLAIGOVINDAN "/xyz/openbmc_project/network/" + ifaceId, 1462f85837bfSRAJESWARAN THILLAIGOVINDAN "org.freedesktop.DBus.Properties", "Set", 1463bf648f77SEd Tanous "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers", 1464168e20c1SEd Tanous dbus::utility::DbusVariantType{updatedStaticNameServers}); 1465f85837bfSRAJESWARAN THILLAIGOVINDAN } 1466f85837bfSRAJESWARAN THILLAIGOVINDAN 14674f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch( 1468f23b7296SEd Tanous const std::string& ifaceId, const nlohmann::json& input, 146901784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data, 14708d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1471e48c0fc5SRavi Teja { 147201784826SJohnathan Mantey if (!input.is_array() || input.empty()) 1473e48c0fc5SRavi Teja { 147471f52d96SEd Tanous messages::propertyValueTypeError( 147571f52d96SEd Tanous asyncResp->res, 1476bf648f77SEd Tanous input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace), 1477e48c0fc5SRavi Teja "IPv6StaticAddresses"); 1478e48c0fc5SRavi Teja return; 1479e48c0fc5SRavi Teja } 1480271584abSEd Tanous size_t entryIdx = 1; 148185ffe86aSJiaqing Zhao boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry = 14822c70f800SEd Tanous getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend()); 1483f23b7296SEd Tanous for (const nlohmann::json& thisJson : input) 1484e48c0fc5SRavi Teja { 1485e48c0fc5SRavi Teja std::string pathString = 1486e48c0fc5SRavi Teja "IPv6StaticAddresses/" + std::to_string(entryIdx); 1487e48c0fc5SRavi Teja 148801784826SJohnathan Mantey if (!thisJson.is_null() && !thisJson.empty()) 1489e48c0fc5SRavi Teja { 1490e48c0fc5SRavi Teja std::optional<std::string> address; 1491e48c0fc5SRavi Teja std::optional<uint8_t> prefixLength; 1492f23b7296SEd Tanous nlohmann::json thisJsonCopy = thisJson; 1493bf648f77SEd Tanous if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 1494bf648f77SEd Tanous address, "PrefixLength", prefixLength)) 1495e48c0fc5SRavi Teja { 149601784826SJohnathan Mantey messages::propertyValueFormatError( 149771f52d96SEd Tanous asyncResp->res, 149871f52d96SEd Tanous thisJson.dump(2, ' ', true, 149971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 150071f52d96SEd Tanous pathString); 1501e48c0fc5SRavi Teja return; 1502e48c0fc5SRavi Teja } 1503e48c0fc5SRavi Teja 1504543f4400SEd Tanous const std::string* addr = nullptr; 1505543f4400SEd Tanous uint8_t prefix = 0; 150601784826SJohnathan Mantey 150701784826SJohnathan Mantey // Find the address and prefixLength values. Any values that are 150801784826SJohnathan Mantey // not explicitly provided are assumed to be unmodified from the 150901784826SJohnathan Mantey // current state of the interface. Merge existing state into the 151001784826SJohnathan Mantey // current request. 1511e48c0fc5SRavi Teja if (address) 1512e48c0fc5SRavi Teja { 151301784826SJohnathan Mantey addr = &(*address); 1514e48c0fc5SRavi Teja } 151585ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 151601784826SJohnathan Mantey { 151785ffe86aSJiaqing Zhao addr = &(nicIpEntry->address); 151801784826SJohnathan Mantey } 151901784826SJohnathan Mantey else 152001784826SJohnathan Mantey { 152101784826SJohnathan Mantey messages::propertyMissing(asyncResp->res, 152201784826SJohnathan Mantey pathString + "/Address"); 152301784826SJohnathan Mantey return; 1524e48c0fc5SRavi Teja } 1525e48c0fc5SRavi Teja 1526e48c0fc5SRavi Teja if (prefixLength) 1527e48c0fc5SRavi Teja { 152801784826SJohnathan Mantey prefix = *prefixLength; 152901784826SJohnathan Mantey } 153085ffe86aSJiaqing Zhao else if (nicIpEntry != ipv6Data.end()) 1531e48c0fc5SRavi Teja { 153285ffe86aSJiaqing Zhao prefix = nicIpEntry->prefixLength; 1533e48c0fc5SRavi Teja } 1534e48c0fc5SRavi Teja else 1535e48c0fc5SRavi Teja { 1536e48c0fc5SRavi Teja messages::propertyMissing(asyncResp->res, 1537e48c0fc5SRavi Teja pathString + "/PrefixLength"); 153801784826SJohnathan Mantey return; 1539e48c0fc5SRavi Teja } 1540e48c0fc5SRavi Teja 154185ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.end()) 1542e48c0fc5SRavi Teja { 154385ffe86aSJiaqing Zhao deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr, 1544e48c0fc5SRavi Teja asyncResp); 154585ffe86aSJiaqing Zhao nicIpEntry = 154685ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 154701784826SJohnathan Mantey } 154801784826SJohnathan Mantey else 154901784826SJohnathan Mantey { 155001784826SJohnathan Mantey createIPv6(ifaceId, *prefixLength, *addr, asyncResp); 1551e48c0fc5SRavi Teja } 1552e48c0fc5SRavi Teja entryIdx++; 1553e48c0fc5SRavi Teja } 155401784826SJohnathan Mantey else 155501784826SJohnathan Mantey { 155685ffe86aSJiaqing Zhao if (nicIpEntry == ipv6Data.end()) 155701784826SJohnathan Mantey { 155801784826SJohnathan Mantey // Requesting a DELETE/DO NOT MODIFY action for an item 155901784826SJohnathan Mantey // that isn't present on the eth(n) interface. Input JSON is 156001784826SJohnathan Mantey // in error, so bail out. 156101784826SJohnathan Mantey if (thisJson.is_null()) 156201784826SJohnathan Mantey { 156301784826SJohnathan Mantey messages::resourceCannotBeDeleted(asyncResp->res); 156401784826SJohnathan Mantey return; 156501784826SJohnathan Mantey } 156601784826SJohnathan Mantey messages::propertyValueFormatError( 156771f52d96SEd Tanous asyncResp->res, 156871f52d96SEd Tanous thisJson.dump(2, ' ', true, 156971f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 157071f52d96SEd Tanous pathString); 157101784826SJohnathan Mantey return; 157201784826SJohnathan Mantey } 157301784826SJohnathan Mantey 157401784826SJohnathan Mantey if (thisJson.is_null()) 157501784826SJohnathan Mantey { 157685ffe86aSJiaqing Zhao deleteIPv6(ifaceId, nicIpEntry->id, asyncResp); 157701784826SJohnathan Mantey } 157885ffe86aSJiaqing Zhao if (nicIpEntry != ipv6Data.cend()) 157901784826SJohnathan Mantey { 158085ffe86aSJiaqing Zhao nicIpEntry = 158185ffe86aSJiaqing Zhao getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend()); 158201784826SJohnathan Mantey } 158301784826SJohnathan Mantey entryIdx++; 158401784826SJohnathan Mantey } 158501784826SJohnathan Mantey } 1586e48c0fc5SRavi Teja } 1587e48c0fc5SRavi Teja 15884f48d5f6SEd Tanous inline void parseInterfaceData( 15898d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 15908d1b46d7Szhanghch05 const std::string& ifaceId, const EthernetInterfaceData& ethData, 1591e48c0fc5SRavi Teja const boost::container::flat_set<IPv4AddressData>& ipv4Data, 159201784826SJohnathan Mantey const boost::container::flat_set<IPv6AddressData>& ipv6Data) 15934a0cb85cSEd Tanous { 15947a1dbc48SGeorge Liu constexpr std::array<std::string_view, 1> inventoryForEthernet = { 1595eeedda23SJohnathan Mantey "xyz.openbmc_project.Inventory.Item.Ethernet"}; 1596eeedda23SJohnathan Mantey 15972c70f800SEd Tanous nlohmann::json& jsonResponse = asyncResp->res.jsonValue; 159881ce609eSEd Tanous jsonResponse["Id"] = ifaceId; 15992c70f800SEd Tanous jsonResponse["@odata.id"] = 160081ce609eSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId; 16012c70f800SEd Tanous jsonResponse["InterfaceEnabled"] = ethData.nicEnabled; 1602eeedda23SJohnathan Mantey 1603eeedda23SJohnathan Mantey auto health = std::make_shared<HealthPopulate>(asyncResp); 1604eeedda23SJohnathan Mantey 16057a1dbc48SGeorge Liu dbus::utility::getSubTreePaths( 16067a1dbc48SGeorge Liu "/", 0, inventoryForEthernet, 16077a1dbc48SGeorge Liu [health](const boost::system::error_code& ec, 1608b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& resp) { 1609eeedda23SJohnathan Mantey if (ec) 1610029573d4SEd Tanous { 1611eeedda23SJohnathan Mantey return; 1612eeedda23SJohnathan Mantey } 1613eeedda23SJohnathan Mantey 1614914e2d5dSEd Tanous health->inventory = resp; 16157a1dbc48SGeorge Liu }); 1616eeedda23SJohnathan Mantey 1617eeedda23SJohnathan Mantey health->populate(); 1618eeedda23SJohnathan Mantey 1619eeedda23SJohnathan Mantey if (ethData.nicEnabled) 1620eeedda23SJohnathan Mantey { 16210ef0e289SJohnathan Mantey jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown"; 16222c70f800SEd Tanous jsonResponse["Status"]["State"] = "Enabled"; 1623029573d4SEd Tanous } 1624029573d4SEd Tanous else 1625029573d4SEd Tanous { 16262c70f800SEd Tanous jsonResponse["LinkStatus"] = "NoLink"; 16272c70f800SEd Tanous jsonResponse["Status"]["State"] = "Disabled"; 1628029573d4SEd Tanous } 1629aa05fb27SJohnathan Mantey 16302c70f800SEd Tanous jsonResponse["SpeedMbps"] = ethData.speed; 163135fb5311STejas Patil jsonResponse["MTUSize"] = ethData.mtuSize; 163282695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 16332c70f800SEd Tanous jsonResponse["DHCPv4"]["DHCPEnabled"] = 163482695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 163582695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled; 163682695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled; 163782695a5bSJiaqing Zhao jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled; 16381f8c7b5dSJohnathan Mantey 16392c70f800SEd Tanous jsonResponse["DHCPv6"]["OperatingMode"] = 164082695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful" 16411f8c7b5dSJohnathan Mantey : "Disabled"; 164282695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled; 164382695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled; 164482695a5bSJiaqing Zhao jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled; 16452a133282Smanojkiraneda 164682695a5bSJiaqing Zhao if (!ethData.hostName.empty()) 16474a0cb85cSEd Tanous { 164882695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 1649ab6554f1SJoshi-Mansi 1650ab6554f1SJoshi-Mansi // When domain name is empty then it means, that it is a network 1651ab6554f1SJoshi-Mansi // without domain names, and the host name itself must be treated as 1652ab6554f1SJoshi-Mansi // FQDN 165382695a5bSJiaqing Zhao std::string fqdn = ethData.hostName; 1654d24bfc7aSJennifer Lee if (!ethData.domainnames.empty()) 1655d24bfc7aSJennifer Lee { 16562c70f800SEd Tanous fqdn += "." + ethData.domainnames[0]; 1657d24bfc7aSJennifer Lee } 16582c70f800SEd Tanous jsonResponse["FQDN"] = fqdn; 16594a0cb85cSEd Tanous } 16604a0cb85cSEd Tanous 1661613dabeaSEd Tanous jsonResponse["VLANs"]["@odata.id"] = 1662613dabeaSEd Tanous crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc", 1663613dabeaSEd Tanous "EthernetInterfaces", ifaceId, "VLANs"); 1664fda13ad2SSunitha Harish 16652c70f800SEd Tanous jsonResponse["NameServers"] = ethData.nameServers; 16662c70f800SEd Tanous jsonResponse["StaticNameServers"] = ethData.staticNameServers; 16674a0cb85cSEd Tanous 16682c70f800SEd Tanous nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 16692c70f800SEd Tanous nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 16702c70f800SEd Tanous ipv4Array = nlohmann::json::array(); 16712c70f800SEd Tanous ipv4StaticArray = nlohmann::json::array(); 16729eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 16734a0cb85cSEd Tanous { 16742c70f800SEd Tanous std::string gatewayStr = ipv4Config.gateway; 1675fa5053a6SGunnar Mills if (gatewayStr.empty()) 1676fa5053a6SGunnar Mills { 1677fa5053a6SGunnar Mills gatewayStr = "0.0.0.0"; 1678fa5053a6SGunnar Mills } 16791476687dSEd Tanous nlohmann::json::object_t ipv4; 16801476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 16811476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 16821476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 16831476687dSEd Tanous ipv4["Gateway"] = gatewayStr; 1684fa5053a6SGunnar Mills 16852c70f800SEd Tanous if (ipv4Config.origin == "Static") 1686d1d50814SRavi Teja { 16871476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 1688d1d50814SRavi Teja } 16891476687dSEd Tanous 16901476687dSEd Tanous ipv4Array.push_back(std::move(ipv4)); 169101784826SJohnathan Mantey } 1692d1d50814SRavi Teja 169382695a5bSJiaqing Zhao std::string ipv6GatewayStr = ethData.ipv6DefaultGateway; 16947ea79e5eSRavi Teja if (ipv6GatewayStr.empty()) 16957ea79e5eSRavi Teja { 16967ea79e5eSRavi Teja ipv6GatewayStr = "0:0:0:0:0:0:0:0"; 16977ea79e5eSRavi Teja } 16987ea79e5eSRavi Teja 16997ea79e5eSRavi Teja jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr; 1700e48c0fc5SRavi Teja 17012c70f800SEd Tanous nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"]; 17022c70f800SEd Tanous nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"]; 17032c70f800SEd Tanous ipv6Array = nlohmann::json::array(); 17042c70f800SEd Tanous ipv6StaticArray = nlohmann::json::array(); 17057f2e23e9SJohnathan Mantey nlohmann::json& ipv6AddrPolicyTable = 17062c70f800SEd Tanous jsonResponse["IPv6AddressPolicyTable"]; 17077f2e23e9SJohnathan Mantey ipv6AddrPolicyTable = nlohmann::json::array(); 17089eb808c1SEd Tanous for (const auto& ipv6Config : ipv6Data) 1709e48c0fc5SRavi Teja { 17101476687dSEd Tanous nlohmann::json::object_t ipv6; 17111476687dSEd Tanous ipv6["Address"] = ipv6Config.address; 17121476687dSEd Tanous ipv6["PrefixLength"] = ipv6Config.prefixLength; 17131476687dSEd Tanous ipv6["AddressOrigin"] = ipv6Config.origin; 17141476687dSEd Tanous ipv6["AddressState"] = nullptr; 17151476687dSEd Tanous ipv6Array.push_back(std::move(ipv6)); 17162c70f800SEd Tanous if (ipv6Config.origin == "Static") 1717e48c0fc5SRavi Teja { 17181476687dSEd Tanous nlohmann::json::object_t ipv6Static; 17191476687dSEd Tanous ipv6Static["Address"] = ipv6Config.address; 17201476687dSEd Tanous ipv6Static["PrefixLength"] = ipv6Config.prefixLength; 17211476687dSEd Tanous ipv6StaticArray.push_back(std::move(ipv6Static)); 172201784826SJohnathan Mantey } 1723e48c0fc5SRavi Teja } 1724588c3f0dSKowalski, Kamil } 1725588c3f0dSKowalski, Kamil 17264f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface) 1727bf648f77SEd Tanous { 172811ba3979SEd Tanous return iface.starts_with(parent + "_"); 1729bf648f77SEd Tanous } 1730bf648f77SEd Tanous 1731bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app) 1732bf648f77SEd Tanous { 1733bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 1734ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 17351476687dSEd Tanous .methods(boost::beast::http::verb::get)( 17361476687dSEd Tanous [&app](const crow::Request& req, 17371476687dSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 17383ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 173945ca1b86SEd Tanous { 174045ca1b86SEd Tanous return; 174145ca1b86SEd Tanous } 174245ca1b86SEd Tanous 1743bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1744bf648f77SEd Tanous "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 1745bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1746bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1747bf648f77SEd Tanous asyncResp->res.jsonValue["Name"] = 1748bf648f77SEd Tanous "Ethernet Network Interface Collection"; 1749bf648f77SEd Tanous asyncResp->res.jsonValue["Description"] = 1750bf648f77SEd Tanous "Collection of EthernetInterfaces for this Manager"; 1751bf648f77SEd Tanous 1752bf648f77SEd Tanous // Get eth interface list, and call the below callback for JSON 1753bf648f77SEd Tanous // preparation 1754002d39b4SEd Tanous getEthernetIfaceList( 1755002d39b4SEd Tanous [asyncResp]( 17561476687dSEd Tanous const bool& success, 1757002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 1758bf648f77SEd Tanous if (!success) 17591abe55efSEd Tanous { 1760f12894f8SJason M. Bills messages::internalError(asyncResp->res); 17619391bb9cSRapkiewicz, Pawel return; 17629391bb9cSRapkiewicz, Pawel } 17639391bb9cSRapkiewicz, Pawel 1764002d39b4SEd Tanous nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 1765bf648f77SEd Tanous ifaceArray = nlohmann::json::array(); 1766bf648f77SEd Tanous std::string tag = "_"; 1767bf648f77SEd Tanous for (const std::string& ifaceItem : ifaceList) 1768bf648f77SEd Tanous { 1769bf648f77SEd Tanous std::size_t found = ifaceItem.find(tag); 1770bf648f77SEd Tanous if (found == std::string::npos) 1771bf648f77SEd Tanous { 17721476687dSEd Tanous nlohmann::json::object_t iface; 17731476687dSEd Tanous iface["@odata.id"] = 1774bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 17751476687dSEd Tanous ifaceItem; 17761476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 1777bf648f77SEd Tanous } 1778bf648f77SEd Tanous } 1779bf648f77SEd Tanous 1780002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 1781bf648f77SEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1782bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces"; 1783bf648f77SEd Tanous }); 1784bf648f77SEd Tanous }); 1785bf648f77SEd Tanous 1786bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1787ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 1788bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 178945ca1b86SEd Tanous [&app](const crow::Request& req, 1790bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1791bf648f77SEd Tanous const std::string& ifaceId) { 17923ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 179345ca1b86SEd Tanous { 179445ca1b86SEd Tanous return; 179545ca1b86SEd Tanous } 17964a0cb85cSEd Tanous getEthernetIfaceData( 1797bf648f77SEd Tanous ifaceId, 1798002d39b4SEd Tanous [asyncResp, ifaceId]( 1799002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1800002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1801002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 18024a0cb85cSEd Tanous if (!success) 18031abe55efSEd Tanous { 1804bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1805bf648f77SEd Tanous // existing object, and other errors 1806002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1807002d39b4SEd Tanous ifaceId); 18084a0cb85cSEd Tanous return; 18099391bb9cSRapkiewicz, Pawel } 18104c9afe43SEd Tanous 1811188cb629SJiaqing Zhao // Keep using the v1.6.0 schema here as currently bmcweb have to use 1812188cb629SJiaqing Zhao // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion. 18130f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 1814188cb629SJiaqing Zhao "#EthernetInterface.v1_6_0.EthernetInterface"; 1815002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 18160f74e643SEd Tanous asyncResp->res.jsonValue["Description"] = 18170f74e643SEd Tanous "Management Network Interface"; 18180f74e643SEd Tanous 1819002d39b4SEd Tanous parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data); 18209391bb9cSRapkiewicz, Pawel }); 1821bf648f77SEd Tanous }); 18229391bb9cSRapkiewicz, Pawel 1823bf648f77SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/") 1824ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 1825bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 182645ca1b86SEd Tanous [&app](const crow::Request& req, 1827bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1828bf648f77SEd Tanous const std::string& ifaceId) { 18293ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 183045ca1b86SEd Tanous { 183145ca1b86SEd Tanous return; 183245ca1b86SEd Tanous } 1833bc0bd6e0SEd Tanous std::optional<std::string> hostname; 1834ab6554f1SJoshi-Mansi std::optional<std::string> fqdn; 1835d577665bSRatan Gupta std::optional<std::string> macAddress; 18369a6fc6feSRavi Teja std::optional<std::string> ipv6DefaultGateway; 1837d1d50814SRavi Teja std::optional<nlohmann::json> ipv4StaticAddresses; 1838e48c0fc5SRavi Teja std::optional<nlohmann::json> ipv6StaticAddresses; 1839f85837bfSRAJESWARAN THILLAIGOVINDAN std::optional<std::vector<std::string>> staticNameServers; 1840da131a9aSJennifer Lee std::optional<nlohmann::json> dhcpv4; 18411f8c7b5dSJohnathan Mantey std::optional<nlohmann::json> dhcpv6; 1842eeedda23SJohnathan Mantey std::optional<bool> interfaceEnabled; 184335fb5311STejas Patil std::optional<size_t> mtuSize; 18441f8c7b5dSJohnathan Mantey DHCPParameters v4dhcpParms; 18451f8c7b5dSJohnathan Mantey DHCPParameters v6dhcpParms; 18460627a2c7SEd Tanous 184715ed6780SWilly Tu if (!json_util::readJsonPatch( 18488d1b46d7Szhanghch05 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn, 1849002d39b4SEd Tanous "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress", 1850002d39b4SEd Tanous macAddress, "StaticNameServers", staticNameServers, 1851002d39b4SEd Tanous "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses", 1852ab6554f1SJoshi-Mansi ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6, 1853002d39b4SEd Tanous "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled)) 18541abe55efSEd Tanous { 1855588c3f0dSKowalski, Kamil return; 1856588c3f0dSKowalski, Kamil } 1857da131a9aSJennifer Lee if (dhcpv4) 1858da131a9aSJennifer Lee { 1859002d39b4SEd Tanous if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", 18601f8c7b5dSJohnathan Mantey v4dhcpParms.dhcpv4Enabled, "UseDNSServers", 186182695a5bSJiaqing Zhao v4dhcpParms.useDnsServers, "UseNTPServers", 186282695a5bSJiaqing Zhao v4dhcpParms.useNtpServers, "UseDomainName", 186382695a5bSJiaqing Zhao v4dhcpParms.useDomainName)) 18641f8c7b5dSJohnathan Mantey { 18651f8c7b5dSJohnathan Mantey return; 18661f8c7b5dSJohnathan Mantey } 18671f8c7b5dSJohnathan Mantey } 18681f8c7b5dSJohnathan Mantey 18691f8c7b5dSJohnathan Mantey if (dhcpv6) 18701f8c7b5dSJohnathan Mantey { 1871002d39b4SEd Tanous if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode", 1872002d39b4SEd Tanous v6dhcpParms.dhcpv6OperatingMode, 1873002d39b4SEd Tanous "UseDNSServers", v6dhcpParms.useDnsServers, 1874002d39b4SEd Tanous "UseNTPServers", v6dhcpParms.useNtpServers, 1875002d39b4SEd Tanous "UseDomainName", 187682695a5bSJiaqing Zhao v6dhcpParms.useDomainName)) 18771f8c7b5dSJohnathan Mantey { 18781f8c7b5dSJohnathan Mantey return; 18791f8c7b5dSJohnathan Mantey } 1880da131a9aSJennifer Lee } 1881da131a9aSJennifer Lee 1882bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1883bf648f77SEd Tanous // for JSON preparation 18844a0cb85cSEd Tanous getEthernetIfaceData( 18852c70f800SEd Tanous ifaceId, 1886bf648f77SEd Tanous [asyncResp, ifaceId, hostname = std::move(hostname), 1887ab6554f1SJoshi-Mansi fqdn = std::move(fqdn), macAddress = std::move(macAddress), 1888d1d50814SRavi Teja ipv4StaticAddresses = std::move(ipv4StaticAddresses), 18899a6fc6feSRavi Teja ipv6DefaultGateway = std::move(ipv6DefaultGateway), 1890e48c0fc5SRavi Teja ipv6StaticAddresses = std::move(ipv6StaticAddresses), 18911f8c7b5dSJohnathan Mantey staticNameServers = std::move(staticNameServers), 1892bc20089aSEd Tanous dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize, 1893bc20089aSEd Tanous v4dhcpParms = std::move(v4dhcpParms), 1894f23b7296SEd Tanous v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled]( 1895002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 1896002d39b4SEd Tanous const boost::container::flat_set<IPv4AddressData>& ipv4Data, 1897002d39b4SEd Tanous const boost::container::flat_set<IPv6AddressData>& ipv6Data) { 18981abe55efSEd Tanous if (!success) 18991abe55efSEd Tanous { 1900588c3f0dSKowalski, Kamil // ... otherwise return error 1901bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 1902bf648f77SEd Tanous // existing object, and other errors 1903002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "EthernetInterface", 1904002d39b4SEd Tanous ifaceId); 1905588c3f0dSKowalski, Kamil return; 1906588c3f0dSKowalski, Kamil } 1907588c3f0dSKowalski, Kamil 19081f8c7b5dSJohnathan Mantey if (dhcpv4 || dhcpv6) 19091f8c7b5dSJohnathan Mantey { 1910002d39b4SEd Tanous handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms, 1911002d39b4SEd Tanous asyncResp); 19121f8c7b5dSJohnathan Mantey } 19131f8c7b5dSJohnathan Mantey 19140627a2c7SEd Tanous if (hostname) 19151abe55efSEd Tanous { 19160627a2c7SEd Tanous handleHostnamePatch(*hostname, asyncResp); 19171abe55efSEd Tanous } 19180627a2c7SEd Tanous 1919ab6554f1SJoshi-Mansi if (fqdn) 1920ab6554f1SJoshi-Mansi { 19212c70f800SEd Tanous handleFqdnPatch(ifaceId, *fqdn, asyncResp); 1922ab6554f1SJoshi-Mansi } 1923ab6554f1SJoshi-Mansi 1924d577665bSRatan Gupta if (macAddress) 1925d577665bSRatan Gupta { 1926002d39b4SEd Tanous handleMACAddressPatch(ifaceId, *macAddress, asyncResp); 1927d577665bSRatan Gupta } 1928d577665bSRatan Gupta 1929d1d50814SRavi Teja if (ipv4StaticAddresses) 1930d1d50814SRavi Teja { 1931bf648f77SEd Tanous // TODO(ed) for some reason the capture of 1932bf648f77SEd Tanous // ipv4Addresses above is returning a const value, 1933bf648f77SEd Tanous // not a non-const value. This doesn't really work 1934bf648f77SEd Tanous // for us, as we need to be able to efficiently move 1935bf648f77SEd Tanous // out the intermedia nlohmann::json objects. This 1936bf648f77SEd Tanous // makes a copy of the structure, and operates on 1937bf648f77SEd Tanous // that, but could be done more efficiently 1938f23b7296SEd Tanous nlohmann::json ipv4Static = *ipv4StaticAddresses; 1939002d39b4SEd Tanous handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp); 19401abe55efSEd Tanous } 19410627a2c7SEd Tanous 1942f85837bfSRAJESWARAN THILLAIGOVINDAN if (staticNameServers) 1943f85837bfSRAJESWARAN THILLAIGOVINDAN { 1944002d39b4SEd Tanous handleStaticNameServersPatch(ifaceId, *staticNameServers, 1945002d39b4SEd Tanous asyncResp); 1946f85837bfSRAJESWARAN THILLAIGOVINDAN } 19479a6fc6feSRavi Teja 19489a6fc6feSRavi Teja if (ipv6DefaultGateway) 19499a6fc6feSRavi Teja { 19509a6fc6feSRavi Teja messages::propertyNotWritable(asyncResp->res, 19519a6fc6feSRavi Teja "IPv6DefaultGateway"); 19529a6fc6feSRavi Teja } 1953e48c0fc5SRavi Teja 1954e48c0fc5SRavi Teja if (ipv6StaticAddresses) 1955e48c0fc5SRavi Teja { 1956002d39b4SEd Tanous const nlohmann::json& ipv6Static = *ipv6StaticAddresses; 1957002d39b4SEd Tanous handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data, 1958002d39b4SEd Tanous asyncResp); 1959e48c0fc5SRavi Teja } 1960eeedda23SJohnathan Mantey 1961eeedda23SJohnathan Mantey if (interfaceEnabled) 1962eeedda23SJohnathan Mantey { 1963002d39b4SEd Tanous setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled", 1964002d39b4SEd Tanous *interfaceEnabled, asyncResp); 1965eeedda23SJohnathan Mantey } 196635fb5311STejas Patil 196735fb5311STejas Patil if (mtuSize) 196835fb5311STejas Patil { 196935fb5311STejas Patil handleMTUSizePatch(ifaceId, *mtuSize, asyncResp); 197035fb5311STejas Patil } 1971588c3f0dSKowalski, Kamil }); 1972bf648f77SEd Tanous }); 19739391bb9cSRapkiewicz, Pawel 1974bf648f77SEd Tanous BMCWEB_ROUTE( 1975bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 1976ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterface) 1977bf648f77SEd Tanous .methods(boost::beast::http::verb::get)( 197845ca1b86SEd Tanous [&app](const crow::Request& req, 1979bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 198045ca1b86SEd Tanous const std::string& parentIfaceId, 198145ca1b86SEd Tanous const std::string& ifaceId) { 19823ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 198345ca1b86SEd Tanous { 198445ca1b86SEd Tanous return; 198545ca1b86SEd Tanous } 19868d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = 19870f74e643SEd Tanous "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 19888d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface"; 1989e439f0f8SKowalski, Kamil 19902c70f800SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 19911abe55efSEd Tanous { 1992a434f2bdSEd Tanous return; 1993a434f2bdSEd Tanous } 1994a434f2bdSEd Tanous 1995bf648f77SEd Tanous // Get single eth interface data, and call the below callback 1996bf648f77SEd Tanous // for JSON preparation 19974a0cb85cSEd Tanous getEthernetIfaceData( 1998bf648f77SEd Tanous ifaceId, 1999002d39b4SEd Tanous [asyncResp, parentIfaceId, 2000002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2001cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2002cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 200317e22024SJiaqing Zhao if (success && ethData.vlanId) 20041abe55efSEd Tanous { 200522872ff3SJiaqing Zhao asyncResp->res.jsonValue["Id"] = ifaceId; 200622872ff3SJiaqing Zhao asyncResp->res.jsonValue["@odata.id"] = 200722872ff3SJiaqing Zhao "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 200822872ff3SJiaqing Zhao parentIfaceId + "/VLANs/" + ifaceId; 200922872ff3SJiaqing Zhao 201023a06317SJiaqing Zhao asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled; 201122872ff3SJiaqing Zhao asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId; 20121abe55efSEd Tanous } 20131abe55efSEd Tanous else 20141abe55efSEd Tanous { 2015e439f0f8SKowalski, Kamil // ... otherwise return error 2016bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2017bf648f77SEd Tanous // existing object, and other errors 2018bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2019d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2020e439f0f8SKowalski, Kamil } 2021e439f0f8SKowalski, Kamil }); 2022bf648f77SEd Tanous }); 2023e439f0f8SKowalski, Kamil 2024bf648f77SEd Tanous BMCWEB_ROUTE( 2025bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 20263d768a16SAbhishek Patel .privileges(redfish::privileges::patchVLanNetworkInterface) 2027bf648f77SEd Tanous .methods(boost::beast::http::verb::patch)( 202845ca1b86SEd Tanous [&app](const crow::Request& req, 2029bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 203045ca1b86SEd Tanous const std::string& parentIfaceId, 203145ca1b86SEd Tanous const std::string& ifaceId) { 20323ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 203345ca1b86SEd Tanous { 203445ca1b86SEd Tanous return; 203545ca1b86SEd Tanous } 2036fda13ad2SSunitha Harish if (!verifyNames(parentIfaceId, ifaceId)) 20371abe55efSEd Tanous { 2038d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2039002d39b4SEd Tanous ifaceId); 2040927a505aSKowalski, Kamil return; 2041927a505aSKowalski, Kamil } 2042927a505aSKowalski, Kamil 20433927e13eSJiaqing Zhao std::optional<bool> vlanEnable; 20443927e13eSJiaqing Zhao std::optional<uint32_t> vlanId; 20450627a2c7SEd Tanous 204615ed6780SWilly Tu if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable", 2047bf648f77SEd Tanous vlanEnable, "VLANId", vlanId)) 20481abe55efSEd Tanous { 2049927a505aSKowalski, Kamil return; 2050927a505aSKowalski, Kamil } 2051927a505aSKowalski, Kamil 20523927e13eSJiaqing Zhao if (vlanId) 20533927e13eSJiaqing Zhao { 20543927e13eSJiaqing Zhao messages::propertyNotWritable(asyncResp->res, "VLANId"); 20553927e13eSJiaqing Zhao return; 20563927e13eSJiaqing Zhao } 20573927e13eSJiaqing Zhao 2058bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2059bf648f77SEd Tanous // for JSON preparation 2060e48c0fc5SRavi Teja getEthernetIfaceData( 2061bf648f77SEd Tanous ifaceId, 20623927e13eSJiaqing Zhao [asyncResp, parentIfaceId, ifaceId, vlanEnable]( 2063002d39b4SEd Tanous const bool& success, const EthernetInterfaceData& ethData, 2064cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2065cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 206617e22024SJiaqing Zhao if (success && ethData.vlanId) 206708244d02SSunitha Harish { 206823a06317SJiaqing Zhao if (vlanEnable) 206923a06317SJiaqing Zhao { 207023a06317SJiaqing Zhao crow::connections::systemBus->async_method_call( 2071002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 207208244d02SSunitha Harish if (ec) 207308244d02SSunitha Harish { 207408244d02SSunitha Harish messages::internalError(asyncResp->res); 20753927e13eSJiaqing Zhao return; 207608244d02SSunitha Harish } 207723a06317SJiaqing Zhao }, 207823a06317SJiaqing Zhao "xyz.openbmc_project.Network", 207923a06317SJiaqing Zhao "/xyz/openbmc_project/network/" + ifaceId, 208023a06317SJiaqing Zhao "org.freedesktop.DBus.Properties", "Set", 208123a06317SJiaqing Zhao "xyz.openbmc_project.Network.EthernetInterface", 208223a06317SJiaqing Zhao "NICEnabled", 208323a06317SJiaqing Zhao dbus::utility::DbusVariantType(*vlanEnable)); 208408244d02SSunitha Harish } 208508244d02SSunitha Harish } 208608244d02SSunitha Harish else 20871abe55efSEd Tanous { 2088bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2089bf648f77SEd Tanous // existing object, and other errors 2090bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2091d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2092bf648f77SEd Tanous return; 2093bf648f77SEd Tanous } 2094bf648f77SEd Tanous }); 2095bf648f77SEd Tanous }); 2096bf648f77SEd Tanous 2097bf648f77SEd Tanous BMCWEB_ROUTE( 2098bf648f77SEd Tanous app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/") 20993d768a16SAbhishek Patel .privileges(redfish::privileges::deleteVLanNetworkInterface) 2100bf648f77SEd Tanous .methods(boost::beast::http::verb::delete_)( 210145ca1b86SEd Tanous [&app](const crow::Request& req, 2102bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 210345ca1b86SEd Tanous const std::string& parentIfaceId, 210445ca1b86SEd Tanous const std::string& ifaceId) { 21053ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 210645ca1b86SEd Tanous { 210745ca1b86SEd Tanous return; 210845ca1b86SEd Tanous } 2109bf648f77SEd Tanous if (!verifyNames(parentIfaceId, ifaceId)) 2110bf648f77SEd Tanous { 2111d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface", 2112002d39b4SEd Tanous ifaceId); 2113927a505aSKowalski, Kamil return; 2114927a505aSKowalski, Kamil } 2115e439f0f8SKowalski, Kamil 2116bf648f77SEd Tanous // Get single eth interface data, and call the below callback 2117bf648f77SEd Tanous // for JSON preparation 2118f12894f8SJason M. Bills getEthernetIfaceData( 2119bf648f77SEd Tanous ifaceId, 2120002d39b4SEd Tanous [asyncResp, parentIfaceId, 2121002d39b4SEd Tanous ifaceId](const bool& success, const EthernetInterfaceData& ethData, 2122cb13a392SEd Tanous const boost::container::flat_set<IPv4AddressData>&, 2123cb13a392SEd Tanous const boost::container::flat_set<IPv6AddressData>&) { 212417e22024SJiaqing Zhao if (success && ethData.vlanId) 21251abe55efSEd Tanous { 2126f12894f8SJason M. Bills auto callback = 2127002d39b4SEd Tanous [asyncResp](const boost::system::error_code ec) { 21281abe55efSEd Tanous if (ec) 21291abe55efSEd Tanous { 2130f12894f8SJason M. Bills messages::internalError(asyncResp->res); 2131927a505aSKowalski, Kamil } 21324a0cb85cSEd Tanous }; 21334a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 2134002d39b4SEd Tanous std::move(callback), "xyz.openbmc_project.Network", 2135002d39b4SEd Tanous std::string("/xyz/openbmc_project/network/") + ifaceId, 21364a0cb85cSEd Tanous "xyz.openbmc_project.Object.Delete", "Delete"); 21371abe55efSEd Tanous } 21381abe55efSEd Tanous else 21391abe55efSEd Tanous { 2140927a505aSKowalski, Kamil // ... otherwise return error 2141bf648f77SEd Tanous // TODO(Pawel)consider distinguish between non 2142bf648f77SEd Tanous // existing object, and other errors 2143bf648f77SEd Tanous messages::resourceNotFound(asyncResp->res, 2144d8a5d5d8SJiaqing Zhao "VLanNetworkInterface", ifaceId); 2145927a505aSKowalski, Kamil } 2146927a505aSKowalski, Kamil }); 2147bf648f77SEd Tanous }); 2148e439f0f8SKowalski, Kamil 2149bf648f77SEd Tanous BMCWEB_ROUTE(app, 2150bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 2151ed398213SEd Tanous 2152ed398213SEd Tanous .privileges(redfish::privileges::getVLanNetworkInterfaceCollection) 21531476687dSEd Tanous .methods(boost::beast::http::verb::get)( 21541476687dSEd Tanous [&app](const crow::Request& req, 2155bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2156bf648f77SEd Tanous const std::string& rootInterfaceName) { 21573ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 215845ca1b86SEd Tanous { 215945ca1b86SEd Tanous return; 216045ca1b86SEd Tanous } 21614a0cb85cSEd Tanous // Get eth interface list, and call the below callback for JSON 21621abe55efSEd Tanous // preparation 2163002d39b4SEd Tanous getEthernetIfaceList( 2164002d39b4SEd Tanous [asyncResp, rootInterfaceName]( 21651abe55efSEd Tanous const bool& success, 2166002d39b4SEd Tanous const boost::container::flat_set<std::string>& ifaceList) { 21674a0cb85cSEd Tanous if (!success) 21681abe55efSEd Tanous { 2169f12894f8SJason M. Bills messages::internalError(asyncResp->res); 21704a0cb85cSEd Tanous return; 21711abe55efSEd Tanous } 21724c9afe43SEd Tanous 217381ce609eSEd Tanous if (ifaceList.find(rootInterfaceName) == ifaceList.end()) 21744c9afe43SEd Tanous { 2175002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, 2176002d39b4SEd Tanous "VLanNetworkInterfaceCollection", 21774c9afe43SEd Tanous rootInterfaceName); 21784c9afe43SEd Tanous return; 21794c9afe43SEd Tanous } 21804c9afe43SEd Tanous 21810f74e643SEd Tanous asyncResp->res.jsonValue["@odata.type"] = 21820f74e643SEd Tanous "#VLanNetworkInterfaceCollection." 21830f74e643SEd Tanous "VLanNetworkInterfaceCollection"; 21840f74e643SEd Tanous asyncResp->res.jsonValue["Name"] = 21850f74e643SEd Tanous "VLAN Network Interface Collection"; 21864a0cb85cSEd Tanous 21872c70f800SEd Tanous nlohmann::json ifaceArray = nlohmann::json::array(); 21884a0cb85cSEd Tanous 218981ce609eSEd Tanous for (const std::string& ifaceItem : ifaceList) 21901abe55efSEd Tanous { 219111ba3979SEd Tanous if (ifaceItem.starts_with(rootInterfaceName + "_")) 21924a0cb85cSEd Tanous { 2193f23b7296SEd Tanous std::string path = 2194f23b7296SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/"; 2195f23b7296SEd Tanous path += rootInterfaceName; 2196f23b7296SEd Tanous path += "/VLANs/"; 2197f23b7296SEd Tanous path += ifaceItem; 21981476687dSEd Tanous nlohmann::json::object_t iface; 21991476687dSEd Tanous iface["@odata.id"] = std::move(path); 22001476687dSEd Tanous ifaceArray.push_back(std::move(iface)); 2201e439f0f8SKowalski, Kamil } 2202e439f0f8SKowalski, Kamil } 2203e439f0f8SKowalski, Kamil 2204002d39b4SEd Tanous asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 22052c70f800SEd Tanous asyncResp->res.jsonValue["Members"] = std::move(ifaceArray); 22064a0cb85cSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 22074a0cb85cSEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 22084a0cb85cSEd Tanous rootInterfaceName + "/VLANs"; 2209e439f0f8SKowalski, Kamil }); 2210bf648f77SEd Tanous }); 2211e439f0f8SKowalski, Kamil 2212bf648f77SEd Tanous BMCWEB_ROUTE(app, 2213bf648f77SEd Tanous "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/") 22143d768a16SAbhishek Patel .privileges(redfish::privileges::postVLanNetworkInterfaceCollection) 2215bf648f77SEd Tanous .methods(boost::beast::http::verb::post)( 221645ca1b86SEd Tanous [&app](const crow::Request& req, 2217bf648f77SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 2218bf648f77SEd Tanous const std::string& rootInterfaceName) { 22193ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 222045ca1b86SEd Tanous { 222145ca1b86SEd Tanous return; 222245ca1b86SEd Tanous } 2223fda13ad2SSunitha Harish bool vlanEnable = false; 22240627a2c7SEd Tanous uint32_t vlanId = 0; 2225002d39b4SEd Tanous if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId, 2226002d39b4SEd Tanous "VLANEnable", vlanEnable)) 22271abe55efSEd Tanous { 22284a0cb85cSEd Tanous return; 2229e439f0f8SKowalski, Kamil } 2230fda13ad2SSunitha Harish // Need both vlanId and vlanEnable to service this request 2231dbb59d4dSEd Tanous if (vlanId == 0U) 2232fda13ad2SSunitha Harish { 2233fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANId"); 2234fda13ad2SSunitha Harish } 2235fda13ad2SSunitha Harish if (!vlanEnable) 2236fda13ad2SSunitha Harish { 2237fda13ad2SSunitha Harish messages::propertyMissing(asyncResp->res, "VLANEnable"); 2238fda13ad2SSunitha Harish } 2239271584abSEd Tanous if (static_cast<bool>(vlanId) ^ vlanEnable) 2240fda13ad2SSunitha Harish { 2241fda13ad2SSunitha Harish return; 2242fda13ad2SSunitha Harish } 2243fda13ad2SSunitha Harish 2244002d39b4SEd Tanous auto callback = [asyncResp](const boost::system::error_code ec) { 22451abe55efSEd Tanous if (ec) 22461abe55efSEd Tanous { 2247bf648f77SEd Tanous // TODO(ed) make more consistent error messages 2248bf648f77SEd Tanous // based on phosphor-network responses 2249f12894f8SJason M. Bills messages::internalError(asyncResp->res); 22504a0cb85cSEd Tanous return; 22511abe55efSEd Tanous } 2252f12894f8SJason M. Bills messages::created(asyncResp->res); 2253e439f0f8SKowalski, Kamil }; 22544a0cb85cSEd Tanous crow::connections::systemBus->async_method_call( 22554a0cb85cSEd Tanous std::move(callback), "xyz.openbmc_project.Network", 22564a0cb85cSEd Tanous "/xyz/openbmc_project/network", 22574a0cb85cSEd Tanous "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 22580627a2c7SEd Tanous rootInterfaceName, vlanId); 2259bf648f77SEd Tanous }); 22604a0cb85cSEd Tanous } 2261bf648f77SEd Tanous 22629391bb9cSRapkiewicz, Pawel } // namespace redfish 2263