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 
  27 #include "jdk_net_LinuxRdmaSocketOptions.h"
  28 #include "rdma_util_md.h"
  29 #include "Rsocket.h"
  30 #include "jni_util.h"
  31 
  32 void throwByNameWithLastError(JNIEnv *env, const char *name,
  33         const char *defaultDetail) {
  34   char defaultMsg[255];
  35   sprintf(defaultMsg, "errno: %d, %s", errno, defaultDetail);
  36   JNU_ThrowByNameWithLastError(env, name, defaultMsg);
  37 }
  38 
  39 JNIEXPORT void JNICALL
  40 Java_jdk_net_LinuxRdmaSocketOptions_init(JNIEnv *env, jclass c) {
  41    loadRdmaFuncs(env);
  42 }
  43  
  44 JNIEXPORT void JNICALL
  45 Java_jdk_net_LinuxRdmaSocketOptions_setSockOpt(JNIEnv *env,
  46         jobject unused, jint fd, jint opt, jint value) {
  47     int optname;
  48     int level = SOL_RDMA;
  49     RDMA_MapSocketOption(opt, &level, &optname);
  50     int len = sizeof(value);
  51     int rv = RDMA_SetSockOpt(fd, level, optname, (char*)&value, len);    
  52     if (rv < 0) {
  53         if (errno == ENOPROTOOPT) {
  54             JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
  55                             "unsupported RDMA socket option");
  56         } else if (errno == EACCES || errno == EPERM) {
  57             JNU_ThrowByName(env, "java/net/SocketException", "Permission denied");
  58         } else {
  59             throwByNameWithLastError(env, "java/net/SocketException",
  60                                      "set RDMA option failed");
  61         }
  62     }
  63 }
  64 
  65 JNIEXPORT jint JNICALL
  66 Java_jdk_net_LinuxRdmaSocketOptions_getSockOpt(JNIEnv *env,
  67         jobject unused, jint fd, jint opt) {
  68     int optname;
  69     int level = SOL_RDMA;
  70     RDMA_MapSocketOption(opt, &level, &optname);
  71     int value;
  72     int len = sizeof(value);
  73     int rv = RDMA_GetSockOpt(fd, level, optname, (char*)&value, &len);
  74 
  75     if (rv < 0) {
  76         if (errno == ENOPROTOOPT) {
  77             JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
  78                             "unsupported RDMA socket option");
  79         } else if (errno == EACCES || errno == EPERM) {
  80             JNU_ThrowByName(env, "java/net/SocketException", "Permission denied");
  81         } else {
  82             throwByNameWithLastError(env, "java/net/SocketException",
  83                                      "get RDMA option failed");
  84         }
  85         return -1;
  86     }
  87     return value;
  88 }
  89 
  90 JNIEXPORT jboolean JNICALL
  91 Java_jdk_net_LinuxRdmaSocketOptions_rdmaSocketSupported(JNIEnv *env,
  92         jobject unused) {
  93     int rv, s;
  94 
  95     s = rs_socket(PF_INET, SOCK_STREAM, 0);
  96     if (s < 0) {
  97         return JNI_FALSE;
  98     }
  99     rs_close(s);
 100     return JNI_TRUE;
 101 }