Skip to content

Commit 557945f

Browse files
committed
KBS/perf: promote the concurrency performance of KBS
Currently we are using a global Mutex to protect the only one attestation service client from unsafe thread sync & send. This brings performance bottle neck. This commit brings some optimization to promote the performance and stability. 1. Abondon the global Mutex of the attestation service and change the API definition of Attest to non-mut. This would let the developers to handle the concurrency safe inside the concrete attestation-service inside. In this way, we prevent to lock the whole process logic. 2. Bring in a gRPC client pool to grpc-coco-as mode. This will help to avoid errors that caused by runing out all the temporaty ports provided by OS. 3. Replace the Mutex of session map with a concurrency-safe HashMap to avoid bottle neck. Fixes #256 Signed-off-by: Xynnn007 <[email protected]> Signed-off-by: Biao Lu <[email protected]>
1 parent f1961f9 commit 557945f

File tree

13 files changed

+268
-103
lines changed

13 files changed

+268
-103
lines changed

Cargo.lock

Lines changed: 115 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

kbs/config/kbs-config-grpc.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ insecure_api = true
33

44
[grpc_config]
55
as_addr = "http://127.0.0.1:50004"
6+
pool_size = 200

kbs/src/api/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ opa = ["policy"]
1515
coco-as = ["as"]
1616
coco-as-builtin = ["coco-as", "attestation-service/default"]
1717
coco-as-builtin-no-verifier = ["coco-as", "attestation-service/rvps-builtin"]
18-
coco-as-grpc = ["coco-as", "tonic", "tonic-build", "prost"]
18+
coco-as-grpc = ["coco-as", "mobc", "tonic", "tonic-build", "prost"]
1919
intel-trust-authority-as = ["as", "reqwest", "jsonwebtoken"]
2020
rustls = ["actix-web/rustls", "dep:rustls", "dep:rustls-pemfile"]
2121
openssl = ["actix-web/openssl", "dep:openssl"]
@@ -37,12 +37,14 @@ jwt-simple = "0.11.6"
3737
kbs-types.workspace = true
3838
lazy_static = "1.4.0"
3939
log.workspace = true
40+
mobc = { version = "0.8.3", optional = true }
4041
prost = { version = "0.11", optional = true }
4142
rand = "0.8.5"
4243
reqwest = { version = "0.11", features = ["json"], optional = true }
4344
rsa = { version = "0.9.2", optional = true, features = ["sha2"] }
4445
rustls = { version = "0.20.8", optional = true }
4546
rustls-pemfile = { version = "1.0.4", optional = true }
47+
scc = "2"
4648
semver = "1.0.16"
4749
serde = { version = "1.0", features = ["derive"] }
4850
serde_json.workspace = true

kbs/src/api/src/attestation/coco/builtin.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,29 @@ use attestation_service::{
1111
};
1212
use kbs_types::{Attestation, Tee};
1313
use serde_json::json;
14+
use tokio::sync::RwLock;
1415

15-
pub struct Native {
16-
inner: AttestationService,
16+
pub struct BuiltInCoCoAs {
17+
inner: RwLock<AttestationService>,
1718
}
1819

1920
#[async_trait]
20-
impl Attest for Native {
21-
async fn set_policy(&mut self, input: &[u8]) -> Result<()> {
21+
impl Attest for BuiltInCoCoAs {
22+
async fn set_policy(&self, input: &[u8]) -> Result<()> {
2223
let request: SetPolicyInput =
2324
serde_json::from_slice(input).context("parse SetPolicyInput")?;
24-
self.inner.set_policy(request).await
25+
self.inner.write().await.set_policy(request).await
2526
}
2627

27-
async fn verify(&mut self, tee: Tee, nonce: &str, attestation: &str) -> Result<String> {
28+
async fn verify(&self, tee: Tee, nonce: &str, attestation: &str) -> Result<String> {
2829
let attestation: Attestation = serde_json::from_str(attestation)?;
2930

3031
// TODO: align with the guest-components/kbs-protocol side.
3132
let runtime_data_plaintext = json!({"tee-pubkey": attestation.tee_pubkey, "nonce": nonce});
3233

3334
self.inner
35+
.read()
36+
.await
3437
.evaluate(
3538
attestation.tee_evidence.into_bytes(),
3639
tee,
@@ -44,10 +47,9 @@ impl Attest for Native {
4447
}
4548
}
4649

47-
impl Native {
48-
pub async fn new(config: &AsConfig) -> Result<Self> {
49-
Ok(Self {
50-
inner: AttestationService::new(config.clone()).await?,
51-
})
50+
impl BuiltInCoCoAs {
51+
pub async fn new(config: AsConfig) -> Result<Self> {
52+
let inner = RwLock::new(AttestationService::new(config).await?);
53+
Ok(Self { inner })
5254
}
5355
}

0 commit comments

Comments
 (0)