Skip to content

Commit 5f4dfb7

Browse files
committed
nitro: Add API to configure enclave start flags
Signed-off-by: Tyler Fanelli <[email protected]>
1 parent 951cd85 commit 5f4dfb7

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
@@ -636,6 +636,16 @@ int32_t krun_split_irqchip(uint32_t ctx_id, bool enable);
636636
int32_t krun_nitro_set_image(uint32_t ctx_id, const char *image_path,
637637
uint32_t image_type);
638638

639+
#define KRUN_NITRO_START_FLAG_DEBUG (1 << 0)
640+
/**
641+
* Configure a Nitro Enclave's start flags.
642+
*
643+
* Arguments:
644+
* "ctx_id" - the configuration context ID.
645+
* "start_flags" - Start flags.
646+
*/
647+
int32_t krun_nitro_set_start_flags(uint32_t ctx_id, uint64_t start_flags);
648+
639649
/**
640650
* Starts and enters the microVM with the configured parameters. The VMM will attempt to take over
641651
* 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.15"
@@ -36,6 +36,7 @@ hvf = { path = "../hvf" }
3636
kvm-bindings = { version = ">=0.11", features = ["fam-wrappers"] }
3737
kvm-ioctls = ">=0.21"
3838
nitro = { path = "../nitro", optional = true }
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
@@ -56,6 +56,9 @@ use vmm::vmm_config::vsock::VsockDeviceConfig;
5656
#[cfg(feature = "nitro")]
5757
use nitro::NitroEnclave;
5858

59+
#[cfg(feature = "nitro")]
60+
use nitro_enclaves::launch::StartFlags;
61+
5962
// Value returned on success. We use libc's errors otherwise.
6063
const KRUN_SUCCESS: i32 = 0;
6164
// Maximum number of arguments/environment variables we allow
@@ -159,6 +162,8 @@ struct ContextConfig {
159162
vmm_gid: Option<libc::gid_t>,
160163
#[cfg(feature = "nitro")]
161164
nitro_image_path: Option<PathBuf>,
165+
#[cfg(feature = "nitro")]
166+
nitro_start_flags: StartFlags,
162167
}
163168

164169
impl ContextConfig {
@@ -308,6 +313,11 @@ impl ContextConfig {
308313
fn set_nitro_image(&mut self, image_path: PathBuf) {
309314
self.nitro_image_path = Some(image_path);
310315
}
316+
317+
#[cfg(feature = "nitro")]
318+
fn set_nitro_start_flags(&mut self, start_flags: StartFlags) {
319+
self.nitro_start_flags = start_flags;
320+
}
311321
}
312322

313323
#[cfg(feature = "nitro")]
@@ -362,6 +372,7 @@ impl TryFrom<ContextConfig> for NitroEnclave {
362372
mem_size_mib,
363373
vcpus,
364374
ipc_stream,
375+
start_flags: ctx.nitro_start_flags,
365376
})
366377
}
367378
}
@@ -1493,6 +1504,30 @@ pub unsafe extern "C" fn krun_nitro_set_image(ctx_id: u32, c_image_filepath: *co
14931504
KRUN_SUCCESS
14941505
}
14951506

1507+
#[cfg(feature = "nitro")]
1508+
#[allow(clippy::missing_safety_doc)]
1509+
#[no_mangle]
1510+
pub unsafe extern "C" fn krun_nitro_set_start_flags(ctx_id: u32, start_flags: u64) -> i32 {
1511+
let mut flags = StartFlags::empty();
1512+
1513+
// Only debug mode is supported at the moment. To avoid doing conversion and
1514+
// checking if the "start_flags" argument is valid, set the flags to debug mode
1515+
// if the "start_flags" argument is greater than zero.
1516+
if start_flags > 0 {
1517+
flags |= StartFlags::DEBUG;
1518+
}
1519+
1520+
match CTX_MAP.lock().unwrap().entry(ctx_id) {
1521+
Entry::Occupied(mut ctx_cfg) => {
1522+
let cfg = ctx_cfg.get_mut();
1523+
cfg.set_nitro_start_flags(flags);
1524+
}
1525+
Entry::Vacant(_) => return -libc::ENOENT,
1526+
}
1527+
1528+
KRUN_SUCCESS
1529+
}
1530+
14961531
#[no_mangle]
14971532
#[allow(unreachable_code)]
14981533
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
@@ -38,7 +38,6 @@ const SO_VM_SOCKETS_CONNECT_TIMEOUT: i32 = 6;
3838
const HEART_BEAT: u8 = 0xb7;
3939

4040
/// Nitro Enclave data.
41-
#[derive(Debug)]
4241
pub struct NitroEnclave {
4342
/// Enclave image.
4443
pub image: File,
@@ -48,6 +47,8 @@ pub struct NitroEnclave {
4847
pub vcpus: u8,
4948
/// Path of vsock for initial enclave communication.
5049
pub ipc_stream: UnixStream,
50+
/// Enclave start flags.
51+
pub start_flags: StartFlags,
5152
}
5253

5354
impl NitroEnclave {
@@ -68,7 +69,7 @@ impl NitroEnclave {
6869
let listener = VsockListener::bind(&sockaddr).map_err(NitroError::HeartbeatBind)?;
6970

7071
let cid = launcher
71-
.start(StartFlags::DEBUG, None)
72+
.start(self.start_flags, None)
7273
.map_err(NitroError::VmStart)?;
7374

7475
// Safe to unwrap.

0 commit comments

Comments
 (0)