libbpf: add ifindex to enable offload support

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>
This commit is contained in:
David Beckett 2018-05-16 14:02:49 -07:00 committed by Daniel Borkmann
parent be2d04d11f
commit f0307a7ed1
4 changed files with 20 additions and 3 deletions

View 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_fd = create_attr->btf_fd;
attr.btf_key_id = create_attr->btf_key_id; attr.btf_key_id = create_attr->btf_key_id;
attr.btf_value_id = create_attr->btf_value_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)); 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_size = 0;
attr.log_level = 0; attr.log_level = 0;
attr.kern_version = load_attr->kern_version; attr.kern_version = load_attr->kern_version;
attr.prog_ifindex = load_attr->prog_ifindex;
memcpy(attr.prog_name, load_attr->name, memcpy(attr.prog_name, load_attr->name,
min(name_len, BPF_OBJ_NAME_LEN - 1)); min(name_len, BPF_OBJ_NAME_LEN - 1));

View File

@ -38,6 +38,7 @@ struct bpf_create_map_attr {
__u32 btf_fd; __u32 btf_fd;
__u32 btf_key_id; __u32 btf_key_id;
__u32 btf_value_id; __u32 btf_value_id;
__u32 map_ifindex;
}; };
int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); 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; size_t insns_cnt;
const char *license; const char *license;
__u32 kern_version; __u32 kern_version;
__u32 prog_ifindex;
}; };
/* Recommend log buffer size */ /* Recommend log buffer size */

View File

@ -178,6 +178,7 @@ struct bpf_program {
/* Index in elf obj file, for relocation use. */ /* Index in elf obj file, for relocation use. */
int idx; int idx;
char *name; char *name;
int prog_ifindex;
char *section_name; char *section_name;
struct bpf_insn *insns; struct bpf_insn *insns;
size_t insns_cnt, main_prog_cnt; size_t insns_cnt, main_prog_cnt;
@ -213,6 +214,7 @@ struct bpf_map {
int fd; int fd;
char *name; char *name;
size_t offset; size_t offset;
int map_ifindex;
struct bpf_map_def def; struct bpf_map_def def;
uint32_t btf_key_id; uint32_t btf_key_id;
uint32_t btf_value_id; uint32_t btf_value_id;
@ -1091,6 +1093,7 @@ bpf_object__create_maps(struct bpf_object *obj)
int *pfd = &map->fd; int *pfd = &map->fd;
create_attr.name = map->name; create_attr.name = map->name;
create_attr.map_ifindex = map->map_ifindex;
create_attr.map_type = def->type; create_attr.map_type = def->type;
create_attr.map_flags = def->map_flags; create_attr.map_flags = def->map_flags;
create_attr.key_size = def->key_size; create_attr.key_size = def->key_size;
@ -1273,7 +1276,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj)
static int static int
load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type,
const char *name, struct bpf_insn *insns, int insns_cnt, 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; struct bpf_load_program_attr load_attr;
char *log_buf; 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.insns_cnt = insns_cnt;
load_attr.license = license; load_attr.license = license;
load_attr.kern_version = kern_version; load_attr.kern_version = kern_version;
load_attr.prog_ifindex = prog_ifindex;
if (!load_attr.insns || !load_attr.insns_cnt) if (!load_attr.insns || !load_attr.insns_cnt)
return -EINVAL; return -EINVAL;
@ -1368,7 +1372,8 @@ bpf_program__load(struct bpf_program *prog,
} }
err = load_program(prog->type, prog->expected_attach_type, err = load_program(prog->type, prog->expected_attach_type,
prog->name, prog->insns, prog->insns_cnt, prog->name, prog->insns, prog->insns_cnt,
license, kern_version, &fd); license, kern_version, &fd,
prog->prog_ifindex);
if (!err) if (!err)
prog->instances.fds[0] = fd; prog->instances.fds[0] = fd;
goto out; goto out;
@ -1399,7 +1404,8 @@ bpf_program__load(struct bpf_program *prog,
err = load_program(prog->type, prog->expected_attach_type, err = load_program(prog->type, prog->expected_attach_type,
prog->name, result.new_insn_ptr, prog->name, result.new_insn_ptr,
result.new_insn_cnt, result.new_insn_cnt,
license, kern_version, &fd); license, kern_version, &fd,
prog->prog_ifindex);
if (err) { if (err) {
pr_warning("Loading the %dth instance of program '%s' failed\n", 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_attach_type expected_attach_type;
enum bpf_prog_type prog_type; enum bpf_prog_type prog_type;
struct bpf_object *obj; struct bpf_object *obj;
struct bpf_map *map;
int section_idx; int section_idx;
int err; int err;
@ -2207,6 +2214,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
* section name. * section name.
*/ */
prog_type = attr->prog_type; prog_type = attr->prog_type;
prog->prog_ifindex = attr->ifindex;
expected_attach_type = attr->expected_attach_type; expected_attach_type = attr->expected_attach_type;
if (prog_type == BPF_PROG_TYPE_UNSPEC) { if (prog_type == BPF_PROG_TYPE_UNSPEC) {
section_idx = bpf_program__identify_section(prog); 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; first_prog = prog;
} }
bpf_map__for_each(map, obj) {
map->map_ifindex = attr->ifindex;
}
if (!first_prog) { if (!first_prog) {
pr_warning("object file doesn't contain bpf program\n"); pr_warning("object file doesn't contain bpf program\n");
bpf_object__close(obj); bpf_object__close(obj);

View File

@ -259,6 +259,7 @@ struct bpf_prog_load_attr {
const char *file; const char *file;
enum bpf_prog_type prog_type; enum bpf_prog_type prog_type;
enum bpf_attach_type expected_attach_type; enum bpf_attach_type expected_attach_type;
int ifindex;
}; };
int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,