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> |
Ed Tanous | d289aea | 2024-02-06 20:33:26 -0800 | [diff] [blame] | 25 | #include <boost/asio/completion_condition.hpp> |
| 26 | #include <boost/asio/io_context.hpp> |
| 27 | #include <boost/asio/read.hpp> |
| 28 | #include <boost/asio/steady_timer.hpp> |
| 29 | #include <boost/asio/write.hpp> |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 30 | #include <phosphor-logging/log.hpp> |
| 31 | #include <sdbusplus/asio/connection.hpp> |
| 32 | #include <sdbusplus/asio/object_server.hpp> |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 33 | #include <sdbusplus/timer.hpp> |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 34 | |
| 35 | #include <iostream> |
| 36 | |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 37 | /* Max length of ipmi ssif message included netfn and cmd field */ |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 38 | constexpr const size_t ipmiSsifPayloadMax = 254; |
Dung Cao | 5183539 | 2022-10-27 04:29:54 +0000 | [diff] [blame] | 39 | |
Ed Tanous | 498b87f | 2024-02-14 09:07:34 -0800 | [diff] [blame] | 40 | using phosphor::logging::level; |
| 41 | using phosphor::logging::log; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 42 | |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 43 | struct IpmiCmd |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 44 | { |
| 45 | uint8_t netfn; |
| 46 | uint8_t lun; |
| 47 | uint8_t cmd; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 48 | }; |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 49 | |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 50 | static constexpr std::string_view devBase = "/dev/ipmi-ssif-host"; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 51 | /* SSIF use IPMI SSIF channel */ |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 52 | |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 53 | /* The timer of driver is set to 15 seconds, need to send |
| 54 | * response before timeout occurs |
| 55 | */ |
| 56 | static constexpr const unsigned int hostReqTimeout = 14000000; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 57 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 58 | class SsifChannel |
| 59 | { |
| 60 | public: |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 61 | static constexpr size_t ssifMessageSize = ipmiSsifPayloadMax + |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 62 | sizeof(unsigned int); |
| 63 | size_t sizeofLenField = sizeof(unsigned int); |
| 64 | static constexpr uint8_t netFnShift = 2; |
| 65 | static constexpr uint8_t lunMask = (1 << netFnShift) - 1; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 66 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 67 | SsifChannel(std::shared_ptr<boost::asio::io_context>& io, |
| 68 | std::shared_ptr<sdbusplus::asio::connection>& bus, |
Ed Tanous | 98f55c7 | 2024-02-14 16:23:30 -0800 | [diff] [blame] | 69 | const std::string& device, bool verbose); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 70 | bool initOK() const |
| 71 | { |
Ed Tanous | fcd0ea4 | 2024-02-14 09:15:41 -0800 | [diff] [blame] | 72 | return dev.is_open(); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 73 | } |
| 74 | void channelAbort(const char* msg, const boost::system::error_code& ec); |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 75 | void asyncRead(); |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 76 | using IpmiDbusRspType = |
| 77 | std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>; |
| 78 | |
| 79 | void afterMethodCall(const boost::system::error_code& ec, |
| 80 | const IpmiDbusRspType& response); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 81 | void processMessage(const boost::system::error_code& ecRd, size_t rlen); |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 82 | int showNumOfReqNotRsp() const; |
Ed Tanous | fcd0ea4 | 2024-02-14 09:15:41 -0800 | [diff] [blame] | 83 | boost::asio::posix::stream_descriptor dev; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 84 | IpmiCmd prevReqCmd{}; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 85 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 86 | protected: |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 87 | std::array<uint8_t, ssifMessageSize> xferBuffer{}; |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 88 | std::shared_ptr<boost::asio::io_context> io; |
| 89 | std::shared_ptr<sdbusplus::asio::connection> bus; |
| 90 | std::shared_ptr<sdbusplus::asio::object_server> server; |
| 91 | bool verbose; |
Thang Q. Nguyen | baabadf | 2024-06-24 08:56:09 +0700 | [diff] [blame^] | 92 | /* This variable is always 0 when a request is responded properly, |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 93 | * any value larger than 0 meaning there is/are request(s) which |
| 94 | * not processed properly |
| 95 | * */ |
Ed Tanous | 98f55c7 | 2024-02-14 16:23:30 -0800 | [diff] [blame] | 96 | int numberOfReqNotRsp = 0; |
Ed Tanous | 4ae3b9e | 2024-02-14 08:54:03 -0800 | [diff] [blame] | 97 | |
| 98 | boost::asio::steady_timer rspTimer; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 101 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 102 | std::unique_ptr<SsifChannel> ssifchannel = nullptr; |
| 103 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 104 | SsifChannel::SsifChannel(std::shared_ptr<boost::asio::io_context>& io, |
| 105 | std::shared_ptr<sdbusplus::asio::connection>& bus, |
Ed Tanous | 98f55c7 | 2024-02-14 16:23:30 -0800 | [diff] [blame] | 106 | const std::string& device, bool verbose) : |
Ed Tanous | fcd0ea4 | 2024-02-14 09:15:41 -0800 | [diff] [blame] | 107 | dev(*io), |
Ed Tanous | 98f55c7 | 2024-02-14 16:23:30 -0800 | [diff] [blame] | 108 | io(io), bus(bus), verbose(verbose), rspTimer(*io) |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 109 | { |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 110 | std::string devName(devBase); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 111 | if (!device.empty()) |
| 112 | { |
| 113 | devName = device; |
| 114 | } |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 115 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 116 | // open device |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 117 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 118 | int fd = open(devName.c_str(), O_RDWR | O_NONBLOCK); |
| 119 | if (fd < 0) |
| 120 | { |
| 121 | std::string msgToLog = "Couldn't open SSIF driver with flags O_RDWR." |
| 122 | " FILENAME=" + |
| 123 | devName + " ERROR=" + strerror(errno); |
| 124 | log<level::ERR>(msgToLog.c_str()); |
| 125 | return; |
| 126 | } |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 127 | |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 128 | dev.assign(fd); |
| 129 | |
| 130 | asyncRead(); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 131 | // register interfaces... |
| 132 | server = std::make_shared<sdbusplus::asio::object_server>(bus); |
| 133 | std::shared_ptr<sdbusplus::asio::dbus_interface> iface = |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 134 | server->add_interface("/xyz/openbmc_project/Ipmi/Channel/ipmi_ssif", |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 135 | "xyz.openbmc_project.Ipmi.Channel.ipmi_ssif"); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 136 | iface->initialize(); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 139 | void SsifChannel::channelAbort(const char* msg, |
| 140 | const boost::system::error_code& ec) |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 141 | { |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 142 | std::string msgToLog = std::string(msg) + " ERROR=" + ec.message(); |
| 143 | log<level::ERR>(msgToLog.c_str()); |
| 144 | // bail; maybe a restart from systemd can clear the error |
| 145 | io->stop(); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 148 | void SsifChannel::asyncRead() |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 149 | { |
Ed Tanous | fcd0ea4 | 2024-02-14 09:15:41 -0800 | [diff] [blame] | 150 | boost::asio::async_read(dev, |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 151 | boost::asio::buffer(xferBuffer, xferBuffer.size()), |
| 152 | boost::asio::transfer_at_least(2), |
| 153 | [this](const boost::system::error_code& ec, |
| 154 | size_t rlen) { processMessage(ec, rlen); }); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 157 | int SsifChannel::showNumOfReqNotRsp() const |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 158 | { |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 159 | return numberOfReqNotRsp; |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 160 | } |
| 161 | |
Ed Tanous | d289aea | 2024-02-06 20:33:26 -0800 | [diff] [blame] | 162 | void rspTimerHandler(const boost::system::error_code& ec) |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 163 | { |
Ed Tanous | d289aea | 2024-02-06 20:33:26 -0800 | [diff] [blame] | 164 | if (ec == boost::asio::error::operation_aborted) |
| 165 | { |
| 166 | return; |
| 167 | } |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 168 | std::vector<uint8_t> rsp; |
| 169 | constexpr uint8_t ccResponseNotAvailable = 0xce; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 170 | IpmiCmd& prevReqCmd = ssifchannel->prevReqCmd; |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 171 | rsp.resize(ssifchannel->sizeofLenField + sizeof(prevReqCmd.cmd) + |
| 172 | sizeof(prevReqCmd.netfn) + sizeof(ccResponseNotAvailable)); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 173 | std::string msgToLog = "timeout, send response to keep host alive" |
| 174 | " netfn=" + |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 175 | std::to_string(prevReqCmd.netfn) + |
| 176 | " lun=" + std::to_string(prevReqCmd.lun) + |
| 177 | " cmd=" + std::to_string(prevReqCmd.cmd) + |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 178 | " cc=" + std::to_string(ccResponseNotAvailable) + |
| 179 | " numberOfReqNotRsp=" + |
| 180 | std::to_string(ssifchannel->showNumOfReqNotRsp()); |
| 181 | log<level::INFO>(msgToLog.c_str()); |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 182 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 183 | unsigned int* t = reinterpret_cast<unsigned int*>(rsp.data()); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 184 | *t = 3; |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 185 | rsp[ssifchannel->sizeofLenField] = ((prevReqCmd.netfn + 1) |
| 186 | << ssifchannel->netFnShift) | |
| 187 | (prevReqCmd.lun & ssifchannel->lunMask); |
| 188 | rsp[ssifchannel->sizeofLenField + 1] = prevReqCmd.cmd; |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 189 | rsp[ssifchannel->sizeofLenField + 2] = ccResponseNotAvailable; |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 190 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 191 | boost::system::error_code ecWr; |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 192 | |
Ed Tanous | fcd0ea4 | 2024-02-14 09:15:41 -0800 | [diff] [blame] | 193 | size_t wlen = boost::asio::write(ssifchannel->dev, boost::asio::buffer(rsp), |
| 194 | ecWr); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 195 | if (ecWr || wlen != rsp.size()) |
| 196 | { |
| 197 | msgToLog = |
| 198 | "Failed to send ssif respond message:" |
| 199 | " size=" + |
| 200 | std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) + |
| 201 | " error=" + ecWr.message() + |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 202 | " netfn=" + std::to_string(prevReqCmd.netfn + 1) + |
| 203 | " lun=" + std::to_string(prevReqCmd.lun) + |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 204 | " cmd=" + std::to_string(rsp[ssifchannel->sizeofLenField + 1]) + |
| 205 | " cc=" + std::to_string(ccResponseNotAvailable); |
| 206 | log<level::ERR>(msgToLog.c_str()); |
| 207 | } |
Quang Nguyen | f2994dc | 2022-11-25 16:31:49 +0700 | [diff] [blame] | 208 | } |
| 209 | |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 210 | void SsifChannel::afterMethodCall(const boost::system::error_code& ec, |
| 211 | const IpmiDbusRspType& response) |
| 212 | { |
| 213 | std::vector<uint8_t> rsp; |
| 214 | const auto& [netfn, lun, cmd, cc, payload] = response; |
| 215 | numberOfReqNotRsp--; |
| 216 | if (ec) |
| 217 | { |
| 218 | std::string msgToLog = |
| 219 | "ssif<->ipmid bus error:" |
| 220 | " netfn=" + |
| 221 | std::to_string(netfn) + " lun=" + std::to_string(lun) + |
| 222 | " cmd=" + std::to_string(cmd) + " error=" + ec.message(); |
| 223 | log<level::ERR>(msgToLog.c_str()); |
| 224 | rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) + sizeof(cc)); |
| 225 | /* if dbusTimeout, just return and do not send any response |
| 226 | * to let host continue with other commands, response here |
| 227 | * is potentially make the response duplicated |
| 228 | * */ |
| 229 | return; |
| 230 | } |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 231 | |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 232 | if ((prevReqCmd.netfn != (netfn - 1) || prevReqCmd.lun != lun || |
| 233 | prevReqCmd.cmd != cmd) || |
| 234 | ((prevReqCmd.netfn == (netfn - 1) && prevReqCmd.lun == lun && |
| 235 | prevReqCmd.cmd == cmd) && |
| 236 | numberOfReqNotRsp != 0)) |
| 237 | { |
| 238 | /* Only send response to the last request command to void |
| 239 | * duplicated response which makes host driver confused and |
| 240 | * failed to create interface |
| 241 | * |
| 242 | * Drop responses which are (1) different from the request |
| 243 | * (2) parameters are the same as request but handshake flow |
Thang Q. Nguyen | baabadf | 2024-06-24 08:56:09 +0700 | [diff] [blame^] | 244 | * are in duplicated request state |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 245 | * */ |
| 246 | if (verbose) |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 247 | { |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 248 | std::string msgToLog = |
| 249 | "Drop ssif respond message with" |
| 250 | " len=" + |
| 251 | std::to_string(payload.size() + 3) + |
| 252 | " netfn=" + std::to_string(netfn) + |
| 253 | " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) + |
| 254 | " cc=" + std::to_string(cc) + |
| 255 | " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp); |
| 256 | log<level::INFO>(msgToLog.c_str()); |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 257 | } |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | rsp.resize(sizeofLenField + sizeof(netfn) + sizeof(cmd) + sizeof(cc) + |
| 261 | payload.size()); |
| 262 | |
| 263 | // write the response |
| 264 | auto rspIter = rsp.begin(); |
| 265 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 266 | unsigned int* p = reinterpret_cast<unsigned int*>(&rspIter[0]); |
| 267 | *p = payload.size() + 3; |
| 268 | rspIter[sizeofLenField] = (netfn << netFnShift) | (lun & lunMask); |
| 269 | rspIter[sizeofLenField + 1] = cmd; |
| 270 | rspIter[sizeofLenField + 2] = cc; |
| 271 | if (static_cast<unsigned int>(!payload.empty()) != 0U) |
| 272 | { |
| 273 | std::copy(payload.cbegin(), payload.cend(), |
| 274 | rspIter + sizeofLenField + 3); |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 275 | } |
| 276 | if (verbose) |
| 277 | { |
| 278 | std::string msgToLog = |
| 279 | "Send ssif respond message with" |
| 280 | " len=" + |
| 281 | std::to_string(payload.size() + 3) + |
| 282 | " netfn=" + std::to_string(netfn) + " lun=" + std::to_string(lun) + |
| 283 | " cmd=" + std::to_string(cmd) + " cc=" + std::to_string(cc) + |
| 284 | " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp); |
| 285 | log<level::INFO>(msgToLog.c_str()); |
| 286 | } |
| 287 | boost::system::error_code ecWr; |
| 288 | size_t wlen = boost::asio::write(dev, boost::asio::buffer(rsp), ecWr); |
| 289 | if (ecWr || wlen != rsp.size()) |
| 290 | { |
| 291 | std::string msgToLog = |
| 292 | "Failed to send ssif respond message:" |
| 293 | " size=" + |
| 294 | std::to_string(wlen) + " expect=" + std::to_string(rsp.size()) + |
| 295 | " error=" + ecWr.message() + " netfn=" + std::to_string(netfn) + |
| 296 | " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) + |
| 297 | " cc=" + std::to_string(cc); |
| 298 | log<level::ERR>(msgToLog.c_str()); |
| 299 | } |
| 300 | rspTimer.cancel(); |
| 301 | } |
| 302 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 303 | void SsifChannel::processMessage(const boost::system::error_code& ecRd, |
| 304 | size_t rlen) |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 305 | { |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 306 | if (ecRd || rlen < 2) |
| 307 | { |
| 308 | channelAbort("Failed to read req msg", ecRd); |
| 309 | return; |
| 310 | } |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 311 | asyncRead(); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 312 | |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 313 | const auto* rawIter = xferBuffer.cbegin(); |
| 314 | const auto* rawEnd = rawIter + rlen; |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 315 | uint8_t netfn = rawIter[sizeofLenField] >> netFnShift; |
| 316 | uint8_t lun = rawIter[sizeofLenField] & lunMask; |
| 317 | uint8_t cmd = rawIter[sizeofLenField + 1]; |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 318 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 319 | /* keep track of previous request */ |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 320 | prevReqCmd.netfn = netfn; |
| 321 | prevReqCmd.lun = lun; |
| 322 | prevReqCmd.cmd = cmd; |
quang.ampere | 14480ee | 2022-10-18 09:04:51 +0700 | [diff] [blame] | 323 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 324 | /* there is a request coming */ |
| 325 | numberOfReqNotRsp++; |
| 326 | /* start response timer */ |
Ed Tanous | 4ae3b9e | 2024-02-14 08:54:03 -0800 | [diff] [blame] | 327 | rspTimer.expires_after(std::chrono::microseconds(hostReqTimeout)); |
| 328 | rspTimer.async_wait(rspTimerHandler); |
quang.name | 507782a | 2022-11-20 17:05:20 +0700 | [diff] [blame] | 329 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 330 | if (verbose) |
| 331 | { |
Ed Tanous | a1585be | 2024-02-14 10:57:31 -0800 | [diff] [blame] | 332 | unsigned int lenRecv = 0; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 333 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 334 | const unsigned int* p = reinterpret_cast<const unsigned int*>(rawIter); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 335 | lenRecv = p[0]; |
| 336 | std::string msgToLog = |
| 337 | "Read ssif request message with" |
| 338 | " len=" + |
| 339 | std::to_string(lenRecv) + " netfn=" + std::to_string(netfn) + |
| 340 | " lun=" + std::to_string(lun) + " cmd=" + std::to_string(cmd) + |
| 341 | " numberOfReqNotRsp=" + std::to_string(numberOfReqNotRsp); |
| 342 | log<level::INFO>(msgToLog.c_str()); |
| 343 | } |
| 344 | // copy out payload |
| 345 | std::vector<uint8_t> data(rawIter + sizeofLenField + 2, rawEnd); |
| 346 | // non-session bridges still need to pass an empty options map |
| 347 | std::map<std::string, std::variant<int>> options; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 348 | static constexpr const char* ipmiQueueService = |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 349 | "xyz.openbmc_project.Ipmi.Host"; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 350 | static constexpr const char* ipmiQueuePath = "/xyz/openbmc_project/Ipmi"; |
| 351 | static constexpr const char* ipmiQueueIntf = |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 352 | "xyz.openbmc_project.Ipmi.Server"; |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 353 | static constexpr const char* ipmiQueueMethod = "execute"; |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 354 | /* now, we do not care dbus timeout, since we already have actions |
| 355 | * before dbus timeout occurs |
| 356 | */ |
| 357 | static constexpr unsigned int dbusTimeout = 60000000; |
| 358 | bus->async_method_call_timed( |
Ed Tanous | b6ad9dc | 2024-02-14 09:28:20 -0800 | [diff] [blame] | 359 | [this](const boost::system::error_code& ec, |
| 360 | const IpmiDbusRspType& response) { |
| 361 | afterMethodCall(ec, response); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 362 | }, |
| 363 | ipmiQueueService, ipmiQueuePath, ipmiQueueIntf, ipmiQueueMethod, |
| 364 | dbusTimeout, netfn, lun, cmd, data, options); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 367 | int main(int argc, char* argv[]) |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 368 | { |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 369 | CLI::App app("SSIF IPMI bridge"); |
| 370 | std::string device; |
| 371 | app.add_option("-d,--device", device, |
| 372 | "use <DEVICE> file. Default is /dev/ipmi-ssif-host"); |
| 373 | bool verbose = false; |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 374 | app.add_option("-v,--verbose", verbose, "print more verbose output"); |
| 375 | CLI11_PARSE(app, argc, argv); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 376 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 377 | auto io = std::make_shared<boost::asio::io_context>(); |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 378 | |
Ed Tanous | d289aea | 2024-02-06 20:33:26 -0800 | [diff] [blame] | 379 | auto bus = std::make_shared<sdbusplus::asio::connection>(*io); |
Ed Tanous | 7fa7e6c | 2024-02-14 14:33:32 -0800 | [diff] [blame] | 380 | bus->request_name("xyz.openbmc_project.Ipmi.Channel.ipmi_ssif"); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 381 | // Create the SSIF channel, listening on D-Bus and on the SSIF device |
Ed Tanous | 98f55c7 | 2024-02-14 16:23:30 -0800 | [diff] [blame] | 382 | ssifchannel = make_unique<SsifChannel>(io, bus, device, verbose); |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 383 | if (!ssifchannel->initOK()) |
| 384 | { |
| 385 | return EXIT_FAILURE; |
| 386 | } |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 387 | io->run(); |
Thang Q. Nguyen | 4300d21 | 2023-05-15 12:22:53 +0700 | [diff] [blame] | 388 | |
Patrick Williams | 8b050c9 | 2023-10-20 11:19:32 -0500 | [diff] [blame] | 389 | return 0; |
Dung Cao | faf6a6a | 2020-12-28 04:44:45 +0000 | [diff] [blame] | 390 | } |