Commit e60438db authored by Yonghong Song's avatar Yonghong Song

include libbpf/src/*.c files in the build system

The libbpf/src/*.c files are included in the build system,
so those functions will be available for bcc internals to use them.

There are two name conflicts, bpf_create_map and bpf_prog_load,
between src/cc/libbpf.c and src/cc/libbpf/src/{bpf.c,libbpf.c}.
To keep src/cc/libbpf intact, the following renaming happened
in bcc repo:
  bpf_create_map  =>  bcc_create_map
  bpf_prog_load   =>  bcc_prog_load
Signed-off-by: default avatarYonghong Song <yhs@fb.com>
parent 751559e9
...@@ -15,3 +15,16 @@ else() ...@@ -15,3 +15,16 @@ else()
endif() endif()
set(CMAKE_REQUIRED_FLAGS "${_backup_c_flags}") set(CMAKE_REQUIRED_FLAGS "${_backup_c_flags}")
endif() endif()
# check whether reallocarray availability
# this is used to satisfy reallocarray usage under src/cc/libbpf/
CHECK_CXX_SOURCE_COMPILES(
"
#define _GNU_SOURCE
#include <stdlib.h>
int main(void)
{
return !!reallocarray(NULL, 1, 1);
}
" HAVE_REALLOCARRAY_SUPPORT)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* UseExternalMap shows how to access an external map through * UseExternalMap shows how to access an external map through
* C++ interface. The external map could be a pinned map. * C++ interface. The external map could be a pinned map.
* This example simulates the pinned map through a locally * This example simulates the pinned map through a locally
* created map by calling libbpf bpf_create_map. * created map by calling libbpf bcc_create_map.
* *
* Copyright (c) Facebook, Inc. * Copyright (c) Facebook, Inc.
* Licensed under the Apache License, Version 2.0 (the "License") * Licensed under the Apache License, Version 2.0 (the "License")
...@@ -79,10 +79,10 @@ int main() { ...@@ -79,10 +79,10 @@ int main() {
int ctrl_map_fd; int ctrl_map_fd;
uint32_t val; uint32_t val;
// create a map through bpf_create_map, bcc knows nothing about this map. // create a map through bcc_create_map, bcc knows nothing about this map.
ctrl_map_fd = bpf_create_map(BPF_MAP_TYPE_ARRAY, "control", sizeof(uint32_t), ctrl_map_fd = bcc_create_map(BPF_MAP_TYPE_ARRAY, "control", sizeof(uint32_t),
sizeof(uint32_t), 1, 0); sizeof(uint32_t), 1, 0);
CHECK(ctrl_map_fd < 0, "bpf_create_map failure"); CHECK(ctrl_map_fd < 0, "bcc_create_map failure");
// populate control map into TableStorage // populate control map into TableStorage
std::unique_ptr<ebpf::TableStorage> local_ts = std::unique_ptr<ebpf::TableStorage> local_ts =
......
...@@ -10,6 +10,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frontends/clang) ...@@ -10,6 +10,7 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frontends/clang)
include_directories(${LLVM_INCLUDE_DIRS}) include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${LIBELF_INCLUDE_DIRS}) include_directories(${LIBELF_INCLUDE_DIRS})
# todo: if check for kernel version # todo: if check for kernel version
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include/uapi) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libbpf/include/uapi)
add_definitions(${LLVM_DEFINITIONS}) add_definitions(${LLVM_DEFINITIONS})
configure_file(libbcc.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc @ONLY) configure_file(libbcc.pc.in ${CMAKE_CURRENT_BINARY_DIR}/libbcc.pc @ONLY)
...@@ -18,14 +19,19 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -DBCC_PROG_TAG_DIR='\"${BCC_PROG_T ...@@ -18,14 +19,19 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -DBCC_PROG_TAG_DIR='\"${BCC_PROG_T
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wno-unused-result") set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Wno-unused-result")
if (NOT HAVE_REALLOCARRAY_SUPPORT)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCOMPAT_NEED_REALLOCARRAY")
endif()
string(REGEX MATCH "^([0-9]+).*" _ ${LLVM_PACKAGE_VERSION}) string(REGEX MATCH "^([0-9]+).*" _ ${LLVM_PACKAGE_VERSION})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLLVM_MAJOR_VERSION=${CMAKE_MATCH_1}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLLVM_MAJOR_VERSION=${CMAKE_MATCH_1}")
include(static_libstdc++) include(static_libstdc++)
add_library(bpf-static STATIC libbpf.c perf_reader.c) file(GLOB libbpf_sources "libbpf/src/*.c")
add_library(bpf-static STATIC libbpf.c perf_reader.c ${libbpf_sources})
set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf) set_target_properties(bpf-static PROPERTIES OUTPUT_NAME bpf)
add_library(bpf-shared SHARED libbpf.c perf_reader.c) add_library(bpf-shared SHARED libbpf.c perf_reader.c ${libbpf_sources})
set_target_properties(bpf-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0) set_target_properties(bpf-shared PROPERTIES VERSION ${REVISION_LAST} SOVERSION 0)
set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf) set_target_properties(bpf-shared PROPERTIES OUTPUT_NAME bpf)
......
...@@ -550,7 +550,7 @@ StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type, ...@@ -550,7 +550,7 @@ StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type,
else if (flag_ & DEBUG_BPF) else if (flag_ & DEBUG_BPF)
log_level = 1; log_level = 1;
fd = bpf_prog_load(type, func_name.c_str(), fd = bcc_prog_load(type, func_name.c_str(),
reinterpret_cast<struct bpf_insn*>(func_start), func_size, reinterpret_cast<struct bpf_insn*>(func_start), func_size,
bpf_module_->license(), bpf_module_->kern_version(), bpf_module_->license(), bpf_module_->kern_version(),
log_level, nullptr, 0); log_level, nullptr, 0);
......
...@@ -1108,7 +1108,7 @@ StatusTuple CodegenLLVM::visit_table_decl_stmt_node(TableDeclStmtNode *n) { ...@@ -1108,7 +1108,7 @@ StatusTuple CodegenLLVM::visit_table_decl_stmt_node(TableDeclStmtNode *n) {
decl_gvar->setSection("maps"); decl_gvar->setSection("maps");
tables_[n] = decl_gvar; tables_[n] = decl_gvar;
int map_fd = bpf_create_map(map_type, n->id_->name_.c_str(), int map_fd = bcc_create_map(map_type, n->id_->name_.c_str(),
key->bit_width_ / 8, leaf->bit_width_ / 8, key->bit_width_ / 8, leaf->bit_width_ / 8,
n->size_, 0); n->size_, 0);
if (map_fd >= 0) if (map_fd >= 0)
......
...@@ -1230,7 +1230,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) { ...@@ -1230,7 +1230,7 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
} }
table.type = map_type; table.type = map_type;
table.fd = bpf_create_map(map_type, table.name.c_str(), table.fd = bcc_create_map(map_type, table.name.c_str(),
table.key_size, table.leaf_size, table.key_size, table.leaf_size,
table.max_entries, table.flags); table.max_entries, table.flags);
} }
......
...@@ -82,7 +82,9 @@ ...@@ -82,7 +82,9 @@
#define AF_ALG 38 #define AF_ALG 38
#endif #endif
#ifndef min
#define min(x, y) ((x) < (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y))
#endif
#define UNUSED(expr) do { (void)(expr); } while (0) #define UNUSED(expr) do { (void)(expr); } while (0)
...@@ -191,7 +193,7 @@ static uint64_t ptr_to_u64(void *ptr) ...@@ -191,7 +193,7 @@ static uint64_t ptr_to_u64(void *ptr)
return (uint64_t) (unsigned long) ptr; return (uint64_t) (unsigned long) ptr;
} }
int bpf_create_map(enum bpf_map_type map_type, const char *name, int bcc_create_map(enum bpf_map_type map_type, const char *name,
int key_size, int value_size, int key_size, int value_size,
int max_entries, int map_flags) int max_entries, int map_flags)
{ {
...@@ -483,7 +485,7 @@ int bpf_prog_get_tag(int fd, unsigned long long *ptag) ...@@ -483,7 +485,7 @@ int bpf_prog_get_tag(int fd, unsigned long long *ptag)
return 0; return 0;
} }
int bpf_prog_load(enum bpf_prog_type prog_type, const char *name, int bcc_prog_load(enum bpf_prog_type prog_type, const char *name,
const struct bpf_insn *insns, int prog_len, const struct bpf_insn *insns, int prog_len,
const char *license, unsigned kern_version, const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size) int log_level, char *log_buf, unsigned log_buf_size)
...@@ -1426,59 +1428,3 @@ int bpf_close_perf_event_fd(int fd) { ...@@ -1426,59 +1428,3 @@ int bpf_close_perf_event_fd(int fd) {
} }
return error; return error;
} }
int bpf_obj_pin(int fd, const char *pathname)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.pathname = ptr_to_u64((void *)pathname);
attr.bpf_fd = fd;
return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr));
}
int bpf_obj_get(const char *pathname)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.pathname = ptr_to_u64((void *)pathname);
return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr));
}
int bpf_prog_get_next_id(uint32_t start_id, uint32_t *next_id)
{
union bpf_attr attr;
int err;
memset(&attr, 0, sizeof(attr));
attr.start_id = start_id;
err = syscall(__NR_bpf, BPF_PROG_GET_NEXT_ID, &attr, sizeof(attr));
if (!err)
*next_id = attr.next_id;
return err;
}
int bpf_prog_get_fd_by_id(uint32_t id)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.prog_id = id;
return syscall(__NR_bpf, BPF_PROG_GET_FD_BY_ID, &attr, sizeof(attr));
}
int bpf_map_get_fd_by_id(uint32_t id)
{
union bpf_attr attr;
memset(&attr, 0, sizeof(attr));
attr.map_id = id;
return syscall(__NR_bpf, BPF_MAP_GET_FD_BY_ID, &attr, sizeof(attr));
}
...@@ -31,7 +31,7 @@ enum bpf_probe_attach_type { ...@@ -31,7 +31,7 @@ enum bpf_probe_attach_type {
BPF_PROBE_RETURN BPF_PROBE_RETURN
}; };
int bpf_create_map(enum bpf_map_type map_type, const char *name, int bcc_create_map(enum bpf_map_type map_type, const char *name,
int key_size, int value_size, int max_entries, int key_size, int value_size, int max_entries,
int map_flags); int map_flags);
int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
...@@ -56,7 +56,7 @@ int bpf_get_next_key(int fd, void *key, void *next_key); ...@@ -56,7 +56,7 @@ int bpf_get_next_key(int fd, void *key, void *next_key);
* printing, and continue to attempt increase that allocated buffer size if * printing, and continue to attempt increase that allocated buffer size if
* initial attemp was insufficient in size. * initial attemp was insufficient in size.
*/ */
int bpf_prog_load(enum bpf_prog_type prog_type, const char *name, int bcc_prog_load(enum bpf_prog_type prog_type, const char *name,
const struct bpf_insn *insns, int insn_len, const struct bpf_insn *insns, int insn_len,
const char *license, unsigned kern_version, const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size); int log_level, char *log_buf, unsigned log_buf_size);
......
...@@ -162,7 +162,7 @@ function Bpf:load_func(fn_name, prog_type) ...@@ -162,7 +162,7 @@ function Bpf:load_func(fn_name, prog_type)
assert(libbcc.bpf_function_start(self.module, fn_name) ~= nil, assert(libbcc.bpf_function_start(self.module, fn_name) ~= nil,
"unknown program: "..fn_name) "unknown program: "..fn_name)
local fd = libbcc.bpf_prog_load(prog_type, local fd = libbcc.bcc_prog_load(prog_type,
fn_name, fn_name,
libbcc.bpf_function_start(self.module, fn_name), libbcc.bpf_function_start(self.module, fn_name),
libbcc.bpf_function_size(self.module, fn_name), libbcc.bpf_function_size(self.module, fn_name),
......
...@@ -24,13 +24,13 @@ enum bpf_prog_type { ...@@ -24,13 +24,13 @@ enum bpf_prog_type {
BPF_PROG_TYPE_SCHED_ACT, BPF_PROG_TYPE_SCHED_ACT,
}; };
int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries, int map_flags); int bcc_create_map(enum bpf_map_type map_type, int key_size, int value_size, int max_entries, int map_flags);
int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags);
int bpf_lookup_elem(int fd, void *key, void *value); int bpf_lookup_elem(int fd, void *key, void *value);
int bpf_delete_elem(int fd, void *key); int bpf_delete_elem(int fd, void *key);
int bpf_get_next_key(int fd, void *key, void *next_key); 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, int bcc_prog_load(enum bpf_prog_type prog_type, const char *name,
const struct bpf_insn *insns, int insn_len, const struct bpf_insn *insns, int insn_len,
const char *license, unsigned kern_version, const char *license, unsigned kern_version,
int log_level, char *log_buf, unsigned log_buf_size); int log_level, char *log_buf, unsigned log_buf_size);
......
...@@ -1475,7 +1475,7 @@ local tracepoint_mt = { ...@@ -1475,7 +1475,7 @@ local tracepoint_mt = {
prog = compile(prog, {proto.type(t.type, {source='ptr_to_probe'})}) prog = compile(prog, {proto.type(t.type, {source='ptr_to_probe'})})
end end
-- Load the BPF program -- Load the BPF program
local prog_fd, err, log = S.bpf_prog_load(S.c.BPF_PROG.TRACEPOINT, prog.insn, prog.pc) local prog_fd, err, log = S.bcc_prog_load(S.c.BPF_PROG.TRACEPOINT, prog.insn, prog.pc)
assert(prog_fd, tostring(err)..': '..tostring(log)) assert(prog_fd, tostring(err)..': '..tostring(log))
-- Open tracepoint and attach -- Open tracepoint and attach
t.reader:setbpf(prog_fd:getfd()) t.reader:setbpf(prog_fd:getfd())
...@@ -1499,7 +1499,7 @@ local function trace_bpf(ptype, pname, pdef, retprobe, prog, pid, cpu, group_fd) ...@@ -1499,7 +1499,7 @@ local function trace_bpf(ptype, pname, pdef, retprobe, prog, pid, cpu, group_fd)
if type(prog) ~= 'table' then if type(prog) ~= 'table' then
prog = compile(prog, {proto.pt_regs}) prog = compile(prog, {proto.pt_regs})
end end
local prog_fd, err, log = S.bpf_prog_load(S.c.BPF_PROG.KPROBE, prog.insn, prog.pc) local prog_fd, err, log = S.bcc_prog_load(S.c.BPF_PROG.KPROBE, prog.insn, prog.pc)
assert(prog_fd, tostring(err)..': '..tostring(log)) assert(prog_fd, tostring(err)..': '..tostring(log))
-- Open tracepoint and attach -- Open tracepoint and attach
local tp, err = S.perf_probe(ptype, pname, pdef, retprobe) local tp, err = S.perf_probe(ptype, pname, pdef, retprobe)
...@@ -1580,7 +1580,7 @@ return setmetatable({ ...@@ -1580,7 +1580,7 @@ return setmetatable({
if type(prog) ~= 'table' then if type(prog) ~= 'table' then
prog = compile(prog, {proto.skb}) prog = compile(prog, {proto.skb})
end end
local prog_fd, err, log = S.bpf_prog_load(S.c.BPF_PROG.SOCKET_FILTER, prog.insn, prog.pc) local prog_fd, err, log = S.bcc_prog_load(S.c.BPF_PROG.SOCKET_FILTER, prog.insn, prog.pc)
assert(prog_fd, tostring(err)..': '..tostring(log)) assert(prog_fd, tostring(err)..': '..tostring(log))
assert(sock:setsockopt('socket', 'attach_bpf', prog_fd:getfd())) assert(sock:setsockopt('socket', 'attach_bpf', prog_fd:getfd()))
return prog_fd, err return prog_fd, err
......
...@@ -365,7 +365,7 @@ class BPF(object): ...@@ -365,7 +365,7 @@ class BPF(object):
log_level = 2 log_level = 2
elif (self.debug & DEBUG_BPF): elif (self.debug & DEBUG_BPF):
log_level = 1 log_level = 1
fd = lib.bpf_prog_load(prog_type, func_name, fd = lib.bcc_prog_load(prog_type, func_name,
lib.bpf_function_start(self.module, func_name), lib.bpf_function_start(self.module, func_name),
lib.bpf_function_size(self.module, func_name), lib.bpf_function_size(self.module, func_name),
lib.bpf_module_license(self.module), lib.bpf_module_license(self.module),
......
...@@ -82,8 +82,8 @@ lib.bpf_open_raw_sock.restype = ct.c_int ...@@ -82,8 +82,8 @@ lib.bpf_open_raw_sock.restype = ct.c_int
lib.bpf_open_raw_sock.argtypes = [ct.c_char_p] lib.bpf_open_raw_sock.argtypes = [ct.c_char_p]
lib.bpf_attach_socket.restype = ct.c_int lib.bpf_attach_socket.restype = ct.c_int
lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int] lib.bpf_attach_socket.argtypes = [ct.c_int, ct.c_int]
lib.bpf_prog_load.restype = ct.c_int lib.bcc_prog_load.restype = ct.c_int
lib.bpf_prog_load.argtypes = [ct.c_int, ct.c_char_p, ct.c_void_p, lib.bcc_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_int, 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]
_RAW_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, ct.c_void_p, ct.c_int) _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) _LOST_CB_TYPE = ct.CFUNCTYPE(None, ct.py_object, 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