1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use {
crate::{
packet::{Meta, Packet},
recvmmsg::NUM_RCVMMSGS,
},
std::{cmp, io},
tokio::net::UdpSocket,
};
pub async fn recv_mmsg(
socket: &UdpSocket,
packets: &mut [Packet],
) -> io::Result<usize> {
debug_assert!(packets.iter().all(|pkt| pkt.meta == Meta::default()));
let count = cmp::min(NUM_RCVMMSGS, packets.len());
socket.readable().await?;
let mut i = 0;
for p in packets.iter_mut().take(count) {
p.meta.size = 0;
match socket.try_recv_from(p.buffer_mut()) {
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
break;
}
Err(e) => {
return Err(e);
}
Ok((nrecv, from)) => {
p.meta.size = nrecv;
p.meta.set_socket_addr(&from);
}
}
i += 1;
}
Ok(i)
}
#[cfg(test)]
mod tests {
use {
crate::{nonblocking::recvmmsg::*, packet::PACKET_DATA_SIZE},
std::{net::SocketAddr, time::Instant},
tokio::net::UdpSocket,
};
type TestConfig = (UdpSocket, SocketAddr, UdpSocket, SocketAddr);
async fn test_setup_reader_sender(ip_str: &str) -> io::Result<TestConfig> {
let reader = UdpSocket::bind(ip_str).await?;
let addr = reader.local_addr()?;
let sender = UdpSocket::bind(ip_str).await?;
let saddr = sender.local_addr()?;
Ok((reader, addr, sender, saddr))
}
const TEST_NUM_MSGS: usize = 32;
async fn test_one_iter((reader, addr, sender, saddr): TestConfig) {
let sent = TEST_NUM_MSGS - 1;
for _ in 0..sent {
let data = [0; PACKET_DATA_SIZE];
sender.send_to(&data[..], &addr).await.unwrap();
}
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(sent, recv);
for packet in packets.iter().take(recv) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr);
}
}
#[tokio::test]
async fn test_recv_mmsg_one_iter() {
test_one_iter(test_setup_reader_sender("127.0.0.1:0").await.unwrap()).await;
match test_setup_reader_sender("::1:0").await {
Ok(config) => test_one_iter(config).await,
Err(e) => warn!("Failed to configure IPv6: {:?}", e),
}
}
async fn test_multi_iter((reader, addr, sender, saddr): TestConfig) {
let sent = TEST_NUM_MSGS + 10;
for _ in 0..sent {
let data = [0; PACKET_DATA_SIZE];
sender.send_to(&data[..], &addr).await.unwrap();
}
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(TEST_NUM_MSGS, recv);
for packet in packets.iter().take(recv) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr);
}
packets
.iter_mut()
.for_each(|pkt| pkt.meta = Meta::default());
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(sent - TEST_NUM_MSGS, recv);
for packet in packets.iter().take(recv) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr);
}
}
#[tokio::test]
async fn test_recv_mmsg_multi_iter() {
test_multi_iter(test_setup_reader_sender("127.0.0.1:0").await.unwrap()).await;
match test_setup_reader_sender("::1:0").await {
Ok(config) => test_multi_iter(config).await,
Err(e) => warn!("Failed to configure IPv6: {:?}", e),
}
}
#[tokio::test]
async fn test_recv_mmsg_multi_iter_timeout() {
let reader = UdpSocket::bind("127.0.0.1:0").await.expect("bind");
let addr = reader.local_addr().unwrap();
let sender = UdpSocket::bind("127.0.0.1:0").await.expect("bind");
let saddr = sender.local_addr().unwrap();
let sent = TEST_NUM_MSGS;
for _ in 0..sent {
let data = [0; PACKET_DATA_SIZE];
sender.send_to(&data[..], &addr).await.unwrap();
}
let start = Instant::now();
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(TEST_NUM_MSGS, recv);
for packet in packets.iter().take(recv) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr);
}
packets
.iter_mut()
.for_each(|pkt| pkt.meta = Meta::default());
let _recv = recv_mmsg(&reader, &mut packets[..]).await;
assert!(start.elapsed().as_secs() < 5);
}
#[tokio::test]
async fn test_recv_mmsg_multi_addrs() {
let reader = UdpSocket::bind("127.0.0.1:0").await.expect("bind");
let addr = reader.local_addr().unwrap();
let sender1 = UdpSocket::bind("127.0.0.1:0").await.expect("bind");
let saddr1 = sender1.local_addr().unwrap();
let sent1 = TEST_NUM_MSGS - 1;
let sender2 = UdpSocket::bind("127.0.0.1:0").await.expect("bind");
let saddr2 = sender2.local_addr().unwrap();
let sent2 = TEST_NUM_MSGS + 1;
for _ in 0..sent1 {
let data = [0; PACKET_DATA_SIZE];
sender1.send_to(&data[..], &addr).await.unwrap();
}
for _ in 0..sent2 {
let data = [0; PACKET_DATA_SIZE];
sender2.send_to(&data[..], &addr).await.unwrap();
}
let mut packets = vec![Packet::default(); TEST_NUM_MSGS];
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(TEST_NUM_MSGS, recv);
for packet in packets.iter().take(sent1) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr1);
}
for packet in packets.iter().skip(sent1).take(recv - sent1) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr2);
}
packets
.iter_mut()
.for_each(|pkt| pkt.meta = Meta::default());
let recv = recv_mmsg(&reader, &mut packets[..]).await.unwrap();
assert_eq!(sent1 + sent2 - TEST_NUM_MSGS, recv);
for packet in packets.iter().take(recv) {
assert_eq!(packet.meta.size, PACKET_DATA_SIZE);
assert_eq!(packet.meta.socket_addr(), saddr2);
}
}
}