Commit cadd6480 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'Add support to set window_clamp from bpf setsockops'

Prankur gupta says:

====================

This patch contains support to set tcp window_field field from bpf setsockops.

v2: Used TCP_WINDOW_CLAMP setsockopt logic for bpf_setsockopt (review comment addressed)

v3: Created a common function for duplicated code (review comment addressed)

v4: Removing logic to pass struct sock and struct tcp_sock together (review comment addressed)
====================
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 2faa7328 55144f31
...@@ -406,6 +406,7 @@ void tcp_syn_ack_timeout(const struct request_sock *req); ...@@ -406,6 +406,7 @@ void tcp_syn_ack_timeout(const struct request_sock *req);
int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock, int tcp_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, int nonblock,
int flags, int *addr_len); int flags, int *addr_len);
int tcp_set_rcvlowat(struct sock *sk, int val); int tcp_set_rcvlowat(struct sock *sk, int val);
int tcp_set_window_clamp(struct sock *sk, int val);
void tcp_data_ready(struct sock *sk); void tcp_data_ready(struct sock *sk);
#ifdef CONFIG_MMU #ifdef CONFIG_MMU
int tcp_mmap(struct file *file, struct socket *sock, int tcp_mmap(struct file *file, struct socket *sock,
......
...@@ -4910,6 +4910,9 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname, ...@@ -4910,6 +4910,9 @@ static int _bpf_setsockopt(struct sock *sk, int level, int optname,
tp->notsent_lowat = val; tp->notsent_lowat = val;
sk->sk_write_space(sk); sk->sk_write_space(sk);
break; break;
case TCP_WINDOW_CLAMP:
ret = tcp_set_window_clamp(sk, val);
break;
default: default:
ret = -EINVAL; ret = -EINVAL;
} }
......
...@@ -3022,6 +3022,21 @@ int tcp_sock_set_keepcnt(struct sock *sk, int val) ...@@ -3022,6 +3022,21 @@ int tcp_sock_set_keepcnt(struct sock *sk, int val)
} }
EXPORT_SYMBOL(tcp_sock_set_keepcnt); EXPORT_SYMBOL(tcp_sock_set_keepcnt);
int tcp_set_window_clamp(struct sock *sk, int val)
{
struct tcp_sock *tp = tcp_sk(sk);
if (!val) {
if (sk->sk_state != TCP_CLOSE)
return -EINVAL;
tp->window_clamp = 0;
} else {
tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
SOCK_MIN_RCVBUF / 2 : val;
}
return 0;
}
/* /*
* Socket option code for TCP. * Socket option code for TCP.
*/ */
...@@ -3235,15 +3250,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname, ...@@ -3235,15 +3250,7 @@ static int do_tcp_setsockopt(struct sock *sk, int level, int optname,
break; break;
case TCP_WINDOW_CLAMP: case TCP_WINDOW_CLAMP:
if (!val) { err = tcp_set_window_clamp(sk, val);
if (sk->sk_state != TCP_CLOSE) {
err = -EINVAL;
break;
}
tp->window_clamp = 0;
} else
tp->window_clamp = val < SOCK_MIN_RCVBUF / 2 ?
SOCK_MIN_RCVBUF / 2 : val;
break; break;
case TCP_QUICKACK: case TCP_QUICKACK:
......
...@@ -56,6 +56,7 @@ struct tcp_sock { ...@@ -56,6 +56,7 @@ struct tcp_sock {
__u32 rcv_nxt; __u32 rcv_nxt;
__u32 snd_nxt; __u32 snd_nxt;
__u32 snd_una; __u32 snd_una;
__u32 window_clamp;
__u8 ecn_flags; __u8 ecn_flags;
__u32 delivered; __u32 delivered;
__u32 delivered_ce; __u32 delivered_ce;
......
...@@ -42,6 +42,10 @@ static void verify_result(struct tcpbpf_globals *result) ...@@ -42,6 +42,10 @@ static void verify_result(struct tcpbpf_globals *result)
/* check getsockopt for SAVED_SYN */ /* check getsockopt for SAVED_SYN */
ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn"); ASSERT_EQ(result->tcp_saved_syn, 1, "tcp_saved_syn");
/* check getsockopt for window_clamp */
ASSERT_EQ(result->window_clamp_client, 9216, "window_clamp_client");
ASSERT_EQ(result->window_clamp_server, 9216, "window_clamp_server");
} }
static void run_test(struct tcpbpf_globals *result) static void run_test(struct tcpbpf_globals *result)
......
...@@ -12,17 +12,41 @@ ...@@ -12,17 +12,41 @@
#include <linux/tcp.h> #include <linux/tcp.h>
#include <bpf/bpf_helpers.h> #include <bpf/bpf_helpers.h>
#include <bpf/bpf_endian.h> #include <bpf/bpf_endian.h>
#include "bpf_tcp_helpers.h"
#include "test_tcpbpf.h" #include "test_tcpbpf.h"
struct tcpbpf_globals global = {}; struct tcpbpf_globals global = {};
int _version SEC("version") = 1; int _version SEC("version") = 1;
/**
* SOL_TCP is defined in <netinet/tcp.h> while
* TCP_SAVED_SYN is defined in already included <linux/tcp.h>
*/
#ifndef SOL_TCP
#define SOL_TCP 6
#endif
static __always_inline int get_tp_window_clamp(struct bpf_sock_ops *skops)
{
struct bpf_sock *sk;
struct tcp_sock *tp;
sk = skops->sk;
if (!sk)
return -1;
tp = bpf_skc_to_tcp_sock(sk);
if (!tp)
return -1;
return tp->window_clamp;
}
SEC("sockops") SEC("sockops")
int bpf_testcb(struct bpf_sock_ops *skops) int bpf_testcb(struct bpf_sock_ops *skops)
{ {
char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)]; char header[sizeof(struct ipv6hdr) + sizeof(struct tcphdr)];
struct bpf_sock_ops *reuse = skops; struct bpf_sock_ops *reuse = skops;
struct tcphdr *thdr; struct tcphdr *thdr;
int window_clamp = 9216;
int good_call_rv = 0; int good_call_rv = 0;
int bad_call_rv = 0; int bad_call_rv = 0;
int save_syn = 1; int save_syn = 1;
...@@ -75,6 +99,11 @@ int bpf_testcb(struct bpf_sock_ops *skops) ...@@ -75,6 +99,11 @@ int bpf_testcb(struct bpf_sock_ops *skops)
global.event_map |= (1 << op); global.event_map |= (1 << op);
switch (op) { switch (op) {
case BPF_SOCK_OPS_TCP_CONNECT_CB:
rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP,
&window_clamp, sizeof(window_clamp));
global.window_clamp_client = get_tp_window_clamp(skops);
break;
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB:
/* Test failure to set largest cb flag (assumes not defined) */ /* Test failure to set largest cb flag (assumes not defined) */
global.bad_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 0x80); global.bad_cb_test_rv = bpf_sock_ops_cb_flags_set(skops, 0x80);
...@@ -100,6 +129,10 @@ int bpf_testcb(struct bpf_sock_ops *skops) ...@@ -100,6 +129,10 @@ int bpf_testcb(struct bpf_sock_ops *skops)
global.tcp_saved_syn = v; global.tcp_saved_syn = v;
} }
} }
rv = bpf_setsockopt(skops, SOL_TCP, TCP_WINDOW_CLAMP,
&window_clamp, sizeof(window_clamp));
global.window_clamp_server = get_tp_window_clamp(skops);
break; break;
case BPF_SOCK_OPS_RTO_CB: case BPF_SOCK_OPS_RTO_CB:
break; break;
......
...@@ -16,5 +16,7 @@ struct tcpbpf_globals { ...@@ -16,5 +16,7 @@ struct tcpbpf_globals {
__u32 num_close_events; __u32 num_close_events;
__u32 tcp_save_syn; __u32 tcp_save_syn;
__u32 tcp_saved_syn; __u32 tcp_saved_syn;
__u32 window_clamp_client;
__u32 window_clamp_server;
}; };
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment