xref: /linux/tools/testing/selftests/drivers/net/README.rst (revision 3929f55da21fb76d931f464c71d2fd27762ef7d8)
1.. SPDX-License-Identifier: GPL-2.0
2
3Running driver tests
4====================
5
6Networking driver tests are executed within kselftest framework like any
7other tests. They support testing both real device drivers and emulated /
8software drivers (latter mostly to test the core parts of the stack).
9
10SW mode
11~~~~~~~
12
13By default, when no extra parameters are set or exported, tests execute
14against software drivers such as netdevsim. No extra preparation is required
15the software devices are created and destroyed as part of the test.
16In this mode the tests are indistinguishable from other selftests and
17(for example) can be run under ``virtme-ng`` like the core networking selftests.
18
19HW mode
20~~~~~~~
21
22Executing tests against a real device requires external preparation.
23The netdevice against which tests will be run must exist, be running
24(in UP state) and be configured with an IP address.
25
26Refer to list of :ref:`Variables` later in this file to set up running
27the tests against a real device.
28
29The current support for bash tests restricts the use of the same interface name
30on the local system and the remote one and will bail if this case is
31encountered.
32
33Both modes required
34~~~~~~~~~~~~~~~~~~~
35
36All tests in drivers/net must support running both against a software device
37and a real device. SW-only tests should instead be placed in net/ or
38drivers/net/netdevsim, HW-only tests in drivers/net/hw.
39
40Variables
41=========
42
43The variables can be set in the environment or by creating a net.config
44file in the same directory as this README file. Example::
45
46  $ NETIF=eth0 ./some_test.sh
47
48or::
49
50  $ cat tools/testing/selftests/drivers/net/net.config
51  # Variable set in a file
52  NETIF=eth0
53
54Please note that the config parser is very simple, if there are
55any non-alphanumeric characters in the value it needs to be in
56double quotes.
57
58Local test (which don't require endpoint for sending / receiving traffic)
59need only the ``NETIF`` variable. Remaining variables define the endpoint
60and communication method.
61
62NETIF
63~~~~~
64
65Name of the netdevice against which the test should be executed.
66When empty or not set software devices will be used.
67
68LOCAL_V4, LOCAL_V6, REMOTE_V4, REMOTE_V6
69~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70
71Local and remote endpoint IP addresses.
72
73Tests reconfigure the device freely, including taking the link down, and
74expect the addresses to work immediately afterwards. IPv6 needs help::
75
76  ip -6 address add 2001:db8:1::1/64 dev eth0 nodad
77  sysctl -w net.ipv6.conf.eth0.keep_addr_on_down=1
78
79Without those the address is flushed when the link goes down, or spends
80a second unusable while waiting for DAD to finish.
81
82LOCAL_PREFIX_V6
83~~~~~~~~~~~~~~~
84
85Local IP prefix/subnet which can be used to allocate extra IP addresses (for
86network name spaces behind macvlan, veth, netkit devices). DUT must be
87reachable using these addresses from the endpoint.
88
89LOCAL_PREFIX_V6 must NOT match LOCAL_V6.
90
91Example:
92  NETIF           = "eth0"
93  LOCAL_V6        = "2001:db8:1::1"
94  REMOTE_V6       = "2001:db8:1::2"
95  LOCAL_PREFIX_V6 = "2001:db8:2::0/64"
96
97          +-----------------------------+        +------------------------------+
98  dst     | INIT NS                     |        | TEST NS                      |
99  2001:   | +---------------+           |        |                              |
100  db8:2::2| | NETIF         |           |  bpf   |                              |
101      +---|>| 2001:db8:1::1 |           |redirect| +-------------------------+  |
102      |   | |               |-----------|--------|>| Netkit                  |  |
103      |   | +---------------+           | _peer  | | nk_guest                |  |
104      |   | +-------------+ Netkit pair |        | | fe80::2/64              |  |
105      |   | | Netkit      |.............|........|>| 2001:db8:2::2/64        |  |
106      |   | | nk_host     |             |        | +-------------------------+  |
107      |   | | fe80::1/64  |             |        |                              |
108      |   | +-------------+             |        | route:                       |
109      |   |                             |        |   default                    |
110      |   | route:                      |        |     via fe80::1 dev nk_guest |
111      |   |   2001:db8:2::2/128         |        +------------------------------+
112      |   |     via fe80::2 dev nk_host |
113      |   +-----------------------------+
114      |
115      |   +---------------+
116      |   | REMOTE        |
117      +---| 2001:db8:1::2 |
118          +---------------+
119
120REMOTE_TYPE
121~~~~~~~~~~~
122
123Communication method used to run commands on the remote endpoint.
124Test framework has built-in support for ``netns`` and ``ssh`` channels.
125``netns`` assumes the "remote" interface is part of the same
126host, just moved to the specified netns.
127``ssh`` communicates with remote endpoint over ``ssh`` and ``scp``.
128Using persistent SSH connections is strongly encouraged to avoid
129the latency of SSH connection setup on every command.
130
131Communication methods are defined by classes in ``lib/py/remote_{name}.py``.
132It should be possible to add a new method without modifying any of
133the framework, by simply adding an appropriately named file to ``lib/py``.
134
135REMOTE_ARGS
136~~~~~~~~~~~
137
138Arguments used to construct the communication channel.
139Communication channel dependent::
140
141  for netns - name of the "remote" namespace
142  for ssh - name/address of the remote host
143
144Example
145=======
146
147Build the selftests::
148
149  # make -C tools/testing/selftests/ TARGETS="drivers/net drivers/net/hw"
150
151"Install" the tests and copy them over to the target machine::
152
153  # make -C tools/testing/selftests/ TARGETS="drivers/net drivers/net/hw" \
154     install INSTALL_PATH=/tmp/ksft-net-drv
155
156  # rsync -ra --delete /tmp/ksft-net-drv root@192.168.1.1:/root/
157
158On the target machine, running the tests will use netdevsim by default::
159
160  [/root] # ./ksft-net-drv/run_kselftest.sh -t drivers/net:ping.py
161  TAP version 13
162  1..1
163  # timeout set to 45
164  # selftests: drivers/net: ping.py
165  # KTAP version 1
166  # 1..3
167  # ok 1 ping.test_v4
168  # ok 2 ping.test_v6
169  # ok 3 ping.test_tcp
170  # # Totals: pass:3 fail:0 xfail:0 xpass:0 skip:0 error:0
171  ok 1 selftests: drivers/net: ping.py
172
173Create a config with remote info::
174
175  [/root] # cat > ./ksft-net-drv/drivers/net/net.config <<EOF
176  NETIF=eth0
177  LOCAL_V4=192.168.1.1
178  REMOTE_V4=192.168.1.2
179  REMOTE_TYPE=ssh
180  REMOTE_ARGS=root@192.168.1.2
181  EOF
182
183Run the test::
184
185  [/root] # ./ksft-net-drv/drivers/net/ping.py
186  KTAP version 1
187  1..3
188  ok 1 ping.test_v4
189  ok 2 ping.test_v6 # SKIP Test requires IPv6 connectivity
190  ok 3 ping.test_tcp
191  # Totals: pass:2 fail:0 xfail:0 xpass:0 skip:1 error:0
192
193Dependencies
194~~~~~~~~~~~~
195
196The tests have a handful of dependencies. For Fedora / CentOS::
197
198  dnf -y install netsniff-ng python-yaml socat iperf3
199
200Guidance for test authors
201=========================
202
203This section mostly applies to Python tests but some of the guidance
204may be more broadly applicable.
205
206Kernel config
207~~~~~~~~~~~~~
208
209Each test directory has a ``config`` file listing which kernel
210configuration options the tests depend on. This file must be kept
211up to date, the CIs build minimal kernels for each test group.
212
213Adding checks inside the tests to validate that the necessary kernel
214configs are enabled is discouraged. The test author may include such
215checks, but standalone patches to make tests compatible e.g. with
216distro kernel configs are unlikely to be accepted.
217
218Avoid libraries and frameworks
219~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220
221Test files should be relatively self contained. The libraries should
222only include very core or non-trivial code.
223It may be tempting to "factor out" the common code to lib/py/, but fight that
224urge. Library code increases the barrier of entry, and complexity in general.
225
226Avoid mixing test code and boilerplate
227~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228
229In Python, try to avoid adding code in the ``main()`` function which
230instantiates ``NetDrvEnv()`` and calls ``ksft_run()``. It's okay to
231set up global resources (e.g. open an RtNetlink socket used by multiple
232tests), but any complex logic, test-specific environment configuration
233and validation should be done in the tests (even if it means it has to
234be repeated).
235
236Local host is the DUT
237~~~~~~~~~~~~~~~~~~~~~
238
239Dual-host tests (tests with an endpoint) should be written from the DUT
240perspective. IOW the local machine should be the one tested, remote is
241just for traffic generation.
242
243Avoid modifying remote
244~~~~~~~~~~~~~~~~~~~~~~
245
246Avoid making configuration changes to the remote system as much as possible.
247Remote system may be used concurrently by multiple DUTs.
248
249defer()
250~~~~~~~
251
252The env must be clean after test exits. Register a ``defer()`` for any
253action that needs an "undo" as soon as possible. If you need to run
254the cancel action as part of the test - ``defer()`` returns an object
255you can ``.exec()``-ute.
256
257ksft_pr()
258~~~~~~~~~
259
260Use ``ksft_pr()`` instead of ``print()`` to avoid breaking TAP format.
261
262ksft_disruptive
263~~~~~~~~~~~~~~~
264
265By default the tests are expected to be able to run on
266single-interface systems. All tests which may disconnect ``NETIF``
267must be annotated with ``@ksft_disruptive``.
268
269ksft_variants
270~~~~~~~~~~~~~
271
272Use the ``@ksft_variants`` decorator to run a test with multiple sets
273of inputs as separate test cases. This avoids duplicating test functions
274that only differ in parameters.
275
276Parameters can be a single value, a tuple, or a ``KsftNamedVariant``
277(which gives an explicit name to the sub-case). The argument to the
278decorator can be a list or a generator.
279
280Example::
281
282  @ksft_variants([
283      KsftNamedVariant("main", False),
284      KsftNamedVariant("ctx", True),
285  ])
286  def resize_periodic(cfg, create_context):
287      # test body receives (cfg, create_context) where create_context
288      # is False for the "main" variant and True for "ctx"
289      pass
290
291or::
292
293  def _gro_variants():
294      for mode in ["sw", "hw"]:
295          for protocol in ["tcp4", "tcp6"]:
296              yield (mode, protocol)
297
298  @ksft_variants(_gro_variants())
299  def test(cfg, mode, protocol):
300      pass
301
302Linters
303~~~~~~~
304
305We expect clean ``ruff check`` and ``pylint --disable=R``.
306The code should be clean, avoid disabling pylint warnings explicitly!
307
308Running tests CI-style
309======================
310
311See https://github.com/linux-netdev/nipa/wiki for instructions on how
312to easily run the tests using ``virtme-ng``.
313