Add 'Find' command to virtio net test util
For finding thet network interface corresponding to a MAC address.
Change-Id: I3bafd4979c47a5299b279bfaf16ec8ffaaaf90d6
diff --git a/src/bin/virtio_input_test_util/main.rs b/src/bin/virtio_input_test_util/main.rs
index 0637d24..d0ac6c6 100644
--- a/src/bin/virtio_input_test_util/main.rs
+++ b/src/bin/virtio_input_test_util/main.rs
@@ -1,6 +1,6 @@
mod events;
-use events::{EventReader, EventType, InputEvent, KeyboardCode, KeyboardValue};
+use crate::events::{EventReader, EventType, InputEvent, KeyboardCode, KeyboardValue};
use std::path::Path;
use std::sync::mpsc;
diff --git a/src/bin/virtio_net_test_util.rs b/src/bin/virtio_net_test_util.rs
index 37c6bfe..cef8fb7 100644
--- a/src/bin/virtio_net_test_util.rs
+++ b/src/bin/virtio_net_test_util.rs
@@ -4,19 +4,57 @@
#![deny(warnings)]
+use std::fs;
+use std::io::{Error, ErrorKind};
use std::net::UdpSocket;
+use std::path::Path;
use structopt::StructOpt;
+const NET_DIR: &str = "/sys/class/net";
+
#[derive(StructOpt, Debug)]
-struct Transfer {
- send_byte: u8,
- receive_byte: u8,
- length: usize,
+enum Command {
+ /// Find the name of network interface with the given MAC address.
+ Find {
+ mac_address: String,
+ },
+ /// Transfer a buffer of <length> containing <send_byte> and wait for a response containing
+ /// <receive_byte>.
+ Transfer {
+ send_byte: u8,
+ receive_byte: u8,
+ length: usize,
+ },
+}
+
+fn get_interface(mac_address: String) -> std::io::Result<String> {
+ let dir = Path::new(NET_DIR);
+ if !dir.is_dir() {
+ return Err(Error::new(
+ ErrorKind::Other,
+ format!("{} is not a directory", NET_DIR),
+ ));
+ }
+ for entry in fs::read_dir(dir)?.collect::<std::io::Result<Vec<_>>>()? {
+ let path = entry.path().join("address");
+ let address = fs::read_to_string(path)?;
+ if address.trim().to_lowercase() == mac_address.trim().to_lowercase() {
+ return entry.file_name().into_string().or(Err(Error::new(
+ ErrorKind::Other,
+ "Failed to read device name",
+ )));
+ }
+ }
+ Err(Error::new(ErrorKind::NotFound, "Could not find interface"))
}
fn main() -> std::io::Result<()> {
- match Transfer::from_args() {
- Transfer {
+ match Command::from_args() {
+ Command::Find { mac_address } => {
+ let interface = get_interface(mac_address)?;
+ println!("{:}", interface);
+ }
+ Command::Transfer {
send_byte,
receive_byte,
length,