11// SPDX-License-Identifier: MIT OR Apache-2.0
22
3- use core:: time:: Duration ;
4-
3+ use core:: ops:: DerefMut ;
54use uefi:: proto:: network:: MacAddress ;
65use uefi:: proto:: network:: snp:: { InterruptStatus , NetworkState , ReceiveFlags , SimpleNetwork } ;
76use uefi:: { Status , boot} ;
87
8+ const ETHERNET_PROTOCOL_IPV4 : u16 = 0x0800 ;
9+
10+ /// Receives the next IPv4 packet and prints corresponding metadata.
11+ fn receive ( simple_network : & mut SimpleNetwork , buffer : & mut [ u8 ] ) -> uefi:: Result < usize > {
12+ let mut recv_src_mac = MacAddress ( [ 0 ; 32 ] ) ;
13+ let mut recv_dst_mac = MacAddress ( [ 0 ; 32 ] ) ;
14+ let mut recv_ethernet_protocol = 0 ;
15+
16+ let res = simple_network. receive (
17+ buffer,
18+ None ,
19+ Some ( & mut recv_src_mac) ,
20+ Some ( & mut recv_dst_mac) ,
21+ Some ( & mut recv_ethernet_protocol) ,
22+ ) ;
23+
24+ res. inspect ( |_| {
25+ debug ! ( "Received:" ) ;
26+ debug ! ( " src_mac = {:x?}" , recv_src_mac) ;
27+ debug ! ( " dst_mac = {:x?}" , recv_dst_mac) ;
28+ debug ! ( " ethernet_proto=0x{:x?}" , recv_ethernet_protocol) ;
29+
30+ // Ensure that we do not accidentally get an ARP packet, which we
31+ // do not expect in this test.
32+ assert_eq ! ( recv_ethernet_protocol, ETHERNET_PROTOCOL_IPV4 ) ;
33+ } )
34+ }
35+
36+ /// This test sends a simple UDP/IP packet to the `EchoService` (created by
37+ /// `cargo xtask run`) and receives its message.
938pub fn test ( ) {
1039 info ! ( "Testing the simple network protocol" ) ;
1140
1241 let handles = boot:: find_handles :: < SimpleNetwork > ( ) . unwrap_or_default ( ) ;
1342
1443 for handle in handles {
15- let Ok ( simple_network) = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) else {
44+ let Ok ( mut simple_network) = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) else {
1645 continue ;
1746 } ;
1847
@@ -55,6 +84,12 @@ pub fn test() {
5584 )
5685 . expect ( "Failed to set receive filters" ) ;
5786
87+ // EthernetFrame(IPv4Packet(UDPPacket(Payload))).
88+ // The ethernet frame header will be filled by `transmit()`.
89+ // The UDP packet contains the byte sequence `4, 4, 3, 2, 1`.
90+ //
91+ // The packet is sent to the `EchoService` created by
92+ // `cargo xtask run`. It runs on UDP port 21572.
5893 let payload = b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \
5994 \x45 \x00 \
6095 \x00 \x21 \
@@ -71,7 +106,6 @@ pub fn test() {
71106 \xa9 \xe4 \
72107 \x04 \x01 \x02 \x03 \x04 ";
73108
74- let dest_addr = MacAddress ( [ 0xffu8 ; 32 ] ) ;
75109 assert ! (
76110 !simple_network
77111 . get_interrupt_status( )
@@ -85,8 +119,8 @@ pub fn test() {
85119 simple_network. mode ( ) . media_header_size as usize ,
86120 payload,
87121 None ,
88- Some ( dest_addr ) ,
89- Some ( 0x0800 ) ,
122+ Some ( simple_network . mode ( ) . broadcast_address ) ,
123+ Some ( ETHERNET_PROTOCOL_IPV4 ) ,
90124 )
91125 . expect ( "Failed to transmit frame" ) ;
92126
@@ -101,16 +135,10 @@ pub fn test() {
101135 let mut buffer = [ 0u8 ; 1500 ] ;
102136
103137 info ! ( "Waiting for the reception" ) ;
104- if simple_network. receive ( & mut buffer, None , None , None , None )
105- == Err ( Status :: NOT_READY . into ( ) )
106- {
107- boot:: stall ( Duration :: from_secs ( 1 ) ) ;
108-
109- simple_network
110- . receive ( & mut buffer, None , None , None , None )
111- . unwrap ( ) ;
112- }
138+ let n = receive ( simple_network. deref_mut ( ) , & mut buffer) . unwrap ( ) ;
139+ debug ! ( "Reply has {n} bytes" ) ;
113140
141+ // Check payload in UDP packet that was reversed by our EchoService.
114142 assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
115143
116144 // Get stats
0 commit comments