From 528a54247d70ecc4d77bac2294eb3c31057fa722 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Fri, 17 Oct 2025 18:12:26 +0530 Subject: [PATCH] Create bpf_object.h as a container for the object file having bpf progs and maps --- src/core/bpf_object.h | 76 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/core/bpf_object.h diff --git a/src/core/bpf_object.h b/src/core/bpf_object.h new file mode 100644 index 0000000..a774bf0 --- /dev/null +++ b/src/core/bpf_object.h @@ -0,0 +1,76 @@ +#ifndef PYLIBBPF_BPF_OBJECT_H +#define PYLIBBPF_BPF_OBJECT_H + +#include +#include +#include +#include +#include +#include + +namespace py = pybind11; + +class BpfProgram; +class BpfMap; + +/** + * BpfObject - Represents a loaded BPF object file. + * + * This is the main entry point for loading BPF programs. + * Owns the bpf_object* and manages all programs and maps within it. + */ +class BpfObject { +private: + struct bpf_object *obj_; + std::string object_path_; + bool loaded_; + + mutable std::unordered_map> maps_cache_; + mutable std::unordered_map> prog_cache_; + +public: + explicit BpfObject(std::string object_path); + ~BpfObject(); + + // Disable copy, allow move + BpfObject(const BpfObject&) = delete; + BpfObject& operator=(const BpfObject&) = delete; + BpfObject(BpfObject&&) noexcept; + BpfObject& operator=(BpfObject&&) noexcept; + + /** + * Load the BPF object into the kernel. + * Must be called before accessing programs or maps. + */ + void load(); + + /** + * Check if object is loaded. + */ + [[nodiscard]] bool is_loaded() const { return loaded_; } + + /** + * Get the underlying bpf_object pointer. + * Only for internal use by BpfProgram and BpfMap. + */ + [[nodiscard]] struct bpf_object* get_obj() const { return obj_; } + + /** + * Attach all programs in the object. + */ + py::dict attach_all(); + + // Program access + [[nodiscard]] py::list get_program_names() const; + [[nodiscard]] std::shared_ptr get_program(const std::string& name); + [[nodiscard]] struct bpf_program* find_program_by_name(const std::string& name) const; + [[nodiscard]] py::dict get_programs() const; + + // Map access + [[nodiscard]] py::list get_map_names() const; + [[nodiscard]] std::shared_ptr get_map(const std::string& name); + [[nodiscard]] struct bpf_map* find_map_by_name(const std::string& name) const; + [[nodiscard]] py::dict get_maps() const; +}; + +#endif // PYLIBBPF_BPF_OBJECT_H