@@ -10,6 +10,7 @@ mod device;
10
10
mod event_handler;
11
11
mod packet;
12
12
pub mod persist;
13
+ pub mod test_utils;
13
14
mod unix;
14
15
15
16
use std:: os:: unix:: io:: AsRawFd ;
@@ -160,182 +161,3 @@ pub trait VsockChannel {
160
161
/// Currently, the only implementation we have is `crate::virtio::unix::muxer::VsockMuxer`, which
161
162
/// translates guest-side vsock connections to host-side Unix domain socket connections.
162
163
pub trait VsockBackend : VsockChannel + VsockEpollListener + Send { }
163
-
164
- #[ cfg( test) ]
165
- pub ( crate ) mod tests {
166
- use super :: device:: { Vsock , RXQ_INDEX , TXQ_INDEX } ;
167
- use super :: packet:: VSOCK_PKT_HDR_SIZE ;
168
- use super :: * ;
169
-
170
- use std:: os:: unix:: io:: { AsRawFd , RawFd } ;
171
- use utils:: eventfd:: EventFd ;
172
-
173
- use crate :: virtio:: queue:: tests:: VirtQueue as GuestQ ;
174
- use crate :: virtio:: { VirtioDevice , VIRTQ_DESC_F_NEXT , VIRTQ_DESC_F_WRITE } ;
175
- use utils:: epoll:: EpollEvent ;
176
- use vm_memory:: { GuestAddress , GuestMemoryMmap } ;
177
-
178
- pub struct TestBackend {
179
- pub evfd : EventFd ,
180
- pub rx_err : Option < VsockError > ,
181
- pub tx_err : Option < VsockError > ,
182
- pub pending_rx : bool ,
183
- pub rx_ok_cnt : usize ,
184
- pub tx_ok_cnt : usize ,
185
- pub evset : Option < EventSet > ,
186
- }
187
-
188
- impl TestBackend {
189
- pub fn new ( ) -> Self {
190
- Self {
191
- evfd : EventFd :: new ( libc:: EFD_NONBLOCK ) . unwrap ( ) ,
192
- rx_err : None ,
193
- tx_err : None ,
194
- pending_rx : false ,
195
- rx_ok_cnt : 0 ,
196
- tx_ok_cnt : 0 ,
197
- evset : None ,
198
- }
199
- }
200
- pub fn set_rx_err ( & mut self , err : Option < VsockError > ) {
201
- self . rx_err = err;
202
- }
203
- pub fn set_tx_err ( & mut self , err : Option < VsockError > ) {
204
- self . tx_err = err;
205
- }
206
- pub fn set_pending_rx ( & mut self , prx : bool ) {
207
- self . pending_rx = prx;
208
- }
209
- }
210
-
211
- impl VsockChannel for TestBackend {
212
- fn recv_pkt ( & mut self , _pkt : & mut VsockPacket ) -> Result < ( ) > {
213
- let cool_buf = [ 0xDu8 , 0xE , 0xA , 0xD , 0xB , 0xE , 0xE , 0xF ] ;
214
- match self . rx_err . take ( ) {
215
- None => {
216
- if let Some ( buf) = _pkt. buf_mut ( ) {
217
- for i in 0 ..buf. len ( ) {
218
- buf[ i] = cool_buf[ i % cool_buf. len ( ) ] ;
219
- }
220
- }
221
- self . rx_ok_cnt += 1 ;
222
- Ok ( ( ) )
223
- }
224
- Some ( e) => Err ( e) ,
225
- }
226
- }
227
-
228
- fn send_pkt ( & mut self , _pkt : & VsockPacket ) -> Result < ( ) > {
229
- match self . tx_err . take ( ) {
230
- None => {
231
- self . tx_ok_cnt += 1 ;
232
- Ok ( ( ) )
233
- }
234
- Some ( e) => Err ( e) ,
235
- }
236
- }
237
-
238
- fn has_pending_rx ( & self ) -> bool {
239
- self . pending_rx
240
- }
241
- }
242
-
243
- impl AsRawFd for TestBackend {
244
- fn as_raw_fd ( & self ) -> RawFd {
245
- self . evfd . as_raw_fd ( )
246
- }
247
- }
248
-
249
- impl VsockEpollListener for TestBackend {
250
- fn get_polled_evset ( & self ) -> EventSet {
251
- EventSet :: IN
252
- }
253
- fn notify ( & mut self , evset : EventSet ) {
254
- self . evset = Some ( evset) ;
255
- }
256
- }
257
- impl VsockBackend for TestBackend { }
258
-
259
- pub struct TestContext {
260
- pub cid : u64 ,
261
- pub mem : GuestMemoryMmap ,
262
- pub mem_size : usize ,
263
- pub device : Vsock < TestBackend > ,
264
- }
265
-
266
- impl TestContext {
267
- pub fn new ( ) -> Self {
268
- const CID : u64 = 52 ;
269
- const MEM_SIZE : usize = 1024 * 1024 * 128 ;
270
- let mem = GuestMemoryMmap :: from_ranges ( & [ ( GuestAddress ( 0 ) , MEM_SIZE ) ] ) . unwrap ( ) ;
271
- Self {
272
- cid : CID ,
273
- mem,
274
- mem_size : MEM_SIZE ,
275
- device : Vsock :: new ( CID , TestBackend :: new ( ) ) . unwrap ( ) ,
276
- }
277
- }
278
-
279
- pub fn create_event_handler_context ( & self ) -> EventHandlerContext {
280
- const QSIZE : u16 = 2 ;
281
-
282
- let guest_rxvq = GuestQ :: new ( GuestAddress ( 0x0010_0000 ) , & self . mem , QSIZE as u16 ) ;
283
- let guest_txvq = GuestQ :: new ( GuestAddress ( 0x0020_0000 ) , & self . mem , QSIZE as u16 ) ;
284
- let guest_evvq = GuestQ :: new ( GuestAddress ( 0x0030_0000 ) , & self . mem , QSIZE as u16 ) ;
285
- let rxvq = guest_rxvq. create_queue ( ) ;
286
- let txvq = guest_txvq. create_queue ( ) ;
287
- let evvq = guest_evvq. create_queue ( ) ;
288
-
289
- // Set up one available descriptor in the RX queue.
290
- guest_rxvq. dtable [ 0 ] . set (
291
- 0x0040_0000 ,
292
- VSOCK_PKT_HDR_SIZE as u32 ,
293
- VIRTQ_DESC_F_WRITE | VIRTQ_DESC_F_NEXT ,
294
- 1 ,
295
- ) ;
296
- guest_rxvq. dtable [ 1 ] . set ( 0x0040_1000 , 4096 , VIRTQ_DESC_F_WRITE , 0 ) ;
297
-
298
- guest_rxvq. avail . ring [ 0 ] . set ( 0 ) ;
299
- guest_rxvq. avail . idx . set ( 1 ) ;
300
-
301
- // Set up one available descriptor in the TX queue.
302
- guest_txvq. dtable [ 0 ] . set ( 0x0050_0000 , VSOCK_PKT_HDR_SIZE as u32 , VIRTQ_DESC_F_NEXT , 1 ) ;
303
- guest_txvq. dtable [ 1 ] . set ( 0x0050_1000 , 4096 , 0 , 0 ) ;
304
- guest_txvq. avail . ring [ 0 ] . set ( 0 ) ;
305
- guest_txvq. avail . idx . set ( 1 ) ;
306
-
307
- let queues = vec ! [ rxvq, txvq, evvq] ;
308
- EventHandlerContext {
309
- guest_rxvq,
310
- guest_txvq,
311
- guest_evvq,
312
- device : Vsock :: with_queues ( self . cid , TestBackend :: new ( ) , queues) . unwrap ( ) ,
313
- }
314
- }
315
- }
316
-
317
- pub struct EventHandlerContext < ' a > {
318
- pub device : Vsock < TestBackend > ,
319
- pub guest_rxvq : GuestQ < ' a > ,
320
- pub guest_txvq : GuestQ < ' a > ,
321
- pub guest_evvq : GuestQ < ' a > ,
322
- }
323
-
324
- impl < ' a > EventHandlerContext < ' a > {
325
- pub fn mock_activate ( & mut self , mem : GuestMemoryMmap ) {
326
- // Artificially activate the device.
327
- self . device . activate ( mem) . unwrap ( ) ;
328
- }
329
-
330
- pub fn signal_txq_event ( & mut self ) {
331
- self . device . queue_events [ TXQ_INDEX ] . write ( 1 ) . unwrap ( ) ;
332
- self . device
333
- . handle_txq_event ( & EpollEvent :: new ( EventSet :: IN , 0 ) ) ;
334
- }
335
- pub fn signal_rxq_event ( & mut self ) {
336
- self . device . queue_events [ RXQ_INDEX ] . write ( 1 ) . unwrap ( ) ;
337
- self . device
338
- . handle_rxq_event ( & EpollEvent :: new ( EventSet :: IN , 0 ) ) ;
339
- }
340
- }
341
- }
0 commit comments