| 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 | #pragma once | 
|  | 8 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 9 | #include <MctpAsioEndpoint.hpp> | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 10 | #include <OcpMctpVdm.hpp> | 
|  | 11 | #include <boost/asio/generic/datagram_protocol.hpp> | 
|  | 12 | #include <boost/asio/io_context.hpp> | 
|  | 13 | #include <boost/asio/steady_timer.hpp> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 14 | #include <boost/circular_buffer.hpp> | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 15 | #include <boost/container/devector.hpp> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 16 | #include <boost/container/flat_map.hpp> | 
|  | 17 | #include <boost/container/small_vector.hpp> | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 18 |  | 
|  | 19 | #include <cstddef> | 
|  | 20 | #include <cstdint> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 21 | #include <expected> | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 22 | #include <functional> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 23 | #include <iostream> | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 24 | #include <memory> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 25 | #include <queue> | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 26 | #include <span> | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 27 | #include <system_error> | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 28 | #include <unordered_map> | 
|  | 29 | #include <utility> | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 30 |  | 
|  | 31 | namespace mctp | 
|  | 32 | { | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 33 | class MctpRequester | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 34 | { | 
|  | 35 | public: | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 36 | MctpRequester() = delete; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 37 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 38 | MctpRequester(const MctpRequester&) = delete; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 39 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 40 | MctpRequester(MctpRequester&&) = delete; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 41 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 42 | MctpRequester& operator=(const MctpRequester&) = delete; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 43 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 44 | MctpRequester& operator=(MctpRequester&&) = delete; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 45 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 46 | explicit MctpRequester(boost::asio::io_context& ctx); | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 47 |  | 
|  | 48 | void sendRecvMsg(uint8_t eid, std::span<const uint8_t> reqMsg, | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 49 | std::move_only_function<void(const std::error_code&, | 
|  | 50 | std::span<const uint8_t>)> | 
|  | 51 | callback); | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 52 |  | 
|  | 53 | private: | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 54 | using cb_t = std::move_only_function<void(const std::error_code&, | 
|  | 55 | std::span<const uint8_t>)>; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 56 |  | 
|  | 57 | static constexpr size_t maxMessageSize = 65536 + 256; | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 58 | static constexpr uint8_t msgType = ocp::accelerator_management::messageType; | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 59 |  | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 60 | struct RequestContext | 
|  | 61 | { | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 62 | std::vector<uint8_t> reqMsg; | 
|  | 63 | cb_t callback; | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 64 |  | 
|  | 65 | RequestContext(const RequestContext&) = delete; | 
|  | 66 | RequestContext& operator=(const RequestContext&) = delete; | 
|  | 67 |  | 
|  | 68 | RequestContext(RequestContext&&) = default; | 
|  | 69 | RequestContext& operator=(RequestContext&&) = default; | 
|  | 70 | ~RequestContext() = default; | 
|  | 71 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 72 | explicit RequestContext(std::span<const uint8_t> req, cb_t&& cb) : | 
|  | 73 | reqMsg(req.begin(), req.end()), callback(std::move(cb)) | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 74 | {} | 
|  | 75 | }; | 
|  | 76 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 77 | struct EidContext | 
|  | 78 | { | 
|  | 79 | boost::asio::steady_timer timer; | 
|  | 80 | uint8_t iid{}; | 
|  | 81 | boost::container::devector<RequestContext> queue; | 
|  | 82 | EidContext(boost::asio::io_context& io) : timer{io}, iid{0xFF} {} | 
|  | 83 | EidContext(EidContext&&) noexcept = default; | 
|  | 84 | EidContext& operator=(EidContext&&) noexcept = default; | 
|  | 85 | EidContext& operator=(const EidContext&) = delete; | 
|  | 86 | EidContext(const EidContext&) = delete; | 
|  | 87 | ~EidContext() = default; | 
|  | 88 | }; | 
|  | 89 |  | 
|  | 90 | std::optional<uint8_t> getNextIid(uint8_t eid); | 
|  | 91 | void startReceive(); | 
|  | 92 | void processRecvMsg(const boost::system::error_code& ec, size_t length); | 
|  | 93 | void handleSendMsgCompletion(uint8_t eid, | 
|  | 94 | const boost::system::error_code& ec, | 
|  | 95 | size_t length); | 
|  | 96 |  | 
|  | 97 | void handleResult(uint8_t eid, const std::error_code& ec, | 
|  | 98 | std::span<const uint8_t> buffer); | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 99 | void processQueue(uint8_t eid); | 
|  | 100 |  | 
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame^] | 101 | boost::asio::io_context& io; | 
|  | 102 | boost::asio::generic::datagram_protocol::endpoint sendEndPoint; | 
|  | 103 |  | 
|  | 104 | boost::asio::generic::datagram_protocol::socket mctpSocket; | 
|  | 105 | std::array<uint8_t, maxMessageSize> buffer{}; | 
|  | 106 | MctpAsioEndpoint recvEndPoint; | 
|  | 107 | std::unordered_map<uint8_t, EidContext> requestContextQueues; | 
| Aditya Kurdunkar | ed0af21 | 2025-06-11 04:38:52 +0530 | [diff] [blame] | 108 | }; | 
|  | 109 |  | 
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 110 | } // namespace mctp |