11#include " bpf_program.h"
22#include " bpf_exception.h"
33#include < filesystem>
4+ #include < utility>
45
5- BpfProgram::BpfProgram (const std::string& object_path, const std::string& program_name)
6+ BpfProgram::BpfProgram (std::string object_path, std::string program_name)
67 : obj_(nullptr ), prog_(nullptr ), link_(nullptr ),
7- object_path_(object_path), program_name_(program_name) {
8+ object_path_(std::move( object_path)) , program_name_(std::move( program_name) ) {
89}
910
1011BpfProgram::~BpfProgram () {
@@ -28,9 +29,12 @@ bool BpfProgram::load() {
2829 throw BpfException (" Program '" + program_name_ + " ' not found in object" );
2930 }
3031 } else {
31- // Use the first program if no name specified
32- prog_ = bpf_object__next_program (obj_, nullptr );
33- if (!prog_) {
32+ while ((prog_ = bpf_object__next_program (obj_, prog_)) != nullptr ) {
33+ programs.emplace_back (prog_, nullptr );
34+ }
35+
36+ // throw if no programs found
37+ if (programs.empty ()) {
3438 throw BpfException (" No programs found in object file" );
3539 }
3640 }
@@ -44,15 +48,23 @@ bool BpfProgram::load() {
4448}
4549
4650bool BpfProgram::attach () {
47- if (!prog_) {
48- throw BpfException (" Program not loaded" );
49- }
51+ for (auto [prog, link] : programs)
52+ {
53+ if (!prog) {
54+ throw BpfException (" Program not loaded" );
55+ }
5056
51- link_ = bpf_program__attach (prog_);
52- if (libbpf_get_error (link_)) {
53- link_ = nullptr ;
54- throw BpfException (" Failed to attach BPF program" );
57+ link = bpf_program__attach (prog);
58+ if (libbpf_get_error (link)) {
59+ link = nullptr ;
60+ throw BpfException (" Failed to attach BPF program" );
61+ }
5562 }
5663
5764 return true ;
5865}
66+
67+ void BpfProgram::load_and_attach () {
68+ load ();
69+ attach ();
70+ }
0 commit comments