diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index e2c7524b..6a8c28f3 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -90,8 +90,8 @@ jobs: RUSTFLAGS: "-C instrument-coverage" CC_x86_64_unknown_none: clang AR_x86_64_unknown_none: llvm-ar - RUN_REQUESTER_FEATURES: "spdm-ring,hashed-transcript-data" - RUN_RESPONDER_FEATURES: "spdm-ring,hashed-transcript-data" + RUN_REQUESTER_FEATURES: "spdm-ring,hashed-transcript-data,async-executor" + RUN_RESPONDER_FEATURES: "spdm-ring,hashed-transcript-data,async-executor" run: | ./sh_script/build.sh -r @@ -101,8 +101,8 @@ jobs: RUSTFLAGS: "-C instrument-coverage" CC_x86_64_unknown_none: clang AR_x86_64_unknown_none: llvm-ar - RUN_REQUESTER_FEATURES: "spdm-mbedtls" - RUN_RESPONDER_FEATURES: "spdm-mbedtls" + RUN_REQUESTER_FEATURES: "spdm-mbedtls,async-executor" + RUN_RESPONDER_FEATURES: "spdm-mbedtls,async-executor" run: | ./sh_script/build.sh -r @@ -112,8 +112,8 @@ jobs: RUSTFLAGS: "-C instrument-coverage" CC_x86_64_unknown_none: clang AR_x86_64_unknown_none: llvm-ar - RUN_REQUESTER_FEATURES: "spdm-mbedtls,hashed-transcript-data" - RUN_RESPONDER_FEATURES: "spdm-mbedtls,hashed-transcript-data" + RUN_REQUESTER_FEATURES: "spdm-mbedtls,hashed-transcript-data,async-executor" + RUN_RESPONDER_FEATURES: "spdm-mbedtls,hashed-transcript-data,async-executor" run: | ./sh_script/build.sh -r diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e57fa87f..e3a680c8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,22 +22,22 @@ jobs: runs-on: [ubuntu-latest] run_requester_features: [ - "spdm-ring", - "spdm-ring,hashed-transcript-data", - "spdm-mbedtls", - "spdm-mbedtls,hashed-transcript-data", + "spdm-ring,async-executor", + "spdm-ring,hashed-transcript-data,async-executor", + "spdm-mbedtls,async-executor", + "spdm-mbedtls,hashed-transcript-data,async-executor", ] run_responder_features: [ - "spdm-ring", - "spdm-ring,hashed-transcript-data", - "spdm-mbedtls", - "spdm-mbedtls,hashed-transcript-data", + "spdm-ring,async-executor", + "spdm-ring,hashed-transcript-data,async-executor", + "spdm-mbedtls,async-executor", + "spdm-mbedtls,hashed-transcript-data,async-executor", ] include: - runs-on: windows-latest - run_requester_features: "spdm-ring,hashed-transcript-data" - run_responder_features: "spdm-ring,hashed-transcript-data" + run_requester_features: "spdm-ring,hashed-transcript-data,async-executor" + run_responder_features: "spdm-ring,hashed-transcript-data,async-executor" # The type of runner that the job will run on runs-on: ${{ matrix.runs-on }} diff --git a/Cargo.lock b/Cargo.lock index 16f68e81..f3529a8f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1601,6 +1601,7 @@ dependencies = [ "async-trait", "bytes", "codec", + "executor", "futures", "log", "mctp_transport", @@ -1609,6 +1610,7 @@ dependencies = [ "spdmlib", "spdmlib_crypto_mbedtls", "spin 0.9.8", + "tokio", "untrusted", "webpki", ] @@ -1618,6 +1620,7 @@ name = "spdm-requester-emu" version = "0.1.0" dependencies = [ "codec", + "executor", "futures", "idekm", "log", @@ -1636,6 +1639,7 @@ name = "spdm-responder-emu" version = "0.1.0" dependencies = [ "codec", + "executor", "futures", "idekm", "log", diff --git a/readme.md b/readme.md index 8531d7af..9d7f9eb0 100644 --- a/readme.md +++ b/readme.md @@ -134,12 +134,12 @@ cargo build -Z build-std=core,alloc,compiler_builtins --target x86_64-unknown-no Open one command windows and run: ``` -cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring,hashed-transcript-data" +cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring,hashed-transcript-data,async-executor" ``` Open another command windows and run: ``` -cargo run -p spdm-requester-emu --no-default-features --features "spdm-ring,hashed-transcript-data" +cargo run -p spdm-requester-emu --no-default-features --features "spdm-ring,hashed-transcript-data,async-executor" ``` ### Run emulator with selected feature @@ -147,23 +147,25 @@ cargo run -p spdm-requester-emu --no-default-features --features "spdm-ring,hash The following list shows the supported combinations for both spdm-requester-emu and spdm-responder-emu -| Features | CryptoLibrary | Hashed transcript data support | Notes | -| ----------------------------------------------------------------------- | ------------- | ------------------------------ | ------------------------------------------------------------------ | -| spdm-ring | ring | No | use ring as crypto library with hashed-transcript-data disabled | -| spdm-ring,hashed-transcript-data | ring | Yes | use ring as crypto library with hashed-transcript-data enabled | -| spdm-mbedtls | mbedtls | No | use mbedtls as crypto library with hashed-transcript-data disabled | -| spdm-mbedtls,hashed-transcript-data | mbedtls | Yes | use mbedtls as crypto library with hashed-transcript-data | +| Features | CryptoLibrary | Hashed transcript data support | async runtime | notes | +|-------------------------------------------------|---------------|--------------------------------|---------------|---------------------------------------------------------------------------------------------------| +| spdm-ring,async-executor | ring | No | executor | use ring as crypto library with hashed-transcript-data disabled, use executor as async runtime | +| spdm-ring,hashed-transcript-data,async-executor | ring | Yes | executor | use ring as crypto library with hashed-transcript-data enabled, use executor as async runtime | +| spdm-ring,hashed-transcript-data,async-tokio | ring | Yes | tokio | use ring as crypto library with hashed-transcript-data enabled, use tokio as async runtime | +| spdm-mbedtls,async-executor | mbedtls | No | executor | use mbedtls as crypto library with hashed-transcript-data disabled, use executor as async runtime | +| spdm-mbedtls,hashed-transcript-data,async-executor | mbedtls | Yes | executor | use mbedtls as crypto library with hashed-transcript-data enabled, use executor as async runtime | +| spdm-mbedtls,hashed-transcript-data,async-tokio | mbedtls | Yes | tokio | use mbedtls as crypto library with hashed-transcript-data enabled, use tokio as async runtime | -For example, run the emulator with spdm-ring enabled and without hashed-transcript-data enabled. +For example, run the emulator with spdm-ring enabled and without hashed-transcript-data enabled, and use executor as async runtime. Open one command windows and run: ``` -cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring" +cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring,async-executor " ``` -run the emulator with spdm-mbedtls enabled and with hashed-transcript-data enabled. +run the emulator with spdm-mbedtls enabled and with hashed-transcript-data enabled, and use tokio as async runtime. Open another command windows and run: ``` -cargo run -p spdm-requester-emu --no-default-features --features "spdm-mbedtls,hashed-transcript-data" +cargo run -p spdm-requester-emu --no-default-features --features "spdm-mbedtls,hashed-transcript-data,async-tokio" ``` NOTE: In order to run the emu without hashed-transcript-data, please change `max_cert_chain_data_size` in `spdmlib/etc/config.json` from `4096` to `3500`. @@ -192,14 +194,14 @@ spdm_responder_emu.exe --trans PCI_DOE 2. run rust-spdm-emu as requester: ``` -cargo run -p spdm-requester-emu --no-default-features --features "spdm-ring,hashed-transcript-data" +cargo run -p spdm-requester-emu --no-default-features --features "spdm-ring,hashed-transcript-data,async-executor " ``` Test rust-spdm as responder: 1. run rust-spdm-emu as Test rust-spdm as responder: ``` -cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring,hashed-transcript-data" +cargo run -p spdm-responder-emu --no-default-features --features "spdm-ring,hashed-transcript-data,async-executor " ``` 2. run libspdm in spdm-emu as requester: @@ -218,12 +220,12 @@ export RUST_MIN_STACK=10485760 Test with hashed-transcript-data: ``` -cargo test --no-default-features --features "spdmlib/std,spdmlib/spdm-ring,spdmlib/hashed-transcript-data" -- --test-threads=1 +cargo test --no-default-features --features "spdmlib/std,spdmlib/spdm-ring,spdmlib/hashed-transcript-data,async-executor" -- --test-threads=1 ``` Test without hashed-transcript-data: ``` -cargo test --no-default-features --features "spdmlib/std,spdmlib/spdm-ring" -- --test-threads=1 +cargo test --no-default-features --features "spdmlib/std,spdmlib/spdm-ring,async-executor" -- --test-threads=1 ``` To run a specific test, use `cargo test ` diff --git a/sh_script/build.sh b/sh_script/build.sh index 2a2a5431..4ebd4fe1 100755 --- a/sh_script/build.sh +++ b/sh_script/build.sh @@ -84,8 +84,8 @@ build() { echo_command cargo build -p spdm-responder-emu } -RUN_REQUESTER_FEATURES=${RUN_REQUESTER_FEATURES:-spdm-ring,hashed-transcript-data} -RUN_RESPONDER_FEATURES=${RUN_RESPONDER_FEATURES:-spdm-ring,hashed-transcript-data} +RUN_REQUESTER_FEATURES=${RUN_REQUESTER_FEATURES:-spdm-ring,hashed-transcript-data,async-executor} +RUN_RESPONDER_FEATURES=${RUN_RESPONDER_FEATURES:-spdm-ring,hashed-transcript-data,async-executor} RUN_REQUESTER_MUTAUTH_FEATURES="${RUN_REQUESTER_FEATURES},mut-auth" RUN_RESPONDER_MUTAUTH_FEATURES="${RUN_RESPONDER_FEATURES},mut-auth" RUN_RESPONDER_MANDATORY_MUTAUTH_FEATURES="${RUN_RESPONDER_FEATURES},mandatory-mut-auth" diff --git a/test/spdm-emu/Cargo.toml b/test/spdm-emu/Cargo.toml index c0d744f8..640f2b7a 100644 --- a/test/spdm-emu/Cargo.toml +++ b/test/spdm-emu/Cargo.toml @@ -20,13 +20,17 @@ futures = { version = "0.3", default-features = false } async-trait = "0.1.71" async-recursion = "1.0.4" spin = { version = "0.9.8" } +tokio = { version = "1.30.0", features = ["full"] } +executor = { path = "../../executor" } spdmlib_crypto_mbedtls = { path = "../../spdmlib_crypto_mbedtls", default-features = false, optional = true } [features] -default = ["spdm-ring", "spdmlib/hashed-transcript-data"] +default = ["spdm-ring", "spdmlib/hashed-transcript-data", "async-executor"] mut-auth = ["spdmlib/mut-auth"] mandatory-mut-auth = ["mut-auth", "spdmlib/mandatory-mut-auth"] spdm-ring = ["spdmlib/spdm-ring", "spdmlib/std"] spdm-mbedtls = ["spdmlib_crypto_mbedtls"] hashed-transcript-data = ["spdmlib/hashed-transcript-data", "spdmlib_crypto_mbedtls?/hashed-transcript-data"] +async-executor = [] +async-tokio = [] diff --git a/test/spdm-emu/src/async_runtime.rs b/test/spdm-emu/src/async_runtime.rs new file mode 100644 index 00000000..45122706 --- /dev/null +++ b/test/spdm-emu/src/async_runtime.rs @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Intel Corporation +// +// SPDX-License-Identifier: Apache-2.0 or MIT + +extern crate alloc; +use alloc::boxed::Box; +use core::{future::Future, pin::Pin}; + +// Run async task +pub fn block_on(future: Pin + 'static + Send>>) -> T +where + T: Send + 'static, +{ + #[cfg(all(feature = "async-executor", feature = "async-tokio"))] + compile_error!("features `async-executor` and `async-tokio` are mutually exclusive"); + + if cfg!(feature = "async-executor") { + executor::block_on(future) + } else if cfg!(feature = "async-tokio") { + let rt = tokio::runtime::Runtime::new().unwrap(); + + rt.block_on(future) + } else { + panic!("Calling block_on require one of `async-executor` or `async-tokio` is enabled!"); + } +} diff --git a/test/spdm-emu/src/lib.rs b/test/spdm-emu/src/lib.rs index 39fc4c7a..1266d5e1 100644 --- a/test/spdm-emu/src/lib.rs +++ b/test/spdm-emu/src/lib.rs @@ -4,6 +4,7 @@ #![forbid(unsafe_code)] +pub mod async_runtime; pub mod crypto; pub mod crypto_callback; pub mod secret_impl_sample; diff --git a/test/spdm-requester-emu/Cargo.toml b/test/spdm-requester-emu/Cargo.toml index c3677ced..40e67fbb 100644 --- a/test/spdm-requester-emu/Cargo.toml +++ b/test/spdm-requester-emu/Cargo.toml @@ -19,10 +19,13 @@ simple_logger = "4.2.0" futures = { version = "0.3", default-features = false } spin = { version = "0.9.8" } tokio = { version = "1.30.0", features = ["full"] } +executor = { path = "../../executor" } [features] -default = ["spdm-emu/default"] +default = ["spdm-emu/default", "async-executor"] mut-auth = ["spdm-emu/mut-auth"] spdm-ring = ["spdm-emu/spdm-ring"] spdm-mbedtls = ["spdm-emu/spdm-mbedtls"] hashed-transcript-data = ["spdm-emu/hashed-transcript-data"] +async-executor = ["spdm-emu/async-executor"] +async-tokio = ["spdm-emu/async-tokio"] \ No newline at end of file diff --git a/test/spdm-requester-emu/src/main.rs b/test/spdm-requester-emu/src/main.rs index f14c1123..b56e20bd 100644 --- a/test/spdm-requester-emu/src/main.rs +++ b/test/spdm-requester-emu/src/main.rs @@ -22,6 +22,7 @@ use log::LevelFilter; use log::*; use simple_logger::SimpleLogger; +use spdm_emu::async_runtime::block_on; use spdm_emu::crypto_callback::SECRET_ASYM_IMPL_INSTANCE; use spdm_emu::secret_impl_sample::SECRET_PSK_IMPL_INSTANCE; use spdm_emu::EMU_STACK_SIZE; @@ -56,8 +57,6 @@ use tdisp::pci_tdisp_requester::pci_tdisp_req_lock_interface_request; use tdisp::pci_tdisp_requester::pci_tdisp_req_start_interface_request; use tdisp::pci_tdisp_requester::pci_tdisp_req_stop_interface_request; -use tokio::runtime::Runtime; - use spin::Mutex; extern crate alloc; use alloc::sync::Arc; @@ -1294,30 +1293,31 @@ fn emu_main() { SOCKET_TRANSPORT_TYPE_MCTP }; - // Create the runtime - let rt = Runtime::new().unwrap(); - - rt.block_on(send_receive_hello( + block_on(Box::pin(send_receive_hello( socket.clone(), transport_encap.clone(), transport_type, - )); + ))); let socket_io_transport = SocketIoTransport::new(socket.clone()); let socket_io_transport: Arc> = Arc::new(Mutex::new(socket_io_transport)); - rt.block_on(test_spdm( + block_on(Box::pin(test_spdm( socket_io_transport.clone(), transport_encap.clone(), - )); + ))); - rt.block_on(test_idekm_tdisp( + block_on(Box::pin(test_idekm_tdisp( socket_io_transport.clone(), transport_encap.clone(), - )); + ))); - rt.block_on(send_receive_stop(socket, transport_encap, transport_type)); + block_on(Box::pin(send_receive_stop( + socket, + transport_encap, + transport_type, + ))); } fn main() { diff --git a/test/spdm-responder-emu/Cargo.toml b/test/spdm-responder-emu/Cargo.toml index 5fe6ac48..a77c7840 100644 --- a/test/spdm-responder-emu/Cargo.toml +++ b/test/spdm-responder-emu/Cargo.toml @@ -19,11 +19,14 @@ log = "0.4.13" futures = { version = "0.3", default-features = false } spin = { version = "0.9.8" } tokio = { version = "1.30.0", features = ["full"] } +executor = { path = "../../executor" } zeroize = { version = "1.5.0", features = ["zeroize_derive"]} [features] -mut-auth = ["spdm-emu/mut-auth"] +mut-auth = ["spdm-emu/mut-auth", "async-executor"] mandatory-mut-auth = ["mut-auth", "spdm-emu/mandatory-mut-auth"] spdm-ring = ["spdm-emu/spdm-ring"] spdm-mbedtls = ["spdm-emu/spdm-mbedtls"] hashed-transcript-data = ["spdm-emu/hashed-transcript-data"] +async-executor = ["spdm-emu/async-executor"] +async-tokio = ["spdm-emu/async-tokio"] diff --git a/test/spdm-responder-emu/src/main.rs b/test/spdm-responder-emu/src/main.rs index 1e2837d0..47a6bf24 100644 --- a/test/spdm-responder-emu/src/main.rs +++ b/test/spdm-responder-emu/src/main.rs @@ -12,6 +12,7 @@ use spdm_device_tdisp_example::init_device_tdisp_instance; use log::LevelFilter; use simple_logger::SimpleLogger; +use spdm_emu::async_runtime::block_on; use spdm_emu::watchdog_impl_sample::init_watchdog; use spdmlib::common::{SecuredMessageVersion, SpdmOpaqueSupport}; use spdmlib::config::{MAX_ROOT_CERT_SUPPORT, RECEIVER_BUFFER_SIZE}; @@ -47,24 +48,28 @@ use spin::Mutex; extern crate alloc; use alloc::sync::Arc; use core::ops::DerefMut; +use std::ops::Deref; use crate::spdm_device_tdisp_example::DeviceContext; async fn process_socket_message( stream: Arc>, transport_encap: Arc>, - buffer: &[u8], + buffer: Arc>, + buffer_size: usize, ) -> bool { - if buffer.len() < SOCKET_HEADER_LEN { + if buffer_size < SOCKET_HEADER_LEN { return false; } - let mut reader = Reader::init(&buffer[..SOCKET_HEADER_LEN]); + let buffer_ref = buffer.lock(); + let buffer_ref = buffer_ref.deref(); + let mut reader = Reader::init(&buffer_ref[..SOCKET_HEADER_LEN]); let socket_header = SpdmSocketHeader::read(&mut reader).unwrap(); let res = ( socket_header.transport_type.to_be(), socket_header.command.to_be(), - &buffer[SOCKET_HEADER_LEN..], + &buffer_ref[SOCKET_HEADER_LEN..], ); match socket_header.command.to_be() { @@ -79,7 +84,13 @@ async fn process_socket_message( SOCKET_SPDM_COMMAND_NORMAL => true, _ => { if USE_PCIDOE { - send_pci_discovery(stream.clone(), transport_encap.clone(), res.0, buffer).await + send_pci_discovery( + stream.clone(), + transport_encap.clone(), + res.0, + &buffer_ref[..buffer_size], + ) + .await } else { send_unknown(stream, transport_encap, res.0).await; false @@ -153,35 +164,34 @@ fn emu_main() { let mctp_transport_encap: Arc> = Arc::new(Mutex::new(MctpTransportEncap {})); - // Create the runtime - let rt = tokio::runtime::Runtime::new().unwrap(); - for stream in listener.incoming() { let stream = stream.expect("Read stream error!"); let stream = Arc::new(Mutex::new(stream)); println!("new connection!"); let mut need_continue; - let mut raw_packet = [0u8; RECEIVER_BUFFER_SIZE]; + let raw_packet = [0u8; RECEIVER_BUFFER_SIZE]; + let raw_packet = Arc::new(Mutex::new(raw_packet)); loop { - let sz = rt.block_on(handle_message( + let sz = block_on(Box::pin(handle_message( stream.clone(), if USE_PCIDOE { pcidoe_transport_encap.clone() } else { mctp_transport_encap.clone() }, - &mut raw_packet, - )); + raw_packet.clone(), + ))); - need_continue = rt.block_on(process_socket_message( + need_continue = block_on(Box::pin(process_socket_message( stream.clone(), if USE_PCIDOE { pcidoe_transport_encap.clone() } else { mctp_transport_encap.clone() }, - &raw_packet[..sz], - )); + raw_packet.clone(), + sz, + ))); if !need_continue { // TBD: return or break?? @@ -194,7 +204,7 @@ fn emu_main() { async fn handle_message( stream: Arc>, transport_encap: Arc>, - raw_packet: &mut [u8; RECEIVER_BUFFER_SIZE], + raw_packet: Arc>, ) -> usize { println!("handle_message!"); let socket_io_transport = SocketIoTransport::new(stream); @@ -316,6 +326,8 @@ async fn handle_message( provision_info, ); loop { + let mut raw_packet = raw_packet.lock(); + let raw_packet = raw_packet.deref_mut(); raw_packet.zeroize(); let res = context.process_message(false, 0, raw_packet).await; match res {