From: Geliang Tang <tanggeliang@kylinos.cn>
Add a helper tcp_sock_set_maxseg() to directly set the TCP_MAXSEG sockopt
from kernel space.
Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
include/linux/tcp.h | 1 +
net/ipv4/tcp.c | 24 +++++++++++++++---------
2 files changed, 16 insertions(+), 9 deletions(-)
diff --git a/include/linux/tcp.h b/include/linux/tcp.h
index 1669d95bb0f9..0041bc0aaee9 100644
--- a/include/linux/tcp.h
+++ b/include/linux/tcp.h
@@ -623,6 +623,7 @@ void tcp_sock_set_nodelay(struct sock *sk);
void tcp_sock_set_quickack(struct sock *sk, int val);
int tcp_sock_set_syncnt(struct sock *sk, int val);
int tcp_sock_set_user_timeout(struct sock *sk, int val);
+int tcp_sock_set_maxseg(struct sock *sk, int val);
static inline bool dst_tcp_usec_ts(const struct dst_entry *dst)
{
diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 4516f93f6cd2..402f4a9b3fd1 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -3725,6 +3725,20 @@ int tcp_set_window_clamp(struct sock *sk, int val)
return 0;
}
+int tcp_sock_set_maxseg(struct sock *sk, int val)
+{
+ /* Values greater than interface MTU won't take effect. However
+ * at the point when this call is done we typically don't yet
+ * know which interface is going to be used
+ */
+ if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW))
+ return -EINVAL;
+
+ tcp_sk(sk)->rx_opt.user_mss = val;
+ return 0;
+}
+EXPORT_SYMBOL(tcp_sock_set_maxseg);
+
/*
* Socket option code for TCP.
*/
@@ -3857,15 +3871,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
switch (optname) {
case TCP_MAXSEG:
- /* Values greater than interface MTU won't take effect. However
- * at the point when this call is done we typically don't yet
- * know which interface is going to be used
- */
- if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW)) {
- err = -EINVAL;
- break;
- }
- tp->rx_opt.user_mss = val;
+ tcp_sock_set_maxseg(sk, val);
break;
case TCP_NODELAY:
--
2.43.0
Hi Geliang,
On 23/04/2025 11:40, Geliang Tang wrote:
> From: Geliang Tang <tanggeliang@kylinos.cn>
>
> Add a helper tcp_sock_set_maxseg() to directly set the TCP_MAXSEG sockopt
> from kernel space.
Probably clearer to add:
This new helper will be used in the following patch.
>
> Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
> ---
> include/linux/tcp.h | 1 +
> net/ipv4/tcp.c | 24 +++++++++++++++---------
> 2 files changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/include/linux/tcp.h b/include/linux/tcp.h
> index 1669d95bb0f9..0041bc0aaee9 100644
> --- a/include/linux/tcp.h
> +++ b/include/linux/tcp.h
> @@ -623,6 +623,7 @@ void tcp_sock_set_nodelay(struct sock *sk);
> void tcp_sock_set_quickack(struct sock *sk, int val);
> int tcp_sock_set_syncnt(struct sock *sk, int val);
> int tcp_sock_set_user_timeout(struct sock *sk, int val);
> +int tcp_sock_set_maxseg(struct sock *sk, int val);
>
> static inline bool dst_tcp_usec_ts(const struct dst_entry *dst)
> {
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 4516f93f6cd2..402f4a9b3fd1 100644
> --- a/net/ipv4/tcp.c
> +++ b/net/ipv4/tcp.c
> @@ -3725,6 +3725,20 @@ int tcp_set_window_clamp(struct sock *sk, int val)
> return 0;
> }
>
> +int tcp_sock_set_maxseg(struct sock *sk, int val)
> +{
> + /* Values greater than interface MTU won't take effect. However
> + * at the point when this call is done we typically don't yet
> + * know which interface is going to be used
> + */
> + if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW))
> + return -EINVAL;
> +
> + tcp_sk(sk)->rx_opt.user_mss = val;
> + return 0;
> +}
> +EXPORT_SYMBOL(tcp_sock_set_maxseg);
This export is not needed: it is only needed when used from a module.
The rest looks OK to me:
Acked-by: Matthieu Baerts (NGI0) <matttbe@kernel.org>
> +
> /*
> * Socket option code for TCP.
> */
> @@ -3857,15 +3871,7 @@ int do_tcp_setsockopt(struct sock *sk, int level, int optname,
>
> switch (optname) {
> case TCP_MAXSEG:
> - /* Values greater than interface MTU won't take effect. However
> - * at the point when this call is done we typically don't yet
> - * know which interface is going to be used
> - */
> - if (val && (val < TCP_MIN_MSS || val > MAX_TCP_WINDOW)) {
> - err = -EINVAL;
> - break;
> - }
> - tp->rx_opt.user_mss = val;
> + tcp_sock_set_maxseg(sk, val);
> break;
>
> case TCP_NODELAY:
Cheers,
Matt
--
Sponsored by the NGI0 Core fund.
© 2016 - 2025 Red Hat, Inc.