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" 8539d8c6bSEd Tanous #include "generated/enums/action_info.hpp" 9539d8c6bSEd Tanous #include "generated/enums/computer_system.hpp" 10539d8c6bSEd Tanous #include "generated/enums/resource.hpp" 113ccb3adbSEd Tanous #include "query.hpp" 123ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 13033f1e4dSEd Tanous #include "utils/ip_utils.hpp" 143ccb3adbSEd Tanous #include "utils/json_utils.hpp" 15033f1e4dSEd Tanous 16ef4c65b7SEd Tanous #include <boost/url/format.hpp> 171e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp> 18d5afb2caSAndrew Geissler 197a1dbc48SGeorge Liu #include <array> 20d5afb2caSAndrew Geissler #include <optional> 217a1dbc48SGeorge Liu #include <string_view> 22d5afb2caSAndrew Geissler #include <utility> 23d5afb2caSAndrew Geissler 2488a8a174SEd Tanous namespace redfish 25d5afb2caSAndrew Geissler { 26d5afb2caSAndrew Geissler 27d5afb2caSAndrew Geissler /** 28cc0bb6f2SAndrew Geissler * @brief Retrieves hypervisor state properties over dbus 29cc0bb6f2SAndrew Geissler * 30cc0bb6f2SAndrew Geissler * The hypervisor state object is optional so this function will only set the 31cc0bb6f2SAndrew Geissler * state variables if the object is found 32cc0bb6f2SAndrew Geissler * 33ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for completing asynchronous calls. 34cc0bb6f2SAndrew Geissler * 35cc0bb6f2SAndrew Geissler * @return None. 36cc0bb6f2SAndrew Geissler */ 37ac106bf6SEd Tanous inline void 38ac106bf6SEd Tanous getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 39cc0bb6f2SAndrew Geissler { 4062598e31SEd Tanous BMCWEB_LOG_DEBUG("Get hypervisor state information."); 41*deae6a78SEd Tanous dbus::utility::getProperty<std::string>( 42*deae6a78SEd Tanous "xyz.openbmc_project.State.Hypervisor", 431e1e598dSJonathan Doman "/xyz/openbmc_project/state/hypervisor0", 441e1e598dSJonathan Doman "xyz.openbmc_project.State.Host", "CurrentHostState", 45ac106bf6SEd Tanous [asyncResp](const boost::system::error_code& ec, 461e1e598dSJonathan Doman const std::string& hostState) { 47cc0bb6f2SAndrew Geissler if (ec) 48cc0bb6f2SAndrew Geissler { 4962598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 50cc0bb6f2SAndrew Geissler // This is an optional D-Bus object so just return if 51cc0bb6f2SAndrew Geissler // error occurs 52cc0bb6f2SAndrew Geissler return; 53cc0bb6f2SAndrew Geissler } 54cc0bb6f2SAndrew Geissler 5562598e31SEd Tanous BMCWEB_LOG_DEBUG("Hypervisor state: {}", hostState); 56cc0bb6f2SAndrew Geissler // Verify Host State 571e1e598dSJonathan Doman if (hostState == "xyz.openbmc_project.State.Host.HostState.Running") 58cc0bb6f2SAndrew Geissler { 59bd79bce8SPatrick Williams asyncResp->res.jsonValue["PowerState"] = 60bd79bce8SPatrick Williams resource::PowerState::On; 61539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 62539d8c6bSEd Tanous resource::State::Enabled; 63cc0bb6f2SAndrew Geissler } 641e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 651e1e598dSJonathan Doman "Quiesced") 66cc0bb6f2SAndrew Geissler { 67bd79bce8SPatrick Williams asyncResp->res.jsonValue["PowerState"] = 68bd79bce8SPatrick Williams resource::PowerState::On; 69539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 70539d8c6bSEd Tanous resource::State::Quiesced; 71cc0bb6f2SAndrew Geissler } 721e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 731e1e598dSJonathan Doman "Standby") 74cc0bb6f2SAndrew Geissler { 75bd79bce8SPatrick Williams asyncResp->res.jsonValue["PowerState"] = 76bd79bce8SPatrick Williams resource::PowerState::On; 77539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 78539d8c6bSEd Tanous resource::State::StandbyOffline; 79cc0bb6f2SAndrew Geissler } 801e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 811e1e598dSJonathan Doman "TransitioningToRunning") 82cc0bb6f2SAndrew Geissler { 83539d8c6bSEd Tanous asyncResp->res.jsonValue["PowerState"] = 84539d8c6bSEd Tanous resource::PowerState::PoweringOn; 85539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 86539d8c6bSEd Tanous resource::State::Starting; 87cc0bb6f2SAndrew Geissler } 881e1e598dSJonathan Doman else if (hostState == "xyz.openbmc_project.State.Host.HostState." 891e1e598dSJonathan Doman "TransitioningToOff") 90cc0bb6f2SAndrew Geissler { 91539d8c6bSEd Tanous asyncResp->res.jsonValue["PowerState"] = 92539d8c6bSEd Tanous resource::PowerState::PoweringOff; 93539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 94539d8c6bSEd Tanous resource::State::Enabled; 95cc0bb6f2SAndrew Geissler } 96bd79bce8SPatrick Williams else if (hostState == 97bd79bce8SPatrick Williams "xyz.openbmc_project.State.Host.HostState.Off") 98cc0bb6f2SAndrew Geissler { 99bd79bce8SPatrick Williams asyncResp->res.jsonValue["PowerState"] = 100bd79bce8SPatrick Williams resource::PowerState::Off; 101539d8c6bSEd Tanous asyncResp->res.jsonValue["Status"]["State"] = 102539d8c6bSEd Tanous resource::State::Disabled; 103cc0bb6f2SAndrew Geissler } 104cc0bb6f2SAndrew Geissler else 105cc0bb6f2SAndrew Geissler { 106ac106bf6SEd Tanous messages::internalError(asyncResp->res); 107cc0bb6f2SAndrew Geissler return; 108cc0bb6f2SAndrew Geissler } 1091e1e598dSJonathan Doman }); 110cc0bb6f2SAndrew Geissler } 111cc0bb6f2SAndrew Geissler 112cc0bb6f2SAndrew Geissler /** 1134fbaf64aSAndrew Geissler * @brief Populate Actions if any are valid for hypervisor object 1144fbaf64aSAndrew Geissler * 1154fbaf64aSAndrew Geissler * The hypervisor state object is optional so this function will only set the 1164fbaf64aSAndrew Geissler * Action if the object is found 1174fbaf64aSAndrew Geissler * 118ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for completing asynchronous calls. 1194fbaf64aSAndrew Geissler * 1204fbaf64aSAndrew Geissler * @return None. 1214fbaf64aSAndrew Geissler */ 1228d1b46d7Szhanghch05 inline void 123ac106bf6SEd Tanous getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1244fbaf64aSAndrew Geissler { 12562598e31SEd Tanous BMCWEB_LOG_DEBUG("Get hypervisor actions."); 1262b73119cSGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 1272b73119cSGeorge Liu "xyz.openbmc_project.State.Host"}; 1282b73119cSGeorge Liu dbus::utility::getDbusObject( 1292b73119cSGeorge Liu "/xyz/openbmc_project/state/hypervisor0", interfaces, 130ac106bf6SEd Tanous [asyncResp]( 1312b73119cSGeorge Liu const boost::system::error_code& ec, 1324fbaf64aSAndrew Geissler const std::vector<std::pair<std::string, std::vector<std::string>>>& 1334fbaf64aSAndrew Geissler objInfo) { 1344fbaf64aSAndrew Geissler if (ec) 1354fbaf64aSAndrew Geissler { 13662598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 1374fbaf64aSAndrew Geissler // This is an optional D-Bus object so just return if 1384fbaf64aSAndrew Geissler // error occurs 1394fbaf64aSAndrew Geissler return; 1404fbaf64aSAndrew Geissler } 1414fbaf64aSAndrew Geissler 14226f6976fSEd Tanous if (objInfo.empty()) 1434fbaf64aSAndrew Geissler { 1444fbaf64aSAndrew Geissler // As noted above, this is an optional interface so just return 1454fbaf64aSAndrew Geissler // if there is no instance found 1464fbaf64aSAndrew Geissler return; 1474fbaf64aSAndrew Geissler } 1484fbaf64aSAndrew Geissler 1494fbaf64aSAndrew Geissler if (objInfo.size() > 1) 1504fbaf64aSAndrew Geissler { 1514fbaf64aSAndrew Geissler // More then one hypervisor object is not supported and is an 1524fbaf64aSAndrew Geissler // error 153ac106bf6SEd Tanous messages::internalError(asyncResp->res); 1544fbaf64aSAndrew Geissler return; 1554fbaf64aSAndrew Geissler } 1564fbaf64aSAndrew Geissler 1574fbaf64aSAndrew Geissler // Object present so system support limited ComputerSystem Action 158613dabeaSEd Tanous nlohmann::json& reset = 159ac106bf6SEd Tanous asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]; 160613dabeaSEd Tanous reset["target"] = 161613dabeaSEd Tanous "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset"; 162613dabeaSEd Tanous reset["@Redfish.ActionInfo"] = 163613dabeaSEd Tanous "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 1642b73119cSGeorge Liu }); 1654fbaf64aSAndrew Geissler } 1664fbaf64aSAndrew Geissler 167d5afb2caSAndrew Geissler inline bool extractHypervisorInterfaceData( 168711ac7a9SEd Tanous const std::string& ethIfaceId, 169711ac7a9SEd Tanous const dbus::utility::ManagedObjectType& dbusData, 17077179532SEd Tanous EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config) 171d5afb2caSAndrew Geissler { 172d5afb2caSAndrew Geissler bool idFound = false; 173d5afb2caSAndrew Geissler for (const auto& objpath : dbusData) 174d5afb2caSAndrew Geissler { 175d5afb2caSAndrew Geissler for (const auto& ifacePair : objpath.second) 176d5afb2caSAndrew Geissler { 177d5afb2caSAndrew Geissler if (objpath.first == 178d5afb2caSAndrew Geissler "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId) 179d5afb2caSAndrew Geissler { 180d5afb2caSAndrew Geissler idFound = true; 1814652c640SAsmitha Karunanithi if (ifacePair.first == 182d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.EthernetInterface") 183d5afb2caSAndrew Geissler { 184d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 185d5afb2caSAndrew Geissler { 186d5afb2caSAndrew Geissler if (propertyPair.first == "DHCPEnabled") 187d5afb2caSAndrew Geissler { 188d5afb2caSAndrew Geissler const std::string* dhcp = 189d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 190d5afb2caSAndrew Geissler if (dhcp != nullptr) 191d5afb2caSAndrew Geissler { 19282695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcp; 193d5afb2caSAndrew Geissler break; // Interested on only "DHCPEnabled". 194d5afb2caSAndrew Geissler // Stop parsing since we got the 195d5afb2caSAndrew Geissler // "DHCPEnabled" value. 196d5afb2caSAndrew Geissler } 197d5afb2caSAndrew Geissler } 198d5afb2caSAndrew Geissler } 199d5afb2caSAndrew Geissler } 200d5afb2caSAndrew Geissler } 201d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" + 202d5afb2caSAndrew Geissler ethIfaceId + "/ipv4/addr0") 203d5afb2caSAndrew Geissler { 20477179532SEd Tanous IPv4AddressData& ipv4Address = ipv4Config.emplace_back(); 205d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Object.Enable") 206d5afb2caSAndrew Geissler { 2079eb808c1SEd Tanous for (const auto& property : ifacePair.second) 208d5afb2caSAndrew Geissler { 209d5afb2caSAndrew Geissler if (property.first == "Enabled") 210d5afb2caSAndrew Geissler { 211d5afb2caSAndrew Geissler const bool* intfEnable = 212d5afb2caSAndrew Geissler std::get_if<bool>(&property.second); 213d5afb2caSAndrew Geissler if (intfEnable != nullptr) 214d5afb2caSAndrew Geissler { 215d5afb2caSAndrew Geissler ipv4Address.isActive = *intfEnable; 216d5afb2caSAndrew Geissler break; 217d5afb2caSAndrew Geissler } 218d5afb2caSAndrew Geissler } 219d5afb2caSAndrew Geissler } 220d5afb2caSAndrew Geissler } 221d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Network.IP") 222d5afb2caSAndrew Geissler { 2239eb808c1SEd Tanous for (const auto& property : ifacePair.second) 224d5afb2caSAndrew Geissler { 225d5afb2caSAndrew Geissler if (property.first == "Address") 226d5afb2caSAndrew Geissler { 227d5afb2caSAndrew Geissler const std::string* address = 228d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 229d5afb2caSAndrew Geissler if (address != nullptr) 230d5afb2caSAndrew Geissler { 231d5afb2caSAndrew Geissler ipv4Address.address = *address; 232d5afb2caSAndrew Geissler } 233d5afb2caSAndrew Geissler } 234d5afb2caSAndrew Geissler else if (property.first == "Origin") 235d5afb2caSAndrew Geissler { 236d5afb2caSAndrew Geissler const std::string* origin = 237d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 238d5afb2caSAndrew Geissler if (origin != nullptr) 239d5afb2caSAndrew Geissler { 240d5afb2caSAndrew Geissler ipv4Address.origin = 241d5afb2caSAndrew Geissler translateAddressOriginDbusToRedfish(*origin, 242d5afb2caSAndrew Geissler true); 243d5afb2caSAndrew Geissler } 244d5afb2caSAndrew Geissler } 245d5afb2caSAndrew Geissler else if (property.first == "PrefixLength") 246d5afb2caSAndrew Geissler { 247d5afb2caSAndrew Geissler const uint8_t* mask = 248d5afb2caSAndrew Geissler std::get_if<uint8_t>(&property.second); 249d5afb2caSAndrew Geissler if (mask != nullptr) 250d5afb2caSAndrew Geissler { 251d5afb2caSAndrew Geissler // convert it to the string 252d5afb2caSAndrew Geissler ipv4Address.netmask = getNetmask(*mask); 253d5afb2caSAndrew Geissler } 254d5afb2caSAndrew Geissler } 255889ff694SAsmitha Karunanithi else if (property.first == "Type" || 256889ff694SAsmitha Karunanithi property.first == "Gateway") 257889ff694SAsmitha Karunanithi { 258889ff694SAsmitha Karunanithi // Type & Gateway is not used 259889ff694SAsmitha Karunanithi continue; 260889ff694SAsmitha Karunanithi } 261d5afb2caSAndrew Geissler else 262d5afb2caSAndrew Geissler { 26362598e31SEd Tanous BMCWEB_LOG_ERROR( 26462598e31SEd Tanous "Got extra property: {} on the {} object", 26562598e31SEd Tanous property.first, objpath.first.str); 266d5afb2caSAndrew Geissler } 267d5afb2caSAndrew Geissler } 268d5afb2caSAndrew Geissler } 269d5afb2caSAndrew Geissler } 270d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor") 271d5afb2caSAndrew Geissler { 272d5afb2caSAndrew Geissler // System configuration shows up in the global namespace, so no 273d5afb2caSAndrew Geissler // need to check eth number 274d5afb2caSAndrew Geissler if (ifacePair.first == 275d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.SystemConfiguration") 276d5afb2caSAndrew Geissler { 277d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 278d5afb2caSAndrew Geissler { 279d5afb2caSAndrew Geissler if (propertyPair.first == "HostName") 280d5afb2caSAndrew Geissler { 281d5afb2caSAndrew Geissler const std::string* hostName = 282d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 283d5afb2caSAndrew Geissler if (hostName != nullptr) 284d5afb2caSAndrew Geissler { 28582695a5bSJiaqing Zhao ethData.hostName = *hostName; 286d5afb2caSAndrew Geissler } 287d5afb2caSAndrew Geissler } 288d5afb2caSAndrew Geissler else if (propertyPair.first == "DefaultGateway") 289d5afb2caSAndrew Geissler { 290d5afb2caSAndrew Geissler const std::string* defaultGateway = 291d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 292d5afb2caSAndrew Geissler if (defaultGateway != nullptr) 293d5afb2caSAndrew Geissler { 29482695a5bSJiaqing Zhao ethData.defaultGateway = *defaultGateway; 295d5afb2caSAndrew Geissler } 296d5afb2caSAndrew Geissler } 297d5afb2caSAndrew Geissler } 298d5afb2caSAndrew Geissler } 299d5afb2caSAndrew Geissler } 300d5afb2caSAndrew Geissler } 301d5afb2caSAndrew Geissler } 302d5afb2caSAndrew Geissler return idFound; 303d5afb2caSAndrew Geissler } 304d5afb2caSAndrew Geissler /** 305d5afb2caSAndrew Geissler * Function that retrieves all properties for given Hypervisor Ethernet 306d5afb2caSAndrew Geissler * Interface Object from Settings Manager 307d5afb2caSAndrew Geissler * @param ethIfaceId Hypervisor ethernet interface id to query on DBus 308d5afb2caSAndrew Geissler * @param callback a function that shall be called to convert Dbus output 309d5afb2caSAndrew Geissler * into JSON 310d5afb2caSAndrew Geissler */ 311d5afb2caSAndrew Geissler template <typename CallbackFunc> 312d5afb2caSAndrew Geissler void getHypervisorIfaceData(const std::string& ethIfaceId, 313d5afb2caSAndrew Geissler CallbackFunc&& callback) 314d5afb2caSAndrew Geissler { 3155eb468daSGeorge Liu sdbusplus::message::object_path path("/"); 3165eb468daSGeorge Liu dbus::utility::getManagedObjects( 3175eb468daSGeorge Liu "xyz.openbmc_project.Settings", path, 318f94c4ecfSEd Tanous [ethIfaceId{std::string{ethIfaceId}}, 3198cb2c024SEd Tanous callback = std::forward<CallbackFunc>(callback)]( 3208b24275dSEd Tanous const boost::system::error_code& ec, 32121fe928bSEd Tanous const dbus::utility::ManagedObjectType& resp) mutable { 322d5afb2caSAndrew Geissler EthernetInterfaceData ethData{}; 32377179532SEd Tanous std::vector<IPv4AddressData> ipv4Data; 3248b24275dSEd Tanous if (ec) 325d5afb2caSAndrew Geissler { 326d5afb2caSAndrew Geissler callback(false, ethData, ipv4Data); 327d5afb2caSAndrew Geissler return; 328d5afb2caSAndrew Geissler } 329d5afb2caSAndrew Geissler 330bd79bce8SPatrick Williams bool found = extractHypervisorInterfaceData(ethIfaceId, resp, 331bd79bce8SPatrick Williams ethData, ipv4Data); 332d5afb2caSAndrew Geissler if (!found) 333d5afb2caSAndrew Geissler { 33462598e31SEd Tanous BMCWEB_LOG_INFO("Hypervisor Interface not found"); 335d5afb2caSAndrew Geissler } 336d5afb2caSAndrew Geissler callback(found, ethData, ipv4Data); 3375eb468daSGeorge Liu }); 338d5afb2caSAndrew Geissler } 339d5afb2caSAndrew Geissler 340d5afb2caSAndrew Geissler /** 341d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface IPAddress DBUS 342d5afb2caSAndrew Geissler * 343ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 344d5afb2caSAndrew Geissler * @param[in] ipv4Address Address from the incoming request 345d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 346d5afb2caSAndrew Geissler * 347d5afb2caSAndrew Geissler * @return None. 348d5afb2caSAndrew Geissler */ 349ac106bf6SEd Tanous inline void setHypervisorIPv4Address( 350ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 351ac106bf6SEd Tanous const std::string& ethIfaceId, const std::string& ipv4Address) 352d5afb2caSAndrew Geissler { 35362598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}", 35462598e31SEd Tanous ipv4Address, ethIfaceId); 355d82b5e1fSAsmitha Karunanithi 356bd79bce8SPatrick Williams setDbusProperty( 357bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/Address", 358e93abac6SGinu George "xyz.openbmc_project.Settings", 359bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0", 360e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Address", ipv4Address); 361d5afb2caSAndrew Geissler } 362d5afb2caSAndrew Geissler 363d5afb2caSAndrew Geissler /** 364d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface SubnetMask DBUS 365d5afb2caSAndrew Geissler * 366ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 367d5afb2caSAndrew Geissler * @param[in] subnet SubnetMask from the incoming request 368d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 369d5afb2caSAndrew Geissler * 370d5afb2caSAndrew Geissler * @return None. 371d5afb2caSAndrew Geissler */ 3728d1b46d7Szhanghch05 inline void 373ac106bf6SEd Tanous setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3748d1b46d7Szhanghch05 const std::string& ethIfaceId, const uint8_t subnet) 375d5afb2caSAndrew Geissler { 37662598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet, 37762598e31SEd Tanous ethIfaceId); 378d5afb2caSAndrew Geissler 379bd79bce8SPatrick Williams setDbusProperty( 380bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/SubnetMask", 381e93abac6SGinu George "xyz.openbmc_project.Settings", 382bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0", 383e93abac6SGinu George "xyz.openbmc_project.Network.IP", "PrefixLength", subnet); 384d5afb2caSAndrew Geissler } 385d5afb2caSAndrew Geissler 386d5afb2caSAndrew Geissler /** 387d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface Gateway DBUS 388d5afb2caSAndrew Geissler * 389ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 390d5afb2caSAndrew Geissler * @param[in] gateway Gateway from the incoming request 391d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 392d5afb2caSAndrew Geissler * 393d5afb2caSAndrew Geissler * @return None. 394d5afb2caSAndrew Geissler */ 395ac106bf6SEd Tanous inline void setHypervisorIPv4Gateway( 396ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 397d5afb2caSAndrew Geissler const std::string& gateway) 398d5afb2caSAndrew Geissler { 39962598e31SEd Tanous BMCWEB_LOG_DEBUG( 40062598e31SEd Tanous "Setting the DefaultGateway to the last configured gateway"); 401d5afb2caSAndrew Geissler 402e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/Gateway", 403e93abac6SGinu George "xyz.openbmc_project.Settings", 404d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 405d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 406d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 407e93abac6SGinu George "DefaultGateway", gateway); 408d5afb2caSAndrew Geissler } 409d5afb2caSAndrew Geissler 410d5afb2caSAndrew Geissler /** 411d5afb2caSAndrew Geissler * @brief Creates a static IPv4 entry 412d5afb2caSAndrew Geissler * 413d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 414d5afb2caSAndrew Geissler * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 415d5afb2caSAndrew Geissler * @param[in] gateway IPv4 address of this interfaces gateway 416d5afb2caSAndrew Geissler * @param[in] address IPv4 address to assign to this interface 417d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 418d5afb2caSAndrew Geissler * 419d5afb2caSAndrew Geissler * @return None 420d5afb2caSAndrew Geissler */ 4218d1b46d7Szhanghch05 inline void 4228d1b46d7Szhanghch05 createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength, 4238d1b46d7Szhanghch05 const std::string& gateway, const std::string& address, 4248d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 425d5afb2caSAndrew Geissler { 426d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 427d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 428d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 429d5afb2caSAndrew Geissler } 430d5afb2caSAndrew Geissler 431d5afb2caSAndrew Geissler /** 432d5afb2caSAndrew Geissler * @brief Deletes given IPv4 interface 433d5afb2caSAndrew Geissler * 434d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface whose IP should be deleted 435d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 436d5afb2caSAndrew Geissler * 437d5afb2caSAndrew Geissler * @return None 438d5afb2caSAndrew Geissler */ 4398d1b46d7Szhanghch05 inline void 4408d1b46d7Szhanghch05 deleteHypervisorIPv4(const std::string& ifaceId, 4418d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 442d5afb2caSAndrew Geissler { 443d5afb2caSAndrew Geissler std::string address = "0.0.0.0"; 444d5afb2caSAndrew Geissler std::string gateway = "0.0.0.0"; 445d5afb2caSAndrew Geissler const uint8_t prefixLength = 0; 446d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 447d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 448d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 449d5afb2caSAndrew Geissler } 450d5afb2caSAndrew Geissler 45177179532SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 45277179532SEd Tanous const std::string& ifaceId, 453d5afb2caSAndrew Geissler const EthernetInterfaceData& ethData, 45477179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) 455d5afb2caSAndrew Geissler { 456d5afb2caSAndrew Geissler jsonResponse["Id"] = ifaceId; 457ef4c65b7SEd Tanous jsonResponse["@odata.id"] = boost::urls::format( 458ef4c65b7SEd Tanous "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId); 459d5afb2caSAndrew Geissler jsonResponse["InterfaceEnabled"] = true; 46082695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 461d5afb2caSAndrew Geissler jsonResponse["DHCPv4"]["DHCPEnabled"] = 46282695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 463d5afb2caSAndrew Geissler 464d5afb2caSAndrew Geissler nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 465d5afb2caSAndrew Geissler nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 466d5afb2caSAndrew Geissler ipv4Array = nlohmann::json::array(); 467d5afb2caSAndrew Geissler ipv4StaticArray = nlohmann::json::array(); 4689eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 469d5afb2caSAndrew Geissler { 470d5afb2caSAndrew Geissler if (ipv4Config.isActive) 471d5afb2caSAndrew Geissler { 4721476687dSEd Tanous nlohmann::json::object_t ipv4; 4731476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 4741476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 4751476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 4761476687dSEd Tanous ipv4["Gateway"] = ethData.defaultGateway; 477d5afb2caSAndrew Geissler 478d5afb2caSAndrew Geissler if (ipv4Config.origin == "Static") 479d5afb2caSAndrew Geissler { 4801476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 481d5afb2caSAndrew Geissler } 482b2ba3072SPatrick Williams ipv4Array.emplace_back(std::move(ipv4)); 483d5afb2caSAndrew Geissler } 484d5afb2caSAndrew Geissler } 485d5afb2caSAndrew Geissler } 486d5afb2caSAndrew Geissler 48777179532SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled, 4888d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 489d5afb2caSAndrew Geissler { 4907e860f15SJohn Edward Broadbent const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false); 491d82b5e1fSAsmitha Karunanithi 492e93abac6SGinu George setDbusProperty( 493e93abac6SGinu George asyncResp, "DHCPv4/DHCPEnabled", "xyz.openbmc_project.Settings", 494d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 495d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor") / 496d82b5e1fSAsmitha Karunanithi ifaceId, 497e93abac6SGinu George "xyz.openbmc_project.Network.EthernetInterface", "DHCPEnabled", dhcp); 498d5afb2caSAndrew Geissler 499d5afb2caSAndrew Geissler // Set the IPv4 address origin to the DHCP / Static as per the new value 500d5afb2caSAndrew Geissler // of the DHCPEnabled property 501d5afb2caSAndrew Geissler std::string origin; 502e05aec50SEd Tanous if (!ipv4DHCPEnabled) 503d5afb2caSAndrew Geissler { 504d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 505d5afb2caSAndrew Geissler } 506d5afb2caSAndrew Geissler else 507d5afb2caSAndrew Geissler { 508d5afb2caSAndrew Geissler // DHCPEnabled is set to true. Delete the current IPv4 settings 509d5afb2caSAndrew Geissler // to receive the new values from DHCP server. 510d5afb2caSAndrew Geissler deleteHypervisorIPv4(ifaceId, asyncResp); 511d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 512d5afb2caSAndrew Geissler } 513d82b5e1fSAsmitha Karunanithi 514bd79bce8SPatrick Williams setDbusProperty( 515bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/AddressOrigin", 516e93abac6SGinu George "xyz.openbmc_project.Settings", 517bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0", 518e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Origin", origin); 519d5afb2caSAndrew Geissler } 520d5afb2caSAndrew Geissler 5217e860f15SJohn Edward Broadbent inline void handleHypervisorIPv4StaticPatch( 52221fe928bSEd Tanous const std::string& ifaceId, 52321fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input, 5247e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 525d5afb2caSAndrew Geissler { 5267e860f15SJohn Edward Broadbent // Hypervisor considers the first IP address in the array list 5277e860f15SJohn Edward Broadbent // as the Hypervisor's virtual management interface supports single IPv4 5287e860f15SJohn Edward Broadbent // address 52921fe928bSEd Tanous std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson = input[0]; 53021fe928bSEd Tanous nlohmann::json::object_t* obj = 53121fe928bSEd Tanous std::get_if<nlohmann::json::object_t>(&thisJson); 53221fe928bSEd Tanous if (obj == nullptr) 5337e860f15SJohn Edward Broadbent { 53421fe928bSEd Tanous deleteHypervisorIPv4(ifaceId, asyncResp); 53521fe928bSEd Tanous return; 53621fe928bSEd Tanous } 53721fe928bSEd Tanous if (obj->empty()) 53821fe928bSEd Tanous { 53921fe928bSEd Tanous return; 54021fe928bSEd Tanous } 541f8fe53e7SEd Tanous // For the error string 542f8fe53e7SEd Tanous std::string pathString = "IPv4StaticAddresses/1"; 54321fe928bSEd Tanous std::string address; 54421fe928bSEd Tanous std::string subnetMask; 54521fe928bSEd Tanous std::string gateway; 546afc474aeSMyung Bae if (!json_util::readJsonObject( // 547afc474aeSMyung Bae *obj, asyncResp->res, // 548afc474aeSMyung Bae "Address", address, // 549afc474aeSMyung Bae "Gateway", gateway, // 550afc474aeSMyung Bae "SubnetMask", subnetMask // 551afc474aeSMyung Bae )) 5527e860f15SJohn Edward Broadbent { 5537e860f15SJohn Edward Broadbent return; 5547e860f15SJohn Edward Broadbent } 5557e860f15SJohn Edward Broadbent 5567e860f15SJohn Edward Broadbent uint8_t prefixLength = 0; 55721fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(address)) 5587e860f15SJohn Edward Broadbent { 55921fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, address, 5607e860f15SJohn Edward Broadbent pathString + "/Address"); 56121fe928bSEd Tanous return; 5627e860f15SJohn Edward Broadbent } 5637e860f15SJohn Edward Broadbent 56421fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(subnetMask, &prefixLength)) 5657e860f15SJohn Edward Broadbent { 56621fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, subnetMask, 5677e860f15SJohn Edward Broadbent pathString + "/SubnetMask"); 56821fe928bSEd Tanous return; 5697e860f15SJohn Edward Broadbent } 5707e860f15SJohn Edward Broadbent 57121fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(gateway)) 5727e860f15SJohn Edward Broadbent { 57321fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, gateway, 5747e860f15SJohn Edward Broadbent pathString + "/Gateway"); 5757e860f15SJohn Edward Broadbent return; 5767e860f15SJohn Edward Broadbent } 5777e860f15SJohn Edward Broadbent 57862598e31SEd Tanous BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId, 57921fe928bSEd Tanous address); 58021fe928bSEd Tanous createHypervisorIPv4(ifaceId, prefixLength, gateway, address, asyncResp); 5817e860f15SJohn Edward Broadbent // Set the DHCPEnabled to false since the Static IPv4 is set 5827e860f15SJohn Edward Broadbent setDHCPEnabled(ifaceId, false, asyncResp); 5837e860f15SJohn Edward Broadbent } 5847e860f15SJohn Edward Broadbent 58588a8a174SEd Tanous inline void handleHypervisorHostnamePatch( 58688a8a174SEd Tanous const std::string& hostName, 5877e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 5887e860f15SJohn Edward Broadbent { 5897e860f15SJohn Edward Broadbent if (!isHostnameValid(hostName)) 5907e860f15SJohn Edward Broadbent { 5917e860f15SJohn Edward Broadbent messages::propertyValueFormatError(asyncResp->res, hostName, 5927e860f15SJohn Edward Broadbent "HostName"); 5937e860f15SJohn Edward Broadbent return; 5947e860f15SJohn Edward Broadbent } 5957e860f15SJohn Edward Broadbent 5967e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HostName"] = hostName; 597e93abac6SGinu George setDbusProperty(asyncResp, "HostName", "xyz.openbmc_project.Settings", 598d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 599d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 600d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 601e93abac6SGinu George "HostName", hostName); 6027e860f15SJohn Edward Broadbent } 6037e860f15SJohn Edward Broadbent 6047e860f15SJohn Edward Broadbent inline void 60577179532SEd Tanous setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive, 6067e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6077e860f15SJohn Edward Broadbent { 608e93abac6SGinu George setDbusProperty( 609e93abac6SGinu George asyncResp, "InterfaceEnabled", "xyz.openbmc_project.Settings", 610e93abac6SGinu George "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0", 611e93abac6SGinu George "xyz.openbmc_project.Object.Enable", "Enabled", isActive); 6127e860f15SJohn Edward Broadbent } 6137e860f15SJohn Edward Broadbent 614f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceCollectionGet( 615f40448e5SGunnar Mills App& app, const crow::Request& req, 616f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 617f40448e5SGunnar Mills { 618f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 619f40448e5SGunnar Mills { 620f40448e5SGunnar Mills return; 621f40448e5SGunnar Mills } 622f40448e5SGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 623f40448e5SGunnar Mills "xyz.openbmc_project.Network.EthernetInterface"}; 624f40448e5SGunnar Mills 625f40448e5SGunnar Mills dbus::utility::getSubTreePaths( 626f40448e5SGunnar Mills "/xyz/openbmc_project/network/hypervisor", 0, interfaces, 627f40448e5SGunnar Mills [asyncResp]( 6288b24275dSEd Tanous const boost::system::error_code& ec, 629f40448e5SGunnar Mills const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) { 6308b24275dSEd Tanous if (ec) 631f40448e5SGunnar Mills { 632bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "System", 633bd79bce8SPatrick Williams "hypervisor"); 634f40448e5SGunnar Mills return; 635f40448e5SGunnar Mills } 636f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 637f40448e5SGunnar Mills "#EthernetInterfaceCollection." 638f40448e5SGunnar Mills "EthernetInterfaceCollection"; 639f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 640f40448e5SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 641f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet " 642f40448e5SGunnar Mills "Interface Collection"; 643f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 644f40448e5SGunnar Mills "Collection of Virtual Management " 645f40448e5SGunnar Mills "Interfaces for the hypervisor"; 646f40448e5SGunnar Mills 647f40448e5SGunnar Mills nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 648f40448e5SGunnar Mills ifaceArray = nlohmann::json::array(); 649f40448e5SGunnar Mills for (const std::string& iface : ifaceList) 650f40448e5SGunnar Mills { 651f40448e5SGunnar Mills sdbusplus::message::object_path path(iface); 652f40448e5SGunnar Mills std::string name = path.filename(); 653f40448e5SGunnar Mills if (name.empty()) 654f40448e5SGunnar Mills { 655f40448e5SGunnar Mills continue; 656f40448e5SGunnar Mills } 657f40448e5SGunnar Mills nlohmann::json::object_t ethIface; 658ef4c65b7SEd Tanous ethIface["@odata.id"] = boost::urls::format( 659bd79bce8SPatrick Williams "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", 660bd79bce8SPatrick Williams name); 661b2ba3072SPatrick Williams ifaceArray.emplace_back(std::move(ethIface)); 662f40448e5SGunnar Mills } 663f40448e5SGunnar Mills asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 664f40448e5SGunnar Mills }); 665f40448e5SGunnar Mills } 666f40448e5SGunnar Mills 667f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceGet( 668f40448e5SGunnar Mills App& app, const crow::Request& req, 669f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 670f40448e5SGunnar Mills { 671f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 672f40448e5SGunnar Mills { 673f40448e5SGunnar Mills return; 674f40448e5SGunnar Mills } 675f40448e5SGunnar Mills getHypervisorIfaceData( 676f40448e5SGunnar Mills id, [asyncResp, ifaceId{std::string(id)}]( 67777179532SEd Tanous bool success, const EthernetInterfaceData& ethData, 67877179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) { 679f40448e5SGunnar Mills if (!success) 680f40448e5SGunnar Mills { 681f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 682f40448e5SGunnar Mills ifaceId); 683f40448e5SGunnar Mills return; 684f40448e5SGunnar Mills } 685f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 68693bbc953SJiaqing Zhao "#EthernetInterface.v1_9_0.EthernetInterface"; 687f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface"; 688f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 689f40448e5SGunnar Mills "Hypervisor's Virtual Management Ethernet Interface"; 690f40448e5SGunnar Mills parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData, 691f40448e5SGunnar Mills ipv4Data); 692f40448e5SGunnar Mills }); 693f40448e5SGunnar Mills } 694f40448e5SGunnar Mills 6958d8c30c3SGunnar Mills inline void handleHypervisorSystemGet( 6968d8c30c3SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6978d8c30c3SGunnar Mills { 6988d8c30c3SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 6998d8c30c3SGunnar Mills "#ComputerSystem.v1_6_0.ComputerSystem"; 70068896206SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/hypervisor"; 7018d8c30c3SGunnar Mills asyncResp->res.jsonValue["Description"] = "Hypervisor"; 7028d8c30c3SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor"; 7038d8c30c3SGunnar Mills asyncResp->res.jsonValue["Id"] = "hypervisor"; 70468896206SGunnar Mills asyncResp->res.jsonValue["SystemType"] = computer_system::SystemType::OS; 7058d8c30c3SGunnar Mills nlohmann::json::array_t managedBy; 7068d8c30c3SGunnar Mills nlohmann::json::object_t manager; 70768896206SGunnar Mills manager["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}", 70868896206SGunnar Mills BMCWEB_REDFISH_MANAGER_URI_NAME); 709ad539545SPatrick Williams managedBy.emplace_back(std::move(manager)); 71068896206SGunnar Mills asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy); 7118d8c30c3SGunnar Mills asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] = 7128d8c30c3SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 7138d8c30c3SGunnar Mills getHypervisorState(asyncResp); 7148d8c30c3SGunnar Mills getHypervisorActions(asyncResp); 7158d8c30c3SGunnar Mills // TODO: Add "SystemType" : "hypervisor" 7168d8c30c3SGunnar Mills } 7178d8c30c3SGunnar Mills 718f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfacePatch( 719f40448e5SGunnar Mills App& app, const crow::Request& req, 720f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 721f40448e5SGunnar Mills const std::string& ifaceId) 722f40448e5SGunnar Mills { 723f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 724f40448e5SGunnar Mills { 725f40448e5SGunnar Mills return; 726f40448e5SGunnar Mills } 727f40448e5SGunnar Mills std::optional<std::string> hostName; 72821fe928bSEd Tanous std::optional< 72921fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>> 73021fe928bSEd Tanous ipv4StaticAddresses; 73121fe928bSEd Tanous std::optional<std::vector<nlohmann::json::object_t>> ipv4Addresses; 732f40448e5SGunnar Mills std::optional<bool> ipv4DHCPEnabled; 733f40448e5SGunnar Mills 734afc474aeSMyung Bae if (!json_util::readJsonPatch( // 735afc474aeSMyung Bae req, asyncResp->res, // 736afc474aeSMyung Bae "DHCPv4/DHCPEnabled", ipv4DHCPEnabled, // 737afc474aeSMyung Bae "IPv4Addresses", ipv4Addresses, // 738afc474aeSMyung Bae "IPv4StaticAddresses", ipv4StaticAddresses, // 739afc474aeSMyung Bae "HostName", hostName // 740afc474aeSMyung Bae )) 741f40448e5SGunnar Mills { 742f40448e5SGunnar Mills return; 743f40448e5SGunnar Mills } 744f40448e5SGunnar Mills 745f40448e5SGunnar Mills if (ipv4Addresses) 746f40448e5SGunnar Mills { 747f40448e5SGunnar Mills messages::propertyNotWritable(asyncResp->res, "IPv4Addresses"); 748f40448e5SGunnar Mills return; 749f40448e5SGunnar Mills } 750f40448e5SGunnar Mills 751f40448e5SGunnar Mills getHypervisorIfaceData( 75221fe928bSEd Tanous ifaceId, 75321fe928bSEd Tanous [asyncResp, ifaceId, hostName = std::move(hostName), 7545a39f77aSPatrick Williams ipv4StaticAddresses = std::move(ipv4StaticAddresses), 75521fe928bSEd Tanous ipv4DHCPEnabled](bool success, const EthernetInterfaceData& ethData, 75621fe928bSEd Tanous const std::vector<IPv4AddressData>&) mutable { 757f40448e5SGunnar Mills if (!success) 758f40448e5SGunnar Mills { 759f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 760f40448e5SGunnar Mills ifaceId); 761f40448e5SGunnar Mills return; 762f40448e5SGunnar Mills } 763f40448e5SGunnar Mills 764f40448e5SGunnar Mills if (ipv4StaticAddresses) 765f40448e5SGunnar Mills { 766bd79bce8SPatrick Williams std::vector<std::variant<nlohmann::json::object_t, 767bd79bce8SPatrick Williams std::nullptr_t>>& ipv4Static = 768bd79bce8SPatrick Williams *ipv4StaticAddresses; 769f40448e5SGunnar Mills if (ipv4Static.begin() == ipv4Static.end()) 770f40448e5SGunnar Mills { 77121fe928bSEd Tanous messages::propertyValueTypeError(asyncResp->res, 77221fe928bSEd Tanous std::vector<std::string>(), 773f40448e5SGunnar Mills "IPv4StaticAddresses"); 774f40448e5SGunnar Mills return; 775f40448e5SGunnar Mills } 776f40448e5SGunnar Mills 777f40448e5SGunnar Mills // One and only one hypervisor instance supported 778f40448e5SGunnar Mills if (ipv4Static.size() != 1) 779f40448e5SGunnar Mills { 78021fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, "[]", 781f40448e5SGunnar Mills "IPv4StaticAddresses"); 782f40448e5SGunnar Mills return; 783f40448e5SGunnar Mills } 784f40448e5SGunnar Mills 785bd79bce8SPatrick Williams std::variant<nlohmann::json::object_t, std::nullptr_t>& 786bd79bce8SPatrick Williams ipv4Json = ipv4Static[0]; 787f40448e5SGunnar Mills // Check if the param is 'null'. If its null, it means 788f40448e5SGunnar Mills // that user wants to delete the IP address. Deleting 789f40448e5SGunnar Mills // the IP address is allowed only if its statically 790f40448e5SGunnar Mills // configured. Deleting the address originated from DHCP 791f40448e5SGunnar Mills // is not allowed. 79221fe928bSEd Tanous if (std::holds_alternative<std::nullptr_t>(ipv4Json) && 79321fe928bSEd Tanous translateDhcpEnabledToBool(ethData.dhcpEnabled, true)) 794f40448e5SGunnar Mills { 795bd79bce8SPatrick Williams BMCWEB_LOG_INFO( 796bd79bce8SPatrick Williams "Ignoring the delete on ipv4StaticAddresses " 79762598e31SEd Tanous "as the interface is DHCP enabled"); 798f40448e5SGunnar Mills } 799f40448e5SGunnar Mills else 800f40448e5SGunnar Mills { 801bd79bce8SPatrick Williams handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, 802bd79bce8SPatrick Williams asyncResp); 803f40448e5SGunnar Mills } 804f40448e5SGunnar Mills } 805f40448e5SGunnar Mills 806f40448e5SGunnar Mills if (hostName) 807f40448e5SGunnar Mills { 808f40448e5SGunnar Mills handleHypervisorHostnamePatch(*hostName, asyncResp); 809f40448e5SGunnar Mills } 810f40448e5SGunnar Mills 81121fe928bSEd Tanous if (ipv4DHCPEnabled) 812f40448e5SGunnar Mills { 813f40448e5SGunnar Mills setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp); 814f40448e5SGunnar Mills } 815f40448e5SGunnar Mills 816f40448e5SGunnar Mills // Set this interface to disabled/inactive. This will be set 817f40448e5SGunnar Mills // to enabled/active by the pldm once the hypervisor 818f40448e5SGunnar Mills // consumes the updated settings from the user. 819f40448e5SGunnar Mills setIPv4InterfaceEnabled(ifaceId, false, asyncResp); 820f40448e5SGunnar Mills }); 821f40448e5SGunnar Mills asyncResp->res.result(boost::beast::http::status::accepted); 822f40448e5SGunnar Mills } 823f40448e5SGunnar Mills 824a5d0cedbSGunnar Mills inline void handleHypervisorResetActionGet( 825a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 826a5d0cedbSGunnar Mills { 827a5d0cedbSGunnar Mills // Only return action info if hypervisor D-Bus object present 828a5d0cedbSGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 829a5d0cedbSGunnar Mills "xyz.openbmc_project.State.Host"}; 830a5d0cedbSGunnar Mills dbus::utility::getDbusObject( 831a5d0cedbSGunnar Mills "/xyz/openbmc_project/state/hypervisor0", interfaces, 832a5d0cedbSGunnar Mills [asyncResp]( 833a5d0cedbSGunnar Mills const boost::system::error_code& ec, 834a5d0cedbSGunnar Mills const std::vector<std::pair<std::string, std::vector<std::string>>>& 835a5d0cedbSGunnar Mills objInfo) { 836a5d0cedbSGunnar Mills if (ec) 837a5d0cedbSGunnar Mills { 83862598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 839a5d0cedbSGunnar Mills 840a5d0cedbSGunnar Mills // No hypervisor objects found by mapper 841a5d0cedbSGunnar Mills if (ec.value() == boost::system::errc::io_error) 842a5d0cedbSGunnar Mills { 843a5d0cedbSGunnar Mills messages::resourceNotFound(asyncResp->res, "hypervisor", 844a5d0cedbSGunnar Mills "ResetActionInfo"); 845a5d0cedbSGunnar Mills return; 846a5d0cedbSGunnar Mills } 847a5d0cedbSGunnar Mills 848a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 849a5d0cedbSGunnar Mills return; 850a5d0cedbSGunnar Mills } 851a5d0cedbSGunnar Mills 852a5d0cedbSGunnar Mills // One and only one hypervisor instance supported 853a5d0cedbSGunnar Mills if (objInfo.size() != 1) 854a5d0cedbSGunnar Mills { 855a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 856a5d0cedbSGunnar Mills return; 857a5d0cedbSGunnar Mills } 858a5d0cedbSGunnar Mills 859a5d0cedbSGunnar Mills // The hypervisor object only support the ability to 860a5d0cedbSGunnar Mills // turn On The system object Action should be utilized 861a5d0cedbSGunnar Mills // for other operations 862a5d0cedbSGunnar Mills 863a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 864a5d0cedbSGunnar Mills "#ActionInfo.v1_1_2.ActionInfo"; 865a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 866a5d0cedbSGunnar Mills "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 867a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Name"] = "Reset Action Info"; 868a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Id"] = "ResetActionInfo"; 869a5d0cedbSGunnar Mills nlohmann::json::array_t parameters; 870a5d0cedbSGunnar Mills nlohmann::json::object_t parameter; 871a5d0cedbSGunnar Mills parameter["Name"] = "ResetType"; 872a5d0cedbSGunnar Mills parameter["Required"] = true; 873539d8c6bSEd Tanous parameter["DataType"] = action_info::ParameterTypes::String; 874a5d0cedbSGunnar Mills nlohmann::json::array_t allowed; 875ad539545SPatrick Williams allowed.emplace_back("On"); 876a5d0cedbSGunnar Mills parameter["AllowableValues"] = std::move(allowed); 877ad539545SPatrick Williams parameters.emplace_back(std::move(parameter)); 878a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Parameters"] = std::move(parameters); 879a5d0cedbSGunnar Mills }); 880a5d0cedbSGunnar Mills } 881a5d0cedbSGunnar Mills 882a5d0cedbSGunnar Mills inline void handleHypervisorSystemResetPost( 883dd7090e6SGunnar Mills const crow::Request& req, 884a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 885a5d0cedbSGunnar Mills { 886a5d0cedbSGunnar Mills std::optional<std::string> resetType; 887a5d0cedbSGunnar Mills if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType)) 888a5d0cedbSGunnar Mills { 889a5d0cedbSGunnar Mills // readJson adds appropriate error to response 890a5d0cedbSGunnar Mills return; 891a5d0cedbSGunnar Mills } 892a5d0cedbSGunnar Mills 893a5d0cedbSGunnar Mills if (!resetType) 894a5d0cedbSGunnar Mills { 895a5d0cedbSGunnar Mills messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset", 896a5d0cedbSGunnar Mills "ResetType"); 897a5d0cedbSGunnar Mills return; 898a5d0cedbSGunnar Mills } 899a5d0cedbSGunnar Mills 900a5d0cedbSGunnar Mills // Hypervisor object only support On operation 901a5d0cedbSGunnar Mills if (resetType != "On") 902a5d0cedbSGunnar Mills { 903a5d0cedbSGunnar Mills messages::propertyValueNotInList(asyncResp->res, *resetType, 904a5d0cedbSGunnar Mills "ResetType"); 905a5d0cedbSGunnar Mills return; 906a5d0cedbSGunnar Mills } 907a5d0cedbSGunnar Mills 908a5d0cedbSGunnar Mills std::string command = "xyz.openbmc_project.State.Host.Transition.On"; 909a5d0cedbSGunnar Mills 910bd79bce8SPatrick Williams setDbusPropertyAction( 911bd79bce8SPatrick Williams asyncResp, "xyz.openbmc_project.State.Hypervisor", 9121827b4f1SAsmitha Karunanithi sdbusplus::message::object_path( 9131827b4f1SAsmitha Karunanithi "/xyz/openbmc_project/state/hypervisor0"), 914bd79bce8SPatrick Williams "xyz.openbmc_project.State.Host", "RequestedHostTransition", 915bd79bce8SPatrick Williams "ResetType", "ComputerSystem.Reset", command); 916a5d0cedbSGunnar Mills } 917a5d0cedbSGunnar Mills 9187e860f15SJohn Edward Broadbent inline void requestRoutesHypervisorSystems(App& app) 9197e860f15SJohn Edward Broadbent { 9207e860f15SJohn Edward Broadbent /** 9217e860f15SJohn Edward Broadbent * HypervisorInterfaceCollection class to handle the GET and PATCH on 9227e860f15SJohn Edward Broadbent * Hypervisor Interface 9237e860f15SJohn Edward Broadbent */ 9247e860f15SJohn Edward Broadbent 9257e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/") 926ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 927f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 928f40448e5SGunnar Mills handleHypervisorEthernetInterfaceCollectionGet, std::ref(app))); 9297e860f15SJohn Edward Broadbent 9307e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9317e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 932ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 933f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 934f40448e5SGunnar Mills handleHypervisorEthernetInterfaceGet, std::ref(app))); 935d5afb2caSAndrew Geissler 9367e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9377e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 938ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 939f40448e5SGunnar Mills .methods(boost::beast::http::verb::patch)(std::bind_front( 940f40448e5SGunnar Mills handleHypervisorEthernetInterfacePatch, std::ref(app))); 9414fbaf64aSAndrew Geissler } 94288a8a174SEd Tanous } // namespace redfish 943