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
|
# bash completion for devlink(8) -*- shell-script -*-
# Get all the optional commands for devlink
_devlink_get_optional_commands()
{
local object=$1; shift
local filter_options=""
local options="$(devlink $object help 2>&1 \
| command sed -n -e "s/^.*devlink $object //p" \
| cut -d " " -f 1)"
# Remove duplicate options from "devlink $OBJECT help" command
local opt
for opt in $options; do
if [[ $filter_options =~ $opt ]]; then
continue
else
filter_options="$filter_options $opt"
fi
done
echo $filter_options
}
# Complete based on given word, for when an argument or an option name has
# but a few possible arguments.
_devlink_direct_complete()
{
local dev port region value
case $1 in
dev)
value=$(devlink dev show 2>/dev/null)
;;
selftests_id)
dev=${words[4]}
value=$(devlink -j dev selftests show 2>/dev/null \
| jq ".selftests[\"$dev\"][]")
;;
param_name)
dev=${words[4]}
value=$(devlink -j dev param show 2>/dev/null \
| jq ".param[\"$dev\"][].name")
;;
port)
value=$(devlink -j port show 2>/dev/null \
| jq '.port as $ports | $ports | keys[] as $key
| ($ports[$key].netdev // $key)')
;;
lc)
dev=${words[3]}
value=$(devlink -j lc show 2>/dev/null \
| jq ".lc[\"$dev\"]" \
| jq '. as $lcs | $lcs | keys[] as $key |($lcs[$key].lc)' \
2>/dev/null)
;;
lc_type)
dev=${words[3]}
lc=${words[5]}
value=$(devlink lc show $dev lc $lc -j 2>/dev/null \
| jq '.[][][]["supported_types"][]')
;;
region)
value=$(devlink -j region show 2>/dev/null \
| jq '.regions' | jq 'keys[]')
;;
snapshot)
region=${words[3]}
value=$(devlink -j region show 2>/dev/null \
| jq ".regions[\"$region\"].snapshot[]")
;;
trap)
dev=${words[3]}
value=$(devlink -j trap show 2>/dev/null \
| jq ".trap[\"$dev\"][].name")
;;
trap_group)
dev=${words[4]}
value=$(devlink -j trap group show 2>/dev/null \
| jq ".trap_group[\"$dev\"][].name")
;;
trap_policer)
dev=${words[4]}
value=$(devlink -j trap policer show 2>/dev/null \
| jq ".trap_policer[\"$dev\"][].policer")
;;
health_dev)
value=$(devlink -j health show 2>/dev/null | jq '.health' \
| jq 'keys[]')
;;
reporter)
dev=${words[cword - 2]}
value=$(devlink -j health show 2>/dev/null \
| jq ".health[\"$dev\"][].reporter")
;;
pool)
dev=$pprev
value=$(devlink -j sb pool show 2>/dev/null \
| jq ".pool[\"$dev\"][].pool")
;;
port_pool)
port=${words[5]}
value=$(devlink -j sb port pool show 2>/dev/null \
| jq ".port_pool[\"$port\"][].pool")
;;
tc)
port=$pprev
value=$(devlink -j sb tc bind show 2>/dev/null \
| jq ".tc_bind[\"$port\"][].tc")
;;
esac
COMPREPLY+=( $( compgen -W "$value" -- "$cur" ) )
# Remove colon containing prefix from COMPREPLY items in order to avoid
# wordbreaks with colon.
__ltrim_colon_completions "$cur"
}
# Completion for devlink dev eswitch set
_devlink_dev_eswitch_set()
{
local -A settings=(
[mode]=notseen
[inline-mode]=notseen
[encap-mode]=notseen
)
if [[ $cword -eq 5 ]]; then
COMPREPLY=( $( compgen -W "mode inline-mode encap-mode" -- "$cur" ) )
fi
# Mark seen settings
local word
for word in "${words[@]:5:${#words[@]}-1}"; do
if [[ -n $word ]]; then
if [[ "${settings[$word]}" ]]; then
settings[$word]=seen
fi
fi
done
case $prev in
mode)
COMPREPLY=( $( compgen -W "legacy switchdev" -- "$cur" ) )
return
;;
inline-mode)
COMPREPLY=( $( compgen -W "none link network transport" -- \
"$cur" ) )
return
;;
encap-mode)
COMPREPLY=( $( compgen -W "none basic" -- "$cur" ) )
return
;;
esac
local -a comp_words=()
# Add settings not seen to completions
local setting
for setting in "${!settings[@]}"; do
if [ "${settings[$setting]}" = notseen ]; then
comp_words+=( "$setting" )
fi
done
COMPREPLY=( $( compgen -W "${comp_words[*]}" -- "$cur" ) )
}
# Completion for devlink dev eswitch
_devlink_dev_eswitch()
{
case "$cword" in
3)
COMPREPLY=( $( compgen -W "show set" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "dev"
return
;;
esac
case "${words[3]}" in
set)
_devlink_dev_eswitch_set
return
;;
show)
return
;;
esac
}
# Completion for devlink dev param set
_devlink_dev_param_set()
{
case $cword in
7)
COMPREPLY=( $( compgen -W "value" -- "$cur" ) )
return
;;
8)
# String argument
return
;;
9)
COMPREPLY=( $( compgen -W "cmode" -- "$cur" ) )
return
;;
10)
COMPREPLY=( $( compgen -W "runtime driverinit permanent" -- \
"$cur" ) )
return
;;
esac
}
# Completion for devlink dev param
_devlink_dev_param()
{
case "$cword" in
3)
COMPREPLY=( $( compgen -W "show set" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "dev"
return
;;
5)
COMPREPLY=( $( compgen -W "name" -- "$cur" ) )
return
;;
6)
_devlink_direct_complete "param_name"
return
;;
esac
if [[ "${words[3]}" == "set" ]]; then
_devlink_dev_param_set
fi
}
# Completion for devlink dev reload
_devlink_dev_reload()
{
case "$cword" in
4)
COMPREPLY=( $( compgen -W "netns" -- "$cur" ) )
return
;;
5)
local nslist=$( ip netns list 2>/dev/null )
COMPREPLY=( $( compgen -W "$nslist" -- "$cur" ) )
return
;;
esac
}
# Completion for devlink dev flash
_devlink_dev_flash()
{
case "$cword" in
4)
COMPREPLY=( $( compgen -W "file" -- "$cur" ) )
return
;;
5)
_filedir
return
;;
6)
COMPREPLY=( $( compgen -W "component" -- "$cur" ) )
return
;;
esac
}
# Completion for devlink dev selftests
_devlink_dev_selftests()
{
if [[ $cword -gt 5 ]]; then
_devlink_direct_complete "selftests_id"
return
fi
case "$cword" in
3)
COMPREPLY=( $( compgen -W "show run" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "dev"
return
;;
5)
COMPREPLY=( $( compgen -W "id" -- "$cur" ) )
return
;;
esac
}
# Completion for devlink dev
_devlink_dev()
{
case $command in
show|reload|info|flash)
if [[ $cword -le 3 ]]; then
_devlink_direct_complete "dev"
elif [[ $command == "reload" || $command == "flash" ]];then
_devlink_dev_$command
fi
return
;;
eswitch|param|selftests)
_devlink_dev_$command
return
;;
esac
}
# Completion for devlink port set
_devlink_port_set()
{
case "$cword" in
3)
_devlink_direct_complete "port"
return
;;
4)
COMPREPLY=( $( compgen -W "type" -- "$cur" ) )
return
;;
5)
COMPREPLY=( $( compgen -W "eth ib auto" -- "$cur" ) )
return
;;
esac
}
# Completion for devlink port split
_devlink_port_split()
{
case "$cword" in
3)
_devlink_direct_complete "port"
return
;;
4)
COMPREPLY=( $( compgen -W "count" -- "$cur" ) )
return
;;
5)
# Integer argument
return
;;
esac
}
# Completion for devlink port param set
_devlink_port_param_set()
{
case $cword in
7)
COMPREPLY=( $( compgen -W "value" -- "$cur" ) )
return
;;
8)
# String argument
return
;;
9)
COMPREPLY=( $( compgen -W "cmode" -- "$cur" ) )
return
;;
10)
COMPREPLY=( $( compgen -W "runtime driverinit permanent" -- \
"$cur" ) )
return
;;
esac
}
# Completion for devlink port param
_devlink_port_param()
{
case "$cword" in
3)
COMPREPLY=( $( compgen -W "show set" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "port"
return
;;
5)
COMPREPLY=( $( compgen -W "name" -- "$cur" ) )
return
;;
6)
_devlink_direct_complete "param_name"
return
;;
esac
if [[ "${words[3]}" == "set" ]]; then
_devlink_port_param_set
fi
}
# Completion for devlink port
_devlink_port()
{
case $command in
set)
_devlink_port_set
return
;;
split)
_devlink_port_split
return
;;
param)
_devlink_port_param
return
;;
show|unsplit)
if [[ $cword -eq 3 ]]; then
_devlink_direct_complete "port"
fi
return
;;
esac
}
# Completion for devlink lc set
_devlink_lc_set()
{
case "$cword" in
3)
_devlink_direct_complete "dev"
return
;;
4)
COMPREPLY=( $( compgen -W "lc" -- "$cur" ) )
;;
5)
_devlink_direct_complete "lc"
;;
6)
COMPREPLY=( $( compgen -W "type notype" -- "$cur" ) )
return
;;
7)
if [[ "$prev" == "type" ]]; then
_devlink_direct_complete "lc_type"
fi
esac
}
# Completion for devlink lc show
_devlink_lc_show()
{
case $cword in
3)
_devlink_direct_complete "dev"
;;
4)
COMPREPLY=( $( compgen -W "lc" -- "$cur" ) )
;;
5)
_devlink_direct_complete "lc"
;;
esac
}
# Completion for devlink lc
_devlink_lc()
{
case $command in
set)
_devlink_lc_set
return
;;
show)
_devlink_lc_show
return
;;
esac
}
# Completion for devlink dpipe
_devlink_dpipe()
{
local options="$(devlink dpipe help 2>&1 \
| command sed -e '/OBJECT-LIST := /!d' \
-e 's/.*{ //' -e 's/}.*//' -e 's/|//g' )"
if [[ $cword -eq 2 ]]; then
COMPREPLY+=( $( compgen -W "$options" -- "$cur" ) )
fi
}
# Completion for devlink monitor
_devlink_monitor()
{
local options="$(devlink monitor help 2>&1 \
| command sed -e '/OBJECT-LIST := /!d' \
-e 's/.*{ //' -e 's/}.*//' -e 's/|//g' )"
if [[ $cword -eq 2 ]]; then
COMPREPLY+=( $( compgen -W "all $options" -- "$cur" ) )
fi
}
# Completion for the rest of devlink sb $command
_devlink_sb_command_options()
{
local subcmd
case $command in
pool)
subcmd=${words[3]}
if [[ $cword -eq 5 ]]; then
COMPREPLY=( $( compgen -W "pool" -- "$cur" ) )
fi
if [[ $subcmd == "set" ]]; then
case $cword in
7)
COMPREPLY+=( $( compgen -W "size" -- "$cur" ) )
;;
9)
COMPREPLY+=( $( compgen -W "thtype" -- "$cur" ) )
;;
esac
fi
;;
port)
subcmd=${words[4]}
if [[ $cword -eq 6 ]]; then
COMPREPLY+=( $( compgen -W "pool" -- "$cur" ) )
fi
if [[ $subcmd == "set" ]]; then
case $cword in
8)
COMPREPLY+=( $( compgen -W "th" -- "$cur" ) )
;;
esac
fi
;;
tc)
subcmd=${words[4]}
case $cword in
6)
COMPREPLY+=( $( compgen -W "tc" -- "$cur" ) )
;;
8)
COMPREPLY+=( $( compgen -W "type" -- "$cur" ) )
;;
esac
if [[ $subcmd == "set" ]]; then
case $cword in
10)
COMPREPLY+=( $( compgen -W "pool" -- "$cur" ) )
;;
12)
COMPREPLY+=( $( compgen -W "th" -- "$cur" ) )
;;
esac
fi
;;
esac
}
# Completion for devlink sb
_devlink_sb()
{
case $prev in
bind)
COMPREPLY=( $( compgen -W "set show" -- "$cur" ) )
;;
occupancy)
COMPREPLY=( $( compgen -W "show snapshot clearmax" -- "$cur" ) )
;;
pool)
if [[ $cword -eq 3 || $cword -eq 4 ]]; then
COMPREPLY=( $( compgen -W "set show" -- "$cur" ) )
elif [[ $command == "port" || $command == "tc" ]]; then
_devlink_direct_complete "port_pool"
else
_devlink_direct_complete "pool"
fi
;;
port)
if [[ $cword -eq 3 ]]; then
COMPREPLY=( $( compgen -W "pool" -- "$cur" ) )
fi
;;
show|set|snapshot|clearmax)
case $command in
show|pool|occupancy)
_devlink_direct_complete "dev"
if [[ $command == "occupancy" && $prev == "show" ]];then
_devlink_direct_complete "port"
fi
;;
port|tc)
_devlink_direct_complete "port"
;;
esac
;;
size)
# Integer argument
;;
thtype)
COMPREPLY=( $( compgen -W "static dynamic" -- "$cur" ) )
;;
th)
# Integer argument
;;
tc)
if [[ $cword -eq 3 ]]; then
COMPREPLY=( $( compgen -W "bind" -- "$cur" ) )
else
_devlink_direct_complete "tc"
fi
;;
type)
COMPREPLY=( $( compgen -W "ingress egress" -- "$cur" ) )
;;
esac
_devlink_sb_command_options
return
}
# Completion for devlink resource set path argument
_devlink_resource_path()
{
local path parents parent all_path
local dev=${words[3]}
local -a path
local all_path=$(
devlink resource show $dev \
| sed -E '# Of resource lines, keep only the name itself.
s/name ([^ ]*) .*/\1/
# Drop headers.
/:$/d
# First layer is not aligned enough, align it.
s/^/ /
# Use slashes as unary code for resource depth.
s, ,/,g
# Separate tally count from resource name.
s,/*,&\t,' \
| while read d name; do
while ((${#path[@]} > ${#d})); do
unset path[$((${#path[@]} - 1))]
done
path[$((${#d} - 1))]=$name
echo ${path[@]}
done \
| sed '# Convert paths to slash-separated
s,^,/,;s, ,/,g;s,$,/,'
)
COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "$all_path" -- "$cur" ) )
}
# Completion for devlink resource set
_devlink_resource_set()
{
case "$cword" in
3)
_devlink_direct_complete "dev"
return
;;
4)
COMPREPLY=( $( compgen -W "path" -- "$cur" ) )
return
;;
5)
_devlink_resource_path
return
;;
6)
COMPREPLY=( $( compgen -W "size" -- "$cur" ) )
return
;;
7)
# Integer argument
return
;;
esac
}
# Completion for devlink resource
_devlink_resource()
{
case $command in
show)
if [[ $cword -eq 3 ]]; then
_devlink_direct_complete "dev"
fi
return
;;
set)
_devlink_resource_set
return
;;
esac
}
# Completion for devlink region read
_devlink_region_read()
{
case "$cword" in
6)
COMPREPLY=( $( compgen -W "address" -- "$cur" ) )
return
;;
7)
# Address argument, for example: 0x10
return
;;
8)
COMPREPLY=( $( compgen -W "length" -- "$cur" ) )
return
;;
9)
# Integer argument
return
;;
esac
}
# Completion for devlink region
_devlink_region()
{
if [[ $cword -eq 3 && $command != "help" ]]; then
_devlink_direct_complete "region"
fi
case $command in
show)
return
;;
del|dump|read)
case "$cword" in
4)
COMPREPLY=( $( compgen -W "snapshot" -- "$cur" ) )
;;
5)
_devlink_direct_complete "snapshot"
;;
esac
if [[ $command == "read" ]]; then
_devlink_region_read
fi
return
;;
esac
}
# Completion reporter for devlink health
_devlink_health_reporter()
{
local i=$1; shift
case $cword in
$((3 + $i)))
_devlink_direct_complete "health_dev"
;;
$((4 + $i)))
COMPREPLY=( $( compgen -W "reporter" -- "$cur" ) )
;;
$((5 + $i)))
_devlink_direct_complete "reporter"
;;
esac
}
# Completion for devlink health
_devlink_health()
{
case $command in
show|recover|diagnose|set|test)
_devlink_health_reporter 0
if [[ $command == "set" ]]; then
case $cword in
6)
COMPREPLY=( $( compgen -W "grace_period auto_recover" \
-- "$cur" ) )
;;
7)
case $prev in
grace_period)
# Integer argument- msec
;;
auto_recover)
COMPREPLY=( $( compgen -W "true false" -- \
"$cur" ) )
;;
esac
esac
fi
return
;;
dump)
if [[ $cword -eq 3 ]]; then
COMPREPLY=( $( compgen -W "show clear" -- "$cur" ) )
fi
_devlink_health_reporter 1
return
;;
esac
}
# Completion for action in devlink trap set
_devlink_trap_set_action()
{
local i=$1; shift
case $cword in
$((6 + $i)))
COMPREPLY=( $( compgen -W "action" -- "$cur" ) )
;;
$((7 + $i)))
COMPREPLY=( $( compgen -W "trap drop mirror" -- "$cur" ) )
;;
esac
}
# Completion for devlink trap group set
_devlink_trap_group_set()
{
local -A settings=(
[action]=notseen
[policer]=notseen
[nopolicer]=notseen
)
if [[ $cword -eq 7 ]]; then
COMPREPLY=( $( compgen -W "action policer nopolicer" -- "$cur" ) )
fi
# Mark seen settings
local word
for word in "${words[@]:7:${#words[@]}-1}"; do
if [[ -n $word ]]; then
if [[ "${settings[$word]}" ]]; then
settings[$word]=seen
fi
fi
done
case $prev in
action)
COMPREPLY=( $( compgen -W "trap drop mirror" -- "$cur" ) )
return
;;
policer)
_devlink_direct_complete "trap_policer"
return
;;
esac
local -a comp_words=()
# Add settings not seen to completions
local setting
for setting in "${!settings[@]}"; do
if [ "${settings[$setting]}" = notseen ]; then
comp_words+=( "$setting" )
fi
done
COMPREPLY=( $( compgen -W "${comp_words[*]}" -- "$cur" ) )
}
# Completion for devlink trap group
_devlink_trap_group()
{
case $cword in
3)
COMPREPLY=( $( compgen -W "set show" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "dev"
return
;;
5)
COMPREPLY=( $( compgen -W "group" -- "$cur" ) )
return
;;
6)
_devlink_direct_complete "trap_group"
return
;;
esac
if [[ ${words[3]} == "set" ]]; then
_devlink_trap_group_set
fi
}
# Completion for devlink trap policer set
_devlink_trap_policer_set()
{
local -A settings=(
[rate]=notseen
[burst]=notseen
)
if [[ $cword -eq 7 ]]; then
COMPREPLY=( $( compgen -W "rate burst" -- "$cur" ) )
fi
# Mark seen settings
local word
for word in "${words[@]:7:${#words[@]}-1}"; do
if [[ -n $word ]]; then
if [[ "${settings[$word]}" ]]; then
settings[$word]=seen
fi
fi
done
case $prev in
rate)
# Integer argument
return
;;
burst)
# Integer argument
return
;;
esac
local -a comp_words=()
# Add settings not seen to completions
local setting
for setting in "${!settings[@]}"; do
if [ "${settings[$setting]}" = notseen ]; then
comp_words+=( "$setting" )
fi
done
COMPREPLY=( $( compgen -W "${comp_words[*]}" -- "$cur" ) )
}
# Completion for devlink trap policer
_devlink_trap_policer()
{
case $cword in
3)
COMPREPLY=( $( compgen -W "set show" -- "$cur" ) )
return
;;
4)
_devlink_direct_complete "dev"
return
;;
5)
COMPREPLY=( $( compgen -W "policer" -- "$cur" ) )
return
;;
6)
_devlink_direct_complete "trap_policer"
return
;;
esac
if [[ ${words[3]} == "set" ]]; then
_devlink_trap_policer_set
fi
}
# Completion for devlink trap
_devlink_trap()
{
case $command in
show|set)
case $cword in
3)
_devlink_direct_complete "dev"
;;
4)
COMPREPLY=( $( compgen -W "trap" -- "$cur" ) )
;;
5)
_devlink_direct_complete "trap"
;;
esac
if [[ $command == "set" ]]; then
_devlink_trap_set_action 0
fi
return
;;
group)
_devlink_trap_$command
return
;;
policer)
_devlink_trap_$command
return
;;
esac
}
# Complete any devlink command
_devlink()
{
local cur prev words cword
local opt='--Version --no-nice-names --json --pretty --verbose \
--statistics --force --Netns --batch'
local objects="$(devlink help 2>&1 | command sed -e '/OBJECT := /!d' \
-e 's/.*{//' -e 's/}.*//' -e \ 's/|//g' )"
_init_completion || return
# Gets the word-to-complete without considering the colon as word breaks
_get_comp_words_by_ref -n : cur prev words cword
if [[ $cword -eq 1 ]]; then
case $cur in
-*)
COMPREPLY=( $( compgen -W "$opt" -- "$cur" ) )
return 0
;;
*)
COMPREPLY=( $( compgen -W "$objects" -- "$cur" ) )
return 0
;;
esac
fi
# Deal with options
if [[ $prev == -* ]]; then
case $prev in
-V|--Version)
return 0
;;
-b|--batch)
_filedir
return 0
;;
--force)
COMPREPLY=( $( compgen -W "--batch" -- "$cur" ) )
return 0
;;
-N|--Netns)
local nslist=$( ip netns list 2>/dev/null )
COMPREPLY=( $( compgen -W "$nslist" -- "$cur" ) )
return 0
;;
-j|--json)
COMPREPLY=( $( compgen -W "--pretty $objects" -- "$cur" ) )
return 0
;;
*)
COMPREPLY=( $( compgen -W "$objects" -- "$cur" ) )
return 0
;;
esac
fi
# Remove all options so completions don't have to deal with them.
local i
for (( i=1; i < ${#words[@]}; )); do
if [[ ${words[i]::1} == - ]]; then
words=( "${words[@]:0:i}" "${words[@]:i+1}" )
[[ $i -le $cword ]] && cword=$(( cword - 1 ))
else
i=$(( ++i ))
fi
done
local object=${words[1]}
local command=${words[2]}
local pprev=${words[cword - 2]}
local prev=${words[cword - 1]}
if [[ $objects =~ $object ]]; then
if [[ $cword -eq 2 ]]; then
COMPREPLY=( $( compgen -W "help" -- "$cur") )
if [[ $object != "monitor" && $object != "dpipe" ]]; then
COMPREPLY+=( $( compgen -W \
"$(_devlink_get_optional_commands $object)" -- "$cur" ) )
fi
fi
"_devlink_$object"
fi
} &&
complete -F _devlink devlink
# ex: ts=4 sw=4 et filetype=sh
|