[PATCH bpf-next v2 26/28] libbpf: add handling for BPF_F_INSERT_HEAD in HID programs

From: Benjamin Tissoires
Date: Fri Mar 04 2022 - 12:36:06 EST


Export the newly created flag to libbpf.

Signed-off-by: Benjamin Tissoires <benjamin.tissoires@xxxxxxxxxx>

---

new in v2
---
tools/include/uapi/linux/bpf.h | 10 ++++++++++
tools/lib/bpf/libbpf.c | 17 +++++++++--------
tools/lib/bpf/libbpf.h | 2 +-
3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h
index 417cf1c31579..23ebe5e96d69 100644
--- a/tools/include/uapi/linux/bpf.h
+++ b/tools/include/uapi/linux/bpf.h
@@ -1123,6 +1123,16 @@ enum bpf_link_type {
*/
#define BPF_F_XDP_HAS_FRAGS (1U << 5)

+/* HID flag used in BPF_LINK_CREATE command
+ *
+ * NONE(default): The bpf program will be added at the tail of the list
+ * of existing bpf program for this type.
+ *
+ * BPF_F_INSERT_HEAD: The bpf program will be added at the beginning
+ * of the list of existing bpf program for this type..
+ */
+#define BPF_F_INSERT_HEAD (1U << 0)
+
/* When BPF ldimm64's insn[0].src_reg != 0 then this can have
* the following extensions:
*
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index d1e305f760bb..6f0cb6717207 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -10533,10 +10533,11 @@ static struct bpf_link *attach_lsm(const struct bpf_program *prog, long cookie)

static struct bpf_link *
bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id,
- const char *target_name)
+ const char *target_name, __u32 flags)
{
DECLARE_LIBBPF_OPTS(bpf_link_create_opts, opts,
- .target_btf_id = btf_id);
+ .target_btf_id = btf_id,
+ .flags = flags);
enum bpf_attach_type attach_type;
char errmsg[STRERR_BUFSIZE];
struct bpf_link *link;
@@ -10570,19 +10571,19 @@ bpf_program__attach_fd(const struct bpf_program *prog, int target_fd, int btf_id
struct bpf_link *
bpf_program__attach_cgroup(const struct bpf_program *prog, int cgroup_fd)
{
- return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup");
+ return bpf_program__attach_fd(prog, cgroup_fd, 0, "cgroup", 0);
}

struct bpf_link *
bpf_program__attach_netns(const struct bpf_program *prog, int netns_fd)
{
- return bpf_program__attach_fd(prog, netns_fd, 0, "netns");
+ return bpf_program__attach_fd(prog, netns_fd, 0, "netns", 0);
}

struct bpf_link *bpf_program__attach_xdp(const struct bpf_program *prog, int ifindex)
{
/* target_fd/target_ifindex use the same field in LINK_CREATE */
- return bpf_program__attach_fd(prog, ifindex, 0, "xdp");
+ return bpf_program__attach_fd(prog, ifindex, 0, "xdp", 0);
}

struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
@@ -10608,7 +10609,7 @@ struct bpf_link *bpf_program__attach_freplace(const struct bpf_program *prog,
if (btf_id < 0)
return libbpf_err_ptr(btf_id);

- return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace");
+ return bpf_program__attach_fd(prog, target_fd, btf_id, "freplace", 0);
} else {
/* no target, so use raw_tracepoint_open for compatibility
* with old kernels
@@ -10663,9 +10664,9 @@ static struct bpf_link *attach_iter(const struct bpf_program *prog, long cookie)
}

struct bpf_link *
-bpf_program__attach_hid(const struct bpf_program *prog, int hid_fd)
+bpf_program__attach_hid(const struct bpf_program *prog, int hid_fd, __u32 flags)
{
- return bpf_program__attach_fd(prog, hid_fd, 0, "hid");
+ return bpf_program__attach_fd(prog, hid_fd, 0, "hid", flags);
}

struct bpf_link *bpf_program__attach(const struct bpf_program *prog)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index f677ac0a9ede..65be3e2ec62d 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -530,7 +530,7 @@ LIBBPF_API struct bpf_link *
bpf_program__attach_iter(const struct bpf_program *prog,
const struct bpf_iter_attach_opts *opts);
LIBBPF_API struct bpf_link *
-bpf_program__attach_hid(const struct bpf_program *prog, int hid_fd);
+bpf_program__attach_hid(const struct bpf_program *prog, int hid_fd, __u32 flags);

/*
* Libbpf allows callers to adjust BPF programs before being loaded
--
2.35.1