summaryrefslogtreecommitdiffhomepage
path: root/src/wasm/nxt_wasm.h
diff options
context:
space:
mode:
authorAndrew Clayton <a.clayton@nginx.com>2023-08-08 14:29:54 +0100
committerAndrew Clayton <a.clayton@nginx.com>2023-08-17 13:09:14 +0100
commit6a211e2b7468a2524715b7f00a24aa460481bea7 (patch)
tree31a765fefa0d6c400a0fe0d4fcbea0b700827c4f /src/wasm/nxt_wasm.h
parent0c444397366aa07e23573a03e733a0552187eac4 (diff)
downloadunit-6a211e2b7468a2524715b7f00a24aa460481bea7.tar.gz
unit-6a211e2b7468a2524715b7f00a24aa460481bea7.tar.bz2
Wasm: Add the core of initial WebAssembly language module support.
This adds the core of runtime WebAssembly[0] support. Future commits will enable this in the Unit core and expose the configuration. This introduces a new src/wasm directory for storing this source. We are initially using Wasmtime[0] as the WebAssembly runtime, however this has been designed with the ability to use different runtimes in mind. src/wasm/nxt_wasm.[ch] is the main interface to Unit. src/wasm/nxt_rt_wasmtime.c is the Wasmtime runtime support. This is nicely insulated from any knowledge of internal Unit workings. Wasmtime is what loads and runs the Wasm modules. The Wasm modules can export functions Wasmtime can call and Wasmtime can export functions that the module can call. We make use of both. The terminology used is that function exports are what the Wasm module exports and function imports are what the Wasm runtime exports to the module. We currently have four function imports (functions exported by the runtime to be called by the Wasm module). 1) nxt_wasm_get_init_mem_size This allows Wasm modules to get the size of the initially allocated shared memory. This is the size allocated at Unit startup and what the Wasm modules can assume they have access to (in reality this shared memory will likely be larger). The amount of memory allocated at startup is NXT_WASM_MEM_SIZE which as of this commit is 32MiB. We do actually allocate NXT_WASM_MEM_SIZE + NXT_WASM_PAGE_SIZE at startup which is an extra 64KiB (the smallest allocation unit), this is to allow room for the response structure and so module developers can just assume they have the full 32MiB for their actual response. 2) nxt_wasm_send_headers This allows WASM modules to send their headers. 3) nxt_wasm_send_response This allows WASM modules to send their response. 4) nxt_wasm_response_end This allows WASM modules to inform Unit they have finished sending their response. This calls nxt_unit_request_done() Then there are currently up to eight functions that a module can export. Three of which are required. These function can be named anything. I'll use the Unit configuration names to refer to them 1) request_handler The main driving function. This may be called multiple times for a single HTTP request if the request is larger than the shared memory. 2) malloc_handler Used to allocate a chunk of memory at language module startup. This memory is allocated from the WASM modules address space and is what is sued for communicating between the WASM module (the guest) and Unit (the host). 3) free_handler Used to free the memory from above at language module shutdown. Then there are the following optional handlers 1) module_init_handler If set, called at language module startup. 2) module_end_handler If set, called at language module shutdown. 3) request_init_handler If set, called at the start of request. Called only once per HTTP request. 4) request_end_handler If set, called once all of a request has been sent to the WASM module. 5) response_end_handler If set, called at the end of a request, once the WASM module has sent all its headers and data. 32bits We currently support 32bit WASM modules, I.e wasm32-wasi. Newer version of clang, 13+[2], do seem to have support for wasm64 as a target (which uses a LP64 model). However it's not entirely clear if the WASI SDK fully supports[3] this and by extension WASI libc/wasi-sysroot. 64bit support is something than can be explored more thoroughly in the future. As such in structures that are used to communicate between the host and guest we use 32bit ints. Even when a single byte might be enough. This is to avoid issues with structure layout differences between a 64bit host and 32bit guest (I.e WASM module) and the need for various bits of structure padding depending on host architecture. Instead everything is 4-byte aligned. [0]: <https://webassembly.org/> [1]: <https://wasmtime.dev/> [2]: <https://reviews.llvm.org/rG670944fb20b226fc22fa993ab521125f9adbd30a> [3]: <https://github.com/WebAssembly/wasi-sdk/issues/185> Reviewed-by: Alejandro Colomar <alx@nginx.com> Signed-off-by: Andrew Clayton <a.clayton@nginx.com>
Diffstat (limited to 'src/wasm/nxt_wasm.h')
-rw-r--r--src/wasm/nxt_wasm.h136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/wasm/nxt_wasm.h b/src/wasm/nxt_wasm.h
new file mode 100644
index 00000000..9e18f931
--- /dev/null
+++ b/src/wasm/nxt_wasm.h
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) Andrew Clayton
+ * Copyright (C) F5, Inc.
+ */
+
+#ifndef _NXT_WASM_H_INCLUDED_
+#define _NXT_WASM_H_INCLUDED_
+
+#include <stddef.h>
+#include <stdint.h>
+
+#include <nxt_unit.h>
+
+#include <wasm.h>
+#if defined(NXT_HAVE_WASM_WASMTIME)
+#include <wasmtime.h>
+#endif
+
+
+#define NXT_WASM_PAGE_SIZE (64 * 1024)
+#define NXT_WASM_MEM_SIZE (32UL * 1024 * 1024)
+
+#if defined(NXT_HAVE_WASM_WASMTIME)
+typedef wasmtime_func_t nxt_wasm_func_t;
+#endif
+
+
+typedef struct nxt_wasm_http_field_s nxt_wasm_http_field_t;
+typedef struct nxt_wasm_request_s nxt_wasm_request_t;
+typedef struct nxt_wasm_response_s nxt_wasm_response_t;
+typedef struct nxt_wasm_response_fields_s nxt_wasm_response_fields_t;
+typedef enum nxt_wasm_fh_e nxt_wasm_fh_t;
+typedef struct nxt_wasm_func_handler_s nxt_wasm_func_handler_t;
+typedef struct nxt_wasm_ctx_s nxt_wasm_ctx_t;
+typedef struct nxt_wasm_operations_s nxt_wasm_operations_t;
+
+struct nxt_wasm_http_field_s {
+ uint32_t name_off;
+ uint32_t name_len;
+ uint32_t value_off;
+ uint32_t value_len;
+};
+
+struct nxt_wasm_request_s {
+ uint32_t method_off;
+ uint32_t method_len;
+ uint32_t version_off;
+ uint32_t version_len;
+ uint32_t path_off;
+ uint32_t path_len;
+ uint32_t query_off;
+ uint32_t query_len;
+ uint32_t remote_off;
+ uint32_t remote_len;
+ uint32_t local_addr_off;
+ uint32_t local_addr_len;
+ uint32_t local_port_off;
+ uint32_t local_port_len;
+ uint32_t server_name_off;
+ uint32_t server_name_len;
+
+ uint32_t content_off;
+ uint32_t content_len;
+ uint32_t content_sent;
+ uint32_t total_content_sent;
+
+ uint32_t request_size;
+
+ uint32_t nfields;
+
+ uint32_t tls;
+
+ nxt_wasm_http_field_t fields[];
+};
+
+struct nxt_wasm_response_s {
+ uint32_t size;
+
+ uint8_t data[];
+};
+
+struct nxt_wasm_response_fields_s {
+ uint32_t nfields;
+
+ nxt_wasm_http_field_t fields[];
+};
+
+enum nxt_wasm_fh_e {
+ NXT_WASM_FH_REQUEST = 0,
+ NXT_WASM_FH_MALLOC,
+ NXT_WASM_FH_FREE,
+
+ /* Optional handlers */
+ NXT_WASM_FH_MODULE_INIT,
+ NXT_WASM_FH_MODULE_END,
+ NXT_WASM_FH_REQUEST_INIT,
+ NXT_WASM_FH_REQUEST_END,
+ NXT_WASM_FH_RESPONSE_END,
+
+ NXT_WASM_FH_NR
+};
+
+struct nxt_wasm_func_handler_s {
+ const char *func_name;
+ nxt_wasm_func_t func;
+};
+
+struct nxt_wasm_ctx_s {
+ const char *module_path;
+
+ nxt_wasm_func_handler_t fh[NXT_WASM_FH_NR];
+
+ nxt_unit_request_info_t *req;
+
+ uint8_t *baddr;
+ size_t baddr_off;
+
+ size_t response_off;
+};
+
+struct nxt_wasm_operations_s {
+ int (*init)(nxt_wasm_ctx_t *ctx);
+ void (*destroy)(const nxt_wasm_ctx_t *ctx);
+ void (*exec_request)(const nxt_wasm_ctx_t *ctx);
+ void (*exec_hook)(const nxt_wasm_ctx_t *ctx, nxt_wasm_fh_t hook);
+};
+
+extern const nxt_wasm_operations_t nxt_wasm_ops;
+
+
+/* Exported to the WASM module */
+extern void nxt_wasm_do_response_end(nxt_wasm_ctx_t *ctx);
+extern void nxt_wasm_do_send_response(nxt_wasm_ctx_t *ctx, uint32_t offset);
+extern void nxt_wasm_do_send_headers(nxt_wasm_ctx_t *ctx, uint32_t offset);
+
+#endif /* _NXT_WASM_H_INCLUDED_ */