Commit b4db9f84 authored by Jakub Kicinski's avatar Jakub Kicinski Committed by David S. Miller
Browse files

selftests: drivers: add scaffolding for Netlink tests in Python



Add drivers/net as a target for mixed-use tests.
The setup is expected to work similarly to the forwarding tests.
Since we only need one interface (unlike forwarding tests)
read the target device name from NETIF. If not present we'll
try to run the test against netdevsim.

Reviewed-by: default avatarPetr Machata <petrm@nvidia.com>
Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f216306b
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ TARGETS += devices
TARGETS += dmabuf-heaps
TARGETS += drivers/dma-buf
TARGETS += drivers/s390x/uvdevice
TARGETS += drivers/net
TARGETS += drivers/net/bonding
TARGETS += drivers/net/team
TARGETS += dt
@@ -117,7 +118,7 @@ TARGETS_HOTPLUG = cpu-hotplug
TARGETS_HOTPLUG += memory-hotplug

# Networking tests want the net/lib target, include it automatically
ifneq ($(filter net,$(TARGETS)),)
ifneq ($(filter net drivers/net,$(TARGETS)),)
ifeq ($(filter net/lib,$(TARGETS)),)
	INSTALL_DEP_TARGETS := net/lib
endif
+7 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0

TEST_INCLUDES := $(wildcard lib/py/*.py)

TEST_PROGS := stats.py

include ../../lib.mk
+30 −0
Original line number Diff line number Diff line
Running tests
=============

Tests are executed within kselftest framework like any other tests.
By default tests execute against software drivers such as netdevsim.
All tests must support running against a real device (SW-only tests
should instead be placed in net/ or drivers/net/netdevsim, HW-only
tests in drivers/net/hw).

Set appropriate variables to point the tests at a real device.

Variables
=========

Variables can be set in the environment or by creating a net.config
file in the same directory as this README file. Example::

  $ NETIF=eth0 ./some_test.sh

or::

  $ cat tools/testing/selftests/drivers/net/net.config
  # Variable set in a file
  NETIF=eth0

NETIF
~~~~~

Name of the netdevice against which the test should be executed.
When empty or not set software devices will be used.
+17 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0

import sys
from pathlib import Path

KSFT_DIR = (Path(__file__).parent / "../../../..").resolve()

try:
    sys.path.append(KSFT_DIR.as_posix())
    from net.lib.py import *
except ModuleNotFoundError as e:
    ksft_pr("Failed importing `net` library from kernel sources")
    ksft_pr(str(e))
    ktap_result(True, comment="SKIP")
    sys.exit(4)

from .env import *
+52 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: GPL-2.0

import os
import shlex
from pathlib import Path
from lib.py import ip
from lib.py import NetdevSimDev

class NetDrvEnv:
    def __init__(self, src_path):
        self._ns = None

        self.env = os.environ.copy()
        self._load_env_file(src_path)

        if 'NETIF' in self.env:
            self.dev = ip("link show dev " + self.env['NETIF'], json=True)[0]
        else:
            self._ns = NetdevSimDev()
            self.dev = self._ns.nsims[0].dev
        self.ifindex = self.dev['ifindex']

    def __enter__(self):
        return self

    def __exit__(self, ex_type, ex_value, ex_tb):
        """
        __exit__ gets called at the end of a "with" block.
        """
        self.__del__()

    def __del__(self):
        if self._ns:
            self._ns.remove()
            self._ns = None

    def _load_env_file(self, src_path):
        src_dir = Path(src_path).parent.resolve()
        if not (src_dir / "net.config").exists():
            return

        lexer = shlex.shlex(open((src_dir / "net.config").as_posix(), 'r').read())
        k = None
        for token in lexer:
            if k is None:
                k = token
                self.env[k] = ""
            elif token == "=":
                pass
            else:
                self.env[k] = token
                k = None
Loading