1d5afb2caSAndrew Geissler #pragma once 2d5afb2caSAndrew Geissler 33ccb3adbSEd Tanous #include "app.hpp" 43ccb3adbSEd Tanous #include "dbus_singleton.hpp" 57a1dbc48SGeorge Liu #include "dbus_utility.hpp" 63ccb3adbSEd Tanous #include "error_messages.hpp" 73ccb3adbSEd Tanous #include "ethernet.hpp" 83ccb3adbSEd Tanous #include "query.hpp" 93ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 10033f1e4dSEd Tanous #include "utils/ip_utils.hpp" 113ccb3adbSEd Tanous #include "utils/json_utils.hpp" 12033f1e4dSEd Tanous 13ef4c65b7SEd Tanous #include <boost/url/format.hpp> 141e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 15d5afb2caSAndrew Geissler 167a1dbc48SGeorge Liu #include <array> 17d5afb2caSAndrew Geissler #include <optional> 187a1dbc48SGeorge Liu #include <string_view> 19d5afb2caSAndrew Geissler #include <utility> 20d5afb2caSAndrew Geissler 2188a8a174SEd Tanous namespace redfish 22d5afb2caSAndrew Geissler { 23d5afb2caSAndrew Geissler 24d5afb2caSAndrew Geissler /** 25cc0bb6f2SAndrew Geissler * @brief Retrieves hypervisor state properties over dbus 26cc0bb6f2SAndrew Geissler * 27cc0bb6f2SAndrew Geissler * The hypervisor state object is optional so this function will only set the 28cc0bb6f2SAndrew Geissler * state variables if the object is found 29cc0bb6f2SAndrew Geissler * 30ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for completing asynchronous calls. 31cc0bb6f2SAndrew Geissler * 32cc0bb6f2SAndrew Geissler * @return None. 33cc0bb6f2SAndrew Geissler */ 34ac106bf6SEd Tanous inline void 35ac106bf6SEd Tanous getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 36cc0bb6f2SAndrew Geissler { 3762598e31SEd Tanous BMCWEB_LOG_DEBUG("Get hypervisor state information."); 381e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 391e1e598dSJonathan Doman *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor", 401e1e598dSJonathan Doman "/xyz/openbmc_project/state/hypervisor0", 411e1e598dSJonathan Doman "xyz.openbmc_project.State.Host", "CurrentHostState", 42ac106bf6SEd Tanous [asyncResp](const boost::system::error_code& ec, 431e1e598dSJonathan Doman const std::string& hostState) { 44cc0bb6f2SAndrew Geissler if (ec) 45cc0bb6f2SAndrew Geissler { 4662598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 47cc0bb6f2SAndrew Geissler // This is an optional D-Bus object so just return if 48cc0bb6f2SAndrew Geissler // error occurs 49cc0bb6f2SAndrew Geissler return; 50cc0bb6f2SAndrew Geissler } 51cc0bb6f2SAndrew Geissler 5262598e31SEd Tanous BMCWEB_LOG_DEBUG("Hypervisor state: {}", hostState); 53cc0bb6f2SAndrew Geissler // Verify Host State 541e1e598dSJonathan Doman if (hostState == "xyz.openbmc_project.State.Host.HostState.Running") 55cc0bb6f2SAndrew Geissler { 56ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "On"; 57ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 58cc0bb6f2SAndrew Geissler } 591e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 601e1e598dSJonathan Doman "Quiesced") 61cc0bb6f2SAndrew Geissler { 62ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "On"; 63ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Quiesced"; 64cc0bb6f2SAndrew Geissler } 651e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 661e1e598dSJonathan Doman "Standby") 67cc0bb6f2SAndrew Geissler { 68ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "On"; 69ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline"; 70cc0bb6f2SAndrew Geissler } 711e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 721e1e598dSJonathan Doman "TransitioningToRunning") 73cc0bb6f2SAndrew Geissler { 74ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "PoweringOn"; 75ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Starting"; 76cc0bb6f2SAndrew Geissler } 771e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 781e1e598dSJonathan Doman "TransitioningToOff") 79cc0bb6f2SAndrew Geissler { 80ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "PoweringOff"; 81ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 82cc0bb6f2SAndrew Geissler } 83002d39b4SEd Tanous else if (hostState == "xyz.openbmc_project.State.Host.HostState.Off") 84cc0bb6f2SAndrew Geissler { 85ac106bf6SEd Tanous asyncResp->res.jsonValue["PowerState"] = "Off"; 86ac106bf6SEd Tanous asyncResp->res.jsonValue["Status"]["State"] = "Disabled"; 87cc0bb6f2SAndrew Geissler } 88cc0bb6f2SAndrew Geissler else 89cc0bb6f2SAndrew Geissler { 90ac106bf6SEd Tanous messages::internalError(asyncResp->res); 91cc0bb6f2SAndrew Geissler return; 92cc0bb6f2SAndrew Geissler } 931e1e598dSJonathan Doman }); 94cc0bb6f2SAndrew Geissler } 95cc0bb6f2SAndrew Geissler 96cc0bb6f2SAndrew Geissler /** 974fbaf64aSAndrew Geissler * @brief Populate Actions if any are valid for hypervisor object 984fbaf64aSAndrew Geissler * 994fbaf64aSAndrew Geissler * The hypervisor state object is optional so this function will only set the 1004fbaf64aSAndrew Geissler * Action if the object is found 1014fbaf64aSAndrew Geissler * 102ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for completing asynchronous calls. 1034fbaf64aSAndrew Geissler * 1044fbaf64aSAndrew Geissler * @return None. 1054fbaf64aSAndrew Geissler */ 1068d1b46d7Szhanghch05 inline void 107ac106bf6SEd Tanous getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1084fbaf64aSAndrew Geissler { 10962598e31SEd Tanous BMCWEB_LOG_DEBUG("Get hypervisor actions."); 1102b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1112b73119cSGeorge Liu "xyz.openbmc_project.State.Host"}; 1122b73119cSGeorge Liu dbus::utility::getDbusObject( 1132b73119cSGeorge Liu "/xyz/openbmc_project/state/hypervisor0", interfaces, 114ac106bf6SEd Tanous [asyncResp]( 1152b73119cSGeorge Liu const boost::system::error_code& ec, 1164fbaf64aSAndrew Geissler const std::vector<std::pair<std::string, std::vector<std::string>>>& 1174fbaf64aSAndrew Geissler objInfo) { 1184fbaf64aSAndrew Geissler if (ec) 1194fbaf64aSAndrew Geissler { 12062598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 1214fbaf64aSAndrew Geissler // This is an optional D-Bus object so just return if 1224fbaf64aSAndrew Geissler // error occurs 1234fbaf64aSAndrew Geissler return; 1244fbaf64aSAndrew Geissler } 1254fbaf64aSAndrew Geissler 12626f6976fSEd Tanous if (objInfo.empty()) 1274fbaf64aSAndrew Geissler { 1284fbaf64aSAndrew Geissler // As noted above, this is an optional interface so just return 1294fbaf64aSAndrew Geissler // if there is no instance found 1304fbaf64aSAndrew Geissler return; 1314fbaf64aSAndrew Geissler } 1324fbaf64aSAndrew Geissler 1334fbaf64aSAndrew Geissler if (objInfo.size() > 1) 1344fbaf64aSAndrew Geissler { 1354fbaf64aSAndrew Geissler // More then one hypervisor object is not supported and is an 1364fbaf64aSAndrew Geissler // error 137ac106bf6SEd Tanous messages::internalError(asyncResp->res); 1384fbaf64aSAndrew Geissler return; 1394fbaf64aSAndrew Geissler } 1404fbaf64aSAndrew Geissler 1414fbaf64aSAndrew Geissler // Object present so system support limited ComputerSystem Action 142613dabeaSEd Tanous nlohmann::json& reset = 143ac106bf6SEd Tanous asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]; 144613dabeaSEd Tanous reset["target"] = 145613dabeaSEd Tanous "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset"; 146613dabeaSEd Tanous reset["@Redfish.ActionInfo"] = 147613dabeaSEd Tanous "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 1482b73119cSGeorge Liu }); 1494fbaf64aSAndrew Geissler } 1504fbaf64aSAndrew Geissler 151d5afb2caSAndrew Geissler inline bool extractHypervisorInterfaceData( 152711ac7a9SEd Tanous const std::string& ethIfaceId, 153711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 15477179532SEd Tanous EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config) 155d5afb2caSAndrew Geissler { 156d5afb2caSAndrew Geissler bool idFound = false; 157d5afb2caSAndrew Geissler for (const auto& objpath : dbusData) 158d5afb2caSAndrew Geissler { 159d5afb2caSAndrew Geissler for (const auto& ifacePair : objpath.second) 160d5afb2caSAndrew Geissler { 161d5afb2caSAndrew Geissler if (objpath.first == 162d5afb2caSAndrew Geissler "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId) 163d5afb2caSAndrew Geissler { 164d5afb2caSAndrew Geissler idFound = true; 165d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 166d5afb2caSAndrew Geissler { 167d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 168d5afb2caSAndrew Geissler { 169d5afb2caSAndrew Geissler if (propertyPair.first == "MACAddress") 170d5afb2caSAndrew Geissler { 171d5afb2caSAndrew Geissler const std::string* mac = 172d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 173d5afb2caSAndrew Geissler if (mac != nullptr) 174d5afb2caSAndrew Geissler { 17582695a5bSJiaqing Zhao ethData.macAddress = *mac; 176d5afb2caSAndrew Geissler } 177d5afb2caSAndrew Geissler } 178d5afb2caSAndrew Geissler } 179d5afb2caSAndrew Geissler } 180d5afb2caSAndrew Geissler else if (ifacePair.first == 181d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.EthernetInterface") 182d5afb2caSAndrew Geissler { 183d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 184d5afb2caSAndrew Geissler { 185d5afb2caSAndrew Geissler if (propertyPair.first == "DHCPEnabled") 186d5afb2caSAndrew Geissler { 187d5afb2caSAndrew Geissler const std::string* dhcp = 188d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 189d5afb2caSAndrew Geissler if (dhcp != nullptr) 190d5afb2caSAndrew Geissler { 19182695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcp; 192d5afb2caSAndrew Geissler break; // Interested on only "DHCPEnabled". 193d5afb2caSAndrew Geissler // Stop parsing since we got the 194d5afb2caSAndrew Geissler // "DHCPEnabled" value. 195d5afb2caSAndrew Geissler } 196d5afb2caSAndrew Geissler } 197d5afb2caSAndrew Geissler } 198d5afb2caSAndrew Geissler } 199d5afb2caSAndrew Geissler } 200d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" + 201d5afb2caSAndrew Geissler ethIfaceId + "/ipv4/addr0") 202d5afb2caSAndrew Geissler { 20377179532SEd Tanous IPv4AddressData& ipv4Address = ipv4Config.emplace_back(); 204d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Object.Enable") 205d5afb2caSAndrew Geissler { 2069eb808c1SEd Tanous for (const auto& property : ifacePair.second) 207d5afb2caSAndrew Geissler { 208d5afb2caSAndrew Geissler if (property.first == "Enabled") 209d5afb2caSAndrew Geissler { 210d5afb2caSAndrew Geissler const bool* intfEnable = 211d5afb2caSAndrew Geissler std::get_if<bool>(&property.second); 212d5afb2caSAndrew Geissler if (intfEnable != nullptr) 213d5afb2caSAndrew Geissler { 214d5afb2caSAndrew Geissler ipv4Address.isActive = *intfEnable; 215d5afb2caSAndrew Geissler break; 216d5afb2caSAndrew Geissler } 217d5afb2caSAndrew Geissler } 218d5afb2caSAndrew Geissler } 219d5afb2caSAndrew Geissler } 220d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Network.IP") 221d5afb2caSAndrew Geissler { 2229eb808c1SEd Tanous for (const auto& property : ifacePair.second) 223d5afb2caSAndrew Geissler { 224d5afb2caSAndrew Geissler if (property.first == "Address") 225d5afb2caSAndrew Geissler { 226d5afb2caSAndrew Geissler const std::string* address = 227d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 228d5afb2caSAndrew Geissler if (address != nullptr) 229d5afb2caSAndrew Geissler { 230d5afb2caSAndrew Geissler ipv4Address.address = *address; 231d5afb2caSAndrew Geissler } 232d5afb2caSAndrew Geissler } 233d5afb2caSAndrew Geissler else if (property.first == "Origin") 234d5afb2caSAndrew Geissler { 235d5afb2caSAndrew Geissler const std::string* origin = 236d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 237d5afb2caSAndrew Geissler if (origin != nullptr) 238d5afb2caSAndrew Geissler { 239d5afb2caSAndrew Geissler ipv4Address.origin = 240d5afb2caSAndrew Geissler translateAddressOriginDbusToRedfish(*origin, 241d5afb2caSAndrew Geissler true); 242d5afb2caSAndrew Geissler } 243d5afb2caSAndrew Geissler } 244d5afb2caSAndrew Geissler else if (property.first == "PrefixLength") 245d5afb2caSAndrew Geissler { 246d5afb2caSAndrew Geissler const uint8_t* mask = 247d5afb2caSAndrew Geissler std::get_if<uint8_t>(&property.second); 248d5afb2caSAndrew Geissler if (mask != nullptr) 249d5afb2caSAndrew Geissler { 250d5afb2caSAndrew Geissler // convert it to the string 251d5afb2caSAndrew Geissler ipv4Address.netmask = getNetmask(*mask); 252d5afb2caSAndrew Geissler } 253d5afb2caSAndrew Geissler } 254889ff694SAsmitha Karunanithi else if (property.first == "Type" || 255889ff694SAsmitha Karunanithi property.first == "Gateway") 256889ff694SAsmitha Karunanithi { 257889ff694SAsmitha Karunanithi // Type & Gateway is not used 258889ff694SAsmitha Karunanithi continue; 259889ff694SAsmitha Karunanithi } 260d5afb2caSAndrew Geissler else 261d5afb2caSAndrew Geissler { 26262598e31SEd Tanous BMCWEB_LOG_ERROR( 26362598e31SEd Tanous "Got extra property: {} on the {} object", 26462598e31SEd Tanous property.first, objpath.first.str); 265d5afb2caSAndrew Geissler } 266d5afb2caSAndrew Geissler } 267d5afb2caSAndrew Geissler } 268d5afb2caSAndrew Geissler } 269d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor") 270d5afb2caSAndrew Geissler { 271d5afb2caSAndrew Geissler // System configuration shows up in the global namespace, so no 272d5afb2caSAndrew Geissler // need to check eth number 273d5afb2caSAndrew Geissler if (ifacePair.first == 274d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.SystemConfiguration") 275d5afb2caSAndrew Geissler { 276d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 277d5afb2caSAndrew Geissler { 278d5afb2caSAndrew Geissler if (propertyPair.first == "HostName") 279d5afb2caSAndrew Geissler { 280d5afb2caSAndrew Geissler const std::string* hostName = 281d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 282d5afb2caSAndrew Geissler if (hostName != nullptr) 283d5afb2caSAndrew Geissler { 28482695a5bSJiaqing Zhao ethData.hostName = *hostName; 285d5afb2caSAndrew Geissler } 286d5afb2caSAndrew Geissler } 287d5afb2caSAndrew Geissler else if (propertyPair.first == "DefaultGateway") 288d5afb2caSAndrew Geissler { 289d5afb2caSAndrew Geissler const std::string* defaultGateway = 290d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 291d5afb2caSAndrew Geissler if (defaultGateway != nullptr) 292d5afb2caSAndrew Geissler { 29382695a5bSJiaqing Zhao ethData.defaultGateway = *defaultGateway; 294d5afb2caSAndrew Geissler } 295d5afb2caSAndrew Geissler } 296d5afb2caSAndrew Geissler } 297d5afb2caSAndrew Geissler } 298d5afb2caSAndrew Geissler } 299d5afb2caSAndrew Geissler } 300d5afb2caSAndrew Geissler } 301d5afb2caSAndrew Geissler return idFound; 302d5afb2caSAndrew Geissler } 303d5afb2caSAndrew Geissler /** 304d5afb2caSAndrew Geissler * Function that retrieves all properties for given Hypervisor Ethernet 305d5afb2caSAndrew Geissler * Interface Object from Settings Manager 306d5afb2caSAndrew Geissler * @param ethIfaceId Hypervisor ethernet interface id to query on DBus 307d5afb2caSAndrew Geissler * @param callback a function that shall be called to convert Dbus output 308d5afb2caSAndrew Geissler * into JSON 309d5afb2caSAndrew Geissler */ 310d5afb2caSAndrew Geissler template <typename CallbackFunc> 311d5afb2caSAndrew Geissler void getHypervisorIfaceData(const std::string& ethIfaceId, 312d5afb2caSAndrew Geissler CallbackFunc&& callback) 313d5afb2caSAndrew Geissler { 3145eb468daSGeorge Liu sdbusplus::message::object_path path("/"); 3155eb468daSGeorge Liu dbus::utility::getManagedObjects( 3165eb468daSGeorge Liu "xyz.openbmc_project.Settings", path, 317f94c4ecfSEd Tanous [ethIfaceId{std::string{ethIfaceId}}, 3188cb2c024SEd Tanous callback = std::forward<CallbackFunc>(callback)]( 3198b24275dSEd Tanous const boost::system::error_code& ec, 32021fe928bSEd Tanous const dbus::utility::ManagedObjectType& resp) mutable { 321d5afb2caSAndrew Geissler EthernetInterfaceData ethData{}; 32277179532SEd Tanous std::vector<IPv4AddressData> ipv4Data; 3238b24275dSEd Tanous if (ec) 324d5afb2caSAndrew Geissler { 325d5afb2caSAndrew Geissler callback(false, ethData, ipv4Data); 326d5afb2caSAndrew Geissler return; 327d5afb2caSAndrew Geissler } 328d5afb2caSAndrew Geissler 32989492a15SPatrick Williams bool found = extractHypervisorInterfaceData(ethIfaceId, resp, ethData, 33089492a15SPatrick Williams ipv4Data); 331d5afb2caSAndrew Geissler if (!found) 332d5afb2caSAndrew Geissler { 33362598e31SEd Tanous BMCWEB_LOG_INFO("Hypervisor Interface not found"); 334d5afb2caSAndrew Geissler } 335d5afb2caSAndrew Geissler callback(found, ethData, ipv4Data); 3365eb468daSGeorge Liu }); 337d5afb2caSAndrew Geissler } 338d5afb2caSAndrew Geissler 339d5afb2caSAndrew Geissler /** 340d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface IPAddress DBUS 341d5afb2caSAndrew Geissler * 342ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 343d5afb2caSAndrew Geissler * @param[in] ipv4Address Address from the incoming request 344d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 345d5afb2caSAndrew Geissler * 346d5afb2caSAndrew Geissler * @return None. 347d5afb2caSAndrew Geissler */ 348ac106bf6SEd Tanous inline void setHypervisorIPv4Address( 349ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 350ac106bf6SEd Tanous const std::string& ethIfaceId, const std::string& ipv4Address) 351d5afb2caSAndrew Geissler { 35262598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}", 35362598e31SEd Tanous ipv4Address, ethIfaceId); 354d82b5e1fSAsmitha Karunanithi 355*e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/Address", 356*e93abac6SGinu George "xyz.openbmc_project.Settings", 357d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + 358d82b5e1fSAsmitha Karunanithi "/ipv4/addr0", 359*e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Address", ipv4Address); 360d5afb2caSAndrew Geissler } 361d5afb2caSAndrew Geissler 362d5afb2caSAndrew Geissler /** 363d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface SubnetMask DBUS 364d5afb2caSAndrew Geissler * 365ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 366d5afb2caSAndrew Geissler * @param[in] subnet SubnetMask from the incoming request 367d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 368d5afb2caSAndrew Geissler * 369d5afb2caSAndrew Geissler * @return None. 370d5afb2caSAndrew Geissler */ 3718d1b46d7Szhanghch05 inline void 372ac106bf6SEd Tanous setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3738d1b46d7Szhanghch05 const std::string& ethIfaceId, const uint8_t subnet) 374d5afb2caSAndrew Geissler { 37562598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet, 37662598e31SEd Tanous ethIfaceId); 377d5afb2caSAndrew Geissler 378*e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/SubnetMask", 379*e93abac6SGinu George "xyz.openbmc_project.Settings", 380d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + 381d82b5e1fSAsmitha Karunanithi "/ipv4/addr0", 382*e93abac6SGinu George "xyz.openbmc_project.Network.IP", "PrefixLength", subnet); 383d5afb2caSAndrew Geissler } 384d5afb2caSAndrew Geissler 385d5afb2caSAndrew Geissler /** 386d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface Gateway DBUS 387d5afb2caSAndrew Geissler * 388ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 389d5afb2caSAndrew Geissler * @param[in] gateway Gateway from the incoming request 390d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 391d5afb2caSAndrew Geissler * 392d5afb2caSAndrew Geissler * @return None. 393d5afb2caSAndrew Geissler */ 394ac106bf6SEd Tanous inline void setHypervisorIPv4Gateway( 395ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 396d5afb2caSAndrew Geissler const std::string& gateway) 397d5afb2caSAndrew Geissler { 39862598e31SEd Tanous BMCWEB_LOG_DEBUG( 39962598e31SEd Tanous "Setting the DefaultGateway to the last configured gateway"); 400d5afb2caSAndrew Geissler 401*e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/Gateway", 402*e93abac6SGinu George "xyz.openbmc_project.Settings", 403d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 404d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 405d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 406*e93abac6SGinu George "DefaultGateway", gateway); 407d5afb2caSAndrew Geissler } 408d5afb2caSAndrew Geissler 409d5afb2caSAndrew Geissler /** 410d5afb2caSAndrew Geissler * @brief Creates a static IPv4 entry 411d5afb2caSAndrew Geissler * 412d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 413d5afb2caSAndrew Geissler * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 414d5afb2caSAndrew Geissler * @param[in] gateway IPv4 address of this interfaces gateway 415d5afb2caSAndrew Geissler * @param[in] address IPv4 address to assign to this interface 416d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 417d5afb2caSAndrew Geissler * 418d5afb2caSAndrew Geissler * @return None 419d5afb2caSAndrew Geissler */ 4208d1b46d7Szhanghch05 inline void 4218d1b46d7Szhanghch05 createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength, 4228d1b46d7Szhanghch05 const std::string& gateway, const std::string& address, 4238d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 424d5afb2caSAndrew Geissler { 425d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 426d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 427d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 428d5afb2caSAndrew Geissler } 429d5afb2caSAndrew Geissler 430d5afb2caSAndrew Geissler /** 431d5afb2caSAndrew Geissler * @brief Deletes given IPv4 interface 432d5afb2caSAndrew Geissler * 433d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface whose IP should be deleted 434d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 435d5afb2caSAndrew Geissler * 436d5afb2caSAndrew Geissler * @return None 437d5afb2caSAndrew Geissler */ 4388d1b46d7Szhanghch05 inline void 4398d1b46d7Szhanghch05 deleteHypervisorIPv4(const std::string& ifaceId, 4408d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 441d5afb2caSAndrew Geissler { 442d5afb2caSAndrew Geissler std::string address = "0.0.0.0"; 443d5afb2caSAndrew Geissler std::string gateway = "0.0.0.0"; 444d5afb2caSAndrew Geissler const uint8_t prefixLength = 0; 445d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 446d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 447d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 448d5afb2caSAndrew Geissler } 449d5afb2caSAndrew Geissler 45077179532SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 45177179532SEd Tanous const std::string& ifaceId, 452d5afb2caSAndrew Geissler const EthernetInterfaceData& ethData, 45377179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) 454d5afb2caSAndrew Geissler { 455d5afb2caSAndrew Geissler jsonResponse["Id"] = ifaceId; 456ef4c65b7SEd Tanous jsonResponse["@odata.id"] = boost::urls::format( 457ef4c65b7SEd Tanous "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId); 458d5afb2caSAndrew Geissler jsonResponse["InterfaceEnabled"] = true; 45982695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 460d5afb2caSAndrew Geissler 46182695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 462d5afb2caSAndrew Geissler jsonResponse["DHCPv4"]["DHCPEnabled"] = 46382695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 464d5afb2caSAndrew Geissler 465d5afb2caSAndrew Geissler nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 466d5afb2caSAndrew Geissler nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 467d5afb2caSAndrew Geissler ipv4Array = nlohmann::json::array(); 468d5afb2caSAndrew Geissler ipv4StaticArray = nlohmann::json::array(); 4699eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 470d5afb2caSAndrew Geissler { 471d5afb2caSAndrew Geissler if (ipv4Config.isActive) 472d5afb2caSAndrew Geissler { 4731476687dSEd Tanous nlohmann::json::object_t ipv4; 4741476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 4751476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 4761476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 4771476687dSEd Tanous ipv4["Gateway"] = ethData.defaultGateway; 478d5afb2caSAndrew Geissler 479d5afb2caSAndrew Geissler if (ipv4Config.origin == "Static") 480d5afb2caSAndrew Geissler { 4811476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 482d5afb2caSAndrew Geissler } 483b2ba3072SPatrick Williams ipv4Array.emplace_back(std::move(ipv4)); 484d5afb2caSAndrew Geissler } 485d5afb2caSAndrew Geissler } 486d5afb2caSAndrew Geissler } 487d5afb2caSAndrew Geissler 48877179532SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled, 4898d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 490d5afb2caSAndrew Geissler { 4917e860f15SJohn Edward Broadbent const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false); 492d82b5e1fSAsmitha Karunanithi 493*e93abac6SGinu George setDbusProperty( 494*e93abac6SGinu George asyncResp, "DHCPv4/DHCPEnabled", "xyz.openbmc_project.Settings", 495d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 496d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor") / 497d82b5e1fSAsmitha Karunanithi ifaceId, 498*e93abac6SGinu George "xyz.openbmc_project.Network.EthernetInterface", "DHCPEnabled", dhcp); 499d5afb2caSAndrew Geissler 500d5afb2caSAndrew Geissler // Set the IPv4 address origin to the DHCP / Static as per the new value 501d5afb2caSAndrew Geissler // of the DHCPEnabled property 502d5afb2caSAndrew Geissler std::string origin; 503e05aec50SEd Tanous if (!ipv4DHCPEnabled) 504d5afb2caSAndrew Geissler { 505d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 506d5afb2caSAndrew Geissler } 507d5afb2caSAndrew Geissler else 508d5afb2caSAndrew Geissler { 509d5afb2caSAndrew Geissler // DHCPEnabled is set to true. Delete the current IPv4 settings 510d5afb2caSAndrew Geissler // to receive the new values from DHCP server. 511d5afb2caSAndrew Geissler deleteHypervisorIPv4(ifaceId, asyncResp); 512d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 513d5afb2caSAndrew Geissler } 514d82b5e1fSAsmitha Karunanithi 515*e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/AddressOrigin", 516*e93abac6SGinu George "xyz.openbmc_project.Settings", 517d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor/" + ifaceId + 518d82b5e1fSAsmitha Karunanithi "/ipv4/addr0", 519*e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Origin", origin); 520d5afb2caSAndrew Geissler } 521d5afb2caSAndrew Geissler 5227e860f15SJohn Edward Broadbent inline void handleHypervisorIPv4StaticPatch( 52321fe928bSEd Tanous const std::string& ifaceId, 52421fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input, 5257e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 526d5afb2caSAndrew Geissler { 5277e860f15SJohn Edward Broadbent // Hypervisor considers the first IP address in the array list 5287e860f15SJohn Edward Broadbent // as the Hypervisor's virtual management interface supports single IPv4 5297e860f15SJohn Edward Broadbent // address 53021fe928bSEd Tanous std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson = input[0]; 53121fe928bSEd Tanous nlohmann::json::object_t* obj = 53221fe928bSEd Tanous std::get_if<nlohmann::json::object_t>(&thisJson); 53321fe928bSEd Tanous if (obj == nullptr) 5347e860f15SJohn Edward Broadbent { 53521fe928bSEd Tanous deleteHypervisorIPv4(ifaceId, asyncResp); 53621fe928bSEd Tanous return; 53721fe928bSEd Tanous } 53821fe928bSEd Tanous if (obj->empty()) 53921fe928bSEd Tanous { 54021fe928bSEd Tanous return; 54121fe928bSEd Tanous } 542f8fe53e7SEd Tanous // For the error string 543f8fe53e7SEd Tanous std::string pathString = "IPv4StaticAddresses/1"; 54421fe928bSEd Tanous std::string address; 54521fe928bSEd Tanous std::string subnetMask; 54621fe928bSEd Tanous std::string gateway; 54721fe928bSEd Tanous if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address, 54821fe928bSEd Tanous "SubnetMask", subnetMask, "Gateway", 5497e860f15SJohn Edward Broadbent gateway)) 5507e860f15SJohn Edward Broadbent { 5517e860f15SJohn Edward Broadbent return; 5527e860f15SJohn Edward Broadbent } 5537e860f15SJohn Edward Broadbent 5547e860f15SJohn Edward Broadbent uint8_t prefixLength = 0; 55521fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(address)) 5567e860f15SJohn Edward Broadbent { 55721fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, address, 5587e860f15SJohn Edward Broadbent pathString + "/Address"); 55921fe928bSEd Tanous return; 5607e860f15SJohn Edward Broadbent } 5617e860f15SJohn Edward Broadbent 56221fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(subnetMask, &prefixLength)) 5637e860f15SJohn Edward Broadbent { 56421fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, subnetMask, 5657e860f15SJohn Edward Broadbent pathString + "/SubnetMask"); 56621fe928bSEd Tanous return; 5677e860f15SJohn Edward Broadbent } 5687e860f15SJohn Edward Broadbent 56921fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(gateway)) 5707e860f15SJohn Edward Broadbent { 57121fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, gateway, 5727e860f15SJohn Edward Broadbent pathString + "/Gateway"); 5737e860f15SJohn Edward Broadbent return; 5747e860f15SJohn Edward Broadbent } 5757e860f15SJohn Edward Broadbent 57662598e31SEd Tanous BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId, 57721fe928bSEd Tanous address); 57821fe928bSEd Tanous createHypervisorIPv4(ifaceId, prefixLength, gateway, address, asyncResp); 5797e860f15SJohn Edward Broadbent // Set the DHCPEnabled to false since the Static IPv4 is set 5807e860f15SJohn Edward Broadbent setDHCPEnabled(ifaceId, false, asyncResp); 5817e860f15SJohn Edward Broadbent } 5827e860f15SJohn Edward Broadbent 58388a8a174SEd Tanous inline void handleHypervisorHostnamePatch( 58488a8a174SEd Tanous const std::string& hostName, 5857e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 5867e860f15SJohn Edward Broadbent { 5877e860f15SJohn Edward Broadbent if (!isHostnameValid(hostName)) 5887e860f15SJohn Edward Broadbent { 5897e860f15SJohn Edward Broadbent messages::propertyValueFormatError(asyncResp->res, hostName, 5907e860f15SJohn Edward Broadbent "HostName"); 5917e860f15SJohn Edward Broadbent return; 5927e860f15SJohn Edward Broadbent } 5937e860f15SJohn Edward Broadbent 5947e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HostName"] = hostName; 595*e93abac6SGinu George setDbusProperty(asyncResp, "HostName", "xyz.openbmc_project.Settings", 596d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 597d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 598d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 599*e93abac6SGinu George "HostName", hostName); 6007e860f15SJohn Edward Broadbent } 6017e860f15SJohn Edward Broadbent 6027e860f15SJohn Edward Broadbent inline void 60377179532SEd Tanous setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive, 6047e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6057e860f15SJohn Edward Broadbent { 606*e93abac6SGinu George setDbusProperty( 607*e93abac6SGinu George asyncResp, "InterfaceEnabled", "xyz.openbmc_project.Settings", 608*e93abac6SGinu George "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0", 609*e93abac6SGinu George "xyz.openbmc_project.Object.Enable", "Enabled", isActive); 6107e860f15SJohn Edward Broadbent } 6117e860f15SJohn Edward Broadbent 612f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceCollectionGet( 613f40448e5SGunnar Mills App& app, const crow::Request& req, 614f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 615f40448e5SGunnar Mills { 616f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 617f40448e5SGunnar Mills { 618f40448e5SGunnar Mills return; 619f40448e5SGunnar Mills } 620f40448e5SGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 621f40448e5SGunnar Mills "xyz.openbmc_project.Network.EthernetInterface"}; 622f40448e5SGunnar Mills 623f40448e5SGunnar Mills dbus::utility::getSubTreePaths( 624f40448e5SGunnar Mills "/xyz/openbmc_project/network/hypervisor", 0, interfaces, 625f40448e5SGunnar Mills [asyncResp]( 6268b24275dSEd Tanous const boost::system::error_code& ec, 627f40448e5SGunnar Mills const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) { 6288b24275dSEd Tanous if (ec) 629f40448e5SGunnar Mills { 630f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "System", "hypervisor"); 631f40448e5SGunnar Mills return; 632f40448e5SGunnar Mills } 633f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 634f40448e5SGunnar Mills "#EthernetInterfaceCollection." 635f40448e5SGunnar Mills "EthernetInterfaceCollection"; 636f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 637f40448e5SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 638f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet " 639f40448e5SGunnar Mills "Interface Collection"; 640f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 641f40448e5SGunnar Mills "Collection of Virtual Management " 642f40448e5SGunnar Mills "Interfaces for the hypervisor"; 643f40448e5SGunnar Mills 644f40448e5SGunnar Mills nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 645f40448e5SGunnar Mills ifaceArray = nlohmann::json::array(); 646f40448e5SGunnar Mills for (const std::string& iface : ifaceList) 647f40448e5SGunnar Mills { 648f40448e5SGunnar Mills sdbusplus::message::object_path path(iface); 649f40448e5SGunnar Mills std::string name = path.filename(); 650f40448e5SGunnar Mills if (name.empty()) 651f40448e5SGunnar Mills { 652f40448e5SGunnar Mills continue; 653f40448e5SGunnar Mills } 654f40448e5SGunnar Mills nlohmann::json::object_t ethIface; 655ef4c65b7SEd Tanous ethIface["@odata.id"] = boost::urls::format( 656ef4c65b7SEd Tanous "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", name); 657b2ba3072SPatrick Williams ifaceArray.emplace_back(std::move(ethIface)); 658f40448e5SGunnar Mills } 659f40448e5SGunnar Mills asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 660f40448e5SGunnar Mills }); 661f40448e5SGunnar Mills } 662f40448e5SGunnar Mills 663f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceGet( 664f40448e5SGunnar Mills App& app, const crow::Request& req, 665f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 666f40448e5SGunnar Mills { 667f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 668f40448e5SGunnar Mills { 669f40448e5SGunnar Mills return; 670f40448e5SGunnar Mills } 671f40448e5SGunnar Mills getHypervisorIfaceData( 672f40448e5SGunnar Mills id, [asyncResp, ifaceId{std::string(id)}]( 67377179532SEd Tanous bool success, const EthernetInterfaceData& ethData, 67477179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) { 675f40448e5SGunnar Mills if (!success) 676f40448e5SGunnar Mills { 677f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 678f40448e5SGunnar Mills ifaceId); 679f40448e5SGunnar Mills return; 680f40448e5SGunnar Mills } 681f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 68293bbc953SJiaqing Zhao "#EthernetInterface.v1_9_0.EthernetInterface"; 683f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface"; 684f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 685f40448e5SGunnar Mills "Hypervisor's Virtual Management Ethernet Interface"; 686f40448e5SGunnar Mills parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData, 687f40448e5SGunnar Mills ipv4Data); 688f40448e5SGunnar Mills }); 689f40448e5SGunnar Mills } 690f40448e5SGunnar Mills 6918d8c30c3SGunnar Mills inline void handleHypervisorSystemGet( 6928d8c30c3SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6938d8c30c3SGunnar Mills { 6948d8c30c3SGunnar Mills sdbusplus::asio::getProperty<std::string>( 6958d8c30c3SGunnar Mills *crow::connections::systemBus, "xyz.openbmc_project.Settings", 6968d8c30c3SGunnar Mills "/xyz/openbmc_project/network/hypervisor", 6978d8c30c3SGunnar Mills "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 6988d8c30c3SGunnar Mills [asyncResp](const boost::system::error_code& ec, 6998d8c30c3SGunnar Mills const std::string& /*hostName*/) { 7008d8c30c3SGunnar Mills if (ec) 7018d8c30c3SGunnar Mills { 7028d8c30c3SGunnar Mills messages::resourceNotFound(asyncResp->res, "System", "hypervisor"); 7038d8c30c3SGunnar Mills return; 7048d8c30c3SGunnar Mills } 70562598e31SEd Tanous BMCWEB_LOG_DEBUG("Hypervisor is available"); 7068d8c30c3SGunnar Mills 7078d8c30c3SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 7088d8c30c3SGunnar Mills "#ComputerSystem.v1_6_0.ComputerSystem"; 7098d8c30c3SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 7108d8c30c3SGunnar Mills "/redfish/v1/Systems/hypervisor"; 7118d8c30c3SGunnar Mills asyncResp->res.jsonValue["Description"] = "Hypervisor"; 7128d8c30c3SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor"; 7138d8c30c3SGunnar Mills asyncResp->res.jsonValue["Id"] = "hypervisor"; 7148d8c30c3SGunnar Mills asyncResp->res.jsonValue["SystemType"] = "OS"; 7158d8c30c3SGunnar Mills nlohmann::json::array_t managedBy; 7168d8c30c3SGunnar Mills nlohmann::json::object_t manager; 717253f11b8SEd Tanous manager["@odata.id"] = boost::urls::format( 718253f11b8SEd Tanous "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME); 719ad539545SPatrick Williams managedBy.emplace_back(std::move(manager)); 7208d8c30c3SGunnar Mills asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy); 7218d8c30c3SGunnar Mills asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] = 7228d8c30c3SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 7238d8c30c3SGunnar Mills getHypervisorState(asyncResp); 7248d8c30c3SGunnar Mills getHypervisorActions(asyncResp); 7258d8c30c3SGunnar Mills // TODO: Add "SystemType" : "hypervisor" 7268d8c30c3SGunnar Mills }); 7278d8c30c3SGunnar Mills } 7288d8c30c3SGunnar Mills 729f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfacePatch( 730f40448e5SGunnar Mills App& app, const crow::Request& req, 731f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 732f40448e5SGunnar Mills const std::string& ifaceId) 733f40448e5SGunnar Mills { 734f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 735f40448e5SGunnar Mills { 736f40448e5SGunnar Mills return; 737f40448e5SGunnar Mills } 738f40448e5SGunnar Mills std::optional<std::string> hostName; 73921fe928bSEd Tanous std::optional< 74021fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>> 74121fe928bSEd Tanous ipv4StaticAddresses; 74221fe928bSEd Tanous std::optional<std::vector<nlohmann::json::object_t>> ipv4Addresses; 743f40448e5SGunnar Mills std::optional<bool> ipv4DHCPEnabled; 744f40448e5SGunnar Mills 745f40448e5SGunnar Mills if (!json_util::readJsonPatch(req, asyncResp->res, "HostName", hostName, 746f40448e5SGunnar Mills "IPv4StaticAddresses", ipv4StaticAddresses, 74721fe928bSEd Tanous "IPv4Addresses", ipv4Addresses, 74821fe928bSEd Tanous "DHCPv4/DHCPEnabled", ipv4DHCPEnabled)) 749f40448e5SGunnar Mills { 750f40448e5SGunnar Mills return; 751f40448e5SGunnar Mills } 752f40448e5SGunnar Mills 753f40448e5SGunnar Mills if (ipv4Addresses) 754f40448e5SGunnar Mills { 755f40448e5SGunnar Mills messages::propertyNotWritable(asyncResp->res, "IPv4Addresses"); 756f40448e5SGunnar Mills return; 757f40448e5SGunnar Mills } 758f40448e5SGunnar Mills 759f40448e5SGunnar Mills getHypervisorIfaceData( 76021fe928bSEd Tanous ifaceId, 76121fe928bSEd Tanous [asyncResp, ifaceId, hostName = std::move(hostName), 7625a39f77aSPatrick Williams ipv4StaticAddresses = std::move(ipv4StaticAddresses), 76321fe928bSEd Tanous ipv4DHCPEnabled](bool success, const EthernetInterfaceData& ethData, 76421fe928bSEd Tanous const std::vector<IPv4AddressData>&) mutable { 765f40448e5SGunnar Mills if (!success) 766f40448e5SGunnar Mills { 767f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 768f40448e5SGunnar Mills ifaceId); 769f40448e5SGunnar Mills return; 770f40448e5SGunnar Mills } 771f40448e5SGunnar Mills 772f40448e5SGunnar Mills if (ipv4StaticAddresses) 773f40448e5SGunnar Mills { 77421fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& 77521fe928bSEd Tanous ipv4Static = *ipv4StaticAddresses; 776f40448e5SGunnar Mills if (ipv4Static.begin() == ipv4Static.end()) 777f40448e5SGunnar Mills { 77821fe928bSEd Tanous messages::propertyValueTypeError(asyncResp->res, 77921fe928bSEd Tanous std::vector<std::string>(), 780f40448e5SGunnar Mills "IPv4StaticAddresses"); 781f40448e5SGunnar Mills return; 782f40448e5SGunnar Mills } 783f40448e5SGunnar Mills 784f40448e5SGunnar Mills // One and only one hypervisor instance supported 785f40448e5SGunnar Mills if (ipv4Static.size() != 1) 786f40448e5SGunnar Mills { 78721fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, "[]", 788f40448e5SGunnar Mills "IPv4StaticAddresses"); 789f40448e5SGunnar Mills return; 790f40448e5SGunnar Mills } 791f40448e5SGunnar Mills 79221fe928bSEd Tanous std::variant<nlohmann::json::object_t, std::nullptr_t>& ipv4Json = 79321fe928bSEd Tanous ipv4Static[0]; 794f40448e5SGunnar Mills // Check if the param is 'null'. If its null, it means 795f40448e5SGunnar Mills // that user wants to delete the IP address. Deleting 796f40448e5SGunnar Mills // the IP address is allowed only if its statically 797f40448e5SGunnar Mills // configured. Deleting the address originated from DHCP 798f40448e5SGunnar Mills // is not allowed. 79921fe928bSEd Tanous if (std::holds_alternative<std::nullptr_t>(ipv4Json) && 80021fe928bSEd Tanous translateDhcpEnabledToBool(ethData.dhcpEnabled, true)) 801f40448e5SGunnar Mills { 80262598e31SEd Tanous BMCWEB_LOG_INFO("Ignoring the delete on ipv4StaticAddresses " 80362598e31SEd Tanous "as the interface is DHCP enabled"); 804f40448e5SGunnar Mills } 805f40448e5SGunnar Mills else 806f40448e5SGunnar Mills { 807f40448e5SGunnar Mills handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, asyncResp); 808f40448e5SGunnar Mills } 809f40448e5SGunnar Mills } 810f40448e5SGunnar Mills 811f40448e5SGunnar Mills if (hostName) 812f40448e5SGunnar Mills { 813f40448e5SGunnar Mills handleHypervisorHostnamePatch(*hostName, asyncResp); 814f40448e5SGunnar Mills } 815f40448e5SGunnar Mills 81621fe928bSEd Tanous if (ipv4DHCPEnabled) 817f40448e5SGunnar Mills { 818f40448e5SGunnar Mills setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp); 819f40448e5SGunnar Mills } 820f40448e5SGunnar Mills 821f40448e5SGunnar Mills // Set this interface to disabled/inactive. This will be set 822f40448e5SGunnar Mills // to enabled/active by the pldm once the hypervisor 823f40448e5SGunnar Mills // consumes the updated settings from the user. 824f40448e5SGunnar Mills setIPv4InterfaceEnabled(ifaceId, false, asyncResp); 825f40448e5SGunnar Mills }); 826f40448e5SGunnar Mills asyncResp->res.result(boost::beast::http::status::accepted); 827f40448e5SGunnar Mills } 828f40448e5SGunnar Mills 829a5d0cedbSGunnar Mills inline void handleHypervisorResetActionGet( 830a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 831a5d0cedbSGunnar Mills { 832a5d0cedbSGunnar Mills // Only return action info if hypervisor D-Bus object present 833a5d0cedbSGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 834a5d0cedbSGunnar Mills "xyz.openbmc_project.State.Host"}; 835a5d0cedbSGunnar Mills dbus::utility::getDbusObject( 836a5d0cedbSGunnar Mills "/xyz/openbmc_project/state/hypervisor0", interfaces, 837a5d0cedbSGunnar Mills [asyncResp]( 838a5d0cedbSGunnar Mills const boost::system::error_code& ec, 839a5d0cedbSGunnar Mills const std::vector<std::pair<std::string, std::vector<std::string>>>& 840a5d0cedbSGunnar Mills objInfo) { 841a5d0cedbSGunnar Mills if (ec) 842a5d0cedbSGunnar Mills { 84362598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 844a5d0cedbSGunnar Mills 845a5d0cedbSGunnar Mills // No hypervisor objects found by mapper 846a5d0cedbSGunnar Mills if (ec.value() == boost::system::errc::io_error) 847a5d0cedbSGunnar Mills { 848a5d0cedbSGunnar Mills messages::resourceNotFound(asyncResp->res, "hypervisor", 849a5d0cedbSGunnar Mills "ResetActionInfo"); 850a5d0cedbSGunnar Mills return; 851a5d0cedbSGunnar Mills } 852a5d0cedbSGunnar Mills 853a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 854a5d0cedbSGunnar Mills return; 855a5d0cedbSGunnar Mills } 856a5d0cedbSGunnar Mills 857a5d0cedbSGunnar Mills // One and only one hypervisor instance supported 858a5d0cedbSGunnar Mills if (objInfo.size() != 1) 859a5d0cedbSGunnar Mills { 860a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 861a5d0cedbSGunnar Mills return; 862a5d0cedbSGunnar Mills } 863a5d0cedbSGunnar Mills 864a5d0cedbSGunnar Mills // The hypervisor object only support the ability to 865a5d0cedbSGunnar Mills // turn On The system object Action should be utilized 866a5d0cedbSGunnar Mills // for other operations 867a5d0cedbSGunnar Mills 868a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 869a5d0cedbSGunnar Mills "#ActionInfo.v1_1_2.ActionInfo"; 870a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 871a5d0cedbSGunnar Mills "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 872a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Name"] = "Reset Action Info"; 873a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Id"] = "ResetActionInfo"; 874a5d0cedbSGunnar Mills nlohmann::json::array_t parameters; 875a5d0cedbSGunnar Mills nlohmann::json::object_t parameter; 876a5d0cedbSGunnar Mills parameter["Name"] = "ResetType"; 877a5d0cedbSGunnar Mills parameter["Required"] = true; 878a5d0cedbSGunnar Mills parameter["DataType"] = "String"; 879a5d0cedbSGunnar Mills nlohmann::json::array_t allowed; 880ad539545SPatrick Williams allowed.emplace_back("On"); 881a5d0cedbSGunnar Mills parameter["AllowableValues"] = std::move(allowed); 882ad539545SPatrick Williams parameters.emplace_back(std::move(parameter)); 883a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Parameters"] = std::move(parameters); 884a5d0cedbSGunnar Mills }); 885a5d0cedbSGunnar Mills } 886a5d0cedbSGunnar Mills 887a5d0cedbSGunnar Mills inline void handleHypervisorSystemResetPost( 888a5d0cedbSGunnar Mills App& app, const crow::Request& req, 889a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 890a5d0cedbSGunnar Mills { 891a5d0cedbSGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 892a5d0cedbSGunnar Mills { 893a5d0cedbSGunnar Mills return; 894a5d0cedbSGunnar Mills } 895a5d0cedbSGunnar Mills std::optional<std::string> resetType; 896a5d0cedbSGunnar Mills if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType)) 897a5d0cedbSGunnar Mills { 898a5d0cedbSGunnar Mills // readJson adds appropriate error to response 899a5d0cedbSGunnar Mills return; 900a5d0cedbSGunnar Mills } 901a5d0cedbSGunnar Mills 902a5d0cedbSGunnar Mills if (!resetType) 903a5d0cedbSGunnar Mills { 904a5d0cedbSGunnar Mills messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset", 905a5d0cedbSGunnar Mills "ResetType"); 906a5d0cedbSGunnar Mills return; 907a5d0cedbSGunnar Mills } 908a5d0cedbSGunnar Mills 909a5d0cedbSGunnar Mills // Hypervisor object only support On operation 910a5d0cedbSGunnar Mills if (resetType != "On") 911a5d0cedbSGunnar Mills { 912a5d0cedbSGunnar Mills messages::propertyValueNotInList(asyncResp->res, *resetType, 913a5d0cedbSGunnar Mills "ResetType"); 914a5d0cedbSGunnar Mills return; 915a5d0cedbSGunnar Mills } 916a5d0cedbSGunnar Mills 917a5d0cedbSGunnar Mills std::string command = "xyz.openbmc_project.State.Host.Transition.On"; 918a5d0cedbSGunnar Mills 9191827b4f1SAsmitha Karunanithi setDbusPropertyAction(asyncResp, "xyz.openbmc_project.State.Hypervisor", 9201827b4f1SAsmitha Karunanithi sdbusplus::message::object_path( 9211827b4f1SAsmitha Karunanithi "/xyz/openbmc_project/state/hypervisor0"), 9221827b4f1SAsmitha Karunanithi "xyz.openbmc_project.State.Host", 9231827b4f1SAsmitha Karunanithi "RequestedHostTransition", "ResetType", 9241827b4f1SAsmitha Karunanithi "ComputerSystem.Reset", command); 925a5d0cedbSGunnar Mills } 926a5d0cedbSGunnar Mills 9277e860f15SJohn Edward Broadbent inline void requestRoutesHypervisorSystems(App& app) 9287e860f15SJohn Edward Broadbent { 9297e860f15SJohn Edward Broadbent /** 9307e860f15SJohn Edward Broadbent * HypervisorInterfaceCollection class to handle the GET and PATCH on 9317e860f15SJohn Edward Broadbent * Hypervisor Interface 9327e860f15SJohn Edward Broadbent */ 9337e860f15SJohn Edward Broadbent 9347e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/") 935ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 936f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 937f40448e5SGunnar Mills handleHypervisorEthernetInterfaceCollectionGet, std::ref(app))); 9387e860f15SJohn Edward Broadbent 9397e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9407e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 941ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 942f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 943f40448e5SGunnar Mills handleHypervisorEthernetInterfaceGet, std::ref(app))); 944d5afb2caSAndrew Geissler 9457e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9467e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 947ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 948f40448e5SGunnar Mills .methods(boost::beast::http::verb::patch)(std::bind_front( 949f40448e5SGunnar Mills handleHypervisorEthernetInterfacePatch, std::ref(app))); 9504fbaf64aSAndrew Geissler 9517e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9524fbaf64aSAndrew Geissler "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset/") 953ed398213SEd Tanous .privileges(redfish::privileges::postComputerSystem) 9547e860f15SJohn Edward Broadbent .methods(boost::beast::http::verb::post)( 955a5d0cedbSGunnar Mills std::bind_front(handleHypervisorSystemResetPost, std::ref(app))); 9564fbaf64aSAndrew Geissler } 95788a8a174SEd Tanous } // namespace redfish 958