utils.rs 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // TODO: this is literally a copy of examples/utils.rs, but without an allow dead code attribute.
  2. // The include logic does not allow having attributes in included files.
  3. use std::cell::RefCell;
  4. use std::str::{self, FromStr};
  5. use std::rc::Rc;
  6. use std::io;
  7. use std::fs::File;
  8. use std::time::{SystemTime, UNIX_EPOCH};
  9. use std::env;
  10. use std::process;
  11. use getopts::{Options, Matches};
  12. use smoltcp::phy::{Device, EthernetTracer, FaultInjector};
  13. use smoltcp::phy::{PcapWriter, PcapSink, PcapMode, PcapLinkType};
  14. use smoltcp::time::Duration;
  15. pub fn create_options() -> (Options, Vec<&'static str>) {
  16. let mut opts = Options::new();
  17. opts.optflag("h", "help", "print this help menu");
  18. (opts, Vec::new())
  19. }
  20. pub fn parse_options(options: &Options, free: Vec<&str>) -> Matches {
  21. match options.parse(env::args().skip(1)) {
  22. Err(err) => {
  23. println!("{}", err);
  24. process::exit(1)
  25. }
  26. Ok(matches) => {
  27. if matches.opt_present("h") || matches.free.len() != free.len() {
  28. let brief = format!("Usage: {} [OPTION]... {}",
  29. env::args().nth(0).unwrap(), free.join(" "));
  30. print!("{}", options.usage(&brief));
  31. process::exit(if matches.free.len() != free.len() { 1 } else { 0 })
  32. }
  33. matches
  34. }
  35. }
  36. }
  37. pub fn add_middleware_options(opts: &mut Options, _free: &mut Vec<&str>) {
  38. opts.optopt("", "pcap", "Write a packet capture file", "FILE");
  39. opts.optopt("", "drop-chance", "Chance of dropping a packet (%)", "CHANCE");
  40. opts.optopt("", "corrupt-chance", "Chance of corrupting a packet (%)", "CHANCE");
  41. opts.optopt("", "size-limit", "Drop packets larger than given size (octets)", "SIZE");
  42. opts.optopt("", "tx-rate-limit", "Drop packets after transmit rate exceeds given limit \
  43. (packets per interval)", "RATE");
  44. opts.optopt("", "rx-rate-limit", "Drop packets after transmit rate exceeds given limit \
  45. (packets per interval)", "RATE");
  46. opts.optopt("", "shaping-interval", "Sets the interval for rate limiting (ms)", "RATE");
  47. }
  48. pub fn parse_middleware_options<D>(matches: &mut Matches, device: D, loopback: bool)
  49. -> FaultInjector<EthernetTracer<PcapWriter<D, Rc<PcapSink>>>>
  50. where D: for<'a> Device<'a>
  51. {
  52. let drop_chance = matches.opt_str("drop-chance").map(|s| u8::from_str(&s).unwrap())
  53. .unwrap_or(0);
  54. let corrupt_chance = matches.opt_str("corrupt-chance").map(|s| u8::from_str(&s).unwrap())
  55. .unwrap_or(0);
  56. let size_limit = matches.opt_str("size-limit").map(|s| usize::from_str(&s).unwrap())
  57. .unwrap_or(0);
  58. let tx_rate_limit = matches.opt_str("tx-rate-limit").map(|s| u64::from_str(&s).unwrap())
  59. .unwrap_or(0);
  60. let rx_rate_limit = matches.opt_str("rx-rate-limit").map(|s| u64::from_str(&s).unwrap())
  61. .unwrap_or(0);
  62. let shaping_interval = matches.opt_str("shaping-interval").map(|s| u64::from_str(&s).unwrap())
  63. .unwrap_or(0);
  64. let pcap_writer: Box<io::Write>;
  65. if let Some(pcap_filename) = matches.opt_str("pcap") {
  66. pcap_writer = Box::new(File::create(pcap_filename).expect("cannot open file"))
  67. } else {
  68. pcap_writer = Box::new(io::sink())
  69. }
  70. let seed = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().subsec_nanos();
  71. let device = PcapWriter::new(device, Rc::new(RefCell::new(pcap_writer)) as Rc<PcapSink>,
  72. if loopback { PcapMode::TxOnly } else { PcapMode::Both },
  73. PcapLinkType::Ethernet);
  74. let device = EthernetTracer::new(device, |_timestamp, _printer| {
  75. #[cfg(feature = "log")]
  76. trace!("{}", _printer);
  77. });
  78. let mut device = FaultInjector::new(device, seed);
  79. device.set_drop_chance(drop_chance);
  80. device.set_corrupt_chance(corrupt_chance);
  81. device.set_max_packet_size(size_limit);
  82. device.set_max_tx_rate(tx_rate_limit);
  83. device.set_max_rx_rate(rx_rate_limit);
  84. device.set_bucket_interval(Duration::from_millis(shaping_interval));
  85. device
  86. }