1c5d03ff4SJennifer Lee /* 2c5d03ff4SJennifer Lee // Copyright (c) 2019 Intel Corporation 3c5d03ff4SJennifer Lee // 4c5d03ff4SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License"); 5c5d03ff4SJennifer Lee // you may not use this file except in compliance with the License. 6c5d03ff4SJennifer Lee // You may obtain a copy of the License at 7c5d03ff4SJennifer Lee // 8c5d03ff4SJennifer Lee // http://www.apache.org/licenses/LICENSE-2.0 9c5d03ff4SJennifer Lee // 10c5d03ff4SJennifer Lee // Unless required by applicable law or agreed to in writing, software 11c5d03ff4SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS, 12c5d03ff4SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13c5d03ff4SJennifer Lee // See the License for the specific language governing permissions and 14c5d03ff4SJennifer Lee // limitations under the License. 15c5d03ff4SJennifer Lee */ 16c5d03ff4SJennifer Lee #pragma once 17e585b905SEd Tanous #ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS 18c5d03ff4SJennifer Lee 193ccb3adbSEd Tanous #include "async_resp.hpp" 203ccb3adbSEd Tanous #include "dbus_utility.hpp" 213ccb3adbSEd Tanous #include "error_messages.hpp" 223ccb3adbSEd Tanous 23e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 24cf7eba09SNan Zhou #include <sdbusplus/asio/property.hpp> 25cf7eba09SNan Zhou 26e99073f5SGeorge Liu #include <array> 27cf7eba09SNan Zhou #include <charconv> 283544d2a7SEd Tanous #include <ranges> 29e99073f5SGeorge Liu #include <string_view> 30cf7eba09SNan Zhou 31c5d03ff4SJennifer Lee namespace redfish 32c5d03ff4SJennifer Lee { 33c5d03ff4SJennifer Lee 34b4bec66bSAbhishek Patel enum NetworkProtocolUnitStructFields 35b4bec66bSAbhishek Patel { 36b4bec66bSAbhishek Patel NET_PROTO_UNIT_NAME, 37b4bec66bSAbhishek Patel NET_PROTO_UNIT_DESC, 38b4bec66bSAbhishek Patel NET_PROTO_UNIT_LOAD_STATE, 39b4bec66bSAbhishek Patel NET_PROTO_UNIT_ACTIVE_STATE, 40b4bec66bSAbhishek Patel NET_PROTO_UNIT_SUB_STATE, 41b4bec66bSAbhishek Patel NET_PROTO_UNIT_DEVICE, 42b4bec66bSAbhishek Patel NET_PROTO_UNIT_OBJ_PATH, 43b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_0, 44b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_EMPTY, 45b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_ROOT_PATH 46b4bec66bSAbhishek Patel }; 47b4bec66bSAbhishek Patel 48b4bec66bSAbhishek Patel enum NetworkProtocolListenResponseElements 49b4bec66bSAbhishek Patel { 50b4bec66bSAbhishek Patel NET_PROTO_LISTEN_TYPE, 51b4bec66bSAbhishek Patel NET_PROTO_LISTEN_STREAM 52b4bec66bSAbhishek Patel }; 53b4bec66bSAbhishek Patel 54b4bec66bSAbhishek Patel /** 55b4bec66bSAbhishek Patel * @brief D-Bus Unit structure returned in array from ListUnits Method 56b4bec66bSAbhishek Patel */ 57b4bec66bSAbhishek Patel using UnitStruct = 58b4bec66bSAbhishek Patel std::tuple<std::string, std::string, std::string, std::string, std::string, 59b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path, uint32_t, 60b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path>; 61b4bec66bSAbhishek Patel 62c5d03ff4SJennifer Lee template <typename CallbackFunc> 638d1b46d7Szhanghch05 void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp, 64c5d03ff4SJennifer Lee CallbackFunc&& callback) 65c5d03ff4SJennifer Lee { 66c5d03ff4SJennifer Lee // Find managed chassis 67e99073f5SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = { 68e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Board", 69e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Chassis"}; 70e99073f5SGeorge Liu dbus::utility::getSubTree( 71e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 72*8cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback), 73e99073f5SGeorge Liu asyncResp](const boost::system::error_code& ec, 74b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 75c5d03ff4SJennifer Lee if (ec) 76c5d03ff4SJennifer Lee { 7762598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 78c5d03ff4SJennifer Lee return; 79c5d03ff4SJennifer Lee } 8026f6976fSEd Tanous if (subtree.empty()) 81c5d03ff4SJennifer Lee { 8262598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find chassis!"); 83c5d03ff4SJennifer Lee return; 84c5d03ff4SJennifer Lee } 85c5d03ff4SJennifer Lee 86f23b7296SEd Tanous std::size_t idPos = subtree[0].first.rfind('/'); 87c5d03ff4SJennifer Lee if (idPos == std::string::npos || 88c5d03ff4SJennifer Lee (idPos + 1) >= subtree[0].first.size()) 89c5d03ff4SJennifer Lee { 90c5d03ff4SJennifer Lee messages::internalError(asyncResp->res); 9162598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't parse chassis ID!"); 92c5d03ff4SJennifer Lee return; 93c5d03ff4SJennifer Lee } 94c5d03ff4SJennifer Lee std::string chassisId = subtree[0].first.substr(idPos + 1); 9562598e31SEd Tanous BMCWEB_LOG_DEBUG("chassisId = {}", chassisId); 96c5d03ff4SJennifer Lee callback(chassisId, asyncResp); 97e99073f5SGeorge Liu }); 98c5d03ff4SJennifer Lee } 99b4bec66bSAbhishek Patel 100b4bec66bSAbhishek Patel template <typename CallbackFunc> 1015c3e9272SAbhishek Patel void getPortStatusAndPath( 1025c3e9272SAbhishek Patel std::span<const std::pair<std::string_view, std::string_view>> 1035c3e9272SAbhishek Patel protocolToDBus, 104b4bec66bSAbhishek Patel CallbackFunc&& callback) 105b4bec66bSAbhishek Patel { 106b4bec66bSAbhishek Patel crow::connections::systemBus->async_method_call( 107*8cb2c024SEd Tanous [protocolToDBus, callback = std::forward<CallbackFunc>(callback)]( 1085e7e2dc5SEd Tanous const boost::system::error_code& ec, 109b4bec66bSAbhishek Patel const std::vector<UnitStruct>& r) { 1105c3e9272SAbhishek Patel std::vector<std::tuple<std::string, std::string, bool>> socketData; 111b4bec66bSAbhishek Patel if (ec) 112b4bec66bSAbhishek Patel { 11362598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 114b4bec66bSAbhishek Patel // return error code 1155c3e9272SAbhishek Patel callback(ec, socketData); 116b4bec66bSAbhishek Patel return; 117b4bec66bSAbhishek Patel } 118b4bec66bSAbhishek Patel 1195c3e9272SAbhishek Patel // save all service output into vector 120b4bec66bSAbhishek Patel for (const UnitStruct& unit : r) 121b4bec66bSAbhishek Patel { 122b4bec66bSAbhishek Patel // Only traverse through <xyz>.socket units 123002d39b4SEd Tanous const std::string& unitName = std::get<NET_PROTO_UNIT_NAME>(unit); 124b4bec66bSAbhishek Patel 125b4bec66bSAbhishek Patel // find "." into unitsName 126b4bec66bSAbhishek Patel size_t lastCharPos = unitName.rfind('.'); 127b4bec66bSAbhishek Patel if (lastCharPos == std::string::npos) 128b4bec66bSAbhishek Patel { 129b4bec66bSAbhishek Patel continue; 130b4bec66bSAbhishek Patel } 131b4bec66bSAbhishek Patel 132b4bec66bSAbhishek Patel // is unitsName end with ".socket" 133b4bec66bSAbhishek Patel std::string unitNameEnd = unitName.substr(lastCharPos); 13455f79e6fSEd Tanous if (unitNameEnd != ".socket") 135b4bec66bSAbhishek Patel { 136b4bec66bSAbhishek Patel continue; 137b4bec66bSAbhishek Patel } 138b4bec66bSAbhishek Patel 139b4bec66bSAbhishek Patel // find "@" into unitsName 140b4bec66bSAbhishek Patel if (size_t atCharPos = unitName.rfind('@'); 141b4bec66bSAbhishek Patel atCharPos != std::string::npos) 142b4bec66bSAbhishek Patel { 143b4bec66bSAbhishek Patel lastCharPos = atCharPos; 144b4bec66bSAbhishek Patel } 145b4bec66bSAbhishek Patel 146b4bec66bSAbhishek Patel // unitsName without "@eth(x).socket", only <xyz> 147b4bec66bSAbhishek Patel // unitsName without ".socket", only <xyz> 148b4bec66bSAbhishek Patel std::string unitNameStr = unitName.substr(0, lastCharPos); 149b4bec66bSAbhishek Patel 1505c3e9272SAbhishek Patel for (const auto& kv : protocolToDBus) 1515c3e9272SAbhishek Patel { 152b4bec66bSAbhishek Patel // We are interested in services, which starts with 153b4bec66bSAbhishek Patel // mapped service name 1545c3e9272SAbhishek Patel if (unitNameStr != kv.second) 155b4bec66bSAbhishek Patel { 156b4bec66bSAbhishek Patel continue; 157b4bec66bSAbhishek Patel } 158b4bec66bSAbhishek Patel 159b4bec66bSAbhishek Patel const std::string& socketPath = 160b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_OBJ_PATH>(unit); 161b4bec66bSAbhishek Patel const std::string& unitState = 162b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_SUB_STATE>(unit); 163b4bec66bSAbhishek Patel 16489492a15SPatrick Williams bool isProtocolEnabled = ((unitState == "running") || 16589492a15SPatrick Williams (unitState == "listening")); 1665c3e9272SAbhishek Patel 167e6feae51SAndrew Geissler // Some protocols may have multiple services associated with 168e6feae51SAndrew Geissler // them (for example IPMI). Look to see if we've already added 169e6feae51SAndrew Geissler // an entry for the current protocol. 1703544d2a7SEd Tanous auto find = std::ranges::find_if( 1713544d2a7SEd Tanous socketData, 172e6feae51SAndrew Geissler [&kv](const std::tuple<std::string, std::string, bool>& i) { 173e6feae51SAndrew Geissler return std::get<1>(i) == kv.first; 174e6feae51SAndrew Geissler }); 175e6feae51SAndrew Geissler if (find != socketData.end()) 176e6feae51SAndrew Geissler { 177e6feae51SAndrew Geissler // It only takes one enabled systemd service to consider a 178e6feae51SAndrew Geissler // protocol enabled so if the current entry already has it 179e6feae51SAndrew Geissler // enabled (or the new one is disabled) then just continue, 180e6feae51SAndrew Geissler // otherwise remove the current one and add this new one. 181e6feae51SAndrew Geissler if (std::get<2>(*find) || !isProtocolEnabled) 182e6feae51SAndrew Geissler { 183e6feae51SAndrew Geissler // Already registered as enabled or current one is not 184e6feae51SAndrew Geissler // enabled, nothing to do 18562598e31SEd Tanous BMCWEB_LOG_DEBUG( 18662598e31SEd Tanous "protocolName: {}, already true or current one is false: {}", 18762598e31SEd Tanous kv.first, isProtocolEnabled); 188e6feae51SAndrew Geissler break; 189e6feae51SAndrew Geissler } 190e6feae51SAndrew Geissler // Remove existing entry and replace with new one (below) 191e6feae51SAndrew Geissler socketData.erase(find); 192e6feae51SAndrew Geissler } 193e6feae51SAndrew Geissler 1945c3e9272SAbhishek Patel socketData.emplace_back(socketPath, std::string(kv.first), 1955c3e9272SAbhishek Patel isProtocolEnabled); 196b4bec66bSAbhishek Patel // We found service, return from inner loop. 1975c3e9272SAbhishek Patel break; 1985c3e9272SAbhishek Patel } 199b4bec66bSAbhishek Patel } 200b4bec66bSAbhishek Patel 2015c3e9272SAbhishek Patel callback(ec, socketData); 202b4bec66bSAbhishek Patel }, 203b4bec66bSAbhishek Patel "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 204b4bec66bSAbhishek Patel "org.freedesktop.systemd1.Manager", "ListUnits"); 205b4bec66bSAbhishek Patel } 206b4bec66bSAbhishek Patel 207b4bec66bSAbhishek Patel template <typename CallbackFunc> 208b4bec66bSAbhishek Patel void getPortNumber(const std::string& socketPath, CallbackFunc&& callback) 209b4bec66bSAbhishek Patel { 2101e1e598dSJonathan Doman sdbusplus::asio::getProperty< 2111e1e598dSJonathan Doman std::vector<std::tuple<std::string, std::string>>>( 2121e1e598dSJonathan Doman *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath, 2131e1e598dSJonathan Doman "org.freedesktop.systemd1.Socket", "Listen", 214*8cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback)]( 2155e7e2dc5SEd Tanous const boost::system::error_code& ec, 2161e1e598dSJonathan Doman const std::vector<std::tuple<std::string, std::string>>& resp) { 217b4bec66bSAbhishek Patel if (ec) 218b4bec66bSAbhishek Patel { 21962598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 220b4bec66bSAbhishek Patel callback(ec, 0); 221b4bec66bSAbhishek Patel return; 222b4bec66bSAbhishek Patel } 22326f6976fSEd Tanous if (resp.empty()) 224b4bec66bSAbhishek Patel { 225b4bec66bSAbhishek Patel // Network Protocol Listen Response Elements is empty 226b4bec66bSAbhishek Patel boost::system::error_code ec1 = 227b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 228b4bec66bSAbhishek Patel boost::system::errc::bad_message); 229b4bec66bSAbhishek Patel // return error code 230b4bec66bSAbhishek Patel callback(ec1, 0); 23162598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec1); 232b4bec66bSAbhishek Patel return; 233b4bec66bSAbhishek Patel } 234b4bec66bSAbhishek Patel const std::string& listenStream = 2351e1e598dSJonathan Doman std::get<NET_PROTO_LISTEN_STREAM>(resp[0]); 236b4bec66bSAbhishek Patel const char* pa = &listenStream[listenStream.rfind(':') + 1]; 237b4bec66bSAbhishek Patel int port{0}; 238b4bec66bSAbhishek Patel if (auto [p, ec2] = std::from_chars(pa, nullptr, port); 239b4bec66bSAbhishek Patel ec2 != std::errc()) 240b4bec66bSAbhishek Patel { 241b4bec66bSAbhishek Patel // there is only two possibility invalid_argument and 242b4bec66bSAbhishek Patel // result_out_of_range 243b4bec66bSAbhishek Patel boost::system::error_code ec3 = 244b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 245b4bec66bSAbhishek Patel boost::system::errc::invalid_argument); 246b4bec66bSAbhishek Patel if (ec2 == std::errc::result_out_of_range) 247b4bec66bSAbhishek Patel { 248b4bec66bSAbhishek Patel ec3 = boost::system::errc::make_error_code( 249b4bec66bSAbhishek Patel boost::system::errc::result_out_of_range); 250b4bec66bSAbhishek Patel } 251b4bec66bSAbhishek Patel // return error code 252b4bec66bSAbhishek Patel callback(ec3, 0); 25362598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec3); 254b4bec66bSAbhishek Patel } 255b4bec66bSAbhishek Patel callback(ec, port); 2561e1e598dSJonathan Doman }); 257b4bec66bSAbhishek Patel } 258b4bec66bSAbhishek Patel 259c5d03ff4SJennifer Lee } // namespace redfish 260c5d03ff4SJennifer Lee #endif 261