-
Notifications
You must be signed in to change notification settings - Fork 10
/
QueuePair.java
351 lines (308 loc) · 12.6 KB
/
QueuePair.java
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
* Copyright (C) 2018, IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ibm.jnvmf;
import com.ibm.disni.verbs.IbvMr;
import com.ibm.disni.verbs.IbvSendWR;
import com.ibm.disni.verbs.IbvWC;
import com.ibm.disni.verbs.SVCPostSend;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
// TODO check valid on public methods
public abstract class QueuePair implements Freeable {
private static final int POLL_CQ_BATCHSIZE = 32;
private boolean valid;
private final Controller controller;
private final NvmfRdmaEndpoint endpoint;
private final QueueId queueId;
private final short submissionQueueSize;
private final int additionalSgls;
private RdmaRecv[] rdmaReceives;
/* Command Identifier -> Response */
private final Response[] responseMap;
private KeyedNativeBufferPool commandBufferPool;
private final int maxCommandCapsuleSize;
private final int inCapsuleDataSize;
/* Command Identifier -> Command */
private final Command[] commandMap;
private final Queue<Short> freeCommandId;
private ThreadLocal<NvmfRdmaEndpoint.PollCq> pollCq;
FabricsConnectResponseCqe connect(long timeout, TimeUnit timeoutUnit) throws IOException {
NativeBuffer buffer = new NativeByteBuffer(
ByteBuffer.allocateDirect(RdmaCmRequestPrivateData.SIZE));
RdmaCmRequestPrivateData privateData = new RdmaCmRequestPrivateData(buffer);
privateData.setQueueId(queueId);
privateData.setRdmaQpReceiveQueueSize(submissionQueueSize);
privateData.setRdmaQpSendQueueSize((short) (submissionQueueSize - 1));
this.endpoint.getConnParam().setPrivate_data(buffer.getAddress());
this.endpoint.getConnParam().setPrivate_data_len((byte) RdmaCmRequestPrivateData.SIZE);
InetSocketAddress socketAddress = controller.getTransportId().getAddress();
try {
//FIXME: rejected requests are not handled by DiSNI
//FIXME: check for overflow
endpoint.connect(socketAddress, (int)TimeUnit.MILLISECONDS.convert(timeout, timeoutUnit));
} catch (Exception exception) {
throw new IOException(exception);
}
this.commandBufferPool = endpoint.getBufferPool(maxCommandCapsuleSize);
this.rdmaReceives = endpoint.getRdmaReceives();
pollCq = ThreadLocal.withInitial(() -> {
try {
return endpoint.new PollCq(POLL_CQ_BATCHSIZE);
} catch (IOException exception) {
throw new IllegalStateException(exception);
}
});
/* Send fabrics connect command */
ByteBuffer dataBuffer = ByteBuffer.allocateDirect(FabricsConnectCommandData.SIZE);
KeyedNativeBuffer registeredDataBuffer = registerMemory(dataBuffer);
FabricsConnectCommandData connectCommandData = new FabricsConnectCommandData(
registeredDataBuffer);
connectCommandData.setControllerId(controller.getControllerId());
connectCommandData.setHostNvmeQualifiedName(controller.getHostNvmeQualifiedName());
connectCommandData.setSubsystemNvmeQualifiedName(controller.getTransportId().getSubsystemNqn());
FabricsConnectCommand command = new FabricsConnectCommand(this);
Future<?> commandFuture = command.newCommandFuture();
FabricsConnectCommandCapsule connectCommand = command.getCommandCapsule();
connectCommand.setSglDescriptor(connectCommandData);
FabricsConnectCommandSqe connectCommandSqe = connectCommand.getSubmissionQueueEntry();
connectCommandSqe.setQueueId(queueId);
connectCommandSqe.setSubmissionQueueSize(submissionQueueSize);
ResponseFuture<FabricsConnectResponseCapsule> responseFuture = command.newResponseFuture();
command.execute(responseFuture);
FabricsConnectResponseCapsule responseCapsule;
try {
commandFuture.get();
responseCapsule = responseFuture.get();
} catch (InterruptedException exception) {
throw new IOException(exception);
} catch (ExecutionException exception) {
throw new IOException(exception);
}
connectCommand.free();
registeredDataBuffer.free();
FabricsConnectResponseCqe cqe = responseCapsule.getCompletionQueueEntry();
if (!cqe.getStatusCode().equals(GenericStatusCode.getInstance().SUCCESS)) {
throw new UnsuccessfulComandException(cqe);
} /* TODO invalid parameter exception */
return cqe;
}
QueuePair(Controller controller, QueueId queueId, short submissionQueueSize) throws IOException {
this(controller, queueId, submissionQueueSize, 0, 0, 0);
}
QueuePair(Controller controller, QueueId queueId, short submissionQueueSize,
int additionalSgls, int inCapsuleDataSize, int maxInlineSize) throws IOException {
this.controller = controller;
this.queueId = queueId;
this.endpoint = controller.getEndpointGroup().createEndpoint();
this.endpoint.setCqSize(2 * submissionQueueSize);
this.endpoint.setRqSize(submissionQueueSize);
this.endpoint.setSqSize(submissionQueueSize);
this.endpoint.setInlineDataSize(maxInlineSize);
this.submissionQueueSize = submissionQueueSize;
this.responseMap = new Response[submissionQueueSize];
if (additionalSgls < 0) {
throw new IllegalArgumentException("Additional SGLs negative");
}
if (inCapsuleDataSize < 0) {
throw new IllegalArgumentException("inCapsuleDataSize negative");
}
this.inCapsuleDataSize = inCapsuleDataSize;
if (maxInlineSize < 0) {
throw new IllegalArgumentException("maxInlineSize negative");
}
if (additionalSgls > 0) {
IdentifyControllerData identifyControllerData = controller.getIdentifyControllerData();
/* A value of 0 maximum SGL data block descriptors indicates no limit */
if ((additionalSgls + 1) > identifyControllerData.getMaximumSglDataBlockDescriptors()
&& identifyControllerData.getMaximumSglDataBlockDescriptors() != 0) {
throw new IllegalArgumentException("Controller only supports "
+ (identifyControllerData.getMaximumSglDataBlockDescriptors() - 1)
+ " additional SGLs not " + additionalSgls);
}
}
this.additionalSgls = additionalSgls;
int maxCommandCapsuleSize;
if (inCapsuleDataSize > 0) {
IdentifyControllerData identifyControllerData = controller.getIdentifyControllerData();
maxCommandCapsuleSize = CommandCapsule.computeCommandCapsuleSize(additionalSgls,
identifyControllerData.getInCapsuleDataOffset(), inCapsuleDataSize);
if (maxCommandCapsuleSize > identifyControllerData.getIoQueueCommandCapsuleSupportedSize()) {
throw new IllegalArgumentException("Command capsule size "
+ maxCommandCapsuleSize + " to large, max supported size is "
+ identifyControllerData.getIoQueueCommandCapsuleSupportedSize());
}
} else {
maxCommandCapsuleSize = CommandCapsule.computeCommandCapsuleSize(additionalSgls, 0, 0);
}
this.maxCommandCapsuleSize = maxCommandCapsuleSize;
this.commandMap = new Command[submissionQueueSize];
this.freeCommandId = new ArrayBlockingQueue<>(submissionQueueSize);
for (int i = 0; i < submissionQueueSize; i++) {
freeCommandId.add((short) i);
}
//FIXME: we don't want to introduce yet another argument to the constructor
// we need to change the way the buffer management works and get rid of
// additionalSgls and inCapsuleDataSize, then we can introduce a timeout arg
connect(10, TimeUnit.SECONDS);
this.valid = true;
}
/* we might want to move this to the controller later */
public KeyedNativeBuffer registerMemory(ByteBuffer buffer) throws IOException {
IbvMr mr = endpoint.registerMemory(buffer).execute().free().getMr();
return new RdmaByteBuffer(buffer, mr);
}
int getInlineDataSize() {
return endpoint.getInlineDataSize();
}
SVCPostSend newPostSend(List<IbvSendWR> sendWrList) throws IOException {
return endpoint.postSend(sendWrList);
}
KeyedNativeBuffer allocateCommandCapsule() throws IOException {
return commandBufferPool.allocate();
}
private short nextCommandIdentifier() throws IOException {
Short commandId;
boolean commandCompleted;
do {
commandId = freeCommandId.poll();
if (commandId == null) {
throw new IOException("submission queue full");
}
commandCompleted = commandMap[commandId] == null;
if (!commandCompleted) {
/*
* response has completed but command has not
* this should rarely happen
*/
boolean empty = freeCommandId.isEmpty();
freeCommandId.add(commandId);
if (empty) {
throw new IOException("submission queue full");
}
}
} while (!commandCompleted);
assert responseMap[commandId] == null;
return commandId;
}
final void post(Command command, SVCPostSend postSend, Response response) throws IOException {
command.getCallback().onStart();
response.getCallback().onStart();
short commandId = nextCommandIdentifier();
command.setCommandId(commandId);
responseMap[commandId] = response;
commandMap[commandId] = command;
postSend.getWrMod(0).setWr_id(commandId);
postSend.execute();
}
private final void handleSendWc(IbvWC wc) throws IOException {
int wrId = (int) wc.getWr_id();
Command command = commandMap[wrId];
if (command == null) {
throw new IOException("No command with CID " + wrId);
}
commandMap[wrId] = null;
if (wc.getStatus() != IbvWC.IbvWcStatus.IBV_WC_SUCCESS.ordinal()) {
command.getCallback().onFailure(RdmaException.fromInteger(wc.getOpcode(), wc.getStatus()));
} else {
command.getCallback().onComplete();
}
}
private final void handleReceiveWc(IbvWC wc) throws IOException {
if (wc.getStatus() != IbvWC.IbvWcStatus.IBV_WC_SUCCESS.ordinal()) {
/* we don't know to which response this wc responds to so we complete all */
for (Response response : responseMap) {
if (response != null) {
response.getCallback()
.onFailure(RdmaException.fromInteger(wc.getOpcode(), wc.getStatus()));
}
}
/* TODO should we free all CIDs or terminate the connection? */
} else {
RdmaRecv receive = rdmaReceives[(int) wc.getWr_id()];
KeyedNativeBuffer buffer = receive.getBuffer();
/* 1) extract CID */
short commandId = CompletionQueueEntry.getCommandIdentifier(buffer);
/* 2) remove response and update */
Response response = responseMap[commandId];
if (response == null) {
throw new IOException("No response with CID " + commandId);
}
responseMap[commandId] = null;
response.update(buffer);
/* 3) Resubmit and add to free receives */
receive.execute();
freeCommandId.add(commandId);
response.getCallback().onComplete();
}
}
public int poll() throws IOException {
NvmfRdmaEndpoint.PollCq pollCq = this.pollCq.get();
int polls = pollCq.execute().getPolls();
IbvWC[] wcs = pollCq.getWorkCompletions();
for (int i = 0; i < polls; i++) {
IbvWC wc = wcs[i];
int opcode = wc.getOpcode();
if (opcode == IbvWC.IbvWcOpcode.IBV_WC_SEND.getOpcode()) {
handleSendWc(wc);
} else if (opcode == IbvWC.IbvWcOpcode.IBV_WC_RECV.getOpcode()) {
handleReceiveWc(wc);
}
}
return polls;
}
public Controller getController() {
return controller;
}
public int getMaximumAdditionalSgls() {
return additionalSgls;
}
@Override
public void free() throws IOException {
if (isValid()) {
valid = false;
for (RdmaRecv receive : rdmaReceives) {
receive.free();
}
Arrays.fill(commandMap, null);
Arrays.fill(responseMap, null);
try {
endpoint.close();
} catch (InterruptedException exception) {
throw new IOException(exception);
}
}
}
@Override
public final boolean isValid() {
return valid;
}
public int getInCapsuleDataSize() {
return inCapsuleDataSize;
}
public short getSubmissionQueueSize() {
return submissionQueueSize;
}
}