Skip to content

Commit f46392e

Browse files
walking-machineborkmann
authored andcommitted
bpftool: Specify XDP Hints ifname when loading program
Add ability to specify a network interface used to resolve XDP hints kfuncs when loading program through bpftool. Usage: bpftool prog load [...] xdpmeta_dev <ifname> Writing just 'dev <ifname>' instead of 'xdpmeta_dev' is a very probable mistake that results in not very descriptive errors, so 'bpftool prog load [...] dev <ifname>' syntax becomes deprecated, followed by 'bpftool map create [...] dev <ifname>' for consistency. Now, to offload program, execute: bpftool prog load [...] offload_dev <ifname> To offload map: bpftool map create [...] offload_dev <ifname> 'dev <ifname>' still performs offloading in the commands above, but now triggers a warning and is excluded from bash completion. 'xdpmeta_dev' and 'offload_dev' are mutually exclusive options, because 'xdpmeta_dev' basically makes a program device-bound without loading it onto the said device. For now, offloaded programs cannot use XDP hints [0], but if this changes, using 'offload_dev <ifname>' should cover this case. [0] https://lore.kernel.org/bpf/[email protected] Signed-off-by: Larysa Zaremba <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Reviewed-by: Quentin Monnet <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 6cc385d commit f46392e

File tree

5 files changed

+64
-20
lines changed

5 files changed

+64
-20
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ MAP COMMANDS
2828
| **bpftool** **map** { **show** | **list** } [*MAP*]
2929
| **bpftool** **map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* \
3030
| **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] \
31-
| [**dev** *NAME*]
31+
| [**offload_dev** *NAME*]
3232
| **bpftool** **map dump** *MAP*
3333
| **bpftool** **map update** *MAP* [**key** *DATA*] [**value** *VALUE*] [*UPDATE_FLAGS*]
3434
| **bpftool** **map lookup** *MAP* [**key** *DATA*]
@@ -73,7 +73,7 @@ DESCRIPTION
7373
maps. On such kernels bpftool will automatically emit this
7474
information as well.
7575

76-
**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] [**dev** *NAME*]
76+
**bpftool map create** *FILE* **type** *TYPE* **key** *KEY_SIZE* **value** *VALUE_SIZE* **entries** *MAX_ENTRIES* **name** *NAME* [**flags** *FLAGS*] [**inner_map** *MAP*] [**offload_dev** *NAME*]
7777
Create a new map with given parameters and pin it to *bpffs*
7878
as *FILE*.
7979

@@ -86,8 +86,8 @@ DESCRIPTION
8686
kernel needs it to collect metadata related to the inner maps
8787
that the new map will work with.
8888

89-
Keyword **dev** expects a network interface name, and is used
90-
to request hardware offload for the map.
89+
Keyword **offload_dev** expects a network interface name,
90+
and is used to request hardware offload for the map.
9191

9292
**bpftool map dump** *MAP*
9393
Dump all entries in a given *MAP*. In case of **name**,

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PROG COMMANDS
3131
| **bpftool** **prog dump xlated** *PROG* [{ **file** *FILE* | [**opcodes**] [**linum**] [**visual**] }]
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*] [**autoattach**]
34+
| **bpftool** **prog** { **load** | **loadall** } *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_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**
@@ -129,7 +129,7 @@ DESCRIPTION
129129
contain a dot character ('.'), which is reserved for future
130130
extensions of *bpffs*.
131131

132-
**bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** {**idx** *IDX* | **name** *NAME*} *MAP*] [**dev** *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
132+
**bpftool prog { load | loadall }** *OBJ* *PATH* [**type** *TYPE*] [**map** { **idx** *IDX* | **name** *NAME* } *MAP*] [{ **offload_dev** | **xdpmeta_dev** } *NAME*] [**pinmaps** *MAP_DIR*] [**autoattach**]
133133
Load bpf program(s) from binary *OBJ* and pin as *PATH*.
134134
**bpftool prog load** pins only the first program from the
135135
*OBJ* as *PATH*. **bpftool prog loadall** pins all programs
@@ -143,8 +143,11 @@ DESCRIPTION
143143
to be replaced in the ELF file counting from 0, while *NAME*
144144
allows to replace a map by name. *MAP* specifies the map to
145145
use, referring to it by **id** or through a **pinned** file.
146-
If **dev** *NAME* is specified program will be loaded onto
147-
given networking device (offload).
146+
If **offload_dev** *NAME* is specified program will be loaded
147+
onto given networking device (offload).
148+
If **xdpmeta_dev** *NAME* is specified program will become
149+
device-bound without offloading, this facilitates access
150+
to XDP metadata.
148151
Optional **pinmaps** argument can be provided to pin all
149152
maps under *MAP_DIR* directory.
150153

tools/bpf/bpftool/bash-completion/bpftool

+4-3
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ _bpftool()
278278
_bpftool_get_prog_tags
279279
return 0
280280
;;
281-
dev)
281+
dev|offload_dev|xdpmeta_dev)
282282
_sysfs_get_netdevs
283283
return 0
284284
;;
@@ -508,7 +508,8 @@ _bpftool()
508508
;;
509509
*)
510510
COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
511-
_bpftool_once_attr 'type dev pinmaps autoattach'
511+
_bpftool_once_attr 'type pinmaps autoattach'
512+
_bpftool_one_of_list 'offload_dev xdpmeta_dev'
512513
return 0
513514
;;
514515
esac
@@ -733,7 +734,7 @@ _bpftool()
733734
esac
734735
;;
735736
*)
736-
_bpftool_once_attr 'type key value entries name flags dev'
737+
_bpftool_once_attr 'type key value entries name flags offload_dev'
737738
if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then
738739
_bpftool_once_attr 'inner_map'
739740
fi

tools/bpf/bpftool/map.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,11 @@ static int do_create(int argc, char **argv)
12871287
"flags"))
12881288
goto exit;
12891289
} else if (is_prefix(*argv, "dev")) {
1290+
p_info("Warning: 'bpftool map create [...] dev <ifname>' syntax is deprecated.\n"
1291+
"Going further, please use 'offload_dev <ifname>' to request hardware offload for the map.");
1292+
goto offload_dev;
1293+
} else if (is_prefix(*argv, "offload_dev")) {
1294+
offload_dev:
12901295
NEXT_ARG();
12911296

12921297
if (attr.map_ifindex) {
@@ -1431,7 +1436,7 @@ static int do_help(int argc, char **argv)
14311436
"Usage: %1$s %2$s { show | list } [MAP]\n"
14321437
" %1$s %2$s create FILE type TYPE key KEY_SIZE value VALUE_SIZE \\\n"
14331438
" entries MAX_ENTRIES name NAME [flags FLAGS] \\\n"
1434-
" [inner_map MAP] [dev NAME]\n"
1439+
" [inner_map MAP] [offload_dev NAME]\n"
14351440
" %1$s %2$s dump MAP\n"
14361441
" %1$s %2$s update MAP [key DATA] [value VALUE] [UPDATE_FLAGS]\n"
14371442
" %1$s %2$s lookup MAP [key DATA]\n"

tools/bpf/bpftool/prog.c

+43-8
Original file line numberDiff line numberDiff line change
@@ -1517,12 +1517,13 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
15171517
struct bpf_program *prog = NULL, *pos;
15181518
unsigned int old_map_fds = 0;
15191519
const char *pinmaps = NULL;
1520+
__u32 xdpmeta_ifindex = 0;
1521+
__u32 offload_ifindex = 0;
15201522
bool auto_attach = false;
15211523
struct bpf_object *obj;
15221524
struct bpf_map *map;
15231525
const char *pinfile;
15241526
unsigned int i, j;
1525-
__u32 ifindex = 0;
15261527
const char *file;
15271528
int idx, err;
15281529

@@ -1614,17 +1615,46 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
16141615
map_replace[old_map_fds].fd = fd;
16151616
old_map_fds++;
16161617
} else if (is_prefix(*argv, "dev")) {
1618+
p_info("Warning: 'bpftool prog load [...] dev <ifname>' syntax is deprecated.\n"
1619+
"Going further, please use 'offload_dev <ifname>' to offload program to device.\n"
1620+
"For applications using XDP hints only, use 'xdpmeta_dev <ifname>'.");
1621+
goto offload_dev;
1622+
} else if (is_prefix(*argv, "offload_dev")) {
1623+
offload_dev:
16171624
NEXT_ARG();
16181625

1619-
if (ifindex) {
1620-
p_err("offload device already specified");
1626+
if (offload_ifindex) {
1627+
p_err("offload_dev already specified");
1628+
goto err_free_reuse_maps;
1629+
} else if (xdpmeta_ifindex) {
1630+
p_err("xdpmeta_dev and offload_dev are mutually exclusive");
1631+
goto err_free_reuse_maps;
1632+
}
1633+
if (!REQ_ARGS(1))
1634+
goto err_free_reuse_maps;
1635+
1636+
offload_ifindex = if_nametoindex(*argv);
1637+
if (!offload_ifindex) {
1638+
p_err("unrecognized netdevice '%s': %s",
1639+
*argv, strerror(errno));
1640+
goto err_free_reuse_maps;
1641+
}
1642+
NEXT_ARG();
1643+
} else if (is_prefix(*argv, "xdpmeta_dev")) {
1644+
NEXT_ARG();
1645+
1646+
if (xdpmeta_ifindex) {
1647+
p_err("xdpmeta_dev already specified");
1648+
goto err_free_reuse_maps;
1649+
} else if (offload_ifindex) {
1650+
p_err("xdpmeta_dev and offload_dev are mutually exclusive");
16211651
goto err_free_reuse_maps;
16221652
}
16231653
if (!REQ_ARGS(1))
16241654
goto err_free_reuse_maps;
16251655

1626-
ifindex = if_nametoindex(*argv);
1627-
if (!ifindex) {
1656+
xdpmeta_ifindex = if_nametoindex(*argv);
1657+
if (!xdpmeta_ifindex) {
16281658
p_err("unrecognized netdevice '%s': %s",
16291659
*argv, strerror(errno));
16301660
goto err_free_reuse_maps;
@@ -1671,7 +1701,12 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
16711701
goto err_close_obj;
16721702
}
16731703

1674-
bpf_program__set_ifindex(pos, ifindex);
1704+
if (prog_type == BPF_PROG_TYPE_XDP && xdpmeta_ifindex) {
1705+
bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
1706+
bpf_program__set_ifindex(pos, xdpmeta_ifindex);
1707+
} else {
1708+
bpf_program__set_ifindex(pos, offload_ifindex);
1709+
}
16751710
if (bpf_program__type(pos) != prog_type)
16761711
bpf_program__set_type(pos, prog_type);
16771712
bpf_program__set_expected_attach_type(pos, expected_attach_type);
@@ -1709,7 +1744,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
17091744
idx = 0;
17101745
bpf_object__for_each_map(map, obj) {
17111746
if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
1712-
bpf_map__set_ifindex(map, ifindex);
1747+
bpf_map__set_ifindex(map, offload_ifindex);
17131748

17141749
if (j < old_map_fds && idx == map_replace[j].idx) {
17151750
err = bpf_map__reuse_fd(map, map_replace[j++].fd);
@@ -2416,7 +2451,7 @@ static int do_help(int argc, char **argv)
24162451
" %1$s %2$s dump jited PROG [{ file FILE | [opcodes] [linum] }]\n"
24172452
" %1$s %2$s pin PROG FILE\n"
24182453
" %1$s %2$s { load | loadall } OBJ PATH \\\n"
2419-
" [type TYPE] [dev NAME] \\\n"
2454+
" [type TYPE] [{ offload_dev | xdpmeta_dev } NAME] \\\n"
24202455
" [map { idx IDX | name NAME } MAP]\\\n"
24212456
" [pinmaps MAP_DIR]\n"
24222457
" [autoattach]\n"

0 commit comments

Comments
 (0)