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