-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDMAServer.cpp
190 lines (156 loc) · 6.67 KB
/
RDMAServer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// Created by jonas on 22.05.21.
//
#include <stdexcept>
#include <fmt/format.h>
#include <utility>
#include <netdb.h>
#include "RDMAServer.hpp"
#include "rdmaLib.hpp"
#include <sys/eventfd.h>
RDMAServer::RDMAServer(int port, std::size_t sendBufferSize, std::size_t recvBufferSize,
std::function<void()> onConnect,
std::function<void(Buffer<char> &&b)> onReceive) :
BufferSet(sendBufferSize, recvBufferSize),
connectCallback(std::move(onConnect)),
receiveCallback(std::move(onReceive)),
ec(rdma_create_event_channel()) {
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
rdma_create_id(ec, &listenerConn, nullptr, RDMA_PS_TCP);
rdma_bind_addr(listenerConn, (sockaddr *) &addr);
rdma_listen(listenerConn, 10); // Arbitrary backlog 10
fmt::print("Created listener rdma_cm_id {}\n", (void *) listenerConn);
uint16_t portRes = ntohs(rdma_get_src_port(listenerConn));
fmt::print("Listening on port {}\n", portRes);
eventLoop = std::make_unique<BreakableEventLoop>(ec, [this](const rdma_cm_event &e) { on_event(e); });
fmt::print("Server init done\n");
}
RDMAServer::~RDMAServer() {
fmt::print("Stopping RDMAServer\n");
if (clientConn != nullptr) {
fmt::print("Disconnecting client\n");
rdma_disconnect(clientConn);
fmt::print("Destroying client connection id\n");
rdma_destroy_id(clientConn);
}
fmt::print("Disconnecting listner\n");
rdma_disconnect(listenerConn);
fmt::print("Destroying listener ID\n");
rdma_destroy_id(listenerConn);
eventLoop.reset();
fmt::print("Destroying event channel\n");
rdma_destroy_event_channel(ec);
ibv_dealloc_pd(protectionDomain);
fmt::print("Stopped. Bye!\n");
}
bool RDMAServer::on_event(const rdma_cm_event &event) {
fmt::print("Received event {} from rdma_cm_id {}\n", rdma_event_str(event.event), (void *) event.id);
switch (event.event) {
case RDMA_CM_EVENT_CONNECT_REQUEST:
Expects(event.listen_id == listenerConn);
return on_connect_request(event.id);
case RDMA_CM_EVENT_ESTABLISHED:
Expects(event.id == clientConn);
return on_connection();
case RDMA_CM_EVENT_DISCONNECTED:
Expects(event.id == clientConn);
return on_disconnect();
default:
throw std::runtime_error{fmt::format("Unknown event: {} ({})", event.event, rdma_event_str(event.event))};
}
}
bool RDMAServer::on_connect_request(gsl::owner<rdma_cm_id *> newConn) {
fmt::print("Received connect request\n");
fmt::print("Building completion queue etc\n");
if (clientConn != nullptr) {
throw std::runtime_error{"Connection already exists"}; // TODO: multiple connections
} else {
clientConn = newConn;
fmt::print("Creating completion poller for IB context of rdma_cm_id {}\n", (void *) clientConn);
completionPoller = std::make_unique<CompletionPoller>(clientConn->verbs,
[this](const auto &wc) { on_completion(wc); });
}
fmt::print("Creating protection domain for connection {}", (void *) clientConn);
protectionDomain = ibv_alloc_pd(
clientConn->verbs); // TODO: Clear buffers associated with old PD when overwriting, as those cant be used with new client
clearBuffers();
fmt::print("Creating queue pair for rdma_cm_id {}\n", (void *) clientConn);
ibv_qp_init_attr qp_attr = build_qp_attr(completionPoller->cq);
if (rdma_create_qp(clientConn, protectionDomain, &qp_attr) != 0) {
throw std::runtime_error{fmt::format("Error creating queue pair: {}", strerror(errno))};
}
post_receives();
rdma_conn_param cm_params{};
fmt::print("Accepting connection {}\n", (void *) clientConn);
if (rdma_accept(clientConn, &cm_params) != 0) {
throw std::runtime_error{fmt::format("Error accepting connection: {}", strerror(errno))};
}
return false;
}
bool RDMAServer::on_disconnect() {
fmt::print("peer disconnected\n");
completionPoller.reset();
ibv_dealloc_pd(protectionDomain);
fmt::print("Destroy RDMA communication identifier\n");
rdma_destroy_id(clientConn); // TODO: Pass client connection via parameter
clientConn = nullptr;
return false;
}
bool RDMAServer::on_connection() {
connectCallback();
return false;
}
void RDMAServer::on_completion(const ibv_wc &wc) {
fmt::print("Received work completion with opcode {}\n", wc.opcode);
if (wc.status != IBV_WC_SUCCESS) {
throw std::runtime_error{fmt::format("on_completion: status is not success: {}", ibv_wc_status_str(wc.status))};
}
char *receiveBufferData = reinterpret_cast<char *>(wc.wr_id);
if (wc.opcode == IBV_WC_RECV) {
fmt::print("Received message, calling callback\n");
receiveCallback(findReceiveBuffer(receiveBufferData));
} else if (wc.opcode == IBV_WC_SEND) {
fmt::print("Send of wr {} completed successfully\n", wc.wr_id);
returnBuffer(findSendBuffer(receiveBufferData));
}
}
void RDMAServer::post_receives() {
fmt::print("Posting receives\n");
Buffer<char> b = getRecvBuffer().value_or(Buffer<char>(getSendSize(), protectionDomain));
ibv_sge sge{};
sge.addr = reinterpret_cast<uint64_t >(b.data());
sge.length = getRecvSize();
sge.lkey = b.getMR()->lkey;
ibv_recv_wr wr{};
wr.wr_id = reinterpret_cast<uintptr_t>(b.data()); // Identify the buffer by the data pointer once WR completed
wr.next = nullptr;
wr.sg_list = &sge;
wr.num_sge = 1;
ibv_recv_wr *bad_wr = nullptr;
fmt::print("Posting receive with size {} to rdma_cm_id {}\n", sge.length, (void *) clientConn);
ibv_post_recv(clientConn->qp, &wr, &bad_wr);;
markInFlightRecv(std::move(b));
}
void RDMAServer::send(Buffer<char> &&b) {
assert(b.size() == getSendSize());
ibv_sge sge{};
sge.addr = reinterpret_cast<uint64_t>(b.data());
sge.length = b.size();
sge.lkey = b.getMR()->lkey;
ibv_send_wr wr{};
wr.opcode = IBV_WR_SEND; // Send request that must match a corresponding receive request on the peer
wr.wr_id = reinterpret_cast<uint64_t>(b.data());
wr.sg_list = &sge;
wr.num_sge = 1;
wr.send_flags = IBV_SEND_SIGNALED; // We want complete notification for this send request
ibv_send_wr *bad_wr = nullptr;
fmt::print("Posting send request for result\n");
ibv_post_send(clientConn->qp, &wr, &bad_wr);
markInFlightSend(std::move(b));
}
Buffer<char> RDMAServer::getSendBuffer() {
// TODO: Handle multiple buffers for PDs
return BufferSet::getSendBuffer().value_or(Buffer<char>(getSendSize(), protectionDomain));
}