src/jdk.net/linux/native/libextnet/RdmaSocketDispatcherImpl.c
author bpb
Wed, 06 Feb 2019 12:48:01 -0800
branchrsocket-branch
changeset 57156 81e4a12fd1a4
parent 57132 fe6629ef9418
child 57160 c502c299d41e
permissions -rw-r--r--
rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57156
81e4a12fd1a4 rsocket-branch: change recent copyright year to 2019 and new files to 2019 only
bpb
parents: 57132
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
    26
#include "jdk_internal_net_rdma_RdmaSocketDispatcherImpl.h"
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
#include "nio.h"
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
#include "nio_util.h"
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
#include <Rsocket.h>
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
static jfieldID fd_fdID;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
jint
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
convertReturnVal(JNIEnv *env, jint n, jboolean reading) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
    if (n > 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
        return n;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
    else if (n == 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
        if (reading) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
            return IOS_EOF;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
            return 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
    else if (errno == EAGAIN)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
        return IOS_UNAVAILABLE;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
    else if (errno == EINTR)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
        return IOS_INTERRUPTED;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
    else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
        const char *msg = reading ? "Read failed" : "Write failed";
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
        JNU_ThrowIOExceptionWithLastError(env, msg);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
        return IOS_THROWN;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
jlong
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
    if (n > 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
        return n;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
    else if (n == 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
        if (reading) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
            return IOS_EOF;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
            return 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
    else if (errno == EAGAIN)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
        return IOS_UNAVAILABLE;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
    else if (errno == EINTR)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
        return IOS_INTERRUPTED;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
    else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
        const char *msg = reading ? "Read failed" : "Write failed";
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
        JNU_ThrowIOExceptionWithLastError(env, msg);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        return IOS_THROWN;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
JNIEXPORT void JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
    78
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_init(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
        jclass cl) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
    loadRdmaFuncs(env);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
    CHECK_NULL(cl = (*env)->FindClass(env, "java/io/FileDescriptor"));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
    CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, cl, "fd", "I"));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
JNIEXPORT jint JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
    86
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_read0(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
        jclass clazz, jobject fdo, jlong address, jint len) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
    jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
    void *buf = (void *)jlong_to_ptr(address);
57129
1e1db86ea836 rsocket-branch: Review comments from Brian, A, B, and D
chegar
parents: 57115
diff changeset
    90
    return convertReturnVal(env, rs_read(fd, buf, len), JNI_TRUE);
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
JNIEXPORT jlong JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
    94
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_readv0(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
        jclass clazz, jobject fdo, jlong address, jint len) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
    jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
    struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
57129
1e1db86ea836 rsocket-branch: Review comments from Brian, A, B, and D
chegar
parents: 57115
diff changeset
    98
    return convertLongReturnVal(env, rs_readv(fd, iov, len), JNI_TRUE);
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
JNIEXPORT jint JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
   102
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_write0(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
        jclass clazz, jobject fdo, jlong address, jint len) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
    jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
    void *buf = (void *)jlong_to_ptr(address);
57129
1e1db86ea836 rsocket-branch: Review comments from Brian, A, B, and D
chegar
parents: 57115
diff changeset
   106
    return convertReturnVal(env, rs_write(fd, buf, len), JNI_FALSE);
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
JNIEXPORT jlong JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
   110
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_writev0(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   111
        jclass clazz, jobject fdo, jlong address, jint len) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   112
    jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   113
    struct iovec *iov = (struct iovec *)jlong_to_ptr(address);
57129
1e1db86ea836 rsocket-branch: Review comments from Brian, A, B, and D
chegar
parents: 57115
diff changeset
   114
    return convertLongReturnVal(env, rs_writev(fd, iov, len), JNI_FALSE);
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   115
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   116
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   117
static void closeFileDescriptor(JNIEnv *env, int fd) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   118
    if (fd != -1) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   119
        int result = rs_close(fd);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   120
        if (result < 0)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   121
            JNU_ThrowIOExceptionWithLastError(env, "Close failed");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   122
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   123
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   124
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   125
JNIEXPORT void JNICALL
57130
3e1ebe33554c rsocket-branch: Review comment C from Brian - rename dispatcher
chegar
parents: 57129
diff changeset
   126
Java_jdk_internal_net_rdma_RdmaSocketDispatcherImpl_close0(JNIEnv *env,
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   127
        jclass clazz, jobject fdo) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   128
    jint fd = (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   129
    closeFileDescriptor(env, fd);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   130
}