use netmap_rs::prelude::*;
use std::thread::sleep;
use std::time::Duration;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("=== Netmap-rs Updated Example ===");
println!("\n1. Testing without sys feature (if available)...");
println!("\n2. Testing with sys feature enabled...");
let result = std::panic::catch_unwind(|| {
NetmapBuilder::new("lo") .num_tx_rings(1)
.num_rx_rings(1)
.build()
});
match result {
Ok(Ok(nm)) => {
println!("✓ Successfully opened netmap interface");
println!(" - TX rings: {}", nm.num_tx_rings());
println!(" - RX rings: {}", nm.num_rx_rings());
println!(" - Is host interface: {}", nm.is_host_if());
let tx_result = nm.tx_ring(0);
let rx_result = nm.rx_ring(0);
match (tx_result, rx_result) {
(Ok(mut tx_ring), Ok(mut rx_ring)) => {
println!("✓ Successfully got ring handles");
let test_packet = b"Hello from updated netmap-rs!";
match tx_ring.send(test_packet) {
Ok(_) => {
println!("✓ Successfully queued packet for transmission");
tx_ring.sync();
println!("✓ Synced TX ring");
}
Err(e) => println!("✗ Failed to send packet: {}", e),
}
rx_ring.sync();
match rx_ring.recv() {
Some(frame) => {
println!("✓ Received packet: {} bytes", frame.len());
println!(" Packet data (first 50 bytes): {:?}",
&frame.payload()[..frame.len().min(50)]);
}
None => println!("ℹ No packets available to receive"),
}
println!("\n3. Testing batch operations...");
match tx_ring.reserve_batch(5) {
Ok(mut batch) => {
println!("✓ Successfully reserved batch of 5 slots");
for i in 0..5 {
let packet_data = format!("Batch packet {}", i);
match batch.packet(i, packet_data.len()) {
Ok(buf) => {
buf.copy_from_slice(packet_data.as_bytes());
println!(" ✓ Prepared packet {}: {}", i, packet_data);
}
Err(e) => println!(" ✗ Failed to get packet {}: {}", i, e),
}
}
batch.commit();
println!("✓ Committed batch");
}
Err(e) => println!("✗ Failed to reserve batch: {}", e),
}
let mut recv_batch = vec![Frame::new_owned(vec![0u8; 1500]); 10];
let received = rx_ring.recv_batch(&mut recv_batch);
if received > 0 {
println!("✓ Received {} packets in batch", received);
} else {
println!("ℹ No packets available for batch receive");
}
}
(Err(e), _) => println!("✗ Failed to get TX ring: {}", e),
(_, Err(e)) => println!("✗ Failed to get RX ring: {}", e),
}
}
Ok(Err(e)) => {
println!("✗ Failed to open netmap interface: {}", e);
println!(" This is expected if netmap is not installed or properly configured");
println!(" See README.md for installation instructions");
}
Err(_) => {
println!("✗ Panic occurred while trying to open netmap interface");
println!(" This might indicate a more serious issue with the netmap installation");
}
}
println!("\n4. Error handling demonstration...");
let io_error: std::io::Error = std::io::Error::new(std::io::ErrorKind::NotFound, "test error");
let netmap_error: Error = io_error.into();
println!("✓ Converted IO error to netmap error: {}", netmap_error);
let errors = vec![
Error::WouldBlock,
Error::BindFail("test interface".to_string()),
Error::InvalidRingIndex(42),
Error::PacketTooLarge(9000),
Error::InsufficientSpace,
Error::UnsupportedPlatform("test platform".to_string()),
Error::FallbackUnsupported("test feature".to_string()),
];
for error in errors {
println!(" Error variant: {}", error);
}
println!("\n=== Example completed ===");
Ok(())
}