Skip to contentSkip to conversation

bpftool: specify XDP Hints ifname when loading program

PATCH Superseded Larysa Zaremba 3 participants 5 messages May 11, 2023 – May 15, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 <bpf_obj_path> <pin_path> dev xdpmeta <ifname>

---
 tools/bpf/bpftool/prog.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/bpf/bpftool/prog.c +21 -7
index 91b6075b2db3..a9cb96d99277 100644
@@ -1517,12 +1517,13 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	struct bpf_program *prog = NULL, *pos;
 	unsigned int old_map_fds = 0;
 	const char *pinmaps = NULL;
+	__u32 offload_ifindex = 0;
 	bool auto_attach = false;
+	__u32 meta_ifindex = 0;
 	struct bpf_object *obj;
 	struct bpf_map *map;
 	const char *pinfile;
 	unsigned int i, j;
-	__u32 ifindex = 0;
 	const char *file;
 	int idx, err;
 
@@ -1614,17 +1615,25 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 			map_replace[old_map_fds].fd = fd;
 			old_map_fds++;
 		} else if (is_prefix(*argv, "dev")) {
+			__u32 *cur_ifindex;
+
 			NEXT_ARG();
 
-			if (ifindex) {
-				p_err("offload device already specified");
+			if (offload_ifindex || meta_ifindex) {
+				p_err("device already specified");
 				goto err_free_reuse_maps;
 			}
+			if (is_prefix(*argv, "xdpmeta")) {
+				cur_ifindex = &meta_ifindex;
+				NEXT_ARG();
+			} else {
+				cur_ifindex = &offload_ifindex;
+			}
 			if (!REQ_ARGS(1))
 				goto err_free_reuse_maps;
 
-			ifindex = if_nametoindex(*argv);
-			if (!ifindex) {
+			*cur_ifindex = if_nametoindex(*argv);
+			if (!(*cur_ifindex)) {
 				p_err("unrecognized netdevice '%s': %s",
 				      *argv, strerror(errno));
 				goto err_free_reuse_maps;
@@ -1671,7 +1680,12 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 				goto err_close_obj;
 		}
 
-		bpf_program__set_ifindex(pos, ifindex);
+		if (prog_type == BPF_PROG_TYPE_XDP && meta_ifindex) {
+			bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
+			bpf_program__set_ifindex(pos, meta_ifindex);
+		} else {
+			bpf_program__set_ifindex(pos, offload_ifindex);
+		}
 		if (bpf_program__type(pos) != prog_type)
 			bpf_program__set_type(pos, prog_type);
 		bpf_program__set_expected_attach_type(pos, expected_attach_type);
@@ -1709,7 +1723,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	idx = 0;
 	bpf_object__for_each_map(map, obj) {
 		if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
-			bpf_map__set_ifindex(map, ifindex);
+			bpf_map__set_ifindex(map, offload_ifindex);
 
 		if (j < old_map_fds && idx == map_replace[j].idx) {
 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);
Signature · 1 line
2.35.3
#1 lore raw

2023-05-11 17:13 UTC+0200 ~ Larysa Zaremba <[email protected]>

Quoted 5 lines

Add ability to specify a network interface used to resolve XDP Hints kfuncs when loading program through bpftool.

Usage:
bpftool prog load <bpf_obj_path> <pin_path> dev xdpmeta <ifname>

Thanks for this patch!

Regarding the command-line syntax, I'm not a big fan of the optional sub-keyword for the device for XDP hints. I must admit I had not anticipated other another use for the "dev" keyword. Instead, have you considered one of the following:

1) Adding a different keyword ("xdpmeta_dev"?) and making it incompatible with "dev"

2) Another alternative would be adding a sub-keyword for offload too:

bpftool p l [...] dev <[offload <ifname> | xdpmeta <ifname>]>

If the ifname is provided with no sub-keyword, we would consider it for offload for legacy support, possibly warn that the syntax is deprecated.

What do you think?

Quoted 27 lines
---
 tools/bpf/bpftool/prog.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/bpf/bpftool/prog.c +2 -1
index 91b6075b2db3..a9cb96d99277 100644
@@ -1517,12 +1517,13 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	struct bpf_program *prog = NULL, *pos;
 	unsigned int old_map_fds = 0;
 	const char *pinmaps = NULL;
+	__u32 offload_ifindex = 0;
 	bool auto_attach = false;
+	__u32 meta_ifindex = 0;
 	struct bpf_object *obj;
 	struct bpf_map *map;
 	const char *pinfile;
 	unsigned int i, j;
-	__u32 ifindex = 0;
 	const char *file;
 	int idx, err;
 
@@ -1614,17 +1615,25 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 			map_replace[old_map_fds].fd = fd;

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1623

Quoted 10 lines
tools/bpf/bpftool/prog.c +4 -2
 			old_map_fds++;
 		} else if (is_prefix(*argv, "dev")) {
+			__u32 *cur_ifindex;
+
 			NEXT_ARG();
 
-			if (ifindex) {
-				p_err("offload device already specified");
+			if (offload_ifindex || meta_ifindex) {
+				p_err("device already specified");

I'd add a note to tell somehow that offload and XDP hints are mutually exclusive. I suspect not all users understand these well.

Quoted 33 lines
tools/bpf/bpftool/prog.c +14 -3
 				goto err_free_reuse_maps;
 			}
+			if (is_prefix(*argv, "xdpmeta")) {
+				cur_ifindex = &meta_ifindex;
+				NEXT_ARG();
+			} else {
+				cur_ifindex = &offload_ifindex;
+			}
 			if (!REQ_ARGS(1))
 				goto err_free_reuse_maps;
 
-			ifindex = if_nametoindex(*argv);
-			if (!ifindex) {
+			*cur_ifindex = if_nametoindex(*argv);
+			if (!(*cur_ifindex)) {
 				p_err("unrecognized netdevice '%s': %s",
 				      *argv, strerror(errno));
 				goto err_free_reuse_maps;
@@ -1671,7 +1680,12 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 				goto err_close_obj;
 		}
 
-		bpf_program__set_ifindex(pos, ifindex);
+		if (prog_type == BPF_PROG_TYPE_XDP && meta_ifindex) {
+			bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
+			bpf_program__set_ifindex(pos, meta_ifindex);
+		} else {
+			bpf_program__set_ifindex(pos, offload_ifindex);
+		}
 		if (bpf_program__type(pos) != prog_type)
 			bpf_program__set_type(pos, prog_type);
 		bpf_program__set_expected_attach_type(pos, expected_attach_type);

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1729

Quoted 8 lines
tools/bpf/bpftool/prog.c +1 -1
@@ -1709,7 +1723,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	idx = 0;
 	bpf_object__for_each_map(map, obj) {
 		if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
-			bpf_map__set_ifindex(map, ifindex);
+			bpf_map__set_ifindex(map, offload_ifindex);
 
 		if (j < old_map_fds && idx == map_replace[j].idx) {
 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);

Could you please also update the following items:

- The usage message for "bpftool prog load" near the end of prog.c

- The related doc in Documentation/bpftool-prog.rst (command summary near the top, and detailed description later in the doc)

- Bash completion (for keyword "dev", look for "_sysfs_get_netdevs" in bash-completion/bpftool). I'm happy to help with this one if necessary.

Thanks,
Quentin

#2 lore raw
Quoted 17 lines · Quentin Monnet

2023-05-11 17:13 UTC+0200 ~ Larysa Zaremba <[email protected]>

Quoted 5 lines

Add ability to specify a network interface used to resolve XDP Hints kfuncs when loading program through bpftool.

Usage:
bpftool prog load <bpf_obj_path> <pin_path> dev xdpmeta <ifname>

Thanks for this patch!

Regarding the command-line syntax, I'm not a big fan of the optional sub-keyword for the device for XDP hints. I must admit I had not anticipated other another use for the "dev" keyword. Instead, have you considered one of the following:

1) Adding a different keyword ("xdpmeta_dev"?) and making it incompatible with "dev"

Quoted 9 lines

2) Another alternative would be adding a sub-keyword for offload too:

bpftool p l [...] dev <[offload <ifname> | xdpmeta <ifname>]>

If the ifname is provided with no sub-keyword, we would consider it for offload for legacy support, possibly warn that the syntax is deprecated.

What do you think?

I think first option would look a little bit nicer, but I like the idea to deprecate "dev <ifname>". In my current version, forgetting to add "xdpmeta" resulted in not very descriptive errors, this may confuse new users. So what about:

bpftool prog load [...] xdpmeta_dev/offload_dev <ifname>

"dev <ifname>" syntax would still work, but with a big warning, like this:

'bpftool prog [...] dev <ifname>' syntax is deprecated. Going further, please use 'offload_dev <ifname>' to offload program to device. For XDP hints applications, use 'xdpmeta_dev <ifname>'.

Quoted 33 lines
Quoted 33 lines
---
 tools/bpf/bpftool/prog.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/bpf/bpftool/prog.c +4 -1
index 91b6075b2db3..a9cb96d99277 100644
@@ -1517,12 +1517,13 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	struct bpf_program *prog = NULL, *pos;
 	unsigned int old_map_fds = 0;
 	const char *pinmaps = NULL;
+	__u32 offload_ifindex = 0;
 	bool auto_attach = false;
+	__u32 meta_ifindex = 0;
 	struct bpf_object *obj;
 	struct bpf_map *map;
 	const char *pinfile;
 	unsigned int i, j;
-	__u32 ifindex = 0;
 	const char *file;
 	int idx, err;
 
@@ -1614,17 +1615,25 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 			map_replace[old_map_fds].fd = fd;
 			old_map_fds++;
 		} else if (is_prefix(*argv, "dev")) {
+			__u32 *cur_ifindex;
+
 			NEXT_ARG();
 
Quoted 7 lines

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1623

Quoted 4 lines
tools/bpf/bpftool/prog.c +2 -2
-			if (ifindex) {
-				p_err("offload device already specified");
+			if (offload_ifindex || meta_ifindex) {
+				p_err("device already specified");

I'd add a note to tell somehow that offload and XDP hints are mutually exclusive. I suspect not all users understand these well.

Ok, will do.

Quoted 45 lines
Quoted 34 lines
tools/bpf/bpftool/prog.c +14 -3
 				goto err_free_reuse_maps;
 			}
+			if (is_prefix(*argv, "xdpmeta")) {
+				cur_ifindex = &meta_ifindex;
+				NEXT_ARG();
+			} else {
+				cur_ifindex = &offload_ifindex;
+			}
 			if (!REQ_ARGS(1))
 				goto err_free_reuse_maps;
 
-			ifindex = if_nametoindex(*argv);
-			if (!ifindex) {
+			*cur_ifindex = if_nametoindex(*argv);
+			if (!(*cur_ifindex)) {
 				p_err("unrecognized netdevice '%s': %s",
 				      *argv, strerror(errno));
 				goto err_free_reuse_maps;
@@ -1671,7 +1680,12 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 				goto err_close_obj;
 		}
 
-		bpf_program__set_ifindex(pos, ifindex);
+		if (prog_type == BPF_PROG_TYPE_XDP && meta_ifindex) {
+			bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
+			bpf_program__set_ifindex(pos, meta_ifindex);
+		} else {
+			bpf_program__set_ifindex(pos, offload_ifindex);
+		}
 		if (bpf_program__type(pos) != prog_type)
 			bpf_program__set_type(pos, prog_type);
 		bpf_program__set_expected_attach_type(pos, expected_attach_type);

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1729

Quoted 8 lines
tools/bpf/bpftool/prog.c +1 -1
@@ -1709,7 +1723,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
 	idx = 0;
 	bpf_object__for_each_map(map, obj) {
 		if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
-			bpf_map__set_ifindex(map, ifindex);
+			bpf_map__set_ifindex(map, offload_ifindex);
 
 		if (j < old_map_fds && idx == map_replace[j].idx) {
 			err = bpf_map__reuse_fd(map, map_replace[j++].fd);

Could you please also update the following items:

Quoted 7 lines

- The usage message for "bpftool prog load" near the end of prog.c

- The related doc in Documentation/bpftool-prog.rst (command summary near the top, and detailed description later in the doc)

- Bash completion (for keyword "dev", look for "_sysfs_get_netdevs" in bash-completion/bpftool). I'm happy to help with this one if necessary.

Will do all the above in v2.

Quoted 3 lines

Thanks,
Quentin

#3 lore raw
Quoted 27 lines · Larysa Zaremba

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 24 lines · Quentin Monnet

2023-05-11 17:13 UTC+0200 ~ Larysa Zaremba <[email protected]>

Quoted 5 lines

Add ability to specify a network interface used to resolve XDP Hints kfuncs when loading program through bpftool.

Usage:
bpftool prog load <bpf_obj_path> <pin_path> dev xdpmeta <ifname>

Thanks for this patch!

Regarding the command-line syntax, I'm not a big fan of the optional sub-keyword for the device for XDP hints. I must admit I had not anticipated other another use for the "dev" keyword. Instead, have you considered one of the following:

1) Adding a different keyword ("xdpmeta_dev"?) and making it incompatible with "dev"

2) Another alternative would be adding a sub-keyword for offload too:

bpftool p l [...] dev <[offload <ifname> | xdpmeta <ifname>]>

If the ifname is provided with no sub-keyword, we would consider it for offload for legacy support, possibly warn that the syntax is deprecated.

Quoted 7 lines
Quoted 2 lines

What do you think?

I think first option would look a little bit nicer, but I like the idea to deprecate "dev <ifname>". In my current version, forgetting to add "xdpmeta" resulted in not very descriptive errors, this may confuse new users. So what about:

I agree the first option looks a little bit nicer, but I think both options would work.

Quoted 110 lines

bpftool prog load [...] xdpmeta_dev/offload_dev <ifname>

"dev <ifname>" syntax would still work, but with a big warning, like this:

'bpftool prog [...] dev <ifname>' syntax is deprecated. Going further, please use 'offload_dev <ifname>' to offload program to device. For XDP hints applications, use 'xdpmeta_dev <ifname>'.

Quoted 33 lines
Quoted 33 lines
---
 tools/bpf/bpftool/prog.c | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
tools/bpf/bpftool/prog.c +4 -1
index 91b6075b2db3..a9cb96d99277 100644
@@ -1517,12 +1517,13 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
    struct bpf_program *prog = NULL, *pos;
    unsigned int old_map_fds = 0;
    const char *pinmaps = NULL;
+   __u32 offload_ifindex = 0;
    bool auto_attach = false;
+   __u32 meta_ifindex = 0;
    struct bpf_object *obj;
    struct bpf_map *map;
    const char *pinfile;
    unsigned int i, j;
-   __u32 ifindex = 0;
    const char *file;
    int idx, err;
 
@@ -1614,17 +1615,25 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                    map_replace[old_map_fds].fd = fd;
                    old_map_fds++;
            } else if (is_prefix(*argv, "dev")) {
+                   __u32 *cur_ifindex;
+
                    NEXT_ARG();
 
Quoted 7 lines

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1623

Quoted 4 lines
tools/bpf/bpftool/prog.c +2 -2
-                   if (ifindex) {
-                           p_err("offload device already specified");
+                   if (offload_ifindex || meta_ifindex) {
+                           p_err("device already specified");

I'd add a note to tell somehow that offload and XDP hints are mutually exclusive. I suspect not all users understand these well.

Ok, will do.

Quoted 45 lines
Quoted 34 lines
tools/bpf/bpftool/prog.c +14 -3
                            goto err_free_reuse_maps;
                    }
+                   if (is_prefix(*argv, "xdpmeta")) {
+                           cur_ifindex = &meta_ifindex;
+                           NEXT_ARG();
+                   } else {
+                           cur_ifindex = &offload_ifindex;
+                   }
                    if (!REQ_ARGS(1))
                            goto err_free_reuse_maps;
 
-                   ifindex = if_nametoindex(*argv);
-                   if (!ifindex) {
+                   *cur_ifindex = if_nametoindex(*argv);
+                   if (!(*cur_ifindex)) {
                            p_err("unrecognized netdevice '%s': %s",
                                  *argv, strerror(errno));
                            goto err_free_reuse_maps;
@@ -1671,7 +1680,12 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
                            goto err_close_obj;
            }
 
-           bpf_program__set_ifindex(pos, ifindex);
+           if (prog_type == BPF_PROG_TYPE_XDP && meta_ifindex) {
+                   bpf_program__set_flags(pos, BPF_F_XDP_DEV_BOUND_ONLY);
+                   bpf_program__set_ifindex(pos, meta_ifindex);
+           } else {
+                   bpf_program__set_ifindex(pos, offload_ifindex);
+           }
            if (bpf_program__type(pos) != prog_type)
                    bpf_program__set_type(pos, prog_type);
            bpf_program__set_expected_attach_type(pos, expected_attach_type);

tools/bpf/bpftool/prog.c static int load_with_options(int argc, char **argv, bool first_prog_only) · line 1729

Quoted 8 lines
tools/bpf/bpftool/prog.c +1 -1
@@ -1709,7 +1723,7 @@ static int load_with_options(int argc, char **argv, bool first_prog_only)
    idx = 0;
    bpf_object__for_each_map(map, obj) {
            if (bpf_map__type(map) != BPF_MAP_TYPE_PERF_EVENT_ARRAY)
-                   bpf_map__set_ifindex(map, ifindex);
+                   bpf_map__set_ifindex(map, offload_ifindex);
 
            if (j < old_map_fds && idx == map_replace[j].idx) {
                    err = bpf_map__reuse_fd(map, map_replace[j++].fd);

Could you please also update the following items:

Quoted 7 lines

- The usage message for "bpftool prog load" near the end of prog.c

- The related doc in Documentation/bpftool-prog.rst (command summary near the top, and detailed description later in the doc)

- Bash completion (for keyword "dev", look for "_sysfs_get_netdevs" in bash-completion/bpftool). I'm happy to help with this one if necessary.

Will do all the above in v2.

Quoted 3 lines

Thanks,
Quentin

Signature · 2 lines
Kind Regards,
Niklas Söderlund
#4 lore raw

2023-05-15 18:30 UTC+0200 ~ Niklas Söderlund <[email protected]>

Quoted 31 lines · Larysa Zaremba
Quoted 30 lines

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Quoted 26 lines · Quentin Monnet

2023-05-11 17:13 UTC+0200 ~ Larysa Zaremba <[email protected]>

Quoted 5 lines

Add ability to specify a network interface used to resolve XDP Hints kfuncs when loading program through bpftool.

Usage:
bpftool prog load <bpf_obj_path> <pin_path> dev xdpmeta <ifname>

Thanks for this patch!

Regarding the command-line syntax, I'm not a big fan of the optional sub-keyword for the device for XDP hints. I must admit I had not anticipated other another use for the "dev" keyword. Instead, have you considered one of the following:

1) Adding a different keyword ("xdpmeta_dev"?) and making it incompatible with "dev"

2) Another alternative would be adding a sub-keyword for offload too:

bpftool p l [...] dev <[offload <ifname> | xdpmeta <ifname>]>

If the ifname is provided with no sub-keyword, we would consider it for offload for legacy support, possibly warn that the syntax is deprecated.

What do you think?

Quoted 16 lines
Quoted 4 lines

I think first option would look a little bit nicer, but I like the idea to deprecate "dev <ifname>". In my current version, forgetting to add "xdpmeta" resulted in not very descriptive errors, this may confuse new users. So what about:

I agree the first option looks a little bit nicer, but I think both options would work.

Quoted 8 lines

bpftool prog load [...] xdpmeta_dev/offload_dev <ifname>

"dev <ifname>" syntax would still work, but with a big warning, like this:

'bpftool prog [...] dev <ifname>' syntax is deprecated. Going further, please use 'offload_dev <ifname>' to offload program to device. For XDP hints applications, use 'xdpmeta_dev <ifname>'.

OK let's go with this

Thanks,
Quentin