Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
add feature to control async runtime.
Browse files Browse the repository at this point in the history
fix #152

Signed-off-by: Yang, Longlong <[email protected]>
  • Loading branch information
longlongyang committed Nov 26, 2023
1 parent 54ab460 commit 1dd8bf0
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 52 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

34 changes: 18 additions & 16 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,36 +134,38 @@ 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

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`.
Expand Down Expand Up @@ -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:
Expand All @@ -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 <test_func_name>`
Expand Down
10 changes: 5 additions & 5 deletions sh_script/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ 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_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"
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,async-executor"
RUN_RESPONDER_MUTAUTH_FEATURES="${RUN_RESPONDER_FEATURES},mut-auth,async-executor"
RUN_RESPONDER_MANDATORY_MUTAUTH_FEATURES="${RUN_RESPONDER_FEATURES},mandatory-mut-auth,async-executor"

run_with_spdm_emu() {
echo "Running with spdm-emu..."
Expand Down
6 changes: 5 additions & 1 deletion test/spdm-emu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
26 changes: 26 additions & 0 deletions test/spdm-emu/src/async_runtime.rs
Original file line number Diff line number Diff line change
@@ -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<T>(future: Pin<Box<dyn Future<Output = T> + '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!");
}
}
1 change: 1 addition & 0 deletions test/spdm-emu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#![forbid(unsafe_code)]

pub mod async_runtime;
pub mod crypto;
pub mod crypto_callback;
pub mod secret_impl_sample;
Expand Down
5 changes: 4 additions & 1 deletion test/spdm-requester-emu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
24 changes: 12 additions & 12 deletions test/spdm-requester-emu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Mutex<dyn SpdmDeviceIo + Send + Sync>> =
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() {
Expand Down
5 changes: 4 additions & 1 deletion test/spdm-responder-emu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
44 changes: 28 additions & 16 deletions test/spdm-responder-emu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Mutex<TcpStream>>,
transport_encap: Arc<Mutex<dyn SpdmTransportEncap + Send + Sync>>,
buffer: &[u8],
buffer: Arc<Mutex<[u8; RECEIVER_BUFFER_SIZE]>>,
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() {
Expand All @@ -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
Expand Down Expand Up @@ -153,35 +164,34 @@ fn emu_main() {
let mctp_transport_encap: Arc<Mutex<(dyn SpdmTransportEncap + Send + Sync)>> =
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??
Expand All @@ -194,7 +204,7 @@ fn emu_main() {
async fn handle_message(
stream: Arc<Mutex<TcpStream>>,
transport_encap: Arc<Mutex<dyn SpdmTransportEncap + Send + Sync>>,
raw_packet: &mut [u8; RECEIVER_BUFFER_SIZE],
raw_packet: Arc<Mutex<[u8; RECEIVER_BUFFER_SIZE]>>,
) -> usize {
println!("handle_message!");
let socket_io_transport = SocketIoTransport::new(stream);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1dd8bf0

Please sign in to comment.