aboutsummaryrefslogtreecommitdiff
path: root/e2e_tests/tests/pci_hotplug.rs
blob: 5e313cbb183d65b5a8e7072808ac7806adbfa125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2023 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Integration test for hotplug of tap devices as virtio-net.

#![cfg(all(unix, target_arch = "x86_64"))]

use std::net::Ipv4Addr;
use std::process::Command;
use std::thread;
use std::time::Duration;
use std::time::Instant;

use base::sys::linux::ioctl_with_val;
use base::test_utils::call_test_with_sudo;
use fixture::vm::Config;
use fixture::vm::TestVm;
use net_util::sys::linux::Tap;
use net_util::sys::linux::TapTLinux;
use net_util::MacAddress;
use net_util::TapTCommon;

/// Count the number of virtio-net devices.
fn count_virtio_net_devices(vm: &mut TestVm) -> usize {
    let lspci_result = vm.exec_in_guest("lspci -n").unwrap();
    // Count occurance for virtio net device: 1af4:1041
    lspci_result.stdout.matches("1af4:1041").count()
}

/// Poll func until it returns true, or timeout is exceeded.
fn poll_until_true<F>(vm: &mut TestVm, func: F, timeout: Duration) -> bool
where
    F: Fn(&mut TestVm) -> bool,
{
    let poll_interval = Duration::from_millis(100);
    let start_time = Instant::now();
    while !func(vm) {
        if start_time.elapsed() > timeout {
            return false;
        }
        thread::sleep(poll_interval);
    }
    true
}

/// setup a tap device for test
fn setup_tap_device(tap_name: &[u8], ip_addr: Ipv4Addr, netmask: Ipv4Addr, mac_addr: MacAddress) {
    let tap = Tap::new_with_name(tap_name, true, false).unwrap();
    // SAFETY:
    // ioctl is safe since we call it with a valid tap fd and check the return value.
    let ret = unsafe { ioctl_with_val(&tap, net_sys::TUNSETPERSIST(), 1) };
    if ret < 0 {
        panic!("Failed to persist tap interface");
    }
    tap.set_ip_addr(ip_addr).unwrap();
    tap.set_netmask(netmask).unwrap();
    tap.set_mac_address(mac_addr).unwrap();
    tap.set_vnet_hdr_size(16).unwrap();
    tap.set_offload(0).unwrap();
    tap.enable().unwrap();
    // Release tap to be used by the VM.
    drop(tap);
}

/// Implementation for tap_hotplug_two
///
/// This test will fail by itself due to permission.
#[ignore = "Only to be called by tap_hotplug_two"]
#[test]
fn tap_hotplug_two_impl() {
    let wait_timeout = Duration::from_secs(5);
    // Setup VM start parameter.
    let config = Config::new().extra_args(vec!["--pci-hotplug-slots".to_owned(), "2".to_owned()]);
    let mut vm = TestVm::new(config).unwrap();

    //Setup test taps.
    let tap1_name = "test_tap1";
    setup_tap_device(
        tap1_name.as_bytes(),
        "100.115.92.15".parse().unwrap(),
        "255.255.255.252".parse().unwrap(),
        "a0:b0:c0:d0:e0:f1".parse().unwrap(),
    );
    let tap2_name = "test_tap2";
    setup_tap_device(
        tap2_name.as_bytes(),
        "100.115.92.25".parse().unwrap(),
        "255.255.255.252".parse().unwrap(),
        "a0:b0:c0:d0:e0:f2".parse().unwrap(),
    );

    // Check number of virtio-net devices after each hotplug.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 0 },
        wait_timeout
    ));
    vm.hotplug_tap(tap1_name).unwrap();
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));
    vm.hotplug_tap(tap2_name).unwrap();
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 2 },
        wait_timeout
    ));

    // Check number of devices after each removal.
    vm.remove_pci_device(1).unwrap();
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));
    vm.remove_pci_device(2).unwrap();
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 0 },
        wait_timeout
    ));

    drop(vm);
    Command::new("ip")
        .args(["link", "delete", tap1_name])
        .status()
        .unwrap();
    Command::new("ip")
        .args(["link", "delete", tap2_name])
        .status()
        .unwrap();
}

/// Checks hotplug works with two tap devices.
#[test]
fn tap_hotplug_two() {
    call_test_with_sudo("tap_hotplug_two_impl");
}

/// Implementation for tap_hotplug_add_remove_add
///
/// This test will fail by itself due to permission.
#[ignore = "Only to be called by tap_hotplug_add_remove_add"]
#[test]
fn tap_hotplug_add_remove_add_impl() {
    let wait_timeout = Duration::from_secs(5);
    // Setup VM start parameter.
    let config = Config::new().extra_args(vec!["--pci-hotplug-slots".to_owned(), "1".to_owned()]);
    let mut vm = TestVm::new(config).unwrap();

    //Setup test tap
    let tap_name = "test_tap";
    setup_tap_device(
        tap_name.as_bytes(),
        "100.115.92.5".parse().unwrap(),
        "255.255.255.252".parse().unwrap(),
        "a0:b0:c0:d0:e0:f0".parse().unwrap(),
    );

    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 0 },
        wait_timeout
    ));
    // Hotplug tap.
    vm.hotplug_tap(tap_name).unwrap();
    // Wait until virtio-net device appears in guest OS.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));

    // Remove hotplugged tap device.
    vm.remove_pci_device(1).unwrap();
    // Wait until virtio-net device disappears from guest OS.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 0 },
        wait_timeout
    ));

    // Hotplug tap again.
    vm.hotplug_tap(tap_name).unwrap();
    // Wait until virtio-net device appears in guest OS.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));

    drop(vm);
    Command::new("ip")
        .args(["link", "delete", tap_name])
        .status()
        .unwrap();
}

/// Checks tap hotplug works with a device added, removed, then added again.
#[test]
fn tap_hotplug_add_remove_add() {
    call_test_with_sudo("tap_hotplug_add_remove_add_impl");
}

/// Implementation for tap_hotplug_add_remove_rapid_add
///
/// This test will fail by itself due to permission.
#[ignore = "Only to be called by tap_hotplug_add_remove_rapid_add"]
#[test]
fn tap_hotplug_add_remove_rapid_add_impl() {
    let wait_timeout = Duration::from_secs(5);
    // Setup VM start parameter.
    let config = Config::new().extra_args(vec!["--pci-hotplug-slots".to_owned(), "1".to_owned()]);
    let mut vm = TestVm::new(config).unwrap();

    //Setup test tap
    let tap_name = "test_tap";
    setup_tap_device(
        tap_name.as_bytes(),
        "100.115.92.5".parse().unwrap(),
        "255.255.255.252".parse().unwrap(),
        "a0:b0:c0:d0:e0:f0".parse().unwrap(),
    );

    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 0 },
        wait_timeout
    ));
    // Hotplug tap.
    vm.hotplug_tap(tap_name).unwrap();
    // Wait until virtio-net device appears in guest OS.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));

    // Remove hotplugged tap device, then hotplug again without waiting for guest.
    vm.remove_pci_device(1).unwrap();
    vm.hotplug_tap(tap_name).unwrap();

    // Wait for a while that the guest likely noticed the removal.
    thread::sleep(Duration::from_millis(500));
    // Wait until virtio-net device reappears in guest OS. This assertion would fail if the device
    // added later is not recognized.
    assert!(poll_until_true(
        &mut vm,
        |vm| { count_virtio_net_devices(vm) == 1 },
        wait_timeout
    ));

    drop(vm);
    Command::new("ip")
        .args(["link", "delete", tap_name])
        .status()
        .unwrap();
}

/// Checks tap hotplug works with a device added, removed, then rapidly added again.
#[test]
fn tap_hotplug_add_remove_rapid_add() {
    call_test_with_sudo("tap_hotplug_add_remove_rapid_add_impl");
}