11// SPDX-License-Identifier: MIT OR Apache-2.0
22
3+ use core:: ops:: DerefMut ;
34use core:: time:: Duration ;
45
56use uefi:: boot:: ScopedProtocol ;
@@ -43,6 +44,47 @@ fn find_network_device() -> Option<ScopedProtocol<SimpleNetwork>> {
4344 maybe_handle
4445}
4546
47+ /// Receives the next IPv4 packet and prints corresponding metadata.
48+ ///
49+ /// Returns the length of the response.
50+ fn receive ( simple_network : & mut SimpleNetwork , buffer : & mut [ u8 ] ) -> uefi:: Result < usize > {
51+ // Wait for a bit to ensure that the previous packet has been processed.
52+ boot:: stall ( Duration :: from_millis ( 500 ) ) ;
53+
54+ let mut recv_src_mac = MacAddress ( [ 0 ; 32 ] ) ;
55+ let mut recv_dst_mac = MacAddress ( [ 0 ; 32 ] ) ;
56+ let mut recv_ethernet_protocol = 0 ;
57+
58+ let res = simple_network. receive (
59+ buffer,
60+ None ,
61+ Some ( & mut recv_src_mac) ,
62+ Some ( & mut recv_dst_mac) ,
63+ Some ( & mut recv_ethernet_protocol) ,
64+ ) ;
65+
66+ // To simplify debugging when receive an unexpected packet, we print the
67+ // necessary info. This is especially useful if an unexpected IPv4 or ARP
68+ // packet is received, which can easily happen when fiddling around with
69+ // this test.
70+ res. inspect ( |_| {
71+ debug ! ( "Received:" ) ;
72+ debug ! ( " src_mac = {:x?}" , & recv_src_mac. 0 [ 0 ..6 ] ) ;
73+ debug ! ( " dst_mac = {:x?}" , & recv_dst_mac. 0 [ 0 ..6 ] ) ;
74+ debug ! ( " ethernet_proto=0x{:x?}" , recv_ethernet_protocol) ;
75+
76+ // Assert the ethernet frame was sent to the expected interface.
77+ {
78+ // UEFI reports proper DST MAC
79+ assert_eq ! ( recv_dst_mac. 0 [ 0 ..6 ] , EXPECTED_MAC ) ;
80+ }
81+
82+ // Ensure that we do not accidentally get an ARP packet, which we
83+ // do not expect in this test.
84+ assert_eq ! ( recv_ethernet_protocol, ETHERNET_PROTOCOL_IPV4 )
85+ } )
86+ }
87+
4688/// This test sends a simple UDP/IP packet to the `EchoService` (created by
4789/// `cargo xtask run`) and receives its response.
4890pub fn test ( ) {
@@ -55,7 +97,7 @@ pub fn test() {
5597
5698 // The handle to our specific network device, as the test requires also a
5799 // specific environment. We do not test all possible handles.
58- let simple_network = find_network_device ( ) . unwrap_or_else ( || panic ! (
100+ let mut simple_network = find_network_device ( ) . unwrap_or_else ( || panic ! (
59101 "Failed to find SNP handle for network device with MAC address {:x}:{:x}:{:x}:{:x}:{:x}:{:x}" ,
60102 EXPECTED_MAC [ 0 ] ,
61103 EXPECTED_MAC [ 1 ] ,
@@ -148,14 +190,8 @@ pub fn test() {
148190 let mut buffer = [ 0u8 ; 1500 ] ;
149191
150192 info ! ( "Waiting for the reception" ) ;
151- if simple_network. receive ( & mut buffer, None , None , None , None ) == Err ( Status :: NOT_READY . into ( ) )
152- {
153- boot:: stall ( Duration :: from_secs ( 1 ) ) ;
154-
155- simple_network
156- . receive ( & mut buffer, None , None , None , None )
157- . unwrap ( ) ;
158- }
193+ let n = receive ( simple_network. deref_mut ( ) , & mut buffer) . unwrap ( ) ;
194+ debug ! ( "Reply has {n} bytes" ) ;
159195
160196 // Check payload in UDP packet that was reversed by our EchoService.
161197 assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
0 commit comments