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