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."); 411e1e598dSJonathan Doman sdbusplus::asio::getProperty<std::string>( 421e1e598dSJonathan Doman *crow::connections::systemBus, "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; 181d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 182d5afb2caSAndrew Geissler { 183d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 184d5afb2caSAndrew Geissler { 185d5afb2caSAndrew Geissler if (propertyPair.first == "MACAddress") 186d5afb2caSAndrew Geissler { 187d5afb2caSAndrew Geissler const std::string* mac = 188d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 189d5afb2caSAndrew Geissler if (mac != nullptr) 190d5afb2caSAndrew Geissler { 19182695a5bSJiaqing Zhao ethData.macAddress = *mac; 192d5afb2caSAndrew Geissler } 193d5afb2caSAndrew Geissler } 194d5afb2caSAndrew Geissler } 195d5afb2caSAndrew Geissler } 196d5afb2caSAndrew Geissler else if (ifacePair.first == 197d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.EthernetInterface") 198d5afb2caSAndrew Geissler { 199d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 200d5afb2caSAndrew Geissler { 201d5afb2caSAndrew Geissler if (propertyPair.first == "DHCPEnabled") 202d5afb2caSAndrew Geissler { 203d5afb2caSAndrew Geissler const std::string* dhcp = 204d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 205d5afb2caSAndrew Geissler if (dhcp != nullptr) 206d5afb2caSAndrew Geissler { 20782695a5bSJiaqing Zhao ethData.dhcpEnabled = *dhcp; 208d5afb2caSAndrew Geissler break; // Interested on only "DHCPEnabled". 209d5afb2caSAndrew Geissler // Stop parsing since we got the 210d5afb2caSAndrew Geissler // "DHCPEnabled" value. 211d5afb2caSAndrew Geissler } 212d5afb2caSAndrew Geissler } 213d5afb2caSAndrew Geissler } 214d5afb2caSAndrew Geissler } 215d5afb2caSAndrew Geissler } 216d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" + 217d5afb2caSAndrew Geissler ethIfaceId + "/ipv4/addr0") 218d5afb2caSAndrew Geissler { 21977179532SEd Tanous IPv4AddressData& ipv4Address = ipv4Config.emplace_back(); 220d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Object.Enable") 221d5afb2caSAndrew Geissler { 2229eb808c1SEd Tanous for (const auto& property : ifacePair.second) 223d5afb2caSAndrew Geissler { 224d5afb2caSAndrew Geissler if (property.first == "Enabled") 225d5afb2caSAndrew Geissler { 226d5afb2caSAndrew Geissler const bool* intfEnable = 227d5afb2caSAndrew Geissler std::get_if<bool>(&property.second); 228d5afb2caSAndrew Geissler if (intfEnable != nullptr) 229d5afb2caSAndrew Geissler { 230d5afb2caSAndrew Geissler ipv4Address.isActive = *intfEnable; 231d5afb2caSAndrew Geissler break; 232d5afb2caSAndrew Geissler } 233d5afb2caSAndrew Geissler } 234d5afb2caSAndrew Geissler } 235d5afb2caSAndrew Geissler } 236d5afb2caSAndrew Geissler if (ifacePair.first == "xyz.openbmc_project.Network.IP") 237d5afb2caSAndrew Geissler { 2389eb808c1SEd Tanous for (const auto& property : ifacePair.second) 239d5afb2caSAndrew Geissler { 240d5afb2caSAndrew Geissler if (property.first == "Address") 241d5afb2caSAndrew Geissler { 242d5afb2caSAndrew Geissler const std::string* address = 243d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 244d5afb2caSAndrew Geissler if (address != nullptr) 245d5afb2caSAndrew Geissler { 246d5afb2caSAndrew Geissler ipv4Address.address = *address; 247d5afb2caSAndrew Geissler } 248d5afb2caSAndrew Geissler } 249d5afb2caSAndrew Geissler else if (property.first == "Origin") 250d5afb2caSAndrew Geissler { 251d5afb2caSAndrew Geissler const std::string* origin = 252d5afb2caSAndrew Geissler std::get_if<std::string>(&property.second); 253d5afb2caSAndrew Geissler if (origin != nullptr) 254d5afb2caSAndrew Geissler { 255d5afb2caSAndrew Geissler ipv4Address.origin = 256d5afb2caSAndrew Geissler translateAddressOriginDbusToRedfish(*origin, 257d5afb2caSAndrew Geissler true); 258d5afb2caSAndrew Geissler } 259d5afb2caSAndrew Geissler } 260d5afb2caSAndrew Geissler else if (property.first == "PrefixLength") 261d5afb2caSAndrew Geissler { 262d5afb2caSAndrew Geissler const uint8_t* mask = 263d5afb2caSAndrew Geissler std::get_if<uint8_t>(&property.second); 264d5afb2caSAndrew Geissler if (mask != nullptr) 265d5afb2caSAndrew Geissler { 266d5afb2caSAndrew Geissler // convert it to the string 267d5afb2caSAndrew Geissler ipv4Address.netmask = getNetmask(*mask); 268d5afb2caSAndrew Geissler } 269d5afb2caSAndrew Geissler } 270889ff694SAsmitha Karunanithi else if (property.first == "Type" || 271889ff694SAsmitha Karunanithi property.first == "Gateway") 272889ff694SAsmitha Karunanithi { 273889ff694SAsmitha Karunanithi // Type & Gateway is not used 274889ff694SAsmitha Karunanithi continue; 275889ff694SAsmitha Karunanithi } 276d5afb2caSAndrew Geissler else 277d5afb2caSAndrew Geissler { 27862598e31SEd Tanous BMCWEB_LOG_ERROR( 27962598e31SEd Tanous "Got extra property: {} on the {} object", 28062598e31SEd Tanous property.first, objpath.first.str); 281d5afb2caSAndrew Geissler } 282d5afb2caSAndrew Geissler } 283d5afb2caSAndrew Geissler } 284d5afb2caSAndrew Geissler } 285d5afb2caSAndrew Geissler if (objpath.first == "/xyz/openbmc_project/network/hypervisor") 286d5afb2caSAndrew Geissler { 287d5afb2caSAndrew Geissler // System configuration shows up in the global namespace, so no 288d5afb2caSAndrew Geissler // need to check eth number 289d5afb2caSAndrew Geissler if (ifacePair.first == 290d5afb2caSAndrew Geissler "xyz.openbmc_project.Network.SystemConfiguration") 291d5afb2caSAndrew Geissler { 292d5afb2caSAndrew Geissler for (const auto& propertyPair : ifacePair.second) 293d5afb2caSAndrew Geissler { 294d5afb2caSAndrew Geissler if (propertyPair.first == "HostName") 295d5afb2caSAndrew Geissler { 296d5afb2caSAndrew Geissler const std::string* hostName = 297d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 298d5afb2caSAndrew Geissler if (hostName != nullptr) 299d5afb2caSAndrew Geissler { 30082695a5bSJiaqing Zhao ethData.hostName = *hostName; 301d5afb2caSAndrew Geissler } 302d5afb2caSAndrew Geissler } 303d5afb2caSAndrew Geissler else if (propertyPair.first == "DefaultGateway") 304d5afb2caSAndrew Geissler { 305d5afb2caSAndrew Geissler const std::string* defaultGateway = 306d5afb2caSAndrew Geissler std::get_if<std::string>(&propertyPair.second); 307d5afb2caSAndrew Geissler if (defaultGateway != nullptr) 308d5afb2caSAndrew Geissler { 30982695a5bSJiaqing Zhao ethData.defaultGateway = *defaultGateway; 310d5afb2caSAndrew Geissler } 311d5afb2caSAndrew Geissler } 312d5afb2caSAndrew Geissler } 313d5afb2caSAndrew Geissler } 314d5afb2caSAndrew Geissler } 315d5afb2caSAndrew Geissler } 316d5afb2caSAndrew Geissler } 317d5afb2caSAndrew Geissler return idFound; 318d5afb2caSAndrew Geissler } 319d5afb2caSAndrew Geissler /** 320d5afb2caSAndrew Geissler * Function that retrieves all properties for given Hypervisor Ethernet 321d5afb2caSAndrew Geissler * Interface Object from Settings Manager 322d5afb2caSAndrew Geissler * @param ethIfaceId Hypervisor ethernet interface id to query on DBus 323d5afb2caSAndrew Geissler * @param callback a function that shall be called to convert Dbus output 324d5afb2caSAndrew Geissler * into JSON 325d5afb2caSAndrew Geissler */ 326d5afb2caSAndrew Geissler template <typename CallbackFunc> 327d5afb2caSAndrew Geissler void getHypervisorIfaceData(const std::string& ethIfaceId, 328d5afb2caSAndrew Geissler CallbackFunc&& callback) 329d5afb2caSAndrew Geissler { 3305eb468daSGeorge Liu sdbusplus::message::object_path path("/"); 3315eb468daSGeorge Liu dbus::utility::getManagedObjects( 3325eb468daSGeorge Liu "xyz.openbmc_project.Settings", path, 333f94c4ecfSEd Tanous [ethIfaceId{std::string{ethIfaceId}}, 3348cb2c024SEd Tanous callback = std::forward<CallbackFunc>(callback)]( 3358b24275dSEd Tanous const boost::system::error_code& ec, 33621fe928bSEd Tanous const dbus::utility::ManagedObjectType& resp) mutable { 337d5afb2caSAndrew Geissler EthernetInterfaceData ethData{}; 33877179532SEd Tanous std::vector<IPv4AddressData> ipv4Data; 3398b24275dSEd Tanous if (ec) 340d5afb2caSAndrew Geissler { 341d5afb2caSAndrew Geissler callback(false, ethData, ipv4Data); 342d5afb2caSAndrew Geissler return; 343d5afb2caSAndrew Geissler } 344d5afb2caSAndrew Geissler 345bd79bce8SPatrick Williams bool found = extractHypervisorInterfaceData(ethIfaceId, resp, 346bd79bce8SPatrick Williams ethData, ipv4Data); 347d5afb2caSAndrew Geissler if (!found) 348d5afb2caSAndrew Geissler { 34962598e31SEd Tanous BMCWEB_LOG_INFO("Hypervisor Interface not found"); 350d5afb2caSAndrew Geissler } 351d5afb2caSAndrew Geissler callback(found, ethData, ipv4Data); 3525eb468daSGeorge Liu }); 353d5afb2caSAndrew Geissler } 354d5afb2caSAndrew Geissler 355d5afb2caSAndrew Geissler /** 356d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface IPAddress DBUS 357d5afb2caSAndrew Geissler * 358ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 359d5afb2caSAndrew Geissler * @param[in] ipv4Address Address from the incoming request 360d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 361d5afb2caSAndrew Geissler * 362d5afb2caSAndrew Geissler * @return None. 363d5afb2caSAndrew Geissler */ 364ac106bf6SEd Tanous inline void setHypervisorIPv4Address( 365ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 366ac106bf6SEd Tanous const std::string& ethIfaceId, const std::string& ipv4Address) 367d5afb2caSAndrew Geissler { 36862598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}", 36962598e31SEd Tanous ipv4Address, ethIfaceId); 370d82b5e1fSAsmitha Karunanithi 371bd79bce8SPatrick Williams setDbusProperty( 372bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/Address", 373e93abac6SGinu George "xyz.openbmc_project.Settings", 374bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0", 375e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Address", ipv4Address); 376d5afb2caSAndrew Geissler } 377d5afb2caSAndrew Geissler 378d5afb2caSAndrew Geissler /** 379d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface SubnetMask DBUS 380d5afb2caSAndrew Geissler * 381ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 382d5afb2caSAndrew Geissler * @param[in] subnet SubnetMask from the incoming request 383d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 384d5afb2caSAndrew Geissler * 385d5afb2caSAndrew Geissler * @return None. 386d5afb2caSAndrew Geissler */ 3878d1b46d7Szhanghch05 inline void 388ac106bf6SEd Tanous setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 3898d1b46d7Szhanghch05 const std::string& ethIfaceId, const uint8_t subnet) 390d5afb2caSAndrew Geissler { 39162598e31SEd Tanous BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet, 39262598e31SEd Tanous ethIfaceId); 393d5afb2caSAndrew Geissler 394bd79bce8SPatrick Williams setDbusProperty( 395bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/SubnetMask", 396e93abac6SGinu George "xyz.openbmc_project.Settings", 397bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0", 398e93abac6SGinu George "xyz.openbmc_project.Network.IP", "PrefixLength", subnet); 399d5afb2caSAndrew Geissler } 400d5afb2caSAndrew Geissler 401d5afb2caSAndrew Geissler /** 402d5afb2caSAndrew Geissler * @brief Sets the Hypervisor Interface Gateway DBUS 403d5afb2caSAndrew Geissler * 404ac106bf6SEd Tanous * @param[in] asyncResp Shared pointer for generating response message. 405d5afb2caSAndrew Geissler * @param[in] gateway Gateway from the incoming request 406d5afb2caSAndrew Geissler * @param[in] ethIfaceId Hypervisor Interface Id 407d5afb2caSAndrew Geissler * 408d5afb2caSAndrew Geissler * @return None. 409d5afb2caSAndrew Geissler */ 410ac106bf6SEd Tanous inline void setHypervisorIPv4Gateway( 411ac106bf6SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 412d5afb2caSAndrew Geissler const std::string& gateway) 413d5afb2caSAndrew Geissler { 41462598e31SEd Tanous BMCWEB_LOG_DEBUG( 41562598e31SEd Tanous "Setting the DefaultGateway to the last configured gateway"); 416d5afb2caSAndrew Geissler 417e93abac6SGinu George setDbusProperty(asyncResp, "IPv4StaticAddresses/1/Gateway", 418e93abac6SGinu George "xyz.openbmc_project.Settings", 419d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 420d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 421d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 422e93abac6SGinu George "DefaultGateway", gateway); 423d5afb2caSAndrew Geissler } 424d5afb2caSAndrew Geissler 425d5afb2caSAndrew Geissler /** 426d5afb2caSAndrew Geissler * @brief Creates a static IPv4 entry 427d5afb2caSAndrew Geissler * 428d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 429d5afb2caSAndrew Geissler * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 430d5afb2caSAndrew Geissler * @param[in] gateway IPv4 address of this interfaces gateway 431d5afb2caSAndrew Geissler * @param[in] address IPv4 address to assign to this interface 432d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 433d5afb2caSAndrew Geissler * 434d5afb2caSAndrew Geissler * @return None 435d5afb2caSAndrew Geissler */ 4368d1b46d7Szhanghch05 inline void 4378d1b46d7Szhanghch05 createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength, 4388d1b46d7Szhanghch05 const std::string& gateway, const std::string& address, 4398d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 440d5afb2caSAndrew Geissler { 441d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 442d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 443d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 444d5afb2caSAndrew Geissler } 445d5afb2caSAndrew Geissler 446d5afb2caSAndrew Geissler /** 447d5afb2caSAndrew Geissler * @brief Deletes given IPv4 interface 448d5afb2caSAndrew Geissler * 449d5afb2caSAndrew Geissler * @param[in] ifaceId Id of interface whose IP should be deleted 450d5afb2caSAndrew Geissler * @param[io] asyncResp Response object that will be returned to client 451d5afb2caSAndrew Geissler * 452d5afb2caSAndrew Geissler * @return None 453d5afb2caSAndrew Geissler */ 4548d1b46d7Szhanghch05 inline void 4558d1b46d7Szhanghch05 deleteHypervisorIPv4(const std::string& ifaceId, 4568d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 457d5afb2caSAndrew Geissler { 458d5afb2caSAndrew Geissler std::string address = "0.0.0.0"; 459d5afb2caSAndrew Geissler std::string gateway = "0.0.0.0"; 460d5afb2caSAndrew Geissler const uint8_t prefixLength = 0; 461d5afb2caSAndrew Geissler setHypervisorIPv4Address(asyncResp, ifaceId, address); 462d5afb2caSAndrew Geissler setHypervisorIPv4Gateway(asyncResp, gateway); 463d5afb2caSAndrew Geissler setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 464d5afb2caSAndrew Geissler } 465d5afb2caSAndrew Geissler 46677179532SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse, 46777179532SEd Tanous const std::string& ifaceId, 468d5afb2caSAndrew Geissler const EthernetInterfaceData& ethData, 46977179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) 470d5afb2caSAndrew Geissler { 471d5afb2caSAndrew Geissler jsonResponse["Id"] = ifaceId; 472ef4c65b7SEd Tanous jsonResponse["@odata.id"] = boost::urls::format( 473ef4c65b7SEd Tanous "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId); 474d5afb2caSAndrew Geissler jsonResponse["InterfaceEnabled"] = true; 47582695a5bSJiaqing Zhao jsonResponse["MACAddress"] = ethData.macAddress; 476d5afb2caSAndrew Geissler 47782695a5bSJiaqing Zhao jsonResponse["HostName"] = ethData.hostName; 478d5afb2caSAndrew Geissler jsonResponse["DHCPv4"]["DHCPEnabled"] = 47982695a5bSJiaqing Zhao translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 480d5afb2caSAndrew Geissler 481d5afb2caSAndrew Geissler nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 482d5afb2caSAndrew Geissler nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 483d5afb2caSAndrew Geissler ipv4Array = nlohmann::json::array(); 484d5afb2caSAndrew Geissler ipv4StaticArray = nlohmann::json::array(); 4859eb808c1SEd Tanous for (const auto& ipv4Config : ipv4Data) 486d5afb2caSAndrew Geissler { 487d5afb2caSAndrew Geissler if (ipv4Config.isActive) 488d5afb2caSAndrew Geissler { 4891476687dSEd Tanous nlohmann::json::object_t ipv4; 4901476687dSEd Tanous ipv4["AddressOrigin"] = ipv4Config.origin; 4911476687dSEd Tanous ipv4["SubnetMask"] = ipv4Config.netmask; 4921476687dSEd Tanous ipv4["Address"] = ipv4Config.address; 4931476687dSEd Tanous ipv4["Gateway"] = ethData.defaultGateway; 494d5afb2caSAndrew Geissler 495d5afb2caSAndrew Geissler if (ipv4Config.origin == "Static") 496d5afb2caSAndrew Geissler { 4971476687dSEd Tanous ipv4StaticArray.push_back(ipv4); 498d5afb2caSAndrew Geissler } 499b2ba3072SPatrick Williams ipv4Array.emplace_back(std::move(ipv4)); 500d5afb2caSAndrew Geissler } 501d5afb2caSAndrew Geissler } 502d5afb2caSAndrew Geissler } 503d5afb2caSAndrew Geissler 50477179532SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled, 5058d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 506d5afb2caSAndrew Geissler { 5077e860f15SJohn Edward Broadbent const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false); 508d82b5e1fSAsmitha Karunanithi 509e93abac6SGinu George setDbusProperty( 510e93abac6SGinu George asyncResp, "DHCPv4/DHCPEnabled", "xyz.openbmc_project.Settings", 511d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 512d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor") / 513d82b5e1fSAsmitha Karunanithi ifaceId, 514e93abac6SGinu George "xyz.openbmc_project.Network.EthernetInterface", "DHCPEnabled", dhcp); 515d5afb2caSAndrew Geissler 516d5afb2caSAndrew Geissler // Set the IPv4 address origin to the DHCP / Static as per the new value 517d5afb2caSAndrew Geissler // of the DHCPEnabled property 518d5afb2caSAndrew Geissler std::string origin; 519e05aec50SEd Tanous if (!ipv4DHCPEnabled) 520d5afb2caSAndrew Geissler { 521d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 522d5afb2caSAndrew Geissler } 523d5afb2caSAndrew Geissler else 524d5afb2caSAndrew Geissler { 525d5afb2caSAndrew Geissler // DHCPEnabled is set to true. Delete the current IPv4 settings 526d5afb2caSAndrew Geissler // to receive the new values from DHCP server. 527d5afb2caSAndrew Geissler deleteHypervisorIPv4(ifaceId, asyncResp); 528d5afb2caSAndrew Geissler origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 529d5afb2caSAndrew Geissler } 530d82b5e1fSAsmitha Karunanithi 531bd79bce8SPatrick Williams setDbusProperty( 532bd79bce8SPatrick Williams asyncResp, "IPv4StaticAddresses/1/AddressOrigin", 533e93abac6SGinu George "xyz.openbmc_project.Settings", 534bd79bce8SPatrick Williams "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0", 535e93abac6SGinu George "xyz.openbmc_project.Network.IP", "Origin", origin); 536d5afb2caSAndrew Geissler } 537d5afb2caSAndrew Geissler 5387e860f15SJohn Edward Broadbent inline void handleHypervisorIPv4StaticPatch( 53921fe928bSEd Tanous const std::string& ifaceId, 54021fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input, 5417e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 542d5afb2caSAndrew Geissler { 5437e860f15SJohn Edward Broadbent // Hypervisor considers the first IP address in the array list 5447e860f15SJohn Edward Broadbent // as the Hypervisor's virtual management interface supports single IPv4 5457e860f15SJohn Edward Broadbent // address 54621fe928bSEd Tanous std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson = input[0]; 54721fe928bSEd Tanous nlohmann::json::object_t* obj = 54821fe928bSEd Tanous std::get_if<nlohmann::json::object_t>(&thisJson); 54921fe928bSEd Tanous if (obj == nullptr) 5507e860f15SJohn Edward Broadbent { 55121fe928bSEd Tanous deleteHypervisorIPv4(ifaceId, asyncResp); 55221fe928bSEd Tanous return; 55321fe928bSEd Tanous } 55421fe928bSEd Tanous if (obj->empty()) 55521fe928bSEd Tanous { 55621fe928bSEd Tanous return; 55721fe928bSEd Tanous } 558f8fe53e7SEd Tanous // For the error string 559f8fe53e7SEd Tanous std::string pathString = "IPv4StaticAddresses/1"; 56021fe928bSEd Tanous std::string address; 56121fe928bSEd Tanous std::string subnetMask; 56221fe928bSEd Tanous std::string gateway; 56321fe928bSEd Tanous if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address, 56421fe928bSEd Tanous "SubnetMask", subnetMask, "Gateway", 5657e860f15SJohn Edward Broadbent gateway)) 5667e860f15SJohn Edward Broadbent { 5677e860f15SJohn Edward Broadbent return; 5687e860f15SJohn Edward Broadbent } 5697e860f15SJohn Edward Broadbent 5707e860f15SJohn Edward Broadbent uint8_t prefixLength = 0; 57121fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(address)) 5727e860f15SJohn Edward Broadbent { 57321fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, address, 5747e860f15SJohn Edward Broadbent pathString + "/Address"); 57521fe928bSEd Tanous return; 5767e860f15SJohn Edward Broadbent } 5777e860f15SJohn Edward Broadbent 57821fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(subnetMask, &prefixLength)) 5797e860f15SJohn Edward Broadbent { 58021fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, subnetMask, 5817e860f15SJohn Edward Broadbent pathString + "/SubnetMask"); 58221fe928bSEd Tanous return; 5837e860f15SJohn Edward Broadbent } 5847e860f15SJohn Edward Broadbent 58521fe928bSEd Tanous if (!ip_util::ipv4VerifyIpAndGetBitcount(gateway)) 5867e860f15SJohn Edward Broadbent { 58721fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, gateway, 5887e860f15SJohn Edward Broadbent pathString + "/Gateway"); 5897e860f15SJohn Edward Broadbent return; 5907e860f15SJohn Edward Broadbent } 5917e860f15SJohn Edward Broadbent 59262598e31SEd Tanous BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId, 59321fe928bSEd Tanous address); 59421fe928bSEd Tanous createHypervisorIPv4(ifaceId, prefixLength, gateway, address, asyncResp); 5957e860f15SJohn Edward Broadbent // Set the DHCPEnabled to false since the Static IPv4 is set 5967e860f15SJohn Edward Broadbent setDHCPEnabled(ifaceId, false, asyncResp); 5977e860f15SJohn Edward Broadbent } 5987e860f15SJohn Edward Broadbent 59988a8a174SEd Tanous inline void handleHypervisorHostnamePatch( 60088a8a174SEd Tanous const std::string& hostName, 6017e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6027e860f15SJohn Edward Broadbent { 6037e860f15SJohn Edward Broadbent if (!isHostnameValid(hostName)) 6047e860f15SJohn Edward Broadbent { 6057e860f15SJohn Edward Broadbent messages::propertyValueFormatError(asyncResp->res, hostName, 6067e860f15SJohn Edward Broadbent "HostName"); 6077e860f15SJohn Edward Broadbent return; 6087e860f15SJohn Edward Broadbent } 6097e860f15SJohn Edward Broadbent 6107e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["HostName"] = hostName; 611e93abac6SGinu George setDbusProperty(asyncResp, "HostName", "xyz.openbmc_project.Settings", 612d82b5e1fSAsmitha Karunanithi sdbusplus::message::object_path( 613d82b5e1fSAsmitha Karunanithi "/xyz/openbmc_project/network/hypervisor"), 614d82b5e1fSAsmitha Karunanithi "xyz.openbmc_project.Network.SystemConfiguration", 615e93abac6SGinu George "HostName", hostName); 6167e860f15SJohn Edward Broadbent } 6177e860f15SJohn Edward Broadbent 6187e860f15SJohn Edward Broadbent inline void 61977179532SEd Tanous setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive, 6207e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 6217e860f15SJohn Edward Broadbent { 622e93abac6SGinu George setDbusProperty( 623e93abac6SGinu George asyncResp, "InterfaceEnabled", "xyz.openbmc_project.Settings", 624e93abac6SGinu George "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0", 625e93abac6SGinu George "xyz.openbmc_project.Object.Enable", "Enabled", isActive); 6267e860f15SJohn Edward Broadbent } 6277e860f15SJohn Edward Broadbent 628f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceCollectionGet( 629f40448e5SGunnar Mills App& app, const crow::Request& req, 630f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 631f40448e5SGunnar Mills { 632f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 633f40448e5SGunnar Mills { 634f40448e5SGunnar Mills return; 635f40448e5SGunnar Mills } 636f40448e5SGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 637f40448e5SGunnar Mills "xyz.openbmc_project.Network.EthernetInterface"}; 638f40448e5SGunnar Mills 639f40448e5SGunnar Mills dbus::utility::getSubTreePaths( 640f40448e5SGunnar Mills "/xyz/openbmc_project/network/hypervisor", 0, interfaces, 641f40448e5SGunnar Mills [asyncResp]( 6428b24275dSEd Tanous const boost::system::error_code& ec, 643f40448e5SGunnar Mills const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) { 6448b24275dSEd Tanous if (ec) 645f40448e5SGunnar Mills { 646bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "System", 647bd79bce8SPatrick Williams "hypervisor"); 648f40448e5SGunnar Mills return; 649f40448e5SGunnar Mills } 650f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 651f40448e5SGunnar Mills "#EthernetInterfaceCollection." 652f40448e5SGunnar Mills "EthernetInterfaceCollection"; 653f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 654f40448e5SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 655f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet " 656f40448e5SGunnar Mills "Interface Collection"; 657f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 658f40448e5SGunnar Mills "Collection of Virtual Management " 659f40448e5SGunnar Mills "Interfaces for the hypervisor"; 660f40448e5SGunnar Mills 661f40448e5SGunnar Mills nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 662f40448e5SGunnar Mills ifaceArray = nlohmann::json::array(); 663f40448e5SGunnar Mills for (const std::string& iface : ifaceList) 664f40448e5SGunnar Mills { 665f40448e5SGunnar Mills sdbusplus::message::object_path path(iface); 666f40448e5SGunnar Mills std::string name = path.filename(); 667f40448e5SGunnar Mills if (name.empty()) 668f40448e5SGunnar Mills { 669f40448e5SGunnar Mills continue; 670f40448e5SGunnar Mills } 671f40448e5SGunnar Mills nlohmann::json::object_t ethIface; 672ef4c65b7SEd Tanous ethIface["@odata.id"] = boost::urls::format( 673bd79bce8SPatrick Williams "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", 674bd79bce8SPatrick Williams name); 675b2ba3072SPatrick Williams ifaceArray.emplace_back(std::move(ethIface)); 676f40448e5SGunnar Mills } 677f40448e5SGunnar Mills asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 678f40448e5SGunnar Mills }); 679f40448e5SGunnar Mills } 680f40448e5SGunnar Mills 681f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceGet( 682f40448e5SGunnar Mills App& app, const crow::Request& req, 683f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 684f40448e5SGunnar Mills { 685f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 686f40448e5SGunnar Mills { 687f40448e5SGunnar Mills return; 688f40448e5SGunnar Mills } 689f40448e5SGunnar Mills getHypervisorIfaceData( 690f40448e5SGunnar Mills id, [asyncResp, ifaceId{std::string(id)}]( 69177179532SEd Tanous bool success, const EthernetInterfaceData& ethData, 69277179532SEd Tanous const std::vector<IPv4AddressData>& ipv4Data) { 693f40448e5SGunnar Mills if (!success) 694f40448e5SGunnar Mills { 695f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 696f40448e5SGunnar Mills ifaceId); 697f40448e5SGunnar Mills return; 698f40448e5SGunnar Mills } 699f40448e5SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 70093bbc953SJiaqing Zhao "#EthernetInterface.v1_9_0.EthernetInterface"; 701f40448e5SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface"; 702f40448e5SGunnar Mills asyncResp->res.jsonValue["Description"] = 703f40448e5SGunnar Mills "Hypervisor's Virtual Management Ethernet Interface"; 704f40448e5SGunnar Mills parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData, 705f40448e5SGunnar Mills ipv4Data); 706f40448e5SGunnar Mills }); 707f40448e5SGunnar Mills } 708f40448e5SGunnar Mills 7098d8c30c3SGunnar Mills inline void handleHypervisorSystemGet( 7108d8c30c3SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7118d8c30c3SGunnar Mills { 7128d8c30c3SGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 7138d8c30c3SGunnar Mills "#ComputerSystem.v1_6_0.ComputerSystem"; 71468896206SGunnar Mills asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Systems/hypervisor"; 7158d8c30c3SGunnar Mills asyncResp->res.jsonValue["Description"] = "Hypervisor"; 7168d8c30c3SGunnar Mills asyncResp->res.jsonValue["Name"] = "Hypervisor"; 7178d8c30c3SGunnar Mills asyncResp->res.jsonValue["Id"] = "hypervisor"; 71868896206SGunnar Mills asyncResp->res.jsonValue["SystemType"] = computer_system::SystemType::OS; 7198d8c30c3SGunnar Mills nlohmann::json::array_t managedBy; 7208d8c30c3SGunnar Mills nlohmann::json::object_t manager; 72168896206SGunnar Mills manager["@odata.id"] = boost::urls::format("/redfish/v1/Managers/{}", 72268896206SGunnar Mills BMCWEB_REDFISH_MANAGER_URI_NAME); 723ad539545SPatrick Williams managedBy.emplace_back(std::move(manager)); 72468896206SGunnar Mills asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy); 7258d8c30c3SGunnar Mills asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] = 7268d8c30c3SGunnar Mills "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 7278d8c30c3SGunnar Mills getHypervisorState(asyncResp); 7288d8c30c3SGunnar Mills getHypervisorActions(asyncResp); 7298d8c30c3SGunnar Mills // TODO: Add "SystemType" : "hypervisor" 7308d8c30c3SGunnar Mills } 7318d8c30c3SGunnar Mills 732f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfacePatch( 733f40448e5SGunnar Mills App& app, const crow::Request& req, 734f40448e5SGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 735f40448e5SGunnar Mills const std::string& ifaceId) 736f40448e5SGunnar Mills { 737f40448e5SGunnar Mills if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 738f40448e5SGunnar Mills { 739f40448e5SGunnar Mills return; 740f40448e5SGunnar Mills } 741f40448e5SGunnar Mills std::optional<std::string> hostName; 74221fe928bSEd Tanous std::optional< 74321fe928bSEd Tanous std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>> 74421fe928bSEd Tanous ipv4StaticAddresses; 74521fe928bSEd Tanous std::optional<std::vector<nlohmann::json::object_t>> ipv4Addresses; 746f40448e5SGunnar Mills std::optional<bool> ipv4DHCPEnabled; 747f40448e5SGunnar Mills 748bd79bce8SPatrick Williams if (!json_util::readJsonPatch( 749bd79bce8SPatrick Williams req, asyncResp->res, "HostName", hostName, "IPv4StaticAddresses", 750bd79bce8SPatrick Williams ipv4StaticAddresses, "IPv4Addresses", ipv4Addresses, 75121fe928bSEd Tanous "DHCPv4/DHCPEnabled", ipv4DHCPEnabled)) 752f40448e5SGunnar Mills { 753f40448e5SGunnar Mills return; 754f40448e5SGunnar Mills } 755f40448e5SGunnar Mills 756f40448e5SGunnar Mills if (ipv4Addresses) 757f40448e5SGunnar Mills { 758f40448e5SGunnar Mills messages::propertyNotWritable(asyncResp->res, "IPv4Addresses"); 759f40448e5SGunnar Mills return; 760f40448e5SGunnar Mills } 761f40448e5SGunnar Mills 762f40448e5SGunnar Mills getHypervisorIfaceData( 76321fe928bSEd Tanous ifaceId, 76421fe928bSEd Tanous [asyncResp, ifaceId, hostName = std::move(hostName), 7655a39f77aSPatrick Williams ipv4StaticAddresses = std::move(ipv4StaticAddresses), 76621fe928bSEd Tanous ipv4DHCPEnabled](bool success, const EthernetInterfaceData& ethData, 76721fe928bSEd Tanous const std::vector<IPv4AddressData>&) mutable { 768f40448e5SGunnar Mills if (!success) 769f40448e5SGunnar Mills { 770f40448e5SGunnar Mills messages::resourceNotFound(asyncResp->res, "EthernetInterface", 771f40448e5SGunnar Mills ifaceId); 772f40448e5SGunnar Mills return; 773f40448e5SGunnar Mills } 774f40448e5SGunnar Mills 775f40448e5SGunnar Mills if (ipv4StaticAddresses) 776f40448e5SGunnar Mills { 777bd79bce8SPatrick Williams std::vector<std::variant<nlohmann::json::object_t, 778bd79bce8SPatrick Williams std::nullptr_t>>& ipv4Static = 779bd79bce8SPatrick Williams *ipv4StaticAddresses; 780f40448e5SGunnar Mills if (ipv4Static.begin() == ipv4Static.end()) 781f40448e5SGunnar Mills { 78221fe928bSEd Tanous messages::propertyValueTypeError(asyncResp->res, 78321fe928bSEd Tanous std::vector<std::string>(), 784f40448e5SGunnar Mills "IPv4StaticAddresses"); 785f40448e5SGunnar Mills return; 786f40448e5SGunnar Mills } 787f40448e5SGunnar Mills 788f40448e5SGunnar Mills // One and only one hypervisor instance supported 789f40448e5SGunnar Mills if (ipv4Static.size() != 1) 790f40448e5SGunnar Mills { 79121fe928bSEd Tanous messages::propertyValueFormatError(asyncResp->res, "[]", 792f40448e5SGunnar Mills "IPv4StaticAddresses"); 793f40448e5SGunnar Mills return; 794f40448e5SGunnar Mills } 795f40448e5SGunnar Mills 796bd79bce8SPatrick Williams std::variant<nlohmann::json::object_t, std::nullptr_t>& 797bd79bce8SPatrick Williams ipv4Json = ipv4Static[0]; 798f40448e5SGunnar Mills // Check if the param is 'null'. If its null, it means 799f40448e5SGunnar Mills // that user wants to delete the IP address. Deleting 800f40448e5SGunnar Mills // the IP address is allowed only if its statically 801f40448e5SGunnar Mills // configured. Deleting the address originated from DHCP 802f40448e5SGunnar Mills // is not allowed. 80321fe928bSEd Tanous if (std::holds_alternative<std::nullptr_t>(ipv4Json) && 80421fe928bSEd Tanous translateDhcpEnabledToBool(ethData.dhcpEnabled, true)) 805f40448e5SGunnar Mills { 806bd79bce8SPatrick Williams BMCWEB_LOG_INFO( 807bd79bce8SPatrick Williams "Ignoring the delete on ipv4StaticAddresses " 80862598e31SEd Tanous "as the interface is DHCP enabled"); 809f40448e5SGunnar Mills } 810f40448e5SGunnar Mills else 811f40448e5SGunnar Mills { 812bd79bce8SPatrick Williams handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, 813bd79bce8SPatrick Williams asyncResp); 814f40448e5SGunnar Mills } 815f40448e5SGunnar Mills } 816f40448e5SGunnar Mills 817f40448e5SGunnar Mills if (hostName) 818f40448e5SGunnar Mills { 819f40448e5SGunnar Mills handleHypervisorHostnamePatch(*hostName, asyncResp); 820f40448e5SGunnar Mills } 821f40448e5SGunnar Mills 82221fe928bSEd Tanous if (ipv4DHCPEnabled) 823f40448e5SGunnar Mills { 824f40448e5SGunnar Mills setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp); 825f40448e5SGunnar Mills } 826f40448e5SGunnar Mills 827f40448e5SGunnar Mills // Set this interface to disabled/inactive. This will be set 828f40448e5SGunnar Mills // to enabled/active by the pldm once the hypervisor 829f40448e5SGunnar Mills // consumes the updated settings from the user. 830f40448e5SGunnar Mills setIPv4InterfaceEnabled(ifaceId, false, asyncResp); 831f40448e5SGunnar Mills }); 832f40448e5SGunnar Mills asyncResp->res.result(boost::beast::http::status::accepted); 833f40448e5SGunnar Mills } 834f40448e5SGunnar Mills 835a5d0cedbSGunnar Mills inline void handleHypervisorResetActionGet( 836a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 837a5d0cedbSGunnar Mills { 838a5d0cedbSGunnar Mills // Only return action info if hypervisor D-Bus object present 839a5d0cedbSGunnar Mills constexpr std::array<std::string_view, 1> interfaces = { 840a5d0cedbSGunnar Mills "xyz.openbmc_project.State.Host"}; 841a5d0cedbSGunnar Mills dbus::utility::getDbusObject( 842a5d0cedbSGunnar Mills "/xyz/openbmc_project/state/hypervisor0", interfaces, 843a5d0cedbSGunnar Mills [asyncResp]( 844a5d0cedbSGunnar Mills const boost::system::error_code& ec, 845a5d0cedbSGunnar Mills const std::vector<std::pair<std::string, std::vector<std::string>>>& 846a5d0cedbSGunnar Mills objInfo) { 847a5d0cedbSGunnar Mills if (ec) 848a5d0cedbSGunnar Mills { 84962598e31SEd Tanous BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 850a5d0cedbSGunnar Mills 851a5d0cedbSGunnar Mills // No hypervisor objects found by mapper 852a5d0cedbSGunnar Mills if (ec.value() == boost::system::errc::io_error) 853a5d0cedbSGunnar Mills { 854a5d0cedbSGunnar Mills messages::resourceNotFound(asyncResp->res, "hypervisor", 855a5d0cedbSGunnar Mills "ResetActionInfo"); 856a5d0cedbSGunnar Mills return; 857a5d0cedbSGunnar Mills } 858a5d0cedbSGunnar Mills 859a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 860a5d0cedbSGunnar Mills return; 861a5d0cedbSGunnar Mills } 862a5d0cedbSGunnar Mills 863a5d0cedbSGunnar Mills // One and only one hypervisor instance supported 864a5d0cedbSGunnar Mills if (objInfo.size() != 1) 865a5d0cedbSGunnar Mills { 866a5d0cedbSGunnar Mills messages::internalError(asyncResp->res); 867a5d0cedbSGunnar Mills return; 868a5d0cedbSGunnar Mills } 869a5d0cedbSGunnar Mills 870a5d0cedbSGunnar Mills // The hypervisor object only support the ability to 871a5d0cedbSGunnar Mills // turn On The system object Action should be utilized 872a5d0cedbSGunnar Mills // for other operations 873a5d0cedbSGunnar Mills 874a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.type"] = 875a5d0cedbSGunnar Mills "#ActionInfo.v1_1_2.ActionInfo"; 876a5d0cedbSGunnar Mills asyncResp->res.jsonValue["@odata.id"] = 877a5d0cedbSGunnar Mills "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 878a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Name"] = "Reset Action Info"; 879a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Id"] = "ResetActionInfo"; 880a5d0cedbSGunnar Mills nlohmann::json::array_t parameters; 881a5d0cedbSGunnar Mills nlohmann::json::object_t parameter; 882a5d0cedbSGunnar Mills parameter["Name"] = "ResetType"; 883a5d0cedbSGunnar Mills parameter["Required"] = true; 884539d8c6bSEd Tanous parameter["DataType"] = action_info::ParameterTypes::String; 885a5d0cedbSGunnar Mills nlohmann::json::array_t allowed; 886ad539545SPatrick Williams allowed.emplace_back("On"); 887a5d0cedbSGunnar Mills parameter["AllowableValues"] = std::move(allowed); 888ad539545SPatrick Williams parameters.emplace_back(std::move(parameter)); 889a5d0cedbSGunnar Mills asyncResp->res.jsonValue["Parameters"] = std::move(parameters); 890a5d0cedbSGunnar Mills }); 891a5d0cedbSGunnar Mills } 892a5d0cedbSGunnar Mills 893a5d0cedbSGunnar Mills inline void handleHypervisorSystemResetPost( 894*dd7090e6SGunnar Mills const crow::Request& req, 895a5d0cedbSGunnar Mills const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 896a5d0cedbSGunnar Mills { 897a5d0cedbSGunnar Mills std::optional<std::string> resetType; 898a5d0cedbSGunnar Mills if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType)) 899a5d0cedbSGunnar Mills { 900a5d0cedbSGunnar Mills // readJson adds appropriate error to response 901a5d0cedbSGunnar Mills return; 902a5d0cedbSGunnar Mills } 903a5d0cedbSGunnar Mills 904a5d0cedbSGunnar Mills if (!resetType) 905a5d0cedbSGunnar Mills { 906a5d0cedbSGunnar Mills messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset", 907a5d0cedbSGunnar Mills "ResetType"); 908a5d0cedbSGunnar Mills return; 909a5d0cedbSGunnar Mills } 910a5d0cedbSGunnar Mills 911a5d0cedbSGunnar Mills // Hypervisor object only support On operation 912a5d0cedbSGunnar Mills if (resetType != "On") 913a5d0cedbSGunnar Mills { 914a5d0cedbSGunnar Mills messages::propertyValueNotInList(asyncResp->res, *resetType, 915a5d0cedbSGunnar Mills "ResetType"); 916a5d0cedbSGunnar Mills return; 917a5d0cedbSGunnar Mills } 918a5d0cedbSGunnar Mills 919a5d0cedbSGunnar Mills std::string command = "xyz.openbmc_project.State.Host.Transition.On"; 920a5d0cedbSGunnar Mills 921bd79bce8SPatrick Williams setDbusPropertyAction( 922bd79bce8SPatrick Williams asyncResp, "xyz.openbmc_project.State.Hypervisor", 9231827b4f1SAsmitha Karunanithi sdbusplus::message::object_path( 9241827b4f1SAsmitha Karunanithi "/xyz/openbmc_project/state/hypervisor0"), 925bd79bce8SPatrick Williams "xyz.openbmc_project.State.Host", "RequestedHostTransition", 926bd79bce8SPatrick Williams "ResetType", "ComputerSystem.Reset", command); 927a5d0cedbSGunnar Mills } 928a5d0cedbSGunnar Mills 9297e860f15SJohn Edward Broadbent inline void requestRoutesHypervisorSystems(App& app) 9307e860f15SJohn Edward Broadbent { 9317e860f15SJohn Edward Broadbent /** 9327e860f15SJohn Edward Broadbent * HypervisorInterfaceCollection class to handle the GET and PATCH on 9337e860f15SJohn Edward Broadbent * Hypervisor Interface 9347e860f15SJohn Edward Broadbent */ 9357e860f15SJohn Edward Broadbent 9367e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/") 937ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterfaceCollection) 938f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 939f40448e5SGunnar Mills handleHypervisorEthernetInterfaceCollectionGet, std::ref(app))); 9407e860f15SJohn Edward Broadbent 9417e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9427e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 943ed398213SEd Tanous .privileges(redfish::privileges::getEthernetInterface) 944f40448e5SGunnar Mills .methods(boost::beast::http::verb::get)(std::bind_front( 945f40448e5SGunnar Mills handleHypervisorEthernetInterfaceGet, std::ref(app))); 946d5afb2caSAndrew Geissler 9477e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, 9487e860f15SJohn Edward Broadbent "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 949ed398213SEd Tanous .privileges(redfish::privileges::patchEthernetInterface) 950f40448e5SGunnar Mills .methods(boost::beast::http::verb::patch)(std::bind_front( 951f40448e5SGunnar Mills handleHypervisorEthernetInterfacePatch, std::ref(app))); 9524fbaf64aSAndrew Geissler } 95388a8a174SEd Tanous } // namespace redfish 954