fn write_to_mmds_or_tap(
        mmds_ns: Option<&mut MmdsNetworkStack>,
        rate_limiter: &mut RateLimiter,
        frame_buf: &[u8],
        tap: &mut Tap,
        guest_mac: Option<MacAddr>,
    ) -> Result<bool> {
        let checked_frame = |frame_buf| {
            frame_bytes_from_buf(frame_buf).map_err(|e| {
                error!("VNET header missing in the TX frame.");
                METRICS.net.tx_malformed_frames.inc();
                e
            })
        };
        if let Some(ns) = mmds_ns {
            if ns.detour_frame(checked_frame(frame_buf)?) {
                METRICS.mmds.rx_accepted.inc();

                // MMDS frames are not accounted by the rate limiter.
                rate_limiter.manual_replenish(frame_buf.len() as u64, TokenType::Bytes);
                rate_limiter.manual_replenish(1, TokenType::Ops);

                // MMDS consumed the frame.
                return Ok(true);
            }
        }

        // This frame goes to the TAP.

        // Check for guest MAC spoofing.
        if let Some(mac) = guest_mac {
            let _ = EthernetFrame::from_bytes(checked_frame(frame_buf)?).map(|eth_frame| {
                if mac != eth_frame.src_mac() {
                    METRICS.net.tx_spoofed_mac_count.inc();
                }
            });
        }

        match tap.write(frame_buf) {
            Ok(_) => {
                METRICS.net.tx_bytes_count.add(frame_buf.len());
                METRICS.net.tx_packets_count.inc();
                METRICS.net.tx_count.inc();
            }
            Err(e) => {
                error!("Failed to write to tap: {:?}", e);
                METRICS.net.tap_write_fails.inc();
            }
        };
        Ok(false)
    }
// Tries to detour the frame to MMDS and if MMDS doesn't accept it, sends it on the host TAP.
//
// `frame_buf` should contain the frame bytes in a slice of exact length.
// Returns whether MMDS consumed the frame.
Tries to detour the frame to MMDS and if MMDS doesn't accept it, sends it on the host TAP.
`frame_buf` should contain the frame bytes in a slice of exact length.
Returns whether MMDS consumed the frame.
Tries to detour the frame to MMDS and if MMDS doesn ' t accept it sends it on the host TAP . ` frame_buf ` should contain the frame bytes in a slice of exact length . Returns whether MMDS consumed the frame .
fn write_to_mmds_or_tap(
        mmds_ns: Option<&mut MmdsNetworkStack>,
        rate_limiter: &mut RateLimiter,
        frame_buf: &[u8],
        tap: &mut Tap,
        guest_mac: Option<MacAddr>,
    ) -> Result<bool> {
        let checked_frame = |frame_buf| {
            frame_bytes_from_buf(frame_buf).map_err(|e| {
                error!("VNET header missing in the TX frame.");
                METRICS.net.tx_malformed_frames.inc();
                e
            })
        };
        if let Some(ns) = mmds_ns {
            if ns.detour_frame(checked_frame(frame_buf)?) {
                METRICS.mmds.rx_accepted.inc();
                // MMDS frames are not accounted by the rate limiter.
                rate_limiter.manual_replenish(frame_buf.len() as u64, TokenType::Bytes);
                rate_limiter.manual_replenish(1, TokenType::Ops);
                // MMDS consumed the frame.
                return Ok(true);
            }
        }
        // This frame goes to the TAP.
        // Check for guest MAC spoofing.
        if let Some(mac) = guest_mac {
            let _ = EthernetFrame::from_bytes(checked_frame(frame_buf)?).map(|eth_frame| {
                if mac != eth_frame.src_mac() {
                    METRICS.net.tx_spoofed_mac_count.inc();
                }
            });
        }
        match tap.write(frame_buf) {
            Ok(_) => {
                METRICS.net.tx_bytes_count.add(frame_buf.len());
                METRICS.net.tx_packets_count.inc();
                METRICS.net.tx_count.inc();
            }
            Err(e) => {
                error!("Failed to write to tap: {:?}", e);
                METRICS.net.tap_write_fails.inc();
            }
        };
        Ok(false)
    }
fn write_to_mmds_or_tap ( mmds_ns : Option < & mut MmdsNetworkStack > , rate_limiter : & mut RateLimiter , frame_buf : & [ u8 ] , tap : & mut Tap , guest_mac : Option < MacAddr > , ) -> Result < bool > { let checked_frame = | frame_buf | { frame_bytes_from_buf ( frame_buf ) . map_err ( | e | { error ! ( " " ) ; METRICS . net . tx_malformed_frames . inc ( ) ; e } ) } ; if let Some ( ns ) = mmds_ns { if ns . detour_frame ( checked_frame ( frame_buf ) ? ) { METRICS . mmds . rx_accepted . inc ( ) ; // MMDS frames are not accounted by the rate limiter. rate_limiter . manual_replenish ( frame_buf . len ( ) as u64 , TokenType :: Bytes ) ; rate_limiter . manual_replenish ( 1 , TokenType :: Ops ) ; // MMDS consumed the frame. return Ok ( true ) ; } } // This frame goes to the TAP. // Check for guest MAC spoofing. if let Some ( mac ) = guest_mac { let _ = EthernetFrame :: from_bytes ( checked_frame ( frame_buf ) ? ) . map ( | eth_frame | { if mac != eth_frame . src_mac ( ) { METRICS . net . tx_spoofed_mac_count . inc ( ) ; } } ) ; } match tap . write ( frame_buf ) { Ok ( _ ) => { METRICS . net . tx_bytes_count . add ( frame_buf . len ( ) ) ; METRICS . net . tx_packets_count . inc ( ) ; METRICS . net . tx_count . inc ( ) ; } Err ( e ) => { error ! ( " " e ) ; METRICS . net . tap_write_fails . inc ( ) ; } } ; Ok ( false ) }
Tries to detour the frame to MMDS and if MMDS doesn't accept it, sends it on the host TAP.
Tries to detour the frame to MMDS and if MMDS doesn ' t accept it sends it on the host TAP .
mmds_ns
Option<&mut MmdsNetworkStack>
rate_limiter
&mut RateLimiter
frame_buf
&[u8]
tap
&mut Tap
guest_mac
Option<MacAddr>
mmds_ns
Option<&mut MmdsNetworkStack>
rate_limiter
&mut RateLimiter
frame_buf
&[u8]
tap
&mut Tap
guest_mac
Option<MacAddr>