src/java.base/share/classes/sun/net/ext/RdmaSocketOptions.java
author bpb
Thu, 07 Feb 2019 11:09:09 -0800
branchrsocket-branch
changeset 57160 c502c299d41e
parent 57156 81e4a12fd1a4
permissions -rw-r--r--
rsocket-branch: correct copyright year
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     1
/*
57160
c502c299d41e rsocket-branch: correct copyright year
bpb
parents: 57156
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
57115
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     4
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    10
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    16
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    20
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    23
 * questions.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    24
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    25
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    26
package sun.net.ext;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    27
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    28
import java.io.FileDescriptor;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    29
import java.net.SocketException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    30
import java.net.SocketOption;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    31
import java.util.Collections;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    32
import java.util.Set;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    33
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    34
/**
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    35
 * Defines the infrastructure to support RDMA socket options.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    36
 *
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    37
 * RDMA socket options are accessed through the jdk.net API, which is in
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    38
 * the jdk.net module.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    39
 */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    40
public abstract class RdmaSocketOptions {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    41
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    42
    private final Set<SocketOption<?>> options;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    43
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    44
    /** Tells whether or not the option is supported. */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    45
    public final boolean isOptionSupported(SocketOption<?> option) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    46
        return options().contains(option);
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    47
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    48
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    49
    /** Return the, possibly empty, set of RDMA socket options available. */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    50
    public final Set<SocketOption<?>> options() { return options; }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    51
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    52
    /** Sets the value of a socket option, for the given socket. */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    53
    public abstract void setOption(FileDescriptor fd, SocketOption<?> option,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    54
            Object value) throws SocketException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    55
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    56
    /** Returns the value of a socket option, for the given socket. */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    57
    public abstract Object getOption(FileDescriptor fd, SocketOption<?> option)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    58
            throws SocketException;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    59
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    60
    protected RdmaSocketOptions(Set<SocketOption<?>> options) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    61
        this.options = options;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    62
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    63
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    64
    private static volatile RdmaSocketOptions instance;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    65
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    66
    public static final RdmaSocketOptions getInstance() { return instance; }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    67
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    68
    /** Registers support for RDMA socket options. Invoked by the jdk.net module. */
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    69
    public static final void register(RdmaSocketOptions rdmaOptions) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    70
        if (instance != null)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    71
            throw new InternalError("Attempting to reregister RDMA options");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    72
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    73
        instance = rdmaOptions;
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    74
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    75
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    76
    static {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    77
        try {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    78
            // If the class is present, it will be initialized which
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    79
            // triggers registration of the RDMA socket options.
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    80
            Class<?> c = Class.forName("jdk.net.RdmaSocketOptions");
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    81
        } catch (ClassNotFoundException e) {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    82
            // the jdk.net module is not present => no RDMA socket options
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    83
            instance = new NoRdmaSocketOptions();
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    84
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    85
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    86
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    87
    static final class NoRdmaSocketOptions extends RdmaSocketOptions {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    88
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    89
        NoRdmaSocketOptions() {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    90
            super(Collections.<SocketOption<?>>emptySet());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    91
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    92
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    93
        @Override
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    94
        public void setOption(FileDescriptor fd, SocketOption<?> option,
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    95
                Object value) throws SocketException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    96
        {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    97
            throw new UnsupportedOperationException(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    98
                    "no RDMA options: " + option.name());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
    99
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   100
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   101
        @Override
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   102
        public Object getOption(FileDescriptor fd, SocketOption<?> option)
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   103
            throws SocketException
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   104
        {
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   105
            throw new UnsupportedOperationException(
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   106
                    "no RDMA options: " + option.name());
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   107
        }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   108
    }
512e7cc6ccce Initial load of JEP 337 implementation
alanb
parents:
diff changeset
   109
}