140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation 4c5d03ff4SJennifer Lee #pragma once 5c5d03ff4SJennifer Lee 63ccb3adbSEd Tanous #include "async_resp.hpp" 7d7857201SEd Tanous #include "dbus_singleton.hpp" 83ccb3adbSEd Tanous #include "dbus_utility.hpp" 93ccb3adbSEd Tanous #include "error_messages.hpp" 10d7857201SEd Tanous #include "logging.hpp" 11*3f95a277SMyung Bae #include "utils/chassis_utils.hpp" 123ccb3adbSEd Tanous 13d7857201SEd Tanous #include <boost/system/errc.hpp> 14e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 15d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp> 16cf7eba09SNan Zhou 17d7857201SEd Tanous #include <algorithm> 18cf7eba09SNan Zhou #include <charconv> 19d7857201SEd Tanous #include <cstddef> 20d7857201SEd Tanous #include <cstdint> 21d7857201SEd Tanous #include <memory> 223544d2a7SEd Tanous #include <ranges> 23d7857201SEd Tanous #include <span> 24d7857201SEd Tanous #include <string> 25e99073f5SGeorge Liu #include <string_view> 26d7857201SEd Tanous #include <system_error> 27d7857201SEd Tanous #include <tuple> 28d7857201SEd Tanous #include <utility> 29d7857201SEd Tanous #include <vector> 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> 63daadfb2eSEd Tanous void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 64c5d03ff4SJennifer Lee CallbackFunc&& callback) 65c5d03ff4SJennifer Lee { 66c5d03ff4SJennifer Lee // Find managed chassis 67e99073f5SGeorge Liu dbus::utility::getSubTree( 68*3f95a277SMyung Bae "/xyz/openbmc_project/inventory", 0, chassisInterfaces, 698cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback), 70e99073f5SGeorge Liu asyncResp](const boost::system::error_code& ec, 71b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 72c5d03ff4SJennifer Lee if (ec) 73c5d03ff4SJennifer Lee { 7462598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 75c5d03ff4SJennifer Lee return; 76c5d03ff4SJennifer Lee } 7726f6976fSEd Tanous if (subtree.empty()) 78c5d03ff4SJennifer Lee { 7962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find chassis!"); 80c5d03ff4SJennifer Lee return; 81c5d03ff4SJennifer Lee } 82c5d03ff4SJennifer Lee 83f23b7296SEd Tanous std::size_t idPos = subtree[0].first.rfind('/'); 84c5d03ff4SJennifer Lee if (idPos == std::string::npos || 85c5d03ff4SJennifer Lee (idPos + 1) >= subtree[0].first.size()) 86c5d03ff4SJennifer Lee { 87c5d03ff4SJennifer Lee messages::internalError(asyncResp->res); 8862598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't parse chassis ID!"); 89c5d03ff4SJennifer Lee return; 90c5d03ff4SJennifer Lee } 91c5d03ff4SJennifer Lee std::string chassisId = subtree[0].first.substr(idPos + 1); 9262598e31SEd Tanous BMCWEB_LOG_DEBUG("chassisId = {}", chassisId); 93c5d03ff4SJennifer Lee callback(chassisId, asyncResp); 94e99073f5SGeorge Liu }); 95c5d03ff4SJennifer Lee } 96b4bec66bSAbhishek Patel 97b4bec66bSAbhishek Patel template <typename CallbackFunc> 985c3e9272SAbhishek Patel void getPortStatusAndPath( 995c3e9272SAbhishek Patel std::span<const std::pair<std::string_view, std::string_view>> 1005c3e9272SAbhishek Patel protocolToDBus, 101b4bec66bSAbhishek Patel CallbackFunc&& callback) 102b4bec66bSAbhishek Patel { 103b4bec66bSAbhishek Patel crow::connections::systemBus->async_method_call( 1048cb2c024SEd Tanous [protocolToDBus, callback = std::forward<CallbackFunc>(callback)]( 1055e7e2dc5SEd Tanous const boost::system::error_code& ec, 106b4bec66bSAbhishek Patel const std::vector<UnitStruct>& r) { 1075c3e9272SAbhishek Patel std::vector<std::tuple<std::string, std::string, bool>> socketData; 108b4bec66bSAbhishek Patel if (ec) 109b4bec66bSAbhishek Patel { 11062598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 111b4bec66bSAbhishek Patel // return error code 1125c3e9272SAbhishek Patel callback(ec, socketData); 113b4bec66bSAbhishek Patel return; 114b4bec66bSAbhishek Patel } 115b4bec66bSAbhishek Patel 1165c3e9272SAbhishek Patel // save all service output into vector 117b4bec66bSAbhishek Patel for (const UnitStruct& unit : r) 118b4bec66bSAbhishek Patel { 119b4bec66bSAbhishek Patel // Only traverse through <xyz>.socket units 120bd79bce8SPatrick Williams const std::string& unitName = 121bd79bce8SPatrick Williams std::get<NET_PROTO_UNIT_NAME>(unit); 122b4bec66bSAbhishek Patel 123b4bec66bSAbhishek Patel // find "." into unitsName 124b4bec66bSAbhishek Patel size_t lastCharPos = unitName.rfind('.'); 125b4bec66bSAbhishek Patel if (lastCharPos == std::string::npos) 126b4bec66bSAbhishek Patel { 127b4bec66bSAbhishek Patel continue; 128b4bec66bSAbhishek Patel } 129b4bec66bSAbhishek Patel 130b4bec66bSAbhishek Patel // is unitsName end with ".socket" 131b4bec66bSAbhishek Patel std::string unitNameEnd = unitName.substr(lastCharPos); 13255f79e6fSEd Tanous if (unitNameEnd != ".socket") 133b4bec66bSAbhishek Patel { 134b4bec66bSAbhishek Patel continue; 135b4bec66bSAbhishek Patel } 136b4bec66bSAbhishek Patel 137b4bec66bSAbhishek Patel // find "@" into unitsName 138b4bec66bSAbhishek Patel if (size_t atCharPos = unitName.rfind('@'); 139b4bec66bSAbhishek Patel atCharPos != std::string::npos) 140b4bec66bSAbhishek Patel { 141b4bec66bSAbhishek Patel lastCharPos = atCharPos; 142b4bec66bSAbhishek Patel } 143b4bec66bSAbhishek Patel 144b4bec66bSAbhishek Patel // unitsName without "@eth(x).socket", only <xyz> 145b4bec66bSAbhishek Patel // unitsName without ".socket", only <xyz> 146b4bec66bSAbhishek Patel std::string unitNameStr = unitName.substr(0, lastCharPos); 147b4bec66bSAbhishek Patel 1485c3e9272SAbhishek Patel for (const auto& kv : protocolToDBus) 1495c3e9272SAbhishek Patel { 150b4bec66bSAbhishek Patel // We are interested in services, which starts with 151b4bec66bSAbhishek Patel // mapped service name 1525c3e9272SAbhishek Patel if (unitNameStr != kv.second) 153b4bec66bSAbhishek Patel { 154b4bec66bSAbhishek Patel continue; 155b4bec66bSAbhishek Patel } 156b4bec66bSAbhishek Patel 157b4bec66bSAbhishek Patel const std::string& socketPath = 158b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_OBJ_PATH>(unit); 159b4bec66bSAbhishek Patel const std::string& unitState = 160b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_SUB_STATE>(unit); 161b4bec66bSAbhishek Patel 16289492a15SPatrick Williams bool isProtocolEnabled = ((unitState == "running") || 16389492a15SPatrick Williams (unitState == "listening")); 1645c3e9272SAbhishek Patel 165e6feae51SAndrew Geissler // Some protocols may have multiple services associated with 166bd79bce8SPatrick Williams // them (for example IPMI). Look to see if we've already 167bd79bce8SPatrick Williams // added an entry for the current protocol. 1683544d2a7SEd Tanous auto find = std::ranges::find_if( 1693544d2a7SEd Tanous socketData, 170bd79bce8SPatrick Williams [&kv](const std::tuple<std::string, std::string, bool>& 171bd79bce8SPatrick Williams i) { return std::get<1>(i) == kv.first; }); 172e6feae51SAndrew Geissler if (find != socketData.end()) 173e6feae51SAndrew Geissler { 174bd79bce8SPatrick Williams // It only takes one enabled systemd service to consider 175bd79bce8SPatrick Williams // a protocol enabled so if the current entry already 176bd79bce8SPatrick Williams // has it enabled (or the new one is disabled) then just 177bd79bce8SPatrick Williams // continue, otherwise remove the current one and add 178bd79bce8SPatrick Williams // this new one. 179e6feae51SAndrew Geissler if (std::get<2>(*find) || !isProtocolEnabled) 180e6feae51SAndrew Geissler { 181bd79bce8SPatrick Williams // Already registered as enabled or current one is 182bd79bce8SPatrick Williams // not enabled, nothing to do 18362598e31SEd Tanous BMCWEB_LOG_DEBUG( 18462598e31SEd Tanous "protocolName: {}, already true or current one is false: {}", 18562598e31SEd Tanous kv.first, isProtocolEnabled); 186e6feae51SAndrew Geissler break; 187e6feae51SAndrew Geissler } 188bd79bce8SPatrick Williams // Remove existing entry and replace with new one 189bd79bce8SPatrick Williams // (below) 190e6feae51SAndrew Geissler socketData.erase(find); 191e6feae51SAndrew Geissler } 192e6feae51SAndrew Geissler 1935c3e9272SAbhishek Patel socketData.emplace_back(socketPath, std::string(kv.first), 1945c3e9272SAbhishek Patel isProtocolEnabled); 195b4bec66bSAbhishek Patel // We found service, return from inner loop. 1965c3e9272SAbhishek Patel break; 1975c3e9272SAbhishek Patel } 198b4bec66bSAbhishek Patel } 199b4bec66bSAbhishek Patel 2005c3e9272SAbhishek Patel callback(ec, socketData); 201b4bec66bSAbhishek Patel }, 202b4bec66bSAbhishek Patel "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 203b4bec66bSAbhishek Patel "org.freedesktop.systemd1.Manager", "ListUnits"); 204b4bec66bSAbhishek Patel } 205b4bec66bSAbhishek Patel 206b4bec66bSAbhishek Patel template <typename CallbackFunc> 207b4bec66bSAbhishek Patel void getPortNumber(const std::string& socketPath, CallbackFunc&& callback) 208b4bec66bSAbhishek Patel { 209deae6a78SEd Tanous dbus::utility::getProperty< 2101e1e598dSJonathan Doman std::vector<std::tuple<std::string, std::string>>>( 2111e1e598dSJonathan Doman *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath, 2121e1e598dSJonathan Doman "org.freedesktop.systemd1.Socket", "Listen", 2138cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback)]( 2145e7e2dc5SEd Tanous const boost::system::error_code& ec, 2151e1e598dSJonathan Doman const std::vector<std::tuple<std::string, std::string>>& resp) { 216b4bec66bSAbhishek Patel if (ec) 217b4bec66bSAbhishek Patel { 21862598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 219b4bec66bSAbhishek Patel callback(ec, 0); 220b4bec66bSAbhishek Patel return; 221b4bec66bSAbhishek Patel } 22226f6976fSEd Tanous if (resp.empty()) 223b4bec66bSAbhishek Patel { 224b4bec66bSAbhishek Patel // Network Protocol Listen Response Elements is empty 225b4bec66bSAbhishek Patel boost::system::error_code ec1 = 226b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 227b4bec66bSAbhishek Patel boost::system::errc::bad_message); 228b4bec66bSAbhishek Patel // return error code 229b4bec66bSAbhishek Patel callback(ec1, 0); 23062598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec1); 231b4bec66bSAbhishek Patel return; 232b4bec66bSAbhishek Patel } 233b4bec66bSAbhishek Patel const std::string& listenStream = 2341e1e598dSJonathan Doman std::get<NET_PROTO_LISTEN_STREAM>(resp[0]); 235b4bec66bSAbhishek Patel const char* pa = &listenStream[listenStream.rfind(':') + 1]; 236b4bec66bSAbhishek Patel int port{0}; 237b4bec66bSAbhishek Patel if (auto [p, ec2] = std::from_chars(pa, nullptr, port); 238b4bec66bSAbhishek Patel ec2 != std::errc()) 239b4bec66bSAbhishek Patel { 240b4bec66bSAbhishek Patel // there is only two possibility invalid_argument and 241b4bec66bSAbhishek Patel // result_out_of_range 242b4bec66bSAbhishek Patel boost::system::error_code ec3 = 243b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 244b4bec66bSAbhishek Patel boost::system::errc::invalid_argument); 245b4bec66bSAbhishek Patel if (ec2 == std::errc::result_out_of_range) 246b4bec66bSAbhishek Patel { 247b4bec66bSAbhishek Patel ec3 = boost::system::errc::make_error_code( 248b4bec66bSAbhishek Patel boost::system::errc::result_out_of_range); 249b4bec66bSAbhishek Patel } 250b4bec66bSAbhishek Patel // return error code 251b4bec66bSAbhishek Patel callback(ec3, 0); 25262598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec3); 253b4bec66bSAbhishek Patel } 254b4bec66bSAbhishek Patel callback(ec, port); 2551e1e598dSJonathan Doman }); 256b4bec66bSAbhishek Patel } 257b4bec66bSAbhishek Patel 258c5d03ff4SJennifer Lee } // namespace redfish 259