Skip to content

Commit 184873a

Browse files
matttbegregkh
authored andcommitted
mptcp: fix full TCP keep-alive support
commit bd11dc4 upstream. 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: multipath-tcp/mptcp_net-next#383 Fixes: 1b3e7ed ("mptcp: setsockopt: handle SO_KEEPALIVE and SO_PRIORITY") Link: golang/go#56539 [1] Acked-by: Paolo Abeni <[email protected]> Signed-off-by: Matthieu Baerts (NGI0) <[email protected]> Signed-off-by: Mat Martineau <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> [ conflicts in the same context, because commit 29b5e5e ("mptcp: implement TCP_NOTSENT_LOWAT support") is not in this version ] Signed-off-by: Matthieu Baerts (NGI0) <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 89e11fe commit 184873a

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
@@ -303,6 +303,9 @@ struct mptcp_sock {
303303
in_accept_queue:1,
304304
free_first:1,
305305
rcvspace_init:1;
306+
int keepalive_cnt;
307+
int keepalive_idle;
308+
int keepalive_intvl;
306309
struct work_struct work;
307310
struct sk_buff *ooo_last_skb;
308311
struct rb_root out_of_order_queue;

net/mptcp/sockopt.c

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

624+
static int __mptcp_setsockopt_set_val(struct mptcp_sock *msk, int max,
625+
int (*set_val)(struct sock *, int),
626+
int *msk_val, int val)
627+
{
628+
struct mptcp_subflow_context *subflow;
629+
int err = 0;
630+
631+
mptcp_for_each_subflow(msk, subflow) {
632+
struct sock *ssk = mptcp_subflow_tcp_sock(subflow);
633+
int ret;
634+
635+
lock_sock(ssk);
636+
ret = set_val(ssk, val);
637+
err = err ? : ret;
638+
release_sock(ssk);
639+
}
640+
641+
if (!err) {
642+
*msk_val = val;
643+
sockopt_seq_inc(msk);
644+
}
645+
646+
return err;
647+
}
648+
624649
static int __mptcp_setsockopt_sol_tcp_cork(struct mptcp_sock *msk, int val)
625650
{
626651
struct mptcp_subflow_context *subflow;
@@ -803,6 +828,22 @@ static int mptcp_setsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
803828
case TCP_NODELAY:
804829
ret = __mptcp_setsockopt_sol_tcp_nodelay(msk, val);
805830
break;
831+
case TCP_KEEPIDLE:
832+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPIDLE,
833+
&tcp_sock_set_keepidle_locked,
834+
&msk->keepalive_idle, val);
835+
break;
836+
case TCP_KEEPINTVL:
837+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPINTVL,
838+
&tcp_sock_set_keepintvl,
839+
&msk->keepalive_intvl, val);
840+
break;
841+
case TCP_KEEPCNT:
842+
ret = __mptcp_setsockopt_set_val(msk, MAX_TCP_KEEPCNT,
843+
&tcp_sock_set_keepcnt,
844+
&msk->keepalive_cnt,
845+
val);
846+
break;
806847
default:
807848
ret = -ENOPROTOOPT;
808849
}
@@ -1303,6 +1344,8 @@ static int mptcp_put_int_option(struct mptcp_sock *msk, char __user *optval,
13031344
static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
13041345
char __user *optval, int __user *optlen)
13051346
{
1347+
struct sock *sk = (void *)msk;
1348+
13061349
switch (optname) {
13071350
case TCP_ULP:
13081351
case TCP_CONGESTION:
@@ -1321,6 +1364,18 @@ static int mptcp_getsockopt_sol_tcp(struct mptcp_sock *msk, int optname,
13211364
return mptcp_put_int_option(msk, optval, optlen, msk->cork);
13221365
case TCP_NODELAY:
13231366
return mptcp_put_int_option(msk, optval, optlen, msk->nodelay);
1367+
case TCP_KEEPIDLE:
1368+
return mptcp_put_int_option(msk, optval, optlen,
1369+
msk->keepalive_idle ? :
1370+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_time) / HZ);
1371+
case TCP_KEEPINTVL:
1372+
return mptcp_put_int_option(msk, optval, optlen,
1373+
msk->keepalive_intvl ? :
1374+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_intvl) / HZ);
1375+
case TCP_KEEPCNT:
1376+
return mptcp_put_int_option(msk, optval, optlen,
1377+
msk->keepalive_cnt ? :
1378+
READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_keepalive_probes));
13241379
}
13251380
return -EOPNOTSUPP;
13261381
}
@@ -1430,6 +1485,9 @@ static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
14301485
tcp_set_congestion_control(ssk, msk->ca_name, false, true);
14311486
__tcp_sock_set_cork(ssk, !!msk->cork);
14321487
__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
1488+
tcp_sock_set_keepidle_locked(ssk, msk->keepalive_idle);
1489+
tcp_sock_set_keepintvl(ssk, msk->keepalive_intvl);
1490+
tcp_sock_set_keepcnt(ssk, msk->keepalive_cnt);
14331491

14341492
inet_assign_bit(TRANSPARENT, ssk, inet_test_bit(TRANSPARENT, sk));
14351493
inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));

0 commit comments

Comments
 (0)