From 955b932e41d580de4e4e5110ce14bc7724bf0d7b Mon Sep 17 00:00:00 2001 From: spacewander Date: Thu, 30 Sep 2021 16:52:37 +0800 Subject: [PATCH] feat: turn wasm vm off by default --- README.md | 15 ++++++ src/http/ngx_http_wasm_module.c | 91 +++++++++++++++++++++++++++++++-- t/WASM.pm | 1 + 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 049a0be..3e4f8a4 100644 --- a/README.md +++ b/README.md @@ -18,8 +18,23 @@ export wasm_prefix=/path/to/wasm-nginx-module/wasmtime-c-api --with-ld-opt="-Wl,-rpath,${wasm_prefix}/lib" \ ``` +## Directives + +### wasm_vm + +**syntax:** *wasm_vm wasmtime* + +**default:** - + +**context:** *http* + +Select the WASM VM. Currently, only wasmtime is supported. If the directive is +set, the WASM VM won't be enabled. + ## Methods +**Remember to set the `wasm_vm` directive!** + ### load `syntax: plugin, err = proxy_wasm.load(path)` diff --git a/src/http/ngx_http_wasm_module.c b/src/http/ngx_http_wasm_module.c index f94a39b..b7925b4 100644 --- a/src/http/ngx_http_wasm_module.c +++ b/src/http/ngx_http_wasm_module.c @@ -6,6 +6,9 @@ static ngx_int_t ngx_http_wasm_init(ngx_conf_t *cf); +static void *ngx_http_wasm_create_main_conf(ngx_conf_t *cf); +static char *ngx_http_wasm_init_main_conf(ngx_conf_t *cf, void *conf); +static char *ngx_http_wasm_vm(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static bool ngx_http_wasm_vm_inited = false; @@ -18,6 +21,11 @@ static ngx_str_t proxy_on_done = ngx_string("proxy_on_done"); static ngx_str_t proxy_on_delete = ngx_string("proxy_on_delete"); +typedef struct { + ngx_str_t vm; +} ngx_http_wasm_main_conf_t; + + typedef struct { void *plugin; uint32_t cur_ctx_id; @@ -36,12 +44,24 @@ typedef struct { } ngx_http_wasm_plugin_ctx_t; +static ngx_command_t ngx_http_wasm_cmds[] = { + { ngx_string("wasm_vm"), + NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1, + ngx_http_wasm_vm, + NGX_HTTP_MAIN_CONF_OFFSET, + 0, + NULL }, + + ngx_null_command +}; + + static ngx_http_module_t ngx_http_wasm_module_ctx = { NULL, /* preconfiguration */ ngx_http_wasm_init, /* postconfiguration */ - NULL, /* create main configuration */ - NULL, /* init main configuration */ + ngx_http_wasm_create_main_conf, /* create main configuration */ + ngx_http_wasm_init_main_conf, /* init main configuration */ NULL, /* create server configuration */ NULL, /* merge server configuration */ @@ -54,7 +74,7 @@ static ngx_http_module_t ngx_http_wasm_module_ctx = { ngx_module_t ngx_http_wasm_module = { NGX_MODULE_V1, &ngx_http_wasm_module_ctx, /* module context */ - NULL, /* module directives */ + ngx_http_wasm_cmds, /* module directives */ NGX_HTTP_MODULE, /* module type */ NULL, /* init master */ NULL, /* init module */ @@ -67,16 +87,71 @@ ngx_module_t ngx_http_wasm_module = { }; +static void * +ngx_http_wasm_create_main_conf(ngx_conf_t *cf) +{ + ngx_http_wasm_main_conf_t *wmcf; + + wmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_wasm_main_conf_t)); + if (wmcf == NULL) { + return NULL; + } + + /* set by ngx_pcalloc: + * wmcf->vm = { 0, NULL }; + */ + + return wmcf; +} + + +static char * +ngx_http_wasm_init_main_conf(ngx_conf_t *cf, void *conf) +{ + return NGX_CONF_OK; +} + + +static char * +ngx_http_wasm_vm(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ + ngx_http_wasm_main_conf_t *wmcf = conf; + ngx_str_t *value; + + if (wmcf->vm.len != 0) { + return "is duplicate"; + } + + value = cf->args->elts; + + if (ngx_strcmp(value[1].data, "wasmtime") != 0) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unsupported wasm vm %V", &value[1]); + return NGX_CONF_ERROR; + } + + wmcf->vm.len = value[1].len; + wmcf->vm.data = value[1].data; + + return NGX_CONF_OK; +} + + static ngx_int_t ngx_http_wasm_init(ngx_conf_t *cf) { ngx_int_t rc; ngx_pool_cleanup_t *cln; + ngx_http_wasm_main_conf_t *wmcf; if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config || ngx_http_wasm_vm_inited) { return NGX_OK; } + wmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_wasm_module); + if (wmcf->vm.len == 0) { + return NGX_OK; + } + cln = ngx_pool_cleanup_add(cf->pool, 0); if (cln == NULL) { return NGX_ERROR; @@ -105,6 +180,11 @@ ngx_http_wasm_load_plugin(const char *bytecode, size_t size) ngx_int_t rc; ngx_http_wasm_plugin_t *hw_plugin; + if (!ngx_http_wasm_vm_inited) { + ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "miss wasm_vm configuration"); + return NULL; + } + plugin = ngx_wasm_vm.load(bytecode, size); if (plugin == NULL) { @@ -217,6 +297,11 @@ ngx_http_wasm_on_configure(ngx_http_wasm_plugin_t *hw_plugin, const char *conf, log = ngx_cycle->log; + if (!ngx_http_wasm_vm_inited) { + ngx_log_error(NGX_LOG_ERR, log, 0, "miss wasm_vm configuration"); + return NULL; + } + if (!ngx_queue_empty(&hw_plugin->free)) { ngx_queue_t *q; diff --git a/t/WASM.pm b/t/WASM.pm index f65f46f..96807f6 100644 --- a/t/WASM.pm +++ b/t/WASM.pm @@ -30,6 +30,7 @@ add_block_preprocessor(sub { my $http_config = $block->http_config // ''; $http_config .= <<_EOC_; lua_package_path "lib/?.lua;;"; + wasm_vm wasmtime; _EOC_ $block->set_value("http_config", $http_config);