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

proxy wasm 0.3.0: bring stop iteration support back #424

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 2 additions & 1 deletion include/proxy-wasm/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "include/proxy-wasm/sdk.h"
#include "include/proxy-wasm/context_interface.h"
#include "include/proxy-wasm/wasm_vm.h"

namespace proxy_wasm {

Expand Down Expand Up @@ -399,7 +400,7 @@ class ContextBase : public RootInterface,

private:
// helper functions
FilterHeadersStatus convertVmCallResultToFilterHeadersStatus(uint64_t result);
FilterHeadersStatus convertVmCallResultToFilterHeadersStatus(uint64_t result, AbiVersion version);
FilterDataStatus convertVmCallResultToFilterDataStatus(uint64_t result);
FilterTrailersStatus convertVmCallResultToFilterTrailersStatus(uint64_t result);
FilterMetadataStatus convertVmCallResultToFilterMetadataStatus(uint64_t result);
Expand Down
1 change: 1 addition & 0 deletions include/proxy-wasm/null_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct NullPluginRegistry {
void (*proxy_abi_version_0_1_0_)() = nullptr;
void (*proxy_abi_version_0_2_0_)() = nullptr;
void (*proxy_abi_version_0_2_1_)() = nullptr;
void (*proxy_abi_version_0_3_0_)() = nullptr;
void (*proxy_on_log_)(uint32_t context_id) = nullptr;
uint32_t (*proxy_validate_configuration_)(uint32_t root_context_id,
uint32_t plugin_configuration_size) = nullptr;
Expand Down
4 changes: 2 additions & 2 deletions include/proxy-wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
WasmCallVoid<2> on_upstream_connection_close_;

WasmCallWord<2> on_request_headers_abi_01_;
WasmCallWord<3> on_request_headers_abi_02_;
WasmCallWord<3> on_request_headers_;
WasmCallWord<3> on_request_body_;
WasmCallWord<2> on_request_trailers_;
WasmCallWord<2> on_request_metadata_;

WasmCallWord<2> on_response_headers_abi_01_;
WasmCallWord<3> on_response_headers_abi_02_;
WasmCallWord<3> on_response_headers_;
WasmCallWord<3> on_response_body_;
WasmCallWord<2> on_response_trailers_;
WasmCallWord<2> on_response_metadata_;
Expand Down
8 changes: 7 additions & 1 deletion include/proxy-wasm/wasm_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ enum class Cloneable {
InstantiatedModule // VMs can be cloned from an instantiated module.
};

enum class AbiVersion { ProxyWasm_0_1_0, ProxyWasm_0_2_0, ProxyWasm_0_2_1, Unknown };
enum class AbiVersion {
ProxyWasm_0_1_0,
ProxyWasm_0_2_0,
ProxyWasm_0_2_1,
ProxyWasm_0_3_0,
Unknown,
};

class NullPlugin;

Expand Down
52 changes: 35 additions & 17 deletions src/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <cassert>
#include <deque>
#include <map>
#include <memory>
Expand Down Expand Up @@ -312,21 +313,26 @@ void ContextBase::onUpstreamConnectionClose(CloseType close_type) {
}
}

// Empty headers/trailers have zero size.
template <typename P> static uint32_t headerSize(const P &p) { return p ? p->size() : 0; }

FilterHeadersStatus ContextBase::onRequestHeaders(uint32_t headers, bool end_of_stream) {
CHECK_FAIL_HTTP(FilterHeadersStatus::Continue, FilterHeadersStatus::StopAllIterationAndWatermark);
if (!wasm_->on_request_headers_abi_01_ && !wasm_->on_request_headers_abi_02_) {
if (!wasm_->on_request_headers_abi_01_ && !wasm_->on_request_headers_) {
return FilterHeadersStatus::Continue;
}
DeferAfterCallActions actions(this);
const auto result = wasm_->on_request_headers_abi_01_
? wasm_->on_request_headers_abi_01_(this, id_, headers)
: wasm_->on_request_headers_abi_02_(this, id_, headers,
static_cast<uint32_t>(end_of_stream));

AbiVersion wasm_abi_version = wasm_->abi_version_;
uint64_t result{};

if (wasm_->abi_version_ == AbiVersion::ProxyWasm_0_1_0) {
// For ProxyWasm_0_1_0.
result = wasm_->on_request_headers_abi_01_(this, id_, headers);
} else {
// For ProxyWasm_0_2_0, ProxyWasm_0_2_1, or ProxyWasm_0_3_0.
result = wasm_->on_request_headers_(this, id_, headers, static_cast<uint32_t>(end_of_stream));
}

CHECK_FAIL_HTTP(FilterHeadersStatus::Continue, FilterHeadersStatus::StopAllIterationAndWatermark);
return convertVmCallResultToFilterHeadersStatus(result);
return convertVmCallResultToFilterHeadersStatus(result, wasm_abi_version);
}

FilterDataStatus ContextBase::onRequestBody(uint32_t body_length, bool end_of_stream) {
Expand Down Expand Up @@ -365,16 +371,24 @@ FilterMetadataStatus ContextBase::onRequestMetadata(uint32_t elements) {

FilterHeadersStatus ContextBase::onResponseHeaders(uint32_t headers, bool end_of_stream) {
CHECK_FAIL_HTTP(FilterHeadersStatus::Continue, FilterHeadersStatus::StopAllIterationAndWatermark);
if (!wasm_->on_response_headers_abi_01_ && !wasm_->on_response_headers_abi_02_) {
if (!wasm_->on_response_headers_abi_01_ && !wasm_->on_response_headers_) {
return FilterHeadersStatus::Continue;
}
DeferAfterCallActions actions(this);
const auto result = wasm_->on_response_headers_abi_01_
? wasm_->on_response_headers_abi_01_(this, id_, headers)
: wasm_->on_response_headers_abi_02_(
this, id_, headers, static_cast<uint32_t>(end_of_stream));

AbiVersion wasm_abi_version = wasm_->abi_version_;
uint64_t result{};

if (wasm_->abi_version_ == AbiVersion::ProxyWasm_0_1_0) {
// For ProxyWasm_0_1_0.
result = wasm_->on_response_headers_abi_01_(this, id_, headers);
} else {
// For ProxyWasm_0_2_0, ProxyWasm_0_2_1, or ProxyWasm_0_3_0.
result = wasm_->on_response_headers_(this, id_, headers, static_cast<uint32_t>(end_of_stream));
}

CHECK_FAIL_HTTP(FilterHeadersStatus::Continue, FilterHeadersStatus::StopAllIterationAndWatermark);
return convertVmCallResultToFilterHeadersStatus(result);
return convertVmCallResultToFilterHeadersStatus(result, wasm_abi_version);
}

FilterDataStatus ContextBase::onResponseBody(uint32_t body_length, bool end_of_stream) {
Expand Down Expand Up @@ -488,12 +502,16 @@ WasmResult ContextBase::setTimerPeriod(std::chrono::milliseconds period,
return WasmResult::Ok;
}

FilterHeadersStatus ContextBase::convertVmCallResultToFilterHeadersStatus(uint64_t result) {
FilterHeadersStatus ContextBase::convertVmCallResultToFilterHeadersStatus(uint64_t result,
AbiVersion abi_version) {
if (wasm()->isNextIterationStopped() ||
result > static_cast<uint64_t>(FilterHeadersStatus::StopAllIterationAndWatermark)) {
return FilterHeadersStatus::StopAllIterationAndWatermark;
}
if (result == static_cast<uint64_t>(FilterHeadersStatus::StopIteration)) {

// Only Proxy-WASM 0.2.1 and earlier convert StopIteration to StopAllIterationAndWatermark.
if (abi_version < AbiVersion::ProxyWasm_0_3_0 &&
result == static_cast<uint64_t>(FilterHeadersStatus::StopIteration)) {
// Always convert StopIteration (pause processing headers, but continue processing body)
// to StopAllIterationAndWatermark (pause all processing), since the former breaks all
// assumptions about HTTP processing.
Expand Down
15 changes: 10 additions & 5 deletions src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "include/proxy-wasm/bytecode_util.h"
#include "include/proxy-wasm/signature_util.h"
#include "include/proxy-wasm/vm_id_handle.h"
#include "include/proxy-wasm/wasm_vm.h"
#include "src/hash.h"

namespace proxy_wasm {
Expand Down Expand Up @@ -139,14 +140,17 @@ void WasmBase::registerCallbacks() {
FOR_ALL_HOST_FUNCTIONS(_REGISTER_PROXY);

if (abiVersion() == AbiVersion::ProxyWasm_0_1_0) {
// For ProxyWasm_0_1_0.
_REGISTER_PROXY(get_configuration);
_REGISTER_PROXY(continue_request);
_REGISTER_PROXY(continue_response);
_REGISTER_PROXY(clear_route_cache);
} else if (abiVersion() == AbiVersion::ProxyWasm_0_2_0) {
// For ProxyWasm_0_2_0.
_REGISTER_PROXY(continue_stream);
_REGISTER_PROXY(close_stream);
} else if (abiVersion() == AbiVersion::ProxyWasm_0_2_1) {
} else {
// For ProxyWasm_0_2_1 or ProxyWasm_0_3_0.
_REGISTER_PROXY(continue_stream);
_REGISTER_PROXY(close_stream);
_REGISTER_PROXY(get_log_level);
Expand Down Expand Up @@ -193,12 +197,13 @@ void WasmBase::getFunctions() {
FOR_ALL_MODULE_FUNCTIONS(_GET_PROXY);

if (abiVersion() == AbiVersion::ProxyWasm_0_1_0) {
// For ProxyWasm_0_1_0.
_GET_PROXY_ABI(on_request_headers, _abi_01);
_GET_PROXY_ABI(on_response_headers, _abi_01);
} else if (abiVersion() == AbiVersion::ProxyWasm_0_2_0 ||
abiVersion() == AbiVersion::ProxyWasm_0_2_1) {
_GET_PROXY_ABI(on_request_headers, _abi_02);
_GET_PROXY_ABI(on_response_headers, _abi_02);
} else {
// For ProxyWasm_0_2_0, ProxyWasm_0_2_1, or ProxyWasm_0_3_0.
_GET_PROXY(on_request_headers);
_GET_PROXY(on_response_headers);
_GET_PROXY(on_foreign_function);
}
#undef _GET_PROXY_ABI
Expand Down
Loading