blob: 0b10a1beec2b31ff1404e1f0c2d9543d711b0f5e [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"
Myung Bae3f95a272024-03-13 07:32:02 -070011#include "utils/chassis_utils.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080012
Ed Tanousd7857202025-01-28 15:32:26 -080013#include <boost/system/errc.hpp>
George Liue99073f2022-12-09 11:06:16 +080014#include <boost/system/error_code.hpp>
Ed Tanousd7857202025-01-28 15:32:26 -080015#include <sdbusplus/message/native_types.hpp>
Nan Zhoucf7eba02022-07-21 23:53:20 +000016
Ed Tanousd7857202025-01-28 15:32:26 -080017#include <algorithm>
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 dbus::utility::getSubTree(
Myung Bae3f95a272024-03-13 07:32:02 -070068 "/xyz/openbmc_project/inventory", 0, chassisInterfaces,
Ed Tanous8cb2c022024-03-27 16:31:46 -070069 [callback = std::forward<CallbackFunc>(callback),
George Liue99073f2022-12-09 11:06:16 +080070 asyncResp](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080071 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040072 if (ec)
73 {
74 BMCWEB_LOG_ERROR("{}", ec);
75 return;
76 }
77 if (subtree.empty())
78 {
79 BMCWEB_LOG_DEBUG("Can't find chassis!");
80 return;
81 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -080082
Patrick Williamsbd79bce2024-08-16 15:22:20 -040083 std::size_t idPos = subtree[0].first.rfind('/');
84 if (idPos == std::string::npos ||
85 (idPos + 1) >= subtree[0].first.size())
86 {
87 messages::internalError(asyncResp->res);
88 BMCWEB_LOG_DEBUG("Can't parse chassis ID!");
89 return;
90 }
91 std::string chassisId = subtree[0].first.substr(idPos + 1);
92 BMCWEB_LOG_DEBUG("chassisId = {}", chassisId);
93 callback(chassisId, asyncResp);
94 });
Jennifer Leec5d03ff2019-03-08 15:42:58 -080095}
Abhishek Patelb4bec662021-06-21 17:32:02 -050096
97template <typename CallbackFunc>
Abhishek Patel5c3e9272021-06-24 10:11:33 -050098void getPortStatusAndPath(
99 std::span<const std::pair<std::string_view, std::string_view>>
100 protocolToDBus,
101 CallbackFunc&& callback)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500102{
103 crow::connections::systemBus->async_method_call(
Ed Tanous8cb2c022024-03-27 16:31:46 -0700104 [protocolToDBus, callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800105 const boost::system::error_code& ec,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800106 const std::vector<UnitStruct>& r) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400107 std::vector<std::tuple<std::string, std::string, bool>> socketData;
108 if (ec)
Ed Tanous002d39b2022-05-31 08:59:27 -0700109 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400110 BMCWEB_LOG_ERROR("{}", ec);
111 // return error code
112 callback(ec, socketData);
113 return;
Ed Tanous002d39b2022-05-31 08:59:27 -0700114 }
115
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400116 // save all service output into vector
117 for (const UnitStruct& unit : r)
Ed Tanous002d39b2022-05-31 08:59:27 -0700118 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400119 // Only traverse through <xyz>.socket units
120 const std::string& unitName =
121 std::get<NET_PROTO_UNIT_NAME>(unit);
Ed Tanous002d39b2022-05-31 08:59:27 -0700122
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400123 // find "." into unitsName
124 size_t lastCharPos = unitName.rfind('.');
125 if (lastCharPos == std::string::npos)
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500126 {
127 continue;
128 }
129
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400130 // is unitsName end with ".socket"
131 std::string unitNameEnd = unitName.substr(lastCharPos);
132 if (unitNameEnd != ".socket")
Andrew Geisslere6feae52023-04-06 10:27:41 -0600133 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400134 continue;
Andrew Geisslere6feae52023-04-06 10:27:41 -0600135 }
136
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400137 // find "@" into unitsName
138 if (size_t atCharPos = unitName.rfind('@');
139 atCharPos != std::string::npos)
140 {
141 lastCharPos = atCharPos;
142 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700143
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400144 // unitsName without "@eth(x).socket", only <xyz>
145 // unitsName without ".socket", only <xyz>
146 std::string unitNameStr = unitName.substr(0, lastCharPos);
147
148 for (const auto& kv : protocolToDBus)
149 {
150 // We are interested in services, which starts with
151 // mapped service name
152 if (unitNameStr != kv.second)
153 {
154 continue;
155 }
156
157 const std::string& socketPath =
158 std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
159 const std::string& unitState =
160 std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
161
162 bool isProtocolEnabled = ((unitState == "running") ||
163 (unitState == "listening"));
164
165 // Some protocols may have multiple services associated with
166 // them (for example IPMI). Look to see if we've already
167 // added an entry for the current protocol.
168 auto find = std::ranges::find_if(
169 socketData,
170 [&kv](const std::tuple<std::string, std::string, bool>&
171 i) { return std::get<1>(i) == kv.first; });
172 if (find != socketData.end())
173 {
174 // It only takes one enabled systemd service to consider
175 // a protocol enabled so if the current entry already
176 // has it enabled (or the new one is disabled) then just
177 // continue, otherwise remove the current one and add
178 // this new one.
179 if (std::get<2>(*find) || !isProtocolEnabled)
180 {
181 // Already registered as enabled or current one is
182 // not enabled, nothing to do
183 BMCWEB_LOG_DEBUG(
184 "protocolName: {}, already true or current one is false: {}",
185 kv.first, isProtocolEnabled);
186 break;
187 }
188 // Remove existing entry and replace with new one
189 // (below)
190 socketData.erase(find);
191 }
192
193 socketData.emplace_back(socketPath, std::string(kv.first),
194 isProtocolEnabled);
195 // We found service, return from inner loop.
196 break;
197 }
198 }
199
200 callback(ec, socketData);
201 },
Abhishek Patelb4bec662021-06-21 17:32:02 -0500202 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
203 "org.freedesktop.systemd1.Manager", "ListUnits");
204}
205
206template <typename CallbackFunc>
207void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
208{
Ed Tanousdeae6a72024-11-11 21:58:57 -0800209 dbus::utility::getProperty<
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700210 std::vector<std::tuple<std::string, std::string>>>(
211 *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
212 "org.freedesktop.systemd1.Socket", "Listen",
Ed Tanous8cb2c022024-03-27 16:31:46 -0700213 [callback = std::forward<CallbackFunc>(callback)](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800214 const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700215 const std::vector<std::tuple<std::string, std::string>>& resp) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400216 if (ec)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500217 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400218 BMCWEB_LOG_ERROR("{}", ec);
219 callback(ec, 0);
220 return;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500221 }
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400222 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);
230 BMCWEB_LOG_ERROR("{}", ec1);
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)
246 {
247 ec3 = boost::system::errc::make_error_code(
248 boost::system::errc::result_out_of_range);
249 }
250 // return error code
251 callback(ec3, 0);
252 BMCWEB_LOG_ERROR("{}", ec3);
253 }
254 callback(ec, port);
255 });
Abhishek Patelb4bec662021-06-21 17:32:02 -0500256}
257
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800258} // namespace redfish