| 1 |
#![warn(rust_2018_idioms)] |
| 2 |
#![cfg(all(feature = "full", target_os = "linux"))] |
| 3 |
|
| 4 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 5 |
use std::sync::Arc; |
| 6 |
use tokio::net::UdpSocket; |
| 7 |
|
| 8 |
/// Ensure that UDP sockets have functional budgeting |
| 9 |
/// |
| 10 |
/// # Design |
| 11 |
/// Two sockets communicate by spamming packets from one to the other. |
| 12 |
/// |
| 13 |
/// In Linux, this packet will be slammed through the entire network stack and into the receiver's buffer during the |
| 14 |
/// send system call because we are using the loopback interface. |
| 15 |
/// This happens because the softirq chain invoked on send when using the loopback interface covers virtually the |
| 16 |
/// entirety of the lifecycle of a packet within the kernel network stack. |
| 17 |
/// |
| 18 |
/// As a result, neither socket will ever encounter an EWOULDBLOCK, and the only way for these to yield during the loop |
| 19 |
/// is through budgeting. |
| 20 |
/// |
| 21 |
/// A second task runs in the background and increments a counter before yielding, allowing us to know how many times sockets yielded. |
| 22 |
/// Since we are both sending and receiving, that should happen once per 64 packets, because budgets are of size 128 |
| 23 |
/// and there are two budget events per packet, a send and a recv. |
| 24 |
#[tokio::test] |
| 25 |
async fn coop_budget_udp_send_recv() { |
| 26 |
const BUDGET: usize = 128; |
| 27 |
const N_ITERATIONS: usize = 1024; |
| 28 |
|
| 29 |
const PACKET: &[u8] = b"Hello, world"; |
| 30 |
const PACKET_LEN: usize = 12; |
| 31 |
|
| 32 |
assert_eq!( |
| 33 |
PACKET_LEN, |
| 34 |
PACKET.len(), |
| 35 |
"Defect in test, programmer can't do math" |
| 36 |
); |
| 37 |
|
| 38 |
// bind each socket to a dynamic port, forcing IPv4 addressing on the localhost interface |
| 39 |
let tx = UdpSocket::bind("127.0.0.1:0").await.unwrap(); |
| 40 |
let rx = UdpSocket::bind("127.0.0.1:0").await.unwrap(); |
| 41 |
|
| 42 |
tx.connect(rx.local_addr().unwrap()).await.unwrap(); |
| 43 |
rx.connect(tx.local_addr().unwrap()).await.unwrap(); |
| 44 |
|
| 45 |
let tracker = Arc::new(AtomicUsize::default()); |
| 46 |
|
| 47 |
let tracker_clone = Arc::clone(&tracker); |
| 48 |
|
| 49 |
tokio::task::yield_now().await; |
| 50 |
|
| 51 |
tokio::spawn(async move { |
| 52 |
loop { |
| 53 |
tracker_clone.fetch_add(1, Ordering::SeqCst); |
| 54 |
|
| 55 |
tokio::task::yield_now().await; |
| 56 |
} |
| 57 |
}); |
| 58 |
|
| 59 |
for _ in 0..N_ITERATIONS { |
| 60 |
tx.send(PACKET).await.unwrap(); |
| 61 |
|
| 62 |
let mut tmp = [0; PACKET_LEN]; |
| 63 |
|
| 64 |
// ensure that we aren't somehow accumulating other |
| 65 |
assert_eq!( |
| 66 |
PACKET_LEN, |
| 67 |
rx.recv(&mut tmp).await.unwrap(), |
| 68 |
"Defect in test case, received unexpected result from socket" |
| 69 |
); |
| 70 |
assert_eq!( |
| 71 |
PACKET, &tmp, |
| 72 |
"Defect in test case, received unexpected result from socket" |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
assert_eq!(N_ITERATIONS / (BUDGET / 2), tracker.load(Ordering::SeqCst)); |
| 77 |
} |