Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down
91 changes: 88 additions & 3 deletions src/http/ngx_http_wasm_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions t/WASM.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down