-
Notifications
You must be signed in to change notification settings - Fork 11
Add --device virtio-net,type=unix{gram,stream} #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e5a2c15
c63e0b7
3790761
7b66479
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,12 @@ | |
|
||
use crate::{status::RestfulUri, virtio::VirtioDeviceConfig}; | ||
|
||
use std::{collections::HashMap, path::PathBuf, str::FromStr}; | ||
use std::{ | ||
collections::HashMap, | ||
ffi::{c_char, CString}, | ||
path::PathBuf, | ||
str::FromStr, | ||
}; | ||
|
||
use anyhow::{anyhow, Context, Result}; | ||
use clap::Parser; | ||
|
@@ -102,6 +107,26 @@ pub fn check_unknown_args(args: HashMap<String, String>, label: &str) -> Result< | |
Ok(()) | ||
} | ||
|
||
/// Parse a string slice and convert it to a boolean if possible. In addition to "true" and "false" | ||
/// being valid strings, "on" and "off" are also valid. | ||
pub fn parse_boolean(value: &str) -> Result<bool, anyhow::Error> { | ||
match value { | ||
"true" | "on" => Ok(true), | ||
"false" | "off" => Ok(false), | ||
_ => Err(anyhow!("invalid boolean value {value}")), | ||
} | ||
} | ||
|
||
/// Convert a CString value to a pointer. If the string is empty, the function will return a NULL | ||
/// ptr. | ||
pub fn cstring_to_ptr(value: &CString) -> *const c_char { | ||
if value.is_empty() { | ||
std::ptr::null() | ||
} else { | ||
value.as_ptr() | ||
} | ||
} | ||
|
||
/// A wrapper of all data associated with the bootloader argument. | ||
mod bootloader { | ||
use super::*; | ||
|
@@ -380,6 +405,16 @@ mod tests { | |
"virtio-gpu,width=800,height=600", | ||
"--device", | ||
"virtio-input,keyboard", | ||
"--device", | ||
"virtio-net,type=unixgram,path=/Users/user/net.sock,mac=00:00:00:00:00:00,offloading=true,vfkitMagic=off", | ||
"--device", | ||
"virtio-net,type=unixgram,fd=4,mac=00:00:00:00:00:00", | ||
"--device", | ||
"virtio-net,type=unixstream,path=/Users/user/net.sock,mac=00:00:00:00:00:00,offloading=on", | ||
"--device", | ||
"virtio-net,type=unixstream,fd=4,mac=00:00:00:00:00:00,offloading=off", | ||
"--device", | ||
"virtio-net,type=unixstream,fd=4,mac=00:00:00:00:00:00", | ||
"--restful-uri", | ||
"tcp://localhost:49573", | ||
"--gui", | ||
|
@@ -389,6 +424,102 @@ mod tests { | |
|
||
let mut args = Args::try_parse_from(cmdline).unwrap(); | ||
|
||
let net = args | ||
.devices | ||
.pop() | ||
.expect("expected 15th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
if let SocketType::UnixStream = net.socket_type { | ||
assert_eq!(net.socket_config.path, None,); | ||
assert_eq!(net.socket_config.fd, Some(4)); | ||
assert_eq!(net.socket_config.offloading, false); | ||
assert_eq!(net.socket_config.send_vfkit_magic, false); | ||
} else { | ||
panic!("expected virtio-net device to use the unixstream argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 15th device config argument"); | ||
} | ||
|
||
let net = args | ||
.devices | ||
.pop() | ||
.expect("expected 14th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
if let SocketType::UnixStream = net.socket_type { | ||
assert_eq!(net.socket_config.path, None,); | ||
assert_eq!(net.socket_config.fd, Some(4)); | ||
assert_eq!(net.socket_config.offloading, false); | ||
assert_eq!(net.socket_config.send_vfkit_magic, false); | ||
} else { | ||
panic!("expected virtio-net device to use the unixstream argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 14th device config argument"); | ||
} | ||
|
||
let net = args | ||
.devices | ||
.pop() | ||
.expect("expected 13th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
if let SocketType::UnixStream = net.socket_type { | ||
assert_eq!( | ||
net.socket_config.path, | ||
Some(PathBuf::from_str("/Users/user/net.sock").unwrap()) | ||
); | ||
assert_eq!(net.socket_config.fd, None); | ||
assert_eq!(net.socket_config.offloading, true); | ||
assert_eq!(net.socket_config.send_vfkit_magic, false); | ||
} else { | ||
panic!("expected virtio-net device to use the unixstream argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 13th device config argument"); | ||
} | ||
|
||
let net = args | ||
.devices | ||
.pop() | ||
.expect("expected 12th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
if let SocketType::UnixGram = net.socket_type { | ||
assert_eq!(net.socket_config.path, None); | ||
assert_eq!(net.socket_config.fd, Some(4)); | ||
assert_eq!(net.socket_config.offloading, false); | ||
assert_eq!(net.socket_config.send_vfkit_magic, false); | ||
} else { | ||
panic!("expected virtio-net device to use the unixgram argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 12th device config argument"); | ||
} | ||
|
||
let net = args | ||
.devices | ||
.pop() | ||
.expect("expected 11th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
if let SocketType::UnixGram = net.socket_type { | ||
assert_eq!( | ||
net.socket_config.path, | ||
Some(PathBuf::from_str("/Users/user/net.sock").unwrap()) | ||
); | ||
assert_eq!(net.socket_config.fd, None); | ||
assert_eq!(net.socket_config.offloading, true); | ||
assert_eq!(net.socket_config.send_vfkit_magic, false); | ||
} else { | ||
panic!("expected virtio-net device to use the unixgram argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 11th device config argument"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is very fragile to depend on the index of this device in the test. Does device order matter except virtio-blk (first device is boot device)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, device order here doesn't matter. I'm just following the pattern of how the test was originally designed. I agree that it's a fragile implementation, but I think that's best for another PR to clean it up. |
||
} | ||
|
||
let input = args | ||
.devices | ||
.pop() | ||
|
@@ -441,10 +572,17 @@ mod tests { | |
.pop() | ||
.expect("expected 6th virtio device config"); | ||
if let VirtioDeviceConfig::Net(net) = net { | ||
assert_eq!( | ||
net.unix_socket_path, | ||
PathBuf::from_str("/Users/user/net.sock").unwrap() | ||
); | ||
if let SocketType::UnixGram = net.socket_type { | ||
assert_eq!( | ||
net.socket_config.path, | ||
Some(PathBuf::from_str("/Users/user/net.sock").unwrap()) | ||
); | ||
assert_eq!(net.socket_config.fd, None); | ||
assert_eq!(net.socket_config.offloading, true); | ||
assert_eq!(net.socket_config.send_vfkit_magic, true); | ||
} else { | ||
panic!("expected virtio-net device to use the unixSocketPath argument"); | ||
} | ||
assert_eq!(net.mac_address, MacAddress::new([0, 0, 0, 0, 0, 0])); | ||
} else { | ||
panic!("expected virtio-net device as 6th device config argument"); | ||
|
Uh oh!
There was an error while loading. Please reload this page.