Skip to content

Commit

Permalink
use filter state for storing shared filter state (envoyproxy#179)
Browse files Browse the repository at this point in the history
* use filter state

Signed-off-by: Kuat Yessenov <kuat@google.com>
  • Loading branch information
kyessenov authored and jplevyak committed Sep 13, 2019
1 parent 0e5c253 commit 3530007
Show file tree
Hide file tree
Showing 24 changed files with 187 additions and 488 deletions.
115 changes: 14 additions & 101 deletions api/wasm/cpp/proxy_wasm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -501,84 +501,6 @@ inline bool ContextBase::isProactivelyCachable(MetadataType type) {
}
}

// Expressions

#define PROXY_EXPRESSION_GET_STRING(_expression, _string_ptr) \
do { \
const char* _value_ptr = nullptr; \
size_t _value_size = 0; \
auto _result = static_cast<WasmResult>(proxy_getMetadata(MetadataType::Expression, \
_expression, sizeof(_expression) - 1, \
&_value_ptr, &_value_size)); \
if (_result != WasmResult::Ok) { \
_string_ptr->clear(); \
return _result; \
} \
_string_ptr->assign(_value_ptr, _value_size); \
return WasmResult::Ok; \
} while (0)

#define PROXY_EXPRESSION_GET(_expression, _data_ptr) \
do { \
const char* _value_ptr = nullptr; \
size_t _value_size = 0; \
auto _result = static_cast<WasmResult>(proxy_getMetadata(MetadataType::Expression, \
_expression, sizeof(_expression) - 1, \
&_value_ptr, &_value_size)); \
if (_result != WasmResult::Ok) { \
return _result; \
} \
if (_value_size != sizeof(*_data_ptr)) { \
return WasmResult::ResultMismatch; \
} \
memcpy(_data_ptr, _value_ptr, sizeof(*_data_ptr)); \
return WasmResult::Ok; \
} while (0)

inline WasmResult getRequestProtocol(std::string* protocol_ptr) {
PROXY_EXPRESSION_GET_STRING("request.protocol", protocol_ptr);
}

inline WasmResult getResponseProtocol(std::string* protocol_ptr) {
PROXY_EXPRESSION_GET_STRING("response.protocol", protocol_ptr);
}

inline WasmResult getRequestDestinationPort(uint32_t* port) {
PROXY_EXPRESSION_GET("request.destination_port", port);
}

inline WasmResult getResponseDestinationPort(uint32_t* port) {
PROXY_EXPRESSION_GET("response.destination_port", port);
}

inline WasmResult getRequestResponseCode(uint32_t* response_code) {
PROXY_EXPRESSION_GET("request.response_code", response_code);
}

inline WasmResult getResponseResponseCode(uint32_t* response_code) {
PROXY_EXPRESSION_GET("response.response_code", response_code);
}

inline WasmResult getRequestTlsVersion(std::string* tls_version_ptr) {
PROXY_EXPRESSION_GET_STRING("request.tls_version", tls_version_ptr);
}

inline WasmResult getResponseTlsVersion(std::string* tls_version_ptr) {
PROXY_EXPRESSION_GET_STRING("response.tls_version", tls_version_ptr);
}

inline WasmResult getRequestPeerCertificatePresented(bool* peer_certificate_presented) {
PROXY_EXPRESSION_GET("request.peer_certificate_presented", peer_certificate_presented);
}

inline WasmResult getResponsePeerCertificatePresented(bool* peer_certificate_presented) {
PROXY_EXPRESSION_GET("response.peer_certificate_presented", peer_certificate_presented);
}

inline WasmResult getPluginDirection(PluginDirection* direction_ptr) {
PROXY_EXPRESSION_GET("plugin.direction", reinterpret_cast<uint32_t*>(direction_ptr));
}

// Generic selector
inline Optional<WasmDataPtr> getSelectorExpression(std::initializer_list<StringView> parts) {
size_t size = 0;
Expand All @@ -605,6 +527,15 @@ inline Optional<WasmDataPtr> getSelectorExpression(std::initializer_list<StringV
return std::make_unique<WasmData>(value_ptr, value_size);
}

inline WasmResult getRequestProtocol(std::string *result) {
auto value = getSelectorExpression({"request_protocol"});
if (value.has_value()) {
result->assign(value.value()->data(), value.value()->size());
return WasmResult::Ok;
}
return WasmResult::NotFound;
}

// Metadata
inline WasmResult getMetadata(MetadataType type, StringView key, WasmDataPtr* wasm_data) {
const char* value_ptr = nullptr;
Expand Down Expand Up @@ -648,25 +579,18 @@ inline WasmResult getMetadataStringValue(MetadataType type, StringView key,
return result;
}

inline WasmResult setMetadata(MetadataType type, StringView key, StringView value) {
return static_cast<WasmResult>(
proxy_setMetadata(type, key.data(), key.size(), value.data(), value.size()));
}

inline WasmResult setMetadataValue(MetadataType type, StringView key,
const google::protobuf::Value& value) {
inline WasmResult setFilterStateValue(StringView key, const google::protobuf::Value& value) {
std::string output;
if (!value.SerializeToString(&output)) {
return WasmResult::SerializationFailure;
}
return static_cast<WasmResult>(
proxy_setMetadata(type, key.data(), key.size(), output.data(), output.size()));
return static_cast<WasmResult>(proxy_setState(key.data(), key.size(), output.data(), output.size()));
}

inline WasmResult setMetadataStringValue(MetadataType type, StringView key, StringView s) {
inline WasmResult setFilterStateStringValue(StringView key, StringView s) {
google::protobuf::Value value;
value.set_string_value(s.data(), s.size());
return setMetadataValue(type, key, value);
return setFilterStateValue(key, value);
}

inline WasmResult getMetadataValuePairs(MetadataType type, WasmDataPtr* pairs_ptr) {
Expand Down Expand Up @@ -698,18 +622,7 @@ inline WasmResult getMetadataStruct(MetadataType type, StringView name,
return WasmResult::Ok;
}

inline WasmResult setMetadataStruct(MetadataType type, StringView name,
const google::protobuf::Struct& s) {
std::string output;
if (!s.SerializeToString(&output)) {
return WasmResult::SerializationFailure;
}
return static_cast<WasmResult>(
proxy_setMetadataStruct(type, name.data(), name.size(), output.data(), output.size()));
}

inline WasmResult ContextBase::metadataValue(MetadataType type, StringView key,
google::protobuf::Value* value_ptr) {
inline WasmResult ContextBase::metadataValue(MetadataType type, StringView key, google::protobuf::Value* value_ptr) {
if (isRootCachable(type)) {
if (auto context = asContext()) {
return context->root()->metadataValue(type, key, value_ptr);
Expand Down
6 changes: 2 additions & 4 deletions api/wasm/cpp/proxy_wasm_externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,12 @@ extern "C" WasmResult proxy_getCurrentTimeNanoseconds(uint64_t* nanoseconds);
// Metadata
extern "C" WasmResult proxy_getMetadata(MetadataType type, const char* key_ptr, size_t key_size,
const char** value_ptr_ptr, size_t* value_size_ptr);
extern "C" WasmResult proxy_setMetadata(MetadataType type, const char* key_ptr, size_t key_size,
const char* value_ptr, size_t value_size);
extern "C" WasmResult proxy_getMetadataPairs(MetadataType type, const char** value_ptr,
size_t* value_size);
extern "C" WasmResult proxy_getMetadataStruct(MetadataType type, const char* name_ptr, size_t name_size,
const char** value_ptr_ptr, size_t* value_size_ptr);
extern "C" WasmResult proxy_setMetadataStruct(MetadataType type, const char* name_ptr, size_t name_size,
const char* value_ptr, size_t value_size);
extern "C" WasmResult proxy_setState(const char* key_ptr, size_t key_size,
const char* value_ptr, size_t value_size);

// Generic selector
extern "C" WasmResult proxy_getSelectorExpression(const char* path_ptr, size_t path_size,
Expand Down
4 changes: 2 additions & 2 deletions api/wasm/cpp/proxy_wasm_intrinsics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ mergeInto(LibraryManager.library, {
proxy_getDestinationPort: function () {},
proxy_getResponseCode: function () {},
proxy_getMetadata: function () {},
proxy_setMetadata: function () {},
proxy_getMetadataPairs: function () {},
proxy_getMetadataStruct: function () {},
proxy_setMetadataStruct: function () {},
proxy_setState: function () {},
proxy_getSelectorExpression: function () {},
proxy_continueRequest: function () {},
proxy_continueResponse: function () {},
proxy_clearRouteCache: function () {},
Expand Down
6 changes: 0 additions & 6 deletions api/wasm/cpp/proxy_wasm_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,3 @@ enum class HeaderMapType : int32_t {
GrpcReceiveTrailingMetadata = 6, // Immutable
MAX = 6,
};

enum class PluginDirection : int32_t {
Unspecified = 0,
Inbound = 1,
Outbound = 2,
};
3 changes: 1 addition & 2 deletions source/extensions/access_loggers/wasm/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ WasmAccessLogFactory::createAccessLogInstance(const Protobuf::Message& proto_con
// individual threads.
auto base_wasm = Common::Wasm::createWasm(
vm_id, config.vm_config(), root_id, context.clusterManager(), context.dispatcher(),
context.api(), context.scope(),
Common::Wasm::pluginDirectionFromTrafficDirection(context.direction()), context.localInfo(),
context.api(), context.scope(), context.direction(), context.localInfo(),
&context.listenerMetadata(), nullptr /* owned_scope */);
// NB: the Slot set() call doesn't complete inline, so all arguments must outlive this call.
tls_slot->set([base_wasm, root_id, configuration](Event::Dispatcher& dispatcher) {
Expand Down
1 change: 1 addition & 0 deletions source/extensions/common/wasm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ envoy_cc_library(
"@com_google_cel_cpp//eval/eval:field_access",
"@com_google_cel_cpp//eval/eval:field_backed_list_impl",
"@com_google_cel_cpp//eval/eval:field_backed_map_impl",
"@com_google_cel_cpp//eval/public:cel_value",
],
)
2 changes: 2 additions & 0 deletions source/extensions/common/wasm/null/sample_plugin/plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ FilterDataStatus PluginContext::onRequestBody(size_t body_buffer_length, bool /*
}

void PluginContext::onLog() {
setFilterStateStringValue("wasm_state", "wasm_value");
auto path = getRequestHeader(":path");
if (path->view() == "/test_context") {
logWarn("request.path: " + getSelectorExpression({"request", "path"}).value()->toString());
Expand All @@ -64,6 +65,7 @@ void PluginContext::onLog() {
int64_t code = absl::bit_cast<int64_t>(buf);
logWarn("response.code: " + absl::StrCat(code));
}
logWarn("state: " + getSelectorExpression({"filter_state", "wasm_state"}).value()->toString());
} else {
logWarn("onLog " + std::to_string(id()) + " " + std::string(path->view()));
}
Expand Down
13 changes: 4 additions & 9 deletions source/extensions/common/wasm/null/wasm_api_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ inline WasmResult proxy_getMetadata(MetadataType type, const char* key_ptr, size
return wordToWasmResult(getMetadataHandler(current_context_, WS(type), WR(key_ptr), WS(key_size),
WR(value_ptr_ptr), WR(value_size_ptr)));
}
inline WasmResult proxy_setMetadata(MetadataType type, const char* key_ptr, size_t key_size,
const char* value_ptr, size_t value_size) {
return wordToWasmResult(setMetadataHandler(current_context_, WS(type), WR(key_ptr), WS(key_size),
WR(value_ptr), WS(value_size)));
inline WasmResult proxy_setState(const char* key_ptr, size_t key_size, const char* value_ptr,
size_t value_size) {
return wordToWasmResult(
setStateHandler(current_context_, WR(key_ptr), WS(key_size), WR(value_ptr), WS(value_size)));
}
inline WasmResult proxy_getMetadataPairs(MetadataType type, const char** value_ptr,
size_t* value_size) {
Expand All @@ -56,11 +56,6 @@ inline WasmResult proxy_getMetadataStruct(MetadataType type, const char* name_pt
WS(name_size), WR(value_ptr_ptr),
WR(value_size_ptr)));
}
inline WasmResult proxy_setMetadataStruct(MetadataType type, const char* name_ptr, size_t name_size,
const char* value_ptr, size_t value_size) {
return wordToWasmResult(setMetadataStructHandler(current_context_, WS(type), WR(name_ptr),
WS(name_size), WR(value_ptr), WS(value_size)));
}

// Generic selector
inline WasmResult proxy_getSelectorExpression(const char* path_ptr, size_t path_size,
Expand Down
Loading

0 comments on commit 3530007

Please sign in to comment.