Skip to contentSkip to conversation

netfilter: xt_bpf: support ebpf

PATCH Superseded Willem de Bruijn 6 participants 16 messages Dec 5, 2016 – Dec 6, 2016

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: xt_bpf: support ebpf

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

---
 include/uapi/linux/netfilter/xt_bpf.h | 21 ++++++++
 net/netfilter/xt_bpf.c                | 96 +++++++++++++++++++++++++++++------
 2 files changed, 101 insertions(+), 16 deletions(-)
include/uapi/linux/netfilter/xt_bpf.h +21 -0
index 1fad2c2..652d2b6 100644
@@ -2,9 +2,11 @@
 #define _XT_BPF_H
 
 #include <linux/filter.h>
+#include <linux/limits.h>
 #include <linux/types.h>
 
 #define XT_BPF_MAX_NUM_INSTR	64
+#define XT_BPF_MAX_NUM_INSTR_V1	(PATH_MAX / sizeof(struct sock_filter))
 
 struct bpf_prog;
 
@@ -16,4 +18,23 @@ struct xt_bpf_info {
 	struct bpf_prog *filter __attribute__((aligned(8)));
 };
 
+enum xt_bpf_modes {
+	XT_BPF_MODE_BYTECODE,
+	XT_BPF_MODE_FD_PINNED,
+	XT_BPF_MODE_FD_ELF,
+};
+
+struct xt_bpf_info_v1 {
+	__u16 mode;
+	__u16 bpf_program_num_elem;
+	__s32 fd;
+	union {
+		struct sock_filter bpf_program[XT_BPF_MAX_NUM_INSTR_V1];
+		char path[PATH_MAX];
+	};
+
+	/* only used in the kernel */
+	struct bpf_prog *filter __attribute__((aligned(8)));
+};
+
 #endif /*_XT_BPF_H */
net/netfilter/xt_bpf.c +80 -16

138 lines not on this page · raw message

Signature · 1 line
2.8.0.rc3.226.g39d4020
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

---

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

maintainer
#2 lore raw
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

Quoted 5 lines
Quoted 2 lines

---

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

It should be possible by setting kernel .matchsize to ~0 which suppresses strict size enforcement.

Its currently only used by ebt_among, but this should work for any xtables module.

#3 lore raw
Quoted 13 lines · Florian Westphal
Quoted 12 lines · Eric Dumazet
Quoted 10 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

---

Quoted 8 lines
Quoted 2 lines

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

It should be possible by setting kernel .matchsize to ~0 which suppresses strict size enforcement.

Its currently only used by ebt_among, but this should work for any xtables module.

This is likely going to trigger a large rewrite of the core userspace iptables codebase, and likely going to pull part of the mess we have in ebtables into iptables. So I'd prefer not to follow this path.

maintainer
#4 lore raw
Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Florian Westphal
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

Quoted 5 lines
Quoted 2 lines

---

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

It should be possible by setting kernel .matchsize to ~0 which suppresses strict size enforcement.

Quoted 6 lines
Quoted 2 lines

Its currently only used by ebt_among, but this should work for any xtables module.

This is likely going to trigger a large rewrite of the core userspace iptables codebase, and likely going to pull part of the mess we have in ebtables into iptables. So I'd prefer not to follow this path.

Fair enough, I have no objections to the patch.

#14 lore raw
In reply to Willem de Bruijn

Hi Willem,

Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

just out of pure curiosity, use case is for android guys wrt accounting, or anything specific that cls_bpf on tc ingress + egress cannot do already?

Thanks,
Daniel

#5 lore raw
In reply to Florian Westphal
Quoted 1 line · Florian Westphal

Fair enough, I have no objections to the patch.

An additional question is about PATH_MAX :

Is it guaranteed to stay at 4096 forever ?

To be safe, maybe we should use a constant of our own.

#7 lore raw
In reply to Pablo Neira Ayuso
Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Florian Westphal
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

Quoted 5 lines
Quoted 2 lines

---

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

It should be possible by setting kernel .matchsize to ~0 which suppresses strict size enforcement.

Quoted 6 lines
Quoted 2 lines

Its currently only used by ebt_among, but this should work for any xtables module.

This is likely going to trigger a large rewrite of the core userspace iptables codebase, and likely going to pull part of the mess we have in ebtables into iptables. So I'd prefer not to follow this path.

So this variable path is there to annotate what userspace claims that is the file that contains the bpf blob that was loaded, actually this is irrelevant to the kernel, so this is just there to dump it back when iptables-save it is called. Just a side note, one could set anything there from userspace, point somewhere else actually...

Well anyway, going back to the path problem to keep it simple: Why don't just trim this down to something smaller, are you really expecting to reach PATH_MAX in your usecase?

#15 lore raw
In reply to Daniel Borkmann
Quoted 12 lines · Daniel Borkmann

Hi Willem,

Quoted 9 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

Quoted 6 lines
Quoted 1 line

just out of pure curiosity, use case is for android guys wrt accounting, or anything specific that cls_bpf on tc ingress + egress cannot do already?

That is the immediate motivation, yes.

#6 lore raw
In reply to Eric Dumazet
Quoted 9 lines · Eric Dumazet
Quoted 1 line · Florian Westphal

Fair enough, I have no objections to the patch.

An additional question is about PATH_MAX :

Is it guaranteed to stay at 4096 forever ?

To be safe, maybe we should use a constant of our own.

Right, this reminds me we have to fix something else.

So constant of our own plus something smaller, if possible, would be good to go. Thanks.

#8 lore raw
In reply to Pablo Neira Ayuso
Quoted 34 lines · Pablo Neira Ayuso
Quoted 20 lines · Pablo Neira Ayuso
Quoted 19 lines · Florian Westphal
Quoted 9 lines · Eric Dumazet
Quoted 8 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

Add support for attaching an eBPF object by file descriptor.

The iptables binary can be called with a path to an elf object or a pinned bpf object. Also pass the mode and path to the kernel to be able to return it later for iptables dump and save.

Quoted 5 lines
Quoted 2 lines

---

Assuming there is no simple way to get variable matchsize in iptables, this looks good to me, thanks.

It should be possible by setting kernel .matchsize to ~0 which suppresses strict size enforcement.

Quoted 6 lines
Quoted 2 lines

Its currently only used by ebt_among, but this should work for any xtables module.

This is likely going to trigger a large rewrite of the core userspace iptables codebase, and likely going to pull part of the mess we have in ebtables into iptables. So I'd prefer not to follow this path.

So this variable path is there to annotate what userspace claims that is the file that contains the bpf blob that was loaded, actually this is irrelevant to the kernel, so this is just there to dump it back when iptables-save it is called. Just a side note, one could set anything there from userspace, point somewhere else actually...

Quoted 3 lines

Well anyway, going back to the path problem to keep it simple: Why don't just trim this down to something smaller, are you really expecting to reach PATH_MAX in your usecase?

Not often. Module-specific limitations that differ from global definitions are just a pain when they bite. This module also has an arbitrary low limit on the length of the cBPF program passed, for instance.

Eric also suggests a private variable to avoid being subject to changes to PATH_MAX. Then we can indeed also choose an arbitrary lower length than current PATH_MAX.

FWIW, there is a workaround for users with deeply nested paths: the path passed does not have to be absolute. It is literally what is passed on the command line to iptables right now, including relative addresses.

#9 lore raw

On Mon, Dec 05, 2016 at 06:06:05PM -0500, Willem de Bruijn wrote:
[...]

Quoted 3 lines

Eric also suggests a private variable to avoid being subject to changes to PATH_MAX. Then we can indeed also choose an arbitrary lower length than current PATH_MAX.

Good.

Quoted 4 lines

FWIW, there is a workaround for users with deeply nested paths: the path passed does not have to be absolute. It is literally what is passed on the command line to iptables right now, including relative addresses.

If iptables userspace always expects to have the bpf file repository in some given location (suggesting to have a directory that we specify at ./configure time, similar to what we do with connlabel.conf), then I think we can rely on relative paths. Would this be flexible enough for your usecase?

#10 lore raw
Quoted 13 lines · Pablo Neira Ayuso

On Mon, Dec 05, 2016 at 06:06:05PM -0500, Willem de Bruijn wrote:
[...]

Quoted 3 lines

Eric also suggests a private variable to avoid being subject to changes to PATH_MAX. Then we can indeed also choose an arbitrary lower length than current PATH_MAX.

Good.

Quoted 4 lines

FWIW, there is a workaround for users with deeply nested paths: the path passed does not have to be absolute. It is literally what is passed on the command line to iptables right now, including relative addresses.

Quoted 5 lines

If iptables userspace always expects to have the bpf file repository in some given location (suggesting to have a directory that we specify at ./configure time, similar to what we do with connlabel.conf), then I think we can rely on relative paths. Would this be flexible enough for your usecase?

As long as it accepts relative paths, I think it will always work. Worst case, a user has to cd. No need for hardcoding the bpf mount point at compile time.

I have the matching iptables patch for pinned objects, btw. Not for elf objects, which requires linking to libelf and parsing the object, which is more work (and perhaps best punted on by expanding libbpf in bcc to include this functionality. it already exists under samples/bpf and iproute2).

#11 lore raw
Quoted 24 lines · Willem de Bruijn
Quoted 13 lines · Pablo Neira Ayuso

On Mon, Dec 05, 2016 at 06:06:05PM -0500, Willem de Bruijn wrote:
[...]

Quoted 3 lines

Eric also suggests a private variable to avoid being subject to changes to PATH_MAX. Then we can indeed also choose an arbitrary lower length than current PATH_MAX.

Good.

Quoted 4 lines

FWIW, there is a workaround for users with deeply nested paths: the path passed does not have to be absolute. It is literally what is passed on the command line to iptables right now, including relative addresses.

Quoted 5 lines

If iptables userspace always expects to have the bpf file repository in some given location (suggesting to have a directory that we specify at ./configure time, similar to what we do with connlabel.conf), then I think we can rely on relative paths. Would this be flexible enough for your usecase?

As long as it accepts relative paths, I think it will always work. Worst case, a user has to cd. No need for hardcoding the bpf mount point at compile time.

Quoted 5 lines

I have the matching iptables patch for pinned objects, btw. Not for elf objects, which requires linking to libelf and parsing the object, which is more work (and perhaps best punted on by expanding libbpf in bcc to include this functionality. it already exists under samples/bpf and iproute2).

While we're discussing the patch, another question, about revisions: I tested both modified and original iptables binaries on both standard and modified kernels. It all works as expected, except for the case where both binaries are used on a single kernel. For instance:

iptables -A OUTPUT -m bpf --bytecode "`./nfbpf_compile RAW 'udp port
8000'`" -j LOG
./iptables.new -L

Here the new binary will interpret the object as xt_bpf_match_v1, but iptables has inserted xt_bpf_match. The same problem happens the other way around. A new binary can be made robust to detect old structs, but not the other way around. Specific to bpf, the existing xt_bpf code has an unfortunate bug that it always prints at least one line of code, even if ->bpf_program_num_elems == 0.

I notice that other extensions also do not necessarily only extend struct vN in vN+1. Is the above a known issue?

maintainer
#12 lore raw
Quoted 16 lines · Willem de Bruijn

While we're discussing the patch, another question, about revisions: I tested both modified and original iptables binaries on both standard and modified kernels. It all works as expected, except for the case where both binaries are used on a single kernel. For instance:

iptables -A OUTPUT -m bpf --bytecode "`./nfbpf_compile RAW 'udp port
8000'`" -j LOG
./iptables.new -L

Here the new binary will interpret the object as xt_bpf_match_v1, but iptables has inserted xt_bpf_match. The same problem happens the other way around. A new binary can be made robust to detect old structs, but not the other way around. Specific to bpf, the existing xt_bpf code has an unfortunate bug that it always prints at least one line of code, even if ->bpf_program_num_elems == 0.

Quoted 2 lines

I notice that other extensions also do not necessarily only extend struct vN in vN+1. Is the above a known issue?

Yes, I guess noone ever bothered to fix this.

The kernel blob should contain the match/target revision number, so userspace can in fact see that 'this is bpf v42', but iirc the netfilter userspace just loads the highest userspace revision supported by the kernel (which is then different for the 2 iptables binaries).

But we *could* display message like 'kernel uses revision 2 but I can only find 0 and 1' or fall back to the lower supported revision without guess-the-struct-by-size games.

#13 lore raw
Quoted 20 lines · Florian Westphal
Quoted 16 lines · Willem de Bruijn

While we're discussing the patch, another question, about revisions: I tested both modified and original iptables binaries on both standard and modified kernels. It all works as expected, except for the case where both binaries are used on a single kernel. For instance:

iptables -A OUTPUT -m bpf --bytecode "`./nfbpf_compile RAW 'udp port
8000'`" -j LOG
./iptables.new -L

Here the new binary will interpret the object as xt_bpf_match_v1, but iptables has inserted xt_bpf_match. The same problem happens the other way around. A new binary can be made robust to detect old structs, but not the other way around. Specific to bpf, the existing xt_bpf code has an unfortunate bug that it always prints at least one line of code, even if ->bpf_program_num_elems == 0.

Quoted 2 lines

I notice that other extensions also do not necessarily only extend struct vN in vN+1. Is the above a known issue?

Quoted 7 lines

Yes, I guess noone ever bothered to fix this.

The kernel blob should contain the match/target revision number, so userspace can in fact see that 'this is bpf v42', but iirc the netfilter userspace just loads the highest userspace revision supported by the kernel (which is then different for the 2 iptables binaries).

We can fall back on not parsing contents on mismatch:

iptables/iptables.c +2 -1
index 540d111..ada7c94 100644
@@ -504,7 +504,8 @@ print_match(const struct xt_entry_match *m,
                xtables_find_match(m->u.user.name, XTF_TRY_LOAD, NULL);
 
        if (match) {
-               if (match->print)
+               if (match->print &&
+                   m->u.user.revision == match->revision)
                        match->print(ip, m, numeric);
                else
                        printf("%s ", match->name);
Quoted 3 lines

But we *could* display message like 'kernel uses revision 2 but I can only find 0 and 1' or fall back to the lower supported revision without guess-the-struct-by-size games.

That's a good idea. A special case printf() with a notice, then.