netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Hangbin Liu <haliu@redhat.com>
To: Stephen Hemminger <stephen@networkplumber.org>,
	Daniel Borkmann <daniel@iogearbox.net>,
	David Ahern <dsahern@gmail.com>,
	Alexei Starovoitov <ast@kernel.org>
Cc: "Martin KaFai Lau" <kafai@fb.com>,
	"Song Liu" <songliubraving@fb.com>, "Yonghong Song" <yhs@fb.com>,
	"David Miller" <davem@davemloft.net>,
	"Jesper Dangaard Brouer" <brouer@redhat.com>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	"Jiri Benc" <jbenc@redhat.com>,
	"Andrii Nakryiko" <andrii@kernel.org>,
	"Toke Høiland-Jørgensen" <toke@redhat.com>,
	"Hangbin Liu" <haliu@redhat.com>
Subject: [PATCHv2 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps
Date: Wed, 28 Oct 2020 21:25:29 +0800	[thread overview]
Message-ID: <20201028132529.3763875-6-haliu@redhat.com> (raw)
In-Reply-To: <20201028132529.3763875-1-haliu@redhat.com>

Users should try use the new BTF defined maps instead of struct
bpf_elf_map defined maps. The tail call examples are not added yet
as libbpf doesn't currently support declaratively populating tail call
maps.

Reviewed-by: Toke Høiland-Jørgensen <toke@redhat.com>
Signed-off-by: Hangbin Liu <haliu@redhat.com>
---
 examples/bpf/README           |  6 ++++
 examples/bpf/bpf_graft.c      | 66 +++++++++++++++++++++++++++++++++++
 examples/bpf/bpf_map_in_map.c | 55 +++++++++++++++++++++++++++++
 examples/bpf/bpf_shared.c     | 53 ++++++++++++++++++++++++++++
 include/bpf_api.h             | 13 +++++++
 5 files changed, 193 insertions(+)
 create mode 100644 examples/bpf/bpf_graft.c
 create mode 100644 examples/bpf/bpf_map_in_map.c
 create mode 100644 examples/bpf/bpf_shared.c

diff --git a/examples/bpf/README b/examples/bpf/README
index 732bcc83..b7261191 100644
--- a/examples/bpf/README
+++ b/examples/bpf/README
@@ -1,6 +1,12 @@
 eBPF toy code examples (running in kernel) to familiarize yourself
 with syntax and features:
 
+- BTF defined map examples
+ - bpf_graft.c		-> Demo on altering runtime behaviour
+ - bpf_shared.c 	-> Ingress/egress map sharing example
+ - bpf_map_in_map.c	-> Using map in map example
+
+- legacy struct bpf_elf_map defined map examples
  - legacy/bpf_shared.c		-> Ingress/egress map sharing example
  - legacy/bpf_tailcall.c	-> Using tail call chains
  - legacy/bpf_cyclic.c		-> Simple cycle as tail calls
diff --git a/examples/bpf/bpf_graft.c b/examples/bpf/bpf_graft.c
new file mode 100644
index 00000000..8066dcce
--- /dev/null
+++ b/examples/bpf/bpf_graft.c
@@ -0,0 +1,66 @@
+#include "../../include/bpf_api.h"
+
+/* This example demonstrates how classifier run-time behaviour
+ * can be altered with tail calls. We start out with an empty
+ * jmp_tc array, then add section aaa to the array slot 0, and
+ * later on atomically replace it with section bbb. Note that
+ * as shown in other examples, the tc loader can prepopulate
+ * tail called sections, here we start out with an empty one
+ * on purpose to show it can also be done this way.
+ *
+ * tc filter add dev foo parent ffff: bpf obj graft.o
+ * tc exec bpf dbg
+ *   [...]
+ *   Socket Thread-20229 [001] ..s. 138993.003923: : fallthrough
+ *   <idle>-0            [001] ..s. 138993.202265: : fallthrough
+ *   Socket Thread-20229 [001] ..s. 138994.004149: : fallthrough
+ *   [...]
+ *
+ * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec aaa
+ * tc exec bpf dbg
+ *   [...]
+ *   Socket Thread-19818 [002] ..s. 139012.053587: : aaa
+ *   <idle>-0            [002] ..s. 139012.172359: : aaa
+ *   Socket Thread-19818 [001] ..s. 139012.173556: : aaa
+ *   [...]
+ *
+ * tc exec bpf graft m:globals/jmp_tc key 0 obj graft.o sec bbb
+ * tc exec bpf dbg
+ *   [...]
+ *   Socket Thread-19818 [002] ..s. 139022.102967: : bbb
+ *   <idle>-0            [002] ..s. 139022.155640: : bbb
+ *   Socket Thread-19818 [001] ..s. 139022.156730: : bbb
+ *   [...]
+ */
+
+struct {
+	__uint(type, BPF_MAP_TYPE_PROG_ARRAY);
+	__uint(key_size, sizeof(uint32_t));
+	__uint(value_size, sizeof(uint32_t));
+	__uint(max_entries, 1);
+	__uint(pinning, LIBBPF_PIN_BY_NAME);
+} jmp_tc __section(".maps");
+
+__section("aaa")
+int cls_aaa(struct __sk_buff *skb)
+{
+	printt("aaa\n");
+	return TC_H_MAKE(1, 42);
+}
+
+__section("bbb")
+int cls_bbb(struct __sk_buff *skb)
+{
+	printt("bbb\n");
+	return TC_H_MAKE(1, 43);
+}
+
+__section_cls_entry
+int cls_entry(struct __sk_buff *skb)
+{
+	tail_call(skb, &jmp_tc, 0);
+	printt("fallthrough\n");
+	return BPF_H_DEFAULT;
+}
+
+BPF_LICENSE("GPL");
diff --git a/examples/bpf/bpf_map_in_map.c b/examples/bpf/bpf_map_in_map.c
new file mode 100644
index 00000000..39c86268
--- /dev/null
+++ b/examples/bpf/bpf_map_in_map.c
@@ -0,0 +1,55 @@
+#include "../../include/bpf_api.h"
+
+struct inner_map {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(key_size, sizeof(uint32_t));
+	__uint(value_size, sizeof(uint32_t));
+	__uint(max_entries, 1);
+} map_inner __section(".maps");
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
+	__uint(key_size, sizeof(uint32_t));
+	__uint(value_size, sizeof(uint32_t));
+	__uint(max_entries, 1);
+	__uint(pinning, LIBBPF_PIN_BY_NAME);
+	__array(values, struct inner_map);
+} map_outer __section(".maps") = {
+	.values = {
+		[0] = &map_inner,
+	},
+};
+
+__section("egress")
+int emain(struct __sk_buff *skb)
+{
+	struct bpf_elf_map *map_inner;
+	int key = 0, *val;
+
+	map_inner = map_lookup_elem(&map_outer, &key);
+	if (map_inner) {
+		val = map_lookup_elem(map_inner, &key);
+		if (val)
+			lock_xadd(val, 1);
+	}
+
+	return BPF_H_DEFAULT;
+}
+
+__section("ingress")
+int imain(struct __sk_buff *skb)
+{
+	struct bpf_elf_map *map_inner;
+	int key = 0, *val;
+
+	map_inner = map_lookup_elem(&map_outer, &key);
+	if (map_inner) {
+		val = map_lookup_elem(map_inner, &key);
+		if (val)
+			printt("map val: %d\n", *val);
+	}
+
+	return BPF_H_DEFAULT;
+}
+
+BPF_LICENSE("GPL");
diff --git a/examples/bpf/bpf_shared.c b/examples/bpf/bpf_shared.c
new file mode 100644
index 00000000..99a332f4
--- /dev/null
+++ b/examples/bpf/bpf_shared.c
@@ -0,0 +1,53 @@
+#include "../../include/bpf_api.h"
+
+/* Minimal, stand-alone toy map pinning example:
+ *
+ * clang -target bpf -O2 [...] -o bpf_shared.o -c bpf_shared.c
+ * tc filter add dev foo parent 1: bpf obj bpf_shared.o sec egress
+ * tc filter add dev foo parent ffff: bpf obj bpf_shared.o sec ingress
+ *
+ * Both classifier will share the very same map instance in this example,
+ * so map content can be accessed from ingress *and* egress side!
+ *
+ * This example has a pinning of PIN_OBJECT_NS, so it's private and
+ * thus shared among various program sections within the object.
+ *
+ * A setting of PIN_GLOBAL_NS would place it into a global namespace,
+ * so that it can be shared among different object files. A setting
+ * of PIN_NONE (= 0) means no sharing, so each tc invocation a new map
+ * instance is being created.
+ */
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARRAY);
+	__uint(key_size, sizeof(uint32_t));
+	__uint(value_size, sizeof(uint32_t));
+	__uint(max_entries, 1);
+	__uint(pinning, LIBBPF_PIN_BY_NAME);	/* or LIBBPF_PIN_NONE */
+} map_sh __section(".maps");
+
+__section("egress")
+int emain(struct __sk_buff *skb)
+{
+	int key = 0, *val;
+
+	val = map_lookup_elem(&map_sh, &key);
+	if (val)
+		lock_xadd(val, 1);
+
+	return BPF_H_DEFAULT;
+}
+
+__section("ingress")
+int imain(struct __sk_buff *skb)
+{
+	int key = 0, *val;
+
+	val = map_lookup_elem(&map_sh, &key);
+	if (val)
+		printt("map val: %d\n", *val);
+
+	return BPF_H_DEFAULT;
+}
+
+BPF_LICENSE("GPL");
diff --git a/include/bpf_api.h b/include/bpf_api.h
index 89d3488d..82c47089 100644
--- a/include/bpf_api.h
+++ b/include/bpf_api.h
@@ -19,6 +19,19 @@
 
 #include "bpf_elf.h"
 
+/** libbpf pin type. */
+enum libbpf_pin_type {
+	LIBBPF_PIN_NONE,
+	/* PIN_BY_NAME: pin maps by name (in /sys/fs/bpf by default) */
+	LIBBPF_PIN_BY_NAME,
+};
+
+/** Type helper macros. */
+
+#define __uint(name, val) int (*name)[val]
+#define __type(name, val) typeof(val) *name
+#define __array(name, val) typeof(val) *name[]
+
 /** Misc macros. */
 
 #ifndef __stringify
-- 
2.25.4


  parent reply	other threads:[~2020-10-28 22:29 UTC|newest]

Thread overview: 167+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-23  3:38 [PATCH iproute2-next 0/5] iproute2: add libbpf support Hangbin Liu
2020-10-23  3:38 ` [PATCH iproute2-next 1/5] configure: add check_libbpf() for later " Hangbin Liu
2020-10-23  3:38 ` [PATCH iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Hangbin Liu
2020-10-23  3:38 ` [PATCH iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-10-23 14:34   ` David Ahern
2020-10-25 15:13     ` Toke Høiland-Jørgensen
2020-10-25 22:12       ` David Ahern
2020-10-26  8:56         ` Hangbin Liu
2020-10-26 15:15           ` David Ahern
2020-10-27  2:58             ` Hangbin Liu
2020-10-24  0:21   ` Andrii Nakryiko
2020-10-25 15:11     ` Toke Høiland-Jørgensen
2020-10-26  8:10     ` Hangbin Liu
2020-10-23  3:38 ` [PATCH iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-10-23  3:38 ` [PATCH iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Hangbin Liu
2020-10-28 13:25 ` [PATCHv2 iproute2-next 0/5] iproute2: add libbpf support Hangbin Liu
2020-10-28 13:25   ` [PATCHv2 iproute2-next 1/5] configure: add check_libbpf() for later " Hangbin Liu
2020-10-28 13:25   ` [PATCHv2 iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Hangbin Liu
2020-10-28 13:25   ` [PATCHv2 iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-10-28 13:25   ` [PATCHv2 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-10-28 13:25   ` Hangbin Liu [this message]
2020-10-28 21:17   ` [PATCHv2 iproute2-next 0/5] iproute2: add libbpf support Alexei Starovoitov
2020-10-28 23:02   ` David Ahern
2020-10-29  2:06     ` Hangbin Liu
2020-10-29  2:20       ` David Ahern
2020-10-29  2:45         ` Hangbin Liu
2020-10-29  3:00           ` David Ahern
2020-10-29  3:17             ` Hangbin Liu
2020-10-29 10:26             ` Hangbin Liu
2020-10-29 10:51               ` Toke Høiland-Jørgensen
2020-10-29  2:27       ` Andrii Nakryiko
2020-10-29  2:33         ` David Ahern
2020-10-29  2:46           ` Andrii Nakryiko
2020-10-29  2:34         ` Stephen Hemminger
2020-10-29  2:50           ` Andrii Nakryiko
2020-10-29 11:38             ` Jesper Dangaard Brouer
2020-10-29 20:30               ` Andrii Nakryiko
2020-10-29  2:33       ` Stephen Hemminger
2020-10-29 15:11   ` [PATCHv3 " Hangbin Liu
2020-10-29 15:11     ` [PATCHv3 iproute2-next 1/5] configure: add check_libbpf() for later " Hangbin Liu
2020-10-29 15:26       ` Toke Høiland-Jørgensen
2020-11-02 15:37       ` David Ahern
2020-11-03  5:54         ` Hangbin Liu
2020-11-03 17:32           ` David Ahern
2020-11-04  8:51             ` Hangbin Liu
2020-11-04 11:09               ` Toke Høiland-Jørgensen
2020-11-04 11:40                 ` Hangbin Liu
2020-10-29 15:11     ` [PATCHv3 iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Hangbin Liu
2020-10-29 15:11     ` [PATCHv3 iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-11-02 15:41       ` David Ahern
2020-11-03  5:48         ` Hangbin Liu
2020-11-03 17:19           ` David Ahern
2020-11-04  8:22         ` Hangbin Liu
2020-11-05  2:33           ` David Ahern
2020-11-05  7:51             ` Hangbin Liu
2020-11-05 15:25               ` David Ahern
2020-11-05 15:57                 ` Toke Høiland-Jørgensen
2020-11-05 16:02                   ` David Ahern
2020-11-06  0:56                     ` Hangbin Liu
2020-11-06  0:41                 ` Hangbin Liu
2020-10-29 15:11     ` [PATCHv3 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-10-29 15:11     ` [PATCHv3 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Hangbin Liu
2020-11-02 15:47     ` [PATCHv3 iproute2-next 0/5] iproute2: add libbpf support David Ahern
2020-11-03  6:58       ` Andrii Nakryiko
2020-11-03  8:42         ` Jiri Benc
2020-11-03 17:45           ` David Ahern
2020-11-03 17:48           ` Alexei Starovoitov
2020-11-03  8:46         ` Daniel Borkmann
2020-11-03 17:35           ` David Ahern
2020-11-03 17:47             ` Alexei Starovoitov
2020-11-03 18:23               ` Stephen Hemminger
2020-11-03 22:32               ` David Ahern
2020-11-03 22:55                 ` Alexei Starovoitov
2020-11-04  1:40                   ` David Ahern
2020-11-04  2:45                     ` Alexei Starovoitov
2020-11-04  9:28                       ` Jiri Benc
2020-11-05  2:39                         ` David Ahern
2020-11-04  2:17                   ` Hangbin Liu
2020-11-04  3:11                     ` Alexei Starovoitov
2020-11-04 10:01                       ` Jiri Benc
2020-11-04 10:21                       ` Daniel Borkmann
2020-11-04 11:20                         ` Toke Høiland-Jørgensen
2020-11-04 13:12                           ` Daniel Borkmann
2020-11-04 19:17                             ` Jakub Kicinski
2020-11-04 20:43                               ` Andrii Nakryiko
2020-11-04 22:24                                 ` Toke Høiland-Jørgensen
2020-11-05 20:14                                   ` Andrii Nakryiko
2020-11-05  3:48                                 ` David Ahern
2020-11-05 20:53                                   ` Andrii Nakryiko
2020-11-05  3:19                         ` David Ahern
2020-11-05 14:05                           ` Jamal Hadi Salim
2020-11-05 21:01                             ` Andrii Nakryiko
2020-11-06 15:27                               ` Jamal Hadi Salim
2020-11-06 21:25                                 ` Andrii Nakryiko
2020-11-10 12:47                             ` Edward Cree
2020-11-11  0:53                               ` Alexei Starovoitov
2020-11-11 11:31                                 ` Edward Cree
2020-11-11 18:08                                   ` Alexei Starovoitov
2020-11-05 20:45                           ` Andrii Nakryiko
2020-11-06  9:00                             ` Jiri Benc
2020-11-06 21:07                               ` Andrii Nakryiko
2020-11-04 21:15                       ` Edward Cree
2020-11-04 22:10                         ` Alexei Starovoitov
2020-11-04 22:35                           ` Toke Høiland-Jørgensen
2020-11-04 23:05                           ` Edward Cree
2020-11-05 20:19                             ` Andrii Nakryiko
2020-11-06  8:44                               ` Jiri Benc
2020-11-06 20:57                                 ` Andrii Nakryiko
2020-11-06 21:04                                   ` Alexei Starovoitov
2020-11-06 23:25                                     ` Stephen Hemminger
2020-11-06 23:30                                       ` Andrii Nakryiko
2020-11-07  0:41                                         ` Stephen Hemminger
2020-11-07  1:07                                           ` Andrii Nakryiko
2020-11-06 23:38                                       ` David Ahern
2020-11-09  1:45                                         ` Alexei Starovoitov
2020-11-10  4:09                                           ` David Ahern
2020-11-11  0:47                                             ` Alexei Starovoitov
2020-11-11 11:02                                               ` Toke Høiland-Jørgensen
2020-11-11 15:06                                                 ` Daniel Borkmann
2020-11-11 16:33                                                   ` David Ahern
2020-11-12 22:36                                                   ` Toke Høiland-Jørgensen
2020-11-12 23:20                                                     ` Daniel Borkmann
2020-11-13  0:04                                                       ` Stephen Hemminger
2020-11-13  0:40                                                         ` Alexei Starovoitov
2020-11-13  3:55                                                       ` David Ahern
2020-11-09  7:07     ` [PATCHv4 " Hangbin Liu
2020-11-09  7:07       ` [PATCHv4 iproute2-next 1/5] configure: add check_libbpf() for later " Hangbin Liu
2020-11-14  3:26         ` David Ahern
2020-11-16  4:30           ` Hangbin Liu
2020-11-16  4:33             ` David Ahern
2020-11-09  7:07       ` [PATCHv4 iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Hangbin Liu
2020-11-14  3:24         ` David Ahern
2020-11-16  3:55           ` Hangbin Liu
2020-11-09  7:08       ` [PATCHv4 iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-11-09  7:08       ` [PATCHv4 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-11-09  7:08       ` [PATCHv4 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Hangbin Liu
2020-11-16  6:53       ` [PATCHv5 iproute2-next 0/5] iproute2: add libbpf support Hangbin Liu
2020-11-16  6:53         ` [PATCHv5 iproute2-next 1/5] configure: add check_libbpf() for later " Hangbin Liu
2020-11-16  6:53         ` [PATCHv5 iproute2-next 2/5] lib: rename bpf.c to bpf_legacy.c Hangbin Liu
2020-11-16  6:53         ` [PATCHv5 iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-11-16  6:53         ` [PATCHv5 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-11-16  6:53         ` [PATCHv5 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Hangbin Liu
2020-11-16  7:19         ` [PATCHv5 iproute2-next 0/5] iproute2: add libbpf support Alexei Starovoitov
2020-11-16 14:54           ` Jesper Dangaard Brouer
2020-11-16 23:29             ` Toke Høiland-Jørgensen
2020-11-17  2:37             ` Alexei Starovoitov
2020-11-17  3:19               ` Hangbin Liu
2020-11-17 18:27                 ` Alexei Starovoitov
2020-11-17 11:56               ` Edward Cree
2020-11-17  3:38             ` David Ahern
2020-11-17 18:19               ` Alexei Starovoitov
2020-11-16 16:45           ` Stephen Hemminger
2020-11-23 13:11         ` [PATCHv6 " Hangbin Liu
2020-11-23 13:11           ` [PATCHv6 iproute2-next 1/5] iproute2: add check_libbpf() and get_libbpf_version() Hangbin Liu
2020-11-23 13:11           ` [PATCHv6 iproute2-next 2/5] lib: make ipvrf able to use libbpf and fix function name conflicts Hangbin Liu
2020-11-23 13:11           ` [PATCHv6 iproute2-next 3/5] lib: add libbpf support Hangbin Liu
2020-11-23 13:12           ` [PATCHv6 iproute2-next 4/5] examples/bpf: move struct bpf_elf_map defined maps to legacy folder Hangbin Liu
2020-11-23 13:12           ` [PATCHv6 iproute2-next 5/5] examples/bpf: add bpf examples with BTF defined maps Hangbin Liu
2020-11-25  5:28           ` [PATCHv6 iproute2-next 0/5] iproute2: add libbpf support David Ahern
2020-11-25  5:30           ` patchwork-bot+netdevbpf
2020-11-29  6:16 ` [PATCH " Stephen Hemminger
2020-11-29  6:22   ` Greg KH
2020-11-30 11:39     ` Michal Kubecek
2020-11-29 17:33   ` Alexei Starovoitov
2020-11-29 19:41   ` David Ahern
2020-11-30 11:04     ` Toke Høiland-Jørgensen
2020-12-01 14:22     ` Jesper Dangaard Brouer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201028132529.3763875-6-haliu@redhat.com \
    --to=haliu@redhat.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brouer@redhat.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=dsahern@gmail.com \
    --cc=jbenc@redhat.com \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    --cc=songliubraving@fb.com \
    --cc=stephen@networkplumber.org \
    --cc=toke@redhat.com \
    --cc=yhs@fb.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).