diff options
author | David Beckett <david.beckett@netronome.com> | 2018-05-16 17:02:49 -0400 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2018-05-16 18:54:26 -0400 |
commit | f0307a7ed17fa8925321a4f58f5ca56eeedd4fa3 (patch) | |
tree | 49c56445e06a7feed53aacd70eb8cef39ebc40f2 /tools/lib/bpf | |
parent | be2d04d11fd33bd46622f94619aae1596d9f9303 (diff) |
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>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r-- | tools/lib/bpf/bpf.c | 2 | ||||
-rw-r--r-- | tools/lib/bpf/bpf.h | 2 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.c | 18 | ||||
-rw-r--r-- | tools/lib/bpf/libbpf.h | 1 |
4 files changed, 20 insertions, 3 deletions
diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c index a3a8fb2ac697..6a8a00097fd8 100644 --- a/tools/lib/bpf/bpf.c +++ b/tools/lib/bpf/bpf.c | |||
@@ -91,6 +91,7 @@ int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr) | |||
91 | attr.btf_fd = create_attr->btf_fd; | 91 | attr.btf_fd = create_attr->btf_fd; |
92 | attr.btf_key_id = create_attr->btf_key_id; | 92 | attr.btf_key_id = create_attr->btf_key_id; |
93 | attr.btf_value_id = create_attr->btf_value_id; | 93 | attr.btf_value_id = create_attr->btf_value_id; |
94 | attr.map_ifindex = create_attr->map_ifindex; | ||
94 | 95 | ||
95 | return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); | 96 | return sys_bpf(BPF_MAP_CREATE, &attr, sizeof(attr)); |
96 | } | 97 | } |
@@ -201,6 +202,7 @@ int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, | |||
201 | attr.log_size = 0; | 202 | attr.log_size = 0; |
202 | attr.log_level = 0; | 203 | attr.log_level = 0; |
203 | attr.kern_version = load_attr->kern_version; | 204 | attr.kern_version = load_attr->kern_version; |
205 | attr.prog_ifindex = load_attr->prog_ifindex; | ||
204 | memcpy(attr.prog_name, load_attr->name, | 206 | memcpy(attr.prog_name, load_attr->name, |
205 | min(name_len, BPF_OBJ_NAME_LEN - 1)); | 207 | min(name_len, BPF_OBJ_NAME_LEN - 1)); |
206 | 208 | ||
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h index fb3a146d92ff..15bff7728cf1 100644 --- a/tools/lib/bpf/bpf.h +++ b/tools/lib/bpf/bpf.h | |||
@@ -38,6 +38,7 @@ struct bpf_create_map_attr { | |||
38 | __u32 btf_fd; | 38 | __u32 btf_fd; |
39 | __u32 btf_key_id; | 39 | __u32 btf_key_id; |
40 | __u32 btf_value_id; | 40 | __u32 btf_value_id; |
41 | __u32 map_ifindex; | ||
41 | }; | 42 | }; |
42 | 43 | ||
43 | int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); | 44 | int bpf_create_map_xattr(const struct bpf_create_map_attr *create_attr); |
@@ -64,6 +65,7 @@ struct bpf_load_program_attr { | |||
64 | size_t insns_cnt; | 65 | size_t insns_cnt; |
65 | const char *license; | 66 | const char *license; |
66 | __u32 kern_version; | 67 | __u32 kern_version; |
68 | __u32 prog_ifindex; | ||
67 | }; | 69 | }; |
68 | 70 | ||
69 | /* Recommend log buffer size */ | 71 | /* Recommend log buffer size */ |
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index df54c4c9e48a..3dbe217bf23e 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -178,6 +178,7 @@ struct bpf_program { | |||
178 | /* Index in elf obj file, for relocation use. */ | 178 | /* Index in elf obj file, for relocation use. */ |
179 | int idx; | 179 | int idx; |
180 | char *name; | 180 | char *name; |
181 | int prog_ifindex; | ||
181 | char *section_name; | 182 | char *section_name; |
182 | struct bpf_insn *insns; | 183 | struct bpf_insn *insns; |
183 | size_t insns_cnt, main_prog_cnt; | 184 | size_t insns_cnt, main_prog_cnt; |
@@ -213,6 +214,7 @@ struct bpf_map { | |||
213 | int fd; | 214 | int fd; |
214 | char *name; | 215 | char *name; |
215 | size_t offset; | 216 | size_t offset; |
217 | int map_ifindex; | ||
216 | struct bpf_map_def def; | 218 | struct bpf_map_def def; |
217 | uint32_t btf_key_id; | 219 | uint32_t btf_key_id; |
218 | uint32_t btf_value_id; | 220 | uint32_t btf_value_id; |
@@ -1091,6 +1093,7 @@ bpf_object__create_maps(struct bpf_object *obj) | |||
1091 | int *pfd = &map->fd; | 1093 | int *pfd = &map->fd; |
1092 | 1094 | ||
1093 | create_attr.name = map->name; | 1095 | create_attr.name = map->name; |
1096 | create_attr.map_ifindex = map->map_ifindex; | ||
1094 | create_attr.map_type = def->type; | 1097 | create_attr.map_type = def->type; |
1095 | create_attr.map_flags = def->map_flags; | 1098 | create_attr.map_flags = def->map_flags; |
1096 | create_attr.key_size = def->key_size; | 1099 | create_attr.key_size = def->key_size; |
@@ -1273,7 +1276,7 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) | |||
1273 | static int | 1276 | static int |
1274 | load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, | 1277 | load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, |
1275 | const char *name, struct bpf_insn *insns, int insns_cnt, | 1278 | const char *name, struct bpf_insn *insns, int insns_cnt, |
1276 | char *license, u32 kern_version, int *pfd) | 1279 | char *license, u32 kern_version, int *pfd, int prog_ifindex) |
1277 | { | 1280 | { |
1278 | struct bpf_load_program_attr load_attr; | 1281 | struct bpf_load_program_attr load_attr; |
1279 | char *log_buf; | 1282 | char *log_buf; |
@@ -1287,6 +1290,7 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, | |||
1287 | load_attr.insns_cnt = insns_cnt; | 1290 | load_attr.insns_cnt = insns_cnt; |
1288 | load_attr.license = license; | 1291 | load_attr.license = license; |
1289 | load_attr.kern_version = kern_version; | 1292 | load_attr.kern_version = kern_version; |
1293 | load_attr.prog_ifindex = prog_ifindex; | ||
1290 | 1294 | ||
1291 | if (!load_attr.insns || !load_attr.insns_cnt) | 1295 | if (!load_attr.insns || !load_attr.insns_cnt) |
1292 | return -EINVAL; | 1296 | return -EINVAL; |
@@ -1368,7 +1372,8 @@ bpf_program__load(struct bpf_program *prog, | |||
1368 | } | 1372 | } |
1369 | err = load_program(prog->type, prog->expected_attach_type, | 1373 | err = load_program(prog->type, prog->expected_attach_type, |
1370 | prog->name, prog->insns, prog->insns_cnt, | 1374 | prog->name, prog->insns, prog->insns_cnt, |
1371 | license, kern_version, &fd); | 1375 | license, kern_version, &fd, |
1376 | prog->prog_ifindex); | ||
1372 | if (!err) | 1377 | if (!err) |
1373 | prog->instances.fds[0] = fd; | 1378 | prog->instances.fds[0] = fd; |
1374 | goto out; | 1379 | goto out; |
@@ -1399,7 +1404,8 @@ bpf_program__load(struct bpf_program *prog, | |||
1399 | err = load_program(prog->type, prog->expected_attach_type, | 1404 | err = load_program(prog->type, prog->expected_attach_type, |
1400 | prog->name, result.new_insn_ptr, | 1405 | prog->name, result.new_insn_ptr, |
1401 | result.new_insn_cnt, | 1406 | result.new_insn_cnt, |
1402 | license, kern_version, &fd); | 1407 | license, kern_version, &fd, |
1408 | prog->prog_ifindex); | ||
1403 | 1409 | ||
1404 | if (err) { | 1410 | if (err) { |
1405 | pr_warning("Loading the %dth instance of program '%s' failed\n", | 1411 | 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, | |||
2188 | enum bpf_attach_type expected_attach_type; | 2194 | enum bpf_attach_type expected_attach_type; |
2189 | enum bpf_prog_type prog_type; | 2195 | enum bpf_prog_type prog_type; |
2190 | struct bpf_object *obj; | 2196 | struct bpf_object *obj; |
2197 | struct bpf_map *map; | ||
2191 | int section_idx; | 2198 | int section_idx; |
2192 | int err; | 2199 | int err; |
2193 | 2200 | ||
@@ -2207,6 +2214,7 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, | |||
2207 | * section name. | 2214 | * section name. |
2208 | */ | 2215 | */ |
2209 | prog_type = attr->prog_type; | 2216 | prog_type = attr->prog_type; |
2217 | prog->prog_ifindex = attr->ifindex; | ||
2210 | expected_attach_type = attr->expected_attach_type; | 2218 | expected_attach_type = attr->expected_attach_type; |
2211 | if (prog_type == BPF_PROG_TYPE_UNSPEC) { | 2219 | if (prog_type == BPF_PROG_TYPE_UNSPEC) { |
2212 | section_idx = bpf_program__identify_section(prog); | 2220 | section_idx = bpf_program__identify_section(prog); |
@@ -2227,6 +2235,10 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, | |||
2227 | first_prog = prog; | 2235 | first_prog = prog; |
2228 | } | 2236 | } |
2229 | 2237 | ||
2238 | bpf_map__for_each(map, obj) { | ||
2239 | map->map_ifindex = attr->ifindex; | ||
2240 | } | ||
2241 | |||
2230 | if (!first_prog) { | 2242 | if (!first_prog) { |
2231 | pr_warning("object file doesn't contain bpf program\n"); | 2243 | pr_warning("object file doesn't contain bpf program\n"); |
2232 | bpf_object__close(obj); | 2244 | bpf_object__close(obj); |
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h index 4574b9563278..cd3fd8d782c7 100644 --- a/tools/lib/bpf/libbpf.h +++ b/tools/lib/bpf/libbpf.h | |||
@@ -259,6 +259,7 @@ struct bpf_prog_load_attr { | |||
259 | const char *file; | 259 | const char *file; |
260 | enum bpf_prog_type prog_type; | 260 | enum bpf_prog_type prog_type; |
261 | enum bpf_attach_type expected_attach_type; | 261 | enum bpf_attach_type expected_attach_type; |
262 | int ifindex; | ||
262 | }; | 263 | }; |
263 | 264 | ||
264 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, | 265 | int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, |