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.Inet4Address;
31 import java.net.Inet6Address;
32 import java.net.InetAddress;
33 import java.net.InetSocketAddress;
34 import java.net.ProtocolFamily;
35 import java.net.Socket;
36 import java.net.SocketAddress;
37 import java.net.SocketOption;
38 import java.net.StandardProtocolFamily;
39 import java.net.StandardSocketOptions;
40 import java.nio.ByteBuffer;
41 import java.nio.channels.AlreadyBoundException;
42 import java.nio.channels.AlreadyConnectedException;
43 import java.nio.channels.AsynchronousCloseException;
44 import java.nio.channels.ClosedChannelException;
45 import java.nio.channels.ConnectionPendingException;
46 import java.nio.channels.NoConnectionPendingException;
47 import java.nio.channels.NotYetConnectedException;
48 import java.nio.channels.SelectionKey;
49 import java.nio.channels.SocketChannel;
50 import java.nio.channels.spi.SelectorProvider;
51 import java.util.Collections;
52 import java.util.HashSet;
53 import java.util.Objects;
54 import java.util.Set;
55 import java.util.concurrent.locks.ReentrantLock;
56 import sun.net.ext.RdmaSocketOptions;
57 import sun.nio.ch.IOStatus;
58 import sun.nio.ch.IOUtil;
59 import sun.nio.ch.Net;
60 import sun.nio.ch.NativeThread;
61 import sun.nio.ch.SelChImpl;
62 import sun.nio.ch.SelectionKeyImpl;
63
64 public class RdmaSocketChannelImpl
65 extends SocketChannel
66 implements SelChImpl
67 {
68 // The protocol family of the socket
69 private final ProtocolFamily family;
70
71 private static RdmaSocketDispatcher nd;
72 private final FileDescriptor fd;
73 private final int fdVal;
74
75 private final ReentrantLock readLock = new ReentrantLock();
76 private final ReentrantLock writeLock = new ReentrantLock();
77
78 private final Object stateLock = new Object();
79
80 private volatile boolean isInputClosed;
81 private volatile boolean isOutputClosed;
82
95
96 private InetSocketAddress localAddress;
97 private InetSocketAddress remoteAddress;
98
99 private Socket socket;
100
101 private static final UnsupportedOperationException unsupported;
102
103 private static final SelectorProvider checkSupported(SelectorProvider sp) {
104 if (unsupported != null)
105 throw new UnsupportedOperationException(unsupported.getMessage(),
106 unsupported);
107 else
108 return sp;
109 }
110
111 protected RdmaSocketChannelImpl(SelectorProvider sp, ProtocolFamily family)
112 throws IOException {
113 super(checkSupported(sp));
114
115 Objects.requireNonNull(family, "'family' is null");
116 if ((family != StandardProtocolFamily.INET) &&
117 (family != StandardProtocolFamily.INET6)) {
118 throw new UnsupportedOperationException(
119 "Protocol family not supported");
120 }
121 if (family == StandardProtocolFamily.INET6) {
122 if (!Net.isIPv6Available()) {
123 throw new UnsupportedOperationException("IPv6 not available");
124 }
125 }
126
127 this.family = family;
128 this.fd = RdmaNet.socket(family, true);
129 this.fdVal = IOUtil.fdVal(fd);
130 }
131
132 RdmaSocketChannelImpl(SelectorProvider sp, FileDescriptor fd,
133 InetSocketAddress isa) throws IOException {
134 super(checkSupported(sp));
135 this.family = Net.isIPv6Available()
136 ? StandardProtocolFamily.INET6
137 : StandardProtocolFamily.INET;
138 this.fd = fd;
139 this.fdVal = IOUtil.fdVal(fd);
140 synchronized (stateLock) {
141 this.localAddress = RdmaNet.localAddress(fd);
142 this.remoteAddress = isa;
143 this.state = ST_CONNECTED;
144 }
145 }
146
147 private void ensureOpen() throws ClosedChannelException {
148 if (!isOpen())
149 throw new ClosedChannelException();
150 }
151
152 private void ensureOpenAndConnected() throws ClosedChannelException {
153 int state = this.state;
154 if (state < ST_CONNECTED) {
155 throw new NotYetConnectedException();
156 } else if (state > ST_CONNECTED) {
157 throw new ClosedChannelException();
458 } finally {
459 writeLock.unlock();
460 }
461 } finally {
462 readLock.unlock();
463 }
464 }
465
466 InetSocketAddress localAddress() {
467 synchronized (stateLock) {
468 return localAddress;
469 }
470 }
471
472 InetSocketAddress remoteAddress() {
473 synchronized (stateLock) {
474 return remoteAddress;
475 }
476 }
477
478 @Override
479 public SocketChannel bind(SocketAddress local) throws IOException {
480 readLock.lock();
481 try {
482 writeLock.lock();
483 try {
484 synchronized (stateLock) {
485 ensureOpen();
486 if (state == ST_CONNECTIONPENDING)
487 throw new ConnectionPendingException();
488 if (localAddress != null)
489 throw new AlreadyBoundException();
490 InetSocketAddress isa = (local == null) ?
491 new InetSocketAddress(0)
492 : RdmaNet.checkAddress(local, family);
493 SecurityManager sm = System.getSecurityManager();
494 if (sm != null) {
495 sm.checkListen(isa.getPort());
496 }
497 RdmaNet.bind(family, fd, isa.getAddress(), isa.getPort());
498 localAddress = RdmaNet.localAddress(fd);
499 }
500 } finally {
501 writeLock.unlock();
502 }
503 } finally {
504 readLock.unlock();
505 }
506 return this;
507 }
508
509 @Override
510 public boolean isConnected() {
511 return (state == ST_CONNECTED);
|
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.Inet4Address;
31 import java.net.Inet6Address;
32 import java.net.InetAddress;
33 import java.net.InetSocketAddress;
34 import java.net.ProtocolFamily;
35 import java.net.Socket;
36 import java.net.SocketAddress;
37 import java.net.SocketOption;
38 import java.net.StandardSocketOptions;
39 import java.nio.ByteBuffer;
40 import java.nio.channels.AlreadyBoundException;
41 import java.nio.channels.AlreadyConnectedException;
42 import java.nio.channels.AsynchronousCloseException;
43 import java.nio.channels.ClosedChannelException;
44 import java.nio.channels.ConnectionPendingException;
45 import java.nio.channels.NoConnectionPendingException;
46 import java.nio.channels.NotYetConnectedException;
47 import java.nio.channels.SelectionKey;
48 import java.nio.channels.SocketChannel;
49 import java.nio.channels.UnsupportedAddressTypeException;
50 import java.nio.channels.spi.SelectorProvider;
51 import java.util.Collections;
52 import java.util.HashSet;
53 import java.util.Objects;
54 import java.util.Set;
55 import java.util.concurrent.locks.ReentrantLock;
56 import sun.net.ext.RdmaSocketOptions;
57 import sun.nio.ch.IOStatus;
58 import sun.nio.ch.IOUtil;
59 import sun.nio.ch.NativeThread;
60 import sun.nio.ch.Net;
61 import sun.nio.ch.SelChImpl;
62 import sun.nio.ch.SelectionKeyImpl;
63 import static java.net.StandardProtocolFamily.INET;
64 import static java.net.StandardProtocolFamily.INET6;
65
66 public class RdmaSocketChannelImpl
67 extends SocketChannel
68 implements SelChImpl
69 {
70 // The protocol family of the socket
71 private final ProtocolFamily family;
72
73 private static RdmaSocketDispatcher nd;
74 private final FileDescriptor fd;
75 private final int fdVal;
76
77 private final ReentrantLock readLock = new ReentrantLock();
78 private final ReentrantLock writeLock = new ReentrantLock();
79
80 private final Object stateLock = new Object();
81
82 private volatile boolean isInputClosed;
83 private volatile boolean isOutputClosed;
84
97
98 private InetSocketAddress localAddress;
99 private InetSocketAddress remoteAddress;
100
101 private Socket socket;
102
103 private static final UnsupportedOperationException unsupported;
104
105 private static final SelectorProvider checkSupported(SelectorProvider sp) {
106 if (unsupported != null)
107 throw new UnsupportedOperationException(unsupported.getMessage(),
108 unsupported);
109 else
110 return sp;
111 }
112
113 protected RdmaSocketChannelImpl(SelectorProvider sp, ProtocolFamily family)
114 throws IOException {
115 super(checkSupported(sp));
116
117 Objects.requireNonNull(family, "null family");
118 if (!(family == INET || family == INET6)) {
119 throw new UnsupportedOperationException("Protocol family not supported");
120 }
121 if (family == INET6) {
122 if (!Net.isIPv6Available()) {
123 throw new UnsupportedOperationException("IPv6 not available");
124 }
125 }
126
127 this.family = family;
128 this.fd = RdmaNet.socket(family, true);
129 this.fdVal = IOUtil.fdVal(fd);
130 }
131
132 RdmaSocketChannelImpl(SelectorProvider sp,
133 FileDescriptor fd,
134 InetSocketAddress isa) throws IOException {
135 super(checkSupported(sp));
136 this.family = Net.isIPv6Available() ? INET6 : INET;
137 this.fd = fd;
138 this.fdVal = IOUtil.fdVal(fd);
139 synchronized (stateLock) {
140 this.localAddress = RdmaNet.localAddress(fd);
141 this.remoteAddress = isa;
142 this.state = ST_CONNECTED;
143 }
144 }
145
146 private void ensureOpen() throws ClosedChannelException {
147 if (!isOpen())
148 throw new ClosedChannelException();
149 }
150
151 private void ensureOpenAndConnected() throws ClosedChannelException {
152 int state = this.state;
153 if (state < ST_CONNECTED) {
154 throw new NotYetConnectedException();
155 } else if (state > ST_CONNECTED) {
156 throw new ClosedChannelException();
457 } finally {
458 writeLock.unlock();
459 }
460 } finally {
461 readLock.unlock();
462 }
463 }
464
465 InetSocketAddress localAddress() {
466 synchronized (stateLock) {
467 return localAddress;
468 }
469 }
470
471 InetSocketAddress remoteAddress() {
472 synchronized (stateLock) {
473 return remoteAddress;
474 }
475 }
476
477 private final InetSocketAddress anyLocalAddress() throws IOException {
478 if (family == INET)
479 return new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0);
480 else if (family == INET6)
481 return new InetSocketAddress(InetAddress.getByName("::"), 0);
482 else
483 throw new UnsupportedAddressTypeException();
484 }
485
486 @Override
487 public SocketChannel bind(SocketAddress local) throws IOException {
488 readLock.lock();
489 try {
490 writeLock.lock();
491 try {
492 synchronized (stateLock) {
493 ensureOpen();
494 if (state == ST_CONNECTIONPENDING)
495 throw new ConnectionPendingException();
496 if (localAddress != null)
497 throw new AlreadyBoundException();
498 InetSocketAddress isa = (local == null)
499 ? anyLocalAddress()
500 : RdmaNet.checkAddress(local, family);
501 SecurityManager sm = System.getSecurityManager();
502 if (sm != null) {
503 sm.checkListen(isa.getPort());
504 }
505 RdmaNet.bind(family, fd, isa.getAddress(), isa.getPort());
506 localAddress = RdmaNet.localAddress(fd);
507 }
508 } finally {
509 writeLock.unlock();
510 }
511 } finally {
512 readLock.unlock();
513 }
514 return this;
515 }
516
517 @Override
518 public boolean isConnected() {
519 return (state == ST_CONNECTED);
|