Skip to content

Commit e202171

Browse files
committed
nitro: Add API to configure enclave start flags
Signed-off-by: Tyler Fanelli <[email protected]>
1 parent 0a6ef84 commit e202171

File tree

6 files changed

+58
-3
lines changed

6 files changed

+58
-3
lines changed

Cargo.lock

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

examples/nitro.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ int main(int argc, char *const argv[])
154154

155155
}
156156

157+
// Configure the nitro enclave to run in debug mode.
158+
if (err = krun_nitro_set_start_flags(ctx_id, KRUN_NITRO_START_FLAG_DEBUG)) {
159+
errno = -err;
160+
perror("Error configuring nitro enclave start flags");
161+
return -1;
162+
}
163+
157164
// Create and initialize UNIX IPC socket for reading enclave output.
158165
sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
159166
if (sock_fd < 0) {

include/libkrun.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ int32_t krun_split_irqchip(uint32_t ctx_id, bool enable);
598598
int32_t krun_nitro_set_image(uint32_t ctx_id, const char *image_path,
599599
uint32_t image_type);
600600

601+
#define KRUN_NITRO_START_FLAG_DEBUG (1 << 0)
602+
/**
603+
* Configure a Nitro Enclave's start flags.
604+
*
605+
* Arguments:
606+
* "ctx_id" - the configuration context ID.
607+
* "start_flags" - Start flags.
608+
*/
609+
int32_t krun_nitro_set_start_flags(uint32_t ctx_id, uint64_t start_flags);
610+
601611
/**
602612
* Starts and enters the microVM with the configured parameters. The VMM will attempt to take over
603613
* stdin/stdout to manage them on behalf of the process running inside the isolated environment,

src/libkrun/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ efi = [ "blk", "net" ]
1414
gpu = []
1515
snd = []
1616
virgl_resource_map2 = []
17-
nitro = [ "dep:nitro" ]
17+
nitro = [ "dep:nitro", "dep:nitro-enclaves" ]
1818

1919
[dependencies]
2020
crossbeam-channel = "0.5"
@@ -36,6 +36,7 @@ hvf = { path = "../hvf" }
3636
[target.'cfg(target_os = "linux")'.dependencies]
3737
kvm-bindings = { version = ">=0.11", features = ["fam-wrappers"] }
3838
kvm-ioctls = ">=0.21"
39+
nitro-enclaves = { version = "0.2.0", optional = true }
3940
vm-memory = ">=0.13"
4041

4142
[lib]

src/libkrun/src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ use libc::{
7272
#[cfg(feature = "nitro")]
7373
use nitro::NitroEnclave;
7474

75+
#[cfg(feature = "nitro")]
76+
use nitro_enclaves::launch::StartFlags;
77+
7578
// Value returned on success. We use libc's errors otherwise.
7679
const KRUN_SUCCESS: i32 = 0;
7780
// Maximum number of arguments/environment variables we allow
@@ -175,6 +178,8 @@ struct ContextConfig {
175178
vmm_gid: Option<libc::gid_t>,
176179
#[cfg(feature = "nitro")]
177180
nitro_image_path: Option<PathBuf>,
181+
#[cfg(feature = "nitro")]
182+
nitro_start_flags: StartFlags,
178183
}
179184

180185
impl ContextConfig {
@@ -324,6 +329,11 @@ impl ContextConfig {
324329
fn set_nitro_image(&mut self, image_path: PathBuf) {
325330
self.nitro_image_path = Some(image_path);
326331
}
332+
333+
#[cfg(feature = "nitro")]
334+
fn set_nitro_start_flags(&mut self, start_flags: StartFlags) {
335+
self.nitro_start_flags = start_flags;
336+
}
327337
}
328338

329339
#[cfg(feature = "nitro")]
@@ -378,6 +388,7 @@ impl TryFrom<ContextConfig> for NitroEnclave {
378388
mem_size_mib,
379389
vcpus,
380390
ipc_stream,
391+
start_flags: ctx.nitro_start_flags,
381392
})
382393
}
383394
}
@@ -1455,6 +1466,30 @@ pub unsafe extern "C" fn krun_nitro_set_image(ctx_id: u32, c_image_filepath: *co
14551466
KRUN_SUCCESS
14561467
}
14571468

1469+
#[cfg(feature = "nitro")]
1470+
#[allow(clippy::missing_safety_doc)]
1471+
#[no_mangle]
1472+
pub unsafe extern "C" fn krun_nitro_set_start_flags(ctx_id: u32, start_flags: u64) -> i32 {
1473+
let mut flags = StartFlags::empty();
1474+
1475+
// Only debug mode is supported at the moment. To avoid doing conversion and
1476+
// checking if the "start_flags" argument is valid, set the flags to debug mode
1477+
// if the "start_flags" argument is greater than zero.
1478+
if start_flags > 0 {
1479+
flags |= StartFlags::DEBUG;
1480+
}
1481+
1482+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
1483+
Entry::Occupied(mut ctx_cfg) => {
1484+
let cfg = ctx_cfg.get_mut();
1485+
cfg.set_nitro_start_flags(flags);
1486+
}
1487+
Entry::Vacant(_) => return -libc::ENOENT,
1488+
}
1489+
1490+
KRUN_SUCCESS
1491+
}
1492+
14581493
#[no_mangle]
14591494
#[allow(unreachable_code)]
14601495
pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {

src/nitro/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ const SO_VM_SOCKETS_CONNECT_TIMEOUT: i32 = 6;
3636
const HEART_BEAT: u8 = 0xb7;
3737

3838
/// Nitro Enclave data.
39-
#[derive(Debug)]
4039
pub struct NitroEnclave {
4140
/// Enclave image.
4241
pub image: File,
@@ -46,6 +45,8 @@ pub struct NitroEnclave {
4645
pub vcpus: u8,
4746
/// Path of vsock for initial enclave communication.
4847
pub ipc_stream: UnixStream,
48+
/// Enclave start flags.
49+
pub start_flags: StartFlags,
4950
}
5051

5152
impl NitroEnclave {
@@ -66,7 +67,7 @@ impl NitroEnclave {
6667
let listener = VsockListener::bind(&sockaddr).map_err(NitroError::HeartbeatBind)?;
6768

6869
let cid = launcher
69-
.start(StartFlags::DEBUG, None)
70+
.start(self.start_flags, None)
7071
.map_err(NitroError::VmStart)?;
7172

7273
// Safe to unwrap.

0 commit comments

Comments
 (0)