netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Toke Høiland-Jørgensen" <toke@redhat.com>
To: daniel@iogearbox.net, ast@fb.com
Cc: "Toke Høiland-Jørgensen" <toke@redhat.com>,
	bpf@vger.kernel.org, netdev@vger.kernel.org,
	"Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Subject: [PATCH v4 1/2] libbpf: Add setter for initial value for internal maps
Date: Sun, 29 Mar 2020 15:22:52 +0200	[thread overview]
Message-ID: <20200329132253.232541-1-toke@redhat.com> (raw)
In-Reply-To: <20200328182834.196578-1-toke@redhat.com>

For internal maps (most notably the maps backing global variables), libbpf
uses an internal mmaped area to store the data after opening the object.
This data is subsequently copied into the kernel map when the object is
loaded.

This adds a function to set a new value for that data, which can be used to
before it is loaded into the kernel. This is especially relevant for RODATA
maps, since those are frozen on load.

Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com>
---
v4:
  - Make void pointer const, check if map was loaded
  - Reject set if map was already loaded
  - Split test into its own file
v3:
  - Add a setter for the initial value instead of a getter for the pointer to it
  - Add selftest
v2:
  - Add per-map getter for data area instead of a global rodata getter for bpf_obj

 tools/lib/bpf/libbpf.c   | 11 +++++++++++
 tools/lib/bpf/libbpf.h   |  2 ++
 tools/lib/bpf/libbpf.map |  1 +
 3 files changed, 14 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 62903302935e..7deab98720ee 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -6758,6 +6758,17 @@ void *bpf_map__priv(const struct bpf_map *map)
 	return map ? map->priv : ERR_PTR(-EINVAL);
 }
 
+int bpf_map__set_initial_value(struct bpf_map *map,
+			       const void *data, size_t size)
+{
+	if (!map->mmaped || map->libbpf_type == LIBBPF_MAP_KCONFIG ||
+	    size != map->def.value_size || map->fd >= 0)
+		return -EINVAL;
+
+	memcpy(map->mmaped, data, size);
+	return 0;
+}
+
 bool bpf_map__is_offload_neutral(const struct bpf_map *map)
 {
 	return map->def.type == BPF_MAP_TYPE_PERF_EVENT_ARRAY;
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index bf7a35a9556d..958ae71c116e 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -407,6 +407,8 @@ typedef void (*bpf_map_clear_priv_t)(struct bpf_map *, void *);
 LIBBPF_API int bpf_map__set_priv(struct bpf_map *map, void *priv,
 				 bpf_map_clear_priv_t clear_priv);
 LIBBPF_API void *bpf_map__priv(const struct bpf_map *map);
+LIBBPF_API int bpf_map__set_initial_value(struct bpf_map *map,
+					  const void *data, size_t size);
 LIBBPF_API int bpf_map__reuse_fd(struct bpf_map *map, int fd);
 LIBBPF_API int bpf_map__resize(struct bpf_map *map, __u32 max_entries);
 LIBBPF_API bool bpf_map__is_offload_neutral(const struct bpf_map *map);
diff --git a/tools/lib/bpf/libbpf.map b/tools/lib/bpf/libbpf.map
index dcc87db3ca8a..159826b36b38 100644
--- a/tools/lib/bpf/libbpf.map
+++ b/tools/lib/bpf/libbpf.map
@@ -243,6 +243,7 @@ LIBBPF_0.0.8 {
 		bpf_link__pin;
 		bpf_link__pin_path;
 		bpf_link__unpin;
+		bpf_map__set_initial_value;
 		bpf_program__set_attach_target;
 		bpf_set_link_xdp_fd_opts;
 } LIBBPF_0.0.7;
-- 
2.26.0


  parent reply	other threads:[~2020-03-29 13:23 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-26 15:17 [PATCH bpf-next] libbpf: Add bpf_object__rodata getter function Toke Høiland-Jørgensen
2020-03-26 19:20 ` Andrii Nakryiko
2020-03-27 12:24   ` Toke Høiland-Jørgensen
2020-03-27 17:49     ` Andrii Nakryiko
2020-03-27 18:39       ` Toke Høiland-Jørgensen
2020-03-27 12:58 ` [PATCH bpf-next v2] libbpf: Add getter for pointer to data area for internal maps Toke Høiland-Jørgensen
2020-03-27 19:17   ` Andrii Nakryiko
2020-03-27 22:26     ` Toke Høiland-Jørgensen
2020-03-27 22:28       ` Alexei Starovoitov
2020-03-28  0:08         ` Toke Høiland-Jørgensen
2020-03-28 18:28   ` [PATCH v3 1/2] libbpf: Add setter for initial value " Toke Høiland-Jørgensen
2020-03-28 20:01     ` Andrii Nakryiko
2020-03-29 12:17       ` Toke Høiland-Jørgensen
2020-03-29 13:22     ` Toke Høiland-Jørgensen [this message]
2020-03-29 20:06       ` [PATCH v4 " Andrii Nakryiko
2020-03-29 23:46       ` Daniel Borkmann
2020-03-29 13:22     ` [PATCH v4 2/2] selftests: Add test for overriding global data value before load Toke Høiland-Jørgensen
2020-03-29 20:13       ` Andrii Nakryiko
2020-03-28 18:28   ` [PATCH v3 " Toke Høiland-Jørgensen
2020-03-28 20:06     ` Andrii Nakryiko
2020-03-29 13:10       ` Toke Høiland-Jørgensen
2020-03-29 20:08         ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200329132253.232541-1-toke@redhat.com \
    --to=toke@redhat.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).