8194298: Add support for per Socket configuration of TCP keepalive

Reviewed-by: chegar, clanger, igerasim, alanb
This commit is contained in:
Vyom Tewari 2018-05-30 16:36:35 +05:30
parent 611c72e5bc
commit 6a48db9cf6
20 changed files with 815 additions and 48 deletions

View file

@ -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.SocketException;
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 @@ import java.util.Set;
*/
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 @@ public abstract class ExtendedSocketOptions {
/** 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;

View file

@ -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.Future;
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 @@ abstract class AsynchronousServerSocketChannelImpl
if (Net.isReusePortAvailable()) {
set.add(StandardSocketOptions.SO_REUSEPORT);
}
set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}

View file

@ -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.*;
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 @@ abstract class AsynchronousSocketChannelImpl
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);
}
}

View file

@ -57,6 +57,7 @@ import java.util.concurrent.locks.ReentrantLock;
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 @@ class DatagramChannelImpl
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);
}
}

View file

@ -49,6 +49,8 @@ import java.util.Set;
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 @@ class ServerSocketChannelImpl
set.add(StandardSocketOptions.SO_REUSEPORT);
}
set.add(StandardSocketOptions.IP_TOS);
set.addAll(ExtendedSocketOptions.options(SOCK_STREAM));
return Collections.unmodifiableSet(set);
}
}

View file

@ -54,6 +54,7 @@ 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 SocketChannels
@ -280,7 +281,7 @@ class SocketChannelImpl
// 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);
}
}