Subversion Repositories configs

Rev

Rev 208 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
192 - 1
# bpftool(8) bash completion                               -*- shell-script -*-
2
#
3
# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
4
# Copyright (C) 2017-2018 Netronome Systems, Inc.
5
#
6
# Author: Quentin Monnet <quentin.monnet@netronome.com>
7
 
8
# Takes a list of words in argument; each one of them is added to COMPREPLY if
9
# it is not already present on the command line. Returns no value.
10
_bpftool_once_attr()
11
{
12
    local w idx found
13
    for w in $*; do
14
        found=0
15
        for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
16
            if [[ $w == ${words[idx]} ]]; then
17
                found=1
18
                break
19
            fi
20
        done
21
        [[ $found -eq 0 ]] && \
22
            COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
23
    done
24
}
25
 
26
# Takes a list of words as argument; if any of those words is present on the
27
# command line, return 0. Otherwise, return 1.
28
_bpftool_search_list()
29
{
30
    local w idx
31
    for w in $*; do
32
        for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
33
            [[ $w == ${words[idx]} ]] && return 0
34
        done
35
    done
36
    return 1
37
}
38
 
39
# Takes a list of words in argument; adds them all to COMPREPLY if none of them
40
# is already present on the command line. Returns no value.
41
_bpftool_one_of_list()
42
{
43
    _bpftool_search_list $* && return 1
44
    COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
45
}
46
 
47
_bpftool_get_map_ids()
48
{
49
    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
50
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
51
}
52
 
53
# Takes map type and adds matching map ids to the list of suggestions.
54
_bpftool_get_map_ids_for_type()
55
{
56
    local type="$1"
57
    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
58
        command grep -C2 "$type" | \
59
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
60
}
61
 
62
_bpftool_get_map_names()
63
{
64
    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
65
        command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
66
}
67
 
68
# Takes map type and adds matching map names to the list of suggestions.
69
_bpftool_get_map_names_for_type()
70
{
71
    local type="$1"
72
    COMPREPLY+=( $( compgen -W "$( bpftool -jp map  2>&1 | \
73
        command grep -C2 "$type" | \
74
        command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
75
}
76
 
77
_bpftool_get_prog_ids()
78
{
79
    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
80
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
81
}
82
 
83
_bpftool_get_prog_tags()
84
{
85
    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
86
        command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
87
}
88
 
89
_bpftool_get_prog_names()
90
{
91
    COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
92
        command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) )
93
}
94
 
95
_bpftool_get_btf_ids()
96
{
97
    COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
98
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
99
}
100
 
101
_bpftool_get_link_ids()
102
{
103
    COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \
104
        command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
105
}
106
 
107
_bpftool_get_obj_map_names()
108
{
109
    local obj
110
 
111
    obj=$1
112
 
113
    maps=$(objdump -j maps -t $obj 2>/dev/null | \
114
        command awk '/g     . maps/ {print $NF}')
115
 
116
    COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
117
}
118
 
119
_bpftool_get_obj_map_idxs()
120
{
121
    local obj
122
 
123
    obj=$1
124
 
125
    nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g     . maps')
126
 
127
    COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
128
}
129
 
130
_sysfs_get_netdevs()
131
{
132
    COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
133
        "$cur" ) )
134
}
135
 
136
# Retrieve type of the map that we are operating on.
137
_bpftool_map_guess_map_type()
138
{
139
    local keyword ref
140
    for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
141
        case "${words[$((idx-2))]}" in
142
            lookup|update)
143
                keyword=${words[$((idx-1))]}
144
                ref=${words[$((idx))]}
145
                ;;
146
            push)
147
                printf "stack"
148
                return 0
149
                ;;
150
            enqueue)
151
                printf "queue"
152
                return 0
153
                ;;
154
        esac
155
    done
156
    [[ -z $ref ]] && return 0
157
 
158
    local type
159
    type=$(bpftool -jp map show $keyword $ref | \
160
        command sed -n 's/.*"type": "\(.*\)",$/\1/p')
161
    [[ -n $type ]] && printf $type
162
}
163
 
164
_bpftool_map_update_get_id()
165
{
166
    local command="$1"
167
 
168
    # Is it the map to update, or a map to insert into the map to update?
169
    # Search for "value" keyword.
170
    local idx value
171
    for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
172
        if [[ ${words[idx]} == "value" ]]; then
173
            value=1
174
            break
175
        fi
176
    done
177
    if [[ $value -eq 0 ]]; then
178
        case "$command" in
179
            push)
180
                _bpftool_get_map_ids_for_type stack
181
                ;;
182
            enqueue)
183
                _bpftool_get_map_ids_for_type queue
184
                ;;
185
            *)
186
                _bpftool_get_map_ids
187
                ;;
188
        esac
189
        return 0
190
    fi
191
 
192
    # Id to complete is for a value. It can be either prog id or map id. This
193
    # depends on the type of the map to update.
194
    local type=$(_bpftool_map_guess_map_type)
195
    case $type in
196
        array_of_maps|hash_of_maps)
197
            _bpftool_get_map_ids
198
            return 0
199
            ;;
200
        prog_array)
201
            _bpftool_get_prog_ids
202
            return 0
203
            ;;
204
        *)
205
            return 0
206
            ;;
207
    esac
208
}
209
 
210
_bpftool_map_update_get_name()
211
{
212
    local command="$1"
213
 
214
    # Is it the map to update, or a map to insert into the map to update?
215
    # Search for "value" keyword.
216
    local idx value
217
    for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
218
        if [[ ${words[idx]} == "value" ]]; then
219
            value=1
220
            break
221
        fi
222
    done
223
    if [[ $value -eq 0 ]]; then
224
        case "$command" in
225
            push)
226
                _bpftool_get_map_names_for_type stack
227
                ;;
228
            enqueue)
229
                _bpftool_get_map_names_for_type queue
230
                ;;
231
            *)
232
                _bpftool_get_map_names
233
                ;;
234
        esac
235
        return 0
236
    fi
237
 
238
    # Name to complete is for a value. It can be either prog name or map name. This
239
    # depends on the type of the map to update.
240
    local type=$(_bpftool_map_guess_map_type)
241
    case $type in
242
        array_of_maps|hash_of_maps)
243
            _bpftool_get_map_names
244
            return 0
245
            ;;
246
        prog_array)
247
            _bpftool_get_prog_names
248
            return 0
249
            ;;
250
        *)
251
            return 0
252
            ;;
253
    esac
254
}
255
 
256
_bpftool()
257
{
258
    local cur prev words objword
259
    _init_completion || return
260
 
261
    # Deal with options
262
    if [[ ${words[cword]} == -* ]]; then
209 - 263
        local c='--version --json --pretty --bpffs --mapcompat --debug \
264
	       --use-loader --base-btf'
192 - 265
        COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
266
        return 0
267
    fi
268
 
269
    # Deal with simplest keywords
270
    case $prev in
271
        help|hex|opcodes|visual|linum)
272
            return 0
273
            ;;
274
        tag)
275
            _bpftool_get_prog_tags
276
            return 0
277
            ;;
278
        dev)
279
            _sysfs_get_netdevs
280
            return 0
281
            ;;
209 - 282
        file|pinned|-B|--base-btf)
192 - 283
            _filedir
284
            return 0
285
            ;;
286
        batch)
287
            COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
288
            return 0
289
            ;;
290
    esac
291
 
292
    # Remove all options so completions don't have to deal with them.
293
    local i
294
    for (( i=1; i < ${#words[@]}; )); do
209 - 295
        if [[ ${words[i]::1} == - ]] &&
296
            [[ ${words[i]} != "-B" ]] && [[ ${words[i]} != "--base-btf" ]]; then
192 - 297
            words=( "${words[@]:0:i}" "${words[@]:i+1}" )
298
            [[ $i -le $cword ]] && cword=$(( cword - 1 ))
299
        else
300
            i=$(( ++i ))
301
        fi
302
    done
303
    cur=${words[cword]}
304
    prev=${words[cword - 1]}
305
    pprev=${words[cword - 2]}
306
 
307
    local object=${words[1]} command=${words[2]}
308
 
309
    if [[ -z $object || $cword -eq 1 ]]; then
310
        case $cur in
311
            *)
312
                COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
313
                    command sed \
314
                    -e '/OBJECT := /!d' \
315
                    -e 's/.*{//' \
316
                    -e 's/}.*//' \
317
                    -e 's/|//g' )" -- "$cur" ) )
318
                COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
319
                return 0
320
                ;;
321
        esac
322
    fi
323
 
324
    [[ $command == help ]] && return 0
325
 
326
    # Completion depends on object and command in use
327
    case $object in
328
        prog)
329
            # Complete id and name, only for subcommands that use prog (but no
330
            # map) ids/names.
331
            case $command in
332
                show|list|dump|pin)
333
                    case $prev in
334
                        id)
335
                            _bpftool_get_prog_ids
336
                            return 0
337
                            ;;
338
                        name)
339
                            _bpftool_get_prog_names
340
                            return 0
341
                            ;;
342
                    esac
343
                    ;;
344
            esac
345
 
346
            local PROG_TYPE='id pinned tag name'
347
            local MAP_TYPE='id pinned name'
348
            local METRIC_TYPE='cycles instructions l1d_loads llc_misses'
349
            case $command in
350
                show|list)
351
                    [[ $prev != "$command" ]] && return 0
352
                    COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
353
                    return 0
354
                    ;;
355
                dump)
356
                    case $prev in
357
                        $command)
358
                            COMPREPLY+=( $( compgen -W "xlated jited" -- \
359
                                "$cur" ) )
360
                            return 0
361
                            ;;
362
                        xlated|jited)
363
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
364
                                "$cur" ) )
365
                            return 0
366
                            ;;
367
                        *)
368
                            _bpftool_once_attr 'file'
369
                            if _bpftool_search_list 'xlated'; then
370
                                COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
371
                                    "$cur" ) )
372
                            else
373
                                COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
374
                                    "$cur" ) )
375
                            fi
376
                            return 0
377
                            ;;
378
                    esac
379
                    ;;
380
                pin)
381
                    if [[ $prev == "$command" ]]; then
382
                        COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
383
                    else
384
                        _filedir
385
                    fi
386
                    return 0
387
                    ;;
388
                attach|detach)
389
                    case $cword in
390
                        3)
391
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
392
                            return 0
393
                            ;;
394
                        4)
395
                            case $prev in
396
                                id)
397
                                    _bpftool_get_prog_ids
398
                                    ;;
399
                                name)
400
                                    _bpftool_get_prog_names
401
                                    ;;
402
                                pinned)
403
                                    _filedir
404
                                    ;;
405
                            esac
406
                            return 0
407
                            ;;
408
                        5)
409
                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
410
                                stream_parser flow_dissector' -- "$cur" ) )
411
                            return 0
412
                            ;;
413
                        6)
414
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
415
                            return 0
416
                            ;;
417
                        7)
418
                            case $prev in
419
                                id)
420
                                    _bpftool_get_map_ids
421
                                    ;;
422
                                name)
423
                                    _bpftool_get_map_names
424
                                    ;;
425
                                pinned)
426
                                    _filedir
427
                                    ;;
428
                            esac
429
                            return 0
430
                            ;;
431
                    esac
432
                    ;;
433
                load|loadall)
434
                    local obj
435
 
436
                    # Propose "load/loadall" to complete "bpftool prog load",
437
                    # or bash tries to complete "load" as a filename below.
438
                    if [[ ${#words[@]} -eq 3 ]]; then
439
                        COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )
440
                        return 0
441
                    fi
442
 
443
                    if [[ ${#words[@]} -lt 6 ]]; then
444
                        _filedir
445
                        return 0
446
                    fi
447
 
448
                    obj=${words[3]}
449
 
450
                    if [[ ${words[-4]} == "map" ]]; then
451
                        COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
452
                        return 0
453
                    fi
454
                    if [[ ${words[-3]} == "map" ]]; then
455
                        if [[ ${words[-2]} == "idx" ]]; then
456
                            _bpftool_get_obj_map_idxs $obj
457
                        elif [[ ${words[-2]} == "name" ]]; then
458
                            _bpftool_get_obj_map_names $obj
459
                        fi
460
                        return 0
461
                    fi
462
                    if [[ ${words[-2]} == "map" ]]; then
463
                        COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
464
                        return 0
465
                    fi
466
 
467
                    case $prev in
468
                        type)
469
                            COMPREPLY=( $( compgen -W "socket kprobe \
470
                                kretprobe classifier flow_dissector \
471
                                action tracepoint raw_tracepoint \
472
                                xdp perf_event cgroup/skb cgroup/sock \
473
                                cgroup/dev lwt_in lwt_out lwt_xmit \
474
                                lwt_seg6local sockops sk_skb sk_msg \
475
                                lirc_mode2 cgroup/bind4 cgroup/bind6 \
476
                                cgroup/connect4 cgroup/connect6 \
477
                                cgroup/getpeername4 cgroup/getpeername6 \
478
                                cgroup/getsockname4 cgroup/getsockname6 \
479
                                cgroup/sendmsg4 cgroup/sendmsg6 \
480
                                cgroup/recvmsg4 cgroup/recvmsg6 \
481
                                cgroup/post_bind4 cgroup/post_bind6 \
482
                                cgroup/sysctl cgroup/getsockopt \
208 - 483
                                cgroup/setsockopt cgroup/sock_release struct_ops \
200 - 484
                                fentry fexit freplace sk_lookup" -- \
192 - 485
                                                   "$cur" ) )
486
                            return 0
487
                            ;;
488
                        id)
489
                            _bpftool_get_map_ids
490
                            return 0
491
                            ;;
492
                        name)
493
                            _bpftool_get_map_names
494
                            return 0
495
                            ;;
496
                        pinned|pinmaps)
497
                            _filedir
498
                            return 0
499
                            ;;
500
                        *)
501
                            COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
502
                            _bpftool_once_attr 'type'
503
                            _bpftool_once_attr 'dev'
504
                            _bpftool_once_attr 'pinmaps'
505
                            return 0
506
                            ;;
507
                    esac
508
                    ;;
509
                tracelog)
510
                    return 0
511
                    ;;
512
                profile)
513
                    case $cword in
514
                        3)
515
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
516
                            return 0
517
                            ;;
518
                        4)
519
                            case $prev in
520
                                id)
521
                                    _bpftool_get_prog_ids
522
                                    ;;
523
                                name)
524
                                    _bpftool_get_prog_names
525
                                    ;;
526
                                pinned)
527
                                    _filedir
528
                                    ;;
529
                            esac
530
                            return 0
531
                            ;;
532
                        5)
533
                            COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
534
                            return 0
535
                            ;;
536
                        6)
537
                            case $prev in
538
                                duration)
539
                                    return 0
540
                                    ;;
541
                                *)
542
                                    COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
543
                                    return 0
544
                                    ;;
545
                            esac
546
                            return 0
547
                            ;;
548
                        *)
549
                            COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
550
                            return 0
551
                            ;;
552
                    esac
553
                    ;;
554
                run)
555
                    if [[ ${#words[@]} -eq 4 ]]; then
556
                        COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
557
                        return 0
558
                    fi
559
                    case $prev in
560
                        id)
561
                            _bpftool_get_prog_ids
562
                            return 0
563
                            ;;
564
                        name)
565
                            _bpftool_get_prog_names
566
                            return 0
567
                            ;;
568
                        data_in|data_out|ctx_in|ctx_out)
569
                            _filedir
570
                            return 0
571
                            ;;
572
                        repeat|data_size_out|ctx_size_out)
573
                            return 0
574
                            ;;
575
                        *)
576
                            _bpftool_once_attr 'data_in data_out data_size_out \
577
                                ctx_in ctx_out ctx_size_out repeat'
578
                            return 0
579
                            ;;
580
                    esac
581
                    ;;
582
                *)
583
                    [[ $prev == $object ]] && \
584
                        COMPREPLY=( $( compgen -W 'dump help pin attach detach \
585
                            load loadall show list tracelog run profile' -- "$cur" ) )
586
                    ;;
587
            esac
588
            ;;
589
        struct_ops)
590
            local STRUCT_OPS_TYPE='id name'
591
            case $command in
592
                show|list|dump|unregister)
593
                    case $prev in
594
                        $command)
595
                            COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
596
                            ;;
597
                        id)
598
                            _bpftool_get_map_ids_for_type struct_ops
599
                            ;;
600
                        name)
601
                            _bpftool_get_map_names_for_type struct_ops
602
                            ;;
603
                    esac
604
                    return 0
605
                    ;;
606
                register)
607
                    _filedir
608
                    return 0
609
                    ;;
610
                *)
611
                    [[ $prev == $object ]] && \
612
                        COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
613
                            -- "$cur" ) )
614
                    ;;
615
            esac
616
            ;;
617
        iter)
618
            case $command in
619
                pin)
200 - 620
                    case $prev in
621
                        $command)
622
                            _filedir
623
                            ;;
624
                        id)
625
                            _bpftool_get_map_ids
626
                            ;;
627
                        name)
628
                            _bpftool_get_map_names
629
                            ;;
630
                        pinned)
631
                            _filedir
632
                            ;;
633
                        *)
634
                            _bpftool_one_of_list $MAP_TYPE
635
                            ;;
636
                    esac
192 - 637
                    return 0
638
                    ;;
639
                *)
640
                    [[ $prev == $object ]] && \
641
                        COMPREPLY=( $( compgen -W 'pin help' \
642
                            -- "$cur" ) )
643
                    ;;
644
            esac
645
            ;;
646
        map)
647
            local MAP_TYPE='id pinned name'
648
            case $command in
649
                show|list|dump|peek|pop|dequeue|freeze)
650
                    case $prev in
651
                        $command)
652
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
653
                            return 0
654
                            ;;
655
                        id)
656
                            case "$command" in
657
                                peek)
658
                                    _bpftool_get_map_ids_for_type stack
659
                                    _bpftool_get_map_ids_for_type queue
660
                                    ;;
661
                                pop)
662
                                    _bpftool_get_map_ids_for_type stack
663
                                    ;;
664
                                dequeue)
665
                                    _bpftool_get_map_ids_for_type queue
666
                                    ;;
667
                                *)
668
                                    _bpftool_get_map_ids
669
                                    ;;
670
                            esac
671
                            return 0
672
                            ;;
673
                        name)
674
                            case "$command" in
675
                                peek)
676
                                    _bpftool_get_map_names_for_type stack
677
                                    _bpftool_get_map_names_for_type queue
678
                                    ;;
679
                                pop)
680
                                    _bpftool_get_map_names_for_type stack
681
                                    ;;
682
                                dequeue)
683
                                    _bpftool_get_map_names_for_type queue
684
                                    ;;
685
                                *)
686
                                    _bpftool_get_map_names
687
                                    ;;
688
                            esac
689
                            return 0
690
                            ;;
691
                        *)
692
                            return 0
693
                            ;;
694
                    esac
695
                    ;;
696
                create)
697
                    case $prev in
698
                        $command)
699
                            _filedir
700
                            return 0
701
                            ;;
702
                        type)
703
                            COMPREPLY=( $( compgen -W 'hash array prog_array \
704
                                perf_event_array percpu_hash percpu_array \
705
                                stack_trace cgroup_array lru_hash \
706
                                lru_percpu_hash lpm_trie array_of_maps \
707
                                hash_of_maps devmap devmap_hash sockmap cpumap \
708
                                xskmap sockhash cgroup_storage reuseport_sockarray \
203 - 709
                                percpu_cgroup_storage queue stack sk_storage \
710
                                struct_ops inode_storage task_storage' -- \
192 - 711
                                                   "$cur" ) )
712
                            return 0
713
                            ;;
203 - 714
                        key|value|flags|entries)
192 - 715
                            return 0
716
                            ;;
203 - 717
                        inner_map)
718
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
719
                            return 0
720
                            ;;
721
                        id)
722
                            _bpftool_get_map_ids
723
                            ;;
724
                        name)
725
                            case $pprev in
726
                                inner_map)
727
                                    _bpftool_get_map_names
728
                                    ;;
729
                                *)
730
                                    return 0
731
                                    ;;
732
                            esac
733
                            ;;
192 - 734
                        *)
735
                            _bpftool_once_attr 'type'
736
                            _bpftool_once_attr 'key'
737
                            _bpftool_once_attr 'value'
738
                            _bpftool_once_attr 'entries'
739
                            _bpftool_once_attr 'name'
740
                            _bpftool_once_attr 'flags'
203 - 741
                            if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then
742
                                _bpftool_once_attr 'inner_map'
743
                            fi
192 - 744
                            _bpftool_once_attr 'dev'
745
                            return 0
746
                            ;;
747
                    esac
748
                    ;;
749
                lookup|getnext|delete)
750
                    case $prev in
751
                        $command)
752
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
753
                            return 0
754
                            ;;
755
                        id)
756
                            _bpftool_get_map_ids
757
                            return 0
758
                            ;;
759
                        name)
760
                            _bpftool_get_map_names
761
                            return 0
762
                            ;;
763
                        key)
764
                            COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
765
                            ;;
766
                        *)
767
                            case $(_bpftool_map_guess_map_type) in
768
                                queue|stack)
769
                                    return 0
770
                                    ;;
771
                            esac
772
 
773
                            _bpftool_once_attr 'key'
774
                            return 0
775
                            ;;
776
                    esac
777
                    ;;
778
                update|push|enqueue)
779
                    case $prev in
780
                        $command)
781
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
782
                            return 0
783
                            ;;
784
                        id)
785
                            _bpftool_map_update_get_id $command
786
                            return 0
787
                            ;;
788
                        name)
789
                            _bpftool_map_update_get_name $command
790
                            return 0
791
                            ;;
792
                        key)
793
                            COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
794
                            ;;
795
                        value)
796
                            # We can have bytes, or references to a prog or a
797
                            # map, depending on the type of the map to update.
798
                            case "$(_bpftool_map_guess_map_type)" in
799
                                array_of_maps|hash_of_maps)
800
                                    local MAP_TYPE='id pinned name'
801
                                    COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
802
                                        -- "$cur" ) )
803
                                    return 0
804
                                    ;;
805
                                prog_array)
806
                                    local PROG_TYPE='id pinned tag name'
807
                                    COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
808
                                        -- "$cur" ) )
809
                                    return 0
810
                                    ;;
811
                                *)
812
                                    COMPREPLY+=( $( compgen -W 'hex' \
813
                                        -- "$cur" ) )
814
                                    return 0
815
                                    ;;
816
                            esac
817
                            return 0
818
                            ;;
819
                        *)
820
                            case $(_bpftool_map_guess_map_type) in
821
                                queue|stack)
822
                                    _bpftool_once_attr 'value'
823
                                    return 0;
824
                                    ;;
825
                            esac
826
 
827
                            _bpftool_once_attr 'key'
828
                            local UPDATE_FLAGS='any exist noexist'
829
                            for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
830
                                if [[ ${words[idx]} == 'value' ]]; then
831
                                    # 'value' is present, but is not the last
832
                                    # word i.e. we can now have UPDATE_FLAGS.
833
                                    _bpftool_one_of_list "$UPDATE_FLAGS"
834
                                    return 0
835
                                fi
836
                            done
837
                            for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
838
                                if [[ ${words[idx]} == 'key' ]]; then
839
                                    # 'key' is present, but is not the last
840
                                    # word i.e. we can now have 'value'.
841
                                    _bpftool_once_attr 'value'
842
                                    return 0
843
                                fi
844
                            done
845
 
846
                            return 0
847
                            ;;
848
                    esac
849
                    ;;
850
                pin)
851
                    case $prev in
852
                        $command)
853
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
854
                            ;;
855
                        id)
856
                            _bpftool_get_map_ids
857
                            ;;
858
                        name)
859
                            _bpftool_get_map_names
860
                            ;;
861
                    esac
862
                    return 0
863
                    ;;
864
                event_pipe)
865
                    case $prev in
866
                        $command)
867
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
868
                            return 0
869
                            ;;
870
                        id)
871
                            _bpftool_get_map_ids_for_type perf_event_array
872
                            return 0
873
                            ;;
874
                        name)
875
                            _bpftool_get_map_names_for_type perf_event_array
876
                            return 0
877
                            ;;
878
                        cpu)
879
                            return 0
880
                            ;;
881
                        index)
882
                            return 0
883
                            ;;
884
                        *)
885
                            _bpftool_once_attr 'cpu'
886
                            _bpftool_once_attr 'index'
887
                            return 0
888
                            ;;
889
                    esac
890
                    ;;
891
                *)
892
                    [[ $prev == $object ]] && \
893
                        COMPREPLY=( $( compgen -W 'delete dump getnext help \
894
                            lookup pin event_pipe show list update create \
895
                            peek push enqueue pop dequeue freeze' -- \
896
                            "$cur" ) )
897
                    ;;
898
            esac
899
            ;;
900
        btf)
901
            local PROG_TYPE='id pinned tag name'
902
            local MAP_TYPE='id pinned name'
903
            case $command in
904
                dump)
905
                    case $prev in
906
                        $command)
907
                            COMPREPLY+=( $( compgen -W "id map prog file" -- \
908
                                "$cur" ) )
909
                            return 0
910
                            ;;
911
                        prog)
912
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
913
                            return 0
914
                            ;;
915
                        map)
916
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
917
                            return 0
918
                            ;;
919
                        id)
920
                            case $pprev in
921
                                prog)
922
                                    _bpftool_get_prog_ids
923
                                    ;;
924
                                map)
925
                                    _bpftool_get_map_ids
926
                                    ;;
927
                                $command)
928
                                    _bpftool_get_btf_ids
929
                                    ;;
930
                            esac
931
                            return 0
932
                            ;;
933
                        name)
934
                            case $pprev in
935
                                prog)
936
                                    _bpftool_get_prog_names
937
                                    ;;
938
                                map)
939
                                    _bpftool_get_map_names
940
                                    ;;
941
                            esac
942
                            return 0
943
                            ;;
944
                        format)
945
                            COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
946
                            ;;
947
                        *)
948
                            # emit extra options
949
                            case ${words[3]} in
950
                                id|file)
951
                                    _bpftool_once_attr 'format'
952
                                    ;;
953
                                map|prog)
954
                                    if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
955
                                        COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
956
                                    fi
957
                                    _bpftool_once_attr 'format'
958
                                    ;;
959
                                *)
960
                                    ;;
961
                            esac
962
                            return 0
963
                            ;;
964
                    esac
965
                    ;;
966
                show|list)
967
                    case $prev in
968
                        $command)
969
                            COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
970
                            ;;
971
                        id)
972
                            _bpftool_get_btf_ids
973
                            ;;
974
                    esac
975
                    return 0
976
                    ;;
977
                *)
978
                    [[ $prev == $object ]] && \
979
                        COMPREPLY=( $( compgen -W 'dump help show list' \
980
                            -- "$cur" ) )
981
                    ;;
982
            esac
983
            ;;
984
        gen)
985
            case $command in
208 - 986
                object)
192 - 987
                    _filedir
208 - 988
                    return 0
192 - 989
                    ;;
208 - 990
                skeleton)
991
                    case $prev in
992
                        $command)
993
                            _filedir
994
                            return 0
995
                            ;;
996
                        *)
997
                            _bpftool_once_attr 'name'
998
                            return 0
999
                            ;;
1000
                    esac
1001
                    ;;
192 - 1002
                *)
1003
                    [[ $prev == $object ]] && \
208 - 1004
                        COMPREPLY=( $( compgen -W 'object skeleton help' -- "$cur" ) )
192 - 1005
                    ;;
1006
            esac
1007
            ;;
1008
        cgroup)
1009
            case $command in
1010
                show|list|tree)
1011
                    case $cword in
1012
                        3)
1013
                            _filedir
1014
                            ;;
1015
                        4)
1016
                            COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
1017
                            ;;
1018
                    esac
1019
                    return 0
1020
                    ;;
1021
                attach|detach)
1022
                    local ATTACH_TYPES='ingress egress sock_create sock_ops \
1023
                        device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
1024
                        getpeername4 getpeername6 getsockname4 getsockname6 \
1025
                        sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
208 - 1026
                        setsockopt sock_release'
192 - 1027
                    local ATTACH_FLAGS='multi override'
1028
                    local PROG_TYPE='id pinned tag name'
1029
                    case $prev in
1030
                        $command)
1031
                            _filedir
1032
                            return 0
1033
                            ;;
1034
                        ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
1035
                        post_bind4|post_bind6|connect4|connect6|getpeername4|\
1036
                        getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
208 - 1037
                        recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt|sock_release)
192 - 1038
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
1039
                                "$cur" ) )
1040
                            return 0
1041
                            ;;
1042
                        id)
1043
                            _bpftool_get_prog_ids
1044
                            return 0
1045
                            ;;
1046
                        *)
1047
                            if ! _bpftool_search_list "$ATTACH_TYPES"; then
1048
                                COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
1049
                                    "$cur" ) )
1050
                            elif [[ "$command" == "attach" ]]; then
1051
                                # We have an attach type on the command line,
1052
                                # but it is not the previous word, or
1053
                                # "id|pinned|tag|name" (we already checked for
1054
                                # that). This should only leave the case when
1055
                                # we need attach flags for "attach" commamnd.
1056
                                _bpftool_one_of_list "$ATTACH_FLAGS"
1057
                            fi
1058
                            return 0
1059
                            ;;
1060
                    esac
1061
                    ;;
1062
                *)
1063
                    [[ $prev == $object ]] && \
1064
                        COMPREPLY=( $( compgen -W 'help attach detach \
1065
                            show list tree' -- "$cur" ) )
1066
                    ;;
1067
            esac
1068
            ;;
1069
        perf)
1070
            case $command in
1071
                *)
1072
                    [[ $prev == $object ]] && \
1073
                        COMPREPLY=( $( compgen -W 'help \
1074
                            show list' -- "$cur" ) )
1075
                    ;;
1076
            esac
1077
            ;;
1078
        net)
1079
            local PROG_TYPE='id pinned tag name'
1080
            local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
1081
            case $command in
1082
                show|list)
1083
                    [[ $prev != "$command" ]] && return 0
1084
                    COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1085
                    return 0
1086
                    ;;
1087
                attach)
1088
                    case $cword in
1089
                        3)
1090
                            COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1091
                            return 0
1092
                            ;;
1093
                        4)
1094
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1095
                            return 0
1096
                            ;;
1097
                        5)
1098
                            case $prev in
1099
                                id)
1100
                                    _bpftool_get_prog_ids
1101
                                    ;;
1102
                                name)
1103
                                    _bpftool_get_prog_names
1104
                                    ;;
1105
                                pinned)
1106
                                    _filedir
1107
                                    ;;
1108
                            esac
1109
                            return 0
1110
                            ;;
1111
                        6)
1112
                            COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1113
                            return 0
1114
                            ;;
1115
                        8)
1116
                            _bpftool_once_attr 'overwrite'
1117
                            return 0
1118
                            ;;
1119
                    esac
1120
                    ;;
1121
                detach)
1122
                    case $cword in
1123
                        3)
1124
                            COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1125
                            return 0
1126
                            ;;
1127
                        4)
1128
                            COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1129
                            return 0
1130
                            ;;
1131
                    esac
1132
                    ;;
1133
                *)
1134
                    [[ $prev == $object ]] && \
1135
                        COMPREPLY=( $( compgen -W 'help \
1136
                            show list attach detach' -- "$cur" ) )
1137
                    ;;
1138
            esac
1139
            ;;
1140
        feature)
1141
            case $command in
1142
                probe)
1143
                    [[ $prev == "prefix" ]] && return 0
1144
                    if _bpftool_search_list 'macros'; then
1145
                        _bpftool_once_attr 'prefix'
1146
                    else
1147
                        COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1148
                    fi
1149
                    _bpftool_one_of_list 'kernel dev'
1150
                    _bpftool_once_attr 'full unprivileged'
1151
                    return 0
1152
                    ;;
1153
                *)
1154
                    [[ $prev == $object ]] && \
1155
                        COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
1156
                    ;;
1157
            esac
1158
            ;;
1159
        link)
1160
            case $command in
200 - 1161
                show|list|pin|detach)
192 - 1162
                    case $prev in
1163
                        id)
1164
                            _bpftool_get_link_ids
1165
                            return 0
1166
                            ;;
1167
                    esac
1168
                    ;;
1169
            esac
1170
 
1171
            local LINK_TYPE='id pinned'
1172
            case $command in
1173
                show|list)
1174
                    [[ $prev != "$command" ]] && return 0
1175
                    COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1176
                    return 0
1177
                    ;;
200 - 1178
                pin|detach)
192 - 1179
                    if [[ $prev == "$command" ]]; then
1180
                        COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1181
                    else
1182
                        _filedir
1183
                    fi
1184
                    return 0
1185
                    ;;
1186
                *)
1187
                    [[ $prev == $object ]] && \
1188
                        COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
1189
                    ;;
1190
            esac
1191
            ;;
1192
    esac
1193
} &&
1194
complete -F _bpftool bpftool
1195
 
1196
# ex: ts=4 sw=4 et filetype=sh