summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/drivers/net/shaper.py
blob: a53316726f69482f359c8015ef9f64013ef7b188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
# pylint: disable=too-many-lines

import errno
import glob

from lib.py import ksft_run, ksft_exit
from lib.py import ksft_eq, ksft_true, ksft_raises, KsftSkipEx
from lib.py import EthtoolFamily, NetshaperFamily
from lib.py import NetDrvEnv
from lib.py import NlError
from lib.py import cmd, defer

def _delete_shaper(cfg, nl_shaper, handle) -> None:
    """ Delete the shaper identified by handle, ignoring a missing-shaper error. """
    try:
        nl_shaper.delete({'ifindex': cfg.ifindex,
                          'handle': handle})
    except NlError as e:
        if e.error != errno.ENOENT:
            raise

def _require_queues(cfg, count):
    """ Return the netdev TX queue count, skipping the test if fewer than count exist. """
    qcnt = len(glob.glob(f"/sys/class/net/{cfg.ifname}/queues/tx-*"))
    if qcnt < count:
        raise KsftSkipEx(f"netdev has {qcnt} queues, {count} required")
    return qcnt

def _cap_get(cfg, nl_shaper, scope):
    """ Return the shaper capabilities for the given scope, caching them on cfg. """
    if not hasattr(cfg, 'cap_cache'):
        cfg.cap_cache = {}
    if scope not in cfg.cap_cache:
        cfg.cap_cache[scope] = nl_shaper.cap_get({'ifindex': cfg.ifindex,
                                                  'scope': scope})

    return cfg.cap_cache[scope]

def _require_caps(cfg, nl_shaper, scope, caps, msg) -> None:
    """ Skip the test unless the given scope advertises all the required caps. """
    try:
        supported = _cap_get(cfg, nl_shaper, scope)
    except NlError as e:
        if e.error == errno.EOPNOTSUPP:
            raise KsftSkipEx(f"{scope} scope shapers not supported by the device")
        raise

    if not set(caps).issubset(supported):
        raise KsftSkipEx(msg)

def get_shapers(cfg, nl_shaper) -> None:
    try:
        shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    except NlError as e:
        if e.error == 95:
            raise KsftSkipEx("shapers not supported by the device")
        raise

    # Default configuration: no shapers configured.
    ksft_eq(len(shapers), 0)

def get_caps(cfg, nl_shaper) -> None:
    try:
        caps = nl_shaper.cap_get({'ifindex': cfg.ifindex}, dump=True)
    except NlError as e:
        if e.error == 95:
            raise KsftSkipEx("shapers not supported by the device")
        raise

    # Each device implementing shaper support must support some
    # features in at least a scope.
    ksft_true(len(caps)> 0)

def set_qshapers(cfg, nl_shaper) -> None:
    try:
        caps = nl_shaper.cap_get({'ifindex': cfg.ifindex,
                                 'scope':'queue'})
    except NlError as e:
        if e.error == 95:
            raise KsftSkipEx("shapers not supported by the device")
        raise
    if not 'support-bw-max' in caps or not 'support-metric-bps' in caps:
        raise KsftSkipEx("device does not support queue scope shapers with bw_max and metric bps")

    _require_queues(cfg, 3)
    cfg.queues = True

    nl_shaper.set({'ifindex': cfg.ifindex,
                   'handle': {'scope': 'queue', 'id': 1},
                   'metric': 'bps',
                   'bw-max': 10000})
    nl_shaper.set({'ifindex': cfg.ifindex,
                   'handle': {'scope': 'queue', 'id': 2},
                   'metric': 'bps',
                   'bw-max': 20000})

    # Querying a specific shaper not yet configured must fail.
    raised = False
    try:
        shaper_q0 = nl_shaper.get({'ifindex': cfg.ifindex,
                                   'handle': {'scope': 'queue', 'id': 0}})
    except (NlError):
        raised = True
    ksft_eq(raised, True)

    shaper_q1 = nl_shaper.get({'ifindex': cfg.ifindex,
                              'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper_q1, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'netdev'},
                        'handle': {'scope': 'queue', 'id': 1},
                        'metric': 'bps',
                        'bw-max': 10000})

    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 1},
                       'metric': 'bps',
                       'bw-max': 10000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 2},
                       'metric': 'bps',
                       'bw-max': 20000}])

def del_qshapers(cfg, nl_shaper) -> None:
    if not cfg.queues:
        raise KsftSkipEx("queue shapers not supported by device, skipping delete")

    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 2}})
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 1}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def set_nshapers(cfg, nl_shaper) -> None:
    # Check required features.
    try:
        caps = nl_shaper.cap_get({'ifindex': cfg.ifindex,
                                  'scope':'netdev'})
    except NlError as e:
        if e.error == 95:
            raise KsftSkipEx("shapers not supported by the device")
        raise
    if not 'support-bw-max' in caps or not 'support-metric-bps' in caps:
        raise KsftSkipEx("device does not support nested netdev scope shapers with weight")

    cfg.netdev = True;
    nl_shaper.set({'ifindex': cfg.ifindex,
                   'handle': {'scope': 'netdev', 'id': 0},
                   'bw-max': 100000})

    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'handle': {'scope': 'netdev'},
                       'metric': 'bps',
                       'bw-max': 100000}])

def del_nshapers(cfg, nl_shaper) -> None:
    if not cfg.netdev:
        raise KsftSkipEx("netdev shaper not supported by device, skipping delete")

    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'netdev'}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def set_all_supported_attrs(cfg, nl_shaper) -> None:
    """ Set every queue-scope attribute the device advertises and verify the read-back. """
    _require_queues(cfg, 1)

    _require_caps(cfg, nl_shaper, 'queue', [],
                  "queue scope shapers not supported by the device")
    caps = _cap_get(cfg, nl_shaper, 'queue')

    attrs = {'ifindex': cfg.ifindex,
             'handle': {'scope': 'queue', 'id': 0}}
    expected = {'ifindex': cfg.ifindex,
                'parent': {'scope': 'netdev'},
                'handle': {'scope': 'queue', 'id': 0}}

    rate_attrs = {'support-bw-min': ('bw-min', 10000, 100),
                  'support-bw-max': ('bw-max', 20000, 200),
                  'support-burst': ('burst', 3000, 30)}
    rate_attr_supported = any(cap in caps for cap in rate_attrs)
    bps_supported = 'support-metric-bps' in caps
    pps_supported = 'support-metric-pps' in caps

    def add_rate_attrs(metric, value_idx) -> None:
        attrs['metric'] = metric
        expected['metric'] = metric
        for cap, (attr, bps_value, pps_value) in rate_attrs.items():
            if cap not in caps:
                continue

            value = bps_value if value_idx == 0 else pps_value
            attrs[attr] = value
            expected[attr] = value

    if rate_attr_supported:
        if bps_supported:
            add_rate_attrs('bps', 0)
        elif pps_supported:
            add_rate_attrs('pps', 1)

    if 'support-priority' in caps:
        attrs['priority'] = 1
        expected['priority'] = 1
    if 'support-weight' in caps:
        attrs['weight'] = 2
        expected['weight'] = 2

    if len(attrs) == 2:
        raise KsftSkipEx("device does not advertise any supported queue shaper attributes")

    nl_shaper.set(attrs)
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 0})

    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper, expected)

    if rate_attr_supported and bps_supported and pps_supported:
        add_rate_attrs('pps', 1)
        nl_shaper.set(attrs)

        shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                                'handle': {'scope': 'queue', 'id': 0}})
        ksft_eq(shaper, expected)

    _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': 0})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def invalid_set_preserves_state(cfg, nl_shaper) -> None:
    """ Verify a rejected .set leaves the existing shaper configuration unchanged. """
    nq = _require_queues(cfg, 1)
    _require_caps(cfg, nl_shaper, 'queue',
                  ['support-bw-max', 'support-metric-bps'],
                  "device does not support queue scope bw_max with bps metric")

    initial = {'ifindex': cfg.ifindex,
               'parent': {'scope': 'netdev'},
               'handle': {'scope': 'queue', 'id': 0},
               'metric': 'bps',
               'bw-max': 10000}
    nl_shaper.set({'ifindex': cfg.ifindex,
                   'handle': {'scope': 'queue', 'id': 0},
                   'metric': 'bps',
                   'bw-max': 10000})
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 0})

    with ksft_raises(NlError):
        nl_shaper.set({'ifindex': cfg.ifindex,
                       'handle': {'scope': 'node', 'id': 0},
                       'metric': 'bps',
                       'bw-max': 20000})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper, initial)

    with ksft_raises(NlError):
        nl_shaper.set({'ifindex': cfg.ifindex,
                       'handle': {'scope': 'queue', 'id': nq},
                       'metric': 'bps',
                       'bw-max': 20000})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper, initial)

    _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': 0})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def mixed_parent_group_requires_parent(cfg, nl_shaper) -> None:
    r"""Grouping leaves from different nodes requires an explicit parent.

        netdev             netdev
        /    \             parent=netdev
       N1     N2  group        N
       |      |   {Q0,Q1}     / \
       Q0     Q1  ------->   Q0  Q1

    Without an explicit parent the group is rejected; parent=netdev
    collapses the leaves into one new node.
    """
    _require_queues(cfg, 2)
    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps'],
                  "device does not support node scope shapers with bw_max and metric bps")
    _require_caps(cfg, nl_shaper, 'queue',
                  ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    n1_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    n1_id = n1_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 0})

    n2_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 2}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 20000})
    n2_id = n2_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 1})

    with ksft_raises(NlError):
        nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 3},
                             {'handle': {'scope': 'queue', 'id': 1},
                              'weight': 4}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 30000})

    shaper_q0 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper_q0, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n1_id},
                        'handle': {'scope': 'queue', 'id': 0},
                        'weight': 1})
    shaper_q1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper_q1, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n2_id},
                        'handle': {'scope': 'queue', 'id': 1},
                        'weight': 2})

    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 3},
                             {'handle': {'scope': 'queue', 'id': 1},
                              'weight': 4}],
                   'handle': {'scope':'node'},
                   'parent': {'scope': 'netdev'},
                   'metric': 'bps',
                   'bw-max': 30000})
    node_id = node_handle['handle']['id']

    for old_id in (n1_id, n2_id):
        with ksft_raises(NlError):
            nl_shaper.get({'ifindex': cfg.ifindex,
                           'handle': {'scope': 'node', 'id': old_id}})

    shaper_q0 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper_q0, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': node_id},
                        'handle': {'scope': 'queue', 'id': 0},
                        'weight': 3})
    shaper_q1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper_q1, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': node_id},
                        'handle': {'scope': 'queue', 'id': 1},
                        'weight': 4})

    for i in range(2):
        _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': i})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def recursive_empty_node_cleanup(cfg, nl_shaper) -> None:
    r"""Deleting the last leaf recursively removes the emptied ancestors.

        netdev             netdev
          |       del Q0
         N1       ------>   (N1 and N2 removed too)
          |
         N2
          |
         Q0
    """
    _require_queues(cfg, 1)
    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support nested node scope shapers")
    _require_caps(cfg, nl_shaper, 'queue',
                  ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    n1_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    n1_id = n1_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 0})

    n2_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'parent': {'scope': 'node', 'id': n1_id},
                   'metric': 'bps',
                   'bw-max': 5000})
    n2_id = n2_handle['handle']['id']

    shaper_q0 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 0}})
    ksft_eq(shaper_q0, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n2_id},
                        'handle': {'scope': 'queue', 'id': 0},
                        'weight': 1})

    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 0}})

    for handle in ({'scope': 'queue', 'id': 0},
                   {'scope': 'node', 'id': n2_id},
                   {'scope': 'node', 'id': n1_id}):
        with ksft_raises(NlError):
            nl_shaper.get({'ifindex': cfg.ifindex, 'handle': handle})

    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def _group_under_netdev(cfg, nl_shaper, bw_max=None):
    r"""Group queues under a netdev-scope node; caller owns node teardown.

        netdev               netdev
         /  \      del Q1,Q2
        Q1  Q2     ------->   (netdev node persists)
    """
    group_args = {
        'ifindex': cfg.ifindex,
        'leaves': [{'handle': {'scope': 'queue', 'id': 1},
                    'weight': 1},
                   {'handle': {'scope': 'queue', 'id': 2},
                    'weight': 2}],
        'handle': {'scope': 'netdev'}}
    if bw_max:
        group_args['metric'] = 'bps'
        group_args['bw-max'] = bw_max

    node_handle = nl_shaper.group(group_args)
    ksft_eq(node_handle, {'ifindex': cfg.ifindex,
                          'handle': {'scope': 'netdev'}})

    del_node = defer(_delete_shaper, cfg, nl_shaper, {'scope': 'netdev'})
    del_queues = [defer(_delete_shaper, cfg, nl_shaper,
                        {'scope': 'queue', 'id': qid})
                  for qid in (1, 2)]

    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'netdev'},
                     'handle': {'scope': 'queue', 'id': 1},
                     'weight': 1})
    for dq in del_queues:
        dq.exec()

    # Caller owns the node teardown so it can verify the netdev-scope node
    # survives leaf deletion before removing it.
    return del_node

def basic_groups(cfg, nl_shaper) -> None:
    r"""Group queues under a netdev-scope node, then tear it down.

        netdev
         /  \
        Q1  Q2
    """
    _require_queues(cfg, 3)

    _require_caps(cfg, nl_shaper, 'netdev', [], "netdev scope not supported by the device")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "queue scope not supported with nesting and weight")

    del_node = _group_under_netdev(cfg, nl_shaper)

    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'handle': {'scope': 'netdev'}}])

    del_node.exec()
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def basic_groups_with_rate(cfg, nl_shaper) -> None:
    r"""Rate-limited netdev-scope node outlives deletion of its leaves.

        netdev[10kbps]          netdev[10kbps]
          /  \       del Q1,Q2
        Q1    Q2     ------->    (node persists)
    """
    bw_max = 10000

    _require_queues(cfg, 3)

    _require_caps(cfg, nl_shaper, 'netdev', ['support-bw-max', 'support-metric-bps'],
                  "device does not support netdev scope rate limiting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support queue scope shapers with nesting and weight")

    del_node = _group_under_netdev(cfg, nl_shaper, bw_max=bw_max)

    # Deleting all the leaves shaper does not affect the node one
    # when the latter has 'netdev' scope.
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'handle': {'scope': 'netdev'},
                       'metric': 'bps',
                       'bw-max': bw_max}])

    del_node.exec()

def qgroups(cfg, nl_shaper) -> None:
    _require_queues(cfg, 4)
    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps'],
                  "device does not support node scope shapers with bw_max and metric bps")
    _require_caps(cfg, nl_shaper, 'queue',
                  ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 3},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 2}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    node_id = node_handle['handle']['id']

    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node_id},
                     'handle': {'scope': 'queue', 'id': 1},
                     'weight': 3})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': 10000})

    # Grouping to a specified, not existing node scope shaper must fail
    raised = False
    try:
        nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 3}],
                   'handle': {'scope':'node', 'id': node_id + 1},
                   'metric': 'bps',
                   'bw-max': 10000})

    except (NlError):
        raised = True
    ksft_eq(raised, True)

    # Add to an existing node
    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 4}],
                   'handle': {'scope':'node', 'id': node_id}})
    ksft_eq(node_handle, {'ifindex': cfg.ifindex,
                          'handle': {'scope': 'node', 'id': node_id}})

    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 3}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node_id},
                     'handle': {'scope': 'queue', 'id': 3},
                     'weight': 4})

    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 2}})
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 1}})

    # Deleting a non empty node will move the leaves downstream.
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'node', 'id': node_id}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 3},
                       'weight': 4}])

    # Finish and verify the complete cleanup.
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'queue', 'id': 3}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def set_node_shaper(cfg, nl_shaper) -> None:
    """ Verify a node-scope shaper rate can be updated via .set. """
    _require_queues(cfg, 2)
    _require_caps(cfg, nl_shaper, 'node', ['support-bw-max', 'support-metric-bps'],
                  "device does not support node scope shapers with bw_max and metric bps")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    node_id = node_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 1})

    # Update the node's rate via .set
    nl_shaper.set({'ifindex': cfg.ifindex,
                   'handle': {'scope': 'node', 'id': node_id},
                   'metric': 'bps',
                   'bw-max': 20000})

    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': 20000})

    # Cleanup
    _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': 1})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def group_update_rate(cfg, nl_shaper) -> None:
    """ Verify re-grouping a node updates its rate while leaving the leaves untouched. """
    _require_queues(cfg, 3)
    _require_caps(cfg, nl_shaper, 'node', ['support-bw-max', 'support-metric-bps'],
                  "device does not support node scope shapers with bw_max and metric bps")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    # Create node with Q1, Q2 at bw_max=10000
    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    node_id = node_handle['handle']['id']
    for i in range(1, 3):
        defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': i})

    # Update rate via .group on the same node
    nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 1}],
                   'handle': {'scope':'node', 'id': node_id},
                   'metric': 'bps',
                   'bw-max': 50000})

    # Verify rate updated
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': 50000})

    # Verify leaves unchanged
    shaper_q1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper_q1, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': node_id},
                        'handle': {'scope': 'queue', 'id': 1},
                        'weight': 1})
    shaper_q2 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 2}})
    ksft_eq(shaper_q2, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': node_id},
                        'handle': {'scope': 'queue', 'id': 2},
                        'weight': 1})

    # Make sure we only have 3 shapers including 2 queues and the node
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 3)

    # Cleanup
    for i in range(1, 3):
        _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': i})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def delegation(cfg, nl_shaper) -> None:
    _require_queues(cfg, 4)
    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support node scope shapers with bw_max, metric bps and nesting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 3},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 2},
                             {'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})
    node_id = node_handle['handle']['id']

    # Create the nested node and validate the hierarchy
    nested_node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 3},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 2}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 5000})
    nested_node_id = nested_node_handle['handle']['id']
    ksft_true(nested_node_id != node_id)
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': nested_node_id},
                       'handle': {'scope': 'queue', 'id': 1},
                       'weight': 3},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': nested_node_id},
                       'handle': {'scope': 'queue', 'id': 2},
                       'weight': 2},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': node_id},
                       'handle': {'scope': 'queue', 'id': 3},
                       'weight': 1},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'node', 'id': node_id},
                       'metric': 'bps',
                       'bw-max': 10000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': node_id},
                       'handle': {'scope': 'node', 'id': nested_node_id},
                       'metric': 'bps',
                       'bw-max': 5000}])

    # Deleting a non empty node will move the leaves downstream.
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'node', 'id': nested_node_id}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': node_id},
                       'handle': {'scope': 'queue', 'id': 1},
                       'weight': 3},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': node_id},
                       'handle': {'scope': 'queue', 'id': 2},
                       'weight': 2},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'node', 'id': node_id},
                       'handle': {'scope': 'queue', 'id': 3},
                       'weight': 1},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'node', 'id': node_id},
                       'metric': 'bps',
                       'bw-max': 10000}])

    # Final cleanup.
    for i in range(1, 4):
        nl_shaper.delete({'ifindex': cfg.ifindex,
                          'handle': {'scope': 'queue', 'id': i}})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def nested_depth_limit(cfg, nl_shaper) -> None:
    r"""Nest nodes as deep as the device allows to find the max depth.

        netdev
          |
         N1 -- Q1
          |
         N2 -- Q2
          |
         N3 -- Q3
          :       (deepen until the driver rejects)
    """
    bw_max = 10000

    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support node scope shapers with bw_max, metric bps and nesting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    nq = _require_queues(cfg, 3)

    node_ids = []
    cleanups = []
    queue_id = 1
    max_depth = 0
    limit_err = None

    # Create initial node with a queue leaf
    node_id = nl_shaper.group({
        'ifindex': cfg.ifindex,
        'leaves': [{'handle': {'scope': 'queue', 'id': queue_id},
                     'weight': 1}],
        'handle': {'scope': 'node'},
        'metric': 'bps',
        'bw-max': bw_max})['handle']['id']
    node_ids.append(node_id)
    cleanups.append(defer(_delete_shaper, cfg, nl_shaper,
                          {'scope': 'node', 'id': node_id}))
    cleanups.append(defer(_delete_shaper, cfg, nl_shaper,
                          {'scope': 'queue', 'id': queue_id}))
    max_depth = 1
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': bw_max})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': queue_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node_id},
                     'handle': {'scope': 'queue', 'id': queue_id},
                     'weight': 1})
    queue_id += 1

    # Keep nesting deeper until the driver rejects or queues run out.
    while queue_id < nq:
        parent_id = node_ids[-1]
        try:
            node_id = nl_shaper.group({
                'ifindex': cfg.ifindex,
                'leaves': [{'handle': {'scope': 'queue',
                                       'id': queue_id},
                             'weight': 1}],
                'handle': {'scope': 'node'},
                'parent': {'scope': 'node',
                           'id': parent_id},
                'metric': 'bps',
                'bw-max': bw_max})['handle']['id']
        except NlError as e:
            # Only treat "cannot nest deeper" errors as the depth limit;
            # drivers report it differently (EOPNOTSUPP/ENOSPC/E2BIG/EINVAL).
            # Anything else (ENOMEM, EIO, EPERM, driver bug) is a real failure.
            if e.error not in (errno.EOPNOTSUPP, errno.ENOSPC,
                               errno.E2BIG, errno.EINVAL):
                raise
            limit_err = e
            break

        node_ids.append(node_id)
        cleanups.append(defer(_delete_shaper, cfg, nl_shaper,
                              {'scope': 'node', 'id': node_id}))
        cleanups.append(defer(_delete_shaper, cfg, nl_shaper,
                              {'scope': 'queue', 'id': queue_id}))
        max_depth += 1
        shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                                'handle': {'scope': 'node', 'id': node_id}})
        ksft_eq(shaper, {'ifindex': cfg.ifindex,
                         'handle': {'scope': 'node', 'id': node_id},
                         'parent': {'scope': 'node', 'id': parent_id},
                         'metric': 'bps',
                         'bw-max': bw_max})
        shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                                'handle': {'scope': 'queue',
                                           'id': queue_id}})
        ksft_eq(shaper, {'ifindex': cfg.ifindex,
                         'parent': {'scope': 'node', 'id': node_id},
                         'handle': {'scope': 'queue', 'id': queue_id},
                         'weight': 1})
        queue_id += 1

    if limit_err:
        print(f"# max nesting depth supported: {max_depth} (errno {limit_err.error})")
    else:
        print(f"# max nesting depth tested: {max_depth}")
    ksft_true(max_depth >= 2,
              f"max nesting depth: {max_depth}")

    # Cleanup: exec the deferred deletes in reverse creation order, so each
    # queue leaf and deeper node is removed before its parent node.
    for cleanup in reversed(cleanups):
        cleanup.exec()
    ksft_eq(len(nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)), 0)

def delete_child_reparent(cfg, nl_shaper) -> None:
    r"""Deleting a child node reparents its queue leaf to the parent.

        netdev              netdev
          |                   |
          N1      del N2      N1
        / | \     ----->    / | \
      Q1 Q2 N2            Q1 Q2 Q3
             |
            Q3
    """
    n1_bw_max = 10000
    n2_bw_max = 5000

    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support node scope shapers with bw_max, metric bps and nesting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    _require_queues(cfg, 4)

    # Create parent node N1 with Q1, Q2
    n1_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': n1_bw_max})
    n1_id = n1_handle['handle']['id']
    for i in range(1, 3):
        defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': i})

    # Create child node N2 under N1 with Q3
    n2_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'parent': {'scope': 'node', 'id': n1_id},
                   'metric': 'bps',
                   'bw-max': n2_bw_max})
    n2_id = n2_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 3})

    # Delete child N2 - Q3 should reparent to N1
    nl_shaper.delete({'ifindex': cfg.ifindex,
                      'handle': {'scope': 'node', 'id': n2_id}})

    with ksft_raises(NlError):
        nl_shaper.get({'ifindex': cfg.ifindex,
                       'handle': {'scope': 'node', 'id': n2_id}})

    shaper_n1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'node', 'id': n1_id}})
    ksft_eq(shaper_n1, {'ifindex': cfg.ifindex,
                        'handle': {'scope': 'node', 'id': n1_id},
                        'parent': {'scope': 'netdev'},
                        'metric': 'bps',
                        'bw-max': n1_bw_max})
    shaper_q3 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 3}})
    ksft_eq(shaper_q3, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n1_id},
                        'handle': {'scope': 'queue', 'id': 3},
                        'weight': 1})

    # Cleanup
    for i in range(1, 4):
        _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': i})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def move_queue_between_nodes(cfg, nl_shaper) -> None:
    r"""Move a queue between nodes by re-grouping the destination node.

        netdev                  netdev
        /    \    .group N2      /    \
       N1     N2  {Q1,Q3}       N1     N2
      /  \    |   ------->       |    /  \
     Q1  Q2  Q3                 Q2  Q1   Q3
    """
    n1_bw_max = 10000
    n2_bw_max = 20000

    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support node scope shapers with bw_max, metric bps and nesting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    _require_queues(cfg, 4)

    # Create N1 with Q1, Q2
    n1_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1},
                             {'handle': {'scope': 'queue', 'id': 2},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': n1_bw_max})
    n1_id = n1_handle['handle']['id']
    for i in range(1, 3):
        defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': i})

    # Create N2 with Q3
    n2_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': n2_bw_max})
    n2_id = n2_handle['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 3})

    # Move Q1 from N1 to N2 by re-grouping N2 with Q1, Q3
    nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 2},
                             {'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node', 'id': n2_id},
                   'metric': 'bps',
                   'bw-max': n2_bw_max})

    shaper_n1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'node', 'id': n1_id}})
    ksft_eq(shaper_n1, {'ifindex': cfg.ifindex,
                        'handle': {'scope': 'node', 'id': n1_id},
                        'parent': {'scope': 'netdev'},
                        'metric': 'bps',
                        'bw-max': n1_bw_max})
    shaper_n2 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'node', 'id': n2_id}})
    ksft_eq(shaper_n2, {'ifindex': cfg.ifindex,
                        'handle': {'scope': 'node', 'id': n2_id},
                        'parent': {'scope': 'netdev'},
                        'metric': 'bps',
                        'bw-max': n2_bw_max})

    # Verify Q1 moved to N2
    shaper_q1 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper_q1, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n2_id},
                        'handle': {'scope': 'queue', 'id': 1},
                        'weight': 2})

    # Verify Q2 still under N1
    shaper_q2 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 2}})
    ksft_eq(shaper_q2, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n1_id},
                        'handle': {'scope': 'queue', 'id': 2},
                        'weight': 1})

    # Verify Q3 remained under N2
    shaper_q3 = nl_shaper.get({'ifindex': cfg.ifindex,
                               'handle': {'scope': 'queue', 'id': 3}})
    ksft_eq(shaper_q3, {'ifindex': cfg.ifindex,
                        'parent': {'scope': 'node', 'id': n2_id},
                        'handle': {'scope': 'queue', 'id': 3},
                        'weight': 1})

    # Cleanup
    for i in range(1, 4):
        _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': i})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def reject_reparenting(cfg, nl_shaper) -> None:
    r"""Reject reparenting an existing node; the hierarchy stays intact.

        netdev
        /    \      rejected:  N3 -> netdev
       N1     N2    rejected:  N1 -> N2
      /  \    |     (both EOPNOTSUPP)
     Q1  N3  Q2
         |
         Q3
    """
    node1_bw_max = 10000
    node2_bw_max = 5000
    node3_bw_max = 20000

    _require_caps(cfg, nl_shaper, 'node',
                  ['support-bw-max', 'support-metric-bps', 'support-nesting'],
                  "device does not support node scope shapers with bw_max, metric bps and nesting")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    _require_queues(cfg, 4)

    # Create Node1 under netdev with Q1.
    node1_id = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': node1_bw_max})['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 1})
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'node', 'id': node1_id})

    # Create Node2 under netdev with Q2.
    node2_id = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 2},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': node2_bw_max})['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 2})
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'node', 'id': node2_id})

    # Create Node3 nested under Node1 with Q3.
    node3_id = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': node3_bw_max,
                   'parent': {'scope': 'node', 'id': node1_id}})['handle']['id']
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'queue', 'id': 3})
    defer(_delete_shaper, cfg, nl_shaper, {'scope': 'node', 'id': node3_id})

    # Reparenting a nested node up to netdev must fail.
    with ksft_raises(NlError) as cm:
        nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 3},
                              'weight': 1}],
                   'handle': {'scope':'node', 'id': node3_id},
                   'parent': {'scope': 'netdev'}})
    if cm.exception:
        ksft_eq(cm.exception.error, errno.EOPNOTSUPP)

    # Reparenting a node under another node must fail as well.
    with ksft_raises(NlError) as cm:
        nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 1}],
                   'handle': {'scope':'node', 'id': node1_id},
                   'parent': {'scope': 'node', 'id': node2_id}})
    if cm.exception:
        ksft_eq(cm.exception.error, errno.EOPNOTSUPP)

    # Updating a node with the same parent must succeed.
    nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 1},
                              'weight': 5}],
                   'handle': {'scope':'node', 'id': node1_id},
                   'parent': {'scope': 'netdev'}})

    # Updating a node without specifying the parent must succeed.
    nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 2},
                              'weight': 7}],
                   'handle': {'scope':'node', 'id': node2_id}})

    # The rejected reparents must have left the hierarchy intact.
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node1_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node1_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': node1_bw_max})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node2_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node2_id},
                     'parent': {'scope': 'netdev'},
                     'metric': 'bps',
                     'bw-max': node2_bw_max})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'node', 'id': node3_id}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'handle': {'scope': 'node', 'id': node3_id},
                     'parent': {'scope': 'node', 'id': node1_id},
                     'metric': 'bps',
                     'bw-max': node3_bw_max})

    # Verify the leaf weights were updated and parents unchanged.
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 1}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node1_id},
                     'handle': {'scope': 'queue', 'id': 1},
                     'weight': 5})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 2}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node2_id},
                     'handle': {'scope': 'queue', 'id': 2},
                     'weight': 7})
    shaper = nl_shaper.get({'ifindex': cfg.ifindex,
                            'handle': {'scope': 'queue', 'id': 3}})
    ksft_eq(shaper, {'ifindex': cfg.ifindex,
                     'parent': {'scope': 'node', 'id': node3_id},
                     'handle': {'scope': 'queue', 'id': 3},
                     'weight': 1})

    # Cleanup. Delete the nodes explicitly instead of relying on the
    # empty-node auto-delete: a kernel that wrongly accepts a reparent may
    # mishandle the leaf accounting and leave a node behind. Removing them
    # by handle keeps a failing run from leaking state into later tests.
    for i in range(1, 4):
        _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': i})
    for nid in (node1_id, node2_id, node3_id):
        _delete_shaper(cfg, nl_shaper, {'scope': 'node', 'id': nid})
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(len(shapers), 0)

def queue_update(cfg, nl_shaper) -> None:
    nq = _require_queues(cfg, 4)
    if not cfg.queues:
        raise KsftSkipEx("device does not support queue scope")

    netnl = EthtoolFamily()
    channels = netnl.channels_get({'header': {'dev-index': cfg.ifindex}})
    ch_type = 'combined' if channels['combined-count'] else 'tx'

    for i in range(3):
        nl_shaper.set({'ifindex': cfg.ifindex,
                       'handle': {'scope': 'queue', 'id': i},
                       'metric': 'bps',
                       'bw-max': (i + 1) * 1000})
    defer(cmd, f"ethtool -L {cfg.dev['ifname']} {ch_type} {nq}")

    # Delete a channel, with no shapers configured on top of the related
    # queue: no changes expected
    cmd(f"ethtool -L {cfg.dev['ifname']} {ch_type} 3")
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 0},
                       'metric': 'bps',
                       'bw-max': 1000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 1},
                       'metric': 'bps',
                       'bw-max': 2000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 2},
                       'metric': 'bps',
                       'bw-max': 3000}])

    # Delete a channel, with a shaper configured on top of the related
    # queue: the shaper must be deleted, too
    cmd(f"ethtool -L {cfg.dev['ifname']} {ch_type} 2")

    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 0},
                       'metric': 'bps',
                       'bw-max': 1000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 1},
                       'metric': 'bps',
                       'bw-max': 2000}])

    # Restore the original channels number, no expected changes
    cmd(f"ethtool -L {cfg.dev['ifname']} {ch_type} {nq}")
    shapers = nl_shaper.get({'ifindex': cfg.ifindex}, dump=True)
    ksft_eq(shapers, [{'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 0},
                       'metric': 'bps',
                       'bw-max': 1000},
                      {'ifindex': cfg.ifindex,
                       'parent': {'scope': 'netdev'},
                       'handle': {'scope': 'queue', 'id': 1},
                       'metric': 'bps',
                       'bw-max': 2000}])

    # Final cleanup.
    for i in range(0, 2):
        nl_shaper.delete({'ifindex': cfg.ifindex,
                          'handle': {'scope': 'queue', 'id': i}})

def dup_leaves(cfg, nl_shaper) -> None:
    """ Ensure that the kernel rejects duplicate leaves. """
    _require_caps(cfg, nl_shaper, 'node', ['support-bw-max', 'support-metric-bps'],
                  "device does not support node scope shapers with bw_max and metric bps")
    _require_caps(cfg, nl_shaper, 'queue', ['support-nesting', 'support-weight'],
                  "device does not support nested queue scope shapers with weight")

    node_handle = None
    with ksft_raises(NlError) as cm:
        node_handle = nl_shaper.group({
                   'ifindex': cfg.ifindex,
                   'leaves':[{'handle': {'scope': 'queue', 'id': 0},
                              'weight': 1},
                             {'handle': {'scope': 'queue', 'id': 0},
                              'weight': 2}],
                   'handle': {'scope':'node'},
                   'metric': 'bps',
                   'bw-max': 10000})

    # Clean up in case the kernel wrongly accepted the request.
    if node_handle:
        _delete_shaper(cfg, nl_shaper, node_handle['handle'])
    _delete_shaper(cfg, nl_shaper, {'scope': 'queue', 'id': 0})

    # ksft_raises() has already recorded the failure if nothing was raised.
    if cm.exception is None:
        return
    ksft_eq(cm.exception.error, errno.EINVAL)

def main() -> None:
    with NetDrvEnv(__file__, queue_count=4) as cfg:
        cfg.queues = False
        cfg.netdev = False
        ksft_run([get_shapers,
                  get_caps,
                  set_qshapers,
                  del_qshapers,
                  set_nshapers,
                  del_nshapers,
                  set_all_supported_attrs,
                  invalid_set_preserves_state,
                  mixed_parent_group_requires_parent,
                  recursive_empty_node_cleanup,
                  basic_groups,
                  basic_groups_with_rate,
                  qgroups,
                  set_node_shaper,
                  group_update_rate,
                  delegation,
                  nested_depth_limit,
                  delete_child_reparent,
                  move_queue_between_nodes,
                  reject_reparenting,
                  dup_leaves,
                  queue_update],
                 args=(cfg, NetshaperFamily()))
    ksft_exit()


if __name__ == "__main__":
    main()