Skip to contentSkip to conversation

Add support for bpf_setsockopt and

PATCH Applied Prankur gupta 3 participants 5 messages Aug 17, 2021 – Aug 20, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v2 cover Add support for bpf_setsockopt and

v2: Added details about the test in commit log

This patch contains support to set and get socket options from setsockopt
bpf program.
This enables us to set multiple socket option when the user changes a particular socket option.
Example use case, when the user sets the IPV6_TCLASS socket option we would also like to change the tcp-cc for that socket. We don't have any use case for calling bpf_setsockopt from supposedly read-only sys_getsockopt, so it is made available to BPF_CGROUP_SETSOCKOPT only.

Prankur gupta (2):
bpf: Add support for {set|get} socket options from setsockopt BPF selftests/bpf: Add test for {set|get} socket option from setsockopt
BPF program

 kernel/bpf/cgroup.c                           |  8 +++
 tools/testing/selftests/bpf/bpf_tcp_helpers.h | 18 +++++
 .../bpf/prog_tests/sockopt_qos_to_cc.c        | 70 +++++++++++++++++++
 .../selftests/bpf/progs/sockopt_qos_to_cc.c   | 39 +++++++++++
 4 files changed, 135 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockopt_qos_to_cc.c
 create mode 100644 tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/2 bpf: Add support for {set|get} socket options from setsockopt BPF

Add logic to call bpf_setsockopt and bpf_getsockopt from setsockopt BPF programs.
Example use case, when the user sets the IPV6_TCLASS socket option we would also like to change the tcp-cc for that socket. We don't have any use case for calling bpf_setsockopt from supposedly read-only sys_getsockopti, so it is made available to BPF_CGROUP_SETSOCKOPT only.

---
 kernel/bpf/cgroup.c | 8 ++++++++
 1 file changed, 8 insertions(+)
kernel/bpf/cgroup.c +8 -0
index 9f35928bab0a..8e9d99e2ade4 100644
@@ -1873,6 +1873,14 @@ cg_sockopt_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 		return &bpf_sk_storage_get_proto;
 	case BPF_FUNC_sk_storage_delete:
 		return &bpf_sk_storage_delete_proto;
+	case BPF_FUNC_setsockopt:
+		if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
+			return &bpf_sk_setsockopt_proto;
+		return NULL;
+	case BPF_FUNC_getsockopt:
+		if (prog->expected_attach_type == BPF_CGROUP_SETSOCKOPT)
+			return &bpf_sk_getsockopt_proto;
+		return NULL;
 #endif
 #ifdef CONFIG_INET
 	case BPF_FUNC_tcp_sock:
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/2 selftests/bpf: Add test for {set|get} socket option from setsockopt BPF program

In reply to Prankur gupta

Adding selftests for new added functionality to call bpf_setsockopt and bpf_getsockopt from setsockopt BPF programs

Test Details:
1. BPF Program
Checks for changes in IPV6_TCLASS(SOL_IPV6) via setsockopt If the cca for the socket is not cubic do nothing
If the newly set value for IPV6_TCLASS is 45 (0x2d) (as per our usecase) then change the cc from cubic to reno

2. User Space Program
Creates an AF_INET6 socket and set the cca for that to be "cubic" Attach the program and set the IPV6_TCLASS to 0x2d using setsockopt Verify the cca for the socket changed to reno

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h | 18 +++++
 .../bpf/prog_tests/sockopt_qos_to_cc.c        | 70 +++++++++++++++++++
 .../selftests/bpf/progs/sockopt_qos_to_cc.c   | 39 +++++++++++
 3 files changed, 127 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/sockopt_qos_to_cc.c
 create mode 100644 tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c
tools/testing/selftests/bpf/bpf_tcp_helpers.h +18 -0
index 029589c008c9..c9f9bdad60c7 100644
@@ -12,6 +12,10 @@
 SEC("struct_ops/"#name) \
 BPF_PROG(name, args)
 
+#ifndef SOL_TCP
+#define SOL_TCP 6
+#endif
+
 #define tcp_jiffies32 ((__u32)bpf_jiffies64())
 
 struct sock_common {
@@ -203,6 +207,20 @@ static __always_inline bool tcp_is_cwnd_limited(const struct sock *sk)
 	return !!BPF_CORE_READ_BITFIELD(tp, is_cwnd_limited);
 }
 
+static __always_inline bool tcp_cc_eq(const char *a, const char *b)
+{
+	int i;
+
+	for (i = 0; i < TCP_CA_NAME_MAX; i++) {
+		if (a[i] != b[i])
+			return false;
+		if (!a[i])
+			break;
+	}
+
+	return true;
+}
+
 extern __u32 tcp_slow_start(struct tcp_sock *tp, __u32 acked) __ksym;
 extern void tcp_cong_avoid_ai(struct tcp_sock *tp, __u32 w, __u32 acked) __ksym;
 
tools/testing/selftests/bpf/prog_tests/sockopt_qos_to_cc.c new file +70 -0

70 lines not on this page · raw message

tools/testing/selftests/bpf/progs/sockopt_qos_to_cc.c new file +39 -0
new file mode 100644
index 000000000000..1bce83b6e3a7
@@ -0,0 +1,39 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2021 Facebook */
+#include <string.h>
+#include <linux/tcp.h>
+#include <netinet/in.h>
+#include <linux/bpf.h>
+#include <bpf/bpf_helpers.h>
+#include "bpf_tcp_helpers.h"
+
+char _license[] SEC("license") = "GPL";
+
+SEC("cgroup/setsockopt")
+int sockopt_qos_to_cc(struct bpf_sockopt *ctx)
+{
+	void *optval_end = ctx->optval_end;
+	int *optval = ctx->optval;
+	char buf[TCP_CA_NAME_MAX];
+	char cc_reno[TCP_CA_NAME_MAX] = "reno";
+	char cc_cubic[TCP_CA_NAME_MAX] = "cubic";
+
+	if (ctx->level != SOL_IPV6 || ctx->optname != IPV6_TCLASS)
+		return 1;
+
+	if (optval + 1 > optval_end)
+		return 0; /* EPERM, bounds check */
+
+	if (bpf_getsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &buf, sizeof(buf)))
+		return 0;
+
+	if (!tcp_cc_eq(buf, cc_cubic))
+		return 0;
+
+	if (*optval == 0x2d) {
+		if (bpf_setsockopt(ctx->sk, SOL_TCP, TCP_CONGESTION, &cc_reno,
+				sizeof(cc_reno)))
+			return 0;
+	}
+	return 1;
+}
Signature · 1 line
2.30.2
reviewer Acked-by on v2 2/2
#3 lore raw

Re: [PATCH bpf-next 2/2] selftests/bpf: Add test for {set|get} socket option from setsockopt BPF program

Quoted 11 lines · Prankur gupta

Adding selftests for new added functionality to call bpf_setsockopt and bpf_getsockopt from setsockopt BPF programs

Test Details:
1. BPF Program
Checks for changes in IPV6_TCLASS(SOL_IPV6) via setsockopt If the cca for the socket is not cubic do nothing
If the newly set value for IPV6_TCLASS is 45 (0x2d) (as per our usecase) then change the cc from cubic to reno

Quoted 6 lines

2. User Space Program
Creates an AF_INET6 socket and set the cca for that to be "cubic" Attach the program and set the IPV6_TCLASS to 0x2d using setsockopt Verify the cca for the socket changed to reno

#4 lore raw
In reply to Prankur gupta
This series was applied to bpf/bpf-next.git (refs/heads/master):

Hello:

This series was applied to bpf/bpf-next.git (refs/heads/master):

Quoted 12 lines · On Tue, 17 Aug 2021 15:42:19 -0700 you wrote:

v2: Added details about the test in commit log

This patch contains support to set and get socket options from setsockopt
bpf program.
This enables us to set multiple socket option when the user changes a particular socket option.
Example use case, when the user sets the IPV6_TCLASS socket option we would also like to change the tcp-cc for that socket. We don't have any use case for calling bpf_setsockopt from supposedly read-only sys_getsockopt, so it is made available to BPF_CGROUP_SETSOCKOPT only.

Quoted 1 line

[...]

Here is the summary with links:
- [bpf-next,1/2] bpf: Add support for {set|get} socket options from setsockopt BPF

    https://git.kernel.org/bpf/bpf-next/c/2c531639deb5
  - [bpf-next,2/2] selftests/bpf: Add test for {set|get} socket option from setsockopt BPF program
    https://git.kernel.org/bpf/bpf-next/c/f2a6ee924d26

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html