aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf
diff options
context:
space:
mode:
authorJohn Fastabend <john.fastabend@gmail.com>2018-10-15 14:19:55 -0400
committerAlexei Starovoitov <ast@kernel.org>2018-10-15 19:13:14 -0400
commitc034a177d3c898f370f52877e7252da8c4f8235c (patch)
treea9e5e822219576241fdd259c52182750a4a267c9 /tools/lib/bpf
parentb7d3826c2ed6c3e626e7ae796c5df2c0d2551c6a (diff)
bpf: bpftool, add flag to allow non-compat map definitions
Multiple map definition structures exist and user may have non-zero fields in their definition that are not recognized by bpftool and libbpf. The normal behavior is to then fail loading the map. Although this is a good default behavior users may still want to load the map for debugging or other reasons. This patch adds a --mapcompat flag that can be used to override the default behavior and allow loading the map even when it has additional non-zero fields. For now the only user is 'bpftool prog' we can switch over other subcommands as needed. The library exposes an API that consumes a flags field now but I kept the original API around also in case users of the API don't want to expose this. The flags field is an int in case we need more control over how the API call handles errors/features/etc in the future. Signed-off-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Diffstat (limited to 'tools/lib/bpf')
-rw-r--r--tools/lib/bpf/bpf.h3
-rw-r--r--tools/lib/bpf/libbpf.c27
-rw-r--r--tools/lib/bpf/libbpf.h2
3 files changed, 23 insertions, 9 deletions
diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 87520a87a75f..69a4d40c4227 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -69,6 +69,9 @@ struct bpf_load_program_attr {
69 __u32 prog_ifindex; 69 __u32 prog_ifindex;
70}; 70};
71 71
72/* Flags to direct loading requirements */
73#define MAPS_RELAX_COMPAT 0x01
74
72/* Recommend log buffer size */ 75/* Recommend log buffer size */
73#define BPF_LOG_BUF_SIZE (256 * 1024) 76#define BPF_LOG_BUF_SIZE (256 * 1024)
74int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr, 77int bpf_load_program_xattr(const struct bpf_load_program_attr *load_attr,
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 176cf5523728..bd71efcc53be 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -562,8 +562,9 @@ static int compare_bpf_map(const void *_a, const void *_b)
562} 562}
563 563
564static int 564static int
565bpf_object__init_maps(struct bpf_object *obj) 565bpf_object__init_maps(struct bpf_object *obj, int flags)
566{ 566{
567 bool strict = !(flags & MAPS_RELAX_COMPAT);
567 int i, map_idx, map_def_sz, nr_maps = 0; 568 int i, map_idx, map_def_sz, nr_maps = 0;
568 Elf_Scn *scn; 569 Elf_Scn *scn;
569 Elf_Data *data; 570 Elf_Data *data;
@@ -685,7 +686,8 @@ bpf_object__init_maps(struct bpf_object *obj)
685 "has unrecognized, non-zero " 686 "has unrecognized, non-zero "
686 "options\n", 687 "options\n",
687 obj->path, map_name); 688 obj->path, map_name);
688 return -EINVAL; 689 if (strict)
690 return -EINVAL;
689 } 691 }
690 } 692 }
691 memcpy(&obj->maps[map_idx].def, def, 693 memcpy(&obj->maps[map_idx].def, def,
@@ -716,7 +718,7 @@ static bool section_have_execinstr(struct bpf_object *obj, int idx)
716 return false; 718 return false;
717} 719}
718 720
719static int bpf_object__elf_collect(struct bpf_object *obj) 721static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
720{ 722{
721 Elf *elf = obj->efile.elf; 723 Elf *elf = obj->efile.elf;
722 GElf_Ehdr *ep = &obj->efile.ehdr; 724 GElf_Ehdr *ep = &obj->efile.ehdr;
@@ -843,7 +845,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
843 return LIBBPF_ERRNO__FORMAT; 845 return LIBBPF_ERRNO__FORMAT;
844 } 846 }
845 if (obj->efile.maps_shndx >= 0) { 847 if (obj->efile.maps_shndx >= 0) {
846 err = bpf_object__init_maps(obj); 848 err = bpf_object__init_maps(obj, flags);
847 if (err) 849 if (err)
848 goto out; 850 goto out;
849 } 851 }
@@ -1515,7 +1517,7 @@ static int bpf_object__validate(struct bpf_object *obj, bool needs_kver)
1515 1517
1516static struct bpf_object * 1518static struct bpf_object *
1517__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz, 1519__bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1518 bool needs_kver) 1520 bool needs_kver, int flags)
1519{ 1521{
1520 struct bpf_object *obj; 1522 struct bpf_object *obj;
1521 int err; 1523 int err;
@@ -1531,7 +1533,7 @@ __bpf_object__open(const char *path, void *obj_buf, size_t obj_buf_sz,
1531 1533
1532 CHECK_ERR(bpf_object__elf_init(obj), err, out); 1534 CHECK_ERR(bpf_object__elf_init(obj), err, out);
1533 CHECK_ERR(bpf_object__check_endianness(obj), err, out); 1535 CHECK_ERR(bpf_object__check_endianness(obj), err, out);
1534 CHECK_ERR(bpf_object__elf_collect(obj), err, out); 1536 CHECK_ERR(bpf_object__elf_collect(obj, flags), err, out);
1535 CHECK_ERR(bpf_object__collect_reloc(obj), err, out); 1537 CHECK_ERR(bpf_object__collect_reloc(obj), err, out);
1536 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out); 1538 CHECK_ERR(bpf_object__validate(obj, needs_kver), err, out);
1537 1539
@@ -1542,7 +1544,8 @@ out:
1542 return ERR_PTR(err); 1544 return ERR_PTR(err);
1543} 1545}
1544 1546
1545struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr) 1547struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
1548 int flags)
1546{ 1549{
1547 /* param validation */ 1550 /* param validation */
1548 if (!attr->file) 1551 if (!attr->file)
@@ -1551,7 +1554,13 @@ struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
1551 pr_debug("loading %s\n", attr->file); 1554 pr_debug("loading %s\n", attr->file);
1552 1555
1553 return __bpf_object__open(attr->file, NULL, 0, 1556 return __bpf_object__open(attr->file, NULL, 0,
1554 bpf_prog_type__needs_kver(attr->prog_type)); 1557 bpf_prog_type__needs_kver(attr->prog_type),
1558 flags);
1559}
1560
1561struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr)
1562{
1563 return __bpf_object__open_xattr(attr, 0);
1555} 1564}
1556 1565
1557struct bpf_object *bpf_object__open(const char *path) 1566struct bpf_object *bpf_object__open(const char *path)
@@ -1584,7 +1593,7 @@ struct bpf_object *bpf_object__open_buffer(void *obj_buf,
1584 pr_debug("loading object '%s' from buffer\n", 1593 pr_debug("loading object '%s' from buffer\n",
1585 name); 1594 name);
1586 1595
1587 return __bpf_object__open(name, obj_buf, obj_buf_sz, true); 1596 return __bpf_object__open(name, obj_buf, obj_buf_sz, true, true);
1588} 1597}
1589 1598
1590int bpf_object__unload(struct bpf_object *obj) 1599int bpf_object__unload(struct bpf_object *obj)
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 8af8d3663991..7e9c801a9fdd 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -61,6 +61,8 @@ struct bpf_object_open_attr {
61 61
62struct bpf_object *bpf_object__open(const char *path); 62struct bpf_object *bpf_object__open(const char *path);
63struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr); 63struct bpf_object *bpf_object__open_xattr(struct bpf_object_open_attr *attr);
64struct bpf_object *__bpf_object__open_xattr(struct bpf_object_open_attr *attr,
65 int flags);
64struct bpf_object *bpf_object__open_buffer(void *obj_buf, 66struct bpf_object *bpf_object__open_buffer(void *obj_buf,
65 size_t obj_buf_sz, 67 size_t obj_buf_sz,
66 const char *name); 68 const char *name);