diff --git a/test/sys/test_socket.rs b/test/sys/test_socket.rs index a9beb1f785..c668d974ea 100644 --- a/test/sys/test_socket.rs +++ b/test/sys/test_socket.rs @@ -169,10 +169,10 @@ mod recvfrom { const MSG: &'static [u8] = b"Hello, World!"; - fn sendrecv(rsock: RawFd, ssock: RawFd, f_send: Fs, f_recv: Fr) -> Option + fn sendrecv(rsock: RawFd, ssock: RawFd, f_send: Fs, mut f_recv: Fr) -> Option where Fs: Fn(RawFd, &[u8], MsgFlags) -> Result + Send + 'static, - Fr: Fn(usize, Option), + Fr: FnMut(usize, Option), { let mut buf: [u8; 13] = [0u8; 13]; let mut l = 0; @@ -240,6 +240,11 @@ mod recvfrom { pub fn gso() { require_kernel_version!(udp_offload::gso, ">= 4.18"); + // In this test, we send the data and provide a GSO segment size. + // Since we are sending the buffer of size 13, six UDP packets + // with size 2 and two UDP packet with size 1 will be sent. + let segment_size: u16 = 2; + let std_sa = SocketAddr::from_str("127.0.0.1:6791").unwrap(); let inet_addr = InetAddr::from_std(&std_sa); let sock_addr = SockAddr::new_inet(inet_addr); @@ -249,9 +254,8 @@ mod recvfrom { None ).unwrap(); - let segment_size = 2; - - setsockopt(rsock, UdpGsoSegment, &segment_size).expect("setsockopt UDP_SEGMENT failed"); + setsockopt(rsock, UdpGsoSegment, &(segment_size as _)) + .expect("setsockopt UDP_SEGMENT failed"); bind(rsock, &sock_addr).unwrap(); let ssock = socket( @@ -261,48 +265,45 @@ mod recvfrom { None, ).expect("send socket failed"); - let from = sendrecv(rsock, ssock, move |s, m, flags| { + let mut num_packets_received: i32 = 0; + + sendrecv(rsock, ssock, move |s, m, flags| { let iov = [IoVec::from_slice(m)]; - let cmsg = ControlMessage::UdpGsoSegments(&2); + let cmsg = ControlMessage::UdpGsoSegments(&segment_size); sendmsg(s, &iov, &[cmsg], flags, Some(&sock_addr)) - }, |len, _| { - assert!(len <= segment_size as usize); + }, { + let num_packets_received_ref = &mut num_packets_received; + + move |len, _| { + // check that we receive UDP packets with payload size + // less or equal to segment size + assert!(len <= segment_size as usize); + *num_packets_received_ref += 1; + } }); - // UDP sockets should set the from address - assert_eq!(AddressFamily::Inet, from.unwrap().family()); + + // Buffer size is 13, we will receive six packets of size 2, + // and one packet of size 1. + assert_eq!(7, num_packets_received); } #[test] pub fn gro() { require_kernel_version!(udp_offload::gro, ">= 5.3"); + // It's hard to guarantee receiving GRO packets. Just checking + // that `setsockopt` doesn't fail with error + let std_sa = SocketAddr::from_str("127.0.0.1:6792").unwrap(); let inet_addr = InetAddr::from_std(&std_sa); - let sock_addr = SockAddr::new_inet(inet_addr); let rsock = socket(AddressFamily::Inet, SockType::Datagram, SockFlag::empty(), None - ).unwrap(); - - setsockopt(rsock, UdpGroSegment, &true).expect("setsockopt UDP_GRO failed"); - - bind(rsock, &sock_addr).unwrap(); - let ssock = socket( - AddressFamily::Inet, - SockType::Datagram, - SockFlag::empty(), - None, - ).expect("send socket failed"); - - let from = sendrecv(rsock, ssock, move |s, m, flags| { - let iov = [IoVec::from_slice(m)]; - let cmsg = ControlMessage::UdpGsoSegments(&2); - sendmsg(s, &iov, &[cmsg], flags, Some(&sock_addr)) - }, |_, _| {}); + ).unwrap();I - // UDP sockets should set the from address - assert_eq!(AddressFamily::Inet, from.unwrap().family()); + setsockopt(rsock, UdpGroSegment, &true) + .expect("setsockopt UDP_GRO failed"); } } }