@@ -30,15 +30,15 @@ use crate::{
30
30
audio_backends:: { alloc_audio_backend, AudioBackend } ,
31
31
stream:: { Buffer , Error as StreamError , Stream } ,
32
32
virtio_sound:: * ,
33
- ControlMessageKind , Direction , Error , IOMessage , Result , SoundConfig ,
33
+ ControlMessageKind , Direction , Error , IOMessage , QueueIdx , Result , SoundConfig ,
34
34
} ;
35
35
36
36
pub struct VhostUserSoundThread {
37
37
mem : Option < GuestMemoryAtomic < GuestMemoryMmap > > ,
38
38
event_idx : bool ,
39
39
chmaps : Arc < RwLock < Vec < VirtioSoundChmapInfo > > > ,
40
40
jacks : Arc < RwLock < Vec < VirtioSoundJackInfo > > > ,
41
- queue_indexes : Vec < u16 > ,
41
+ queue_indexes : Vec < QueueIdx > ,
42
42
streams : Arc < RwLock < Vec < Stream > > > ,
43
43
streams_no : usize ,
44
44
}
@@ -49,11 +49,11 @@ impl VhostUserSoundThread {
49
49
pub fn new (
50
50
chmaps : Arc < RwLock < Vec < VirtioSoundChmapInfo > > > ,
51
51
jacks : Arc < RwLock < Vec < VirtioSoundJackInfo > > > ,
52
- mut queue_indexes : Vec < u16 > ,
52
+ mut queue_indexes : Vec < QueueIdx > ,
53
53
streams : Arc < RwLock < Vec < Stream > > > ,
54
54
streams_no : usize ,
55
55
) -> Result < Self > {
56
- queue_indexes. sort ( ) ;
56
+ queue_indexes. sort_by_key ( |idx| * idx as u16 ) ;
57
57
58
58
Ok ( Self {
59
59
event_idx : false ,
@@ -70,7 +70,7 @@ impl VhostUserSoundThread {
70
70
let mut queues_per_thread = 0u64 ;
71
71
72
72
for idx in self . queue_indexes . iter ( ) {
73
- queues_per_thread |= 1u64 << idx
73
+ queues_per_thread |= 1u64 << * idx as u16
74
74
}
75
75
76
76
queues_per_thread
@@ -94,7 +94,10 @@ impl VhostUserSoundThread {
94
94
let vring = & vrings
95
95
. get ( device_event as usize )
96
96
. ok_or_else ( || Error :: HandleUnknownEvent ( device_event) ) ?;
97
- let queue_idx = self . queue_indexes [ device_event as usize ] ;
97
+ let queue_idx = self
98
+ . queue_indexes
99
+ . get ( device_event as usize )
100
+ . ok_or_else ( || Error :: HandleUnknownEvent ( device_event) ) ?;
98
101
if self . event_idx {
99
102
// vm-virtio's Queue implementation only checks avail_index
100
103
// once, so to properly support EVENT_IDX we need to keep
@@ -103,11 +106,10 @@ impl VhostUserSoundThread {
103
106
loop {
104
107
vring. disable_notification ( ) . unwrap ( ) ;
105
108
match queue_idx {
106
- CONTROL_QUEUE_IDX => self . process_control ( vring, audio_backend) ,
107
- EVENT_QUEUE_IDX => self . process_event ( vring) ,
108
- TX_QUEUE_IDX => self . process_io ( vring, audio_backend, Direction :: Output ) ,
109
- RX_QUEUE_IDX => self . process_io ( vring, audio_backend, Direction :: Input ) ,
110
- _ => Err ( Error :: HandleUnknownEvent ( queue_idx) . into ( ) ) ,
109
+ QueueIdx :: Control => self . process_control ( vring, audio_backend) ,
110
+ QueueIdx :: Event => self . process_event ( vring) ,
111
+ QueueIdx :: Tx => self . process_io ( vring, audio_backend, Direction :: Output ) ,
112
+ QueueIdx :: Rx => self . process_io ( vring, audio_backend, Direction :: Input ) ,
111
113
} ?;
112
114
if !vring. enable_notification ( ) . unwrap ( ) {
113
115
break ;
@@ -116,11 +118,10 @@ impl VhostUserSoundThread {
116
118
} else {
117
119
// Without EVENT_IDX, a single call is enough.
118
120
match queue_idx {
119
- CONTROL_QUEUE_IDX => self . process_control ( vring, audio_backend) ,
120
- EVENT_QUEUE_IDX => self . process_event ( vring) ,
121
- TX_QUEUE_IDX => self . process_io ( vring, audio_backend, Direction :: Output ) ,
122
- RX_QUEUE_IDX => self . process_io ( vring, audio_backend, Direction :: Input ) ,
123
- _ => Err ( Error :: HandleUnknownEvent ( queue_idx) . into ( ) ) ,
121
+ QueueIdx :: Control => self . process_control ( vring, audio_backend) ,
122
+ QueueIdx :: Event => self . process_event ( vring) ,
123
+ QueueIdx :: Tx => self . process_io ( vring, audio_backend, Direction :: Output ) ,
124
+ QueueIdx :: Rx => self . process_io ( vring, audio_backend, Direction :: Input ) ,
124
125
} ?;
125
126
}
126
127
Ok ( ( ) )
@@ -635,21 +636,21 @@ impl VhostUserSoundBackend {
635
636
RwLock :: new( VhostUserSoundThread :: new(
636
637
chmaps. clone( ) ,
637
638
jacks. clone( ) ,
638
- vec![ CONTROL_QUEUE_IDX , EVENT_QUEUE_IDX ] ,
639
+ vec![ QueueIdx :: Control , QueueIdx :: Event ] ,
639
640
streams. clone( ) ,
640
641
streams_no,
641
642
) ?) ,
642
643
RwLock :: new( VhostUserSoundThread :: new(
643
644
chmaps. clone( ) ,
644
645
jacks. clone( ) ,
645
- vec![ TX_QUEUE_IDX ] ,
646
+ vec![ QueueIdx :: Tx ] ,
646
647
streams. clone( ) ,
647
648
streams_no,
648
649
) ?) ,
649
650
RwLock :: new( VhostUserSoundThread :: new(
650
651
chmaps,
651
652
jacks,
652
- vec![ RX_QUEUE_IDX ] ,
653
+ vec![ QueueIdx :: Rx ] ,
653
654
streams. clone( ) ,
654
655
streams_no,
655
656
) ?) ,
@@ -659,10 +660,10 @@ impl VhostUserSoundBackend {
659
660
chmaps,
660
661
jacks,
661
662
vec![
662
- CONTROL_QUEUE_IDX ,
663
- EVENT_QUEUE_IDX ,
664
- TX_QUEUE_IDX ,
665
- RX_QUEUE_IDX ,
663
+ QueueIdx :: Control ,
664
+ QueueIdx :: Event ,
665
+ QueueIdx :: Tx ,
666
+ QueueIdx :: Rx ,
666
667
] ,
667
668
streams. clone( ) ,
668
669
streams_no,
@@ -832,7 +833,7 @@ mod tests {
832
833
833
834
let chmaps = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
834
835
let jacks = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
835
- let queue_indexes = vec ! [ 1 , 2 , 3 ] ;
836
+ let queue_indexes = vec ! [ QueueIdx :: Event , QueueIdx :: Tx , QueueIdx :: Rx ] ;
836
837
let streams = vec ! [ Stream :: default ( ) ] ;
837
838
let streams_no = streams. len ( ) ;
838
839
let streams = Arc :: new ( RwLock :: new ( streams) ) ;
@@ -927,7 +928,7 @@ mod tests {
927
928
928
929
let chmaps = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
929
930
let jacks = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
930
- let queue_indexes = vec ! [ 1 , 2 , 3 ] ;
931
+ let queue_indexes = vec ! [ QueueIdx :: Event , QueueIdx :: Tx , QueueIdx :: Rx ] ;
931
932
let streams = Arc :: new ( RwLock :: new ( vec ! [ ] ) ) ;
932
933
let streams_no = 0 ;
933
934
let thread =
0 commit comments