Skip to contentSkip to conversation

bpftool: skel: Introduce NAME__open_and_load_opts()

PATCH In review Rong Tao 2 participants 3 messages Jun 5, 2025 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Rong Tao

author
#1 lore raw

PATCH 1/2 bpftool: skel: Introduce NAME__open_and_load_opts()

From: Rong Tao <[email protected]>

Introduce functions that support opts input parameters, Obviously, it is more convenient to use. for example, skel with name=tc will include the following functions:

static inline struct tc_bpf * tc_bpf__open_and_load_opts(const struct bpf_object_open_opts *opts)

---
 tools/bpf/bpftool/gen.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
tools/bpf/bpftool/gen.c +14 -3
index 67a60114368f..1487e61c6970 100644
@@ -1371,6 +1371,7 @@ static int do_skeleton(int argc, char **argv)
 		#ifdef __cplusplus					    \n\
 			static inline struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\
 			static inline struct %1$s *open_and_load();	    \n\
+			static inline struct %1$s *open_and_load_opts(const struct bpf_object_open_opts *opts = nullptr);    \n\
 			static inline int load(struct %1$s *skel);	    \n\
 			static inline int attach(struct %1$s *skel);	    \n\
 			static inline void detach(struct %1$s *skel);	    \n\
@@ -1438,12 +1439,12 @@ static int do_skeleton(int argc, char **argv)
 		}							    \n\
 									    \n\
 		static inline struct %1$s *				    \n\
-		%1$s__open_and_load(void)				    \n\
+		%1$s__open_and_load_opts(const struct bpf_object_open_opts *opts)\n\
 		{							    \n\
 			struct %1$s *obj;				    \n\
 			int err;					    \n\
 									    \n\
-			obj = %1$s__open();				    \n\
+			obj = %1$s__open_opts(opts);			    \n\
 			if (!obj)					    \n\
 				return NULL;				    \n\
 			err = %1$s__load(obj);				    \n\
@@ -1455,6 +1456,12 @@ static int do_skeleton(int argc, char **argv)
 			return obj;					    \n\
 		}							    \n\
 									    \n\
+		static inline struct %1$s *				    \n\
+		%1$s__open_and_load(void)				    \n\
+		{							    \n\
+			return %1$s__open_and_load_opts(NULL);		    \n\
+		}							    \n\
+									    \n\
 		static inline int					    \n\
 		%1$s__attach(struct %1$s *obj)				    \n\
 		{							    \n\
@@ -1530,7 +1537,11 @@ static int do_skeleton(int argc, char **argv)
 									    \n\
 		#ifdef __cplusplus					    \n\
 		struct %1$s *%1$s::open(const struct bpf_object_open_opts *opts) { return %1$s__open_opts(opts); }\n\
-		struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); }	\n\
+		struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); }\n\
+		struct %1$s *%1$s::open_and_load_opts(const struct bpf_object_open_opts *opts)\n\
+		{							    \n\
+			return %1$s__open_and_load_opts(opts);		    \n\
+		}							    \n\
 		int %1$s::load(struct %1$s *skel) { return %1$s__load(skel); }		\n\
 		int %1$s::attach(struct %1$s *skel) { return %1$s__attach(skel); }	\n\
 		void %1$s::detach(struct %1$s *skel) { %1$s__detach(skel); }		\n\
Signature · 1 line
2.49.0
#2 lore raw
Quoted 5 lines · Rong Tao

From: Rong Tao <[email protected]>

Introduce functions that support opts input parameters, Obviously, it is more convenient to use. for example, skel with name=tc will include the

No, I don't see the need to add one extra variant just to add a bit more convenience. If you need opts at open, then just do open_opts, followed by load.

If we take this convenience to its logical conclusion, we'd need to add open_and_load_and_attach() and open_and_load_and_attach_opts(). And yes, that would save a few lines of code here and there, but I don't think we should do it.

pw-bot: cr

Quoted 10 lines

following functions:

static inline struct tc_bpf * tc_bpf__open_and_load_opts(const struct bpf_object_open_opts *opts)

---
 tools/bpf/bpftool/gen.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)

[...]

author
#3 lore raw

PATCH 2/2 bpftool: skel: Introduce NAME__open_and_load_opts()

In reply to a message not in the archive

From: Rong Tao <[email protected]>

Update Documentation for skel NAME__open_and_load_opts()

---
 tools/bpf/bpftool/Documentation/bpftool-gen.rst | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tools/bpf/bpftool/Documentation/bpftool-gen.rst +7 -2
index ca860fd97d8d..4dc2a624090e 100644
@@ -107,8 +107,10 @@ bpftool gen skeleton *FILE*
       global data maps. It corresponds to libbpf's **bpf_object__load**\ ()
       API.
 
-    - **example__open_and_load** combines **example__open** and
-      **example__load** invocations in one commonly used operation.
+    - **example__open_and_load** and **example__open_and_load_opts**.
+      Combines **example__open** and **example__load** invocations in one
+      commonly used operation. **_opts** variants accepts extra
+      **bpf_object_open_opts** options.
 
     - **example__attach** and **example__detach**.
       This pair of functions allow to attach and detach, correspondingly,
@@ -336,6 +338,9 @@ files into the final BPF ELF object file *example.bpf.o*.
                 const struct bpf_object_open_opts *opts);
   static inline struct example *example__open();
   static inline int example__load(struct example *obj);
+  static inline struct example *example__open();
+  static inline struct example *example__open_and_load_opts(
+                const struct bpf_object_open_opts *opts);
   static inline struct example *example__open_and_load();
   static inline int example__attach(struct example *obj);
   static inline void example__detach(struct example *obj);
Signature · 1 line
2.49.0