diff --git a/.clang-format b/.clang-format index a94263d..e6471c8 100644 --- a/.clang-format +++ b/.clang-format @@ -1,5 +1,7 @@ BasedOnStyle: LLVM IndentWidth: 2 +ContinuationIndentWidth: 2 +BracedInitializerIndentWidth: 2 ColumnLimit: 120 PointerAlignment: Right AlignAfterOpenBracket: Align diff --git a/demos/async/client.c b/demos/async/client.c index 363fe57..660d4e5 100644 --- a/demos/async/client.c +++ b/demos/async/client.c @@ -1,4 +1,4 @@ -#include "rpc/client.h" +#include "wirecall/client.h" #include #include @@ -9,34 +9,34 @@ int main(int argc, char **argv) { int64_t a = argc > 3 ? strtoll(argv[3], NULL, 10) : 20; int64_t b = argc > 4 ? strtoll(argv[4], NULL, 10) : 22; - rpc_client *client = NULL; - if (rpc_client_connect(&client, host, port) != 0) { + wirecall_client *client = NULL; + if (wirecall_client_connect(&client, host, port) != 0) { fprintf(stderr, "connect failed\n"); return 1; } - rpc_writer args; - rpc_writer_init(&args); - rpc_writer_i64(&args, a); - rpc_writer_i64(&args, b); + wirecall_writer args; + wirecall_writer_init(&args); + wirecall_writer_i64(&args, a); + wirecall_writer_i64(&args, b); - rpc_value *result = NULL; + wirecall_value *result = NULL; size_t result_count = 0; - if (rpc_client_call_name(client, "add", &args, &result, &result_count) != 0) { - fprintf(stderr, "async RPC error: %s\n", rpc_client_error(client)); - rpc_writer_free(&args); - rpc_client_close(client); + if (wirecall_client_call_name(client, "add", &args, &result, &result_count) != 0) { + fprintf(stderr, "async Wirecall error: %s\n", wirecall_client_error(client)); + wirecall_writer_free(&args); + wirecall_client_close(client); return 1; } - if (result_count == 1 && result[0].type == RPC_TYPE_I64) { + if (result_count == 1 && result[0].type == WIRECALL_TYPE_I64) { printf("async %lld + %lld = %lld\n", (long long)a, (long long)b, (long long)result[0].as.i64); } else { fprintf(stderr, "unexpected response\n"); } - rpc_values_free(result); - rpc_writer_free(&args); - rpc_client_close(client); + wirecall_values_free(result); + wirecall_writer_free(&args); + wirecall_client_close(client); return 0; } diff --git a/demos/async/server.c b/demos/async/server.c index 1215bb9..9b7b7f3 100644 --- a/demos/async/server.c +++ b/demos/async/server.c @@ -1,4 +1,4 @@ -#include "rpc/server.h" +#include "wirecall/server.h" #include #include @@ -25,7 +25,7 @@ typedef struct async_queue { pthread_t thread; } async_queue; -static rpc_server *g_server; +static wirecall_server *g_server; static async_queue g_queue; static void queue_push(async_queue *queue, async_job *job) { @@ -62,8 +62,8 @@ static void *queue_worker(void *arg) { if (!job) { return NULL; } struct timespec delay = { - .tv_sec = 0, - .tv_nsec = 1000000, + .tv_sec = 0, + .tv_nsec = 1000000, }; nanosleep(&delay, NULL); job->result = job->a + job->b; @@ -98,12 +98,13 @@ static void queue_stop(async_queue *queue) { static void on_signal(int signo) { (void)signo; - if (g_server) { rpc_server_stop(g_server); } + if (g_server) { wirecall_server_stop(g_server); } } -static int async_add(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int async_add(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { async_queue *queue = user_data; - if (argc != 2 || args[0].type != RPC_TYPE_I64 || args[1].type != RPC_TYPE_I64) { return -1; } + if (argc != 2 || args[0].type != WIRECALL_TYPE_I64 || args[1].type != WIRECALL_TYPE_I64) { return -1; } async_job job; memset(&job, 0, sizeof(job)); @@ -113,10 +114,10 @@ static int async_add(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_write queue_push(queue, &job); while (!atomic_load_explicit(&job.done, memory_order_acquire)) { - rpc_ctx_yield(ctx); + wirecall_ctx_yield(ctx); } - return rpc_writer_i64(out, job.result); + return wirecall_writer_i64(out, job.result); } int main(int argc, char **argv) { @@ -129,21 +130,21 @@ int main(int argc, char **argv) { return 1; } - if (rpc_server_init(&g_server) != 0 || rpc_server_set_workers(g_server, workers) != 0 || - rpc_server_add_async_route_name(g_server, "add", async_add, &g_queue) != 0 || rpc_server_bind(g_server, host, port) != 0 || - rpc_server_listen(g_server) != 0) { - fprintf(stderr, "failed to start async RPC server\n"); - rpc_server_destroy(g_server); + if (wirecall_server_init(&g_server) != 0 || wirecall_server_set_workers(g_server, workers) != 0 || + wirecall_server_add_async_route_name(g_server, "add", async_add, &g_queue) != 0 || + wirecall_server_bind(g_server, host, port) != 0 || wirecall_server_listen(g_server) != 0) { + fprintf(stderr, "failed to start async Wirecall server\n"); + wirecall_server_destroy(g_server); queue_stop(&g_queue); return 1; } signal(SIGINT, on_signal); signal(SIGTERM, on_signal); - printf("async rpc demo server listening on %s:%u\n", host, rpc_server_port(g_server)); + printf("async rpc demo server listening on %s:%u\n", host, wirecall_server_port(g_server)); - int rc = rpc_server_run(g_server); - rpc_server_destroy(g_server); + int rc = wirecall_server_run(g_server); + wirecall_server_destroy(g_server); queue_stop(&g_queue); return rc == 0 ? 0 : 1; } diff --git a/demos/bench.c b/demos/bench.c index 8436e91..fed5550 100644 --- a/demos/bench.c +++ b/demos/bench.c @@ -1,6 +1,6 @@ -#include "rpc/client.h" -#include "rpc/server.h" -#include "rpc/trace.h" +#include "wirecall/client.h" +#include "wirecall/server.h" +#include "wirecall/trace.h" #include #include @@ -23,7 +23,7 @@ typedef struct bench_memory { } bench_memory; typedef struct bench_server { - rpc_server *rpc; + wirecall_server *rpc; pthread_t thread; uint32_t workers; uint32_t integrity; @@ -90,8 +90,7 @@ static bench_memory current_memory(void) { #ifdef __APPLE__ task_vm_info_data_t info; mach_msg_type_number_t count = TASK_VM_INFO_COUNT; - if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &count) == - KERN_SUCCESS) { + if (task_info(mach_task_self(), TASK_VM_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) { memory.rss = info.resident_size; memory.virtual_size = info.virtual_size; memory.footprint = info.phys_footprint; @@ -100,31 +99,32 @@ static bench_memory current_memory(void) { return memory; } -static int add_handler(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int add_handler(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)ctx; (void)user_data; - if (argc != 2 || args[0].type != RPC_TYPE_I64 || args[1].type != RPC_TYPE_I64) { return -1; } - return rpc_writer_i64(out, args[0].as.i64 + args[1].as.i64); + if (argc != 2 || args[0].type != WIRECALL_TYPE_I64 || args[1].type != WIRECALL_TYPE_I64) { return -1; } + return wirecall_writer_i64(out, args[0].as.i64 + args[1].as.i64); } static void *server_main(void *arg) { bench_server *server = arg; - (void)rpc_server_run(server->rpc); + (void)wirecall_server_run(server->rpc); return NULL; } static int start_server(bench_server *server, char port[16]) { - if (rpc_server_init(&server->rpc) != 0 || - rpc_server_set_integrity(server->rpc, server->integrity, server->mac_key) != 0 || - (server->workers != 0 && rpc_server_set_workers(server->rpc, server->workers) != 0) || - rpc_server_add_route_name(server->rpc, "add", add_handler, NULL) != 0 || - rpc_server_bind(server->rpc, "127.0.0.1", "0") != 0 || rpc_server_listen(server->rpc) != 0) { + if (wirecall_server_init(&server->rpc) != 0 || + wirecall_server_set_integrity(server->rpc, server->integrity, server->mac_key) != 0 || + (server->workers != 0 && wirecall_server_set_workers(server->rpc, server->workers) != 0) || + wirecall_server_add_route_name(server->rpc, "add", add_handler, NULL) != 0 || + wirecall_server_bind(server->rpc, "127.0.0.1", "0") != 0 || wirecall_server_listen(server->rpc) != 0) { return -1; } - snprintf(port, 16, "%u", rpc_server_port(server->rpc)); + snprintf(port, 16, "%u", wirecall_server_port(server->rpc)); if (pthread_create(&server->thread, NULL, server_main, server) != 0) { - rpc_server_destroy(server->rpc); + wirecall_server_destroy(server->rpc); server->rpc = NULL; return -1; } @@ -133,44 +133,44 @@ static int start_server(bench_server *server, char port[16]) { static void stop_server(bench_server *server) { if (!server->rpc) { return; } - rpc_server_stop(server->rpc); + wirecall_server_stop(server->rpc); pthread_join(server->thread, NULL); - rpc_server_destroy(server->rpc); + wirecall_server_destroy(server->rpc); server->rpc = NULL; } -static int run_one_call(rpc_client *client, const char *proc_name, rpc_writer *payload, int64_t expected) { - rpc_value *values = NULL; +static int run_one_call(wirecall_client *client, const char *proc_name, wirecall_writer *payload, int64_t expected) { + wirecall_value *values = NULL; size_t count = 0; - int rc = rpc_client_call_name(client, proc_name, payload, &values, &count); - if (rc == 0 && (count != 1 || values[0].type != RPC_TYPE_I64 || values[0].as.i64 != expected)) { rc = -1; } - rpc_values_free(values); + int rc = wirecall_client_call_name(client, proc_name, payload, &values, &count); + if (rc == 0 && (count != 1 || values[0].type != WIRECALL_TYPE_I64 || values[0].as.i64 != expected)) { rc = -1; } + wirecall_values_free(values); return rc; } -static int validate_response(rpc_value *values, size_t count, int64_t expected) { - return count == 1 && values[0].type == RPC_TYPE_I64 && values[0].as.i64 == expected; +static int validate_response(wirecall_value *values, size_t count, int64_t expected) { + return count == 1 && values[0].type == WIRECALL_TYPE_I64 && values[0].as.i64 == expected; } static void *worker_main(void *arg) { worker_args *worker = arg; int announced_gate = 0; - rpc_client *client = NULL; - if (rpc_client_connect(&client, worker->host, worker->port) != 0) { + wirecall_client *client = NULL; + if (wirecall_client_connect(&client, worker->host, worker->port) != 0) { worker->failed = 1; (void)gate_ready_and_wait(worker->gate); return NULL; } - if (rpc_client_set_integrity(client, worker->integrity, worker->mac_key) != 0) { + if (wirecall_client_set_integrity(client, worker->integrity, worker->mac_key) != 0) { worker->failed = 1; (void)gate_ready_and_wait(worker->gate); - rpc_client_close(client); + wirecall_client_close(client); return NULL; } - rpc_writer payload; - rpc_writer_init(&payload); - if (rpc_writer_i64(&payload, 20) != 0 || rpc_writer_i64(&payload, 22) != 0) { + wirecall_writer payload; + wirecall_writer_init(&payload); + if (wirecall_writer_i64(&payload, 20) != 0 || wirecall_writer_i64(&payload, 22) != 0) { worker->failed = 1; goto done; } @@ -214,7 +214,7 @@ static void *worker_main(void *arg) { while (sent < worker->requests && sent - received < worker->pipeline) { uint64_t start = now_ns(); uint64_t call_id = 0; - if (rpc_client_send_call_name(client, worker->proc_name, &payload, &call_id) != 0) { + if (wirecall_client_send_call_name(client, worker->proc_name, &payload, &call_id) != 0) { worker->failed = 1; goto pipeline_done; } @@ -226,28 +226,29 @@ static void *worker_main(void *arg) { while (received < worker->requests) { uint64_t call_id = 0; - rpc_value *values = NULL; + wirecall_value *values = NULL; size_t count = 0; - if (rpc_client_recv_response(client, &call_id, &values, &count) != 0 || !validate_response(values, count, 42)) { - rpc_values_free(values); + if (wirecall_client_recv_response(client, &call_id, &values, &count) != 0 || + !validate_response(values, count, 42)) { + wirecall_values_free(values); worker->failed = 1; goto pipeline_done; } uint64_t slot = call_id % worker->pipeline; if (call_ids[slot] != call_id) { - rpc_values_free(values); + wirecall_values_free(values); worker->failed = 1; goto pipeline_done; } worker->latencies_ns[worker->latency_offset + received] = now_ns() - starts[slot]; - rpc_values_free(values); + wirecall_values_free(values); received++; if (sent < worker->requests) { uint64_t start = now_ns(); uint64_t next_call_id = 0; - if (rpc_client_send_call_name(client, worker->proc_name, &payload, &next_call_id) != 0) { + if (wirecall_client_send_call_name(client, worker->proc_name, &payload, &next_call_id) != 0) { worker->failed = 1; goto pipeline_done; } @@ -264,8 +265,8 @@ pipeline_done: done: if (!announced_gate) { (void)gate_ready_and_wait(worker->gate); } - rpc_writer_free(&payload); - rpc_client_close(client); + wirecall_writer_free(&payload); + wirecall_client_close(client); return NULL; } @@ -380,12 +381,11 @@ int main(int argc, char **argv) { if (clients > total_requests) { clients = total_requests; } static const uint8_t bench_mac_key[16] = { - 0x30, 0x9d, 0x92, 0x4f, 0x28, 0xaa, 0x62, 0xa9, - 0x0f, 0xf1, 0x68, 0x87, 0xcf, 0x41, 0xc2, 0x65, + 0x30, 0x9d, 0x92, 0x4f, 0x28, 0xaa, 0x62, 0xa9, 0x0f, 0xf1, 0x68, 0x87, 0xcf, 0x41, 0xc2, 0x65, }; - uint32_t integrity = RPC_INTEGRITY_NONE; - if (checksum_enabled) { integrity |= RPC_INTEGRITY_CHECKSUM; } - if (mac_enabled) { integrity |= RPC_INTEGRITY_MAC; } + uint32_t integrity = WIRECALL_INTEGRITY_NONE; + if (checksum_enabled) { integrity |= WIRECALL_INTEGRITY_CHECKSUM; } + if (mac_enabled) { integrity |= WIRECALL_INTEGRITY_MAC; } uint64_t *latencies = calloc(total_requests, sizeof(*latencies)); pthread_t *threads = calloc(clients, sizeof(*threads)); @@ -425,15 +425,15 @@ int main(int argc, char **argv) { for (uint64_t i = 0; i < clients; ++i) { uint64_t count = request_base + (i < request_rem ? 1u : 0u); workers[i] = (worker_args){ - .host = "127.0.0.1", - .requests = count, - .warmup = warmup_base + (i < warmup_rem ? 1u : 0u), - .pipeline = pipeline, - .proc_name = "add", - .integrity = integrity, - .latency_offset = offset, - .latencies_ns = latencies, - .gate = &gate, + .host = "127.0.0.1", + .requests = count, + .warmup = warmup_base + (i < warmup_rem ? 1u : 0u), + .pipeline = pipeline, + .proc_name = "add", + .integrity = integrity, + .latency_offset = offset, + .latencies_ns = latencies, + .gate = &gate, }; memcpy(workers[i].mac_key, bench_mac_key, sizeof(workers[i].mac_key)); snprintf(workers[i].port, sizeof(workers[i].port), "%s", port); @@ -463,8 +463,8 @@ int main(int argc, char **argv) { gate_abort(&gate); stop_server(&server); } else { - rpc_trace_reset(); - rpc_trace_set_enabled(trace_enabled); + wirecall_trace_reset(); + wirecall_trace_set_enabled(trace_enabled); getrusage(RUSAGE_SELF, &usage_start); bench_start = now_ns(); bench_started = 1; @@ -476,7 +476,7 @@ int main(int argc, char **argv) { if (workers[i].started) { pthread_join(threads[i], NULL); } failed |= workers[i].failed; } - rpc_trace_set_enabled(0); + wirecall_trace_set_enabled(0); uint64_t bench_elapsed = bench_started ? now_ns() - bench_start : 0; struct rusage usage_end; memset(&usage_end, 0, sizeof(usage_end)); @@ -563,7 +563,7 @@ int main(int argc, char **argv) { printf(" major faults: %ld\n", usage_end.ru_majflt - usage_start.ru_majflt); printf(" voluntary csw: %ld\n", usage_end.ru_nvcsw - usage_start.ru_nvcsw); printf(" involuntary csw:%ld\n", usage_end.ru_nivcsw - usage_start.ru_nivcsw); - if (trace_enabled) { rpc_trace_dump(stdout); } + if (trace_enabled) { wirecall_trace_dump(stdout); } pthread_cond_destroy(&gate.cond); pthread_mutex_destroy(&gate.mutex); diff --git a/demos/client.c b/demos/client.c index 6efbfbd..dd6582c 100644 --- a/demos/client.c +++ b/demos/client.c @@ -1,4 +1,4 @@ -#include "rpc/client.h" +#include "wirecall/client.h" #include #include @@ -12,34 +12,34 @@ int main(int argc, char **argv) { int64_t a = argc > 3 ? strtoll(argv[3], NULL, 10) : 20; int64_t b = argc > 4 ? strtoll(argv[4], NULL, 10) : 22; - rpc_client *client = NULL; - if (rpc_client_connect(&client, host, port) != 0) { + wirecall_client *client = NULL; + if (wirecall_client_connect(&client, host, port) != 0) { fprintf(stderr, "connect failed\n"); return 1; } - rpc_writer args; - rpc_writer_init(&args); - rpc_writer_i64(&args, a); - rpc_writer_i64(&args, b); + wirecall_writer args; + wirecall_writer_init(&args); + wirecall_writer_i64(&args, a); + wirecall_writer_i64(&args, b); - rpc_value *result = NULL; + wirecall_value *result = NULL; size_t result_count = 0; - if (rpc_client_call_name(client, "add", &args, &result, &result_count) != 0) { - fprintf(stderr, "RPC error: %s\n", rpc_client_error(client)); - rpc_writer_free(&args); - rpc_client_close(client); + if (wirecall_client_call_name(client, "add", &args, &result, &result_count) != 0) { + fprintf(stderr, "Wirecall error: %s\n", wirecall_client_error(client)); + wirecall_writer_free(&args); + wirecall_client_close(client); return 1; } - if (result_count == 1 && result[0].type == RPC_TYPE_I64) + if (result_count == 1 && result[0].type == WIRECALL_TYPE_I64) printf("%lld + %lld = %lld\n", (longer)a, (longer)b, (longer)result[0].as.i64); else fprintf(stderr, "unexpected response\n"); - rpc_values_free(result); - rpc_writer_free(&args); - rpc_client_close(client); + wirecall_values_free(result); + wirecall_writer_free(&args); + wirecall_client_close(client); return 0; } diff --git a/demos/embed.c b/demos/embed.c index c73ef16..fa6718d 100644 --- a/demos/embed.c +++ b/demos/embed.c @@ -1,5 +1,5 @@ -#include "rpc/client.h" -#include "rpc/server.h" +#include "wirecall/client.h" +#include "wirecall/server.h" #include #include @@ -19,7 +19,7 @@ typedef struct embed_state { } embed_state; typedef struct embed_server { - rpc_server *rpc; + wirecall_server *rpc; pthread_t thread; } embed_server; @@ -45,9 +45,9 @@ static embed_state *state_new(embed_alloc_stats *stats, const char *name, int64_ embed_state *state = embed_alloc(stats, sizeof(*state)); if (!state) return NULL; *state = (embed_state){ - .name = name, - .bias = bias, - .stats = stats, + .name = name, + .bias = bias, + .stats = stats, }; return state; } @@ -59,29 +59,30 @@ static void state_finalizer(void *user_data) { embed_free(state->stats, state); } -static int add_with_bias(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int add_with_bias(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)ctx; embed_state *state = user_data; - if (!state || argc != 2 || args[0].type != RPC_TYPE_I64 || args[1].type != RPC_TYPE_I64) return -1; - return rpc_writer_i64(out, args[0].as.i64 + args[1].as.i64 + state->bias); + if (!state || argc != 2 || args[0].type != WIRECALL_TYPE_I64 || args[1].type != WIRECALL_TYPE_I64) return -1; + return wirecall_writer_i64(out, args[0].as.i64 + args[1].as.i64 + state->bias); } static void *server_main(void *arg) { embed_server *server = arg; - (void)rpc_server_run(server->rpc); + (void)wirecall_server_run(server->rpc); return NULL; } int main(void) { embed_alloc_stats stats = {0}; - rpc_allocator allocator = { - .ctx = &stats, - .alloc = embed_alloc, - .realloc = embed_realloc, - .free = embed_free, + wirecall_allocator allocator = { + .ctx = &stats, + .alloc = embed_alloc, + .realloc = embed_realloc, + .free = embed_free, }; - if (rpc_set_allocator(&allocator) != 0) { + if (wirecall_set_allocator(&allocator) != 0) { fprintf(stderr, "allocator install failed\n"); return 1; } @@ -93,49 +94,49 @@ int main(void) { fprintf(stderr, "state allocation failed\n"); state_finalizer(first); state_finalizer(second); - rpc_set_allocator(NULL); + wirecall_set_allocator(NULL); return 1; } - if (rpc_server_init(&server.rpc) != 0 || rpc_server_set_workers(server.rpc, 1) != 0 || - rpc_server_add_route_name_ex(server.rpc, "add", add_with_bias, first, state_finalizer) != 0 || - rpc_server_add_route_name_ex(server.rpc, "add", add_with_bias, second, state_finalizer) != 0 || - rpc_server_bind(server.rpc, "127.0.0.1", "0") != 0 || rpc_server_listen(server.rpc) != 0) { + if (wirecall_server_init(&server.rpc) != 0 || wirecall_server_set_workers(server.rpc, 1) != 0 || + wirecall_server_add_route_name_ex(server.rpc, "add", add_with_bias, first, state_finalizer) != 0 || + wirecall_server_add_route_name_ex(server.rpc, "add", add_with_bias, second, state_finalizer) != 0 || + wirecall_server_bind(server.rpc, "127.0.0.1", "0") != 0 || wirecall_server_listen(server.rpc) != 0) { fprintf(stderr, "server setup failed\n"); - rpc_server_destroy(server.rpc); - rpc_set_allocator(NULL); + wirecall_server_destroy(server.rpc); + wirecall_set_allocator(NULL); return 1; } char port[16]; - snprintf(port, sizeof(port), "%u", rpc_server_port(server.rpc)); + snprintf(port, sizeof(port), "%u", wirecall_server_port(server.rpc)); if (pthread_create(&server.thread, NULL, server_main, &server) != 0) { fprintf(stderr, "server thread failed\n"); - rpc_server_destroy(server.rpc); - rpc_set_allocator(NULL); + wirecall_server_destroy(server.rpc); + wirecall_set_allocator(NULL); return 1; } - rpc_client *client = NULL; - rpc_writer args; - rpc_writer_init(&args); - rpc_value *values = NULL; + wirecall_client *client = NULL; + wirecall_writer args; + wirecall_writer_init(&args); + wirecall_value *values = NULL; size_t value_count = 0; int rc = 1; - if (rpc_client_connect(&client, "127.0.0.1", port) != 0) { + if (wirecall_client_connect(&client, "127.0.0.1", port) != 0) { fprintf(stderr, "connect failed\n"); goto done; } - if (rpc_writer_i64(&args, 20) != 0 || rpc_writer_i64(&args, 22) != 0) { + if (wirecall_writer_i64(&args, 20) != 0 || wirecall_writer_i64(&args, 22) != 0) { fprintf(stderr, "argument encode failed\n"); goto done; } - if (rpc_client_call_name(client, "add", &args, &values, &value_count) != 0) { - fprintf(stderr, "call failed: %s\n", rpc_client_error(client)); + if (wirecall_client_call_name(client, "add", &args, &values, &value_count) != 0) { + fprintf(stderr, "call failed: %s\n", wirecall_client_error(client)); goto done; } - if (value_count != 1 || values[0].type != RPC_TYPE_I64) { + if (value_count != 1 || values[0].type != WIRECALL_TYPE_I64) { fprintf(stderr, "unexpected response\n"); goto done; } @@ -144,16 +145,16 @@ int main(void) { rc = 0; done: - rpc_values_free(values); - rpc_writer_free(&args); - rpc_client_close(client); + wirecall_values_free(values); + wirecall_writer_free(&args); + wirecall_client_close(client); - (void)rpc_server_remove_route_name(server.rpc, "add"); - rpc_server_stop(server.rpc); + (void)wirecall_server_remove_route_name(server.rpc, "add"); + wirecall_server_stop(server.rpc); pthread_join(server.thread, NULL); - rpc_server_destroy(server.rpc); + wirecall_server_destroy(server.rpc); printf("allocator stats: alloc=%zu realloc=%zu free=%zu\n", stats.allocs, stats.reallocs, stats.frees); - rpc_set_allocator(NULL); + wirecall_set_allocator(NULL); return rc; } diff --git a/demos/server.c b/demos/server.c index 488374b..af01459 100644 --- a/demos/server.c +++ b/demos/server.c @@ -1,29 +1,30 @@ -#include "rpc/server.h" +#include "wirecall/server.h" #include #include #include #include -static rpc_server *g_server; +static wirecall_server *g_server; static void on_signal(int signo) { (void)signo; - if (g_server) { rpc_server_stop(g_server); } + if (g_server) { wirecall_server_stop(g_server); } } -static int add_i64(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int add_i64(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, void *user_data) { (void)ctx; (void)user_data; - if (argc != 2 || args[0].type != RPC_TYPE_I64 || args[1].type != RPC_TYPE_I64) { return -1; } - return rpc_writer_i64(out, args[0].as.i64 + args[1].as.i64); + if (argc != 2 || args[0].type != WIRECALL_TYPE_I64 || args[1].type != WIRECALL_TYPE_I64) { return -1; } + return wirecall_writer_i64(out, args[0].as.i64 + args[1].as.i64); } -static int echo_string(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int echo_string(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)user_data; - if (argc != 1 || args[0].type != RPC_TYPE_STRING) { return -1; } - rpc_ctx_yield(ctx); - return rpc_writer_string(out, args[0].as.string.data, args[0].as.string.len); + if (argc != 1 || args[0].type != WIRECALL_TYPE_STRING) { return -1; } + wirecall_ctx_yield(ctx); + return wirecall_writer_string(out, args[0].as.string.data, args[0].as.string.len); } int main(int argc, char **argv) { @@ -31,19 +32,19 @@ int main(int argc, char **argv) { const char *port = argc > 2 ? argv[2] : "7000"; uint32_t workers = argc > 3 ? (uint32_t)strtoul(argv[3], NULL, 10) : 0; - if (rpc_server_init(&g_server) != 0 || (workers != 0 && rpc_server_set_workers(g_server, workers) != 0) || - rpc_server_add_route_name(g_server, "add", add_i64, NULL) != 0 || - rpc_server_add_async_route_name(g_server, "echo", echo_string, NULL) != 0 || rpc_server_bind(g_server, host, port) != 0 || - rpc_server_listen(g_server) != 0) { - fprintf(stderr, "failed to start RPC server\n"); - rpc_server_destroy(g_server); + if (wirecall_server_init(&g_server) != 0 || (workers != 0 && wirecall_server_set_workers(g_server, workers) != 0) || + wirecall_server_add_route_name(g_server, "add", add_i64, NULL) != 0 || + wirecall_server_add_async_route_name(g_server, "echo", echo_string, NULL) != 0 || + wirecall_server_bind(g_server, host, port) != 0 || wirecall_server_listen(g_server) != 0) { + fprintf(stderr, "failed to start Wirecall server\n"); + wirecall_server_destroy(g_server); return 1; } signal(SIGINT, on_signal); signal(SIGTERM, on_signal); - printf("rpc demo server listening on %s:%u\n", host, rpc_server_port(g_server)); - int rc = rpc_server_run(g_server); - rpc_server_destroy(g_server); + printf("rpc demo server listening on %s:%u\n", host, wirecall_server_port(g_server)); + int rc = wirecall_server_run(g_server); + wirecall_server_destroy(g_server); return rc == 0 ? 0 : 1; } diff --git a/include/arena.h b/include/arena.h index 54e93a2..d9e66b9 100644 --- a/include/arena.h +++ b/include/arena.h @@ -1,28 +1,28 @@ -#ifndef RPC_ARENA_H -#define RPC_ARENA_H +#ifndef WIRECALL_ARENA_H +#define WIRECALL_ARENA_H #include #include #include #include -typedef struct rpc_fixed_arena { +typedef struct wirecall_fixed_arena { uint8_t *base; size_t elem_size; size_t capacity; size_t watermark; size_t bytes; void *free_list; -} rpc_fixed_arena; +} wirecall_fixed_arena; -static inline size_t rpc_arena_align(size_t n) { +static inline size_t wirecall_arena_align(size_t n) { size_t a = sizeof(void *); return (n + a - 1u) & ~(a - 1u); } -static inline int rpc_fixed_arena_init(rpc_fixed_arena *arena, size_t elem_size, size_t capacity) { +static inline int wirecall_fixed_arena_init(wirecall_fixed_arena *arena, size_t elem_size, size_t capacity) { memset(arena, 0, sizeof(*arena)); - arena->elem_size = rpc_arena_align(elem_size < sizeof(void *) ? sizeof(void *) : elem_size); + arena->elem_size = wirecall_arena_align(elem_size < sizeof(void *) ? sizeof(void *) : elem_size); arena->capacity = capacity; arena->bytes = arena->elem_size * capacity; arena->base = mmap(NULL, arena->bytes, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); @@ -33,12 +33,12 @@ static inline int rpc_fixed_arena_init(rpc_fixed_arena *arena, size_t elem_size, return 0; } -static inline void rpc_fixed_arena_destroy(rpc_fixed_arena *arena) { +static inline void wirecall_fixed_arena_destroy(wirecall_fixed_arena *arena) { if (arena->base) { munmap(arena->base, arena->bytes); } memset(arena, 0, sizeof(*arena)); } -static inline void *rpc_fixed_arena_alloc(rpc_fixed_arena *arena) { +static inline void *wirecall_fixed_arena_alloc(wirecall_fixed_arena *arena) { void *p = NULL; if (arena->free_list) { p = arena->free_list; @@ -50,7 +50,7 @@ static inline void *rpc_fixed_arena_alloc(rpc_fixed_arena *arena) { return p; } -static inline void rpc_fixed_arena_free(rpc_fixed_arena *arena, void *p) { +static inline void wirecall_fixed_arena_free(wirecall_fixed_arena *arena, void *p) { if (!p) { return; } *(void **)p = arena->free_list; arena->free_list = p; diff --git a/include/backend.h b/include/backend.h index fec9248..0db0e1e 100644 --- a/include/backend.h +++ b/include/backend.h @@ -1,28 +1,28 @@ -#ifndef RPC_BACKEND_H -#define RPC_BACKEND_H +#ifndef WIRECALL_BACKEND_H +#define WIRECALL_BACKEND_H #include enum { - RPC_BACKEND_READ = 1u << 0u, - RPC_BACKEND_WRITE = 1u << 1u, - RPC_BACKEND_WAKE = 1u << 2u, + WIRECALL_BACKEND_READ = 1u << 0u, + WIRECALL_BACKEND_WRITE = 1u << 1u, + WIRECALL_BACKEND_WAKE = 1u << 2u, }; -typedef struct rpc_backend_event { +typedef struct wirecall_backend_event { int fd; uint32_t events; uintptr_t user; -} rpc_backend_event; +} wirecall_backend_event; -typedef struct rpc_backend rpc_backend; +typedef struct wirecall_backend wirecall_backend; -int rpc_backend_create(rpc_backend **out); -void rpc_backend_destroy(rpc_backend *backend); -int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_t user); -int rpc_backend_modify(rpc_backend *backend, int fd, uint32_t events, uintptr_t user); -int rpc_backend_remove(rpc_backend *backend, int fd); -int rpc_backend_wake(rpc_backend *backend); -int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_events, int timeout_ms); +int wirecall_backend_create(wirecall_backend **out); +void wirecall_backend_destroy(wirecall_backend *backend); +int wirecall_backend_register(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user); +int wirecall_backend_modify(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user); +int wirecall_backend_remove(wirecall_backend *backend, int fd); +int wirecall_backend_wake(wirecall_backend *backend); +int wirecall_backend_poll(wirecall_backend *backend, wirecall_backend_event *events, int max_events, int timeout_ms); #endif diff --git a/include/memory.h b/include/memory.h index d6cd54d..9650505 100644 --- a/include/memory.h +++ b/include/memory.h @@ -1,11 +1,11 @@ -#ifndef RPC_MEMORY_H -#define RPC_MEMORY_H +#ifndef WIRECALL_MEMORY_H +#define WIRECALL_MEMORY_H #include -void *rpc_mem_alloc(size_t size); -void *rpc_mem_calloc(size_t count, size_t size); -void *rpc_mem_realloc(void *ptr, size_t size); -void rpc_mem_free(void *ptr); +void *wirecall_mem_alloc(size_t size); +void *wirecall_mem_calloc(size_t count, size_t size); +void *wirecall_mem_realloc(void *ptr, size_t size); +void wirecall_mem_free(void *ptr); #endif diff --git a/include/minicoro.h b/include/minicoro.h index 17fc10f..c8266dc 100644 --- a/include/minicoro.h +++ b/include/minicoro.h @@ -431,7 +431,7 @@ extern "C" { #else #if __GNUC__ >= 3 /* Assembly extension supported. */ #if defined(__x86_64__) || defined(__i386) || defined(__i386__) || defined(__ARM_EABI__) || defined(__aarch64__) || \ - defined(__riscv) + defined(__riscv) #define MCO_USE_ASM #else #define MCO_USE_UCONTEXT @@ -495,7 +495,7 @@ extern "C" { #define MCO_FORCE_INLINE inline __attribute__((always_inline)) #endif #elif defined(__BORLANDC__) || defined(__DMC__) || defined(__SC__) || defined(__WATCOMC__) || defined(__LCC__) || \ - defined(__DECC) + defined(__DECC) #define MCO_FORCE_INLINE __inline #else /* No inline support. */ #define MCO_FORCE_INLINE @@ -718,74 +718,74 @@ typedef struct _mco_ctxbuf { #endif _MCO_ASM_BLOB static unsigned char _mco_wrap_main_code[] = { - 0x4c, 0x89, 0xe9, /* mov %r13,%rcx */ - 0x41, 0xff, 0xe4, /* jmpq *%r12 */ - 0xc3, /* retq */ - 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 /* nop */ + 0x4c, 0x89, 0xe9, /* mov %r13,%rcx */ + 0x41, 0xff, 0xe4, /* jmpq *%r12 */ + 0xc3, /* retq */ + 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 /* nop */ }; _MCO_ASM_BLOB static unsigned char _mco_switch_code[] = { - 0x48, 0x8d, 0x05, 0x3e, 0x01, 0x00, 0x00, /* lea 0x13e(%rip),%rax */ - 0x48, 0x89, 0x01, /* mov %rax,(%rcx) */ - 0x48, 0x89, 0x61, 0x08, /* mov %rsp,0x8(%rcx) */ - 0x48, 0x89, 0x69, 0x10, /* mov %rbp,0x10(%rcx) */ - 0x48, 0x89, 0x59, 0x18, /* mov %rbx,0x18(%rcx) */ - 0x4c, 0x89, 0x61, 0x20, /* mov %r12,0x20(%rcx) */ - 0x4c, 0x89, 0x69, 0x28, /* mov %r13,0x28(%rcx) */ - 0x4c, 0x89, 0x71, 0x30, /* mov %r14,0x30(%rcx) */ - 0x4c, 0x89, 0x79, 0x38, /* mov %r15,0x38(%rcx) */ - 0x48, 0x89, 0x79, 0x40, /* mov %rdi,0x40(%rcx) */ - 0x48, 0x89, 0x71, 0x48, /* mov %rsi,0x48(%rcx) */ - 0x0f, 0x11, 0x71, 0x50, /* movups %xmm6,0x50(%rcx) */ - 0x0f, 0x11, 0x79, 0x60, /* movups %xmm7,0x60(%rcx) */ - 0x44, 0x0f, 0x11, 0x41, 0x70, /* movups %xmm8,0x70(%rcx) */ - 0x44, 0x0f, 0x11, 0x89, 0x80, 0x00, 0x00, 0x00, /* movups %xmm9,0x80(%rcx) */ - 0x44, 0x0f, 0x11, 0x91, 0x90, 0x00, 0x00, 0x00, /* movups %xmm10,0x90(%rcx) */ - 0x44, 0x0f, 0x11, 0x99, 0xa0, 0x00, 0x00, 0x00, /* movups %xmm11,0xa0(%rcx) */ - 0x44, 0x0f, 0x11, 0xa1, 0xb0, 0x00, 0x00, 0x00, /* movups %xmm12,0xb0(%rcx) */ - 0x44, 0x0f, 0x11, 0xa9, 0xc0, 0x00, 0x00, 0x00, /* movups %xmm13,0xc0(%rcx) */ - 0x44, 0x0f, 0x11, 0xb1, 0xd0, 0x00, 0x00, 0x00, /* movups %xmm14,0xd0(%rcx) */ - 0x44, 0x0f, 0x11, 0xb9, 0xe0, 0x00, 0x00, 0x00, /* movups %xmm15,0xe0(%rcx) */ - 0x65, 0x4c, 0x8b, 0x14, 0x25, 0x30, 0x00, 0x00, 0x00, /* mov %gs:0x30,%r10 */ - 0x49, 0x8b, 0x42, 0x20, /* mov 0x20(%r10),%rax */ - 0x48, 0x89, 0x81, 0xf0, 0x00, 0x00, 0x00, /* mov %rax,0xf0(%rcx) */ - 0x49, 0x8b, 0x82, 0x78, 0x14, 0x00, 0x00, /* mov 0x1478(%r10),%rax */ - 0x48, 0x89, 0x81, 0xf8, 0x00, 0x00, 0x00, /* mov %rax,0xf8(%rcx) */ - 0x49, 0x8b, 0x42, 0x10, /* mov 0x10(%r10),%rax */ - 0x48, 0x89, 0x81, 0x00, 0x01, 0x00, 0x00, /* mov %rax,0x100(%rcx) */ - 0x49, 0x8b, 0x42, 0x08, /* mov 0x8(%r10),%rax */ - 0x48, 0x89, 0x81, 0x08, 0x01, 0x00, 0x00, /* mov %rax,0x108(%rcx) */ - 0x48, 0x8b, 0x82, 0x08, 0x01, 0x00, 0x00, /* mov 0x108(%rdx),%rax */ - 0x49, 0x89, 0x42, 0x08, /* mov %rax,0x8(%r10) */ - 0x48, 0x8b, 0x82, 0x00, 0x01, 0x00, 0x00, /* mov 0x100(%rdx),%rax */ - 0x49, 0x89, 0x42, 0x10, /* mov %rax,0x10(%r10) */ - 0x48, 0x8b, 0x82, 0xf8, 0x00, 0x00, 0x00, /* mov 0xf8(%rdx),%rax */ - 0x49, 0x89, 0x82, 0x78, 0x14, 0x00, 0x00, /* mov %rax,0x1478(%r10) */ - 0x48, 0x8b, 0x82, 0xf0, 0x00, 0x00, 0x00, /* mov 0xf0(%rdx),%rax */ - 0x49, 0x89, 0x42, 0x20, /* mov %rax,0x20(%r10) */ - 0x44, 0x0f, 0x10, 0xba, 0xe0, 0x00, 0x00, 0x00, /* movups 0xe0(%rdx),%xmm15 */ - 0x44, 0x0f, 0x10, 0xb2, 0xd0, 0x00, 0x00, 0x00, /* movups 0xd0(%rdx),%xmm14 */ - 0x44, 0x0f, 0x10, 0xaa, 0xc0, 0x00, 0x00, 0x00, /* movups 0xc0(%rdx),%xmm13 */ - 0x44, 0x0f, 0x10, 0xa2, 0xb0, 0x00, 0x00, 0x00, /* movups 0xb0(%rdx),%xmm12 */ - 0x44, 0x0f, 0x10, 0x9a, 0xa0, 0x00, 0x00, 0x00, /* movups 0xa0(%rdx),%xmm11 */ - 0x44, 0x0f, 0x10, 0x92, 0x90, 0x00, 0x00, 0x00, /* movups 0x90(%rdx),%xmm10 */ - 0x44, 0x0f, 0x10, 0x8a, 0x80, 0x00, 0x00, 0x00, /* movups 0x80(%rdx),%xmm9 */ - 0x44, 0x0f, 0x10, 0x42, 0x70, /* movups 0x70(%rdx),%xmm8 */ - 0x0f, 0x10, 0x7a, 0x60, /* movups 0x60(%rdx),%xmm7 */ - 0x0f, 0x10, 0x72, 0x50, /* movups 0x50(%rdx),%xmm6 */ - 0x48, 0x8b, 0x72, 0x48, /* mov 0x48(%rdx),%rsi */ - 0x48, 0x8b, 0x7a, 0x40, /* mov 0x40(%rdx),%rdi */ - 0x4c, 0x8b, 0x7a, 0x38, /* mov 0x38(%rdx),%r15 */ - 0x4c, 0x8b, 0x72, 0x30, /* mov 0x30(%rdx),%r14 */ - 0x4c, 0x8b, 0x6a, 0x28, /* mov 0x28(%rdx),%r13 */ - 0x4c, 0x8b, 0x62, 0x20, /* mov 0x20(%rdx),%r12 */ - 0x48, 0x8b, 0x5a, 0x18, /* mov 0x18(%rdx),%rbx */ - 0x48, 0x8b, 0x6a, 0x10, /* mov 0x10(%rdx),%rbp */ - 0x48, 0x8b, 0x62, 0x08, /* mov 0x8(%rdx),%rsp */ - 0xff, 0x22, /* jmpq *(%rdx) */ - 0xc3, /* retq */ - 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, /* nop */ - 0x90, 0x90, /* nop */ + 0x48, 0x8d, 0x05, 0x3e, 0x01, 0x00, 0x00, /* lea 0x13e(%rip),%rax */ + 0x48, 0x89, 0x01, /* mov %rax,(%rcx) */ + 0x48, 0x89, 0x61, 0x08, /* mov %rsp,0x8(%rcx) */ + 0x48, 0x89, 0x69, 0x10, /* mov %rbp,0x10(%rcx) */ + 0x48, 0x89, 0x59, 0x18, /* mov %rbx,0x18(%rcx) */ + 0x4c, 0x89, 0x61, 0x20, /* mov %r12,0x20(%rcx) */ + 0x4c, 0x89, 0x69, 0x28, /* mov %r13,0x28(%rcx) */ + 0x4c, 0x89, 0x71, 0x30, /* mov %r14,0x30(%rcx) */ + 0x4c, 0x89, 0x79, 0x38, /* mov %r15,0x38(%rcx) */ + 0x48, 0x89, 0x79, 0x40, /* mov %rdi,0x40(%rcx) */ + 0x48, 0x89, 0x71, 0x48, /* mov %rsi,0x48(%rcx) */ + 0x0f, 0x11, 0x71, 0x50, /* movups %xmm6,0x50(%rcx) */ + 0x0f, 0x11, 0x79, 0x60, /* movups %xmm7,0x60(%rcx) */ + 0x44, 0x0f, 0x11, 0x41, 0x70, /* movups %xmm8,0x70(%rcx) */ + 0x44, 0x0f, 0x11, 0x89, 0x80, 0x00, 0x00, 0x00, /* movups %xmm9,0x80(%rcx) */ + 0x44, 0x0f, 0x11, 0x91, 0x90, 0x00, 0x00, 0x00, /* movups %xmm10,0x90(%rcx) */ + 0x44, 0x0f, 0x11, 0x99, 0xa0, 0x00, 0x00, 0x00, /* movups %xmm11,0xa0(%rcx) */ + 0x44, 0x0f, 0x11, 0xa1, 0xb0, 0x00, 0x00, 0x00, /* movups %xmm12,0xb0(%rcx) */ + 0x44, 0x0f, 0x11, 0xa9, 0xc0, 0x00, 0x00, 0x00, /* movups %xmm13,0xc0(%rcx) */ + 0x44, 0x0f, 0x11, 0xb1, 0xd0, 0x00, 0x00, 0x00, /* movups %xmm14,0xd0(%rcx) */ + 0x44, 0x0f, 0x11, 0xb9, 0xe0, 0x00, 0x00, 0x00, /* movups %xmm15,0xe0(%rcx) */ + 0x65, 0x4c, 0x8b, 0x14, 0x25, 0x30, 0x00, 0x00, 0x00, /* mov %gs:0x30,%r10 */ + 0x49, 0x8b, 0x42, 0x20, /* mov 0x20(%r10),%rax */ + 0x48, 0x89, 0x81, 0xf0, 0x00, 0x00, 0x00, /* mov %rax,0xf0(%rcx) */ + 0x49, 0x8b, 0x82, 0x78, 0x14, 0x00, 0x00, /* mov 0x1478(%r10),%rax */ + 0x48, 0x89, 0x81, 0xf8, 0x00, 0x00, 0x00, /* mov %rax,0xf8(%rcx) */ + 0x49, 0x8b, 0x42, 0x10, /* mov 0x10(%r10),%rax */ + 0x48, 0x89, 0x81, 0x00, 0x01, 0x00, 0x00, /* mov %rax,0x100(%rcx) */ + 0x49, 0x8b, 0x42, 0x08, /* mov 0x8(%r10),%rax */ + 0x48, 0x89, 0x81, 0x08, 0x01, 0x00, 0x00, /* mov %rax,0x108(%rcx) */ + 0x48, 0x8b, 0x82, 0x08, 0x01, 0x00, 0x00, /* mov 0x108(%rdx),%rax */ + 0x49, 0x89, 0x42, 0x08, /* mov %rax,0x8(%r10) */ + 0x48, 0x8b, 0x82, 0x00, 0x01, 0x00, 0x00, /* mov 0x100(%rdx),%rax */ + 0x49, 0x89, 0x42, 0x10, /* mov %rax,0x10(%r10) */ + 0x48, 0x8b, 0x82, 0xf8, 0x00, 0x00, 0x00, /* mov 0xf8(%rdx),%rax */ + 0x49, 0x89, 0x82, 0x78, 0x14, 0x00, 0x00, /* mov %rax,0x1478(%r10) */ + 0x48, 0x8b, 0x82, 0xf0, 0x00, 0x00, 0x00, /* mov 0xf0(%rdx),%rax */ + 0x49, 0x89, 0x42, 0x20, /* mov %rax,0x20(%r10) */ + 0x44, 0x0f, 0x10, 0xba, 0xe0, 0x00, 0x00, 0x00, /* movups 0xe0(%rdx),%xmm15 */ + 0x44, 0x0f, 0x10, 0xb2, 0xd0, 0x00, 0x00, 0x00, /* movups 0xd0(%rdx),%xmm14 */ + 0x44, 0x0f, 0x10, 0xaa, 0xc0, 0x00, 0x00, 0x00, /* movups 0xc0(%rdx),%xmm13 */ + 0x44, 0x0f, 0x10, 0xa2, 0xb0, 0x00, 0x00, 0x00, /* movups 0xb0(%rdx),%xmm12 */ + 0x44, 0x0f, 0x10, 0x9a, 0xa0, 0x00, 0x00, 0x00, /* movups 0xa0(%rdx),%xmm11 */ + 0x44, 0x0f, 0x10, 0x92, 0x90, 0x00, 0x00, 0x00, /* movups 0x90(%rdx),%xmm10 */ + 0x44, 0x0f, 0x10, 0x8a, 0x80, 0x00, 0x00, 0x00, /* movups 0x80(%rdx),%xmm9 */ + 0x44, 0x0f, 0x10, 0x42, 0x70, /* movups 0x70(%rdx),%xmm8 */ + 0x0f, 0x10, 0x7a, 0x60, /* movups 0x60(%rdx),%xmm7 */ + 0x0f, 0x10, 0x72, 0x50, /* movups 0x50(%rdx),%xmm6 */ + 0x48, 0x8b, 0x72, 0x48, /* mov 0x48(%rdx),%rsi */ + 0x48, 0x8b, 0x7a, 0x40, /* mov 0x40(%rdx),%rdi */ + 0x4c, 0x8b, 0x7a, 0x38, /* mov 0x38(%rdx),%r15 */ + 0x4c, 0x8b, 0x72, 0x30, /* mov 0x30(%rdx),%r14 */ + 0x4c, 0x8b, 0x6a, 0x28, /* mov 0x28(%rdx),%r13 */ + 0x4c, 0x8b, 0x62, 0x20, /* mov 0x20(%rdx),%r12 */ + 0x48, 0x8b, 0x5a, 0x18, /* mov 0x18(%rdx),%rbx */ + 0x48, 0x8b, 0x6a, 0x10, /* mov 0x10(%rdx),%rbp */ + 0x48, 0x8b, 0x62, 0x08, /* mov 0x8(%rdx),%rsp */ + 0xff, 0x22, /* jmpq *(%rdx) */ + 0xc3, /* retq */ + 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, /* nop */ + 0x90, 0x90, /* nop */ }; void (*_mco_wrap_main)(void) = (void (*)(void))(void *)_mco_wrap_main_code; @@ -1089,36 +1089,36 @@ void _mco_switch(_mco_ctxbuf *from, _mco_ctxbuf *to); __asm__( #ifdef __DJGPP__ /* DOS compiler */ - "__mco_switch:\n" + "__mco_switch:\n" #else - ".text\n" - ".globl _mco_switch\n" - ".type _mco_switch @function\n" - ".hidden _mco_switch\n" - "_mco_switch:\n" -#endif - " call 1f\n" - " 1:\n" - " popl %ecx\n" - " addl $(2f-1b), %ecx\n" - " movl 4(%esp), %eax\n" - " movl 8(%esp), %edx\n" - " movl %ecx, (%eax)\n" - " movl %esp, 4(%eax)\n" - " movl %ebp, 8(%eax)\n" - " movl %ebx, 12(%eax)\n" - " movl %esi, 16(%eax)\n" - " movl %edi, 20(%eax)\n" - " movl 20(%edx), %edi\n" - " movl 16(%edx), %esi\n" - " movl 12(%edx), %ebx\n" - " movl 8(%edx), %ebp\n" - " movl 4(%edx), %esp\n" - " jmp *(%edx)\n" - " 2:\n" - " ret\n" + ".text\n" + ".globl _mco_switch\n" + ".type _mco_switch @function\n" + ".hidden _mco_switch\n" + "_mco_switch:\n" +#endif + " call 1f\n" + " 1:\n" + " popl %ecx\n" + " addl $(2f-1b), %ecx\n" + " movl 4(%esp), %eax\n" + " movl 8(%esp), %edx\n" + " movl %ecx, (%eax)\n" + " movl %esp, 4(%eax)\n" + " movl %ebp, 8(%eax)\n" + " movl %ebx, 12(%eax)\n" + " movl %esi, 16(%eax)\n" + " movl %edi, 20(%eax)\n" + " movl 20(%edx), %edi\n" + " movl 16(%edx), %esi\n" + " movl 12(%edx), %ebx\n" + " movl 8(%edx), %ebp\n" + " movl 4(%edx), %esp\n" + " jmp *(%edx)\n" + " 2:\n" + " ret\n" #ifndef __DJGPP__ - ".size _mco_switch, .-_mco_switch\n" + ".size _mco_switch, .-_mco_switch\n" #endif ); @@ -1467,7 +1467,7 @@ static mco_result _mco_create_context(mco_coro *co, mco_desc *desc) { unsigned char *storage = (unsigned char *)storage_addr; /* Create the fiber. */ _mco_fiber *fib = - (_mco_fiber *)CreateFiberEx(desc->stack_size, desc->stack_size, FIBER_FLAG_FLOAT_SWITCH, _mco_wrap_main, co); + (_mco_fiber *)CreateFiberEx(desc->stack_size, desc->stack_size, FIBER_FLAG_FLOAT_SWITCH, _mco_wrap_main, co); if (!fib) { MCO_LOG("failed to create fiber"); return MCO_MAKE_CONTEXT_ERROR; diff --git a/include/proc.h b/include/proc.h index 9e98e4a..d0f4d82 100644 --- a/include/proc.h +++ b/include/proc.h @@ -1,8 +1,8 @@ -#ifndef RPC_PROC_H -#define RPC_PROC_H +#ifndef WIRECALL_PROC_H +#define WIRECALL_PROC_H #include -uint64_t rpc_proc_id(const char *name); +uint64_t wirecall_proc_id(const char *name); #endif diff --git a/include/routes.h b/include/routes.h index 00d3d70..2d0fabc 100644 --- a/include/routes.h +++ b/include/routes.h @@ -1,50 +1,50 @@ -#ifndef RPC_ROUTES_H -#define RPC_ROUTES_H +#ifndef WIRECALL_ROUTES_H +#define WIRECALL_ROUTES_H -#include "rpc/server.h" +#include "wirecall/server.h" #include #include -#define RPC_ROUTE_PAGE_BITS 16u -#define RPC_ROUTE_PAGE_SIZE (1u << RPC_ROUTE_PAGE_BITS) -#define RPC_ROUTE_PAGE_MASK (RPC_ROUTE_PAGE_SIZE - 1u) -#define RPC_ROUTE_ROOT_SIZE (1u << (32u - RPC_ROUTE_PAGE_BITS)) +#define WIRECALL_ROUTE_PAGE_BITS 16u +#define WIRECALL_ROUTE_PAGE_SIZE (1u << WIRECALL_ROUTE_PAGE_BITS) +#define WIRECALL_ROUTE_PAGE_MASK (WIRECALL_ROUTE_PAGE_SIZE - 1u) +#define WIRECALL_ROUTE_ROOT_SIZE (1u << (32u - WIRECALL_ROUTE_PAGE_BITS)) -typedef struct rpc_route { +typedef struct wirecall_route { uint64_t proc_id; - rpc_handler_fn handler; + wirecall_handler_fn handler; void *user_data; - rpc_route_finalizer_fn finalizer; + wirecall_route_finalizer_fn finalizer; int is_async; - struct rpc_route *next; -} rpc_route; + struct wirecall_route *next; +} wirecall_route; -typedef struct rpc_retired_route { - rpc_route *route; +typedef struct wirecall_retired_route { + wirecall_route *route; uint64_t finalize_proc_id; int finalize_all; int finalize_one; - struct rpc_retired_route *next; -} rpc_retired_route; + struct wirecall_retired_route *next; +} wirecall_retired_route; -typedef _Atomic(rpc_route *) rpc_route_slot; +typedef _Atomic(wirecall_route *) wirecall_route_slot; -typedef struct rpc_routes { - _Atomic(rpc_route_slot *) *pages; +typedef struct wirecall_routes { + _Atomic(wirecall_route_slot *) *pages; size_t root_bytes; size_t page_bytes; atomic_uint active_readers; pthread_mutex_t mutate_lock; - rpc_retired_route *retired; -} rpc_routes; - -int rpc_routes_init(rpc_routes *routes); -void rpc_routes_destroy(rpc_routes *routes); -int rpc_routes_add(rpc_routes *routes, uint64_t proc_id, rpc_handler_fn handler, void *user_data); -int rpc_routes_add_ex(rpc_routes *routes, uint64_t proc_id, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer, int is_async); -int rpc_routes_remove(rpc_routes *routes, uint64_t proc_id); -int rpc_routes_lookup(rpc_routes *routes, uint64_t proc_id, rpc_route *out); + wirecall_retired_route *retired; +} wirecall_routes; + +int wirecall_routes_init(wirecall_routes *routes); +void wirecall_routes_destroy(wirecall_routes *routes); +int wirecall_routes_add(wirecall_routes *routes, uint64_t proc_id, wirecall_handler_fn handler, void *user_data); +int wirecall_routes_add_ex(wirecall_routes *routes, uint64_t proc_id, wirecall_handler_fn handler, void *user_data, + wirecall_route_finalizer_fn finalizer, int is_async); +int wirecall_routes_remove(wirecall_routes *routes, uint64_t proc_id); +int wirecall_routes_lookup(wirecall_routes *routes, uint64_t proc_id, wirecall_route *out); #endif diff --git a/include/rpc/client.h b/include/rpc/client.h deleted file mode 100644 index d621ad9..0000000 --- a/include/rpc/client.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef RPC_CLIENT_H -#define RPC_CLIENT_H - -#include "rpc/protocol.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct rpc_client rpc_client; - -int rpc_client_connect(rpc_client **out_client, const char *host, const char *port); -int rpc_client_set_integrity(rpc_client *client, uint32_t integrity, const uint8_t mac_key[16]); -void rpc_client_close(rpc_client *client); - -int rpc_client_ping(rpc_client *client); -int rpc_client_call_name(rpc_client *client, const char *proc_name, const rpc_writer *args, rpc_value **out_values, - size_t *out_count); -int rpc_client_send_call_name(rpc_client *client, const char *proc_name, const rpc_writer *args, uint64_t *out_call_id); -int rpc_client_recv_response(rpc_client *client, uint64_t *out_call_id, rpc_value **out_values, size_t *out_count); - -const char *rpc_client_error(const rpc_client *client); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/rpc/protocol.h b/include/rpc/protocol.h deleted file mode 100644 index 9e41903..0000000 --- a/include/rpc/protocol.h +++ /dev/null @@ -1,118 +0,0 @@ -#ifndef RPC_PROTOCOL_H -#define RPC_PROTOCOL_H - -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define RPC_HEADER_SIZE 34u -#define RPC_MAX_PAYLOAD_SIZE (1024u * 1024u) - -#define RPC_INTEGRITY_NONE 0u -#define RPC_INTEGRITY_CHECKSUM (1u << 0u) -#define RPC_INTEGRITY_MAC (1u << 1u) -#define RPC_INTEGRITY_DEFAULT RPC_INTEGRITY_CHECKSUM - -typedef enum rpc_op { - RPC_OP_RPC = 1, - RPC_OP_PING = 2, - RPC_OP_DISCONNECT = 3, - RPC_OP_RESPONSE = 4, - RPC_OP_ERROR = 5, -} rpc_op; - -typedef enum rpc_flag { - RPC_FLAG_NONE = 0u, - RPC_FLAG_MORE = 1u << 0u, -} rpc_flag; - -typedef enum rpc_type { - RPC_TYPE_NULL = 0, - RPC_TYPE_BOOL = 1, - RPC_TYPE_I64 = 2, - RPC_TYPE_U64 = 3, - RPC_TYPE_F64 = 4, - RPC_TYPE_BYTES = 5, - RPC_TYPE_STRING = 6, -} rpc_type; - -typedef struct rpc_value { - rpc_type type; - union { - bool boolean; - int64_t i64; - uint64_t u64; - double f64; - struct { - const uint8_t *data; - uint32_t len; - } bytes; - struct { - const char *data; - uint32_t len; - } string; - } as; -} rpc_value; - -typedef struct rpc_allocator { - void *ctx; - void *(*alloc)(void *ctx, size_t size); - void *(*realloc)(void *ctx, void *ptr, size_t size); - void (*free)(void *ctx, void *ptr); -} rpc_allocator; - -typedef struct rpc_header { - rpc_op op; - uint8_t flags; - uint64_t proc_id; - uint32_t size; - uint64_t call_id; - uint32_t checksum; - uint64_t mac; -} rpc_header; - -typedef struct rpc_writer { - uint8_t *data; - size_t len; - size_t cap; -} rpc_writer; - -/* Process-wide allocator hook. Set before creating/freeing RPC objects or payload buffers; do not swap it live. */ -int rpc_set_allocator(const rpc_allocator *allocator); -void rpc_get_allocator(rpc_allocator *out_allocator); - -int rpc_header_encode(const rpc_header *header, uint8_t out[RPC_HEADER_SIZE]); -int rpc_header_decode(const uint8_t in[RPC_HEADER_SIZE], rpc_header *out); -uint32_t rpc_packet_checksum(const rpc_header *header, const uint8_t *payload, size_t payload_len); -uint64_t rpc_packet_mac(const rpc_header *header, const uint8_t *payload, size_t payload_len, - const uint8_t key[16]); -int rpc_packet_sign(rpc_header *header, const uint8_t *payload, size_t payload_len); -int rpc_packet_verify(const rpc_header *header, const uint8_t *payload, size_t payload_len); -int rpc_packet_sign_ex(rpc_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, - const uint8_t mac_key[16]); -int rpc_packet_verify_ex(const rpc_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, - const uint8_t mac_key[16]); - -void rpc_writer_init(rpc_writer *writer); -void rpc_writer_reset(rpc_writer *writer); -void rpc_writer_free(rpc_writer *writer); -int rpc_writer_null(rpc_writer *writer); -int rpc_writer_bool(rpc_writer *writer, bool value); -int rpc_writer_i64(rpc_writer *writer, int64_t value); -int rpc_writer_u64(rpc_writer *writer, uint64_t value); -int rpc_writer_f64(rpc_writer *writer, double value); -int rpc_writer_bytes(rpc_writer *writer, const void *data, uint32_t len); -int rpc_writer_string(rpc_writer *writer, const char *data, uint32_t len); - -int rpc_payload_decode(const uint8_t *data, size_t len, rpc_value **out_values, size_t *out_count); -void rpc_values_free(rpc_value *values); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/rpc/server.h b/include/rpc/server.h deleted file mode 100644 index 5c1a44f..0000000 --- a/include/rpc/server.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef RPC_SERVER_H -#define RPC_SERVER_H - -#include "rpc/protocol.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct rpc_ctx rpc_ctx; -typedef struct rpc_server rpc_server; - -typedef int (*rpc_handler_fn)(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data); -typedef void (*rpc_route_finalizer_fn)(void *user_data); - -uint64_t rpc_ctx_call_id(const rpc_ctx *ctx); -uint64_t rpc_ctx_proc_id(const rpc_ctx *ctx); -void rpc_ctx_yield(rpc_ctx *ctx); - -int rpc_server_init(rpc_server **out_server); -int rpc_server_set_workers(rpc_server *server, uint32_t worker_count); -int rpc_server_set_integrity(rpc_server *server, uint32_t integrity, const uint8_t mac_key[16]); -int rpc_server_bind(rpc_server *server, const char *host, const char *port); -uint16_t rpc_server_port(const rpc_server *server); -int rpc_server_listen(rpc_server *server); -int rpc_server_run(rpc_server *server); -void rpc_server_stop(rpc_server *server); -void rpc_server_destroy(rpc_server *server); - -int rpc_server_add_route_name(rpc_server *server, const char *proc_name, rpc_handler_fn handler, void *user_data); -int rpc_server_add_async_route_name(rpc_server *server, const char *proc_name, rpc_handler_fn handler, - void *user_data); -int rpc_server_add_route_name_ex(rpc_server *server, const char *proc_name, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer); -int rpc_server_add_async_route_name_ex(rpc_server *server, const char *proc_name, rpc_handler_fn handler, - void *user_data, rpc_route_finalizer_fn finalizer); -int rpc_server_remove_route_name(rpc_server *server, const char *proc_name); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/rpc/trace.h b/include/rpc/trace.h deleted file mode 100644 index fc00041..0000000 --- a/include/rpc/trace.h +++ /dev/null @@ -1,104 +0,0 @@ -#ifndef RPC_TRACE_H -#define RPC_TRACE_H - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum rpc_trace_metric { - RPC_TRACE_CLIENT_CALL, - RPC_TRACE_CLIENT_SEND, - RPC_TRACE_CLIENT_RECV, - RPC_TRACE_CLIENT_DECODE, - RPC_TRACE_SERVER_ACCEPT, - RPC_TRACE_SERVER_POLL_WAIT, - RPC_TRACE_SERVER_POLL_EVENTS, - RPC_TRACE_SERVER_LOOP_ACTIVE, - RPC_TRACE_SERVER_READ, - RPC_TRACE_SERVER_PARSE, - RPC_TRACE_SERVER_ROUTE, - RPC_TRACE_SERVER_SCHEDULE, - RPC_TRACE_SERVER_WRITE, - RPC_TRACE_SCHED_SUBMIT, - RPC_TRACE_SCHED_DECODE, - RPC_TRACE_SCHED_CORO_CREATE, - RPC_TRACE_SCHED_RESUME, - RPC_TRACE_PAYLOAD_DECODE, - RPC_TRACE_ROUTE_LOOKUP, - RPC_TRACE_COUNT, -} rpc_trace_metric; - -typedef enum rpc_trace_worker_metric { - RPC_TRACE_WORKER_POLL_EVENTS, - RPC_TRACE_WORKER_ACCEPTS, - RPC_TRACE_WORKER_READS, - RPC_TRACE_WORKER_RPCS, - RPC_TRACE_WORKER_WRITES, - RPC_TRACE_WORKER_ACTIVE, - RPC_TRACE_WORKER_COUNT, -} rpc_trace_worker_metric; - -#define RPC_TRACE_MAX_WORKERS 128u - -typedef struct rpc_trace_stat { - const char *name; - uint64_t count; - uint64_t total; - uint64_t max; - int is_time; -} rpc_trace_stat; - -extern int rpc_trace_enabled; - -void rpc_trace_set_enabled(int enabled); -uint64_t rpc_trace_begin_slow(void); -void rpc_trace_end_slow(rpc_trace_metric metric, uint64_t start_ns); -void rpc_trace_add_slow(rpc_trace_metric metric, uint64_t value); -void rpc_trace_worker_end_slow(uint32_t worker, rpc_trace_worker_metric metric, uint64_t start_ns); -void rpc_trace_worker_add_slow(uint32_t worker, rpc_trace_worker_metric metric, uint64_t value); -void rpc_trace_reset(void); -void rpc_trace_snapshot(rpc_trace_stat out[RPC_TRACE_COUNT]); -void rpc_trace_dump(FILE *out); - -#if defined(__GNUC__) || defined(__clang__) -#define RPC_TRACE_UNLIKELY(x) __builtin_expect(!!(x), 0) -#else -#define RPC_TRACE_UNLIKELY(x) (x) -#endif - -static inline int rpc_trace_is_enabled(void) { -#if defined(__GNUC__) || defined(__clang__) - return RPC_TRACE_UNLIKELY(__atomic_load_n(&rpc_trace_enabled, __ATOMIC_RELAXED)); -#else - return RPC_TRACE_UNLIKELY(rpc_trace_enabled); -#endif -} - -static inline uint64_t rpc_trace_begin(void) { - return rpc_trace_is_enabled() ? rpc_trace_begin_slow() : 0; -} - -static inline void rpc_trace_end(rpc_trace_metric metric, uint64_t start_ns) { - if (rpc_trace_is_enabled() && start_ns != 0) { rpc_trace_end_slow(metric, start_ns); } -} - -static inline void rpc_trace_add(rpc_trace_metric metric, uint64_t value) { - if (rpc_trace_is_enabled()) { rpc_trace_add_slow(metric, value); } -} - -static inline void rpc_trace_worker_end(uint32_t worker, rpc_trace_worker_metric metric, uint64_t start_ns) { - if (rpc_trace_is_enabled() && start_ns != 0) { rpc_trace_worker_end_slow(worker, metric, start_ns); } -} - -static inline void rpc_trace_worker_add(uint32_t worker, rpc_trace_worker_metric metric, uint64_t value) { - if (rpc_trace_is_enabled()) { rpc_trace_worker_add_slow(worker, metric, value); } -} - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/include/scheduler.h b/include/scheduler.h index 67a04ff..4534c83 100644 --- a/include/scheduler.h +++ b/include/scheduler.h @@ -1,31 +1,31 @@ -#ifndef RPC_SCHEDULER_H -#define RPC_SCHEDULER_H +#ifndef WIRECALL_SCHEDULER_H +#define WIRECALL_SCHEDULER_H -#include "rpc/server.h" +#include "wirecall/server.h" #include -typedef struct rpc_scheduler rpc_scheduler; -typedef struct rpc_call rpc_call; -typedef void (*rpc_call_done_fn)(rpc_call *call, void *user_data); +typedef struct wirecall_scheduler wirecall_scheduler; +typedef struct wirecall_call wirecall_call; +typedef void (*wirecall_call_done_fn)(wirecall_call *call, void *user_data); -struct rpc_ctx { +struct wirecall_ctx { uint64_t call_id; uint64_t proc_id; void *server; }; -int rpc_scheduler_init(rpc_scheduler **out); -void rpc_scheduler_destroy(rpc_scheduler *scheduler); -int rpc_scheduler_submit(rpc_scheduler *scheduler, uint64_t call_id, uint64_t proc_id, rpc_handler_fn handler, - void *handler_data, const uint8_t *payload, size_t payload_len, rpc_call_done_fn done, - void *done_data); -void rpc_scheduler_run_ready(rpc_scheduler *scheduler); +int wirecall_scheduler_init(wirecall_scheduler **out); +void wirecall_scheduler_destroy(wirecall_scheduler *scheduler); +int wirecall_scheduler_submit(wirecall_scheduler *scheduler, uint64_t call_id, uint64_t proc_id, + wirecall_handler_fn handler, void *handler_data, const uint8_t *payload, + size_t payload_len, wirecall_call_done_fn done, void *done_data); +void wirecall_scheduler_run_ready(wirecall_scheduler *scheduler); -int rpc_call_result(const rpc_call *call); -const rpc_writer *rpc_call_response(const rpc_call *call); -const char *rpc_call_error(const rpc_call *call); -uint64_t rpc_call_id(const rpc_call *call); -uint64_t rpc_call_proc_id(const rpc_call *call); +int wirecall_call_result(const wirecall_call *call); +const wirecall_writer *wirecall_call_response(const wirecall_call *call); +const char *wirecall_call_error(const wirecall_call *call); +uint64_t wirecall_call_id(const wirecall_call *call); +uint64_t wirecall_call_proc_id(const wirecall_call *call); #endif diff --git a/include/wirecall/client.h b/include/wirecall/client.h new file mode 100644 index 0000000..bbf60af --- /dev/null +++ b/include/wirecall/client.h @@ -0,0 +1,30 @@ +#ifndef WIRECALL_CLIENT_H +#define WIRECALL_CLIENT_H + +#include "wirecall/protocol.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct wirecall_client wirecall_client; + +int wirecall_client_connect(wirecall_client **out_client, const char *host, const char *port); +int wirecall_client_set_integrity(wirecall_client *client, uint32_t integrity, const uint8_t mac_key[16]); +void wirecall_client_close(wirecall_client *client); + +int wirecall_client_ping(wirecall_client *client); +int wirecall_client_call_name(wirecall_client *client, const char *proc_name, const wirecall_writer *args, + wirecall_value **out_values, size_t *out_count); +int wirecall_client_send_call_name(wirecall_client *client, const char *proc_name, const wirecall_writer *args, + uint64_t *out_call_id); +int wirecall_client_recv_response(wirecall_client *client, uint64_t *out_call_id, wirecall_value **out_values, + size_t *out_count); + +const char *wirecall_client_error(const wirecall_client *client); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/wirecall/protocol.h b/include/wirecall/protocol.h new file mode 100644 index 0000000..3c250c7 --- /dev/null +++ b/include/wirecall/protocol.h @@ -0,0 +1,118 @@ +#ifndef WIRECALL_PROTOCOL_H +#define WIRECALL_PROTOCOL_H + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define WIRECALL_HEADER_SIZE 34u +#define WIRECALL_MAX_PAYLOAD_SIZE (1024u * 1024u) + +#define WIRECALL_INTEGRITY_NONE 0u +#define WIRECALL_INTEGRITY_CHECKSUM (1u << 0u) +#define WIRECALL_INTEGRITY_MAC (1u << 1u) +#define WIRECALL_INTEGRITY_DEFAULT WIRECALL_INTEGRITY_CHECKSUM + +typedef enum wirecall_op { + WIRECALL_OP_RPC = 1, + WIRECALL_OP_PING = 2, + WIRECALL_OP_DISCONNECT = 3, + WIRECALL_OP_RESPONSE = 4, + WIRECALL_OP_ERROR = 5, +} wirecall_op; + +typedef enum wirecall_flag { + WIRECALL_FLAG_NONE = 0u, + WIRECALL_FLAG_MORE = 1u << 0u, +} wirecall_flag; + +typedef enum wirecall_type { + WIRECALL_TYPE_NULL = 0, + WIRECALL_TYPE_BOOL = 1, + WIRECALL_TYPE_I64 = 2, + WIRECALL_TYPE_U64 = 3, + WIRECALL_TYPE_F64 = 4, + WIRECALL_TYPE_BYTES = 5, + WIRECALL_TYPE_STRING = 6, +} wirecall_type; + +typedef struct wirecall_value { + wirecall_type type; + union { + bool boolean; + int64_t i64; + uint64_t u64; + double f64; + struct { + const uint8_t *data; + uint32_t len; + } bytes; + struct { + const char *data; + uint32_t len; + } string; + } as; +} wirecall_value; + +typedef struct wirecall_allocator { + void *ctx; + void *(*alloc)(void *ctx, size_t size); + void *(*realloc)(void *ctx, void *ptr, size_t size); + void (*free)(void *ctx, void *ptr); +} wirecall_allocator; + +typedef struct wirecall_header { + wirecall_op op; + uint8_t flags; + uint64_t proc_id; + uint32_t size; + uint64_t call_id; + uint32_t checksum; + uint64_t mac; +} wirecall_header; + +typedef struct wirecall_writer { + uint8_t *data; + size_t len; + size_t cap; +} wirecall_writer; + +/* Process-wide allocator hook. Set before creating/freeing Wirecall objects or payload buffers; do not swap it live. */ +int wirecall_set_allocator(const wirecall_allocator *allocator); +void wirecall_get_allocator(wirecall_allocator *out_allocator); + +int wirecall_header_encode(const wirecall_header *header, uint8_t out[WIRECALL_HEADER_SIZE]); +int wirecall_header_decode(const uint8_t in[WIRECALL_HEADER_SIZE], wirecall_header *out); +uint32_t wirecall_packet_checksum(const wirecall_header *header, const uint8_t *payload, size_t payload_len); +uint64_t wirecall_packet_mac(const wirecall_header *header, const uint8_t *payload, size_t payload_len, + const uint8_t key[16]); +int wirecall_packet_sign(wirecall_header *header, const uint8_t *payload, size_t payload_len); +int wirecall_packet_verify(const wirecall_header *header, const uint8_t *payload, size_t payload_len); +int wirecall_packet_sign_ex(wirecall_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, + const uint8_t mac_key[16]); +int wirecall_packet_verify_ex(const wirecall_header *header, const uint8_t *payload, size_t payload_len, + uint32_t integrity, const uint8_t mac_key[16]); + +void wirecall_writer_init(wirecall_writer *writer); +void wirecall_writer_reset(wirecall_writer *writer); +void wirecall_writer_free(wirecall_writer *writer); +int wirecall_writer_null(wirecall_writer *writer); +int wirecall_writer_bool(wirecall_writer *writer, bool value); +int wirecall_writer_i64(wirecall_writer *writer, int64_t value); +int wirecall_writer_u64(wirecall_writer *writer, uint64_t value); +int wirecall_writer_f64(wirecall_writer *writer, double value); +int wirecall_writer_bytes(wirecall_writer *writer, const void *data, uint32_t len); +int wirecall_writer_string(wirecall_writer *writer, const char *data, uint32_t len); + +int wirecall_payload_decode(const uint8_t *data, size_t len, wirecall_value **out_values, size_t *out_count); +void wirecall_values_free(wirecall_value *values); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/wirecall/server.h b/include/wirecall/server.h new file mode 100644 index 0000000..e588a04 --- /dev/null +++ b/include/wirecall/server.h @@ -0,0 +1,45 @@ +#ifndef WIRECALL_SERVER_H +#define WIRECALL_SERVER_H + +#include "wirecall/protocol.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct wirecall_ctx wirecall_ctx; +typedef struct wirecall_server wirecall_server; + +typedef int (*wirecall_handler_fn)(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data); +typedef void (*wirecall_route_finalizer_fn)(void *user_data); + +uint64_t wirecall_ctx_call_id(const wirecall_ctx *ctx); +uint64_t wirecall_ctx_proc_id(const wirecall_ctx *ctx); +void wirecall_ctx_yield(wirecall_ctx *ctx); + +int wirecall_server_init(wirecall_server **out_server); +int wirecall_server_set_workers(wirecall_server *server, uint32_t worker_count); +int wirecall_server_set_integrity(wirecall_server *server, uint32_t integrity, const uint8_t mac_key[16]); +int wirecall_server_bind(wirecall_server *server, const char *host, const char *port); +uint16_t wirecall_server_port(const wirecall_server *server); +int wirecall_server_listen(wirecall_server *server); +int wirecall_server_run(wirecall_server *server); +void wirecall_server_stop(wirecall_server *server); +void wirecall_server_destroy(wirecall_server *server); + +int wirecall_server_add_route_name(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data); +int wirecall_server_add_async_route_name(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data); +int wirecall_server_add_route_name_ex(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data, wirecall_route_finalizer_fn finalizer); +int wirecall_server_add_async_route_name_ex(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data, wirecall_route_finalizer_fn finalizer); +int wirecall_server_remove_route_name(wirecall_server *server, const char *proc_name); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/wirecall/trace.h b/include/wirecall/trace.h new file mode 100644 index 0000000..ad2bcf0 --- /dev/null +++ b/include/wirecall/trace.h @@ -0,0 +1,104 @@ +#ifndef WIRECALL_TRACE_H +#define WIRECALL_TRACE_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum wirecall_trace_metric { + WIRECALL_TRACE_CLIENT_CALL, + WIRECALL_TRACE_CLIENT_SEND, + WIRECALL_TRACE_CLIENT_RECV, + WIRECALL_TRACE_CLIENT_DECODE, + WIRECALL_TRACE_SERVER_ACCEPT, + WIRECALL_TRACE_SERVER_POLL_WAIT, + WIRECALL_TRACE_SERVER_POLL_EVENTS, + WIRECALL_TRACE_SERVER_LOOP_ACTIVE, + WIRECALL_TRACE_SERVER_READ, + WIRECALL_TRACE_SERVER_PARSE, + WIRECALL_TRACE_SERVER_ROUTE, + WIRECALL_TRACE_SERVER_SCHEDULE, + WIRECALL_TRACE_SERVER_WRITE, + WIRECALL_TRACE_SCHED_SUBMIT, + WIRECALL_TRACE_SCHED_DECODE, + WIRECALL_TRACE_SCHED_CORO_CREATE, + WIRECALL_TRACE_SCHED_RESUME, + WIRECALL_TRACE_PAYLOAD_DECODE, + WIRECALL_TRACE_ROUTE_LOOKUP, + WIRECALL_TRACE_COUNT, +} wirecall_trace_metric; + +typedef enum wirecall_trace_worker_metric { + WIRECALL_TRACE_WORKER_POLL_EVENTS, + WIRECALL_TRACE_WORKER_ACCEPTS, + WIRECALL_TRACE_WORKER_READS, + WIRECALL_TRACE_WORKER_RPCS, + WIRECALL_TRACE_WORKER_WRITES, + WIRECALL_TRACE_WORKER_ACTIVE, + WIRECALL_TRACE_WORKER_COUNT, +} wirecall_trace_worker_metric; + +#define WIRECALL_TRACE_MAX_WORKERS 128u + +typedef struct wirecall_trace_stat { + const char *name; + uint64_t count; + uint64_t total; + uint64_t max; + int is_time; +} wirecall_trace_stat; + +extern int wirecall_trace_enabled; + +void wirecall_trace_set_enabled(int enabled); +uint64_t wirecall_trace_begin_slow(void); +void wirecall_trace_end_slow(wirecall_trace_metric metric, uint64_t start_ns); +void wirecall_trace_add_slow(wirecall_trace_metric metric, uint64_t value); +void wirecall_trace_worker_end_slow(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t start_ns); +void wirecall_trace_worker_add_slow(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t value); +void wirecall_trace_reset(void); +void wirecall_trace_snapshot(wirecall_trace_stat out[WIRECALL_TRACE_COUNT]); +void wirecall_trace_dump(FILE *out); + +#if defined(__GNUC__) || defined(__clang__) +#define WIRECALL_TRACE_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define WIRECALL_TRACE_UNLIKELY(x) (x) +#endif + +static inline int wirecall_trace_is_enabled(void) { +#if defined(__GNUC__) || defined(__clang__) + return WIRECALL_TRACE_UNLIKELY(__atomic_load_n(&wirecall_trace_enabled, __ATOMIC_RELAXED)); +#else + return WIRECALL_TRACE_UNLIKELY(wirecall_trace_enabled); +#endif +} + +static inline uint64_t wirecall_trace_begin(void) { + return wirecall_trace_is_enabled() ? wirecall_trace_begin_slow() : 0; +} + +static inline void wirecall_trace_end(wirecall_trace_metric metric, uint64_t start_ns) { + if (wirecall_trace_is_enabled() && start_ns != 0) { wirecall_trace_end_slow(metric, start_ns); } +} + +static inline void wirecall_trace_add(wirecall_trace_metric metric, uint64_t value) { + if (wirecall_trace_is_enabled()) { wirecall_trace_add_slow(metric, value); } +} + +static inline void wirecall_trace_worker_end(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t start_ns) { + if (wirecall_trace_is_enabled() && start_ns != 0) { wirecall_trace_worker_end_slow(worker, metric, start_ns); } +} + +static inline void wirecall_trace_worker_add(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t value) { + if (wirecall_trace_is_enabled()) { wirecall_trace_worker_add_slow(worker, metric, value); } +} + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/meson.build b/meson.build index 517046e..0835d3e 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,4 @@ -project('rpc', 'c', default_options: [ +project('wirecall', 'c', default_options: [ 'buildtype=release', 'optimization=3', 'c_std=gnu23', @@ -24,7 +24,7 @@ elif system == 'windows' backend_sources += files('src/backend/windows.c') platform_deps += c.find_library('ws2_32', required: true) else - error('unsupported RPC backend platform: ' + system) + error('unsupported Wirecall backend platform: ' + system) endif core_sources = files( @@ -39,45 +39,45 @@ core_sources = files( ) core_sources += backend_sources -rpc_core = static_library('rpc_core', +wirecall_core = static_library('wirecall_core', core_sources, include_directories: include, dependencies: platform_deps, ) -rpc_dep = declare_dependency( +wirecall_dep = declare_dependency( include_directories: include, - link_with: rpc_core, + link_with: wirecall_core, dependencies: platform_deps, ) -executable('rpc-bench', 'demos/bench.c', dependencies: rpc_dep) -executable('rpc-embed', 'demos/embed.c', dependencies: rpc_dep) +executable('wirecall-bench', 'demos/bench.c', dependencies: wirecall_dep) +executable('wirecall-embed', 'demos/embed.c', dependencies: wirecall_dep) -executable('rpc-server', 'demos/server.c', dependencies: rpc_dep) -executable('rpc-client', 'demos/client.c', dependencies: rpc_dep) +executable('wirecall-server', 'demos/server.c', dependencies: wirecall_dep) +executable('wirecall-client', 'demos/client.c', dependencies: wirecall_dep) -executable('rpc-async-server', 'demos/async/server.c', dependencies: rpc_dep) -executable('rpc-async-client', 'demos/async/client.c', dependencies: rpc_dep) +executable('wirecall-async-server', 'demos/async/server.c', dependencies: wirecall_dep) +executable('wirecall-async-client', 'demos/async/client.c', dependencies: wirecall_dep) test_protocol = executable('test_protocol', 'tests/test_protocol.c', include_directories: include, - dependencies: rpc_dep, + dependencies: wirecall_dep, ) test_routes = executable('test_routes', 'tests/test_routes.c', include_directories: include, - link_with: rpc_core, + link_with: wirecall_core, ) test_scheduler = executable('test_scheduler', 'tests/test_scheduler.c', include_directories: include, - link_with: rpc_core, + link_with: wirecall_core, ) test_integration = executable('test_integration', 'tests/test_integration.c', include_directories: include, - dependencies: rpc_dep, + dependencies: wirecall_dep, ) test('protocol', test_protocol) diff --git a/src/backend/io_uring.c b/src/backend/io_uring.c index 3aff91a..ce2f34c 100644 --- a/src/backend/io_uring.c +++ b/src/backend/io_uring.c @@ -1,7 +1,7 @@ #if defined(__linux__) -#include "memory.h" #include "backend.h" +#include "memory.h" #include #include @@ -13,8 +13,8 @@ #include #include -#define RPC_BACKEND_NAME "io_uring" -#define RPC_URING_ENTRIES 1024u +#define WIRECALL_BACKEND_NAME "io_uring" +#define WIRECALL_URING_ENTRIES 1024u #ifndef MAP_POPULATE #define MAP_POPULATE 0 #endif @@ -22,16 +22,16 @@ #define POLLRDHUP 0x2000 #endif -typedef struct rpc_backend_watch { +typedef struct wirecall_backend_watch { int fd; uint32_t events; uintptr_t user; int active; int armed; - struct rpc_backend_watch *next; -} rpc_backend_watch; + struct wirecall_backend_watch *next; +} wirecall_backend_watch; -struct rpc_backend { +struct wirecall_backend { int ring_fd; int wake_fd; struct io_uring_params params; @@ -50,8 +50,8 @@ struct rpc_backend { unsigned *cq_tail; unsigned *cq_ring_mask; struct io_uring_cqe *cqes; - rpc_backend_watch *watches; - rpc_backend_watch wake_watch; + wirecall_backend_watch *watches; + wirecall_backend_watch wake_watch; }; static int uring_setup(unsigned entries, struct io_uring_params *params) { @@ -62,8 +62,8 @@ static int uring_enter(int fd, unsigned to_submit, unsigned min_complete, unsign return (int)syscall(__NR_io_uring_enter, fd, to_submit, min_complete, flags, NULL, 0); } -static rpc_backend_watch *find_watch(rpc_backend *backend, int fd) { - for (rpc_backend_watch *watch = backend->watches; watch; watch = watch->next) { +static wirecall_backend_watch *find_watch(wirecall_backend *backend, int fd) { + for (wirecall_backend_watch *watch = backend->watches; watch; watch = watch->next) { if (watch->fd == fd) return watch; } return NULL; @@ -71,12 +71,12 @@ static rpc_backend_watch *find_watch(rpc_backend *backend, int fd) { static unsigned poll_mask(uint32_t events) { unsigned mask = 0; - if (events & RPC_BACKEND_READ) { mask |= POLLIN | POLLRDHUP; } - if (events & RPC_BACKEND_WRITE) { mask |= POLLOUT; } + if (events & WIRECALL_BACKEND_READ) { mask |= POLLIN | POLLRDHUP; } + if (events & WIRECALL_BACKEND_WRITE) { mask |= POLLOUT; } return mask; } -static int sqe_push(rpc_backend *backend, struct io_uring_sqe **out_sqe) { +static int sqe_push(wirecall_backend *backend, struct io_uring_sqe **out_sqe) { unsigned head = __atomic_load_n(backend->sq_head, __ATOMIC_ACQUIRE); unsigned tail = *backend->sq_tail; if (tail - head >= *backend->sq_ring_entries) { @@ -94,7 +94,7 @@ static int sqe_push(rpc_backend *backend, struct io_uring_sqe **out_sqe) { return 0; } -static int submit_pending(rpc_backend *backend) { +static int submit_pending(wirecall_backend *backend) { unsigned head = __atomic_load_n(backend->sq_head, __ATOMIC_ACQUIRE); unsigned tail = *backend->sq_tail; unsigned pending = tail - head; @@ -102,7 +102,7 @@ static int submit_pending(rpc_backend *backend) { return uring_enter(backend->ring_fd, pending, 0, 0) < 0 ? -1 : 0; } -static int arm_watch(rpc_backend *backend, rpc_backend_watch *watch) { +static int arm_watch(wirecall_backend *backend, wirecall_backend_watch *watch) { if (!watch->active || watch->armed || watch->events == 0) return 0; struct io_uring_sqe *sqe = NULL; if (sqe_push(backend, &sqe) != 0) return -1; @@ -114,7 +114,7 @@ static int arm_watch(rpc_backend *backend, rpc_backend_watch *watch) { return submit_pending(backend); } -static void cancel_watch(rpc_backend *backend, rpc_backend_watch *watch) { +static void cancel_watch(wirecall_backend *backend, wirecall_backend_watch *watch) { if (!watch->armed) return; struct io_uring_sqe *sqe = NULL; if (sqe_push(backend, &sqe) != 0) return; @@ -125,7 +125,7 @@ static void cancel_watch(rpc_backend *backend, rpc_backend_watch *watch) { watch->armed = 0; } -static int map_rings(rpc_backend *backend) { +static int map_rings(wirecall_backend *backend) { struct io_uring_params *p = &backend->params; backend->sq_map_sz = p->sq_off.array + p->sq_entries * sizeof(unsigned); backend->cq_map_sz = p->cq_off.cqes + p->cq_entries * sizeof(struct io_uring_cqe); @@ -133,8 +133,8 @@ static int map_rings(rpc_backend *backend) { backend->sq_map_sz = backend->cq_map_sz; } - backend->sq_ptr = mmap(NULL, backend->sq_map_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, - backend->ring_fd, IORING_OFF_SQ_RING); + backend->sq_ptr = mmap(NULL, backend->sq_map_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, backend->ring_fd, + IORING_OFF_SQ_RING); if (backend->sq_ptr == MAP_FAILED) return -1; if (p->features & IORING_FEAT_SINGLE_MMAP) { @@ -162,32 +162,32 @@ static int map_rings(rpc_backend *backend) { return 0; } -int rpc_backend_create(rpc_backend **out) { +int wirecall_backend_create(wirecall_backend **out) { if (!out) return -1; - rpc_backend *backend = rpc_mem_calloc(1, sizeof(*backend)); + wirecall_backend *backend = wirecall_mem_calloc(1, sizeof(*backend)); if (!backend) return -1; backend->ring_fd = -1; backend->wake_fd = -1; - backend->ring_fd = uring_setup(RPC_URING_ENTRIES, &backend->params); + backend->ring_fd = uring_setup(WIRECALL_URING_ENTRIES, &backend->params); if (backend->ring_fd < 0 || map_rings(backend) != 0) { - rpc_backend_destroy(backend); + wirecall_backend_destroy(backend); return -1; } backend->wake_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); if (backend->wake_fd < 0) { - rpc_backend_destroy(backend); + wirecall_backend_destroy(backend); return -1; } - backend->wake_watch = (rpc_backend_watch){ - .fd = backend->wake_fd, - .events = RPC_BACKEND_READ, - .active = 1, + backend->wake_watch = (wirecall_backend_watch){ + .fd = backend->wake_fd, + .events = WIRECALL_BACKEND_READ, + .active = 1, }; if (arm_watch(backend, &backend->wake_watch) != 0) { - rpc_backend_destroy(backend); + wirecall_backend_destroy(backend); return -1; } @@ -195,11 +195,11 @@ int rpc_backend_create(rpc_backend **out) { return 0; } -void rpc_backend_destroy(rpc_backend *backend) { +void wirecall_backend_destroy(wirecall_backend *backend) { if (!backend) return; - for (rpc_backend_watch *watch = backend->watches; watch;) { - rpc_backend_watch *next = watch->next; - rpc_mem_free(watch); + for (wirecall_backend_watch *watch = backend->watches; watch;) { + wirecall_backend_watch *next = watch->next; + wirecall_mem_free(watch); watch = next; } if (backend->wake_fd >= 0) close(backend->wake_fd); @@ -211,14 +211,14 @@ void rpc_backend_destroy(rpc_backend *backend) { } if (backend->sq_ptr && backend->sq_ptr != MAP_FAILED) munmap(backend->sq_ptr, backend->sq_map_sz); if (backend->ring_fd >= 0) close(backend->ring_fd); - rpc_mem_free(backend); + wirecall_mem_free(backend); } -int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { +int wirecall_backend_register(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { if (!backend || fd < 0) return -1; - rpc_backend_watch *watch = find_watch(backend, fd); + wirecall_backend_watch *watch = find_watch(backend, fd); if (!watch) { - watch = rpc_mem_calloc(1, sizeof(*watch)); + watch = wirecall_mem_calloc(1, sizeof(*watch)); if (!watch) return -1; watch->fd = fd; watch->next = backend->watches; @@ -232,27 +232,27 @@ int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_ return arm_watch(backend, watch); } -int rpc_backend_modify(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { - return rpc_backend_register(backend, fd, events, user); +int wirecall_backend_modify(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { + return wirecall_backend_register(backend, fd, events, user); } -int rpc_backend_remove(rpc_backend *backend, int fd) { +int wirecall_backend_remove(wirecall_backend *backend, int fd) { if (!backend || fd < 0) return -1; - rpc_backend_watch *watch = find_watch(backend, fd); + wirecall_backend_watch *watch = find_watch(backend, fd); if (!watch) return 0; watch->active = 0; cancel_watch(backend, watch); return 0; } -int rpc_backend_wake(rpc_backend *backend) { +int wirecall_backend_wake(wirecall_backend *backend) { if (!backend || backend->wake_fd < 0) return -1; uint64_t one = 1; ssize_t n = write(backend->wake_fd, &one, sizeof(one)); return n == (ssize_t)sizeof(one) || errno == EAGAIN ? 0 : -1; } -int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_events, int timeout_ms) { +int wirecall_backend_poll(wirecall_backend *backend, wirecall_backend_event *events, int max_events, int timeout_ms) { if (!backend || !events || max_events <= 0) return -1; unsigned want = timeout_ms == 0 ? 0u : 1u; int rc = uring_enter(backend->ring_fd, 0, want, IORING_ENTER_GETEVENTS); @@ -266,19 +266,19 @@ int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_ev unsigned tail = __atomic_load_n(backend->cq_tail, __ATOMIC_ACQUIRE); while (head != tail && out < max_events) { struct io_uring_cqe *cqe = &backend->cqes[head & *backend->cq_ring_mask]; - rpc_backend_watch *watch = (rpc_backend_watch *)(uintptr_t)cqe->user_data; + wirecall_backend_watch *watch = (wirecall_backend_watch *)(uintptr_t)cqe->user_data; if (watch) { watch->armed = 0; if (watch == &backend->wake_watch) { uint64_t value; while (read(backend->wake_fd, &value, sizeof(value)) == (ssize_t)sizeof(value)) {} - events[out++] = (rpc_backend_event){.fd = backend->wake_fd, .events = RPC_BACKEND_WAKE, .user = 0}; + events[out++] = (wirecall_backend_event){.fd = backend->wake_fd, .events = WIRECALL_BACKEND_WAKE, .user = 0}; (void)arm_watch(backend, watch); } else if (watch->active && cqe->res >= 0) { uint32_t ev = 0; - if ((uint32_t)cqe->res & (POLLIN | POLLRDHUP | POLLHUP | POLLERR)) { ev |= RPC_BACKEND_READ; } - if ((uint32_t)cqe->res & (POLLOUT | POLLHUP | POLLERR)) { ev |= RPC_BACKEND_WRITE; } - if (ev != 0) { events[out++] = (rpc_backend_event){.fd = watch->fd, .events = ev, .user = watch->user}; } + if ((uint32_t)cqe->res & (POLLIN | POLLRDHUP | POLLHUP | POLLERR)) { ev |= WIRECALL_BACKEND_READ; } + if ((uint32_t)cqe->res & (POLLOUT | POLLHUP | POLLERR)) { ev |= WIRECALL_BACKEND_WRITE; } + if (ev != 0) { events[out++] = (wirecall_backend_event){.fd = watch->fd, .events = ev, .user = watch->user}; } (void)arm_watch(backend, watch); } } diff --git a/src/backend/kqueue.c b/src/backend/kqueue.c index 1afddd3..72eeacc 100644 --- a/src/backend/kqueue.c +++ b/src/backend/kqueue.c @@ -1,7 +1,7 @@ #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) -#include "memory.h" #include "backend.h" +#include "memory.h" #include #include @@ -9,14 +9,14 @@ #include #include -#define RPC_BACKEND_NAME "kqueue" -#define RPC_KQUEUE_MAX_EVENTS 1024 +#define WIRECALL_BACKEND_NAME "kqueue" +#define WIRECALL_KQUEUE_MAX_EVENTS 1024 -struct rpc_backend { +struct wirecall_backend { int kq; }; -static int apply_filter(rpc_backend *backend, int fd, int16_t filter, uint16_t flags, uintptr_t user) { +static int apply_filter(wirecall_backend *backend, int fd, int16_t filter, uint16_t flags, uintptr_t user) { struct kevent change; EV_SET(&change, (uintptr_t)fd, filter, flags, 0, 0, (void *)user); int rc = kevent(backend->kq, &change, 1, NULL, 0, NULL); @@ -24,14 +24,14 @@ static int apply_filter(rpc_backend *backend, int fd, int16_t filter, uint16_t f return rc; } -static int set_interest(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { +static int set_interest(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { int rc = 0; - if (events & RPC_BACKEND_READ) { + if (events & WIRECALL_BACKEND_READ) { rc |= apply_filter(backend, fd, EVFILT_READ, EV_ADD | EV_ENABLE, user); } else { rc |= apply_filter(backend, fd, EVFILT_READ, EV_DELETE, user); } - if (events & RPC_BACKEND_WRITE) { + if (events & WIRECALL_BACKEND_WRITE) { rc |= apply_filter(backend, fd, EVFILT_WRITE, EV_ADD | EV_ENABLE, user); } else { rc |= apply_filter(backend, fd, EVFILT_WRITE, EV_DELETE, user); @@ -39,13 +39,13 @@ static int set_interest(rpc_backend *backend, int fd, uint32_t events, uintptr_t return rc == 0 ? 0 : -1; } -int rpc_backend_create(rpc_backend **out) { +int wirecall_backend_create(wirecall_backend **out) { if (!out) { return -1; } - rpc_backend *backend = rpc_mem_calloc(1, sizeof(*backend)); + wirecall_backend *backend = wirecall_mem_calloc(1, sizeof(*backend)); if (!backend) { return -1; } backend->kq = kqueue(); if (backend->kq < 0) { - rpc_mem_free(backend); + wirecall_mem_free(backend); return -1; } @@ -53,7 +53,7 @@ int rpc_backend_create(rpc_backend **out) { EV_SET(&wake, 1, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, NULL); if (kevent(backend->kq, &wake, 1, NULL, 0, NULL) != 0) { close(backend->kq); - rpc_mem_free(backend); + wirecall_mem_free(backend); return -1; } @@ -61,37 +61,37 @@ int rpc_backend_create(rpc_backend **out) { return 0; } -void rpc_backend_destroy(rpc_backend *backend) { +void wirecall_backend_destroy(wirecall_backend *backend) { if (backend) { if (backend->kq >= 0) { close(backend->kq); } - rpc_mem_free(backend); + wirecall_mem_free(backend); } } -int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { +int wirecall_backend_register(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { if (!backend || fd < 0) { return -1; } return set_interest(backend, fd, events, user); } -int rpc_backend_modify(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { - return rpc_backend_register(backend, fd, events, user); +int wirecall_backend_modify(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { + return wirecall_backend_register(backend, fd, events, user); } -int rpc_backend_remove(rpc_backend *backend, int fd) { +int wirecall_backend_remove(wirecall_backend *backend, int fd) { if (!backend || fd < 0) { return -1; } (void)apply_filter(backend, fd, EVFILT_READ, EV_DELETE, 0); (void)apply_filter(backend, fd, EVFILT_WRITE, EV_DELETE, 0); return 0; } -int rpc_backend_wake(rpc_backend *backend) { +int wirecall_backend_wake(wirecall_backend *backend) { if (!backend) { return -1; } struct kevent wake; EV_SET(&wake, 1, EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL); return kevent(backend->kq, &wake, 1, NULL, 0, NULL); } -int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_events, int timeout_ms) { +int wirecall_backend_poll(wirecall_backend *backend, wirecall_backend_event *events, int max_events, int timeout_ms) { if (!backend || !events || max_events <= 0) { return -1; } struct timespec timeout; @@ -102,8 +102,8 @@ int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_ev timeout_ptr = &timeout; } - struct kevent kev[RPC_KQUEUE_MAX_EVENTS]; - int limit = max_events < RPC_KQUEUE_MAX_EVENTS ? max_events : RPC_KQUEUE_MAX_EVENTS; + struct kevent kev[WIRECALL_KQUEUE_MAX_EVENTS]; + int limit = max_events < WIRECALL_KQUEUE_MAX_EVENTS ? max_events : WIRECALL_KQUEUE_MAX_EVENTS; int n = kevent(backend->kq, NULL, 0, kev, limit, timeout_ptr); if (n < 0) { if (errno == EINTR) { return 0; } @@ -114,9 +114,9 @@ int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_ev events[i].fd = (int)kev[i].ident; events[i].events = 0; events[i].user = (uintptr_t)kev[i].udata; - if (kev[i].filter == EVFILT_USER) { events[i].events |= RPC_BACKEND_WAKE; } - if (kev[i].filter == EVFILT_READ) { events[i].events |= RPC_BACKEND_READ; } - if (kev[i].filter == EVFILT_WRITE) { events[i].events |= RPC_BACKEND_WRITE; } + if (kev[i].filter == EVFILT_USER) { events[i].events |= WIRECALL_BACKEND_WAKE; } + if (kev[i].filter == EVFILT_READ) { events[i].events |= WIRECALL_BACKEND_READ; } + if (kev[i].filter == EVFILT_WRITE) { events[i].events |= WIRECALL_BACKEND_WRITE; } } return n; } diff --git a/src/backend/windows.c b/src/backend/windows.c index 289b1ff..7c9721c 100644 --- a/src/backend/windows.c +++ b/src/backend/windows.c @@ -1,74 +1,74 @@ #if defined(_WIN32) -#include "memory.h" #include "backend.h" +#include "memory.h" #define WIN32_LEAN_AND_MEAN -#include -#include #include +#include +#include -#define RPC_BACKEND_NAME "windows" -#define RPC_WINDOWS_MAX_WATCHES 1024u +#define WIRECALL_BACKEND_NAME "windows" +#define WIRECALL_WINDOWS_MAX_WATCHES 1024u -typedef struct rpc_backend_watch { +typedef struct wirecall_backend_watch { SOCKET socket; WSAEVENT event; uint32_t interests; uintptr_t user; int active; -} rpc_backend_watch; +} wirecall_backend_watch; -struct rpc_backend { +struct wirecall_backend { WSAEVENT wake_event; - rpc_backend_watch watches[RPC_WINDOWS_MAX_WATCHES]; + wirecall_backend_watch watches[WIRECALL_WINDOWS_MAX_WATCHES]; WSADATA wsa; int wsa_ready; }; static long network_events(uint32_t interests) { long events = FD_CLOSE; - if (interests & RPC_BACKEND_READ) { events |= FD_ACCEPT | FD_READ; } - if (interests & RPC_BACKEND_WRITE) { events |= FD_WRITE; } + if (interests & WIRECALL_BACKEND_READ) { events |= FD_ACCEPT | FD_READ; } + if (interests & WIRECALL_BACKEND_WRITE) { events |= FD_WRITE; } return events; } -static rpc_backend_watch *find_watch(rpc_backend *backend, int fd) { +static wirecall_backend_watch *find_watch(wirecall_backend *backend, int fd) { SOCKET socket = (SOCKET)(uintptr_t)fd; - for (size_t i = 0; i < RPC_WINDOWS_MAX_WATCHES; ++i) { + for (size_t i = 0; i < WIRECALL_WINDOWS_MAX_WATCHES; ++i) { if (backend->watches[i].active && backend->watches[i].socket == socket) { return &backend->watches[i]; } } return NULL; } -static rpc_backend_watch *alloc_watch(rpc_backend *backend) { - for (size_t i = 0; i < RPC_WINDOWS_MAX_WATCHES; ++i) { +static wirecall_backend_watch *alloc_watch(wirecall_backend *backend) { + for (size_t i = 0; i < WIRECALL_WINDOWS_MAX_WATCHES; ++i) { if (!backend->watches[i].active) { return &backend->watches[i]; } } return NULL; } -int rpc_backend_create(rpc_backend **out) { +int wirecall_backend_create(wirecall_backend **out) { if (!out) { return -1; } - rpc_backend *backend = rpc_mem_calloc(1, sizeof(*backend)); + wirecall_backend *backend = wirecall_mem_calloc(1, sizeof(*backend)); if (!backend) { return -1; } if (WSAStartup(MAKEWORD(2, 2), &backend->wsa) != 0) { - rpc_mem_free(backend); + wirecall_mem_free(backend); return -1; } backend->wsa_ready = 1; backend->wake_event = WSACreateEvent(); if (backend->wake_event == WSA_INVALID_EVENT) { - rpc_backend_destroy(backend); + wirecall_backend_destroy(backend); return -1; } *out = backend; return 0; } -void rpc_backend_destroy(rpc_backend *backend) { +void wirecall_backend_destroy(wirecall_backend *backend) { if (!backend) { return; } - for (size_t i = 0; i < RPC_WINDOWS_MAX_WATCHES; ++i) { + for (size_t i = 0; i < WIRECALL_WINDOWS_MAX_WATCHES; ++i) { if (backend->watches[i].active) { (void)WSAEventSelect(backend->watches[i].socket, NULL, 0); WSACloseEvent(backend->watches[i].event); @@ -76,12 +76,12 @@ void rpc_backend_destroy(rpc_backend *backend) { } if (backend->wake_event != WSA_INVALID_EVENT) { WSACloseEvent(backend->wake_event); } if (backend->wsa_ready) { WSACleanup(); } - rpc_mem_free(backend); + wirecall_mem_free(backend); } -int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { +int wirecall_backend_register(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { if (!backend || fd < 0) { return -1; } - rpc_backend_watch *watch = find_watch(backend, fd); + wirecall_backend_watch *watch = find_watch(backend, fd); if (!watch) { watch = alloc_watch(backend); if (!watch) { return -1; } @@ -96,13 +96,13 @@ int rpc_backend_register(rpc_backend *backend, int fd, uint32_t events, uintptr_ return 0; } -int rpc_backend_modify(rpc_backend *backend, int fd, uint32_t events, uintptr_t user) { - return rpc_backend_register(backend, fd, events, user); +int wirecall_backend_modify(wirecall_backend *backend, int fd, uint32_t events, uintptr_t user) { + return wirecall_backend_register(backend, fd, events, user); } -int rpc_backend_remove(rpc_backend *backend, int fd) { +int wirecall_backend_remove(wirecall_backend *backend, int fd) { if (!backend || fd < 0) { return -1; } - rpc_backend_watch *watch = find_watch(backend, fd); + wirecall_backend_watch *watch = find_watch(backend, fd); if (!watch) { return 0; } (void)WSAEventSelect(watch->socket, NULL, 0); WSACloseEvent(watch->event); @@ -110,20 +110,20 @@ int rpc_backend_remove(rpc_backend *backend, int fd) { return 0; } -int rpc_backend_wake(rpc_backend *backend) { +int wirecall_backend_wake(wirecall_backend *backend) { return backend && WSASetEvent(backend->wake_event) ? 0 : -1; } -int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_events, int timeout_ms) { +int wirecall_backend_poll(wirecall_backend *backend, wirecall_backend_event *events, int max_events, int timeout_ms) { if (!backend || !events || max_events <= 0) { return -1; } WSAEVENT handles[MAXIMUM_WAIT_OBJECTS]; - rpc_backend_watch *watch_for_handle[MAXIMUM_WAIT_OBJECTS]; + wirecall_backend_watch *watch_for_handle[MAXIMUM_WAIT_OBJECTS]; DWORD count = 1; handles[0] = backend->wake_event; watch_for_handle[0] = NULL; - for (size_t i = 0; i < RPC_WINDOWS_MAX_WATCHES && count < MAXIMUM_WAIT_OBJECTS; ++i) { + for (size_t i = 0; i < WIRECALL_WINDOWS_MAX_WATCHES && count < MAXIMUM_WAIT_OBJECTS; ++i) { if (!backend->watches[i].active) { continue; } handles[count] = backend->watches[i].event; watch_for_handle[count] = &backend->watches[i]; @@ -141,18 +141,18 @@ int rpc_backend_poll(rpc_backend *backend, rpc_backend_event *events, int max_ev if (WSAWaitForMultipleEvents(1, &handles[i], TRUE, 0, FALSE) != WSA_WAIT_EVENT_0) { continue; } if (i == 0) { WSAResetEvent(backend->wake_event); - events[out++] = (rpc_backend_event){.fd = -1, .events = RPC_BACKEND_WAKE, .user = 0}; + events[out++] = (wirecall_backend_event){.fd = -1, .events = WIRECALL_BACKEND_WAKE, .user = 0}; continue; } - rpc_backend_watch *watch = watch_for_handle[i]; + wirecall_backend_watch *watch = watch_for_handle[i]; WSANETWORKEVENTS nevents; if (WSAEnumNetworkEvents(watch->socket, watch->event, &nevents) != 0) { continue; } uint32_t ev = 0; - if (nevents.lNetworkEvents & (FD_ACCEPT | FD_READ | FD_CLOSE)) { ev |= RPC_BACKEND_READ; } - if (nevents.lNetworkEvents & FD_WRITE) { ev |= RPC_BACKEND_WRITE; } + if (nevents.lNetworkEvents & (FD_ACCEPT | FD_READ | FD_CLOSE)) { ev |= WIRECALL_BACKEND_READ; } + if (nevents.lNetworkEvents & FD_WRITE) { ev |= WIRECALL_BACKEND_WRITE; } if (ev != 0) { - events[out++] = (rpc_backend_event){.fd = (int)(uintptr_t)watch->socket, .events = ev, .user = watch->user}; + events[out++] = (wirecall_backend_event){.fd = (int)(uintptr_t)watch->socket, .events = ev, .user = watch->user}; } } return out; diff --git a/src/client.c b/src/client.c index d4abf01..2e29c9e 100644 --- a/src/client.c +++ b/src/client.c @@ -1,5 +1,5 @@ -#include "rpc/client.h" -#include "rpc/trace.h" +#include "wirecall/client.h" +#include "wirecall/trace.h" #include "memory.h" #include "proc.h" @@ -14,12 +14,12 @@ #include #ifdef MSG_NOSIGNAL -#define RPC_SEND_FLAGS MSG_NOSIGNAL +#define WIRECALL_SEND_FLAGS MSG_NOSIGNAL #else -#define RPC_SEND_FLAGS 0 +#define WIRECALL_SEND_FLAGS 0 #endif -struct rpc_client { +struct wirecall_client { int fd; uint64_t next_call_id; uint8_t *read_buf; @@ -31,7 +31,7 @@ struct rpc_client { char error[160]; }; -static void set_error(rpc_client *client, const char *message) { +static void set_error(wirecall_client *client, const char *message) { if (client) { snprintf(client->error, sizeof(client->error), "%s", message); } } @@ -46,7 +46,7 @@ static int send_iov_full(int fd, const struct iovec *iov, int iov_count) { msg.msg_iov = local; msg.msg_iovlen = (size_t)iov_count; - ssize_t n = sendmsg(fd, &msg, RPC_SEND_FLAGS); + ssize_t n = sendmsg(fd, &msg, WIRECALL_SEND_FLAGS); if (n > 0) { size_t sent = (size_t)n; while (iov_count > 0 && sent >= local[0].iov_len) { @@ -66,11 +66,11 @@ static int send_iov_full(int fd, const struct iovec *iov, int iov_count) { return 0; } -static size_t client_read_available(const rpc_client *client) { +static size_t client_read_available(const wirecall_client *client) { return client->read_len - client->read_off; } -static int client_read_reserve(rpc_client *client, size_t need) { +static int client_read_reserve(wirecall_client *client, size_t need) { if (client_read_available(client) >= need) { return 0; } if (client->read_off > 0) { memmove(client->read_buf, client->read_buf + client->read_off, client_read_available(client)); @@ -83,14 +83,14 @@ static int client_read_reserve(rpc_client *client, size_t need) { while (next_cap < need) { next_cap *= 2u; } - uint8_t *next = rpc_mem_realloc(client->read_buf, next_cap); + uint8_t *next = wirecall_mem_realloc(client->read_buf, next_cap); if (!next) { return -1; } client->read_buf = next; client->read_cap = next_cap; return 0; } -static int client_read_fill(rpc_client *client, size_t need) { +static int client_read_fill(wirecall_client *client, size_t need) { while (client_read_available(client) < need) { if (client_read_reserve(client, need) != 0) { return -1; } if (client->read_len == client->read_cap && client_read_reserve(client, client->read_cap + 1u) != 0) { return -1; } @@ -105,81 +105,83 @@ static int client_read_fill(rpc_client *client, size_t need) { return 0; } -static int send_packet(rpc_client *client, rpc_op op, uint64_t proc_id, uint64_t call_id, const rpc_writer *payload) { - uint64_t trace = rpc_trace_begin(); - rpc_header header = { - .op = op, - .flags = RPC_FLAG_NONE, - .proc_id = proc_id, - .size = payload ? (uint32_t)payload->len : 0, - .call_id = call_id, +static int send_packet(wirecall_client *client, wirecall_op op, uint64_t proc_id, uint64_t call_id, + const wirecall_writer *payload) { + uint64_t trace = wirecall_trace_begin(); + wirecall_header header = { + .op = op, + .flags = WIRECALL_FLAG_NONE, + .proc_id = proc_id, + .size = payload ? (uint32_t)payload->len : 0, + .call_id = call_id, }; - uint8_t header_buf[RPC_HEADER_SIZE]; + uint8_t header_buf[WIRECALL_HEADER_SIZE]; - if (rpc_packet_sign_ex(&header, payload ? payload->data : NULL, payload ? payload->len : 0, client->integrity, - client->mac_key) != 0) { + if (wirecall_packet_sign_ex(&header, payload ? payload->data : NULL, payload ? payload->len : 0, client->integrity, + client->mac_key) != 0) { set_error(client, "send failed"); - rpc_trace_end(RPC_TRACE_CLIENT_SEND, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_SEND, trace); return -1; } - if (rpc_header_encode(&header, header_buf) != 0) { + if (wirecall_header_encode(&header, header_buf) != 0) { set_error(client, "send failed"); - rpc_trace_end(RPC_TRACE_CLIENT_SEND, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_SEND, trace); return -1; } struct iovec iov[2]; int iov_count = 1; iov[0] = (struct iovec){ - .iov_base = header_buf, - .iov_len = sizeof(header_buf), + .iov_base = header_buf, + .iov_len = sizeof(header_buf), }; if (payload && payload->len > 0) { iov[1] = (struct iovec){ - .iov_base = payload->data, - .iov_len = payload->len, + .iov_base = payload->data, + .iov_len = payload->len, }; iov_count = 2; } if (send_iov_full(client->fd, iov, iov_count) != 0) { set_error(client, "send failed"); - rpc_trace_end(RPC_TRACE_CLIENT_SEND, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_SEND, trace); return -1; } - rpc_trace_end(RPC_TRACE_CLIENT_SEND, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_SEND, trace); return 0; } -static int recv_packet(rpc_client *client, rpc_header *header, uint8_t **body) { - uint64_t trace = rpc_trace_begin(); +static int recv_packet(wirecall_client *client, wirecall_header *header, uint8_t **body) { + uint64_t trace = wirecall_trace_begin(); *body = NULL; - if (client_read_fill(client, RPC_HEADER_SIZE) != 0 || - rpc_header_decode(client->read_buf + client->read_off, header) != 0 || header->size > RPC_MAX_PAYLOAD_SIZE) { + if (client_read_fill(client, WIRECALL_HEADER_SIZE) != 0 || + wirecall_header_decode(client->read_buf + client->read_off, header) != 0 || + header->size > WIRECALL_MAX_PAYLOAD_SIZE) { set_error(client, "read failed"); - rpc_trace_end(RPC_TRACE_CLIENT_RECV, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_RECV, trace); return -1; } - size_t packet_size = RPC_HEADER_SIZE + (size_t)header->size; + size_t packet_size = WIRECALL_HEADER_SIZE + (size_t)header->size; if (client_read_fill(client, packet_size) != 0) { set_error(client, "read failed"); - rpc_trace_end(RPC_TRACE_CLIENT_RECV, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_RECV, trace); return -1; } - const uint8_t *payload = client->read_buf + client->read_off + RPC_HEADER_SIZE; - if (rpc_packet_verify_ex(header, payload, header->size, client->integrity, client->mac_key) != 0) { + const uint8_t *payload = client->read_buf + client->read_off + WIRECALL_HEADER_SIZE; + if (wirecall_packet_verify_ex(header, payload, header->size, client->integrity, client->mac_key) != 0) { set_error(client, "packet integrity failed"); - rpc_trace_end(RPC_TRACE_CLIENT_RECV, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_RECV, trace); return -1; } - *body = rpc_mem_alloc(header->size ? header->size : 1); + *body = wirecall_mem_alloc(header->size ? header->size : 1); if (!*body) { set_error(client, "out of memory"); - rpc_trace_end(RPC_TRACE_CLIENT_RECV, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_RECV, trace); return -1; } if (header->size > 0) { memcpy(*body, payload, header->size); } @@ -188,20 +190,21 @@ static int recv_packet(rpc_client *client, rpc_header *header, uint8_t **body) { client->read_off = 0; client->read_len = 0; } - rpc_trace_end(RPC_TRACE_CLIENT_RECV, trace); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_RECV, trace); return 0; } -static int client_send_call_id(rpc_client *client, uint64_t proc_id, const rpc_writer *args, uint64_t *out_call_id); +static int client_send_call_id(wirecall_client *client, uint64_t proc_id, const wirecall_writer *args, + uint64_t *out_call_id); -int rpc_client_connect(rpc_client **out_client, const char *host, const char *port) { +int wirecall_client_connect(wirecall_client **out_client, const char *host, const char *port) { if (!out_client || !port) { return -1; } - rpc_client *client = rpc_mem_calloc(1, sizeof(*client)); + wirecall_client *client = wirecall_mem_calloc(1, sizeof(*client)); if (!client) { return -1; } client->fd = -1; client->next_call_id = 1; - client->integrity = RPC_INTEGRITY_DEFAULT; + client->integrity = WIRECALL_INTEGRITY_DEFAULT; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); @@ -210,7 +213,7 @@ int rpc_client_connect(rpc_client **out_client, const char *host, const char *po struct addrinfo *res = NULL; if (getaddrinfo(host, port, &hints, &res) != 0) { - rpc_mem_free(client); + wirecall_mem_free(client); return -1; } @@ -230,7 +233,7 @@ int rpc_client_connect(rpc_client **out_client, const char *host, const char *po freeaddrinfo(res); if (client->fd < 0) { - rpc_mem_free(client); + wirecall_mem_free(client); return -1; } @@ -238,9 +241,9 @@ int rpc_client_connect(rpc_client **out_client, const char *host, const char *po return 0; } -int rpc_client_set_integrity(rpc_client *client, uint32_t integrity, const uint8_t mac_key[16]) { - if (!client || (integrity & ~(RPC_INTEGRITY_CHECKSUM | RPC_INTEGRITY_MAC)) || - ((integrity & RPC_INTEGRITY_MAC) && !mac_key)) { +int wirecall_client_set_integrity(wirecall_client *client, uint32_t integrity, const uint8_t mac_key[16]) { + if (!client || (integrity & ~(WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC)) || + ((integrity & WIRECALL_INTEGRITY_MAC) && !mac_key)) { return -1; } client->integrity = integrity; @@ -248,38 +251,38 @@ int rpc_client_set_integrity(rpc_client *client, uint32_t integrity, const uint8 return 0; } -void rpc_client_close(rpc_client *client) { +void wirecall_client_close(wirecall_client *client) { if (!client) { return; } if (client->fd >= 0) { - (void)send_packet(client, RPC_OP_DISCONNECT, 0, client->next_call_id++, NULL); + (void)send_packet(client, WIRECALL_OP_DISCONNECT, 0, client->next_call_id++, NULL); close(client->fd); } - rpc_mem_free(client->read_buf); - rpc_mem_free(client); + wirecall_mem_free(client->read_buf); + wirecall_mem_free(client); } -int rpc_client_ping(rpc_client *client) { +int wirecall_client_ping(wirecall_client *client) { if (!client || client->fd < 0) { return -1; } uint64_t call_id = client->next_call_id++; - if (send_packet(client, RPC_OP_PING, 0, call_id, NULL) != 0) { return -1; } + if (send_packet(client, WIRECALL_OP_PING, 0, call_id, NULL) != 0) { return -1; } - rpc_header header; + wirecall_header header; uint8_t *body = NULL; if (recv_packet(client, &header, &body) != 0) { return -1; } - rpc_mem_free(body); + wirecall_mem_free(body); - if (header.op != RPC_OP_RESPONSE || header.call_id != call_id || header.size != 0) { + if (header.op != WIRECALL_OP_RESPONSE || header.call_id != call_id || header.size != 0) { set_error(client, "unexpected ping response"); return -1; } return 0; } -static int client_call_id(rpc_client *client, uint64_t proc_id, const rpc_writer *args, rpc_value **out_values, - size_t *out_count) { - uint64_t trace_call = rpc_trace_begin(); +static int client_call_id(wirecall_client *client, uint64_t proc_id, const wirecall_writer *args, + wirecall_value **out_values, size_t *out_count) { + uint64_t trace_call = wirecall_trace_begin(); if (!client || client->fd < 0 || !out_values || !out_count) { - rpc_trace_end(RPC_TRACE_CLIENT_CALL, trace_call); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_CALL, trace_call); return -1; } *out_values = NULL; @@ -287,67 +290,69 @@ static int client_call_id(rpc_client *client, uint64_t proc_id, const rpc_writer uint64_t call_id = 0; if (client_send_call_id(client, proc_id, args, &call_id) != 0) { - rpc_trace_end(RPC_TRACE_CLIENT_CALL, trace_call); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_CALL, trace_call); return -1; } uint64_t response_call_id = 0; - int rc = rpc_client_recv_response(client, &response_call_id, out_values, out_count); + int rc = wirecall_client_recv_response(client, &response_call_id, out_values, out_count); if (rc == 0 && response_call_id != call_id) { - rpc_values_free(*out_values); + wirecall_values_free(*out_values); *out_values = NULL; *out_count = 0; set_error(client, "unexpected call id"); rc = -1; } - rpc_trace_end(RPC_TRACE_CLIENT_CALL, trace_call); + wirecall_trace_end(WIRECALL_TRACE_CLIENT_CALL, trace_call); return rc; } -int rpc_client_call_name(rpc_client *client, const char *proc_name, const rpc_writer *args, rpc_value **out_values, - size_t *out_count) { - return client_call_id(client, rpc_proc_id(proc_name), args, out_values, out_count); +int wirecall_client_call_name(wirecall_client *client, const char *proc_name, const wirecall_writer *args, + wirecall_value **out_values, size_t *out_count) { + return client_call_id(client, wirecall_proc_id(proc_name), args, out_values, out_count); } -static int client_send_call_id(rpc_client *client, uint64_t proc_id, const rpc_writer *args, uint64_t *out_call_id) { +static int client_send_call_id(wirecall_client *client, uint64_t proc_id, const wirecall_writer *args, + uint64_t *out_call_id) { if (!client || client->fd < 0 || !out_call_id) { return -1; } uint64_t call_id = client->next_call_id++; - if (send_packet(client, RPC_OP_RPC, proc_id, call_id, args) != 0) { return -1; } + if (send_packet(client, WIRECALL_OP_RPC, proc_id, call_id, args) != 0) { return -1; } *out_call_id = call_id; return 0; } -int rpc_client_send_call_name(rpc_client *client, const char *proc_name, const rpc_writer *args, - uint64_t *out_call_id) { - return client_send_call_id(client, rpc_proc_id(proc_name), args, out_call_id); +int wirecall_client_send_call_name(wirecall_client *client, const char *proc_name, const wirecall_writer *args, + uint64_t *out_call_id) { + return client_send_call_id(client, wirecall_proc_id(proc_name), args, out_call_id); } -int rpc_client_recv_response(rpc_client *client, uint64_t *out_call_id, rpc_value **out_values, size_t *out_count) { +int wirecall_client_recv_response(wirecall_client *client, uint64_t *out_call_id, wirecall_value **out_values, + size_t *out_count) { if (!client || client->fd < 0 || !out_call_id || !out_values || !out_count) { return -1; } *out_call_id = 0; *out_values = NULL; *out_count = 0; - rpc_header header; + wirecall_header header; uint8_t *body = NULL; if (recv_packet(client, &header, &body) != 0) { return -1; } int rc = -1; *out_call_id = header.call_id; - if (header.op == RPC_OP_RESPONSE) { - uint64_t trace_decode = rpc_trace_begin(); - if (rpc_payload_decode(body, header.size, out_values, out_count) == 0) { + if (header.op == WIRECALL_OP_RESPONSE) { + uint64_t trace_decode = wirecall_trace_begin(); + if (wirecall_payload_decode(body, header.size, out_values, out_count) == 0) { rc = 0; } else { set_error(client, "malformed response payload"); } - rpc_trace_end(RPC_TRACE_CLIENT_DECODE, trace_decode); - } else if (header.op == RPC_OP_ERROR) { - rpc_value *values = NULL; + wirecall_trace_end(WIRECALL_TRACE_CLIENT_DECODE, trace_decode); + } else if (header.op == WIRECALL_OP_ERROR) { + wirecall_value *values = NULL; size_t count = 0; - if (rpc_payload_decode(body, header.size, &values, &count) == 0 && count == 1 && - values[0].type == RPC_TYPE_STRING) { + if (wirecall_payload_decode(body, header.size, &values, &count) == 0 && count == 1 && + values[0].type == WIRECALL_TYPE_STRING) { size_t n = values[0].as.string.len; if (n >= sizeof(client->error)) { n = sizeof(client->error) - 1u; } memcpy(client->error, values[0].as.string.data, n); @@ -355,15 +360,15 @@ int rpc_client_recv_response(rpc_client *client, uint64_t *out_call_id, rpc_valu } else { set_error(client, "server returned error"); } - rpc_values_free(values); + wirecall_values_free(values); } else { set_error(client, "unexpected response op"); } - rpc_mem_free(body); + wirecall_mem_free(body); return rc; } -const char *rpc_client_error(const rpc_client *client) { +const char *wirecall_client_error(const wirecall_client *client) { return client && client->error[0] ? client->error : "client error"; } diff --git a/src/memory.c b/src/memory.c index eaad0cc..cdf3752 100644 --- a/src/memory.c +++ b/src/memory.c @@ -1,22 +1,20 @@ #include "memory.h" -#include "rpc/protocol.h" +#include "wirecall/protocol.h" #include #include #include -static rpc_allocator g_allocator; +static wirecall_allocator g_allocator; -static int allocator_valid(const rpc_allocator *allocator) { +static int allocator_valid(const wirecall_allocator *allocator) { if (!allocator) { return 0; } return allocator->alloc && allocator->realloc && allocator->free; } -int rpc_set_allocator(const rpc_allocator *allocator) { +int wirecall_set_allocator(const wirecall_allocator *allocator) { if (allocator) { - if (!allocator_valid(allocator)) { - return -1; - } + if (!allocator_valid(allocator)) { return -1; } g_allocator = *allocator; } else { memset(&g_allocator, 0, sizeof(g_allocator)); @@ -24,39 +22,39 @@ int rpc_set_allocator(const rpc_allocator *allocator) { return 0; } -void rpc_get_allocator(rpc_allocator *out_allocator) { +void wirecall_get_allocator(wirecall_allocator *out_allocator) { if (!out_allocator) { return; } *out_allocator = g_allocator; } -void *rpc_mem_alloc(size_t size) { +void *wirecall_mem_alloc(size_t size) { if (size == 0) { size = 1; } - rpc_allocator allocator; - rpc_get_allocator(&allocator); + wirecall_allocator allocator; + wirecall_get_allocator(&allocator); if (allocator.alloc) { return allocator.alloc(allocator.ctx, size); } return malloc(size); } -void *rpc_mem_calloc(size_t count, size_t size) { +void *wirecall_mem_calloc(size_t count, size_t size) { if (count != 0 && size > SIZE_MAX / count) { return NULL; } size_t bytes = count * size; - void *ptr = rpc_mem_alloc(bytes); + void *ptr = wirecall_mem_alloc(bytes); if (ptr) { memset(ptr, 0, bytes); } return ptr; } -void *rpc_mem_realloc(void *ptr, size_t size) { +void *wirecall_mem_realloc(void *ptr, size_t size) { if (size == 0) { size = 1; } - rpc_allocator allocator; - rpc_get_allocator(&allocator); + wirecall_allocator allocator; + wirecall_get_allocator(&allocator); if (allocator.realloc) { return allocator.realloc(allocator.ctx, ptr, size); } return realloc(ptr, size); } -void rpc_mem_free(void *ptr) { +void wirecall_mem_free(void *ptr) { if (!ptr) { return; } - rpc_allocator allocator; - rpc_get_allocator(&allocator); + wirecall_allocator allocator; + wirecall_get_allocator(&allocator); if (allocator.free) { allocator.free(allocator.ctx, ptr); } else { diff --git a/src/payload.c b/src/payload.c index 584eef9..082becd 100644 --- a/src/payload.c +++ b/src/payload.c @@ -1,5 +1,5 @@ -#include "rpc/protocol.h" -#include "rpc/trace.h" +#include "wirecall/protocol.h" +#include "wirecall/trace.h" #include "memory.h" @@ -31,113 +31,113 @@ static uint64_t get_u64(const uint8_t *in) { return value; } -static int writer_reserve(rpc_writer *writer, size_t extra) { - if (!writer || extra > RPC_MAX_PAYLOAD_SIZE || writer->len > RPC_MAX_PAYLOAD_SIZE - extra) { return -1; } +static int writer_reserve(wirecall_writer *writer, size_t extra) { + if (!writer || extra > WIRECALL_MAX_PAYLOAD_SIZE || writer->len > WIRECALL_MAX_PAYLOAD_SIZE - extra) { return -1; } size_t need = writer->len + extra; if (need <= writer->cap) { return 0; } size_t cap = writer->cap ? writer->cap : 64u; while (cap < need) { - if (cap > RPC_MAX_PAYLOAD_SIZE / 2u) { - cap = RPC_MAX_PAYLOAD_SIZE; + if (cap > WIRECALL_MAX_PAYLOAD_SIZE / 2u) { + cap = WIRECALL_MAX_PAYLOAD_SIZE; break; } cap *= 2u; } - uint8_t *next = rpc_mem_realloc(writer->data, cap); + uint8_t *next = wirecall_mem_realloc(writer->data, cap); if (!next) { return -1; } writer->data = next; writer->cap = cap; return 0; } -static int writer_push(rpc_writer *writer, const void *data, size_t len) { +static int writer_push(wirecall_writer *writer, const void *data, size_t len) { if (writer_reserve(writer, len) != 0) { return -1; } if (len > 0) { memcpy(writer->data + writer->len, data, len); } writer->len += len; return 0; } -void rpc_writer_init(rpc_writer *writer) { +void wirecall_writer_init(wirecall_writer *writer) { if (writer) { memset(writer, 0, sizeof(*writer)); } } -void rpc_writer_reset(rpc_writer *writer) { +void wirecall_writer_reset(wirecall_writer *writer) { if (writer) { writer->len = 0; } } -void rpc_writer_free(rpc_writer *writer) { +void wirecall_writer_free(wirecall_writer *writer) { if (writer) { - rpc_mem_free(writer->data); + wirecall_mem_free(writer->data); memset(writer, 0, sizeof(*writer)); } } -int rpc_writer_null(rpc_writer *writer) { - uint8_t type = RPC_TYPE_NULL; +int wirecall_writer_null(wirecall_writer *writer) { + uint8_t type = WIRECALL_TYPE_NULL; return writer_push(writer, &type, 1); } -int rpc_writer_bool(rpc_writer *writer, bool value) { - uint8_t data[2] = {RPC_TYPE_BOOL, value ? 1u : 0u}; +int wirecall_writer_bool(wirecall_writer *writer, bool value) { + uint8_t data[2] = {WIRECALL_TYPE_BOOL, value ? 1u : 0u}; return writer_push(writer, data, sizeof(data)); } -int rpc_writer_i64(rpc_writer *writer, int64_t value) { +int wirecall_writer_i64(wirecall_writer *writer, int64_t value) { uint8_t data[9]; - data[0] = RPC_TYPE_I64; + data[0] = WIRECALL_TYPE_I64; put_u64(data + 1, (uint64_t)value); return writer_push(writer, data, sizeof(data)); } -int rpc_writer_u64(rpc_writer *writer, uint64_t value) { +int wirecall_writer_u64(wirecall_writer *writer, uint64_t value) { uint8_t data[9]; - data[0] = RPC_TYPE_U64; + data[0] = WIRECALL_TYPE_U64; put_u64(data + 1, value); return writer_push(writer, data, sizeof(data)); } -int rpc_writer_f64(rpc_writer *writer, double value) { +int wirecall_writer_f64(wirecall_writer *writer, double value) { uint8_t data[9]; uint64_t bits = 0; memcpy(&bits, &value, sizeof(bits)); - data[0] = RPC_TYPE_F64; + data[0] = WIRECALL_TYPE_F64; put_u64(data + 1, bits); return writer_push(writer, data, sizeof(data)); } -int rpc_writer_bytes(rpc_writer *writer, const void *data, uint32_t len) { +int wirecall_writer_bytes(wirecall_writer *writer, const void *data, uint32_t len) { uint8_t prefix[5]; if (len > 0 && !data) { return -1; } - prefix[0] = RPC_TYPE_BYTES; + prefix[0] = WIRECALL_TYPE_BYTES; put_u32(prefix + 1, len); if (writer_push(writer, prefix, sizeof(prefix)) != 0) { return -1; } return writer_push(writer, data, len); } -int rpc_writer_string(rpc_writer *writer, const char *data, uint32_t len) { +int wirecall_writer_string(wirecall_writer *writer, const char *data, uint32_t len) { uint8_t prefix[5]; if (len > 0 && !data) { return -1; } - prefix[0] = RPC_TYPE_STRING; + prefix[0] = WIRECALL_TYPE_STRING; put_u32(prefix + 1, len); if (writer_push(writer, prefix, sizeof(prefix)) != 0) { return -1; } return writer_push(writer, data, len); } -int rpc_payload_decode(const uint8_t *data, size_t len, rpc_value **out_values, size_t *out_count) { - uint64_t trace = rpc_trace_begin(); +int wirecall_payload_decode(const uint8_t *data, size_t len, wirecall_value **out_values, size_t *out_count) { + uint64_t trace = wirecall_trace_begin(); static const void *dispatch[] = { - [RPC_TYPE_NULL] = &&type_null, [RPC_TYPE_BOOL] = &&type_bool, [RPC_TYPE_I64] = &&type_i64, - [RPC_TYPE_U64] = &&type_u64, [RPC_TYPE_F64] = &&type_f64, [RPC_TYPE_BYTES] = &&type_bytes, - [RPC_TYPE_STRING] = &&type_string, + [WIRECALL_TYPE_NULL] = &&type_null, [WIRECALL_TYPE_BOOL] = &&type_bool, [WIRECALL_TYPE_I64] = &&type_i64, + [WIRECALL_TYPE_U64] = &&type_u64, [WIRECALL_TYPE_F64] = &&type_f64, [WIRECALL_TYPE_BYTES] = &&type_bytes, + [WIRECALL_TYPE_STRING] = &&type_string, }; if ((!data && len > 0) || !out_values || !out_count) { - rpc_trace_end(RPC_TRACE_PAYLOAD_DECODE, trace); + wirecall_trace_end(WIRECALL_TRACE_PAYLOAD_DECODE, trace); return -1; } - rpc_value *values = NULL; + wirecall_value *values = NULL; size_t count = 0; size_t cap = 0; size_t off = 0; @@ -145,19 +145,19 @@ int rpc_payload_decode(const uint8_t *data, size_t len, rpc_value **out_values, while (off < len) { if (count == cap) { size_t next_cap = cap ? cap * 2u : 4u; - rpc_value *next = rpc_mem_realloc(values, next_cap * sizeof(*values)); + wirecall_value *next = wirecall_mem_realloc(values, next_cap * sizeof(*values)); if (!next) { - rpc_mem_free(values); - rpc_trace_end(RPC_TRACE_PAYLOAD_DECODE, trace); + wirecall_mem_free(values); + wirecall_trace_end(WIRECALL_TRACE_PAYLOAD_DECODE, trace); return -1; } values = next; cap = next_cap; } - rpc_value value; + wirecall_value value; memset(&value, 0, sizeof(value)); - value.type = (rpc_type)data[off++]; + value.type = (wirecall_type)data[off++]; if ((size_t)value.type >= sizeof(dispatch) / sizeof(*dispatch) || !dispatch[value.type]) { goto malformed; } goto *dispatch[value.type]; @@ -196,7 +196,7 @@ int rpc_payload_decode(const uint8_t *data, size_t len, rpc_value **out_values, uint32_t value_len = get_u32(data + off); off += 4u; if (off + value_len > len) { goto malformed; } - if (value.type == RPC_TYPE_BYTES) { + if (value.type == WIRECALL_TYPE_BYTES) { value.as.bytes.data = data + off; value.as.bytes.len = value_len; } else { @@ -212,17 +212,17 @@ int rpc_payload_decode(const uint8_t *data, size_t len, rpc_value **out_values, continue; malformed: - rpc_mem_free(values); - rpc_trace_end(RPC_TRACE_PAYLOAD_DECODE, trace); + wirecall_mem_free(values); + wirecall_trace_end(WIRECALL_TRACE_PAYLOAD_DECODE, trace); return -1; } *out_values = values; *out_count = count; - rpc_trace_end(RPC_TRACE_PAYLOAD_DECODE, trace); + wirecall_trace_end(WIRECALL_TRACE_PAYLOAD_DECODE, trace); return 0; } -void rpc_values_free(rpc_value *values) { - rpc_mem_free(values); +void wirecall_values_free(wirecall_value *values) { + wirecall_mem_free(values); } diff --git a/src/protocol.c b/src/protocol.c index fa48519..dfd688e 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -1,10 +1,10 @@ -#include "rpc/protocol.h" +#include "wirecall/protocol.h" #include "proc.h" #include -uint64_t rpc_proc_id(const char *name) { +uint64_t wirecall_proc_id(const char *name) { uint64_t hash = 14695981039346656037ull; if (!name) { return 0; } while (*name) { @@ -58,9 +58,9 @@ static uint64_t mix64(uint64_t value) { return value; } -static uint64_t packet_seed(const rpc_header *header) { - uint64_t meta = ((uint64_t)(uint8_t)header->op << 56u) | ((uint64_t)header->flags << 48u) | - ((uint64_t)header->size << 16u); +static uint64_t packet_seed(const wirecall_header *header) { + uint64_t meta = + ((uint64_t)(uint8_t)header->op << 56u) | ((uint64_t)header->flags << 48u) | ((uint64_t)header->size << 16u); return mix64(0x9e3779b97f4a7c15ull ^ meta ^ rotl64(header->proc_id, 17u) ^ rotl64(header->call_id, 41u)); } @@ -136,12 +136,12 @@ static void sip_update(uint64_t *v0, uint64_t *v1, uint64_t *v2, uint64_t *v3, c } static int valid_op(uint8_t op) { - return op == RPC_OP_RPC || op == RPC_OP_PING || op == RPC_OP_DISCONNECT || op == RPC_OP_RESPONSE || - op == RPC_OP_ERROR; + return op == WIRECALL_OP_RPC || op == WIRECALL_OP_PING || op == WIRECALL_OP_DISCONNECT || + op == WIRECALL_OP_RESPONSE || op == WIRECALL_OP_ERROR; } -int rpc_header_encode(const rpc_header *header, uint8_t out[RPC_HEADER_SIZE]) { - if (!header || !out || !valid_op((uint8_t)header->op) || header->size > RPC_MAX_PAYLOAD_SIZE) { return -1; } +int wirecall_header_encode(const wirecall_header *header, uint8_t out[WIRECALL_HEADER_SIZE]) { + if (!header || !out || !valid_op((uint8_t)header->op) || header->size > WIRECALL_MAX_PAYLOAD_SIZE) { return -1; } out[0] = (uint8_t)header->op; out[1] = header->flags; @@ -153,31 +153,32 @@ int rpc_header_encode(const rpc_header *header, uint8_t out[RPC_HEADER_SIZE]) { return 0; } -int rpc_header_decode(const uint8_t in[RPC_HEADER_SIZE], rpc_header *out) { +int wirecall_header_decode(const uint8_t in[WIRECALL_HEADER_SIZE], wirecall_header *out) { if (!in || !out || !valid_op(in[0])) { return -1; } memset(out, 0, sizeof(*out)); - out->op = (rpc_op)in[0]; + out->op = (wirecall_op)in[0]; out->flags = in[1]; out->proc_id = get_u64(in + 2); out->size = get_u32(in + 10); out->call_id = get_u64(in + 14); out->checksum = get_u32(in + 22); out->mac = get_u64(in + 26); - if (out->size > RPC_MAX_PAYLOAD_SIZE) { return -1; } + if (out->size > WIRECALL_MAX_PAYLOAD_SIZE) { return -1; } return 0; } -uint32_t rpc_packet_checksum(const rpc_header *header, const uint8_t *payload, size_t payload_len) { - if (!header || (!payload && payload_len > 0) || payload_len > RPC_MAX_PAYLOAD_SIZE) { return 0; } +uint32_t wirecall_packet_checksum(const wirecall_header *header, const uint8_t *payload, size_t payload_len) { + if (!header || (!payload && payload_len > 0) || payload_len > WIRECALL_MAX_PAYLOAD_SIZE) { return 0; } uint64_t hash = packet_seed(header) ^ 0xa0761d6478bd642full; hash = cheap_hash_update(hash, payload, payload_len); return (uint32_t)(hash ^ (hash >> 32u)); } -uint64_t rpc_packet_mac(const rpc_header *header, const uint8_t *payload, size_t payload_len, const uint8_t key[16]) { - if (!header || !key || (!payload && payload_len > 0) || payload_len > RPC_MAX_PAYLOAD_SIZE) { return 0; } +uint64_t wirecall_packet_mac(const wirecall_header *header, const uint8_t *payload, size_t payload_len, + const uint8_t key[16]) { + if (!header || !key || (!payload && payload_len > 0) || payload_len > WIRECALL_MAX_PAYLOAD_SIZE) { return 0; } uint64_t k0 = sip_load64(key); uint64_t k1 = sip_load64(key + 8); @@ -209,35 +210,38 @@ uint64_t rpc_packet_mac(const rpc_header *header, const uint8_t *payload, size_t return v0 ^ v1 ^ v2 ^ v3; } -int rpc_packet_sign(rpc_header *header, const uint8_t *payload, size_t payload_len) { - return rpc_packet_sign_ex(header, payload, payload_len, RPC_INTEGRITY_DEFAULT, NULL); +int wirecall_packet_sign(wirecall_header *header, const uint8_t *payload, size_t payload_len) { + return wirecall_packet_sign_ex(header, payload, payload_len, WIRECALL_INTEGRITY_DEFAULT, NULL); } -int rpc_packet_verify(const rpc_header *header, const uint8_t *payload, size_t payload_len) { - return rpc_packet_verify_ex(header, payload, payload_len, RPC_INTEGRITY_DEFAULT, NULL); +int wirecall_packet_verify(const wirecall_header *header, const uint8_t *payload, size_t payload_len) { + return wirecall_packet_verify_ex(header, payload, payload_len, WIRECALL_INTEGRITY_DEFAULT, NULL); } -int rpc_packet_sign_ex(rpc_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, - const uint8_t mac_key[16]) { - if (!header || (!payload && payload_len > 0) || payload_len > RPC_MAX_PAYLOAD_SIZE || - header->size != payload_len || ((integrity & RPC_INTEGRITY_MAC) && !mac_key)) { +int wirecall_packet_sign_ex(wirecall_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, + const uint8_t mac_key[16]) { + if (!header || (!payload && payload_len > 0) || payload_len > WIRECALL_MAX_PAYLOAD_SIZE || + header->size != payload_len || ((integrity & WIRECALL_INTEGRITY_MAC) && !mac_key)) { return -1; } - header->checksum = (integrity & RPC_INTEGRITY_CHECKSUM) ? rpc_packet_checksum(header, payload, payload_len) : 0; - header->mac = (integrity & RPC_INTEGRITY_MAC) ? rpc_packet_mac(header, payload, payload_len, mac_key) : 0; + header->checksum = + (integrity & WIRECALL_INTEGRITY_CHECKSUM) ? wirecall_packet_checksum(header, payload, payload_len) : 0; + header->mac = (integrity & WIRECALL_INTEGRITY_MAC) ? wirecall_packet_mac(header, payload, payload_len, mac_key) : 0; return 0; } -int rpc_packet_verify_ex(const rpc_header *header, const uint8_t *payload, size_t payload_len, uint32_t integrity, - const uint8_t mac_key[16]) { - if (!header || (!payload && payload_len > 0) || payload_len > RPC_MAX_PAYLOAD_SIZE || - header->size != payload_len || ((integrity & RPC_INTEGRITY_MAC) && !mac_key)) { +int wirecall_packet_verify_ex(const wirecall_header *header, const uint8_t *payload, size_t payload_len, + uint32_t integrity, const uint8_t mac_key[16]) { + if (!header || (!payload && payload_len > 0) || payload_len > WIRECALL_MAX_PAYLOAD_SIZE || + header->size != payload_len || ((integrity & WIRECALL_INTEGRITY_MAC) && !mac_key)) { return -1; } - if ((integrity & RPC_INTEGRITY_CHECKSUM) && rpc_packet_checksum(header, payload, payload_len) != header->checksum) { + if ((integrity & WIRECALL_INTEGRITY_CHECKSUM) && + wirecall_packet_checksum(header, payload, payload_len) != header->checksum) { return -1; } - if ((integrity & RPC_INTEGRITY_MAC) && rpc_packet_mac(header, payload, payload_len, mac_key) != header->mac) { + if ((integrity & WIRECALL_INTEGRITY_MAC) && + wirecall_packet_mac(header, payload, payload_len, mac_key) != header->mac) { return -1; } return 0; diff --git a/src/routes.c b/src/routes.c index 170808f..a46e54b 100644 --- a/src/routes.c +++ b/src/routes.c @@ -1,5 +1,5 @@ #include "routes.h" -#include "rpc/trace.h" +#include "wirecall/trace.h" #include "memory.h" @@ -13,19 +13,19 @@ static void *route_mmap(size_t bytes) { return p == MAP_FAILED ? NULL : p; } -static rpc_route_slot *route_page_alloc(const rpc_routes *routes) { +static wirecall_route_slot *route_page_alloc(const wirecall_routes *routes) { return route_mmap(routes->page_bytes); } -static void route_finalize(const rpc_route *route) { +static void route_finalize(const wirecall_route *route) { if (route && route->finalizer) { route->finalizer(route->user_data); } } -static void route_chain_free(rpc_route *route, int finalize_all, int finalize_one, uint64_t finalize_proc_id) { +static void route_chain_free(wirecall_route *route, int finalize_all, int finalize_one, uint64_t finalize_proc_id) { while (route) { - rpc_route *next = route->next; + wirecall_route *next = route->next; if (finalize_all || (finalize_one && route->proc_id == finalize_proc_id)) { route_finalize(route); } - rpc_mem_free(route); + wirecall_mem_free(route); route = next; } } @@ -39,28 +39,28 @@ static uint32_t route_index(uint64_t proc_id) { return (uint32_t)proc_id; } -static void free_retired(rpc_routes *routes) { +static void free_retired(wirecall_routes *routes) { if (atomic_load_explicit(&routes->active_readers, memory_order_acquire) != 0) return; - rpc_retired_route *node = routes->retired; + wirecall_retired_route *node = routes->retired; routes->retired = NULL; while (node) { - rpc_retired_route *next = node->next; + wirecall_retired_route *next = node->next; route_chain_free(node->route, node->finalize_all, node->finalize_one, node->finalize_proc_id); - rpc_mem_free(node); + wirecall_mem_free(node); node = next; } } -static int retire_route(rpc_routes *routes, rpc_route *route, int finalize_one, uint64_t finalize_proc_id) { +static int retire_route(wirecall_routes *routes, wirecall_route *route, int finalize_one, uint64_t finalize_proc_id) { if (!route) return 0; if (atomic_load_explicit(&routes->active_readers, memory_order_acquire) == 0) { route_chain_free(route, 0, finalize_one, finalize_proc_id); return 0; } - rpc_retired_route *node = rpc_mem_calloc(1, sizeof(*node)); + wirecall_retired_route *node = wirecall_mem_calloc(1, sizeof(*node)); if (!node) return -1; node->route = route; @@ -73,12 +73,12 @@ static int retire_route(rpc_routes *routes, rpc_route *route, int finalize_one, return 0; } -int rpc_routes_init(rpc_routes *routes) { +int wirecall_routes_init(wirecall_routes *routes) { if (!routes) return -1; memset(routes, 0, sizeof(*routes)); - routes->root_bytes = RPC_ROUTE_ROOT_SIZE * sizeof(*routes->pages); - routes->page_bytes = RPC_ROUTE_PAGE_SIZE * sizeof(rpc_route_slot); + routes->root_bytes = WIRECALL_ROUTE_ROOT_SIZE * sizeof(*routes->pages); + routes->page_bytes = WIRECALL_ROUTE_PAGE_SIZE * sizeof(wirecall_route_slot); routes->pages = route_mmap(routes->root_bytes); if (!routes->pages) return -1; @@ -94,18 +94,18 @@ int rpc_routes_init(rpc_routes *routes) { return 0; } -void rpc_routes_destroy(rpc_routes *routes) { +void wirecall_routes_destroy(wirecall_routes *routes) { if (!routes) return; pthread_mutex_lock(&routes->mutate_lock); if (!routes->pages) goto retired; - for (size_t page_idx = 0; page_idx < RPC_ROUTE_ROOT_SIZE; ++page_idx) { - rpc_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_relaxed); + for (size_t page_idx = 0; page_idx < WIRECALL_ROUTE_ROOT_SIZE; ++page_idx) { + wirecall_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_relaxed); if (!page) continue; - for (size_t i = 0; i < RPC_ROUTE_PAGE_SIZE; ++i) { - rpc_route *route = atomic_load_explicit(&page[i], memory_order_relaxed); + for (size_t i = 0; i < WIRECALL_ROUTE_PAGE_SIZE; ++i) { + wirecall_route *route = atomic_load_explicit(&page[i], memory_order_relaxed); route_chain_free(route, 1, 0, 0); } munmap(page, routes->page_bytes); @@ -113,12 +113,12 @@ void rpc_routes_destroy(rpc_routes *routes) { munmap(routes->pages, routes->root_bytes); retired: - rpc_retired_route *node = routes->retired; + wirecall_retired_route *node = routes->retired; while (node) { - rpc_retired_route *next = node->next; + wirecall_retired_route *next = node->next; route_chain_free(node->route, node->finalize_all, node->finalize_one, node->finalize_proc_id); - rpc_mem_free(node); + wirecall_mem_free(node); node = next; } @@ -127,47 +127,47 @@ retired: pthread_mutex_destroy(&routes->mutate_lock); } -int rpc_routes_add(rpc_routes *routes, uint64_t proc_id, rpc_handler_fn handler, void *user_data) { - return rpc_routes_add_ex(routes, proc_id, handler, user_data, NULL, 0); +int wirecall_routes_add(wirecall_routes *routes, uint64_t proc_id, wirecall_handler_fn handler, void *user_data) { + return wirecall_routes_add_ex(routes, proc_id, handler, user_data, NULL, 0); } -int rpc_routes_add_ex(rpc_routes *routes, uint64_t proc_id, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer, int is_async) { +int wirecall_routes_add_ex(wirecall_routes *routes, uint64_t proc_id, wirecall_handler_fn handler, void *user_data, + wirecall_route_finalizer_fn finalizer, int is_async) { if (!routes || !handler) return -1; - rpc_route *route = rpc_mem_calloc(1, sizeof(*route)); + wirecall_route *route = wirecall_mem_calloc(1, sizeof(*route)); if (!route) return -1; - *route = (rpc_route){.proc_id = proc_id, - .handler = handler, - .user_data = user_data, - .finalizer = finalizer, - .is_async = is_async ? 1 : 0}; + *route = (wirecall_route){.proc_id = proc_id, + .handler = handler, + .user_data = user_data, + .finalizer = finalizer, + .is_async = is_async ? 1 : 0}; pthread_mutex_lock(&routes->mutate_lock); uint32_t index = route_index(proc_id); - uint32_t page_idx = index >> RPC_ROUTE_PAGE_BITS; - uint32_t slot_idx = index & RPC_ROUTE_PAGE_MASK; - rpc_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); + uint32_t page_idx = index >> WIRECALL_ROUTE_PAGE_BITS; + uint32_t slot_idx = index & WIRECALL_ROUTE_PAGE_MASK; + wirecall_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); if (!page) { page = route_page_alloc(routes); if (!page) { pthread_mutex_unlock(&routes->mutate_lock); - rpc_mem_free(route); + wirecall_mem_free(route); return -1; } atomic_store_explicit(&routes->pages[page_idx], page, memory_order_release); } - rpc_route *old = atomic_load_explicit(&page[slot_idx], memory_order_acquire); - rpc_route *copy_head = route; - rpc_route **copy_tail = &route->next; + wirecall_route *old = atomic_load_explicit(&page[slot_idx], memory_order_acquire); + wirecall_route *copy_head = route; + wirecall_route **copy_tail = &route->next; int replaced = 0; - for (rpc_route *it = old; it; it = it->next) { + for (wirecall_route *it = old; it; it = it->next) { if (it->proc_id == proc_id) { replaced = 1; continue; } - rpc_route *copy = rpc_mem_calloc(1, sizeof(*copy)); + wirecall_route *copy = wirecall_mem_calloc(1, sizeof(*copy)); if (!copy) { route_chain_free(copy_head, 0, 0, 0); pthread_mutex_unlock(&routes->mutate_lock); @@ -186,24 +186,24 @@ int rpc_routes_add_ex(rpc_routes *routes, uint64_t proc_id, rpc_handler_fn handl return rc; } -int rpc_routes_remove(rpc_routes *routes, uint64_t proc_id) { +int wirecall_routes_remove(wirecall_routes *routes, uint64_t proc_id) { if (!routes) return -1; pthread_mutex_lock(&routes->mutate_lock); uint32_t index = route_index(proc_id); - uint32_t page_idx = index >> RPC_ROUTE_PAGE_BITS; - uint32_t slot_idx = index & RPC_ROUTE_PAGE_MASK; - rpc_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); - rpc_route *old = page ? atomic_load_explicit(&page[slot_idx], memory_order_acquire) : NULL; - rpc_route *copy_head = NULL; - rpc_route **copy_tail = ©_head; + uint32_t page_idx = index >> WIRECALL_ROUTE_PAGE_BITS; + uint32_t slot_idx = index & WIRECALL_ROUTE_PAGE_MASK; + wirecall_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); + wirecall_route *old = page ? atomic_load_explicit(&page[slot_idx], memory_order_acquire) : NULL; + wirecall_route *copy_head = NULL; + wirecall_route **copy_tail = ©_head; int removed = 0; - for (rpc_route *it = old; it; it = it->next) { + for (wirecall_route *it = old; it; it = it->next) { if (it->proc_id == proc_id) { removed = 1; continue; } - rpc_route *copy = rpc_mem_calloc(1, sizeof(*copy)); + wirecall_route *copy = wirecall_mem_calloc(1, sizeof(*copy)); if (!copy) { route_chain_free(copy_head, 0, 0, 0); pthread_mutex_unlock(&routes->mutate_lock); @@ -226,19 +226,19 @@ int rpc_routes_remove(rpc_routes *routes, uint64_t proc_id) { return rc; } -int rpc_routes_lookup(rpc_routes *routes, uint64_t proc_id, rpc_route *out) { - uint64_t trace = rpc_trace_begin(); +int wirecall_routes_lookup(wirecall_routes *routes, uint64_t proc_id, wirecall_route *out) { + uint64_t trace = wirecall_trace_begin(); if (!routes || !out) { - rpc_trace_end(RPC_TRACE_ROUTE_LOOKUP, trace); + wirecall_trace_end(WIRECALL_TRACE_ROUTE_LOOKUP, trace); return -1; } atomic_fetch_add_explicit(&routes->active_readers, 1u, memory_order_acquire); uint32_t index = route_index(proc_id); - uint32_t page_idx = index >> RPC_ROUTE_PAGE_BITS; - uint32_t slot_idx = index & RPC_ROUTE_PAGE_MASK; - rpc_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); - rpc_route *route = page ? atomic_load_explicit(&page[slot_idx], memory_order_acquire) : NULL; + uint32_t page_idx = index >> WIRECALL_ROUTE_PAGE_BITS; + uint32_t slot_idx = index & WIRECALL_ROUTE_PAGE_MASK; + wirecall_route_slot *page = atomic_load_explicit(&routes->pages[page_idx], memory_order_acquire); + wirecall_route *route = page ? atomic_load_explicit(&page[slot_idx], memory_order_acquire) : NULL; while (route && route->proc_id != proc_id) { route = route->next; } @@ -249,6 +249,6 @@ int rpc_routes_lookup(rpc_routes *routes, uint64_t proc_id, rpc_route *out) { free_retired(routes); pthread_mutex_unlock(&routes->mutate_lock); } - rpc_trace_end(RPC_TRACE_ROUTE_LOOKUP, trace); + wirecall_trace_end(WIRECALL_TRACE_ROUTE_LOOKUP, trace); return route ? 0 : -1; } diff --git a/src/scheduler.c b/src/scheduler.c index d996e37..d5803a0 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -2,13 +2,13 @@ #include "arena.h" #include "memory.h" -#include "rpc/trace.h" +#include "wirecall/trace.h" #define MCO_USE_VMEM_ALLOCATOR #define MCO_ZERO_MEMORY #define MCO_DEFAULT_STACK_SIZE (1024 * 1024) -#define MCO_ALLOC(size) rpc_mem_calloc(1, size) -#define MCO_DEALLOC(ptr, size) rpc_mem_free(ptr) +#define MCO_ALLOC(size) wirecall_mem_calloc(1, size) +#define MCO_DEALLOC(ptr, size) wirecall_mem_free(ptr) #define MINICORO_IMPL #include "minicoro.h" @@ -16,42 +16,42 @@ #include #include -#define RPC_CALL_ARENA_CAPACITY 262144u +#define WIRECALL_CALL_ARENA_CAPACITY 262144u -struct rpc_scheduler { - rpc_call *head; - rpc_call *tail; - rpc_fixed_arena call_arena; +struct wirecall_scheduler { + wirecall_call *head; + wirecall_call *tail; + wirecall_fixed_arena call_arena; }; -struct rpc_call { +struct wirecall_call { mco_coro *co; - rpc_ctx ctx; - rpc_handler_fn handler; + wirecall_ctx ctx; + wirecall_handler_fn handler; void *handler_data; uint8_t *payload; size_t payload_len; - rpc_value *args; + wirecall_value *args; size_t argc; - rpc_writer response; + wirecall_writer response; int result; char error[160]; int completed; - rpc_call_done_fn done; + wirecall_call_done_fn done; void *done_data; - rpc_call *next_ready; + wirecall_call *next_ready; }; -static void call_free(rpc_scheduler *scheduler, rpc_call *call) { +static void call_free(wirecall_scheduler *scheduler, wirecall_call *call) { if (!call) { return; } if (call->co) { (void)mco_destroy(call->co); } - rpc_values_free(call->args); - rpc_writer_free(&call->response); - rpc_mem_free(call->payload); - rpc_fixed_arena_free(&scheduler->call_arena, call); + wirecall_values_free(call->args); + wirecall_writer_free(&call->response); + wirecall_mem_free(call->payload); + wirecall_fixed_arena_free(&scheduler->call_arena, call); } -static int enqueue(rpc_scheduler *scheduler, rpc_call *call) { +static int enqueue(wirecall_scheduler *scheduler, wirecall_call *call) { call->next_ready = NULL; if (scheduler->tail) { scheduler->tail->next_ready = call; @@ -62,8 +62,8 @@ static int enqueue(rpc_scheduler *scheduler, rpc_call *call) { return 0; } -static rpc_call *dequeue(rpc_scheduler *scheduler) { - rpc_call *call = scheduler->head; +static wirecall_call *dequeue(wirecall_scheduler *scheduler) { + wirecall_call *call = scheduler->head; if (!call) { return NULL; } scheduler->head = call->next_ready; if (!scheduler->head) { scheduler->tail = NULL; } @@ -72,7 +72,7 @@ static rpc_call *dequeue(rpc_scheduler *scheduler) { } static void call_entry(mco_coro *co) { - rpc_call *call = mco_get_user_data(co); + wirecall_call *call = mco_get_user_data(co); call->result = call->handler(&call->ctx, call->args, call->argc, &call->response, call->handler_data); call->completed = 1; if (call->result != 0 && call->error[0] == '\0') { @@ -80,15 +80,15 @@ static void call_entry(mco_coro *co) { } } -static void call_finish(rpc_scheduler *scheduler, rpc_call *call) { +static void call_finish(wirecall_scheduler *scheduler, wirecall_call *call) { if (call->done) { call->done(call, call->done_data); } call_free(scheduler, call); } -static int call_resume(rpc_call *call) { - uint64_t trace_resume = rpc_trace_begin(); +static int call_resume(wirecall_call *call) { + uint64_t trace_resume = wirecall_trace_begin(); mco_result rc = mco_resume(call->co); - rpc_trace_end(RPC_TRACE_SCHED_RESUME, trace_resume); + wirecall_trace_end(WIRECALL_TRACE_SCHED_RESUME, trace_resume); if (rc != MCO_SUCCESS) { snprintf(call->error, sizeof(call->error), "coroutine resume failed: %s", mco_result_description(rc)); call->result = -1; @@ -98,40 +98,40 @@ static int call_resume(rpc_call *call) { return 0; } -int rpc_scheduler_init(rpc_scheduler **out) { +int wirecall_scheduler_init(wirecall_scheduler **out) { if (!out) { return -1; } - rpc_scheduler *scheduler = rpc_mem_calloc(1, sizeof(*scheduler)); + wirecall_scheduler *scheduler = wirecall_mem_calloc(1, sizeof(*scheduler)); if (!scheduler) { return -1; } - if (rpc_fixed_arena_init(&scheduler->call_arena, sizeof(rpc_call), RPC_CALL_ARENA_CAPACITY) != 0) { - rpc_mem_free(scheduler); + if (wirecall_fixed_arena_init(&scheduler->call_arena, sizeof(wirecall_call), WIRECALL_CALL_ARENA_CAPACITY) != 0) { + wirecall_mem_free(scheduler); return -1; } *out = scheduler; return 0; } -void rpc_scheduler_destroy(rpc_scheduler *scheduler) { +void wirecall_scheduler_destroy(wirecall_scheduler *scheduler) { if (!scheduler) { return; } - rpc_call *call = NULL; + wirecall_call *call = NULL; while ((call = dequeue(scheduler)) != NULL) { call_free(scheduler, call); } - rpc_fixed_arena_destroy(&scheduler->call_arena); - rpc_mem_free(scheduler); + wirecall_fixed_arena_destroy(&scheduler->call_arena); + wirecall_mem_free(scheduler); } -int rpc_scheduler_submit(rpc_scheduler *scheduler, uint64_t call_id, uint64_t proc_id, rpc_handler_fn handler, - void *handler_data, const uint8_t *payload, size_t payload_len, rpc_call_done_fn done, - void *done_data) { - uint64_t trace_submit = rpc_trace_begin(); +int wirecall_scheduler_submit(wirecall_scheduler *scheduler, uint64_t call_id, uint64_t proc_id, + wirecall_handler_fn handler, void *handler_data, const uint8_t *payload, + size_t payload_len, wirecall_call_done_fn done, void *done_data) { + uint64_t trace_submit = wirecall_trace_begin(); if (!scheduler || !handler || (!payload && payload_len > 0)) { - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return -1; } - rpc_call *call = rpc_fixed_arena_alloc(&scheduler->call_arena); + wirecall_call *call = wirecall_fixed_arena_alloc(&scheduler->call_arena); if (!call) { - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return -1; } call->ctx.call_id = call_id; @@ -141,59 +141,59 @@ int rpc_scheduler_submit(rpc_scheduler *scheduler, uint64_t call_id, uint64_t pr call->payload_len = payload_len; call->done = done; call->done_data = done_data; - rpc_writer_init(&call->response); + wirecall_writer_init(&call->response); if (payload_len > 0) { - call->payload = rpc_mem_alloc(payload_len); + call->payload = wirecall_mem_alloc(payload_len); if (!call->payload) { call_free(scheduler, call); - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return -1; } memcpy(call->payload, payload, payload_len); } - uint64_t trace_decode = rpc_trace_begin(); - if (rpc_payload_decode(call->payload, call->payload_len, &call->args, &call->argc) != 0) { - rpc_trace_end(RPC_TRACE_SCHED_DECODE, trace_decode); + uint64_t trace_decode = wirecall_trace_begin(); + if (wirecall_payload_decode(call->payload, call->payload_len, &call->args, &call->argc) != 0) { + wirecall_trace_end(WIRECALL_TRACE_SCHED_DECODE, trace_decode); snprintf(call->error, sizeof(call->error), "malformed payload"); call->result = -1; call->completed = 1; done(call, done_data); call_free(scheduler, call); - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return 0; } - rpc_trace_end(RPC_TRACE_SCHED_DECODE, trace_decode); + wirecall_trace_end(WIRECALL_TRACE_SCHED_DECODE, trace_decode); mco_desc desc = mco_desc_init(call_entry, 0); desc.user_data = call; - uint64_t trace_create = rpc_trace_begin(); + uint64_t trace_create = wirecall_trace_begin(); mco_result rc = mco_create(&call->co, &desc); - rpc_trace_end(RPC_TRACE_SCHED_CORO_CREATE, trace_create); + wirecall_trace_end(WIRECALL_TRACE_SCHED_CORO_CREATE, trace_create); if (rc != MCO_SUCCESS) { snprintf(call->error, sizeof(call->error), "coroutine create failed: %s", mco_result_description(rc)); call->result = -1; call->completed = 1; done(call, done_data); call_free(scheduler, call); - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return 0; } if (enqueue(scheduler, call) != 0) { call_free(scheduler, call); - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return -1; } - rpc_trace_end(RPC_TRACE_SCHED_SUBMIT, trace_submit); + wirecall_trace_end(WIRECALL_TRACE_SCHED_SUBMIT, trace_submit); return 0; } -void rpc_scheduler_run_ready(rpc_scheduler *scheduler) { +void wirecall_scheduler_run_ready(wirecall_scheduler *scheduler) { if (!scheduler) { return; } - rpc_call *call = NULL; + wirecall_call *call = NULL; while ((call = dequeue(scheduler)) != NULL) { (void)call_resume(call); @@ -208,35 +208,35 @@ void rpc_scheduler_run_ready(rpc_scheduler *scheduler) { } } -int rpc_call_result(const rpc_call *call) { +int wirecall_call_result(const wirecall_call *call) { return call ? call->result : -1; } -const rpc_writer *rpc_call_response(const rpc_call *call) { +const wirecall_writer *wirecall_call_response(const wirecall_call *call) { return call ? &call->response : NULL; } -const char *rpc_call_error(const rpc_call *call) { +const char *wirecall_call_error(const wirecall_call *call) { return call && call->error[0] ? call->error : "procedure failed"; } -uint64_t rpc_call_id(const rpc_call *call) { +uint64_t wirecall_call_id(const wirecall_call *call) { return call ? call->ctx.call_id : 0; } -uint64_t rpc_call_proc_id(const rpc_call *call) { +uint64_t wirecall_call_proc_id(const wirecall_call *call) { return call ? call->ctx.proc_id : 0; } -uint64_t rpc_ctx_call_id(const rpc_ctx *ctx) { +uint64_t wirecall_ctx_call_id(const wirecall_ctx *ctx) { return ctx ? ctx->call_id : 0; } -uint64_t rpc_ctx_proc_id(const rpc_ctx *ctx) { +uint64_t wirecall_ctx_proc_id(const wirecall_ctx *ctx) { return ctx ? ctx->proc_id : 0; } -void rpc_ctx_yield(rpc_ctx *ctx) { +void wirecall_ctx_yield(wirecall_ctx *ctx) { (void)ctx; mco_coro *co = mco_running(); if (co) { (void)mco_yield(co); } diff --git a/src/server.c b/src/server.c index 75e402d..72e327a 100644 --- a/src/server.c +++ b/src/server.c @@ -1,5 +1,5 @@ -#include "rpc/server.h" -#include "rpc/trace.h" +#include "wirecall/server.h" +#include "wirecall/trace.h" #include "arena.h" #include "backend.h" @@ -21,22 +21,22 @@ #include #include -#define RPC_MAX_LISTENERS 8 -#define RPC_READ_CHUNK 4096 -#define RPC_EVENT_BATCH 1024 -#define RPC_CONNECTION_ARENA_CAPACITY 65536u -#define RPC_MAX_WORKERS 128u +#define WIRECALL_MAX_LISTENERS 8 +#define WIRECALL_READ_CHUNK 4096 +#define WIRECALL_EVENT_BATCH 1024 +#define WIRECALL_CONNECTION_ARENA_CAPACITY 65536u +#define WIRECALL_MAX_WORKERS 128u -typedef struct rpc_worker rpc_worker; +typedef struct wirecall_worker wirecall_worker; -typedef struct rpc_listener { +typedef struct wirecall_listener { int fd; - rpc_worker *worker; -} rpc_listener; + wirecall_worker *worker; +} wirecall_listener; -typedef struct rpc_connection { +typedef struct wirecall_connection { int fd; - rpc_worker *worker; + wirecall_worker *worker; uint8_t *read_buf; size_t read_len; size_t read_cap; @@ -47,36 +47,36 @@ typedef struct rpc_connection { uint32_t interests; int closing; int write_queued; - struct rpc_connection *next; - struct rpc_connection *write_next; -} rpc_connection; + struct wirecall_connection *next; + struct wirecall_connection *write_next; +} wirecall_connection; -typedef struct rpc_pending_fd { +typedef struct wirecall_pending_fd { int fd; - struct rpc_pending_fd *next; -} rpc_pending_fd; - -struct rpc_worker { - rpc_server *server; - rpc_backend *backend; - rpc_scheduler *scheduler; - rpc_fixed_arena connection_arena; - rpc_listener listeners[RPC_MAX_LISTENERS]; + struct wirecall_pending_fd *next; +} wirecall_pending_fd; + +struct wirecall_worker { + wirecall_server *server; + wirecall_backend *backend; + wirecall_scheduler *scheduler; + wirecall_fixed_arena connection_arena; + wirecall_listener listeners[WIRECALL_MAX_LISTENERS]; size_t listener_count; - rpc_connection *connections; - rpc_connection *pending_writes; + wirecall_connection *connections; + wirecall_connection *pending_writes; pthread_mutex_t pending_mutex; - rpc_pending_fd *pending_head; - rpc_pending_fd *pending_tail; + wirecall_pending_fd *pending_head; + wirecall_pending_fd *pending_tail; int pending_mutex_ready; pthread_t thread; int thread_started; uint32_t index; }; -struct rpc_server { - rpc_routes routes; - rpc_worker *workers; +struct wirecall_server { + wirecall_routes routes; + wirecall_worker *workers; uint32_t worker_count; uint32_t workers_ready; atomic_uint next_worker; @@ -88,14 +88,14 @@ struct rpc_server { uint8_t mac_key[16]; }; -static void connection_write(rpc_connection *conn); -static int worker_add_connection(rpc_worker *worker, int fd); -static void worker_queue_write(rpc_worker *worker, rpc_connection *conn); -static void worker_flush_writes(rpc_worker *worker); +static void connection_write(wirecall_connection *conn); +static int worker_add_connection(wirecall_worker *worker, int fd); +static void worker_queue_write(wirecall_worker *worker, wirecall_connection *conn); +static void worker_flush_writes(wirecall_worker *worker); -static int connection_set_interests(rpc_connection *conn, uint32_t interests) { +static int connection_set_interests(wirecall_connection *conn, uint32_t interests) { if (conn->interests == interests) { return 0; } - if (rpc_backend_modify(conn->worker->backend, conn->fd, interests, (uintptr_t)conn) != 0) { return -1; } + if (wirecall_backend_modify(conn->worker->backend, conn->fd, interests, (uintptr_t)conn) != 0) { return -1; } conn->interests = interests; return 0; } @@ -109,7 +109,7 @@ static int set_nonblock(int fd) { static uint32_t cpu_count(void) { long n = sysconf(_SC_NPROCESSORS_ONLN); if (n <= 0) { return 1; } - if ((unsigned long)n > RPC_MAX_WORKERS) { return RPC_MAX_WORKERS; } + if ((unsigned long)n > WIRECALL_MAX_WORKERS) { return WIRECALL_MAX_WORKERS; } return (uint32_t)n; } @@ -136,7 +136,7 @@ static int append_bytes(uint8_t **buf, size_t *len, size_t *cap, const void *dat while (next_cap < need) { next_cap *= 2u; } - uint8_t *next = rpc_mem_realloc(*buf, next_cap); + uint8_t *next = wirecall_mem_realloc(*buf, next_cap); if (!next) { return -1; } *buf = next; *cap = next_cap; @@ -152,29 +152,29 @@ static int reserve_bytes(uint8_t **buf, size_t *cap, size_t need) { while (next_cap < need) { next_cap *= 2u; } - uint8_t *next = rpc_mem_realloc(*buf, next_cap); + uint8_t *next = wirecall_mem_realloc(*buf, next_cap); if (!next) { return -1; } *buf = next; *cap = next_cap; return 0; } -static int queue_packet(rpc_connection *conn, rpc_op op, uint8_t flags, uint64_t proc_id, uint64_t call_id, +static int queue_packet(wirecall_connection *conn, wirecall_op op, uint8_t flags, uint64_t proc_id, uint64_t call_id, const uint8_t *payload, size_t payload_len) { - if (payload_len > RPC_MAX_PAYLOAD_SIZE) { return -1; } - uint8_t header_buf[RPC_HEADER_SIZE]; - rpc_header header = { - .op = op, - .flags = flags, - .proc_id = proc_id, - .size = (uint32_t)payload_len, - .call_id = call_id, + if (payload_len > WIRECALL_MAX_PAYLOAD_SIZE) { return -1; } + uint8_t header_buf[WIRECALL_HEADER_SIZE]; + wirecall_header header = { + .op = op, + .flags = flags, + .proc_id = proc_id, + .size = (uint32_t)payload_len, + .call_id = call_id, }; - if (rpc_packet_sign_ex(&header, payload, payload_len, conn->worker->server->integrity, conn->worker->server->mac_key) != - 0) { + if (wirecall_packet_sign_ex(&header, payload, payload_len, conn->worker->server->integrity, + conn->worker->server->mac_key) != 0) { return -1; } - if (rpc_header_encode(&header, header_buf) != 0) { return -1; } + if (wirecall_header_encode(&header, header_buf) != 0) { return -1; } if (append_bytes(&conn->write_buf, &conn->write_len, &conn->write_cap, header_buf, sizeof(header_buf)) != 0) { return -1; } @@ -184,30 +184,32 @@ static int queue_packet(rpc_connection *conn, rpc_op op, uint8_t flags, uint64_t return 0; } -static int queue_string_error(rpc_connection *conn, uint64_t proc_id, uint64_t call_id, const char *message) { - rpc_writer writer; - rpc_writer_init(&writer); - int rc = rpc_writer_string(&writer, message, (uint32_t)strlen(message)); - if (rc == 0) { rc = queue_packet(conn, RPC_OP_ERROR, RPC_FLAG_NONE, proc_id, call_id, writer.data, writer.len); } - rpc_writer_free(&writer); +static int queue_string_error(wirecall_connection *conn, uint64_t proc_id, uint64_t call_id, const char *message) { + wirecall_writer writer; + wirecall_writer_init(&writer); + int rc = wirecall_writer_string(&writer, message, (uint32_t)strlen(message)); + if (rc == 0) { + rc = queue_packet(conn, WIRECALL_OP_ERROR, WIRECALL_FLAG_NONE, proc_id, call_id, writer.data, writer.len); + } + wirecall_writer_free(&writer); return rc; } -static void connection_destroy(rpc_connection *conn) { +static void connection_destroy(wirecall_connection *conn) { if (!conn) { return; } - rpc_worker *worker = conn->worker; - (void)rpc_backend_remove(worker->backend, conn->fd); + wirecall_worker *worker = conn->worker; + (void)wirecall_backend_remove(worker->backend, conn->fd); close(conn->fd); - rpc_mem_free(conn->read_buf); - rpc_mem_free(conn->write_buf); + wirecall_mem_free(conn->read_buf); + wirecall_mem_free(conn->write_buf); - rpc_connection **link = &worker->connections; + wirecall_connection **link = &worker->connections; while (*link && *link != conn) { link = &(*link)->next; } if (*link == conn) { *link = conn->next; } if (conn->write_queued) { - rpc_connection **write_link = &worker->pending_writes; + wirecall_connection **write_link = &worker->pending_writes; while (*write_link && *write_link != conn) { write_link = &(*write_link)->write_next; } @@ -215,25 +217,25 @@ static void connection_destroy(rpc_connection *conn) { conn->write_queued = 0; conn->write_next = NULL; } - rpc_fixed_arena_free(&worker->connection_arena, conn); + wirecall_fixed_arena_free(&worker->connection_arena, conn); } -static void maybe_close(rpc_connection *conn) { +static void maybe_close(wirecall_connection *conn) { if (conn->closing && conn->write_len == conn->write_off) { connection_destroy(conn); } } -static void worker_queue_write(rpc_worker *worker, rpc_connection *conn) { +static void worker_queue_write(wirecall_worker *worker, wirecall_connection *conn) { if (conn->write_queued) { return; } conn->write_queued = 1; conn->write_next = worker->pending_writes; worker->pending_writes = conn; } -static void worker_flush_writes(rpc_worker *worker) { - rpc_connection *conn = worker->pending_writes; +static void worker_flush_writes(wirecall_worker *worker) { + wirecall_connection *conn = worker->pending_writes; worker->pending_writes = NULL; while (conn) { - rpc_connection *next = conn->write_next; + wirecall_connection *next = conn->write_next; conn->write_next = NULL; conn->write_queued = 0; if (conn->write_off < conn->write_len && !conn->closing) { @@ -246,81 +248,82 @@ static void worker_flush_writes(rpc_worker *worker) { } } -static void on_call_done(rpc_call *call, void *user_data) { - rpc_connection *conn = user_data; - const rpc_writer *response = rpc_call_response(call); - if (rpc_call_result(call) == 0) { - (void)queue_packet(conn, RPC_OP_RESPONSE, RPC_FLAG_NONE, rpc_call_proc_id(call), rpc_call_id(call), response->data, - response->len); +static void on_call_done(wirecall_call *call, void *user_data) { + wirecall_connection *conn = user_data; + const wirecall_writer *response = wirecall_call_response(call); + if (wirecall_call_result(call) == 0) { + (void)queue_packet(conn, WIRECALL_OP_RESPONSE, WIRECALL_FLAG_NONE, wirecall_call_proc_id(call), + wirecall_call_id(call), response->data, response->len); } else { - (void)queue_string_error(conn, rpc_call_proc_id(call), rpc_call_id(call), rpc_call_error(call)); + (void)queue_string_error(conn, wirecall_call_proc_id(call), wirecall_call_id(call), wirecall_call_error(call)); } } -static int handle_sync_call(rpc_connection *conn, const rpc_header *header, rpc_route *route, const uint8_t *payload) { - rpc_value *args = NULL; +static int handle_sync_call(wirecall_connection *conn, const wirecall_header *header, wirecall_route *route, + const uint8_t *payload) { + wirecall_value *args = NULL; size_t argc = 0; - if (rpc_payload_decode(payload, header->size, &args, &argc) != 0) { + if (wirecall_payload_decode(payload, header->size, &args, &argc) != 0) { return queue_string_error(conn, header->proc_id, header->call_id, "malformed payload"); } - rpc_ctx ctx = { - .call_id = header->call_id, - .proc_id = header->proc_id, + wirecall_ctx ctx = { + .call_id = header->call_id, + .proc_id = header->proc_id, }; - rpc_writer response; - rpc_writer_init(&response); + wirecall_writer response; + wirecall_writer_init(&response); int result = route->handler(&ctx, args, argc, &response, route->user_data); int rc = 0; if (result == 0) { - rc = queue_packet(conn, RPC_OP_RESPONSE, RPC_FLAG_NONE, header->proc_id, header->call_id, response.data, + rc = queue_packet(conn, WIRECALL_OP_RESPONSE, WIRECALL_FLAG_NONE, header->proc_id, header->call_id, response.data, response.len); } else { rc = queue_string_error(conn, header->proc_id, header->call_id, "procedure failed"); } - rpc_writer_free(&response); - rpc_values_free(args); + wirecall_writer_free(&response); + wirecall_values_free(args); return rc; } -static int handle_packet(rpc_connection *conn, const rpc_header *header, const uint8_t *payload) { +static int handle_packet(wirecall_connection *conn, const wirecall_header *header, const uint8_t *payload) { static const void *dispatch[] = { - [RPC_OP_RPC] = &&op_rpc, - [RPC_OP_PING] = &&op_ping, - [RPC_OP_DISCONNECT] = &&op_disconnect, - [RPC_OP_RESPONSE] = &&op_unsupported, - [RPC_OP_ERROR] = &&op_unsupported, + [WIRECALL_OP_RPC] = &&op_rpc, + [WIRECALL_OP_PING] = &&op_ping, + [WIRECALL_OP_DISCONNECT] = &&op_disconnect, + [WIRECALL_OP_RESPONSE] = &&op_unsupported, + [WIRECALL_OP_ERROR] = &&op_unsupported, }; if ((size_t)header->op >= sizeof(dispatch) / sizeof(*dispatch) || !dispatch[header->op]) { goto op_unsupported; } goto *dispatch[header->op]; op_ping: - return queue_packet(conn, RPC_OP_RESPONSE, RPC_FLAG_NONE, 0, header->call_id, NULL, 0); + return queue_packet(conn, WIRECALL_OP_RESPONSE, WIRECALL_FLAG_NONE, 0, header->call_id, NULL, 0); op_disconnect: conn->closing = 1; return 0; op_rpc: { - rpc_trace_worker_add(conn->worker->index, RPC_TRACE_WORKER_RPCS, 1); - rpc_route route; - uint64_t trace_route = rpc_trace_begin(); - if (rpc_routes_lookup(&conn->worker->server->routes, header->proc_id, &route) != 0) { - rpc_trace_end(RPC_TRACE_SERVER_ROUTE, trace_route); + wirecall_trace_worker_add(conn->worker->index, WIRECALL_TRACE_WORKER_RPCS, 1); + wirecall_route route; + uint64_t trace_route = wirecall_trace_begin(); + if (wirecall_routes_lookup(&conn->worker->server->routes, header->proc_id, &route) != 0) { + wirecall_trace_end(WIRECALL_TRACE_SERVER_ROUTE, trace_route); return queue_string_error(conn, header->proc_id, header->call_id, "unknown procedure"); } - rpc_trace_end(RPC_TRACE_SERVER_ROUTE, trace_route); + wirecall_trace_end(WIRECALL_TRACE_SERVER_ROUTE, trace_route); if (!route.is_async) { return handle_sync_call(conn, header, &route, payload); } - uint64_t trace_schedule = rpc_trace_begin(); - if (rpc_scheduler_submit(conn->worker->scheduler, header->call_id, header->proc_id, route.handler, route.user_data, - payload, header->size, on_call_done, conn) != 0) { - rpc_trace_end(RPC_TRACE_SERVER_SCHEDULE, trace_schedule); + uint64_t trace_schedule = wirecall_trace_begin(); + if (wirecall_scheduler_submit(conn->worker->scheduler, header->call_id, header->proc_id, route.handler, + route.user_data, payload, header->size, on_call_done, conn) != 0) { + wirecall_trace_end(WIRECALL_TRACE_SERVER_SCHEDULE, trace_schedule); return queue_string_error(conn, header->proc_id, header->call_id, "scheduler failure"); } - rpc_trace_end(RPC_TRACE_SERVER_SCHEDULE, trace_schedule); + wirecall_trace_end(WIRECALL_TRACE_SERVER_SCHEDULE, trace_schedule); return 0; } @@ -328,19 +331,19 @@ op_unsupported: return queue_string_error(conn, header->proc_id, header->call_id, "unsupported operation"); } -static void parse_available(rpc_connection *conn) { - uint64_t trace = rpc_trace_begin(); +static void parse_available(wirecall_connection *conn) { + uint64_t trace = wirecall_trace_begin(); size_t off = 0; - while (conn->read_len - off >= RPC_HEADER_SIZE) { - rpc_header header; - if (rpc_header_decode(conn->read_buf + off, &header) != 0) { + while (conn->read_len - off >= WIRECALL_HEADER_SIZE) { + wirecall_header header; + if (wirecall_header_decode(conn->read_buf + off, &header) != 0) { conn->closing = 1; break; } - if (conn->read_len - off - RPC_HEADER_SIZE < header.size) { break; } - const uint8_t *payload = conn->read_buf + off + RPC_HEADER_SIZE; - if (rpc_packet_verify_ex(&header, payload, header.size, conn->worker->server->integrity, - conn->worker->server->mac_key) != 0) { + if (conn->read_len - off - WIRECALL_HEADER_SIZE < header.size) { break; } + const uint8_t *payload = conn->read_buf + off + WIRECALL_HEADER_SIZE; + if (wirecall_packet_verify_ex(&header, payload, header.size, conn->worker->server->integrity, + conn->worker->server->mac_key) != 0) { conn->closing = 1; break; } @@ -348,24 +351,24 @@ static void parse_available(rpc_connection *conn) { conn->closing = 1; break; } - off += RPC_HEADER_SIZE + header.size; + off += WIRECALL_HEADER_SIZE + header.size; } if (off > 0) { memmove(conn->read_buf, conn->read_buf + off, conn->read_len - off); conn->read_len -= off; } - rpc_trace_end(RPC_TRACE_SERVER_PARSE, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_PARSE, trace); } -static void connection_read(rpc_connection *conn) { - rpc_trace_worker_add(conn->worker->index, RPC_TRACE_WORKER_READS, 1); - uint64_t trace = rpc_trace_begin(); +static void connection_read(wirecall_connection *conn) { + wirecall_trace_worker_add(conn->worker->index, WIRECALL_TRACE_WORKER_READS, 1); + uint64_t trace = wirecall_trace_begin(); for (;;) { - if (conn->read_cap - conn->read_len < RPC_READ_CHUNK) { - if (reserve_bytes(&conn->read_buf, &conn->read_cap, conn->read_len + RPC_READ_CHUNK) != 0) { + if (conn->read_cap - conn->read_len < WIRECALL_READ_CHUNK) { + if (reserve_bytes(&conn->read_buf, &conn->read_cap, conn->read_len + WIRECALL_READ_CHUNK) != 0) { conn->closing = 1; - rpc_trace_end(RPC_TRACE_SERVER_READ, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_READ, trace); return; } } @@ -374,30 +377,30 @@ static void connection_read(rpc_connection *conn) { conn->read_len += (size_t)n; parse_available(conn); if (conn->closing) { - rpc_trace_end(RPC_TRACE_SERVER_READ, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_READ, trace); return; } continue; } if (n == 0) { conn->closing = 1; - rpc_trace_end(RPC_TRACE_SERVER_READ, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_READ, trace); return; } if (errno == EAGAIN || errno == EWOULDBLOCK) { - rpc_trace_end(RPC_TRACE_SERVER_READ, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_READ, trace); return; } if (errno == EINTR) { continue; } conn->closing = 1; - rpc_trace_end(RPC_TRACE_SERVER_READ, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_READ, trace); return; } } -static void connection_write(rpc_connection *conn) { - rpc_trace_worker_add(conn->worker->index, RPC_TRACE_WORKER_WRITES, 1); - uint64_t trace = rpc_trace_begin(); +static void connection_write(wirecall_connection *conn) { + wirecall_trace_worker_add(conn->worker->index, WIRECALL_TRACE_WORKER_WRITES, 1); + uint64_t trace = wirecall_trace_begin(); while (conn->write_off < conn->write_len) { ssize_t n = send(conn->fd, conn->write_buf + conn->write_off, conn->write_len - conn->write_off, 0); if (n > 0) { @@ -406,23 +409,23 @@ static void connection_write(rpc_connection *conn) { } if (n < 0 && errno == EINTR) { continue; } if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) { - (void)connection_set_interests(conn, RPC_BACKEND_READ | RPC_BACKEND_WRITE); - rpc_trace_end(RPC_TRACE_SERVER_WRITE, trace); + (void)connection_set_interests(conn, WIRECALL_BACKEND_READ | WIRECALL_BACKEND_WRITE); + wirecall_trace_end(WIRECALL_TRACE_SERVER_WRITE, trace); return; } conn->closing = 1; - rpc_trace_end(RPC_TRACE_SERVER_WRITE, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_WRITE, trace); return; } conn->write_len = 0; conn->write_off = 0; - (void)connection_set_interests(conn, RPC_BACKEND_READ); - rpc_trace_end(RPC_TRACE_SERVER_WRITE, trace); + (void)connection_set_interests(conn, WIRECALL_BACKEND_READ); + wirecall_trace_end(WIRECALL_TRACE_SERVER_WRITE, trace); } -static int worker_add_connection(rpc_worker *worker, int fd) { - rpc_connection *conn = rpc_fixed_arena_alloc(&worker->connection_arena); +static int worker_add_connection(wirecall_worker *worker, int fd) { + wirecall_connection *conn = wirecall_fixed_arena_alloc(&worker->connection_arena); if (!conn) { close(fd); return -1; @@ -431,17 +434,17 @@ static int worker_add_connection(rpc_worker *worker, int fd) { conn->worker = worker; conn->next = worker->connections; worker->connections = conn; - if (rpc_backend_register(worker->backend, fd, RPC_BACKEND_READ, (uintptr_t)conn) != 0) { + if (wirecall_backend_register(worker->backend, fd, WIRECALL_BACKEND_READ, (uintptr_t)conn) != 0) { connection_destroy(conn); return -1; } - conn->interests = RPC_BACKEND_READ; - rpc_trace_worker_add(worker->index, RPC_TRACE_WORKER_ACCEPTS, 1); + conn->interests = WIRECALL_BACKEND_READ; + wirecall_trace_worker_add(worker->index, WIRECALL_TRACE_WORKER_ACCEPTS, 1); return 0; } -static void worker_enqueue_connection(rpc_worker *worker, int fd) { - rpc_pending_fd *pending = rpc_mem_alloc(sizeof(*pending)); +static void worker_enqueue_connection(wirecall_worker *worker, int fd) { + wirecall_pending_fd *pending = wirecall_mem_alloc(sizeof(*pending)); if (!pending) { close(fd); return; @@ -458,35 +461,35 @@ static void worker_enqueue_connection(rpc_worker *worker, int fd) { worker->pending_tail = pending; pthread_mutex_unlock(&worker->pending_mutex); - (void)rpc_backend_wake(worker->backend); + (void)wirecall_backend_wake(worker->backend); } -static void worker_drain_pending(rpc_worker *worker) { +static void worker_drain_pending(wirecall_worker *worker) { pthread_mutex_lock(&worker->pending_mutex); - rpc_pending_fd *pending = worker->pending_head; + wirecall_pending_fd *pending = worker->pending_head; worker->pending_head = NULL; worker->pending_tail = NULL; pthread_mutex_unlock(&worker->pending_mutex); while (pending) { - rpc_pending_fd *next = pending->next; + wirecall_pending_fd *next = pending->next; (void)worker_add_connection(worker, pending->fd); - rpc_mem_free(pending); + wirecall_mem_free(pending); pending = next; } } -static void accept_ready(rpc_listener *listener) { - uint64_t trace = rpc_trace_begin(); - rpc_worker *worker = listener->worker; - rpc_server *server = worker->server; +static void accept_ready(wirecall_listener *listener) { + uint64_t trace = wirecall_trace_begin(); + wirecall_worker *worker = listener->worker; + wirecall_server *server = worker->server; for (;;) { struct sockaddr_storage addr; socklen_t addr_len = sizeof(addr); int fd = accept(listener->fd, (struct sockaddr *)&addr, &addr_len); if (fd < 0) { if (errno == EINTR) { continue; } - rpc_trace_end(RPC_TRACE_SERVER_ACCEPT, trace); + wirecall_trace_end(WIRECALL_TRACE_SERVER_ACCEPT, trace); return; } if (set_nonblock(fd) != 0) { @@ -498,69 +501,70 @@ static void accept_ready(rpc_listener *listener) { } } -static int worker_init(rpc_server *server, rpc_worker *worker, uint32_t index) { +static int worker_init(wirecall_server *server, wirecall_worker *worker, uint32_t index) { memset(worker, 0, sizeof(*worker)); worker->server = server; worker->index = index; - for (size_t i = 0; i < RPC_MAX_LISTENERS; ++i) { + for (size_t i = 0; i < WIRECALL_MAX_LISTENERS; ++i) { worker->listeners[i].fd = -1; } if (pthread_mutex_init(&worker->pending_mutex, NULL) != 0) { return -1; } worker->pending_mutex_ready = 1; - if (rpc_backend_create(&worker->backend) != 0) { return -1; } - if (rpc_fixed_arena_init(&worker->connection_arena, sizeof(rpc_connection), RPC_CONNECTION_ARENA_CAPACITY) != 0) { + if (wirecall_backend_create(&worker->backend) != 0) { return -1; } + if (wirecall_fixed_arena_init(&worker->connection_arena, sizeof(wirecall_connection), + WIRECALL_CONNECTION_ARENA_CAPACITY) != 0) { return -1; } - if (rpc_scheduler_init(&worker->scheduler) != 0) { return -1; } + if (wirecall_scheduler_init(&worker->scheduler) != 0) { return -1; } return 0; } -static void worker_destroy(rpc_worker *worker) { +static void worker_destroy(wirecall_worker *worker) { if (!worker) { return; } - rpc_connection *conn = worker->connections; + wirecall_connection *conn = worker->connections; while (conn) { - rpc_connection *next = conn->next; + wirecall_connection *next = conn->next; connection_destroy(conn); conn = next; } for (size_t i = 0; i < worker->listener_count; ++i) { if (worker->listeners[i].fd >= 0) { - (void)rpc_backend_remove(worker->backend, worker->listeners[i].fd); + (void)wirecall_backend_remove(worker->backend, worker->listeners[i].fd); close(worker->listeners[i].fd); worker->listeners[i].fd = -1; } } if (worker->pending_mutex_ready) { pthread_mutex_lock(&worker->pending_mutex); - rpc_pending_fd *pending = worker->pending_head; + wirecall_pending_fd *pending = worker->pending_head; worker->pending_head = NULL; worker->pending_tail = NULL; pthread_mutex_unlock(&worker->pending_mutex); while (pending) { - rpc_pending_fd *next = pending->next; + wirecall_pending_fd *next = pending->next; close(pending->fd); - rpc_mem_free(pending); + wirecall_mem_free(pending); pending = next; } pthread_mutex_destroy(&worker->pending_mutex); } - rpc_scheduler_destroy(worker->scheduler); - rpc_fixed_arena_destroy(&worker->connection_arena); - rpc_backend_destroy(worker->backend); + wirecall_scheduler_destroy(worker->scheduler); + wirecall_fixed_arena_destroy(&worker->connection_arena); + wirecall_backend_destroy(worker->backend); memset(worker, 0, sizeof(*worker)); } -static int server_ensure_workers(rpc_server *server) { +static int server_ensure_workers(wirecall_server *server) { if (server->workers_ready) { return 0; } if (server->worker_count == 0) { server->worker_count = cpu_count(); } - server->workers = rpc_mem_calloc(server->worker_count, sizeof(*server->workers)); + server->workers = wirecall_mem_calloc(server->worker_count, sizeof(*server->workers)); if (!server->workers) { return -1; } for (uint32_t i = 0; i < server->worker_count; ++i) { if (worker_init(server, &server->workers[i], i) != 0) { for (uint32_t j = 0; j <= i; ++j) { worker_destroy(&server->workers[j]); } - rpc_mem_free(server->workers); + wirecall_mem_free(server->workers); server->workers = NULL; return -1; } @@ -569,16 +573,16 @@ static int server_ensure_workers(rpc_server *server) { return 0; } -int rpc_server_init(rpc_server **out_server) { +int wirecall_server_init(wirecall_server **out_server) { if (!out_server) { return -1; } - rpc_server *server = rpc_mem_calloc(1, sizeof(*server)); + wirecall_server *server = wirecall_mem_calloc(1, sizeof(*server)); if (!server) { return -1; } atomic_init(&server->stopping, false); atomic_init(&server->next_worker, 0); - server->integrity = RPC_INTEGRITY_DEFAULT; + server->integrity = WIRECALL_INTEGRITY_DEFAULT; server->worker_count = cpu_count(); - if (rpc_routes_init(&server->routes) != 0) { - rpc_server_destroy(server); + if (wirecall_routes_init(&server->routes) != 0) { + wirecall_server_destroy(server); return -1; } server->routes_ready = 1; @@ -586,18 +590,19 @@ int rpc_server_init(rpc_server **out_server) { return 0; } -int rpc_server_set_workers(rpc_server *server, uint32_t worker_count) { - if (!server || server->workers_ready || server->listening || worker_count == 0 || worker_count > RPC_MAX_WORKERS) { +int wirecall_server_set_workers(wirecall_server *server, uint32_t worker_count) { + if (!server || server->workers_ready || server->listening || worker_count == 0 || + worker_count > WIRECALL_MAX_WORKERS) { return -1; } server->worker_count = worker_count; return 0; } -int rpc_server_set_integrity(rpc_server *server, uint32_t integrity, const uint8_t mac_key[16]) { +int wirecall_server_set_integrity(wirecall_server *server, uint32_t integrity, const uint8_t mac_key[16]) { if (!server || server->workers_ready || server->listening || - (integrity & ~(RPC_INTEGRITY_CHECKSUM | RPC_INTEGRITY_MAC)) || - ((integrity & RPC_INTEGRITY_MAC) && !mac_key)) { + (integrity & ~(WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC)) || + ((integrity & WIRECALL_INTEGRITY_MAC) && !mac_key)) { return -1; } server->integrity = integrity; @@ -605,7 +610,7 @@ int rpc_server_set_integrity(rpc_server *server, uint32_t integrity, const uint8 return 0; } -int rpc_server_bind(rpc_server *server, const char *host, const char *port) { +int wirecall_server_bind(wirecall_server *server, const char *host, const char *port) { if (!server || !port || server_ensure_workers(server) != 0) { return -1; } struct addrinfo hints; @@ -619,9 +624,9 @@ int rpc_server_bind(rpc_server *server, const char *host, const char *port) { int ok = -1; for (struct addrinfo *ai = res; ai; ai = ai->ai_next) { - if (server->workers[0].listener_count >= RPC_MAX_LISTENERS) { break; } + if (server->workers[0].listener_count >= WIRECALL_MAX_LISTENERS) { break; } - rpc_worker *worker = &server->workers[0]; + wirecall_worker *worker = &server->workers[0]; int fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (fd < 0) { continue; } int yes = 1; @@ -644,7 +649,7 @@ int rpc_server_bind(rpc_server *server, const char *host, const char *port) { } } - rpc_listener *listener = &worker->listeners[worker->listener_count++]; + wirecall_listener *listener = &worker->listeners[worker->listener_count++]; listener->fd = fd; listener->worker = worker; ok = 0; @@ -654,19 +659,19 @@ int rpc_server_bind(rpc_server *server, const char *host, const char *port) { return ok; } -uint16_t rpc_server_port(const rpc_server *server) { +uint16_t wirecall_server_port(const wirecall_server *server) { return server ? server->port : 0; } -int rpc_server_listen(rpc_server *server) { +int wirecall_server_listen(wirecall_server *server) { if (!server) { return -1; } for (uint32_t wi = 0; wi < server->worker_count; ++wi) { - rpc_worker *worker = &server->workers[wi]; + wirecall_worker *worker = &server->workers[wi]; for (size_t i = 0; i < worker->listener_count; ++i) { - rpc_listener *listener = &worker->listeners[i]; + wirecall_listener *listener = &worker->listeners[i]; if (listen(listener->fd, SOMAXCONN) != 0) { return -1; } uintptr_t user = ((uintptr_t)listener) | 1u; - if (rpc_backend_register(worker->backend, listener->fd, RPC_BACKEND_READ, user) != 0) { return -1; } + if (wirecall_backend_register(worker->backend, listener->fd, WIRECALL_BACKEND_READ, user) != 0) { return -1; } } } server->listening = 1; @@ -675,38 +680,38 @@ int rpc_server_listen(rpc_server *server) { static void *worker_run_main(void *arg); -static int worker_run(rpc_worker *worker) { - rpc_server *server = worker->server; - rpc_backend_event events[RPC_EVENT_BATCH]; +static int worker_run(wirecall_worker *worker) { + wirecall_server *server = worker->server; + wirecall_backend_event events[WIRECALL_EVENT_BATCH]; while (!atomic_load_explicit(&server->stopping, memory_order_acquire)) { - uint64_t trace_poll = rpc_trace_begin(); - int n = rpc_backend_poll(worker->backend, events, RPC_EVENT_BATCH, -1); - rpc_trace_end(RPC_TRACE_SERVER_POLL_WAIT, trace_poll); + uint64_t trace_poll = wirecall_trace_begin(); + int n = wirecall_backend_poll(worker->backend, events, WIRECALL_EVENT_BATCH, -1); + wirecall_trace_end(WIRECALL_TRACE_SERVER_POLL_WAIT, trace_poll); if (n < 0) { return -1; } - rpc_trace_add(RPC_TRACE_SERVER_POLL_EVENTS, (uint64_t)n); - rpc_trace_worker_add(worker->index, RPC_TRACE_WORKER_POLL_EVENTS, (uint64_t)n); - uint64_t trace_active = rpc_trace_begin(); + wirecall_trace_add(WIRECALL_TRACE_SERVER_POLL_EVENTS, (uint64_t)n); + wirecall_trace_worker_add(worker->index, WIRECALL_TRACE_WORKER_POLL_EVENTS, (uint64_t)n); + uint64_t trace_active = wirecall_trace_begin(); for (int i = 0; i < n; ++i) { - if (events[i].events & RPC_BACKEND_WAKE) { + if (events[i].events & WIRECALL_BACKEND_WAKE) { worker_drain_pending(worker); continue; } if (events[i].user & 1u) { - rpc_listener *listener = (rpc_listener *)(events[i].user & ~(uintptr_t)1u); + wirecall_listener *listener = (wirecall_listener *)(events[i].user & ~(uintptr_t)1u); accept_ready(listener); } else { - rpc_connection *conn = (rpc_connection *)events[i].user; - if ((events[i].events & RPC_BACKEND_READ) && !conn->closing) { connection_read(conn); } - if ((events[i].events & RPC_BACKEND_WRITE) && !conn->closing) { connection_write(conn); } + wirecall_connection *conn = (wirecall_connection *)events[i].user; + if ((events[i].events & WIRECALL_BACKEND_READ) && !conn->closing) { connection_read(conn); } + if ((events[i].events & WIRECALL_BACKEND_WRITE) && !conn->closing) { connection_write(conn); } maybe_close(conn); } } - uint64_t trace_schedule = rpc_trace_begin(); - rpc_scheduler_run_ready(worker->scheduler); - rpc_trace_end(RPC_TRACE_SERVER_SCHEDULE, trace_schedule); + uint64_t trace_schedule = wirecall_trace_begin(); + wirecall_scheduler_run_ready(worker->scheduler); + wirecall_trace_end(WIRECALL_TRACE_SERVER_SCHEDULE, trace_schedule); worker_flush_writes(worker); - rpc_trace_worker_end(worker->index, RPC_TRACE_WORKER_ACTIVE, trace_active); - rpc_trace_end(RPC_TRACE_SERVER_LOOP_ACTIVE, trace_active); + wirecall_trace_worker_end(worker->index, WIRECALL_TRACE_WORKER_ACTIVE, trace_active); + wirecall_trace_end(WIRECALL_TRACE_SERVER_LOOP_ACTIVE, trace_active); } return 0; } @@ -716,14 +721,14 @@ static void *worker_run_main(void *arg) { return NULL; } -int rpc_server_run(rpc_server *server) { +int wirecall_server_run(wirecall_server *server) { if (!server || !server->listening) { return -1; } if (server->worker_count == 1) { return worker_run(&server->workers[0]); } for (uint32_t i = 0; i < server->worker_count; ++i) { if (pthread_create(&server->workers[i].thread, NULL, worker_run_main, &server->workers[i]) != 0) { - rpc_server_stop(server); + wirecall_server_stop(server); for (uint32_t j = 0; j < i; ++j) { pthread_join(server->workers[j].thread, NULL); server->workers[j].thread_started = 0; @@ -740,18 +745,18 @@ int rpc_server_run(rpc_server *server) { return 0; } -void rpc_server_stop(rpc_server *server) { +void wirecall_server_stop(wirecall_server *server) { if (!server) { return; } atomic_store_explicit(&server->stopping, true, memory_order_release); for (uint32_t i = 0; i < server->worker_count; ++i) { - if (server->workers && server->workers[i].backend) { (void)rpc_backend_wake(server->workers[i].backend); } + if (server->workers && server->workers[i].backend) { (void)wirecall_backend_wake(server->workers[i].backend); } } } -void rpc_server_destroy(rpc_server *server) { +void wirecall_server_destroy(wirecall_server *server) { if (!server) { return; } if (server->workers) { - rpc_server_stop(server); + wirecall_server_stop(server); for (uint32_t i = 0; i < server->worker_count; ++i) { if (server->workers[i].thread_started) { pthread_join(server->workers[i].thread, NULL); @@ -759,45 +764,46 @@ void rpc_server_destroy(rpc_server *server) { } worker_destroy(&server->workers[i]); } - rpc_mem_free(server->workers); + wirecall_mem_free(server->workers); } - if (server->routes_ready) { rpc_routes_destroy(&server->routes); } - rpc_mem_free(server); + if (server->routes_ready) { wirecall_routes_destroy(&server->routes); } + wirecall_mem_free(server); } -static int server_add_route_id(rpc_server *server, uint64_t proc_id, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer) { - return server ? rpc_routes_add_ex(&server->routes, proc_id, handler, user_data, finalizer, 0) : -1; +static int server_add_route_id(wirecall_server *server, uint64_t proc_id, wirecall_handler_fn handler, void *user_data, + wirecall_route_finalizer_fn finalizer) { + return server ? wirecall_routes_add_ex(&server->routes, proc_id, handler, user_data, finalizer, 0) : -1; } -int rpc_server_add_route_name(rpc_server *server, const char *proc_name, rpc_handler_fn handler, void *user_data) { - return rpc_server_add_route_name_ex(server, proc_name, handler, user_data, NULL); +int wirecall_server_add_route_name(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data) { + return wirecall_server_add_route_name_ex(server, proc_name, handler, user_data, NULL); } -int rpc_server_add_route_name_ex(rpc_server *server, const char *proc_name, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer) { - return server_add_route_id(server, rpc_proc_id(proc_name), handler, user_data, finalizer); +int wirecall_server_add_route_name_ex(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data, wirecall_route_finalizer_fn finalizer) { + return server_add_route_id(server, wirecall_proc_id(proc_name), handler, user_data, finalizer); } -static int server_add_async_route_id(rpc_server *server, uint64_t proc_id, rpc_handler_fn handler, void *user_data, - rpc_route_finalizer_fn finalizer) { - return server ? rpc_routes_add_ex(&server->routes, proc_id, handler, user_data, finalizer, 1) : -1; +static int server_add_async_route_id(wirecall_server *server, uint64_t proc_id, wirecall_handler_fn handler, + void *user_data, wirecall_route_finalizer_fn finalizer) { + return server ? wirecall_routes_add_ex(&server->routes, proc_id, handler, user_data, finalizer, 1) : -1; } -int rpc_server_add_async_route_name(rpc_server *server, const char *proc_name, rpc_handler_fn handler, - void *user_data) { - return rpc_server_add_async_route_name_ex(server, proc_name, handler, user_data, NULL); +int wirecall_server_add_async_route_name(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data) { + return wirecall_server_add_async_route_name_ex(server, proc_name, handler, user_data, NULL); } -int rpc_server_add_async_route_name_ex(rpc_server *server, const char *proc_name, rpc_handler_fn handler, - void *user_data, rpc_route_finalizer_fn finalizer) { - return server_add_async_route_id(server, rpc_proc_id(proc_name), handler, user_data, finalizer); +int wirecall_server_add_async_route_name_ex(wirecall_server *server, const char *proc_name, wirecall_handler_fn handler, + void *user_data, wirecall_route_finalizer_fn finalizer) { + return server_add_async_route_id(server, wirecall_proc_id(proc_name), handler, user_data, finalizer); } -static int server_remove_route_id(rpc_server *server, uint64_t proc_id) { - return server ? rpc_routes_remove(&server->routes, proc_id) : -1; +static int server_remove_route_id(wirecall_server *server, uint64_t proc_id) { + return server ? wirecall_routes_remove(&server->routes, proc_id) : -1; } -int rpc_server_remove_route_name(rpc_server *server, const char *proc_name) { - return server_remove_route_id(server, rpc_proc_id(proc_name)); +int wirecall_server_remove_route_name(wirecall_server *server, const char *proc_name) { + return server_remove_route_id(server, wirecall_proc_id(proc_name)); } diff --git a/src/trace.c b/src/trace.c index 4516fef..66a78ed 100644 --- a/src/trace.c +++ b/src/trace.c @@ -1,57 +1,57 @@ -#include "rpc/trace.h" +#include "wirecall/trace.h" #include #include -typedef struct rpc_trace_counter { +typedef struct wirecall_trace_counter { atomic_uint_fast64_t count; atomic_uint_fast64_t total_ns; atomic_uint_fast64_t max_ns; -} rpc_trace_counter; - -static const char *trace_names[RPC_TRACE_COUNT] = { - [RPC_TRACE_CLIENT_CALL] = "client.call", - [RPC_TRACE_CLIENT_SEND] = "client.send", - [RPC_TRACE_CLIENT_RECV] = "client.recv", - [RPC_TRACE_CLIENT_DECODE] = "client.decode", - [RPC_TRACE_SERVER_ACCEPT] = "server.accept", - [RPC_TRACE_SERVER_POLL_WAIT] = "server.poll_wait", - [RPC_TRACE_SERVER_POLL_EVENTS] = "server.poll_events", - [RPC_TRACE_SERVER_LOOP_ACTIVE] = "server.loop_active", - [RPC_TRACE_SERVER_READ] = "server.read", - [RPC_TRACE_SERVER_PARSE] = "server.parse", - [RPC_TRACE_SERVER_ROUTE] = "server.route", - [RPC_TRACE_SERVER_SCHEDULE] = "server.schedule", - [RPC_TRACE_SERVER_WRITE] = "server.write", - [RPC_TRACE_SCHED_SUBMIT] = "sched.submit", - [RPC_TRACE_SCHED_DECODE] = "sched.decode", - [RPC_TRACE_SCHED_CORO_CREATE] = "sched.coro_create", - [RPC_TRACE_SCHED_RESUME] = "sched.resume", - [RPC_TRACE_PAYLOAD_DECODE] = "payload.decode", - [RPC_TRACE_ROUTE_LOOKUP] = "route.lookup", +} wirecall_trace_counter; + +static const char *trace_names[WIRECALL_TRACE_COUNT] = { + [WIRECALL_TRACE_CLIENT_CALL] = "client.call", + [WIRECALL_TRACE_CLIENT_SEND] = "client.send", + [WIRECALL_TRACE_CLIENT_RECV] = "client.recv", + [WIRECALL_TRACE_CLIENT_DECODE] = "client.decode", + [WIRECALL_TRACE_SERVER_ACCEPT] = "server.accept", + [WIRECALL_TRACE_SERVER_POLL_WAIT] = "server.poll_wait", + [WIRECALL_TRACE_SERVER_POLL_EVENTS] = "server.poll_events", + [WIRECALL_TRACE_SERVER_LOOP_ACTIVE] = "server.loop_active", + [WIRECALL_TRACE_SERVER_READ] = "server.read", + [WIRECALL_TRACE_SERVER_PARSE] = "server.parse", + [WIRECALL_TRACE_SERVER_ROUTE] = "server.route", + [WIRECALL_TRACE_SERVER_SCHEDULE] = "server.schedule", + [WIRECALL_TRACE_SERVER_WRITE] = "server.write", + [WIRECALL_TRACE_SCHED_SUBMIT] = "sched.submit", + [WIRECALL_TRACE_SCHED_DECODE] = "sched.decode", + [WIRECALL_TRACE_SCHED_CORO_CREATE] = "sched.coro_create", + [WIRECALL_TRACE_SCHED_RESUME] = "sched.resume", + [WIRECALL_TRACE_PAYLOAD_DECODE] = "payload.decode", + [WIRECALL_TRACE_ROUTE_LOOKUP] = "route.lookup", }; -static const int trace_is_time[RPC_TRACE_COUNT] = { - [RPC_TRACE_CLIENT_CALL] = 1, [RPC_TRACE_CLIENT_SEND] = 1, [RPC_TRACE_CLIENT_RECV] = 1, - [RPC_TRACE_CLIENT_DECODE] = 1, [RPC_TRACE_SERVER_ACCEPT] = 1, [RPC_TRACE_SERVER_POLL_WAIT] = 1, - [RPC_TRACE_SERVER_LOOP_ACTIVE] = 1, [RPC_TRACE_SERVER_READ] = 1, [RPC_TRACE_SERVER_PARSE] = 1, - [RPC_TRACE_SERVER_ROUTE] = 1, [RPC_TRACE_SERVER_SCHEDULE] = 1, [RPC_TRACE_SERVER_WRITE] = 1, - [RPC_TRACE_SCHED_SUBMIT] = 1, [RPC_TRACE_SCHED_DECODE] = 1, [RPC_TRACE_SCHED_CORO_CREATE] = 1, - [RPC_TRACE_SCHED_RESUME] = 1, [RPC_TRACE_PAYLOAD_DECODE] = 1, [RPC_TRACE_ROUTE_LOOKUP] = 1, +static const int trace_is_time[WIRECALL_TRACE_COUNT] = { + [WIRECALL_TRACE_CLIENT_CALL] = 1, [WIRECALL_TRACE_CLIENT_SEND] = 1, [WIRECALL_TRACE_CLIENT_RECV] = 1, + [WIRECALL_TRACE_CLIENT_DECODE] = 1, [WIRECALL_TRACE_SERVER_ACCEPT] = 1, [WIRECALL_TRACE_SERVER_POLL_WAIT] = 1, + [WIRECALL_TRACE_SERVER_LOOP_ACTIVE] = 1, [WIRECALL_TRACE_SERVER_READ] = 1, [WIRECALL_TRACE_SERVER_PARSE] = 1, + [WIRECALL_TRACE_SERVER_ROUTE] = 1, [WIRECALL_TRACE_SERVER_SCHEDULE] = 1, [WIRECALL_TRACE_SERVER_WRITE] = 1, + [WIRECALL_TRACE_SCHED_SUBMIT] = 1, [WIRECALL_TRACE_SCHED_DECODE] = 1, [WIRECALL_TRACE_SCHED_CORO_CREATE] = 1, + [WIRECALL_TRACE_SCHED_RESUME] = 1, [WIRECALL_TRACE_PAYLOAD_DECODE] = 1, [WIRECALL_TRACE_ROUTE_LOOKUP] = 1, }; -static const char *trace_avg_units[RPC_TRACE_COUNT] = { - [RPC_TRACE_SERVER_POLL_EVENTS] = "evt", +static const char *trace_avg_units[WIRECALL_TRACE_COUNT] = { + [WIRECALL_TRACE_SERVER_POLL_EVENTS] = "evt", }; -static const char *trace_max_units[RPC_TRACE_COUNT] = { - [RPC_TRACE_SERVER_POLL_EVENTS] = "events", +static const char *trace_max_units[WIRECALL_TRACE_COUNT] = { + [WIRECALL_TRACE_SERVER_POLL_EVENTS] = "events", }; -static rpc_trace_counter counters[RPC_TRACE_COUNT]; -static rpc_trace_counter worker_counters[RPC_TRACE_MAX_WORKERS][RPC_TRACE_WORKER_COUNT]; +static wirecall_trace_counter counters[WIRECALL_TRACE_COUNT]; +static wirecall_trace_counter worker_counters[WIRECALL_TRACE_MAX_WORKERS][WIRECALL_TRACE_WORKER_COUNT]; -int rpc_trace_enabled = 0; +int wirecall_trace_enabled = 0; static void format_duration(uint64_t ns, char out[16]) { if (ns < 1000ull) { @@ -65,27 +65,27 @@ static void format_duration(uint64_t ns, char out[16]) { } } -void rpc_trace_set_enabled(int enabled) { +void wirecall_trace_set_enabled(int enabled) { #if defined(__GNUC__) || defined(__clang__) - __atomic_store_n(&rpc_trace_enabled, enabled ? 1 : 0, __ATOMIC_RELAXED); + __atomic_store_n(&wirecall_trace_enabled, enabled ? 1 : 0, __ATOMIC_RELAXED); #else - rpc_trace_enabled = enabled ? 1 : 0; + wirecall_trace_enabled = enabled ? 1 : 0; #endif } -uint64_t rpc_trace_begin_slow(void) { +uint64_t wirecall_trace_begin_slow(void) { struct timespec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec; } -void rpc_trace_end_slow(rpc_trace_metric metric, uint64_t start_ns) { - rpc_trace_add_slow(metric, rpc_trace_begin_slow() - start_ns); +void wirecall_trace_end_slow(wirecall_trace_metric metric, uint64_t start_ns) { + wirecall_trace_add_slow(metric, wirecall_trace_begin_slow() - start_ns); } -void rpc_trace_add_slow(rpc_trace_metric metric, uint64_t value) { - if ((unsigned)metric >= RPC_TRACE_COUNT) { return; } - rpc_trace_counter *counter = &counters[metric]; +void wirecall_trace_add_slow(wirecall_trace_metric metric, uint64_t value) { + if ((unsigned)metric >= WIRECALL_TRACE_COUNT) { return; } + wirecall_trace_counter *counter = &counters[metric]; atomic_fetch_add_explicit(&counter->count, 1, memory_order_relaxed); atomic_fetch_add_explicit(&counter->total_ns, value, memory_order_relaxed); @@ -94,7 +94,7 @@ void rpc_trace_add_slow(rpc_trace_metric metric, uint64_t value) { memory_order_relaxed)) {} } -static void trace_counter_add(rpc_trace_counter *counter, uint64_t value) { +static void trace_counter_add(wirecall_trace_counter *counter, uint64_t value) { atomic_fetch_add_explicit(&counter->count, 1, memory_order_relaxed); atomic_fetch_add_explicit(&counter->total_ns, value, memory_order_relaxed); @@ -103,23 +103,23 @@ static void trace_counter_add(rpc_trace_counter *counter, uint64_t value) { memory_order_relaxed)) {} } -void rpc_trace_worker_end_slow(uint32_t worker, rpc_trace_worker_metric metric, uint64_t start_ns) { - rpc_trace_worker_add_slow(worker, metric, rpc_trace_begin_slow() - start_ns); +void wirecall_trace_worker_end_slow(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t start_ns) { + wirecall_trace_worker_add_slow(worker, metric, wirecall_trace_begin_slow() - start_ns); } -void rpc_trace_worker_add_slow(uint32_t worker, rpc_trace_worker_metric metric, uint64_t value) { - if (worker >= RPC_TRACE_MAX_WORKERS || (unsigned)metric >= RPC_TRACE_WORKER_COUNT) { return; } +void wirecall_trace_worker_add_slow(uint32_t worker, wirecall_trace_worker_metric metric, uint64_t value) { + if (worker >= WIRECALL_TRACE_MAX_WORKERS || (unsigned)metric >= WIRECALL_TRACE_WORKER_COUNT) { return; } trace_counter_add(&worker_counters[worker][metric], value); } -void rpc_trace_reset(void) { - for (size_t i = 0; i < RPC_TRACE_COUNT; ++i) { +void wirecall_trace_reset(void) { + for (size_t i = 0; i < WIRECALL_TRACE_COUNT; ++i) { atomic_store_explicit(&counters[i].count, 0, memory_order_relaxed); atomic_store_explicit(&counters[i].total_ns, 0, memory_order_relaxed); atomic_store_explicit(&counters[i].max_ns, 0, memory_order_relaxed); } - for (size_t worker = 0; worker < RPC_TRACE_MAX_WORKERS; ++worker) { - for (size_t metric = 0; metric < RPC_TRACE_WORKER_COUNT; ++metric) { + for (size_t worker = 0; worker < WIRECALL_TRACE_MAX_WORKERS; ++worker) { + for (size_t metric = 0; metric < WIRECALL_TRACE_WORKER_COUNT; ++metric) { atomic_store_explicit(&worker_counters[worker][metric].count, 0, memory_order_relaxed); atomic_store_explicit(&worker_counters[worker][metric].total_ns, 0, memory_order_relaxed); atomic_store_explicit(&worker_counters[worker][metric].max_ns, 0, memory_order_relaxed); @@ -127,26 +127,26 @@ void rpc_trace_reset(void) { } } -void rpc_trace_snapshot(rpc_trace_stat out[RPC_TRACE_COUNT]) { - for (size_t i = 0; i < RPC_TRACE_COUNT; ++i) { - out[i] = (rpc_trace_stat){ - .name = trace_names[i], - .count = atomic_load_explicit(&counters[i].count, memory_order_relaxed), - .total = atomic_load_explicit(&counters[i].total_ns, memory_order_relaxed), - .max = atomic_load_explicit(&counters[i].max_ns, memory_order_relaxed), - .is_time = trace_is_time[i], +void wirecall_trace_snapshot(wirecall_trace_stat out[WIRECALL_TRACE_COUNT]) { + for (size_t i = 0; i < WIRECALL_TRACE_COUNT; ++i) { + out[i] = (wirecall_trace_stat){ + .name = trace_names[i], + .count = atomic_load_explicit(&counters[i].count, memory_order_relaxed), + .total = atomic_load_explicit(&counters[i].total_ns, memory_order_relaxed), + .max = atomic_load_explicit(&counters[i].max_ns, memory_order_relaxed), + .is_time = trace_is_time[i], }; } } -void rpc_trace_dump(FILE *out) { - rpc_trace_stat stats[RPC_TRACE_COUNT]; - rpc_trace_snapshot(stats); +void wirecall_trace_dump(FILE *out) { + wirecall_trace_stat stats[WIRECALL_TRACE_COUNT]; + wirecall_trace_snapshot(stats); if (!out) { out = stderr; } fprintf(out, "\ntrace:\n"); fprintf(out, " %-22s %12s %12s %12s\n", "metric", "count", "avg", "max"); - for (size_t i = 0; i < RPC_TRACE_COUNT; ++i) { + for (size_t i = 0; i < WIRECALL_TRACE_COUNT; ++i) { if (stats[i].count == 0) { continue; } if (stats[i].is_time) { char avg[16]; @@ -172,26 +172,26 @@ void rpc_trace_dump(FILE *out) { } int printed_workers = 0; - for (size_t worker = 0; worker < RPC_TRACE_MAX_WORKERS; ++worker) { - rpc_trace_counter *poll_events = &worker_counters[worker][RPC_TRACE_WORKER_POLL_EVENTS]; - rpc_trace_counter *accepts = &worker_counters[worker][RPC_TRACE_WORKER_ACCEPTS]; - rpc_trace_counter *reads = &worker_counters[worker][RPC_TRACE_WORKER_READS]; - rpc_trace_counter *rpcs = &worker_counters[worker][RPC_TRACE_WORKER_RPCS]; - rpc_trace_counter *writes = &worker_counters[worker][RPC_TRACE_WORKER_WRITES]; - rpc_trace_counter *active = &worker_counters[worker][RPC_TRACE_WORKER_ACTIVE]; + for (size_t worker = 0; worker < WIRECALL_TRACE_MAX_WORKERS; ++worker) { + wirecall_trace_counter *poll_events = &worker_counters[worker][WIRECALL_TRACE_WORKER_POLL_EVENTS]; + wirecall_trace_counter *accepts = &worker_counters[worker][WIRECALL_TRACE_WORKER_ACCEPTS]; + wirecall_trace_counter *reads = &worker_counters[worker][WIRECALL_TRACE_WORKER_READS]; + wirecall_trace_counter *rpcs = &worker_counters[worker][WIRECALL_TRACE_WORKER_RPCS]; + wirecall_trace_counter *writes = &worker_counters[worker][WIRECALL_TRACE_WORKER_WRITES]; + wirecall_trace_counter *active = &worker_counters[worker][WIRECALL_TRACE_WORKER_ACTIVE]; uint64_t polls_count = atomic_load_explicit(&poll_events->count, memory_order_relaxed); uint64_t polls_total = atomic_load_explicit(&poll_events->total_ns, memory_order_relaxed); uint64_t polls_max = atomic_load_explicit(&poll_events->max_ns, memory_order_relaxed); uint64_t accept_total = atomic_load_explicit(&accepts->total_ns, memory_order_relaxed); uint64_t read_total = atomic_load_explicit(&reads->total_ns, memory_order_relaxed); - uint64_t rpc_total = atomic_load_explicit(&rpcs->total_ns, memory_order_relaxed); + uint64_t wirecall_total = atomic_load_explicit(&rpcs->total_ns, memory_order_relaxed); uint64_t write_total = atomic_load_explicit(&writes->total_ns, memory_order_relaxed); uint64_t active_count = atomic_load_explicit(&active->count, memory_order_relaxed); uint64_t active_total = atomic_load_explicit(&active->total_ns, memory_order_relaxed); uint64_t active_max = atomic_load_explicit(&active->max_ns, memory_order_relaxed); - if (polls_count == 0 && accept_total == 0 && read_total == 0 && rpc_total == 0 && write_total == 0 && + if (polls_count == 0 && accept_total == 0 && read_total == 0 && wirecall_total == 0 && write_total == 0 && active_count == 0) { continue; } @@ -211,7 +211,7 @@ void rpc_trace_dump(FILE *out) { fprintf(out, " %6zu %8llu %14.2f %10llu %9llu %9llu %9llu %9llu %12s %12s\n", worker, (unsigned long long)polls_count, events_per_poll, (unsigned long long)polls_max, - (unsigned long long)accept_total, (unsigned long long)read_total, (unsigned long long)rpc_total, + (unsigned long long)accept_total, (unsigned long long)read_total, (unsigned long long)wirecall_total, (unsigned long long)write_total, active_avg_buf, active_max_buf); } } diff --git a/tests/test_integration.c b/tests/test_integration.c index b906353..d68163d 100644 --- a/tests/test_integration.c +++ b/tests/test_integration.c @@ -1,19 +1,20 @@ -#include "rpc/client.h" -#include "rpc/server.h" +#include "wirecall/client.h" +#include "wirecall/server.h" #include #include #include -static int add_handler(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int add_handler(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)user_data; - if (argc != 2 || args[0].type != RPC_TYPE_I64 || args[1].type != RPC_TYPE_I64) { return -1; } - rpc_ctx_yield(ctx); - return rpc_writer_i64(out, args[0].as.i64 + args[1].as.i64); + if (argc != 2 || args[0].type != WIRECALL_TYPE_I64 || args[1].type != WIRECALL_TYPE_I64) { return -1; } + wirecall_ctx_yield(ctx); + return wirecall_writer_i64(out, args[0].as.i64 + args[1].as.i64); } static void *server_thread(void *arg) { - rpc_server_run(arg); + wirecall_server_run(arg); return NULL; } @@ -33,33 +34,33 @@ static void *client_thread(void *arg) { char port[16]; port_string(job->port, port); - rpc_client *client = NULL; - assert(rpc_client_connect(&client, "127.0.0.1", port) == 0); + wirecall_client *client = NULL; + assert(wirecall_client_connect(&client, "127.0.0.1", port) == 0); - rpc_writer payload; - rpc_writer_init(&payload); - rpc_writer_i64(&payload, job->index); - rpc_writer_i64(&payload, 10); + wirecall_writer payload; + wirecall_writer_init(&payload); + wirecall_writer_i64(&payload, job->index); + wirecall_writer_i64(&payload, 10); - rpc_value *values = NULL; + wirecall_value *values = NULL; size_t count = 0; - assert(rpc_client_call_name(client, "add", &payload, &values, &count) == 0); + assert(wirecall_client_call_name(client, "add", &payload, &values, &count) == 0); assert(count == 1); assert(values[0].as.i64 == job->index + 10); - rpc_values_free(values); - rpc_writer_free(&payload); - rpc_client_close(client); + wirecall_values_free(values); + wirecall_writer_free(&payload); + wirecall_client_close(client); return NULL; } int main(void) { - rpc_server *server = NULL; - assert(rpc_server_init(&server) == 0); - assert(rpc_server_add_async_route_name(server, "add", add_handler, NULL) == 0); - assert(rpc_server_bind(server, "127.0.0.1", "0") == 0); - assert(rpc_server_listen(server) == 0); - uint16_t port = rpc_server_port(server); + wirecall_server *server = NULL; + assert(wirecall_server_init(&server) == 0); + assert(wirecall_server_add_async_route_name(server, "add", add_handler, NULL) == 0); + assert(wirecall_server_bind(server, "127.0.0.1", "0") == 0); + assert(wirecall_server_listen(server) == 0); + uint16_t port = wirecall_server_port(server); assert(port != 0); pthread_t thread; @@ -68,23 +69,23 @@ int main(void) { char port_buf[16]; port_string(port, port_buf); - rpc_client *client = NULL; - assert(rpc_client_connect(&client, "127.0.0.1", port_buf) == 0); - assert(rpc_client_ping(client) == 0); + wirecall_client *client = NULL; + assert(wirecall_client_connect(&client, "127.0.0.1", port_buf) == 0); + assert(wirecall_client_ping(client) == 0); - rpc_writer payload; - rpc_writer_init(&payload); - rpc_writer_i64(&payload, 5); - rpc_writer_i64(&payload, 6); + wirecall_writer payload; + wirecall_writer_init(&payload); + wirecall_writer_i64(&payload, 5); + wirecall_writer_i64(&payload, 6); - rpc_value *values = NULL; + wirecall_value *values = NULL; size_t count = 0; - assert(rpc_client_call_name(client, "add", &payload, &values, &count) == 0); - assert(count == 1 && values[0].type == RPC_TYPE_I64 && values[0].as.i64 == 11); - rpc_values_free(values); - rpc_writer_reset(&payload); + assert(wirecall_client_call_name(client, "add", &payload, &values, &count) == 0); + assert(count == 1 && values[0].type == WIRECALL_TYPE_I64 && values[0].as.i64 == 11); + wirecall_values_free(values); + wirecall_writer_reset(&payload); - assert(rpc_client_call_name(client, "missing", &payload, &values, &count) != 0); + assert(wirecall_client_call_name(client, "missing", &payload, &values, &count) != 0); enum { CLIENTS = 4 }; pthread_t clients[CLIENTS]; @@ -97,13 +98,13 @@ int main(void) { assert(pthread_join(clients[i], NULL) == 0); } - assert(rpc_server_remove_route_name(server, "add") == 0); - assert(rpc_client_call_name(client, "add", &payload, &values, &count) != 0); - rpc_writer_free(&payload); - rpc_client_close(client); + assert(wirecall_server_remove_route_name(server, "add") == 0); + assert(wirecall_client_call_name(client, "add", &payload, &values, &count) != 0); + wirecall_writer_free(&payload); + wirecall_client_close(client); - rpc_server_stop(server); + wirecall_server_stop(server); assert(pthread_join(thread, NULL) == 0); - rpc_server_destroy(server); + wirecall_server_destroy(server); return 0; } diff --git a/tests/test_protocol.c b/tests/test_protocol.c index 7f52648..549ed3c 100644 --- a/tests/test_protocol.c +++ b/tests/test_protocol.c @@ -1,4 +1,4 @@ -#include "rpc/protocol.h" +#include "wirecall/protocol.h" #include #include @@ -30,25 +30,24 @@ static void test_free(void *ctx, void *ptr) { } int main(void) { - uint8_t buf[RPC_HEADER_SIZE]; - rpc_header h = { - .op = RPC_OP_RPC, - .flags = RPC_FLAG_MORE, - .proc_id = 0x1122334455667788ull, - .size = 9, - .call_id = 0x0102030405060708ull, + uint8_t buf[WIRECALL_HEADER_SIZE]; + wirecall_header h = { + .op = WIRECALL_OP_RPC, + .flags = WIRECALL_FLAG_MORE, + .proc_id = 0x1122334455667788ull, + .size = 9, + .call_id = 0x0102030405060708ull, }; - uint8_t packet_payload[9] = {RPC_TYPE_I64, 0, 0, 0, 0, 0, 0, 0, 42}; + uint8_t packet_payload[9] = {WIRECALL_TYPE_I64, 0, 0, 0, 0, 0, 0, 0, 42}; uint8_t mac_key[16] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, }; - assert(rpc_packet_sign(&h, packet_payload, sizeof(packet_payload)) == 0); + assert(wirecall_packet_sign(&h, packet_payload, sizeof(packet_payload)) == 0); assert(h.checksum != 0); assert(h.mac == 0); - assert(rpc_header_encode(&h, buf) == 0); - rpc_header out; - assert(rpc_header_decode(buf, &out) == 0); + assert(wirecall_header_encode(&h, buf) == 0); + wirecall_header out; + assert(wirecall_header_decode(buf, &out) == 0); assert(out.op == h.op); assert(out.flags == h.flags); assert(out.proc_id == h.proc_id); @@ -56,48 +55,48 @@ int main(void) { assert(out.call_id == h.call_id); assert(out.checksum == h.checksum); assert(out.mac == h.mac); - assert(rpc_packet_verify(&out, packet_payload, sizeof(packet_payload)) == 0); + assert(wirecall_packet_verify(&out, packet_payload, sizeof(packet_payload)) == 0); packet_payload[8] = 43; - assert(rpc_packet_verify(&out, packet_payload, sizeof(packet_payload)) != 0); + assert(wirecall_packet_verify(&out, packet_payload, sizeof(packet_payload)) != 0); packet_payload[8] = 42; - assert(rpc_packet_sign_ex(&h, packet_payload, sizeof(packet_payload), RPC_INTEGRITY_CHECKSUM | RPC_INTEGRITY_MAC, - mac_key) == 0); + assert(wirecall_packet_sign_ex(&h, packet_payload, sizeof(packet_payload), + WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) == 0); assert(h.checksum != 0); assert(h.mac != 0); - assert(rpc_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), RPC_INTEGRITY_CHECKSUM | RPC_INTEGRITY_MAC, - mac_key) == 0); + assert(wirecall_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), + WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) == 0); h.mac ^= 1u; - assert(rpc_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), RPC_INTEGRITY_CHECKSUM | RPC_INTEGRITY_MAC, - mac_key) != 0); + assert(wirecall_packet_verify_ex(&h, packet_payload, sizeof(packet_payload), + WIRECALL_INTEGRITY_CHECKSUM | WIRECALL_INTEGRITY_MAC, mac_key) != 0); h.mac ^= 1u; buf[0] = 99; - assert(rpc_header_decode(buf, &out) != 0); + assert(wirecall_header_decode(buf, &out) != 0); alloc_stats stats = {0}; - rpc_allocator allocator = { - .ctx = &stats, - .alloc = test_alloc, - .realloc = test_realloc, - .free = test_free, + wirecall_allocator allocator = { + .ctx = &stats, + .alloc = test_alloc, + .realloc = test_realloc, + .free = test_free, }; - assert(rpc_set_allocator(&allocator) == 0); + assert(wirecall_set_allocator(&allocator) == 0); - rpc_writer w; - rpc_writer_init(&w); - assert(rpc_writer_null(&w) == 0); - assert(rpc_writer_bool(&w, true) == 0); - assert(rpc_writer_i64(&w, -42) == 0); - assert(rpc_writer_u64(&w, 42) == 0); - assert(rpc_writer_f64(&w, 3.5) == 0); - assert(rpc_writer_bytes(&w, "abc", 3) == 0); - assert(rpc_writer_string(&w, "hello", 5) == 0); + wirecall_writer w; + wirecall_writer_init(&w); + assert(wirecall_writer_null(&w) == 0); + assert(wirecall_writer_bool(&w, true) == 0); + assert(wirecall_writer_i64(&w, -42) == 0); + assert(wirecall_writer_u64(&w, 42) == 0); + assert(wirecall_writer_f64(&w, 3.5) == 0); + assert(wirecall_writer_bytes(&w, "abc", 3) == 0); + assert(wirecall_writer_string(&w, "hello", 5) == 0); - rpc_value *values = NULL; + wirecall_value *values = NULL; size_t count = 0; - assert(rpc_payload_decode(w.data, w.len, &values, &count) == 0); + assert(wirecall_payload_decode(w.data, w.len, &values, &count) == 0); assert(count == 7); - assert(values[0].type == RPC_TYPE_NULL); + assert(values[0].type == WIRECALL_TYPE_NULL); assert(values[1].as.boolean); assert(values[2].as.i64 == -42); assert(values[3].as.u64 == 42); @@ -106,13 +105,13 @@ int main(void) { assert(memcmp(values[5].as.bytes.data, "abc", 3) == 0); assert(values[6].as.string.len == 5); assert(memcmp(values[6].as.string.data, "hello", 5) == 0); - rpc_values_free(values); + wirecall_values_free(values); - uint8_t malformed[] = {RPC_TYPE_STRING, 0, 0, 0, 10, 'x'}; - assert(rpc_payload_decode(malformed, sizeof(malformed), &values, &count) != 0); - rpc_writer_free(&w); + uint8_t malformed[] = {WIRECALL_TYPE_STRING, 0, 0, 0, 10, 'x'}; + assert(wirecall_payload_decode(malformed, sizeof(malformed), &values, &count) != 0); + wirecall_writer_free(&w); assert(stats.reallocs > 0); assert(stats.frees > 0); - assert(rpc_set_allocator(NULL) == 0); + assert(wirecall_set_allocator(NULL) == 0); return 0; } diff --git a/tests/test_routes.c b/tests/test_routes.c index 1b2440a..ba18b5c 100644 --- a/tests/test_routes.c +++ b/tests/test_routes.c @@ -2,7 +2,8 @@ #include -static int handler_a(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int handler_a(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)ctx; (void)args; (void)argc; @@ -11,7 +12,8 @@ static int handler_a(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_write return 0; } -static int handler_b(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int handler_b(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { (void)ctx; (void)args; (void)argc; @@ -26,25 +28,25 @@ static void count_finalizer(void *user_data) { } int main(void) { - rpc_routes routes; - rpc_route route; + wirecall_routes routes; + wirecall_route route; int finalized_a = 0; int finalized_b = 0; - assert(rpc_routes_init(&routes) == 0); - assert(rpc_routes_lookup(&routes, 10, &route) != 0); - assert(rpc_routes_add_ex(&routes, 10, handler_a, &finalized_a, count_finalizer, 0) == 0); - assert(rpc_routes_lookup(&routes, 10, &route) == 0); + assert(wirecall_routes_init(&routes) == 0); + assert(wirecall_routes_lookup(&routes, 10, &route) != 0); + assert(wirecall_routes_add_ex(&routes, 10, handler_a, &finalized_a, count_finalizer, 0) == 0); + assert(wirecall_routes_lookup(&routes, 10, &route) == 0); assert(route.handler == handler_a); assert(route.user_data == &finalized_a); - assert(rpc_routes_add_ex(&routes, 10, handler_b, &finalized_b, count_finalizer, 0) == 0); + assert(wirecall_routes_add_ex(&routes, 10, handler_b, &finalized_b, count_finalizer, 0) == 0); assert(finalized_a == 1); - assert(rpc_routes_lookup(&routes, 10, &route) == 0); + assert(wirecall_routes_lookup(&routes, 10, &route) == 0); assert(route.handler == handler_b); assert(route.user_data == &finalized_b); - assert(rpc_routes_remove(&routes, 10) == 0); + assert(wirecall_routes_remove(&routes, 10) == 0); assert(finalized_b == 1); - assert(rpc_routes_lookup(&routes, 10, &route) != 0); - rpc_routes_destroy(&routes); + assert(wirecall_routes_lookup(&routes, 10, &route) != 0); + wirecall_routes_destroy(&routes); assert(finalized_a == 1); assert(finalized_b == 1); return 0; diff --git a/tests/test_scheduler.c b/tests/test_scheduler.c index bbd5162..335e991 100644 --- a/tests/test_scheduler.c +++ b/tests/test_scheduler.c @@ -4,42 +4,44 @@ static int done_count; -static int yielding_handler(rpc_ctx *ctx, const rpc_value *args, size_t argc, rpc_writer *out, void *user_data) { +static int yielding_handler(wirecall_ctx *ctx, const wirecall_value *args, size_t argc, wirecall_writer *out, + void *user_data) { int *seen = user_data; assert(argc == 1); - assert(args[0].type == RPC_TYPE_I64); + assert(args[0].type == WIRECALL_TYPE_I64); *seen += 1; - rpc_ctx_yield(ctx); + wirecall_ctx_yield(ctx); *seen += 1; - return rpc_writer_i64(out, args[0].as.i64 + 1); + return wirecall_writer_i64(out, args[0].as.i64 + 1); } -static void on_done(rpc_call *call, void *user_data) { +static void on_done(wirecall_call *call, void *user_data) { (void)user_data; done_count++; - assert(rpc_call_result(call) == 0); - const rpc_writer *w = rpc_call_response(call); - rpc_value *values = NULL; + assert(wirecall_call_result(call) == 0); + const wirecall_writer *w = wirecall_call_response(call); + wirecall_value *values = NULL; size_t count = 0; - assert(rpc_payload_decode(w->data, w->len, &values, &count) == 0); + assert(wirecall_payload_decode(w->data, w->len, &values, &count) == 0); assert(count == 1); - assert(values[0].type == RPC_TYPE_I64); + assert(values[0].type == WIRECALL_TYPE_I64); assert(values[0].as.i64 == 42); - rpc_values_free(values); + wirecall_values_free(values); } int main(void) { - rpc_scheduler *scheduler = NULL; + wirecall_scheduler *scheduler = NULL; int seen = 0; - rpc_writer payload; - rpc_writer_init(&payload); - assert(rpc_writer_i64(&payload, 41) == 0); - assert(rpc_scheduler_init(&scheduler) == 0); - assert(rpc_scheduler_submit(scheduler, 7, 9, yielding_handler, &seen, payload.data, payload.len, on_done, NULL) == 0); - rpc_scheduler_run_ready(scheduler); + wirecall_writer payload; + wirecall_writer_init(&payload); + assert(wirecall_writer_i64(&payload, 41) == 0); + assert(wirecall_scheduler_init(&scheduler) == 0); + assert( + wirecall_scheduler_submit(scheduler, 7, 9, yielding_handler, &seen, payload.data, payload.len, on_done, NULL) == 0); + wirecall_scheduler_run_ready(scheduler); assert(seen == 2); assert(done_count == 1); - rpc_scheduler_destroy(scheduler); - rpc_writer_free(&payload); + wirecall_scheduler_destroy(scheduler); + wirecall_writer_free(&payload); return 0; }