bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
@ 2023-09-04 13:02 David Wang
  2023-09-05 16:38 ` Daniel Xu
  0 siblings, 1 reply; 6+ messages in thread
From: David Wang @ 2023-09-04 13:02 UTC (permalink / raw)
  Cc: David Wang, Pablo Neira Ayuso, Jozsef Kadlecsik,
	Florian Westphal, netfilter-devel, coreteam, linux-kernel, bpf

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.

Signed-off-by: David Wang <00107082@163.com>
---
 include/uapi/linux/netfilter.h | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/include/uapi/linux/netfilter.h b/include/uapi/linux/netfilter.h
index 5a79ccb76701..d2f5dfab20dc 100644
--- a/include/uapi/linux/netfilter.h
+++ b/include/uapi/linux/netfilter.h
@@ -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
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
  2023-09-04 13:02 [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum David Wang
@ 2023-09-05 16:38 ` Daniel Xu
  2023-09-05 16:57   ` David Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Daniel Xu @ 2023-09-05 16:38 UTC (permalink / raw)
  To: David Wang
  Cc: Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	netfilter-devel, coreteam, linux-kernel, bpf

Hi David,

On Mon, Sep 04, 2023 at 09:02:02PM +0800, David Wang wrote:
> 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.
> 
> Signed-off-by: David Wang <00107082@163.com>
> ---
>  include/uapi/linux/netfilter.h | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
> 
> diff --git a/include/uapi/linux/netfilter.h b/include/uapi/linux/netfilter.h
> index 5a79ccb76701..d2f5dfab20dc 100644
> --- a/include/uapi/linux/netfilter.h
> +++ b/include/uapi/linux/netfilter.h
> @@ -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,
> +};

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

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
  2023-09-05 16:38 ` Daniel Xu
@ 2023-09-05 16:57   ` David Wang
  2023-09-07 18:44     ` Florian Westphal
  2023-09-28 11:53     ` Florian Westphal
  0 siblings, 2 replies; 6+ messages in thread
From: David Wang @ 2023-09-05 16:57 UTC (permalink / raw)
  To: Daniel Xu
  Cc: Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	netfilter-devel, coreteam, linux-kernel, bpf



At 2023-09-06 00:38:02, "Daniel Xu" <dxu@dxuuu.xyz> wrote:
>Hi David,
>
>On Mon, Sep 04, 2023 at 09:02:02PM +0800, David Wang wrote:

>>  #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,
>> +};
>
>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


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...

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
  2023-09-05 16:57   ` David Wang
@ 2023-09-07 18:44     ` Florian Westphal
  2023-09-28 11:53     ` Florian Westphal
  1 sibling, 0 replies; 6+ messages in thread
From: Florian Westphal @ 2023-09-07 18:44 UTC (permalink / raw)
  To: David Wang
  Cc: Daniel Xu, Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	netfilter-devel, coreteam, linux-kernel, bpf

David Wang <00107082@163.com> wrote:
> At 2023-09-06 00:38:02, "Daniel Xu" <dxu@dxuuu.xyz> wrote:
> >Hi David,
> >
> >On Mon, Sep 04, 2023 at 09:02:02PM +0800, David Wang wrote:
> 
> >>  #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,
> >> +};
> >
> >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
> 
> 
> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
  2023-09-05 16:57   ` David Wang
  2023-09-07 18:44     ` Florian Westphal
@ 2023-09-28 11:53     ` Florian Westphal
  2023-10-16  9:22       ` David Wang
  1 sibling, 1 reply; 6+ messages in thread
From: Florian Westphal @ 2023-09-28 11:53 UTC (permalink / raw)
  To: David Wang
  Cc: Daniel Xu, Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	netfilter-devel, coreteam, linux-kernel, bpf

David Wang <00107082@163.com> wrote:

Hello,

> At 2023-09-06 00:38:02, "Daniel Xu" <dxu@dxuuu.xyz> wrote:
> >Hi David,
> >
> >On Mon, Sep 04, 2023 at 09:02:02PM +0800, David Wang wrote:
> 
> >>  #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,
> >> +};
> >
> >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
> 
> 
> 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.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re:Re: [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum
  2023-09-28 11:53     ` Florian Westphal
@ 2023-10-16  9:22       ` David Wang
  0 siblings, 0 replies; 6+ messages in thread
From: David Wang @ 2023-10-16  9:22 UTC (permalink / raw)
  To: Florian Westphal
  Cc: Daniel Xu, Pablo Neira Ayuso, Jozsef Kadlecsik, netfilter-devel,
	coreteam, linux-kernel, bpf




At 2023-09-28 19:53:59, "Florian Westphal" <fw@strlen.de> wrote:

>
>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.


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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-10-16  9:38 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-04 13:02 [PATCH] uapi/netfilter: Change netfilter hook verdict code definition from macro to enum David Wang
2023-09-05 16:38 ` Daniel Xu
2023-09-05 16:57   ` David Wang
2023-09-07 18:44     ` Florian Westphal
2023-09-28 11:53     ` Florian Westphal
2023-10-16  9:22       ` David Wang

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).