pub fn new_with_tap(
        id: String,
        tap_if_name: String,
        guest_mac: Option<&MacAddr>,
        rx_rate_limiter: RateLimiter,
        tx_rate_limiter: RateLimiter,
        allow_mmds_requests: bool,
    ) -> Result<Self> {
        let tap = Tap::open_named(&tap_if_name).map_err(Error::TapOpen)?;

        // Set offload flags to match the virtio features below.
        tap.set_offload(
            net_gen::TUN_F_CSUM | net_gen::TUN_F_UFO | net_gen::TUN_F_TSO4 | net_gen::TUN_F_TSO6,
        )
        .map_err(Error::TapSetOffload)?;

        let vnet_hdr_size = vnet_hdr_len() as i32;
        tap.set_vnet_hdr_size(vnet_hdr_size)
            .map_err(Error::TapSetVnetHdrSize)?;

        let mut avail_features = 1 << VIRTIO_NET_F_GUEST_CSUM
            | 1 << VIRTIO_NET_F_CSUM
            | 1 << VIRTIO_NET_F_GUEST_TSO4
            | 1 << VIRTIO_NET_F_GUEST_UFO
            | 1 << VIRTIO_NET_F_HOST_TSO4
            | 1 << VIRTIO_NET_F_HOST_UFO
            | 1 << VIRTIO_F_VERSION_1;

        let mut config_space = ConfigSpace::default();
        if let Some(mac) = guest_mac {
            config_space.guest_mac.copy_from_slice(mac.get_bytes());
            // When this feature isn't available, the driver generates a random MAC address.
            // Otherwise, it should attempt to read the device MAC address from the config space.
            avail_features |= 1 << VIRTIO_NET_F_MAC;
        }

        let mut queue_evts = Vec::new();
        for _ in QUEUE_SIZES.iter() {
            queue_evts.push(EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?);
        }

        let queues = QUEUE_SIZES.iter().map(|&s| Queue::new(s)).collect();

        let mmds_ns = if allow_mmds_requests {
            Some(MmdsNetworkStack::new_with_defaults(None))
        } else {
            None
        };
        Ok(Net {
            id,
            tap,
            avail_features,
            acked_features: 0u64,
            queues,
            queue_evts,
            rx_rate_limiter,
            tx_rate_limiter,
            rx_deferred_frame: false,
            rx_deferred_irqs: false,
            rx_bytes_read: 0,
            rx_frame_buf: [0u8; MAX_BUFFER_SIZE],
            tx_frame_buf: [0u8; MAX_BUFFER_SIZE],
            tx_iovec: Vec::with_capacity(QUEUE_SIZE as usize),
            irq_trigger: IrqTrigger::new().map_err(Error::EventFd)?,
            device_state: DeviceState::Inactive,
            activate_evt: EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?,
            config_space,
            mmds_ns,
            guest_mac: guest_mac.copied(),

            #[cfg(test)]
            mocks: Mocks::default(),
        })
    }
/// Create a new virtio network device with the given TAP interface.
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(
        id: String,
        tap_if_name: String,
        guest_mac: Option<&MacAddr>,
        rx_rate_limiter: RateLimiter,
        tx_rate_limiter: RateLimiter,
        allow_mmds_requests: bool,
    ) -> Result<Self> {
        let tap = Tap::open_named(&tap_if_name).map_err(Error::TapOpen)?;
        // Set offload flags to match the virtio features below.
        tap.set_offload(
            net_gen::TUN_F_CSUM | net_gen::TUN_F_UFO | net_gen::TUN_F_TSO4 | net_gen::TUN_F_TSO6,
        )
        .map_err(Error::TapSetOffload)?;
        let vnet_hdr_size = vnet_hdr_len() as i32;
        tap.set_vnet_hdr_size(vnet_hdr_size)
            .map_err(Error::TapSetVnetHdrSize)?;
        let mut avail_features = 1 << VIRTIO_NET_F_GUEST_CSUM
            | 1 << VIRTIO_NET_F_CSUM
            | 1 << VIRTIO_NET_F_GUEST_TSO4
            | 1 << VIRTIO_NET_F_GUEST_UFO
            | 1 << VIRTIO_NET_F_HOST_TSO4
            | 1 << VIRTIO_NET_F_HOST_UFO
            | 1 << VIRTIO_F_VERSION_1;
        let mut config_space = ConfigSpace::default();
        if let Some(mac) = guest_mac {
            config_space.guest_mac.copy_from_slice(mac.get_bytes());
            // When this feature isn't available, the driver generates a random MAC address.
            // Otherwise, it should attempt to read the device MAC address from the config space.
            avail_features |= 1 << VIRTIO_NET_F_MAC;
        }
        let mut queue_evts = Vec::new();
        for _ in QUEUE_SIZES.iter() {
            queue_evts.push(EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?);
        }
        let queues = QUEUE_SIZES.iter().map(|&s| Queue::new(s)).collect();
        let mmds_ns = if allow_mmds_requests {
            Some(MmdsNetworkStack::new_with_defaults(None))
        } else {
            None
        };
        Ok(Net {
            id,
            tap,
            avail_features,
            acked_features: 0u64,
            queues,
            queue_evts,
            rx_rate_limiter,
            tx_rate_limiter,
            rx_deferred_frame: false,
            rx_deferred_irqs: false,
            rx_bytes_read: 0,
            rx_frame_buf: [0u8; MAX_BUFFER_SIZE],
            tx_frame_buf: [0u8; MAX_BUFFER_SIZE],
            tx_iovec: Vec::with_capacity(QUEUE_SIZE as usize),
            irq_trigger: IrqTrigger::new().map_err(Error::EventFd)?,
            device_state: DeviceState::Inactive,
            activate_evt: EventFd::new(libc::EFD_NONBLOCK).map_err(Error::EventFd)?,
            config_space,
            mmds_ns,
            guest_mac: guest_mac.copied(),
            #[cfg(test)]
            mocks: Mocks::default(),
        })
    }
pub fn new_with_tap ( id : String , tap_if_name : String , guest_mac : Option < & MacAddr > , rx_rate_limiter : RateLimiter , tx_rate_limiter : RateLimiter , allow_mmds_requests : bool , ) -> Result < Self > { let tap = Tap :: open_named ( & tap_if_name ) . map_err ( Error :: TapOpen ) ? ; // Set offload flags to match the virtio features below. tap . set_offload ( net_gen :: TUN_F_CSUM | net_gen :: TUN_F_UFO | net_gen :: TUN_F_TSO4 | net_gen :: TUN_F_TSO6 , ) . map_err ( Error :: TapSetOffload ) ? ; let vnet_hdr_size = vnet_hdr_len ( ) as i32 ; tap . set_vnet_hdr_size ( vnet_hdr_size ) . map_err ( Error :: TapSetVnetHdrSize ) ? ; let mut avail_features = 1 << VIRTIO_NET_F_GUEST_CSUM | 1 << VIRTIO_NET_F_CSUM | 1 << VIRTIO_NET_F_GUEST_TSO4 | 1 << VIRTIO_NET_F_GUEST_UFO | 1 << VIRTIO_NET_F_HOST_TSO4 | 1 << VIRTIO_NET_F_HOST_UFO | 1 << VIRTIO_F_VERSION_1 ; let mut config_space = ConfigSpace :: default ( ) ; if let Some ( mac ) = guest_mac { config_space . guest_mac . copy_from_slice ( mac . get_bytes ( ) ) ; // When this feature isn't available, the driver generates a random MAC address. // Otherwise, it should attempt to read the device MAC address from the config space. avail_features |= 1 << VIRTIO_NET_F_MAC ; } let mut queue_evts = Vec :: new ( ) ; for _ in QUEUE_SIZES . iter ( ) { queue_evts . push ( EventFd :: new ( libc :: EFD_NONBLOCK ) . map_err ( Error :: EventFd ) ? ) ; } let queues = QUEUE_SIZES . iter ( ) . map ( | & s | Queue :: new ( s ) ) . collect ( ) ; let mmds_ns = if allow_mmds_requests { Some ( MmdsNetworkStack :: new_with_defaults ( None ) ) } else { None } ; Ok ( Net { id , tap , avail_features , acked_features : 0u64 , queues , queue_evts , rx_rate_limiter , tx_rate_limiter , rx_deferred_frame : false , rx_deferred_irqs : false , rx_bytes_read : 0 , rx_frame_buf : [ 0u8 ; MAX_BUFFER_SIZE ] , tx_frame_buf : [ 0u8 ; MAX_BUFFER_SIZE ] , tx_iovec : Vec :: with_capacity ( QUEUE_SIZE as usize ) , irq_trigger : IrqTrigger :: new ( ) . map_err ( Error :: EventFd ) ? , device_state : DeviceState :: Inactive , activate_evt : EventFd :: new ( libc :: EFD_NONBLOCK ) . map_err ( Error :: EventFd ) ? , config_space , mmds_ns , guest_mac : guest_mac . copied ( ) , # [ cfg ( test ) ] mocks : Mocks :: default ( ) , } ) }
Create a new virtio network device with the given TAP interface.
Create a new virtio network device with the given TAP interface .
id
String
tap_if_name
String
guest_mac
Option<&MacAddr>
rx_rate_limiter
RateLimiter
tx_rate_limiter
RateLimiter
allow_mmds_requests
bool
id
String
tap_if_name
String
guest_mac
Option<&MacAddr>
rx_rate_limiter
RateLimiter
tx_rate_limiter
RateLimiter
allow_mmds_requests
bool