uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
PATCH 1/1 uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
As BPF_PROG_TYPE_NETFILTER was added in 6.4, a netfilter bpf program can attach to netfilter hooks, process package and return verdict back to netfilter. But those verdict codes are defined as macro, which could not be compiled into BTF with btf.c. libbpf, and maybe other bpf tools, would extract information from BTF and generate a common header "vmlinux.h". With macro definition, netfilter bpf program would have to redefine those macro again, besides including "vmlinux.h".
This code change netfilter hook verdict code definition to enum, this way, make it into BTF.
--- include/uapi/linux/netfilter.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
include/uapi/linux/netfilter.h +9 -7
@@ -8,13 +8,15 @@#include <linux/in6.h> /* Responses from hook functions. */ -#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */ -#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};/* we overload the higher bits for encoding auxiliary data such as the queue * number or errno values. Not nice, but better than additional function
Signature · 1 line
2.20.1
Hi David,
Quoted 32 lines · David Wang
As
BPF_PROG_TYPE_NETFILTERwas added in 6.4, a netfilter bpf program can attach to netfilter hooks, process package and return verdict back to netfilter. But those verdict codes are defined as macro, which could not be compiled into BTF withbtf.c. libbpf, and maybe other bpf tools, would extract information from BTF and generate a common header "vmlinux.h". With macro definition, netfilter bpf program would have to redefine those macro again, besides including "vmlinux.h".This code change netfilter hook verdict code definition to enum, this way, make it into BTF.
Signed-off-by David Wang <[email protected]>--- include/uapi/linux/netfilter.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)include/uapi/linux/netfilter.h +0
-6@@ -8,13 +8,15 @@#include <linux/in6.h> /* Responses from hook functions. */ -#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */
include/uapi/linux/netfilter.h line 19
Quoted 10 lines
include/uapi/linux/netfilter.h +9
-1-#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};
Switching from macro to enum works for almost all use cases, but not all. If someone if #ifdefing the symbols (which is plausible) this change would break them.
I think I've seen some other networking code define both enums and macros. But it was a little ugly. Not sure if that is acceptable here or not.
[...]
Thanks,
Daniel
Quoted 28 lines · Daniel Xu
Hi David,
Quoted 9 lines · David Wang
#include <
linux/in6.h>/* Responses from hook functions. */
include/uapi/linux/netfilter.h +0
-6-#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */ include/uapi/linux/netfilter.h line 19
Quoted 10 lines
include/uapi/linux/netfilter.h +9
-1-#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};Switching from macro to enum works for almost all use cases, but not all. If someone if #ifdefing the symbols (which is plausible) this change would break them.
Quoted 8 lines
I think I've seen some other networking code define both enums and macros. But it was a little ugly. Not sure if that is acceptable here or not.
[...]
Thanks,
Daniel
Thanks for the review~
I do not have a strong reasoning to deny the possibility of breaking unexpected usage of this macros,
but I also agree that it is ugly to use both enum and macro at the same time.
Kind of don't know how to proceed from here now...
Quoted 41 lines · David Wang
Quoted 28 lines · Daniel Xu
Hi David,
Quoted 9 lines · David Wang
#include <
linux/in6.h>/* Responses from hook functions. */
include/uapi/linux/netfilter.h +0
-6-#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */ include/uapi/linux/netfilter.h line 19
Quoted 10 lines
include/uapi/linux/netfilter.h +9
-1-#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};Switching from macro to enum works for almost all use cases, but not all. If someone if #ifdefing the symbols (which is plausible) this change would break them.
Quoted 8 lines
I think I've seen some other networking code define both enums and macros. But it was a little ugly. Not sure if that is acceptable here or not.
[...]
Thanks,
Daniel
Quoted 6 lines
Thanks for the review~
I do not have a strong reasoning to deny the possibility of breaking unexpected usage of this macros,but I also agree that it is ugly to use both enum and macro at the same time.
Kind of don't know how to proceed from here now...
I did see code like that somewhere and wondered what was going on. The #define lines were interspersed with the enum members which indeed looked ugly to me.
I'd suggest a block of #defines after the enum close e.g.
Quoted 2 lines
#define
NF_DROPNF_DROP
...
perhaps with a comment preceding to advise that the defines were there for the benefit of anyone using #ifdef.
Cheers ... Duncan.
Florian Westphal
maintainerQuoted 39 lines · David Wang
Quoted 28 lines · Daniel Xu
Hi David,
Quoted 9 lines · David Wang
#include <
linux/in6.h>/* Responses from hook functions. */
include/uapi/linux/netfilter.h +0
-6-#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */ include/uapi/linux/netfilter.h line 19
Quoted 10 lines
include/uapi/linux/netfilter.h +9
-1-#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};Switching from macro to enum works for almost all use cases, but not all. If someone if #ifdefing the symbols (which is plausible) this change would break them.
Quoted 8 lines
I think I've seen some other networking code define both enums and macros. But it was a little ugly. Not sure if that is acceptable here or not.
[...]
Thanks,
Daniel
Quoted 6 lines
Thanks for the review~
I do not have a strong reasoning to deny the possibility of breaking unexpected usage of this macros,but I also agree that it is ugly to use both enum and macro at the same time.
Kind of don't know how to proceed from here now...
I don't see anyone doing #ifdef tests on these, so I suggest we give your patch a try and see if anything breaks.
Technically only ACCEPT and DROP can be used by bpf programs but splitting it in enum-for-accept-drop-and-define-for-the-rest looks even more silly.
Florian Westphal
maintainerDavid Wang <[email protected]> wrote:
Hello,
Quoted 39 lines
Quoted 28 lines · Daniel Xu
Hi David,
Quoted 9 lines · David Wang
#include <
linux/in6.h>/* Responses from hook functions. */
include/uapi/linux/netfilter.h +0
-6-#define NF_DROP 0 -#define NF_ACCEPT 1 -#define NF_STOLEN 2 -#define NF_QUEUE 3 -#define NF_REPEAT 4 -#define NF_STOP 5/* Deprecated, for userspace nf_queue compatibility. */ include/uapi/linux/netfilter.h line 19
Quoted 10 lines
include/uapi/linux/netfilter.h +9
-1-#define NF_MAX_VERDICT NF_STOP +enum { +NF_DROP =0 , +NF_ACCEPT =1 , +NF_STOLEN =2 , +NF_QUEUE =3 , +NF_REPEAT =4 , +NF_STOP =5 ,/* Deprecated, for userspace nf_queue compatibility. */ +NF_MAX_VERDICT =NF_STOP , +};Switching from macro to enum works for almost all use cases, but not all. If someone if #ifdefing the symbols (which is plausible) this change would break them.
Quoted 8 lines
I think I've seen some other networking code define both enums and macros. But it was a little ugly. Not sure if that is acceptable here or not.
[...]
Thanks,
Daniel
Quoted 6 lines
Thanks for the review~
I do not have a strong reasoning to deny the possibility of breaking unexpected usage of this macros,but I also agree that it is ugly to use both enum and macro at the same time.
Kind of don't know how to proceed from here now...
I was about to apply this as-is, but Pablo Neira would prefer to keep the defines as well.
So, as a compromise, I would suggest to just *add*
/* verdicts available to BPF are exported via vmlinux.h */
enum {
NF_DROP = 0,
NF_ACCEPT = 1,
};#define NF_DROP 0
...
This way BTF won't have the other verdicts, but ATM those cannot be used in BPF programs anyway.
Would you mind making a new version of the patch?
Otherwise I can mangle it locally here as needed.
Quoted 12 lines · Florian Westphal
I was about to apply this as-is, but Pablo Neira would prefer to keep the defines as well.
So, as a compromise, I would suggest to just *add*
/* verdicts available to BPF are exported via
vmlinux.h*/enum { NF_DROP = 0, NF_ACCEPT = 1, };
Quoted 8 lines
#define
NF_DROP0
...This way BTF won't have the other verdicts, but ATM those cannot be used in BPF programs anyway.
Would you mind making a new version of the patch?
Otherwise I can mangle it locally here as needed.
Sorry for this late response, I got caught up by an unexpected personal "crisis" for quite a long while.. Hope you have already made the change, and it is OK.
David