1212
1313from lib .py import defer
1414from 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
1616from lib .py import KsftSkipEx
1717from lib .py import NetDrvEpEnv , PSPFamily , NlError
1818from 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+
3946def _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+
184341def _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 ()
0 commit comments