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