]> git.ipfire.org Git - thirdparty/iproute2.git/commitdiff
libbpf: add xdp program name support
authorHangbin Liu <liuhangbin@gmail.com>
Tue, 5 Jul 2022 04:25:01 +0000 (12:25 +0800)
committerDavid Ahern <dsahern@kernel.org>
Fri, 8 Jul 2022 15:09:13 +0000 (09:09 -0600)
In bpf program, only the program name is unique. Before this patch, if there
are multiple programs with the same section name, only the first program
will be attached. With program name support, users could specify the exact
program they want to attach.

Note this feature is only supported when iproute2 build with libbpf.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
Signed-off-by: David Ahern <dsahern@kernel.org>
include/bpf_util.h
ip/iplink.c
lib/bpf_legacy.c
lib/bpf_libbpf.c
man/man8/ip-link.8.in

index abb9627556ef098e7c19281e77d1896d6b73094f..6a5f8ec6529c319153ad9f4161a9d28346fd31f2 100644 (file)
@@ -68,6 +68,7 @@ enum bpf_mode {
 struct bpf_cfg_in {
        const char *object;
        const char *section;
+       const char *prog_name;
        const char *uds;
        enum bpf_prog_type type;
        enum bpf_mode mode;
index c64721bc6bceb57ede26209269e79853fb997aa1..b98c1694991e8d629618a5260eac751ed6a644ed 100644 (file)
@@ -107,7 +107,11 @@ void iplink_usage(void)
                "                        [ node_guid EUI64 ]\n"
                "                        [ port_guid EUI64 ] ]\n"
                "               [ { xdp | xdpgeneric | xdpdrv | xdpoffload } { off |\n"
+#ifdef HAVE_LIBBPF
+               "                         object FILE [ { section | program } NAME ] [ verbose ] |\n"
+#else
                "                         object FILE [ section NAME ] [ verbose ] |\n"
+#endif
                "                         pinned FILE } ]\n"
                "               [ master DEVICE ][ vrf NAME ]\n"
                "               [ nomaster ]\n"
index 9bf7c1c493b49179b6a3ab72b23915b7b2f7bf74..4fabdcc899e4d5685be71b9d82fa685cf753bcc9 100644 (file)
@@ -831,7 +831,7 @@ static int bpf_obj_pinned(const char *pathname, enum bpf_prog_type type)
 
 static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
 {
-       const char *file, *section, *uds_name;
+       const char *file, *section, *uds_name, *prog_name;
        bool verbose = false;
        int i, ret, argc;
        char **argv;
@@ -862,7 +862,7 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
        }
 
        NEXT_ARG();
-       file = section = uds_name = NULL;
+       file = section = uds_name = prog_name = NULL;
        if (cfg->mode == EBPF_OBJECT || cfg->mode == EBPF_PINNED) {
                file = *argv;
                NEXT_ARG_FWD();
@@ -899,6 +899,12 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
                        NEXT_ARG_FWD();
                }
 
+               if (argc > 0 && strcmp(*argv, "program") == 0) {
+                       NEXT_ARG();
+                       prog_name = *argv;
+                       NEXT_ARG_FWD();
+               }
+
                if (__bpf_prog_meta[cfg->type].may_uds_export) {
                        uds_name = getenv(BPF_ENV_UDS);
                        if (argc > 0 && !uds_name &&
@@ -936,6 +942,7 @@ static int bpf_do_parse(struct bpf_cfg_in *cfg, const bool *opt_tbl)
        cfg->argc    = argc;
        cfg->argv    = argv;
        cfg->verbose = verbose;
+       cfg->prog_name = prog_name;
 
        return ret;
 }
index 7b16ee71589d79b5c4aa5277fccbacc1292216b2..e1c211a1b3b722b64c307d87bf3205dba34555cc 100644 (file)
@@ -254,6 +254,22 @@ static bool bpf_map_is_offload_neutral(const struct bpf_map *map)
        return bpf_map__type(map) == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
 }
 
+static bool find_prog_to_attach(struct bpf_program *prog,
+                               struct bpf_program *exist_prog,
+                               const char *section, const char *prog_name)
+{
+       if (exist_prog)
+               return false;
+
+       /* We have default section name 'prog'. So do not check
+        * section name if there already has program name.
+        */
+       if (prog_name)
+               return !strcmp(bpf_program__name(prog), prog_name);
+       else
+               return !strcmp(get_bpf_program__section_name(prog), section);
+}
+
 static int load_bpf_object(struct bpf_cfg_in *cfg)
 {
        struct bpf_program *p, *prog = NULL;
@@ -278,8 +294,9 @@ static int load_bpf_object(struct bpf_cfg_in *cfg)
        }
 
        bpf_object__for_each_program(p, obj) {
-               bool prog_to_attach = !prog && cfg->section &&
-                       !strcmp(get_bpf_program__section_name(p), cfg->section);
+               bool prog_to_attach = find_prog_to_attach(p, prog,
+                                                         cfg->section,
+                                                         cfg->prog_name);
 
                /* Only load the programs that will either be subsequently
                 * attached or inserted into a tail call map */
@@ -304,7 +321,10 @@ static int load_bpf_object(struct bpf_cfg_in *cfg)
        }
 
        if (!prog) {
-               fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section);
+               if (cfg->prog_name)
+                       fprintf(stderr, "object file doesn't contain prog %s\n", cfg->prog_name);
+               else
+                       fprintf(stderr, "object file doesn't contain sec %s\n", cfg->section);
                return -ENOENT;
        }
 
index 3dbcdbb60c5645a54694b8137a611c3ac5a43614..c45c10622251782e312bca004a471d8b8831293f 100644 (file)
@@ -149,7 +149,7 @@ ip-link \- network device configuration
 .in +8
 .BR object
 .IR FILE
-.RB "[ " section
+.RB "[ { " section " | " program " } "
 .IR NAME " ]"
 .RB "[ " verbose " ] |"
 .br
@@ -2342,6 +2342,13 @@ to be passed with the
 .B object
 option.
 
+.BI program " NAME "
+- Specifies the BPF program name that need to be attached. When the program
+name is specified, the section name parameter will be ignored. This option
+only works when iproute2 build with
+.B libbpf
+support.
+
 .BI verbose
 - Act in verbose mode. For example, even in case of success, this will
 print the verifier log in case a program was loaded from a BPF ELF file.