1 /* 2 // Copyright (c) 2018 Intel Corporation 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 */ 16 #pragma once 17 18 #include <boost/container/flat_map.hpp> 19 #include <boost/container/flat_set.hpp> 20 #include <boost/optional.hpp> 21 #include <dbus_singleton.hpp> 22 #include <error_messages.hpp> 23 #include <node.hpp> 24 #include <utils/json_utils.hpp> 25 26 namespace redfish 27 { 28 29 /** 30 * DBus types primitives for several generic DBus interfaces 31 * TODO(Pawel) consider move this to separate file into boost::dbus 32 */ 33 using PropertiesMapType = boost::container::flat_map< 34 std::string, 35 sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t, 36 int32_t, uint32_t, int64_t, uint64_t, double>>; 37 38 using GetManagedObjects = std::vector<std::pair< 39 sdbusplus::message::object_path, 40 std::vector<std::pair< 41 std::string, 42 boost::container::flat_map< 43 std::string, sdbusplus::message::variant< 44 std::string, bool, uint8_t, int16_t, uint16_t, 45 int32_t, uint32_t, int64_t, uint64_t, double>>>>>>; 46 47 enum class LinkType 48 { 49 Local, 50 Global 51 }; 52 53 /** 54 * Structure for keeping IPv4 data required by Redfish 55 */ 56 struct IPv4AddressData 57 { 58 std::string id; 59 std::string address; 60 std::string domain; 61 std::string gateway; 62 std::string netmask; 63 std::string origin; 64 LinkType linktype; 65 66 bool operator<(const IPv4AddressData &obj) const 67 { 68 return id < obj.id; 69 } 70 }; 71 72 /** 73 * Structure for keeping basic single Ethernet Interface information 74 * available from DBus 75 */ 76 struct EthernetInterfaceData 77 { 78 uint32_t speed; 79 bool auto_neg; 80 std::string hostname; 81 std::string default_gateway; 82 std::string mac_address; 83 boost::optional<uint32_t> vlan_id; 84 }; 85 86 // Helper function that changes bits netmask notation (i.e. /24) 87 // into full dot notation 88 inline std::string getNetmask(unsigned int bits) 89 { 90 uint32_t value = 0xffffffff << (32 - bits); 91 std::string netmask = std::to_string((value >> 24) & 0xff) + "." + 92 std::to_string((value >> 16) & 0xff) + "." + 93 std::to_string((value >> 8) & 0xff) + "." + 94 std::to_string(value & 0xff); 95 return netmask; 96 } 97 98 inline std::string 99 translateAddressOriginDbusToRedfish(const std::string &inputOrigin, 100 bool isIPv4) 101 { 102 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static") 103 { 104 return "Static"; 105 } 106 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal") 107 { 108 if (isIPv4) 109 { 110 return "IPv4LinkLocal"; 111 } 112 else 113 { 114 return "LinkLocal"; 115 } 116 } 117 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP") 118 { 119 if (isIPv4) 120 { 121 return "DHCP"; 122 } 123 else 124 { 125 return "DHCPv6"; 126 } 127 } 128 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC") 129 { 130 return "SLAAC"; 131 } 132 return ""; 133 } 134 135 inline std::string 136 translateAddressOriginRedfishToDbus(const std::string &inputOrigin) 137 { 138 if (inputOrigin == "Static") 139 { 140 return "xyz.openbmc_project.Network.IP.AddressOrigin.Static"; 141 } 142 if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6") 143 { 144 return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP"; 145 } 146 if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal") 147 { 148 return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal"; 149 } 150 if (inputOrigin == "SLAAC") 151 { 152 return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC"; 153 } 154 return ""; 155 } 156 157 inline void extractEthernetInterfaceData(const std::string ðiface_id, 158 const GetManagedObjects &dbus_data, 159 EthernetInterfaceData ðData) 160 { 161 for (const auto &objpath : dbus_data) 162 { 163 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id) 164 { 165 for (const auto &ifacePair : objpath.second) 166 { 167 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress") 168 { 169 for (const auto &propertyPair : ifacePair.second) 170 { 171 if (propertyPair.first == "MACAddress") 172 { 173 const std::string *mac = 174 mapbox::getPtr<const std::string>( 175 propertyPair.second); 176 if (mac != nullptr) 177 { 178 ethData.mac_address = *mac; 179 } 180 } 181 } 182 } 183 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN") 184 { 185 for (const auto &propertyPair : ifacePair.second) 186 { 187 if (propertyPair.first == "Id") 188 { 189 const uint32_t *id = mapbox::getPtr<const uint32_t>( 190 propertyPair.second); 191 if (id != nullptr) 192 { 193 ethData.vlan_id = *id; 194 } 195 } 196 } 197 } 198 else if (ifacePair.first == 199 "xyz.openbmc_project.Network.EthernetInterface") 200 { 201 for (const auto &propertyPair : ifacePair.second) 202 { 203 if (propertyPair.first == "AutoNeg") 204 { 205 const bool *auto_neg = 206 mapbox::getPtr<const bool>(propertyPair.second); 207 if (auto_neg != nullptr) 208 { 209 ethData.auto_neg = *auto_neg; 210 } 211 } 212 else if (propertyPair.first == "Speed") 213 { 214 const uint32_t *speed = 215 mapbox::getPtr<const uint32_t>( 216 propertyPair.second); 217 if (speed != nullptr) 218 { 219 ethData.speed = *speed; 220 } 221 } 222 } 223 } 224 else if (ifacePair.first == 225 "xyz.openbmc_project.Network.SystemConfiguration") 226 { 227 for (const auto &propertyPair : ifacePair.second) 228 { 229 if (propertyPair.first == "HostName") 230 { 231 const std::string *hostname = 232 mapbox::getPtr<const std::string>( 233 propertyPair.second); 234 if (hostname != nullptr) 235 { 236 ethData.hostname = *hostname; 237 } 238 } 239 else if (propertyPair.first == "DefaultGateway") 240 { 241 const std::string *defaultGateway = 242 mapbox::getPtr<const std::string>( 243 propertyPair.second); 244 if (defaultGateway != nullptr) 245 { 246 ethData.default_gateway = *defaultGateway; 247 } 248 } 249 } 250 } 251 } 252 } 253 } 254 } 255 256 // Helper function that extracts data for single ethernet ipv4 address 257 inline void 258 extractIPData(const std::string ðiface_id, 259 const GetManagedObjects &dbus_data, 260 boost::container::flat_set<IPv4AddressData> &ipv4_config) 261 { 262 const std::string ipv4PathStart = 263 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/"; 264 265 // Since there might be several IPv4 configurations aligned with 266 // single ethernet interface, loop over all of them 267 for (const auto &objpath : dbus_data) 268 { 269 // Check if proper pattern for object path appears 270 if (boost::starts_with(objpath.first.str, ipv4PathStart)) 271 { 272 for (auto &interface : objpath.second) 273 { 274 if (interface.first == "xyz.openbmc_project.Network.IP") 275 { 276 // Instance IPv4AddressData structure, and set as 277 // appropriate 278 std::pair< 279 boost::container::flat_set<IPv4AddressData>::iterator, 280 bool> 281 it = ipv4_config.insert( 282 {objpath.first.str.substr(ipv4PathStart.size())}); 283 IPv4AddressData &ipv4_address = *it.first; 284 for (auto &property : interface.second) 285 { 286 if (property.first == "Address") 287 { 288 const std::string *address = 289 mapbox::getPtr<const std::string>( 290 property.second); 291 if (address != nullptr) 292 { 293 ipv4_address.address = *address; 294 } 295 } 296 else if (property.first == "Gateway") 297 { 298 const std::string *gateway = 299 mapbox::getPtr<const std::string>( 300 property.second); 301 if (gateway != nullptr) 302 { 303 ipv4_address.gateway = *gateway; 304 } 305 } 306 else if (property.first == "Origin") 307 { 308 const std::string *origin = 309 mapbox::getPtr<const std::string>( 310 property.second); 311 if (origin != nullptr) 312 { 313 ipv4_address.origin = 314 translateAddressOriginDbusToRedfish(*origin, 315 true); 316 } 317 } 318 else if (property.first == "PrefixLength") 319 { 320 const uint8_t *mask = 321 mapbox::getPtr<uint8_t>(property.second); 322 if (mask != nullptr) 323 { 324 // convert it to the string 325 ipv4_address.netmask = getNetmask(*mask); 326 } 327 } 328 else 329 { 330 BMCWEB_LOG_ERROR 331 << "Got extra property: " << property.first 332 << " on the " << objpath.first.str << " object"; 333 } 334 } 335 // Check if given address is local, or global 336 ipv4_address.linktype = 337 boost::starts_with(ipv4_address.address, "169.254.") 338 ? LinkType::Global 339 : LinkType::Local; 340 } 341 } 342 } 343 } 344 } 345 346 /** 347 * @brief Sets given Id on the given VLAN interface through D-Bus 348 * 349 * @param[in] ifaceId Id of VLAN interface that should be modified 350 * @param[in] inputVlanId New ID of the VLAN 351 * @param[in] callback Function that will be called after the operation 352 * 353 * @return None. 354 */ 355 template <typename CallbackFunc> 356 void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId, 357 CallbackFunc &&callback) 358 { 359 crow::connections::systemBus->async_method_call( 360 callback, "xyz.openbmc_project.Network", 361 std::string("/xyz/openbmc_project/network/") + ifaceId, 362 "org.freedesktop.DBus.Properties", "Set", 363 "xyz.openbmc_project.Network.VLAN", "Id", 364 sdbusplus::message::variant<uint32_t>(inputVlanId)); 365 } 366 367 /** 368 * @brief Helper function that verifies IP address to check if it is in 369 * proper format. If bits pointer is provided, also calculates active 370 * bit count for Subnet Mask. 371 * 372 * @param[in] ip IP that will be verified 373 * @param[out] bits Calculated mask in bits notation 374 * 375 * @return true in case of success, false otherwise 376 */ 377 inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip, 378 uint8_t *bits = nullptr) 379 { 380 std::vector<std::string> bytesInMask; 381 382 boost::split(bytesInMask, ip, boost::is_any_of(".")); 383 384 static const constexpr int ipV4AddressSectionsCount = 4; 385 if (bytesInMask.size() != ipV4AddressSectionsCount) 386 { 387 return false; 388 } 389 390 if (bits != nullptr) 391 { 392 *bits = 0; 393 } 394 395 char *endPtr; 396 long previousValue = 255; 397 bool firstZeroInByteHit; 398 for (const std::string &byte : bytesInMask) 399 { 400 if (byte.empty()) 401 { 402 return false; 403 } 404 405 // Use strtol instead of stroi to avoid exceptions 406 long value = std::strtol(byte.c_str(), &endPtr, 10); 407 408 // endPtr should point to the end of the string, otherwise given string 409 // is not 100% number 410 if (*endPtr != '\0') 411 { 412 return false; 413 } 414 415 // Value should be contained in byte 416 if (value < 0 || value > 255) 417 { 418 return false; 419 } 420 421 if (bits != nullptr) 422 { 423 // Mask has to be continuous between bytes 424 if (previousValue != 255 && value != 0) 425 { 426 return false; 427 } 428 429 // Mask has to be continuous inside bytes 430 firstZeroInByteHit = false; 431 432 // Count bits 433 for (int bitIdx = 7; bitIdx >= 0; bitIdx--) 434 { 435 if (value & (1 << bitIdx)) 436 { 437 if (firstZeroInByteHit) 438 { 439 // Continuity not preserved 440 return false; 441 } 442 else 443 { 444 (*bits)++; 445 } 446 } 447 else 448 { 449 firstZeroInByteHit = true; 450 } 451 } 452 } 453 454 previousValue = value; 455 } 456 457 return true; 458 } 459 460 /** 461 * @brief Changes IPv4 address type property (Address, Gateway) 462 * 463 * @param[in] ifaceId Id of interface whose IP should be modified 464 * @param[in] ipIdx Index of IP in input array that should be modified 465 * @param[in] ipHash DBus Hash id of modified IP 466 * @param[in] name Name of field in JSON representation 467 * @param[in] newValue New value that should be written 468 * @param[io] asyncResp Response object that will be returned to client 469 * 470 * @return true if give IP is valid and has been sent do D-Bus, false 471 * otherwise 472 */ 473 inline void changeIPv4AddressProperty( 474 const std::string &ifaceId, int ipIdx, const std::string &ipHash, 475 const std::string &name, const std::string &newValue, 476 const std::shared_ptr<AsyncResp> asyncResp) 477 { 478 auto callback = [asyncResp, ipIdx, name{std::string(name)}, 479 newValue{std::move(newValue)}]( 480 const boost::system::error_code ec) { 481 if (ec) 482 { 483 messages::internalError(asyncResp->res); 484 } 485 else 486 { 487 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue; 488 } 489 }; 490 491 crow::connections::systemBus->async_method_call( 492 std::move(callback), "xyz.openbmc_project.Network", 493 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 494 "org.freedesktop.DBus.Properties", "Set", 495 "xyz.openbmc_project.Network.IP", name, 496 sdbusplus::message::variant<std::string>(newValue)); 497 } 498 499 /** 500 * @brief Changes IPv4 address origin property 501 * 502 * @param[in] ifaceId Id of interface whose IP should be modified 503 * @param[in] ipIdx Index of IP in input array that should be 504 * modified 505 * @param[in] ipHash DBus Hash id of modified IP 506 * @param[in] newValue New value in Redfish format 507 * @param[in] newValueDbus New value in D-Bus format 508 * @param[io] asyncResp Response object that will be returned to client 509 * 510 * @return true if give IP is valid and has been sent do D-Bus, false 511 * otherwise 512 */ 513 inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx, 514 const std::string &ipHash, 515 const std::string &newValue, 516 const std::string &newValueDbus, 517 const std::shared_ptr<AsyncResp> asyncResp) 518 { 519 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}]( 520 const boost::system::error_code ec) { 521 if (ec) 522 { 523 messages::internalError(asyncResp->res); 524 } 525 else 526 { 527 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] = 528 newValue; 529 } 530 }; 531 532 crow::connections::systemBus->async_method_call( 533 std::move(callback), "xyz.openbmc_project.Network", 534 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 535 "org.freedesktop.DBus.Properties", "Set", 536 "xyz.openbmc_project.Network.IP", "Origin", 537 sdbusplus::message::variant<std::string>(newValueDbus)); 538 } 539 540 /** 541 * @brief Modifies SubnetMask for given IP 542 * 543 * @param[in] ifaceId Id of interface whose IP should be modified 544 * @param[in] ipIdx Index of IP in input array that should be 545 * modified 546 * @param[in] ipHash DBus Hash id of modified IP 547 * @param[in] newValueStr Mask in dot notation as string 548 * @param[in] newValue Mask as PrefixLength in bitcount 549 * @param[io] asyncResp Response object that will be returned to client 550 * 551 * @return None 552 */ 553 inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx, 554 const std::string &ipHash, 555 const std::string &newValueStr, 556 uint8_t &newValue, 557 std::shared_ptr<AsyncResp> asyncResp) 558 { 559 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}]( 560 const boost::system::error_code ec) { 561 if (ec) 562 { 563 messages::internalError(asyncResp->res); 564 } 565 else 566 { 567 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] = 568 newValueStr; 569 } 570 }; 571 572 crow::connections::systemBus->async_method_call( 573 std::move(callback), "xyz.openbmc_project.Network", 574 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 575 "org.freedesktop.DBus.Properties", "Set", 576 "xyz.openbmc_project.Network.IP", "PrefixLength", 577 sdbusplus::message::variant<uint8_t>(newValue)); 578 } 579 580 /** 581 * @brief Sets given HostName of the machine through D-Bus 582 * 583 * @param[in] newHostname New name that HostName will be changed to 584 * @param[in] callback Function that will be called after the operation 585 * 586 * @return None. 587 */ 588 template <typename CallbackFunc> 589 void setHostName(const std::string &newHostname, CallbackFunc &&callback) 590 { 591 crow::connections::systemBus->async_method_call( 592 callback, "xyz.openbmc_project.Network", 593 "/xyz/openbmc_project/network/config", 594 "org.freedesktop.DBus.Properties", "Set", 595 "xyz.openbmc_project.Network.SystemConfiguration", "HostName", 596 sdbusplus::message::variant<std::string>(newHostname)); 597 } 598 599 /** 600 * @brief Deletes given IPv4 601 * 602 * @param[in] ifaceId Id of interface whose IP should be deleted 603 * @param[in] ipIdx Index of IP in input array that should be deleted 604 * @param[in] ipHash DBus Hash id of IP that should be deleted 605 * @param[io] asyncResp Response object that will be returned to client 606 * 607 * @return None 608 */ 609 inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash, 610 unsigned int ipIdx, 611 const std::shared_ptr<AsyncResp> asyncResp) 612 { 613 crow::connections::systemBus->async_method_call( 614 [ipIdx, asyncResp](const boost::system::error_code ec) { 615 if (ec) 616 { 617 messages::internalError(asyncResp->res); 618 } 619 else 620 { 621 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr; 622 } 623 }, 624 "xyz.openbmc_project.Network", 625 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash, 626 "xyz.openbmc_project.Object.Delete", "Delete"); 627 } 628 629 /** 630 * @brief Creates IPv4 with given data 631 * 632 * @param[in] ifaceId Id of interface whose IP should be deleted 633 * @param[in] ipIdx Index of IP in input array that should be deleted 634 * @param[in] ipHash DBus Hash id of IP that should be deleted 635 * @param[io] asyncResp Response object that will be returned to client 636 * 637 * @return None 638 */ 639 inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx, 640 uint8_t subnetMask, const std::string &gateway, 641 const std::string &address, 642 std::shared_ptr<AsyncResp> asyncResp) 643 { 644 auto createIpHandler = [ipIdx, 645 asyncResp](const boost::system::error_code ec) { 646 if (ec) 647 { 648 messages::internalError(asyncResp->res); 649 } 650 }; 651 652 crow::connections::systemBus->async_method_call( 653 std::move(createIpHandler), "xyz.openbmc_project.Network", 654 "/xyz/openbmc_project/network/" + ifaceId, 655 "xyz.openbmc_project.Network.IP.Create", "IP", 656 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask, 657 gateway); 658 } 659 660 /** 661 * Function that retrieves all properties for given Ethernet Interface 662 * Object 663 * from EntityManager Network Manager 664 * @param ethiface_id a eth interface id to query on DBus 665 * @param callback a function that shall be called to convert Dbus output 666 * into JSON 667 */ 668 template <typename CallbackFunc> 669 void getEthernetIfaceData(const std::string ðiface_id, 670 CallbackFunc &&callback) 671 { 672 crow::connections::systemBus->async_method_call( 673 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}]( 674 const boost::system::error_code error_code, 675 const GetManagedObjects &resp) { 676 EthernetInterfaceData ethData{}; 677 boost::container::flat_set<IPv4AddressData> ipv4Data; 678 679 if (error_code) 680 { 681 callback(false, ethData, ipv4Data); 682 return; 683 } 684 685 extractEthernetInterfaceData(ethiface_id, resp, ethData); 686 extractIPData(ethiface_id, resp, ipv4Data); 687 688 // Fix global GW 689 for (IPv4AddressData &ipv4 : ipv4Data) 690 { 691 if ((ipv4.linktype == LinkType::Global) && 692 (ipv4.gateway == "0.0.0.0")) 693 { 694 ipv4.gateway = ethData.default_gateway; 695 } 696 } 697 698 // Finally make a callback with usefull data 699 callback(true, ethData, ipv4Data); 700 }, 701 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 702 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 703 }; 704 705 /** 706 * Function that retrieves all Ethernet Interfaces available through Network 707 * Manager 708 * @param callback a function that shall be called to convert Dbus output 709 * into JSON. 710 */ 711 template <typename CallbackFunc> 712 void getEthernetIfaceList(CallbackFunc &&callback) 713 { 714 crow::connections::systemBus->async_method_call( 715 [callback{std::move(callback)}]( 716 const boost::system::error_code error_code, 717 GetManagedObjects &resp) { 718 // Callback requires vector<string> to retrieve all available 719 // ethernet interfaces 720 std::vector<std::string> iface_list; 721 iface_list.reserve(resp.size()); 722 if (error_code) 723 { 724 callback(false, iface_list); 725 return; 726 } 727 728 // Iterate over all retrieved ObjectPaths. 729 for (const auto &objpath : resp) 730 { 731 // And all interfaces available for certain ObjectPath. 732 for (const auto &interface : objpath.second) 733 { 734 // If interface is 735 // xyz.openbmc_project.Network.EthernetInterface, this is 736 // what we're looking for. 737 if (interface.first == 738 "xyz.openbmc_project.Network.EthernetInterface") 739 { 740 // Cut out everyting until last "/", ... 741 const std::string &iface_id = objpath.first.str; 742 std::size_t last_pos = iface_id.rfind("/"); 743 if (last_pos != std::string::npos) 744 { 745 // and put it into output vector. 746 iface_list.emplace_back( 747 iface_id.substr(last_pos + 1)); 748 } 749 } 750 } 751 } 752 // Finally make a callback with useful data 753 callback(true, iface_list); 754 }, 755 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network", 756 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); 757 }; 758 759 /** 760 * EthernetCollection derived class for delivering Ethernet Collection Schema 761 */ 762 class EthernetCollection : public Node 763 { 764 public: 765 template <typename CrowApp> 766 EthernetCollection(CrowApp &app) : 767 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/") 768 { 769 entityPrivileges = { 770 {boost::beast::http::verb::get, {{"Login"}}}, 771 {boost::beast::http::verb::head, {{"Login"}}}, 772 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 773 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 774 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 775 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 776 } 777 778 private: 779 /** 780 * Functions triggers appropriate requests on DBus 781 */ 782 void doGet(crow::Response &res, const crow::Request &req, 783 const std::vector<std::string> ¶ms) override 784 { 785 res.jsonValue["@odata.type"] = 786 "#EthernetInterfaceCollection.EthernetInterfaceCollection"; 787 res.jsonValue["@odata.context"] = 788 "/redfish/v1/" 789 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection"; 790 res.jsonValue["@odata.id"] = 791 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 792 res.jsonValue["Name"] = "Ethernet Network Interface Collection"; 793 res.jsonValue["Description"] = 794 "Collection of EthernetInterfaces for this Manager"; 795 796 // Get eth interface list, and call the below callback for JSON 797 // preparation 798 getEthernetIfaceList( 799 [&res](const bool &success, 800 const std::vector<std::string> &iface_list) { 801 if (!success) 802 { 803 messages::internalError(res); 804 res.end(); 805 return; 806 } 807 808 nlohmann::json &iface_array = res.jsonValue["Members"]; 809 iface_array = nlohmann::json::array(); 810 for (const std::string &iface_item : iface_list) 811 { 812 iface_array.push_back( 813 {{"@odata.id", 814 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 815 iface_item}}); 816 } 817 818 res.jsonValue["Members@odata.count"] = iface_array.size(); 819 res.jsonValue["@odata.id"] = 820 "/redfish/v1/Managers/bmc/EthernetInterfaces"; 821 res.end(); 822 }); 823 } 824 }; 825 826 /** 827 * EthernetInterface derived class for delivering Ethernet Schema 828 */ 829 class EthernetInterface : public Node 830 { 831 public: 832 /* 833 * Default Constructor 834 */ 835 template <typename CrowApp> 836 EthernetInterface(CrowApp &app) : 837 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/", 838 std::string()) 839 { 840 entityPrivileges = { 841 {boost::beast::http::verb::get, {{"Login"}}}, 842 {boost::beast::http::verb::head, {{"Login"}}}, 843 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 844 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 845 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 846 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 847 } 848 849 // TODO(kkowalsk) Find a suitable class/namespace for this 850 static void handleVlanPatch(const std::string &ifaceId, 851 const nlohmann::json &input, 852 const EthernetInterfaceData ðData, 853 const std::shared_ptr<AsyncResp> asyncResp) 854 { 855 if (!input.is_object()) 856 { 857 messages::propertyValueTypeError(asyncResp->res, input.dump(), 858 "VLAN"); 859 return; 860 } 861 862 nlohmann::json::const_iterator vlanEnable = input.find("VLANEnable"); 863 if (vlanEnable == input.end()) 864 { 865 messages::propertyMissing(asyncResp->res, "VLANEnable"); 866 return; 867 } 868 const bool *vlanEnableBool = vlanEnable->get_ptr<const bool *>(); 869 if (vlanEnableBool == nullptr) 870 { 871 messages::propertyValueTypeError(asyncResp->res, vlanEnable->dump(), 872 "VLANEnable"); 873 return; 874 } 875 876 nlohmann::json::const_iterator vlanId = input.find("VLANId"); 877 if (vlanId == input.end()) 878 { 879 messages::propertyMissing(asyncResp->res, "VLANId"); 880 return; 881 } 882 const uint64_t *vlanIdUint = vlanId->get_ptr<const uint64_t *>(); 883 if (vlanIdUint == nullptr) 884 { 885 messages::propertyValueTypeError(asyncResp->res, vlanId->dump(), 886 "VLANId"); 887 return; 888 } 889 890 if (!ethData.vlan_id) 891 { 892 // This interface is not a VLAN. Cannot do anything with it 893 // TODO(kkowalsk) Change this message 894 messages::propertyNotWritable(asyncResp->res, "VLANEnable"); 895 896 return; 897 } 898 899 // VLAN is configured on the interface 900 if (*vlanEnableBool == true) 901 { 902 // Change VLAN Id 903 asyncResp->res.jsonValue["VLANId"] = *vlanIdUint; 904 auto callback = [asyncResp](const boost::system::error_code ec) { 905 if (ec) 906 { 907 messages::internalError(asyncResp->res); 908 } 909 else 910 { 911 asyncResp->res.jsonValue["VLANEnable"] = true; 912 } 913 }; 914 crow::connections::systemBus->async_method_call( 915 std::move(callback), "xyz.openbmc_project.Network", 916 "/xyz/openbmc_project/network/" + ifaceId, 917 "org.freedesktop.DBus.Properties", "Set", 918 "xyz.openbmc_project.Network.VLAN", "Id", 919 sdbusplus::message::variant<uint32_t>(*vlanIdUint)); 920 } 921 else 922 { 923 auto callback = [asyncResp](const boost::system::error_code ec) { 924 if (ec) 925 { 926 messages::internalError(asyncResp->res); 927 return; 928 } 929 asyncResp->res.jsonValue["VLANEnable"] = false; 930 }; 931 932 crow::connections::systemBus->async_method_call( 933 std::move(callback), "xyz.openbmc_project.Network", 934 "/xyz/openbmc_project/network/" + ifaceId, 935 "xyz.openbmc_project.Object.Delete", "Delete"); 936 } 937 } 938 939 private: 940 void handleHostnamePatch(const nlohmann::json &input, 941 const std::shared_ptr<AsyncResp> asyncResp) 942 { 943 const std::string *newHostname = input.get_ptr<const std::string *>(); 944 if (newHostname == nullptr) 945 { 946 messages::propertyValueTypeError(asyncResp->res, input.dump(), 947 "HostName"); 948 return; 949 } 950 951 // Change hostname 952 setHostName(*newHostname, 953 [asyncResp, newHostname{std::string(*newHostname)}]( 954 const boost::system::error_code ec) { 955 if (ec) 956 { 957 messages::internalError(asyncResp->res); 958 } 959 else 960 { 961 asyncResp->res.jsonValue["HostName"] = newHostname; 962 } 963 }); 964 } 965 966 void handleIPv4Patch( 967 const std::string &ifaceId, const nlohmann::json &input, 968 const boost::container::flat_set<IPv4AddressData> &ipv4Data, 969 const std::shared_ptr<AsyncResp> asyncResp) 970 { 971 if (!input.is_array()) 972 { 973 messages::propertyValueTypeError(asyncResp->res, input.dump(), 974 "IPv4Addresses"); 975 return; 976 } 977 978 // According to Redfish PATCH definition, size must be at least equal 979 if (input.size() < ipv4Data.size()) 980 { 981 messages::propertyValueFormatError(asyncResp->res, input.dump(), 982 "IPv4Addresses"); 983 return; 984 } 985 986 int entryIdx = 0; 987 boost::container::flat_set<IPv4AddressData>::const_iterator thisData = 988 ipv4Data.begin(); 989 for (const nlohmann::json &thisJson : input) 990 { 991 std::string pathString = 992 "IPv4Addresses/" + std::to_string(entryIdx); 993 // Check that entry is not of some unexpected type 994 if (!thisJson.is_object() && !thisJson.is_null()) 995 { 996 messages::propertyValueTypeError(asyncResp->res, 997 thisJson.dump(), 998 pathString + "/IPv4Address"); 999 1000 continue; 1001 } 1002 1003 nlohmann::json::const_iterator addressFieldIt = 1004 thisJson.find("Address"); 1005 const std::string *addressField = nullptr; 1006 if (addressFieldIt != thisJson.end()) 1007 { 1008 addressField = addressFieldIt->get_ptr<const std::string *>(); 1009 if (addressField == nullptr) 1010 { 1011 messages::propertyValueFormatError(asyncResp->res, 1012 addressFieldIt->dump(), 1013 pathString + "/Address"); 1014 continue; 1015 } 1016 else 1017 { 1018 if (!ipv4VerifyIpAndGetBitcount(*addressField)) 1019 { 1020 messages::propertyValueFormatError( 1021 asyncResp->res, *addressField, 1022 pathString + "/Address"); 1023 continue; 1024 } 1025 } 1026 } 1027 1028 boost::optional<uint8_t> prefixLength; 1029 const std::string *subnetField = nullptr; 1030 nlohmann::json::const_iterator subnetFieldIt = 1031 thisJson.find("SubnetMask"); 1032 if (subnetFieldIt != thisJson.end()) 1033 { 1034 subnetField = subnetFieldIt->get_ptr<const std::string *>(); 1035 if (subnetField == nullptr) 1036 { 1037 messages::propertyValueFormatError( 1038 asyncResp->res, *subnetField, 1039 pathString + "/SubnetMask"); 1040 continue; 1041 } 1042 else 1043 { 1044 prefixLength = 0; 1045 if (!ipv4VerifyIpAndGetBitcount(*subnetField, 1046 &*prefixLength)) 1047 { 1048 messages::propertyValueFormatError( 1049 asyncResp->res, *subnetField, 1050 pathString + "/SubnetMask"); 1051 continue; 1052 } 1053 } 1054 } 1055 1056 std::string addressOriginInDBusFormat; 1057 const std::string *addressOriginField = nullptr; 1058 nlohmann::json::const_iterator addressOriginFieldIt = 1059 thisJson.find("AddressOrigin"); 1060 if (addressOriginFieldIt != thisJson.end()) 1061 { 1062 const std::string *addressOriginField = 1063 addressOriginFieldIt->get_ptr<const std::string *>(); 1064 if (addressOriginField == nullptr) 1065 { 1066 messages::propertyValueFormatError( 1067 asyncResp->res, *addressOriginField, 1068 pathString + "/AddressOrigin"); 1069 continue; 1070 } 1071 else 1072 { 1073 // Get Address origin in proper format 1074 addressOriginInDBusFormat = 1075 translateAddressOriginRedfishToDbus( 1076 *addressOriginField); 1077 if (addressOriginInDBusFormat.empty()) 1078 { 1079 messages::propertyValueNotInList( 1080 asyncResp->res, *addressOriginField, 1081 pathString + "/AddressOrigin"); 1082 continue; 1083 } 1084 } 1085 } 1086 1087 nlohmann::json::const_iterator gatewayFieldIt = 1088 thisJson.find("Gateway"); 1089 const std::string *gatewayField = nullptr; 1090 if (gatewayFieldIt != thisJson.end()) 1091 { 1092 const std::string *gatewayField = 1093 gatewayFieldIt->get_ptr<const std::string *>(); 1094 if (gatewayField == nullptr || 1095 !ipv4VerifyIpAndGetBitcount(*gatewayField)) 1096 { 1097 messages::propertyValueFormatError( 1098 asyncResp->res, *gatewayField, pathString + "/Gateway"); 1099 continue; 1100 } 1101 } 1102 1103 // if a vlan already exists, modify the existing 1104 if (thisData != ipv4Data.end()) 1105 { 1106 // Existing object that should be modified/deleted/remain 1107 // unchanged 1108 if (thisJson.is_null()) 1109 { 1110 auto callback = [entryIdx{std::to_string(entryIdx)}, 1111 asyncResp]( 1112 const boost::system::error_code ec) { 1113 if (ec) 1114 { 1115 messages::internalError(asyncResp->res); 1116 return; 1117 } 1118 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = 1119 nullptr; 1120 }; 1121 crow::connections::systemBus->async_method_call( 1122 std::move(callback), "xyz.openbmc_project.Network", 1123 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + 1124 thisData->id, 1125 "xyz.openbmc_project.Object.Delete", "Delete"); 1126 } 1127 else if (thisJson.is_object()) 1128 { 1129 // Apply changes 1130 if (addressField != nullptr) 1131 { 1132 auto callback = 1133 [asyncResp, entryIdx, 1134 addressField{std::string(*addressField)}]( 1135 const boost::system::error_code ec) { 1136 if (ec) 1137 { 1138 messages::internalError(asyncResp->res); 1139 return; 1140 } 1141 asyncResp->res 1142 .jsonValue["IPv4Addresses"][std::to_string( 1143 entryIdx)]["Address"] = addressField; 1144 }; 1145 1146 crow::connections::systemBus->async_method_call( 1147 std::move(callback), "xyz.openbmc_project.Network", 1148 "/xyz/openbmc_project/network/" + ifaceId + 1149 "/ipv4/" + thisData->id, 1150 "org.freedesktop.DBus.Properties", "Set", 1151 "xyz.openbmc_project.Network.IP", "Address", 1152 sdbusplus::message::variant<std::string>( 1153 *addressField)); 1154 } 1155 1156 if (prefixLength && subnetField != nullptr) 1157 { 1158 changeIPv4SubnetMaskProperty(ifaceId, entryIdx, 1159 thisData->id, *subnetField, 1160 *prefixLength, asyncResp); 1161 } 1162 1163 if (!addressOriginInDBusFormat.empty() && 1164 addressOriginField != nullptr) 1165 { 1166 changeIPv4Origin(ifaceId, entryIdx, thisData->id, 1167 *addressOriginField, 1168 addressOriginInDBusFormat, asyncResp); 1169 } 1170 1171 if (gatewayField != nullptr) 1172 { 1173 auto callback = 1174 [asyncResp, entryIdx, 1175 gatewayField{std::string(*gatewayField)}]( 1176 const boost::system::error_code ec) { 1177 if (ec) 1178 { 1179 messages::internalError(asyncResp->res); 1180 return; 1181 } 1182 asyncResp->res 1183 .jsonValue["IPv4Addresses"][std::to_string( 1184 entryIdx)]["Gateway"] = 1185 std::move(gatewayField); 1186 }; 1187 1188 crow::connections::systemBus->async_method_call( 1189 std::move(callback), "xyz.openbmc_project.Network", 1190 "/xyz/openbmc_project/network/" + ifaceId + 1191 "/ipv4/" + thisData->id, 1192 "org.freedesktop.DBus.Properties", "Set", 1193 "xyz.openbmc_project.Network.IP", "Gateway", 1194 sdbusplus::message::variant<std::string>( 1195 *gatewayField)); 1196 } 1197 } 1198 thisData++; 1199 } 1200 else 1201 { 1202 // Create IPv4 with provided data 1203 if (gatewayField == nullptr) 1204 { 1205 messages::propertyMissing(asyncResp->res, 1206 pathString + "/Gateway"); 1207 continue; 1208 } 1209 1210 if (addressField == nullptr) 1211 { 1212 messages::propertyMissing(asyncResp->res, 1213 pathString + "/Address"); 1214 continue; 1215 } 1216 1217 if (!prefixLength) 1218 { 1219 messages::propertyMissing(asyncResp->res, 1220 pathString + "/SubnetMask"); 1221 continue; 1222 } 1223 1224 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField, 1225 *addressField, asyncResp); 1226 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson; 1227 } 1228 entryIdx++; 1229 } 1230 } 1231 1232 void parseInterfaceData( 1233 nlohmann::json &json_response, const std::string &iface_id, 1234 const EthernetInterfaceData ðData, 1235 const boost::container::flat_set<IPv4AddressData> &ipv4Data) 1236 { 1237 json_response["Id"] = iface_id; 1238 json_response["@odata.id"] = 1239 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id; 1240 1241 json_response["SpeedMbps"] = ethData.speed; 1242 json_response["MACAddress"] = ethData.mac_address; 1243 if (!ethData.hostname.empty()) 1244 { 1245 json_response["HostName"] = ethData.hostname; 1246 } 1247 1248 nlohmann::json &vlanObj = json_response["VLAN"]; 1249 if (ethData.vlan_id) 1250 { 1251 vlanObj["VLANEnable"] = true; 1252 vlanObj["VLANId"] = *ethData.vlan_id; 1253 } 1254 else 1255 { 1256 vlanObj["VLANEnable"] = false; 1257 vlanObj["VLANId"] = 0; 1258 } 1259 1260 if (ipv4Data.size() > 0) 1261 { 1262 nlohmann::json &ipv4_array = json_response["IPv4Addresses"]; 1263 ipv4_array = nlohmann::json::array(); 1264 for (auto &ipv4_config : ipv4Data) 1265 { 1266 if (!ipv4_config.address.empty()) 1267 { 1268 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin}, 1269 {"SubnetMask", ipv4_config.netmask}, 1270 {"Address", ipv4_config.address}}); 1271 1272 if (!ipv4_config.gateway.empty()) 1273 { 1274 ipv4_array.back()["Gateway"] = ipv4_config.gateway; 1275 } 1276 } 1277 } 1278 } 1279 } 1280 1281 /** 1282 * Functions triggers appropriate requests on DBus 1283 */ 1284 void doGet(crow::Response &res, const crow::Request &req, 1285 const std::vector<std::string> ¶ms) override 1286 { 1287 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1288 if (params.size() != 1) 1289 { 1290 messages::internalError(asyncResp->res); 1291 return; 1292 } 1293 1294 getEthernetIfaceData( 1295 params[0], 1296 [this, asyncResp, iface_id{std::string(params[0])}]( 1297 const bool &success, const EthernetInterfaceData ðData, 1298 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1299 if (!success) 1300 { 1301 // TODO(Pawel)consider distinguish between non existing 1302 // object, and other errors 1303 messages::resourceNotFound(asyncResp->res, 1304 "EthernetInterface", iface_id); 1305 return; 1306 } 1307 asyncResp->res.jsonValue["@odata.type"] = 1308 "#EthernetInterface.v1_2_0.EthernetInterface"; 1309 asyncResp->res.jsonValue["@odata.context"] = 1310 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface"; 1311 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface"; 1312 asyncResp->res.jsonValue["Description"] = 1313 "Management Network Interface"; 1314 1315 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 1316 ipv4Data); 1317 }); 1318 } 1319 1320 void doPatch(crow::Response &res, const crow::Request &req, 1321 const std::vector<std::string> ¶ms) override 1322 { 1323 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1324 if (params.size() != 1) 1325 { 1326 messages::internalError(asyncResp->res); 1327 return; 1328 } 1329 1330 const std::string &iface_id = params[0]; 1331 1332 nlohmann::json patchReq; 1333 if (!json_util::processJsonFromRequest(res, req, patchReq)) 1334 { 1335 return; 1336 } 1337 1338 // Get single eth interface data, and call the below callback for JSON 1339 // preparation 1340 getEthernetIfaceData( 1341 iface_id, 1342 [this, asyncResp, iface_id, patchReq = std::move(patchReq)]( 1343 const bool &success, const EthernetInterfaceData ðData, 1344 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1345 if (!success) 1346 { 1347 // ... otherwise return error 1348 // TODO(Pawel)consider distinguish between non existing 1349 // object, and other errors 1350 messages::resourceNotFound( 1351 asyncResp->res, "VLAN Network Interface", iface_id); 1352 return; 1353 } 1354 1355 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData, 1356 ipv4Data); 1357 1358 for (auto propertyIt : patchReq.items()) 1359 { 1360 if (propertyIt.key() == "VLAN") 1361 { 1362 handleVlanPatch(iface_id, propertyIt.value(), ethData, 1363 asyncResp); 1364 } 1365 else if (propertyIt.key() == "HostName") 1366 { 1367 handleHostnamePatch(propertyIt.value(), asyncResp); 1368 } 1369 else if (propertyIt.key() == "IPv4Addresses") 1370 { 1371 handleIPv4Patch(iface_id, propertyIt.value(), ipv4Data, 1372 asyncResp); 1373 } 1374 else if (propertyIt.key() == "IPv6Addresses") 1375 { 1376 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet 1377 messages::propertyNotWritable(asyncResp->res, 1378 propertyIt.key()); 1379 } 1380 else 1381 { 1382 auto fieldInJsonIt = 1383 asyncResp->res.jsonValue.find(propertyIt.key()); 1384 1385 if (fieldInJsonIt == asyncResp->res.jsonValue.end()) 1386 { 1387 // Field not in scope of defined fields 1388 messages::propertyUnknown(asyncResp->res, 1389 propertyIt.key()); 1390 } 1391 else 1392 { 1393 // User attempted to modify non-writable field 1394 messages::propertyNotWritable(asyncResp->res, 1395 propertyIt.key()); 1396 } 1397 } 1398 } 1399 }); 1400 } 1401 }; 1402 1403 /** 1404 * VlanNetworkInterface derived class for delivering VLANNetworkInterface 1405 * Schema 1406 */ 1407 class VlanNetworkInterface : public Node 1408 { 1409 public: 1410 /* 1411 * Default Constructor 1412 */ 1413 template <typename CrowApp> 1414 VlanNetworkInterface(CrowApp &app) : 1415 Node(app, 1416 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>", 1417 std::string(), std::string()) 1418 { 1419 entityPrivileges = { 1420 {boost::beast::http::verb::get, {{"Login"}}}, 1421 {boost::beast::http::verb::head, {{"Login"}}}, 1422 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1423 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1424 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1425 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1426 } 1427 1428 private: 1429 void parseInterfaceData( 1430 nlohmann::json &json_response, const std::string &parent_iface_id, 1431 const std::string &iface_id, const EthernetInterfaceData ðData, 1432 const boost::container::flat_set<IPv4AddressData> &ipv4Data) 1433 { 1434 // Fill out obvious data... 1435 json_response["Id"] = iface_id; 1436 json_response["@odata.id"] = 1437 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id + 1438 "/VLANs/" + iface_id; 1439 1440 json_response["VLANEnable"] = true; 1441 if (ethData.vlan_id) 1442 { 1443 json_response["VLANId"] = *ethData.vlan_id; 1444 } 1445 } 1446 1447 bool verifyNames(crow::Response &res, const std::string &parent, 1448 const std::string &iface) 1449 { 1450 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1451 if (!boost::starts_with(iface, parent + "_")) 1452 { 1453 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface", 1454 iface); 1455 return false; 1456 } 1457 else 1458 { 1459 return true; 1460 } 1461 } 1462 1463 /** 1464 * Functions triggers appropriate requests on DBus 1465 */ 1466 void doGet(crow::Response &res, const crow::Request &req, 1467 const std::vector<std::string> ¶ms) override 1468 { 1469 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1470 // TODO(Pawel) this shall be parameterized call (two params) to get 1471 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'. 1472 // Check if there is required param, truly entering this shall be 1473 // impossible. 1474 if (params.size() != 2) 1475 { 1476 messages::internalError(res); 1477 res.end(); 1478 return; 1479 } 1480 1481 const std::string &parent_iface_id = params[0]; 1482 const std::string &iface_id = params[1]; 1483 res.jsonValue["@odata.type"] = 1484 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface"; 1485 res.jsonValue["@odata.context"] = 1486 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface"; 1487 res.jsonValue["Name"] = "VLAN Network Interface"; 1488 1489 if (!verifyNames(res, parent_iface_id, iface_id)) 1490 { 1491 return; 1492 } 1493 1494 // Get single eth interface data, and call the below callback for JSON 1495 // preparation 1496 getEthernetIfaceData( 1497 iface_id, 1498 [this, asyncResp, parent_iface_id, iface_id]( 1499 const bool &success, const EthernetInterfaceData ðData, 1500 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1501 if (success && ethData.vlan_id) 1502 { 1503 parseInterfaceData(asyncResp->res.jsonValue, 1504 parent_iface_id, iface_id, ethData, 1505 ipv4Data); 1506 } 1507 else 1508 { 1509 // ... otherwise return error 1510 // TODO(Pawel)consider distinguish between non existing 1511 // object, and other errors 1512 messages::resourceNotFound( 1513 asyncResp->res, "VLAN Network Interface", iface_id); 1514 } 1515 }); 1516 } 1517 1518 void doPatch(crow::Response &res, const crow::Request &req, 1519 const std::vector<std::string> ¶ms) override 1520 { 1521 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1522 if (params.size() != 2) 1523 { 1524 messages::internalError(asyncResp->res); 1525 return; 1526 } 1527 1528 const std::string &parentIfaceId = params[0]; 1529 const std::string &ifaceId = params[1]; 1530 1531 if (!verifyNames(res, parentIfaceId, ifaceId)) 1532 { 1533 return; 1534 } 1535 1536 nlohmann::json patchReq; 1537 if (!json_util::processJsonFromRequest(res, req, patchReq)) 1538 { 1539 return; 1540 } 1541 1542 // Get single eth interface data, and call the below callback for JSON 1543 // preparation 1544 getEthernetIfaceData( 1545 ifaceId, 1546 [this, asyncResp, parentIfaceId, ifaceId, 1547 patchReq{std::move(patchReq)}]( 1548 const bool &success, const EthernetInterfaceData ðData, 1549 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1550 if (!success) 1551 { 1552 // TODO(Pawel)consider distinguish between non existing 1553 // object, and other errors 1554 messages::resourceNotFound( 1555 asyncResp->res, "VLAN Network Interface", ifaceId); 1556 1557 return; 1558 } 1559 1560 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 1561 ifaceId, ethData, ipv4Data); 1562 1563 for (auto propertyIt : patchReq.items()) 1564 { 1565 if (propertyIt.key() != "VLANEnable" && 1566 propertyIt.key() != "VLANId") 1567 { 1568 auto fieldInJsonIt = 1569 asyncResp->res.jsonValue.find(propertyIt.key()); 1570 if (fieldInJsonIt == asyncResp->res.jsonValue.end()) 1571 { 1572 // Field not in scope of defined fields 1573 messages::propertyUnknown(asyncResp->res, 1574 propertyIt.key()); 1575 } 1576 else 1577 { 1578 // User attempted to modify non-writable field 1579 messages::propertyNotWritable(asyncResp->res, 1580 propertyIt.key()); 1581 } 1582 } 1583 } 1584 1585 EthernetInterface::handleVlanPatch(ifaceId, patchReq, ethData, 1586 asyncResp); 1587 }); 1588 } 1589 1590 void doDelete(crow::Response &res, const crow::Request &req, 1591 const std::vector<std::string> ¶ms) override 1592 { 1593 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1594 if (params.size() != 2) 1595 { 1596 messages::internalError(asyncResp->res); 1597 return; 1598 } 1599 1600 const std::string &parentIfaceId = params[0]; 1601 const std::string &ifaceId = params[1]; 1602 1603 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId)) 1604 { 1605 return; 1606 } 1607 1608 // Get single eth interface data, and call the below callback for JSON 1609 // preparation 1610 getEthernetIfaceData( 1611 ifaceId, 1612 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)}, 1613 ifaceId{std::string(ifaceId)}]( 1614 const bool &success, const EthernetInterfaceData ðData, 1615 const boost::container::flat_set<IPv4AddressData> &ipv4Data) { 1616 if (success && ethData.vlan_id) 1617 { 1618 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId, 1619 ifaceId, ethData, ipv4Data); 1620 1621 auto callback = 1622 [asyncResp](const boost::system::error_code ec) { 1623 if (ec) 1624 { 1625 messages::internalError(asyncResp->res); 1626 } 1627 }; 1628 crow::connections::systemBus->async_method_call( 1629 std::move(callback), "xyz.openbmc_project.Network", 1630 std::string("/xyz/openbmc_project/network/") + ifaceId, 1631 "xyz.openbmc_project.Object.Delete", "Delete"); 1632 } 1633 else 1634 { 1635 // ... otherwise return error 1636 // TODO(Pawel)consider distinguish between non existing 1637 // object, and other errors 1638 messages::resourceNotFound( 1639 asyncResp->res, "VLAN Network Interface", ifaceId); 1640 } 1641 }); 1642 } 1643 }; 1644 1645 /** 1646 * VlanNetworkInterfaceCollection derived class for delivering 1647 * VLANNetworkInterface Collection Schema 1648 */ 1649 class VlanNetworkInterfaceCollection : public Node 1650 { 1651 public: 1652 template <typename CrowApp> 1653 VlanNetworkInterfaceCollection(CrowApp &app) : 1654 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/", 1655 std::string()) 1656 { 1657 entityPrivileges = { 1658 {boost::beast::http::verb::get, {{"Login"}}}, 1659 {boost::beast::http::verb::head, {{"Login"}}}, 1660 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}}, 1661 {boost::beast::http::verb::put, {{"ConfigureComponents"}}}, 1662 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}}, 1663 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}}; 1664 } 1665 1666 private: 1667 /** 1668 * Functions triggers appropriate requests on DBus 1669 */ 1670 void doGet(crow::Response &res, const crow::Request &req, 1671 const std::vector<std::string> ¶ms) override 1672 { 1673 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1674 if (params.size() != 1) 1675 { 1676 // This means there is a problem with the router 1677 messages::internalError(asyncResp->res); 1678 return; 1679 } 1680 1681 const std::string &rootInterfaceName = params[0]; 1682 1683 // Get eth interface list, and call the below callback for JSON 1684 // preparation 1685 getEthernetIfaceList( 1686 [this, asyncResp, 1687 rootInterfaceName{std::string(rootInterfaceName)}]( 1688 const bool &success, 1689 const std::vector<std::string> &iface_list) { 1690 if (!success) 1691 { 1692 messages::internalError(asyncResp->res); 1693 return; 1694 } 1695 asyncResp->res.jsonValue["@odata.type"] = 1696 "#VLanNetworkInterfaceCollection." 1697 "VLanNetworkInterfaceCollection"; 1698 asyncResp->res.jsonValue["@odata.context"] = 1699 "/redfish/v1/$metadata" 1700 "#VLanNetworkInterfaceCollection." 1701 "VLanNetworkInterfaceCollection"; 1702 asyncResp->res.jsonValue["Name"] = 1703 "VLAN Network Interface Collection"; 1704 1705 nlohmann::json iface_array = nlohmann::json::array(); 1706 1707 for (const std::string &iface_item : iface_list) 1708 { 1709 if (boost::starts_with(iface_item, rootInterfaceName + "_")) 1710 { 1711 iface_array.push_back( 1712 {{"@odata.id", 1713 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1714 rootInterfaceName + "/VLANs/" + iface_item}}); 1715 } 1716 } 1717 1718 if (iface_array.empty()) 1719 { 1720 messages::resourceNotFound( 1721 asyncResp->res, "EthernetInterface", rootInterfaceName); 1722 return; 1723 } 1724 asyncResp->res.jsonValue["Members@odata.count"] = 1725 iface_array.size(); 1726 asyncResp->res.jsonValue["Members"] = std::move(iface_array); 1727 asyncResp->res.jsonValue["@odata.id"] = 1728 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + 1729 rootInterfaceName + "/VLANs"; 1730 }); 1731 } 1732 1733 void doPost(crow::Response &res, const crow::Request &req, 1734 const std::vector<std::string> ¶ms) override 1735 { 1736 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); 1737 if (params.size() != 1) 1738 { 1739 messages::internalError(asyncResp->res); 1740 return; 1741 } 1742 1743 nlohmann::json postReq; 1744 if (!json_util::processJsonFromRequest(res, req, postReq)) 1745 { 1746 return; 1747 } 1748 1749 auto vlanIdJson = postReq.find("VLANId"); 1750 if (vlanIdJson == postReq.end()) 1751 { 1752 messages::propertyMissing(asyncResp->res, "VLANId"); 1753 return; 1754 } 1755 1756 const uint64_t *vlanId = vlanIdJson->get_ptr<const uint64_t *>(); 1757 if (vlanId == nullptr) 1758 { 1759 messages::propertyValueTypeError(asyncResp->res, vlanIdJson->dump(), 1760 "VLANId"); 1761 return; 1762 } 1763 const std::string &rootInterfaceName = params[0]; 1764 1765 auto callback = [asyncResp](const boost::system::error_code ec) { 1766 if (ec) 1767 { 1768 // TODO(ed) make more consistent error messages based on 1769 // phosphor-network responses 1770 messages::internalError(asyncResp->res); 1771 return; 1772 } 1773 messages::created(asyncResp->res); 1774 }; 1775 crow::connections::systemBus->async_method_call( 1776 std::move(callback), "xyz.openbmc_project.Network", 1777 "/xyz/openbmc_project/network", 1778 "xyz.openbmc_project.Network.VLAN.Create", "VLAN", 1779 rootInterfaceName, static_cast<uint32_t>(*vlanId)); 1780 } 1781 }; 1782 } // namespace redfish 1783