1
1
// SPDX-License-Identifier: MIT OR Apache-2.0
2
2
3
- use core:: time:: Duration ;
4
-
3
+ use core:: ops:: DerefMut ;
5
4
use uefi:: proto:: network:: MacAddress ;
6
5
use uefi:: proto:: network:: snp:: { InterruptStatus , NetworkState , ReceiveFlags , SimpleNetwork } ;
7
6
use uefi:: { Status , boot} ;
8
7
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.
9
38
pub fn test ( ) {
10
39
info ! ( "Testing the simple network protocol" ) ;
11
40
12
41
let handles = boot:: find_handles :: < SimpleNetwork > ( ) . unwrap_or_default ( ) ;
13
42
14
43
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 {
16
45
continue ;
17
46
} ;
18
47
@@ -55,6 +84,12 @@ pub fn test() {
55
84
)
56
85
. expect ( "Failed to set receive filters" ) ;
57
86
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.
58
93
let payload = b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \
59
94
\x45 \x00 \
60
95
\x00 \x21 \
@@ -71,7 +106,6 @@ pub fn test() {
71
106
\xa9 \xe4 \
72
107
\x04 \x01 \x02 \x03 \x04 ";
73
108
74
- let dest_addr = MacAddress ( [ 0xffu8 ; 32 ] ) ;
75
109
assert ! (
76
110
!simple_network
77
111
. get_interrupt_status( )
@@ -85,8 +119,8 @@ pub fn test() {
85
119
simple_network. mode ( ) . media_header_size as usize ,
86
120
payload,
87
121
None ,
88
- Some ( dest_addr ) ,
89
- Some ( 0x0800 ) ,
122
+ Some ( simple_network . mode ( ) . broadcast_address ) ,
123
+ Some ( ETHERNET_PROTOCOL_IPV4 ) ,
90
124
)
91
125
. expect ( "Failed to transmit frame" ) ;
92
126
@@ -101,16 +135,10 @@ pub fn test() {
101
135
let mut buffer = [ 0u8 ; 1500 ] ;
102
136
103
137
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" ) ;
113
140
141
+ // Check payload in UDP packet that was reversed by our EchoService.
114
142
assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
115
143
116
144
// Get stats
0 commit comments