@@ -8,6 +8,7 @@ use std::env;
8
8
use std:: ffi:: CStr ;
9
9
#[ cfg( target_os = "linux" ) ]
10
10
use std:: ffi:: CString ;
11
+ use std:: os:: fd:: RawFd ;
11
12
#[ cfg( not( feature = "tee" ) ) ]
12
13
use std:: path:: Path ;
13
14
#[ cfg( feature = "tee" ) ]
@@ -44,6 +45,27 @@ const MAX_ARGS: usize = 4096;
44
45
// Path to the init binary to be executed inside the VM.
45
46
const INIT_PATH : & str = "/init.krun" ;
46
47
48
+ #[ derive( Default ) ]
49
+ struct TsiConfig {
50
+ port_map : Option < HashMap < u16 , u16 > > ,
51
+ }
52
+
53
+ struct PasstConfig {
54
+ fd : RawFd ,
55
+ }
56
+
57
+ enum NetworkConfig {
58
+ Tsi ( TsiConfig ) ,
59
+ Passt ( PasstConfig ) ,
60
+ }
61
+
62
+ impl Default for NetworkConfig {
63
+ /// Default network mode is TSI, for backwards compatibility
64
+ fn default ( ) -> Self {
65
+ NetworkConfig :: Tsi ( Default :: default ( ) )
66
+ }
67
+ }
68
+
47
69
#[ derive( Default ) ]
48
70
struct ContextConfig {
49
71
vmr : VmResources ,
@@ -52,13 +74,13 @@ struct ContextConfig {
52
74
env : Option < String > ,
53
75
args : Option < String > ,
54
76
rlimits : Option < String > ,
77
+ net_cfg : NetworkConfig ,
55
78
#[ cfg( not( feature = "tee" ) ) ]
56
79
fs_cfg : Option < FsDeviceConfig > ,
57
80
#[ cfg( feature = "tee" ) ]
58
81
root_block_cfg : Option < BlockDeviceConfig > ,
59
82
#[ cfg( feature = "tee" ) ]
60
83
data_block_cfg : Option < BlockDeviceConfig > ,
61
- port_map : Option < HashMap < u16 , u16 > > ,
62
84
#[ cfg( feature = "tee" ) ]
63
85
tee_config_file : Option < PathBuf > ,
64
86
}
@@ -149,12 +171,18 @@ impl ContextConfig {
149
171
self . data_block_cfg . clone ( )
150
172
}
151
173
152
- fn set_port_map ( & mut self , port_map : HashMap < u16 , u16 > ) {
153
- self . port_map = Some ( port_map ) ;
174
+ fn set_net_cfg ( & mut self , net_cfg : NetworkConfig ) {
175
+ self . net_cfg = net_cfg ;
154
176
}
155
177
156
- fn get_port_map ( & self ) -> Option < HashMap < u16 , u16 > > {
157
- self . port_map . clone ( )
178
+ fn set_port_map ( & mut self , new_port_map : HashMap < u16 , u16 > ) -> Result < ( ) , ( ) > {
179
+ match & mut self . net_cfg {
180
+ NetworkConfig :: Tsi ( tsi_config) => {
181
+ tsi_config. port_map . replace ( new_port_map) ;
182
+ Ok ( ( ) )
183
+ }
184
+ NetworkConfig :: Passt ( _) => Err ( ( ) ) ,
185
+ }
158
186
}
159
187
160
188
#[ cfg( feature = "tee" ) ]
@@ -461,7 +489,19 @@ pub unsafe extern "C" fn krun_set_data_disk(ctx_id: u32, c_disk_path: *const c_c
461
489
#[ allow( clippy:: missing_safety_doc) ]
462
490
#[ no_mangle]
463
491
pub unsafe extern "C" fn krun_set_passt_fd ( ctx_id : u32 , fd : c_int ) -> i32 {
464
- todo ! ( "krun_set_passt_fd({},{})" , ctx_id, fd) ;
492
+ if fd < 0 {
493
+ return -libc:: EINVAL ;
494
+ }
495
+
496
+ match CTX_MAP . lock ( ) . unwrap ( ) . entry ( ctx_id) {
497
+ Entry :: Occupied ( mut ctx_cfg) => {
498
+ let cfg = ctx_cfg. get_mut ( ) ;
499
+ cfg. set_net_cfg ( NetworkConfig :: Passt ( PasstConfig { fd } ) ) ;
500
+ }
501
+ Entry :: Vacant ( _) => return -libc:: ENOENT ,
502
+ }
503
+
504
+ KRUN_SUCCESS
465
505
}
466
506
467
507
#[ allow( clippy:: missing_safety_doc) ]
@@ -505,7 +545,9 @@ pub unsafe extern "C" fn krun_set_port_map(ctx_id: u32, c_port_map: *const *cons
505
545
match CTX_MAP . lock ( ) . unwrap ( ) . entry ( ctx_id) {
506
546
Entry :: Occupied ( mut ctx_cfg) => {
507
547
let cfg = ctx_cfg. get_mut ( ) ;
508
- cfg. set_port_map ( port_map) ;
548
+ if cfg. set_port_map ( port_map) . is_err ( ) {
549
+ return -libc:: ENOTSUP ;
550
+ }
509
551
}
510
552
Entry :: Vacant ( _) => return -libc:: ENOENT ,
511
553
}
@@ -768,12 +810,19 @@ pub extern "C" fn krun_start_enter(ctx_id: u32) -> i32 {
768
810
return -libc:: EINVAL ;
769
811
}
770
812
771
- let vsock_device_config = VsockDeviceConfig {
772
- vsock_id : "vsock0" . to_string ( ) ,
773
- guest_cid : 3 ,
774
- host_port_map : ctx_cfg. get_port_map ( ) ,
775
- } ;
776
- ctx_cfg. vmr . set_vsock_device ( vsock_device_config) . unwrap ( ) ;
813
+ match ctx_cfg. net_cfg {
814
+ NetworkConfig :: Tsi ( tsi_cfg) => {
815
+ let vsock_device_config = VsockDeviceConfig {
816
+ vsock_id : "vsock0" . to_string ( ) ,
817
+ guest_cid : 3 ,
818
+ host_port_map : tsi_cfg. port_map ,
819
+ } ;
820
+ ctx_cfg. vmr . set_vsock_device ( vsock_device_config) . unwrap ( ) ;
821
+ }
822
+ NetworkConfig :: Passt ( passt_cfg) => {
823
+ todo ! ( "Connect to fd {} and implement networking" , passt_cfg. fd)
824
+ }
825
+ }
777
826
778
827
let _vmm = match vmm:: builder:: build_microvm ( & ctx_cfg. vmr , & mut event_manager) {
779
828
Ok ( vmm) => vmm,
0 commit comments