1 #pragma once 2 3 #include "app.hpp" 4 #include "dbus_singleton.hpp" 5 #include "dbus_utility.hpp" 6 #include "error_messages.hpp" 7 #include "ethernet.hpp" 8 #include "query.hpp" 9 #include "registries/privilege_registry.hpp" 10 #include "utils/ip_utils.hpp" 11 #include "utils/json_utils.hpp" 12 13 #include <boost/url/format.hpp> 14 #include <sdbusplus/asio/property.hpp> 15 16 #include <array> 17 #include <optional> 18 #include <string_view> 19 #include <utility> 20 21 namespace redfish 22 { 23 24 /** 25 * @brief Retrieves hypervisor state properties over dbus 26 * 27 * The hypervisor state object is optional so this function will only set the 28 * state variables if the object is found 29 * 30 * @param[in] asyncResp Shared pointer for completing asynchronous calls. 31 * 32 * @return None. 33 */ 34 inline void 35 getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 36 { 37 BMCWEB_LOG_DEBUG("Get hypervisor state information."); 38 sdbusplus::asio::getProperty<std::string>( 39 *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor", 40 "/xyz/openbmc_project/state/hypervisor0", 41 "xyz.openbmc_project.State.Host", "CurrentHostState", 42 [asyncResp](const boost::system::error_code& ec, 43 const std::string& hostState) { 44 if (ec) 45 { 46 BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 47 // This is an optional D-Bus object so just return if 48 // error occurs 49 return; 50 } 51 52 BMCWEB_LOG_DEBUG("Hypervisor state: {}", hostState); 53 // Verify Host State 54 if (hostState == "xyz.openbmc_project.State.Host.HostState.Running") 55 { 56 asyncResp->res.jsonValue["PowerState"] = "On"; 57 asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 58 } 59 else if (hostState == "xyz.openbmc_project.State.Host.HostState." 60 "Quiesced") 61 { 62 asyncResp->res.jsonValue["PowerState"] = "On"; 63 asyncResp->res.jsonValue["Status"]["State"] = "Quiesced"; 64 } 65 else if (hostState == "xyz.openbmc_project.State.Host.HostState." 66 "Standby") 67 { 68 asyncResp->res.jsonValue["PowerState"] = "On"; 69 asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline"; 70 } 71 else if (hostState == "xyz.openbmc_project.State.Host.HostState." 72 "TransitioningToRunning") 73 { 74 asyncResp->res.jsonValue["PowerState"] = "PoweringOn"; 75 asyncResp->res.jsonValue["Status"]["State"] = "Starting"; 76 } 77 else if (hostState == "xyz.openbmc_project.State.Host.HostState." 78 "TransitioningToOff") 79 { 80 asyncResp->res.jsonValue["PowerState"] = "PoweringOff"; 81 asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 82 } 83 else if (hostState == "xyz.openbmc_project.State.Host.HostState.Off") 84 { 85 asyncResp->res.jsonValue["PowerState"] = "Off"; 86 asyncResp->res.jsonValue["Status"]["State"] = "Disabled"; 87 } 88 else 89 { 90 messages::internalError(asyncResp->res); 91 return; 92 } 93 }); 94 } 95 96 /** 97 * @brief Populate Actions if any are valid for hypervisor object 98 * 99 * The hypervisor state object is optional so this function will only set the 100 * Action if the object is found 101 * 102 * @param[in] asyncResp Shared pointer for completing asynchronous calls. 103 * 104 * @return None. 105 */ 106 inline void 107 getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 108 { 109 BMCWEB_LOG_DEBUG("Get hypervisor actions."); 110 constexpr std::array<std::string_view, 1> interfaces = { 111 "xyz.openbmc_project.State.Host"}; 112 dbus::utility::getDbusObject( 113 "/xyz/openbmc_project/state/hypervisor0", interfaces, 114 [asyncResp]( 115 const boost::system::error_code& ec, 116 const std::vector<std::pair<std::string, std::vector<std::string>>>& 117 objInfo) { 118 if (ec) 119 { 120 BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 121 // This is an optional D-Bus object so just return if 122 // error occurs 123 return; 124 } 125 126 if (objInfo.empty()) 127 { 128 // As noted above, this is an optional interface so just return 129 // if there is no instance found 130 return; 131 } 132 133 if (objInfo.size() > 1) 134 { 135 // More then one hypervisor object is not supported and is an 136 // error 137 messages::internalError(asyncResp->res); 138 return; 139 } 140 141 // Object present so system support limited ComputerSystem Action 142 nlohmann::json& reset = 143 asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"]; 144 reset["target"] = 145 "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset"; 146 reset["@Redfish.ActionInfo"] = 147 "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 148 }); 149 } 150 151 inline bool extractHypervisorInterfaceData( 152 const std::string& ethIfaceId, 153 const dbus::utility::ManagedObjectType& dbusData, 154 EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config) 155 { 156 bool idFound = false; 157 for (const auto& objpath : dbusData) 158 { 159 for (const auto& ifacePair : objpath.second) 160 { 161 if (objpath.first == 162 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId) 163 { 164 idFound = true; 165 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 166 { 167 for (const auto& propertyPair : ifacePair.second) 168 { 169 if (propertyPair.first == "MACAddress") 170 { 171 const std::string* mac = 172 std::get_if<std::string>(&propertyPair.second); 173 if (mac != nullptr) 174 { 175 ethData.macAddress = *mac; 176 } 177 } 178 } 179 } 180 else if (ifacePair.first == 181 "xyz.openbmc_project.Network.EthernetInterface") 182 { 183 for (const auto& propertyPair : ifacePair.second) 184 { 185 if (propertyPair.first == "DHCPEnabled") 186 { 187 const std::string* dhcp = 188 std::get_if<std::string>(&propertyPair.second); 189 if (dhcp != nullptr) 190 { 191 ethData.dhcpEnabled = *dhcp; 192 break; // Interested on only "DHCPEnabled". 193 // Stop parsing since we got the 194 // "DHCPEnabled" value. 195 } 196 } 197 } 198 } 199 } 200 if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" + 201 ethIfaceId + "/ipv4/addr0") 202 { 203 IPv4AddressData& ipv4Address = ipv4Config.emplace_back(); 204 if (ifacePair.first == "xyz.openbmc_project.Object.Enable") 205 { 206 for (const auto& property : ifacePair.second) 207 { 208 if (property.first == "Enabled") 209 { 210 const bool* intfEnable = 211 std::get_if<bool>(&property.second); 212 if (intfEnable != nullptr) 213 { 214 ipv4Address.isActive = *intfEnable; 215 break; 216 } 217 } 218 } 219 } 220 if (ifacePair.first == "xyz.openbmc_project.Network.IP") 221 { 222 for (const auto& property : ifacePair.second) 223 { 224 if (property.first == "Address") 225 { 226 const std::string* address = 227 std::get_if<std::string>(&property.second); 228 if (address != nullptr) 229 { 230 ipv4Address.address = *address; 231 } 232 } 233 else if (property.first == "Origin") 234 { 235 const std::string* origin = 236 std::get_if<std::string>(&property.second); 237 if (origin != nullptr) 238 { 239 ipv4Address.origin = 240 translateAddressOriginDbusToRedfish(*origin, 241 true); 242 } 243 } 244 else if (property.first == "PrefixLength") 245 { 246 const uint8_t* mask = 247 std::get_if<uint8_t>(&property.second); 248 if (mask != nullptr) 249 { 250 // convert it to the string 251 ipv4Address.netmask = getNetmask(*mask); 252 } 253 } 254 else if (property.first == "Type" || 255 property.first == "Gateway") 256 { 257 // Type & Gateway is not used 258 continue; 259 } 260 else 261 { 262 BMCWEB_LOG_ERROR( 263 "Got extra property: {} on the {} object", 264 property.first, objpath.first.str); 265 } 266 } 267 } 268 } 269 if (objpath.first == "/xyz/openbmc_project/network/hypervisor") 270 { 271 // System configuration shows up in the global namespace, so no 272 // need to check eth number 273 if (ifacePair.first == 274 "xyz.openbmc_project.Network.SystemConfiguration") 275 { 276 for (const auto& propertyPair : ifacePair.second) 277 { 278 if (propertyPair.first == "HostName") 279 { 280 const std::string* hostName = 281 std::get_if<std::string>(&propertyPair.second); 282 if (hostName != nullptr) 283 { 284 ethData.hostName = *hostName; 285 } 286 } 287 else if (propertyPair.first == "DefaultGateway") 288 { 289 const std::string* defaultGateway = 290 std::get_if<std::string>(&propertyPair.second); 291 if (defaultGateway != nullptr) 292 { 293 ethData.defaultGateway = *defaultGateway; 294 } 295 } 296 } 297 } 298 } 299 } 300 } 301 return idFound; 302 } 303 /** 304 * Function that retrieves all properties for given Hypervisor Ethernet 305 * Interface Object from Settings Manager 306 * @param ethIfaceId Hypervisor ethernet interface id to query on DBus 307 * @param callback a function that shall be called to convert Dbus output 308 * into JSON 309 */ 310 template <typename CallbackFunc> 311 void getHypervisorIfaceData(const std::string& ethIfaceId, 312 CallbackFunc&& callback) 313 { 314 sdbusplus::message::object_path path("/"); 315 dbus::utility::getManagedObjects( 316 "xyz.openbmc_project.Settings", path, 317 [ethIfaceId{std::string{ethIfaceId}}, 318 callback{std::forward<CallbackFunc>(callback)}]( 319 const boost::system::error_code& ec, 320 const dbus::utility::ManagedObjectType& resp) { 321 EthernetInterfaceData ethData{}; 322 std::vector<IPv4AddressData> ipv4Data; 323 if (ec) 324 { 325 callback(false, ethData, ipv4Data); 326 return; 327 } 328 329 bool found = extractHypervisorInterfaceData(ethIfaceId, resp, ethData, 330 ipv4Data); 331 if (!found) 332 { 333 BMCWEB_LOG_INFO("Hypervisor Interface not found"); 334 } 335 callback(found, ethData, ipv4Data); 336 }); 337 } 338 339 /** 340 * @brief Sets the Hypervisor Interface IPAddress DBUS 341 * 342 * @param[in] asyncResp Shared pointer for generating response message. 343 * @param[in] ipv4Address Address from the incoming request 344 * @param[in] ethIfaceId Hypervisor Interface Id 345 * 346 * @return None. 347 */ 348 inline void setHypervisorIPv4Address( 349 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 350 const std::string& ethIfaceId, const std::string& ipv4Address) 351 { 352 BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}", 353 ipv4Address, ethIfaceId); 354 355 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 356 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + 357 "/ipv4/addr0", 358 "xyz.openbmc_project.Network.IP", "Address", 359 "IPv4StaticAddresses/1/Address", ipv4Address); 360 } 361 362 /** 363 * @brief Sets the Hypervisor Interface SubnetMask DBUS 364 * 365 * @param[in] asyncResp Shared pointer for generating response message. 366 * @param[in] subnet SubnetMask from the incoming request 367 * @param[in] ethIfaceId Hypervisor Interface Id 368 * 369 * @return None. 370 */ 371 inline void 372 setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 373 const std::string& ethIfaceId, const uint8_t subnet) 374 { 375 BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet, 376 ethIfaceId); 377 378 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 379 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + 380 "/ipv4/addr0", 381 "xyz.openbmc_project.Network.IP", "PrefixLength", 382 "IPv4StaticAddresses/1/SubnetMask", subnet); 383 } 384 385 /** 386 * @brief Sets the Hypervisor Interface Gateway DBUS 387 * 388 * @param[in] asyncResp Shared pointer for generating response message. 389 * @param[in] gateway Gateway from the incoming request 390 * @param[in] ethIfaceId Hypervisor Interface Id 391 * 392 * @return None. 393 */ 394 inline void setHypervisorIPv4Gateway( 395 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 396 const std::string& gateway) 397 { 398 BMCWEB_LOG_DEBUG( 399 "Setting the DefaultGateway to the last configured gateway"); 400 401 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 402 sdbusplus::message::object_path( 403 "/xyz/openbmc_project/network/hypervisor"), 404 "xyz.openbmc_project.Network.SystemConfiguration", 405 "DefaultGateway", "IPv4StaticAddresses/1/Gateway", gateway); 406 } 407 408 /** 409 * @brief Creates a static IPv4 entry 410 * 411 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry 412 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask 413 * @param[in] gateway IPv4 address of this interfaces gateway 414 * @param[in] address IPv4 address to assign to this interface 415 * @param[io] asyncResp Response object that will be returned to client 416 * 417 * @return None 418 */ 419 inline void 420 createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength, 421 const std::string& gateway, const std::string& address, 422 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 423 { 424 setHypervisorIPv4Address(asyncResp, ifaceId, address); 425 setHypervisorIPv4Gateway(asyncResp, gateway); 426 setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 427 } 428 429 /** 430 * @brief Deletes given IPv4 interface 431 * 432 * @param[in] ifaceId Id of interface whose IP should be deleted 433 * @param[io] asyncResp Response object that will be returned to client 434 * 435 * @return None 436 */ 437 inline void 438 deleteHypervisorIPv4(const std::string& ifaceId, 439 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 440 { 441 std::string address = "0.0.0.0"; 442 std::string gateway = "0.0.0.0"; 443 const uint8_t prefixLength = 0; 444 setHypervisorIPv4Address(asyncResp, ifaceId, address); 445 setHypervisorIPv4Gateway(asyncResp, gateway); 446 setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength); 447 } 448 449 inline void parseInterfaceData(nlohmann::json& jsonResponse, 450 const std::string& ifaceId, 451 const EthernetInterfaceData& ethData, 452 const std::vector<IPv4AddressData>& ipv4Data) 453 { 454 jsonResponse["Id"] = ifaceId; 455 jsonResponse["@odata.id"] = boost::urls::format( 456 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId); 457 jsonResponse["InterfaceEnabled"] = true; 458 jsonResponse["MACAddress"] = ethData.macAddress; 459 460 jsonResponse["HostName"] = ethData.hostName; 461 jsonResponse["DHCPv4"]["DHCPEnabled"] = 462 translateDhcpEnabledToBool(ethData.dhcpEnabled, true); 463 464 nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"]; 465 nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"]; 466 ipv4Array = nlohmann::json::array(); 467 ipv4StaticArray = nlohmann::json::array(); 468 for (const auto& ipv4Config : ipv4Data) 469 { 470 if (ipv4Config.isActive) 471 { 472 nlohmann::json::object_t ipv4; 473 ipv4["AddressOrigin"] = ipv4Config.origin; 474 ipv4["SubnetMask"] = ipv4Config.netmask; 475 ipv4["Address"] = ipv4Config.address; 476 ipv4["Gateway"] = ethData.defaultGateway; 477 478 if (ipv4Config.origin == "Static") 479 { 480 ipv4StaticArray.push_back(ipv4); 481 } 482 ipv4Array.emplace_back(std::move(ipv4)); 483 } 484 } 485 } 486 487 inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled, 488 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 489 { 490 const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false); 491 492 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 493 sdbusplus::message::object_path( 494 "/xyz/openbmc_project/network/hypervisor") / 495 ifaceId, 496 "xyz.openbmc_project.Network.EthernetInterface", 497 "DHCPEnabled", "DHCPv4/DHCPEnabled", dhcp); 498 499 // Set the IPv4 address origin to the DHCP / Static as per the new value 500 // of the DHCPEnabled property 501 std::string origin; 502 if (!ipv4DHCPEnabled) 503 { 504 origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 505 } 506 else 507 { 508 // DHCPEnabled is set to true. Delete the current IPv4 settings 509 // to receive the new values from DHCP server. 510 deleteHypervisorIPv4(ifaceId, asyncResp); 511 origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 512 } 513 514 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 515 "/xyz/openbmc_project/network/hypervisor/" + ifaceId + 516 "/ipv4/addr0", 517 "xyz.openbmc_project.Network.IP", "Origin", 518 "IPv4StaticAddresses/1/AddressOrigin", origin); 519 } 520 521 inline void handleHypervisorIPv4StaticPatch( 522 const std::string& ifaceId, const nlohmann::json& input, 523 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 524 { 525 if ((!input.is_array()) || input.empty()) 526 { 527 messages::propertyValueTypeError(asyncResp->res, input, 528 "IPv4StaticAddresses"); 529 return; 530 } 531 532 // Hypervisor considers the first IP address in the array list 533 // as the Hypervisor's virtual management interface supports single IPv4 534 // address 535 const nlohmann::json& thisJson = input[0]; 536 537 if (!thisJson.is_null() && !thisJson.empty()) 538 { 539 // For the error string 540 std::string pathString = "IPv4StaticAddresses/1"; 541 std::optional<std::string> address; 542 std::optional<std::string> subnetMask; 543 std::optional<std::string> gateway; 544 nlohmann::json thisJsonCopy = thisJson; 545 if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address", 546 address, "SubnetMask", subnetMask, "Gateway", 547 gateway)) 548 { 549 messages::propertyValueFormatError(asyncResp->res, thisJson, 550 pathString); 551 return; 552 } 553 554 uint8_t prefixLength = 0; 555 bool errorInEntry = false; 556 if (address) 557 { 558 if (!ip_util::ipv4VerifyIpAndGetBitcount(*address)) 559 { 560 messages::propertyValueFormatError(asyncResp->res, *address, 561 pathString + "/Address"); 562 errorInEntry = true; 563 } 564 } 565 else 566 { 567 messages::propertyMissing(asyncResp->res, pathString + "/Address"); 568 errorInEntry = true; 569 } 570 571 if (subnetMask) 572 { 573 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask, 574 &prefixLength)) 575 { 576 messages::propertyValueFormatError(asyncResp->res, *subnetMask, 577 pathString + "/SubnetMask"); 578 errorInEntry = true; 579 } 580 } 581 else 582 { 583 messages::propertyMissing(asyncResp->res, 584 pathString + "/SubnetMask"); 585 errorInEntry = true; 586 } 587 588 if (gateway) 589 { 590 if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway)) 591 { 592 messages::propertyValueFormatError(asyncResp->res, *gateway, 593 pathString + "/Gateway"); 594 errorInEntry = true; 595 } 596 } 597 else 598 { 599 messages::propertyMissing(asyncResp->res, pathString + "/Gateway"); 600 errorInEntry = true; 601 } 602 603 if (errorInEntry) 604 { 605 return; 606 } 607 608 BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId, 609 *address); 610 createHypervisorIPv4(ifaceId, prefixLength, *gateway, *address, 611 asyncResp); 612 // Set the DHCPEnabled to false since the Static IPv4 is set 613 setDHCPEnabled(ifaceId, false, asyncResp); 614 } 615 else 616 { 617 if (thisJson.is_null()) 618 { 619 deleteHypervisorIPv4(ifaceId, asyncResp); 620 } 621 } 622 } 623 624 inline void handleHypervisorHostnamePatch( 625 const std::string& hostName, 626 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 627 { 628 if (!isHostnameValid(hostName)) 629 { 630 messages::propertyValueFormatError(asyncResp->res, hostName, 631 "HostName"); 632 return; 633 } 634 635 asyncResp->res.jsonValue["HostName"] = hostName; 636 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 637 sdbusplus::message::object_path( 638 "/xyz/openbmc_project/network/hypervisor"), 639 "xyz.openbmc_project.Network.SystemConfiguration", 640 "HostName", "HostName", hostName); 641 } 642 643 inline void 644 setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive, 645 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 646 { 647 setDbusProperty(asyncResp, "xyz.openbmc_project.Settings", 648 "/xyz/openbmc_project/network/hypervisor/" + ifaceId + 649 "/ipv4/addr0", 650 "xyz.openbmc_project.Object.Enable", "Enabled", 651 "InterfaceEnabled", isActive); 652 } 653 654 inline void handleHypervisorEthernetInterfaceCollectionGet( 655 App& app, const crow::Request& req, 656 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 657 { 658 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 659 { 660 return; 661 } 662 constexpr std::array<std::string_view, 1> interfaces = { 663 "xyz.openbmc_project.Network.EthernetInterface"}; 664 665 dbus::utility::getSubTreePaths( 666 "/xyz/openbmc_project/network/hypervisor", 0, interfaces, 667 [asyncResp]( 668 const boost::system::error_code& ec, 669 const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) { 670 if (ec) 671 { 672 messages::resourceNotFound(asyncResp->res, "System", "hypervisor"); 673 return; 674 } 675 asyncResp->res.jsonValue["@odata.type"] = 676 "#EthernetInterfaceCollection." 677 "EthernetInterfaceCollection"; 678 asyncResp->res.jsonValue["@odata.id"] = 679 "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 680 asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet " 681 "Interface Collection"; 682 asyncResp->res.jsonValue["Description"] = 683 "Collection of Virtual Management " 684 "Interfaces for the hypervisor"; 685 686 nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"]; 687 ifaceArray = nlohmann::json::array(); 688 for (const std::string& iface : ifaceList) 689 { 690 sdbusplus::message::object_path path(iface); 691 std::string name = path.filename(); 692 if (name.empty()) 693 { 694 continue; 695 } 696 nlohmann::json::object_t ethIface; 697 ethIface["@odata.id"] = boost::urls::format( 698 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", name); 699 ifaceArray.emplace_back(std::move(ethIface)); 700 } 701 asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size(); 702 }); 703 } 704 705 inline void handleHypervisorEthernetInterfaceGet( 706 App& app, const crow::Request& req, 707 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 708 { 709 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 710 { 711 return; 712 } 713 getHypervisorIfaceData( 714 id, [asyncResp, ifaceId{std::string(id)}]( 715 bool success, const EthernetInterfaceData& ethData, 716 const std::vector<IPv4AddressData>& ipv4Data) { 717 if (!success) 718 { 719 messages::resourceNotFound(asyncResp->res, "EthernetInterface", 720 ifaceId); 721 return; 722 } 723 asyncResp->res.jsonValue["@odata.type"] = 724 "#EthernetInterface.v1_9_0.EthernetInterface"; 725 asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface"; 726 asyncResp->res.jsonValue["Description"] = 727 "Hypervisor's Virtual Management Ethernet Interface"; 728 parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData, 729 ipv4Data); 730 }); 731 } 732 733 inline void handleHypervisorSystemGet( 734 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 735 { 736 sdbusplus::asio::getProperty<std::string>( 737 *crow::connections::systemBus, "xyz.openbmc_project.Settings", 738 "/xyz/openbmc_project/network/hypervisor", 739 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 740 [asyncResp](const boost::system::error_code& ec, 741 const std::string& /*hostName*/) { 742 if (ec) 743 { 744 messages::resourceNotFound(asyncResp->res, "System", "hypervisor"); 745 return; 746 } 747 BMCWEB_LOG_DEBUG("Hypervisor is available"); 748 749 asyncResp->res.jsonValue["@odata.type"] = 750 "#ComputerSystem.v1_6_0.ComputerSystem"; 751 asyncResp->res.jsonValue["@odata.id"] = 752 "/redfish/v1/Systems/hypervisor"; 753 asyncResp->res.jsonValue["Description"] = "Hypervisor"; 754 asyncResp->res.jsonValue["Name"] = "Hypervisor"; 755 asyncResp->res.jsonValue["Id"] = "hypervisor"; 756 asyncResp->res.jsonValue["SystemType"] = "OS"; 757 nlohmann::json::array_t managedBy; 758 nlohmann::json::object_t manager; 759 manager["@odata.id"] = "/redfish/v1/Managers/bmc"; 760 managedBy.emplace_back(std::move(manager)); 761 asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy); 762 asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] = 763 "/redfish/v1/Systems/hypervisor/EthernetInterfaces"; 764 getHypervisorState(asyncResp); 765 getHypervisorActions(asyncResp); 766 // TODO: Add "SystemType" : "hypervisor" 767 }); 768 } 769 770 inline void handleHypervisorEthernetInterfacePatch( 771 App& app, const crow::Request& req, 772 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 773 const std::string& ifaceId) 774 { 775 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 776 { 777 return; 778 } 779 std::optional<std::string> hostName; 780 std::optional<std::vector<nlohmann::json>> ipv4StaticAddresses; 781 std::optional<nlohmann::json> ipv4Addresses; 782 std::optional<nlohmann::json> dhcpv4; 783 std::optional<bool> ipv4DHCPEnabled; 784 785 if (!json_util::readJsonPatch(req, asyncResp->res, "HostName", hostName, 786 "IPv4StaticAddresses", ipv4StaticAddresses, 787 "IPv4Addresses", ipv4Addresses, "DHCPv4", 788 dhcpv4)) 789 { 790 return; 791 } 792 793 if (ipv4Addresses) 794 { 795 messages::propertyNotWritable(asyncResp->res, "IPv4Addresses"); 796 return; 797 } 798 799 if (dhcpv4) 800 { 801 if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled", 802 ipv4DHCPEnabled)) 803 { 804 return; 805 } 806 } 807 808 getHypervisorIfaceData( 809 ifaceId, [asyncResp, ifaceId, hostName = std::move(hostName), 810 ipv4StaticAddresses = std::move(ipv4StaticAddresses), 811 ipv4DHCPEnabled, dhcpv4 = std::move(dhcpv4)]( 812 bool success, const EthernetInterfaceData& ethData, 813 const std::vector<IPv4AddressData>&) { 814 if (!success) 815 { 816 messages::resourceNotFound(asyncResp->res, "EthernetInterface", 817 ifaceId); 818 return; 819 } 820 821 if (ipv4StaticAddresses) 822 { 823 const nlohmann::json& ipv4Static = *ipv4StaticAddresses; 824 if (ipv4Static.begin() == ipv4Static.end()) 825 { 826 messages::propertyValueTypeError(asyncResp->res, ipv4Static, 827 "IPv4StaticAddresses"); 828 return; 829 } 830 831 // One and only one hypervisor instance supported 832 if (ipv4Static.size() != 1) 833 { 834 messages::propertyValueFormatError(asyncResp->res, ipv4Static, 835 "IPv4StaticAddresses"); 836 return; 837 } 838 839 const nlohmann::json& ipv4Json = ipv4Static[0]; 840 // Check if the param is 'null'. If its null, it means 841 // that user wants to delete the IP address. Deleting 842 // the IP address is allowed only if its statically 843 // configured. Deleting the address originated from DHCP 844 // is not allowed. 845 if ((ipv4Json.is_null()) && 846 (translateDhcpEnabledToBool(ethData.dhcpEnabled, true))) 847 { 848 BMCWEB_LOG_INFO("Ignoring the delete on ipv4StaticAddresses " 849 "as the interface is DHCP enabled"); 850 } 851 else 852 { 853 handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, asyncResp); 854 } 855 } 856 857 if (hostName) 858 { 859 handleHypervisorHostnamePatch(*hostName, asyncResp); 860 } 861 862 if (dhcpv4) 863 { 864 setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp); 865 } 866 867 // Set this interface to disabled/inactive. This will be set 868 // to enabled/active by the pldm once the hypervisor 869 // consumes the updated settings from the user. 870 setIPv4InterfaceEnabled(ifaceId, false, asyncResp); 871 }); 872 asyncResp->res.result(boost::beast::http::status::accepted); 873 } 874 875 inline void handleHypervisorResetActionGet( 876 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 877 { 878 // Only return action info if hypervisor D-Bus object present 879 constexpr std::array<std::string_view, 1> interfaces = { 880 "xyz.openbmc_project.State.Host"}; 881 dbus::utility::getDbusObject( 882 "/xyz/openbmc_project/state/hypervisor0", interfaces, 883 [asyncResp]( 884 const boost::system::error_code& ec, 885 const std::vector<std::pair<std::string, std::vector<std::string>>>& 886 objInfo) { 887 if (ec) 888 { 889 BMCWEB_LOG_DEBUG("DBUS response error {}", ec); 890 891 // No hypervisor objects found by mapper 892 if (ec.value() == boost::system::errc::io_error) 893 { 894 messages::resourceNotFound(asyncResp->res, "hypervisor", 895 "ResetActionInfo"); 896 return; 897 } 898 899 messages::internalError(asyncResp->res); 900 return; 901 } 902 903 // One and only one hypervisor instance supported 904 if (objInfo.size() != 1) 905 { 906 messages::internalError(asyncResp->res); 907 return; 908 } 909 910 // The hypervisor object only support the ability to 911 // turn On The system object Action should be utilized 912 // for other operations 913 914 asyncResp->res.jsonValue["@odata.type"] = 915 "#ActionInfo.v1_1_2.ActionInfo"; 916 asyncResp->res.jsonValue["@odata.id"] = 917 "/redfish/v1/Systems/hypervisor/ResetActionInfo"; 918 asyncResp->res.jsonValue["Name"] = "Reset Action Info"; 919 asyncResp->res.jsonValue["Id"] = "ResetActionInfo"; 920 nlohmann::json::array_t parameters; 921 nlohmann::json::object_t parameter; 922 parameter["Name"] = "ResetType"; 923 parameter["Required"] = true; 924 parameter["DataType"] = "String"; 925 nlohmann::json::array_t allowed; 926 allowed.emplace_back("On"); 927 parameter["AllowableValues"] = std::move(allowed); 928 parameters.emplace_back(std::move(parameter)); 929 asyncResp->res.jsonValue["Parameters"] = std::move(parameters); 930 }); 931 } 932 933 inline void handleHypervisorSystemResetPost( 934 App& app, const crow::Request& req, 935 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 936 { 937 if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 938 { 939 return; 940 } 941 std::optional<std::string> resetType; 942 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType)) 943 { 944 // readJson adds appropriate error to response 945 return; 946 } 947 948 if (!resetType) 949 { 950 messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset", 951 "ResetType"); 952 return; 953 } 954 955 // Hypervisor object only support On operation 956 if (resetType != "On") 957 { 958 messages::propertyValueNotInList(asyncResp->res, *resetType, 959 "ResetType"); 960 return; 961 } 962 963 std::string command = "xyz.openbmc_project.State.Host.Transition.On"; 964 965 sdbusplus::asio::setProperty( 966 *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor", 967 "/xyz/openbmc_project/state/hypervisor0", 968 "xyz.openbmc_project.State.Host", "RequestedHostTransition", command, 969 [asyncResp, resetType](const boost::system::error_code& ec) { 970 if (ec) 971 { 972 BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec); 973 if (ec.value() == boost::asio::error::invalid_argument) 974 { 975 messages::actionParameterNotSupported(asyncResp->res, 976 *resetType, "Reset"); 977 return; 978 } 979 980 if (ec.value() == boost::asio::error::host_unreachable) 981 { 982 messages::resourceNotFound(asyncResp->res, "Actions", "Reset"); 983 return; 984 } 985 986 messages::internalError(asyncResp->res); 987 return; 988 } 989 messages::success(asyncResp->res); 990 }); 991 } 992 993 inline void requestRoutesHypervisorSystems(App& app) 994 { 995 /** 996 * HypervisorInterfaceCollection class to handle the GET and PATCH on 997 * Hypervisor Interface 998 */ 999 1000 BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/") 1001 .privileges(redfish::privileges::getEthernetInterfaceCollection) 1002 .methods(boost::beast::http::verb::get)(std::bind_front( 1003 handleHypervisorEthernetInterfaceCollectionGet, std::ref(app))); 1004 1005 BMCWEB_ROUTE(app, 1006 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 1007 .privileges(redfish::privileges::getEthernetInterface) 1008 .methods(boost::beast::http::verb::get)(std::bind_front( 1009 handleHypervisorEthernetInterfaceGet, std::ref(app))); 1010 1011 BMCWEB_ROUTE(app, 1012 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/") 1013 .privileges(redfish::privileges::patchEthernetInterface) 1014 .methods(boost::beast::http::verb::patch)(std::bind_front( 1015 handleHypervisorEthernetInterfacePatch, std::ref(app))); 1016 1017 BMCWEB_ROUTE(app, 1018 "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset/") 1019 .privileges(redfish::privileges::postComputerSystem) 1020 .methods(boost::beast::http::verb::post)( 1021 std::bind_front(handleHypervisorSystemResetPost, std::ref(app))); 1022 } 1023 } // namespace redfish 1024