-
Notifications
You must be signed in to change notification settings - Fork 8
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) foreign function support #626
base: main
Are you sure you want to change the base?
Conversation
d4b4267
to
99b8e75
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #626 +/- ##
===================================================
- Coverage 90.84180% 90.45578% -0.38603%
===================================================
Files 52 53 +1
Lines 11214 11431 +217
===================================================
+ Hits 10187 10340 +153
- Misses 1027 1091 +64
... and 1 file with indirect coverage changes
Flags with carried forward coverage won't be shown. Click here to find out more.
|
99b8e75
to
5bb49f5
Compare
7aa55fb
to
08ec198
Compare
08ec198
to
2ecb389
Compare
#ifdef NGX_WASM_HTTP | ||
ngx_http_proxy_wasm_dispatch_t *call; /* swap pointer for host functions */ | ||
ngx_http_proxy_wasm_dispatch_t *call; /* swap pointer for host functions */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dispatch_call
#endif | ||
ngx_queue_t calls; | ||
ngx_queue_t calls; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dispatch_calls
#endif | ||
ngx_queue_t calls; | ||
ngx_queue_t calls; | ||
ngx_proxy_wasm_foreign_cb_t *fcallback; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreign_call
ngx_queue_t calls; | ||
ngx_queue_t calls; | ||
ngx_proxy_wasm_foreign_cb_t *fcallback; | ||
ngx_queue_t fcallbacks; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreign_calls
@@ -415,6 +424,9 @@ ngx_proxy_wasm_err_e ngx_proxy_wasm_run_step(ngx_proxy_wasm_exec_t *pwexec, | |||
ngx_proxy_wasm_step_e step); | |||
ngx_uint_t ngx_proxy_wasm_dispatch_calls_total(ngx_proxy_wasm_exec_t *pwexec); | |||
void ngx_proxy_wasm_dispatch_calls_cancel(ngx_proxy_wasm_exec_t *pwexec); | |||
ngx_uint_t ngx_proxy_wasm_foreign_callbacks_total( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foreign_calls_total
...
void | ||
ngx_proxy_wasm_foreign_callback_destroy(ngx_proxy_wasm_foreign_cb_t *cb) | ||
{ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_wa_assert(pwexec); | ||
ngx_wa_assert(size); | ||
|
||
cl = ngx_alloc_chain_link(pwexec->pool); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_wasm_chain_get_free_buf
return NGX_ERROR; | ||
} | ||
|
||
/* TODO: if size exceeds a threshold, split allocation into N buffers */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_wa_assert(len < NGX_MAX_ERRLEN)
} | ||
|
||
|
||
ngx_int_t |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void
ret_size = NGX_WAVM_HOST_LIFT(instance, args[5].of.i32, int32_t); | ||
|
||
if (ngx_str_eq(fname.data, fname.len, "resolve_lua", -1)) { | ||
return ngx_proxy_wasm_hfuncs_resolve_lua(instance, rctx, &fargs, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_proxy_wasm_foreign_call_lua_resolve (?) -> foreign_call.c
|
||
pwexec = ngx_proxy_wasm_instance2pwexec(instance); | ||
|
||
cb = ngx_proxy_wasm_foreign_callback_alloc(pwexec); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(inlined)
b = cb->args_out->buf; | ||
s = *b->start; /* first byte is the length of the resolved address */ | ||
|
||
p = ngx_proxy_wasm_alloc(pwexec, s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
catch bad alloc
|
||
|
||
ngx_int_t | ||
ngx_http_wasm_create_fake_req_ctx(ngx_proxy_wasm_exec_t *pwexec, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
create_fake_rctx
ngx_queue_t q; | ||
ngx_proxy_wasm_exec_t *pwexec; | ||
#if (NGX_WASM_HTTP) | ||
ngx_http_wasm_req_ctx_t *rctx; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_http_wasm_req_ctx_t *rctx; |
|
||
break; | ||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
*(p++) = sizeof(struct in6_addr); | ||
p = ngx_cpymem(p, &sin6->sin6_addr, sizeof(struct in6_addr)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
p++; | ||
goto not_found; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_wa_assert(rslv_ctx->naddrs == 1) (perhaps?)
ngx_wasm_socket_tcp_t *sock; | ||
ngx_proxy_wasm_exec_t *pwexec; | ||
ngx_proxy_wasm_foreign_cb_t *cb; | ||
ngx_wavm_ptr_t p = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ngx_wavm_ptr_t p = 0; | |
ngx_wavm_ptr_t p; |
cb->rctx = rctx; | ||
} | ||
|
||
sock = ngx_palloc(pwexec->pool, sizeof(ngx_wasm_socket_tcp_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sock = ngx_palloc(pwexec->pool, sizeof(ngx_wasm_socket_tcp_t)); | |
sock = ngx_pcalloc(pwexec->pool, sizeof(ngx_wasm_socket_tcp_t)); |
{ | ||
ngx_proxy_wasm_foreign_cb_t *cb; | ||
|
||
cb = ngx_palloc(pwexec->pool, sizeof(ngx_proxy_wasm_foreign_cb_t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cb = ngx_palloc(pwexec->pool, sizeof(ngx_proxy_wasm_foreign_cb_t)); | |
cb = ngx_pcalloc(pwexec->pool, sizeof(ngx_proxy_wasm_foreign_cb_t)); |
- inlined
goto error; | ||
} | ||
|
||
cb->fcode = NGX_PROXY_WASM_FOREIGN_RESOLVE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's try with a function pointer when all those routiens move to "foreign_call.c"
b->last = ngx_cpymem(b->last, data->data, data->len); | ||
} | ||
|
||
/* TODO: data->len > b_size */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move up to the assert
Foreign Function Support
The Proxy-Wasm spec defines the function
proxy_call_foreign_function
and the callbackproxy_on_foreign_function
that filter developers can use to invoke host specific functions, a.k.a. foreign functions, and receive callbacks.A foreign function invoked with
proxy_call_foreign_function
may return its value immediately -- as part of the returned value of theproxy_call_foreign_function
call; or it can return it later, writing it to theFOREIGN_FUNCTION_ARGUMENTS
buffer, and invokingproxy_on_foreign_function
with an id identifying the function initially called.This PR adds support for the mechanism described above; and although the spec doesn't restrict when foreign functions can be called, in
ngx_wasm_module
they cannot be invoked fromproxy_on_configure
orproxy_on_vm_start
.DNS resolution
This PR also exposes the Lua DNS resolver through the
resolve_lua
foreign function.This function expects the name to be resolved as its single argument. If the name can be resolved without performing any IO, the resolved address is put in the returned value of
proxy_call_foreign_function
.If the resolver needs to forward the resolution request to a DNS server, the resolved address and the name itself are written to the
FOREIGN_FUNCTION_ARGUMENTS
buffer and theproxy_on_foreign_function
callback is invoked withfunction_id
0, as soon as the address is returned from the server.TODO
on_tick
support