Skip to content

Commit e0863e0

Browse files
jemoreiravireshk
authored andcommitted
vsock: Fix handling of data in the tx queue
Data from the tx queue was being handled incorrectly when it only partially fit at the end of the circular buffer: The beginning of the data was being copied again to start of the circular buffer. Signed-off-by: Jorge E. Moreira <[email protected]>
1 parent de03220 commit e0863e0

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

vhost-device-vsock/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Fixed
1212
- [#800](https://github.com/rust-vmm/vhost-device/pull/800) Disable EPOLLOUT if triggered while txbuf is empty
13+
- [#838](https://github.com/rust-vmm/vhost-device/pull/838) Fix handling of data in the tx queue
1314

1415
### Deprecated
1516

vhost-device-vsock/src/txbuf.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{io::Write, num::Wrapping};
44

5-
use vm_memory::{bitmap::BitmapSlice, VolatileSlice};
5+
use vm_memory::{bitmap::BitmapSlice, Bytes, VolatileSlice};
66

77
use crate::vhu_vsock::{Error, Result};
88

@@ -55,7 +55,9 @@ impl LocalTxBuf {
5555
// Check if there is more data to be wrapped around
5656
if len < data_buf.len() {
5757
let remain_txbuf = &mut self.buf[..(data_buf.len() - len)];
58-
data_buf.copy_to(remain_txbuf);
58+
data_buf
59+
.read_slice(remain_txbuf, len)
60+
.expect("shouldn't faile because remain_txbuf's len is data_buf.len() - len");
5961
}
6062

6163
// Increment tail by the amount of data that has been added to the buffer
@@ -159,18 +161,20 @@ mod tests {
159161
assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE * 2));
160162

161163
// only tail wraps at full
162-
let mut buf = vec![1; 4];
164+
let mut buf = vec![1, 1, 3, 3];
163165
// SAFETY: Safe as the buffer is guaranteed to be valid here.
164166
let data = unsafe { VolatileSlice::new(buf.as_mut_ptr(), buf.len()) };
165-
let mut cmp_data = vec![1; 4];
166-
cmp_data.append(&mut vec![0; (CONN_TX_BUF_SIZE - 4) as usize]);
167-
loc_tx_buf.head = Wrapping(4);
168-
loc_tx_buf.tail = Wrapping(CONN_TX_BUF_SIZE);
167+
loc_tx_buf.head = Wrapping(2);
168+
loc_tx_buf.tail = Wrapping(CONN_TX_BUF_SIZE - 2);
169169
let res_push = loc_tx_buf.push(&data);
170170
assert!(res_push.is_ok());
171-
assert_eq!(loc_tx_buf.head, Wrapping(4));
172-
assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE + 4));
173-
assert_eq!(loc_tx_buf.buf, cmp_data);
171+
assert_eq!(loc_tx_buf.head, Wrapping(2));
172+
assert_eq!(loc_tx_buf.tail, Wrapping(CONN_TX_BUF_SIZE + 2));
173+
assert_eq!(loc_tx_buf.buf[0..2], buf[2..4]);
174+
assert_eq!(
175+
loc_tx_buf.buf[CONN_TX_BUF_SIZE as usize - 2..CONN_TX_BUF_SIZE as usize],
176+
buf[0..2]
177+
);
174178
}
175179

176180
#[test]

0 commit comments

Comments
 (0)