Class: LibBPFRuby::Object

Inherits:
Object
  • Object
show all
Defined in:
ext/libbpf_ruby/libbpf_ruby.c

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'ext/libbpf_ruby/libbpf_ruby.c', line 44

static VALUE rb_cObject_initialize(VALUE self, VALUE path) {
  libbpf_ruby_object_t *libbpf_ruby_object;
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
  const char *path_str = StringValueCStr(path);

  struct bpf_object *bpf_object = bpf_object__open_file(path_str, NULL);
  long err = libbpf_get_error(bpf_object);
  if (err) {
    rb_raise(rb_eRuntimeError, "bpf_object__open_file failed: %s", strerror(-err));
  }
  err = bpf_object__load(bpf_object);
  if (err) {
    bpf_object__close(bpf_object);
    rb_raise(rb_eRuntimeError, "bpf_object__load failed: %s", strerror(-err));
  }
  libbpf_ruby_object->bpf_object = bpf_object;
  return self;
}

Instance Method Details

#closeObject



87
88
89
90
91
92
93
94
95
# File 'ext/libbpf_ruby/libbpf_ruby.c', line 87

static VALUE rb_cObject_close(VALUE self) {
  libbpf_ruby_object_t *libbpf_ruby_object;
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
  if (libbpf_ruby_object->bpf_object) {
    bpf_object__close(libbpf_ruby_object->bpf_object);
    libbpf_ruby_object->bpf_object = NULL;
  }
  return Qnil;
}

#map_fd(name) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'ext/libbpf_ruby/libbpf_ruby.c', line 75

static VALUE rb_cObject_map_fd(VALUE self, VALUE name) {
  libbpf_ruby_object_t *libbpf_ruby_object;
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
  const char *name_str = StringValueCStr(name);

  int fd = bpf_object__find_map_fd_by_name(libbpf_ruby_object->bpf_object, name_str);
  if (fd < 0) {
    rb_raise(rb_eRuntimeError, "map %s not found", name_str);
  }
  return INT2NUM(fd);
}

#program_fd(name) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'ext/libbpf_ruby/libbpf_ruby.c', line 63

static VALUE rb_cObject_program_fd(VALUE self, VALUE name) {
  libbpf_ruby_object_t *libbpf_ruby_object;
  TypedData_Get_Struct(self, libbpf_ruby_object_t, &libbpf_ruby_object_type, libbpf_ruby_object);
  const char *name_str = StringValueCStr(name);

  struct bpf_program *program = bpf_object__find_program_by_name(libbpf_ruby_object->bpf_object, name_str);
  if (!program) {
    rb_raise(rb_eRuntimeError, "program %s not found", name_str);
  }
  return INT2NUM(bpf_program__fd(program));
}