| 1 |
/* |
| 2 |
* Copyright 2013 The Netty Project |
| 3 |
* |
| 4 |
* The Netty Project licenses this file to you under the Apache License, |
| 5 |
* version 2.0 (the "License"); you may not use this file except in compliance |
| 6 |
* with the License. You may obtain a copy of the License at: |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
* License for the specific language governing permissions and limitations |
| 14 |
* under the License. |
| 15 |
*/ |
| 16 |
#define _GNU_SOURCE |
| 17 |
#include <jni.h> |
| 18 |
#include <stdint.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <sys/epoll.h> |
| 23 |
#include <sys/eventfd.h> |
| 24 |
#include <sys/un.h> |
| 25 |
#include <netinet/in.h> |
| 26 |
#include <netinet/tcp.h> |
| 27 |
#include <sys/types.h> |
| 28 |
#include <sys/socket.h> |
| 29 |
#include <sys/timerfd.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <arpa/inet.h> |
| 32 |
#include <fcntl.h> |
| 33 |
#include <sys/utsname.h> |
| 34 |
#include <stddef.h> |
| 35 |
#include <limits.h> |
| 36 |
#include <inttypes.h> |
| 37 |
#include <link.h> |
| 38 |
#include <time.h> |
| 39 |
// Needed to be able to use syscalls directly and so not depend on newer GLIBC versions |
| 40 |
#include <linux/net.h> |
| 41 |
#include <sys/syscall.h> |
| 42 |
|
| 43 |
#include "netty_epoll_linuxsocket.h" |
| 44 |
#include "netty_unix_buffer.h" |
| 45 |
#include "netty_unix_errors.h" |
| 46 |
#include "netty_unix_filedescriptor.h" |
| 47 |
#include "netty_unix_jni.h" |
| 48 |
#include "netty_unix_limits.h" |
| 49 |
#include "netty_unix_socket.h" |
| 50 |
#include "netty_unix_util.h" |
| 51 |
|
| 52 |
// TCP_FASTOPEN is defined in linux 3.7. We define this here so older kernels can compile. |
| 53 |
#ifndef TCP_FASTOPEN |
| 54 |
#define TCP_FASTOPEN 23 |
| 55 |
#endif |
| 56 |
|
| 57 |
// optional |
| 58 |
extern int epoll_create1(int flags) __attribute__((weak)); |
| 59 |
|
| 60 |
#ifndef __USE_GNU |
| 61 |
struct mmsghdr { |
| 62 |
struct msghdr msg_hdr; /* Message header */ |
| 63 |
unsigned int msg_len; /* Number of bytes transmitted */ |
| 64 |
}; |
| 65 |
#endif |
| 66 |
|
| 67 |
// All linux syscall numbers are stable so this is safe. |
| 68 |
#ifndef SYS_recvmmsg |
| 69 |
// Only support SYS_recvmmsg for __x86_64__ / __i386__ for now |
| 70 |
#if defined(__x86_64__) |
| 71 |
// See https://github.com/torvalds/linux/blob/v5.4/arch/x86/entry/syscalls/syscall_64.tbl |
| 72 |
#define SYS_recvmmsg 299 |
| 73 |
#elif defined(__i386__) |
| 74 |
// See https://github.com/torvalds/linux/blob/v5.4/arch/x86/entry/syscalls/syscall_32.tbl |
| 75 |
#define SYS_recvmmsg 337 |
| 76 |
#else |
| 77 |
#define SYS_recvmmsg -1 |
| 78 |
#endif |
| 79 |
#endif // SYS_recvmmsg |
| 80 |
|
| 81 |
#ifndef SYS_sendmmsg |
| 82 |
// Only support SYS_sendmmsg for __x86_64__ / __i386__ for now |
| 83 |
#if defined(__x86_64__) |
| 84 |
// See https://github.com/torvalds/linux/blob/v5.4/arch/x86/entry/syscalls/syscall_64.tbl |
| 85 |
#define SYS_sendmmsg 307 |
| 86 |
#elif defined(__i386__) |
| 87 |
// See https://github.com/torvalds/linux/blob/v5.4/arch/x86/entry/syscalls/syscall_32.tbl |
| 88 |
#define SYS_sendmmsg 345 |
| 89 |
#else |
| 90 |
#define SYS_sendmmsg -1 |
| 91 |
#endif |
| 92 |
#endif // SYS_sendmmsg |
| 93 |
|
| 94 |
// Those are initialized in the init(...) method and cached for performance reasons |
| 95 |
static jfieldID packetAddrFieldId = NULL; |
| 96 |
static jfieldID packetAddrLenFieldId = NULL; |
| 97 |
static jfieldID packetScopeIdFieldId = NULL; |
| 98 |
static jfieldID packetPortFieldId = NULL; |
| 99 |
static jfieldID packetMemoryAddressFieldId = NULL; |
| 100 |
static jfieldID packetCountFieldId = NULL; |
| 101 |
|
| 102 |
// util methods |
| 103 |
static int getSysctlValue(const char * property, int* returnValue) { |
| 104 |
int rc = -1; |
| 105 |
FILE *fd=fopen(property, "r"); |
| 106 |
if (fd != NULL) { |
| 107 |
char buf[32] = {0x0}; |
| 108 |
if (fgets(buf, 32, fd) != NULL) { |
| 109 |
*returnValue = atoi(buf); |
| 110 |
rc = 0; |
| 111 |
} |
| 112 |
fclose(fd); |
| 113 |
} |
| 114 |
return rc; |
| 115 |
} |
| 116 |
|
| 117 |
static inline jint epollCtl(JNIEnv* env, jint efd, int op, jint fd, jint flags) { |
| 118 |
uint32_t events = flags; |
| 119 |
struct epoll_event ev = { |
| 120 |
.data.fd = fd, |
| 121 |
.events = events |
| 122 |
}; |
| 123 |
|
| 124 |
return epoll_ctl(efd, op, fd, &ev); |
| 125 |
} |
| 126 |
// JNI Registered Methods Begin |
| 127 |
static jint netty_epoll_native_eventFd(JNIEnv* env, jclass clazz) { |
| 128 |
jint eventFD = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK); |
| 129 |
|
| 130 |
if (eventFD < 0) { |
| 131 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "eventfd() failed: ", errno); |
| 132 |
} |
| 133 |
return eventFD; |
| 134 |
} |
| 135 |
|
| 136 |
static jint netty_epoll_native_timerFd(JNIEnv* env, jclass clazz) { |
| 137 |
jint timerFD = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); |
| 138 |
|
| 139 |
if (timerFD < 0) { |
| 140 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "timerfd_create() failed: ", errno); |
| 141 |
} |
| 142 |
return timerFD; |
| 143 |
} |
| 144 |
|
| 145 |
static void netty_epoll_native_eventFdWrite(JNIEnv* env, jclass clazz, jint fd, jlong value) { |
| 146 |
uint64_t val; |
| 147 |
|
| 148 |
for (;;) { |
| 149 |
jint ret = eventfd_write(fd, (eventfd_t) value); |
| 150 |
|
| 151 |
if (ret < 0) { |
| 152 |
// We need to read before we can write again, let's try to read and then write again and if this |
| 153 |
// fails we will bail out. |
| 154 |
// |
| 155 |
// See http://man7.org/linux/man-pages/man2/eventfd.2.html. |
| 156 |
if (errno == EAGAIN) { |
| 157 |
if (eventfd_read(fd, &val) == 0 || errno == EAGAIN) { |
| 158 |
// Try again |
| 159 |
continue; |
| 160 |
} |
| 161 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "eventfd_read(...) failed: ", errno); |
| 162 |
} else { |
| 163 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "eventfd_write(...) failed: ", errno); |
| 164 |
} |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
static void netty_epoll_native_eventFdRead(JNIEnv* env, jclass clazz, jint fd) { |
| 171 |
uint64_t eventfd_t; |
| 172 |
|
| 173 |
if (eventfd_read(fd, &eventfd_t) != 0) { |
| 174 |
// something is serious wrong |
| 175 |
netty_unix_errors_throwRuntimeException(env, "eventfd_read() failed"); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
static void netty_epoll_native_timerFdRead(JNIEnv* env, jclass clazz, jint fd) { |
| 180 |
uint64_t timerFireCount; |
| 181 |
|
| 182 |
if (read(fd, &timerFireCount, sizeof(uint64_t)) < 0) { |
| 183 |
// it is expected that this is only called where there is known to be activity, so this is an error. |
| 184 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "read() failed: ", errno); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
static jint netty_epoll_native_epollCreate(JNIEnv* env, jclass clazz) { |
| 189 |
jint efd; |
| 190 |
if (epoll_create1) { |
| 191 |
efd = epoll_create1(EPOLL_CLOEXEC); |
| 192 |
} else { |
| 193 |
// size will be ignored anyway but must be positive |
| 194 |
efd = epoll_create(126); |
| 195 |
} |
| 196 |
if (efd < 0) { |
| 197 |
int err = errno; |
| 198 |
if (epoll_create1) { |
| 199 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "epoll_create1() failed: ", err); |
| 200 |
} else { |
| 201 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "epoll_create() failed: ", err); |
| 202 |
} |
| 203 |
return efd; |
| 204 |
} |
| 205 |
if (!epoll_create1) { |
| 206 |
if (fcntl(efd, F_SETFD, FD_CLOEXEC) < 0) { |
| 207 |
int err = errno; |
| 208 |
close(efd); |
| 209 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "fcntl() failed: ", err); |
| 210 |
return err; |
| 211 |
} |
| 212 |
} |
| 213 |
return efd; |
| 214 |
} |
| 215 |
|
| 216 |
static void netty_epoll_native_timerFdSetTime(JNIEnv* env, jclass clazz, jint timerFd, jint tvSec, jint tvNsec) { |
| 217 |
struct itimerspec ts; |
| 218 |
memset(&ts.it_interval, 0, sizeof(struct timespec)); |
| 219 |
ts.it_value.tv_sec = tvSec; |
| 220 |
ts.it_value.tv_nsec = tvNsec; |
| 221 |
if (timerfd_settime(timerFd, 0, &ts, NULL) < 0) { |
| 222 |
netty_unix_errors_throwIOExceptionErrorNo(env, "timerfd_settime() failed: ", errno); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static jint netty_epoll_native_epollWait(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timeout) { |
| 227 |
struct epoll_event *ev = (struct epoll_event*) (intptr_t) address; |
| 228 |
int result, err; |
| 229 |
|
| 230 |
do { |
| 231 |
result = epoll_wait(efd, ev, len, timeout); |
| 232 |
if (result >= 0) { |
| 233 |
return result; |
| 234 |
} |
| 235 |
} while((err = errno) == EINTR); |
| 236 |
return -err; |
| 237 |
} |
| 238 |
|
| 239 |
// This method is deprecated! |
| 240 |
static jint netty_epoll_native_epollWait0(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len, jint timerFd, jint tvSec, jint tvNsec) { |
| 241 |
// only reschedule the timer if there is a newer event. |
| 242 |
// -1 is a special value used by EpollEventLoop. |
| 243 |
if (tvSec != ((jint) -1) && tvNsec != ((jint) -1)) { |
| 244 |
struct itimerspec ts; |
| 245 |
memset(&ts.it_interval, 0, sizeof(struct timespec)); |
| 246 |
ts.it_value.tv_sec = tvSec; |
| 247 |
ts.it_value.tv_nsec = tvNsec; |
| 248 |
if (timerfd_settime(timerFd, 0, &ts, NULL) < 0) { |
| 249 |
netty_unix_errors_throwChannelExceptionErrorNo(env, "timerfd_settime() failed: ", errno); |
| 250 |
return -1; |
| 251 |
} |
| 252 |
} |
| 253 |
return netty_epoll_native_epollWait(env, clazz, efd, address, len, -1); |
| 254 |
} |
| 255 |
|
| 256 |
static inline void cpu_relax() { |
| 257 |
#if defined(__x86_64__) |
| 258 |
asm volatile("pause\n": : :"memory"); |
| 259 |
#endif |
| 260 |
} |
| 261 |
|
| 262 |
static jint netty_epoll_native_epollBusyWait0(JNIEnv* env, jclass clazz, jint efd, jlong address, jint len) { |
| 263 |
struct epoll_event *ev = (struct epoll_event*) (intptr_t) address; |
| 264 |
int result, err; |
| 265 |
|
| 266 |
// Zeros = poll (aka return immediately). |
| 267 |
do { |
| 268 |
result = epoll_wait(efd, ev, len, 0); |
| 269 |
if (result == 0) { |
| 270 |
// Since we're always polling epoll_wait with no timeout, |
| 271 |
// signal CPU that we're in a busy loop |
| 272 |
cpu_relax(); |
| 273 |
} |
| 274 |
|
| 275 |
if (result >= 0) { |
| 276 |
return result; |
| 277 |
} |
| 278 |
} while((err = errno) == EINTR); |
| 279 |
|
| 280 |
return -err; |
| 281 |
} |
| 282 |
|
| 283 |
static jint netty_epoll_native_epollCtlAdd0(JNIEnv* env, jclass clazz, jint efd, jint fd, jint flags) { |
| 284 |
int res = epollCtl(env, efd, EPOLL_CTL_ADD, fd, flags); |
| 285 |
if (res < 0) { |
| 286 |
return -errno; |
| 287 |
} |
| 288 |
return res; |
| 289 |
} |
| 290 |
static jint netty_epoll_native_epollCtlMod0(JNIEnv* env, jclass clazz, jint efd, jint fd, jint flags) { |
| 291 |
int res = epollCtl(env, efd, EPOLL_CTL_MOD, fd, flags); |
| 292 |
if (res < 0) { |
| 293 |
return -errno; |
| 294 |
} |
| 295 |
return res; |
| 296 |
} |
| 297 |
|
| 298 |
static jint netty_epoll_native_epollCtlDel0(JNIEnv* env, jclass clazz, jint efd, jint fd) { |
| 299 |
// Create an empty event to workaround a bug in older kernels which can not handle NULL. |
| 300 |
struct epoll_event event = { 0 }; |
| 301 |
int res = epoll_ctl(efd, EPOLL_CTL_DEL, fd, &event); |
| 302 |
if (res < 0) { |
| 303 |
return -errno; |
| 304 |
} |
| 305 |
return res; |
| 306 |
} |
| 307 |
|
| 308 |
static jint netty_epoll_native_sendmmsg0(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jobjectArray packets, jint offset, jint len) { |
| 309 |
struct mmsghdr msg[len]; |
| 310 |
struct sockaddr_storage addr[len]; |
| 311 |
socklen_t addrSize; |
| 312 |
int i; |
| 313 |
|
| 314 |
memset(msg, 0, sizeof(msg)); |
| 315 |
|
| 316 |
for (i = 0; i < len; i++) { |
| 317 |
|
| 318 |
jobject packet = (*env)->GetObjectArrayElement(env, packets, i + offset); |
| 319 |
jbyteArray address = (jbyteArray) (*env)->GetObjectField(env, packet, packetAddrFieldId); |
| 320 |
jint addrLen = (*env)->GetIntField(env, packet, packetAddrLenFieldId); |
| 321 |
|
| 322 |
if (addrLen != 0) { |
| 323 |
jint scopeId = (*env)->GetIntField(env, packet, packetScopeIdFieldId); |
| 324 |
jint port = (*env)->GetIntField(env, packet, packetPortFieldId); |
| 325 |
|
| 326 |
if (netty_unix_socket_initSockaddr(env, ipv6, address, scopeId, port, &addr[i], &addrSize) == -1) { |
| 327 |
return -1; |
| 328 |
} |
| 329 |
msg[i].msg_hdr.msg_name = &addr[i]; |
| 330 |
msg[i].msg_hdr.msg_namelen = addrSize; |
| 331 |
} |
| 332 |
|
| 333 |
msg[i].msg_hdr.msg_iov = (struct iovec*) (intptr_t) (*env)->GetLongField(env, packet, packetMemoryAddressFieldId); |
| 334 |
msg[i].msg_hdr.msg_iovlen = (*env)->GetIntField(env, packet, packetCountFieldId); |
| 335 |
} |
| 336 |
|
| 337 |
ssize_t res; |
| 338 |
int err; |
| 339 |
do { |
| 340 |
// We directly use the syscall to prevent depending on GLIBC 2.14. |
| 341 |
res = syscall(SYS_sendmmsg, fd, msg, len, 0); |
| 342 |
// keep on writing if it was interrupted |
| 343 |
} while (res == -1 && ((err = errno) == EINTR)); |
| 344 |
|
| 345 |
if (res < 0) { |
| 346 |
return -err; |
| 347 |
} |
| 348 |
return (jint) res; |
| 349 |
} |
| 350 |
|
| 351 |
static jint netty_epoll_native_recvmmsg0(JNIEnv* env, jclass clazz, jint fd, jboolean ipv6, jobjectArray packets, jint offset, jint len) { |
| 352 |
struct mmsghdr msg[len]; |
| 353 |
memset(msg, 0, sizeof(msg)); |
| 354 |
struct sockaddr_storage addr[len]; |
| 355 |
int addrSize = sizeof(addr); |
| 356 |
memset(addr, 0, addrSize); |
| 357 |
|
| 358 |
int i; |
| 359 |
|
| 360 |
for (i = 0; i < len; i++) { |
| 361 |
jobject packet = (*env)->GetObjectArrayElement(env, packets, i + offset); |
| 362 |
msg[i].msg_hdr.msg_iov = (struct iovec*) (intptr_t) (*env)->GetLongField(env, packet, packetMemoryAddressFieldId); |
| 363 |
msg[i].msg_hdr.msg_iovlen = (*env)->GetIntField(env, packet, packetCountFieldId); |
| 364 |
|
| 365 |
msg[i].msg_hdr.msg_name = addr + i; |
| 366 |
msg[i].msg_hdr.msg_namelen = (socklen_t) addrSize; |
| 367 |
} |
| 368 |
|
| 369 |
ssize_t res; |
| 370 |
int err; |
| 371 |
do { |
| 372 |
// We directly use the syscall to prevent depending on GLIBC 2.12. |
| 373 |
res = syscall(SYS_recvmmsg, fd, &msg, len, 0, NULL); |
| 374 |
|
| 375 |
// keep on reading if it was interrupted |
| 376 |
} while (res == -1 && ((err = errno) == EINTR)); |
| 377 |
|
| 378 |
if (res < 0) { |
| 379 |
return -err; |
| 380 |
} |
| 381 |
|
| 382 |
for (i = 0; i < res; i++) { |
| 383 |
jobject packet = (*env)->GetObjectArrayElement(env, packets, i + offset); |
| 384 |
jbyteArray address = (jbyteArray) (*env)->GetObjectField(env, packet, packetAddrFieldId); |
| 385 |
|
| 386 |
(*env)->SetIntField(env, packet, packetCountFieldId, msg[i].msg_len); |
| 387 |
|
| 388 |
struct sockaddr_storage* addr = (struct sockaddr_storage*) msg[i].msg_hdr.msg_name; |
| 389 |
|
| 390 |
if (addr->ss_family == AF_INET) { |
| 391 |
struct sockaddr_in* ipaddr = (struct sockaddr_in*) addr; |
| 392 |
|
| 393 |
(*env)->SetByteArrayRegion(env, address, 0, 4, (jbyte*) &ipaddr->sin_addr.s_addr); |
| 394 |
(*env)->SetIntField(env, packet, packetAddrLenFieldId, 4); |
| 395 |
(*env)->SetIntField(env, packet, packetScopeIdFieldId, 0); |
| 396 |
(*env)->SetIntField(env, packet, packetPortFieldId, ntohs(ipaddr->sin_port)); |
| 397 |
} else { |
| 398 |
int addrLen = netty_unix_socket_ipAddressLength(addr); |
| 399 |
struct sockaddr_in6* ip6addr = (struct sockaddr_in6*) addr; |
| 400 |
|
| 401 |
if (addrLen == 4) { |
| 402 |
// IPV4 mapped IPV6 address |
| 403 |
jbyte* addr = (jbyte*) &ip6addr->sin6_addr.s6_addr; |
| 404 |
(*env)->SetByteArrayRegion(env, address, 0, 4, addr + 12); |
| 405 |
} else { |
| 406 |
(*env)->SetByteArrayRegion(env, address, 0, 16, (jbyte*) &ip6addr->sin6_addr.s6_addr); |
| 407 |
} |
| 408 |
(*env)->SetIntField(env, packet, packetAddrLenFieldId, addrLen); |
| 409 |
(*env)->SetIntField(env, packet, packetScopeIdFieldId, ip6addr->sin6_scope_id); |
| 410 |
(*env)->SetIntField(env, packet, packetPortFieldId, ntohs(ip6addr->sin6_port)); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
return (jint) res; |
| 415 |
} |
| 416 |
|
| 417 |
static jstring netty_epoll_native_kernelVersion(JNIEnv* env, jclass clazz) { |
| 418 |
struct utsname name; |
| 419 |
|
| 420 |
int res = uname(&name); |
| 421 |
if (res == 0) { |
| 422 |
return (*env)->NewStringUTF(env, name.release); |
| 423 |
} |
| 424 |
netty_unix_errors_throwRuntimeExceptionErrorNo(env, "uname() failed: ", errno); |
| 425 |
return NULL; |
| 426 |
} |
| 427 |
static jboolean netty_epoll_native_isSupportingSendmmsg(JNIEnv* env, jclass clazz) { |
| 428 |
if (SYS_sendmmsg == -1) { |
| 429 |
return JNI_FALSE; |
| 430 |
} |
| 431 |
if (syscall(SYS_sendmmsg, -1, NULL, 0, 0) == -1) { |
| 432 |
if (errno == ENOSYS) { |
| 433 |
return JNI_FALSE; |
| 434 |
} |
| 435 |
} |
| 436 |
return JNI_TRUE; |
| 437 |
} |
| 438 |
|
| 439 |
static jboolean netty_epoll_native_isSupportingRecvmmsg(JNIEnv* env, jclass clazz) { |
| 440 |
if (SYS_recvmmsg == -1) { |
| 441 |
return JNI_FALSE; |
| 442 |
} |
| 443 |
if (syscall(SYS_recvmmsg, -1, NULL, 0, 0, NULL) == -1) { |
| 444 |
if (errno == ENOSYS) { |
| 445 |
return JNI_FALSE; |
| 446 |
} |
| 447 |
} |
| 448 |
return JNI_TRUE; |
| 449 |
} |
| 450 |
|
| 451 |
static jboolean netty_epoll_native_isSupportingTcpFastopen(JNIEnv* env, jclass clazz) { |
| 452 |
int fastopen = 0; |
| 453 |
getSysctlValue("/proc/sys/net/ipv4/tcp_fastopen", &fastopen); |
| 454 |
if (fastopen > 0) { |
| 455 |
return JNI_TRUE; |
| 456 |
} |
| 457 |
return JNI_FALSE; |
| 458 |
} |
| 459 |
|
| 460 |
static jint netty_epoll_native_epollet(JNIEnv* env, jclass clazz) { |
| 461 |
return EPOLLET; |
| 462 |
} |
| 463 |
|
| 464 |
static jint netty_epoll_native_epollin(JNIEnv* env, jclass clazz) { |
| 465 |
return EPOLLIN; |
| 466 |
} |
| 467 |
|
| 468 |
static jint netty_epoll_native_epollout(JNIEnv* env, jclass clazz) { |
| 469 |
return EPOLLOUT; |
| 470 |
} |
| 471 |
|
| 472 |
static jint netty_epoll_native_epollrdhup(JNIEnv* env, jclass clazz) { |
| 473 |
return EPOLLRDHUP; |
| 474 |
} |
| 475 |
|
| 476 |
static jint netty_epoll_native_epollerr(JNIEnv* env, jclass clazz) { |
| 477 |
return EPOLLERR; |
| 478 |
} |
| 479 |
|
| 480 |
static jint netty_epoll_native_sizeofEpollEvent(JNIEnv* env, jclass clazz) { |
| 481 |
return sizeof(struct epoll_event); |
| 482 |
} |
| 483 |
|
| 484 |
static jint netty_epoll_native_offsetofEpollData(JNIEnv* env, jclass clazz) { |
| 485 |
return offsetof(struct epoll_event, data); |
| 486 |
} |
| 487 |
|
| 488 |
static jint netty_epoll_native_splice0(JNIEnv* env, jclass clazz, jint fd, jlong offIn, jint fdOut, jlong offOut, jlong len) { |
| 489 |
ssize_t res; |
| 490 |
int err; |
| 491 |
loff_t off_in = (loff_t) offIn; |
| 492 |
loff_t off_out = (loff_t) offOut; |
| 493 |
|
| 494 |
loff_t* p_off_in = off_in >= 0 ? &off_in : NULL; |
| 495 |
loff_t* p_off_out = off_out >= 0 ? &off_out : NULL; |
| 496 |
|
| 497 |
do { |
| 498 |
res = splice(fd, p_off_in, fdOut, p_off_out, (size_t) len, SPLICE_F_NONBLOCK | SPLICE_F_MOVE); |
| 499 |
// keep on splicing if it was interrupted |
| 500 |
} while (res == -1 && ((err = errno) == EINTR)); |
| 501 |
|
| 502 |
if (res < 0) { |
| 503 |
return -err; |
| 504 |
} |
| 505 |
return (jint) res; |
| 506 |
} |
| 507 |
|
| 508 |
static jint netty_epoll_native_tcpMd5SigMaxKeyLen(JNIEnv* env, jclass clazz) { |
| 509 |
struct tcp_md5sig md5sig; |
| 510 |
|
| 511 |
// Defensive size check |
| 512 |
if (sizeof(md5sig.tcpm_key) < TCP_MD5SIG_MAXKEYLEN) { |
| 513 |
return sizeof(md5sig.tcpm_key); |
| 514 |
} |
| 515 |
|
| 516 |
return TCP_MD5SIG_MAXKEYLEN; |
| 517 |
} |
| 518 |
// JNI Registered Methods End |
| 519 |
|
| 520 |
// JNI Method Registration Table Begin |
| 521 |
static const JNINativeMethod statically_referenced_fixed_method_table[] = { |
| 522 |
{ "epollet", "()I", (void *) netty_epoll_native_epollet }, |
| 523 |
{ "epollin", "()I", (void *) netty_epoll_native_epollin }, |
| 524 |
{ "epollout", "()I", (void *) netty_epoll_native_epollout }, |
| 525 |
{ "epollrdhup", "()I", (void *) netty_epoll_native_epollrdhup }, |
| 526 |
{ "epollerr", "()I", (void *) netty_epoll_native_epollerr }, |
| 527 |
{ "tcpMd5SigMaxKeyLen", "()I", (void *) netty_epoll_native_tcpMd5SigMaxKeyLen }, |
| 528 |
{ "isSupportingSendmmsg", "()Z", (void *) netty_epoll_native_isSupportingSendmmsg }, |
| 529 |
{ "isSupportingRecvmmsg", "()Z", (void *) netty_epoll_native_isSupportingRecvmmsg }, |
| 530 |
{ "isSupportingTcpFastopen", "()Z", (void *) netty_epoll_native_isSupportingTcpFastopen }, |
| 531 |
{ "kernelVersion", "()Ljava/lang/String;", (void *) netty_epoll_native_kernelVersion } |
| 532 |
}; |
| 533 |
static const jint statically_referenced_fixed_method_table_size = sizeof(statically_referenced_fixed_method_table) / sizeof(statically_referenced_fixed_method_table[0]); |
| 534 |
static const JNINativeMethod fixed_method_table[] = { |
| 535 |
{ "eventFd", "()I", (void *) netty_epoll_native_eventFd }, |
| 536 |
{ "timerFd", "()I", (void *) netty_epoll_native_timerFd }, |
| 537 |
{ "eventFdWrite", "(IJ)V", (void *) netty_epoll_native_eventFdWrite }, |
| 538 |
{ "eventFdRead", "(I)V", (void *) netty_epoll_native_eventFdRead }, |
| 539 |
{ "timerFdRead", "(I)V", (void *) netty_epoll_native_timerFdRead }, |
| 540 |
{ "timerFdSetTime", "(III)V", (void *) netty_epoll_native_timerFdSetTime }, |
| 541 |
{ "epollCreate", "()I", (void *) netty_epoll_native_epollCreate }, |
| 542 |
{ "epollWait0", "(IJIIII)I", (void *) netty_epoll_native_epollWait0 }, // This method is deprecated! |
| 543 |
{ "epollWait", "(IJII)I", (void *) netty_epoll_native_epollWait }, |
| 544 |
{ "epollBusyWait0", "(IJI)I", (void *) netty_epoll_native_epollBusyWait0 }, |
| 545 |
{ "epollCtlAdd0", "(III)I", (void *) netty_epoll_native_epollCtlAdd0 }, |
| 546 |
{ "epollCtlMod0", "(III)I", (void *) netty_epoll_native_epollCtlMod0 }, |
| 547 |
{ "epollCtlDel0", "(II)I", (void *) netty_epoll_native_epollCtlDel0 }, |
| 548 |
// "sendmmsg0" has a dynamic signature |
| 549 |
{ "sizeofEpollEvent", "()I", (void *) netty_epoll_native_sizeofEpollEvent }, |
| 550 |
{ "offsetofEpollData", "()I", (void *) netty_epoll_native_offsetofEpollData }, |
| 551 |
{ "splice0", "(IJIJJ)I", (void *) netty_epoll_native_splice0 } |
| 552 |
}; |
| 553 |
static const jint fixed_method_table_size = sizeof(fixed_method_table) / sizeof(fixed_method_table[0]); |
| 554 |
|
| 555 |
static jint dynamicMethodsTableSize() { |
| 556 |
return fixed_method_table_size + 2; // 2 is for the dynamic method signatures. |
| 557 |
} |
| 558 |
|
| 559 |
static JNINativeMethod* createDynamicMethodsTable(const char* packagePrefix) { |
| 560 |
char* dynamicTypeName = NULL; |
| 561 |
size_t size = sizeof(JNINativeMethod) * dynamicMethodsTableSize(); |
| 562 |
JNINativeMethod* dynamicMethods = malloc(size); |
| 563 |
if (dynamicMethods == NULL) { |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
memset(dynamicMethods, 0, size); |
| 567 |
memcpy(dynamicMethods, fixed_method_table, sizeof(fixed_method_table)); |
| 568 |
|
| 569 |
JNINativeMethod* dynamicMethod = &dynamicMethods[fixed_method_table_size]; |
| 570 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/epoll/NativeDatagramPacketArray$NativeDatagramPacket;II)I", dynamicTypeName, error); |
| 571 |
NETTY_PREPEND("(IZ[L", dynamicTypeName, dynamicMethod->signature, error); |
| 572 |
dynamicMethod->name = "sendmmsg0"; |
| 573 |
dynamicMethod->fnPtr = (void *) netty_epoll_native_sendmmsg0; |
| 574 |
netty_unix_util_free_dynamic_name(&dynamicTypeName); |
| 575 |
|
| 576 |
++dynamicMethod; |
| 577 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/epoll/NativeDatagramPacketArray$NativeDatagramPacket;II)I", dynamicTypeName, error); |
| 578 |
NETTY_PREPEND("(IZ[L", dynamicTypeName, dynamicMethod->signature, error); |
| 579 |
dynamicMethod->name = "recvmmsg0"; |
| 580 |
dynamicMethod->fnPtr = (void *) netty_epoll_native_recvmmsg0; |
| 581 |
netty_unix_util_free_dynamic_name(&dynamicTypeName); |
| 582 |
|
| 583 |
return dynamicMethods; |
| 584 |
error: |
| 585 |
free(dynamicTypeName); |
| 586 |
netty_unix_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize()); |
| 587 |
return NULL; |
| 588 |
} |
| 589 |
|
| 590 |
// JNI Method Registration Table End |
| 591 |
|
| 592 |
static jint netty_epoll_native_JNI_OnLoad(JNIEnv* env, const char* packagePrefix) { |
| 593 |
int ret = JNI_ERR; |
| 594 |
int limitsOnLoadCalled = 0; |
| 595 |
int errorsOnLoadCalled = 0; |
| 596 |
int filedescriptorOnLoadCalled = 0; |
| 597 |
int socketOnLoadCalled = 0; |
| 598 |
int bufferOnLoadCalled = 0; |
| 599 |
int linuxsocketOnLoadCalled = 0; |
| 600 |
char* nettyClassName = NULL; |
| 601 |
jclass nativeDatagramPacketCls = NULL; |
| 602 |
JNINativeMethod* dynamicMethods = NULL; |
| 603 |
|
| 604 |
// We must register the statically referenced methods first! |
| 605 |
if (netty_unix_util_register_natives(env, |
| 606 |
packagePrefix, |
| 607 |
"io/netty/channel/epoll/NativeStaticallyReferencedJniMethods", |
| 608 |
statically_referenced_fixed_method_table, |
| 609 |
statically_referenced_fixed_method_table_size) != 0) { |
| 610 |
goto done; |
| 611 |
} |
| 612 |
// Register the methods which are not referenced by static member variables |
| 613 |
dynamicMethods = createDynamicMethodsTable(packagePrefix); |
| 614 |
if (dynamicMethods == NULL) { |
| 615 |
goto done; |
| 616 |
} |
| 617 |
|
| 618 |
if (netty_unix_util_register_natives(env, |
| 619 |
packagePrefix, |
| 620 |
"io/netty/channel/epoll/Native", |
| 621 |
dynamicMethods, |
| 622 |
dynamicMethodsTableSize()) != 0) { |
| 623 |
goto done; |
| 624 |
} |
| 625 |
// Load all c modules that we depend upon |
| 626 |
if (netty_unix_limits_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 627 |
goto done; |
| 628 |
} |
| 629 |
limitsOnLoadCalled = 1; |
| 630 |
|
| 631 |
if (netty_unix_errors_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 632 |
goto done; |
| 633 |
} |
| 634 |
errorsOnLoadCalled = 1; |
| 635 |
|
| 636 |
if (netty_unix_filedescriptor_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 637 |
goto done; |
| 638 |
} |
| 639 |
filedescriptorOnLoadCalled = 1; |
| 640 |
|
| 641 |
if (netty_unix_socket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 642 |
goto done; |
| 643 |
} |
| 644 |
socketOnLoadCalled = 1; |
| 645 |
|
| 646 |
if (netty_unix_buffer_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 647 |
goto done; |
| 648 |
} |
| 649 |
bufferOnLoadCalled = 1; |
| 650 |
|
| 651 |
if (netty_epoll_linuxsocket_JNI_OnLoad(env, packagePrefix) == JNI_ERR) { |
| 652 |
goto done; |
| 653 |
} |
| 654 |
linuxsocketOnLoadCalled = 1; |
| 655 |
|
| 656 |
// Initialize this module |
| 657 |
NETTY_PREPEND(packagePrefix, "io/netty/channel/epoll/NativeDatagramPacketArray$NativeDatagramPacket", nettyClassName, done); |
| 658 |
NETTY_FIND_CLASS(env, nativeDatagramPacketCls, nettyClassName, done); |
| 659 |
netty_unix_util_free_dynamic_name(&nettyClassName); |
| 660 |
|
| 661 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetAddrFieldId, "addr", "[B", done); |
| 662 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetAddrLenFieldId, "addrLen", "I", done); |
| 663 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetScopeIdFieldId, "scopeId", "I", done); |
| 664 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetPortFieldId, "port", "I", done); |
| 665 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetMemoryAddressFieldId, "memoryAddress", "J", done); |
| 666 |
NETTY_GET_FIELD(env, nativeDatagramPacketCls, packetCountFieldId, "count", "I", done); |
| 667 |
|
| 668 |
ret = NETTY_JNI_VERSION; |
| 669 |
done: |
| 670 |
|
| 671 |
netty_unix_util_free_dynamic_methods_table(dynamicMethods, fixed_method_table_size, dynamicMethodsTableSize()); |
| 672 |
free(nettyClassName); |
| 673 |
|
| 674 |
if (ret == JNI_ERR) { |
| 675 |
if (limitsOnLoadCalled == 1) { |
| 676 |
netty_unix_limits_JNI_OnUnLoad(env); |
| 677 |
} |
| 678 |
if (errorsOnLoadCalled == 1) { |
| 679 |
netty_unix_errors_JNI_OnUnLoad(env); |
| 680 |
} |
| 681 |
if (filedescriptorOnLoadCalled == 1) { |
| 682 |
netty_unix_filedescriptor_JNI_OnUnLoad(env); |
| 683 |
} |
| 684 |
if (socketOnLoadCalled == 1) { |
| 685 |
netty_unix_socket_JNI_OnUnLoad(env); |
| 686 |
} |
| 687 |
if (bufferOnLoadCalled == 1) { |
| 688 |
netty_unix_buffer_JNI_OnUnLoad(env); |
| 689 |
} |
| 690 |
if (linuxsocketOnLoadCalled == 1) { |
| 691 |
netty_epoll_linuxsocket_JNI_OnUnLoad(env); |
| 692 |
} |
| 693 |
packetAddrFieldId = NULL; |
| 694 |
packetAddrLenFieldId = NULL; |
| 695 |
packetScopeIdFieldId = NULL; |
| 696 |
packetPortFieldId = NULL; |
| 697 |
packetMemoryAddressFieldId = NULL; |
| 698 |
packetCountFieldId = NULL; |
| 699 |
} |
| 700 |
return ret; |
| 701 |
} |
| 702 |
|
| 703 |
static void netty_epoll_native_JNI_OnUnLoad(JNIEnv* env) { |
| 704 |
netty_unix_limits_JNI_OnUnLoad(env); |
| 705 |
netty_unix_errors_JNI_OnUnLoad(env); |
| 706 |
netty_unix_filedescriptor_JNI_OnUnLoad(env); |
| 707 |
netty_unix_socket_JNI_OnUnLoad(env); |
| 708 |
netty_unix_buffer_JNI_OnUnLoad(env); |
| 709 |
netty_epoll_linuxsocket_JNI_OnUnLoad(env); |
| 710 |
|
| 711 |
packetAddrFieldId = NULL; |
| 712 |
packetAddrLenFieldId = NULL; |
| 713 |
packetScopeIdFieldId = NULL; |
| 714 |
packetPortFieldId = NULL; |
| 715 |
packetMemoryAddressFieldId = NULL; |
| 716 |
packetCountFieldId = NULL; |
| 717 |
} |
| 718 |
|
| 719 |
// Invoked by the JVM when statically linked |
| 720 |
static jint JNI_OnLoad_netty_transport_native_epoll0(JavaVM* vm, void* reserved) { |
| 721 |
JNIEnv* env; |
| 722 |
if ((*vm)->GetEnv(vm, (void**) &env, NETTY_JNI_VERSION) != JNI_OK) { |
| 723 |
return JNI_ERR; |
| 724 |
} |
| 725 |
char* packagePrefix = NULL; |
| 726 |
#ifndef NETTY_BUILD_STATIC |
| 727 |
Dl_info dlinfo; |
| 728 |
jint status = 0; |
| 729 |
// We need to use an address of a function that is uniquely part of this library, so choose a static |
| 730 |
// function. See https://github.com/netty/netty/issues/4840. |
| 731 |
if (!dladdr((void*) netty_epoll_native_JNI_OnUnLoad, &dlinfo)) { |
| 732 |
fprintf(stderr, "FATAL: transport-native-epoll JNI call to dladdr failed!\n"); |
| 733 |
return JNI_ERR; |
| 734 |
} |
| 735 |
packagePrefix = netty_unix_util_parse_package_prefix(dlinfo.dli_fname, "netty_transport_native_epoll", &status); |
| 736 |
if (status == JNI_ERR) { |
| 737 |
fprintf(stderr, "FATAL: transport-native-epoll JNI encountered unexpected dlinfo.dli_fname: %s\n", dlinfo.dli_fname); |
| 738 |
return JNI_ERR; |
| 739 |
} |
| 740 |
#endif /* NETTY_BUILD_STATIC */ |
| 741 |
jint ret = netty_epoll_native_JNI_OnLoad(env, packagePrefix); |
| 742 |
|
| 743 |
free(packagePrefix); |
| 744 |
return ret; |
| 745 |
} |
| 746 |
|
| 747 |
static void JNI_OnUnload_netty_transport_native_epoll0(JavaVM* vm, void* reserved) { |
| 748 |
JNIEnv* env; |
| 749 |
if ((*vm)->GetEnv(vm, (void**) &env, NETTY_JNI_VERSION) != JNI_OK) { |
| 750 |
// Something is wrong but nothing we can do about this :( |
| 751 |
return; |
| 752 |
} |
| 753 |
netty_epoll_native_JNI_OnUnLoad(env); |
| 754 |
} |
| 755 |
|
| 756 |
// We build with -fvisibility=hidden so ensure we mark everything that needs to be visible with JNIEXPORT |
| 757 |
// http://mail.openjdk.java.net/pipermail/core-libs-dev/2013-February/014549.html |
| 758 |
|
| 759 |
// Invoked by the JVM when statically linked |
| 760 |
JNIEXPORT jint JNI_OnLoad_netty_transport_native_epoll(JavaVM* vm, void* reserved) { |
| 761 |
return JNI_OnLoad_netty_transport_native_epoll0(vm, reserved); |
| 762 |
} |
| 763 |
|
| 764 |
// Invoked by the JVM when statically linked |
| 765 |
JNIEXPORT void JNI_OnUnload_netty_transport_native_epoll(JavaVM* vm, void* reserved) { |
| 766 |
JNI_OnUnload_netty_transport_native_epoll0(vm, reserved); |
| 767 |
} |
| 768 |
|
| 769 |
#ifndef NETTY_BUILD_STATIC |
| 770 |
JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { |
| 771 |
return JNI_OnLoad_netty_transport_native_epoll0(vm, reserved); |
| 772 |
} |
| 773 |
|
| 774 |
JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved) { |
| 775 |
JNI_OnUnload_netty_transport_native_epoll0(vm, reserved); |
| 776 |
} |
| 777 |
#endif /* NETTY_BUILD_STATIC */ |