Skip to content

Commit 7df5fa3

Browse files
matttbePatchew Applier
authored and
Patchew Applier
committed
mptcp: fix full TCP keep-alive support
SO_KEEPALIVE support has been added a while ago, as part of a series "adding SOL_SOCKET" support. To have a full control of this keep-alive feature, it is important to also support TCP_KEEP* socket options at the SOL_TCP level. Supporting them on the setsockopt() part is easy, it is just a matter of remembering each value in the MPTCP sock structure, and calling tcp_sock_set_keep*() helpers on each subflow. If the value is not modified (0), calling these helpers will not do anything. For the getsockopt() part, the corresponding value from the MPTCP sock structure or the default one is simply returned. All of this is very similar to other TCP_* socket options supported by MPTCP. It looks important for kernels supporting SO_KEEPALIVE, to also support TCP_KEEP* options as well: some apps seem to (wrongly) consider that if the former is supported, the latter ones will be supported as well. But also, not having this simple and isolated change is preventing MPTCP support in some apps, and libraries like GoLang [1]. This is why this patch is seen as a fix. Closes: #383 Fixes: 1b3e7ed ("mptcp: setsockopt: handle SO_KEEPALIVE and SO_PRIORITY") Link: golang/go#56539 [1] Signed-off-by: Matthieu Baerts (NGI0) <[email protected]> Message-Id: <20240509-mptcp-tcp-keepalive-sockopts-v2-2-5f97067847a2@kernel.org>
1 parent 54ce26e commit 7df5fa3

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

net/mptcp/protocol.h

+3
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ struct mptcp_sock {
310310
free_first:1,
311311
rcvspace_init:1;
312312
u32 notsent_lowat;
313+
int keepalive_cnt;
314+
int keepalive_idle;
315+
int keepalive_intvl;
313316
struct work_struct work;
314317
struct sk_buff *ooo_last_skb;
315318
struct rb_root out_of_order_queue;

net/mptcp/sockopt.c

+58
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,31 @@ static int mptcp_setsockopt_sol_tcp_congestion(struct mptcp_sock *msk, sockptr_t
622622
return ret;
623623
}
624624

625+
static int __mptcp_setsockopt_set_val(struct mptcp_sock *msk, int max,
626+
int (*set_val)(struct sock *, int),
627+
int *msk_val, int val)
628+
{
629+
struct mptcp_subflow_context *subflow;
630+
int err = 0;
631+
632+
mptcp_for_each_subflow(msk, subflow) {
633+
struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
634+
int ret;
635+
636+
lock_sock(ssk);
637+
ret = set_val(ssk, val);
638+
err = err ? : ret;
639+
release_sock(ssk);
640+
}
641+
642+
if (!err) {
643+
*msk_val = val;
644+
sockopt_seq_inc(msk);
645+
}
646+
647+
return err;
648+
}
649+
625650
static int __mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, int val)
626651
{
627652
struct mptcp_subflow_context *subflow;
@@ -818,6 +843,22 @@ static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
818843
case TCP_NODELAY:
819844
ret = __mptcp_setsockopt_sol_tcp_nodelay(msk, val);
820845
break;
846+
case TCP_KEEPIDLE:
847+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPIDLE,
848+
&tcp_sock_set_keepidle_locked,
849+
&msk->keepalive_idle, val);
850+
break;
851+
case TCP_KEEPINTVL:
852+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPINTVL,
853+
&tcp_sock_set_keepintvl,
854+
&msk->keepalive_intvl, val);
855+
break;
856+
case TCP_KEEPCNT:
857+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPCNT,
858+
&tcp_sock_set_keepcnt,
859+
&msk->keepalive_cnt,
860+
val);
861+
break;
821862
default:
822863
ret = -ENOPROTOOPT;
823864
}
@@ -1332,6 +1373,8 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
13321373
static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
13331374
char __user *optval, int __user *optlen)
13341375
{
1376+
struct sock *sk = (void *)msk;
1377+
13351378
switch (optname) {
13361379
case TCP_ULP:
13371380
case TCP_CONGESTION:
@@ -1352,6 +1395,18 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
13521395
return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
13531396
case TCP_NOTSENT_LOWAT:
13541397
return mptcp_put_int_option(msk, optval, optlen, msk->notsent_lowat);
1398+
case TCP_KEEPIDLE:
1399+
return mptcp_put_int_option(msk, optval, optlen,
1400+
msk->keepalive_idle ? :
1401+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_time) / HZ);
1402+
case TCP_KEEPINTVL:
1403+
return mptcp_put_int_option(msk, optval, optlen,
1404+
msk->keepalive_intvl ? :
1405+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_intvl) / HZ);
1406+
case TCP_KEEPCNT:
1407+
return mptcp_put_int_option(msk, optval, optlen,
1408+
msk->keepalive_cnt ? :
1409+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_probes));
13551410
}
13561411
return -EOPNOTSUPP;
13571412
}
@@ -1467,6 +1522,9 @@ static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
14671522
tcp_set_congestion_control(ssk, msk->ca_name, false, true);
14681523
__tcp_sock_set_cork(ssk, !!msk->cork);
14691524
__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1525+
tcp_sock_set_keepidle_locked(ssk, msk->keepalive_idle);
1526+
tcp_sock_set_keepintvl(ssk, msk->keepalive_intvl);
1527+
tcp_sock_set_keepcnt(ssk, msk->keepalive_cnt);
14701528

14711529
inet_assign_bit(TRANSPARENT, ssk, inet_test_bit(TRANSPARENT, sk));
14721530
inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));

0 commit comments

Comments
 (0)