rogue
Loading...
Searching...
No Matches
Server.h
Go to the documentation of this file.
1
18#ifndef __ROGUE_PROTOCOLS_ROCEV2_SERVER_H__
19#define __ROGUE_PROTOCOLS_ROCEV2_SERVER_H__
20#include "rogue/Directives.h"
21
22#include <infiniband/verbs.h>
23#include <stdint.h>
24
25#include <atomic>
26#include <memory>
27#include <string>
28#include <thread>
29#include <vector>
30
36
37namespace rogue {
38namespace protocols {
39namespace rocev2 {
40
44 private:
45 // -----------------------------------------------------------------------
46 // ibverbs resources
47 // -----------------------------------------------------------------------
48 struct ibv_cq* cq_;
49 struct ibv_qp* qp_;
50 struct ibv_mr* mr_;
51 struct ibv_comp_channel* comp_channel_; // event-driven CQ notification channel
52
53 // Contiguous slab — subdivided into numBufs_ fixed-size slots.
54 // Registered with the HCA once; never copied from.
55 uint8_t* slab_;
56 uint32_t slabSize_;
57 uint32_t numBufs_;
58 uint32_t bufSize_; // == maxPayload_ (no GRH on RC)
59
60 // -----------------------------------------------------------------------
61 // Host-side RC parameters. All initialized to 0 in the member-init list
62 // so an early throw inside the ctor body cannot leave a reader of the
63 // getters (e.g. a getXxx() hooked into a LocalVariable's localGet in the
64 // failed-construction propagation path) observing indeterminate values.
65 // Real values are written inside the ctor body as each ibverbs stage
66 // completes successfully.
67 // -----------------------------------------------------------------------
68 uint32_t hostQpn_;
69 uint8_t hostGid_[16];
70 uint32_t hostRqPsn_;
71 uint32_t hostSqPsn_;
72 uint64_t mrAddr_;
73 uint32_t mrRkey_;
74
75 // FPGA GID — set by setFpgaGid() before completeConnection()
76 uint8_t fpgaGid_[16];
77
78 // -----------------------------------------------------------------------
79 // Receive thread
80 // -----------------------------------------------------------------------
81 std::thread* thread_;
82 std::atomic<bool> threadEn_;
83
84 // Self-pipe used to break the receive thread out of its blocking select()
85 // for prompt, sleepless shutdown. stop() writes one byte to wakeFd_[1];
86 // runThread() selects on wakeFd_[0]. Both ends are non-blocking.
87 int wakeFd_[2];
88
89 // Intentional shadow: both Core and stream::Slave expose a `log_` member,
90 // so any unqualified `log_` from inside Server would otherwise be
91 // ambiguous. Declaring our own `log_` here consolidates both names onto
92 // Server's "rocev2.Server" logger (assigned in the Server ctor) and
93 // silences the multiple-inheritance ambiguity.
94 std::shared_ptr<rogue::Logging> log_;
95
96 // RX counters
97 std::atomic<uint64_t> frameCount_;
98 std::atomic<uint64_t> byteCount_;
99
100 void postRecvWr(uint32_t slot);
101 void runThread(std::weak_ptr<int> lockPtr);
102
103 // Process one receive completion (decode immediate, wrap zero-copy buffer,
104 // forward downstream). On IBV_WC_WR_FLUSH_ERR it clears threadEn_ so the
105 // poll/drain loop in runThread() exits.
106 void processCompletion(struct ibv_wc& wc);
107
108 // Idempotent helper that releases every ibverbs / heap resource owned by
109 // Server in reverse allocation order. Called by stop() and from the
110 // failed-construction path in the constructor.
111 void cleanupResources();
112
113 protected:
114 // -----------------------------------------------------------------------
115 // Zero-copy hook — called by Buffer::~Buffer() when the last downstream
116 // reference to a frame is released. Re-posts the slab slot to the QP.
117 // The slot index is encoded in the lower 24 bits of meta.
118 // -----------------------------------------------------------------------
119 void retBuffer(uint8_t* data, uint32_t meta, uint32_t rawSize) override;
120
121 public:
122 static std::shared_ptr<rogue::protocols::rocev2::Server> create(
123 const std::string& deviceName,
124 uint8_t ibPort,
125 uint8_t gidIndex,
126 uint32_t maxPayload = DefaultMaxPayload,
127 uint32_t rxQueueDepth = DefaultRxQueueDepth);
128
129 Server(const std::string& deviceName,
130 uint8_t ibPort,
131 uint8_t gidIndex,
132 uint32_t maxPayload,
133 uint32_t rxQueueDepth);
134
135 ~Server();
136 void stop();
137
138 void setFpgaGid(const std::string& gidBytes);
139
140 // minRnrTimer: IB spec RNR timer code embedded in RNR NAK packets.
141 // 1=0.01ms 14=1ms 18=4ms 22=16ms 31=491ms
142 void completeConnection(uint32_t fpgaQpn,
143 uint32_t fpgaRqPsn,
144 uint32_t pmtu = 5,
145 uint32_t minRnrTimer = 1);
146
147 uint32_t getQpn() const { return hostQpn_; }
148 std::string getGid() const;
149 uint32_t getRqPsn() const { return hostRqPsn_; }
150 uint32_t getSqPsn() const { return hostSqPsn_; }
151 uint64_t getMrAddr() const { return mrAddr_; }
152 uint32_t getMrRkey() const { return mrRkey_; }
153 uint64_t getFrameCount() const { return frameCount_.load(); }
154 uint64_t getByteCount() const { return byteCount_.load(); }
155
157
158 static void setup_python();
159};
160
161typedef std::shared_ptr<rogue::protocols::rocev2::Server> ServerPtr;
162
163} // namespace rocev2
164} // namespace protocols
165} // namespace rogue
166
167#endif
Stream master endpoint.
Definition Master.h:65
static std::shared_ptr< rogue::interfaces::stream::Master > create()
Creates a stream master.
Definition Master.cpp:40
Stream slave endpoint and default frame pool.
Definition Slave.h:72
uint32_t maxPayload() const
Definition Core.h:65
uint64_t getFrameCount() const
Definition Server.h:153
void retBuffer(uint8_t *data, uint32_t meta, uint32_t rawSize) override
Definition Server.cpp:497
void setFpgaGid(const std::string &gidBytes)
Definition Server.cpp:326
uint64_t getMrAddr() const
Definition Server.h:151
uint32_t getMrRkey() const
Definition Server.h:152
uint32_t getRqPsn() const
Definition Server.h:149
uint32_t getSqPsn() const
Definition Server.h:150
std::string getGid() const
Definition Server.cpp:441
void completeConnection(uint32_t fpgaQpn, uint32_t fpgaRqPsn, uint32_t pmtu=5, uint32_t minRnrTimer=1)
Definition Server.cpp:338
void acceptFrame(rogue::interfaces::stream::FramePtr frame) override
Definition Server.cpp:724
uint64_t getByteCount() const
Definition Server.h:154
std::shared_ptr< rogue::interfaces::stream::Frame > FramePtr
Shared pointer alias for Frame.
Definition Frame.h:549
std::shared_ptr< rogue::protocols::rocev2::Server > ServerPtr
Definition Server.h:161
static const uint32_t DefaultMaxPayload
Definition Core.h:40
static const uint32_t DefaultRxQueueDepth
Definition Core.h:35