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