Skip to content

feat(proxy-wasm) foreign function support #626

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

Closed
wants to merge 7 commits into from

Conversation

casimiro
Copy link
Contributor

@casimiro casimiro commented Nov 18, 2024

Foreign Function Support

The Proxy-Wasm spec defines the function proxy_call_foreign_function and the callback proxy_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 the proxy_call_foreign_function call; or it can return it later, writing it to the FOREIGN_FUNCTION_ARGUMENTS buffer, and invoking proxy_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 from proxy_on_configure or proxy_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 the proxy_on_foreign_function callback is invoked with function_id 0, as soon as the address is returned from the server.

TODO

  • on_tick support
  • update docs
  • general review

@casimiro casimiro added the pr/work-in-progress PR: Work in progress label Nov 18, 2024
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 5 times, most recently from d4b4267 to 99b8e75 Compare November 19, 2024 21:58
Copy link

codecov bot commented Nov 19, 2024

Codecov Report

Attention: Patch coverage is 82.97872% with 48 lines in your changes missing coverage. Please review.

Project coverage is 90.71034%. Comparing base (004ae21) to head (a03e4b4).
Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
...rc/common/proxy_wasm/ngx_proxy_wasm_foreign_call.c 76.00000% 36 Missing ⚠️
src/common/proxy_wasm/ngx_proxy_wasm_host.c 87.50000% 3 Missing ⚠️
src/http/ngx_http_wasm_util.c 80.00000% 3 Missing ⚠️
src/http/proxy_wasm/ngx_http_proxy_wasm.c 84.21053% 3 Missing ⚠️
src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c 93.75000% 2 Missing ⚠️
src/common/proxy_wasm/ngx_proxy_wasm.c 96.15385% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@                 Coverage Diff                 @@
##                main        #626         +/-   ##
===================================================
- Coverage   90.82100%   90.71034%   -0.11066%     
===================================================
  Files             53          54          +1     
  Lines          11352       11572        +220     
  Branches        1691        1713         +22     
===================================================
+ Hits           10310       10497        +187     
- Misses          1036        1069         +33     
  Partials           6           6                 
Files with missing lines Coverage Δ
src/common/proxy_wasm/ngx_proxy_wasm.h 92.30769% <ø> (ø)
src/common/proxy_wasm/ngx_proxy_wasm_maps.c 94.01042% <100.00000%> (ø)
src/common/proxy_wasm/ngx_proxy_wasm_util.c 94.25287% <100.00000%> (ø)
src/common/proxy_wasm/ngx_proxy_wasm.c 92.69603% <96.15385%> (-0.01345%) ⬇️
src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c 91.74528% <93.75000%> (+0.31671%) ⬆️
src/common/proxy_wasm/ngx_proxy_wasm_host.c 93.61233% <87.50000%> (-0.18699%) ⬇️
src/http/ngx_http_wasm_util.c 86.95652% <80.00000%> (-0.27752%) ⬇️
src/http/proxy_wasm/ngx_http_proxy_wasm.c 92.99065% <84.21053%> (-0.88690%) ⬇️
...rc/common/proxy_wasm/ngx_proxy_wasm_foreign_call.c 76.00000% <76.00000%> (ø)

... and 2 files with indirect coverage changes

Flag Coverage Δ
unit 90.65053% <90.36145%> (+0.08790%) ⬆️
valgrind 81.00649% <29.41176%> (-1.47085%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@casimiro casimiro force-pushed the feat/wasm-foreign-function branch from 99b8e75 to 5bb49f5 Compare November 20, 2024 12:01
@casimiro casimiro changed the title feat(proxy-wasm) foreign function feat(proxy-wasm) foreign function support Nov 20, 2024
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 5 times, most recently from 08ec198 to 2ecb389 Compare November 21, 2024 13:28
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch from b661016 to 0d11869 Compare November 27, 2024 17:15
@casimiro
Copy link
Contributor Author

Codecov is playing games with me. It's reporting lines that are definitely covered by tests as uncovered.

@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 2 times, most recently from 90edd6e to b8d786f Compare December 2, 2024 22:01
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 6 times, most recently from 7eb64b5 to d217d1a Compare December 10, 2024 19:09
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 2 times, most recently from 8be992b to 5df0e17 Compare January 3, 2025 12:39
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch 4 times, most recently from 1ad9711 to b8f7332 Compare January 23, 2025 19:39
Making struct member names explict to distinguish them from the upcoming
Proxy-Wasm foreign call support.
Also adds `resolve_lua` foreign function, which allows filter developers
to resolve names using the Lua DNS resolver.
…aration

Handles the case when the expression defining a variable being declared
is a function call receiving arguments that don't fit in the same line,
e.g.:

ngx_proxy_wasm_exec_t          *pwexec = ngx_proxy_wasm_instance2pwexec(
                                             instance);
The trap message describing the lua_resolver being called in unsupported
context exceeds the current limit.
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch from b8f7332 to 2bd615c Compare January 23, 2025 19:40
@casimiro casimiro force-pushed the feat/wasm-foreign-function branch from 2bd615c to a03e4b4 Compare January 23, 2025 19:43
@casimiro casimiro removed the pr/work-in-progress PR: Work in progress label Jan 23, 2025
@thibaultcha thibaultcha added the pr/merge-in-progress PR: Merge in progress (do not push) label Jan 23, 2025
@casimiro casimiro closed this Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/merge-in-progress PR: Merge in progress (do not push)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants