libbpf-ruby 0.1.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 +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/ext/libbpf_ruby/extconf.rb +10 -0
- data/ext/libbpf_ruby/libbpf_ruby.c +151 -0
- data/ext/libbpf_ruby/libbpf_ruby.h +20 -0
- data/lib/libbpf-ruby/version.rb +5 -0
- data/lib/libbpf-ruby.rb +4 -0
- data/libbpf-ruby.gemspec +23 -0
- data/tmp/aarch64-linux/stage/CHANGELOG.md +5 -0
- data/tmp/aarch64-linux/stage/CODE_OF_CONDUCT.md +10 -0
- data/tmp/aarch64-linux/stage/LICENSE.txt +21 -0
- data/tmp/aarch64-linux/stage/README.md +61 -0
- data/tmp/aarch64-linux/stage/libbpf-ruby.gemspec +23 -0
- metadata +58 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 935f86e01757713957956186f9d055ca51561a1305eb7be844d03080a5ae9719
|
|
4
|
+
data.tar.gz: ca2450392bd3164e0f50d3d80d6f2ae82b8f013dcea65363f82ee31165d2bb4c
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 14b98694511d9ef4ca3a52e856e5b577a5c5985a9ac2d789c8fb4db56cc6c1650926727007e13cd44e3371a4927d8bb5ef67499b6a2c54417008c6576f12340c
|
|
7
|
+
data.tar.gz: d6965a3d87fdefbbce469c047edee897183814bbb927e3542844a37b7a241368f62dfd8ece25b7048a43dcb0a747bb22e6aba94bdb28b2a74eb740ca34732312
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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).
|
data/LICENSE.txt
ADDED
|
@@ -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.
|
data/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# LibBPFRuby
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
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 `gem install` aborts with a message about the missing prerequisite.
|
|
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
|
+
program_fd = object.program_fd("my_program")
|
|
37
|
+
map_fd = object.map_fd("my_map")
|
|
38
|
+
|
|
39
|
+
LibBPFRuby.map_update(map_fd, [0].pack("L"), [42].pack("L"))
|
|
40
|
+
|
|
41
|
+
listening_socket = TCPServer.new(3000)
|
|
42
|
+
LibBPFRuby.attach_reuseport(listening_socket, program_fd)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
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.
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
## Contributing
|
|
52
|
+
|
|
53
|
+
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).
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
58
|
+
|
|
59
|
+
## Code of Conduct
|
|
60
|
+
|
|
61
|
+
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,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "mkmf"
|
|
4
|
+
|
|
5
|
+
abort "libbpf-ruby requires Linux" unless RUBY_PLATFORM.include?("linux")
|
|
6
|
+
abort "libbpf-ruby requires libbpf (install libbpf-dev)" unless have_library("bpf")
|
|
7
|
+
abort "libbpf-ruby requires libbpf headers (install libbpf-dev)" unless have_header("bpf/libbpf.h")
|
|
8
|
+
|
|
9
|
+
append_cflags("-fvisibility=hidden")
|
|
10
|
+
create_makefile("libbpf_ruby/libbpf_ruby")
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
#include "libbpf_ruby.h"
|
|
2
|
+
|
|
3
|
+
static int libbpf_ruby_print(enum libbpf_print_level level, const char *format, va_list args) {
|
|
4
|
+
(void)level;
|
|
5
|
+
(void)format;
|
|
6
|
+
(void)args;
|
|
7
|
+
return 0;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
typedef struct {
|
|
11
|
+
struct bpf_object *bpf_object;
|
|
12
|
+
} libbpf_ruby_object_t;
|
|
13
|
+
|
|
14
|
+
static void libbpf_ruby_object_free(void *ptr) {
|
|
15
|
+
libbpf_ruby_object_t *libbpf_ruby_object = (libbpf_ruby_object_t *)ptr;
|
|
16
|
+
if (libbpf_ruby_object->bpf_object) {
|
|
17
|
+
bpf_object__close(libbpf_ruby_object->bpf_object);
|
|
18
|
+
}
|
|
19
|
+
xfree(libbpf_ruby_object);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
static size_t libbpf_ruby_object_memsize(const void *ptr) {
|
|
23
|
+
return sizeof(libbpf_ruby_object_t);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
static const rb_data_type_t libbpf_ruby_object_type = {
|
|
27
|
+
.wrap_struct_name = "LibBPFRuby::Object",
|
|
28
|
+
.function = {
|
|
29
|
+
.dmark = NULL,
|
|
30
|
+
.dfree = libbpf_ruby_object_free,
|
|
31
|
+
.dsize = libbpf_ruby_object_memsize,
|
|
32
|
+
.dcompact = NULL
|
|
33
|
+
},
|
|
34
|
+
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
static VALUE rb_cObject_allocate(VALUE klass) {
|
|
38
|
+
libbpf_ruby_object_t *libbpf_ruby_object;
|
|
39
|
+
VALUE obj = TypedData_Make_Struct(klass, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
|
|
40
|
+
libbpf_ruby_object->bpf_object = NULL;
|
|
41
|
+
return obj;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static VALUE rb_cObject_initialize(VALUE self, VALUE path) {
|
|
45
|
+
libbpf_ruby_object_t *libbpf_ruby_object;
|
|
46
|
+
TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
|
|
47
|
+
const char *path_str = StringValueCStr(path);
|
|
48
|
+
|
|
49
|
+
struct bpf_object *bpf_object = bpf_object__open_file(path_str, NULL);
|
|
50
|
+
long err = libbpf_get_error(bpf_object);
|
|
51
|
+
if (err) {
|
|
52
|
+
rb_raise(rb_eRuntimeError, "bpf_object__open_file failed: %s", strerror(-err));
|
|
53
|
+
}
|
|
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));
|
|
58
|
+
}
|
|
59
|
+
libbpf_ruby_object->bpf_object = bpf_object;
|
|
60
|
+
return self;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static VALUE rb_cObject_program_fd(VALUE self, VALUE name) {
|
|
64
|
+
libbpf_ruby_object_t *libbpf_ruby_object;
|
|
65
|
+
TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
|
|
66
|
+
const char *name_str = StringValueCStr(name);
|
|
67
|
+
|
|
68
|
+
struct bpf_program *program = bpf_object__find_program_by_name(libbpf_ruby_object->bpf_object, name_str);
|
|
69
|
+
if (!program) {
|
|
70
|
+
rb_raise(rb_eRuntimeError, "program %s not found", name_str);
|
|
71
|
+
}
|
|
72
|
+
return INT2NUM(bpf_program__fd(program));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static VALUE rb_cObject_map_fd(VALUE self, VALUE name) {
|
|
76
|
+
libbpf_ruby_object_t *libbpf_ruby_object;
|
|
77
|
+
TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
|
|
78
|
+
const char *name_str = StringValueCStr(name);
|
|
79
|
+
|
|
80
|
+
int fd = bpf_object__find_map_fd_by_name(libbpf_ruby_object->bpf_object, name_str);
|
|
81
|
+
if (fd < 0) {
|
|
82
|
+
rb_raise(rb_eRuntimeError, "map %s not found", name_str);
|
|
83
|
+
}
|
|
84
|
+
return INT2NUM(fd);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
static VALUE rb_cObject_close(VALUE self) {
|
|
88
|
+
libbpf_ruby_object_t *libbpf_ruby_object;
|
|
89
|
+
TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
|
|
90
|
+
if (libbpf_ruby_object->bpf_object) {
|
|
91
|
+
bpf_object__close(libbpf_ruby_object->bpf_object);
|
|
92
|
+
libbpf_ruby_object->bpf_object = NULL;
|
|
93
|
+
}
|
|
94
|
+
return Qnil;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
static VALUE rb_mLibBPFRuby_map_update(VALUE self, VALUE map_fd, VALUE key, VALUE value) {
|
|
98
|
+
Check_Type(key, T_STRING);
|
|
99
|
+
Check_Type(value, T_STRING);
|
|
100
|
+
if (bpf_map_update_elem(NUM2INT(map_fd), RSTRING_PTR(key), RSTRING_PTR(value), BPF_ANY) < 0) {
|
|
101
|
+
rb_raise(rb_eRuntimeError, "bpf_map_update_elem failed: %s", strerror(errno));
|
|
102
|
+
}
|
|
103
|
+
return Qtrue;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
static VALUE rb_mLibBPFRuby_sockmap_update(VALUE self, VALUE map_fd, VALUE key, VALUE socket) {
|
|
107
|
+
Check_Type(key, T_STRING);
|
|
108
|
+
__u64 fd = (__u64)rb_io_descriptor(socket);
|
|
109
|
+
if (bpf_map_update_elem(NUM2INT(map_fd), RSTRING_PTR(key), &fd, BPF_ANY) < 0) {
|
|
110
|
+
rb_raise(rb_eRuntimeError, "bpf_map_update_elem failed: %s", strerror(errno));
|
|
111
|
+
}
|
|
112
|
+
return Qtrue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static VALUE rb_mLibBPFRuby_map_lookup(VALUE self, VALUE map_fd, VALUE key, VALUE value_size) {
|
|
116
|
+
Check_Type(key, T_STRING);
|
|
117
|
+
VALUE value = rb_str_new(NULL, NUM2LONG(value_size));
|
|
118
|
+
if (bpf_map_lookup_elem(NUM2INT(map_fd), RSTRING_PTR(key), RSTRING_PTR(value)) < 0) {
|
|
119
|
+
if (errno == ENOENT) return Qnil;
|
|
120
|
+
rb_raise(rb_eRuntimeError, "bpf_map_lookup_elem failed: %s", strerror(errno));
|
|
121
|
+
}
|
|
122
|
+
return value;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
static VALUE rb_mLibBPFRuby_attach_reuseport(VALUE self, VALUE socket, VALUE program_fd) {
|
|
126
|
+
int sock_fd = rb_io_descriptor(socket);
|
|
127
|
+
int prog_fd = NUM2INT(program_fd);
|
|
128
|
+
if (setsockopt(sock_fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_EBPF, &prog_fd, sizeof(prog_fd)) < 0) {
|
|
129
|
+
rb_raise(rb_eRuntimeError, "SO_ATTACH_REUSEPORT_EBPF failed: %s", strerror(errno));
|
|
130
|
+
}
|
|
131
|
+
return Qtrue;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
RUBY_FUNC_EXPORTED void Init_libbpf_ruby(void) {
|
|
135
|
+
rb_ext_ractor_safe(true);
|
|
136
|
+
libbpf_set_print(libbpf_ruby_print);
|
|
137
|
+
|
|
138
|
+
VALUE rb_mLibBPFRuby = rb_define_module("LibBPFRuby");
|
|
139
|
+
VALUE rb_cLibBPFRubyObject = rb_define_class_under(rb_mLibBPFRuby, "Object", rb_cObject);
|
|
140
|
+
|
|
141
|
+
rb_define_alloc_func(rb_cLibBPFRubyObject, rb_cObject_allocate);
|
|
142
|
+
rb_define_method(rb_cLibBPFRubyObject, "initialize", rb_cObject_initialize, 1);
|
|
143
|
+
rb_define_method(rb_cLibBPFRubyObject, "program_fd", rb_cObject_program_fd, 1);
|
|
144
|
+
rb_define_method(rb_cLibBPFRubyObject, "map_fd", rb_cObject_map_fd, 1);
|
|
145
|
+
rb_define_method(rb_cLibBPFRubyObject, "close", rb_cObject_close, 0);
|
|
146
|
+
|
|
147
|
+
rb_define_module_function(rb_mLibBPFRuby, "map_update", rb_mLibBPFRuby_map_update, 3);
|
|
148
|
+
rb_define_module_function(rb_mLibBPFRuby, "sockmap_update", rb_mLibBPFRuby_sockmap_update, 3);
|
|
149
|
+
rb_define_module_function(rb_mLibBPFRuby, "map_lookup", rb_mLibBPFRuby_map_lookup, 3);
|
|
150
|
+
rb_define_module_function(rb_mLibBPFRuby, "attach_reuseport", rb_mLibBPFRuby_attach_reuseport, 2);
|
|
151
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#ifndef LIBBPF_RUBY_H
|
|
2
|
+
#define LIBBPF_RUBY_H 1
|
|
3
|
+
|
|
4
|
+
#include "ruby.h"
|
|
5
|
+
#include "ruby/io.h"
|
|
6
|
+
#include "ruby/ractor.h"
|
|
7
|
+
|
|
8
|
+
#include <bpf/bpf.h>
|
|
9
|
+
#include <bpf/libbpf.h>
|
|
10
|
+
|
|
11
|
+
#include <errno.h>
|
|
12
|
+
#include <stdarg.h>
|
|
13
|
+
#include <string.h>
|
|
14
|
+
#include <sys/socket.h>
|
|
15
|
+
|
|
16
|
+
#ifndef SO_ATTACH_REUSEPORT_EBPF
|
|
17
|
+
#define SO_ATTACH_REUSEPORT_EBPF 52
|
|
18
|
+
#endif
|
|
19
|
+
|
|
20
|
+
#endif /* LIBBPF_RUBY_H */
|
data/lib/libbpf-ruby.rb
ADDED
data/libbpf-ruby.gemspec
ADDED
|
@@ -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
|
|
@@ -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,61 @@
|
|
|
1
|
+
# LibBPFRuby
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
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 `gem install` aborts with a message about the missing prerequisite.
|
|
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
|
+
program_fd = object.program_fd("my_program")
|
|
37
|
+
map_fd = object.map_fd("my_map")
|
|
38
|
+
|
|
39
|
+
LibBPFRuby.map_update(map_fd, [0].pack("L"), [42].pack("L"))
|
|
40
|
+
|
|
41
|
+
listening_socket = TCPServer.new(3000)
|
|
42
|
+
LibBPFRuby.attach_reuseport(listening_socket, program_fd)
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Development
|
|
46
|
+
|
|
47
|
+
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.
|
|
48
|
+
|
|
49
|
+
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.
|
|
50
|
+
|
|
51
|
+
## Contributing
|
|
52
|
+
|
|
53
|
+
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).
|
|
54
|
+
|
|
55
|
+
## License
|
|
56
|
+
|
|
57
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
58
|
+
|
|
59
|
+
## Code of Conduct
|
|
60
|
+
|
|
61
|
+
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
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: libbpf-ruby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Joshua Young
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
email:
|
|
13
|
+
- djry1999@gmail.com
|
|
14
|
+
executables: []
|
|
15
|
+
extensions:
|
|
16
|
+
- ext/libbpf_ruby/extconf.rb
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- CHANGELOG.md
|
|
20
|
+
- CODE_OF_CONDUCT.md
|
|
21
|
+
- LICENSE.txt
|
|
22
|
+
- README.md
|
|
23
|
+
- ext/libbpf_ruby/extconf.rb
|
|
24
|
+
- ext/libbpf_ruby/libbpf_ruby.c
|
|
25
|
+
- ext/libbpf_ruby/libbpf_ruby.h
|
|
26
|
+
- lib/libbpf-ruby.rb
|
|
27
|
+
- lib/libbpf-ruby/version.rb
|
|
28
|
+
- libbpf-ruby.gemspec
|
|
29
|
+
- tmp/aarch64-linux/stage/CHANGELOG.md
|
|
30
|
+
- tmp/aarch64-linux/stage/CODE_OF_CONDUCT.md
|
|
31
|
+
- tmp/aarch64-linux/stage/LICENSE.txt
|
|
32
|
+
- tmp/aarch64-linux/stage/README.md
|
|
33
|
+
- tmp/aarch64-linux/stage/libbpf-ruby.gemspec
|
|
34
|
+
homepage: https://github.com/joshuay03/libbpf-ruby
|
|
35
|
+
licenses:
|
|
36
|
+
- MIT
|
|
37
|
+
metadata:
|
|
38
|
+
documentation_uri: https://joshuay03.github.io/libbpf-ruby/
|
|
39
|
+
source_code_uri: https://github.com/joshuay03/libbpf-ruby
|
|
40
|
+
changelog_uri: https://github.com/joshuay03/libbpf-ruby/blob/main/CHANGELOG.md
|
|
41
|
+
rdoc_options: []
|
|
42
|
+
require_paths:
|
|
43
|
+
- lib
|
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: 3.3.0
|
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - ">="
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '0'
|
|
54
|
+
requirements: []
|
|
55
|
+
rubygems_version: 4.0.10
|
|
56
|
+
specification_version: 4
|
|
57
|
+
summary: libbpf wrapper for Ruby
|
|
58
|
+
test_files: []
|