xref: /linux/tools/testing/selftests/net/lib/py/ynl.py (revision 40286d6379aacfcc053253ef78dc78b09addffda)
1# SPDX-License-Identifier: GPL-2.0
2
3import sys
4from pathlib import Path
5from .consts import KSRC, KSFT_DIR
6from .ksft import ksft_pr, ktap_result
7
8# Resolve paths
9try:
10    if (KSFT_DIR / "kselftest-list.txt").exists():
11        # Running in "installed" selftests
12        tools_full_path = KSFT_DIR
13        SPEC_PATH = KSFT_DIR / "net/lib/specs"
14
15        sys.path.append(tools_full_path.as_posix())
16        from net.lib.ynl.pyynl.lib import YnlFamily, NlError, NlPolicy, Netlink
17    else:
18        # Running in tree
19        tools_full_path = KSRC / "tools"
20        SPEC_PATH = KSRC / "Documentation/netlink/specs"
21
22        sys.path.append(tools_full_path.as_posix())
23        from net.ynl.pyynl.lib import YnlFamily, NlError, NlPolicy, Netlink
24except ModuleNotFoundError as e:
25    ksft_pr("Failed importing `ynl` library from kernel sources")
26    ksft_pr(str(e))
27    ktap_result(True, comment="SKIP")
28    sys.exit(4)
29
30__all__ = [
31    "NlError", "NlPolicy", "Netlink", "YnlFamily", "SPEC_PATH",
32    "EthtoolFamily", "RtnlFamily", "RtnlAddrFamily",
33    "NetdevFamily", "NetshaperFamily", "NlctrlFamily", "DevlinkFamily",
34    "PSPFamily",
35]
36
37#
38# Wrapper classes, loading the right specs
39# Set schema='' to avoid jsonschema validation, it's slow
40#
41class EthtoolFamily(YnlFamily):
42    def __init__(self, recv_size=0):
43        super().__init__((SPEC_PATH / Path('ethtool.yaml')).as_posix(),
44                         schema='', recv_size=recv_size)
45
46
47class RtnlFamily(YnlFamily):
48    def __init__(self, recv_size=0):
49        super().__init__((SPEC_PATH / Path('rt-link.yaml')).as_posix(),
50                         schema='', recv_size=recv_size)
51
52class RtnlAddrFamily(YnlFamily):
53    def __init__(self, recv_size=0):
54        super().__init__((SPEC_PATH / Path('rt-addr.yaml')).as_posix(),
55                         schema='', recv_size=recv_size)
56
57class NetdevFamily(YnlFamily):
58    def __init__(self, recv_size=0):
59        super().__init__((SPEC_PATH / Path('netdev.yaml')).as_posix(),
60                         schema='', recv_size=recv_size)
61
62class NetshaperFamily(YnlFamily):
63    def __init__(self, recv_size=0):
64        super().__init__((SPEC_PATH / Path('net_shaper.yaml')).as_posix(),
65                         schema='', recv_size=recv_size)
66
67
68class NlctrlFamily(YnlFamily):
69    def __init__(self, recv_size=0):
70        super().__init__((SPEC_PATH / Path('nlctrl.yaml')).as_posix(),
71                         schema='', recv_size=recv_size)
72
73
74class DevlinkFamily(YnlFamily):
75    def __init__(self, recv_size=0):
76        super().__init__((SPEC_PATH / Path('devlink.yaml')).as_posix(),
77                         schema='', recv_size=recv_size)
78
79class PSPFamily(YnlFamily):
80    def __init__(self, recv_size=0):
81        super().__init__((SPEC_PATH / Path('psp.yaml')).as_posix(),
82                         schema='', recv_size=recv_size)
83