[PATCH mptcp-next 5/7] Add MPTCP support to ServerSocket class

Geliang Tang posted 7 patches 6 months, 2 weeks ago
There is a newer version of this series
[PATCH mptcp-next 5/7] Add MPTCP support to ServerSocket class
Posted by Geliang Tang 6 months, 2 weeks ago
This patch extends ServerSocket to support Multipath TCP (MPTCP) by:

1. Adding a new constructor that accepts an mptcp parameter to enable
   MPTCP
2. Introducing a new createImpl factory method that propagates the MPTCP
   setting to the underlying socket implementation
3. Maintaining all existing functionality while adding MPTCP capabilities

The new constructor follows the same pattern as existing ServerSocket
constructors but adds the mptcp parameter to control protocol selection.
When enabled, the implementation uses the MPTCP-enabled socket creation
path established in previous patches.

Co-Developed-by: Gang Yan <yangang@kylinos.cn>
Signed-off-by: Gang Yan <yangang@kylinos.cn>
Signed-off-by: Geliang Tang <geliang@kernel.org>
---
 .../share/classes/java/net/ServerSocket.java  | 66 +++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/src/java.base/share/classes/java/net/ServerSocket.java b/src/java.base/share/classes/java/net/ServerSocket.java
index 945693ef65e..ba07bc4d721 100644
--- a/src/java.base/share/classes/java/net/ServerSocket.java
+++ b/src/java.base/share/classes/java/net/ServerSocket.java
@@ -226,6 +226,57 @@ public ServerSocket(int port, int backlog, InetAddress bindAddr) throws IOExcept
         }
     }
 
+    /**
+     * Create a server using <i>mptcp</i> (Multipath TCP) or TCP protocol with
+     * the specified port, listen backlog, and local IP address to bind to.
+     * The <i>bindAddr</i> argument can be used on a multi-homed host for a
+     * ServerSocket that will only accept connect requests to one of its addresses.
+     * If <i>bindAddr</i> is null, it will default accepting
+     * connections on any/all local addresses.
+     * The port must be between 0 and 65535, inclusive.
+     * A port number of {@code 0} means that the port number is
+     * automatically allocated, typically from an ephemeral port range.
+     * This port number can then be retrieved by calling
+     * {@link #getLocalPort getLocalPort}.
+     *
+     * The {@code backlog} argument is the requested maximum number of
+     * pending connections on the socket. Its exact semantics are implementation
+     * specific. In particular, an implementation may impose a maximum length
+     * or may choose to ignore the parameter altogether. The value provided
+     * should be greater than {@code 0}. If it is less than or equal to
+     * {@code 0}, then an implementation specific default will be used.
+     *
+     * The {@code mptcp} argument is used to control whether to create a socket
+     * with MPTCP or TCP protocol.
+     *
+     * @param port  the port number, or {@code 0} to use a port
+     *              number that is automatically allocated.
+     * @param backlog requested maximum length of the queue of incoming
+     *                connections.
+     * @param bindAddr the local InetAddress the server will bind to
+     * @param mptcp create a socket with MPTCP or TCP protocol.
+     *
+     * @throws  IOException if an I/O error occurs when opening the socket.
+     * @throws     IllegalArgumentException if the port parameter is outside
+     *             the specified range of valid port values, which is between
+     *             0 and 65535, inclusive.
+     */
+    @SuppressWarnings("this-escape")
+    public ServerSocket(int port, int backlog, InetAddress bindAddr, boolean mptcp) throws IOException {
+        if (port < 0 || port > 0xFFFF)
+            throw new IllegalArgumentException("Port value out of range: " + port);
+        if (backlog < 1)
+            backlog = 50;
+
+        this.impl = createImpl(mptcp);
+        try {
+            bind(new InetSocketAddress(bindAddr, port), backlog);
+        } catch (IOException e) {
+            close();
+            throw e;
+        }
+    }
+
     /**
      * Create a SocketImpl for a server socket. The SocketImpl is created
      * without an underlying socket.
@@ -239,6 +290,21 @@ private static SocketImpl createImpl() {
         }
     }
 
+    /**
+     * Create a SocketImpl for a server socket. The SocketImpl is created
+     * without an underlying socket.
+     *
+     * @param mptcp create a socket with MPTCP or TCP protocol.
+     */
+    private static SocketImpl createImpl(boolean mptcp) {
+        SocketImplFactory factory = ServerSocket.factory;
+        if (factory != null) {
+            return factory.createSocketImpl();
+        } else {
+            return SocketImpl.createPlatformSocketImpl(true, mptcp);
+        }
+    }
+
     /**
      * Returns the {@code SocketImpl} for this ServerSocket, creating the
      * underlying socket if required.
-- 
2.48.1
Re: [PATCH mptcp-next 5/7] Add MPTCP support to ServerSocket class
Posted by Matthieu Baerts 6 months, 1 week ago
Hi Geliang,

On 23/07/2025 07:16, Geliang Tang wrote:
> This patch extends ServerSocket to support Multipath TCP (MPTCP) by:
> 
> 1. Adding a new constructor that accepts an mptcp parameter to enable
>    MPTCP
> 2. Introducing a new createImpl factory method that propagates the MPTCP
>    setting to the underlying socket implementation
> 3. Maintaining all existing functionality while adding MPTCP capabilities
> 
> The new constructor follows the same pattern as existing ServerSocket
> constructors but adds the mptcp parameter to control protocol selection.
> When enabled, the implementation uses the MPTCP-enabled socket creation
> path established in previous patches.

Similar to my comment in patch 2/7, I don't know what is best in this
project, but maybe instead of duplicating helpers, you could have the
previous helper calling the new extension?

So ServerSocket(int port, int backlog, InetAddress bindAddr) would call
the new one with 'mptcp' as new parameter? Same for createImpl()?

But not sure what the Java maintainers prefer. Maybe they are OK with
what you suggested.

Cheers,
Matt
-- 
Sponsored by the NGI0 Core fund.