-
Notifications
You must be signed in to change notification settings - Fork 67
/
SendRecvClient.java
245 lines (200 loc) · 7.54 KB
/
SendRecvClient.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
/*
* DiSNI: Direct Storage and Networking Interface
*
* Author: Patrick Stuedi <stu@zurich.ibm.com>
*
* Copyright (C) 2016-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.disni.examples;
import com.ibm.disni.CmdLineCommon;
import com.ibm.disni.RdmaActiveEndpoint;
import com.ibm.disni.RdmaActiveEndpointGroup;
import com.ibm.disni.RdmaEndpointFactory;
import com.ibm.disni.verbs.*;
import org.apache.commons.cli.ParseException;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.concurrent.ArrayBlockingQueue;
public class SendRecvClient implements RdmaEndpointFactory<SendRecvClient.CustomClientEndpoint> {
RdmaActiveEndpointGroup<SendRecvClient.CustomClientEndpoint> endpointGroup;
private String host;
private int port;
public SendRecvClient.CustomClientEndpoint createEndpoint(RdmaCmId idPriv, boolean serverSide) throws IOException {
return new CustomClientEndpoint(endpointGroup, idPriv, serverSide, 100);
}
public void run() throws Exception {
//create a EndpointGroup. The RdmaActiveEndpointGroup contains CQ processing and delivers CQ event to the endpoint.dispatchCqEvent() method.
endpointGroup = new RdmaActiveEndpointGroup<SendRecvClient.CustomClientEndpoint>(1000, false, 128, 4, 128);
endpointGroup.init(this);
//we have passed our own endpoint factory to the group, therefore new endpoints will be of type CustomClientEndpoint
//let's create a new client endpoint
SendRecvClient.CustomClientEndpoint endpoint = endpointGroup.createEndpoint();
//connect to the server
InetAddress ipAddress = InetAddress.getByName(host);
InetSocketAddress address = new InetSocketAddress(ipAddress, port);
endpoint.connect(address, 1000);
System.out.println("SimpleClient::client channel set up ");
//in our custom endpoints we have prepared (memory registration and work request creation) some memory
//buffers beforehand.
//let's send one of those buffers out using a send operation
ByteBuffer sendBuf = endpoint.getSendBuf();
sendBuf.asCharBuffer().put("Hello from the client");
sendBuf.clear();
SVCPostSend postSend = endpoint.postSend(endpoint.getWrList_send());
postSend.getWrMod(0).setWr_id(4444);
postSend.execute().free();
//in our custom endpoints we make sure CQ events get stored in a queue, we now query that queue for new CQ events.
//in this case a new CQ event means we have sent data, i.e., the message has been sent to the server
IbvWC wc = endpoint.getWcEvents().take();
System.out.println("SimpleClient::message sent, wr_id " + wc.getWr_id());
//in this case a new CQ event means we have received data
endpoint.getWcEvents().take();
System.out.println("SimpleClient::message received");
//the response should be received in this buffer, let's print it
ByteBuffer recvBuf = endpoint.getRecvBuf();
recvBuf.clear();
System.out.println("Message from the server: " + recvBuf.asCharBuffer().toString());
//close everything
endpoint.close();
System.out.println("endpoint closed");
endpointGroup.close();
System.out.println("group closed");
// System.exit(0);
}
public void launch(String[] args) throws Exception {
CmdLineCommon cmdLine = new CmdLineCommon("SendRecvClient");
try {
cmdLine.parse(args);
} catch (ParseException e) {
cmdLine.printHelp();
System.exit(-1);
}
host = cmdLine.getIp();
port = cmdLine.getPort();
this.run();
}
public static void main(String[] args) throws Exception {
SendRecvClient simpleClient = new SendRecvClient();
simpleClient.launch(args);
}
public static class CustomClientEndpoint extends RdmaActiveEndpoint {
private ByteBuffer buffers[];
private IbvMr mrlist[];
private int buffercount = 3;
private ByteBuffer dataBuf;
private IbvMr dataMr;
private ByteBuffer sendBuf;
private IbvMr sendMr;
private ByteBuffer recvBuf;
private IbvMr recvMr;
private LinkedList<IbvSendWR> wrList_send;
private IbvSge sgeSend;
private LinkedList<IbvSge> sgeList;
private IbvSendWR sendWR;
private LinkedList<IbvRecvWR> wrList_recv;
private IbvSge sgeRecv;
private LinkedList<IbvSge> sgeListRecv;
private IbvRecvWR recvWR;
private ArrayBlockingQueue<IbvWC> wcEvents;
public CustomClientEndpoint(RdmaActiveEndpointGroup<CustomClientEndpoint> endpointGroup,
RdmaCmId idPriv, boolean serverSide, int buffersize) throws IOException {
super(endpointGroup, idPriv, serverSide);
this.buffercount = 3;
buffers = new ByteBuffer[buffercount];
this.mrlist = new IbvMr[buffercount];
for (int i = 0; i < buffercount; i++){
buffers[i] = ByteBuffer.allocateDirect(buffersize);
}
this.wrList_send = new LinkedList<IbvSendWR>();
this.sgeSend = new IbvSge();
this.sgeList = new LinkedList<IbvSge>();
this.sendWR = new IbvSendWR();
this.wrList_recv = new LinkedList<IbvRecvWR>();
this.sgeRecv = new IbvSge();
this.sgeListRecv = new LinkedList<IbvSge>();
this.recvWR = new IbvRecvWR();
this.wcEvents = new ArrayBlockingQueue<IbvWC>(10);
}
//important: we override the init method to prepare some buffers (memory registration, post recv, etc).
//This guarantees that at least one recv operation will be posted at the moment this endpoint is connected.
public void init() throws IOException{
super.init();
for (int i = 0; i < buffercount; i++){
mrlist[i] = registerMemory(buffers[i]).execute().free().getMr();
}
this.dataBuf = buffers[0];
this.dataMr = mrlist[0];
this.sendBuf = buffers[1];
this.sendMr = mrlist[1];
this.recvBuf = buffers[2];
this.recvMr = mrlist[2];
dataBuf.clear();
sendBuf.putLong(dataMr.getAddr());
sendBuf.putInt(dataMr.getLength());
sendBuf.putInt(dataMr.getLkey());
sendBuf.clear();
sgeSend.setAddr(sendMr.getAddr());
sgeSend.setLength(sendMr.getLength());
sgeSend.setLkey(sendMr.getLkey());
sgeList.add(sgeSend);
sendWR.setWr_id(2000);
sendWR.setSg_list(sgeList);
sendWR.setOpcode(IbvSendWR.IBV_WR_SEND);
sendWR.setSend_flags(IbvSendWR.IBV_SEND_SIGNALED);
wrList_send.add(sendWR);
sgeRecv.setAddr(recvMr.getAddr());
sgeRecv.setLength(recvMr.getLength());
int lkey = recvMr.getLkey();
sgeRecv.setLkey(lkey);
sgeListRecv.add(sgeRecv);
recvWR.setSg_list(sgeListRecv);
recvWR.setWr_id(2001);
wrList_recv.add(recvWR);
System.out.println("SimpleClient::initiated recv");
this.postRecv(wrList_recv).execute().free();
}
public void dispatchCqEvent(IbvWC wc) throws IOException {
wcEvents.add(wc);
}
public ArrayBlockingQueue<IbvWC> getWcEvents() {
return wcEvents;
}
public LinkedList<IbvSendWR> getWrList_send() {
return wrList_send;
}
public LinkedList<IbvRecvWR> getWrList_recv() {
return wrList_recv;
}
public ByteBuffer getDataBuf() {
return dataBuf;
}
public ByteBuffer getSendBuf() {
return sendBuf;
}
public ByteBuffer getRecvBuf() {
return recvBuf;
}
public IbvSendWR getSendWR() {
return sendWR;
}
public IbvRecvWR getRecvWR() {
return recvWR;
}
}
}