1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2019 Intel Corporation 4c5d03ff4SJennifer Lee #pragma once 5c5d03ff4SJennifer Lee 63ccb3adbSEd Tanous #include "async_resp.hpp" 73ccb3adbSEd Tanous #include "dbus_utility.hpp" 83ccb3adbSEd Tanous #include "error_messages.hpp" 93ccb3adbSEd Tanous 10e99073f5SGeorge Liu #include <boost/system/error_code.hpp> 11cf7eba09SNan Zhou #include <sdbusplus/asio/property.hpp> 12cf7eba09SNan Zhou 13e99073f5SGeorge Liu #include <array> 14cf7eba09SNan Zhou #include <charconv> 153544d2a7SEd Tanous #include <ranges> 16e99073f5SGeorge Liu #include <string_view> 17cf7eba09SNan Zhou 18c5d03ff4SJennifer Lee namespace redfish 19c5d03ff4SJennifer Lee { 20c5d03ff4SJennifer Lee 21b4bec66bSAbhishek Patel enum NetworkProtocolUnitStructFields 22b4bec66bSAbhishek Patel { 23b4bec66bSAbhishek Patel NET_PROTO_UNIT_NAME, 24b4bec66bSAbhishek Patel NET_PROTO_UNIT_DESC, 25b4bec66bSAbhishek Patel NET_PROTO_UNIT_LOAD_STATE, 26b4bec66bSAbhishek Patel NET_PROTO_UNIT_ACTIVE_STATE, 27b4bec66bSAbhishek Patel NET_PROTO_UNIT_SUB_STATE, 28b4bec66bSAbhishek Patel NET_PROTO_UNIT_DEVICE, 29b4bec66bSAbhishek Patel NET_PROTO_UNIT_OBJ_PATH, 30b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_0, 31b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_EMPTY, 32b4bec66bSAbhishek Patel NET_PROTO_UNIT_ALWAYS_ROOT_PATH 33b4bec66bSAbhishek Patel }; 34b4bec66bSAbhishek Patel 35b4bec66bSAbhishek Patel enum NetworkProtocolListenResponseElements 36b4bec66bSAbhishek Patel { 37b4bec66bSAbhishek Patel NET_PROTO_LISTEN_TYPE, 38b4bec66bSAbhishek Patel NET_PROTO_LISTEN_STREAM 39b4bec66bSAbhishek Patel }; 40b4bec66bSAbhishek Patel 41b4bec66bSAbhishek Patel /** 42b4bec66bSAbhishek Patel * @brief D-Bus Unit structure returned in array from ListUnits Method 43b4bec66bSAbhishek Patel */ 44b4bec66bSAbhishek Patel using UnitStruct = 45b4bec66bSAbhishek Patel std::tuple<std::string, std::string, std::string, std::string, std::string, 46b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path, uint32_t, 47b4bec66bSAbhishek Patel std::string, sdbusplus::message::object_path>; 48b4bec66bSAbhishek Patel 49c5d03ff4SJennifer Lee template <typename CallbackFunc> 50daadfb2eSEd Tanous void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 51c5d03ff4SJennifer Lee CallbackFunc&& callback) 52c5d03ff4SJennifer Lee { 53c5d03ff4SJennifer Lee // Find managed chassis 54e99073f5SGeorge Liu constexpr std::array<std::string_view, 2> interfaces = { 55e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Board", 56e99073f5SGeorge Liu "xyz.openbmc_project.Inventory.Item.Chassis"}; 57e99073f5SGeorge Liu dbus::utility::getSubTree( 58e99073f5SGeorge Liu "/xyz/openbmc_project/inventory", 0, interfaces, 598cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback), 60e99073f5SGeorge Liu asyncResp](const boost::system::error_code& ec, 61b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 62c5d03ff4SJennifer Lee if (ec) 63c5d03ff4SJennifer Lee { 6462598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 65c5d03ff4SJennifer Lee return; 66c5d03ff4SJennifer Lee } 6726f6976fSEd Tanous if (subtree.empty()) 68c5d03ff4SJennifer Lee { 6962598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't find chassis!"); 70c5d03ff4SJennifer Lee return; 71c5d03ff4SJennifer Lee } 72c5d03ff4SJennifer Lee 73f23b7296SEd Tanous std::size_t idPos = subtree[0].first.rfind('/'); 74c5d03ff4SJennifer Lee if (idPos == std::string::npos || 75c5d03ff4SJennifer Lee (idPos + 1) >= subtree[0].first.size()) 76c5d03ff4SJennifer Lee { 77c5d03ff4SJennifer Lee messages::internalError(asyncResp->res); 7862598e31SEd Tanous BMCWEB_LOG_DEBUG("Can't parse chassis ID!"); 79c5d03ff4SJennifer Lee return; 80c5d03ff4SJennifer Lee } 81c5d03ff4SJennifer Lee std::string chassisId = subtree[0].first.substr(idPos + 1); 8262598e31SEd Tanous BMCWEB_LOG_DEBUG("chassisId = {}", chassisId); 83c5d03ff4SJennifer Lee callback(chassisId, asyncResp); 84e99073f5SGeorge Liu }); 85c5d03ff4SJennifer Lee } 86b4bec66bSAbhishek Patel 87b4bec66bSAbhishek Patel template <typename CallbackFunc> 885c3e9272SAbhishek Patel void getPortStatusAndPath( 895c3e9272SAbhishek Patel std::span<const std::pair<std::string_view, std::string_view>> 905c3e9272SAbhishek Patel protocolToDBus, 91b4bec66bSAbhishek Patel CallbackFunc&& callback) 92b4bec66bSAbhishek Patel { 93b4bec66bSAbhishek Patel crow::connections::systemBus->async_method_call( 948cb2c024SEd Tanous [protocolToDBus, callback = std::forward<CallbackFunc>(callback)]( 955e7e2dc5SEd Tanous const boost::system::error_code& ec, 96b4bec66bSAbhishek Patel const std::vector<UnitStruct>& r) { 975c3e9272SAbhishek Patel std::vector<std::tuple<std::string, std::string, bool>> socketData; 98b4bec66bSAbhishek Patel if (ec) 99b4bec66bSAbhishek Patel { 10062598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 101b4bec66bSAbhishek Patel // return error code 1025c3e9272SAbhishek Patel callback(ec, socketData); 103b4bec66bSAbhishek Patel return; 104b4bec66bSAbhishek Patel } 105b4bec66bSAbhishek Patel 1065c3e9272SAbhishek Patel // save all service output into vector 107b4bec66bSAbhishek Patel for (const UnitStruct& unit : r) 108b4bec66bSAbhishek Patel { 109b4bec66bSAbhishek Patel // Only traverse through <xyz>.socket units 110bd79bce8SPatrick Williams const std::string& unitName = 111bd79bce8SPatrick Williams std::get<NET_PROTO_UNIT_NAME>(unit); 112b4bec66bSAbhishek Patel 113b4bec66bSAbhishek Patel // find "." into unitsName 114b4bec66bSAbhishek Patel size_t lastCharPos = unitName.rfind('.'); 115b4bec66bSAbhishek Patel if (lastCharPos == std::string::npos) 116b4bec66bSAbhishek Patel { 117b4bec66bSAbhishek Patel continue; 118b4bec66bSAbhishek Patel } 119b4bec66bSAbhishek Patel 120b4bec66bSAbhishek Patel // is unitsName end with ".socket" 121b4bec66bSAbhishek Patel std::string unitNameEnd = unitName.substr(lastCharPos); 12255f79e6fSEd Tanous if (unitNameEnd != ".socket") 123b4bec66bSAbhishek Patel { 124b4bec66bSAbhishek Patel continue; 125b4bec66bSAbhishek Patel } 126b4bec66bSAbhishek Patel 127b4bec66bSAbhishek Patel // find "@" into unitsName 128b4bec66bSAbhishek Patel if (size_t atCharPos = unitName.rfind('@'); 129b4bec66bSAbhishek Patel atCharPos != std::string::npos) 130b4bec66bSAbhishek Patel { 131b4bec66bSAbhishek Patel lastCharPos = atCharPos; 132b4bec66bSAbhishek Patel } 133b4bec66bSAbhishek Patel 134b4bec66bSAbhishek Patel // unitsName without "@eth(x).socket", only <xyz> 135b4bec66bSAbhishek Patel // unitsName without ".socket", only <xyz> 136b4bec66bSAbhishek Patel std::string unitNameStr = unitName.substr(0, lastCharPos); 137b4bec66bSAbhishek Patel 1385c3e9272SAbhishek Patel for (const auto& kv : protocolToDBus) 1395c3e9272SAbhishek Patel { 140b4bec66bSAbhishek Patel // We are interested in services, which starts with 141b4bec66bSAbhishek Patel // mapped service name 1425c3e9272SAbhishek Patel if (unitNameStr != kv.second) 143b4bec66bSAbhishek Patel { 144b4bec66bSAbhishek Patel continue; 145b4bec66bSAbhishek Patel } 146b4bec66bSAbhishek Patel 147b4bec66bSAbhishek Patel const std::string& socketPath = 148b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_OBJ_PATH>(unit); 149b4bec66bSAbhishek Patel const std::string& unitState = 150b4bec66bSAbhishek Patel std::get<NET_PROTO_UNIT_SUB_STATE>(unit); 151b4bec66bSAbhishek Patel 15289492a15SPatrick Williams bool isProtocolEnabled = ((unitState == "running") || 15389492a15SPatrick Williams (unitState == "listening")); 1545c3e9272SAbhishek Patel 155e6feae51SAndrew Geissler // Some protocols may have multiple services associated with 156bd79bce8SPatrick Williams // them (for example IPMI). Look to see if we've already 157bd79bce8SPatrick Williams // added an entry for the current protocol. 1583544d2a7SEd Tanous auto find = std::ranges::find_if( 1593544d2a7SEd Tanous socketData, 160bd79bce8SPatrick Williams [&kv](const std::tuple<std::string, std::string, bool>& 161bd79bce8SPatrick Williams i) { return std::get<1>(i) == kv.first; }); 162e6feae51SAndrew Geissler if (find != socketData.end()) 163e6feae51SAndrew Geissler { 164bd79bce8SPatrick Williams // It only takes one enabled systemd service to consider 165bd79bce8SPatrick Williams // a protocol enabled so if the current entry already 166bd79bce8SPatrick Williams // has it enabled (or the new one is disabled) then just 167bd79bce8SPatrick Williams // continue, otherwise remove the current one and add 168bd79bce8SPatrick Williams // this new one. 169e6feae51SAndrew Geissler if (std::get<2>(*find) || !isProtocolEnabled) 170e6feae51SAndrew Geissler { 171bd79bce8SPatrick Williams // Already registered as enabled or current one is 172bd79bce8SPatrick Williams // not enabled, nothing to do 17362598e31SEd Tanous BMCWEB_LOG_DEBUG( 17462598e31SEd Tanous "protocolName: {}, already true or current one is false: {}", 17562598e31SEd Tanous kv.first, isProtocolEnabled); 176e6feae51SAndrew Geissler break; 177e6feae51SAndrew Geissler } 178bd79bce8SPatrick Williams // Remove existing entry and replace with new one 179bd79bce8SPatrick Williams // (below) 180e6feae51SAndrew Geissler socketData.erase(find); 181e6feae51SAndrew Geissler } 182e6feae51SAndrew Geissler 1835c3e9272SAbhishek Patel socketData.emplace_back(socketPath, std::string(kv.first), 1845c3e9272SAbhishek Patel isProtocolEnabled); 185b4bec66bSAbhishek Patel // We found service, return from inner loop. 1865c3e9272SAbhishek Patel break; 1875c3e9272SAbhishek Patel } 188b4bec66bSAbhishek Patel } 189b4bec66bSAbhishek Patel 1905c3e9272SAbhishek Patel callback(ec, socketData); 191b4bec66bSAbhishek Patel }, 192b4bec66bSAbhishek Patel "org.freedesktop.systemd1", "/org/freedesktop/systemd1", 193b4bec66bSAbhishek Patel "org.freedesktop.systemd1.Manager", "ListUnits"); 194b4bec66bSAbhishek Patel } 195b4bec66bSAbhishek Patel 196b4bec66bSAbhishek Patel template <typename CallbackFunc> 197b4bec66bSAbhishek Patel void getPortNumber(const std::string& socketPath, CallbackFunc&& callback) 198b4bec66bSAbhishek Patel { 199deae6a78SEd Tanous dbus::utility::getProperty< 2001e1e598dSJonathan Doman std::vector<std::tuple<std::string, std::string>>>( 2011e1e598dSJonathan Doman *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath, 2021e1e598dSJonathan Doman "org.freedesktop.systemd1.Socket", "Listen", 2038cb2c024SEd Tanous [callback = std::forward<CallbackFunc>(callback)]( 2045e7e2dc5SEd Tanous const boost::system::error_code& ec, 2051e1e598dSJonathan Doman const std::vector<std::tuple<std::string, std::string>>& resp) { 206b4bec66bSAbhishek Patel if (ec) 207b4bec66bSAbhishek Patel { 20862598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec); 209b4bec66bSAbhishek Patel callback(ec, 0); 210b4bec66bSAbhishek Patel return; 211b4bec66bSAbhishek Patel } 21226f6976fSEd Tanous if (resp.empty()) 213b4bec66bSAbhishek Patel { 214b4bec66bSAbhishek Patel // Network Protocol Listen Response Elements is empty 215b4bec66bSAbhishek Patel boost::system::error_code ec1 = 216b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 217b4bec66bSAbhishek Patel boost::system::errc::bad_message); 218b4bec66bSAbhishek Patel // return error code 219b4bec66bSAbhishek Patel callback(ec1, 0); 22062598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec1); 221b4bec66bSAbhishek Patel return; 222b4bec66bSAbhishek Patel } 223b4bec66bSAbhishek Patel const std::string& listenStream = 2241e1e598dSJonathan Doman std::get<NET_PROTO_LISTEN_STREAM>(resp[0]); 225b4bec66bSAbhishek Patel const char* pa = &listenStream[listenStream.rfind(':') + 1]; 226b4bec66bSAbhishek Patel int port{0}; 227b4bec66bSAbhishek Patel if (auto [p, ec2] = std::from_chars(pa, nullptr, port); 228b4bec66bSAbhishek Patel ec2 != std::errc()) 229b4bec66bSAbhishek Patel { 230b4bec66bSAbhishek Patel // there is only two possibility invalid_argument and 231b4bec66bSAbhishek Patel // result_out_of_range 232b4bec66bSAbhishek Patel boost::system::error_code ec3 = 233b4bec66bSAbhishek Patel boost::system::errc::make_error_code( 234b4bec66bSAbhishek Patel boost::system::errc::invalid_argument); 235b4bec66bSAbhishek Patel if (ec2 == std::errc::result_out_of_range) 236b4bec66bSAbhishek Patel { 237b4bec66bSAbhishek Patel ec3 = boost::system::errc::make_error_code( 238b4bec66bSAbhishek Patel boost::system::errc::result_out_of_range); 239b4bec66bSAbhishek Patel } 240b4bec66bSAbhishek Patel // return error code 241b4bec66bSAbhishek Patel callback(ec3, 0); 24262598e31SEd Tanous BMCWEB_LOG_ERROR("{}", ec3); 243b4bec66bSAbhishek Patel } 244b4bec66bSAbhishek Patel callback(ec, port); 2451e1e598dSJonathan Doman }); 246b4bec66bSAbhishek Patel } 247b4bec66bSAbhishek Patel 248c5d03ff4SJennifer Lee } // namespace redfish 249