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

wasm: use static registration for runtimes #14014

Merged
merged 7 commits into from
Nov 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ extensions/filters/common/original_src @snowp @klarose
/*/extensions/filters/network/wasm @PiotrSikora @lizan
# webassembly common extension
/*/extensions/common/wasm @PiotrSikora @lizan
# webassembly runtimes
/*/extensions/wasm_runtime/ @PiotrSikora @lizan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not putting it under extensions/common/wasm/runtimes or similar?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we put any actual extension registrations under extensions/common, extensions/common is for libraries used by extensions.

# common matcher
/*/extensions/common/matcher @mattklein123 @yangminzhu
# common crypto extension
Expand Down
24 changes: 23 additions & 1 deletion api/envoy/extensions/wasm/v3/wasm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,29 @@ message VmConfig {
// See ref: "TODO: add ref" for details.
string vm_id = 1;

// The Wasm runtime type (either "v8" or "null" for code compiled into Envoy).
// The Wasm runtime type.
// Available Wasm runtime types are registered as extensions. The following runtimes are included
// in Envoy code base:
//
// .. _extension_envoy.wasm.runtime.null:
//
// **envoy.wasm.runtime.null**: Null sandbox, the Wasm module must be compiled and linked into the
// Envoy binary. The registered name is given in the *code* field as *inline_string*.
//
// .. _extension_envoy.wasm.runtime.v8:
//
// **envoy.wasm.runtime.v8**: `V8 <https://v8.dev/>`_-based WebAssembly runtime.
//
// .. _extension_envoy.wasm.runtime.wavm:
//
// **envoy.wasm.runtime.wavm**: `WAVM <https://wavm.github.io/>`_-based WebAssembly runtime.
// This runtime is not enabled in the official build.
//
// .. _extension_envoy.wasm.runtime.wasmtime:
//
// **envoy.wasm.runtime.wasmtime**: `Wasmtime <https://wasmtime.dev/>`_-based WebAssembly runtime.
// This runtime is not enabled in the official build.
//
string runtime = 2 [(validate.rules).string = {min_len: 1}];

// The Wasm code that Envoy will execute.
Expand Down
41 changes: 41 additions & 0 deletions bazel/repository_locations.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
cpe = "cpe:2.3:a:llvm:*:*",
),
Expand All @@ -612,6 +615,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
cpe = "cpe:2.3:a:webassembly_virtual_machine_project:webassembly_virtual_machine:*",
),
Expand All @@ -631,6 +637,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
cpe = "cpe:2.3:a:webassembly_virtual_machine_project:webassembly_virtual_machine:*",
),
Expand All @@ -652,6 +661,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
cpe = "N/A",
),
Expand Down Expand Up @@ -703,6 +715,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-10-27",
cpe = "cpe:2.3:a:google:v8:*",
Expand Down Expand Up @@ -750,6 +765,10 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.network.rbac",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.null",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-07-14",
cpe = "N/A",
Expand All @@ -769,6 +788,10 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.null",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-04-02",
cpe = "N/A",
Expand Down Expand Up @@ -862,6 +885,10 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.null",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-10-22",
cpe = "N/A",
Expand All @@ -881,6 +908,10 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.null",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-11-10",
cpe = "N/A",
Expand Down Expand Up @@ -911,6 +942,10 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.null",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2020-10-21",
cpe = "N/A",
Expand All @@ -931,6 +966,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2019-06-21",
cpe = "N/A",
Expand All @@ -950,6 +988,9 @@ REPOSITORY_LOCATIONS_SPEC = dict(
"envoy.filters.http.wasm",
"envoy.filters.network.wasm",
"envoy.stat_sinks.wasm",
"envoy.wasm.runtime.v8",
"envoy.wasm.runtime.wavm",
"envoy.wasm.runtime.wasmtime",
],
release_date = "2018-12-18",
cpe = "N/A",
Expand Down
24 changes: 23 additions & 1 deletion generated_api_shadow/envoy/extensions/wasm/v3/wasm.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions source/extensions/common/wasm/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ envoy_cc_library(
],
)

envoy_cc_library(
name = "wasm_runtime_factory_interface",
hdrs = [
"wasm_runtime_factory.h",
],
deps = [
":wasm_hdr",
],
)

# NB: Used to break the circular dependency between wasm_lib and null_plugin_lib.
envoy_cc_library(
name = "wasm_hdr",
Expand Down Expand Up @@ -84,6 +94,7 @@ envoy_cc_library(
deps = [
":wasm_hdr",
":wasm_interoperation_lib",
":wasm_runtime_factory_interface",
"//external:abseil_base",
"//external:abseil_node_hash_map",
"//include/envoy/server:lifecycle_notifier_interface",
Expand Down
26 changes: 26 additions & 0 deletions source/extensions/common/wasm/wasm_runtime_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <string_view>

#include "extensions/common/wasm/wasm_vm.h"

namespace Envoy {
namespace Extensions {
namespace Common {
namespace Wasm {

class WasmRuntimeFactory {
public:
virtual ~WasmRuntimeFactory() = default;
virtual WasmVmPtr createWasmVm() PURE;

virtual absl::string_view name() PURE;
virtual absl::string_view shortName() PURE;

std::string category() { return "envoy.wasm.runtime"; }
};

} // namespace Wasm
} // namespace Common
} // namespace Extensions
} // namespace Envoy
45 changes: 10 additions & 35 deletions source/extensions/common/wasm/wasm_vm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,11 @@
#include "extensions/common/wasm/context.h"
#include "extensions/common/wasm/ext/envoy_null_vm_wasm_api.h"
#include "extensions/common/wasm/wasm_extension.h"
#include "extensions/common/wasm/wasm_runtime_factory.h"
#include "extensions/common/wasm/well_known_names.h"

#include "include/proxy-wasm/null.h"
#include "include/proxy-wasm/null_plugin.h"

#if defined(ENVOY_WASM_V8)
#include "include/proxy-wasm/v8.h"
#endif
#if defined(ENVOY_WASM_WAVM)
#include "include/proxy-wasm/wavm.h"
#endif
#if defined(ENVOY_WASM_WASMTIME)
#include "include/proxy-wasm/wasmtime.h"
#endif

using ContextBase = proxy_wasm::ContextBase;
using Word = proxy_wasm::Word;

Expand Down Expand Up @@ -70,36 +60,21 @@ WasmVmPtr createWasmVm(absl::string_view runtime, const Stats::ScopeSharedPtr& s
ENVOY_LOG_TO_LOGGER(Envoy::Logger::Registry::getLog(Envoy::Logger::Id::wasm), warn,
"Failed to create Wasm VM with unspecified runtime");
return nullptr;
} else if (runtime == WasmRuntimeNames::get().Null) {
auto wasm = proxy_wasm::createNullVm();
wasm->integration() = getWasmExtension()->createEnvoyWasmVmIntegration(scope, runtime, "null");
return wasm;
#if defined(ENVOY_WASM_V8)
} else if (runtime == WasmRuntimeNames::get().V8) {
auto wasm = proxy_wasm::createV8Vm();
wasm->integration() = getWasmExtension()->createEnvoyWasmVmIntegration(scope, runtime, "v8");
return wasm;
#endif
#if defined(ENVOY_WASM_WAVM)
} else if (runtime == WasmRuntimeNames::get().Wavm) {
auto wasm = proxy_wasm::createWavmVm();
wasm->integration() = getWasmExtension()->createEnvoyWasmVmIntegration(scope, runtime, "wavm");
return wasm;
#endif
#if defined(ENVOY_WASM_WASMTIME)
} else if (runtime == WasmRuntimeNames::get().Wasmtime) {
auto wasm = proxy_wasm::createWasmtimeVm();
wasm->integration() =
getWasmExtension()->createEnvoyWasmVmIntegration(scope, runtime, "wasmtime");
return wasm;
#endif
} else {
}

auto runtime_factory = Registry::FactoryRegistry<WasmRuntimeFactory>::getFactory(runtime);
if (runtime_factory == nullptr) {
ENVOY_LOG_TO_LOGGER(
Envoy::Logger::Registry::getLog(Envoy::Logger::Id::wasm), warn,
"Failed to create Wasm VM using {} runtime. Envoy was compiled without support for it",
runtime);
return nullptr;
}

auto wasm = runtime_factory->createWasmVm();
wasm->integration() = getWasmExtension()->createEnvoyWasmVmIntegration(
scope, runtime_factory->name(), runtime_factory->shortName());
return wasm;
}

} // namespace Wasm
Expand Down
9 changes: 9 additions & 0 deletions source/extensions/extensions_build_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,17 @@ EXTENSIONS = {
#
# Watchdog actions
#

"envoy.watchdog.profile_action": "//source/extensions/watchdog/profile_action:config",

#
# WebAssembly runtimes
#

"envoy.wasm.runtime.null": "//source/extensions/wasm_runtime/null:config",
"envoy.wasm.runtime.v8": "//source/extensions/wasm_runtime/v8:config",
"envoy.wasm.runtime.wavm": "//source/extensions/wasm_runtime/wavm:config",
"envoy.wasm.runtime.wasmtime": "//source/extensions/wasm_runtime/wasmtime:config",
}

# These can be changed to ["//visibility:public"], for downstream builds which
Expand Down
21 changes: 21 additions & 0 deletions source/extensions/wasm_runtime/null/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load(
"//bazel:envoy_build_system.bzl",
"envoy_cc_extension",
"envoy_extension_package",
)

licenses(["notice"]) # Apache 2

envoy_extension_package()

envoy_cc_extension(
name = "config",
srcs = ["config.cc"],
security_posture = "unknown",
status = "alpha",
deps = [
"//include/envoy/registry",
"//source/extensions/common/wasm:wasm_hdr",
"//source/extensions/common/wasm:wasm_runtime_factory_interface",
],
)
25 changes: 25 additions & 0 deletions source/extensions/wasm_runtime/null/config.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "envoy/registry/registry.h"

#include "extensions/common/wasm/wasm_runtime_factory.h"

#include "include/proxy-wasm/null.h"

namespace Envoy {
namespace Extensions {
namespace Common {
namespace Wasm {

class NullRuntimeFactory : public WasmRuntimeFactory {
public:
WasmVmPtr createWasmVm() override { return proxy_wasm::createNullVm(); }

absl::string_view name() override { return "envoy.wasm.runtime.null"; }
absl::string_view shortName() override { return "null"; }
};

REGISTER_FACTORY(NullRuntimeFactory, WasmRuntimeFactory);

} // namespace Wasm
} // namespace Common
} // namespace Extensions
} // namespace Envoy
Loading