Skip to content

Commit

Permalink
selftests/bpf: extend sockopt_sk selftest with TCP_CONGESTION use case
Browse files Browse the repository at this point in the history
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 <sdf@google.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
  • Loading branch information
Stanislav Fomichev authored and Alexei Starovoitov committed Aug 1, 2019
1 parent 9babe82 commit fd5ef31
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tools/testing/selftests/bpf/progs/sockopt_sk.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
#include <string.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <linux/bpf.h>
#include "bpf_helpers.h"

Expand Down Expand Up @@ -42,6 +44,14 @@ int _getsockopt(struct bpf_sockopt *ctx)
return 1;
}

if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
/* Not interested in SOL_TCP:TCP_CONGESTION;
* let next BPF program in the cgroup chain or kernel
* handle it.
*/
return 1;
}

if (ctx->level != SOL_CUSTOM)
return 0; /* EPERM, deny everything except custom level */

Expand Down Expand Up @@ -91,6 +101,18 @@ int _setsockopt(struct bpf_sockopt *ctx)
return 1;
}

if (ctx->level == SOL_TCP && ctx->optname == TCP_CONGESTION) {
/* Always use cubic */

if (optval + 5 > optval_end)
return 0; /* EPERM, bounds check */

memcpy(optval, "cubic", 5);
ctx->optlen = 5;

return 1;
}

if (ctx->level != SOL_CUSTOM)
return 0; /* EPERM, deny everything except custom level */

Expand Down
25 changes: 25 additions & 0 deletions tools/testing/selftests/bpf/test_sockopt_sk.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>

#include <linux/filter.h>
#include <bpf/bpf.h>
Expand All @@ -25,6 +26,7 @@ static int getsetsockopt(void)
union {
char u8[4];
__u32 u32;
char cc[16]; /* TCP_CA_NAME_MAX */
} buf = {};
socklen_t optlen;

Expand Down Expand Up @@ -115,6 +117,29 @@ static int getsetsockopt(void)
goto err;
}

/* TCP_CONGESTION can extend the string */

strcpy(buf.cc, "nv");
err = setsockopt(fd, SOL_TCP, TCP_CONGESTION, &buf, strlen("nv"));
if (err) {
log_err("Failed to call setsockopt(TCP_CONGESTION)");
goto err;
}


optlen = sizeof(buf.cc);
err = getsockopt(fd, SOL_TCP, TCP_CONGESTION, &buf, &optlen);
if (err) {
log_err("Failed to call getsockopt(TCP_CONGESTION)");
goto err;
}

if (strcmp(buf.cc, "cubic") != 0) {
log_err("Unexpected getsockopt(TCP_CONGESTION) %s != %s",
buf.cc, "cubic");
goto err;
}

close(fd);
return 0;
err:
Expand Down

0 comments on commit fd5ef31

Please sign in to comment.