blob: 0fd468353a85e9af8c0c058e9cb0c462362e4595 [file] [log] [blame]
Harshit Aghera560e6af2025-04-21 20:04:56 +05301/*
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 Olberdingd0125c92025-10-08 14:37:19 -07009#include <MctpAsioEndpoint.hpp>
Harshit Aghera560e6af2025-04-21 20:04:56 +053010#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 Olberdingd0125c92025-10-08 14:37:19 -070014#include <boost/circular_buffer.hpp>
Aditya Kurdunkared0af212025-06-11 04:38:52 +053015#include <boost/container/devector.hpp>
Marc Olberdingd0125c92025-10-08 14:37:19 -070016#include <boost/container/flat_map.hpp>
17#include <boost/container/small_vector.hpp>
Harshit Aghera560e6af2025-04-21 20:04:56 +053018
19#include <cstddef>
20#include <cstdint>
Marc Olberdingd0125c92025-10-08 14:37:19 -070021#include <expected>
Harshit Aghera560e6af2025-04-21 20:04:56 +053022#include <functional>
Marc Olberdingd0125c92025-10-08 14:37:19 -070023#include <iostream>
Aditya Kurdunkared0af212025-06-11 04:38:52 +053024#include <memory>
Marc Olberdingd0125c92025-10-08 14:37:19 -070025#include <queue>
Harshit Aghera560e6af2025-04-21 20:04:56 +053026#include <span>
Marc Olberdingd0125c92025-10-08 14:37:19 -070027#include <system_error>
Aditya Kurdunkared0af212025-06-11 04:38:52 +053028#include <unordered_map>
29#include <utility>
Harshit Aghera560e6af2025-04-21 20:04:56 +053030
31namespace mctp
32{
Marc Olberdingd0125c92025-10-08 14:37:19 -070033class MctpRequester
Harshit Aghera560e6af2025-04-21 20:04:56 +053034{
35 public:
Marc Olberdingd0125c92025-10-08 14:37:19 -070036 MctpRequester() = delete;
Harshit Aghera560e6af2025-04-21 20:04:56 +053037
Marc Olberdingd0125c92025-10-08 14:37:19 -070038 MctpRequester(const MctpRequester&) = delete;
Harshit Aghera560e6af2025-04-21 20:04:56 +053039
Marc Olberdingd0125c92025-10-08 14:37:19 -070040 MctpRequester(MctpRequester&&) = delete;
Harshit Aghera560e6af2025-04-21 20:04:56 +053041
Marc Olberdingd0125c92025-10-08 14:37:19 -070042 MctpRequester& operator=(const MctpRequester&) = delete;
Harshit Aghera560e6af2025-04-21 20:04:56 +053043
Marc Olberdingd0125c92025-10-08 14:37:19 -070044 MctpRequester& operator=(MctpRequester&&) = delete;
Harshit Aghera560e6af2025-04-21 20:04:56 +053045
Marc Olberdingd0125c92025-10-08 14:37:19 -070046 explicit MctpRequester(boost::asio::io_context& ctx);
Harshit Aghera560e6af2025-04-21 20:04:56 +053047
48 void sendRecvMsg(uint8_t eid, std::span<const uint8_t> reqMsg,
Marc Olberdingd0125c92025-10-08 14:37:19 -070049 std::move_only_function<void(const std::error_code&,
50 std::span<const uint8_t>)>
51 callback);
Harshit Aghera560e6af2025-04-21 20:04:56 +053052
53 private:
Marc Olberdingd0125c92025-10-08 14:37:19 -070054 using cb_t = std::move_only_function<void(const std::error_code&,
55 std::span<const uint8_t>)>;
Harshit Aghera560e6af2025-04-21 20:04:56 +053056
57 static constexpr size_t maxMessageSize = 65536 + 256;
Harshit Aghera560e6af2025-04-21 20:04:56 +053058 static constexpr uint8_t msgType = ocp::accelerator_management::messageType;
Aditya Kurdunkared0af212025-06-11 04:38:52 +053059
Aditya Kurdunkared0af212025-06-11 04:38:52 +053060 struct RequestContext
61 {
Marc Olberdingd0125c92025-10-08 14:37:19 -070062 std::vector<uint8_t> reqMsg;
63 cb_t callback;
Aditya Kurdunkared0af212025-06-11 04:38:52 +053064
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 Olberdingd0125c92025-10-08 14:37:19 -070072 explicit RequestContext(std::span<const uint8_t> req, cb_t&& cb) :
73 reqMsg(req.begin(), req.end()), callback(std::move(cb))
Aditya Kurdunkared0af212025-06-11 04:38:52 +053074 {}
75 };
76
Marc Olberdingd0125c92025-10-08 14:37:19 -070077 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 Kurdunkared0af212025-06-11 04:38:52 +053099 void processQueue(uint8_t eid);
100
Marc Olberdingd0125c92025-10-08 14:37:19 -0700101 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 Kurdunkared0af212025-06-11 04:38:52 +0530108};
109
Harshit Aghera560e6af2025-04-21 20:04:56 +0530110} // namespace mctp