Skip to content

Commit ae31c18

Browse files
committed
vsock: Move iter outside of loop in process_tx_queue.
the iter() function is used for produce a queue iterator to iterate over the descriptors. But usually, it shouldn't be in the while loop, which might brings more unnecessary overhead. So move iter outside of the while loop. Signed-off-by: Li Zebin <[email protected]>
1 parent be8a7ac commit ae31c18

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

crates/vsock/src/vhu_vsock_thread.rs

Lines changed: 60 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -621,68 +621,75 @@ impl VhostUserVsockThread {
621621
None => return Err(Error::NoMemoryConfigured),
622622
};
623623

624-
while let Some(mut avail_desc) = vring
625-
.get_mut()
626-
.get_queue_mut()
627-
.iter(atomic_mem.memory())
628-
.map_err(|_| Error::IterateQueue)?
629-
.next()
630-
{
631-
used_any = true;
632-
let mem = atomic_mem.clone().memory();
633-
634-
let head_idx = avail_desc.head_index();
635-
let pkt = match VsockPacket::from_tx_virtq_chain(
636-
mem.deref(),
637-
&mut avail_desc,
638-
self.tx_buffer_size,
639-
) {
640-
Ok(pkt) => pkt,
641-
Err(e) => {
642-
dbg!("vsock: error reading TX packet: {:?}", e);
643-
continue;
644-
}
645-
};
646-
647-
if self.thread_backend.send_pkt(&pkt).is_err() {
648-
vring
649-
.get_mut()
650-
.get_queue_mut()
651-
.iter(mem)
652-
.unwrap()
653-
.go_to_previous_position();
654-
break;
655-
}
624+
let mut vring_mut = vring.get_mut();
625+
626+
let queue = vring_mut.get_queue_mut();
656627

657-
// TODO: Check if the protocol requires read length to be correct
658-
let used_len = 0;
628+
let mut iter_has_elemnt = true;
629+
while iter_has_elemnt {
630+
let queue_iter = queue
631+
.iter(atomic_mem.memory())
632+
.map_err(|_| Error::IterateQueue)?;
659633

660-
let vring = vring.clone();
661-
let event_idx = self.event_idx;
634+
iter_has_elemnt = false;
635+
for mut avail_desc in queue_iter {
636+
iter_has_elemnt = true;
637+
used_any = true;
638+
let mem = atomic_mem.clone().memory();
662639

663-
self.pool.spawn_ok(async move {
664-
if event_idx {
665-
if vring.add_used(head_idx, used_len as u32).is_err() {
666-
warn!("Could not return used descriptors to ring");
640+
let head_idx = avail_desc.head_index();
641+
let pkt = match VsockPacket::from_tx_virtq_chain(
642+
mem.deref(),
643+
&mut avail_desc,
644+
self.tx_buffer_size,
645+
) {
646+
Ok(pkt) => pkt,
647+
Err(e) => {
648+
dbg!("vsock: error reading TX packet: {:?}", e);
649+
continue;
667650
}
668-
match vring.needs_notification() {
669-
Err(_) => {
670-
warn!("Could not check if queue needs to be notified");
671-
vring.signal_used_queue().unwrap();
651+
};
652+
653+
if self.thread_backend.send_pkt(&pkt).is_err() {
654+
vring
655+
.get_mut()
656+
.get_queue_mut()
657+
.iter(mem)
658+
.unwrap()
659+
.go_to_previous_position();
660+
break;
661+
}
662+
663+
// TODO: Check if the protocol requires read length to be correct
664+
let used_len = 0;
665+
666+
let vring = vring.clone();
667+
let event_idx = self.event_idx;
668+
669+
self.pool.spawn_ok(async move {
670+
if event_idx {
671+
if vring.add_used(head_idx, used_len as u32).is_err() {
672+
warn!("Could not return used descriptors to ring");
672673
}
673-
Ok(needs_notification) => {
674-
if needs_notification {
674+
match vring.needs_notification() {
675+
Err(_) => {
676+
warn!("Could not check if queue needs to be notified");
675677
vring.signal_used_queue().unwrap();
676678
}
679+
Ok(needs_notification) => {
680+
if needs_notification {
681+
vring.signal_used_queue().unwrap();
682+
}
683+
}
677684
}
685+
} else {
686+
if vring.add_used(head_idx, used_len as u32).is_err() {
687+
warn!("Could not return used descriptors to ring");
688+
}
689+
vring.signal_used_queue().unwrap();
678690
}
679-
} else {
680-
if vring.add_used(head_idx, used_len as u32).is_err() {
681-
warn!("Could not return used descriptors to ring");
682-
}
683-
vring.signal_used_queue().unwrap();
684-
}
685-
});
691+
});
692+
}
686693
}
687694

688695
Ok(used_any)

0 commit comments

Comments
 (0)