Thread (21 messages) flat view 21 messages, 5 authors, 2023-01-28

Re: [PATCH v3 bpf-next 5/8] libbpf: add API to get XDP/XSK supported features

From: Lorenzo Bianconi <lorenzo@kernel.org>
Date: 2023-01-28 12:48:54
Also in: bpf, intel-wired-lan

On Thu, Jan 26, 2023 at 4:59 AM Lorenzo Bianconi [off-list ref] wrote:
quoted
Extend bpf_xdp_query routine in order to get XDP/XSK supported features
of netdev over route netlink interface.
Extend libbpf netlink implementation in order to support netlink_generic
protocol.

Co-developed-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
Co-developed-by: Marek Majtyka <redacted>
Signed-off-by: Marek Majtyka <redacted>
Signed-off-by: Lorenzo Bianconi <lorenzo@kernel.org>
---
 tools/lib/bpf/libbpf.h  |  3 +-
 tools/lib/bpf/netlink.c | 99 +++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/nlattr.h  | 12 +++++
 3 files changed, 113 insertions(+), 1 deletion(-)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 898db26e42e9..29cb7040fa77 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -982,9 +982,10 @@ struct bpf_xdp_query_opts {
        __u32 hw_prog_id;       /* output */
        __u32 skb_prog_id;      /* output */
        __u8 attach_mode;       /* output */
+       __u64 fflags;           /* output */
        size_t :0;
 };
-#define bpf_xdp_query_opts__last_field attach_mode
+#define bpf_xdp_query_opts__last_field fflags
is "fflags" an obvious name in this context? I'd expect
"feature_flags", especially that there are already "flags". Is saving
a few characters worth the confusion?
ack, I will fix it.
quoted
 LIBBPF_API int bpf_xdp_attach(int ifindex, int prog_fd, __u32 flags,
                              const struct bpf_xdp_attach_opts *opts);
diff --git a/tools/lib/bpf/netlink.c b/tools/lib/bpf/netlink.c
index d2468a04a6c3..674e4d61e67e 100644
--- a/tools/lib/bpf/netlink.c
+++ b/tools/lib/bpf/netlink.c
@@ -9,6 +9,7 @@
 #include <linux/if_ether.h>
 #include <linux/pkt_cls.h>
 #include <linux/rtnetlink.h>
+#include <linux/netdev.h>
 #include <sys/socket.h>
 #include <errno.h>
 #include <time.h>
@@ -39,6 +40,12 @@ struct xdp_id_md {
        int ifindex;
        __u32 flags;
        struct xdp_link_info info;
+       __u64 fflags;
+};
+
+struct xdp_features_md {
+       int ifindex;
+       __u64 flags;
 };

 static int libbpf_netlink_open(__u32 *nl_pid, int proto)
[...]
quoted
 int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
 {
        struct libbpf_nla_req req = {
@@ -393,6 +460,38 @@ int bpf_xdp_query(int ifindex, int xdp_flags, struct bpf_xdp_query_opts *opts)
        OPTS_SET(opts, skb_prog_id, xdp_id.info.skb_prog_id);
        OPTS_SET(opts, attach_mode, xdp_id.info.attach_mode);

+       if (OPTS_HAS(opts, fflags)) {
maybe invert condition, return early, reduce nesting of the following code?
ack, fine, I will fix it.
quoted
+               struct xdp_features_md md = {
+                       .ifindex = ifindex,
+               };
+               __u16 id;
+
+               err = libbpf_netlink_resolve_genl_family_id("netdev",
+                                                           sizeof("netdev"),
+                                                           &id);
nit: if it fits under 100 characters, let's leave it on a single line
ack, fine, I will fix it (I am still used to 79 char limits :))

Regards,
Lorenzo
quoted
+               if (err < 0)
+                       return libbpf_err(err);
+
+               memset(&req, 0, sizeof(req));
+               req.nh.nlmsg_len = NLMSG_LENGTH(GENL_HDRLEN);
+               req.nh.nlmsg_flags = NLM_F_REQUEST;
+               req.nh.nlmsg_type = id;
+               req.gnl.cmd = NETDEV_CMD_DEV_GET;
+               req.gnl.version = 2;
+
+               err = nlattr_add(&req, NETDEV_A_DEV_IFINDEX, &ifindex,
+                                sizeof(ifindex));
+               if (err < 0)
+                       return err;
+
+               err = libbpf_netlink_send_recv(&req, NETLINK_GENERIC,
+                                              parse_xdp_features, NULL, &md);
+               if (err)
+                       return libbpf_err(err);
+
+               opts->fflags = md.flags;
+       }
+
        return 0;
 }
[...]

Attachments

Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help