diff mbox series

[bpf] libbpf: Fix bpf_xdp_query() in old kernels

Message ID 20230227224943.1153459-1-yhs@fb.com (mailing list archive)
State Accepted
Commit c8ee37bde4021a275d2e4f33bd48d54912bb00c4
Delegated to: BPF
Headers show
Series [bpf] libbpf: Fix bpf_xdp_query() in old kernels | expand

Checks

Context Check Description
netdev/series_format success Single patches do not need cover letters
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers fail 2 blamed authors not CCed: alardam@gmail.com memxor@gmail.com; 13 maintainers not CCed: alardam@gmail.com davem@davemloft.net jolsa@kernel.org john.fastabend@gmail.com martin.lau@linux.dev kpsingh@kernel.org song@kernel.org memxor@gmail.com haoluo@google.com kuba@kernel.org netdev@vger.kernel.org hawk@kernel.org sdf@google.com
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0

Commit Message

Yonghong Song Feb. 27, 2023, 10:49 p.m. UTC
Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features")
added feature_flags to struct bpf_xdp_query_opts. If a user uses
bpf_xdp_query_opts with feature_flags member, the bpf_xdp_query()
will check whether 'netdev' family exists or not in the kernel.
If it does not exist, the bpf_xdp_query() will return -ENOENT.

But 'netdev' family does not exist in old kernels as it is
introduced in the same patch set as Commit 04d58f1b26a4.
So old kernel with newer libbpf won't work properly with
bpf_xdp_query() api call.

To fix this issue, if the return value of
libbpf_netlink_resolve_genl_family_id() is -ENOENT, bpf_xdp_query()
will just return 0, skipping the rest of xdp feature query.
This preserves backward compatibility.

Fixes: 04d58f1b26a4 ("libbpf: add API to get XDP/XSK supported features")
Cc: Lorenzo Bianconi <lorenzo@kernel.org>
Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/lib/bpf/netlink.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

Comments

Andrii Nakryiko Feb. 27, 2023, 11:35 p.m. UTC | #1
On Mon, Feb 27, 2023 at 2:50 PM Yonghong Song <yhs@fb.com> wrote:
>
> Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features")
> added feature_flags to struct bpf_xdp_query_opts. If a user uses
> bpf_xdp_query_opts with feature_flags member, the bpf_xdp_query()
> will check whether 'netdev' family exists or not in the kernel.
> If it does not exist, the bpf_xdp_query() will return -ENOENT.
>
> But 'netdev' family does not exist in old kernels as it is
> introduced in the same patch set as Commit 04d58f1b26a4.
> So old kernel with newer libbpf won't work properly with
> bpf_xdp_query() api call.
>
> To fix this issue, if the return value of
> libbpf_netlink_resolve_genl_family_id() is -ENOENT, bpf_xdp_query()
> will just return 0, skipping the rest of xdp feature query.
> This preserves backward compatibility.
>
> Fixes: 04d58f1b26a4 ("libbpf: add API to get XDP/XSK supported features")
> Cc: Lorenzo Bianconi <lorenzo@kernel.org>
> Signed-off-by: Yonghong Song <yhs@fb.com>
> ---
>  tools/lib/bpf/netlink.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> index 1653e7a8b0a1..4c1b3502f88d 100644
> --- a/tools/lib/bpf/netlink.c
> +++ b/tools/lib/bpf/netlink.c
> @@ -468,8 +468,11 @@ int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
>                 return 0;
>
>         err = libbpf_netlink_resolve_genl_family_id("netdev", sizeof("netdev"), &id);
> -       if (err < 0)
> +       if (err < 0) {
> +               if (err == -ENOENT)
> +                       return 0;
>                 return libbpf_err(err);
> +       }
>

As I mentioned in another thread, I'm a bit worried of this early
return, because query_opts might be extended and then we'll forget
about this early return. So I did these changes and pushed to
bpf-next:

diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 4c1b3502f88d..84dd5fa14905 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -469,8 +469,10 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
struct bpf_xdp_query_opts *opts)

        err = libbpf_netlink_resolve_genl_family_id("netdev",
sizeof("netdev"), &id);
        if (err < 0) {
-               if (err == -ENOENT)
-                       return 0;
+               if (err == -ENOENT) {
+                       opts->feature_flags = 0;
+                       goto skip_feature_flags;
+               }
                return libbpf_err(err);
        }

@@ -492,6 +494,7 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
struct bpf_xdp_query_opts *opts)

        opts->feature_flags = md.flags;

+skip_feature_flags:
        return 0;
 }

>         memset(&req, 0, sizeof(req));
>         req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
> --
> 2.30.2
>
patchwork-bot+netdevbpf@kernel.org Feb. 27, 2023, 11:40 p.m. UTC | #2
Hello:

This patch was applied to bpf/bpf-next.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Mon, 27 Feb 2023 14:49:43 -0800 you wrote:
> Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features")
> added feature_flags to struct bpf_xdp_query_opts. If a user uses
> bpf_xdp_query_opts with feature_flags member, the bpf_xdp_query()
> will check whether 'netdev' family exists or not in the kernel.
> If it does not exist, the bpf_xdp_query() will return -ENOENT.
> 
> But 'netdev' family does not exist in old kernels as it is
> introduced in the same patch set as Commit 04d58f1b26a4.
> So old kernel with newer libbpf won't work properly with
> bpf_xdp_query() api call.
> 
> [...]

Here is the summary with links:
  - [bpf] libbpf: Fix bpf_xdp_query() in old kernels
    https://git.kernel.org/bpf/bpf-next/c/c8ee37bde402

You are awesome, thank you!
Yonghong Song Feb. 27, 2023, 11:44 p.m. UTC | #3
On 2/27/23 3:35 PM, Andrii Nakryiko wrote:
> On Mon, Feb 27, 2023 at 2:50 PM Yonghong Song <yhs@fb.com> wrote:
>>
>> Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features")
>> added feature_flags to struct bpf_xdp_query_opts. If a user uses
>> bpf_xdp_query_opts with feature_flags member, the bpf_xdp_query()
>> will check whether 'netdev' family exists or not in the kernel.
>> If it does not exist, the bpf_xdp_query() will return -ENOENT.
>>
>> But 'netdev' family does not exist in old kernels as it is
>> introduced in the same patch set as Commit 04d58f1b26a4.
>> So old kernel with newer libbpf won't work properly with
>> bpf_xdp_query() api call.
>>
>> To fix this issue, if the return value of
>> libbpf_netlink_resolve_genl_family_id() is -ENOENT, bpf_xdp_query()
>> will just return 0, skipping the rest of xdp feature query.
>> This preserves backward compatibility.
>>
>> Fixes: 04d58f1b26a4 ("libbpf: add API to get XDP/XSK supported features")
>> Cc: Lorenzo Bianconi <lorenzo@kernel.org>
>> Signed-off-by: Yonghong Song <yhs@fb.com>
>> ---
>>   tools/lib/bpf/netlink.c | 5 ++++-
>>   1 file changed, 4 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
>> index 1653e7a8b0a1..4c1b3502f88d 100644
>> --- a/tools/lib/bpf/netlink.c
>> +++ b/tools/lib/bpf/netlink.c
>> @@ -468,8 +468,11 @@ int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
>>                  return 0;
>>
>>          err = libbpf_netlink_resolve_genl_family_id("netdev", sizeof("netdev"), &id);
>> -       if (err < 0)
>> +       if (err < 0) {
>> +               if (err == -ENOENT)
>> +                       return 0;
>>                  return libbpf_err(err);
>> +       }
>>
> 
> As I mentioned in another thread, I'm a bit worried of this early
> return, because query_opts might be extended and then we'll forget
> about this early return. So I did these changes and pushed to
> bpf-next:
> 
> diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> index 4c1b3502f88d..84dd5fa14905 100644
> --- a/tools/lib/bpf/netlink.c
> +++ b/tools/lib/bpf/netlink.c
> @@ -469,8 +469,10 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
> struct bpf_xdp_query_opts *opts)
> 
>          err = libbpf_netlink_resolve_genl_family_id("netdev",
> sizeof("netdev"), &id);
>          if (err < 0) {
> -               if (err == -ENOENT)
> -                       return 0;
> +               if (err == -ENOENT) {
> +                       opts->feature_flags = 0;
> +                       goto skip_feature_flags;
> +               }
>                  return libbpf_err(err);
>          }
> 
> @@ -492,6 +494,7 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
> struct bpf_xdp_query_opts *opts)
> 
>          opts->feature_flags = md.flags;
> 
> +skip_feature_flags:
>          return 0;
>   }

Sounds good to me. Thanks!

> 
>>          memset(&req, 0, sizeof(req));
>>          req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
>> --
>> 2.30.2
>>
Lorenzo Bianconi Feb. 28, 2023, 9:05 a.m. UTC | #4
> On Mon, Feb 27, 2023 at 2:50 PM Yonghong Song <yhs@fb.com> wrote:
> >
> > Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features")
> > added feature_flags to struct bpf_xdp_query_opts. If a user uses
> > bpf_xdp_query_opts with feature_flags member, the bpf_xdp_query()
> > will check whether 'netdev' family exists or not in the kernel.
> > If it does not exist, the bpf_xdp_query() will return -ENOENT.
> >
> > But 'netdev' family does not exist in old kernels as it is
> > introduced in the same patch set as Commit 04d58f1b26a4.
> > So old kernel with newer libbpf won't work properly with
> > bpf_xdp_query() api call.
> >
> > To fix this issue, if the return value of
> > libbpf_netlink_resolve_genl_family_id() is -ENOENT, bpf_xdp_query()
> > will just return 0, skipping the rest of xdp feature query.
> > This preserves backward compatibility.
> >
> > Fixes: 04d58f1b26a4 ("libbpf: add API to get XDP/XSK supported features")
> > Cc: Lorenzo Bianconi <lorenzo@kernel.org>
> > Signed-off-by: Yonghong Song <yhs@fb.com>
> > ---
> >  tools/lib/bpf/netlink.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> >
> > diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> > index 1653e7a8b0a1..4c1b3502f88d 100644
> > --- a/tools/lib/bpf/netlink.c
> > +++ b/tools/lib/bpf/netlink.c
> > @@ -468,8 +468,11 @@ int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
> >                 return 0;
> >
> >         err = libbpf_netlink_resolve_genl_family_id("netdev", sizeof("netdev"), &id);
> > -       if (err < 0)
> > +       if (err < 0) {
> > +               if (err == -ENOENT)
> > +                       return 0;
> >                 return libbpf_err(err);
> > +       }
> >
> 
> As I mentioned in another thread, I'm a bit worried of this early
> return, because query_opts might be extended and then we'll forget
> about this early return. So I did these changes and pushed to
> bpf-next:
> 
> diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
> index 4c1b3502f88d..84dd5fa14905 100644
> --- a/tools/lib/bpf/netlink.c
> +++ b/tools/lib/bpf/netlink.c
> @@ -469,8 +469,10 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
> struct bpf_xdp_query_opts *opts)
> 
>         err = libbpf_netlink_resolve_genl_family_id("netdev",
> sizeof("netdev"), &id);
>         if (err < 0) {
> -               if (err == -ENOENT)
> -                       return 0;
> +               if (err == -ENOENT) {
> +                       opts->feature_flags = 0;
> +                       goto skip_feature_flags;
> +               }
>                 return libbpf_err(err);
>         }
> 
> @@ -492,6 +494,7 @@ int bpf_xdp_query(int ifindex, int xdp_flags,
> struct bpf_xdp_query_opts *opts)
> 
>         opts->feature_flags = md.flags;
> 
> +skip_feature_flags:
>         return 0;

thx for fixing this:
Acked-by: Lorenzo Bianconi <lorenzo@kernel.org>

Regards,
Lorenzo

>  }
> 
> >         memset(&req, 0, sizeof(req));
> >         req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
> > --
> > 2.30.2
> >
diff mbox series

Patch

diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index 1653e7a8b0a1..4c1b3502f88d 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -468,8 +468,11 @@  int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
 		return 0;
 
 	err = libbpf_netlink_resolve_genl_family_id("netdev", sizeof("netdev"), &id);
-	if (err < 0)
+	if (err < 0) {
+		if (err == -ENOENT)
+			return 0;
 		return libbpf_err(err);
+	}
 
 	memset(&req, 0, sizeof(req));
 	req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);