libbpf: Fix bpf_xdp_query() in old kernels
PATCH 1/1 libbpf: Fix bpf_xdp_query() in old kernels
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.
--- tools/lib/bpf/netlink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
tools/lib/bpf/netlink.c +4 -1
@@ -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 );
Signature · 1 line
2.30.2
Quoted 29 lines · Yonghong Song
Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features") added
feature_flagsto structbpf_xdp_query_opts. If a user usesbpf_xdp_query_optswithfeature_flagsmember, thebpf_xdp_query()will check whether 'netdev' family exists or not in the kernel. If it does not exist, thebpf_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 withbpf_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 <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- tools/lib/bpf/netlink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)tools/lib/bpf/netlink.c +0
-0
tools/lib/bpf/netlink.c int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts) · line 476
Quoted 10 lines
tools/lib/bpf/netlink.c +4
-1@@ -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:
tools/lib/bpf/netlink.c +0 -0
@@ -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;
}
Quoted 5 lines
tools/lib/bpf/netlink.c +0
-1memset (&req,0 ,sizeof (req)); req.nh.nlmsg_len =NLMSG_LENGTH (GENL_HDRLEN ); --2.30.2
Quoted 66 lines · Andrii Nakryiko
Quoted 29 lines · Yonghong Song
Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features") added
feature_flagsto structbpf_xdp_query_opts. If a user usesbpf_xdp_query_optswithfeature_flagsmember, thebpf_xdp_query()will check whether 'netdev' family exists or not in the kernel. If it does not exist, thebpf_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 withbpf_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 <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- tools/lib/bpf/netlink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)tools/lib/bpf/netlink.c +0
-0tools/lib/bpf/netlink.c int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts) · line 476
Quoted 10 lines
tools/lib/bpf/netlink.c +4
-1@@ -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_optsmight be extended and then we'll forget about this early return. So I did these changes and pushed to bpf-next:tools/lib/bpf/netlink.c +4
-2@@ -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);}
tools/lib/bpf/netlink.c int bpf_xdp_query(int ifindex, int xdp_flags, · line 500
Quoted 7 lines
tools/lib/bpf/netlink.c +1
-0@@ -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!
Quoted 6 lines
Quoted 6 lines
tools/lib/bpf/netlink.c +0
-1memset (&req,0 ,sizeof (req)); req.nh.nlmsg_len =NLMSG_LENGTH (GENL_HDRLEN ); --2.30.2
Lorenzo Bianconi
Acked-byQuoted 66 lines · Yonghong Song
Quoted 29 lines
Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features") added
feature_flagsto structbpf_xdp_query_opts. If a user usesbpf_xdp_query_optswithfeature_flagsmember, thebpf_xdp_query()will check whether 'netdev' family exists or not in the kernel. If it does not exist, thebpf_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 withbpf_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 <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- tools/lib/bpf/netlink.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)tools/lib/bpf/netlink.c +0
-0tools/lib/bpf/netlink.c int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts) · line 476
Quoted 10 lines
tools/lib/bpf/netlink.c +4
-1@@ -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_optsmight be extended and then we'll forget about this early return. So I did these changes and pushed to bpf-next:tools/lib/bpf/netlink.c +4
-2@@ -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);}
tools/lib/bpf/netlink.c int bpf_xdp_query(int ifindex, int xdp_flags, · line 499
Quoted 6 lines
tools/lib/bpf/netlink.c +1
-0@@ -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:
Regards,
Lorenzo
Quoted 7 lines
tools/lib/bpf/netlink.c +0
-0}Quoted 6 lines
tools/lib/bpf/netlink.c +0
-1memset (&req,0 ,sizeof (req)); req.nh.nlmsg_len =NLMSG_LENGTH (GENL_HDRLEN ); --2.30.2
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:
Quoted 6 lines · On Mon, 27 Feb 2023 14:49:43 -0800 you wrote:
Commit 04d58f1b26a4("libbpf: add API to get XDP/XSK supported features") added
feature_flagsto structbpf_xdp_query_opts. If a user usesbpf_xdp_query_optswithfeature_flagsmember, thebpf_xdp_query()will check whether 'netdev' family exists or not in the kernel. If it does not exist, thebpf_xdp_query()will return-ENOENT.
Quoted 6 lines
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 withbpf_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!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html