Skip to content

Commit 528a542

Browse files
committed
Create bpf_object.h as a container for the object file having bpf progs and maps
1 parent 8d27a35 commit 528a542

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

src/core/bpf_object.h

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#ifndef PYLIBBPF_BPF_OBJECT_H
2+
#define PYLIBBPF_BPF_OBJECT_H
3+
4+
#include <libbpf.h>
5+
#include <pybind11/pybind11.h>
6+
#include <memory>
7+
#include <string>
8+
#include <unordered_map>
9+
#include <vector>
10+
11+
namespace py = pybind11;
12+
13+
class BpfProgram;
14+
class BpfMap;
15+
16+
/**
17+
* BpfObject - Represents a loaded BPF object file.
18+
*
19+
* This is the main entry point for loading BPF programs.
20+
* Owns the bpf_object* and manages all programs and maps within it.
21+
*/
22+
class BpfObject {
23+
private:
24+
struct bpf_object *obj_;
25+
std::string object_path_;
26+
bool loaded_;
27+
28+
mutable std::unordered_map<std::string, std::shared_ptr<BpfMap>> maps_cache_;
29+
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>> prog_cache_;
30+
31+
public:
32+
explicit BpfObject(std::string object_path);
33+
~BpfObject();
34+
35+
// Disable copy, allow move
36+
BpfObject(const BpfObject&) = delete;
37+
BpfObject& operator=(const BpfObject&) = delete;
38+
BpfObject(BpfObject&&) noexcept;
39+
BpfObject& operator=(BpfObject&&) noexcept;
40+
41+
/**
42+
* Load the BPF object into the kernel.
43+
* Must be called before accessing programs or maps.
44+
*/
45+
void load();
46+
47+
/**
48+
* Check if object is loaded.
49+
*/
50+
[[nodiscard]] bool is_loaded() const { return loaded_; }
51+
52+
/**
53+
* Get the underlying bpf_object pointer.
54+
* Only for internal use by BpfProgram and BpfMap.
55+
*/
56+
[[nodiscard]] struct bpf_object* get_obj() const { return obj_; }
57+
58+
/**
59+
* Attach all programs in the object.
60+
*/
61+
py::dict attach_all();
62+
63+
// Program access
64+
[[nodiscard]] py::list get_program_names() const;
65+
[[nodiscard]] std::shared_ptr<BpfProgram> get_program(const std::string& name);
66+
[[nodiscard]] struct bpf_program* find_program_by_name(const std::string& name) const;
67+
[[nodiscard]] py::dict get_programs() const;
68+
69+
// Map access
70+
[[nodiscard]] py::list get_map_names() const;
71+
[[nodiscard]] std::shared_ptr<BpfMap> get_map(const std::string& name);
72+
[[nodiscard]] struct bpf_map* find_map_by_name(const std::string& name) const;
73+
[[nodiscard]] py::dict get_maps() const;
74+
};
75+
76+
#endif // PYLIBBPF_BPF_OBJECT_H

0 commit comments

Comments
 (0)