@@ -45,7 +45,10 @@ pub(crate) enum NetworkState<'a> {
4545 feature = "virtio-net" ,
4646) ) ]
4747pub ( crate ) fn network_handler ( ) {
48- NIC . lock ( ) . as_nic_mut ( ) . unwrap ( ) . handle_interrupt ( ) ;
48+ // It is possible for us to receive interrupts before we are done with initializing the network interface.
49+ if let Ok ( nic) = NIC . lock ( ) . as_nic_mut ( ) {
50+ nic. handle_interrupt ( ) ;
51+ }
4952}
5053
5154impl < ' a > NetworkState < ' a > {
@@ -63,13 +66,20 @@ static LOCAL_ENDPOINT: AtomicU16 = AtomicU16::new(0);
6366pub ( crate ) static NIC : InterruptTicketMutex < NetworkState < ' _ > > =
6467 InterruptTicketMutex :: new ( NetworkState :: Missing ) ;
6568
69+ cfg_if:: cfg_if! {
70+ if #[ cfg( feature = "trace" ) ] {
71+ type InterfaceDevice = smoltcp:: phy:: Tracer <NetworkDevice >;
72+ } else if #[ cfg( feature = "pcap" ) ] {
73+ type InterfaceDevice = smoltcp:: phy:: PcapWriter <NetworkDevice , crate :: executor:: device:: FileSink >;
74+ } else {
75+ type InterfaceDevice = NetworkDevice ;
76+ }
77+ }
78+
6679pub ( crate ) struct NetworkInterface < ' a > {
6780 pub ( super ) iface : smoltcp:: iface:: Interface ,
6881 pub ( super ) sockets : SocketSet < ' a > ,
69- #[ cfg( feature = "trace" ) ]
70- pub ( super ) device : smoltcp:: phy:: Tracer < NetworkDevice > ,
71- #[ cfg( not( feature = "trace" ) ) ]
72- pub ( super ) device : NetworkDevice ,
82+ pub ( super ) device : InterfaceDevice ,
7383 #[ cfg( feature = "dhcpv4" ) ]
7484 pub ( super ) dhcp_handle : SocketHandle ,
7585 #[ cfg( feature = "dns" ) ]
@@ -344,16 +354,22 @@ impl<'a> NetworkInterface<'a> {
344354 feature = "virtio-net" ,
345355 ) ) ]
346356 fn handle_interrupt ( & mut self ) {
347- #[ cfg( feature = "trace" ) ]
348- self . device . get_mut ( ) . handle_interrupt ( ) ;
349- #[ cfg( not( feature = "trace" ) ) ]
350- self . device . handle_interrupt ( ) ;
357+ cfg_if:: cfg_if! {
358+ if #[ cfg( any( feature = "trace" , feature="pcap" ) ) ] {
359+ self . device. get_mut( ) . handle_interrupt( ) ;
360+ } else {
361+ self . device. handle_interrupt( ) ;
362+ }
363+ }
351364 }
352365
353366 pub ( crate ) fn set_polling_mode ( & mut self , value : bool ) {
354- #[ cfg( feature = "trace" ) ]
355- self . device . get_mut ( ) . set_polling_mode ( value) ;
356- #[ cfg( not( feature = "trace" ) ) ]
357- self . device . set_polling_mode ( value) ;
367+ cfg_if:: cfg_if! {
368+ if #[ cfg( any( feature = "trace" , feature="pcap" ) ) ] {
369+ self . device. get_mut( ) . set_polling_mode( value) ;
370+ } else {
371+ self . device. set_polling_mode( value) ;
372+ }
373+ }
358374 }
359375}
0 commit comments