vhost-user_net, vm-virtio, vmm: Permit host MAC address setting

Add a new "host_mac" parameter to "--net" and "--net-backend" and use
this to set the MAC address on the tap interface. If no address is given
one is randomly assigned and is stored in the config.

Support for vhost-user-net self spawning was also included.

Fixes: #1177

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford
2020-05-15 10:00:38 +01:00
parent 11049401ce
commit 1b8b5ac179
6 changed files with 79 additions and 35 deletions
+19 -5
View File
@@ -701,6 +701,8 @@ pub struct NetConfig {
pub mask: Ipv4Addr,
#[serde(default = "default_netconfig_mac")]
pub mac: MacAddr,
#[serde(default = "default_netconfig_mac")]
pub host_mac: MacAddr,
#[serde(default)]
pub iommu: bool,
#[serde(default = "default_netconfig_num_queues")]
@@ -745,6 +747,7 @@ impl Default for NetConfig {
ip: default_netconfig_ip(),
mask: default_netconfig_mask(),
mac: default_netconfig_mac(),
host_mac: default_netconfig_mac(),
iommu: false,
num_queues: default_netconfig_num_queues(),
queue_size: default_netconfig_queue_size(),
@@ -769,6 +772,7 @@ impl NetConfig {
.add("ip")
.add("mask")
.add("mac")
.add("host_mac")
.add("iommu")
.add("queue_size")
.add("num_queues")
@@ -790,6 +794,10 @@ impl NetConfig {
.convert("mac")
.map_err(Error::ParseNetwork)?
.unwrap_or_else(default_netconfig_mac);
let host_mac = parser
.convert("host_mac")
.map_err(Error::ParseNetwork)?
.unwrap_or_else(default_netconfig_mac);
let iommu = parser
.convert::<Toggle>("iommu")
.map_err(Error::ParseNetwork)?
@@ -816,6 +824,7 @@ impl NetConfig {
ip,
mask,
mac,
host_mac,
iommu,
num_queues,
queue_size,
@@ -1641,17 +1650,19 @@ mod tests {
fn test_net_parsing() -> Result<()> {
// mac address is random
assert_eq!(
NetConfig::parse("mac=de:ad:be:ef:12:34")?,
NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef")?,
NetConfig {
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
host_mac: MacAddr::parse_str("12:34:de:ad:be:ef").unwrap(),
..Default::default()
}
);
assert_eq!(
NetConfig::parse("mac=de:ad:be:ef:12:34,id=mynet0")?,
NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,id=mynet0")?,
NetConfig {
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
host_mac: MacAddr::parse_str("12:34:de:ad:be:ef").unwrap(),
id: Some("mynet0".to_owned()),
..Default::default()
}
@@ -1659,10 +1670,11 @@ mod tests {
assert_eq!(
NetConfig::parse(
"mac=de:ad:be:ef:12:34,tap=tap0,ip=192.168.100.1,mask=255.255.255.128"
"mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,tap=tap0,ip=192.168.100.1,mask=255.255.255.128"
)?,
NetConfig {
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
host_mac: MacAddr::parse_str("12:34:de:ad:be:ef").unwrap(),
tap: Some("tap0".to_owned()),
ip: "192.168.100.1".parse().unwrap(),
mask: "255.255.255.128".parse().unwrap(),
@@ -1671,9 +1683,10 @@ mod tests {
);
assert_eq!(
NetConfig::parse("mac=de:ad:be:ef:12:34,vhost_user=true,socket=/tmp/socket")?,
NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,vhost_user=true,socket=/tmp/socket")?,
NetConfig {
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
host_mac: MacAddr::parse_str("12:34:de:ad:be:ef").unwrap(),
vhost_user: true,
vhost_socket: Some("/tmp/socket".to_owned()),
..Default::default()
@@ -1681,9 +1694,10 @@ mod tests {
);
assert_eq!(
NetConfig::parse("mac=de:ad:be:ef:12:34,num_queues=4,queue_size=1024,iommu=on")?,
NetConfig::parse("mac=de:ad:be:ef:12:34,host_mac=12:34:de:ad:be:ef,num_queues=4,queue_size=1024,iommu=on")?,
NetConfig {
mac: MacAddr::parse_str("de:ad:be:ef:12:34").unwrap(),
host_mac: MacAddr::parse_str("12:34:de:ad:be:ef").unwrap(),
num_queues: 4,
queue_size: 1024,
iommu: true,
+9 -2
View File
@@ -1463,8 +1463,13 @@ impl DeviceManager {
.args(&[
"--net-backend",
&format!(
"ip={},mask={},socket={},num_queues={},queue_size={}",
net_cfg.ip, net_cfg.mask, &sock, net_cfg.num_queues, net_cfg.queue_size
"ip={},mask={},socket={},num_queues={},queue_size={},host_mac={}",
net_cfg.ip,
net_cfg.mask,
&sock,
net_cfg.num_queues,
net_cfg.queue_size,
net_cfg.host_mac
),
])
.spawn()
@@ -1529,6 +1534,7 @@ impl DeviceManager {
None,
None,
Some(net_cfg.mac),
Some(net_cfg.host_mac),
net_cfg.iommu,
net_cfg.num_queues,
net_cfg.queue_size,
@@ -1543,6 +1549,7 @@ impl DeviceManager {
Some(net_cfg.ip),
Some(net_cfg.mask),
Some(net_cfg.mac),
Some(net_cfg.host_mac),
net_cfg.iommu,
net_cfg.num_queues,
net_cfg.queue_size,