author | bpb |
Wed, 06 Feb 2019 12:48:01 -0800 | |
branch | rsocket-branch |
changeset 57156 | 81e4a12fd1a4 |
parent 57115 | 512e7cc6ccce |
child 57160 | c502c299d41e |
permissions | -rw-r--r-- |
57115 | 1 |
/* |
57156
81e4a12fd1a4
rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
bpb
parents:
57115
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
57115 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.internal.net.rdma; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
30 |
import java.net.InetAddress; |
|
31 |
import java.net.InetSocketAddress; |
|
32 |
import java.net.ProtocolFamily; |
|
33 |
import java.net.ServerSocket; |
|
34 |
import java.net.SocketAddress; |
|
35 |
import java.net.SocketOption; |
|
36 |
import java.net.StandardSocketOptions; |
|
37 |
import java.net.StandardProtocolFamily; |
|
38 |
import java.nio.channels.AlreadyBoundException; |
|
39 |
import java.nio.channels.AsynchronousCloseException; |
|
40 |
import java.nio.channels.ClosedChannelException; |
|
41 |
import java.nio.channels.NotYetBoundException; |
|
42 |
import java.nio.channels.SelectionKey; |
|
43 |
import java.nio.channels.ServerSocketChannel; |
|
44 |
import java.nio.channels.SocketChannel; |
|
45 |
import java.nio.channels.UnsupportedAddressTypeException; |
|
46 |
import java.nio.channels.spi.SelectorProvider; |
|
47 |
import java.util.Collections; |
|
48 |
import java.util.HashSet; |
|
49 |
import java.util.Objects; |
|
50 |
import java.util.Set; |
|
51 |
import java.util.concurrent.locks.ReentrantLock; |
|
52 |
import sun.nio.ch.IOStatus; |
|
53 |
import sun.nio.ch.IOUtil; |
|
54 |
import sun.nio.ch.NativeThread; |
|
55 |
import sun.nio.ch.Net; |
|
56 |
import sun.nio.ch.SelChImpl; |
|
57 |
import sun.nio.ch.SelectionKeyImpl; |
|
58 |
import sun.net.ext.RdmaSocketOptions; |
|
59 |
import static java.net.StandardProtocolFamily.INET; |
|
60 |
import static java.net.StandardProtocolFamily.INET6; |
|
61 |
||
62 |
public class RdmaServerSocketChannelImpl |
|
63 |
extends ServerSocketChannel |
|
64 |
implements SelChImpl |
|
65 |
{ |
|
66 |
//The protocol family of the socket |
|
67 |
private final ProtocolFamily family; |
|
68 |
||
69 |
private static RdmaSocketDispatcher nd; |
|
70 |
||
71 |
private final FileDescriptor fd; |
|
72 |
private final int fdVal; |
|
73 |
||
74 |
private final ReentrantLock acceptLock = new ReentrantLock(); |
|
75 |
||
76 |
private final Object stateLock = new Object(); |
|
77 |
||
78 |
private static final int ST_INUSE = 0; |
|
79 |
private static final int ST_CLOSING = 1; |
|
80 |
private static final int ST_KILLPENDING = 2; |
|
81 |
private static final int ST_KILLED = 3; |
|
82 |
private int state; |
|
83 |
||
84 |
private long thread; |
|
85 |
||
86 |
private InetSocketAddress localAddress; |
|
87 |
||
88 |
private boolean isReuseAddress; |
|
89 |
||
90 |
private ServerSocket socket; |
|
91 |
||
92 |
private static final UnsupportedOperationException unsupported; |
|
93 |
||
94 |
private static final SelectorProvider checkSupported(SelectorProvider sp) { |
|
95 |
if (unsupported != null) |
|
96 |
throw new UnsupportedOperationException(unsupported.getMessage(), unsupported); |
|
97 |
else |
|
98 |
return sp; |
|
99 |
} |
|
100 |
||
101 |
RdmaServerSocketChannelImpl(SelectorProvider sp, ProtocolFamily family) |
|
102 |
throws IOException { |
|
103 |
super(checkSupported(sp)); |
|
104 |
Objects.requireNonNull(family, "'family' is null"); |
|
105 |
if (!(family == INET || family == INET6)) { |
|
106 |
throw new UnsupportedOperationException( |
|
107 |
"Protocol family not supported"); |
|
108 |
} |
|
109 |
if (family == INET6) { |
|
110 |
if (!Net.isIPv6Available()) { |
|
111 |
throw new UnsupportedOperationException( |
|
112 |
"IPv6 not available"); |
|
113 |
} |
|
114 |
} |
|
115 |
this.family = family; |
|
116 |
this.fd = RdmaNet.serverSocket(family, true); |
|
117 |
this.fdVal = IOUtil.fdVal(fd); |
|
118 |
} |
|
119 |
||
120 |
private void ensureOpen() throws ClosedChannelException { |
|
121 |
if (!isOpen()) |
|
122 |
throw new ClosedChannelException(); |
|
123 |
} |
|
124 |
||
125 |
@Override |
|
126 |
public ServerSocket socket() { |
|
127 |
synchronized (stateLock) { |
|
128 |
if (socket == null) |
|
129 |
socket = RdmaServerSocketAdaptor.create(this); |
|
130 |
return socket; |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
@Override |
|
135 |
public SocketAddress getLocalAddress() throws IOException { |
|
136 |
synchronized (stateLock) { |
|
137 |
ensureOpen(); |
|
138 |
return (localAddress == null) |
|
139 |
? null |
|
140 |
: Net.getRevealedLocalAddress(localAddress); |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public <T> ServerSocketChannel setOption(SocketOption<T> name, T value) |
|
146 |
throws IOException |
|
147 |
{ |
|
148 |
Objects.requireNonNull(name); |
|
149 |
if (!supportedOptions().contains(name)) |
|
150 |
throw new UnsupportedOperationException("'" + name |
|
151 |
+ "' not supported"); |
|
152 |
synchronized (stateLock) { |
|
153 |
ensureOpen(); |
|
154 |
if (isBound() && (name == StandardSocketOptions.SO_REUSEADDR)) |
|
155 |
throw new UnsupportedOperationException( |
|
156 |
"RDMA server socket channel cannot set the socket option " |
|
157 |
+ name.toString() + " after bind."); |
|
158 |
||
159 |
RdmaNet.setSocketOption(fd, RdmaNet.UNSPEC, name, value); |
|
160 |
return this; |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
@Override |
|
165 |
@SuppressWarnings("unchecked") |
|
166 |
public <T> T getOption(SocketOption<T> name) |
|
167 |
throws IOException |
|
168 |
{ |
|
169 |
Objects.requireNonNull(name); |
|
170 |
if (!supportedOptions().contains(name)) |
|
171 |
throw new UnsupportedOperationException("'" + name |
|
172 |
+ "' not supported"); |
|
173 |
||
174 |
synchronized (stateLock) { |
|
175 |
ensureOpen(); |
|
176 |
return (T) RdmaNet.getSocketOption(fd, RdmaNet.UNSPEC, name); |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
private static class DefaultOptionsHolder { |
|
181 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
|
182 |
||
183 |
private static Set<SocketOption<?>> defaultOptions() { |
|
184 |
HashSet<SocketOption<?>> set = new HashSet<>(2); |
|
185 |
set.add(StandardSocketOptions.SO_RCVBUF); |
|
186 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
|
187 |
if (RdmaNet.isRdmaAvailable()) { |
|
188 |
RdmaSocketOptions rdmaOptions = |
|
189 |
RdmaSocketOptions.getInstance(); |
|
190 |
set.addAll(rdmaOptions.options()); |
|
191 |
} |
|
192 |
return Collections.unmodifiableSet(set); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
public final Set<SocketOption<?>> supportedOptions() { |
|
197 |
return DefaultOptionsHolder.defaultOptions; |
|
198 |
} |
|
199 |
||
200 |
private final InetSocketAddress anyLocalAddress() throws IOException { |
|
201 |
if (family == INET) |
|
202 |
return new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0); |
|
203 |
else if (family == INET6) |
|
204 |
return new InetSocketAddress(InetAddress.getByName("::"), 0); |
|
205 |
else |
|
206 |
throw new UnsupportedAddressTypeException(); |
|
207 |
} |
|
208 |
||
209 |
@Override |
|
210 |
public ServerSocketChannel bind(SocketAddress local, int backlog) |
|
211 |
throws IOException { |
|
212 |
synchronized (stateLock) { |
|
213 |
ensureOpen(); |
|
214 |
if (localAddress != null) |
|
215 |
throw new AlreadyBoundException(); |
|
216 |
InetSocketAddress isa = (local == null) |
|
217 |
? anyLocalAddress() |
|
218 |
: RdmaNet.checkAddress(local, family); |
|
219 |
SecurityManager sm = System.getSecurityManager(); |
|
220 |
if (sm != null) |
|
221 |
sm.checkListen(isa.getPort()); |
|
222 |
RdmaNet.bind(family, fd, isa.getAddress(), isa.getPort()); |
|
223 |
RdmaNet.listen(fd, backlog < 1 ? 50 : backlog); |
|
224 |
localAddress = RdmaNet.localAddress(fd); |
|
225 |
} |
|
226 |
return this; |
|
227 |
} |
|
228 |
||
229 |
private void begin(boolean blocking) throws ClosedChannelException { |
|
230 |
if (blocking) |
|
231 |
begin(); |
|
232 |
synchronized (stateLock) { |
|
233 |
ensureOpen(); |
|
234 |
if (localAddress == null) |
|
235 |
throw new NotYetBoundException(); |
|
236 |
if (blocking) |
|
237 |
thread = NativeThread.current(); |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
private void end(boolean blocking, boolean completed) |
|
242 |
throws AsynchronousCloseException { |
|
243 |
if (blocking) { |
|
244 |
synchronized (stateLock) { |
|
245 |
thread = 0; |
|
246 |
if (state == ST_CLOSING) { |
|
247 |
stateLock.notifyAll(); |
|
248 |
} |
|
249 |
} |
|
250 |
end(completed); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
@Override |
|
255 |
public SocketChannel accept() throws IOException { |
|
256 |
acceptLock.lock(); |
|
257 |
try { |
|
258 |
int n = 0; |
|
259 |
FileDescriptor newfd = new FileDescriptor(); |
|
260 |
InetSocketAddress[] isaa = new InetSocketAddress[1]; |
|
261 |
||
262 |
boolean blocking = isBlocking(); |
|
263 |
try { |
|
264 |
begin(blocking); |
|
265 |
do { |
|
266 |
if (blocking) { |
|
267 |
do { |
|
268 |
n = checkAccept(this.fd); |
|
269 |
} while ((n == 0 || n == IOStatus.INTERRUPTED) |
|
270 |
&& isOpen()); |
|
271 |
} |
|
272 |
n = accept(this.fd, newfd, isaa); |
|
273 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
|
274 |
} finally { |
|
275 |
end(blocking, n > 0); |
|
276 |
assert IOStatus.check(n); |
|
277 |
} |
|
278 |
||
279 |
if (n < 1) |
|
280 |
return null; |
|
281 |
||
282 |
// newly accepted socket is initially in blocking mode |
|
283 |
RdmaNet.configureBlocking(newfd, true); |
|
284 |
||
285 |
InetSocketAddress isa = isaa[0]; |
|
286 |
SocketChannel sc = new RdmaSocketChannelImpl(provider(), |
|
287 |
newfd, isa); |
|
288 |
||
289 |
// check permitted to accept connections from the remote address |
|
290 |
SecurityManager sm = System.getSecurityManager(); |
|
291 |
if (sm != null) { |
|
292 |
try { |
|
293 |
sm.checkAccept(isa.getAddress().getHostAddress(), |
|
294 |
isa.getPort()); |
|
295 |
} catch (SecurityException x) { |
|
296 |
sc.close(); |
|
297 |
throw x; |
|
298 |
} |
|
299 |
} |
|
300 |
return sc; |
|
301 |
||
302 |
} finally { |
|
303 |
acceptLock.unlock(); |
|
304 |
} |
|
305 |
} |
|
306 |
||
307 |
@Override |
|
308 |
protected void implConfigureBlocking(boolean block) throws IOException { |
|
309 |
acceptLock.lock(); |
|
310 |
try { |
|
311 |
synchronized (stateLock) { |
|
312 |
ensureOpen(); |
|
313 |
RdmaNet.configureBlocking(fd, block); |
|
314 |
} |
|
315 |
} finally { |
|
316 |
acceptLock.unlock(); |
|
317 |
} |
|
318 |
} |
|
319 |
||
320 |
@Override |
|
321 |
protected void implCloseSelectableChannel() throws IOException { |
|
322 |
assert !isOpen(); |
|
323 |
||
324 |
boolean interrupted = false; |
|
325 |
boolean blocking; |
|
326 |
||
327 |
// set state to ST_CLOSING |
|
328 |
synchronized (stateLock) { |
|
329 |
assert state < ST_CLOSING; |
|
330 |
state = ST_CLOSING; |
|
331 |
blocking = isBlocking(); |
|
332 |
} |
|
333 |
||
334 |
// wait for any outstanding accept to complete |
|
335 |
if (blocking) { |
|
336 |
synchronized (stateLock) { |
|
337 |
assert state == ST_CLOSING; |
|
338 |
long th = thread; |
|
339 |
if (th != 0) { |
|
340 |
nd.preClose(fd); |
|
341 |
NativeThread.signal(th); |
|
342 |
||
343 |
// wait for accept operation to end |
|
344 |
while (thread != 0) { |
|
345 |
try { |
|
346 |
stateLock.wait(); |
|
347 |
} catch (InterruptedException e) { |
|
348 |
interrupted = true; |
|
349 |
} |
|
350 |
} |
|
351 |
} |
|
352 |
} |
|
353 |
} else { |
|
354 |
// non-blocking mode: wait for accept to complete |
|
355 |
acceptLock.lock(); |
|
356 |
acceptLock.unlock(); |
|
357 |
} |
|
358 |
||
359 |
// set state to ST_KILLPENDING |
|
360 |
synchronized (stateLock) { |
|
361 |
assert state == ST_CLOSING; |
|
362 |
state = ST_KILLPENDING; |
|
363 |
} |
|
364 |
||
365 |
// close socket if not registered with Selector |
|
366 |
if (!isRegistered()) |
|
367 |
kill(); |
|
368 |
||
369 |
// restore interrupt status |
|
370 |
if (interrupted) |
|
371 |
Thread.currentThread().interrupt(); |
|
372 |
} |
|
373 |
||
374 |
@Override |
|
375 |
public void kill() throws IOException { |
|
376 |
synchronized (stateLock) { |
|
377 |
if (state == ST_KILLPENDING) { |
|
378 |
state = ST_KILLED; |
|
379 |
nd.close(fd); |
|
380 |
} |
|
381 |
} |
|
382 |
} |
|
383 |
||
384 |
boolean isBound() { |
|
385 |
synchronized (stateLock) { |
|
386 |
return localAddress != null; |
|
387 |
} |
|
388 |
} |
|
389 |
||
390 |
InetSocketAddress localAddress() { |
|
391 |
synchronized (stateLock) { |
|
392 |
return localAddress; |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Poll this channel's socket for a new connection up to the given timeout. |
|
398 |
* @return {@code true} if there is a connection to accept |
|
399 |
*/ |
|
400 |
boolean pollAccept(long timeout) throws IOException { |
|
401 |
assert Thread.holdsLock(blockingLock()) && isBlocking(); |
|
402 |
acceptLock.lock(); |
|
403 |
try { |
|
404 |
boolean polled = false; |
|
405 |
try { |
|
406 |
begin(true); |
|
407 |
int events = RdmaNet.poll(fd, Net.POLLIN, timeout); |
|
408 |
polled = (events != 0); |
|
409 |
} finally { |
|
410 |
end(true, polled); |
|
411 |
} |
|
412 |
return polled; |
|
413 |
} finally { |
|
414 |
acceptLock.unlock(); |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
public boolean translateReadyOps(int ops, int initialOps, |
|
419 |
SelectionKeyImpl ski) { |
|
420 |
int intOps = ski.nioInterestOps(); |
|
421 |
int oldOps = ski.nioReadyOps(); |
|
422 |
int newOps = initialOps; |
|
423 |
||
424 |
if ((ops & Net.POLLNVAL) != 0) { |
|
425 |
return false; |
|
426 |
} |
|
427 |
||
428 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
|
429 |
newOps = intOps; |
|
430 |
ski.nioReadyOps(newOps); |
|
431 |
return (newOps & ~oldOps) != 0; |
|
432 |
} |
|
433 |
||
434 |
if (((ops & Net.POLLIN) != 0) && |
|
435 |
((intOps & SelectionKey.OP_ACCEPT) != 0)) |
|
436 |
newOps |= SelectionKey.OP_ACCEPT; |
|
437 |
||
438 |
ski.nioReadyOps(newOps); |
|
439 |
return (newOps & ~oldOps) != 0; |
|
440 |
} |
|
441 |
||
442 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) { |
|
443 |
return translateReadyOps(ops, ski.nioReadyOps(), ski); |
|
444 |
} |
|
445 |
||
446 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) { |
|
447 |
return translateReadyOps(ops, 0, ski); |
|
448 |
} |
|
449 |
||
450 |
public int translateInterestOps(int ops) { |
|
451 |
int newOps = 0; |
|
452 |
if ((ops & SelectionKey.OP_ACCEPT) != 0) |
|
453 |
newOps |= Net.POLLIN; |
|
454 |
return newOps; |
|
455 |
} |
|
456 |
||
457 |
public FileDescriptor getFD() { |
|
458 |
return fd; |
|
459 |
} |
|
460 |
||
461 |
public int getFDVal() { |
|
462 |
return fdVal; |
|
463 |
} |
|
464 |
||
465 |
public String toString() { |
|
466 |
StringBuilder sb = new StringBuilder(); |
|
467 |
sb.append(this.getClass().getName()); |
|
468 |
sb.append('['); |
|
469 |
if (!isOpen()) { |
|
470 |
sb.append("closed"); |
|
471 |
} else { |
|
472 |
synchronized (stateLock) { |
|
473 |
InetSocketAddress addr = localAddress; |
|
474 |
if (addr == null) { |
|
475 |
sb.append("unbound"); |
|
476 |
} else { |
|
477 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
|
478 |
} |
|
479 |
} |
|
480 |
} |
|
481 |
sb.append(']'); |
|
482 |
return sb.toString(); |
|
483 |
} |
|
484 |
||
485 |
private int accept(FileDescriptor ssfd, FileDescriptor newfd, |
|
486 |
InetSocketAddress[] isaa) throws IOException { |
|
487 |
return accept0(ssfd, newfd, isaa); |
|
488 |
} |
|
489 |
||
490 |
// -- Native methods -- |
|
491 |
private static native int checkAccept(FileDescriptor fd) |
|
492 |
throws IOException; |
|
493 |
||
494 |
private native int accept0(FileDescriptor ssfd, FileDescriptor newfd, |
|
495 |
InetSocketAddress[] isaa) throws IOException; |
|
496 |
||
497 |
private static native void initIDs()throws UnsupportedOperationException; |
|
498 |
||
499 |
static { |
|
500 |
IOUtil.load(); |
|
501 |
System.loadLibrary("extnet"); |
|
502 |
UnsupportedOperationException uoe = null; |
|
503 |
try { |
|
504 |
initIDs(); |
|
505 |
} catch (UnsupportedOperationException e) { |
|
506 |
uoe = e; |
|
507 |
} |
|
508 |
unsupported = uoe; |
|
509 |
nd = new RdmaSocketDispatcher(); |
|
510 |
} |
|
511 |
} |