Skip to contentSkip to conversation

bpf: tcp: Add bpf_cubic example

PATCH Superseded Martin KaFai Lau 2 participants 6 messages Jan 22, 2020

Subsystems (7)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover bpf: tcp: Add bpf_cubic example

This set adds bpf_cubic.c example. It was separated from the earlier BPF STRUCT_OPS series. Some highlights since the last post:

1. It is based on EricD recent fixes to the kernel tcp_cubic. [1]
2. The bpf jiffies reading helper is inlined by the verifier. Different from the earlier version, it only reads jiffies alone and does not do usecs/jiffies conversion.
3. The bpf .kconfig map is used to read CONFIG_HZ.

[1]: https://patchwork.ozlabs.org/cover/1215066/

v2:
- Move inlining to fixup_bpf_calls() in patch 1. (Daniel)
- It is inlined for 64 BITS_PER_LONG and jit_requested as the map_gen_lookup(). Other cases could be considered together with map_gen_lookup() if needed.
- Use usec resolution in bictcp_update() calculation in patch 3. usecs_to_jiffies() is then removed(). (Eric)

Martin KaFai Lau (3):
bpf: Add BPF_FUNC_jiffies64
bpf: Sync uapi bpf.h to tools/
bpf: tcp: Add bpf_cubic example

 include/linux/bpf.h                           |   1 +
 include/uapi/linux/bpf.h                      |   9 +-
 kernel/bpf/core.c                             |   1 +
 kernel/bpf/helpers.c                          |  12 +
 kernel/bpf/verifier.c                         |  24 +
 net/core/filter.c                             |   2 +
 tools/include/uapi/linux/bpf.h                |   9 +-
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  16 +
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     |  25 +
 tools/testing/selftests/bpf/progs/bpf_cubic.c | 544 ++++++++++++++++++
 10 files changed, 641 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_cubic.c
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/3 bpf: Add BPF_FUNC_jiffies64

This patch adds a helper to read the 64bit jiffies. It will be used in a later patch to implement the bpf_cubic.c.

The helper is inlined for jit_requested and 64 BITS_PER_LONG as the map_gen_lookup(). Other cases could be considered together with map_gen_lookup() if needed.

---
 include/linux/bpf.h      |  1 +
 include/uapi/linux/bpf.h |  9 ++++++++-
 kernel/bpf/core.c        |  1 +
 kernel/bpf/helpers.c     | 12 ++++++++++++
 kernel/bpf/verifier.c    | 24 ++++++++++++++++++++++++
 net/core/filter.c        |  2 ++
 6 files changed, 48 insertions(+), 1 deletion(-)
include/linux/bpf.h +1 -0
index 8e3b8f4ad183..81edd081c00d 100644
@@ -1406,6 +1406,7 @@ extern const struct bpf_func_proto bpf_get_local_storage_proto;
 extern const struct bpf_func_proto bpf_strtol_proto;
 extern const struct bpf_func_proto bpf_strtoul_proto;
 extern const struct bpf_func_proto bpf_tcp_sock_proto;
+extern const struct bpf_func_proto bpf_jiffies64_proto;
 
 /* Shared helpers among cBPF and eBPF. */
 void bpf_user_rnd_init_once(void);
include/uapi/linux/bpf.h +8 -1
index 033d90a2282d..d17c6bcd50cd 100644
@@ -2885,6 +2885,12 @@ union bpf_attr {
  *		**-EPERM** if no permission to send the *sig*.
  *
  *		**-EAGAIN** if bpf program can try again.
+ *
+ * u64 bpf_jiffies64(void)
+ *	Description
+ *		Obtain the 64bit jiffies
+ *	Return
+ *		The 64 bit jiffies
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3004,7 +3010,8 @@ union bpf_attr {
 	FN(probe_read_user_str),	\
 	FN(probe_read_kernel_str),	\
 	FN(tcp_send_ack),		\
-	FN(send_signal_thread),
+	FN(send_signal_thread),		\
+	FN(jiffies64),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
kernel/bpf/core.c +1 -0
index 29d47aae0dd1..973a20d49749 100644
@@ -2137,6 +2137,7 @@ const struct bpf_func_proto bpf_map_pop_elem_proto __weak;
 const struct bpf_func_proto bpf_map_peek_elem_proto __weak;
 const struct bpf_func_proto bpf_spin_lock_proto __weak;
 const struct bpf_func_proto bpf_spin_unlock_proto __weak;
+const struct bpf_func_proto bpf_jiffies64_proto __weak;
 
 const struct bpf_func_proto bpf_get_prandom_u32_proto __weak;
 const struct bpf_func_proto bpf_get_smp_processor_id_proto __weak;
kernel/bpf/helpers.c +12 -0
index cada974c9f4e..d8b7b110a1c5 100644
@@ -11,6 +11,7 @@
 #include <linux/uidgid.h>
 #include <linux/filter.h>
 #include <linux/ctype.h>
+#include <linux/jiffies.h>
 
 #include "../../lib/kstrtox.h"
 
@@ -312,6 +313,17 @@ void copy_map_value_locked(struct bpf_map *map, void *dst, void *src,
 	preempt_enable();
 }
 
+BPF_CALL_0(bpf_jiffies64)
+{
+	return get_jiffies_64();
+}
+
+const struct bpf_func_proto bpf_jiffies64_proto = {
+	.func		= bpf_jiffies64,
+	.gpl_only	= false,
+	.ret_type	= RET_INTEGER,
+};
+
 #ifdef CONFIG_CGROUPS
 BPF_CALL_0(bpf_get_current_cgroup_id)
 {
kernel/bpf/verifier.c +24 -0
index ca17dccc17ba..41915b335813 100644
@@ -9445,6 +9445,30 @@ static int fixup_bpf_calls(struct bpf_verifier_env *env)
 			goto patch_call_imm;
 		}
 
+		if (prog->jit_requested && BITS_PER_LONG == 64 &&
+		    insn->imm == BPF_FUNC_jiffies64) {
+			struct bpf_insn ld_jiffies_addr[2] = {
+				BPF_LD_IMM64(BPF_REG_0,
+					     (unsigned long)&jiffies),
+			};
+
+			insn_buf[0] = ld_jiffies_addr[0];
+			insn_buf[1] = ld_jiffies_addr[1];
+			insn_buf[2] = BPF_LDX_MEM(BPF_DW, BPF_REG_0,
+						  BPF_REG_0, 0);
+			cnt = 3;
+
+			new_prog = bpf_patch_insn_data(env, i + delta, insn_buf,
+						       cnt);
+			if (!new_prog)
+				return -ENOMEM;
+
+			delta    += cnt - 1;
+			env->prog = prog = new_prog;
+			insn      = new_prog->insnsi + i + delta;
+			continue;
+		}
+
 patch_call_imm:
 		fn = env->ops->get_func_proto(insn->imm, env->prog);
 		/* all functions that have prototype and verifier allowed
net/core/filter.c +2 -0
index 17de6747d9e3..4bf3e4aa8a7a 100644
@@ -5923,6 +5923,8 @@ bpf_base_func_proto(enum bpf_func_id func_id)
 		return &bpf_spin_unlock_proto;
 	case BPF_FUNC_trace_printk:
 		return bpf_get_trace_printk_proto();
+	case BPF_FUNC_jiffies64:
+		return &bpf_jiffies64_proto;
 	default:
 		return NULL;
 	}
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/3 bpf: Sync uapi bpf.h to tools/

In reply to Martin KaFai Lau

This patch sync uapi bpf.h to tools/.

---
 tools/include/uapi/linux/bpf.h | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
tools/include/uapi/linux/bpf.h +8 -1
index 033d90a2282d..d17c6bcd50cd 100644
@@ -2885,6 +2885,12 @@ union bpf_attr {
  *		**-EPERM** if no permission to send the *sig*.
  *
  *		**-EAGAIN** if bpf program can try again.
+ *
+ * u64 bpf_jiffies64(void)
+ *	Description
+ *		Obtain the 64bit jiffies
+ *	Return
+ *		The 64 bit jiffies
  */
 #define __BPF_FUNC_MAPPER(FN)		\
 	FN(unspec),			\
@@ -3004,7 +3010,8 @@ union bpf_attr {
 	FN(probe_read_user_str),	\
 	FN(probe_read_kernel_str),	\
 	FN(tcp_send_ack),		\
-	FN(send_signal_thread),
+	FN(send_signal_thread),		\
+	FN(jiffies64),
 
 /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  * function eBPF program intends to call
Signature · 1 line
2.17.1
#3 lore raw

PATCH v2 3/3 bpf: tcp: Add bpf_cubic example

In reply to Martin KaFai Lau

This patch adds a bpf_cubic example. Some highlights:
1. CONFIG_HZ .kconfig map is used.
2. In bictcp_update(), calculation is changed to use usec resolution (i.e. USEC_PER_JIFFY) instead of using jiffies. Thus, usecs_to_jiffies() is not used in the bpf_cubic.c.
3. In bitctcp_update() [under tcp_friendliness], the original "while (ca->ack_cnt > delta)" loop is changed to the equivalent "ca->ack_cnt / delta" operation.

---
 tools/testing/selftests/bpf/bpf_tcp_helpers.h |  16 +
 .../selftests/bpf/prog_tests/bpf_tcp_ca.c     |  25 +
 tools/testing/selftests/bpf/progs/bpf_cubic.c | 544 ++++++++++++++++++
 3 files changed, 585 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_cubic.c
tools/testing/selftests/bpf/bpf_tcp_helpers.h +16 -0
index 6fee732f0297..8f21965ffc6c 100644
@@ -6,13 +6,28 @@
 #include <linux/types.h>
 #include <bpf/bpf_helpers.h>
 #include <bpf/bpf_core_read.h>
+#include "bpf_trace_helpers.h"
+
+#define BPF_STRUCT_OPS(name, args...) \
+SEC("struct_ops/"#name) \
+BPF_PROG(name, args)
+
+#define tcp_jiffies32 ((__u32)bpf_jiffies64())
 
 struct sock_common {
 	unsigned char	skc_state;
 } __attribute__((preserve_access_index));
 
+enum sk_pacing {
+	SK_PACING_NONE		= 0,
+	SK_PACING_NEEDED	= 1,
+	SK_PACING_FQ		= 2,
+};
+
 struct sock {
 	struct sock_common	__sk_common;
+	unsigned long		sk_pacing_rate;
+	__u32			sk_pacing_status; /* see enum sk_pacing */
 } __attribute__((preserve_access_index));
 
 struct inet_sock {
@@ -54,6 +69,7 @@ struct tcp_sock {
 	__u32	max_packets_out;
 	__u32	lsndtime;
 	__u32	prior_cwnd;
+	__u64	tcp_mstamp;	/* most recent packet received/sent */
 } __attribute__((preserve_access_index));
 
 static __always_inline struct inet_connection_sock *inet_csk(const struct sock *sk)
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +25 -0
index 517318f05b1d..8482bbc67eec 100644
@@ -4,6 +4,7 @@
 #include <linux/err.h>
 #include <test_progs.h>
 #include "bpf_dctcp.skel.h"
+#include "bpf_cubic.skel.h"
 
 #define min(a, b) ((a) < (b) ? (a) : (b))
 
@@ -158,6 +159,28 @@ static void do_test(const char *tcp_ca)
 	close(fd);
 }
 
+static void test_cubic(void)
+{
+	struct bpf_cubic *cubic_skel;
+	struct bpf_link *link;
+
+	cubic_skel = bpf_cubic__open_and_load();
+	if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
+		return;
+
+	link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);
+	if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n",
+		  PTR_ERR(link))) {
+		bpf_cubic__destroy(cubic_skel);
+		return;
+	}
+
+	do_test("bpf_cubic");
+
+	bpf_link__destroy(link);
+	bpf_cubic__destroy(cubic_skel);
+}
+
 static void test_dctcp(void)
 {
 	struct bpf_dctcp *dctcp_skel;
@@ -184,4 +207,6 @@ void test_bpf_tcp_ca(void)
 {
 	if (test__start_subtest("dctcp"))
 		test_dctcp();
+	if (test__start_subtest("cubic"))
+		test_cubic();
 }
tools/testing/selftests/bpf/progs/bpf_cubic.c new file +544 -0

544 lines not on this page · raw message

Signature · 1 line
2.17.1
#4 lore raw
Quoted 10 lines · Martin KaFai Lau

This patch adds a bpf_cubic example. Some highlights:
1. CONFIG_HZ .kconfig map is used.
2. In bictcp_update(), calculation is changed to use usec resolution (i.e. USEC_PER_JIFFY) instead of using jiffies. Thus, usecs_to_jiffies() is not used in the bpf_cubic.c.
3. In bitctcp_update() [under tcp_friendliness], the original "while (ca->ack_cnt > delta)" loop is changed to the equivalent "ca->ack_cnt / delta" operation.

Quoted 2 lines

---

just my few cents below...

[...]

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c static void do_test(const char *tcp_ca) · line 171

Quoted 11 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +10 -0
+static void test_cubic(void)
+{
+       struct bpf_cubic *cubic_skel;
+       struct bpf_link *link;
+
+       cubic_skel = bpf_cubic__open_and_load();
+       if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
+               return;
+
+       link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);

we should probably teach bpftool and libbpf to generate a link for struct_ops map and also auto-attach it as part of skeleton's attach... I'll add it if noone gets to it sooner

Quoted 12 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +12 -0
+       if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n",
+                 PTR_ERR(link))) {
+               bpf_cubic__destroy(cubic_skel);
+               return;
+       }
+
+       do_test("bpf_cubic");
+
+       bpf_link__destroy(link);
+       bpf_cubic__destroy(cubic_skel);
+}
+

[...]

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+extern unsigned long CONFIG_HZ __kconfig __weak;

you probably don't want __weak, if CONFIG_HZ is not defined in Kconfig, then something wrong is going on, probably, so it's better to error out early

Quoted 5 lines
tools/testing/selftests/bpf/progs/bpf_cubic.c new file +5 -0
+#define HZ CONFIG_HZ
+#define USEC_PER_MSEC  1000UL
+#define USEC_PER_SEC   1000000UL
+#define USEC_PER_JIFFY (USEC_PER_SEC / HZ)
+

[...]

author
#5 lore raw
Quoted 19 lines · Andrii Nakryiko
Quoted 10 lines · Martin KaFai Lau

This patch adds a bpf_cubic example. Some highlights:
1. CONFIG_HZ .kconfig map is used.
2. In bictcp_update(), calculation is changed to use usec resolution (i.e. USEC_PER_JIFFY) instead of using jiffies. Thus, usecs_to_jiffies() is not used in the bpf_cubic.c.
3. In bitctcp_update() [under tcp_friendliness], the original "while (ca->ack_cnt > delta)" loop is changed to the equivalent "ca->ack_cnt / delta" operation.

Quoted 2 lines

---

just my few cents below...

[...]

Quoted 1 line
Quoted 14 lines

tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c static void do_test(const char *tcp_ca) · line 171

Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +10 -0
+static void test_cubic(void)
+{
+       struct bpf_cubic *cubic_skel;
+       struct bpf_link *link;
+
+       cubic_skel = bpf_cubic__open_and_load();
+       if (CHECK(!cubic_skel, "bpf_cubic__open_and_load", "failed\n"))
+               return;
+
+       link = bpf_map__attach_struct_ops(cubic_skel->maps.cubic);

we should probably teach bpftool and libbpf to generate a link for struct_ops map and also auto-attach it as part of skeleton's attach... I'll add it if noone gets to it sooner

It is in my TODO list. I am happy to be beaten though ;)

Quoted 16 lines
Quoted 13 lines
tools/testing/selftests/bpf/prog_tests/bpf_tcp_ca.c +12 -0
+       if (CHECK(IS_ERR(link), "bpf_map__attach_struct_ops", "err:%ld\n",
+                 PTR_ERR(link))) {
+               bpf_cubic__destroy(cubic_skel);
+               return;
+       }
+
+       do_test("bpf_cubic");
+
+       bpf_link__destroy(link);
+       bpf_cubic__destroy(cubic_skel);
+}
+

[...]

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+extern unsigned long CONFIG_HZ __kconfig __weak;

you probably don't want __weak, if CONFIG_HZ is not defined in Kconfig, then something wrong is going on, probably, so it's better to error out early

Thanks for pointing it out. Will do.