8194298: Add support for per Socket configuration of TCP keepalive
Reviewed-by: chegar, clanger, igerasim, alanb
--- a/make/lib/Lib-jdk.net.gmk Wed May 30 08:52:59 2018 +0200
+++ b/make/lib/Lib-jdk.net.gmk Wed May 30 16:36:35 2018 +0530
@@ -27,7 +27,7 @@
################################################################################
-ifneq ($(filter $(OPENJDK_TARGET_OS), solaris linux), )
+ifneq ($(filter $(OPENJDK_TARGET_OS), solaris linux macosx), )
$(eval $(call SetupJdkLibrary, BUILD_LIBEXTNET, \
NAME := extnet, \
--- a/src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/net/ext/ExtendedSocketOptions.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
import java.net.SocketOption;
import java.util.Collections;
import java.util.Set;
+import java.util.stream.Collectors;
/**
* Defines the infrastructure to support extended socket options, beyond those
@@ -40,6 +41,9 @@
*/
public abstract class ExtendedSocketOptions {
+ public static final short SOCK_STREAM = 1;
+ public static final short SOCK_DGRAM = 2;
+
private final Set<SocketOption<?>> options;
/** Tells whether or not the option is supported. */
@@ -50,6 +54,30 @@
/** Return the, possibly empty, set of extended socket options available. */
public final Set<SocketOption<?>> options() { return options; }
+ public static final Set<SocketOption<?>> options(short type) {
+ return getInstance().options0(type);
+ }
+
+ private Set<SocketOption<?>> options0(short type) {
+ Set<SocketOption<?>> extOptions = null;
+ switch (type) {
+ case SOCK_DGRAM:
+ extOptions = options.stream()
+ .filter((option) -> !option.name().startsWith("TCP_"))
+ .collect(Collectors.toUnmodifiableSet());
+ break;
+ case SOCK_STREAM:
+ extOptions = options.stream()
+ .filter((option) -> !option.name().startsWith("UDP_"))
+ .collect(Collectors.toUnmodifiableSet());
+ break;
+ default:
+ //this will never happen
+ throw new IllegalArgumentException("Invalid socket option type");
+ }
+ return extOptions;
+ }
+
/** Sets the value of a socket option, for the given socket. */
public abstract void setOption(FileDescriptor fd, SocketOption<?> option, Object value)
throws SocketException;
--- a/src/java.base/share/classes/sun/nio/ch/AsynchronousServerSocketChannelImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/nio/ch/AsynchronousServerSocketChannelImpl.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -39,6 +39,8 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import sun.net.NetHooks;
+import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/**
* Base implementation of AsynchronousServerSocketChannel.
@@ -234,6 +236,7 @@
if (Net.isReusePortAvailable()) {
set.add(StandardSocketOptions.SO_REUSEPORT);
}
+ set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}
--- a/src/java.base/share/classes/sun/nio/ch/AsynchronousSocketChannelImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/nio/ch/AsynchronousSocketChannelImpl.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -40,6 +40,7 @@
import java.util.concurrent.locks.*;
import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/**
* Base implementation of AsynchronousSocketChannel
@@ -512,9 +513,7 @@
set.add(StandardSocketOptions.SO_REUSEPORT);
}
set.add(StandardSocketOptions.TCP_NODELAY);
- ExtendedSocketOptions extendedOptions =
- ExtendedSocketOptions.getInstance();
- set.addAll(extendedOptions.options());
+ set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}
--- a/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/nio/ch/DatagramChannelImpl.java Wed May 30 16:36:35 2018 +0530
@@ -57,6 +57,7 @@
import sun.net.ResourceManager;
import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_DGRAM;
/**
* An implementation of DatagramChannels.
@@ -334,7 +335,7 @@
set.add(StandardSocketOptions.IP_MULTICAST_IF);
set.add(StandardSocketOptions.IP_MULTICAST_TTL);
set.add(StandardSocketOptions.IP_MULTICAST_LOOP);
- set.addAll(ExtendedSocketOptions.getInstance().options());
+ set.addAll(ExtendedSocketOptions.options(SOCK_DGRAM));
return Collections.unmodifiableSet(set);
}
}
--- a/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/nio/ch/ServerSocketChannelImpl.java Wed May 30 16:36:35 2018 +0530
@@ -49,6 +49,8 @@
import java.util.concurrent.locks.ReentrantLock;
import sun.net.NetHooks;
+import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/**
* An implementation of ServerSocketChannels
@@ -199,6 +201,7 @@
set.add(StandardSocketOptions.SO_REUSEPORT);
}
set.add(StandardSocketOptions.IP_TOS);
+ set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}
--- a/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/share/classes/sun/nio/ch/SocketChannelImpl.java Wed May 30 16:36:35 2018 +0530
@@ -54,6 +54,7 @@
import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/**
* An implementation of SocketChannels
@@ -280,7 +281,7 @@
// additional options required by socket adaptor
set.add(StandardSocketOptions.IP_TOS);
set.add(ExtendedSocketOption.SO_OOBINLINE);
- set.addAll(ExtendedSocketOptions.getInstance().options());
+ set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}
--- a/src/java.base/unix/classes/java/net/PlainDatagramSocketImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/unix/classes/java/net/PlainDatagramSocketImpl.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -28,6 +28,7 @@
import java.util.Set;
import java.util.HashSet;
import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_DGRAM;
/*
* On Unix systems we simply delegate to native methods.
@@ -77,18 +78,10 @@
protected Set<SocketOption<?>> supportedOptions() {
HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions());
- addExtSocketOptions(extendedOptions.options(), options);
+ options.addAll(ExtendedSocketOptions.options(SOCK_DGRAM));
return options;
}
- private void addExtSocketOptions(Set<SocketOption<?>> extOptions,
- Set<SocketOption<?>> options) {
- // TCP_QUICKACK is applicable for TCP Sockets only.
- extOptions.stream()
- .filter((option) -> !option.name().equals("TCP_QUICKACK"))
- .forEach((option) -> options.add(option));
- }
-
protected void socketSetOption(int opt, Object val) throws SocketException {
if (opt == SocketOptions.SO_REUSEPORT &&
!supportedOptions().contains(StandardSocketOptions.SO_REUSEPORT)) {
--- a/src/java.base/unix/classes/java/net/PlainSocketImpl.java Wed May 30 08:52:59 2018 +0200
+++ b/src/java.base/unix/classes/java/net/PlainSocketImpl.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
import java.util.Set;
import java.util.HashSet;
import sun.net.ext.ExtendedSocketOptions;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/*
* On Unix systems we simply delegate to native methods.
@@ -90,7 +91,7 @@
protected Set<SocketOption<?>> supportedOptions() {
HashSet<SocketOption<?>> options = new HashSet<>(super.supportedOptions());
- addExtSocketOptions(extendedOptions.options(), options);
+ addExtSocketOptions(ExtendedSocketOptions.options(SOCK_STREAM), options);
return options;
}
--- a/src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java Wed May 30 08:52:59 2018 +0200
+++ b/src/jdk.net/linux/classes/jdk/net/LinuxSocketOptions.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -49,16 +49,55 @@
return quickAckSupported0();
}
- native static private void setQuickAck0(int fd, boolean on) throws SocketException;
+ @Override
+ boolean keepAliveOptionsSupported() {
+ return keepAliveOptionsSupported0();
+ }
+
+ @Override
+ void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
+ setTcpkeepAliveProbes0(fd, value);
+ }
+
+ @Override
+ void setTcpKeepAliveTime(int fd, final int value) throws SocketException {
+ setTcpKeepAliveTime0(fd, value);
+ }
+
+ @Override
+ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
+ setTcpKeepAliveIntvl0(fd, value);
+ }
- native static private boolean getQuickAck0(int fd) throws SocketException;
+ @Override
+ int getTcpkeepAliveProbes(int fd) throws SocketException {
+ return getTcpkeepAliveProbes0(fd);
+ }
+
+ @Override
+ int getTcpKeepAliveTime(int fd) throws SocketException {
+ return getTcpKeepAliveTime0(fd);
+ }
- native static private boolean quickAckSupported0();
+ @Override
+ int getTcpKeepAliveIntvl(int fd) throws SocketException {
+ return getTcpKeepAliveIntvl0(fd);
+ }
+ private static native void setTcpkeepAliveProbes0(int fd, int value) throws SocketException;
+ private static native void setTcpKeepAliveTime0(int fd, int value) throws SocketException;
+ private static native void setTcpKeepAliveIntvl0(int fd, int value) throws SocketException;
+ private static native int getTcpkeepAliveProbes0(int fd) throws SocketException;
+ private static native int getTcpKeepAliveTime0(int fd) throws SocketException;
+ private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;
+ private static native void setQuickAck0(int fd, boolean on) throws SocketException;
+ private static native boolean getQuickAck0(int fd) throws SocketException;
+ private static native boolean keepAliveOptionsSupported0();
+ private static native boolean quickAckSupported0();
static {
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
System.loadLibrary("extnet");
return null;
});
}
-}
+}
\ No newline at end of file
--- a/src/jdk.net/linux/native/libextnet/LinuxSocketOptions.c Wed May 30 08:52:59 2018 +0200
+++ b/src/jdk.net/linux/native/libextnet/LinuxSocketOptions.c Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -29,6 +29,7 @@
#include <jni.h>
#include <netinet/tcp.h>
+#include <netinet/in.h>
#include "jni_util.h"
/*
@@ -97,3 +98,117 @@
close(s);
return rv;
}
+
+static jint socketOptionSupported(jint sockopt) {
+ jint one = 1;
+ jint rv, s;
+ s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (s < 0) {
+ return 0;
+ }
+ rv = setsockopt(s, SOL_TCP, sockopt, (void *) &one, sizeof (one));
+ if (rv != 0 && errno == ENOPROTOOPT) {
+ rv = 0;
+ } else {
+ rv = 1;
+ }
+ close(s);
+ return rv;
+}
+
+static void handleError(JNIEnv *env, jint rv, const char *errmsg) {
+ if (rv < 0) {
+ if (errno == ENOPROTOOPT) {
+ JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
+ "unsupported socket option");
+ } else {
+ JNU_ThrowByNameWithLastError(env, "java/net/SocketException", errmsg);
+ }
+ }
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: keepAliveOptionsSupported0
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL Java_jdk_net_LinuxSocketOptions_keepAliveOptionsSupported0
+(JNIEnv *env, jobject unused) {
+ return socketOptionSupported(TCP_KEEPIDLE) && socketOptionSupported(TCP_KEEPCNT)
+ && socketOptionSupported(TCP_KEEPINTVL);
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: setTcpkeepAliveProbes0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpkeepAliveProbes0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPCNT, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPCNT failed");
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: setTcpKeepAliveTime0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveTime0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPIDLE failed");
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: setTcpKeepAliveIntvl0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_LinuxSocketOptions_setTcpKeepAliveIntvl0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPINTVL failed");
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: getTcpkeepAliveProbes0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpkeepAliveProbes0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, SOL_TCP, TCP_KEEPCNT, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPCNT failed");
+ return optval;
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: getTcpKeepAliveTime0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpKeepAliveTime0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, SOL_TCP, TCP_KEEPIDLE, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPIDLE failed");
+ return optval;
+}
+
+/*
+ * Class: jdk_net_LinuxSocketOptions
+ * Method: getTcpKeepAliveIntvl0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_LinuxSocketOptions_getTcpKeepAliveIntvl0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, SOL_TCP, TCP_KEEPINTVL, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPINTVL failed");
+ return optval;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.net/macosx/classes/jdk/net/MacOSXSocketOptions.java Wed May 30 16:36:35 2018 +0530
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package jdk.net;
+
+import java.net.SocketException;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import jdk.net.ExtendedSocketOptions.PlatformSocketOptions;
+
+class MacOSXSocketOptions extends PlatformSocketOptions {
+
+ public MacOSXSocketOptions() {
+ }
+
+ @Override
+ boolean keepAliveOptionsSupported() {
+ return keepAliveOptionsSupported0();
+ }
+
+ @Override
+ void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
+ setTcpkeepAliveProbes0(fd, value);
+ }
+
+ @Override
+ void setTcpKeepAliveTime(int fd, final int value) throws SocketException {
+ setTcpKeepAliveTime0(fd, value);
+ }
+
+ @Override
+ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
+ setTcpKeepAliveIntvl0(fd, value);
+ }
+
+ @Override
+ int getTcpkeepAliveProbes(int fd) throws SocketException {
+ return getTcpkeepAliveProbes0(fd);
+ }
+
+ @Override
+ int getTcpKeepAliveTime(int fd) throws SocketException {
+ return getTcpKeepAliveTime0(fd);
+ }
+
+ @Override
+ int getTcpKeepAliveIntvl(int fd) throws SocketException {
+ return getTcpKeepAliveIntvl0(fd);
+ }
+
+ private static native void setTcpkeepAliveProbes0(int fd, int value) throws SocketException;
+ private static native void setTcpKeepAliveTime0(int fd, int value) throws SocketException;
+ private static native void setTcpKeepAliveIntvl0(int fd, int value) throws SocketException;
+ private static native int getTcpkeepAliveProbes0(int fd) throws SocketException;
+ private static native int getTcpKeepAliveTime0(int fd) throws SocketException;
+ private static native int getTcpKeepAliveIntvl0(int fd) throws SocketException;
+ private static native boolean keepAliveOptionsSupported0();
+ static {
+ AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
+ System.loadLibrary("extnet");
+ return null;
+ });
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/jdk.net/macosx/native/libextnet/MacOSXSocketOptions.c Wed May 30 16:36:35 2018 +0530
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+#include <sys/socket.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <jni.h>
+#include <netinet/tcp.h>
+#include <netinet/in.h>
+#include "jni_util.h"
+
+static jint socketOptionSupported(jint sockopt) {
+ jint one = 1;
+ jint rv, s;
+ s = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
+ if (s < 0) {
+ return 0;
+ }
+ rv = setsockopt(s, IPPROTO_TCP, sockopt, (void *) &one, sizeof (one));
+ if (rv != 0 && errno == ENOPROTOOPT) {
+ rv = 0;
+ } else {
+ rv = 1;
+ }
+ close(s);
+ return rv;
+}
+
+static void handleError(JNIEnv *env, jint rv, const char *errmsg) {
+ if (rv < 0) {
+ if (errno == ENOPROTOOPT) {
+ JNU_ThrowByName(env, "java/lang/UnsupportedOperationException",
+ "unsupported socket option");
+ } else {
+ JNU_ThrowByNameWithLastError(env, "java/net/SocketException", errmsg);
+ }
+ }
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: keepAliveOptionsSupported0
+ * Signature: ()Z
+ */
+JNIEXPORT jboolean JNICALL Java_jdk_net_MacOSXSocketOptions_keepAliveOptionsSupported0
+(JNIEnv *env, jobject unused) {
+ return socketOptionSupported(TCP_KEEPALIVE) && socketOptionSupported(TCP_KEEPCNT)
+ && socketOptionSupported(TCP_KEEPINTVL);
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: setTcpkeepAliveProbes0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpkeepAliveProbes0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPCNT failed");
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: setTcpKeepAliveTime0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpKeepAliveTime0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPALIVE failed");// mac TCP_KEEPIDLE ->TCP_KEEPALIVE
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: setTcpKeepAliveIntvl0
+ * Signature: (II)V
+ */
+JNIEXPORT void JNICALL Java_jdk_net_MacOSXSocketOptions_setTcpKeepAliveIntvl0
+(JNIEnv *env, jobject unused, jint fd, jint optval) {
+ jint rv = setsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, sizeof (optval));
+ handleError(env, rv, "set option TCP_KEEPINTVL failed");
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: getTcpkeepAliveProbes0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_MacOSXSocketOptions_getTcpkeepAliveProbes0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, IPPROTO_TCP, TCP_KEEPCNT, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPCNT failed");
+ return optval;
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: getTcpKeepAliveTime0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_MacOSXSocketOptions_getTcpKeepAliveTime0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, IPPROTO_TCP, TCP_KEEPALIVE, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPALIVE failed");// mac TCP_KEEPIDLE ->TCP_KEEPALIVE
+ return optval;
+}
+
+/*
+ * Class: jdk_net_MacOSXSocketOptions
+ * Method: getTcpKeepAliveIntvl0
+ * Signature: (I)I;
+ */
+JNIEXPORT jint JNICALL Java_jdk_net_MacOSXSocketOptions_getTcpKeepAliveIntvl0
+(JNIEnv *env, jobject unused, jint fd) {
+ jint optval, rv;
+ socklen_t sz = sizeof (optval);
+ rv = getsockopt(fd, IPPROTO_TCP, TCP_KEEPINTVL, &optval, &sz);
+ handleError(env, rv, "get option TCP_KEEPINTVL failed");
+ return optval;
+}
--- a/src/jdk.net/share/classes/jdk/net/ExtendedSocketOptions.java Wed May 30 08:52:59 2018 +0200
+++ b/src/jdk.net/share/classes/jdk/net/ExtendedSocketOptions.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collections;
+import java.util.HashSet;
import java.util.Set;
import jdk.internal.misc.JavaIOFileDescriptorAccess;
import jdk.internal.misc.SharedSecrets;
@@ -92,6 +93,74 @@
public static final SocketOption<Boolean> TCP_QUICKACK =
new ExtSocketOption<Boolean>("TCP_QUICKACK", Boolean.class);
+ /**
+ * Keep-Alive idle time.
+ *
+ * <p>
+ * The value of this socket option is an {@code Integer} that is the number
+ * of seconds of idle time before keep-alive initiates a probe. The socket
+ * option is specific to stream-oriented sockets using the TCP/IP protocol.
+ * The exact semantics of this socket option are system dependent.
+ *
+ * <p>
+ * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
+ * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
+ * idle for some amount of time. The default value for this idle period is
+ * system dependent, but is typically 2 hours. The {@code TCP_KEEPIDLE}
+ * option can be used to affect this value for a given socket.
+ *
+ * @since 11
+ */
+ public static final SocketOption<Integer> TCP_KEEPIDLE
+ = new ExtSocketOption<Integer>("TCP_KEEPIDLE", Integer.class);
+
+ /**
+ * Keep-Alive retransmission interval time.
+ *
+ * <p>
+ * The value of this socket option is an {@code Integer} that is the number
+ * of seconds to wait before retransmitting a keep-alive probe. The socket
+ * option is specific to stream-oriented sockets using the TCP/IP protocol.
+ * The exact semantics of this socket option are system dependent.
+ *
+ * <p>
+ * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
+ * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
+ * idle for some amount of time. If the remote system does not respond to a
+ * keep-alive probe, TCP retransmits the probe after some amount of time.
+ * The default value for this retransmission interval is system dependent,
+ * but is typically 75 seconds. The {@code TCP_KEEPINTERVAL} option can be
+ * used to affect this value for a given socket.
+ *
+ * @since 11
+ */
+ public static final SocketOption<Integer> TCP_KEEPINTERVAL
+ = new ExtSocketOption<Integer>("TCP_KEEPINTERVAL", Integer.class);
+
+ /**
+ * Keep-Alive retransmission maximum limit.
+ *
+ * <p>
+ * The value of this socket option is an {@code Integer} that is the maximum
+ * number of keep-alive probes to be sent. The socket option is specific to
+ * stream-oriented sockets using the TCP/IP protocol. The exact semantics of
+ * this socket option are system dependent.
+ *
+ * <p>
+ * When the {@link java.net.StandardSocketOptions#SO_KEEPALIVE
+ * SO_KEEPALIVE} option is enabled, TCP probes a connection that has been
+ * idle for some amount of time. If the remote system does not respond to a
+ * keep-alive probe, TCP retransmits the probe a certain number of times
+ * before a connection is considered to be broken. The default value for
+ * this keep-alive probe retransmit limit is system dependent, but is
+ * typically 8. The {@code TCP_KEEPCOUNT} option can be used to affect this
+ * value for a given socket.
+ *
+ * @since 11
+ */
+ public static final SocketOption<Integer> TCP_KEEPCOUNT
+ = new ExtSocketOption<Integer>("TCP_KEEPCOUNT", Integer.class);
+
private static final PlatformSocketOptions platformSocketOptions =
PlatformSocketOptions.get();
@@ -99,21 +168,22 @@
platformSocketOptions.flowSupported();
private static final boolean quickAckSupported =
platformSocketOptions.quickAckSupported();
-
+ private static final boolean keepAliveOptSupported =
+ platformSocketOptions.keepAliveOptionsSupported();
private static final Set<SocketOption<?>> extendedOptions = options();
static Set<SocketOption<?>> options() {
+ Set<SocketOption<?>> options = new HashSet<>();
if (flowSupported) {
- if (quickAckSupported) {
- return Set.of(SO_FLOW_SLA, TCP_QUICKACK);
- } else {
- return Set.of(SO_FLOW_SLA);
- }
- } else if (quickAckSupported) {
- return Set.of(TCP_QUICKACK);
- } else {
- return Collections.<SocketOption<?>>emptySet();
+ options.add(SO_FLOW_SLA);
+ }
+ if (quickAckSupported) {
+ options.add(TCP_QUICKACK);
}
+ if (keepAliveOptSupported) {
+ options.addAll(Set.of(TCP_KEEPCOUNT, TCP_KEEPIDLE, TCP_KEEPINTERVAL));
+ }
+ return Collections.unmodifiableSet(options);
}
static {
@@ -140,6 +210,12 @@
setFlowOption(fd, flow);
} else if (option == TCP_QUICKACK) {
setQuickAckOption(fd, (boolean) value);
+ } else if (option == TCP_KEEPCOUNT) {
+ setTcpkeepAliveProbes(fd, (Integer) value);
+ } else if (option == TCP_KEEPIDLE) {
+ setTcpKeepAliveTime(fd, (Integer) value);
+ } else if (option == TCP_KEEPINTERVAL) {
+ setTcpKeepAliveIntvl(fd, (Integer) value);
} else {
throw new InternalError("Unexpected option " + option);
}
@@ -164,6 +240,12 @@
return flow;
} else if (option == TCP_QUICKACK) {
return getQuickAckOption(fd);
+ } else if (option == TCP_KEEPCOUNT) {
+ return getTcpkeepAliveProbes(fd);
+ } else if (option == TCP_KEEPIDLE) {
+ return getTcpKeepAliveTime(fd);
+ } else if (option == TCP_KEEPINTERVAL) {
+ return getTcpKeepAliveIntvl(fd);
} else {
throw new InternalError("Unexpected option " + option);
}
@@ -208,6 +290,33 @@
return platformSocketOptions.getQuickAck(fdAccess.get(fd));
}
+ private static void setTcpkeepAliveProbes(FileDescriptor fd, int value)
+ throws SocketException {
+ platformSocketOptions.setTcpkeepAliveProbes(fdAccess.get(fd), value);
+ }
+
+ private static void setTcpKeepAliveTime(FileDescriptor fd, int value)
+ throws SocketException {
+ platformSocketOptions.setTcpKeepAliveTime(fdAccess.get(fd), value);
+ }
+
+ private static void setTcpKeepAliveIntvl(FileDescriptor fd, int value)
+ throws SocketException {
+ platformSocketOptions.setTcpKeepAliveIntvl(fdAccess.get(fd), value);
+ }
+
+ private static int getTcpkeepAliveProbes(FileDescriptor fd) throws SocketException {
+ return platformSocketOptions.getTcpkeepAliveProbes(fdAccess.get(fd));
+ }
+
+ private static int getTcpKeepAliveTime(FileDescriptor fd) throws SocketException {
+ return platformSocketOptions.getTcpKeepAliveTime(fdAccess.get(fd));
+ }
+
+ private static int getTcpKeepAliveIntvl(FileDescriptor fd) throws SocketException {
+ return platformSocketOptions.getTcpKeepAliveIntvl(fdAccess.get(fd));
+ }
+
static class PlatformSocketOptions {
protected PlatformSocketOptions() {}
@@ -234,6 +343,8 @@
return newInstance("jdk.net.SolarisSocketOptions");
} else if ("Linux".equals(osname)) {
return newInstance("jdk.net.LinuxSocketOptions");
+ } else if (osname.startsWith("Mac")) {
+ return newInstance("jdk.net.MacOSXSocketOptions");
} else {
return new PlatformSocketOptions();
}
@@ -270,5 +381,33 @@
boolean quickAckSupported() {
return false;
}
+
+ boolean keepAliveOptionsSupported() {
+ return false;
+ }
+
+ void setTcpkeepAliveProbes(int fd, final int value) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
+ }
+
+ void setTcpKeepAliveTime(int fd, final int value) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPIDLE option");
+ }
+
+ void setTcpKeepAliveIntvl(int fd, final int value) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPINTVL option");
+ }
+
+ int getTcpkeepAliveProbes(int fd) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPCNT option");
+ }
+
+ int getTcpKeepAliveTime(int fd) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPIDLE option");
+ }
+
+ int getTcpKeepAliveIntvl(int fd) throws SocketException {
+ throw new UnsupportedOperationException("unsupported TCP_KEEPINTVL option");
+ }
}
}
--- a/src/jdk.net/share/classes/jdk/net/Sockets.java Wed May 30 08:52:59 2018 +0200
+++ b/src/jdk.net/share/classes/jdk/net/Sockets.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -282,6 +282,11 @@
if (QuickAck.available) {
set.add(ExtendedSocketOptions.TCP_QUICKACK);
}
+ if (KeepAliveOptions.AVAILABLE) {
+ set.addAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
+ ExtendedSocketOptions.TCP_KEEPIDLE,
+ ExtendedSocketOptions.TCP_KEEPINTERVAL));
+ }
set = Collections.unmodifiableSet(set);
options.put(Socket.class, set);
@@ -296,6 +301,11 @@
if (QuickAck.available) {
set.add(ExtendedSocketOptions.TCP_QUICKACK);
}
+ if (KeepAliveOptions.AVAILABLE) {
+ set.addAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
+ ExtendedSocketOptions.TCP_KEEPIDLE,
+ ExtendedSocketOptions.TCP_KEEPINTERVAL));
+ }
set.add(StandardSocketOptions.IP_TOS);
set = Collections.unmodifiableSet(set);
options.put(ServerSocket.class, set);
@@ -350,4 +360,19 @@
available = s.contains(ExtendedSocketOptions.TCP_QUICKACK);
}
}
+
+ /**
+ * Tells whether TCP_KEEPALIVE options are supported.
+ */
+ static class KeepAliveOptions {
+
+ static final boolean AVAILABLE;
+
+ static {
+ Set<SocketOption<?>> s = new Socket().supportedOptions();
+ AVAILABLE = s.containsAll(Set.of(ExtendedSocketOptions.TCP_KEEPCOUNT,
+ ExtendedSocketOptions.TCP_KEEPIDLE,
+ ExtendedSocketOptions.TCP_KEEPINTERVAL));
+ }
+ }
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/SocketOption/TcpKeepAliveTest.java Wed May 30 16:36:35 2018 +0530
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+ /*
+ * @test
+ * @bug 8194298
+ * @summary Add support for per Socket configuration of TCP keepalive
+ * @modules jdk.net
+ * @run main TcpKeepAliveTest
+ */
+import java.io.IOException;
+import java.net.DatagramSocket;
+import java.net.MulticastSocket;
+import java.net.ServerSocket;
+import java.net.Socket;
+import jdk.net.ExtendedSocketOptions;
+
+public class TcpKeepAliveTest {
+
+ private static final String LOCAL_HOST = "127.0.0.1";
+ private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;
+ private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;
+ private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;
+
+ public static void main(String args[]) throws IOException {
+
+ try (ServerSocket ss = new ServerSocket(0);
+ Socket s = new Socket(LOCAL_HOST, ss.getLocalPort());
+ DatagramSocket ds = new DatagramSocket(0);
+ MulticastSocket mc = new MulticastSocket(0)) {
+ if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
+ ss.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
+ if (ss.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
+ throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
+ }
+ }
+ if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
+ ss.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
+ if (ss.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
+ throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
+ }
+ }
+ if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
+ ss.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
+ if (ss.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
+ throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
+ }
+ }
+ if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
+ s.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
+ if (s.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
+ throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
+ }
+ }
+ if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
+ s.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
+ if (s.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
+ throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
+ }
+ }
+ if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
+ s.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
+ if (s.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
+ throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
+ }
+ }
+ if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
+ throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
+ + " for TCP Sockets only.");
+ }
+ if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
+ throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
+ + " for TCP Sockets only.");
+ }
+ if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
+ throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
+ + " for TCP Sockets only.");
+ }
+ if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
+ throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
+ + " for TCP Sockets only");
+ }
+ if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
+ throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
+ + " for TCP Sockets only");
+ }
+ if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
+ throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
+ + " for TCP Sockets only");
+ }
+ }
+ }
+}
--- a/test/jdk/java/nio/channels/AsynchronousServerSocketChannel/Basic.java Wed May 30 08:52:59 2018 +0200
+++ b/test/jdk/java/nio/channels/AsynchronousServerSocketChannel/Basic.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,7 @@
/* @test
* @bug 4607272 6842687
* @summary Unit test for AsynchronousServerSocketChannel
+ * @modules jdk.net
* @run main/timeout=180 Basic
*/
@@ -31,10 +32,14 @@
import java.net.*;
import static java.net.StandardSocketOptions.*;
import java.io.IOException;
+import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;
public class Basic {
@@ -166,6 +171,16 @@
ch.setOption(SO_REUSEPORT, false);
checkOption(ch, SO_REUSEPORT, false);
}
+ List<? extends SocketOption> extOptions = List.of(TCP_KEEPCOUNT,
+ TCP_KEEPIDLE, TCP_KEEPINTERVAL);
+ if (options.containsAll(extOptions)) {
+ ch.setOption(TCP_KEEPIDLE, 1234);
+ checkOption(ch, TCP_KEEPIDLE, 1234);
+ ch.setOption(TCP_KEEPINTERVAL, 123);
+ checkOption(ch, TCP_KEEPINTERVAL, 123);
+ ch.setOption(TCP_KEEPCOUNT, 7);
+ checkOption(ch, TCP_KEEPCOUNT, 7);
+ }
} finally {
ch.close();
}
--- a/test/jdk/java/nio/channels/AsynchronousSocketChannel/Basic.java Wed May 30 08:52:59 2018 +0200
+++ b/test/jdk/java/nio/channels/AsynchronousSocketChannel/Basic.java Wed May 30 16:36:35 2018 +0530
@@ -25,9 +25,10 @@
* @bug 4607272 6842687 6878369 6944810 7023403
* @summary Unit test for AsynchronousSocketChannel(use -Dseed=X to set PRNG seed)
* @library /test/lib
+ * @modules jdk.net
+ * @key randomness intermittent
* @build jdk.test.lib.RandomFactory jdk.test.lib.Utils
* @run main/othervm/timeout=600 Basic -skipSlowConnectTest
- * @key randomness intermittent
*/
import java.io.Closeable;
@@ -41,6 +42,10 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import jdk.test.lib.RandomFactory;
+import java.util.List;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPCOUNT;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPIDLE;
+import static jdk.net.ExtendedSocketOptions.TCP_KEEPINTERVAL;
public class Basic {
private static final Random RAND = RandomFactory.getRandom();
@@ -183,9 +188,26 @@
if (!ch.setOption(SO_REUSEPORT, true).getOption(SO_REUSEPORT))
throw new RuntimeException("SO_REUSEPORT did not change");
}
+ List<? extends SocketOption> extOptions = List.of(TCP_KEEPCOUNT,
+ TCP_KEEPIDLE, TCP_KEEPINTERVAL);
+ if (options.containsAll(extOptions)) {
+ ch.setOption(TCP_KEEPIDLE, 1234);
+ checkOption(ch, TCP_KEEPIDLE, 1234);
+ ch.setOption(TCP_KEEPINTERVAL, 123);
+ checkOption(ch, TCP_KEEPINTERVAL, 123);
+ ch.setOption(TCP_KEEPCOUNT, 7);
+ checkOption(ch, TCP_KEEPCOUNT, 7);
+ }
}
}
+ static void checkOption(AsynchronousSocketChannel sc, SocketOption name, Object expectedValue)
+ throws IOException {
+ Object value = sc.getOption(name);
+ if (!value.equals(expectedValue)) {
+ throw new RuntimeException("value not as expected");
+ }
+ }
static void testConnect() throws Exception {
System.out.println("-- connect --");
--- a/test/jdk/java/nio/channels/ServerSocketChannel/SocketOptionTests.java Wed May 30 08:52:59 2018 +0200
+++ b/test/jdk/java/nio/channels/ServerSocketChannel/SocketOptionTests.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,20 +25,25 @@
* @bug 4640544 8044773
* @summary Unit test for ServerSocketChannel setOption/getOption/options
* methods.
+ * @modules jdk.net
* @requires !vm.graal.enabled
* @run main SocketOptionTests
* @run main/othervm --limit-modules=java.base SocketOptionTests
*/
-import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.IOException;
import java.util.*;
import static java.net.StandardSocketOptions.*;
+import static jdk.net.ExtendedSocketOptions.*;
public class SocketOptionTests {
+ private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;
+ private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;
+ private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;
+
static void checkOption(ServerSocketChannel ssc, SocketOption name, Object expectedValue)
throws IOException
{
@@ -76,6 +81,16 @@
ssc.setOption(SO_REUSEPORT, false);
checkOption(ssc, SO_REUSEPORT, false);
}
+ if (ssc.supportedOptions().containsAll(List.of(TCP_KEEPCOUNT,
+ TCP_KEEPIDLE, TCP_KEEPINTERVAL))) {
+ ssc.setOption(TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
+ checkOption(ssc, TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
+ ssc.setOption(TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
+ checkOption(ssc, TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
+ ssc.setOption(TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
+ checkOption(ssc, TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
+
+ }
// NullPointerException
try {
--- a/test/jdk/java/nio/channels/SocketChannel/SocketOptionTests.java Wed May 30 08:52:59 2018 +0200
+++ b/test/jdk/java/nio/channels/SocketChannel/SocketOptionTests.java Wed May 30 16:36:35 2018 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -25,17 +25,21 @@
* @bug 4640544 8044773
* @summary Unit test to check SocketChannel setOption/getOption/options
* methods.
+ * @modules java.base/sun.net.ext
+ * jdk.net
* @requires !vm.graal.enabled
* @run main SocketOptionTests
* @run main/othervm --limit-modules=java.base SocketOptionTests
*/
-import java.nio.*;
import java.nio.channels.*;
import java.net.*;
import java.io.IOException;
import java.util.*;
import static java.net.StandardSocketOptions.*;
+import static jdk.net.ExtendedSocketOptions.*;
+import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
+import sun.net.ext.ExtendedSocketOptions;
public class SocketOptionTests {
@@ -52,8 +56,20 @@
// check supported options
Set<SocketOption<?>> options = sc.supportedOptions();
- List<? extends SocketOption> expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF,
- SO_KEEPALIVE, SO_REUSEADDR, SO_LINGER, TCP_NODELAY);
+
+ List<? extends SocketOption> extOptions = List.of(TCP_KEEPCOUNT,
+ TCP_KEEPIDLE, TCP_KEEPINTERVAL);
+ List<? extends SocketOption> expected;
+ boolean keepAliveOptsupported;
+ if (keepAliveOptsupported=ExtendedSocketOptions.options(SOCK_STREAM)
+ .containsAll(extOptions)) {
+ expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,
+ SO_REUSEADDR, SO_LINGER, TCP_NODELAY, TCP_KEEPCOUNT,
+ TCP_KEEPIDLE, TCP_KEEPINTERVAL);
+ } else {
+ expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,
+ SO_REUSEADDR, SO_LINGER, TCP_NODELAY);
+ }
for (SocketOption opt: expected) {
if (!options.contains(opt))
throw new RuntimeException(opt.name() + " should be supported");
@@ -117,7 +133,14 @@
throw new RuntimeException("expected linger to be disabled");
sc.setOption(TCP_NODELAY, true); // can't check
sc.setOption(TCP_NODELAY, false); // can't check
-
+ if (keepAliveOptsupported) {
+ sc.setOption(TCP_KEEPIDLE, 1234);
+ checkOption(sc, TCP_KEEPIDLE, 1234);
+ sc.setOption(TCP_KEEPINTERVAL, 123);
+ checkOption(sc, TCP_KEEPINTERVAL, 123);
+ sc.setOption(TCP_KEEPCOUNT, 7);
+ checkOption(sc, TCP_KEEPCOUNT, 7);
+ }
// NullPointerException
try {
sc.setOption(null, "value");