Skip to content

Commit 1eb7ed4

Browse files
committed
Fix Bindings and PerfEventArray
1 parent 8babf30 commit 1eb7ed4

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

src/bindings/main.cpp

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@ extern "C" {
66
#include <libbpf.h>
77
}
88

9-
#include "core/bpf_object.h"
10-
#include "core/bpf_program.h"
119
#include "core/bpf_exception.h"
1210
#include "core/bpf_map.h"
11+
#include "core/bpf_object.h"
12+
#include "core/bpf_program.h"
1313
#include "maps/perf_event_array.h"
14+
#include "utils/struct_parser.h"
1415

1516
namespace py = pybind11;
1617

1718
PYBIND11_MODULE(pylibbpf, m) {
18-
m.doc() = R"pbdoc(
19+
m.doc() = R"pbdoc(
1920
Pylibbpf - libbpf bindings for Python
2021
-----------------------
2122
@@ -29,56 +30,72 @@ PYBIND11_MODULE(pylibbpf, m) {
2930
BpfException
3031
)pbdoc";
3132

32-
// Register the custom exception
33-
py::register_exception<BpfException>(m, "BpfException");
33+
// Register the custom exception
34+
py::register_exception<BpfException>(m, "BpfException");
3435

35-
// BpfObject
36-
py::class_<BpfObject, std::shared_ptr<BpfObject>>(m, "BpfObject")
37-
.def(py::init<std::string>(), py::arg("object_path"))
38-
.def("load", &BpfObject::load)
39-
.def("is_loaded", &BpfObject::is_loaded)
40-
.def("get_program_names", &BpfObject::get_program_names)
41-
.def("get_program", &BpfObject::get_program, py::arg("name"))
42-
.def("attach_all", &BpfObject::attach_all)
43-
.def("get_map_names", &BpfObject::get_map_names)
44-
.def("get_map", &BpfObject::get_map, py::arg("name"));
36+
// BpfObject
37+
py::class_<BpfObject, std::shared_ptr<BpfObject>>(m, "BpfObject")
38+
.def(py::init<std::string, py::dict>(), py::arg("object_path"),
39+
py::arg("structs") = py::dict())
40+
.def("load", &BpfObject::load)
41+
.def("is_loaded", &BpfObject::is_loaded)
42+
.def("get_program_names", &BpfObject::get_program_names)
43+
.def("get_program", &BpfObject::get_program, py::arg("name"))
44+
.def("attach_all", &BpfObject::attach_all)
45+
.def("get_map_names", &BpfObject::get_map_names)
46+
.def("get_map", &BpfObject::get_map, py::arg("name"))
47+
.def("get_struct_defs", &BpfObject::get_struct_defs)
48+
.def("__getitem__", &BpfObject::get_map, py::arg("name"));
4549

46-
// BpfProgram
47-
py::class_<BpfProgram, std::shared_ptr<BpfProgram>>(m, "BpfProgram")
48-
.def("attach", &BpfProgram::attach)
49-
.def("detach", &BpfProgram::detach)
50-
.def("is_attached", &BpfProgram::is_attached)
51-
.def("get_name", &BpfProgram::get_name);
50+
// BpfProgram
51+
py::class_<BpfProgram, std::shared_ptr<BpfProgram>>(m, "BpfProgram")
52+
.def("attach", &BpfProgram::attach)
53+
.def("detach", &BpfProgram::detach)
54+
.def("is_attached", &BpfProgram::is_attached)
55+
.def("get_name", &BpfProgram::get_name);
5256

53-
// BpfMap
54-
py::class_<BpfMap, std::shared_ptr<BpfMap>>(m, "BpfMap")
55-
.def("lookup", &BpfMap::lookup, py::arg("key"))
56-
.def("update", &BpfMap::update, py::arg("key"), py::arg("value"))
57-
.def("delete_elem", &BpfMap::delete_elem, py::arg("key"))
58-
.def("get_next_key", &BpfMap::get_next_key, py::arg("key") = py::none())
59-
.def("items", &BpfMap::items)
60-
.def("keys", &BpfMap::keys)
61-
.def("values", &BpfMap::values)
62-
.def("get_name", &BpfMap::get_name)
63-
.def("get_fd", &BpfMap::get_fd)
64-
.def("get_type", &BpfMap::get_type)
65-
.def("get_key_size", &BpfMap::get_key_size)
66-
.def("get_value_size", &BpfMap::get_value_size)
67-
.def("get_max_entries", &BpfMap::get_max_entries);
57+
// BpfMap
58+
py::class_<BpfMap, std::shared_ptr<BpfMap>>(m, "BpfMap")
59+
.def("lookup", &BpfMap::lookup, py::arg("key"))
60+
.def("update", &BpfMap::update, py::arg("key"), py::arg("value"))
61+
.def("delete_elem", &BpfMap::delete_elem, py::arg("key"))
62+
.def("get_next_key", &BpfMap::get_next_key, py::arg("key") = py::none())
63+
.def("items", &BpfMap::items)
64+
.def("keys", &BpfMap::keys)
65+
.def("values", &BpfMap::values)
66+
.def("get_name", &BpfMap::get_name)
67+
.def("get_fd", &BpfMap::get_fd)
68+
.def("get_type", &BpfMap::get_type)
69+
.def("get_key_size", &BpfMap::get_key_size)
70+
.def("get_value_size", &BpfMap::get_value_size)
71+
.def("get_max_entries", &BpfMap::get_max_entries)
72+
.def("__getitem__", &BpfMap::lookup, py::arg("key"))
73+
.def("__setitem__", &BpfMap::update, py::arg("key"), py::arg("value"));
6874

69-
py::class_<PerfEventArray>(m, "PerfEventArray")
70-
.def(py::init<int, int, py::function, py::object>(),
71-
py::arg("map_fd"),
72-
py::arg("page_cnt") = 8,
73-
py::arg("callback"),
74-
py::arg("lost_callback") = py::none())
75-
.def("poll", &PerfEventArray::poll, py::arg("timeout_ms") = -1)
76-
.def("consume", &PerfEventArray::consume);
75+
// StructParser
76+
py::class_<StructParser>(m, "StructParser")
77+
.def(py::init<py::dict>(), py::arg("structs"))
78+
.def("parse", &StructParser::parse, py::arg("struct_name"),
79+
py::arg("data"))
80+
.def("has_struct", &StructParser::has_struct, py::arg("struct_name"));
7781

82+
// PerfEventArray
83+
py::class_<PerfEventArray, std::shared_ptr<PerfEventArray>>(m,
84+
"PerfEventArray")
85+
.def(py::init<std::shared_ptr<BpfMap>, int, py::function, py::object>(),
86+
py::arg("map"), py::arg("page_cnt"), py::arg("callback"),
87+
py::arg("lost_callback") = py::none())
88+
.def(py::init<std::shared_ptr<BpfMap>, int, py::function, std::string,
89+
py::object>(),
90+
py::arg("map"), py::arg("page_cnt"), py::arg("callback"),
91+
py::arg("struct_name"), py::arg("lost_callback") = py::none())
92+
.def("poll", &PerfEventArray::poll, py::arg("timeout_ms"))
93+
.def("consume", &PerfEventArray::consume)
94+
.def("get_map", &PerfEventArray::get_map);
7895

7996
#ifdef VERSION_INFO
80-
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
97+
m.attr("__version__") = MACRO_STRINGIFY(VERSION_INFO);
8198
#else
82-
m.attr("__version__") = "dev";
99+
m.attr("__version__") = "dev";
83100
#endif
84101
}

src/maps/perf_event_array.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "perf_event_array.h"
22
#include "core/bpf_exception.h"
33
#include "core/bpf_map.h"
4+
#include "core/bpf_object.h"
5+
#include "utils/struct_parser.h"
46

57
PerfEventArray::PerfEventArray(std::shared_ptr<BpfMap> map, int page_cnt,
68
py::function callback, py::object lost_callback)

0 commit comments

Comments
 (0)