From cc7a4e78386c7d0de7069aa66b945bde6f76d826 Mon Sep 17 00:00:00 2001 From: xhuacmer Date: Wed, 8 Mar 2023 16:35:11 +0800 Subject: [PATCH 1/2] add CTinspector test entry --- ebpf_vm_test/mp_vm_test.c | 231 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 ebpf_vm_test/mp_vm_test.c diff --git a/ebpf_vm_test/mp_vm_test.c b/ebpf_vm_test/mp_vm_test.c new file mode 100644 index 0000000..1c05f06 --- /dev/null +++ b/ebpf_vm_test/mp_vm_test.c @@ -0,0 +1,231 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mp_vm_test.h" + +struct vm_test_config { + char *vm_file; + int test_case; + int act_as_client; +}; + +static struct vm_test_case *tests[MP_VM_TEST_NUM]; + +int register_test_case(struct vm_test_case *test) +{ + tests[test->index] = test; + return 0; +} + +static void usage(void) +{ + printf("Usage:\n"); + printf(" -a, --self-ip= listen on ip \n"); + printf(" -p, --self-port= listen on port (default 18515)\n"); + printf(" -d, --ib-dev= use IB device (default first device found)\n"); + printf(" -i, --ib-port= use port of IB device (default 1)\n"); + printf(" -s, --size= size of message to exchange (default 2048)\n"); + printf(" -r, --rx-depth= number of receives to post at a time (default 500)\n"); + printf(" -g, --gid-idx= local port gid index\n"); + printf(" -f, --ebpf-program= path to ebpf program\n"); + printf(" -t, --test-case= test case index\n"); + printf(" -c, --client act as client\n"); +} + +static int parse_config(struct vm_test_config *test_cfg, + struct ebpf_vm_executor_config *executor_cfg, + int argc, char **argv) +{ + static struct option long_options[] = { + {.name = "ebpf-program", .has_arg = 1, .val = 'f'}, + {.name = "test-case", .has_arg = 1, .val = 't'}, + {.name = "self-ip", .has_arg = 1, .val = 'a'}, + {.name = "self-port", .has_arg = 1, .val = 'p'}, + {.name = "ib-dev", .has_arg = 1, .val = 'd'}, + {.name = "ib-port", .has_arg = 1, .val = 'i'}, + {.name = "msg-size", .has_arg = 1, .val = 's'}, + {.name = "rx-depth", .has_arg = 1, .val = 'r'}, + {.name = "gid-idx", .has_arg = 1, .val = 'g'}, + {.name = "client", .has_arg = 0, .val = 'c'}, + }; + struct rdma_transport_config *rdma_cfg = &executor_cfg->transport.rdma_cfg; + + while (1) { + int c = getopt_long(argc, argv, "f:t:a:p:d:i:s:r:g:c", long_options, NULL); + if (c == -1) + break; + + switch (c) { + case 'f': + test_cfg->vm_file = strdup(optarg); + break; + + case 't': + test_cfg->test_case = strtol(optarg, NULL, 0); + if (test_cfg->test_case >= MP_VM_TEST_NUM || tests[test_cfg->test_case] == NULL) { + perror("test case is not supported"); + return 1; + } + break; + + case 'a': + inet_pton(AF_INET, optarg, &rdma_cfg->self_url.ip); + break; + + case 'p': + rdma_cfg->self_url.port = strtol(optarg, NULL, 0); + rdma_cfg->self_url.port = htons(rdma_cfg->self_url.port); + break; + + case 'd': + rdma_cfg->ib_devname = strdup(optarg); + break; + + case 'i': + rdma_cfg->ib_port = strtol(optarg, NULL, 0); + if (rdma_cfg->ib_port < 1) { + usage(); + return 1; + } + break; + + case 's': + rdma_cfg->max_msg_size = strtoul(optarg, NULL, 0); + break; + + case 'r': + rdma_cfg->rx_depth = strtoul(optarg, NULL, 0); + break; + + case 'g': + rdma_cfg->gid_index = strtoul(optarg, NULL, 0); + break; + + case 'c': + test_cfg->act_as_client = 1; + break; + } + } + + executor_cfg->transport.transport_type = PKT_VM_TRANSPORT_TYPE_RDMA; + return 0; +} + +static void say_hello_to(struct ebpf_vm_executor *executor, char *ip, uint16_t port) +{ + struct transport_message send_msg; + struct node_url dst = {0}; + char msg[64]; + int ret; + + inet_pton(AF_INET, ip, &dst.ip); + dst.port = htons(port); + + sprintf(msg, "hello %s", ip); + send_msg.buf = msg; + send_msg.buf_size = strlen(msg) + 1; + + ret = executor->transport->send(executor->transport_ctx, &dst, &send_msg); + if (ret != send_msg.buf_size) { + printf("Message is not sent.\n"); + } +} + +static void test_transport(struct ebpf_vm_executor *executor, int act_as_client) +{ + char *ip_str[2] = {"192.168.100.20", "192.168.100.10"}; + int dst_ip_idx = (act_as_client == 1); + int recv_msg_num = 0; + int expected_msg_num = 2; + + if (act_as_client == 1) { + say_hello_to(executor, ip_str[dst_ip_idx], 1881); + } + + while (recv_msg_num < expected_msg_num) { + struct transport_message recv_msg; + int msg_len = executor->transport->recv(executor->transport_ctx, &recv_msg); + if (msg_len != 0) { + recv_msg_num++; + printf("Received message: %s\n", (char *)recv_msg.buf); + say_hello_to(executor, ip_str[dst_ip_idx], 1881); + } + } +} + +int main(int argc, char **argv) +{ + struct ebpf_vm_executor *executor = NULL; + struct ebpf_vm *vm = NULL; + struct ebpf_vm_executor_config cfg = {0}; + struct vm_test_config test_cfg = {0}; + void *test_ctx; + + if (parse_config(&test_cfg, &cfg, argc, argv) != 0) { + perror("failed to parse test config"); + return -1; + } + + executor = vm_executor_init(&cfg); + if (executor == NULL) { + perror("failed to initialize vm executor"); + return -1; + } + + if (test_cfg.vm_file != NULL) { + vm = create_vm_from_elf(test_cfg.vm_file); + if (vm == NULL) { + printf("Failed to create ebpf vm from file %s\n", test_cfg.vm_file); + vm_executor_destroy(executor); + return -1; + } + } + + test_ctx = tests[test_cfg.test_case]->setup(executor, vm, argc, argv); + if (test_ctx == NULL) { + destroy_vm(vm); + vm_executor_destroy(executor); + return 0; + } + + if (vm != NULL) { + add_vm(executor, vm); + } + + vm_executor_run(executor); + + //test_transport(executor, test_cfg.act_as_client); + + tests[test_cfg.test_case]->teardown(test_ctx); + vm_executor_destroy(executor); + return 0; +} + +static void *general_test_setup(struct ebpf_vm_executor *executor, struct ebpf_vm *vm, int argc, char **argv) +{ + return (void *)-1; +} + +static void general_test_teardown(void *ctx) +{ + return; +} + +static struct vm_test_case general_test = { + .index = MP_VM_TEST_GENERAL, + .setup = general_test_setup, + .teardown = general_test_teardown +}; + +static __attribute__((constructor)) void general_register_test(void) +{ + register_test_case(&general_test); +} \ No newline at end of file -- Gitee From 4916690262c25c79f5273a4d99a14675fe028b57 Mon Sep 17 00:00:00 2001 From: xhuacmer Date: Wed, 8 Mar 2023 16:38:13 +0800 Subject: [PATCH 2/2] add CTinspector monitor address --- ebpf_vm_test/test_monitor_address.c | 85 +++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 ebpf_vm_test/test_monitor_address.c diff --git a/ebpf_vm_test/test_monitor_address.c b/ebpf_vm_test/test_monitor_address.c new file mode 100644 index 0000000..a83bca3 --- /dev/null +++ b/ebpf_vm_test/test_monitor_address.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "mp_vm_test.h" + +struct test_config { + int test_value; +}; + +struct monitor_test_context { + pthread_t thread; + uint64_t test_mem; + uint64_t test_value; +} test_ctx; + +static void *thread_change_test_mem(void *unused) +{ + sleep(1); + test_ctx.test_mem = test_ctx.test_value; + return NULL; +} + +static int parse_test_config(struct test_config *cfg, int argc, char **argv) +{ + static struct option long_options[] = { + {.name = "test-value", .has_arg = 1, .val = 'v'}, + {} + }; + + while (1) { + int c = getopt_long(argc, argv, "v:", long_options, NULL); + if (c == -1) + break; + + switch (c) { + case 'v': + cfg->test_value = strtoul(optarg, NULL, 0); + break; + } + } + + return 0; +} + +static void *monitor_addr_test_setup(struct ebpf_vm_executor *executor, struct ebpf_vm *vm, int argc, char **argv) +{ + struct test_config test_cfg = {0}; + + if (parse_test_config(&test_cfg, argc, argv) != 0) { + perror("failed to parse test config"); + return NULL; + } + + test_ctx.test_value = test_cfg.test_value; + + vm->reg[1] = (uint64_t)&test_ctx.test_mem; + vm->reg[2] = sizeof(test_ctx.test_mem); + vm->reg[3] = test_ctx.test_value; + + pthread_create(&test_ctx.thread, NULL, thread_change_test_mem, NULL); + return &test_ctx; +} + +static void monitor_addr_test_teardown(void *ctx) +{ + return; +} + +static struct vm_test_case monitor_addr_test = { + .index = MP_VM_TEST_MONITOR_ADDR, + .setup = monitor_addr_test_setup, + .teardown = monitor_addr_test_teardown +}; + +static __attribute__((constructor)) void monitor_addr_register_test(void) +{ + register_test_case(&monitor_addr_test); +} \ No newline at end of file -- Gitee