From 5f869b0125933f096b9c8cb0977aa7a3bf1e6fe7 Mon Sep 17 00:00:00 2001 From: Yaroslav Skopets Date: Thu, 27 Aug 2020 14:36:50 +0200 Subject: [PATCH 1/2] update ABI to match `release/v1.15` branch of `envoyproxy/envoy-wasm` --- src/hostcalls.rs | 67 +++++++++--------------------------------------- src/traits.rs | 12 ++------- src/types.rs | 19 +++++++++++--- 3 files changed, 30 insertions(+), 68 deletions(-) diff --git a/src/hostcalls.rs b/src/hostcalls.rs index b4c05c9d..bc4664f0 100644 --- a/src/hostcalls.rs +++ b/src/hostcalls.rs @@ -32,7 +32,6 @@ mod abi { pub const PROXY_LOG: &str = "proxy_log"; pub const PROXY_GET_CURRENT_TIME_NANOSECONDS: &str = "proxy_get_current_time_nanoseconds"; pub const PROXY_SET_TICK_PERIOD_MILLISECONDS: &str = "proxy_set_tick_period_milliseconds"; - pub const PROXY_GET_CONFIGURATION: &str = "proxy_get_configuration"; pub const PROXY_GET_BUFFER_BYTES: &str = "proxy_get_buffer_bytes"; pub const PROXY_SET_BUFFER_BYTES: &str = "proxy_set_buffer_bytes"; pub const PROXY_GET_HEADER_MAP_PAIRS: &str = "proxy_get_header_map_pairs"; @@ -49,10 +48,9 @@ mod abi { pub const PROXY_RESOLVE_SHARED_QUEUE: &str = "proxy_resolve_shared_queue"; pub const PROXY_DEQUEUE_SHARED_QUEUE: &str = "proxy_dequeue_shared_queue"; pub const PROXY_ENQUEUE_SHARED_QUEUE: &str = "proxy_enqueue_shared_queue"; - pub const PROXY_CONTINUE_REQUEST: &str = "proxy_continue_request"; - pub const PROXY_CONTINUE_RESPONSE: &str = "proxy_continue_response"; + pub const PROXY_CONTINUE_STREAM: &str = "proxy_continue_stream"; + pub const PROXY_CLOSE_STREAM: &str = "proxy_close_stream"; pub const PROXY_SEND_LOCAL_RESPONSE: &str = "proxy_send_local_response"; - pub const PROXY_CLEAR_ROUTE_CACHE: &str = "proxy_clear_route_cache"; pub const PROXY_HTTP_CALL: &str = "proxy_http_call"; pub const PROXY_SET_EFFECTIVE_CONTEXT: &str = "proxy_set_effective_context"; pub const PROXY_DONE: &str = "proxy_done"; @@ -105,33 +103,6 @@ pub fn set_tick_period(period: Duration) -> Result<()> { } } -extern "C" { - fn proxy_get_configuration( - return_buffer_data: *mut *mut u8, - return_buffer_size: *mut usize, - ) -> Status; -} - -/// Returns configuration, e.g. VM configuration, extension configuration, etc. -pub fn get_configuration() -> Result> { - let mut return_data: *mut u8 = null_mut(); - let mut return_size: usize = 0; - unsafe { - match proxy_get_configuration(&mut return_data, &mut return_size) { - Status::Ok => { - if !return_data.is_null() { - Ok(Vec::from_raw_parts(return_data, return_size, return_size)) - .map(ByteString::from) - .map(Option::from) - } else { - Ok(None) - } - } - status => Err(HostCallError::new(abi::PROXY_GET_CONFIGURATION, status).into()), - } - } -} - extern "C" { fn proxy_get_buffer_bytes( buffer_type: BufferType, @@ -752,29 +723,29 @@ where } extern "C" { - fn proxy_continue_request() -> Status; + fn proxy_continue_stream(stream: StreamType) -> Status; } -/// Resume processing of paused HTTP request. -pub fn resume_http_request() -> Result<()> { +/// Resumes processing of a given stream, i.e. HTTP request or HTTP response. +pub fn continue_stream(stream_type: StreamType) -> Result<()> { unsafe { - match proxy_continue_request() { + match proxy_continue_stream(stream_type) { Status::Ok => Ok(()), - status => Err(HostCallError::new(abi::PROXY_CONTINUE_REQUEST, status).into()), + status => Err(HostCallError::new(abi::PROXY_CONTINUE_STREAM, status).into()), } } } extern "C" { - fn proxy_continue_response() -> Status; + fn proxy_close_stream(stream: StreamType) -> Status; } -/// Resume processing of paused HTTP response. -pub fn resume_http_response() -> Result<()> { +/// Terminates processing of a given stream, i.e. HTTP request or HTTP response. +pub fn close_stream(stream_type: StreamType) -> Result<()> { unsafe { - match proxy_continue_response() { + match proxy_close_stream(stream_type) { Status::Ok => Ok(()), - status => Err(HostCallError::new(abi::PROXY_CONTINUE_RESPONSE, status).into()), + status => Err(HostCallError::new(abi::PROXY_CLOSE_STREAM, status).into()), } } } @@ -840,20 +811,6 @@ where } } -extern "C" { - fn proxy_clear_route_cache() -> Status; -} - -/// Clears HTTP route cache. -pub fn clear_http_route_cache() -> Result<()> { - unsafe { - match proxy_clear_route_cache() { - Status::Ok => Ok(()), - status => Err(HostCallError::new(abi::PROXY_CLEAR_ROUTE_CACHE, status).into()), - } - } -} - extern "C" { fn proxy_http_call( upstream_data: *const u8, diff --git a/src/traits.rs b/src/traits.rs index cc929d11..445c9b63 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -111,10 +111,6 @@ pub trait RootContext: Context { true } - fn get_configuration(&self) -> Option { - hostcalls::get_configuration().unwrap() - } - fn set_tick_period(&self, period: Duration) { hostcalls::set_tick_period(period).unwrap() } @@ -218,7 +214,7 @@ pub trait HttpContext: Context { } fn resume_http_request(&self) { - hostcalls::resume_http_request().unwrap() + hostcalls::continue_stream(StreamType::Request).unwrap() } fn on_http_response_headers(&mut self, _num_headers: usize) -> Action { @@ -278,7 +274,7 @@ pub trait HttpContext: Context { } fn resume_http_response(&self) { - hostcalls::resume_http_response().unwrap() + hostcalls::continue_stream(StreamType::Response).unwrap() } fn send_http_response( @@ -290,9 +286,5 @@ pub trait HttpContext: Context { hostcalls::send_http_response(status_code, &headers, body).unwrap() } - fn clear_http_route_cache(&self) { - hostcalls::clear_http_route_cache().unwrap() - } - fn on_log(&mut self) {} } diff --git a/src/types.rs b/src/types.rs index 95d02bf9..4e64d8c7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -56,7 +56,11 @@ pub enum BufferType { HttpResponseBody = 1, DownstreamData = 2, UpstreamData = 3, - HttpCallResponseBody = 4, + HttpCallResponseBody = 4, // Immutable + GrpcReceiveBuffer = 5, // Immutable + VmConfiguration = 6, // Immutable + PluginConfiguration = 7, // Immutable + CallData = 8, // Immutable } #[repr(u32)] @@ -66,8 +70,10 @@ pub enum MapType { HttpRequestTrailers = 1, HttpResponseHeaders = 2, HttpResponseTrailers = 3, - HttpCallResponseHeaders = 6, - HttpCallResponseTrailers = 7, + GrpcReceiveInitialMetadata = 4, // Immutable + GrpcReceiveTrailingMetadata = 5, // Immutable + HttpCallResponseHeaders = 6, // Immutable + HttpCallResponseTrailers = 7, // Immutable } #[repr(u32)] @@ -77,3 +83,10 @@ pub enum PeerType { Local = 1, Remote = 2, } + +#[repr(u32)] +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub enum StreamType { + Request = 0, + Response = 1, +} From d0699ddccf51fa56844e570afc32c669ba43e2b7 Mon Sep 17 00:00:00 2001 From: Yaroslav Skopets Date: Thu, 27 Aug 2020 16:10:40 +0200 Subject: [PATCH 2/2] update `proxy_on_request_headers` and `proxy_on_response_headers` --- examples/http_auth_random.rs | 4 ++-- examples/http_headers.rs | 4 ++-- src/dispatcher.rs | 38 ++++++++++++++++++++++++++++-------- src/traits.rs | 4 ++-- 4 files changed, 36 insertions(+), 14 deletions(-) diff --git a/examples/http_auth_random.rs b/examples/http_auth_random.rs index 7fb1397b..1470bb3a 100644 --- a/examples/http_auth_random.rs +++ b/examples/http_auth_random.rs @@ -28,7 +28,7 @@ pub fn _start() { struct HttpAuthRandom; impl HttpContext for HttpAuthRandom { - fn on_http_request_headers(&mut self, _: usize) -> Action { + fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { self.dispatch_http_call( "httpbin", vec![ @@ -44,7 +44,7 @@ impl HttpContext for HttpAuthRandom { Action::Pause } - fn on_http_response_headers(&mut self, _: usize) -> Action { + fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action { self.set_http_response_header("Powered-By", Some("proxy-wasm")); Action::Continue } diff --git a/examples/http_headers.rs b/examples/http_headers.rs index aa071b39..13afc393 100644 --- a/examples/http_headers.rs +++ b/examples/http_headers.rs @@ -33,7 +33,7 @@ struct HttpHeaders { impl Context for HttpHeaders {} impl HttpContext for HttpHeaders { - fn on_http_request_headers(&mut self, _: usize) -> Action { + fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { for (name, value) in &self.get_http_request_headers() { trace!("#{} -> {}: {}", self.context_id, name, value); } @@ -51,7 +51,7 @@ impl HttpContext for HttpHeaders { } } - fn on_http_response_headers(&mut self, _: usize) -> Action { + fn on_http_response_headers(&mut self, _: usize, _: bool) -> Action { for (name, value) in &self.get_http_response_headers() { trace!("#{} <- {}: {}", self.context_id, name, value); } diff --git a/src/dispatcher.rs b/src/dispatcher.rs index d808cb4c..be312169 100644 --- a/src/dispatcher.rs +++ b/src/dispatcher.rs @@ -311,10 +311,15 @@ impl Dispatcher { } } - fn on_http_request_headers(&self, context_id: u32, num_headers: usize) -> Action { + fn on_http_request_headers( + &self, + context_id: u32, + num_headers: usize, + end_of_stream: bool, + ) -> Action { if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) { self.active_id.set(context_id); - http_stream.on_http_request_headers(num_headers) + http_stream.on_http_request_headers(num_headers, end_of_stream) } else { panic!("invalid context_id") } @@ -343,10 +348,15 @@ impl Dispatcher { } } - fn on_http_response_headers(&self, context_id: u32, num_headers: usize) -> Action { + fn on_http_response_headers( + &self, + context_id: u32, + num_headers: usize, + end_of_stream: bool, + ) -> Action { if let Some(http_stream) = self.http_streams.borrow_mut().get_mut(&context_id) { self.active_id.set(context_id); - http_stream.on_http_response_headers(num_headers) + http_stream.on_http_response_headers(num_headers, end_of_stream) } else { panic!("invalid context_id") } @@ -477,8 +487,14 @@ pub extern "C" fn proxy_on_upstream_connection_close(context_id: u32, peer_type: } #[no_mangle] -pub extern "C" fn proxy_on_request_headers(context_id: u32, num_headers: usize) -> Action { - DISPATCHER.with(|dispatcher| dispatcher.on_http_request_headers(context_id, num_headers)) +pub extern "C" fn proxy_on_request_headers( + context_id: u32, + num_headers: usize, + end_of_stream: bool, +) -> Action { + DISPATCHER.with(|dispatcher| { + dispatcher.on_http_request_headers(context_id, num_headers, end_of_stream) + }) } #[no_mangle] @@ -497,8 +513,14 @@ pub extern "C" fn proxy_on_request_trailers(context_id: u32, num_trailers: usize } #[no_mangle] -pub extern "C" fn proxy_on_response_headers(context_id: u32, num_headers: usize) -> Action { - DISPATCHER.with(|dispatcher| dispatcher.on_http_response_headers(context_id, num_headers)) +pub extern "C" fn proxy_on_response_headers( + context_id: u32, + num_headers: usize, + end_of_stream: bool, +) -> Action { + DISPATCHER.with(|dispatcher| { + dispatcher.on_http_response_headers(context_id, num_headers, end_of_stream) + }) } #[no_mangle] diff --git a/src/traits.rs b/src/traits.rs index 445c9b63..a6190a00 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -157,7 +157,7 @@ pub trait StreamContext: Context { } pub trait HttpContext: Context { - fn on_http_request_headers(&mut self, _num_headers: usize) -> Action { + fn on_http_request_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { Action::Continue } @@ -217,7 +217,7 @@ pub trait HttpContext: Context { hostcalls::continue_stream(StreamType::Request).unwrap() } - fn on_http_response_headers(&mut self, _num_headers: usize) -> Action { + fn on_http_response_headers(&mut self, _num_headers: usize, _end_of_stream: bool) -> Action { Action::Continue }