2018-09-13 14:41:28 -07:00
|
|
|
//! The `recvmmsg` module provides recvmmsg() API implementation
|
|
|
|
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet::Packet;
|
2019-11-06 10:52:30 -08:00
|
|
|
pub use solana_perf::packet::NUM_RCVMMSGS;
|
2018-09-13 14:41:28 -07:00
|
|
|
use std::cmp;
|
|
|
|
use std::io;
|
|
|
|
use std::net::UdpSocket;
|
|
|
|
|
|
|
|
#[cfg(not(target_os = "linux"))]
|
2019-05-29 12:17:50 -07:00
|
|
|
pub fn recv_mmsg(socket: &UdpSocket, packets: &mut [Packet]) -> io::Result<(usize, usize)> {
|
2018-09-13 14:41:28 -07:00
|
|
|
let mut i = 0;
|
|
|
|
let count = cmp::min(NUM_RCVMMSGS, packets.len());
|
2019-05-29 12:17:50 -07:00
|
|
|
let mut total_size = 0;
|
2018-09-14 11:40:05 -07:00
|
|
|
for p in packets.iter_mut().take(count) {
|
2018-09-13 14:41:28 -07:00
|
|
|
p.meta.size = 0;
|
|
|
|
match socket.recv_from(&mut p.data) {
|
|
|
|
Err(_) if i > 0 => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
return Err(e);
|
|
|
|
}
|
|
|
|
Ok((nrecv, from)) => {
|
2019-05-29 12:17:50 -07:00
|
|
|
total_size += nrecv;
|
2018-09-13 14:41:28 -07:00
|
|
|
p.meta.size = nrecv;
|
|
|
|
p.meta.set_addr(&from);
|
|
|
|
if i == 0 {
|
|
|
|
socket.set_nonblocking(true)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += 1;
|
|
|
|
}
|
2019-05-29 12:17:50 -07:00
|
|
|
Ok((total_size, i))
|
2018-09-13 14:41:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
2019-05-29 12:17:50 -07:00
|
|
|
pub fn recv_mmsg(sock: &UdpSocket, packets: &mut [Packet]) -> io::Result<(usize, usize)> {
|
2018-09-13 14:41:28 -07:00
|
|
|
use libc::{
|
|
|
|
c_void, iovec, mmsghdr, recvmmsg, sockaddr_in, socklen_t, time_t, timespec, MSG_WAITFORONE,
|
|
|
|
};
|
|
|
|
use nix::sys::socket::InetAddr;
|
|
|
|
use std::mem;
|
|
|
|
use std::os::unix::io::AsRawFd;
|
|
|
|
|
|
|
|
let mut hdrs: [mmsghdr; NUM_RCVMMSGS] = unsafe { mem::zeroed() };
|
|
|
|
let mut iovs: [iovec; NUM_RCVMMSGS] = unsafe { mem::zeroed() };
|
|
|
|
let mut addr: [sockaddr_in; NUM_RCVMMSGS] = unsafe { mem::zeroed() };
|
|
|
|
let addrlen = mem::size_of_val(&addr) as socklen_t;
|
|
|
|
|
|
|
|
let sock_fd = sock.as_raw_fd();
|
|
|
|
|
|
|
|
let count = cmp::min(iovs.len(), packets.len());
|
|
|
|
|
|
|
|
for i in 0..count {
|
|
|
|
iovs[i].iov_base = packets[i].data.as_mut_ptr() as *mut c_void;
|
|
|
|
iovs[i].iov_len = packets[i].data.len();
|
|
|
|
|
|
|
|
hdrs[i].msg_hdr.msg_name = &mut addr[i] as *mut _ as *mut _;
|
|
|
|
hdrs[i].msg_hdr.msg_namelen = addrlen;
|
|
|
|
hdrs[i].msg_hdr.msg_iov = &mut iovs[i];
|
|
|
|
hdrs[i].msg_hdr.msg_iovlen = 1;
|
|
|
|
}
|
|
|
|
let mut ts = timespec {
|
|
|
|
tv_sec: 1 as time_t,
|
|
|
|
tv_nsec: 0,
|
|
|
|
};
|
|
|
|
|
2019-05-29 12:17:50 -07:00
|
|
|
let mut total_size = 0;
|
2018-09-13 14:41:28 -07:00
|
|
|
let npkts =
|
|
|
|
match unsafe { recvmmsg(sock_fd, &mut hdrs[0], count as u32, MSG_WAITFORONE, &mut ts) } {
|
|
|
|
-1 => return Err(io::Error::last_os_error()),
|
|
|
|
n => {
|
|
|
|
for i in 0..n as usize {
|
|
|
|
let mut p = &mut packets[i];
|
|
|
|
p.meta.size = hdrs[i].msg_len as usize;
|
2019-05-29 12:17:50 -07:00
|
|
|
total_size += p.meta.size;
|
2018-09-13 14:41:28 -07:00
|
|
|
let inet_addr = InetAddr::V4(addr[i]);
|
|
|
|
p.meta.set_addr(&inet_addr.to_std());
|
|
|
|
}
|
|
|
|
n as usize
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-29 12:17:50 -07:00
|
|
|
Ok((total_size, npkts))
|
2018-09-13 14:41:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-12-07 19:16:27 -08:00
|
|
|
use crate::packet::PACKET_DATA_SIZE;
|
|
|
|
use crate::recvmmsg::*;
|
2019-01-30 19:47:53 -08:00
|
|
|
use std::time::{Duration, Instant};
|
2018-09-13 14:41:28 -07:00
|
|
|
|
2019-10-08 09:54:49 -07:00
|
|
|
const TEST_NUM_MSGS: usize = 32;
|
2018-09-13 14:41:28 -07:00
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_one_iter() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr = sender.local_addr().unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
let sent = TEST_NUM_MSGS - 1;
|
2018-09-13 14:41:28 -07:00
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:54:49 -07:00
|
|
|
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2018-09-13 14:41:28 -07:00
|
|
|
assert_eq!(sent, recv);
|
|
|
|
for i in 0..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_multi_iter() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr = sender.local_addr().unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
let sent = TEST_NUM_MSGS + 10;
|
2018-09-13 14:41:28 -07:00
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:54:49 -07:00
|
|
|
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2019-10-08 09:54:49 -07:00
|
|
|
assert_eq!(TEST_NUM_MSGS, recv);
|
2018-09-13 14:41:28 -07:00
|
|
|
for i in 0..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr);
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2019-10-08 09:54:49 -07:00
|
|
|
assert_eq!(sent - TEST_NUM_MSGS, recv);
|
2018-09-13 14:41:28 -07:00
|
|
|
for i in 0..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 19:47:53 -08:00
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_multi_iter_timeout() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
reader.set_read_timeout(Some(Duration::new(5, 0))).unwrap();
|
|
|
|
reader.set_nonblocking(false).unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr = sender.local_addr().unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
let sent = TEST_NUM_MSGS;
|
2019-01-30 19:47:53 -08:00
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = Instant::now();
|
2019-10-08 09:54:49 -07:00
|
|
|
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2019-10-08 09:54:49 -07:00
|
|
|
assert_eq!(TEST_NUM_MSGS, recv);
|
2019-01-30 19:47:53 -08:00
|
|
|
for i in 0..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr);
|
|
|
|
}
|
2019-01-31 11:00:36 -08:00
|
|
|
reader.set_nonblocking(true).unwrap();
|
2019-01-30 19:47:53 -08:00
|
|
|
|
2019-01-31 10:47:31 -08:00
|
|
|
let _recv = recv_mmsg(&reader, &mut packets[..]);
|
2019-01-30 19:47:53 -08:00
|
|
|
assert!(start.elapsed().as_secs() < 5);
|
|
|
|
}
|
|
|
|
|
2018-09-13 14:41:28 -07:00
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_multi_addrs() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
|
|
|
|
let sender1 = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr1 = sender1.local_addr().unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
let sent1 = TEST_NUM_MSGS - 1;
|
2018-09-13 14:41:28 -07:00
|
|
|
|
|
|
|
let sender2 = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let saddr2 = sender2.local_addr().unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
let sent2 = TEST_NUM_MSGS + 1;
|
2018-09-13 14:41:28 -07:00
|
|
|
|
|
|
|
for _ in 0..sent1 {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender1.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..sent2 {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender2.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
|
2019-10-08 09:54:49 -07:00
|
|
|
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
|
2018-09-13 14:41:28 -07:00
|
|
|
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2019-10-08 09:54:49 -07:00
|
|
|
assert_eq!(TEST_NUM_MSGS, recv);
|
2018-09-13 14:41:28 -07:00
|
|
|
for i in 0..sent1 {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in sent1..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr2);
|
|
|
|
}
|
|
|
|
|
2019-05-29 12:17:50 -07:00
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
2019-10-08 09:54:49 -07:00
|
|
|
assert_eq!(sent1 + sent2 - TEST_NUM_MSGS, recv);
|
2018-09-13 14:41:28 -07:00
|
|
|
for i in 0..recv {
|
|
|
|
assert_eq!(packets[i].meta.size, PACKET_DATA_SIZE);
|
|
|
|
assert_eq!(packets[i].meta.addr(), saddr2);
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 09:54:49 -07:00
|
|
|
|
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[test]
|
|
|
|
pub fn test_recv_mmsg_batch_size() {
|
|
|
|
let reader = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
let addr = reader.local_addr().unwrap();
|
|
|
|
let sender = UdpSocket::bind("127.0.0.1:0").expect("bind");
|
|
|
|
|
2019-10-16 11:01:41 -07:00
|
|
|
const TEST_BATCH_SIZE: usize = 64;
|
|
|
|
let sent = TEST_BATCH_SIZE;
|
2019-10-08 09:54:49 -07:00
|
|
|
|
2019-10-16 11:01:41 -07:00
|
|
|
let mut elapsed_in_max_batch = 0;
|
|
|
|
(0..1000).for_each(|_| {
|
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender.send_to(&data[..], &addr).unwrap();
|
2019-10-08 09:54:49 -07:00
|
|
|
}
|
2019-10-16 11:01:41 -07:00
|
|
|
let mut packets = vec![Packet::default(); TEST_BATCH_SIZE];
|
|
|
|
let now = Instant::now();
|
|
|
|
let recv = recv_mmsg(&reader, &mut packets[..]).unwrap().1;
|
|
|
|
elapsed_in_max_batch += now.elapsed().as_nanos();
|
|
|
|
assert_eq!(TEST_BATCH_SIZE, recv);
|
|
|
|
});
|
|
|
|
|
|
|
|
let mut elapsed_in_small_batch = 0;
|
|
|
|
(0..1000).for_each(|_| {
|
|
|
|
for _ in 0..sent {
|
|
|
|
let data = [0; PACKET_DATA_SIZE];
|
|
|
|
sender.send_to(&data[..], &addr).unwrap();
|
|
|
|
}
|
|
|
|
let mut packets = vec![Packet::default(); 4];
|
|
|
|
let mut recv = 0;
|
|
|
|
let now = Instant::now();
|
|
|
|
while let Ok(num) = recv_mmsg(&reader, &mut packets[..]) {
|
|
|
|
recv += num.1;
|
|
|
|
if recv >= TEST_BATCH_SIZE {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elapsed_in_small_batch += now.elapsed().as_nanos();
|
|
|
|
assert_eq!(TEST_BATCH_SIZE, recv);
|
|
|
|
});
|
2019-10-08 09:54:49 -07:00
|
|
|
|
|
|
|
assert!(elapsed_in_max_batch <= elapsed_in_small_batch);
|
|
|
|
}
|
2018-09-13 14:41:28 -07:00
|
|
|
}
|