Skip to content

Commit 4e3bfa5

Browse files
authored
feat: turn wasm vm off by default (#10)
1 parent 07694c1 commit 4e3bfa5

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,23 @@ export wasm_prefix=/path/to/wasm-nginx-module/wasmtime-c-api
1717
--with-ld-opt="-Wl,-rpath,${wasm_prefix}/lib" \
1818
```
1919

20+
## Directives
21+
22+
### wasm_vm
23+
24+
**syntax:** *wasm_vm wasmtime*
25+
26+
**default:** -
27+
28+
**context:** *http*
29+
30+
Select the WASM VM. Currently, only wasmtime is supported. If the directive is
31+
set, the WASM VM won't be enabled.
32+
2033
## Methods
2134

35+
**Remember to set the `wasm_vm` directive!**
36+
2237
### load
2338

2439
`syntax: plugin, err = proxy_wasm.load(path)`

src/http/ngx_http_wasm_module.c

Lines changed: 88 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77

88
static ngx_int_t ngx_http_wasm_init(ngx_conf_t *cf);
9+
static void *ngx_http_wasm_create_main_conf(ngx_conf_t *cf);
10+
static char *ngx_http_wasm_init_main_conf(ngx_conf_t *cf, void *conf);
11+
static char *ngx_http_wasm_vm(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
912

1013

1114
static bool ngx_http_wasm_vm_inited = false;
@@ -18,6 +21,11 @@ static ngx_str_t proxy_on_done = ngx_string("proxy_on_done");
1821
static ngx_str_t proxy_on_delete = ngx_string("proxy_on_delete");
1922

2023

24+
typedef struct {
25+
ngx_str_t vm;
26+
} ngx_http_wasm_main_conf_t;
27+
28+
2129
typedef struct {
2230
void *plugin;
2331
uint32_t cur_ctx_id;
@@ -36,12 +44,24 @@ typedef struct {
3644
} ngx_http_wasm_plugin_ctx_t;
3745

3846

47+
static ngx_command_t ngx_http_wasm_cmds[] = {
48+
{ ngx_string("wasm_vm"),
49+
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
50+
ngx_http_wasm_vm,
51+
NGX_HTTP_MAIN_CONF_OFFSET,
52+
0,
53+
NULL },
54+
55+
ngx_null_command
56+
};
57+
58+
3959
static ngx_http_module_t ngx_http_wasm_module_ctx = {
4060
NULL, /* preconfiguration */
4161
ngx_http_wasm_init, /* postconfiguration */
4262

43-
NULL, /* create main configuration */
44-
NULL, /* init main configuration */
63+
ngx_http_wasm_create_main_conf, /* create main configuration */
64+
ngx_http_wasm_init_main_conf, /* init main configuration */
4565

4666
NULL, /* create server configuration */
4767
NULL, /* merge server configuration */
@@ -54,7 +74,7 @@ static ngx_http_module_t ngx_http_wasm_module_ctx = {
5474
ngx_module_t ngx_http_wasm_module = {
5575
NGX_MODULE_V1,
5676
&ngx_http_wasm_module_ctx, /* module context */
57-
NULL, /* module directives */
77+
ngx_http_wasm_cmds, /* module directives */
5878
NGX_HTTP_MODULE, /* module type */
5979
NULL, /* init master */
6080
NULL, /* init module */
@@ -67,16 +87,71 @@ ngx_module_t ngx_http_wasm_module = {
6787
};
6888

6989

90+
static void *
91+
ngx_http_wasm_create_main_conf(ngx_conf_t *cf)
92+
{
93+
ngx_http_wasm_main_conf_t *wmcf;
94+
95+
wmcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_wasm_main_conf_t));
96+
if (wmcf == NULL) {
97+
return NULL;
98+
}
99+
100+
/* set by ngx_pcalloc:
101+
* wmcf->vm = { 0, NULL };
102+
*/
103+
104+
return wmcf;
105+
}
106+
107+
108+
static char *
109+
ngx_http_wasm_init_main_conf(ngx_conf_t *cf, void *conf)
110+
{
111+
return NGX_CONF_OK;
112+
}
113+
114+
115+
static char *
116+
ngx_http_wasm_vm(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
117+
{
118+
ngx_http_wasm_main_conf_t *wmcf = conf;
119+
ngx_str_t *value;
120+
121+
if (wmcf->vm.len != 0) {
122+
return "is duplicate";
123+
}
124+
125+
value = cf->args->elts;
126+
127+
if (ngx_strcmp(value[1].data, "wasmtime") != 0) {
128+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "unsupported wasm vm %V", &value[1]);
129+
return NGX_CONF_ERROR;
130+
}
131+
132+
wmcf->vm.len = value[1].len;
133+
wmcf->vm.data = value[1].data;
134+
135+
return NGX_CONF_OK;
136+
}
137+
138+
70139
static ngx_int_t
71140
ngx_http_wasm_init(ngx_conf_t *cf)
72141
{
73142
ngx_int_t rc;
74143
ngx_pool_cleanup_t *cln;
144+
ngx_http_wasm_main_conf_t *wmcf;
75145

76146
if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config || ngx_http_wasm_vm_inited) {
77147
return NGX_OK;
78148
}
79149

150+
wmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_wasm_module);
151+
if (wmcf->vm.len == 0) {
152+
return NGX_OK;
153+
}
154+
80155
cln = ngx_pool_cleanup_add(cf->pool, 0);
81156
if (cln == NULL) {
82157
return NGX_ERROR;
@@ -105,6 +180,11 @@ ngx_http_wasm_load_plugin(const char *bytecode, size_t size)
105180
ngx_int_t rc;
106181
ngx_http_wasm_plugin_t *hw_plugin;
107182

183+
if (!ngx_http_wasm_vm_inited) {
184+
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0, "miss wasm_vm configuration");
185+
return NULL;
186+
}
187+
108188
plugin = ngx_wasm_vm.load(bytecode, size);
109189

110190
if (plugin == NULL) {
@@ -217,6 +297,11 @@ ngx_http_wasm_on_configure(ngx_http_wasm_plugin_t *hw_plugin, const char *conf,
217297

218298
log = ngx_cycle->log;
219299

300+
if (!ngx_http_wasm_vm_inited) {
301+
ngx_log_error(NGX_LOG_ERR, log, 0, "miss wasm_vm configuration");
302+
return NULL;
303+
}
304+
220305
if (!ngx_queue_empty(&hw_plugin->free)) {
221306
ngx_queue_t *q;
222307

t/WASM.pm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ add_block_preprocessor(sub {
3030
my $http_config = $block->http_config // '';
3131
$http_config .= <<_EOC_;
3232
lua_package_path "lib/?.lua;;";
33+
wasm_vm wasmtime;
3334
_EOC_
3435

3536
$block->set_value("http_config", $http_config);

0 commit comments

Comments
 (0)