|
1 | | -//! Forward Error Correction (FEC) example using Reed-Solomon |
| 1 | +#![cfg(feature = "sys")] |
2 | 2 |
|
3 | 3 | use netmap_rs::prelude::*; |
4 | 4 | use reed_solomon_erasure::galois_8::ReedSolomon; |
5 | 5 | use std::time::Duration; |
6 | 6 |
|
7 | | -const DATA_SHARDS: usize = 4; |
8 | | -const PARITY_SHARDS: usize = 2; |
| 7 | +// Example: 2 data shards, 1 parity shard |
| 8 | +const DATA_SHARDS: usize = 2; |
| 9 | +const PARITY_SHARDS: usize = 1; |
| 10 | +const TOTAL_SHARDS: usize = DATA_SHARDS + PARITY_SHARDS; |
9 | 11 |
|
10 | 12 | fn main() -> Result<(), Error> { |
11 | | - let nm = NetmapBuilder::new("netmap:eth0") |
| 13 | + let nm = NetmapBuilder::new("netmap:eth0") // Replace with your interface |
12 | 14 | .num_tx_rings(1) |
13 | 15 | .num_rx_rings(1) |
14 | | - .open()?; |
| 16 | + .build()?; |
15 | 17 |
|
16 | 18 | let mut tx_ring = nm.tx_ring(0)?; |
17 | 19 | let mut rx_ring = nm.rx_ring(0)?; |
18 | 20 |
|
19 | | - // Create encoder/decoder |
20 | | - let rs = ReedSolomon::new(DATA_SHARDS, PARITY_SHARDS)?; |
| 21 | + let r = ReedSolomon::new(DATA_SHARDS, PARITY_SHARDS).unwrap(); |
21 | 22 |
|
22 | 23 | // Original data |
23 | | - let mut data: Vec<Vec<u8>> = (0..DATA_SHARDS).map(|i| vec![i as u8; 128]).collect(); |
| 24 | + let original_data = b"Hello Netmap with FEC!".to_vec(); |
| 25 | + let chunk_size = (original_data.len() + DATA_SHARDS - 1) / DATA_SHARDS; |
| 26 | + let mut shards = Vec::with_capacity(TOTAL_SHARDS); |
24 | 27 |
|
25 | | - // Add parity shards |
26 | | - let mut shards = data.clone(); |
27 | | - shards.resize(DATA_SHARDS + PARITY_SHARDS, vec![0; 128]); |
28 | | - rs.encode(&mut shards)?; |
| 28 | + for i in 0..DATA_SHARDS { |
| 29 | + let start = i * chunk_size; |
| 30 | + let end = std::cmp::min(start + chunk_size, original_data.len()); |
| 31 | + let mut shard = original_data[start..end].to_vec(); |
| 32 | + shard.resize(chunk_size, 0); // Pad if necessary |
| 33 | + shards.push(shard); |
| 34 | + } |
| 35 | + for _ in 0..PARITY_SHARDS { |
| 36 | + shards.push(vec![0u8; chunk_size]); |
| 37 | + } |
29 | 38 |
|
30 | | - // Send all shards |
31 | | - for shard in &shards { |
32 | | - tx_ring.send(shard)?; |
| 39 | + // Encode |
| 40 | + r.encode(&mut shards).unwrap(); |
| 41 | + |
| 42 | + // Simulate sending shards (e.g., as separate packets) |
| 43 | + println!("Sending shards..."); |
| 44 | + for (i, shard) in shards.iter().enumerate() { |
| 45 | + // In a real scenario, prepend shard index or other metadata |
| 46 | + let mut packet_data = vec![i as u8]; // Shard index |
| 47 | + packet_data.extend_from_slice(shard); |
| 48 | + tx_ring.send(&packet_data)?; |
| 49 | + println!("Sent shard {}: len {}", i, shard.len()); |
33 | 50 | } |
34 | 51 | tx_ring.sync(); |
35 | 52 |
|
36 | | - // Simulate packet loss (drop 2 random shards) |
37 | | - let mut received_shards = shards.clone(); |
38 | | - received_shards[1] = vec![0; 128]; // Mark as missing |
39 | | - received_shards[4] = vec![0; 128]; // Mark as missing |
| 53 | + // Simulate receiving shards (and potentially losing one) |
| 54 | + let mut received_shards: Vec<Option<Vec<u8>>> = vec![None; TOTAL_SHARDS]; |
| 55 | + let mut received_count = 0; |
40 | 56 |
|
41 | | - // Receive and reconstruct |
42 | | - let mut reconstructed = received_shards.clone(); |
43 | | - rs.reconstruct(&mut reconstructed)?; |
| 57 | + println!("Receiving shards (simulating loss of shard 0)..."); |
| 58 | + for _ in 0..10 { // Try to receive for a bit |
| 59 | + rx_ring.sync(); |
| 60 | + while let Some(frame) = rx_ring.recv() { |
| 61 | + let payload = frame.payload(); |
| 62 | + if payload.is_empty() { continue; } |
| 63 | + let shard_index = payload[0] as usize; |
44 | 64 |
|
45 | | - // Verify reconstruction |
46 | | - for i in 0..DATA_SHARDS { |
47 | | - assert_eq!( |
48 | | - reconstructed[i], data[i], |
49 | | - "Reconstruction failed for shard {}", |
50 | | - i |
51 | | - ); |
| 65 | + // SIMULATE LOSS OF SHARD 0 |
| 66 | + if shard_index == 0 && received_shards[0].is_none() && received_count < DATA_SHARDS { |
| 67 | + println!("Simulated loss of shard 0"); |
| 68 | + received_shards[0] = Some(vec![]); // Mark as lost for reconstruction logic |
| 69 | + // but don't actually store it / increment received_count for it yet |
| 70 | + // to ensure reconstruction is attempted. |
| 71 | + // For this test, we'll actually skip storing it to force reconstruction. |
| 72 | + continue; |
| 73 | + } |
| 74 | + |
| 75 | + if shard_index < TOTAL_SHARDS && received_shards[shard_index].is_none() { |
| 76 | + received_shards[shard_index] = Some(payload[1..].to_vec()); |
| 77 | + received_count += 1; |
| 78 | + println!("Received shard {}", shard_index); |
| 79 | + } |
| 80 | + if received_count >= DATA_SHARDS { break; } |
| 81 | + } |
| 82 | + if received_count >= DATA_SHARDS { break; } |
| 83 | + std::thread::sleep(Duration::from_millis(50)); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + if received_count < DATA_SHARDS { |
| 88 | + eprintln!("Did not receive enough shards to reconstruct."); |
| 89 | + return Ok(()); |
| 90 | + } |
| 91 | + |
| 92 | + println!("Attempting reconstruction..."); |
| 93 | + match r.reconstruct(&mut received_shards) { |
| 94 | + Ok(_) => { |
| 95 | + println!("Reconstruction successful!"); |
| 96 | + let mut reconstructed_data = Vec::new(); |
| 97 | + for i in 0..DATA_SHARDS { |
| 98 | + if let Some(shard_data) = &received_shards[i] { |
| 99 | + reconstructed_data.extend_from_slice(shard_data); |
| 100 | + } else { |
| 101 | + eprintln!("Missing data shard {} after reconstruction attempt.", i); |
| 102 | + return Ok(()); |
| 103 | + } |
| 104 | + } |
| 105 | + // Trim padding if original length known, or handle as per application logic |
| 106 | + reconstructed_data.truncate(original_data.len()); |
| 107 | + |
| 108 | + if reconstructed_data == original_data { |
| 109 | + println!("Data successfully reconstructed: {:?}", String::from_utf8_lossy(&reconstructed_data)); |
| 110 | + } else { |
| 111 | + eprintln!("Data mismatch after reconstruction!"); |
| 112 | + eprintln!("Original: {:?}", String::from_utf8_lossy(&original_data)); |
| 113 | + eprintln!("Reconstructed: {:?}", String::from_utf8_lossy(&reconstructed_data)); |
| 114 | + } |
| 115 | + } |
| 116 | + Err(e) => { |
| 117 | + eprintln!("Reconstruction failed: {:?}", e); |
| 118 | + } |
52 | 119 | } |
53 | 120 |
|
54 | | - println!("FEC test successful - data reconstructed correctly"); |
55 | 121 | Ok(()) |
56 | 122 | } |
0 commit comments