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