1#!/usr/bin/env python3
2#
3# ethtool tests for emulated network devices
4#
5# This test leverages ethtool's --test sequence to validate network
6# device behaviour.
7#
8# SPDX-License-Identifier: GPL-2.0-or-later
9
10from unittest import skip
11from qemu_test import QemuSystemTest, Asset
12from qemu_test import wait_for_console_pattern
13
14class NetDevEthtool(QemuSystemTest):
15
16    # Runs in about 17s under KVM, 19s under TCG, 25s under GCOV
17    timeout = 45
18
19    # Fetch assets from the netdev-ethtool subdir of my shared test
20    # images directory on fileserver.linaro.org.
21    ASSET_BASEURL = ('https://fileserver.linaro.org/s/kE4nCFLdQcoBF9t/'
22                     'download?path=%2Fnetdev-ethtool&files=')
23    ASSET_BZIMAGE = Asset(
24        ASSET_BASEURL + "bzImage",
25        "ed62ee06ea620b1035747f3f66a5e9fc5d3096b29f75562ada888b04cd1c4baf")
26    ASSET_ROOTFS = Asset(
27        ASSET_BASEURL + "rootfs.squashfs",
28        "8f0207e3c4d40832ae73c1a927e42ca30ccb1e71f047acb6ddb161ba422934e6")
29
30    def common_test_code(self, netdev, extra_args=None):
31        self.set_machine('q35')
32
33        # This custom kernel has drivers for all the supported network
34        # devices we can emulate in QEMU
35        kernel = self.ASSET_BZIMAGE.fetch()
36        rootfs = self.ASSET_ROOTFS.fetch()
37
38        append = 'printk.time=0 console=ttyS0 '
39        append += 'root=/dev/sr0 rootfstype=squashfs '
40
41        # any additional kernel tweaks for the test
42        if extra_args:
43            append += extra_args
44
45        # finally invoke ethtool directly
46        append += ' init=/usr/sbin/ethtool -- -t eth1 offline'
47
48        # add the rootfs via a readonly cdrom image
49        drive = f"file={rootfs},if=ide,index=0,media=cdrom"
50
51        self.vm.add_args('-kernel', kernel,
52                         '-append', append,
53                         '-drive', drive,
54                         '-device', netdev)
55
56        self.vm.set_console(console_index=0)
57        self.vm.launch()
58
59        wait_for_console_pattern(self,
60                                 "The test result is PASS",
61                                 "The test result is FAIL",
62                                 vm=None)
63        # no need to gracefully shutdown, just finish
64        self.vm.kill()
65
66    def test_igb(self):
67        self.common_test_code("igb")
68
69    def test_igb_nomsi(self):
70        self.common_test_code("igb", "pci=nomsi")
71
72    # It seems the other popular cards we model in QEMU currently fail
73    # the pattern test with:
74    #
75    #   pattern test failed (reg 0x00178): got 0x00000000 expected 0x00005A5A
76    #
77    # So for now we skip them.
78
79    @skip("Incomplete reg 0x00178 support")
80    def test_e1000(self):
81        self.common_test_code("e1000")
82
83    @skip("Incomplete reg 0x00178 support")
84    def test_i82550(self):
85        self.common_test_code("i82550")
86
87if __name__ == '__main__':
88    QemuSystemTest.main()
89