[DTrace-devel] [PATCH 4/5] Implement dt_bpf_map_lookup_inner() and dt_bpf_map_update_inner()

Kris Van Hees kris.van.hees at oracle.com
Fri Aug 19 17:26:10 UTC 2022


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

diff --git a/libdtrace/dt_bpf.c b/libdtrace/dt_bpf.c
index 7a886278..66c76022 100644
--- a/libdtrace/dt_bpf.c
+++ b/libdtrace/dt_bpf.c
@@ -164,6 +164,20 @@ dt_bpf_map_create_meta(enum bpf_map_type otype, const char *name,
 	return ofd;
 }
 
+/*
+ * Get a file descriptor for a BPF map based on it id.
+ */
+int
+dt_bpf_map_get_fd_by_id(uint32_t id)
+{
+	union bpf_attr	attr;
+
+	memset(&attr, 0, sizeof(attr));
+	attr.map_id = id;
+
+	return bpf(BPF_MAP_GET_FD_BY_ID, &attr);
+}
+
 /*
  * Load the value for the given key in the map referenced by the given fd.
  */
@@ -228,6 +242,51 @@ dt_bpf_map_update(int fd, const void *key, const void *val)
 	return bpf(BPF_MAP_UPDATE_ELEM, &attr);
 }
 
+/*
+ * Retrieve the value in a map-of-maps, i.e. map[key1][key2] = value.
+ */
+int
+dt_bpf_map_lookup_inner(int fd, const void *key1, const void *key2, void *val)
+{
+	uint32_t	id;
+	int		rc;
+
+	if (dt_bpf_map_lookup(fd, key1, &id) < 0)
+		return -1;
+
+	fd = dt_bpf_map_get_fd_by_id(id);
+	if (fd < 0)
+		return -1;
+
+	rc = dt_bpf_map_lookup(fd, key2, val);
+	close(fd);
+
+	return rc;
+}
+
+/*
+ * Store the value in a map-of-maps, i.e. map[key1][key2] = value.
+ */
+int
+dt_bpf_map_update_inner(int fd, const void *key1, const void *key2,
+			const void *val)
+{
+	uint32_t	id;
+	int		rc;
+
+	if (dt_bpf_map_lookup(fd, key1, &id) < 0)
+		return -1;
+
+	fd = dt_bpf_map_get_fd_by_id(id);
+	if (fd < 0)
+		return -1;
+
+	rc = dt_bpf_map_update(fd, key2, val);
+	close(fd);
+
+	return rc;
+}
+
 static int
 have_helper(uint32_t func_id)
 {
diff --git a/libdtrace/dt_bpf.h b/libdtrace/dt_bpf.h
index f8c75012..7a16c03d 100644
--- a/libdtrace/dt_bpf.h
+++ b/libdtrace/dt_bpf.h
@@ -50,6 +50,11 @@ extern int dt_bpf_map_lookup(int fd, const void *key, void *val);
 extern int dt_bpf_map_next_key(int fd, const void *key, void *nxt);
 extern int dt_bpf_map_update(int fd, const void *key, const void *val);
 extern int dt_bpf_map_delete(int fd, const void *key);
+extern int dt_bpf_map_get_fd_by_id(uint32_t id);
+extern int dt_bpf_map_lookup_inner(int fd, const void *key1, const void *key2,
+				   void *val);
+extern int dt_bpf_map_update_inner(int fd, const void *key1, const void *key2,
+				   const void *val);
 extern int dt_bpf_load_progs(struct dtrace_hdl *, uint_t);
 extern void dt_bpf_init_helpers(struct dtrace_hdl *dtp);
 
-- 
2.34.1




More information about the DTrace-devel mailing list