Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | #include "MctpRequester.hpp" |
| 8 | |
| 9 | #include <linux/mctp.h> |
| 10 | #include <sys/socket.h> |
| 11 | |
| 12 | #include <OcpMctpVdm.hpp> |
| 13 | #include <boost/asio/buffer.hpp> |
| 14 | #include <boost/asio/error.hpp> |
| 15 | #include <boost/asio/generic/datagram_protocol.hpp> |
| 16 | #include <boost/asio/io_context.hpp> |
| 17 | #include <boost/asio/steady_timer.hpp> |
| 18 | #include <phosphor-logging/lg2.hpp> |
| 19 | |
| 20 | #include <cerrno> |
| 21 | #include <cstddef> |
| 22 | #include <cstdint> |
| 23 | #include <cstring> |
| 24 | #include <functional> |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 25 | #include <memory> |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 26 | #include <span> |
| 27 | #include <utility> |
| 28 | |
| 29 | using namespace std::literals; |
| 30 | |
| 31 | namespace mctp |
| 32 | { |
| 33 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 34 | Requester::Requester(boost::asio::io_context& ctx) : |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 35 | mctpSocket(ctx, boost::asio::generic::datagram_protocol{AF_MCTP, 0}), |
| 36 | expiryTimer(ctx) |
| 37 | {} |
| 38 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 39 | void Requester::processRecvMsg( |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 40 | uint8_t eid, const std::span<const uint8_t> reqMsg, |
| 41 | const std::span<uint8_t> respMsg, const boost::system::error_code& ec, |
| 42 | const size_t /*length*/) |
| 43 | { |
| 44 | expiryTimer.cancel(); |
| 45 | |
| 46 | if (ec) |
| 47 | { |
| 48 | lg2::error( |
| 49 | "MctpRequester failed to receive data from the MCTP socket - ErrorCode={EC}, Error={ER}.", |
| 50 | "EC", ec.value(), "ER", ec.message()); |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 51 | completionCallbacks[eid](EIO); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
| 55 | const auto* respAddr = |
| 56 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 57 | reinterpret_cast<const struct sockaddr_mctp*>(recvEndPoint.data()); |
| 58 | |
| 59 | if (respAddr->smctp_type != msgType) |
| 60 | { |
| 61 | lg2::error("MctpRequester: Message type mismatch"); |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 62 | completionCallbacks[eid](EPROTO); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 63 | return; |
| 64 | } |
| 65 | |
| 66 | uint8_t respEid = respAddr->smctp_addr.s_addr; |
| 67 | |
| 68 | if (respEid != eid) |
| 69 | { |
| 70 | lg2::error( |
| 71 | "MctpRequester: EID mismatch - expected={EID}, received={REID}", |
| 72 | "EID", eid, "REID", respEid); |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 73 | completionCallbacks[eid](EPROTO); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | |
| 77 | if (respMsg.size() > sizeof(ocp::accelerator_management::BindingPciVid)) |
| 78 | { |
| 79 | const auto* reqHdr = |
| 80 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 81 | reinterpret_cast<const ocp::accelerator_management::BindingPciVid*>( |
| 82 | reqMsg.data()); |
| 83 | |
| 84 | uint8_t reqInstanceId = reqHdr->instance_id & |
| 85 | ocp::accelerator_management::instanceIdBitMask; |
| 86 | const auto* respHdr = |
| 87 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 88 | reinterpret_cast<const ocp::accelerator_management::BindingPciVid*>( |
| 89 | respMsg.data()); |
| 90 | |
| 91 | uint8_t respInstanceId = respHdr->instance_id & |
| 92 | ocp::accelerator_management::instanceIdBitMask; |
| 93 | |
| 94 | if (reqInstanceId != respInstanceId) |
| 95 | { |
| 96 | lg2::error( |
| 97 | "MctpRequester: Instance ID mismatch - request={REQ}, response={RESP}", |
| 98 | "REQ", static_cast<int>(reqInstanceId), "RESP", |
| 99 | static_cast<int>(respInstanceId)); |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 100 | completionCallbacks[eid](EPROTO); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 101 | return; |
| 102 | } |
| 103 | } |
| 104 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 105 | completionCallbacks[eid](0); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 106 | } |
| 107 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 108 | void Requester::handleSendMsgCompletion( |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 109 | uint8_t eid, const std::span<const uint8_t> reqMsg, |
| 110 | std::span<uint8_t> respMsg, const boost::system::error_code& ec, |
| 111 | size_t /* length */) |
| 112 | { |
| 113 | if (ec) |
| 114 | { |
| 115 | lg2::error( |
| 116 | "MctpRequester failed to send data from the MCTP socket - ErrorCode={EC}, Error={ER}.", |
| 117 | "EC", ec.value(), "ER", ec.message()); |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 118 | completionCallbacks[eid](EIO); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
| 122 | expiryTimer.expires_after(2s); |
| 123 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 124 | expiryTimer.async_wait([this, eid](const boost::system::error_code& ec) { |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 125 | if (ec != boost::asio::error::operation_aborted) |
| 126 | { |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 127 | completionCallbacks[eid](ETIME); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 128 | } |
| 129 | }); |
| 130 | |
| 131 | mctpSocket.async_receive_from( |
| 132 | boost::asio::mutable_buffer(respMsg), recvEndPoint, |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 133 | std::bind_front(&Requester::processRecvMsg, this, eid, reqMsg, |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 134 | respMsg)); |
| 135 | } |
| 136 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 137 | void Requester::sendRecvMsg(uint8_t eid, const std::span<const uint8_t> reqMsg, |
| 138 | std::span<uint8_t> respMsg, |
| 139 | std::move_only_function<void(int)> callback) |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 140 | { |
| 141 | if (reqMsg.size() < sizeof(ocp::accelerator_management::BindingPciVid)) |
| 142 | { |
| 143 | lg2::error("MctpRequester: Message too small"); |
| 144 | callback(EPROTO); |
| 145 | return; |
| 146 | } |
| 147 | |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 148 | completionCallbacks[eid] = std::move(callback); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 149 | |
| 150 | struct sockaddr_mctp addr{}; |
| 151 | addr.smctp_family = AF_MCTP; |
| 152 | addr.smctp_addr.s_addr = eid; |
| 153 | addr.smctp_type = msgType; |
| 154 | addr.smctp_tag = MCTP_TAG_OWNER; |
| 155 | |
| 156 | sendEndPoint = {&addr, sizeof(addr)}; |
| 157 | |
| 158 | mctpSocket.async_send_to( |
| 159 | boost::asio::const_buffer(reqMsg), sendEndPoint, |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 160 | std::bind_front(&Requester::handleSendMsgCompletion, this, eid, reqMsg, |
| 161 | respMsg)); |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 162 | } |
Aditya Kurdunkar | 1213bcb | 2025-06-11 04:38:52 +0530 | [diff] [blame^] | 163 | |
| 164 | void QueuingRequester::sendRecvMsg(uint8_t eid, std::span<const uint8_t> reqMsg, |
| 165 | std::span<uint8_t> respMsg, |
| 166 | std::move_only_function<void(int)> callback) |
| 167 | { |
| 168 | auto reqCtx = |
| 169 | std::make_unique<RequestContext>(reqMsg, respMsg, std::move(callback)); |
| 170 | |
| 171 | // Add request to queue |
| 172 | auto& queue = requestContextQueues[eid]; |
| 173 | queue.push(std::move(reqCtx)); |
| 174 | |
| 175 | processQueue(eid); |
| 176 | } |
| 177 | |
| 178 | void QueuingRequester::processQueue(uint8_t eid) |
| 179 | { |
| 180 | auto& queue = requestContextQueues[eid]; |
| 181 | if (queue.empty() || activeRequestContexts.contains(eid)) |
| 182 | { |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | activeRequestContexts[eid] = std::move(queue.front()); |
| 187 | queue.pop(); |
| 188 | |
| 189 | const auto& reqCtx = activeRequestContexts[eid]; |
| 190 | |
| 191 | requester.sendRecvMsg( |
| 192 | eid, reqCtx->reqMsg, reqCtx->respMsg, [this, eid](int result) { |
| 193 | const auto& reqCtx = activeRequestContexts[eid]; |
| 194 | |
| 195 | reqCtx->callback(result); // Call the original callback |
| 196 | |
| 197 | activeRequestContexts.erase(eid); |
| 198 | |
| 199 | processQueue(eid); |
| 200 | }); |
| 201 | } |
| 202 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 203 | } // namespace mctp |