Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 1 | /* |
| 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 Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 32 | /* Max length of ipmi ssif message included netfn and cmd field */ |
| 33 | #define IPMI_SSIF_PAYLOAD_MAX 254 |
| 34 | |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 35 | using namespace phosphor::logging; |
| 36 | |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 37 | struct ipmi_cmd { |
| 38 | uint8_t netfn; |
| 39 | uint8_t lun; |
| 40 | uint8_t cmd; |
| 41 | } prev_req_cmd; |
| 42 | |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 43 | static constexpr const char devBase[] = "/dev/ipmi-ssif-host"; |
| 44 | /* SSIF use IPMI SSIF channel */ |
| 45 | static constexpr const char* ssifBus = |
| 46 | "xyz.openbmc_project.Ipmi.Channel.ipmi_ssif"; |
| 47 | static constexpr const char* ssifObj = |
| 48 | "/xyz/openbmc_project/Ipmi/Channel/ipmi_ssif"; |
| 49 | |
| 50 | class SsifChannel |
| 51 | { |
| 52 | public: |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 53 | static constexpr size_t ssifMessageSize = IPMI_SSIF_PAYLOAD_MAX + |
| 54 | sizeof(unsigned int); |
| 55 | size_t sizeofLenField = sizeof(unsigned int); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 56 | 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, |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 61 | const std::string& channel, bool verbose, |
| 62 | int numberOfReqNotRsp); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 63 | bool initOK() const |
| 64 | { |
| 65 | return !!dev; |
| 66 | } |
| 67 | void channelAbort(const char* msg, const boost::system::error_code& ec); |
| 68 | void async_read(); |
| 69 | void processMessage(const boost::system::error_code& ecRd, size_t rlen); |
| 70 | |
| 71 | protected: |
| 72 | std::array<uint8_t, ssifMessageSize> xferBuffer; |
| 73 | std::shared_ptr<boost::asio::io_context> io; |
| 74 | std::shared_ptr<sdbusplus::asio::connection> bus; |
| 75 | std::shared_ptr<sdbusplus::asio::object_server> server; |
| 76 | std::unique_ptr<boost::asio::posix::stream_descriptor> dev = nullptr; |
| 77 | bool verbose; |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 78 | /* This variable is always 0 when a request is responsed properly, |
| 79 | * any value larger than 0 meaning there is/are request(s) which |
| 80 | * not processed properly |
| 81 | * */ |
| 82 | int numberOfReqNotRsp; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | SsifChannel::SsifChannel(std::shared_ptr<boost::asio::io_context>& io, |
| 86 | std::shared_ptr<sdbusplus::asio::connection>& bus, |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 87 | const std::string& device, bool verbose, int numberOfReqNotRsp) : |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 88 | io(io), |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 89 | bus(bus), verbose(verbose), numberOfReqNotRsp(numberOfReqNotRsp) |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 90 | { |
| 91 | std::string devName = devBase; |
| 92 | if (!device.empty()) |
| 93 | { |
| 94 | devName = device; |
| 95 | } |
| 96 | |
| 97 | // open device |
| 98 | int fd = open(devName.c_str(), O_RDWR | O_NONBLOCK); |
| 99 | if (fd < 0) |
| 100 | { |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 101 | std::string msgToLog = "Couldn't open SSIF driver with flags O_RDWR." |
| 102 | " FILENAME=" + devName + |
| 103 | " ERROR=" + strerror(errno); |
| 104 | log<level::ERR>(msgToLog.c_str()); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | dev = std::make_unique<boost::asio::posix::stream_descriptor>(*io, |
| 110 | fd); |
| 111 | } |
| 112 | |
| 113 | async_read(); |
| 114 | // register interfaces... |
| 115 | server = std::make_shared<sdbusplus::asio::object_server>(bus); |
| 116 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
| 117 | server->add_interface(ssifObj, ssifBus); |
| 118 | iface->initialize(); |
| 119 | } |
| 120 | |
| 121 | void SsifChannel::channelAbort(const char* msg, |
| 122 | const boost::system::error_code& ec) |
| 123 | { |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 124 | std::string msgToLog = std::string(msg) + " ERROR=" + ec.message(); |
| 125 | log<level::ERR>(msgToLog.c_str()); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 126 | // bail; maybe a restart from systemd can clear the error |
| 127 | io->stop(); |
| 128 | } |
| 129 | |
| 130 | void SsifChannel::async_read() |
| 131 | { |
| 132 | boost::asio::async_read(*dev, |
| 133 | boost::asio::buffer(xferBuffer, xferBuffer.size()), |
| 134 | boost::asio::transfer_at_least(2), |
| 135 | [this](const boost::system::error_code& ec, |
| 136 | size_t rlen) { |
| 137 | processMessage(ec, rlen); |
| 138 | }); |
| 139 | } |
| 140 | |
| 141 | void SsifChannel::processMessage(const boost::system::error_code& ecRd, |
| 142 | size_t rlen) |
| 143 | { |
| 144 | if (ecRd || rlen < 2) |
| 145 | { |
| 146 | channelAbort("Failed to read req msg", ecRd); |
| 147 | return; |
| 148 | } |
| 149 | async_read(); |
| 150 | |
| 151 | auto rawIter = xferBuffer.cbegin(); |
| 152 | auto rawEnd = rawIter + rlen; |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 153 | uint8_t netfn = rawIter[sizeofLenField] >> netFnShift; |
| 154 | uint8_t lun = rawIter[sizeofLenField] & lunMask; |
| 155 | uint8_t cmd = rawIter[sizeofLenField + 1]; |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 156 | |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 157 | /* keep track of previous request */ |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 158 | prev_req_cmd.netfn = netfn; |
| 159 | prev_req_cmd.lun = lun; |
| 160 | prev_req_cmd.cmd = cmd; |
| 161 | |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 162 | /* there is a request coming */ |
| 163 | numberOfReqNotRsp++; |
| 164 | |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 165 | if (verbose) |
| 166 | { |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 167 | unsigned int lenRecv; |
| 168 | unsigned int *p = (unsigned int *) rawIter; |
| 169 | lenRecv = p[0]; |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 170 | std::string msgToLog = "Read ssif request message with" |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 171 | " len=" + std::to_string(lenRecv) + |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 172 | " netfn=" + std::to_string(netfn) + |
| 173 | " lun=" + std::to_string(lun) + |
| 174 | " cmd=" + std::to_string(cmd); |
| 175 | log<level::INFO>(msgToLog.c_str()); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 176 | } |
| 177 | // copy out payload |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 178 | std::vector<uint8_t> data(rawIter + sizeofLenField + 2, rawEnd); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 179 | // non-session bridges still need to pass an empty options map |
| 180 | std::map<std::string, std::variant<int>> options; |
| 181 | // the response is a tuple because dbus can only return a single value |
| 182 | using IpmiDbusRspType = std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, |
| 183 | std::vector<uint8_t>>; |
| 184 | static constexpr const char ipmiQueueService[] = |
| 185 | "xyz.openbmc_project.Ipmi.Host"; |
| 186 | static constexpr const char ipmiQueuePath[] = |
| 187 | "/xyz/openbmc_project/Ipmi"; |
| 188 | static constexpr const char ipmiQueueIntf[] = |
| 189 | "xyz.openbmc_project.Ipmi.Server"; |
| 190 | static constexpr const char ipmiQueueMethod[] = "execute"; |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 191 | static constexpr int dbusTimeout = 40000000; |
| 192 | bus->async_method_call_timed( |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 193 | [this, netfnCap{netfn}, lunCap{lun}, |
| 194 | cmdCap{cmd}](const boost::system::error_code& ec, |
| 195 | const IpmiDbusRspType& response) { |
| 196 | std::vector<uint8_t> rsp; |
| 197 | const auto& [netfn, lun, cmd, cc, payload] = response; |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 198 | numberOfReqNotRsp--; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 199 | if (ec) |
| 200 | { |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 201 | std::string msgToLog = "ssif<->ipmid bus error:" |
| 202 | " netfn=" + std::to_string(netfn) + |
| 203 | " lun=" + std::to_string(lun) + |
| 204 | " cmd=" + std::to_string(cmd) + |
| 205 | " error=" + ec.message(); |
| 206 | log<level::ERR>(msgToLog.c_str()); |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 207 | rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) + |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 208 | sizeof(cc)); |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 209 | /* if dbusTimeout, just return and do not send any response |
| 210 | * to let host continue with other commands, response here |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 211 | * is potentially make the response duplicated |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 212 | * */ |
| 213 | return; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 214 | } |
| 215 | else |
| 216 | { |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 217 | if ((prev_req_cmd.netfn != (netfn-1) || |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 218 | prev_req_cmd.lun != lun || |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 219 | prev_req_cmd.cmd != cmd) || |
| 220 | ((prev_req_cmd.netfn == (netfn-1) || |
| 221 | prev_req_cmd.lun == lun || |
| 222 | prev_req_cmd.cmd == cmd) && |
| 223 | numberOfReqNotRsp > 0)) |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 224 | { |
| 225 | /* Only send response to the last request command to void |
| 226 | * duplicated response which makes host driver confused and |
| 227 | * failed to create interface |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 228 | * |
| 229 | * Drop responses which are (1) different from the request |
| 230 | * (2) parameters are the same as request but handshake flow |
| 231 | * are in dupplicate request state |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 232 | * */ |
| 233 | return; |
| 234 | } |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 235 | rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) + |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 236 | sizeof(cc) + payload.size()); |
| 237 | |
| 238 | // write the response |
| 239 | auto rspIter = rsp.begin(); |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 240 | unsigned int *p = (unsigned int *) &rspIter[0]; |
| 241 | *p = payload.size() + 3; |
| 242 | rspIter[sizeofLenField] = (netfn << netFnShift) | (lun & lunMask); |
| 243 | rspIter[sizeofLenField + 1] = cmd; |
| 244 | rspIter[sizeofLenField + 2] = cc; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 245 | if (payload.size()) |
| 246 | { |
| 247 | std::copy(payload.cbegin(), payload.cend(), |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 248 | rspIter + sizeofLenField + 3); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | if (verbose) |
| 252 | { |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 253 | std::string msgToLog = "Send ssif respond message with" |
| 254 | " len=" + std::to_string(payload.size() + 3) + |
| 255 | " netfn=" + std::to_string(netfn) + |
| 256 | " lun=" + std::to_string(lun) + |
| 257 | " cmd=" + std::to_string(cmd) + |
| 258 | " cc=" + std::to_string(cc); |
| 259 | log<level::INFO>(msgToLog.c_str()); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 260 | } |
| 261 | boost::system::error_code ecWr; |
| 262 | size_t wlen = |
| 263 | boost::asio::write(*dev, boost::asio::buffer(rsp), ecWr); |
| 264 | if (ecWr || wlen != rsp.size()) |
| 265 | { |
Dung Cao | f06b8fd | 2021-01-29 03:31:43 +0000 | [diff] [blame] | 266 | std::string msgToLog = "Failed to send ssif respond message:" |
| 267 | " size=" + std::to_string(wlen) + |
| 268 | " expect=" + std::to_string(rsp.size()) + |
| 269 | " error=" + ecWr.message() + |
| 270 | " netfn=" + std::to_string(netfn) + |
| 271 | " lun=" + std::to_string(lun) + |
| 272 | " cmd=" + std::to_string(cmd) + |
| 273 | " cc=" + std::to_string(cc); |
| 274 | log<level::ERR>(msgToLog.c_str()); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 275 | } |
| 276 | }, |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 277 | ipmiQueueService, ipmiQueuePath, ipmiQueueIntf, ipmiQueueMethod, dbusTimeout, |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 278 | netfn, lun, cmd, data, options); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | int main(int argc, char* argv[]) |
| 283 | { |
| 284 | CLI::App app("SSIF IPMI bridge"); |
| 285 | std::string device; |
| 286 | app.add_option("-d,--device", device, |
| 287 | "use <DEVICE> file. Default is /dev/ipmi-ssif-host"); |
| 288 | bool verbose = false; |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 289 | int numberOfReqNotRsp = 0; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 290 | app.add_option("-v,--verbose", verbose, "print more verbose output"); |
| 291 | CLI11_PARSE(app, argc, argv); |
| 292 | |
| 293 | // Connect to system bus |
| 294 | auto io = std::make_shared<boost::asio::io_context>(); |
| 295 | sd_bus* dbus; |
| 296 | sd_bus_default_system(&dbus); |
| 297 | auto bus = std::make_shared<sdbusplus::asio::connection>(*io, dbus); |
| 298 | bus->request_name(ssifBus); |
| 299 | // Create the SSIF channel, listening on D-Bus and on the SSIF device |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame^] | 300 | SsifChannel ssifchannel(io, bus, device, verbose, numberOfReqNotRsp); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 301 | if (!ssifchannel.initOK()) |
| 302 | { |
| 303 | return EXIT_FAILURE; |
| 304 | } |
| 305 | io->run(); |
| 306 | |
| 307 | return 0; |
| 308 | } |