@@ -29,7 +29,7 @@ use vm_memory::GuestMemoryMmap;
2929
3030use super :: super :: super :: Error as DeviceError ;
3131use super :: super :: {
32- ActivateError , ActivateResult , Queue as VirtQueue , VirtioDevice , VsockError ,
32+ ActivateError , ActivateResult , DeviceState , Queue as VirtQueue , VirtioDevice , VsockError ,
3333 VIRTIO_MMIO_INT_VRING ,
3434} ;
3535use super :: packet:: VsockPacket ;
@@ -51,7 +51,6 @@ pub struct Vsock<B> {
5151 cid : u64 ,
5252 pub ( crate ) queues : Vec < VirtQueue > ,
5353 pub ( crate ) queue_events : Vec < EventFd > ,
54- mem : GuestMemoryMmap ,
5554 pub ( crate ) backend : B ,
5655 avail_features : u64 ,
5756 acked_features : u64 ,
@@ -63,7 +62,7 @@ pub struct Vsock<B> {
6362 // mostly something we wanted to happen for the backend events, to prevent (potentially)
6463 // continuous triggers from happening before the device gets activated.
6564 pub ( crate ) activate_evt : EventFd ,
66- device_activated : bool ,
65+ pub ( crate ) device_state : DeviceState ,
6766}
6867
6968// TODO: Detect / handle queue deadlock:
7776{
7877 pub ( crate ) fn with_queues (
7978 cid : u64 ,
80- mem : GuestMemoryMmap ,
8179 backend : B ,
8280 queues : Vec < VirtQueue > ,
8381 ) -> super :: Result < Vsock < B > > {
@@ -90,24 +88,23 @@ where
9088 cid,
9189 queues,
9290 queue_events,
93- mem,
9491 backend,
9592 avail_features : AVAIL_FEATURES ,
9693 acked_features : 0 ,
9794 interrupt_status : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
9895 interrupt_evt : EventFd :: new ( libc:: EFD_NONBLOCK ) . map_err ( VsockError :: EventFd ) ?,
9996 activate_evt : EventFd :: new ( libc:: EFD_NONBLOCK ) . map_err ( VsockError :: EventFd ) ?,
100- device_activated : false ,
97+ device_state : DeviceState :: Inactive ,
10198 } )
10299 }
103100
104101 /// Create a new virtio-vsock device with the given VM CID and vsock backend.
105- pub fn new ( cid : u64 , mem : GuestMemoryMmap , backend : B ) -> super :: Result < Vsock < B > > {
102+ pub fn new ( cid : u64 , backend : B ) -> super :: Result < Vsock < B > > {
106103 let queues: Vec < VirtQueue > = defs:: QUEUE_SIZES
107104 . iter ( )
108105 . map ( |& max_size| VirtQueue :: new ( max_size) )
109106 . collect ( ) ;
110- Self :: with_queues ( cid, mem , backend, queues)
107+ Self :: with_queues ( cid, backend, queues)
111108 }
112109
113110 pub fn cid ( & self ) -> u64 {
@@ -131,10 +128,15 @@ where
131128 /// otherwise.
132129 pub fn process_rx ( & mut self ) -> bool {
133130 debug ! ( "vsock: process_rx()" ) ;
131+ let mem = match self . device_state {
132+ DeviceState :: Activated ( ref mem) => mem,
133+ // This should never happen, it's been already validated in the event handler.
134+ DeviceState :: Inactive => return false ,
135+ } ;
134136
135137 let mut have_used = false ;
136138
137- while let Some ( head) = self . queues [ RXQ_INDEX ] . pop ( & self . mem ) {
139+ while let Some ( head) = self . queues [ RXQ_INDEX ] . pop ( mem) {
138140 let used_len = match VsockPacket :: from_rx_virtq_head ( & head) {
139141 Ok ( mut pkt) => {
140142 if self . backend . recv_pkt ( & mut pkt) . is_ok ( ) {
@@ -153,7 +155,7 @@ where
153155 } ;
154156
155157 have_used = true ;
156- self . queues [ RXQ_INDEX ] . add_used ( & self . mem , head. index , used_len) ;
158+ self . queues [ RXQ_INDEX ] . add_used ( mem, head. index , used_len) ;
157159 }
158160
159161 have_used
@@ -164,16 +166,21 @@ where
164166 /// ring, and `false` otherwise.
165167 pub fn process_tx ( & mut self ) -> bool {
166168 debug ! ( "vsock::process_tx()" ) ;
169+ let mem = match self . device_state {
170+ DeviceState :: Activated ( ref mem) => mem,
171+ // This should never happen, it's been already validated in the event handler.
172+ DeviceState :: Inactive => return false ,
173+ } ;
167174
168175 let mut have_used = false ;
169176
170- while let Some ( head) = self . queues [ TXQ_INDEX ] . pop ( & self . mem ) {
177+ while let Some ( head) = self . queues [ TXQ_INDEX ] . pop ( mem) {
171178 let pkt = match VsockPacket :: from_tx_virtq_head ( & head) {
172179 Ok ( pkt) => pkt,
173180 Err ( e) => {
174181 error ! ( "vsock: error reading TX packet: {:?}" , e) ;
175182 have_used = true ;
176- self . queues [ TXQ_INDEX ] . add_used ( & self . mem , head. index , 0 ) ;
183+ self . queues [ TXQ_INDEX ] . add_used ( mem, head. index , 0 ) ;
177184 continue ;
178185 }
179186 } ;
@@ -184,7 +191,7 @@ where
184191 }
185192
186193 have_used = true ;
187- self . queues [ TXQ_INDEX ] . add_used ( & self . mem , head. index , 0 ) ;
194+ self . queues [ TXQ_INDEX ] . add_used ( mem, head. index , 0 ) ;
188195 }
189196
190197 have_used
@@ -252,7 +259,7 @@ where
252259 ) ;
253260 }
254261
255- fn activate ( & mut self , _ : GuestMemoryMmap ) -> ActivateResult {
262+ fn activate ( & mut self , mem : GuestMemoryMmap ) -> ActivateResult {
256263 if self . queues . len ( ) != defs:: NUM_QUEUES {
257264 error ! (
258265 "Cannot perform activate. Expected {} queue(s), got {}" ,
@@ -267,13 +274,16 @@ where
267274 return Err ( ActivateError :: BadActivate ) ;
268275 }
269276
270- self . device_activated = true ;
277+ self . device_state = DeviceState :: Activated ( mem ) ;
271278
272279 Ok ( ( ) )
273280 }
274281
275282 fn is_activated ( & self ) -> bool {
276- self . device_activated
283+ match self . device_state {
284+ DeviceState :: Inactive => false ,
285+ DeviceState :: Activated ( _) => true ,
286+ }
277287 }
278288}
279289
0 commit comments