@@ -219,57 +219,61 @@ impl VsockPacket {
219219 ///
220220 /// Return value will equal the total length of the underlying descriptor chain's buffers,
221221 /// minus the length of the vsock header.
222- pub fn buf_size ( & self ) -> usize {
222+ pub fn buf_size ( & self ) -> u32 {
223223 let chain_length = match self . buffer {
224224 VsockPacketBuffer :: Tx ( ref iovec_buf) => iovec_buf. len ( ) ,
225225 VsockPacketBuffer :: Rx ( ref iovec_buf) => iovec_buf. len ( ) ,
226226 } ;
227- ( chain_length - VSOCK_PKT_HDR_SIZE ) as usize
227+ chain_length - VSOCK_PKT_HDR_SIZE
228228 }
229229
230230 pub fn read_at_offset_from < T : ReadVolatile + Debug > (
231231 & mut self ,
232232 src : & mut T ,
233- offset : usize ,
234- count : usize ,
235- ) -> Result < usize , VsockError > {
233+ offset : u32 ,
234+ count : u32 ,
235+ ) -> Result < u32 , VsockError > {
236236 match self . buffer {
237237 VsockPacketBuffer :: Tx ( _) => Err ( VsockError :: UnwritableDescriptor ) ,
238238 VsockPacketBuffer :: Rx ( ref mut buffer) => {
239239 if count
240- > ( buffer. len ( ) as usize )
241- . saturating_sub ( VSOCK_PKT_HDR_SIZE as usize )
240+ > buffer
241+ . len ( )
242+ . saturating_sub ( VSOCK_PKT_HDR_SIZE )
242243 . saturating_sub ( offset)
243244 {
244245 return Err ( VsockError :: GuestMemoryBounds ) ;
245246 }
246247
247248 buffer
248- . write_volatile_at ( src, offset + VSOCK_PKT_HDR_SIZE as usize , count)
249+ . write_volatile_at ( src, ( offset + VSOCK_PKT_HDR_SIZE ) as usize , count as usize )
249250 . map_err ( |err| VsockError :: GuestMemoryMmap ( GuestMemoryError :: from ( err) ) )
251+ . and_then ( |read| read. try_into ( ) . map_err ( |_| VsockError :: DescChainOverflow ) )
250252 }
251253 }
252254 }
253255
254256 pub fn write_from_offset_to < T : WriteVolatile + Debug > (
255257 & self ,
256258 dst : & mut T ,
257- offset : usize ,
258- count : usize ,
259- ) -> Result < usize , VsockError > {
259+ offset : u32 ,
260+ count : u32 ,
261+ ) -> Result < u32 , VsockError > {
260262 match self . buffer {
261263 VsockPacketBuffer :: Tx ( ref buffer) => {
262264 if count
263- > ( buffer. len ( ) as usize )
264- . saturating_sub ( VSOCK_PKT_HDR_SIZE as usize )
265+ > buffer
266+ . len ( )
267+ . saturating_sub ( VSOCK_PKT_HDR_SIZE )
265268 . saturating_sub ( offset)
266269 {
267270 return Err ( VsockError :: GuestMemoryBounds ) ;
268271 }
269272
270273 buffer
271- . read_volatile_at ( dst, offset + VSOCK_PKT_HDR_SIZE as usize , count)
274+ . read_volatile_at ( dst, ( offset + VSOCK_PKT_HDR_SIZE ) as usize , count as usize )
272275 . map_err ( |err| VsockError :: GuestMemoryMmap ( GuestMemoryError :: from ( err) ) )
276+ . and_then ( |read| read. try_into ( ) . map_err ( |_| VsockError :: DescChainOverflow ) )
273277 }
274278 VsockPacketBuffer :: Rx ( _) => Err ( VsockError :: UnreadableDescriptor ) ,
275279 }
@@ -537,10 +541,7 @@ mod tests {
537541 handler_ctx. device . queues [ RXQ_INDEX ] . pop ( ) . unwrap ( ) ,
538542 )
539543 . unwrap ( ) ;
540- assert_eq ! (
541- pkt. buf_size( ) ,
542- handler_ctx. guest_rxvq. dtable[ 1 ] . len. get( ) as usize
543- ) ;
544+ assert_eq ! ( pkt. buf_size( ) , handler_ctx. guest_rxvq. dtable[ 1 ] . len. get( ) ) ;
544545 }
545546
546547 // Test case: read-only RX packet header.
@@ -646,35 +647,38 @@ mod tests {
646647 . unwrap ( ) ;
647648
648649 let buf_desc = & mut handler_ctx. guest_rxvq . dtable [ 1 ] ;
649- assert_eq ! ( pkt. buf_size( ) , buf_desc. len. get( ) as usize ) ;
650- let zeros = vec ! [ 0_u8 ; pkt. buf_size( ) ] ;
650+ assert_eq ! ( pkt. buf_size( ) , buf_desc. len. get( ) ) ;
651+ let zeros = vec ! [ 0_u8 ; pkt. buf_size( ) as usize ] ;
651652 let data: Vec < u8 > = ( 0 ..pkt. buf_size ( ) )
652653 . map ( |i| ( ( i as u64 ) & 0xff ) as u8 )
653654 . collect ( ) ;
654655 for offset in 0 ..pkt. buf_size ( ) {
655656 buf_desc. set_data ( & zeros) ;
656657
657- let mut expected_data = zeros[ ..offset] . to_vec ( ) ;
658- expected_data. extend_from_slice ( & data[ ..pkt. buf_size ( ) - offset] ) ;
658+ let mut expected_data = zeros[ ..offset as usize ] . to_vec ( ) ;
659+ expected_data. extend_from_slice ( & data[ ..( pkt. buf_size ( ) - offset) as usize ] ) ;
659660
660661 pkt. read_at_offset_from ( & mut data. as_slice ( ) , offset, pkt. buf_size ( ) - offset)
661662 . unwrap ( ) ;
662663
663664 buf_desc. check_data ( & expected_data) ;
664665
665- let mut buf = vec ! [ 0 ; pkt. buf_size( ) ] ;
666+ let mut buf = vec ! [ 0 ; pkt. buf_size( ) as usize ] ;
666667 pkt2. write_from_offset_to ( & mut buf. as_mut_slice ( ) , offset, pkt. buf_size ( ) - offset)
667668 . unwrap ( ) ;
668- assert_eq ! ( & buf[ ..pkt. buf_size( ) - offset] , & expected_data[ offset..] ) ;
669+ assert_eq ! (
670+ & buf[ ..( pkt. buf_size( ) - offset) as usize ] ,
671+ & expected_data[ offset as usize ..]
672+ ) ;
669673 }
670674
671675 let oob_cases = vec ! [
672676 ( 1 , pkt. buf_size( ) ) ,
673677 ( pkt. buf_size( ) , 1 ) ,
674- ( usize :: MAX , 1 ) ,
675- ( 1 , usize :: MAX ) ,
678+ ( u32 :: MAX , 1 ) ,
679+ ( 1 , u32 :: MAX ) ,
676680 ] ;
677- let mut buf = vec ! [ 0 ; pkt. buf_size( ) ] ;
681+ let mut buf = vec ! [ 0 ; pkt. buf_size( ) as usize ] ;
678682 for ( offset, count) in oob_cases {
679683 let res = pkt. read_at_offset_from ( & mut data. as_slice ( ) , offset, count) ;
680684 assert ! ( matches!( res, Err ( VsockError :: GuestMemoryBounds ) ) ) ;
0 commit comments