1
1
// SPDX-License-Identifier: MIT OR Apache-2.0
2
2
3
+ use core:: ops:: DerefMut ;
3
4
use uefi:: proto:: network:: snp:: { InterruptStatus , NetworkState , ReceiveFlags , SimpleNetwork } ;
4
5
use uefi:: proto:: network:: MacAddress ;
5
6
use uefi:: { boot, Status } ;
6
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
+ debug ! (
17
+ "InterruptStatus (before receive): {:?}" ,
18
+ simple_network. get_interrupt_status( ) ?
19
+ ) ;
20
+ let res = simple_network. receive (
21
+ buffer,
22
+ None ,
23
+ Some ( & mut recv_src_mac) ,
24
+ Some ( & mut recv_dst_mac) ,
25
+ Some ( & mut recv_ethernet_protocol) ,
26
+ ) ;
27
+ debug ! (
28
+ "InterruptStatus (after receive) : {:?}" ,
29
+ simple_network. get_interrupt_status( ) ?
30
+ ) ;
31
+
32
+ res. inspect ( |_| {
33
+ debug ! ( "Received:" ) ;
34
+ debug ! ( " src_mac = {:x?}" , recv_src_mac) ;
35
+ debug ! ( " dst_mac = {:x?}" , recv_dst_mac) ;
36
+ debug ! ( " ethernet_proto=0x{:x?}" , recv_ethernet_protocol) ;
37
+
38
+ // Ensure that we do not accidentally get an ARP packet, which we
39
+ // do not expect in this test.
40
+ assert_eq ! ( recv_ethernet_protocol, ETHERNET_PROTOCOL_IPV4 ) ;
41
+ } )
42
+ }
43
+
44
+ /// This test sends a simple UDP/IP packet to the `EchoService` (created by
45
+ /// `cargo xtask run`) and receives its message.
7
46
pub fn test ( ) {
8
47
info ! ( "Testing the simple network protocol" ) ;
9
48
10
49
let handles = boot:: find_handles :: < SimpleNetwork > ( ) . unwrap_or_default ( ) ;
11
50
12
51
for handle in handles {
13
- let Ok ( simple_network) = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) else {
52
+ let Ok ( mut simple_network) = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) else {
14
53
continue ;
15
54
} ;
16
55
@@ -51,6 +90,12 @@ pub fn test() {
51
90
)
52
91
. expect ( "Failed to set receive filters" ) ;
53
92
93
+ // EthernetFrame(IPv4Packet(UDPPacket(Payload))).
94
+ // The ethernet frame header will be filled by `transmit()`.
95
+ // The UDP packet contains the byte sequence `4, 4, 3, 2, 1`.
96
+ //
97
+ // The packet is sent to the `EchoService` created by
98
+ // `cargo xtask run`. It runs on UDP port 21572.
54
99
let payload = b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \
55
100
\x45 \x00 \
56
101
\x00 \x21 \
@@ -67,7 +112,6 @@ pub fn test() {
67
112
\xa9 \xe4 \
68
113
\x04 \x01 \x02 \x03 \x04 ";
69
114
70
- let dest_addr = MacAddress ( [ 0xffu8 ; 32 ] ) ;
71
115
assert ! ( !simple_network
72
116
. get_interrupt_status( )
73
117
. unwrap( )
@@ -79,8 +123,8 @@ pub fn test() {
79
123
simple_network. mode ( ) . media_header_size as usize ,
80
124
payload,
81
125
None ,
82
- Some ( dest_addr ) ,
83
- Some ( 0x0800 ) ,
126
+ Some ( simple_network . mode ( ) . broadcast_address ) ,
127
+ Some ( ETHERNET_PROTOCOL_IPV4 ) ,
84
128
)
85
129
. expect ( "Failed to transmit frame" ) ;
86
130
@@ -95,16 +139,10 @@ pub fn test() {
95
139
let mut buffer = [ 0u8 ; 1500 ] ;
96
140
97
141
info ! ( "Waiting for the reception" ) ;
98
- if simple_network. receive ( & mut buffer, None , None , None , None )
99
- == Err ( Status :: NOT_READY . into ( ) )
100
- {
101
- boot:: stall ( 1_000_000 ) ;
102
-
103
- simple_network
104
- . receive ( & mut buffer, None , None , None , None )
105
- . unwrap ( ) ;
106
- }
142
+ let n = receive ( simple_network. deref_mut ( ) , & mut buffer) . unwrap ( ) ;
143
+ debug ! ( "Reply has {n} bytes" ) ;
107
144
145
+ // Check payload in UDP packet that was reversed by our EchoService.
108
146
assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
109
147
110
148
// Get stats
0 commit comments