Skip to content

Commit

Permalink
Support expected_attach_type attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
guedou authored and yonghong-song committed May 21, 2022
1 parent 9105a1e commit ffff0ed
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ int BPF::poll_perf_buffer(const std::string& name, int timeout_ms) {
}

StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type,
int& fd, unsigned flags) {
int& fd, unsigned flags, bpf_attach_type attach_type) {
if (funcs_.find(func_name) != funcs_.end()) {
fd = funcs_[func_name];
return StatusTuple::OK();
Expand All @@ -692,7 +692,7 @@ StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type,
fd = bpf_module_->bcc_func_load(type, func_name.c_str(),
reinterpret_cast<struct bpf_insn*>(func_start), func_size,
bpf_module_->license(), bpf_module_->kern_version(),
log_level, nullptr, 0, nullptr, flags);
log_level, nullptr, 0, nullptr, flags, attach_type);

if (fd < 0)
return StatusTuple(-1, "Failed to load %s: %d", func_name.c_str(), fd);
Expand Down
2 changes: 1 addition & 1 deletion src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ class BPF {
int poll_perf_buffer(const std::string& name, int timeout_ms = -1);

StatusTuple load_func(const std::string& func_name, enum bpf_prog_type type,
int& fd, unsigned flags = 0);
int& fd, unsigned flags = 0, enum bpf_attach_type = (bpf_attach_type) __MAX_BPF_ATTACH_TYPE);
StatusTuple unload_func(const std::string& func_name);

StatusTuple attach_func(int prog_fd, int attachable_fd,
Expand Down
4 changes: 2 additions & 2 deletions src/cc/bcc_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,12 @@ int bcc_func_load(void *program, int prog_type, const char *name,
const struct bpf_insn *insns, int prog_len,
const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size,
const char *dev_name) {
const char *dev_name, int attach_type) {
auto mod = static_cast<ebpf::BPFModule *>(program);
if (!mod) return -1;
return mod->bcc_func_load(prog_type, name, insns, prog_len,
license, kern_version, log_level,
log_buf, log_buf_size, dev_name);
log_buf, log_buf_size, dev_name, 0, attach_type);

}

Expand Down
2 changes: 1 addition & 1 deletion src/cc/bcc_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ int bcc_func_load(void *program, int prog_type, const char *name,
const struct bpf_insn *insns, int prog_len,
const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size,
const char *dev_name);
const char *dev_name, int attach_type);

#ifdef __cplusplus
}
Expand Down
5 changes: 4 additions & 1 deletion src/cc/bpf_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -978,13 +978,16 @@ int BPFModule::bcc_func_load(int prog_type, const char *name,
const struct bpf_insn *insns, int prog_len,
const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size,
const char *dev_name, unsigned flags) {
const char *dev_name, unsigned flags, int attach_type) {
struct bpf_load_program_attr attr = {};
unsigned func_info_cnt, line_info_cnt, finfo_rec_size, linfo_rec_size;
void *func_info = NULL, *line_info = NULL;
int ret;

attr.prog_type = (enum bpf_prog_type)prog_type;
if (attach_type != __MAX_BPF_ATTACH_TYPE) {
attr.expected_attach_type = (enum bpf_attach_type)attach_type;
}
attr.name = name;
attr.insns = insns;
attr.license = license;
Expand Down
3 changes: 2 additions & 1 deletion src/cc/bpf_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <memory>
#include <string>
#include <vector>
#include <linux/bpf.h>

#include "bcc_exception.h"
#include "table_storage.h"
Expand Down Expand Up @@ -144,7 +145,7 @@ class BPFModule {
const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size,
const char *dev_name = nullptr,
unsigned flags = 0);
unsigned flags = 0, int attach_type = __MAX_BPF_ATTACH_TYPE);
int bcc_func_attach(int prog_fd, int attachable_fd,
int attach_type, unsigned int flags);
int bcc_func_detach(int prog_fd, int attachable_fd, int attach_type);
Expand Down
4 changes: 2 additions & 2 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def load_funcs(self, prog_type=KPROBE):

return fns

def load_func(self, func_name, prog_type, device = None):
def load_func(self, func_name, prog_type, device = None, attach_type = 38):
func_name = _assert_is_bytes(func_name)
if func_name in self.funcs:
return self.funcs[func_name]
Expand All @@ -511,7 +511,7 @@ def load_func(self, func_name, prog_type, device = None):
lib.bpf_function_size(self.module, func_name),
lib.bpf_module_license(self.module),
lib.bpf_module_kern_version(self.module),
log_level, None, 0, device)
log_level, None, 0, device, attach_type)

if fd < 0:
atexit.register(self.donothing)
Expand Down
2 changes: 1 addition & 1 deletion src/python/bcc/libbcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int]
lib.bcc_func_load.restype = ct.c_int
lib.bcc_func_load.argtypes = [ct.c_void_p, ct.c_int, ct.c_char_p, ct.c_void_p,
ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint, ct.c_char_p]
ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint, ct.c_char_p, ct.c_uint]
_RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int)
_LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_ulonglong)
lib.bpf_attach_kprobe.restype = ct.c_int
Expand Down

0 comments on commit ffff0ed

Please sign in to comment.