Skip to content

Commit d80efd5

Browse files
committed
integration-test: SNP: improve clarity
Interestingly, the interrupt status never shows that we can receive a packet. Buggy OVMF firmware?
1 parent d655bb8 commit d80efd5

File tree

1 file changed

+43
-13
lines changed
  • uefi-test-runner/src/proto/network

1 file changed

+43
-13
lines changed

uefi-test-runner/src/proto/network/snp.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
// SPDX-License-Identifier: MIT OR Apache-2.0
22

3+
use core::ops::DerefMut;
34
use uefi::proto::network::snp::{InterruptStatus, NetworkState, ReceiveFlags, SimpleNetwork};
45
use uefi::proto::network::MacAddress;
56
use uefi::{boot, Status};
67

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.
738
pub fn test() {
839
info!("Testing the simple network protocol");
940

1041
let handles = boot::find_handles::<SimpleNetwork>().unwrap_or_default();
1142

1243
for handle in handles {
13-
let Ok(simple_network) = boot::open_protocol_exclusive::<SimpleNetwork>(handle) else {
44+
let Ok(mut simple_network) = boot::open_protocol_exclusive::<SimpleNetwork>(handle) else {
1445
continue;
1546
};
1647

@@ -53,6 +84,12 @@ pub fn test() {
5384
)
5485
.expect("Failed to set receive filters");
5586

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.
5693
let payload = b"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\
5794
\x45\x00\
5895
\x00\x21\
@@ -69,7 +106,6 @@ pub fn test() {
69106
\xa9\xe4\
70107
\x04\x01\x02\x03\x04";
71108

72-
let dest_addr = MacAddress([0xffu8; 32]);
73109
assert!(!simple_network
74110
.get_interrupt_status()
75111
.unwrap()
@@ -81,8 +117,8 @@ pub fn test() {
81117
simple_network.mode().media_header_size as usize,
82118
payload,
83119
None,
84-
Some(dest_addr),
85-
Some(0x0800),
120+
Some(simple_network.mode().broadcast_address),
121+
Some(ETHERNET_PROTOCOL_IPV4),
86122
)
87123
.expect("Failed to transmit frame");
88124

@@ -97,16 +133,10 @@ pub fn test() {
97133
let mut buffer = [0u8; 1500];
98134

99135
info!("Waiting for the reception");
100-
if simple_network.receive(&mut buffer, None, None, None, None)
101-
== Err(Status::NOT_READY.into())
102-
{
103-
boot::stall(1_000_000);
104-
105-
simple_network
106-
.receive(&mut buffer, None, None, None, None)
107-
.unwrap();
108-
}
136+
let n = receive(simple_network.deref_mut(), &mut buffer).unwrap();
137+
debug!("Reply has {n} bytes");
109138

139+
// Check payload in UDP packet that was reversed by our EchoService.
110140
assert_eq!(buffer[42..47], [4, 4, 3, 2, 1]);
111141

112142
// Get stats

0 commit comments

Comments
 (0)