BPF List
 help / color / mirror / Atom feed
From: Andrey Ignatov <rdna@fb.com>
To: <bpf@vger.kernel.org>
Cc: Andrey Ignatov <rdna@fb.com>, <ast@kernel.org>,
	<daniel@iogearbox.net>, <kafai@fb.com>, <andriin@fb.com>,
	<kernel-team@fb.com>
Subject: [PATCH v2 bpf-next 4/6] libbpf: Make DECLARE_LIBBPF_OPTS available in bpf.h
Date: Thu, 12 Dec 2019 15:30:51 -0800	[thread overview]
Message-ID: <db7f94fd1735a28b6729a7f908c1a219af046dec.1576193131.git.rdna@fb.com> (raw)
In-Reply-To: <cover.1576193131.git.rdna@fb.com>

DECLARE_LIBBPF_OPTS is the way to use option structures in a backward
compatible and extensible way. It's available only in libbpf.h.  Though
public interfaces in bpf.h have the same requirement  to accept options
in a way that can be simply extended w/o introducing new xattr-functions
every time.

libbpf.c depends on bpf.h, hence to share the macros a couple of options
exist:
* either a common header should be introduced;
* or the macro can be moved to bpf.h and used by libbpf.h from there;

The former seems to be an overkill, so do the latter:
* move DECLARE_LIBBPF_OPTS from libbpf.h to bpf.h;
* move `#include "bpf.h"` from libbpf.c to libbpf.h not to break users
  of the macro who already include only libbpf.h.

That makes the macro available to use in bpf.{h,c} and should not break
those who already use it.

Signed-off-by: Andrey Ignatov <rdna@fb.com>
---
 tools/lib/bpf/bpf.h    | 22 ++++++++++++++++++++++
 tools/lib/bpf/libbpf.c |  1 -
 tools/lib/bpf/libbpf.h | 24 ++----------------------
 3 files changed, 24 insertions(+), 23 deletions(-)

diff --git a/tools/lib/bpf/bpf.h b/tools/lib/bpf/bpf.h
index 3c791fa8e68e..5cfe6e0a1aef 100644
--- a/tools/lib/bpf/bpf.h
+++ b/tools/lib/bpf/bpf.h
@@ -36,6 +36,28 @@ extern "C" {
 #define LIBBPF_API __attribute__((visibility("default")))
 #endif
 
+/* Helper macro to declare and initialize libbpf options struct
+ *
+ * This dance with uninitialized declaration, followed by memset to zero,
+ * followed by assignment using compound literal syntax is done to preserve
+ * ability to use a nice struct field initialization syntax and **hopefully**
+ * have all the padding bytes initialized to zero. It's not guaranteed though,
+ * when copying literal, that compiler won't copy garbage in literal's padding
+ * bytes, but that's the best way I've found and it seems to work in practice.
+ *
+ * Macro declares opts struct of given type and name, zero-initializes,
+ * including any extra padding, it with memset() and then assigns initial
+ * values provided by users in struct initializer-syntax as varargs.
+ */
+#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)				    \
+	struct TYPE NAME = ({						    \
+		memset(&NAME, 0, sizeof(struct TYPE));			    \
+		(struct TYPE) {						    \
+			.sz = sizeof(struct TYPE),			    \
+			__VA_ARGS__					    \
+		};							    \
+	})
+
 struct bpf_create_map_attr {
 	const char *name;
 	enum bpf_map_type map_type;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 920d4e06a5f9..9d788c1b9874 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -46,7 +46,6 @@
 #include <gelf.h>
 
 #include "libbpf.h"
-#include "bpf.h"
 #include "btf.h"
 #include "str_error.h"
 #include "libbpf_internal.h"
diff --git a/tools/lib/bpf/libbpf.h b/tools/lib/bpf/libbpf.h
index 0dbf4bfba0c4..03189880d99e 100644
--- a/tools/lib/bpf/libbpf.h
+++ b/tools/lib/bpf/libbpf.h
@@ -17,6 +17,8 @@
 #include <sys/types.h>  // for size_t
 #include <linux/bpf.h>
 
+#include "bpf.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -67,28 +69,6 @@ struct bpf_object_open_attr {
 	enum bpf_prog_type prog_type;
 };
 
-/* Helper macro to declare and initialize libbpf options struct
- *
- * This dance with uninitialized declaration, followed by memset to zero,
- * followed by assignment using compound literal syntax is done to preserve
- * ability to use a nice struct field initialization syntax and **hopefully**
- * have all the padding bytes initialized to zero. It's not guaranteed though,
- * when copying literal, that compiler won't copy garbage in literal's padding
- * bytes, but that's the best way I've found and it seems to work in practice.
- *
- * Macro declares opts struct of given type and name, zero-initializes,
- * including any extra padding, it with memset() and then assigns initial
- * values provided by users in struct initializer-syntax as varargs.
- */
-#define DECLARE_LIBBPF_OPTS(TYPE, NAME, ...)				    \
-	struct TYPE NAME = ({ 						    \
-		memset(&NAME, 0, sizeof(struct TYPE));			    \
-		(struct TYPE) {						    \
-			.sz = sizeof(struct TYPE),			    \
-			__VA_ARGS__					    \
-		};							    \
-	})
-
 struct bpf_object_open_opts {
 	/* size of this struct, for forward/backward compatiblity */
 	size_t sz;
-- 
2.17.1


  parent reply	other threads:[~2019-12-12 23:31 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-12 23:30 [PATCH v2 bpf-next 0/6] bpf: Support replacing cgroup-bpf program in MULTI mode Andrey Ignatov
2019-12-12 23:30 ` [PATCH v2 bpf-next 1/6] bpf: Simplify __cgroup_bpf_attach Andrey Ignatov
2019-12-12 23:30 ` [PATCH v2 bpf-next 2/6] bpf: Remove unused new_flags in hierarchy_allows_attach() Andrey Ignatov
2019-12-12 23:30 ` [PATCH v2 bpf-next 3/6] bpf: Support replacing cgroup-bpf program in MULTI mode Andrey Ignatov
2019-12-12 23:30 ` Andrey Ignatov [this message]
2019-12-13  6:53   ` [PATCH v2 bpf-next 4/6] libbpf: Make DECLARE_LIBBPF_OPTS available in bpf.h Andrii Nakryiko
2019-12-12 23:30 ` [PATCH v2 bpf-next 5/6] libbpf: Introduce bpf_prog_attach_xattr Andrey Ignatov
2019-12-13  6:58   ` Andrii Nakryiko
2019-12-13 17:58     ` Andrey Ignatov
2019-12-13 20:42       ` Andrii Nakryiko
2019-12-12 23:30 ` [PATCH v2 bpf-next 6/6] selftests/bpf: Cover BPF_F_REPLACE in test_cgroup_attach Andrey Ignatov
2019-12-13  7:01   ` Andrii Nakryiko
2019-12-18 16:57     ` Andrey Ignatov
2019-12-18 17:24       ` Andrii Nakryiko
2019-12-18 17:37         ` Andrey Ignatov
2019-12-13  5:39 ` [PATCH v2 bpf-next 0/6] bpf: Support replacing cgroup-bpf program in MULTI mode Alexei Starovoitov

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=db7f94fd1735a28b6729a7f908c1a219af046dec.1576193131.git.rdna@fb.com \
    --to=rdna@fb.com \
    --cc=andriin@fb.com \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=kernel-team@fb.com \
    /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