Skip to content

Commit

Permalink
add test for json import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
Littlefisher619 committed Oct 22, 2023
1 parent 38c127e commit 72260ed
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 45 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ build-unit-test:
cmake --build build --config Debug --target bpftime_runtime_tests

unit-test: ## run catch2 unit tests
make -C runtime/test/bpf
make -C runtime/test/bpf && cp runtime/test/bpf/*.bpf.o build/runtime/test/
./build/runtime/unit-test/bpftime_runtime_tests
cd build/runtime && ctest -VV
cd build/runtime/test && ctest -VV

build: ## build the package
cmake -Bbuild -DBPFTIME_ENABLE_UNIT_TESTING=1
Expand Down
40 changes: 21 additions & 19 deletions runtime/src/bpftime_shm_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,22 @@ extern "C" int bpftime_import_global_shm_from_json(const char *filename)
static int import_shm_handler_from_json(bpftime_shm &shm, json value, int fd)
{
std::string handler_type = value["type"];
spdlog::info("import handler type {} fd {}", handler_type, fd);
if (handler_type == "bpf_prog_handler") {
std::string insns_str = value["attr"]["insns"];
std::string name = value["attr"]["name"];
std::string name = value["name"];
int type = value["attr"]["type"];
int cnt = value["attr"]["cnt"];
std::vector<ebpf_inst> insns;
insns.resize(cnt);
int res = hex_string_to_buffer(insns_str,
(unsigned char *)insns.data(),
insns_str.size());
insns.size() * sizeof(ebpf_inst));
if (res < 0) {
spdlog::error("Failed to parse insns in json");
return -1;
}

shm.add_bpf_prog(fd, insns.data(), cnt, name.c_str(), type);
} else if (handler_type == "bpf_map_handler") {
std::string name = value["name"];
Expand Down Expand Up @@ -141,6 +142,7 @@ int bpftime::bpftime_import_shm_from_json(bpftime_shm &shm,
file.close();
for (auto &[key, value] : j.items()) {
int fd = std::stoi(key);
spdlog::info("import handler fd {} {}", fd, value.dump());
int res = import_shm_handler_from_json(shm, value, fd);
if (res < 0) {
spdlog::error("Failed to import handler from json");
Expand Down Expand Up @@ -182,17 +184,16 @@ int bpftime::bpftime_export_shm_to_json(const bpftime_shm &shm,
const char *name = prog_handler.name.c_str();
// record the prog into json, key is the index of the
// prog
j[std::to_string(i)] = {
{ "type", "bpf_prog_handler" },
{ "attr",
{ "type", prog_handler.type },
{ "insns",
buffer_to_hex_string(
(const unsigned char *)insns,
sizeof(ebpf_inst) * cnt) },
{ "cnt", cnt },
{ "name", name } }
};
json attr = { { "type", prog_handler.type },
{ "insns",
buffer_to_hex_string(
(const unsigned char *)insns,
sizeof(ebpf_inst) * cnt) },
{ "cnt", cnt } };
j[std::to_string(i)] =
json{ { "type", "bpf_prog_handler" },
{ "attr", attr },
{ "name", name } };
// append attach fds to the json
for (auto &fd : prog_handler.attach_fds) {
j[std::to_string(i)]["attr"]["attach_fds"]
Expand Down Expand Up @@ -226,11 +227,12 @@ int bpftime::bpftime_export_shm_to_json(const bpftime_shm &shm,
spdlog::info("epoll_handler found at {}", i);
} else if (std::holds_alternative<bpf_link_handler>(handler)) {
auto &h = std::get<bpf_link_handler>(handler);
j[std::to_string(i)] = { { "type", "bpf_link_handler" },
{ "attr",
{ "prog_fd", h.prog_fd },
{ "target_fd",
h.target_fd } } };
j[std::to_string(i)] = {
{ "type", "bpf_link_handler" },
{ "attr",
{ { "prog_fd", h.prog_fd },
{ "target_fd", h.target_fd } } }
};
spdlog::info(
"bpf_link_handler found at {},link {} -> {}",
i, h.prog_fd, h.target_fd);
Expand Down
108 changes: 93 additions & 15 deletions runtime/test/bpf/.output/bpf/bpf.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,68 @@ LIBBPF_API int bpf_obj_get(const char *pathname);
LIBBPF_API int bpf_obj_get_opts(const char *pathname,
const struct bpf_obj_get_opts *opts);

struct bpf_prog_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
unsigned int flags;
int replace_prog_fd;
};
#define bpf_prog_attach_opts__last_field replace_prog_fd

LIBBPF_API int bpf_prog_attach(int prog_fd, int attachable_fd,
enum bpf_attach_type type, unsigned int flags);
LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int attachable_fd,
enum bpf_attach_type type,
const struct bpf_prog_attach_opts *opts);
LIBBPF_API int bpf_prog_detach(int attachable_fd, enum bpf_attach_type type);
LIBBPF_API int bpf_prog_detach2(int prog_fd, int attachable_fd,
enum bpf_attach_type type);

struct bpf_prog_attach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 flags;
union {
int replace_prog_fd;
int replace_fd;
};
int relative_fd;
__u32 relative_id;
__u64 expected_revision;
size_t :0;
};
#define bpf_prog_attach_opts__last_field expected_revision

struct bpf_prog_detach_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
__u32 flags;
int relative_fd;
__u32 relative_id;
__u64 expected_revision;
size_t :0;
};
#define bpf_prog_detach_opts__last_field expected_revision

/**
* @brief **bpf_prog_attach_opts()** attaches the BPF program corresponding to
* *prog_fd* to a *target* which can represent a file descriptor or netdevice
* ifindex.
*
* @param prog_fd BPF program file descriptor
* @param target attach location file descriptor or ifindex
* @param type attach type for the BPF program
* @param opts options for configuring the attachment
* @return 0, on success; negative error code, otherwise (errno is also set to
* the error code)
*/
LIBBPF_API int bpf_prog_attach_opts(int prog_fd, int target,
enum bpf_attach_type type,
const struct bpf_prog_attach_opts *opts);

/**
* @brief **bpf_prog_detach_opts()** detaches the BPF program corresponding to
* *prog_fd* from a *target* which can represent a file descriptor or netdevice
* ifindex.
*
* @param prog_fd BPF program file descriptor
* @param target detach location file descriptor or ifindex
* @param type detach type for the BPF program
* @param opts options for configuring the detachment
* @return 0, on success; negative error code, otherwise (errno is also set to
* the error code)
*/
LIBBPF_API int bpf_prog_detach_opts(int prog_fd, int target,
enum bpf_attach_type type,
const struct bpf_prog_detach_opts *opts);

union bpf_iter_link_info; /* defined in up-to-date linux/bpf.h */
struct bpf_link_create_opts {
size_t sz; /* size of this struct for forward/backward compatibility */
Expand All @@ -346,6 +392,15 @@ struct bpf_link_create_opts {
const unsigned long *addrs;
const __u64 *cookies;
} kprobe_multi;
struct {
__u32 flags;
__u32 cnt;
const char *path;
const unsigned long *offsets;
const unsigned long *ref_ctr_offsets;
const __u64 *cookies;
__u32 pid;
} uprobe_multi;
struct {
__u64 cookie;
} tracing;
Expand All @@ -355,10 +410,15 @@ struct bpf_link_create_opts {
__s32 priority;
__u32 flags;
} netfilter;
struct {
__u32 relative_fd;
__u32 relative_id;
__u64 expected_revision;
} tcx;
};
size_t :0;
};
#define bpf_link_create_opts__last_field kprobe_multi.cookies
#define bpf_link_create_opts__last_field uprobe_multi.pid

LIBBPF_API int bpf_link_create(int prog_fd, int target_fd,
enum bpf_attach_type attach_type,
Expand Down Expand Up @@ -495,13 +555,31 @@ struct bpf_prog_query_opts {
__u32 query_flags;
__u32 attach_flags; /* output argument */
__u32 *prog_ids;
__u32 prog_cnt; /* input+output argument */
union {
/* input+output argument */
__u32 prog_cnt;
__u32 count;
};
__u32 *prog_attach_flags;
__u32 *link_ids;
__u32 *link_attach_flags;
__u64 revision;
size_t :0;
};
#define bpf_prog_query_opts__last_field prog_attach_flags
#define bpf_prog_query_opts__last_field revision

LIBBPF_API int bpf_prog_query_opts(int target_fd,
enum bpf_attach_type type,
/**
* @brief **bpf_prog_query_opts()** queries the BPF programs and BPF links
* which are attached to *target* which can represent a file descriptor or
* netdevice ifindex.
*
* @param target query location file descriptor or ifindex
* @param type attach type for the BPF program
* @param opts options for configuring the query
* @return 0, on success; negative error code, otherwise (errno is also set to
* the error code)
*/
LIBBPF_API int bpf_prog_query_opts(int target, enum bpf_attach_type type,
struct bpf_prog_query_opts *opts);
LIBBPF_API int bpf_prog_query(int target_fd, enum bpf_attach_type type,
__u32 query_flags, __u32 *attach_flags,
Expand Down
14 changes: 9 additions & 5 deletions runtime/test/bpf/.output/bpf/bpf_helper_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,9 @@ static long (*bpf_tail_call)(void *ctx, void *prog_array_map, __u32 index) = (vo
* direct packet access.
*
* Returns
* 0 on success, or a negative error in case of failure.
* 0 on success, or a negative error in case of failure. Positive
* error indicates a potential drop or congestion in the target
* device. The particular positive error codes are not defined.
*/
static long (*bpf_clone_redirect)(struct __sk_buff *skb, __u32 ifindex, __u64 flags) = (void *) 13;

Expand Down Expand Up @@ -3033,9 +3035,6 @@ static __u64 (*bpf_get_current_ancestor_cgroup_id)(int ancestor_level) = (void *
*
* **-EOPNOTSUPP** if the operation is not supported, for example
* a call from outside of TC ingress.
*
* **-ESOCKTNOSUPPORT** if the socket type is not supported
* (reuseport).
*/
static long (*bpf_sk_assign)(void *ctx, void *sk, __u64 flags) = (void *) 124;

Expand Down Expand Up @@ -4065,9 +4064,14 @@ static long (*bpf_timer_cancel)(struct bpf_timer *timer) = (void *) 172;
*
* Get address of the traced function (for tracing and kprobe programs).
*
* When called for kprobe program attached as uprobe it returns
* probe address for both entry and return uprobe.
*
*
* Returns
* Address of the traced function.
* Address of the traced function for kprobe.
* 0 for kprobes placed within the function (not at the entry).
* Address of the probe for uprobe and return uprobe.
*/
static __u64 (*bpf_get_func_ip)(void *ctx) = (void *) 173;

Expand Down
1 change: 1 addition & 0 deletions runtime/test/bpf/.output/bpf/bpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ enum libbpf_tristate {
#define __ksym __attribute__((section(".ksyms")))
#define __kptr_untrusted __attribute__((btf_type_tag("kptr_untrusted")))
#define __kptr __attribute__((btf_type_tag("kptr")))
#define __percpu_kptr __attribute__((btf_type_tag("percpu_kptr")))

#define bpf_ksym_exists(sym) ({ \
_Static_assert(!__builtin_constant_p(!!sym), #sym " should be marked as __weak"); \
Expand Down
2 changes: 1 addition & 1 deletion runtime/test/bpf/.output/bpf/bpf_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef __BPF_TRACING_H__
#define __BPF_TRACING_H__

#include <bpf/bpf_helpers.h>
#include "bpf_helpers.h"

/* Scan the ARCH passed in from ARCH env variable (see Makefile) */
#if defined(__TARGET_ARCH_x86)
Expand Down
Loading

0 comments on commit 72260ed

Please sign in to comment.