Skip to content

Commit 17387dd

Browse files
Jakub Kicinskiborkmann
authored andcommitted
tools: bpf: don't complain about no kernel version for networking code
BPF programs only have to specify the target kernel version for tracing related hooks, in networking world that requirement does not really apply. Loosen the checks in libbpf to reflect that. bpf_object__open() users will continue to see the error for backward compatibility (and because prog_type is not available there). Error code for NULL file name is changed from ENOENT to EINVAL, as it seems more appropriate, hopefully, that's an OK change. Signed-off-by: Jakub Kicinski <jakub.kicinski@netronome.com> Reviewed-by: Quentin Monnet <quentin.monnet@netronome.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 2eb57bb commit 17387dd

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,9 +1438,37 @@ bpf_object__load_progs(struct bpf_object *obj)
14381438
return 0;
14391439
}
14401440

1441-
static int bpf_object__validate(struct bpf_object *obj)
1441+
static bool bpf_prog_type__needs_kver(enum bpf_prog_type type)
1442+
{
1443+
switch (type) {
1444+
case BPF_PROG_TYPE_SOCKET_FILTER:
1445+
case BPF_PROG_TYPE_SCHED_CLS:
1446+
case BPF_PROG_TYPE_SCHED_ACT:
1447+
case BPF_PROG_TYPE_XDP:
1448+
case BPF_PROG_TYPE_CGROUP_SKB:
1449+
case BPF_PROG_TYPE_CGROUP_SOCK:
1450+
case BPF_PROG_TYPE_LWT_IN:
1451+
case BPF_PROG_TYPE_LWT_OUT:
1452+
case BPF_PROG_TYPE_LWT_XMIT:
1453+
case BPF_PROG_TYPE_SOCK_OPS:
1454+
case BPF_PROG_TYPE_SK_SKB:
1455+
case BPF_PROG_TYPE_CGROUP_DEVICE:
1456+
case BPF_PROG_TYPE_SK_MSG:
1457+
case BPF_PROG_TYPE_CGROUP_SOCK_ADDR:
1458+
return false;
1459+
case BPF_PROG_TYPE_UNSPEC:
1460+
case BPF_PROG_TYPE_KPROBE:
1461+
case BPF_PROG_TYPE_TRACEPOINT:
1462+
case BPF_PROG_TYPE_PERF_EVENT:
1463+
case BPF_PROG_TYPE_RAW_TRACEPOINT:
1464+
default:
1465+
return true;
1466+
}
1467+
}
1468+
1469+
static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
14421470
{
1443-
if (obj->kern_version == 0) {
1471+
if (needs_kver && obj->kern_version == 0) {
14441472
pr_warning("%s doesn't provide kernel version\n",
14451473
obj->path);
14461474
return -LIBBPF_ERRNO__KVERSION;
@@ -1449,7 +1477,8 @@ static int bpf_object__validate(struct bpf_object *obj)
14491477
}
14501478

14511479
static struct bpf_object *
1452-
__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
1480+
__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1481+
bool needs_kver)
14531482
{
14541483
struct bpf_object *obj;
14551484
int err;
@@ -1467,7 +1496,7 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz)
14671496
CHECK_ERR(bpf_object__check_endianness(obj), err, out);
14681497
CHECK_ERR(bpf_object__elf_collect(obj), err, out);
14691498
CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1470-
CHECK_ERR(bpf_object__validate(obj), err, out);
1499+
CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
14711500

14721501
bpf_object__elf_finish(obj);
14731502
return obj;
@@ -1484,7 +1513,7 @@ struct bpf_object *bpf_object__open(const char *path)
14841513

14851514
pr_debug("loading %s\n", path);
14861515

1487-
return __bpf_object__open(path, NULL, 0);
1516+
return __bpf_object__open(path, NULL, 0, true);
14881517
}
14891518

14901519
struct bpf_object *bpf_object__open_buffer(void *obj_buf,
@@ -1507,7 +1536,7 @@ struct bpf_object *bpf_object__open_buffer(void *obj_buf,
15071536
pr_debug("loading object '%s' from buffer\n",
15081537
name);
15091538

1510-
return __bpf_object__open(name, obj_buf, obj_buf_sz);
1539+
return __bpf_object__open(name, obj_buf, obj_buf_sz, true);
15111540
}
15121541

15131542
int bpf_object__unload(struct bpf_object *obj)
@@ -2164,8 +2193,11 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
21642193

21652194
if (!attr)
21662195
return -EINVAL;
2196+
if (!attr->file)
2197+
return -EINVAL;
21672198

2168-
obj = bpf_object__open(attr->file);
2199+
obj = __bpf_object__open(attr->file, NULL, 0,
2200+
bpf_prog_type__needs_kver(attr->prog_type));
21692201
if (IS_ERR(obj))
21702202
return -ENOENT;
21712203

0 commit comments

Comments
 (0)