blob: aa0d86140ca4ad7bcb98efb4966212bfc1d79a9c [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3// SPDX-FileCopyrightText: Copyright 2019 Intel Corporation
Jennifer Leec5d03ff2019-03-08 15:42:58 -08004#pragma once
5
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08006#include "async_resp.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -08007#include "dbus_singleton.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08008#include "dbus_utility.hpp"
9#include "error_messages.hpp"
Ed Tanousd7857202025-01-28 15:32:26 -080010#include "logging.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080011
Ed Tanousd7857202025-01-28 15:32:26 -080012#include <boost/system/errc.hpp>
George Liue99073f2022-12-09 11:06:16 +080013#include <boost/system/error_code.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <sdbusplus/message/native_types.hpp>
Nan Zhoucf7eba02022-07-21 23:53:20 +000015
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <algorithm>
George Liue99073f2022-12-09 11:06:16 +080017#include <array>
Nan Zhoucf7eba02022-07-21 23:53:20 +000018#include <charconv>
Ed Tanousd7857202025-01-28 15:32:26 -080019#include <cstddef>
20#include <cstdint>
21#include <memory>
Ed Tanous3544d2a2023-08-06 18:12:20 -070022#include <ranges>
Ed Tanousd7857202025-01-28 15:32:26 -080023#include <span>
24#include <string>
George Liue99073f2022-12-09 11:06:16 +080025#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080026#include <system_error>
27#include <tuple>
28#include <utility>
29#include <vector>
Nan Zhoucf7eba02022-07-21 23:53:20 +000030
Jennifer Leec5d03ff2019-03-08 15:42:58 -080031namespace redfish
32{
33
Abhishek Patelb4bec662021-06-21 17:32:02 -050034enum 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
48enum 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 */
57using 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 Leec5d03ff2019-03-08 15:42:58 -080062template <typename CallbackFunc>
Ed Tanousdaadfb22024-12-20 09:25:54 -080063void getMainChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Jennifer Leec5d03ff2019-03-08 15:42:58 -080064 CallbackFunc&& callback)
65{
66 // Find managed chassis
George Liue99073f2022-12-09 11:06:16 +080067 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,
Ed Tanous8cb2c022024-03-27 16:31:46 -070072 [callback = std::forward<CallbackFunc>(callback),
George Liue99073f2022-12-09 11:06:16 +080073 asyncResp](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080074 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040075 if (ec)
76 {
77 BMCWEB_LOG_ERROR("{}", ec);
78 return;
79 }
80 if (subtree.empty())
81 {
82 BMCWEB_LOG_DEBUG("Can't find chassis!");
83 return;
84 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -080085
Patrick Williamsbd79bce2024-08-16 15:22:20 -040086 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);
91 BMCWEB_LOG_DEBUG("Can't parse chassis ID!");
92 return;
93 }
94 std::string chassisId = subtree[0].first.substr(idPos + 1);
95 BMCWEB_LOG_DEBUG("chassisId = {}", chassisId);
96 callback(chassisId, asyncResp);
97 });
Jennifer Leec5d03ff2019-03-08 15:42:58 -080098}
Abhishek Patelb4bec662021-06-21 17:32:02 -050099
100template <typename CallbackFunc>
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500101void getPortStatusAndPath(
102 std::span<const std::pair<std::string_view, std::string_view>>
103 protocolToDBus,
104 CallbackFunc&& callback)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500105{
106 crow::connections::systemBus->async_method_call(
Ed Tanous8cb2c022024-03-27 16:31:46 -0700107 [protocolToDBus, callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800108 const boost::system::error_code& ec,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800109 const std::vector<UnitStruct>& r) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400110 std::vector<std::tuple<std::string, std::string, bool>> socketData;
111 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -0700112 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400113 BMCWEB_LOG_ERROR("{}", ec);
114 // return error code
115 callback(ec, socketData);
116 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700117 }
118
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400119 // save all service output into vector
120 for (const UnitStruct& unit : r)
Ed Tanous002d39b2022-05-31 08:59:27 -0700121 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400122 // Only traverse through <xyz>.socket units
123 const std::string& unitName =
124 std::get<NET_PROTO_UNIT_NAME>(unit);
Ed Tanous002d39b2022-05-31 08:59:27 -0700125
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400126 // find "." into unitsName
127 size_t lastCharPos = unitName.rfind('.');
128 if (lastCharPos == std::string::npos)
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500129 {
130 continue;
131 }
132
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400133 // is unitsName end with ".socket"
134 std::string unitNameEnd = unitName.substr(lastCharPos);
135 if (unitNameEnd != ".socket")
Andrew Geisslere6feae52023-04-06 10:27:41 -0600136 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400137 continue;
Andrew Geisslere6feae52023-04-06 10:27:41 -0600138 }
139
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400140 // find "@" into unitsName
141 if (size_t atCharPos = unitName.rfind('@');
142 atCharPos != std::string::npos)
143 {
144 lastCharPos = atCharPos;
145 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700146
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400147 // unitsName without "@eth(x).socket", only <xyz>
148 // unitsName without ".socket", only <xyz>
149 std::string unitNameStr = unitName.substr(0, lastCharPos);
150
151 for (const auto& kv : protocolToDBus)
152 {
153 // We are interested in services, which starts with
154 // mapped service name
155 if (unitNameStr != kv.second)
156 {
157 continue;
158 }
159
160 const std::string& socketPath =
161 std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
162 const std::string& unitState =
163 std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
164
165 bool isProtocolEnabled = ((unitState == "running") ||
166 (unitState == "listening"));
167
168 // Some protocols may have multiple services associated with
169 // them (for example IPMI). Look to see if we've already
170 // added an entry for the current protocol.
171 auto find = std::ranges::find_if(
172 socketData,
173 [&kv](const std::tuple<std::string, std::string, bool>&
174 i) { return std::get<1>(i) == kv.first; });
175 if (find != socketData.end())
176 {
177 // It only takes one enabled systemd service to consider
178 // a protocol enabled so if the current entry already
179 // has it enabled (or the new one is disabled) then just
180 // continue, otherwise remove the current one and add
181 // this new one.
182 if (std::get<2>(*find) || !isProtocolEnabled)
183 {
184 // Already registered as enabled or current one is
185 // not enabled, nothing to do
186 BMCWEB_LOG_DEBUG(
187 "protocolName: {}, already true or current one is false: {}",
188 kv.first, isProtocolEnabled);
189 break;
190 }
191 // Remove existing entry and replace with new one
192 // (below)
193 socketData.erase(find);
194 }
195
196 socketData.emplace_back(socketPath, std::string(kv.first),
197 isProtocolEnabled);
198 // We found service, return from inner loop.
199 break;
200 }
201 }
202
203 callback(ec, socketData);
204 },
Abhishek Patelb4bec662021-06-21 17:32:02 -0500205 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
206 "org.freedesktop.systemd1.Manager", "ListUnits");
207}
208
209template <typename CallbackFunc>
210void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
211{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800212 dbus::utility::getProperty<
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700213 std::vector<std::tuple<std::string, std::string>>>(
214 *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
215 "org.freedesktop.systemd1.Socket", "Listen",
Ed Tanous8cb2c022024-03-27 16:31:46 -0700216 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800217 const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700218 const std::vector<std::tuple<std::string, std::string>>& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400219 if (ec)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500220 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400221 BMCWEB_LOG_ERROR("{}", ec);
222 callback(ec, 0);
223 return;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500224 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400225 if (resp.empty())
226 {
227 // Network Protocol Listen Response Elements is empty
228 boost::system::error_code ec1 =
229 boost::system::errc::make_error_code(
230 boost::system::errc::bad_message);
231 // return error code
232 callback(ec1, 0);
233 BMCWEB_LOG_ERROR("{}", ec1);
234 return;
235 }
236 const std::string& listenStream =
237 std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
238 const char* pa = &listenStream[listenStream.rfind(':') + 1];
239 int port{0};
240 if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
241 ec2 != std::errc())
242 {
243 // there is only two possibility invalid_argument and
244 // result_out_of_range
245 boost::system::error_code ec3 =
246 boost::system::errc::make_error_code(
247 boost::system::errc::invalid_argument);
248 if (ec2 == std::errc::result_out_of_range)
249 {
250 ec3 = boost::system::errc::make_error_code(
251 boost::system::errc::result_out_of_range);
252 }
253 // return error code
254 callback(ec3, 0);
255 BMCWEB_LOG_ERROR("{}", ec3);
256 }
257 callback(ec, port);
258 });
Abhishek Patelb4bec662021-06-21 17:32:02 -0500259}
260
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800261} // namespace redfish