Skip to content

Commit 81b8908

Browse files
kuba-mooPaolo Abeni
authored andcommitted
selftests: drv-net: psp: add association tests
Add tests for exercising PSP associations for TCP sockets. Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Daniel Zahka <[email protected]> Link: https://patch.msgid.link/[email protected] Reviewed-by: Willem de Bruijn <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 8f90dc6 commit 81b8908

File tree

4 files changed

+167
-4
lines changed

4 files changed

+167
-4
lines changed

tools/testing/selftests/drivers/net/hw/lib/py/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from net.lib.py import ksft_disruptive, ksft_exit, ksft_pr, ksft_run, \
2323
ksft_setup
2424
from net.lib.py import ksft_eq, ksft_ge, ksft_in, ksft_is, ksft_lt, \
25-
ksft_ne, ksft_not_in, ksft_raises, ksft_true
25+
ksft_ne, ksft_not_in, ksft_raises, ksft_true, ksft_gt
2626
from net.lib.py import NetNSEnter
2727
from drivers.net.lib.py import GenerateTraffic
2828
from drivers.net.lib.py import NetDrvEnv, NetDrvEpEnv

tools/testing/selftests/drivers/net/lib/py/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from net.lib.py import ksft_disruptive, ksft_exit, ksft_pr, ksft_run, \
2222
ksft_setup
2323
from net.lib.py import ksft_eq, ksft_ge, ksft_in, ksft_is, ksft_lt, \
24-
ksft_ne, ksft_not_in, ksft_raises, ksft_true
24+
ksft_ne, ksft_not_in, ksft_raises, ksft_true, ksft_gt
2525
except ModuleNotFoundError as e:
2626
ksft_pr("Failed importing `net` library from kernel sources")
2727
ksft_pr(str(e))

tools/testing/selftests/drivers/net/psp.py

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
from lib.py import defer
1414
from lib.py import ksft_run, ksft_exit, ksft_pr
15-
from lib.py import ksft_true, ksft_eq, ksft_ne, ksft_raises
15+
from lib.py import ksft_true, ksft_eq, ksft_ne, ksft_gt, ksft_raises
1616
from lib.py import KsftSkipEx
1717
from lib.py import NetDrvEpEnv, PSPFamily, NlError
1818
from lib.py import bkg, rand_port, wait_port_listen
@@ -36,6 +36,13 @@ def _remote_read_len(cfg):
3636
return int(cfg.comm_sock.recv(1024)[:-1].decode('utf-8'))
3737

3838

39+
def _make_clr_conn(cfg, ipver=None):
40+
_send_with_ack(cfg, b'conn clr\0')
41+
remote_addr = cfg.remote_addr_v[ipver] if ipver else cfg.remote_addr
42+
s = socket.create_connection((remote_addr, cfg.comm_port), )
43+
return s
44+
45+
3946
def _make_psp_conn(cfg, version=0, ipver=None):
4047
_send_with_ack(cfg, b'conn psp\0' + struct.pack('BB', version, version))
4148
remote_addr = cfg.remote_addr_v[ipver] if ipver else cfg.remote_addr
@@ -181,6 +188,156 @@ def dev_rotate_spi(cfg):
181188
ksft_ne(top_a, top_b)
182189

183190

191+
def assoc_basic(cfg):
192+
""" Test creating associations """
193+
_init_psp_dev(cfg)
194+
195+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
196+
assoc = cfg.pspnl.rx_assoc({"version": 0,
197+
"dev-id": cfg.psp_dev_id,
198+
"sock-fd": s.fileno()})
199+
ksft_eq(assoc['dev-id'], cfg.psp_dev_id)
200+
ksft_gt(assoc['rx-key']['spi'], 0)
201+
ksft_eq(len(assoc['rx-key']['key']), 16)
202+
203+
assoc = cfg.pspnl.tx_assoc({"dev-id": cfg.psp_dev_id,
204+
"version": 0,
205+
"tx-key": assoc['rx-key'],
206+
"sock-fd": s.fileno()})
207+
ksft_eq(len(assoc), 0)
208+
s.close()
209+
210+
211+
def assoc_bad_dev(cfg):
212+
""" Test creating associations with bad device ID """
213+
_init_psp_dev(cfg)
214+
215+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
216+
with ksft_raises(NlError) as cm:
217+
cfg.pspnl.rx_assoc({"version": 0,
218+
"dev-id": cfg.psp_dev_id + 1234567,
219+
"sock-fd": s.fileno()})
220+
ksft_eq(cm.exception.nl_msg.error, -errno.ENODEV)
221+
222+
223+
def assoc_sk_only_conn(cfg):
224+
""" Test creating associations based on socket """
225+
_init_psp_dev(cfg)
226+
227+
with _make_clr_conn(cfg) as s:
228+
assoc = cfg.pspnl.rx_assoc({"version": 0,
229+
"sock-fd": s.fileno()})
230+
ksft_eq(assoc['dev-id'], cfg.psp_dev_id)
231+
cfg.pspnl.tx_assoc({"version": 0,
232+
"tx-key": assoc['rx-key'],
233+
"sock-fd": s.fileno()})
234+
_close_conn(cfg, s)
235+
236+
237+
def assoc_sk_only_mismatch(cfg):
238+
""" Test creating associations based on socket (dev mismatch) """
239+
_init_psp_dev(cfg)
240+
241+
with _make_clr_conn(cfg) as s:
242+
with ksft_raises(NlError) as cm:
243+
cfg.pspnl.rx_assoc({"version": 0,
244+
"dev-id": cfg.psp_dev_id + 1234567,
245+
"sock-fd": s.fileno()})
246+
the_exception = cm.exception
247+
ksft_eq(the_exception.nl_msg.extack['bad-attr'], ".dev-id")
248+
ksft_eq(the_exception.nl_msg.error, -errno.EINVAL)
249+
250+
251+
def assoc_sk_only_mismatch_tx(cfg):
252+
""" Test creating associations based on socket (dev mismatch) """
253+
_init_psp_dev(cfg)
254+
255+
with _make_clr_conn(cfg) as s:
256+
with ksft_raises(NlError) as cm:
257+
assoc = cfg.pspnl.rx_assoc({"version": 0,
258+
"sock-fd": s.fileno()})
259+
cfg.pspnl.tx_assoc({"version": 0,
260+
"tx-key": assoc['rx-key'],
261+
"dev-id": cfg.psp_dev_id + 1234567,
262+
"sock-fd": s.fileno()})
263+
the_exception = cm.exception
264+
ksft_eq(the_exception.nl_msg.extack['bad-attr'], ".dev-id")
265+
ksft_eq(the_exception.nl_msg.error, -errno.EINVAL)
266+
267+
268+
def assoc_sk_only_unconn(cfg):
269+
""" Test creating associations based on socket (unconnected, should fail) """
270+
_init_psp_dev(cfg)
271+
272+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
273+
with ksft_raises(NlError) as cm:
274+
cfg.pspnl.rx_assoc({"version": 0,
275+
"sock-fd": s.fileno()})
276+
the_exception = cm.exception
277+
ksft_eq(the_exception.nl_msg.extack['miss-type'], "dev-id")
278+
ksft_eq(the_exception.nl_msg.error, -errno.EINVAL)
279+
280+
281+
def assoc_version_mismatch(cfg):
282+
""" Test creating associations where Rx and Tx PSP versions do not match """
283+
_init_psp_dev(cfg)
284+
285+
versions = list(cfg.psp_info['psp-versions-cap'])
286+
if len(versions) < 2:
287+
raise KsftSkipEx("Not enough PSP versions supported by the device for the test")
288+
289+
# Translate versions to integers
290+
versions = [cfg.pspnl.consts["version"].entries[v].value for v in versions]
291+
292+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
293+
rx = cfg.pspnl.rx_assoc({"version": versions[0],
294+
"dev-id": cfg.psp_dev_id,
295+
"sock-fd": s.fileno()})
296+
297+
for version in versions[1:]:
298+
with ksft_raises(NlError) as cm:
299+
cfg.pspnl.tx_assoc({"dev-id": cfg.psp_dev_id,
300+
"version": version,
301+
"tx-key": rx['rx-key'],
302+
"sock-fd": s.fileno()})
303+
the_exception = cm.exception
304+
ksft_eq(the_exception.nl_msg.error, -errno.EINVAL)
305+
306+
307+
def assoc_twice(cfg):
308+
""" Test reusing Tx assoc for two sockets """
309+
_init_psp_dev(cfg)
310+
311+
def rx_assoc_check(s):
312+
assoc = cfg.pspnl.rx_assoc({"version": 0,
313+
"dev-id": cfg.psp_dev_id,
314+
"sock-fd": s.fileno()})
315+
ksft_eq(assoc['dev-id'], cfg.psp_dev_id)
316+
ksft_gt(assoc['rx-key']['spi'], 0)
317+
ksft_eq(len(assoc['rx-key']['key']), 16)
318+
319+
return assoc
320+
321+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
322+
assoc = rx_assoc_check(s)
323+
tx = cfg.pspnl.tx_assoc({"dev-id": cfg.psp_dev_id,
324+
"version": 0,
325+
"tx-key": assoc['rx-key'],
326+
"sock-fd": s.fileno()})
327+
ksft_eq(len(tx), 0)
328+
329+
# Use the same Tx assoc second time
330+
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s2:
331+
rx_assoc_check(s2)
332+
tx = cfg.pspnl.tx_assoc({"dev-id": cfg.psp_dev_id,
333+
"version": 0,
334+
"tx-key": assoc['rx-key'],
335+
"sock-fd": s2.fileno()})
336+
ksft_eq(len(tx), 0)
337+
338+
s.close()
339+
340+
184341
def _data_basic_send(cfg, version, ipver):
185342
""" Test basic data send """
186343
_init_psp_dev(cfg)
@@ -252,7 +409,8 @@ def main() -> None:
252409
for ipver in ("4", "6")
253410
]
254411

255-
ksft_run(cases=cases, globs=globals(), case_pfx={"dev_",}, args=(cfg, ))
412+
ksft_run(cases=cases, globs=globals(),
413+
case_pfx={"dev_", "assoc_"}, args=(cfg, ))
256414

257415
cfg.comm_sock.send(b"exit\0")
258416
cfg.comm_sock.close()

tools/testing/selftests/net/lib/py/ksft.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ def ksft_ge(a, b, comment=""):
9292
_fail("Check failed", a, "<", b, comment)
9393

9494

95+
def ksft_gt(a, b, comment=""):
96+
if a <= b:
97+
_fail("Check failed", a, "<=", b, comment)
98+
99+
95100
def ksft_lt(a, b, comment=""):
96101
if a >= b:
97102
_fail("Check failed", a, ">=", b, comment)

0 commit comments

Comments
 (0)