Skip to content

Commit 12f9682

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpftool: Add autoattach for bpf prog load|loadall'
Wang Yufen says: ==================== This patchset add "autoattach" optional for "bpftool prog load(_all)" to support one-step load-attach-pin_link. v8 -> v9: fix link leak, and change pathname_concat(specify not just buffer pointer, but also it's size) v7 -> v8: for the programs not supporting autoattach, fall back to reguler pinning instead of skipping v6 -> v7: add info msg print and update doc for the skip program v5 -> v6: skip the programs not supporting auto-attach, and change optional name from "auto_attach" to "autoattach" v4 -> v5: some formatting nits of doc v3 -> v4: rename functions, update doc, bash and do_help() v2 -> v3: switch to extend prog load command instead of extend perf v2: https://patchwork.kernel.org/project/netdevbpf/patch/20220824033837.458197-1-weiyongjun1@huawei.com/ v1: https://patchwork.kernel.org/project/netdevbpf/patch/20220816151725.153343-1-weiyongjun1@huawei.com/ ==================== Signed-off-by: Alexei Starovoitov <ast@kernel.org>
2 parents 98af374 + b81a677 commit 12f9682

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

tools/bpf/bpftool/Documentation/bpftool-prog.rst

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PROG COMMANDS
3131
| **bpftool** **prog dump xlated** *PROG* [{**file** *FILE* | **opcodes** | **visual** | **linum**}]
3232
| **bpftool** **prog dump jited** *PROG* [{**file** *FILE* | **opcodes** | **linum**}]
3333
| **bpftool** **prog pin** *PROG* *FILE*
34-
| **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*]
34+
| **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
3535
| **bpftool** **prog attach** *PROG* *ATTACH_TYPE* [*MAP*]
3636
| **bpftool** **prog detach** *PROG* *ATTACH_TYPE* [*MAP*]
3737
| **bpftool** **prog tracelog**
@@ -131,7 +131,7 @@ DESCRIPTION
131131
contain a dot character ('.'), which is reserved for future
132132
extensions of *bpffs*.
133133

134-
**bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*]
134+
**bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
135135
Load bpf program(s) from binary *OBJ* and pin as *PATH*.
136136
**bpftool prog load** pins only the first program from the
137137
*OBJ* as *PATH*. **bpftool prog loadall** pins all programs
@@ -150,6 +150,17 @@ DESCRIPTION
150150
Optional **pinmaps** argument can be provided to pin all
151151
maps under *MAP_DIR* directory.
152152

153+
If **autoattach** is specified program will be attached
154+
before pin. In that case, only the link (representing the
155+
program attached to its hook) is pinned, not the program as
156+
such, so the path won't show in **bpftool prog show -f**,
157+
only show in **bpftool link show -f**. Also, this only works
158+
when bpftool (libbpf) is able to infer all necessary
159+
information from the object file, in particular, it's not
160+
supported for all program types. If a program does not
161+
support autoattach, bpftool falls back to regular pinning
162+
for that program instead.
163+
153164
Note: *PATH* must be located in *bpffs* mount. It must not
154165
contain a dot character ('.'), which is reserved for future
155166
extensions of *bpffs*.

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,7 @@ _bpftool()
505505
_bpftool_once_attr 'type'
506506
_bpftool_once_attr 'dev'
507507
_bpftool_once_attr 'pinmaps'
508+
_bpftool_once_attr 'autoattach'
508509
return 0
509510
;;
510511
esac

tools/bpf/bpftool/prog.c

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,67 @@ get_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
14531453
return ret;
14541454
}
14551455

1456+
static int
1457+
auto_attach_program(struct bpf_program *prog, const char *path)
1458+
{
1459+
struct bpf_link *link;
1460+
int err;
1461+
1462+
link = bpf_program__attach(prog);
1463+
if (!link) {
1464+
p_info("Program %s does not support autoattach, falling back to pinning",
1465+
bpf_program__name(prog));
1466+
return bpf_obj_pin(bpf_program__fd(prog), path);
1467+
}
1468+
1469+
err = bpf_link__pin(link, path);
1470+
bpf_link__destroy(link);
1471+
return err;
1472+
}
1473+
1474+
static int pathname_concat(char *buf, size_t buf_sz, const char *path, const char *name)
1475+
{
1476+
int len;
1477+
1478+
len = snprintf(buf, buf_sz, "%s/%s", path, name);
1479+
if (len < 0)
1480+
return -EINVAL;
1481+
if ((size_t)len >= buf_sz)
1482+
return -ENAMETOOLONG;
1483+
1484+
return 0;
1485+
}
1486+
1487+
static int
1488+
auto_attach_programs(struct bpf_object *obj, const char *path)
1489+
{
1490+
struct bpf_program *prog;
1491+
char buf[PATH_MAX];
1492+
int err;
1493+
1494+
bpf_object__for_each_program(prog, obj) {
1495+
err = pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog));
1496+
if (err)
1497+
goto err_unpin_programs;
1498+
1499+
err = auto_attach_program(prog, buf);
1500+
if (err)
1501+
goto err_unpin_programs;
1502+
}
1503+
1504+
return 0;
1505+
1506+
err_unpin_programs:
1507+
while ((prog = bpf_object__prev_program(obj, prog))) {
1508+
if (pathname_concat(buf, sizeof(buf), path, bpf_program__name(prog)))
1509+
continue;
1510+
1511+
bpf_program__unpin(prog, buf);
1512+
}
1513+
1514+
return err;
1515+
}
1516+
14561517
static int load_with_options(int argc, char **argv, bool first_prog_only)
14571518
{
14581519
enum bpf_prog_type common_prog_type = BPF_PROG_TYPE_UNSPEC;
@@ -1464,6 +1525,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
14641525
struct bpf_program *prog = NULL, *pos;
14651526
unsigned int old_map_fds = 0;
14661527
const char *pinmaps = NULL;
1528+
bool auto_attach = false;
14671529
struct bpf_object *obj;
14681530
struct bpf_map *map;
14691531
const char *pinfile;
@@ -1583,6 +1645,9 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
15831645
goto err_free_reuse_maps;
15841646

15851647
pinmaps = GET_ARG();
1648+
} else if (is_prefix(*argv, "autoattach")) {
1649+
auto_attach = true;
1650+
NEXT_ARG();
15861651
} else {
15871652
p_err("expected no more arguments, 'type', 'map' or 'dev', got: '%s'?",
15881653
*argv);
@@ -1692,14 +1757,20 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
16921757
goto err_close_obj;
16931758
}
16941759

1695-
err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
1760+
if (auto_attach)
1761+
err = auto_attach_program(prog, pinfile);
1762+
else
1763+
err = bpf_obj_pin(bpf_program__fd(prog), pinfile);
16961764
if (err) {
16971765
p_err("failed to pin program %s",
16981766
bpf_program__section_name(prog));
16991767
goto err_close_obj;
17001768
}
17011769
} else {
1702-
err = bpf_object__pin_programs(obj, pinfile);
1770+
if (auto_attach)
1771+
err = auto_attach_programs(obj, pinfile);
1772+
else
1773+
err = bpf_object__pin_programs(obj, pinfile);
17031774
if (err) {
17041775
p_err("failed to pin all programs");
17051776
goto err_close_obj;
@@ -2338,6 +2409,7 @@ static int do_help(int argc, char **argv)
23382409
" [type TYPE] [dev NAME] \\\n"
23392410
" [map { idx IDX | name NAME } MAP]\\\n"
23402411
" [pinmaps MAP_DIR]\n"
2412+
" [autoattach]\n"
23412413
" %1$s %2$s attach PROG ATTACH_TYPE [MAP]\n"
23422414
" %1$s %2$s detach PROG ATTACH_TYPE [MAP]\n"
23432415
" %1$s %2$s run PROG \\\n"

0 commit comments

Comments
 (0)