mirror of
https://github.com/varun-r-mallya/pylibbpf.git
synced 2025-12-31 20:36:26 +00:00
Create bpf_object.h as a container for the object file having bpf progs and maps
This commit is contained in:
76
src/core/bpf_object.h
Normal file
76
src/core/bpf_object.h
Normal file
@ -0,0 +1,76 @@
|
||||
#ifndef PYLIBBPF_BPF_OBJECT_H
|
||||
#define PYLIBBPF_BPF_OBJECT_H
|
||||
|
||||
#include <libbpf.h>
|
||||
#include <pybind11/pybind11.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
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<std::string, std::shared_ptr<BpfMap>> maps_cache_;
|
||||
mutable std::unordered_map<std::string, std::shared_ptr<BpfProgram>> 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<BpfProgram> 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<BpfMap> 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
|
||||
Reference in New Issue
Block a user