Skip to contentSkip to conversation

bpftool: add support of pin prog by name

PATCH In review [email protected] 2 participants 3 messages Dec 5, 2021 – Dec 7, 2021

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpftool: add support of pin prog by name

From: Menglong Dong <[email protected]>

For now, the command 'bpftool prog loadall' use section name as the name of the pin file. However, once there are prog with the same section name in ELF file, this command will failed with the error 'File Exist'.

So, add the support of pin prog by function name with the 'pinbyname' argument.

---
 tools/bpf/bpftool/prog.c | 7 +++++++
 tools/lib/bpf/libbpf.c   | 5 +++++
 tools/lib/bpf/libbpf.h   | 2 ++
 3 files changed, 14 insertions(+)
tools/bpf/bpftool/prog.c +7 -0
index e47e8b06cc3d..74e0aaebfefc 100644
@@ -1471,6 +1471,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	unsigned int old_map_fds = 0;
 	const char *pinmaps = NULL;
 	struct bpf_object *obj;
+	bool pinbyname = false;
 	struct bpf_map *map;
 	const char *pinfile;
 	unsigned int i, j;
@@ -1589,6 +1590,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 				goto err_free_reuse_maps;
 
 			pinmaps = GET_ARG();
+		} else if (is_prefix(*argv, "pinbyname")) {
+			pinbyname = true;
+			NEXT_ARG();
 		} else {
 			p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
 			      *argv);
@@ -1616,6 +1620,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 				goto err_close_obj;
 		}
 
+		if (pinbyname)
+			bpf_program__set_pinname(pos,
+						 (char *)bpf_program__name(pos));
 		bpf_program__set_ifindex(pos, ifindex);
 		bpf_program__set_type(pos, prog_type);
 		bpf_program__set_expected_attach_type(pos, expected_attach_type);
tools/lib/bpf/libbpf.c +5 -0
index f6faa33c80fa..e8fc1d0fe16e 100644
@@ -8119,6 +8119,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
 	prog->prog_ifindex = ifindex;
 }
 
+void bpf_program__set_pinname(struct bpf_program *prog, char *name)
+{
+	prog->pin_name = name;
+}
+
 const char *bpf_program__name(const struct bpf_program *prog)
 {
 	return prog->name;
tools/lib/bpf/libbpf.h +2 -0
index 4ec69f224342..107cf736c2bb 100644
@@ -216,6 +216,8 @@ LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
 					 __u32 ifindex);
+LIBBPF_API void bpf_program__set_pinname(struct bpf_program *prog,
+					 char *name);
 
 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
Signature · 1 line
2.30.2
#1 lore raw
Quoted 8 lines · On Sat, Dec 4, 2021 at 8:51 PM <[email protected]> wrote:

From: Menglong Dong <[email protected]>

For now, the command 'bpftool prog loadall' use section name as the name of the pin file. However, once there are prog with the same section name in ELF file, this command will failed with the error 'File Exist'.

Quoted 5 lines

So, add the support of pin prog by function name with the 'pinbyname' argument.

---

Doesn't [0] do that already?

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Quoted 42 lines
 tools/bpf/bpftool/prog.c | 7 +++++++
 tools/lib/bpf/libbpf.c   | 5 +++++
 tools/lib/bpf/libbpf.h   | 2 ++
 3 files changed, 14 insertions(+)
tools/bpf/bpftool/prog.c +7 -0
index e47e8b06cc3d..74e0aaebfefc 100644
@@ -1471,6 +1471,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
        unsigned int old_map_fds = 0;
        const char *pinmaps = NULL;
        struct bpf_object *obj;
+       bool pinbyname = false;
        struct bpf_map *map;
        const char *pinfile;
        unsigned int i, j;
@@ -1589,6 +1590,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                                goto err_free_reuse_maps;
 
                        pinmaps = GET_ARG();
+               } else if (is_prefix(*argv, "pinbyname")) {
+                       pinbyname = true;
+                       NEXT_ARG();
                } else {
                        p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
                              *argv);
@@ -1616,6 +1620,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                                goto err_close_obj;
                }
 
+               if (pinbyname)
+                       bpf_program__set_pinname(pos,
+                                                (char *)bpf_program__name(pos));
                bpf_program__set_ifindex(pos, ifindex);
                bpf_program__set_type(pos, prog_type);
                bpf_program__set_expected_attach_type(pos, expected_attach_type);
tools/lib/bpf/libbpf.c +0 -0
index f6faa33c80fa..e8fc1d0fe16e 100644

tools/lib/bpf/libbpf.c void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) · line 8124

Quoted 6 lines
tools/lib/bpf/libbpf.c +3 -0
@@ -8119,6 +8119,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
        prog->prog_ifindex = ifindex;
 }
 
+void bpf_program__set_pinname(struct bpf_program *prog, char *name)
+{
+       prog->pin_name = name;

BPF maps have bpf_map__set_pin_path(), setting a full path is more flexible approach, I think, so if we had to do something here, it's better to add bpf_program__set_ping_path().

Quoted 21 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
 const char *bpf_program__name(const struct bpf_program *prog)
 {
        return prog->name;
tools/lib/bpf/libbpf.h +2 -1
index 4ec69f224342..107cf736c2bb 100644
@@ -216,6 +216,8 @@ LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
                                         __u32 ifindex);
+LIBBPF_API void bpf_program__set_pinname(struct bpf_program *prog,
+                                        char *name);
 
 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
--

2.30.2

#2 lore raw
Quoted 13 lines · Andrii Nakryiko
Quoted 11 lines · On Sat, Dec 4, 2021 at 8:51 PM <[email protected]> wrote:

From: Menglong Dong <[email protected]>

For now, the command 'bpftool prog loadall' use section name as the name of the pin file. However, once there are prog with the same section name in ELF file, this command will failed with the error 'File Exist'.

So, add the support of pin prog by function name with the 'pinbyname' argument.

Quoted 7 lines
Quoted 2 lines

---

Doesn't [0] do that already?

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Ops....Sorry, I didn't notice that patch :/

Quoted 45 lines
Quoted 45 lines
 tools/bpf/bpftool/prog.c | 7 +++++++
 tools/lib/bpf/libbpf.c   | 5 +++++
 tools/lib/bpf/libbpf.h   | 2 ++
 3 files changed, 14 insertions(+)
tools/bpf/bpftool/prog.c +7 -0
index e47e8b06cc3d..74e0aaebfefc 100644
@@ -1471,6 +1471,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
        unsigned int old_map_fds = 0;
        const char *pinmaps = NULL;
        struct bpf_object *obj;
+       bool pinbyname = false;
        struct bpf_map *map;
        const char *pinfile;
        unsigned int i, j;
@@ -1589,6 +1590,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                                goto err_free_reuse_maps;
 
                        pinmaps = GET_ARG();
+               } else if (is_prefix(*argv, "pinbyname")) {
+                       pinbyname = true;
+                       NEXT_ARG();
                } else {
                        p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
                              *argv);
@@ -1616,6 +1620,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                                goto err_close_obj;
                }
 
+               if (pinbyname)
+                       bpf_program__set_pinname(pos,
+                                                (char *)bpf_program__name(pos));
                bpf_program__set_ifindex(pos, ifindex);
                bpf_program__set_type(pos, prog_type);
                bpf_program__set_expected_attach_type(pos, expected_attach_type);
tools/lib/bpf/libbpf.c +0 -0
index f6faa33c80fa..e8fc1d0fe16e 100644
@@ -8119,6 +8119,11 @@ void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex)
        prog->prog_ifindex = ifindex;
 }
 
Quoted 7 lines

tools/lib/bpf/libbpf.c void bpf_program__set_ifindex(struct bpf_program *prog, __u32 ifindex) · line 8124

Quoted 3 lines
tools/lib/bpf/libbpf.c +3 -0
+void bpf_program__set_pinname(struct bpf_program *prog, char *name)
+{
+       prog->pin_name = name;

BPF maps have bpf_map__set_pin_path(), setting a full path is more flexible approach, I think, so if we had to do something here, it's better to add bpf_program__set_ping_path().

Yeah, I think it's a good idea. I'll do something about it.

Thanks!
Menglong Dong

Quoted 23 lines
Quoted 23 lines
tools/lib/bpf/libbpf.c +2 -0
+}
+
 const char *bpf_program__name(const struct bpf_program *prog)
 {
        return prog->name;
tools/lib/bpf/libbpf.h +2 -1
index 4ec69f224342..107cf736c2bb 100644
@@ -216,6 +216,8 @@ LIBBPF_API int bpf_program__set_priv(struct bpf_program *prog, void *priv,
 LIBBPF_API void *bpf_program__priv(const struct bpf_program *prog);
 LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
                                         __u32 ifindex);
+LIBBPF_API void bpf_program__set_pinname(struct bpf_program *prog,
+                                        char *name);
 
 LIBBPF_API const char *bpf_program__name(const struct bpf_program *prog);
 LIBBPF_API const char *bpf_program__section_name(const struct bpf_program *prog);
--

2.30.2