Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(proxy-wasm) v0.1.0 proxy_wasm_get_configuration support #7

Merged
merged 2 commits into from
Apr 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/common/proxy_wasm/ngx_proxy_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ ngx_proxy_wasm_init(ngx_proxy_wasm_t *pwm)

rc = ngx_wavm_instance_call_funcref(pwm->instance,
pwm->proxy_on_plugin_start, &rets,
pwm->rctxid, 0); // TODO: size
pwm->rctxid, pwm->config.len);
if (rc != NGX_OK || !rets->data[0].of.i32) {
goto error;
}
Expand All @@ -320,6 +320,12 @@ ngx_proxy_wasm_init(ngx_proxy_wasm_t *pwm)
void
ngx_proxy_wasm_destroy(ngx_proxy_wasm_t *pwm)
{
if (pwm->config.len) {
ngx_pfree(pwm->pool, pwm->config.data);
pwm->config.data = NULL;
pwm->config.len = 0;
}

ngx_wavm_ctx_destroy(&pwm->wvctx);
}

Expand Down
1 change: 1 addition & 0 deletions src/common/proxy_wasm/ngx_proxy_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ typedef ngx_uint_t (*ngx_proxy_wasm_ecode_pt)(ngx_proxy_wasm_t *pwm, ngx_uint_t

struct ngx_proxy_wasm_s {

ngx_str_t config;
ngx_proxy_wasm_resume_pt resume_;
ngx_proxy_wasm_ctxid_pt ctxid_;
ngx_proxy_wasm_ecode_pt ecode_;
Expand Down
35 changes: 34 additions & 1 deletion src/common/proxy_wasm/ngx_proxy_wasm_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,39 @@ ngx_proxy_wasm_hfuncs_send_http_response(ngx_wavm_instance_t *instance,
}


static ngx_int_t
ngx_proxy_wasm_hfuncs_get_configuration(ngx_wavm_instance_t *instance,
wasm_val_t args[], wasm_val_t rets[])
{
size_t *rlen;
ngx_wavm_ptr_t *rbuf, p;
ngx_proxy_wasm_t *pwm;

pwm = ngx_proxy_wasm_get_pwm(instance);

rbuf = ngx_wavm_memory_lift(instance->memory, args[0].of.i32);
rlen = ngx_wavm_memory_lift(instance->memory, args[1].of.i32);

if (pwm->config.len) {
p = ngx_proxy_wasm_alloc(pwm, pwm->config.len);
if (p == 0) {
return ngx_proxy_wasm_result_err(rets);
}

if (!ngx_wavm_memory_memcpy(instance->memory, p,
pwm->config.data, pwm->config.len))
{
return ngx_proxy_wasm_result_invalid_mem(rets);
}

*rbuf = p;
*rlen = pwm->config.len;
}

return ngx_proxy_wasm_result_ok(rets);
}


static ngx_int_t
ngx_proxy_wasm_hfuncs_nop(ngx_wavm_instance_t *instance,
wasm_val_t args[], wasm_val_t rets[])
Expand Down Expand Up @@ -744,7 +777,7 @@ static ngx_wavm_host_func_def_t ngx_proxy_wasm_hfuncs[] = {
ngx_wavm_arity_i32 },

{ ngx_string("proxy_get_configuration"),
&ngx_proxy_wasm_hfuncs_nop,
&ngx_proxy_wasm_hfuncs_get_configuration,
ngx_wavm_arity_i32x2,
ngx_wavm_arity_i32 },

Expand Down
6 changes: 3 additions & 3 deletions src/http/ngx_http_wasm_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ static ngx_command_t ngx_http_wasm_module_cmds[] = {
&ngx_http_wasm_op_post },

{ ngx_string("proxy_wasm"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
ngx_http_wasm_proxy_wasm_directive,
NGX_HTTP_LOC_CONF_OFFSET,
NGX_HTTP_MODULE,
Expand Down Expand Up @@ -286,8 +286,8 @@ ngx_http_wasm_proxy_wasm_directive(ngx_conf_t *cf, ngx_command_t *cmd,

loc->pwmodule->max_pairs = NGX_HTTP_WASM_MAX_REQ_HEADERS;

op = ngx_wasm_conf_add_op_proxy_wasm(cf, loc->ops_engine, cf->args->elts,
loc->pwmodule);
op = ngx_wasm_conf_add_op_proxy_wasm(cf, loc->ops_engine, cf->args->nelts,
cf->args->elts, loc->pwmodule);
if (op == NULL) {
return NGX_CONF_ERROR;
}
Expand Down
54 changes: 40 additions & 14 deletions src/wasm/ngx_wasm_ops.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,22 @@ ngx_wasm_ops_engine_destroy(ngx_wasm_ops_engine_t *engine)

static ngx_int_t
ngx_wasm_op_add_helper(ngx_conf_t *cf, ngx_wasm_ops_engine_t *ops_engine,
ngx_wasm_op_t *op, ngx_str_t *mod_name)
ngx_wasm_op_t *op, ngx_str_t *arg_name)
{
ngx_wasm_phase_t *phase = ops_engine->subsystem->phases;
ngx_wasm_ops_pipeline_t *pipeline;
ngx_wasm_op_t **opp;

if (mod_name->len == 0) {
if (arg_name->len == 0) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid module name \"%V\"", mod_name);
"invalid module name \"%V\"", arg_name);
return NGX_ERROR;
}

op->module = ngx_wavm_module_lookup(ops_engine->vm, mod_name);
op->module = ngx_wavm_module_lookup(ops_engine->vm, arg_name);
if (op->module == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"no \"%V\" module defined", mod_name);
"no \"%V\" module defined", arg_name);
return NGX_ERROR;
}

Expand Down Expand Up @@ -222,10 +222,10 @@ ngx_wasm_conf_add_op_call(ngx_conf_t *cf, ngx_wasm_ops_engine_t *ops_engine,
{
ngx_wasm_op_t *op;
ngx_wasm_phase_t *phase = ops_engine->subsystem->phases;
ngx_str_t *phase_name, *mod_name, *func_name;
ngx_str_t *phase_name, *arg_name, *func_name;

phase_name = &value[1];
mod_name = &value[2];
arg_name = &value[2];
func_name = &value[3];

if (phase_name->len == 0) {
Expand Down Expand Up @@ -272,7 +272,7 @@ ngx_wasm_conf_add_op_call(ngx_conf_t *cf, ngx_wasm_ops_engine_t *ops_engine,

op->conf.call.func_name = *func_name;

if (ngx_wasm_op_add_helper(cf, ops_engine, op, mod_name) != NGX_OK) {
if (ngx_wasm_op_add_helper(cf, ops_engine, op, arg_name) != NGX_OK) {
return NULL;
}

Expand All @@ -282,13 +282,18 @@ ngx_wasm_conf_add_op_call(ngx_conf_t *cf, ngx_wasm_ops_engine_t *ops_engine,

ngx_wasm_op_t *
ngx_wasm_conf_add_op_proxy_wasm(ngx_conf_t *cf,
ngx_wasm_ops_engine_t *ops_engine, ngx_str_t *value,
ngx_wasm_ops_engine_t *ops_engine, ngx_uint_t nvalues, ngx_str_t *value,
ngx_proxy_wasm_t *pwmodule)
{
ngx_wasm_op_t *op;
ngx_str_t *mod_name;
u_char *p;
ngx_wasm_op_t *op = NULL;
ngx_str_t *arg_name, *arg_config = NULL, *mod_config;

mod_name = &value[1];
arg_name = &value[1];

if (nvalues > 2) {
arg_config = &value[2];
}

op = ngx_pcalloc(cf->pool, sizeof(ngx_wasm_op_t));
if (op == NULL) {
Expand All @@ -302,14 +307,35 @@ ngx_wasm_conf_add_op_proxy_wasm(ngx_conf_t *cf,
| (1 << NGX_HTTP_WASM_HEADER_FILTER_PHASE)
| (1 << NGX_HTTP_LOG_PHASE);

if (ngx_wasm_op_add_helper(cf, ops_engine, op, mod_name) != NGX_OK) {
return NULL;
if (ngx_wasm_op_add_helper(cf, ops_engine, op, arg_name) != NGX_OK) {
goto error;
}

op->conf.proxy_wasm.pwmodule = pwmodule;
op->conf.proxy_wasm.pwmodule->module = op->module;

if (arg_config) {
mod_config = &op->conf.proxy_wasm.pwmodule->config;

mod_config->len = arg_config->len;
mod_config->data = ngx_pnalloc(pwmodule->pool, mod_config->len + 1);
if (mod_config->data == NULL) {
goto error;
}

p = ngx_copy(mod_config->data, arg_config->data, mod_config->len);
*p = '\0';
}

return op;

error:

if (op) {
ngx_pfree(cf->pool, op);
}

return NULL;
}


Expand Down
2 changes: 1 addition & 1 deletion src/wasm/ngx_wasm_ops.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ ngx_wasm_op_t *ngx_wasm_conf_add_op_call(ngx_conf_t *cf,
ngx_wasm_ops_engine_t *ops_engine, ngx_wavm_host_def_t *host,
ngx_str_t *value);
ngx_wasm_op_t *ngx_wasm_conf_add_op_proxy_wasm(ngx_conf_t *cf,
ngx_wasm_ops_engine_t *ops_engine, ngx_str_t *value,
ngx_wasm_ops_engine_t *ops_engine, ngx_uint_t nvalues, ngx_str_t *value,
ngx_proxy_wasm_t *pwmodule);

ngx_int_t ngx_wasm_ops_resume(ngx_wasm_op_ctx_t *ctx, ngx_uint_t phaseidx);
Expand Down
2 changes: 1 addition & 1 deletion t/03-proxy_wasm/001-proxy_wasm_directive.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ qr/\[emerg\] .*? "proxy_wasm" directive is specified but config has no "wasm" se
=== TEST 2: proxy_wasm directive - invalid number of arguments
--- config
location /t {
proxy_wasm a foo;
proxy_wasm a foo bar;
return 200;
}
--- error_log eval
Expand Down
78 changes: 78 additions & 0 deletions t/03-proxy_wasm/002-on_phases.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:

use strict;
use lib '.';
use t::TestWasm;

skip_valgrind();

#repeat_each(2);

plan tests => repeat_each() * (blocks() * 7);

run_tests();

__DATA__

=== TEST 1: proxy_wasm - on_vm_start
--- main_config
wasm {
module on_phases $TEST_NGINX_CRATES_DIR/on_phases.wasm;
}
--- config
location /t {
proxy_wasm on_phases;
return 200;
}
--- response_body
--- error_log eval
qr/\[info\] .*? \[wasm\] #0 on_vm_start, config_size: 0/
--- no_error_log
[error]
[crit]
[emerg]
[alert]



=== TEST 2: proxy_wasm - on_configure without configuration
--- main_config
wasm {
module on_phases $TEST_NGINX_CRATES_DIR/on_phases.wasm;
}
--- config
location /t {
proxy_wasm on_phases;
return 200;
}
--- response_body
--- error_log eval
qr/\[info\] .*? \[wasm\] #0 on_configure, config_size: 0/
--- no_error_log
[error]
[crit]
[emerg]
[alert]



=== TEST 3: proxy_wasm - on_configure with configuration
--- main_config
wasm {
module on_phases $TEST_NGINX_CRATES_DIR/on_phases.wasm;
}
--- config
location /t {
proxy_wasm on_phases 'key=value foo=bar';
return 200;
}
--- response_body
--- error_log eval
[
qr/\[info\] .*? \[wasm\] #0 on_configure, config_size: 17/,
qr/\[info\] .*? \[wasm\] #0 config: \{"foo": "bar", "key": "value"\}/
]
--- no_error_log
[error]
[crit]
[emerg]
File renamed without changes.