1
1
// SPDX-License-Identifier: MIT OR Apache-2.0
2
2
3
+ use core:: ops:: DerefMut ;
3
4
use core:: time:: Duration ;
4
5
5
6
use uefi:: boot:: ScopedProtocol ;
@@ -43,6 +44,47 @@ fn find_network_device() -> Option<ScopedProtocol<SimpleNetwork>> {
43
44
maybe_handle
44
45
}
45
46
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
+
46
88
/// This test sends a simple UDP/IP packet to the `EchoService` (created by
47
89
/// `cargo xtask run`) and receives its response.
48
90
pub fn test ( ) {
@@ -55,7 +97,7 @@ pub fn test() {
55
97
56
98
// The handle to our specific network device, as the test requires also a
57
99
// 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 ! (
59
101
"Failed to find SNP handle for network device with MAC address {:x}:{:x}:{:x}:{:x}:{:x}:{:x}" ,
60
102
EXPECTED_MAC [ 0 ] ,
61
103
EXPECTED_MAC [ 1 ] ,
@@ -149,14 +191,8 @@ pub fn test() {
149
191
let mut buffer = [ 0u8 ; 1500 ] ;
150
192
151
193
info ! ( "Waiting for the reception" ) ;
152
- if simple_network. receive ( & mut buffer, None , None , None , None ) == Err ( Status :: NOT_READY . into ( ) )
153
- {
154
- boot:: stall ( Duration :: from_secs ( 1 ) ) ;
155
-
156
- simple_network
157
- . receive ( & mut buffer, None , None , None , None )
158
- . unwrap ( ) ;
159
- }
194
+ let n = receive ( simple_network. deref_mut ( ) , & mut buffer) . unwrap ( ) ;
195
+ debug ! ( "Reply has {n} bytes" ) ;
160
196
161
197
// Check payload in UDP packet that was reversed by our EchoService.
162
198
assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
0 commit comments