Re: [PATCH iproute2-next 3/5] lib: add libbpf support
From: Andrii Nakryiko <hidden>
Date: 2020-10-24 00:21:33
Also in:
bpf
On Thu, Oct 22, 2020 at 8:39 PM Hangbin Liu [off-list ref] wrote:
This patch converts iproute2 to use libbpf for loading and attaching BPF programs when it is available, which is started by Toke's implementation[1]. With libbpf iproute2 could correctly process BTF information and support the new-style BTF-defined maps, while keeping compatibility with the old internal map definition syntax. The old iproute2 bpf code is kept and will be used if no suitable libbpf is available. When using libbpf, wrapper code in bpf_legacy.c ensures that iproute2 will still understand the old map definition format, including populating map-in-map and tail call maps before load. In bpf_libbpf.c, we init iproute2 ctx and elf info first to check the legacy bytes. When handling the legacy maps, for map-in-maps, we create them manually and re-use the fd as they are associated with id/inner_id. For pin maps, we only set the pin path and let libbp load to handle it. For tail calls, we find it first and update the element after prog load.
I never implemented tail call map initialization using the same approach as declarative map-in-map support in libbpf, because no one asked and/or showed a use case. But all the pieces are there, and if there's interest, we should probably support that in libbpf as well.
Other maps/progs will be loaded by libbpf directly. Note: ip/ipvrf.c is not convert to use libbpf as it only encodes a few instructions and load directly. [1] https://lore.kernel.org/bpf/20190820114706.18546-1-toke@redhat.com/ (local) Reviewed-by: Toke Høiland-Jørgensen <redacted> Signed-off-by: Hangbin Liu <redacted> --- include/bpf_util.h | 11 ++ lib/Makefile | 4 + lib/bpf_legacy.c | 178 ++++++++++++++++++++++++ lib/bpf_libbpf.c | 338 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 531 insertions(+) create mode 100644 lib/bpf_libbpf.c
[...]
+
+static int load_bpf_object(struct bpf_cfg_in *cfg)
+{
+ struct bpf_program *p, *prog = NULL;
+ struct bpf_object *obj;
+ char root_path[PATH_MAX];
+ struct bpf_map *map;
+ int prog_fd, ret = 0;
+
+ ret = iproute2_get_root_path(root_path, PATH_MAX);
+ if (ret)
+ return ret;
+
+ DECLARE_LIBBPF_OPTS(bpf_object_open_opts, open_opts,
+ .relaxed_maps = true,
+ .pin_root_path = root_path,
+ );
+
+ obj = bpf_object__open_file(cfg->object, &open_opts);
+ if (IS_ERR_OR_NULL(obj))libbpf defines libbpf_get_error() to check that the returned pointer is not encoding error, you shouldn't need to define your IS_ERR macros.
+ return -ENOENT; +
[...]