libbpf-ruby 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 380f5e9be54f279c6211b57cd8401ce93c917203e5b5d6737761b93e67504f18
4
- data.tar.gz: a1e72cbcde4b31a7d9cf2dbb72fe171638f1b129347026ffc141dbff475f8f36
3
+ metadata.gz: b8277f696bfd122d3366718fa451b9213aa939ac293a435e07467f41a6f32d0b
4
+ data.tar.gz: cac0061ddd89bd4b8c87156d904d09bd722f3ad440cce5de4e3115ded32d1eee
5
5
  SHA512:
6
- metadata.gz: 5c1fc35aaf5677ccb43ad57abc45905db21b0ef1f74d69df3e3c4cde7855bfd93b6ddb59a9ddd632b21e2aabc236b8e4bd7b069b1c34c0e7293f47db37e9dcdb
7
- data.tar.gz: 9d5a11b204cc0feff9dd41d97e225ac376f669d5b604dc427b320ac203f38c98b898ace5e781c704239afbc1298566ab08ae99cbe62e08324ecb924de9a21098
6
+ metadata.gz: 2a8f61134a7cea49cd875ee5c76059889f438af05cc3ca8c202ca7a2df95148f3a4f756be39ac05b1ce7002b40c9f9ce3f3c7d237f558180a7ed780e8ffd2e45
7
+ data.tar.gz: 1bc728b63b499f5f3de64e663ad94be6e7a6692cb09886806a857fcba18caa515856d8595e4fe188eb2bbedec95d2c3e2b45192febb929d6e29c5416bfb4117b
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2026-08-09
4
+
5
+ - Release the GVL during blocking libbpf calls
6
+ - Add `Program#attach_perf_event`
7
+ - Add `Program#attach_cgroup`
8
+ - Add `Program#attach_raw_tracepoint`
9
+ - Add `Program#attach_tracepoint`
10
+ - Add `Program#attach_uprobe`
11
+ - Add `Program#attach_kprobe`
12
+ - Add `Program#attach_tcx`
13
+ - Add `Program#attach_xdp`
14
+ - Add `Program` and `Link` classes with `Program#attach`
15
+
3
16
  ## [0.1.1] - 2026-07-05
4
17
 
5
18
  - Skip native extension compilation on non-Linux platforms
data/README.md CHANGED
@@ -33,13 +33,21 @@ require "socket"
33
33
 
34
34
  object = LibBPFRuby::Object.new("my_program.bpf.o")
35
35
 
36
- program_fd = object.program_fd("my_program")
36
+ # Map updates and reuseport-based load balancing:
37
37
  map_fd = object.map_fd("my_map")
38
-
39
38
  LibBPFRuby.map_update(map_fd, [0].pack("L"), [42].pack("L"))
40
39
 
41
40
  listening_socket = TCPServer.new(3000)
42
- LibBPFRuby.attach_reuseport(listening_socket, program_fd)
41
+ LibBPFRuby.attach_reuseport(listening_socket, object.program_fd("my_reuseport_program"))
42
+
43
+ # First-class Programs and Links for every other hook:
44
+ lo_ifindex = File.read("/sys/class/net/lo/ifindex").to_i
45
+ xdp_link = object.program("my_xdp_program").attach_xdp(lo_ifindex)
46
+ kprobe_link = object.program("my_kprobe_program").attach_kprobe("do_nanosleep")
47
+ tp_link = object.program("my_tracepoint_program").attach_tracepoint("syscalls", "sys_enter_read")
48
+
49
+ # Detach explicitly, or let GC destroy the link:
50
+ xdp_link.detach
43
51
  ```
44
52
 
45
53
  ## Development
@@ -4,6 +4,7 @@ require "mkmf"
4
4
 
5
5
  if RUBY_PLATFORM.include?("linux") && have_library("bpf") && have_header("bpf/libbpf.h")
6
6
  append_cflags("-fvisibility=hidden")
7
+ have_func("bpf_program__attach_tcx", "bpf/libbpf.h")
7
8
  create_makefile("libbpf_ruby/libbpf_ruby")
8
9
  else
9
10
  File.write("Makefile", "all install clean:\n\t@true\n")
@@ -7,10 +7,28 @@ static int libbpf_ruby_print(enum libbpf_print_level level, const char *format,
7
7
  return 0;
8
8
  }
9
9
 
10
+ static VALUE rb_cLibBPFRubyProgram;
11
+ static VALUE rb_cLibBPFRubyLink;
12
+
13
+ static ID id_ivar_object;
14
+ static ID id_ivar_program;
15
+ static ID id_kwarg_retprobe;
16
+ static ID id_kwarg_pid;
17
+ static ID id_kwarg_offset;
18
+ static ID id_kwarg_func_name;
19
+
10
20
  typedef struct {
11
21
  struct bpf_object *bpf_object;
12
22
  } libbpf_ruby_object_t;
13
23
 
24
+ typedef struct {
25
+ struct bpf_program *bpf_program;
26
+ } libbpf_ruby_program_t;
27
+
28
+ typedef struct {
29
+ struct bpf_link *bpf_link;
30
+ } libbpf_ruby_link_t;
31
+
14
32
  static void libbpf_ruby_object_free(void *ptr) {
15
33
  libbpf_ruby_object_t *libbpf_ruby_object = (libbpf_ruby_object_t *)ptr;
16
34
  if (libbpf_ruby_object->bpf_object) {
@@ -23,6 +41,26 @@ static size_t libbpf_ruby_object_memsize(const void *ptr) {
23
41
  return sizeof(libbpf_ruby_object_t);
24
42
  }
25
43
 
44
+ static void libbpf_ruby_program_free(void *ptr) {
45
+ xfree(ptr);
46
+ }
47
+
48
+ static size_t libbpf_ruby_program_memsize(const void *ptr) {
49
+ return sizeof(libbpf_ruby_program_t);
50
+ }
51
+
52
+ static void libbpf_ruby_link_free(void *ptr) {
53
+ libbpf_ruby_link_t *libbpf_ruby_link = (libbpf_ruby_link_t *)ptr;
54
+ if (libbpf_ruby_link->bpf_link) {
55
+ bpf_link__destroy(libbpf_ruby_link->bpf_link);
56
+ }
57
+ xfree(libbpf_ruby_link);
58
+ }
59
+
60
+ static size_t libbpf_ruby_link_memsize(const void *ptr) {
61
+ return sizeof(libbpf_ruby_link_t);
62
+ }
63
+
26
64
  static const rb_data_type_t libbpf_ruby_object_type = {
27
65
  .wrap_struct_name = "LibBPFRuby::Object",
28
66
  .function = {
@@ -34,6 +72,184 @@ static const rb_data_type_t libbpf_ruby_object_type = {
34
72
  .flags = RUBY_TYPED_FREE_IMMEDIATELY
35
73
  };
36
74
 
75
+ static const rb_data_type_t libbpf_ruby_program_type = {
76
+ .wrap_struct_name = "LibBPFRuby::Program",
77
+ .function = {
78
+ .dmark = NULL,
79
+ .dfree = libbpf_ruby_program_free,
80
+ .dsize = libbpf_ruby_program_memsize,
81
+ .dcompact = NULL
82
+ },
83
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
84
+ };
85
+
86
+ static const rb_data_type_t libbpf_ruby_link_type = {
87
+ .wrap_struct_name = "LibBPFRuby::Link",
88
+ .function = {
89
+ .dmark = NULL,
90
+ .dfree = libbpf_ruby_link_free,
91
+ .dsize = libbpf_ruby_link_memsize,
92
+ .dcompact = NULL
93
+ },
94
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY
95
+ };
96
+
97
+ static VALUE libbpf_ruby_program_wrap(VALUE object, struct bpf_program *program) {
98
+ libbpf_ruby_program_t *libbpf_ruby_program;
99
+ VALUE obj = TypedData_Make_Struct(rb_cLibBPFRubyProgram, libbpf_ruby_program_t, &libbpf_ruby_program_type, libbpf_ruby_program);
100
+ libbpf_ruby_program->bpf_program = program;
101
+ rb_ivar_set(obj, id_ivar_object, object);
102
+ return obj;
103
+ }
104
+
105
+ static VALUE libbpf_ruby_link_wrap(VALUE program, struct bpf_link *link) {
106
+ libbpf_ruby_link_t *libbpf_ruby_link;
107
+ VALUE obj = TypedData_Make_Struct(rb_cLibBPFRubyLink, libbpf_ruby_link_t, &libbpf_ruby_link_type, libbpf_ruby_link);
108
+ libbpf_ruby_link->bpf_link = link;
109
+ rb_ivar_set(obj, id_ivar_program, program);
110
+ return obj;
111
+ }
112
+
113
+ static struct bpf_program *libbpf_ruby_program_bpf(VALUE self) {
114
+ libbpf_ruby_program_t *libbpf_ruby_program;
115
+ TypedData_Get_Struct(self, libbpf_ruby_program_t, &libbpf_ruby_program_type, libbpf_ruby_program);
116
+
117
+ libbpf_ruby_object_t *libbpf_ruby_object;
118
+ TypedData_Get_Struct(rb_ivar_get(self, id_ivar_object), libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
119
+ if (!libbpf_ruby_object->bpf_object) {
120
+ rb_raise(rb_eRuntimeError, "program's object is closed");
121
+ }
122
+ return libbpf_ruby_program->bpf_program;
123
+ }
124
+
125
+ typedef struct {
126
+ const char *path;
127
+ struct bpf_object *bpf_object;
128
+ } nogvl_open_ctx_t;
129
+
130
+ typedef struct {
131
+ struct bpf_object *bpf_object;
132
+ int err;
133
+ } nogvl_load_ctx_t;
134
+
135
+ typedef struct {
136
+ struct bpf_program *bpf_program;
137
+ struct bpf_link *bpf_link;
138
+ } nogvl_attach_ctx_t;
139
+
140
+ typedef struct {
141
+ struct bpf_program *bpf_program;
142
+ int int_arg;
143
+ struct bpf_link *bpf_link;
144
+ } nogvl_attach_int_ctx_t;
145
+
146
+ typedef struct {
147
+ struct bpf_program *bpf_program;
148
+ bool retprobe;
149
+ const char *func_name;
150
+ struct bpf_link *bpf_link;
151
+ } nogvl_attach_kprobe_ctx_t;
152
+
153
+ typedef struct {
154
+ struct bpf_program *bpf_program;
155
+ pid_t pid;
156
+ const char *binary_path;
157
+ size_t offset;
158
+ const struct bpf_uprobe_opts *opts;
159
+ struct bpf_link *bpf_link;
160
+ } nogvl_attach_uprobe_ctx_t;
161
+
162
+ typedef struct {
163
+ struct bpf_program *bpf_program;
164
+ const char *category;
165
+ const char *name;
166
+ struct bpf_link *bpf_link;
167
+ } nogvl_attach_tracepoint_ctx_t;
168
+
169
+ typedef struct {
170
+ struct bpf_program *bpf_program;
171
+ const char *name;
172
+ struct bpf_link *bpf_link;
173
+ } nogvl_attach_raw_tracepoint_ctx_t;
174
+
175
+ static void *nogvl_bpf_object__open_file(void *data) {
176
+ nogvl_open_ctx_t *ctx = data;
177
+ ctx->bpf_object = bpf_object__open_file(ctx->path, NULL);
178
+ return NULL;
179
+ }
180
+
181
+ static void *nogvl_bpf_object__load(void *data) {
182
+ nogvl_load_ctx_t *ctx = data;
183
+ ctx->err = bpf_object__load(ctx->bpf_object);
184
+ return NULL;
185
+ }
186
+
187
+ static void *nogvl_bpf_object__close(void *data) {
188
+ bpf_object__close(data);
189
+ return NULL;
190
+ }
191
+
192
+ static void *nogvl_bpf_link__destroy(void *data) {
193
+ bpf_link__destroy(data);
194
+ return NULL;
195
+ }
196
+
197
+ static void *nogvl_bpf_program__attach(void *data) {
198
+ nogvl_attach_ctx_t *ctx = data;
199
+ ctx->bpf_link = bpf_program__attach(ctx->bpf_program);
200
+ return NULL;
201
+ }
202
+
203
+ static void *nogvl_bpf_program__attach_xdp(void *data) {
204
+ nogvl_attach_int_ctx_t *ctx = data;
205
+ ctx->bpf_link = bpf_program__attach_xdp(ctx->bpf_program, ctx->int_arg);
206
+ return NULL;
207
+ }
208
+
209
+ #ifdef HAVE_BPF_PROGRAM__ATTACH_TCX
210
+ static void *nogvl_bpf_program__attach_tcx(void *data) {
211
+ nogvl_attach_int_ctx_t *ctx = data;
212
+ ctx->bpf_link = bpf_program__attach_tcx(ctx->bpf_program, ctx->int_arg, NULL);
213
+ return NULL;
214
+ }
215
+ #endif
216
+
217
+ static void *nogvl_bpf_program__attach_kprobe(void *data) {
218
+ nogvl_attach_kprobe_ctx_t *ctx = data;
219
+ ctx->bpf_link = bpf_program__attach_kprobe(ctx->bpf_program, ctx->retprobe, ctx->func_name);
220
+ return NULL;
221
+ }
222
+
223
+ static void *nogvl_bpf_program__attach_uprobe_opts(void *data) {
224
+ nogvl_attach_uprobe_ctx_t *ctx = data;
225
+ ctx->bpf_link = bpf_program__attach_uprobe_opts(ctx->bpf_program, ctx->pid, ctx->binary_path, ctx->offset, ctx->opts);
226
+ return NULL;
227
+ }
228
+
229
+ static void *nogvl_bpf_program__attach_tracepoint(void *data) {
230
+ nogvl_attach_tracepoint_ctx_t *ctx = data;
231
+ ctx->bpf_link = bpf_program__attach_tracepoint(ctx->bpf_program, ctx->category, ctx->name);
232
+ return NULL;
233
+ }
234
+
235
+ static void *nogvl_bpf_program__attach_raw_tracepoint(void *data) {
236
+ nogvl_attach_raw_tracepoint_ctx_t *ctx = data;
237
+ ctx->bpf_link = bpf_program__attach_raw_tracepoint(ctx->bpf_program, ctx->name);
238
+ return NULL;
239
+ }
240
+
241
+ static void *nogvl_bpf_program__attach_cgroup(void *data) {
242
+ nogvl_attach_int_ctx_t *ctx = data;
243
+ ctx->bpf_link = bpf_program__attach_cgroup(ctx->bpf_program, ctx->int_arg);
244
+ return NULL;
245
+ }
246
+
247
+ static void *nogvl_bpf_program__attach_perf_event(void *data) {
248
+ nogvl_attach_int_ctx_t *ctx = data;
249
+ ctx->bpf_link = bpf_program__attach_perf_event(ctx->bpf_program, ctx->int_arg);
250
+ return NULL;
251
+ }
252
+
37
253
  static VALUE rb_cObject_allocate(VALUE klass) {
38
254
  libbpf_ruby_object_t *libbpf_ruby_object;
39
255
  VALUE obj = TypedData_Make_Struct(klass, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
@@ -44,22 +260,38 @@ static VALUE rb_cObject_allocate(VALUE klass) {
44
260
  static VALUE rb_cObject_initialize(VALUE self, VALUE path) {
45
261
  libbpf_ruby_object_t *libbpf_ruby_object;
46
262
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
47
- const char *path_str = StringValueCStr(path);
263
+ StringValueCStr(path);
264
+ VALUE frozen_path = rb_str_new_frozen(path);
48
265
 
49
- struct bpf_object *bpf_object = bpf_object__open_file(path_str, NULL);
50
- long err = libbpf_get_error(bpf_object);
266
+ nogvl_open_ctx_t open_ctx = { .path = RSTRING_PTR(frozen_path) };
267
+ rb_nogvl(nogvl_bpf_object__open_file, &open_ctx, NULL, NULL, 0);
268
+ long err = libbpf_get_error(open_ctx.bpf_object);
51
269
  if (err) {
52
270
  rb_raise(rb_eRuntimeError, "bpf_object__open_file failed: %s", strerror(-err));
53
271
  }
54
- err = bpf_object__load(bpf_object);
55
- if (err) {
56
- bpf_object__close(bpf_object);
57
- rb_raise(rb_eRuntimeError, "bpf_object__load failed: %s", strerror(-err));
272
+
273
+ nogvl_load_ctx_t load_ctx = { .bpf_object = open_ctx.bpf_object };
274
+ rb_nogvl(nogvl_bpf_object__load, &load_ctx, NULL, NULL, 0);
275
+ if (load_ctx.err) {
276
+ rb_nogvl(nogvl_bpf_object__close, open_ctx.bpf_object, NULL, NULL, 0);
277
+ rb_raise(rb_eRuntimeError, "bpf_object__load failed: %s", strerror(-load_ctx.err));
58
278
  }
59
- libbpf_ruby_object->bpf_object = bpf_object;
279
+ libbpf_ruby_object->bpf_object = open_ctx.bpf_object;
60
280
  return self;
61
281
  }
62
282
 
283
+ static VALUE rb_cObject_program(VALUE self, VALUE name) {
284
+ libbpf_ruby_object_t *libbpf_ruby_object;
285
+ TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
286
+ const char *name_str = StringValueCStr(name);
287
+
288
+ struct bpf_program *program = bpf_object__find_program_by_name(libbpf_ruby_object->bpf_object, name_str);
289
+ if (!program) {
290
+ rb_raise(rb_eRuntimeError, "program %s not found", name_str);
291
+ }
292
+ return libbpf_ruby_program_wrap(self, program);
293
+ }
294
+
63
295
  static VALUE rb_cObject_program_fd(VALUE self, VALUE name) {
64
296
  libbpf_ruby_object_t *libbpf_ruby_object;
65
297
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
@@ -88,12 +320,199 @@ static VALUE rb_cObject_close(VALUE self) {
88
320
  libbpf_ruby_object_t *libbpf_ruby_object;
89
321
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
90
322
  if (libbpf_ruby_object->bpf_object) {
91
- bpf_object__close(libbpf_ruby_object->bpf_object);
323
+ rb_nogvl(nogvl_bpf_object__close, libbpf_ruby_object->bpf_object, NULL, NULL, 0);
92
324
  libbpf_ruby_object->bpf_object = NULL;
93
325
  }
94
326
  return Qnil;
95
327
  }
96
328
 
329
+ static VALUE rb_cProgram_fd(VALUE self) {
330
+ return INT2NUM(bpf_program__fd(libbpf_ruby_program_bpf(self)));
331
+ }
332
+
333
+ static VALUE rb_cProgram_name(VALUE self) {
334
+ return rb_str_new_cstr(bpf_program__name(libbpf_ruby_program_bpf(self)));
335
+ }
336
+
337
+ static VALUE rb_cProgram_attach(VALUE self) {
338
+ nogvl_attach_ctx_t ctx = { .bpf_program = libbpf_ruby_program_bpf(self) };
339
+ rb_nogvl(nogvl_bpf_program__attach, &ctx, NULL, NULL, 0);
340
+ long err = libbpf_get_error(ctx.bpf_link);
341
+ if (err) {
342
+ rb_raise(rb_eRuntimeError, "bpf_program__attach failed: %s", strerror(-err));
343
+ }
344
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
345
+ }
346
+
347
+ static VALUE rb_cProgram_attach_xdp(VALUE self, VALUE ifindex) {
348
+ nogvl_attach_int_ctx_t ctx = { .bpf_program = libbpf_ruby_program_bpf(self), .int_arg = NUM2INT(ifindex) };
349
+ rb_nogvl(nogvl_bpf_program__attach_xdp, &ctx, NULL, NULL, 0);
350
+ long err = libbpf_get_error(ctx.bpf_link);
351
+ if (err) {
352
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_xdp failed: %s", strerror(-err));
353
+ }
354
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
355
+ }
356
+
357
+ #ifdef HAVE_BPF_PROGRAM__ATTACH_TCX
358
+ static VALUE rb_cProgram_attach_tcx(VALUE self, VALUE ifindex) {
359
+ nogvl_attach_int_ctx_t ctx = { .bpf_program = libbpf_ruby_program_bpf(self), .int_arg = NUM2INT(ifindex) };
360
+ rb_nogvl(nogvl_bpf_program__attach_tcx, &ctx, NULL, NULL, 0);
361
+ long err = libbpf_get_error(ctx.bpf_link);
362
+ if (err) {
363
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_tcx failed: %s", strerror(-err));
364
+ }
365
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
366
+ }
367
+ #endif
368
+
369
+ static VALUE rb_cProgram_attach_kprobe(int argc, VALUE *argv, VALUE self) {
370
+ VALUE func_name, kwargs;
371
+ rb_scan_args(argc, argv, "1:", &func_name, &kwargs);
372
+ bool retprobe = false;
373
+ if (!NIL_P(kwargs)) {
374
+ VALUE value = rb_hash_lookup2(kwargs, ID2SYM(id_kwarg_retprobe), Qundef);
375
+ if (value != Qundef) retprobe = RTEST(value);
376
+ }
377
+ StringValueCStr(func_name);
378
+ VALUE frozen_func_name = rb_str_new_frozen(func_name);
379
+
380
+ nogvl_attach_kprobe_ctx_t ctx = {
381
+ .bpf_program = libbpf_ruby_program_bpf(self),
382
+ .retprobe = retprobe,
383
+ .func_name = RSTRING_PTR(frozen_func_name)
384
+ };
385
+ rb_nogvl(nogvl_bpf_program__attach_kprobe, &ctx, NULL, NULL, 0);
386
+ long err = libbpf_get_error(ctx.bpf_link);
387
+ if (err) {
388
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_kprobe failed: %s", strerror(-err));
389
+ }
390
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
391
+ }
392
+
393
+ static VALUE rb_cProgram_attach_uprobe(int argc, VALUE *argv, VALUE self) {
394
+ VALUE binary_path, kwargs;
395
+ rb_scan_args(argc, argv, "1:", &binary_path, &kwargs);
396
+ VALUE frozen_func_name = Qnil;
397
+ size_t offset = 0;
398
+ pid_t pid = -1;
399
+ bool retprobe = false;
400
+ if (!NIL_P(kwargs)) {
401
+ VALUE value;
402
+ value = rb_hash_lookup2(kwargs, ID2SYM(id_kwarg_func_name), Qundef);
403
+ if (value != Qundef) {
404
+ StringValueCStr(value);
405
+ frozen_func_name = rb_str_new_frozen(value);
406
+ }
407
+ value = rb_hash_lookup2(kwargs, ID2SYM(id_kwarg_offset), Qundef);
408
+ if (value != Qundef) offset = NUM2SIZET(value);
409
+ value = rb_hash_lookup2(kwargs, ID2SYM(id_kwarg_pid), Qundef);
410
+ if (value != Qundef) pid = NUM2INT(value);
411
+ value = rb_hash_lookup2(kwargs, ID2SYM(id_kwarg_retprobe), Qundef);
412
+ if (value != Qundef) retprobe = RTEST(value);
413
+ }
414
+ StringValueCStr(binary_path);
415
+ VALUE frozen_binary_path = rb_str_new_frozen(binary_path);
416
+
417
+ LIBBPF_OPTS(bpf_uprobe_opts, uopts,
418
+ .retprobe = retprobe,
419
+ .func_name = NIL_P(frozen_func_name) ? NULL : RSTRING_PTR(frozen_func_name)
420
+ );
421
+ nogvl_attach_uprobe_ctx_t ctx = {
422
+ .bpf_program = libbpf_ruby_program_bpf(self),
423
+ .pid = pid,
424
+ .binary_path = RSTRING_PTR(frozen_binary_path),
425
+ .offset = offset,
426
+ .opts = &uopts
427
+ };
428
+ rb_nogvl(nogvl_bpf_program__attach_uprobe_opts, &ctx, NULL, NULL, 0);
429
+ long err = libbpf_get_error(ctx.bpf_link);
430
+ if (err) {
431
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_uprobe_opts failed: %s", strerror(-err));
432
+ }
433
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
434
+ }
435
+
436
+ static VALUE rb_cProgram_attach_tracepoint(VALUE self, VALUE category, VALUE name) {
437
+ StringValueCStr(category);
438
+ StringValueCStr(name);
439
+ VALUE frozen_category = rb_str_new_frozen(category);
440
+ VALUE frozen_name = rb_str_new_frozen(name);
441
+
442
+ nogvl_attach_tracepoint_ctx_t ctx = {
443
+ .bpf_program = libbpf_ruby_program_bpf(self),
444
+ .category = RSTRING_PTR(frozen_category),
445
+ .name = RSTRING_PTR(frozen_name)
446
+ };
447
+ rb_nogvl(nogvl_bpf_program__attach_tracepoint, &ctx, NULL, NULL, 0);
448
+ long err = libbpf_get_error(ctx.bpf_link);
449
+ if (err) {
450
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_tracepoint failed: %s", strerror(-err));
451
+ }
452
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
453
+ }
454
+
455
+ static VALUE rb_cProgram_attach_raw_tracepoint(VALUE self, VALUE name) {
456
+ StringValueCStr(name);
457
+ VALUE frozen_name = rb_str_new_frozen(name);
458
+
459
+ nogvl_attach_raw_tracepoint_ctx_t ctx = {
460
+ .bpf_program = libbpf_ruby_program_bpf(self),
461
+ .name = RSTRING_PTR(frozen_name)
462
+ };
463
+ rb_nogvl(nogvl_bpf_program__attach_raw_tracepoint, &ctx, NULL, NULL, 0);
464
+ long err = libbpf_get_error(ctx.bpf_link);
465
+ if (err) {
466
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_raw_tracepoint failed: %s", strerror(-err));
467
+ }
468
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
469
+ }
470
+
471
+ static VALUE rb_cProgram_attach_cgroup(VALUE self, VALUE cgroup) {
472
+ nogvl_attach_int_ctx_t ctx = { .bpf_program = libbpf_ruby_program_bpf(self), .int_arg = rb_io_descriptor(cgroup) };
473
+ rb_nogvl(nogvl_bpf_program__attach_cgroup, &ctx, NULL, NULL, 0);
474
+ long err = libbpf_get_error(ctx.bpf_link);
475
+ if (err) {
476
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_cgroup failed: %s", strerror(-err));
477
+ }
478
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
479
+ }
480
+
481
+ static VALUE rb_cProgram_attach_perf_event(VALUE self, VALUE perf_event) {
482
+ struct bpf_program *program = libbpf_ruby_program_bpf(self);
483
+ int perf_fd = dup(rb_io_descriptor(perf_event));
484
+ if (perf_fd < 0) {
485
+ rb_raise(rb_eRuntimeError, "dup failed: %s", strerror(errno));
486
+ }
487
+ nogvl_attach_int_ctx_t ctx = { .bpf_program = program, .int_arg = perf_fd };
488
+ rb_nogvl(nogvl_bpf_program__attach_perf_event, &ctx, NULL, NULL, 0);
489
+ long err = libbpf_get_error(ctx.bpf_link);
490
+ if (err) {
491
+ close(perf_fd);
492
+ rb_raise(rb_eRuntimeError, "bpf_program__attach_perf_event failed: %s", strerror(-err));
493
+ }
494
+ return libbpf_ruby_link_wrap(self, ctx.bpf_link);
495
+ }
496
+
497
+ static VALUE rb_cLink_fd(VALUE self) {
498
+ libbpf_ruby_link_t *libbpf_ruby_link;
499
+ TypedData_Get_Struct(self, libbpf_ruby_link_t, &libbpf_ruby_link_type, libbpf_ruby_link);
500
+ if (!libbpf_ruby_link->bpf_link) {
501
+ rb_raise(rb_eRuntimeError, "link is detached");
502
+ }
503
+ return INT2NUM(bpf_link__fd(libbpf_ruby_link->bpf_link));
504
+ }
505
+
506
+ static VALUE rb_cLink_detach(VALUE self) {
507
+ libbpf_ruby_link_t *libbpf_ruby_link;
508
+ TypedData_Get_Struct(self, libbpf_ruby_link_t, &libbpf_ruby_link_type, libbpf_ruby_link);
509
+ if (libbpf_ruby_link->bpf_link) {
510
+ rb_nogvl(nogvl_bpf_link__destroy, libbpf_ruby_link->bpf_link, NULL, NULL, 0);
511
+ libbpf_ruby_link->bpf_link = NULL;
512
+ }
513
+ return Qnil;
514
+ }
515
+
97
516
  static VALUE rb_mLibBPFRuby_map_update(VALUE self, VALUE map_fd, VALUE key, VALUE value) {
98
517
  Check_Type(key, T_STRING);
99
518
  Check_Type(value, T_STRING);
@@ -135,15 +554,44 @@ RUBY_FUNC_EXPORTED void Init_libbpf_ruby(void) {
135
554
  rb_ext_ractor_safe(true);
136
555
  libbpf_set_print(libbpf_ruby_print);
137
556
 
557
+ id_ivar_object = rb_intern("@object");
558
+ id_ivar_program = rb_intern("@program");
559
+ id_kwarg_retprobe = rb_intern("retprobe");
560
+ id_kwarg_pid = rb_intern("pid");
561
+ id_kwarg_offset = rb_intern("offset");
562
+ id_kwarg_func_name = rb_intern("func_name");
563
+
138
564
  VALUE rb_mLibBPFRuby = rb_define_module("LibBPFRuby");
139
565
  VALUE rb_cLibBPFRubyObject = rb_define_class_under(rb_mLibBPFRuby, "Object", rb_cObject);
566
+ rb_cLibBPFRubyProgram = rb_define_class_under(rb_mLibBPFRuby, "Program", rb_cObject);
567
+ rb_cLibBPFRubyLink = rb_define_class_under(rb_mLibBPFRuby, "Link", rb_cObject);
140
568
 
141
569
  rb_define_alloc_func(rb_cLibBPFRubyObject, rb_cObject_allocate);
142
570
  rb_define_method(rb_cLibBPFRubyObject, "initialize", rb_cObject_initialize, 1);
571
+ rb_define_method(rb_cLibBPFRubyObject, "program", rb_cObject_program, 1);
143
572
  rb_define_method(rb_cLibBPFRubyObject, "program_fd", rb_cObject_program_fd, 1);
144
573
  rb_define_method(rb_cLibBPFRubyObject, "map_fd", rb_cObject_map_fd, 1);
145
574
  rb_define_method(rb_cLibBPFRubyObject, "close", rb_cObject_close, 0);
146
575
 
576
+ rb_undef_alloc_func(rb_cLibBPFRubyProgram);
577
+ rb_define_method(rb_cLibBPFRubyProgram, "fd", rb_cProgram_fd, 0);
578
+ rb_define_method(rb_cLibBPFRubyProgram, "name", rb_cProgram_name, 0);
579
+ rb_define_method(rb_cLibBPFRubyProgram, "attach", rb_cProgram_attach, 0);
580
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_xdp", rb_cProgram_attach_xdp, 1);
581
+ #ifdef HAVE_BPF_PROGRAM__ATTACH_TCX
582
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_tcx", rb_cProgram_attach_tcx, 1);
583
+ #endif
584
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_kprobe", rb_cProgram_attach_kprobe, -1);
585
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_uprobe", rb_cProgram_attach_uprobe, -1);
586
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_tracepoint", rb_cProgram_attach_tracepoint, 2);
587
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_raw_tracepoint", rb_cProgram_attach_raw_tracepoint, 1);
588
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_cgroup", rb_cProgram_attach_cgroup, 1);
589
+ rb_define_method(rb_cLibBPFRubyProgram, "attach_perf_event", rb_cProgram_attach_perf_event, 1);
590
+
591
+ rb_undef_alloc_func(rb_cLibBPFRubyLink);
592
+ rb_define_method(rb_cLibBPFRubyLink, "fd", rb_cLink_fd, 0);
593
+ rb_define_method(rb_cLibBPFRubyLink, "detach", rb_cLink_detach, 0);
594
+
147
595
  rb_define_module_function(rb_mLibBPFRuby, "map_update", rb_mLibBPFRuby_map_update, 3);
148
596
  rb_define_module_function(rb_mLibBPFRuby, "sockmap_update", rb_mLibBPFRuby_sockmap_update, 3);
149
597
  rb_define_module_function(rb_mLibBPFRuby, "map_lookup", rb_mLibBPFRuby_map_lookup, 3);
@@ -3,6 +3,7 @@
3
3
 
4
4
  #include "ruby.h"
5
5
  #include "ruby/io.h"
6
+ #include "ruby/thread.h"
6
7
  #include "ruby/ractor.h"
7
8
 
8
9
  #include <bpf/bpf.h>
@@ -12,6 +13,7 @@
12
13
  #include <stdarg.h>
13
14
  #include <string.h>
14
15
  #include <sys/socket.h>
16
+ #include <unistd.h>
15
17
 
16
18
  #ifndef SO_ATTACH_REUSEPORT_EBPF
17
19
  #define SO_ATTACH_REUSEPORT_EBPF 52
@@ -1,5 +1,6 @@
1
+ # rbs_inline: enabled
1
2
  # frozen_string_literal: true
2
3
 
3
4
  module LibBPFRuby
4
- VERSION = "0.1.1"
5
+ VERSION = "0.2.0"
5
6
  end
data/lib/libbpf-ruby.rb CHANGED
@@ -1,3 +1,4 @@
1
+ # rbs_inline: enabled
1
2
  # frozen_string_literal: true
2
3
 
3
4
  require "libbpf_ruby/libbpf_ruby"
@@ -1,5 +1,22 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2026-08-09
4
+
5
+ - Release the GVL during blocking libbpf calls
6
+ - Add `Program#attach_perf_event`
7
+ - Add `Program#attach_cgroup`
8
+ - Add `Program#attach_raw_tracepoint`
9
+ - Add `Program#attach_tracepoint`
10
+ - Add `Program#attach_uprobe`
11
+ - Add `Program#attach_kprobe`
12
+ - Add `Program#attach_tcx`
13
+ - Add `Program#attach_xdp`
14
+ - Add `Program` and `Link` classes with `Program#attach`
15
+
16
+ ## [0.1.1] - 2026-07-05
17
+
18
+ - Skip native extension compilation on non-Linux platforms
19
+
3
20
  ## [0.1.0] - 2026-07-05
4
21
 
5
22
  - Initial release
@@ -21,7 +21,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
21
21
  gem install libbpf-ruby
22
22
  ```
23
23
 
24
- The native extension links against [`libbpf`](https://github.com/libbpf/libbpf), which is Linux-only. On unsupported platforms `gem install` aborts with a message about the missing prerequisite.
24
+ The native extension links against [`libbpf`](https://github.com/libbpf/libbpf), which is Linux-only. On unsupported platforms the gem installs without compiling the extension; requiring it at runtime raises `LoadError`.
25
25
 
26
26
  ## Usage
27
27
 
@@ -33,13 +33,21 @@ require "socket"
33
33
 
34
34
  object = LibBPFRuby::Object.new("my_program.bpf.o")
35
35
 
36
- program_fd = object.program_fd("my_program")
36
+ # Map updates and reuseport-based load balancing:
37
37
  map_fd = object.map_fd("my_map")
38
-
39
38
  LibBPFRuby.map_update(map_fd, [0].pack("L"), [42].pack("L"))
40
39
 
41
40
  listening_socket = TCPServer.new(3000)
42
- LibBPFRuby.attach_reuseport(listening_socket, program_fd)
41
+ LibBPFRuby.attach_reuseport(listening_socket, object.program_fd("my_reuseport_program"))
42
+
43
+ # First-class Programs and Links for every other hook:
44
+ lo_ifindex = File.read("/sys/class/net/lo/ifindex").to_i
45
+ xdp_link = object.program("my_xdp_program").attach_xdp(lo_ifindex)
46
+ kprobe_link = object.program("my_kprobe_program").attach_kprobe("do_nanosleep")
47
+ tp_link = object.program("my_tracepoint_program").attach_tracepoint("syscalls", "sys_enter_read")
48
+
49
+ # Detach explicitly, or let GC destroy the link:
50
+ xdp_link.detach
43
51
  ```
44
52
 
45
53
  ## Development
@@ -0,0 +1,22 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.2.0] - 2026-08-09
4
+
5
+ - Release the GVL during blocking libbpf calls
6
+ - Add `Program#attach_perf_event`
7
+ - Add `Program#attach_cgroup`
8
+ - Add `Program#attach_raw_tracepoint`
9
+ - Add `Program#attach_tracepoint`
10
+ - Add `Program#attach_uprobe`
11
+ - Add `Program#attach_kprobe`
12
+ - Add `Program#attach_tcx`
13
+ - Add `Program#attach_xdp`
14
+ - Add `Program` and `Link` classes with `Program#attach`
15
+
16
+ ## [0.1.1] - 2026-07-05
17
+
18
+ - Skip native extension compilation on non-Linux platforms
19
+
20
+ ## [0.1.0] - 2026-07-05
21
+
22
+ - Initial release
@@ -0,0 +1,10 @@
1
+ # Code of Conduct
2
+
3
+ "libbpf-ruby" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
4
+
5
+ * Participants will be tolerant of opposing views.
6
+ * Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
7
+ * When interpreting the words and actions of others, participants should always assume good intentions.
8
+ * Behaviour which can be reasonably considered harassment will not be tolerated.
9
+
10
+ If you have any concerns about behaviour within this project, please contact us at [djry1999@gmail.com](mailto:djry1999@gmail.com).
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Joshua Young
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,69 @@
1
+ # LibBPFRuby
2
+
3
+ ![Version](https://img.shields.io/gem/v/libbpf-ruby)
4
+ ![Build](https://badge.buildkite.com/c7ce68e58e1f481f8b62f452756532c75983ec5aeafb151dd3.svg)
5
+
6
+ [`libbpf`](https://github.com/libbpf/libbpf) wrapper for Ruby.
7
+
8
+ **Docs:** https://joshuay03.github.io/libbpf-ruby
9
+
10
+ ## Installation
11
+
12
+ Install the gem and add to the application's Gemfile by executing:
13
+
14
+ ```bash
15
+ bundle add libbpf-ruby
16
+ ```
17
+
18
+ If bundler is not being used to manage dependencies, install the gem by executing:
19
+
20
+ ```bash
21
+ gem install libbpf-ruby
22
+ ```
23
+
24
+ The native extension links against [`libbpf`](https://github.com/libbpf/libbpf), which is Linux-only. On unsupported platforms the gem installs without compiling the extension; requiring it at runtime raises `LoadError`.
25
+
26
+ ## Usage
27
+
28
+ ```ruby
29
+ # frozen_string_literal: true
30
+
31
+ require "libbpf-ruby"
32
+ require "socket"
33
+
34
+ object = LibBPFRuby::Object.new("my_program.bpf.o")
35
+
36
+ # Map updates and reuseport-based load balancing:
37
+ map_fd = object.map_fd("my_map")
38
+ LibBPFRuby.map_update(map_fd, [0].pack("L"), [42].pack("L"))
39
+
40
+ listening_socket = TCPServer.new(3000)
41
+ LibBPFRuby.attach_reuseport(listening_socket, object.program_fd("my_reuseport_program"))
42
+
43
+ # First-class Programs and Links for every other hook:
44
+ lo_ifindex = File.read("/sys/class/net/lo/ifindex").to_i
45
+ xdp_link = object.program("my_xdp_program").attach_xdp(lo_ifindex)
46
+ kprobe_link = object.program("my_kprobe_program").attach_kprobe("do_nanosleep")
47
+ tp_link = object.program("my_tracepoint_program").attach_tracepoint("syscalls", "sys_enter_read")
48
+
49
+ # Detach explicitly, or let GC destroy the link:
50
+ xdp_link.detach
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to compile native extensions and run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ If you're on macOS (or any non-Linux host), `bin/dev` builds and drops you into a Docker image with `libbpf-dev`, `clang`, and Ruby preinstalled, mounting the repo at `/workspace`. Run `bin/dev` for an interactive shell, or `bin/dev bundle exec rake` to run the full build and test cycle in one shot.
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/joshuay03/libbpf-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/joshuay03/libbpf-ruby/blob/main/CODE_OF_CONDUCT.md).
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
66
+
67
+ ## Code of Conduct
68
+
69
+ Everyone interacting in the LibBPFRuby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/joshuay03/libbpf-ruby/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/libbpf-ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "libbpf-ruby"
7
+ spec.version = LibBPFRuby::VERSION
8
+ spec.authors = ["Joshua Young"]
9
+ spec.email = ["djry1999@gmail.com"]
10
+
11
+ spec.summary = "libbpf wrapper for Ruby"
12
+ spec.homepage = "https://github.com/joshuay03/libbpf-ruby"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 3.3.0"
15
+
16
+ spec.metadata["documentation_uri"] = "https://joshuay03.github.io/libbpf-ruby/"
17
+ spec.metadata["source_code_uri"] = spec.homepage
18
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
19
+
20
+ spec.files = Dir["lib/**/*.rb", "ext/**/*.{rb,c,h}", "**/*.{gemspec,md,txt}"]
21
+ spec.require_paths = ["lib"]
22
+ spec.extensions = ["ext/libbpf_ruby/extconf.rb"]
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libbpf-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Young
@@ -31,6 +31,11 @@ files:
31
31
  - tmp/aarch64-linux/stage/LICENSE.txt
32
32
  - tmp/aarch64-linux/stage/README.md
33
33
  - tmp/aarch64-linux/stage/libbpf-ruby.gemspec
34
+ - tmp/aarch64-linux/stage/tmp/aarch64-linux/stage/CHANGELOG.md
35
+ - tmp/aarch64-linux/stage/tmp/aarch64-linux/stage/CODE_OF_CONDUCT.md
36
+ - tmp/aarch64-linux/stage/tmp/aarch64-linux/stage/LICENSE.txt
37
+ - tmp/aarch64-linux/stage/tmp/aarch64-linux/stage/README.md
38
+ - tmp/aarch64-linux/stage/tmp/aarch64-linux/stage/libbpf-ruby.gemspec
34
39
  homepage: https://github.com/joshuay03/libbpf-ruby
35
40
  licenses:
36
41
  - MIT
@@ -52,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
57
  - !ruby/object:Gem::Version
53
58
  version: '0'
54
59
  requirements: []
55
- rubygems_version: 4.0.10
60
+ rubygems_version: 4.0.16
56
61
  specification_version: 4
57
62
  summary: libbpf wrapper for Ruby
58
63
  test_files: []