libbpf: add ifindex to enable offload support
authorDavid Beckett <david.beckett@netronome.com>
Wed, 16 May 2018 21:02:49 +0000 (14:02 -0700)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 16 May 2018 22:54:26 +0000 (00:54 +0200)
BPF programs currently can only be offloaded using iproute2. This
patch will allow programs to be offloaded using libbpf calls.

Signed-off-by: David Beckett <david.beckett@netronome.com>
Reviewed-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/lib/bpf/bpf.c
tools/lib/bpf/bpf.h
tools/lib/bpf/libbpf.c
tools/lib/bpf/libbpf.h

index a3a8fb2ac6970484e7f9280282fe6ab71b6843d5..6a8a00097fd8f07956dca01fcb040fc92cb54774 100644 (file)
@@ -91,6 +91,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr)
        attr.btf_fd = create_attr->btf_fd;
        attr.btf_key_id = create_attr->btf_key_id;
        attr.btf_value_id = create_attr->btf_value_id;
+       attr.map_ifindex = create_attr->map_ifindex;
 
        return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
 }
@@ -201,6 +202,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
        attr.log_size = 0;
        attr.log_level = 0;
        attr.kern_version = load_attr->kern_version;
+       attr.prog_ifindex = load_attr->prog_ifindex;
        memcpy(attr.prog_name, load_attr->name,
               min(name_len, BPF_OBJ_NAME_LEN - 1));
 
index fb3a146d92ff680ef729e277521668e97284f2d4..15bff7728cf1d0131316633d7e60ab27aa3100fe 100644 (file)
@@ -38,6 +38,7 @@ struct bpf_create_map_attr {
        __u32 btf_fd;
        __u32 btf_key_id;
        __u32 btf_value_id;
+       __u32 map_ifindex;
 };
 
 int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr);
@@ -64,6 +65,7 @@ struct bpf_load_program_attr {
        size_t insns_cnt;
        const char *license;
        __u32 kern_version;
+       __u32 prog_ifindex;
 };
 
 /* Recommend log buffer size */
index df54c4c9e48a2c698bc3c964a0877a9f54f64ebe..3dbe217bf23ea50561a1c08adc207943642aa1d7 100644 (file)
@@ -178,6 +178,7 @@ struct bpf_program {
        /* Index in elf obj file, for relocation use. */
        int idx;
        char *name;
+       int prog_ifindex;
        char *section_name;
        struct bpf_insn *insns;
        size_t insns_cnt, main_prog_cnt;
@@ -213,6 +214,7 @@ struct bpf_map {
        int fd;
        char *name;
        size_t offset;
+       int map_ifindex;
        struct bpf_map_def def;
        uint32_t btf_key_id;
        uint32_t btf_value_id;
@@ -1091,6 +1093,7 @@ bpf_object__create_maps(struct bpf_object *obj)
                int *pfd = &map->fd;
 
                create_attr.name = map->name;
+               create_attr.map_ifindex = map->map_ifindex;
                create_attr.map_type = def->type;
                create_attr.map_flags = def->map_flags;
                create_attr.key_size = def->key_size;
@@ -1273,7 +1276,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
 static int
 load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
             const char *name, struct bpf_insn *insns, int insns_cnt,
-            char *license, u32 kern_version, int *pfd)
+            char *license, u32 kern_version, int *pfd, int prog_ifindex)
 {
        struct bpf_load_program_attr load_attr;
        char *log_buf;
@@ -1287,6 +1290,7 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
        load_attr.insns_cnt = insns_cnt;
        load_attr.license = license;
        load_attr.kern_version = kern_version;
+       load_attr.prog_ifindex = prog_ifindex;
 
        if (!load_attr.insns || !load_attr.insns_cnt)
                return -EINVAL;
@@ -1368,7 +1372,8 @@ bpf_program__load(struct bpf_program *prog,
                }
                err = load_program(prog->type, prog->expected_attach_type,
                                   prog->name, prog->insns, prog->insns_cnt,
-                                  license, kern_version, &fd);
+                                  license, kern_version, &fd,
+                                  prog->prog_ifindex);
                if (!err)
                        prog->instances.fds[0] = fd;
                goto out;
@@ -1399,7 +1404,8 @@ bpf_program__load(struct bpf_program *prog,
                err = load_program(prog->type, prog->expected_attach_type,
                                   prog->name, result.new_insn_ptr,
                                   result.new_insn_cnt,
-                                  license, kern_version, &fd);
+                                  license, kern_version, &fd,
+                                  prog->prog_ifindex);
 
                if (err) {
                        pr_warning("Loading the %dth instance of program '%s' failed\n",
@@ -2188,6 +2194,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
        enum bpf_attach_type expected_attach_type;
        enum bpf_prog_type prog_type;
        struct bpf_object *obj;
+       struct bpf_map *map;
        int section_idx;
        int err;
 
@@ -2207,6 +2214,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
                 * section name.
                 */
                prog_type = attr->prog_type;
+               prog->prog_ifindex = attr->ifindex;
                expected_attach_type = attr->expected_attach_type;
                if (prog_type == BPF_PROG_TYPE_UNSPEC) {
                        section_idx = bpf_program__identify_section(prog);
@@ -2227,6 +2235,10 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
                        first_prog = prog;
        }
 
+       bpf_map__for_each(map, obj) {
+               map->map_ifindex = attr->ifindex;
+       }
+
        if (!first_prog) {
                pr_warning("object file doesn't contain bpf program\n");
                bpf_object__close(obj);
index 4574b956327879a171f03c32401dc6ca1d03d10f..cd3fd8d782c7bd8813d6b36f0378fc38ac3a370f 100644 (file)
@@ -259,6 +259,7 @@ struct bpf_prog_load_attr {
        const char *file;
        enum bpf_prog_type prog_type;
        enum bpf_attach_type expected_attach_type;
+       int ifindex;
 };
 
 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,