[DTrace-devel] [PATCH] Add dt_bpf_map_lookup() function to retrieve values from BPF maps

Kris Van Hees kris.van.hees at oracle.com
Thu Aug 27 11:54:34 PDT 2020


Signed-off-by: Kris Van Hees <kris.van.hees at oracle.com>
---
 libdtrace/dt_bpf.c | 15 +++++++++++++++
 libdtrace/dt_bpf.h |  1 +
 2 files changed, 16 insertions(+)

diff --git a/libdtrace/dt_bpf.c b/libdtrace/dt_bpf.c
index d6d05dc8..0531a37e 100644
--- a/libdtrace/dt_bpf.c
+++ b/libdtrace/dt_bpf.c
@@ -185,6 +185,21 @@ dt_bpf_gmap_create(dtrace_hdl_t *dtp)
 	return 0;
 }
 
+/*
+ * Load the value for the given key in the map referenced by the given fd.
+ */
+int dt_bpf_map_lookup(int fd, const void *key, void *val)
+{
+	union bpf_attr attr;
+
+	memset(&attr, 0, sizeof(attr));
+	attr.map_fd = fd;
+	attr.key = (uint64_t)(unsigned long)key;
+	attr.value = (uint64_t)(unsigned long)val;
+
+	return bpf(BPF_MAP_LOOKUP_ELEM, &attr);
+}
+
 /*
  * Store the (key, value) pair in the map referenced by the given fd.
  */
diff --git a/libdtrace/dt_bpf.h b/libdtrace/dt_bpf.h
index c4a4a99c..e87d4653 100644
--- a/libdtrace/dt_bpf.h
+++ b/libdtrace/dt_bpf.h
@@ -23,6 +23,7 @@ extern int perf_event_open(struct perf_event_attr *attr, pid_t pid, int cpu,
 extern int bpf(enum bpf_cmd cmd, union bpf_attr *attr);
 
 extern int dt_bpf_gmap_create(dtrace_hdl_t *);
+extern int dt_bpf_map_lookup(int fd, const void *key, void *val);
 extern int dt_bpf_map_update(int fd, const void *key, const void *val);
 extern int dt_bpf_load_progs(dtrace_hdl_t *, uint_t);
 
-- 
2.28.0




More information about the DTrace-devel mailing list