src/jdk.net/linux/native/libextnet/RdmaSocketChannelImpl.c
author alanb
Sat, 26 Jan 2019 14:02:35 +0000
branchrsocket-branch
changeset 57115 512e7cc6ccce
child 57156 81e4a12fd1a4
permissions -rw-r--r--
Initial load of JEP 337 implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
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
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
#include "jdk_internal_net_rdma_RdmaSocketChannelImpl.h"
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
#include "nio_util.h"
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
#include "nio.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
JNIEXPORT void JNICALL
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
Java_jdk_internal_net_rdma_RdmaSocketChannelImpl_initIDs(JNIEnv *env,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
        jclass clazz) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
    loadRdmaFuncs(env);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
    CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor"));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
    CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I"));
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
jint fdVal(JNIEnv *env, jobject fdo) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
    return (*env)->GetIntField(env, fdo, fd_fdID);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
JNIEXPORT jint JNICALL
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
Java_jdk_internal_net_rdma_RdmaSocketChannelImpl_checkConnect(JNIEnv *env,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
        jobject this, jobject fdo, jboolean block) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
    int error = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    socklen_t n = sizeof(int);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
    jint fd = fdVal(env, fdo);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
    int result = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    struct pollfd poller;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
    poller.fd = fd;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
    poller.events = POLLOUT;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
    poller.revents = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
    result = rs_poll(&poller, 1, block ? -1 : 0);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
    if (result < 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
        if (errno == EINTR) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
            return IOS_INTERRUPTED;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
        } else {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
            JNU_ThrowIOExceptionWithLastError(env, "rpoll failed");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
            return IOS_THROWN;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
    if (!block && (result == 0))
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
        return IOS_UNAVAILABLE;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
    if (result > 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
        errno = 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        result = rs_getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &n);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
        if (result < 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
            return handleSocketError(env, errno);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
        } else if (error) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
            return handleSocketError(env, error);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
        } else if ((poller.revents & POLLHUP) != 0) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
            return handleSocketError(env, ENOTCONN);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
        // connected
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
        return 1;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
    return 0;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
}
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
JNIEXPORT jint JNICALL
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
Java_jdk_internal_net_rdma_RdmaSocketChannelImpl_sendOutOfBandData(JNIEnv* env,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        jclass this, jobject fdo, jbyte b) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
    int n = rs_send(fdVal(env, fdo), (const void*)&b, 1, MSG_OOB);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
    return convertReturnVal(env, n, JNI_FALSE);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
}