diff options
author | Roman Gushchin <guro@fb.com> | 2017-12-13 10:18:51 -0500 |
---|---|---|
committer | Daniel Borkmann <daniel@iogearbox.net> | 2017-12-14 07:37:13 -0500 |
commit | 583c90097f7271ab90f149b52b9ac2098bf2cbb5 (patch) | |
tree | e373de139565faec82b867167abd5c7aed7e4eac /tools/lib/bpf/libbpf.c | |
parent | f4e2298e63d24bb7f5cf0f56f72867973cb7e652 (diff) |
libbpf: add ability to guess program type based on section name
The bpf_prog_load() function will guess program type if it's not
specified explicitly. This functionality will be used to implement
loading of different programs without asking a user to specify
the program type. In first order it will be used by bpftool.
Signed-off-by: Roman Gushchin <guro@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Jakub Kicinski <jakub.kicinski@netronome.com>
Cc: Martin KaFai Lau <kafai@fb.com>
Cc: Quentin Monnet <quentin.monnet@netronome.com>
Cc: David Ahern <dsahern@gmail.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'tools/lib/bpf/libbpf.c')
-rw-r--r-- | tools/lib/bpf/libbpf.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 5aa45f89da93..205b7822fa0a 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
@@ -1721,6 +1721,45 @@ BPF_PROG_TYPE_FNS(tracepoint, BPF_PROG_TYPE_TRACEPOINT); | |||
1721 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); | 1721 | BPF_PROG_TYPE_FNS(xdp, BPF_PROG_TYPE_XDP); |
1722 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); | 1722 | BPF_PROG_TYPE_FNS(perf_event, BPF_PROG_TYPE_PERF_EVENT); |
1723 | 1723 | ||
1724 | #define BPF_PROG_SEC(string, type) { string, sizeof(string), type } | ||
1725 | static const struct { | ||
1726 | const char *sec; | ||
1727 | size_t len; | ||
1728 | enum bpf_prog_type prog_type; | ||
1729 | } section_names[] = { | ||
1730 | BPF_PROG_SEC("socket", BPF_PROG_TYPE_SOCKET_FILTER), | ||
1731 | BPF_PROG_SEC("kprobe/", BPF_PROG_TYPE_KPROBE), | ||
1732 | BPF_PROG_SEC("kretprobe/", BPF_PROG_TYPE_KPROBE), | ||
1733 | BPF_PROG_SEC("tracepoint/", BPF_PROG_TYPE_TRACEPOINT), | ||
1734 | BPF_PROG_SEC("xdp", BPF_PROG_TYPE_XDP), | ||
1735 | BPF_PROG_SEC("perf_event", BPF_PROG_TYPE_PERF_EVENT), | ||
1736 | BPF_PROG_SEC("cgroup/skb", BPF_PROG_TYPE_CGROUP_SKB), | ||
1737 | BPF_PROG_SEC("cgroup/sock", BPF_PROG_TYPE_CGROUP_SOCK), | ||
1738 | BPF_PROG_SEC("cgroup/dev", BPF_PROG_TYPE_CGROUP_DEVICE), | ||
1739 | BPF_PROG_SEC("sockops", BPF_PROG_TYPE_SOCK_OPS), | ||
1740 | BPF_PROG_SEC("sk_skb", BPF_PROG_TYPE_SK_SKB), | ||
1741 | }; | ||
1742 | #undef BPF_PROG_SEC | ||
1743 | |||
1744 | static enum bpf_prog_type bpf_program__guess_type(struct bpf_program *prog) | ||
1745 | { | ||
1746 | int i; | ||
1747 | |||
1748 | if (!prog->section_name) | ||
1749 | goto err; | ||
1750 | |||
1751 | for (i = 0; i < ARRAY_SIZE(section_names); i++) | ||
1752 | if (strncmp(prog->section_name, section_names[i].sec, | ||
1753 | section_names[i].len) == 0) | ||
1754 | return section_names[i].prog_type; | ||
1755 | |||
1756 | err: | ||
1757 | pr_warning("failed to guess program type based on section name %s\n", | ||
1758 | prog->section_name); | ||
1759 | |||
1760 | return BPF_PROG_TYPE_UNSPEC; | ||
1761 | } | ||
1762 | |||
1724 | int bpf_map__fd(struct bpf_map *map) | 1763 | int bpf_map__fd(struct bpf_map *map) |
1725 | { | 1764 | { |
1726 | return map ? map->fd : -EINVAL; | 1765 | return map ? map->fd : -EINVAL; |
@@ -1832,6 +1871,18 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, | |||
1832 | return -ENOENT; | 1871 | return -ENOENT; |
1833 | } | 1872 | } |
1834 | 1873 | ||
1874 | /* | ||
1875 | * If type is not specified, try to guess it based on | ||
1876 | * section name. | ||
1877 | */ | ||
1878 | if (type == BPF_PROG_TYPE_UNSPEC) { | ||
1879 | type = bpf_program__guess_type(prog); | ||
1880 | if (type == BPF_PROG_TYPE_UNSPEC) { | ||
1881 | bpf_object__close(obj); | ||
1882 | return -EINVAL; | ||
1883 | } | ||
1884 | } | ||
1885 | |||
1835 | bpf_program__set_type(prog, type); | 1886 | bpf_program__set_type(prog, type); |
1836 | err = bpf_object__load(obj); | 1887 | err = bpf_object__load(obj); |
1837 | if (err) { | 1888 | if (err) { |