Skip to contentSkip to conversation

samples/bpf: Add sample usage for BPF_PROG_TYPE_NETFILTER

PATCH In review David Wang 5 participants 13 messages Sep 4, 2023 – Sep 5, 2023 BPF

lore
Share & tools
lore

David Wang

author
#0 lore raw

PATCH 1/1 samples/bpf: Add sample usage for BPF_PROG_TYPE_NETFILTER

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program,
attach it to netfilter's FORWARD/OUTPUT hook,
and then write ip patterns into the bpf map.

---
 samples/bpf/Makefile                      |  3 +
 samples/bpf/netfilter_ip4_blacklist.bpf.c | 62 +++++++++++++++
 samples/bpf/netfilter_ip4_blacklist.c     | 96 +++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.bpf.c
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.c
samples/bpf/Makefile +3 -0
index 4ccf4236031c..ff027ea5ce24 100644
@@ -46,6 +46,7 @@ tprogs-y += xdp_fwd
 tprogs-y += task_fd_query
 tprogs-y += ibumad
 tprogs-y += hbm
+tprogs-y += netfilter_ip4_blacklist
 
 # Libbpf dependencies
 LIBBPF_SRC = $(TOOLS_PATH)/lib/bpf
@@ -96,6 +97,7 @@ xdp_fwd-objs := xdp_fwd_user.o
 task_fd_query-objs := task_fd_query_user.o $(TRACE_HELPERS)
 ibumad-objs := ibumad_user.o
 hbm-objs := hbm.o $(CGROUP_HELPERS)
+netfilter_ip4_blacklist-objs := netfilter_ip4_blacklist.o
 
 xdp_router_ipv4-objs := xdp_router_ipv4_user.o $(XDP_SAMPLE)
 
@@ -149,6 +151,7 @@ always-y += task_fd_query_kern.o
 always-y += ibumad_kern.o
 always-y += hbm_out_kern.o
 always-y += hbm_edt_kern.o
+always-y += netfilter_ip4_blacklist.bpf.o
 
 ifeq ($(ARCH), arm)
 # Strip all except -D__LINUX_ARM_ARCH__ option needed to handle linux
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +62 -0

62 lines not on this page · raw message

samples/bpf/netfilter_ip4_blacklist.c new file +96 -0

96 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw
Quoted 10 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program,
attach it to netfilter's FORWARD/OUTPUT hook,
and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Quoted 6 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +0 -0
new file mode 100644
index 000000000000..d315d64fda7f

samples/bpf/netfilter_ip4_blacklist.bpf.c line 8

Quoted 8 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +8 -0
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+
+
+#define NF_DROP 0
+#define NF_ACCEPT 1

If you are interested, you could send a patch for nf-next that makes the uapi headers expose this as enum, AFAIU that would make the verdict nanes available via vmlinux.h.

samples/bpf/netfilter_ip4_blacklist.bpf.c line 55

Quoted 7 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +7 -0
+	/* search p->daddr in trie */
+	key.prefixlen = 32;
+	key.data = p->daddr;
+	pvalue = bpf_map_lookup_elem(&ipv4_lpm_map, &key);
+	if (pvalue) {
+		/* cat /sys/kernel/debug/tracing/trace_pipe */
+		bpf_printk("rule matched with %d...\n", *pvalue);

If you are interested you could send a patch that adds a kfunc to nf_bpf_link that exposes nf_log_packet() to bpf.

nf_log_packet has a terrible api, I suggest to have the kfunc take 'struct nf_hook_state *' instead of 6+ members of that struct as argument.

Thanks for the example.

author
#2 lore raw
Quoted 5 lines · Florian Westphal
Quoted 4 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

Quoted 8 lines
Quoted 6 lines

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program,
attach it to netfilter's FORWARD/OUTPUT hook,
and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Thanks for the quick response.

Quoted 11 lines
Quoted 11 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +4 -0
new file mode 100644
index 000000000000..d315d64fda7f
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
Quoted 9 lines

samples/bpf/netfilter_ip4_blacklist.bpf.c line 8

Quoted 4 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +4 -0
+
+
+#define NF_DROP 0
+#define NF_ACCEPT 1

If you are interested, you could send a patch for nf-next that makes the uapi headers expose this as enum, AFAIU that would make the verdict nanes available via vmlinux.h.

I think I can work on this.

Quoted 11 lines

samples/bpf/netfilter_ip4_blacklist.bpf.c line 55

Quoted 7 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +7 -0
+	/* search p->daddr in trie */
+	key.prefixlen = 32;
+	key.data = p->daddr;
+	pvalue = bpf_map_lookup_elem(&ipv4_lpm_map, &key);
+	if (pvalue) {
+		/* cat /sys/kernel/debug/tracing/trace_pipe */
+		bpf_printk("rule matched with %d...\n", *pvalue);

If you are interested you could send a patch that adds a kfunc to nf_bpf_link that exposes nf_log_packet() to bpf.

Quoted 4 lines

nf_log_packet has a terrible api, I suggest to have the kfunc take 'struct nf_hook_state *' instead of 6+ members of that struct as argument.

Logging strategy is out of my league, but I will keep eye on this.

Glad to contribute.

David

author
#3 lore raw

In reply to Florian Westphal
Quoted 5 lines · Florian Westphal
Quoted 4 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

Quoted 8 lines
Quoted 6 lines

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Thanks for the quick response! Glad to contribute!

Quoted 8 lines

samples/bpf/netfilter_ip4_blacklist.bpf.c line 8

Quoted 3 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +3 -0
+
+#define NF_DROP 0
+#define NF_ACCEPT 1

If you are interested, you could send a patch for nf-next that makes the uapi headers expose this as enum, AFAIU that would make the verdict nanes available via vmlinux.h.

I think I can work on this.

Quoted 11 lines

samples/bpf/netfilter_ip4_blacklist.bpf.c line 55

Quoted 3 lines
samples/bpf/netfilter_ip4_blacklist.bpf.c new file +3 -0
+	if (pvalue) {
+		/* cat /sys/kernel/debug/tracing/trace_pipe */
+		bpf_printk("rule matched with %d...\n", *pvalue);

If you are interested you could send a patch that adds a kfunc to nf_bpf_link that exposes nf_log_packet() to bpf.

nf_log_packet has a terrible api, I suggest to have the kfunc take 'struct nf_hook_state *' instead of 6+ members of that struct as argument.

Package logging strategy is out of my league, for now, but I will keep eye on this.

David

#4 lore raw
In reply to Florian Westphal
Quoted 6 lines · Florian Westphal
Quoted 4 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

Quoted 8 lines
Quoted 6 lines

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

author
#5 lore raw
Quoted 10 lines · Alexei Starovoitov
Quoted 9 lines · Florian Westphal
Quoted 7 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

My purpose is to demonstrate the basic usage of BPF_PROG_TYPE_NETFILTER , showing what bpf program and userspace program should do to make it work. The code is neither thorough enough to make a valid test suite, nor detailed enough to make out a tool (Could be a start for a tool)

samples/bpf is a good place to start for beginners to get along with bpf quickly, those sample/bpf codes do help me a lot,
but selftests/bpf is not that friendly, at least not friendly for beginners, I think.
There are already test codes for BPF_PROG_TYPE_NETFILTER in selftests/bpf, actually I did refer to those code when I made this sample.

Get a feeling samples/bpf would be deprecated sooner or later, hope that would not happen.

Anyway, this sample code is not meant to test.

#6 lore raw
Quoted 23 lines · David Wang
Quoted 10 lines · Alexei Starovoitov
Quoted 9 lines · Florian Westphal
Quoted 7 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

My purpose is to demonstrate the basic usage of BPF_PROG_TYPE_NETFILTER , showing what bpf program and userspace program should do to make it work. The code is neither thorough enough to make a valid test suite, nor detailed enough to make out a tool (Could be a start for a tool)

Quoted 7 lines

samples/bpf is a good place to start for beginners to get along with bpf quickly, those sample/bpf codes do help me a lot,
but selftests/bpf is not that friendly, at least not friendly for beginners, I think.
There are already test codes for BPF_PROG_TYPE_NETFILTER in selftests/bpf, actually I did refer to those code when I made this sample.

Get a feeling samples/bpf would be deprecated sooner or later, hope that would not happen.

Anyway, this sample code is not meant to test.

FYI, we maintain a Github repository with BPF example programs of various types at https://github.com/xdp-project/bpf-examples

Happy to include this example there as an alternative to the in-tree samples/bpf :)

-Toke

author
#7 lore raw
Quoted 7 lines · Toke Høiland-Jørgensen
Quoted 3 lines · David Wang

Get a feeling samples/bpf would be deprecated sooner or later, hope that would not happen.

Anyway, this sample code is not meant to test.

Quoted 7 lines

FYI, we maintain a Github repository with BPF example programs of various types at https://github.com/xdp-project/bpf-examples

Happy to include this example there as an alternative to the in-tree samples/bpf :)

-Toke

Cool project~! I will submit a PR there.

author
#8 lore raw
In reply to Alexei Starovoitov
Quoted 10 lines · Alexei Starovoitov
Quoted 9 lines · Florian Westphal
Quoted 7 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

Hi Alexei,

I need to know whether samples/bpf is still a good place to put code.
I will put the code in another open source project for bpf samples, mentioned by Toke. But I still want to put it in samples/bpf , since the code only compile/work with new kernel.

Need your feedback on this, could this code be kept in samples/bpf? :)

Thanks
David.

#9 lore raw
Quoted 32 lines · David Wang
Quoted 10 lines · Alexei Starovoitov
Quoted 9 lines · Florian Westphal
Quoted 7 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Thanks, I think its good to have this.

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

Quoted 7 lines

Hi Alexei,

I need to know whether samples/bpf is still a good place to put code.
I will put the code in another open source project for bpf samples, mentioned by Toke. But I still want to put it in samples/bpf , since the code only compile/work with new kernel.

Need your feedback on this, could this code be kept in samples/bpf? :)

Sorry, but we don't accept new code to samples/bpf/. Everything in there will be moved/removed.
If you want to stay in the kernel selftests/bpf is the only place and it's gotta be the real test and not just a sample.

author
#10 lore raw
Quoted 12 lines · Alexei Starovoitov
Quoted 10 lines · David Wang
Quoted 3 lines

Yes, but only in selftests/bpf.
samples/bpf/ are not tested and bit rot heavily.

Hi Alexei,

I need to know whether samples/bpf is still a good place to put code.
I will put the code in another open source project for bpf samples, mentioned by Toke. But I still want to put it in samples/bpf , since the code only compile/work with new kernel.

Quoted 6 lines
Quoted 1 line

Need your feedback on this, could this code be kept in samples/bpf? :)

Sorry, but we don't accept new code to samples/bpf/. Everything in there will be moved/removed.
If you want to stay in the kernel selftests/bpf is the only place and it's gotta be the real test and not just a sample.

Sad to hear this....
Anyway, thank you and all others who took time reviewing this.

David

#11 lore raw
In reply to David Wang
Quoted 11 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Quoted 8 lines
---
 samples/bpf/Makefile                      |  3 +
 samples/bpf/netfilter_ip4_blacklist.bpf.c | 62 +++++++++++++++
 samples/bpf/netfilter_ip4_blacklist.c     | 96 +++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.bpf.c
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.c

According to https://docs.kernel.org/process/coding-style.html#naming
you should avoid new use of blacklist. You should use somethink like denylist or blocklist instead.

author
#12 lore raw
Quoted 22 lines · Donald Hunter
Quoted 11 lines · David Wang

This sample code implements a simple ipv4 blacklist via the new bpf type BPF_PROG_TYPE_NETFILTER, which was introduced in 6.4.

The bpf program drops package if destination ip address hits a match in the map of type BPF_MAP_TYPE_LPM_TRIE,

The userspace code would load the bpf program, attach it to netfilter's FORWARD/OUTPUT hook, and then write ip patterns into the bpf map.

Quoted 8 lines
---
 samples/bpf/Makefile                      |  3 +
 samples/bpf/netfilter_ip4_blacklist.bpf.c | 62 +++++++++++++++
 samples/bpf/netfilter_ip4_blacklist.c     | 96 +++++++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.bpf.c
 create mode 100644 samples/bpf/netfilter_ip4_blacklist.c
Quoted 3 lines

According to https://docs.kernel.org/process/coding-style.html#naming
you should avoid new use of blacklist. You should use somethink like denylist or blocklist instead.

Thanks for the information~!
I will make the changes, and resend a patch if samples/bpf is still a good place to put the code.