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     private static final UnsupportedOperationException unsupported;
  38 
  39     private static final Void checkSupported() {
  40         if (unsupported != null)
  41             throw new UnsupportedOperationException(unsupported.getMessage(), unsupported);
  42         else
  43             return null;
  44     }
  45 
  46     static {
  47         java.security.AccessController.doPrivileged(
  48             new java.security.PrivilegedAction<>() {
  49                 public Void run() {
  50                     System.loadLibrary("net");
  51                     System.loadLibrary("extnet");
  52                     return null;
  53                 }
  54             });
  55         UnsupportedOperationException uoe = null;
  56         try {
  57             initProto();
  58         } catch (UnsupportedOperationException e) {
  59             uoe = e;
  60         }
  61         unsupported = uoe;
  62     }
  63 
  64     public LinuxRdmaSocketImpl() {
  65         this(checkSupported());
  66     }
  67 
  68     private LinuxRdmaSocketImpl(Void unused) { }
  69 
  70     static final RdmaSocketOptions rdmaOptions =
  71             RdmaSocketOptions.getInstance();
  72 
  73     private static volatile boolean checkedRdma;
  74     private static volatile boolean isRdmaAvailable;
  75 
  76     boolean isRdmaAvailable() {
  77         if (!checkedRdma) {
  78             isRdmaAvailable = isRdmaAvailable0();
  79             checkedRdma = true;
  80         }
  81         return isRdmaAvailable;
  82     }
  83 
  84     protected synchronized RdmaSocketInputStream getRdmaInputStream(
  85             RdmaSocketImpl impl) throws IOException {
  86         return new RdmaSocketInputStream(impl);
  87     }
  88 
  89     protected synchronized RdmaSocketOutputStream getRdmaOutputStream(
  90             RdmaSocketImpl impl) throws IOException {
  91         return new RdmaSocketOutputStream(impl);
  92     }
  93 
  94     protected void rdmaSocketShutdownInput(int howto, RdmaSocketImpl impl,
  95             InputStream socketInputStream) throws IOException {
  96         rdmaSocketShutdown(howto, impl);
  97         if (socketInputStream != null) {
  98             ((RdmaSocketInputStream)socketInputStream).setEOF(true);
  99         }
 100     }
 101 
 102     static native void initProto() throws UnsupportedOperationException;
 103 
 104     private static native boolean isRdmaAvailable0();
 105 
 106     native void rdmaSocketCreate(RdmaSocketImpl impl) throws IOException;
 107 
 108     native void rdmaSocketConnect(RdmaSocketImpl impl, InetAddress address, int port, int timeout)
 109         throws IOException;
 110 
 111     native void rdmaSocketBind(RdmaSocketImpl impl, InetAddress address, int port)
 112         throws IOException;
 113 
 114     native void rdmaSocketListen(RdmaSocketImpl impl, int count) throws IOException;
 115 
 116     native void rdmaSocketAccept(SocketImpl impl, RdmaSocketImpl s) throws IOException;
 117 
 118     native int rdmaSocketAvailable(RdmaSocketImpl impl) throws IOException;
 119 
 120     native void rdmaSocketClose(boolean useDeferredClose, RdmaSocketImpl impl) throws IOException;
 121 
 122     native void rdmaSocketShutdown(int howto, RdmaSocketImpl impl) throws IOException;
 123 
 124     native void rdmaSocketSetOption(RdmaSocketImpl impl, int cmd, boolean on, Object value)
 125         throws SocketException;
 126 
 127     native int rdmaSocketGetOption(RdmaSocketImpl impl, int opt, Object iaContainerObj) throws SocketException;
 128 
 129     native void rdmaSocketSendUrgentData(RdmaSocketImpl impl, int data) throws IOException;
 130 }