blob: 2e4c6e5a38dbbba4e64ec5cd26fceb10b7863425 [file] [log] [blame]
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001/*
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 Leec5d03ff2019-03-08 15:42:58 -080016#pragma once
Ed Tanouse585b902021-11-02 15:50:54 -070017#ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
Jennifer Leec5d03ff2019-03-08 15:42:58 -080018
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080019#include "async_resp.hpp"
20#include "dbus_utility.hpp"
21#include "error_messages.hpp"
22
George Liue99073f2022-12-09 11:06:16 +080023#include <boost/system/error_code.hpp>
Nan Zhoucf7eba02022-07-21 23:53:20 +000024#include <sdbusplus/asio/property.hpp>
25
George Liue99073f2022-12-09 11:06:16 +080026#include <array>
Nan Zhoucf7eba02022-07-21 23:53:20 +000027#include <charconv>
George Liue99073f2022-12-09 11:06:16 +080028#include <string_view>
Nan Zhoucf7eba02022-07-21 23:53:20 +000029
Jennifer Leec5d03ff2019-03-08 15:42:58 -080030namespace redfish
31{
32
Abhishek Patelb4bec662021-06-21 17:32:02 -050033enum 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
47enum 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 */
56using 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 Leec5d03ff2019-03-08 15:42:58 -080061template <typename CallbackFunc>
zhanghch058d1b46d2021-04-01 11:18:24 +080062void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Jennifer Leec5d03ff2019-03-08 15:42:58 -080063 CallbackFunc&& callback)
64{
65 // Find managed chassis
George Liue99073f2022-12-09 11:06:16 +080066 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,
Jennifer Leec5d03ff2019-03-08 15:42:58 -080071 [callback,
George Liue99073f2022-12-09 11:06:16 +080072 asyncResp](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080073 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -070074 if (ec)
75 {
76 BMCWEB_LOG_ERROR << ec;
77 return;
78 }
79 if (subtree.empty())
80 {
81 BMCWEB_LOG_DEBUG << "Can't find chassis!";
82 return;
83 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -080084
Ed Tanous002d39b2022-05-31 08:59:27 -070085 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);
90 BMCWEB_LOG_DEBUG << "Can't parse chassis ID!";
91 return;
92 }
93 std::string chassisId = subtree[0].first.substr(idPos + 1);
94 BMCWEB_LOG_DEBUG << "chassisId = " << chassisId;
95 callback(chassisId, asyncResp);
George Liue99073f2022-12-09 11:06:16 +080096 });
Jennifer Leec5d03ff2019-03-08 15:42:58 -080097}
Abhishek Patelb4bec662021-06-21 17:32:02 -050098
99template <typename CallbackFunc>
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500100void getPortStatusAndPath(
101 std::span<const std::pair<std::string_view, std::string_view>>
102 protocolToDBus,
103 CallbackFunc&& callback)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500104{
105 crow::connections::systemBus->async_method_call(
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500106 [protocolToDBus, callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800107 const boost::system::error_code& ec,
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800108 const std::vector<UnitStruct>& r) {
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500109 std::vector<std::tuple<std::string, std::string, bool>> socketData;
Ed Tanous002d39b2022-05-31 08:59:27 -0700110 if (ec)
111 {
112 BMCWEB_LOG_ERROR << ec;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500113 // return error code
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500114 callback(ec, socketData);
Ed Tanous002d39b2022-05-31 08:59:27 -0700115 return;
116 }
117
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500118 // save all service output into vector
Ed Tanous002d39b2022-05-31 08:59:27 -0700119 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 Patel5c3e9272021-06-24 10:11:33 -0500149 for (const auto& kv : protocolToDBus)
Ed Tanous002d39b2022-05-31 08:59:27 -0700150 {
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500151 // 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 Williams89492a12023-05-10 07:51:34 -0500163 bool isProtocolEnabled = ((unitState == "running") ||
164 (unitState == "listening"));
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500165
166 socketData.emplace_back(socketPath, std::string(kv.first),
167 isProtocolEnabled);
168 // We found service, return from inner loop.
169 break;
Ed Tanous002d39b2022-05-31 08:59:27 -0700170 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700171 }
172
Abhishek Patel5c3e9272021-06-24 10:11:33 -0500173 callback(ec, socketData);
Abhishek Patelb4bec662021-06-21 17:32:02 -0500174 },
175 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
176 "org.freedesktop.systemd1.Manager", "ListUnits");
177}
178
179template <typename CallbackFunc>
180void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
181{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700182 sdbusplus::asio::getProperty<
183 std::vector<std::tuple<std::string, std::string>>>(
184 *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
185 "org.freedesktop.systemd1.Socket", "Listen",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800186 [callback{std::forward<CallbackFunc>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800187 const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700188 const std::vector<std::tuple<std::string, std::string>>& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700189 if (ec)
190 {
191 BMCWEB_LOG_ERROR << ec;
192 callback(ec, 0);
193 return;
194 }
195 if (resp.empty())
196 {
197 // Network Protocol Listen Response Elements is empty
198 boost::system::error_code ec1 =
199 boost::system::errc::make_error_code(
200 boost::system::errc::bad_message);
201 // return error code
202 callback(ec1, 0);
203 BMCWEB_LOG_ERROR << ec1;
204 return;
205 }
206 const std::string& listenStream =
207 std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
208 const char* pa = &listenStream[listenStream.rfind(':') + 1];
209 int port{0};
210 if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
211 ec2 != std::errc())
212 {
213 // there is only two possibility invalid_argument and
214 // result_out_of_range
215 boost::system::error_code ec3 =
216 boost::system::errc::make_error_code(
217 boost::system::errc::invalid_argument);
218 if (ec2 == std::errc::result_out_of_range)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500219 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700220 ec3 = boost::system::errc::make_error_code(
221 boost::system::errc::result_out_of_range);
Abhishek Patelb4bec662021-06-21 17:32:02 -0500222 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700223 // return error code
224 callback(ec3, 0);
225 BMCWEB_LOG_ERROR << ec3;
226 }
227 callback(ec, port);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700228 });
Abhishek Patelb4bec662021-06-21 17:32:02 -0500229}
230
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800231} // namespace redfish
232#endif