mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2025-02-02 01:45:21 +00:00
vm-virtio: net: Expect an identifier upon device creation
This identifier is chosen from the DeviceManager so that it will manage all identifiers across the VM, which will ensure uniqueness. Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
This commit is contained in:
parent
be946caf4b
commit
9eb7413fab
@ -311,6 +311,7 @@ impl NetEpollHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct Net {
|
pub struct Net {
|
||||||
|
id: String,
|
||||||
kill_evt: Option<EventFd>,
|
kill_evt: Option<EventFd>,
|
||||||
pause_evt: Option<EventFd>,
|
pause_evt: Option<EventFd>,
|
||||||
taps: Option<Vec<Tap>>,
|
taps: Option<Vec<Tap>>,
|
||||||
@ -336,6 +337,7 @@ pub struct NetState {
|
|||||||
impl Net {
|
impl Net {
|
||||||
/// Create a new virtio network device with the given TAP interface.
|
/// Create a new virtio network device with the given TAP interface.
|
||||||
pub fn new_with_tap(
|
pub fn new_with_tap(
|
||||||
|
id: String,
|
||||||
taps: Vec<Tap>,
|
taps: Vec<Tap>,
|
||||||
guest_mac: Option<MacAddr>,
|
guest_mac: Option<MacAddr>,
|
||||||
iommu: bool,
|
iommu: bool,
|
||||||
@ -365,6 +367,7 @@ impl Net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Ok(Net {
|
Ok(Net {
|
||||||
|
id,
|
||||||
kill_evt: None,
|
kill_evt: None,
|
||||||
pause_evt: None,
|
pause_evt: None,
|
||||||
taps: Some(taps),
|
taps: Some(taps),
|
||||||
@ -382,7 +385,9 @@ impl Net {
|
|||||||
|
|
||||||
/// Create a new virtio network device with the given IP address and
|
/// Create a new virtio network device with the given IP address and
|
||||||
/// netmask.
|
/// netmask.
|
||||||
|
#[allow(clippy::too_many_arguments)]
|
||||||
pub fn new(
|
pub fn new(
|
||||||
|
id: String,
|
||||||
if_name: Option<&str>,
|
if_name: Option<&str>,
|
||||||
ip_addr: Option<Ipv4Addr>,
|
ip_addr: Option<Ipv4Addr>,
|
||||||
netmask: Option<Ipv4Addr>,
|
netmask: Option<Ipv4Addr>,
|
||||||
@ -393,7 +398,7 @@ impl Net {
|
|||||||
) -> Result<Self> {
|
) -> Result<Self> {
|
||||||
let taps = open_tap(if_name, ip_addr, netmask, num_queues / 2).map_err(Error::OpenTap)?;
|
let taps = open_tap(if_name, ip_addr, netmask, num_queues / 2).map_err(Error::OpenTap)?;
|
||||||
|
|
||||||
Self::new_with_tap(taps, guest_mac, iommu, num_queues, queue_size)
|
Self::new_with_tap(id, taps, guest_mac, iommu, num_queues, queue_size)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn state(&self) -> NetState {
|
fn state(&self) -> NetState {
|
||||||
@ -611,19 +616,18 @@ impl VirtioDevice for Net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
virtio_ctrl_q_pausable!(Net);
|
virtio_ctrl_q_pausable!(Net);
|
||||||
const NET_SNAPSHOT_ID: &str = "virtio-net";
|
|
||||||
impl Snapshottable for Net {
|
impl Snapshottable for Net {
|
||||||
fn id(&self) -> String {
|
fn id(&self) -> String {
|
||||||
NET_SNAPSHOT_ID.to_string()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
|
fn snapshot(&self) -> std::result::Result<Snapshot, MigratableError> {
|
||||||
let snapshot =
|
let snapshot =
|
||||||
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
serde_json::to_vec(&self.state()).map_err(|e| MigratableError::Snapshot(e.into()))?;
|
||||||
|
|
||||||
let mut net_snapshot = Snapshot::new(NET_SNAPSHOT_ID);
|
let mut net_snapshot = Snapshot::new(self.id.as_str());
|
||||||
net_snapshot.add_data_section(SnapshotDataSection {
|
net_snapshot.add_data_section(SnapshotDataSection {
|
||||||
id: format!("{}-section", NET_SNAPSHOT_ID),
|
id: format!("{}-section", self.id),
|
||||||
snapshot,
|
snapshot,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -631,10 +635,7 @@ impl Snapshottable for Net {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
fn restore(&mut self, snapshot: Snapshot) -> std::result::Result<(), MigratableError> {
|
||||||
if let Some(net_section) = snapshot
|
if let Some(net_section) = snapshot.snapshot_data.get(&format!("{}-section", self.id)) {
|
||||||
.snapshot_data
|
|
||||||
.get(&format!("{}-section", NET_SNAPSHOT_ID))
|
|
||||||
{
|
|
||||||
let net_state = match serde_json::from_slice(&net_section.snapshot) {
|
let net_state = match serde_json::from_slice(&net_section.snapshot) {
|
||||||
Ok(state) => state,
|
Ok(state) => state,
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
|
@ -1340,9 +1340,13 @@ impl DeviceManager {
|
|||||||
&mut self,
|
&mut self,
|
||||||
net_cfg: &mut NetConfig,
|
net_cfg: &mut NetConfig,
|
||||||
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
|
) -> DeviceManagerResult<(VirtioDeviceArc, bool, Option<String>)> {
|
||||||
if net_cfg.id.is_none() {
|
let id = if let Some(id) = &net_cfg.id {
|
||||||
net_cfg.id = Some(self.next_device_name(NET_DEVICE_NAME_PREFIX)?);
|
id.clone()
|
||||||
}
|
} else {
|
||||||
|
let id = self.next_device_name(NET_DEVICE_NAME_PREFIX)?;
|
||||||
|
net_cfg.id = Some(id.clone());
|
||||||
|
id
|
||||||
|
};
|
||||||
|
|
||||||
if net_cfg.vhost_user {
|
if net_cfg.vhost_user {
|
||||||
let sock = if let Some(sock) = net_cfg.vhost_socket.clone() {
|
let sock = if let Some(sock) = net_cfg.vhost_socket.clone() {
|
||||||
@ -1371,6 +1375,7 @@ impl DeviceManager {
|
|||||||
let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap {
|
let virtio_net_device = if let Some(ref tap_if_name) = net_cfg.tap {
|
||||||
Arc::new(Mutex::new(
|
Arc::new(Mutex::new(
|
||||||
vm_virtio::Net::new(
|
vm_virtio::Net::new(
|
||||||
|
id,
|
||||||
Some(tap_if_name),
|
Some(tap_if_name),
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
@ -1384,6 +1389,7 @@ impl DeviceManager {
|
|||||||
} else {
|
} else {
|
||||||
Arc::new(Mutex::new(
|
Arc::new(Mutex::new(
|
||||||
vm_virtio::Net::new(
|
vm_virtio::Net::new(
|
||||||
|
id,
|
||||||
None,
|
None,
|
||||||
Some(net_cfg.ip),
|
Some(net_cfg.ip),
|
||||||
Some(net_cfg.mask),
|
Some(net_cfg.mask),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user