From 59d7c69342edbf8943699394513fb9b8fb175d7e Mon Sep 17 00:00:00 2001 From: Matthieu Baerts Date: Fri, 24 Feb 2023 17:51:57 +0100 Subject: [PATCH] net: add initial MPTCP support This currently defines an internal function supportsMultipathTCP which reports whether MPTCP[1] is supported on the current platform. Only Linux is supported here. The check on Linux is performed once by attemting to create an MPTCP socket and look at the returned error: - If the protocol is not supported, EINVAL (kernel < 5.6) or EPROTONOSUPPORT (kernel >= 5.6) is returned and there is no point to try again. - Other errors can be returned: - ENOPROTOOPT: the sysctl knob net.mptcp.enabled is set to 0 - Unpredictable ones: if MPTCP is blocked using SELinux, eBPF, etc. These other errors are due to modifications that can be reverted during the session: MPTCP can be available again later. In this case, it is fine to always try to create an MPTCP socket and fallback to TCP in case of error. This work has been co-developped by Gregory Detal . [1] https://www.rfc-editor.org/rfc/rfc8684.html Updates #56539 Change-Id: Ic84fe85aad887a2be4556a898e649bf6b6f12f03 Reviewed-on: https://go-review.googlesource.com/c/go/+/471135 TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Emmanuel Odeke Reviewed-by: Heschi Kreinick Auto-Submit: Ian Lance Taylor --- src/net/mptcpsock_linux.go | 43 ++++++++++++++++++++++++++++++++++++++ src/net/mptcpsock_stub.go | 7 +++++++ 2 files changed, 50 insertions(+) create mode 100644 src/net/mptcpsock_linux.go create mode 100644 src/net/mptcpsock_stub.go diff --git a/src/net/mptcpsock_linux.go b/src/net/mptcpsock_linux.go new file mode 100644 index 0000000000..c88b07c907 --- /dev/null +++ b/src/net/mptcpsock_linux.go @@ -0,0 +1,43 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package net + +import ( + "errors" + "internal/poll" + "sync" + "syscall" +) + +var ( + mptcpOnce sync.Once + mptcpAvailable bool +) + +// These constants aren't in the syscall package, which is frozen +const ( + _IPPROTO_MPTCP = 0x106 +) + +func supportsMultipathTCP() bool { + mptcpOnce.Do(initMPTCPavailable) + return mptcpAvailable +} + +// Check that MPTCP is supported by attemting to create an MPTCP socket and by +// looking at the returned error if any. +func initMPTCPavailable() { + s, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, _IPPROTO_MPTCP) + switch { + case errors.Is(err, syscall.EPROTONOSUPPORT): // Not supported: >= v5.6 + case errors.Is(err, syscall.EINVAL): // Not supported: < v5.6 + case err == nil: // Supported and no error + poll.CloseFunc(s) + fallthrough + default: + // another error: MPTCP was not available but it might be later + mptcpAvailable = true + } +} diff --git a/src/net/mptcpsock_stub.go b/src/net/mptcpsock_stub.go new file mode 100644 index 0000000000..5508288ef5 --- /dev/null +++ b/src/net/mptcpsock_stub.go @@ -0,0 +1,7 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !linux + +package net -- 2.50.0