Commit eea55286 authored by Teng Qin's avatar Teng Qin

Use bpf_prog_load_flag in APIs

parent 797669f4
......@@ -483,9 +483,8 @@ StatusTuple BPF::load_func(const std::string& func_name,
fd = bpf_prog_load(type, func_name.c_str(),
reinterpret_cast<struct bpf_insn*>(func_start),
func_size, bpf_module_->license(),
bpf_module_->kern_version(), nullptr,
0 // BPFModule will handle error printing
);
bpf_module_->kern_version(),
flag_ & DEBUG_BPF ? 1 : 0, nullptr, 0);
if (fd < 0)
return StatusTuple(-1, "Failed to load %s: %d", func_name.c_str(), fd);
......
......@@ -46,7 +46,7 @@ public:
static const int BPF_MAX_STACK_DEPTH = 127;
explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr)
: bpf_module_(new BPFModule(flag, ts)) {}
: flag_(flag), bpf_module_(new BPFModule(flag, ts)) {}
StatusTuple init(const std::string& bpf_program,
const std::vector<std::string>& cflags = {},
const std::vector<USDT>& usdt = {});
......@@ -185,6 +185,8 @@ private:
std::string &module_res,
uint64_t &offset_res);
int flag_;
std::unique_ptr<BPFModule> bpf_module_;
std::map<std::string, int> funcs_;
......
......@@ -160,7 +160,8 @@ function Bpf:load_func(fn_name, prog_type)
libbcc.bpf_function_start(self.module, fn_name),
libbcc.bpf_function_size(self.module, fn_name),
libbcc.bpf_module_license(self.module),
libbcc.bpf_module_kern_version(self.module), nil, 0)
libbcc.bpf_module_kern_version(self.module),
0, nil, 0)
assert(fd >= 0, "failed to load BPF program "..fn_name)
log.info("loaded %s (%d)", fn_name, fd)
......
......@@ -32,7 +32,8 @@ int bpf_get_next_key(int fd, void *key, void *next_key);
int bpf_prog_load(enum bpf_prog_type prog_type, const char *name,
const struct bpf_insn *insns, int insn_len,
const char *license, unsigned kern_version, char *log_buf, unsigned log_buf_size);
const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size);
int bpf_attach_socket(int sockfd, int progfd);
/* create RAW socket and bind to interface 'name' */
......
......@@ -320,23 +320,13 @@ class BPF(object):
return self.funcs[func_name]
if not lib.bpf_function_start(self.module, func_name.encode("ascii")):
raise Exception("Unknown program %s" % func_name)
buffer_len = LOG_BUFFER_SIZE
while True:
log_buf = ct.create_string_buffer(buffer_len) if self.debug else None
fd = lib.bpf_prog_load(prog_type,
func_name.encode("ascii"),
lib.bpf_function_start(self.module, func_name.encode("ascii")),
lib.bpf_function_size(self.module, func_name.encode("ascii")),
lib.bpf_module_license(self.module),
lib.bpf_module_kern_version(self.module),
log_buf, ct.sizeof(log_buf) if log_buf else 0)
if fd < 0 and ct.get_errno() == errno.ENOSPC and self.debug:
buffer_len <<= 1
else:
break
if self.debug & DEBUG_BPF and log_buf.value:
print(log_buf.value.decode(), file=sys.stderr)
fd = lib.bpf_prog_load(prog_type,
func_name.encode("ascii"),
lib.bpf_function_start(self.module, func_name.encode("ascii")),
lib.bpf_function_size(self.module, func_name.encode("ascii")),
lib.bpf_module_license(self.module),
lib.bpf_module_kern_version(self.module),
1 if (self.debug & DEBUG_BPF) else 0, None, 0);
if fd < 0:
atexit.register(self.donothing)
......
......@@ -84,7 +84,7 @@ lib.bpf_attach_socket.restype = ct.c_int
lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int]
lib.bpf_prog_load.restype = ct.c_int
lib.bpf_prog_load.argtypes = [ct.c_int, ct.c_char_p, ct.c_void_p,
ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_char_p, ct.c_uint]
ct.c_size_t, ct.c_char_p, ct.c_uint, ct.c_int, ct.c_char_p, ct.c_uint]
lib.bpf_attach_kprobe.restype = ct.c_void_p
_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_int,
ct.c_ulonglong, ct.POINTER(ct.c_ulonglong))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment