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.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.SocketAddress; |
|
36 |
import java.net.SocketOption; |
|
37 |
import java.net.StandardProtocolFamily; |
|
38 |
import java.net.StandardSocketOptions; |
|
39 |
import java.nio.channels.UnsupportedAddressTypeException; |
|
40 |
import java.security.AccessController; |
|
41 |
import java.security.PrivilegedAction; |
|
42 |
import java.util.Enumeration; |
|
43 |
import sun.net.ext.RdmaSocketOptions; |
|
44 |
import sun.nio.ch.IOUtil; |
|
45 |
import sun.nio.ch.Net; |
|
46 |
||
47 |
public class RdmaNet { |
|
48 |
private RdmaNet() { } |
|
49 |
||
50 |
static final ProtocolFamily UNSPEC = new ProtocolFamily() { |
|
51 |
public String name() { |
|
52 |
return "UNSPEC"; |
|
53 |
} |
|
54 |
}; |
|
55 |
||
56 |
static boolean isReusePortAvailable() { |
|
57 |
return false; |
|
58 |
} |
|
59 |
||
60 |
private static volatile boolean checkedRdma; |
|
61 |
private static volatile boolean isRdmaAvailable; |
|
62 |
||
63 |
static boolean isRdmaAvailable() { |
|
64 |
if (!checkedRdma) { |
|
65 |
isRdmaAvailable = isRdmaAvailable0(); |
|
66 |
checkedRdma = true; |
|
67 |
} |
|
68 |
return isRdmaAvailable; |
|
69 |
} |
|
70 |
||
71 |
private static native boolean isRdmaAvailable0(); |
|
72 |
||
73 |
static InetSocketAddress checkAddress(SocketAddress sa, ProtocolFamily family) { |
|
74 |
InetSocketAddress isa = Net.checkAddress(sa); |
|
75 |
if (family == StandardProtocolFamily.INET) { |
|
76 |
InetAddress addr = isa.getAddress(); |
|
77 |
if (!(addr instanceof Inet4Address)) |
|
78 |
throw new UnsupportedAddressTypeException(); |
|
79 |
} |
|
80 |
if (family == StandardProtocolFamily.INET6) { |
|
81 |
InetAddress addr = isa.getAddress(); |
|
82 |
if (!(addr instanceof Inet6Address)) |
|
83 |
throw new UnsupportedAddressTypeException(); |
|
84 |
} |
|
85 |
return isa; |
|
86 |
} |
|
87 |
||
88 |
// -- Socket options |
|
89 |
||
90 |
static final RdmaSocketOptions rdmaOptions = |
|
91 |
RdmaSocketOptions.getInstance(); |
|
92 |
||
93 |
static void setSocketOption(FileDescriptor fd, ProtocolFamily family, |
|
94 |
SocketOption<?> name, Object value) throws IOException |
|
95 |
{ |
|
96 |
if (value == null) |
|
97 |
throw new IllegalArgumentException("Invalid option value"); |
|
98 |
||
99 |
Class<?> type = name.type(); |
|
100 |
||
101 |
if (rdmaOptions.isOptionSupported(name)) { |
|
102 |
rdmaOptions.setOption(fd, name, value); |
|
103 |
return; |
|
104 |
} |
|
105 |
||
106 |
if (type != Integer.class && type != Boolean.class) |
|
107 |
throw new AssertionError("Should not reach here"); |
|
108 |
||
109 |
if (name == StandardSocketOptions.SO_RCVBUF || |
|
110 |
name == StandardSocketOptions.SO_SNDBUF) |
|
111 |
{ |
|
112 |
int i = ((Integer)value).intValue(); |
|
113 |
if (i < 0) |
|
114 |
throw new IllegalArgumentException( |
|
115 |
"Invalid send/receive buffer size"); |
|
116 |
} |
|
117 |
||
118 |
RdmaOptionKey key = RdmaSocketOptionRegistry.findOption(name, family); |
|
119 |
if (key == null) |
|
120 |
throw new AssertionError("Option not found"); |
|
121 |
||
122 |
int arg; |
|
123 |
int maxValue = 1024 * 1024 * 1024 - 1; |
|
124 |
if (type == Integer.class) { |
|
125 |
arg = ((Integer)value).intValue(); |
|
126 |
if (arg > maxValue) |
|
127 |
arg = maxValue; |
|
128 |
} else { |
|
129 |
boolean b = ((Boolean)value).booleanValue(); |
|
130 |
arg = (b) ? 1 : 0; |
|
131 |
} |
|
132 |
||
133 |
boolean mayNeedConversion = (family == UNSPEC); |
|
134 |
setIntOption0(fd, mayNeedConversion, key.level(), |
|
135 |
key.name(), arg); |
|
136 |
} |
|
137 |
||
138 |
static Object getSocketOption(FileDescriptor fd, ProtocolFamily family, |
|
139 |
SocketOption<?> name) throws IOException |
|
140 |
{ |
|
141 |
Class<?> type = name.type(); |
|
142 |
||
143 |
if (rdmaOptions.isOptionSupported(name)) { |
|
144 |
return rdmaOptions.getOption(fd, name); |
|
145 |
} |
|
146 |
||
147 |
if (type != Integer.class && type != Boolean.class) |
|
148 |
throw new AssertionError("Should not reach here"); |
|
149 |
||
150 |
RdmaOptionKey key = RdmaSocketOptionRegistry.findOption(name, family); |
|
151 |
if (key == null) |
|
152 |
throw new AssertionError("Option not found"); |
|
153 |
||
154 |
boolean mayNeedConversion = (family == UNSPEC); |
|
155 |
int value = getIntOption0(fd, mayNeedConversion, key.level(), |
|
156 |
key.name()); |
|
157 |
||
158 |
if (type == Integer.class) { |
|
159 |
return Integer.valueOf(value); |
|
160 |
} else { |
|
161 |
return (value == 0) ? Boolean.FALSE : Boolean.TRUE; |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
// -- Socket operations -- |
|
166 |
static FileDescriptor socket(ProtocolFamily family, boolean stream) |
|
167 |
throws IOException { |
|
168 |
boolean preferIPv6 = Net.isIPv6Available() && |
|
169 |
(family != StandardProtocolFamily.INET); |
|
170 |
return IOUtil.newFD(socket0(preferIPv6, stream, false)); |
|
171 |
} |
|
172 |
||
173 |
static FileDescriptor serverSocket(ProtocolFamily family, boolean stream) |
|
174 |
throws IOException { |
|
175 |
boolean preferIPv6 = Net.isIPv6Available() && |
|
176 |
(family != StandardProtocolFamily.INET); |
|
177 |
return IOUtil.newFD(socket0(preferIPv6, stream, true)); |
|
178 |
} |
|
179 |
||
180 |
private static native int socket0(boolean preferIPv6, boolean stream, |
|
181 |
boolean reuse); |
|
182 |
static void bind(ProtocolFamily family, FileDescriptor fd, |
|
183 |
InetAddress addr, int port) throws IOException |
|
184 |
{ |
|
185 |
boolean preferIPv6 = Net.isIPv6Available() && |
|
186 |
(family != StandardProtocolFamily.INET); |
|
187 |
bind0(fd, preferIPv6, addr, port); |
|
188 |
} |
|
189 |
||
190 |
private static native void bind0(FileDescriptor fd, boolean preferIPv6, |
|
191 |
InetAddress addr, int port) throws IOException; |
|
192 |
||
193 |
static native void listen(FileDescriptor fd, int backlog) |
|
194 |
throws IOException; |
|
195 |
||
196 |
static int connect(FileDescriptor fd, InetAddress remote, int remotePort) |
|
197 |
throws IOException |
|
198 |
{ |
|
199 |
return connect(UNSPEC, fd, remote, remotePort); |
|
200 |
} |
|
201 |
||
202 |
static int connect(ProtocolFamily family, FileDescriptor fd, |
|
203 |
InetAddress remote, int remotePort) throws IOException |
|
204 |
{ |
|
205 |
boolean preferIPv6 = Net.isIPv6Available() && |
|
206 |
(family != StandardProtocolFamily.INET); |
|
207 |
return connect0(preferIPv6, fd, remote, remotePort); |
|
208 |
} |
|
209 |
||
210 |
public static InetSocketAddress localAddress(FileDescriptor fd) |
|
211 |
throws IOException |
|
212 |
{ |
|
213 |
return new InetSocketAddress(localInetAddress(fd), localPort(fd)); |
|
214 |
} |
|
215 |
||
216 |
private static native int connect0(boolean preferIPv6, FileDescriptor fd, |
|
217 |
InetAddress remote, int remotePort) throws IOException; |
|
218 |
||
219 |
static native void shutdown(FileDescriptor fd, int how) throws IOException; |
|
220 |
||
221 |
private static native int localPort(FileDescriptor fd) |
|
222 |
throws IOException; |
|
223 |
||
224 |
private static native InetAddress localInetAddress(FileDescriptor fd) |
|
225 |
throws IOException; |
|
226 |
||
227 |
private static native int getIntOption0(FileDescriptor fd, |
|
228 |
boolean mayNeedConversion, int level, int opt) throws IOException; |
|
229 |
||
230 |
private static native void setIntOption0(FileDescriptor fd, |
|
231 |
boolean mayNeedConversion, int level, int opt, int arg) |
|
232 |
throws IOException; |
|
233 |
||
234 |
static native int poll(FileDescriptor fd, int events, long timeout) |
|
235 |
throws IOException; |
|
236 |
||
237 |
public static native void configureBlocking(FileDescriptor fd, |
|
238 |
boolean blocking); |
|
239 |
||
240 |
private static native void initIDs(); |
|
241 |
||
242 |
static { |
|
243 |
java.security.AccessController.doPrivileged( |
|
244 |
new java.security.PrivilegedAction<>() { |
|
245 |
public Void run() { |
|
246 |
System.loadLibrary("extnet"); |
|
247 |
return null; |
|
248 |
} |
|
249 |
}); |
|
250 |
IOUtil.load(); |
|
251 |
initIDs(); |
|
252 |
} |
|
253 |
} |