Commit 472aeb38 authored by Alexei Starovoitov's avatar Alexei Starovoitov

Merge branch 'map-pinning'

Toke Høiland-Jørgensen says:

====================
This series fixes a few bugs in libbpf that I discovered while playing around
with the new auto-pinning code, and writing the first utility in xdp-tools[0]:

- If object loading fails, libbpf does not clean up the pinnings created by the
  auto-pinning mechanism.
- EPERM is not propagated to the caller on program load
- Netlink functions write error messages directly to stderr

In addition, libbpf currently only has a somewhat limited getter function for
XDP link info, which makes it impossible to discover whether an attached program
is in SKB mode or not. So the last patch in the series adds a new getter for XDP
link info which returns all the information returned via netlink (and which can
be extended later).

Finally, add a getter for BPF program size, which can be used by the caller to
estimate the amount of locked memory needed to load a program.

A selftest is added for the pinning change, while the other features were tested
in the xdp-filter tool from the xdp-tools repo. The 'new-libbpf-features' branch
contains the commits that make use of the new XDP getter and the corrected EPERM
error code.

[0] https://github.com/xdp-project/xdp-tools

Changelog:

v4:
  - Don't do any size checks on struct xdp_info, just copy (and/or zero)
    whatever size the caller supplied.

v3:
  - Pass through all kernel error codes on program load (instead of just EPERM).
  - No new bpf_object__unload() variant, just do the loop at the caller
  - Don't reject struct xdp_info sizes that are bigger than what we expect.
  - Add a comment noting that bpf_program__size() returns the size in bytes

v2:
  - Keep function names in libbpf.map sorted properly
====================
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parents 451d1dc8 1a734efe
...@@ -229,6 +229,7 @@ struct bpf_map { ...@@ -229,6 +229,7 @@ struct bpf_map {
enum libbpf_map_type libbpf_type; enum libbpf_map_type libbpf_type;
char *pin_path; char *pin_path;
bool pinned; bool pinned;
bool reused;
}; };
struct bpf_secdata { struct bpf_secdata {
...@@ -1995,6 +1996,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd) ...@@ -1995,6 +1996,7 @@ int bpf_map__reuse_fd(struct bpf_map *map, int fd)
map->def.map_flags = info.map_flags; map->def.map_flags = info.map_flags;
map->btf_key_type_id = info.btf_key_type_id; map->btf_key_type_id = info.btf_key_type_id;
map->btf_value_type_id = info.btf_value_type_id; map->btf_value_type_id = info.btf_value_type_id;
map->reused = true;
return 0; return 0;
...@@ -3719,7 +3721,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -3719,7 +3721,7 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
free(log_buf); free(log_buf);
goto retry_load; goto retry_load;
} }
ret = -LIBBPF_ERRNO__LOAD; ret = -errno;
cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg)); cp = libbpf_strerror_r(errno, errmsg, sizeof(errmsg));
pr_warn("load bpf program failed: %s\n", cp); pr_warn("load bpf program failed: %s\n", cp);
...@@ -3732,9 +3734,8 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -3732,9 +3734,8 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
pr_warn("Program too large (%zu insns), at most %d insns\n", pr_warn("Program too large (%zu insns), at most %d insns\n",
load_attr.insns_cnt, BPF_MAXINSNS); load_attr.insns_cnt, BPF_MAXINSNS);
ret = -LIBBPF_ERRNO__PROG2BIG; ret = -LIBBPF_ERRNO__PROG2BIG;
} else { } else if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
/* Wrong program type? */ /* Wrong program type? */
if (load_attr.prog_type != BPF_PROG_TYPE_KPROBE) {
int fd; int fd;
load_attr.prog_type = BPF_PROG_TYPE_KPROBE; load_attr.prog_type = BPF_PROG_TYPE_KPROBE;
...@@ -3747,10 +3748,6 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -3747,10 +3748,6 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
} }
} }
if (log_buf)
ret = -LIBBPF_ERRNO__KVER;
}
out: out:
free(log_buf); free(log_buf);
return ret; return ret;
...@@ -4026,7 +4023,7 @@ int bpf_object__unload(struct bpf_object *obj) ...@@ -4026,7 +4023,7 @@ int bpf_object__unload(struct bpf_object *obj)
int bpf_object__load_xattr(struct bpf_object_load_attr *attr) int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
{ {
struct bpf_object *obj; struct bpf_object *obj;
int err; int err, i;
if (!attr) if (!attr)
return -EINVAL; return -EINVAL;
...@@ -4047,6 +4044,11 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr) ...@@ -4047,6 +4044,11 @@ int bpf_object__load_xattr(struct bpf_object_load_attr *attr)
return 0; return 0;
out: out:
/* unpin any maps that were auto-pinned during load */
for (i = 0; i < obj->nr_maps; i++)
if (obj->maps[i].pinned && !obj->maps[i].reused)
bpf_map__unpin(&obj->maps[i], NULL);
bpf_object__unload(obj); bpf_object__unload(obj);
pr_warn("failed to load object '%s'\n", obj->path); pr_warn("failed to load object '%s'\n", obj->path);
return err; return err;
...@@ -4780,6 +4782,11 @@ int bpf_program__fd(const struct bpf_program *prog) ...@@ -4780,6 +4782,11 @@ int bpf_program__fd(const struct bpf_program *prog)
return bpf_program__nth_fd(prog, 0); return bpf_program__nth_fd(prog, 0);
} }
size_t bpf_program__size(const struct bpf_program *prog)
{
return prog->insns_cnt * sizeof(struct bpf_insn);
}
int bpf_program__set_prep(struct bpf_program *prog, int nr_instances, int bpf_program__set_prep(struct bpf_program *prog, int nr_instances,
bpf_program_prep_t prep) bpf_program_prep_t prep)
{ {
......
...@@ -214,6 +214,9 @@ LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog, ...@@ -214,6 +214,9 @@ LIBBPF_API void bpf_program__set_ifindex(struct bpf_program *prog,
LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog, LIBBPF_API const char *bpf_program__title(const struct bpf_program *prog,
bool needs_copy); bool needs_copy);
/* returns program size in bytes */
LIBBPF_API size_t bpf_program__size(const struct bpf_program *prog);
LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license, LIBBPF_API int bpf_program__load(struct bpf_program *prog, char *license,
__u32 kern_version); __u32 kern_version);
LIBBPF_API int bpf_program__fd(const struct bpf_program *prog); LIBBPF_API int bpf_program__fd(const struct bpf_program *prog);
...@@ -427,8 +430,18 @@ LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, ...@@ -427,8 +430,18 @@ LIBBPF_API int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type, LIBBPF_API int bpf_prog_load(const char *file, enum bpf_prog_type type,
struct bpf_object **pobj, int *prog_fd); struct bpf_object **pobj, int *prog_fd);
struct xdp_link_info {
__u32 prog_id;
__u32 drv_prog_id;
__u32 hw_prog_id;
__u32 skb_prog_id;
__u8 attach_mode;
};
LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags); LIBBPF_API int bpf_set_link_xdp_fd(int ifindex, int fd, __u32 flags);
LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags); LIBBPF_API int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags);
LIBBPF_API int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
size_t info_size, __u32 flags);
struct perf_buffer; struct perf_buffer;
......
...@@ -193,6 +193,7 @@ LIBBPF_0.0.5 { ...@@ -193,6 +193,7 @@ LIBBPF_0.0.5 {
LIBBPF_0.0.6 { LIBBPF_0.0.6 {
global: global:
bpf_get_link_xdp_info;
bpf_map__get_pin_path; bpf_map__get_pin_path;
bpf_map__is_pinned; bpf_map__is_pinned;
bpf_map__set_pin_path; bpf_map__set_pin_path;
...@@ -202,4 +203,5 @@ LIBBPF_0.0.6 { ...@@ -202,4 +203,5 @@ LIBBPF_0.0.6 {
bpf_program__get_type; bpf_program__get_type;
bpf_program__is_tracing; bpf_program__is_tracing;
bpf_program__set_tracing; bpf_program__set_tracing;
bpf_program__size;
} LIBBPF_0.0.5; } LIBBPF_0.0.5;
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "bpf.h" #include "bpf.h"
#include "libbpf.h" #include "libbpf.h"
#include "libbpf_internal.h"
#include "nlattr.h" #include "nlattr.h"
#ifndef SOL_NETLINK #ifndef SOL_NETLINK
...@@ -24,7 +25,7 @@ typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t, ...@@ -24,7 +25,7 @@ typedef int (*__dump_nlmsg_t)(struct nlmsghdr *nlmsg, libbpf_dump_nlmsg_t,
struct xdp_id_md { struct xdp_id_md {
int ifindex; int ifindex;
__u32 flags; __u32 flags;
__u32 id; struct xdp_link_info info;
}; };
int libbpf_netlink_open(__u32 *nl_pid) int libbpf_netlink_open(__u32 *nl_pid)
...@@ -43,7 +44,7 @@ int libbpf_netlink_open(__u32 *nl_pid) ...@@ -43,7 +44,7 @@ int libbpf_netlink_open(__u32 *nl_pid)
if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK, if (setsockopt(sock, SOL_NETLINK, NETLINK_EXT_ACK,
&one, sizeof(one)) < 0) { &one, sizeof(one)) < 0) {
fprintf(stderr, "Netlink error reporting not supported\n"); pr_warn("Netlink error reporting not supported\n");
} }
if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) { if (bind(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
...@@ -202,26 +203,11 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh, ...@@ -202,26 +203,11 @@ static int __dump_link_nlmsg(struct nlmsghdr *nlh,
return dump_link_nlmsg(cookie, ifi, tb); return dump_link_nlmsg(cookie, ifi, tb);
} }
static unsigned char get_xdp_id_attr(unsigned char mode, __u32 flags) static int get_xdp_info(void *cookie, void *msg, struct nlattr **tb)
{
if (mode != XDP_ATTACHED_MULTI)
return IFLA_XDP_PROG_ID;
if (flags & XDP_FLAGS_DRV_MODE)
return IFLA_XDP_DRV_PROG_ID;
if (flags & XDP_FLAGS_HW_MODE)
return IFLA_XDP_HW_PROG_ID;
if (flags & XDP_FLAGS_SKB_MODE)
return IFLA_XDP_SKB_PROG_ID;
return IFLA_XDP_UNSPEC;
}
static int get_xdp_id(void *cookie, void *msg, struct nlattr **tb)
{ {
struct nlattr *xdp_tb[IFLA_XDP_MAX + 1]; struct nlattr *xdp_tb[IFLA_XDP_MAX + 1];
struct xdp_id_md *xdp_id = cookie; struct xdp_id_md *xdp_id = cookie;
struct ifinfomsg *ifinfo = msg; struct ifinfomsg *ifinfo = msg;
unsigned char mode, xdp_attr;
int ret; int ret;
if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index) if (xdp_id->ifindex && xdp_id->ifindex != ifinfo->ifi_index)
...@@ -237,27 +223,40 @@ static int get_xdp_id(void *cookie, void *msg, struct nlattr **tb) ...@@ -237,27 +223,40 @@ static int get_xdp_id(void *cookie, void *msg, struct nlattr **tb)
if (!xdp_tb[IFLA_XDP_ATTACHED]) if (!xdp_tb[IFLA_XDP_ATTACHED])
return 0; return 0;
mode = libbpf_nla_getattr_u8(xdp_tb[IFLA_XDP_ATTACHED]); xdp_id->info.attach_mode = libbpf_nla_getattr_u8(
if (mode == XDP_ATTACHED_NONE) xdp_tb[IFLA_XDP_ATTACHED]);
return 0;
xdp_attr = get_xdp_id_attr(mode, xdp_id->flags); if (xdp_id->info.attach_mode == XDP_ATTACHED_NONE)
if (!xdp_attr || !xdp_tb[xdp_attr])
return 0; return 0;
xdp_id->id = libbpf_nla_getattr_u32(xdp_tb[xdp_attr]); if (xdp_tb[IFLA_XDP_PROG_ID])
xdp_id->info.prog_id = libbpf_nla_getattr_u32(
xdp_tb[IFLA_XDP_PROG_ID]);
if (xdp_tb[IFLA_XDP_SKB_PROG_ID])
xdp_id->info.skb_prog_id = libbpf_nla_getattr_u32(
xdp_tb[IFLA_XDP_SKB_PROG_ID]);
if (xdp_tb[IFLA_XDP_DRV_PROG_ID])
xdp_id->info.drv_prog_id = libbpf_nla_getattr_u32(
xdp_tb[IFLA_XDP_DRV_PROG_ID]);
if (xdp_tb[IFLA_XDP_HW_PROG_ID])
xdp_id->info.hw_prog_id = libbpf_nla_getattr_u32(
xdp_tb[IFLA_XDP_HW_PROG_ID]);
return 0; return 0;
} }
int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags) int bpf_get_link_xdp_info(int ifindex, struct xdp_link_info *info,
size_t info_size, __u32 flags)
{ {
struct xdp_id_md xdp_id = {}; struct xdp_id_md xdp_id = {};
int sock, ret; int sock, ret;
__u32 nl_pid; __u32 nl_pid;
__u32 mask; __u32 mask;
if (flags & ~XDP_FLAGS_MASK) if (flags & ~XDP_FLAGS_MASK || !info_size)
return -EINVAL; return -EINVAL;
/* Check whether the single {HW,DRV,SKB} mode is set */ /* Check whether the single {HW,DRV,SKB} mode is set */
...@@ -273,14 +272,44 @@ int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags) ...@@ -273,14 +272,44 @@ int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
xdp_id.ifindex = ifindex; xdp_id.ifindex = ifindex;
xdp_id.flags = flags; xdp_id.flags = flags;
ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_id, &xdp_id); ret = libbpf_nl_get_link(sock, nl_pid, get_xdp_info, &xdp_id);
if (!ret) if (!ret) {
*prog_id = xdp_id.id; size_t sz = min(info_size, sizeof(xdp_id.info));
memcpy(info, &xdp_id.info, sz);
memset((void *) info + sz, 0, info_size - sz);
}
close(sock); close(sock);
return ret; return ret;
} }
static __u32 get_xdp_id(struct xdp_link_info *info, __u32 flags)
{
if (info->attach_mode != XDP_ATTACHED_MULTI)
return info->prog_id;
if (flags & XDP_FLAGS_DRV_MODE)
return info->drv_prog_id;
if (flags & XDP_FLAGS_HW_MODE)
return info->hw_prog_id;
if (flags & XDP_FLAGS_SKB_MODE)
return info->skb_prog_id;
return 0;
}
int bpf_get_link_xdp_id(int ifindex, __u32 *prog_id, __u32 flags)
{
struct xdp_link_info info;
int ret;
ret = bpf_get_link_xdp_info(ifindex, &info, sizeof(info), flags);
if (!ret)
*prog_id = get_xdp_id(&info, flags);
return ret;
}
int libbpf_nl_get_link(int sock, unsigned int nl_pid, int libbpf_nl_get_link(int sock, unsigned int nl_pid,
libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie) libbpf_dump_nlmsg_t dump_link_nlmsg, void *cookie)
{ {
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include <errno.h> #include <errno.h>
#include "nlattr.h" #include "nlattr.h"
#include "libbpf_internal.h"
#include <linux/rtnetlink.h> #include <linux/rtnetlink.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
...@@ -121,7 +122,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, ...@@ -121,7 +122,7 @@ int libbpf_nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head,
} }
if (tb[type]) if (tb[type])
fprintf(stderr, "Attribute of type %#x found multiple times in message, " pr_warn("Attribute of type %#x found multiple times in message, "
"previous attribute is being ignored.\n", type); "previous attribute is being ignored.\n", type);
tb[type] = nla; tb[type] = nla;
...@@ -181,15 +182,14 @@ int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh) ...@@ -181,15 +182,14 @@ int libbpf_nla_dump_errormsg(struct nlmsghdr *nlh)
if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen, if (libbpf_nla_parse(tb, NLMSGERR_ATTR_MAX, attr, alen,
extack_policy) != 0) { extack_policy) != 0) {
fprintf(stderr, pr_warn("Failed to parse extended error attributes\n");
"Failed to parse extended error attributes\n");
return 0; return 0;
} }
if (tb[NLMSGERR_ATTR_MSG]) if (tb[NLMSGERR_ATTR_MSG])
errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]); errmsg = (char *) libbpf_nla_data(tb[NLMSGERR_ATTR_MSG]);
fprintf(stderr, "Kernel error message: %s\n", errmsg); pr_warn("Kernel error message: %s\n", errmsg);
return 0; return 0;
} }
...@@ -163,12 +163,15 @@ void test_pinning(void) ...@@ -163,12 +163,15 @@ void test_pinning(void)
goto out; goto out;
} }
/* swap pin paths of the two maps */ /* set pin paths so that nopinmap2 will attempt to reuse the map at
* pinpath (which will fail), but not before pinmap has already been
* reused
*/
bpf_object__for_each_map(map, obj) { bpf_object__for_each_map(map, obj) {
if (!strcmp(bpf_map__name(map), "nopinmap")) if (!strcmp(bpf_map__name(map), "nopinmap"))
err = bpf_map__set_pin_path(map, nopinpath2);
else if (!strcmp(bpf_map__name(map), "nopinmap2"))
err = bpf_map__set_pin_path(map, pinpath); err = bpf_map__set_pin_path(map, pinpath);
else if (!strcmp(bpf_map__name(map), "pinmap"))
err = bpf_map__set_pin_path(map, NULL);
else else
continue; continue;
...@@ -181,6 +184,17 @@ void test_pinning(void) ...@@ -181,6 +184,17 @@ void test_pinning(void)
if (CHECK(err != -EINVAL, "param mismatch load", "err %d errno %d\n", err, errno)) if (CHECK(err != -EINVAL, "param mismatch load", "err %d errno %d\n", err, errno))
goto out; goto out;
/* nopinmap2 should have been pinned and cleaned up again */
err = stat(nopinpath2, &statbuf);
if (CHECK(!err || errno != ENOENT, "stat nopinpath2",
"err %d errno %d\n", err, errno))
goto out;
/* pinmap should still be there */
err = stat(pinpath, &statbuf);
if (CHECK(err, "stat pinpath", "err %d errno %d\n", err, errno))
goto out;
bpf_object__close(obj); bpf_object__close(obj);
/* test auto-pinning at custom path with open opt */ /* test auto-pinning at custom path with open opt */
......
...@@ -21,7 +21,7 @@ struct { ...@@ -21,7 +21,7 @@ struct {
} nopinmap SEC(".maps"); } nopinmap SEC(".maps");
struct { struct {
__uint(type, BPF_MAP_TYPE_ARRAY); __uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1); __uint(max_entries, 1);
__type(key, __u32); __type(key, __u32);
__type(value, __u64); __type(value, __u64);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment