2
2
3
3
use core:: time:: Duration ;
4
4
5
+ use uefi:: boot:: ScopedProtocol ;
5
6
use uefi:: proto:: network:: MacAddress ;
6
7
use uefi:: proto:: network:: snp:: { InterruptStatus , ReceiveFlags , SimpleNetwork } ;
7
8
use uefi:: { Status , boot} ;
8
9
9
- pub fn test ( ) {
10
- // This test currently depends on the PXE test running first.
11
- if cfg ! ( not( feature = "pxe" ) ) {
12
- return ;
13
- }
10
+ /// The MAC address configured for the interface.
11
+ const EXPECTED_MAC : [ u8 ; 6 ] = [ 0x52 , 0x54 , 0 , 0 , 0 , 0x1 ] ;
14
12
15
- info ! ( "Testing the simple network protocol" ) ;
13
+ fn find_network_device ( ) -> Option < ScopedProtocol < SimpleNetwork > > {
14
+ let mut maybe_handle = None ;
16
15
17
16
let handles = boot:: find_handles :: < SimpleNetwork > ( ) . unwrap_or_default ( ) ;
18
17
18
+ // We iterate over all handles until we found the right network device.
19
19
for handle in handles {
20
- let simple_network = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) ;
21
- if simple_network. is_err ( ) {
20
+ let Ok ( handle) = boot:: open_protocol_exclusive :: < SimpleNetwork > ( handle) else {
22
21
continue ;
23
- }
24
- let simple_network = simple_network. unwrap ( ) ;
25
-
26
- // Check shutdown
27
- let res = simple_network. shutdown ( ) ;
28
- assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: NOT_STARTED . into( ) ) ) ;
22
+ } ;
29
23
30
- // Check stop
31
- let res = simple_network. stop ( ) ;
32
- assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: NOT_STARTED . into( ) ) ) ;
24
+ // Check media is present
25
+ if !bool:: from ( handle. mode ( ) . media_present_supported )
26
+ || !bool:: from ( handle. mode ( ) . media_present )
27
+ {
28
+ continue ;
29
+ }
33
30
34
- // Check start
35
- simple_network
36
- . start ( )
37
- . expect ( "Failed to start Simple Network" ) ;
31
+ // Check MAC address
32
+ let has_mac = handle. mode ( ) . current_address . 0 [ 0 ..6 ] == EXPECTED_MAC
33
+ && handle. mode ( ) . permanent_address . 0 [ 0 ..6 ] == EXPECTED_MAC ;
34
+ if !has_mac {
35
+ continue ;
36
+ }
38
37
39
- // Check initialize
40
- simple_network
41
- . initialize ( 0 , 0 )
42
- . expect ( "Failed to initialize Simple Network" ) ;
38
+ maybe_handle. replace ( handle) ;
39
+ }
43
40
44
- // edk2 virtio-net driver does not support statistics, so
45
- // allow UNSUPPORTED (same for collect_statistics below).
46
- let res = simple_network. reset_statistics ( ) ;
47
- assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: UNSUPPORTED . into( ) ) ) ;
41
+ maybe_handle
42
+ }
48
43
49
- // Reading the interrupt status clears it
50
- simple_network. get_interrupt_status ( ) . unwrap ( ) ;
44
+ pub fn test ( ) {
45
+ // This test currently depends on the PXE test running first.
46
+ if cfg ! ( not( feature = "pxe" ) ) {
47
+ return ;
48
+ }
51
49
52
- // Set receive filters
53
- simple_network
54
- . receive_filters (
55
- ReceiveFlags :: UNICAST | ReceiveFlags :: BROADCAST ,
56
- ReceiveFlags :: empty ( ) ,
57
- false ,
58
- None ,
59
- )
60
- . expect ( "Failed to set receive filters" ) ;
61
-
62
- // Check media
63
- if !bool:: from ( simple_network. mode ( ) . media_present_supported )
64
- || !bool:: from ( simple_network. mode ( ) . media_present )
65
- {
66
- continue ;
67
- }
50
+ info ! ( "Testing the simple network protocol" ) ;
68
51
69
- let payload = b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \
52
+ // The handle to our specific network device, as the test requires also a
53
+ // specific environment. We do not test all possible handles.
54
+ let simple_network = find_network_device ( ) . unwrap_or_else ( || panic ! (
55
+ "Failed to find SNP handle for network device with MAC address {:x}:{:x}:{:x}:{:x}:{:x}:{:x}" ,
56
+ EXPECTED_MAC [ 0 ] ,
57
+ EXPECTED_MAC [ 1 ] ,
58
+ EXPECTED_MAC [ 2 ] ,
59
+ EXPECTED_MAC [ 3 ] ,
60
+ EXPECTED_MAC [ 4 ] ,
61
+ EXPECTED_MAC [ 5 ]
62
+ ) ) ;
63
+
64
+ // Check shutdown
65
+ let res = simple_network. shutdown ( ) ;
66
+ assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: NOT_STARTED . into( ) ) ) ;
67
+
68
+ // Check stop
69
+ let res = simple_network. stop ( ) ;
70
+ assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: NOT_STARTED . into( ) ) ) ;
71
+
72
+ // Check start
73
+ simple_network
74
+ . start ( )
75
+ . expect ( "Failed to start Simple Network" ) ;
76
+
77
+ // Check initialize
78
+ simple_network
79
+ . initialize ( 0 , 0 )
80
+ . expect ( "Failed to initialize Simple Network" ) ;
81
+
82
+ // edk2 virtio-net driver does not support statistics, so
83
+ // allow UNSUPPORTED (same for collect_statistics below).
84
+ let res = simple_network. reset_statistics ( ) ;
85
+ assert ! ( res == Ok ( ( ) ) || res == Err ( Status :: UNSUPPORTED . into( ) ) ) ;
86
+
87
+ // Reading the interrupt status clears it
88
+ simple_network. get_interrupt_status ( ) . unwrap ( ) ;
89
+
90
+ // Set receive filters
91
+ simple_network
92
+ . receive_filters (
93
+ ReceiveFlags :: UNICAST | ReceiveFlags :: BROADCAST ,
94
+ ReceiveFlags :: empty ( ) ,
95
+ false ,
96
+ None ,
97
+ )
98
+ . expect ( "Failed to set receive filters" ) ;
99
+
100
+ let payload = b"\0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \
70
101
\x45 \x00 \
71
102
\x00 \x21 \
72
103
\x00 \x01 \
@@ -82,64 +113,62 @@ pub fn test() {
82
113
\xa9 \xe4 \
83
114
\x04 \x01 \x02 \x03 \x04 ";
84
115
85
- let dest_addr = MacAddress ( [ 0xffu8 ; 32 ] ) ;
86
- assert ! (
87
- !simple_network
88
- . get_interrupt_status( )
89
- . unwrap( )
90
- . contains( InterruptStatus :: TRANSMIT )
91
- ) ;
92
-
93
- // Send the frame
94
- simple_network
95
- . transmit (
96
- simple_network. mode ( ) . media_header_size as usize ,
97
- payload,
98
- None ,
99
- Some ( dest_addr) ,
100
- Some ( 0x0800 ) ,
101
- )
102
- . expect ( "Failed to transmit frame" ) ;
103
-
104
- info ! ( "Waiting for the transmit" ) ;
105
- while !simple_network
116
+ let dest_addr = MacAddress ( [ 0xffu8 ; 32 ] ) ;
117
+ assert ! (
118
+ !simple_network
106
119
. get_interrupt_status( )
107
120
. unwrap( )
108
121
. contains( InterruptStatus :: TRANSMIT )
109
- { }
110
-
111
- // Attempt to receive a frame
112
- let mut buffer = [ 0u8 ; 1500 ] ;
122
+ ) ;
123
+
124
+ // Send the frame
125
+ simple_network
126
+ . transmit (
127
+ simple_network. mode ( ) . media_header_size as usize ,
128
+ payload,
129
+ None ,
130
+ Some ( dest_addr) ,
131
+ Some ( 0x0800 ) ,
132
+ )
133
+ . expect ( "Failed to transmit frame" ) ;
134
+
135
+ info ! ( "Waiting for the transmit" ) ;
136
+ while !simple_network
137
+ . get_interrupt_status ( )
138
+ . unwrap ( )
139
+ . contains ( InterruptStatus :: TRANSMIT )
140
+ { }
141
+
142
+ // Attempt to receive a frame
143
+ let mut buffer = [ 0u8 ; 1500 ] ;
144
+
145
+ info ! ( "Waiting for the reception" ) ;
146
+ if simple_network. receive ( & mut buffer, None , None , None , None ) == Err ( Status :: NOT_READY . into ( ) )
147
+ {
148
+ boot:: stall ( Duration :: from_secs ( 1 ) ) ;
113
149
114
- info ! ( "Waiting for the reception" ) ;
115
- if simple_network. receive ( & mut buffer, None , None , None , None )
116
- == Err ( Status :: NOT_READY . into ( ) )
117
- {
118
- boot:: stall ( Duration :: from_secs ( 1 ) ) ;
119
-
120
- simple_network
121
- . receive ( & mut buffer, None , None , None , None )
122
- . unwrap ( ) ;
123
- }
150
+ simple_network
151
+ . receive ( & mut buffer, None , None , None , None )
152
+ . unwrap ( ) ;
153
+ }
124
154
125
- assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
155
+ assert_eq ! ( buffer[ 42 ..47 ] , [ 4 , 4 , 3 , 2 , 1 ] ) ;
126
156
127
- // Get stats
128
- let res = simple_network. collect_statistics ( ) ;
129
- match res {
130
- Ok ( stats) => {
131
- info ! ( "Stats: {:?}" , stats) ;
157
+ // Get stats
158
+ let res = simple_network. collect_statistics ( ) ;
159
+ match res {
160
+ Ok ( stats) => {
161
+ info ! ( "Stats: {:?}" , stats) ;
132
162
133
- // One frame should have been transmitted and one received
134
- assert_eq ! ( stats. tx_total_frames( ) . unwrap( ) , 1 ) ;
135
- assert_eq ! ( stats. rx_total_frames( ) . unwrap( ) , 1 ) ;
136
- }
137
- Err ( e) => {
138
- if e == Status :: UNSUPPORTED . into ( ) {
139
- info ! ( "Stats: unsupported." ) ;
140
- } else {
141
- panic ! ( "{e}" ) ;
142
- }
163
+ // One frame should have been transmitted and one received
164
+ assert_eq ! ( stats. tx_total_frames( ) . unwrap( ) , 1 ) ;
165
+ assert_eq ! ( stats. rx_total_frames( ) . unwrap( ) , 1 ) ;
166
+ }
167
+ Err ( e) => {
168
+ if e == Status :: UNSUPPORTED . into ( ) {
169
+ info ! ( "Stats: unsupported." ) ;
170
+ } else {
171
+ panic ! ( "{e}" ) ;
143
172
}
144
173
}
145
174
}
0 commit comments