Subversion Repositories configs

Rev

Rev 200 | Go to most recent revision | Details | 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
263
        local c='--version --json --pretty --bpffs --mapcompat --debug'
264
        COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
265
        return 0
266
    fi
267
 
268
    # Deal with simplest keywords
269
    case $prev in
270
        help|hex|opcodes|visual|linum)
271
            return 0
272
            ;;
273
        tag)
274
            _bpftool_get_prog_tags
275
            return 0
276
            ;;
277
        dev)
278
            _sysfs_get_netdevs
279
            return 0
280
            ;;
281
        file|pinned)
282
            _filedir
283
            return 0
284
            ;;
285
        batch)
286
            COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
287
            return 0
288
            ;;
289
    esac
290
 
291
    # Remove all options so completions don't have to deal with them.
292
    local i
293
    for (( i=1; i < ${#words[@]}; )); do
294
        if [[ ${words[i]::1} == - ]]; then
295
            words=( "${words[@]:0:i}" "${words[@]:i+1}" )
296
            [[ $i -le $cword ]] && cword=$(( cword - 1 ))
297
        else
298
            i=$(( ++i ))
299
        fi
300
    done
301
    cur=${words[cword]}
302
    prev=${words[cword - 1]}
303
    pprev=${words[cword - 2]}
304
 
305
    local object=${words[1]} command=${words[2]}
306
 
307
    if [[ -z $object || $cword -eq 1 ]]; then
308
        case $cur in
309
            *)
310
                COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
311
                    command sed \
312
                    -e '/OBJECT := /!d' \
313
                    -e 's/.*{//' \
314
                    -e 's/}.*//' \
315
                    -e 's/|//g' )" -- "$cur" ) )
316
                COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
317
                return 0
318
                ;;
319
        esac
320
    fi
321
 
322
    [[ $command == help ]] && return 0
323
 
324
    # Completion depends on object and command in use
325
    case $object in
326
        prog)
327
            # Complete id and name, only for subcommands that use prog (but no
328
            # map) ids/names.
329
            case $command in
330
                show|list|dump|pin)
331
                    case $prev in
332
                        id)
333
                            _bpftool_get_prog_ids
334
                            return 0
335
                            ;;
336
                        name)
337
                            _bpftool_get_prog_names
338
                            return 0
339
                            ;;
340
                    esac
341
                    ;;
342
            esac
343
 
344
            local PROG_TYPE='id pinned tag name'
345
            local MAP_TYPE='id pinned name'
346
            local METRIC_TYPE='cycles instructions l1d_loads llc_misses'
347
            case $command in
348
                show|list)
349
                    [[ $prev != "$command" ]] && return 0
350
                    COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
351
                    return 0
352
                    ;;
353
                dump)
354
                    case $prev in
355
                        $command)
356
                            COMPREPLY+=( $( compgen -W "xlated jited" -- \
357
                                "$cur" ) )
358
                            return 0
359
                            ;;
360
                        xlated|jited)
361
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
362
                                "$cur" ) )
363
                            return 0
364
                            ;;
365
                        *)
366
                            _bpftool_once_attr 'file'
367
                            if _bpftool_search_list 'xlated'; then
368
                                COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
369
                                    "$cur" ) )
370
                            else
371
                                COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
372
                                    "$cur" ) )
373
                            fi
374
                            return 0
375
                            ;;
376
                    esac
377
                    ;;
378
                pin)
379
                    if [[ $prev == "$command" ]]; then
380
                        COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
381
                    else
382
                        _filedir
383
                    fi
384
                    return 0
385
                    ;;
386
                attach|detach)
387
                    case $cword in
388
                        3)
389
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
390
                            return 0
391
                            ;;
392
                        4)
393
                            case $prev in
394
                                id)
395
                                    _bpftool_get_prog_ids
396
                                    ;;
397
                                name)
398
                                    _bpftool_get_prog_names
399
                                    ;;
400
                                pinned)
401
                                    _filedir
402
                                    ;;
403
                            esac
404
                            return 0
405
                            ;;
406
                        5)
407
                            COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
408
                                stream_parser flow_dissector' -- "$cur" ) )
409
                            return 0
410
                            ;;
411
                        6)
412
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
413
                            return 0
414
                            ;;
415
                        7)
416
                            case $prev in
417
                                id)
418
                                    _bpftool_get_map_ids
419
                                    ;;
420
                                name)
421
                                    _bpftool_get_map_names
422
                                    ;;
423
                                pinned)
424
                                    _filedir
425
                                    ;;
426
                            esac
427
                            return 0
428
                            ;;
429
                    esac
430
                    ;;
431
                load|loadall)
432
                    local obj
433
 
434
                    # Propose "load/loadall" to complete "bpftool prog load",
435
                    # or bash tries to complete "load" as a filename below.
436
                    if [[ ${#words[@]} -eq 3 ]]; then
437
                        COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )
438
                        return 0
439
                    fi
440
 
441
                    if [[ ${#words[@]} -lt 6 ]]; then
442
                        _filedir
443
                        return 0
444
                    fi
445
 
446
                    obj=${words[3]}
447
 
448
                    if [[ ${words[-4]} == "map" ]]; then
449
                        COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
450
                        return 0
451
                    fi
452
                    if [[ ${words[-3]} == "map" ]]; then
453
                        if [[ ${words[-2]} == "idx" ]]; then
454
                            _bpftool_get_obj_map_idxs $obj
455
                        elif [[ ${words[-2]} == "name" ]]; then
456
                            _bpftool_get_obj_map_names $obj
457
                        fi
458
                        return 0
459
                    fi
460
                    if [[ ${words[-2]} == "map" ]]; then
461
                        COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
462
                        return 0
463
                    fi
464
 
465
                    case $prev in
466
                        type)
467
                            COMPREPLY=( $( compgen -W "socket kprobe \
468
                                kretprobe classifier flow_dissector \
469
                                action tracepoint raw_tracepoint \
470
                                xdp perf_event cgroup/skb cgroup/sock \
471
                                cgroup/dev lwt_in lwt_out lwt_xmit \
472
                                lwt_seg6local sockops sk_skb sk_msg \
473
                                lirc_mode2 cgroup/bind4 cgroup/bind6 \
474
                                cgroup/connect4 cgroup/connect6 \
475
                                cgroup/getpeername4 cgroup/getpeername6 \
476
                                cgroup/getsockname4 cgroup/getsockname6 \
477
                                cgroup/sendmsg4 cgroup/sendmsg6 \
478
                                cgroup/recvmsg4 cgroup/recvmsg6 \
479
                                cgroup/post_bind4 cgroup/post_bind6 \
480
                                cgroup/sysctl cgroup/getsockopt \
481
                                cgroup/setsockopt struct_ops \
482
                                fentry fexit freplace" -- \
483
                                                   "$cur" ) )
484
                            return 0
485
                            ;;
486
                        id)
487
                            _bpftool_get_map_ids
488
                            return 0
489
                            ;;
490
                        name)
491
                            _bpftool_get_map_names
492
                            return 0
493
                            ;;
494
                        pinned|pinmaps)
495
                            _filedir
496
                            return 0
497
                            ;;
498
                        *)
499
                            COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
500
                            _bpftool_once_attr 'type'
501
                            _bpftool_once_attr 'dev'
502
                            _bpftool_once_attr 'pinmaps'
503
                            return 0
504
                            ;;
505
                    esac
506
                    ;;
507
                tracelog)
508
                    return 0
509
                    ;;
510
                profile)
511
                    case $cword in
512
                        3)
513
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
514
                            return 0
515
                            ;;
516
                        4)
517
                            case $prev in
518
                                id)
519
                                    _bpftool_get_prog_ids
520
                                    ;;
521
                                name)
522
                                    _bpftool_get_prog_names
523
                                    ;;
524
                                pinned)
525
                                    _filedir
526
                                    ;;
527
                            esac
528
                            return 0
529
                            ;;
530
                        5)
531
                            COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
532
                            return 0
533
                            ;;
534
                        6)
535
                            case $prev in
536
                                duration)
537
                                    return 0
538
                                    ;;
539
                                *)
540
                                    COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
541
                                    return 0
542
                                    ;;
543
                            esac
544
                            return 0
545
                            ;;
546
                        *)
547
                            COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
548
                            return 0
549
                            ;;
550
                    esac
551
                    ;;
552
                run)
553
                    if [[ ${#words[@]} -eq 4 ]]; then
554
                        COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
555
                        return 0
556
                    fi
557
                    case $prev in
558
                        id)
559
                            _bpftool_get_prog_ids
560
                            return 0
561
                            ;;
562
                        name)
563
                            _bpftool_get_prog_names
564
                            return 0
565
                            ;;
566
                        data_in|data_out|ctx_in|ctx_out)
567
                            _filedir
568
                            return 0
569
                            ;;
570
                        repeat|data_size_out|ctx_size_out)
571
                            return 0
572
                            ;;
573
                        *)
574
                            _bpftool_once_attr 'data_in data_out data_size_out \
575
                                ctx_in ctx_out ctx_size_out repeat'
576
                            return 0
577
                            ;;
578
                    esac
579
                    ;;
580
                *)
581
                    [[ $prev == $object ]] && \
582
                        COMPREPLY=( $( compgen -W 'dump help pin attach detach \
583
                            load loadall show list tracelog run profile' -- "$cur" ) )
584
                    ;;
585
            esac
586
            ;;
587
        struct_ops)
588
            local STRUCT_OPS_TYPE='id name'
589
            case $command in
590
                show|list|dump|unregister)
591
                    case $prev in
592
                        $command)
593
                            COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
594
                            ;;
595
                        id)
596
                            _bpftool_get_map_ids_for_type struct_ops
597
                            ;;
598
                        name)
599
                            _bpftool_get_map_names_for_type struct_ops
600
                            ;;
601
                    esac
602
                    return 0
603
                    ;;
604
                register)
605
                    _filedir
606
                    return 0
607
                    ;;
608
                *)
609
                    [[ $prev == $object ]] && \
610
                        COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
611
                            -- "$cur" ) )
612
                    ;;
613
            esac
614
            ;;
615
        iter)
616
            case $command in
617
                pin)
618
                    _filedir
619
                    return 0
620
                    ;;
621
                *)
622
                    [[ $prev == $object ]] && \
623
                        COMPREPLY=( $( compgen -W 'pin help' \
624
                            -- "$cur" ) )
625
                    ;;
626
            esac
627
            ;;
628
        map)
629
            local MAP_TYPE='id pinned name'
630
            case $command in
631
                show|list|dump|peek|pop|dequeue|freeze)
632
                    case $prev in
633
                        $command)
634
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
635
                            return 0
636
                            ;;
637
                        id)
638
                            case "$command" in
639
                                peek)
640
                                    _bpftool_get_map_ids_for_type stack
641
                                    _bpftool_get_map_ids_for_type queue
642
                                    ;;
643
                                pop)
644
                                    _bpftool_get_map_ids_for_type stack
645
                                    ;;
646
                                dequeue)
647
                                    _bpftool_get_map_ids_for_type queue
648
                                    ;;
649
                                *)
650
                                    _bpftool_get_map_ids
651
                                    ;;
652
                            esac
653
                            return 0
654
                            ;;
655
                        name)
656
                            case "$command" in
657
                                peek)
658
                                    _bpftool_get_map_names_for_type stack
659
                                    _bpftool_get_map_names_for_type queue
660
                                    ;;
661
                                pop)
662
                                    _bpftool_get_map_names_for_type stack
663
                                    ;;
664
                                dequeue)
665
                                    _bpftool_get_map_names_for_type queue
666
                                    ;;
667
                                *)
668
                                    _bpftool_get_map_names
669
                                    ;;
670
                            esac
671
                            return 0
672
                            ;;
673
                        *)
674
                            return 0
675
                            ;;
676
                    esac
677
                    ;;
678
                create)
679
                    case $prev in
680
                        $command)
681
                            _filedir
682
                            return 0
683
                            ;;
684
                        type)
685
                            COMPREPLY=( $( compgen -W 'hash array prog_array \
686
                                perf_event_array percpu_hash percpu_array \
687
                                stack_trace cgroup_array lru_hash \
688
                                lru_percpu_hash lpm_trie array_of_maps \
689
                                hash_of_maps devmap devmap_hash sockmap cpumap \
690
                                xskmap sockhash cgroup_storage reuseport_sockarray \
691
                                percpu_cgroup_storage queue stack' -- \
692
                                                   "$cur" ) )
693
                            return 0
694
                            ;;
695
                        key|value|flags|name|entries)
696
                            return 0
697
                            ;;
698
                        *)
699
                            _bpftool_once_attr 'type'
700
                            _bpftool_once_attr 'key'
701
                            _bpftool_once_attr 'value'
702
                            _bpftool_once_attr 'entries'
703
                            _bpftool_once_attr 'name'
704
                            _bpftool_once_attr 'flags'
705
                            _bpftool_once_attr 'dev'
706
                            return 0
707
                            ;;
708
                    esac
709
                    ;;
710
                lookup|getnext|delete)
711
                    case $prev in
712
                        $command)
713
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
714
                            return 0
715
                            ;;
716
                        id)
717
                            _bpftool_get_map_ids
718
                            return 0
719
                            ;;
720
                        name)
721
                            _bpftool_get_map_names
722
                            return 0
723
                            ;;
724
                        key)
725
                            COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
726
                            ;;
727
                        *)
728
                            case $(_bpftool_map_guess_map_type) in
729
                                queue|stack)
730
                                    return 0
731
                                    ;;
732
                            esac
733
 
734
                            _bpftool_once_attr 'key'
735
                            return 0
736
                            ;;
737
                    esac
738
                    ;;
739
                update|push|enqueue)
740
                    case $prev in
741
                        $command)
742
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
743
                            return 0
744
                            ;;
745
                        id)
746
                            _bpftool_map_update_get_id $command
747
                            return 0
748
                            ;;
749
                        name)
750
                            _bpftool_map_update_get_name $command
751
                            return 0
752
                            ;;
753
                        key)
754
                            COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
755
                            ;;
756
                        value)
757
                            # We can have bytes, or references to a prog or a
758
                            # map, depending on the type of the map to update.
759
                            case "$(_bpftool_map_guess_map_type)" in
760
                                array_of_maps|hash_of_maps)
761
                                    local MAP_TYPE='id pinned name'
762
                                    COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
763
                                        -- "$cur" ) )
764
                                    return 0
765
                                    ;;
766
                                prog_array)
767
                                    local PROG_TYPE='id pinned tag name'
768
                                    COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
769
                                        -- "$cur" ) )
770
                                    return 0
771
                                    ;;
772
                                *)
773
                                    COMPREPLY+=( $( compgen -W 'hex' \
774
                                        -- "$cur" ) )
775
                                    return 0
776
                                    ;;
777
                            esac
778
                            return 0
779
                            ;;
780
                        *)
781
                            case $(_bpftool_map_guess_map_type) in
782
                                queue|stack)
783
                                    _bpftool_once_attr 'value'
784
                                    return 0;
785
                                    ;;
786
                            esac
787
 
788
                            _bpftool_once_attr 'key'
789
                            local UPDATE_FLAGS='any exist noexist'
790
                            for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
791
                                if [[ ${words[idx]} == 'value' ]]; then
792
                                    # 'value' is present, but is not the last
793
                                    # word i.e. we can now have UPDATE_FLAGS.
794
                                    _bpftool_one_of_list "$UPDATE_FLAGS"
795
                                    return 0
796
                                fi
797
                            done
798
                            for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
799
                                if [[ ${words[idx]} == 'key' ]]; then
800
                                    # 'key' is present, but is not the last
801
                                    # word i.e. we can now have 'value'.
802
                                    _bpftool_once_attr 'value'
803
                                    return 0
804
                                fi
805
                            done
806
 
807
                            return 0
808
                            ;;
809
                    esac
810
                    ;;
811
                pin)
812
                    case $prev in
813
                        $command)
814
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
815
                            ;;
816
                        id)
817
                            _bpftool_get_map_ids
818
                            ;;
819
                        name)
820
                            _bpftool_get_map_names
821
                            ;;
822
                    esac
823
                    return 0
824
                    ;;
825
                event_pipe)
826
                    case $prev in
827
                        $command)
828
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
829
                            return 0
830
                            ;;
831
                        id)
832
                            _bpftool_get_map_ids_for_type perf_event_array
833
                            return 0
834
                            ;;
835
                        name)
836
                            _bpftool_get_map_names_for_type perf_event_array
837
                            return 0
838
                            ;;
839
                        cpu)
840
                            return 0
841
                            ;;
842
                        index)
843
                            return 0
844
                            ;;
845
                        *)
846
                            _bpftool_once_attr 'cpu'
847
                            _bpftool_once_attr 'index'
848
                            return 0
849
                            ;;
850
                    esac
851
                    ;;
852
                *)
853
                    [[ $prev == $object ]] && \
854
                        COMPREPLY=( $( compgen -W 'delete dump getnext help \
855
                            lookup pin event_pipe show list update create \
856
                            peek push enqueue pop dequeue freeze' -- \
857
                            "$cur" ) )
858
                    ;;
859
            esac
860
            ;;
861
        btf)
862
            local PROG_TYPE='id pinned tag name'
863
            local MAP_TYPE='id pinned name'
864
            case $command in
865
                dump)
866
                    case $prev in
867
                        $command)
868
                            COMPREPLY+=( $( compgen -W "id map prog file" -- \
869
                                "$cur" ) )
870
                            return 0
871
                            ;;
872
                        prog)
873
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
874
                            return 0
875
                            ;;
876
                        map)
877
                            COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
878
                            return 0
879
                            ;;
880
                        id)
881
                            case $pprev in
882
                                prog)
883
                                    _bpftool_get_prog_ids
884
                                    ;;
885
                                map)
886
                                    _bpftool_get_map_ids
887
                                    ;;
888
                                $command)
889
                                    _bpftool_get_btf_ids
890
                                    ;;
891
                            esac
892
                            return 0
893
                            ;;
894
                        name)
895
                            case $pprev in
896
                                prog)
897
                                    _bpftool_get_prog_names
898
                                    ;;
899
                                map)
900
                                    _bpftool_get_map_names
901
                                    ;;
902
                            esac
903
                            return 0
904
                            ;;
905
                        format)
906
                            COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
907
                            ;;
908
                        *)
909
                            # emit extra options
910
                            case ${words[3]} in
911
                                id|file)
912
                                    _bpftool_once_attr 'format'
913
                                    ;;
914
                                map|prog)
915
                                    if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
916
                                        COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
917
                                    fi
918
                                    _bpftool_once_attr 'format'
919
                                    ;;
920
                                *)
921
                                    ;;
922
                            esac
923
                            return 0
924
                            ;;
925
                    esac
926
                    ;;
927
                show|list)
928
                    case $prev in
929
                        $command)
930
                            COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
931
                            ;;
932
                        id)
933
                            _bpftool_get_btf_ids
934
                            ;;
935
                    esac
936
                    return 0
937
                    ;;
938
                *)
939
                    [[ $prev == $object ]] && \
940
                        COMPREPLY=( $( compgen -W 'dump help show list' \
941
                            -- "$cur" ) )
942
                    ;;
943
            esac
944
            ;;
945
        gen)
946
            case $command in
947
                skeleton)
948
                    _filedir
949
                    ;;
950
                *)
951
                    [[ $prev == $object ]] && \
952
                        COMPREPLY=( $( compgen -W 'skeleton help' -- "$cur" ) )
953
                    ;;
954
            esac
955
            ;;
956
        cgroup)
957
            case $command in
958
                show|list|tree)
959
                    case $cword in
960
                        3)
961
                            _filedir
962
                            ;;
963
                        4)
964
                            COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
965
                            ;;
966
                    esac
967
                    return 0
968
                    ;;
969
                attach|detach)
970
                    local ATTACH_TYPES='ingress egress sock_create sock_ops \
971
                        device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
972
                        getpeername4 getpeername6 getsockname4 getsockname6 \
973
                        sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
974
                        setsockopt'
975
                    local ATTACH_FLAGS='multi override'
976
                    local PROG_TYPE='id pinned tag name'
977
                    case $prev in
978
                        $command)
979
                            _filedir
980
                            return 0
981
                            ;;
982
                        ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
983
                        post_bind4|post_bind6|connect4|connect6|getpeername4|\
984
                        getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
985
                        recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt)
986
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
987
                                "$cur" ) )
988
                            return 0
989
                            ;;
990
                        id)
991
                            _bpftool_get_prog_ids
992
                            return 0
993
                            ;;
994
                        *)
995
                            if ! _bpftool_search_list "$ATTACH_TYPES"; then
996
                                COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
997
                                    "$cur" ) )
998
                            elif [[ "$command" == "attach" ]]; then
999
                                # We have an attach type on the command line,
1000
                                # but it is not the previous word, or
1001
                                # "id|pinned|tag|name" (we already checked for
1002
                                # that). This should only leave the case when
1003
                                # we need attach flags for "attach" commamnd.
1004
                                _bpftool_one_of_list "$ATTACH_FLAGS"
1005
                            fi
1006
                            return 0
1007
                            ;;
1008
                    esac
1009
                    ;;
1010
                *)
1011
                    [[ $prev == $object ]] && \
1012
                        COMPREPLY=( $( compgen -W 'help attach detach \
1013
                            show list tree' -- "$cur" ) )
1014
                    ;;
1015
            esac
1016
            ;;
1017
        perf)
1018
            case $command in
1019
                *)
1020
                    [[ $prev == $object ]] && \
1021
                        COMPREPLY=( $( compgen -W 'help \
1022
                            show list' -- "$cur" ) )
1023
                    ;;
1024
            esac
1025
            ;;
1026
        net)
1027
            local PROG_TYPE='id pinned tag name'
1028
            local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
1029
            case $command in
1030
                show|list)
1031
                    [[ $prev != "$command" ]] && return 0
1032
                    COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1033
                    return 0
1034
                    ;;
1035
                attach)
1036
                    case $cword in
1037
                        3)
1038
                            COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1039
                            return 0
1040
                            ;;
1041
                        4)
1042
                            COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1043
                            return 0
1044
                            ;;
1045
                        5)
1046
                            case $prev in
1047
                                id)
1048
                                    _bpftool_get_prog_ids
1049
                                    ;;
1050
                                name)
1051
                                    _bpftool_get_prog_names
1052
                                    ;;
1053
                                pinned)
1054
                                    _filedir
1055
                                    ;;
1056
                            esac
1057
                            return 0
1058
                            ;;
1059
                        6)
1060
                            COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1061
                            return 0
1062
                            ;;
1063
                        8)
1064
                            _bpftool_once_attr 'overwrite'
1065
                            return 0
1066
                            ;;
1067
                    esac
1068
                    ;;
1069
                detach)
1070
                    case $cword in
1071
                        3)
1072
                            COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1073
                            return 0
1074
                            ;;
1075
                        4)
1076
                            COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1077
                            return 0
1078
                            ;;
1079
                    esac
1080
                    ;;
1081
                *)
1082
                    [[ $prev == $object ]] && \
1083
                        COMPREPLY=( $( compgen -W 'help \
1084
                            show list attach detach' -- "$cur" ) )
1085
                    ;;
1086
            esac
1087
            ;;
1088
        feature)
1089
            case $command in
1090
                probe)
1091
                    [[ $prev == "prefix" ]] && return 0
1092
                    if _bpftool_search_list 'macros'; then
1093
                        _bpftool_once_attr 'prefix'
1094
                    else
1095
                        COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1096
                    fi
1097
                    _bpftool_one_of_list 'kernel dev'
1098
                    _bpftool_once_attr 'full unprivileged'
1099
                    return 0
1100
                    ;;
1101
                *)
1102
                    [[ $prev == $object ]] && \
1103
                        COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
1104
                    ;;
1105
            esac
1106
            ;;
1107
        link)
1108
            case $command in
1109
                show|list|pin)
1110
                    case $prev in
1111
                        id)
1112
                            _bpftool_get_link_ids
1113
                            return 0
1114
                            ;;
1115
                    esac
1116
                    ;;
1117
            esac
1118
 
1119
            local LINK_TYPE='id pinned'
1120
            case $command in
1121
                show|list)
1122
                    [[ $prev != "$command" ]] && return 0
1123
                    COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1124
                    return 0
1125
                    ;;
1126
                pin)
1127
                    if [[ $prev == "$command" ]]; then
1128
                        COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1129
                    else
1130
                        _filedir
1131
                    fi
1132
                    return 0
1133
                    ;;
1134
                *)
1135
                    [[ $prev == $object ]] && \
1136
                        COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
1137
                    ;;
1138
            esac
1139
            ;;
1140
    esac
1141
} &&
1142
complete -F _bpftool bpftool
1143
 
1144
# ex: ts=4 sw=4 et filetype=sh