57115
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
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 |
#include "jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl.h"
|
|
27 |
#include "nio.h"
|
|
28 |
#include "nio_util.h"
|
|
29 |
#include <Rsocket.h>
|
|
30 |
|
|
31 |
static jfieldID fd_fdID;
|
|
32 |
|
|
33 |
jint
|
|
34 |
convertReturnVal(JNIEnv *env, jint n, jboolean reading) {
|
|
35 |
if (n > 0)
|
|
36 |
return n;
|
|
37 |
else if (n == 0) {
|
|
38 |
if (reading) {
|
|
39 |
return IOS_EOF;
|
|
40 |
} else {
|
|
41 |
return 0;
|
|
42 |
}
|
|
43 |
}
|
|
44 |
else if (errno == EAGAIN)
|
|
45 |
return IOS_UNAVAILABLE;
|
|
46 |
else if (errno == EINTR)
|
|
47 |
return IOS_INTERRUPTED;
|
|
48 |
else {
|
|
49 |
const char *msg = reading ? "Read failed" : "Write failed";
|
|
50 |
JNU_ThrowIOExceptionWithLastError(env, msg);
|
|
51 |
return IOS_THROWN;
|
|
52 |
}
|
|
53 |
}
|
|
54 |
|
|
55 |
jlong
|
|
56 |
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) {
|
|
57 |
if (n > 0)
|
|
58 |
return n;
|
|
59 |
else if (n == 0) {
|
|
60 |
if (reading) {
|
|
61 |
return IOS_EOF;
|
|
62 |
} else {
|
|
63 |
return 0;
|
|
64 |
}
|
|
65 |
}
|
|
66 |
else if (errno == EAGAIN)
|
|
67 |
return IOS_UNAVAILABLE;
|
|
68 |
else if (errno == EINTR)
|
|
69 |
return IOS_INTERRUPTED;
|
|
70 |
else {
|
|
71 |
const char *msg = reading ? "Read failed" : "Write failed";
|
|
72 |
JNU_ThrowIOExceptionWithLastError(env, msg);
|
|
73 |
return IOS_THROWN;
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
JNIEXPORT void JNICALL
|
|
78 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_init(JNIEnv *env,
|
|
79 |
jclass cl) {
|
|
80 |
loadRdmaFuncs(env);
|
|
81 |
CHECK_NULL(cl = (*env)->FindClass(env, "java/io/FileDescriptor"));
|
|
82 |
CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, cl, "fd", "I"));
|
|
83 |
}
|
|
84 |
|
|
85 |
JNIEXPORT jint JNICALL
|
|
86 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_read0(JNIEnv *env,
|
|
87 |
jclass clazz, jobject fdo, jlong address, jint len) {
|
|
88 |
jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
|
|
89 |
void *buf = (void *)jlong_to_ptr(address);
|
|
90 |
jint result = convertReturnVal(env, rs_read(fd, buf, len), JNI_TRUE);
|
|
91 |
/*
|
|
92 |
if (result == IOS_UNAVAILABLE
|
|
93 |
&& (rs_fcntl(fd, F_GETFL, 0) & O_NONBLOCK) == 0) { // blocking
|
|
94 |
struct pollfd pfd[1];
|
|
95 |
pfd[0].fd = fd;
|
|
96 |
pfd[0].events = POLLIN;
|
|
97 |
rs_poll(pfd, 1, -1);
|
|
98 |
if (pfd[0].revents & POLLIN)
|
|
99 |
result = convertReturnVal(env, rs_read(fd, buf, len), JNI_TRUE);
|
|
100 |
else {
|
|
101 |
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
|
|
102 |
return IOS_THROWN;
|
|
103 |
}
|
|
104 |
}
|
|
105 |
*/
|
|
106 |
return result;
|
|
107 |
}
|
|
108 |
|
|
109 |
JNIEXPORT jlong JNICALL
|
|
110 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_readv0(JNIEnv *env,
|
|
111 |
jclass clazz, jobject fdo, jlong address, jint len) {
|
|
112 |
jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
|
|
113 |
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
|
|
114 |
jlong result = convertLongReturnVal(env, rs_readv(fd, iov, len), JNI_TRUE);
|
|
115 |
/*
|
|
116 |
if (result == IOS_UNAVAILABLE
|
|
117 |
&& (rs_fcntl(fd, F_GETFL, 0) & O_NONBLOCK) == 0) { // blocking
|
|
118 |
struct pollfd pfd[1];
|
|
119 |
pfd[0].fd = fd;
|
|
120 |
pfd[0].events = POLLIN;
|
|
121 |
rs_poll(pfd, 1, -1);
|
|
122 |
if (pfd[0].revents & POLLIN)
|
|
123 |
result = convertLongReturnVal(env, rs_readv(fd, iov, len), JNI_TRUE);
|
|
124 |
else {
|
|
125 |
JNU_ThrowIOExceptionWithLastError(env, "Read failed");
|
|
126 |
return IOS_THROWN;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
*/
|
|
130 |
return result;
|
|
131 |
}
|
|
132 |
|
|
133 |
JNIEXPORT jint JNICALL
|
|
134 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_write0(JNIEnv *env,
|
|
135 |
jclass clazz, jobject fdo, jlong address, jint len) {
|
|
136 |
jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
|
|
137 |
void *buf = (void *)jlong_to_ptr(address);
|
|
138 |
jint result = convertReturnVal(env, rs_write(fd, buf, len), JNI_FALSE);
|
|
139 |
/*
|
|
140 |
if (result == IOS_UNAVAILABLE
|
|
141 |
&& (rs_fcntl(fd, F_GETFL, 0) & O_NONBLOCK) == 0) { // blocking
|
|
142 |
struct pollfd pfd[1];
|
|
143 |
pfd[0].fd = fd;
|
|
144 |
pfd[0].events = POLLOUT;
|
|
145 |
rs_poll(pfd, 1, -1);
|
|
146 |
if (pfd[0].revents & POLLOUT)
|
|
147 |
result = convertReturnVal(env, rs_write(fd, buf, len), JNI_FALSE);
|
|
148 |
else {
|
|
149 |
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
|
|
150 |
return IOS_THROWN;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
*/
|
|
154 |
return result;
|
|
155 |
}
|
|
156 |
|
|
157 |
JNIEXPORT jlong JNICALL
|
|
158 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_writev0(JNIEnv *env,
|
|
159 |
jclass clazz, jobject fdo, jlong address, jint len) {
|
|
160 |
jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
|
|
161 |
struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
|
|
162 |
jlong result = convertLongReturnVal(env, rs_writev(fd, iov, len),
|
|
163 |
JNI_FALSE);
|
|
164 |
/*
|
|
165 |
if (result == IOS_UNAVAILABLE
|
|
166 |
&& (rs_fcntl(fd, F_GETFL, 0) & O_NONBLOCK) == 0) { // blocking
|
|
167 |
struct pollfd pfd[1];
|
|
168 |
pfd[0].fd = fd;
|
|
169 |
pfd[0].events = POLLOUT;
|
|
170 |
rs_poll(pfd, 1, -1);
|
|
171 |
if (pfd[0].revents & POLLOUT)
|
|
172 |
result = convertLongReturnVal(env, rs_writev(fd, iov, len), JNI_FALSE);
|
|
173 |
else {
|
|
174 |
JNU_ThrowIOExceptionWithLastError(env, "Write failed");
|
|
175 |
return IOS_THROWN;
|
|
176 |
}
|
|
177 |
}
|
|
178 |
*/
|
|
179 |
return result;
|
|
180 |
}
|
|
181 |
|
|
182 |
static void closeFileDescriptor(JNIEnv *env, int fd) {
|
|
183 |
if (fd != -1) {
|
|
184 |
int result = rs_close(fd);
|
|
185 |
if (result < 0)
|
|
186 |
JNU_ThrowIOExceptionWithLastError(env, "Close failed");
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
JNIEXPORT void JNICALL
|
|
191 |
Java_jdk_internal_net_rdma_LinuxRdmaSocketDispatcherImpl_close0(JNIEnv *env,
|
|
192 |
jclass clazz, jobject fdo) {
|
|
193 |
jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
|
|
194 |
closeFileDescriptor(env, fd);
|
|
195 |
}
|