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