blob: 9b743371a76b840f3bacec4fe214b62676e475e6 [file] [log] [blame]
Dung Caofaf6a6a2020-12-28 04:44:45 +00001/*
2 * Copyright (c) 2021 Ampere Computing LLC
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 *
16 * This is a daemon that forwards requests and receive responses from SSIF over
17 * the D-Bus IPMI Interface.
18 */
19
20#include <getopt.h>
21#include <linux/ipmi_bmc.h>
22
23#include <CLI/CLI.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/asio.hpp>
26#include <phosphor-logging/log.hpp>
27#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29
30#include <iostream>
31
Dung Cao51835392022-10-27 04:29:54 +000032/* Max length of ipmi ssif message included netfn and cmd field */
33#define IPMI_SSIF_PAYLOAD_MAX 254
34
Dung Caofaf6a6a2020-12-28 04:44:45 +000035using namespace phosphor::logging;
36
quang.ampere14480ee2022-10-18 09:04:51 +070037struct ipmi_cmd {
38 uint8_t netfn;
39 uint8_t lun;
40 uint8_t cmd;
41} prev_req_cmd;
42
Dung Caofaf6a6a2020-12-28 04:44:45 +000043static constexpr const char devBase[] = "/dev/ipmi-ssif-host";
44/* SSIF use IPMI SSIF channel */
45static constexpr const char* ssifBus =
46 "xyz.openbmc_project.Ipmi.Channel.ipmi_ssif";
47static constexpr const char* ssifObj =
48 "/xyz/openbmc_project/Ipmi/Channel/ipmi_ssif";
49
50class SsifChannel
51{
52 public:
Dung Cao51835392022-10-27 04:29:54 +000053 static constexpr size_t ssifMessageSize = IPMI_SSIF_PAYLOAD_MAX +
54 sizeof(unsigned int);
55 size_t sizeofLenField = sizeof(unsigned int);
Dung Caofaf6a6a2020-12-28 04:44:45 +000056 static constexpr uint8_t netFnShift = 2;
57 static constexpr uint8_t lunMask = (1 << netFnShift) - 1;
58
59 SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
60 std::shared_ptr<sdbusplus::asio::connection>& bus,
61 const std::string& channel, bool verbose);
62 bool initOK() const
63 {
64 return !!dev;
65 }
66 void channelAbort(const char* msg, const boost::system::error_code& ec);
67 void async_read();
68 void processMessage(const boost::system::error_code& ecRd, size_t rlen);
69
70 protected:
71 std::array<uint8_t, ssifMessageSize> xferBuffer;
72 std::shared_ptr<boost::asio::io_context> io;
73 std::shared_ptr<sdbusplus::asio::connection> bus;
74 std::shared_ptr<sdbusplus::asio::object_server> server;
75 std::unique_ptr<boost::asio::posix::stream_descriptor> dev = nullptr;
76 bool verbose;
77};
78
79SsifChannel::SsifChannel(std::shared_ptr<boost::asio::io_context>& io,
80 std::shared_ptr<sdbusplus::asio::connection>& bus,
81 const std::string& device, bool verbose) :
82 io(io),
83 bus(bus), verbose(verbose)
84{
85 std::string devName = devBase;
86 if (!device.empty())
87 {
88 devName = device;
89 }
90
91 // open device
92 int fd = open(devName.c_str(), O_RDWR | O_NONBLOCK);
93 if (fd < 0)
94 {
Dung Caof06b8fd2021-01-29 03:31:43 +000095 std::string msgToLog = "Couldn't open SSIF driver with flags O_RDWR."
96 " FILENAME=" + devName +
97 " ERROR=" + strerror(errno);
98 log<level::ERR>(msgToLog.c_str());
Dung Caofaf6a6a2020-12-28 04:44:45 +000099 return;
100 }
101 else
102 {
103 dev = std::make_unique<boost::asio::posix::stream_descriptor>(*io,
104 fd);
105 }
106
107 async_read();
108 // register interfaces...
109 server = std::make_shared<sdbusplus::asio::object_server>(bus);
110 std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
111 server->add_interface(ssifObj, ssifBus);
112 iface->initialize();
113}
114
115void SsifChannel::channelAbort(const char* msg,
116 const boost::system::error_code& ec)
117{
Dung Caof06b8fd2021-01-29 03:31:43 +0000118 std::string msgToLog = std::string(msg) + " ERROR=" + ec.message();
119 log<level::ERR>(msgToLog.c_str());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000120 // bail; maybe a restart from systemd can clear the error
121 io->stop();
122}
123
124void SsifChannel::async_read()
125{
126 boost::asio::async_read(*dev,
127 boost::asio::buffer(xferBuffer, xferBuffer.size()),
128 boost::asio::transfer_at_least(2),
129 [this](const boost::system::error_code& ec,
130 size_t rlen) {
131 processMessage(ec, rlen);
132 });
133}
134
135void SsifChannel::processMessage(const boost::system::error_code& ecRd,
136 size_t rlen)
137{
138 if (ecRd || rlen < 2)
139 {
140 channelAbort("Failed to read req msg", ecRd);
141 return;
142 }
143 async_read();
144
145 auto rawIter = xferBuffer.cbegin();
146 auto rawEnd = rawIter + rlen;
Dung Cao51835392022-10-27 04:29:54 +0000147 uint8_t netfn = rawIter[sizeofLenField] >> netFnShift;
148 uint8_t lun = rawIter[sizeofLenField] & lunMask;
149 uint8_t cmd = rawIter[sizeofLenField + 1];
quang.ampere14480ee2022-10-18 09:04:51 +0700150
151 prev_req_cmd.netfn = netfn;
152 prev_req_cmd.lun = lun;
153 prev_req_cmd.cmd = cmd;
154
Dung Caofaf6a6a2020-12-28 04:44:45 +0000155 if (verbose)
156 {
Dung Cao51835392022-10-27 04:29:54 +0000157 unsigned int lenRecv;
158 unsigned int *p = (unsigned int *) rawIter;
159 lenRecv = p[0];
Dung Caof06b8fd2021-01-29 03:31:43 +0000160 std::string msgToLog = "Read ssif request message with"
Dung Cao51835392022-10-27 04:29:54 +0000161 " len=" + std::to_string(lenRecv) +
Dung Caof06b8fd2021-01-29 03:31:43 +0000162 " netfn=" + std::to_string(netfn) +
163 " lun=" + std::to_string(lun) +
164 " cmd=" + std::to_string(cmd);
165 log<level::INFO>(msgToLog.c_str());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000166 }
167 // copy out payload
Dung Cao51835392022-10-27 04:29:54 +0000168 std::vector<uint8_t> data(rawIter + sizeofLenField + 2, rawEnd);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000169 // non-session bridges still need to pass an empty options map
170 std::map<std::string, std::variant<int>> options;
171 // the response is a tuple because dbus can only return a single value
172 using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t,
173 std::vector<uint8_t>>;
174 static constexpr const char ipmiQueueService[] =
175 "xyz.openbmc_project.Ipmi.Host";
176 static constexpr const char ipmiQueuePath[] =
177 "/xyz/openbmc_project/Ipmi";
178 static constexpr const char ipmiQueueIntf[] =
179 "xyz.openbmc_project.Ipmi.Server";
180 static constexpr const char ipmiQueueMethod[] = "execute";
quang.ampere14480ee2022-10-18 09:04:51 +0700181 static constexpr int dbusTimeout = 40000000;
182 bus->async_method_call_timed(
Dung Caofaf6a6a2020-12-28 04:44:45 +0000183 [this, netfnCap{netfn}, lunCap{lun},
184 cmdCap{cmd}](const boost::system::error_code& ec,
185 const IpmiDbusRspType& response) {
186 std::vector<uint8_t> rsp;
187 const auto& [netfn, lun, cmd, cc, payload] = response;
188 if (ec)
189 {
Dung Caof06b8fd2021-01-29 03:31:43 +0000190 std::string msgToLog = "ssif<->ipmid bus error:"
191 " netfn=" + std::to_string(netfn) +
192 " lun=" + std::to_string(lun) +
193 " cmd=" + std::to_string(cmd) +
194 " error=" + ec.message();
195 log<level::ERR>(msgToLog.c_str());
Dung Cao51835392022-10-27 04:29:54 +0000196 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
Dung Caofaf6a6a2020-12-28 04:44:45 +0000197 sizeof(cc));
quang.ampere14480ee2022-10-18 09:04:51 +0700198 /* if dbusTimeout, just return and do not send any response
199 * to let host continue with other commands, response here
200 * is potentionally make the response duplicated
201 * */
202 return;
Dung Caofaf6a6a2020-12-28 04:44:45 +0000203 }
204 else
205 {
quang.ampere14480ee2022-10-18 09:04:51 +0700206 if(prev_req_cmd.netfn != (netfn-1) ||
207 prev_req_cmd.lun != lun ||
208 prev_req_cmd.cmd != cmd)
209 {
210 /* Only send response to the last request command to void
211 * duplicated response which makes host driver confused and
212 * failed to create interface
213 * */
214 return;
215 }
Dung Cao51835392022-10-27 04:29:54 +0000216 rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) +
Dung Caofaf6a6a2020-12-28 04:44:45 +0000217 sizeof(cc) + payload.size());
218
219 // write the response
220 auto rspIter = rsp.begin();
Dung Cao51835392022-10-27 04:29:54 +0000221 unsigned int *p = (unsigned int *) &rspIter[0];
222 *p = payload.size() + 3;
223 rspIter[sizeofLenField] = (netfn << netFnShift) | (lun & lunMask);
224 rspIter[sizeofLenField + 1] = cmd;
225 rspIter[sizeofLenField + 2] = cc;
Dung Caofaf6a6a2020-12-28 04:44:45 +0000226 if (payload.size())
227 {
228 std::copy(payload.cbegin(), payload.cend(),
Dung Cao51835392022-10-27 04:29:54 +0000229 rspIter + sizeofLenField + 3);
Dung Caofaf6a6a2020-12-28 04:44:45 +0000230 }
231 }
232 if (verbose)
233 {
Dung Caof06b8fd2021-01-29 03:31:43 +0000234 std::string msgToLog = "Send ssif respond message with"
235 " len=" + std::to_string(payload.size() + 3) +
236 " netfn=" + std::to_string(netfn) +
237 " lun=" + std::to_string(lun) +
238 " cmd=" + std::to_string(cmd) +
239 " cc=" + std::to_string(cc);
240 log<level::INFO>(msgToLog.c_str());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000241 }
242 boost::system::error_code ecWr;
243 size_t wlen =
244 boost::asio::write(*dev, boost::asio::buffer(rsp), ecWr);
245 if (ecWr || wlen != rsp.size())
246 {
Dung Caof06b8fd2021-01-29 03:31:43 +0000247 std::string msgToLog = "Failed to send ssif respond message:"
248 " size=" + std::to_string(wlen) +
249 " expect=" + std::to_string(rsp.size()) +
250 " error=" + ecWr.message() +
251 " netfn=" + std::to_string(netfn) +
252 " lun=" + std::to_string(lun) +
253 " cmd=" + std::to_string(cmd) +
254 " cc=" + std::to_string(cc);
255 log<level::ERR>(msgToLog.c_str());
Dung Caofaf6a6a2020-12-28 04:44:45 +0000256 }
257 },
quang.ampere14480ee2022-10-18 09:04:51 +0700258 ipmiQueueService, ipmiQueuePath, ipmiQueueIntf, ipmiQueueMethod, dbusTimeout,
Dung Caofaf6a6a2020-12-28 04:44:45 +0000259 netfn, lun, cmd, data, options);
260}
261
262
263int main(int argc, char* argv[])
264{
265 CLI::App app("SSIF IPMI bridge");
266 std::string device;
267 app.add_option("-d,--device", device,
268 "use <DEVICE> file. Default is /dev/ipmi-ssif-host");
269 bool verbose = false;
270 app.add_option("-v,--verbose", verbose, "print more verbose output");
271 CLI11_PARSE(app, argc, argv);
272
273 // Connect to system bus
274 auto io = std::make_shared<boost::asio::io_context>();
275 sd_bus* dbus;
276 sd_bus_default_system(&dbus);
277 auto bus = std::make_shared<sdbusplus::asio::connection>(*io, dbus);
278 bus->request_name(ssifBus);
279 // Create the SSIF channel, listening on D-Bus and on the SSIF device
280 SsifChannel ssifchannel(io, bus, device, verbose);
281 if (!ssifchannel.initOK())
282 {
283 return EXIT_FAILURE;
284 }
285 io->run();
286
287 return 0;
288}