Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / testing / selftests / net / devlink_port_split.py
blob834066d465fc130941861bcaa5b8b7a744df7402
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-2.0
4 from subprocess import PIPE, Popen
5 import json
6 import time
7 import argparse
8 import collections
9 import sys
12 # Test port split configuration using devlink-port lanes attribute.
13 # The test is skipped in case the attribute is not available.
15 # First, check that all the ports with 1 lane fail to split.
16 # Second, check that all the ports with more than 1 lane can be split
17 # to all valid configurations (e.g., split to 2, split to 4 etc.)
21 Port = collections.namedtuple('Port', 'bus_info name')
24 def run_command(cmd, should_fail=False):
25 """
26 Run a command in subprocess.
27 Return: Tuple of (stdout, stderr).
28 """
30 p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
31 stdout, stderr = p.communicate()
32 stdout, stderr = stdout.decode(), stderr.decode()
34 if stderr != "" and not should_fail:
35 print("Error sending command: %s" % cmd)
36 print(stdout)
37 print(stderr)
38 return stdout, stderr
41 class devlink_ports(object):
42 """
43 Class that holds information on the devlink ports, required to the tests;
44 if_names: A list of interfaces in the devlink ports.
45 """
47 def get_if_names(dev):
48 """
49 Get a list of physical devlink ports.
50 Return: Array of tuples (bus_info/port, if_name).
51 """
53 arr = []
55 cmd = "devlink -j port show"
56 stdout, stderr = run_command(cmd)
57 assert stderr == ""
58 ports = json.loads(stdout)['port']
60 for port in ports:
61 if dev in port:
62 if ports[port]['flavour'] == 'physical':
63 arr.append(Port(bus_info=port, name=ports[port]['netdev']))
65 return arr
67 def __init__(self, dev):
68 self.if_names = devlink_ports.get_if_names(dev)
71 def get_max_lanes(port):
72 """
73 Get the $port's maximum number of lanes.
74 Return: number of lanes, e.g. 1, 2, 4 and 8.
75 """
77 cmd = "devlink -j port show %s" % port
78 stdout, stderr = run_command(cmd)
79 assert stderr == ""
80 values = list(json.loads(stdout)['port'].values())[0]
82 if 'lanes' in values:
83 lanes = values['lanes']
84 else:
85 lanes = 0
86 return lanes
89 def get_split_ability(port):
90 """
91 Get the $port split ability.
92 Return: split ability, true or false.
93 """
95 cmd = "devlink -j port show %s" % port.name
96 stdout, stderr = run_command(cmd)
97 assert stderr == ""
98 values = list(json.loads(stdout)['port'].values())[0]
100 return values['splittable']
103 def split(k, port, should_fail=False):
105 Split $port into $k ports.
106 If should_fail == True, the split should fail. Otherwise, should pass.
107 Return: Array of sub ports after splitting.
108 If the $port wasn't split, the array will be empty.
111 cmd = "devlink port split %s count %s" % (port.bus_info, k)
112 stdout, stderr = run_command(cmd, should_fail=should_fail)
114 if should_fail:
115 if not test(stderr != "", "%s is unsplittable" % port.name):
116 print("split an unsplittable port %s" % port.name)
117 return create_split_group(port, k)
118 else:
119 if stderr == "":
120 return create_split_group(port, k)
121 print("didn't split a splittable port %s" % port.name)
123 return []
126 def unsplit(port):
128 Unsplit $port.
131 cmd = "devlink port unsplit %s" % port
132 stdout, stderr = run_command(cmd)
133 test(stderr == "", "Unsplit port %s" % port)
136 def exists(port, dev):
138 Check if $port exists in the devlink ports.
139 Return: True is so, False otherwise.
142 return any(dev_port.name == port
143 for dev_port in devlink_ports.get_if_names(dev))
146 def exists_and_lanes(ports, lanes, dev):
148 Check if every port in the list $ports exists in the devlink ports and has
149 $lanes number of lanes after splitting.
150 Return: True if both are True, False otherwise.
153 for port in ports:
154 max_lanes = get_max_lanes(port)
155 if not exists(port, dev):
156 print("port %s doesn't exist in devlink ports" % port)
157 return False
158 if max_lanes != lanes:
159 print("port %s has %d lanes, but %s were expected"
160 % (port, lanes, max_lanes))
161 return False
162 return True
165 def test(cond, msg):
167 Check $cond and print a message accordingly.
168 Return: True is pass, False otherwise.
171 if cond:
172 print("TEST: %-60s [ OK ]" % msg)
173 else:
174 print("TEST: %-60s [FAIL]" % msg)
176 return cond
179 def create_split_group(port, k):
181 Create the split group for $port.
182 Return: Array with $k elements, which are the split port group.
185 return list(port.name + "s" + str(i) for i in range(k))
188 def split_unsplittable_port(port, k):
190 Test that splitting of unsplittable port fails.
193 # split to max
194 new_split_group = split(k, port, should_fail=True)
196 if new_split_group != []:
197 unsplit(port.bus_info)
200 def split_splittable_port(port, k, lanes, dev):
202 Test that splitting of splittable port passes correctly.
205 new_split_group = split(k, port)
207 # Once the split command ends, it takes some time to the sub ifaces'
208 # to get their names. Use udevadm to continue only when all current udev
209 # events are handled.
210 cmd = "udevadm settle"
211 stdout, stderr = run_command(cmd)
212 assert stderr == ""
214 if new_split_group != []:
215 test(exists_and_lanes(new_split_group, lanes/k, dev),
216 "split port %s into %s" % (port.name, k))
218 unsplit(port.bus_info)
221 def make_parser():
222 parser = argparse.ArgumentParser(description='A test for port splitting.')
223 parser.add_argument('--dev',
224 help='The devlink handle of the device under test. ' +
225 'The default is the first registered devlink ' +
226 'handle.')
228 return parser
231 def main(cmdline=None):
232 parser = make_parser()
233 args = parser.parse_args(cmdline)
235 dev = args.dev
236 if not dev:
237 cmd = "devlink -j dev show"
238 stdout, stderr = run_command(cmd)
239 assert stderr == ""
241 devs = json.loads(stdout)['dev']
242 dev = list(devs.keys())[0]
244 cmd = "devlink dev show %s" % dev
245 stdout, stderr = run_command(cmd)
246 if stderr != "":
247 print("devlink device %s can not be found" % dev)
248 sys.exit(1)
250 ports = devlink_ports(dev)
252 for port in ports.if_names:
253 max_lanes = get_max_lanes(port.name)
255 # If max lanes is 0, do not test port splitting at all
256 if max_lanes == 0:
257 continue
259 # If 1 lane, shouldn't be able to split
260 elif max_lanes == 1:
261 test(not get_split_ability(port),
262 "%s should not be able to split" % port.name)
263 split_unsplittable_port(port, max_lanes)
265 # Else, splitting should pass and all the split ports should exist.
266 else:
267 lane = max_lanes
268 test(get_split_ability(port),
269 "%s should be able to split" % port.name)
270 while lane > 1:
271 split_splittable_port(port, lane, max_lanes, dev)
273 lane //= 2
276 if __name__ == "__main__":
277 main()