Skip to content

Commit fd5ef31

Browse files
fomichevAlexei Starovoitov
authored andcommitted
selftests/bpf: extend sockopt_sk selftest with TCP_CONGESTION use case
Ignore SOL_TCP:TCP_CONGESTION in getsockopt and always override SOL_TCP:TCP_CONGESTION with "cubic" in setsockopt hook. Call setsockopt(SOL_TCP, TCP_CONGESTION) with short optval ("nv") to make sure BPF program has enough buffer space to replace it with "cubic". Signed-off-by: Stanislav Fomichev <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 9babe82 commit fd5ef31

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

tools/testing/selftests/bpf/progs/sockopt_sk.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
2+
#include <string.h>
23
#include <netinet/in.h>
4+
#include <netinet/tcp.h>
35
#include <linux/bpf.h>
46
#include "bpf_helpers.h"
57

@@ -42,6 +44,14 @@ int _getsockopt(struct bpf_sockopt *ctx)
4244
return 1;
4345
}
4446

47+
if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
48+
/* Not interested in SOL_TCP:TCP_CONGESTION;
49+
* let next BPF program in the cgroup chain or kernel
50+
* handle it.
51+
*/
52+
return 1;
53+
}
54+
4555
if (ctx->level != SOL_CUSTOM)
4656
return 0; /* EPERM, deny everything except custom level */
4757

@@ -91,6 +101,18 @@ int _setsockopt(struct bpf_sockopt *ctx)
91101
return 1;
92102
}
93103

104+
if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
105+
/* Always use cubic */
106+
107+
if (optval + 5 > optval_end)
108+
return 0; /* EPERM, bounds check */
109+
110+
memcpy(optval, "cubic", 5);
111+
ctx->optlen = 5;
112+
113+
return 1;
114+
}
115+
94116
if (ctx->level != SOL_CUSTOM)
95117
return 0; /* EPERM, deny everything except custom level */
96118

tools/testing/selftests/bpf/test_sockopt_sk.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <sys/types.h>
77
#include <sys/socket.h>
88
#include <netinet/in.h>
9+
#include <netinet/tcp.h>
910

1011
#include <linux/filter.h>
1112
#include <bpf/bpf.h>
@@ -25,6 +26,7 @@ static int getsetsockopt(void)
2526
union {
2627
char u8[4];
2728
__u32 u32;
29+
char cc[16]; /* TCP_CA_NAME_MAX */
2830
} buf = {};
2931
socklen_t optlen;
3032

@@ -115,6 +117,29 @@ static int getsetsockopt(void)
115117
goto err;
116118
}
117119

120+
/* TCP_CONGESTION can extend the string */
121+
122+
strcpy(buf.cc, "nv");
123+
err = setsockopt(fd, SOL_TCP, TCP_CONGESTION, &buf, strlen("nv"));
124+
if (err) {
125+
log_err("Failed to call setsockopt(TCP_CONGESTION)");
126+
goto err;
127+
}
128+
129+
130+
optlen = sizeof(buf.cc);
131+
err = getsockopt(fd, SOL_TCP, TCP_CONGESTION, &buf, &optlen);
132+
if (err) {
133+
log_err("Failed to call getsockopt(TCP_CONGESTION)");
134+
goto err;
135+
}
136+
137+
if (strcmp(buf.cc, "cubic") != 0) {
138+
log_err("Unexpected getsockopt(TCP_CONGESTION) %s != %s",
139+
buf.cc, "cubic");
140+
goto err;
141+
}
142+
118143
close(fd);
119144
return 0;
120145
err:

0 commit comments

Comments
 (0)