Skip to content

Commit 6270e6a

Browse files
committed
integration-test: Use veth pair instead of lo
1 parent 9f28e3e commit 6270e6a

File tree

4 files changed

+193
-57
lines changed

4 files changed

+193
-57
lines changed

test/integration-test/src/tests/load.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ use aya::{
1818
use aya_obj::programs::XdpAttachType;
1919
use test_log::test;
2020

21+
use crate::utils::NetNsGuard;
22+
2123
const MAX_RETRIES: usize = 100;
2224
const RETRY_DURATION: Duration = Duration::from_millis(10);
2325

2426
#[test]
2527
fn long_name() {
28+
let netns = NetNsGuard::new();
29+
2630
let mut bpf = Ebpf::load(crate::NAME_TEST).unwrap();
2731
let name_prog: &mut Xdp = bpf
2832
.program_mut("ihaveaverylongname")
2933
.unwrap()
3034
.try_into()
3135
.unwrap();
3236
name_prog.load().unwrap();
33-
name_prog.attach("lo", XdpFlags::default()).unwrap();
37+
name_prog
38+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
39+
.unwrap();
3440

3541
// We used to be able to assert with bpftool that the program name was short.
3642
// It seem though that it now uses the name from the ELF symbol table instead.
@@ -198,11 +204,15 @@ fn assert_unloaded(name: &str) {
198204

199205
#[test]
200206
fn unload_xdp() {
207+
let netns = NetNsGuard::new();
208+
201209
let mut bpf = Ebpf::load(crate::TEST).unwrap();
202210
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
203211
prog.load().unwrap();
204212
assert_loaded("pass");
205-
let link = prog.attach("lo", XdpFlags::default()).unwrap();
213+
let link = prog
214+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
215+
.unwrap();
206216
{
207217
let _link_owned = prog.take_link(link).unwrap();
208218
prog.unload().unwrap();
@@ -213,7 +223,8 @@ fn unload_xdp() {
213223
prog.load().unwrap();
214224

215225
assert_loaded("pass");
216-
prog.attach("lo", XdpFlags::default()).unwrap();
226+
prog.attach_to_if_index(netns.if_idx2, XdpFlags::default())
227+
.unwrap();
217228

218229
assert_loaded("pass");
219230
prog.unload().unwrap();
@@ -351,15 +362,19 @@ fn pin_link() {
351362
return;
352363
}
353364

365+
let netns = NetNsGuard::new();
366+
354367
let mut bpf = Ebpf::load(crate::TEST).unwrap();
355368
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
356369
prog.load().unwrap();
357-
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
370+
let link_id = prog
371+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
372+
.unwrap();
358373
let link = prog.take_link(link_id).unwrap();
359374
assert_loaded("pass");
360375

361376
let fd_link: FdLink = link.try_into().unwrap();
362-
let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap();
377+
let pinned = fd_link.pin("/sys/fs/bpf/aya-xdp-test-veth-pair").unwrap();
363378

364379
// because of the pin, the program is still attached
365380
prog.unload().unwrap();
@@ -382,6 +397,8 @@ fn pin_lifecycle() {
382397
return;
383398
}
384399

400+
let netns = NetNsGuard::new();
401+
385402
// 1. Load Program and Pin
386403
{
387404
let mut bpf = Ebpf::load(crate::PASS).unwrap();
@@ -405,10 +422,12 @@ fn pin_lifecycle() {
405422
{
406423
let mut prog =
407424
Xdp::from_pin("/sys/fs/bpf/aya-xdp-test-prog", XdpAttachType::Interface).unwrap();
408-
let link_id = prog.attach("lo", XdpFlags::default()).unwrap();
425+
let link_id = prog
426+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
427+
.unwrap();
409428
let link = prog.take_link(link_id).unwrap();
410429
let fd_link: FdLink = link.try_into().unwrap();
411-
fd_link.pin("/sys/fs/bpf/aya-xdp-test-lo").unwrap();
430+
fd_link.pin("/sys/fs/bpf/aya-xdp-test-veth-pair").unwrap();
412431

413432
// Unpin the program. It will stay attached since its links were pinned.
414433
prog.unpin().unwrap();
@@ -423,7 +442,7 @@ fn pin_lifecycle() {
423442
let prog: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
424443
prog.load().unwrap();
425444

426-
let link = PinnedLink::from_pin("/sys/fs/bpf/aya-xdp-test-lo")
445+
let link = PinnedLink::from_pin("/sys/fs/bpf/aya-xdp-test-veth-pair")
427446
.unwrap()
428447
.unpin()
429448
.unwrap();

test/integration-test/src/tests/smoke.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ fn xdp() {
1616
return;
1717
}
1818

19-
let _netns = NetNsGuard::new();
19+
let netns = NetNsGuard::new();
2020

2121
let mut bpf = Ebpf::load(crate::PASS).unwrap();
2222
let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
2323
dispatcher.load().unwrap();
24-
dispatcher.attach("lo", XdpFlags::default()).unwrap();
24+
dispatcher
25+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
26+
.unwrap();
2527
}
2628

2729
#[test]
@@ -54,12 +56,13 @@ fn extension() {
5456
return;
5557
}
5658

57-
let _netns = NetNsGuard::new();
59+
let netns = NetNsGuard::new();
5860

5961
let mut bpf = Ebpf::load(crate::MAIN).unwrap();
6062
let pass: &mut Xdp = bpf.program_mut("xdp_pass").unwrap().try_into().unwrap();
6163
pass.load().unwrap();
62-
pass.attach("lo", XdpFlags::default()).unwrap();
64+
pass.attach_to_if_index(netns.if_idx2, XdpFlags::default())
65+
.unwrap();
6366

6467
let mut bpf = EbpfLoader::new()
6568
.extension("xdp_drop")
@@ -73,11 +76,15 @@ fn extension() {
7376

7477
#[test]
7578
fn list_loaded_programs() {
79+
let netns = NetNsGuard::new();
80+
7681
// Load a program.
7782
let mut bpf = Ebpf::load(crate::PASS).unwrap();
7883
let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
7984
dispatcher.load().unwrap();
80-
dispatcher.attach("lo", XdpFlags::default()).unwrap();
85+
dispatcher
86+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
87+
.unwrap();
8188

8289
// Ensure the loaded_programs() api doesn't panic.
8390
let prog = loaded_programs()
@@ -102,11 +109,15 @@ fn list_loaded_programs() {
102109

103110
#[test]
104111
fn list_loaded_maps() {
112+
let netns = NetNsGuard::new();
113+
105114
// Load a program with maps.
106115
let mut bpf = Ebpf::load(crate::MAP_TEST).unwrap();
107116
let dispatcher: &mut Xdp = bpf.program_mut("pass").unwrap().try_into().unwrap();
108117
dispatcher.load().unwrap();
109-
dispatcher.attach("lo", XdpFlags::default()).unwrap();
118+
dispatcher
119+
.attach_to_if_index(netns.if_idx2, XdpFlags::default())
120+
.unwrap();
110121

111122
// Ensure the loaded_maps() api doesn't panic and retrieve a map.
112123
let map = loaded_maps()

test/integration-test/src/tests/xdp.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ffi::CStr, mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, time::Duration};
1+
use std::{mem::MaybeUninit, net::UdpSocket, num::NonZeroU32, time::Duration};
22

33
use aya::{
44
maps::{Array, CpuMap, XskMap},
@@ -9,11 +9,11 @@ use object::{Object, ObjectSection, ObjectSymbol, SymbolSection};
99
use test_log::test;
1010
use xdpilone::{BufIdx, IfInfo, Socket, SocketConfig, Umem, UmemConfig};
1111

12-
use crate::utils::NetNsGuard;
12+
use crate::utils::{NetNsGuard, IP_ADDR_1, IP_ADDR_2};
1313

1414
#[test]
1515
fn af_xdp() {
16-
let _netns = NetNsGuard::new();
16+
let netns = NetNsGuard::new();
1717

1818
let mut bpf = Ebpf::load(crate::REDIRECT).unwrap();
1919
let mut socks: XskMap<_> = bpf.take_map("SOCKS").unwrap().try_into().unwrap();
@@ -24,7 +24,8 @@ fn af_xdp() {
2424
.try_into()
2525
.unwrap();
2626
xdp.load().unwrap();
27-
xdp.attach("lo", XdpFlags::default()).unwrap();
27+
xdp.attach_to_if_index(netns.if_idx2, XdpFlags::default())
28+
.unwrap();
2829

2930
// So this needs to be page aligned. Pages are 4k on all mainstream architectures except for
3031
// Apple Silicon which uses 16k pages. So let's align on that for tests to run natively there.
@@ -41,9 +42,7 @@ fn af_xdp() {
4142
};
4243

4344
let mut iface = IfInfo::invalid();
44-
iface
45-
.from_name(CStr::from_bytes_with_nul(b"lo\0").unwrap())
46-
.unwrap();
45+
iface.from_ifindex(netns.if_idx1).unwrap();
4746
let sock = Socket::with_shared(&iface, &umem).unwrap();
4847

4948
let mut fq_cq = umem.fq_cq(&sock).unwrap(); // Fill Queue / Completion Queue
@@ -66,9 +65,12 @@ fn af_xdp() {
6665
writer.insert_once(frame.offset);
6766
writer.commit();
6867

69-
let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
70-
let port = sock.local_addr().unwrap().port();
71-
sock.send_to(b"hello AF_XDP", "127.0.0.1:1777").unwrap();
68+
let sock = UdpSocket::bind((IP_ADDR_1, 0)).unwrap();
69+
let src_port = sock.local_addr().unwrap().port();
70+
71+
const DST_PORT: u16 = 1777;
72+
sock.send_to(b"hello AF_XDP", (IP_ADDR_2, DST_PORT))
73+
.unwrap();
7274

7375
assert_eq!(rx.available(), 1);
7476
let desc = rx.receive(1).read().unwrap();
@@ -81,8 +83,8 @@ fn af_xdp() {
8183
let (ip, buf) = buf.split_at(20);
8284
assert_eq!(ip[9], 17); // UDP
8385
let (udp, payload) = buf.split_at(8);
84-
assert_eq!(&udp[0..2], port.to_be_bytes().as_slice()); // Source
85-
assert_eq!(&udp[2..4], 1777u16.to_be_bytes().as_slice()); // Dest
86+
assert_eq!(&udp[0..2], src_port.to_be_bytes().as_slice()); // Source
87+
assert_eq!(&udp[2..4], DST_PORT.to_be_bytes().as_slice()); // Dest
8688
assert_eq!(payload, b"hello AF_XDP");
8789
}
8890

@@ -134,7 +136,7 @@ fn map_load() {
134136

135137
#[test]
136138
fn cpumap_chain() {
137-
let _netns = NetNsGuard::new();
139+
let netns = NetNsGuard::new();
138140

139141
let mut bpf = Ebpf::load(crate::REDIRECT).unwrap();
140142

@@ -157,20 +159,24 @@ fn cpumap_chain() {
157159
// Load the main program
158160
let xdp: &mut Xdp = bpf.program_mut("redirect_cpu").unwrap().try_into().unwrap();
159161
xdp.load().unwrap();
160-
xdp.attach("lo", XdpFlags::default()).unwrap();
162+
xdp.attach_to_if_index(netns.if_idx2, XdpFlags::default())
163+
.unwrap();
161164

162165
const PAYLOAD: &str = "hello cpumap";
163166

164-
let sock = UdpSocket::bind("127.0.0.1:0").unwrap();
165-
let addr = sock.local_addr().unwrap();
166-
sock.set_read_timeout(Some(Duration::from_secs(60)))
167+
let sock1 = UdpSocket::bind((IP_ADDR_1, 0)).unwrap();
168+
let sock2 = UdpSocket::bind((IP_ADDR_2, 0)).unwrap();
169+
sock2
170+
.set_read_timeout(Some(Duration::from_secs(60)))
171+
.unwrap();
172+
sock1
173+
.send_to(PAYLOAD.as_bytes(), sock2.local_addr().unwrap())
167174
.unwrap();
168-
sock.send_to(PAYLOAD.as_bytes(), addr).unwrap();
169175

170176
// Read back the packet to ensure it went through the entire network stack, including our two
171177
// probes.
172178
let mut buf = [0u8; PAYLOAD.len() + 1];
173-
let n = sock.recv(&mut buf).unwrap();
179+
let n = sock2.recv(&mut buf).unwrap();
174180

175181
assert_eq!(&buf[..n], PAYLOAD.as_bytes());
176182
assert_eq!(hits.get(&0, 0).unwrap(), 1);

0 commit comments

Comments
 (0)