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 package jdk.internal.net.rdma;
  26 
  27 import java.io.IOException;
  28 import java.io.InputStream;
  29 import java.net.SocketException;
  30 import java.net.InetAddress;
  31 import java.net.SocketImpl;
  32 import sun.net.ext.RdmaSocketOptions;
  33 import static jdk.internal.net.rdma.RdmaSocketImpl.PlatformRdmaSocketImpl;
  34 
  35 class LinuxRdmaSocketImpl extends PlatformRdmaSocketImpl
  36 {
  37     static {
  38         java.security.AccessController.doPrivileged(
  39             new java.security.PrivilegedAction<>() {
  40                 public Void run() {
  41                     System.loadLibrary("net");
  42                     System.loadLibrary("extnet");
  43                     return null;
  44                 }
  45             });
  46         initProto();
  47     }
  48 
  49     public LinuxRdmaSocketImpl() { }
  50 
  51     static final RdmaSocketOptions rdmaOptions =
  52             RdmaSocketOptions.getInstance();
  53 
  54     private static volatile boolean checkedRdma;
  55     private static volatile boolean isRdmaAvailable;
  56 
  57     boolean isRdmaAvailable() {
  58         if (!checkedRdma) {
  59             isRdmaAvailable = isRdmaAvailable0();
  60             checkedRdma = true;
  61         }
  62         return isRdmaAvailable;
  63     }
  64 
  65     protected synchronized RdmaSocketInputStream getRdmaInputStream(
  66             RdmaSocketImpl impl) throws IOException {
  67         return new RdmaSocketInputStream(impl);
  68     }
  69 
  70     protected synchronized RdmaSocketOutputStream getRdmaOutputStream(
  71             RdmaSocketImpl impl) throws IOException {
  72         return new RdmaSocketOutputStream(impl);
  73     }
  74 
  75     protected void rdmaSocketShutdownInput(int howto, RdmaSocketImpl impl,
  76             InputStream socketInputStream) throws IOException {
  77         rdmaSocketShutdown(howto, impl);
  78         if (socketInputStream != null) {
  79             ((RdmaSocketInputStream)socketInputStream).setEOF(true);
  80         }
  81     }
  82 
  83     static native void initProto();
  84 
  85     private static native boolean isRdmaAvailable0();
  86 
  87     native void rdmaSocketCreate(boolean isServer, RdmaSocketImpl impl) throws IOException;
  88 
  89     native void rdmaSocketConnect(RdmaSocketImpl impl, InetAddress address, int port, int timeout)
  90         throws IOException;
  91 
  92     native void rdmaSocketBind(RdmaSocketImpl impl, InetAddress address, int port)
  93         throws IOException;
  94 
  95     native void rdmaSocketListen(RdmaSocketImpl impl, int count) throws IOException;
  96 
  97     native void rdmaSocketAccept(SocketImpl impl, RdmaSocketImpl s) throws IOException;
  98 
  99     native int rdmaSocketAvailable(RdmaSocketImpl impl) throws IOException;
 100 
 101     native void rdmaSocketClose(boolean useDeferredClose, RdmaSocketImpl impl) throws IOException;
 102 
 103     native void rdmaSocketShutdown(int howto, RdmaSocketImpl impl) throws IOException;
 104 
 105     native void rdmaSocketSetOption(RdmaSocketImpl impl, int cmd, boolean on, Object value)
 106         throws SocketException;
 107 
 108     native int rdmaSocketGetOption(RdmaSocketImpl impl, int opt, Object iaContainerObj) throws SocketException;
 109 
 110     native void rdmaSocketSendUrgentData(RdmaSocketImpl impl, int data) throws IOException;
 111 }