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