Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 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 | */ |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 16 | #pragma once |
| 17 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 18 | #include "async_resp.hpp" |
| 19 | #include "dbus_utility.hpp" |
| 20 | #include "error_messages.hpp" |
| 21 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 22 | #include <boost/system/error_code.hpp> |
Nan Zhou | cf7eba0 | 2022-07-21 23:53:20 +0000 | [diff] [blame] | 23 | #include <sdbusplus/asio/property.hpp> |
| 24 | |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 25 | #include <array> |
Nan Zhou | cf7eba0 | 2022-07-21 23:53:20 +0000 | [diff] [blame] | 26 | #include <charconv> |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 27 | #include <ranges> |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 28 | #include <string_view> |
Nan Zhou | cf7eba0 | 2022-07-21 23:53:20 +0000 | [diff] [blame] | 29 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 30 | namespace redfish |
| 31 | { |
| 32 | |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 33 | enum NetworkProtocolUnitStructFields |
| 34 | { |
| 35 | NET_PROTO_UNIT_NAME, |
| 36 | NET_PROTO_UNIT_DESC, |
| 37 | NET_PROTO_UNIT_LOAD_STATE, |
| 38 | NET_PROTO_UNIT_ACTIVE_STATE, |
| 39 | NET_PROTO_UNIT_SUB_STATE, |
| 40 | NET_PROTO_UNIT_DEVICE, |
| 41 | NET_PROTO_UNIT_OBJ_PATH, |
| 42 | NET_PROTO_UNIT_ALWAYS_0, |
| 43 | NET_PROTO_UNIT_ALWAYS_EMPTY, |
| 44 | NET_PROTO_UNIT_ALWAYS_ROOT_PATH |
| 45 | }; |
| 46 | |
| 47 | enum NetworkProtocolListenResponseElements |
| 48 | { |
| 49 | NET_PROTO_LISTEN_TYPE, |
| 50 | NET_PROTO_LISTEN_STREAM |
| 51 | }; |
| 52 | |
| 53 | /** |
| 54 | * @brief D-Bus Unit structure returned in array from ListUnits Method |
| 55 | */ |
| 56 | using UnitStruct = |
| 57 | std::tuple<std::string, std::string, std::string, std::string, std::string, |
| 58 | std::string, sdbusplus::message::object_path, uint32_t, |
| 59 | std::string, sdbusplus::message::object_path>; |
| 60 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 61 | template <typename CallbackFunc> |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 62 | void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp, |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 63 | CallbackFunc&& callback) |
| 64 | { |
| 65 | // Find managed chassis |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 66 | constexpr std::array<std::string_view, 2> interfaces = { |
| 67 | "xyz.openbmc_project.Inventory.Item.Board", |
| 68 | "xyz.openbmc_project.Inventory.Item.Chassis"}; |
| 69 | dbus::utility::getSubTree( |
| 70 | "/xyz/openbmc_project/inventory", 0, interfaces, |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 71 | [callback = std::forward<CallbackFunc>(callback), |
George Liu | e99073f | 2022-12-09 11:06:16 +0800 | [diff] [blame] | 72 | asyncResp](const boost::system::error_code& ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 73 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 74 | if (ec) |
| 75 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 76 | BMCWEB_LOG_ERROR("{}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 77 | return; |
| 78 | } |
| 79 | if (subtree.empty()) |
| 80 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 81 | BMCWEB_LOG_DEBUG("Can't find chassis!"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 82 | return; |
| 83 | } |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 84 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 85 | std::size_t idPos = subtree[0].first.rfind('/'); |
| 86 | if (idPos == std::string::npos || |
| 87 | (idPos + 1) >= subtree[0].first.size()) |
| 88 | { |
| 89 | messages::internalError(asyncResp->res); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 90 | BMCWEB_LOG_DEBUG("Can't parse chassis ID!"); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 91 | return; |
| 92 | } |
| 93 | std::string chassisId = subtree[0].first.substr(idPos + 1); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 94 | BMCWEB_LOG_DEBUG("chassisId = {}", chassisId); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 95 | callback(chassisId, asyncResp); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 96 | }); |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 97 | } |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 98 | |
| 99 | template <typename CallbackFunc> |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 100 | void getPortStatusAndPath( |
| 101 | std::span<const std::pair<std::string_view, std::string_view>> |
| 102 | protocolToDBus, |
| 103 | CallbackFunc&& callback) |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 104 | { |
| 105 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 106 | [protocolToDBus, callback = std::forward<CallbackFunc>(callback)]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 107 | const boost::system::error_code& ec, |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 108 | const std::vector<UnitStruct>& r) { |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 109 | std::vector<std::tuple<std::string, std::string, bool>> socketData; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 110 | if (ec) |
| 111 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 112 | BMCWEB_LOG_ERROR("{}", ec); |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 113 | // return error code |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 114 | callback(ec, socketData); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 118 | // save all service output into vector |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 119 | for (const UnitStruct& unit : r) |
| 120 | { |
| 121 | // Only traverse through <xyz>.socket units |
| 122 | const std::string& unitName = std::get<NET_PROTO_UNIT_NAME>(unit); |
| 123 | |
| 124 | // find "." into unitsName |
| 125 | size_t lastCharPos = unitName.rfind('.'); |
| 126 | if (lastCharPos == std::string::npos) |
| 127 | { |
| 128 | continue; |
| 129 | } |
| 130 | |
| 131 | // is unitsName end with ".socket" |
| 132 | std::string unitNameEnd = unitName.substr(lastCharPos); |
| 133 | if (unitNameEnd != ".socket") |
| 134 | { |
| 135 | continue; |
| 136 | } |
| 137 | |
| 138 | // find "@" into unitsName |
| 139 | if (size_t atCharPos = unitName.rfind('@'); |
| 140 | atCharPos != std::string::npos) |
| 141 | { |
| 142 | lastCharPos = atCharPos; |
| 143 | } |
| 144 | |
| 145 | // unitsName without "@eth(x).socket", only <xyz> |
| 146 | // unitsName without ".socket", only <xyz> |
| 147 | std::string unitNameStr = unitName.substr(0, lastCharPos); |
| 148 | |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 149 | for (const auto& kv : protocolToDBus) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 150 | { |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 151 | // We are interested in services, which starts with |
| 152 | // mapped service name |
| 153 | if (unitNameStr != kv.second) |
| 154 | { |
| 155 | continue; |
| 156 | } |
| 157 | |
| 158 | const std::string& socketPath = |
| 159 | std::get<NET_PROTO_UNIT_OBJ_PATH>(unit); |
| 160 | const std::string& unitState = |
| 161 | std::get<NET_PROTO_UNIT_SUB_STATE>(unit); |
| 162 | |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 163 | bool isProtocolEnabled = ((unitState == "running") || |
| 164 | (unitState == "listening")); |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 165 | |
Andrew Geissler | e6feae5 | 2023-04-06 10:27:41 -0600 | [diff] [blame] | 166 | // Some protocols may have multiple services associated with |
| 167 | // them (for example IPMI). Look to see if we've already added |
| 168 | // an entry for the current protocol. |
Ed Tanous | 3544d2a | 2023-08-06 18:12:20 -0700 | [diff] [blame] | 169 | auto find = std::ranges::find_if( |
| 170 | socketData, |
Andrew Geissler | e6feae5 | 2023-04-06 10:27:41 -0600 | [diff] [blame] | 171 | [&kv](const std::tuple<std::string, std::string, bool>& i) { |
| 172 | return std::get<1>(i) == kv.first; |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 173 | }); |
Andrew Geissler | e6feae5 | 2023-04-06 10:27:41 -0600 | [diff] [blame] | 174 | if (find != socketData.end()) |
| 175 | { |
| 176 | // It only takes one enabled systemd service to consider a |
| 177 | // protocol enabled so if the current entry already has it |
| 178 | // enabled (or the new one is disabled) then just continue, |
| 179 | // otherwise remove the current one and add this new one. |
| 180 | if (std::get<2>(*find) || !isProtocolEnabled) |
| 181 | { |
| 182 | // Already registered as enabled or current one is not |
| 183 | // enabled, nothing to do |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 184 | BMCWEB_LOG_DEBUG( |
| 185 | "protocolName: {}, already true or current one is false: {}", |
| 186 | kv.first, isProtocolEnabled); |
Andrew Geissler | e6feae5 | 2023-04-06 10:27:41 -0600 | [diff] [blame] | 187 | break; |
| 188 | } |
| 189 | // Remove existing entry and replace with new one (below) |
| 190 | socketData.erase(find); |
| 191 | } |
| 192 | |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 193 | socketData.emplace_back(socketPath, std::string(kv.first), |
| 194 | isProtocolEnabled); |
| 195 | // We found service, return from inner loop. |
| 196 | break; |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 197 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Abhishek Patel | 5c3e927 | 2021-06-24 10:11:33 -0500 | [diff] [blame] | 200 | callback(ec, socketData); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 201 | }, |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 202 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 203 | "org.freedesktop.systemd1.Manager", "ListUnits"); |
| 204 | } |
| 205 | |
| 206 | template <typename CallbackFunc> |
| 207 | void getPortNumber(const std::string& socketPath, CallbackFunc&& callback) |
| 208 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 209 | sdbusplus::asio::getProperty< |
| 210 | std::vector<std::tuple<std::string, std::string>>>( |
| 211 | *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath, |
| 212 | "org.freedesktop.systemd1.Socket", "Listen", |
Ed Tanous | 8cb2c02 | 2024-03-27 16:31:46 -0700 | [diff] [blame] | 213 | [callback = std::forward<CallbackFunc>(callback)]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 214 | const boost::system::error_code& ec, |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 215 | const std::vector<std::tuple<std::string, std::string>>& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 216 | if (ec) |
| 217 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 218 | BMCWEB_LOG_ERROR("{}", ec); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 219 | callback(ec, 0); |
| 220 | return; |
| 221 | } |
| 222 | if (resp.empty()) |
| 223 | { |
| 224 | // Network Protocol Listen Response Elements is empty |
| 225 | boost::system::error_code ec1 = |
| 226 | boost::system::errc::make_error_code( |
| 227 | boost::system::errc::bad_message); |
| 228 | // return error code |
| 229 | callback(ec1, 0); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 230 | BMCWEB_LOG_ERROR("{}", ec1); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | const std::string& listenStream = |
| 234 | std::get<NET_PROTO_LISTEN_STREAM>(resp[0]); |
| 235 | const char* pa = &listenStream[listenStream.rfind(':') + 1]; |
| 236 | int port{0}; |
| 237 | if (auto [p, ec2] = std::from_chars(pa, nullptr, port); |
| 238 | ec2 != std::errc()) |
| 239 | { |
| 240 | // there is only two possibility invalid_argument and |
| 241 | // result_out_of_range |
| 242 | boost::system::error_code ec3 = |
| 243 | boost::system::errc::make_error_code( |
| 244 | boost::system::errc::invalid_argument); |
| 245 | if (ec2 == std::errc::result_out_of_range) |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 246 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 247 | ec3 = boost::system::errc::make_error_code( |
| 248 | boost::system::errc::result_out_of_range); |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 249 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 250 | // return error code |
| 251 | callback(ec3, 0); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 252 | BMCWEB_LOG_ERROR("{}", ec3); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 253 | } |
| 254 | callback(ec, port); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 255 | }); |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 258 | } // namespace redfish |