lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <467620c966825173dbd65b37a3f9bd7dd4fb8184.1567024943.git.hex@fb.com>
Date:   Wed, 28 Aug 2019 14:03:07 -0700
From:   Julia Kartseva <hex@...com>
To:     <rdna@...com>, <bpf@...r.kernel.org>, <ast@...nel.org>,
        <daniel@...earbox.net>, <netdev@...r.kernel.org>,
        <kernel-team@...com>
CC:     Julia Kartseva <hex@...com>
Subject: [PATCH bpf-next 04/10] tools/bpf: add libbpf_prog_type_(from|to)_str helpers

Standardize string representation of prog types by putting commonly used
names to libbpf.
The prog_type to string mapping is taken from bpftool:
tools/bpf/bpftool/main.h

Signed-off-by: Julia Kartseva <hex@...com>
---
 tools/lib/bpf/libbpf.c   | 51 ++++++++++++++++++++++++++++++++++++++++
 tools/lib/bpf/libbpf.h   |  8 +++++++
 tools/lib/bpf/libbpf.map |  2 ++
 3 files changed, 61 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 72e6e5eb397f..946a4d41f223 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -296,6 +296,35 @@ struct bpf_object {
 };
 #define obj_elf_valid(o)	((o)->efile.elf)
 
+static const char *const prog_type_strs[] = {
+	[BPF_PROG_TYPE_UNSPEC] = "unspec",
+	[BPF_PROG_TYPE_SOCKET_FILTER] = "socket_filter",
+	[BPF_PROG_TYPE_KPROBE] = "kprobe",
+	[BPF_PROG_TYPE_SCHED_CLS] = "sched_cls",
+	[BPF_PROG_TYPE_SCHED_ACT] = "sched_act",
+	[BPF_PROG_TYPE_TRACEPOINT] = "tracepoint",
+	[BPF_PROG_TYPE_XDP] = "xdp",
+	[BPF_PROG_TYPE_PERF_EVENT] = "perf_event",
+	[BPF_PROG_TYPE_CGROUP_SKB] = "cgroup_skb",
+	[BPF_PROG_TYPE_CGROUP_SOCK] = "cgroup_sock",
+	[BPF_PROG_TYPE_LWT_IN] = "lwt_in",
+	[BPF_PROG_TYPE_LWT_OUT] = "lwt_out",
+	[BPF_PROG_TYPE_LWT_XMIT] = "lwt_xmit",
+	[BPF_PROG_TYPE_SOCK_OPS] = "sock_ops",
+	[BPF_PROG_TYPE_SK_SKB] = "sk_skb",
+	[BPF_PROG_TYPE_CGROUP_DEVICE] = "cgroup_device",
+	[BPF_PROG_TYPE_SK_MSG] = "sk_msg",
+	[BPF_PROG_TYPE_RAW_TRACEPOINT] = "raw_tracepoint",
+	[BPF_PROG_TYPE_CGROUP_SOCK_ADDR] = "cgroup_sock_addr",
+	[BPF_PROG_TYPE_LWT_SEG6LOCAL] = "lwt_seg6local",
+	[BPF_PROG_TYPE_LIRC_MODE2] = "lirc_mode2",
+	[BPF_PROG_TYPE_SK_REUSEPORT] = "sk_reuseport",
+	[BPF_PROG_TYPE_FLOW_DISSECTOR] = "flow_dissector",
+	[BPF_PROG_TYPE_CGROUP_SYSCTL] = "cgroup_sysctl",
+	[BPF_PROG_TYPE_RAW_TRACEPOINT_WRITABLE] = "raw_tracepoint_writable",
+	[BPF_PROG_TYPE_CGROUP_SOCKOPT] = "cgroup_sockopt",
+};
+
 void bpf_program__unload(struct bpf_program *prog)
 {
 	int i;
@@ -4632,6 +4661,28 @@ int libbpf_attach_type_by_name(const char *name,
 	return -EINVAL;
 }
 
+int libbpf_prog_type_from_str(const char *str, enum bpf_prog_type *type)
+{
+	unsigned int i;
+
+	for (i = 0; i < ARRAY_SIZE(prog_type_strs); i++)
+		if (prog_type_strs[i] && strcmp(prog_type_strs[i], str) == 0) {
+			*type = i;
+			return 0;
+		}
+
+	return -EINVAL;
+}
+
+int libbpf_prog_type_to_str(enum bpf_prog_type type, const char **str)
+{
+	if (type < BPF_PROG_TYPE_UNSPEC || type >= ARRAY_SIZE(prog_type_strs))
+		return -EINVAL;
+
+	*str = prog_type_strs[type];
+	return 0;
+}
+
 static int
 bpf_program__identify_section(struct bpf_program *prog,
 			      enum bpf_prog_type *prog_type,
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index e8f70977d137..6846c488d8a2 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -122,12 +122,20 @@ LIBBPF_API int bpf_object__set_priv(struct bpf_object *obj, void *priv,
 				    bpf_object_clear_priv_t clear_priv);
 LIBBPF_API void *bpf_object__priv(const struct bpf_object *prog);
 
+/* Program and expected attach types by section name */
 LIBBPF_API int
 libbpf_prog_type_by_name(const char *name, enum bpf_prog_type *prog_type,
 			 enum bpf_attach_type *expected_attach_type);
+/* Attach type by section name */
 LIBBPF_API int libbpf_attach_type_by_name(const char *name,
 					  enum bpf_attach_type *attach_type);
 
+/* String representation of program type */
+LIBBPF_API int libbpf_prog_type_from_str(const char *str,
+					 enum bpf_prog_type *type);
+LIBBPF_API int libbpf_prog_type_to_str(enum bpf_prog_type type,
+				       const char **str);
+
 /* Accessors of bpf_program */
 struct bpf_program;
 LIBBPF_API struct bpf_program *bpf_program__next(struct bpf_program *prog,
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index 664ce8e7a60e..2ea7c99f1579 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -188,4 +188,6 @@ LIBBPF_0.0.4 {
 LIBBPF_0.0.5 {
 	global:
 		bpf_btf_get_next_id;
+		libbpf_prog_type_from_str;
+		libbpf_prog_type_to_str;
 } LIBBPF_0.0.4;
-- 
2.17.1

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ