1 # bpftool(8) bash completion -*- shell-script -*-
3 # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
4 # Copyright (C) 2017-2018 Netronome Systems, Inc.
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.
15 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
16 if [[ $w == ${words[idx]} ]]; then
21 [[ $found -eq 0 ]] && \
22 COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
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()
32 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
33 [[ $w == ${words[idx]} ]] && return 0
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()
43 _bpftool_search_list $* && return 1
44 COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
47 _bpftool_get_map_ids()
49 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
50 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
53 # Takes map type and adds matching map ids to the list of suggestions.
54 _bpftool_get_map_ids_for_type()
57 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
58 command grep -C2 "$type" | \
59 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
62 _bpftool_get_map_names()
64 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
65 command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
68 # Takes map type and adds matching map names to the list of suggestions.
69 _bpftool_get_map_names_for_type()
72 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
73 command grep -C2 "$type" | \
74 command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
77 _bpftool_get_prog_ids()
79 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
80 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
83 _bpftool_get_prog_tags()
85 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
86 command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
89 _bpftool_get_prog_names()
91 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
92 command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) )
95 _bpftool_get_btf_ids()
97 COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
98 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
101 _bpftool_get_link_ids()
103 COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \
104 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
107 _bpftool_get_obj_map_names()
113 maps=$(objdump -j .maps -t $obj 2>/dev/null | \
114 command awk '/g . .maps/ {print $NF}')
116 COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
119 _bpftool_get_obj_map_idxs()
125 nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
127 COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
132 COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
136 # Retrieve type of the map that we are operating on.
137 _bpftool_map_guess_map_type()
139 local keyword idx ref=""
140 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
141 case "${words[$((idx-2))]}" in
143 keyword=${words[$((idx-1))]}
144 ref=${words[$((idx))]}
156 [[ -z $ref ]] && return 0
159 type=$(bpftool -jp map show $keyword $ref | \
160 command sed -n 's/.*"type": "\(.*\)",$/\1/p')
161 [[ -n $type ]] && printf $type
164 _bpftool_map_update_get_id()
168 # Is it the map to update, or a map to insert into the map to update?
169 # Search for "value" keyword.
171 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
172 if [[ ${words[idx]} == "value" ]]; then
177 if [[ $value -eq 0 ]]; then
180 _bpftool_get_map_ids_for_type stack
183 _bpftool_get_map_ids_for_type queue
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)
196 array_of_maps|hash_of_maps)
201 _bpftool_get_prog_ids
210 _bpftool_map_update_get_name()
214 # Is it the map to update, or a map to insert into the map to update?
215 # Search for "value" keyword.
217 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
218 if [[ ${words[idx]} == "value" ]]; then
223 if [[ $value -eq 0 ]]; then
226 _bpftool_get_map_names_for_type stack
229 _bpftool_get_map_names_for_type queue
232 _bpftool_get_map_names
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)
242 array_of_maps|hash_of_maps)
243 _bpftool_get_map_names
247 _bpftool_get_prog_names
258 local cur prev words cword comp_args
260 _init_completion -- "$@" || return
263 if [[ ${words[cword]} == -* ]]; then
264 local c='--version --json --pretty --bpffs --mapcompat --debug \
265 --use-loader --base-btf'
266 COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
269 if _bpftool_search_list -j --json -p --pretty; then
273 # Deal with simplest keywords
279 _bpftool_get_prog_tags
282 dev|offload_dev|xdpmeta_dev)
286 file|pinned|-B|--base-btf)
291 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
296 # Remove all options so completions don't have to deal with them.
298 for (( i=1; i < ${#words[@]}; )); do
299 if [[ ${words[i]::1} == - ]] &&
300 [[ ${words[i]} != "-B" ]] && [[ ${words[i]} != "--base-btf" ]]; then
301 words=( "${words[@]:0:i}" "${words[@]:i+1}" )
302 [[ $i -le $cword ]] && cword=$(( cword - 1 ))
308 prev=${words[cword - 1]}
309 pprev=${words[cword - 2]}
311 local object=${words[1]}
313 if [[ -z $object || $cword -eq 1 ]]; then
316 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
318 -e '/OBJECT := /!d' \
321 -e 's/|//g' )" -- "$cur" ) )
322 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
328 local command=${words[2]}
329 [[ $command == help ]] && return 0
331 local MAP_TYPE='id pinned name'
332 local PROG_TYPE='id pinned tag name'
334 # Completion depends on object and command in use
337 # Complete id and name, only for subcommands that use prog (but no
343 _bpftool_get_prog_ids
347 _bpftool_get_prog_names
354 local METRIC_TYPE='cycles instructions l1d_loads llc_misses \
355 itlb_misses dtlb_misses'
358 [[ $prev != "$command" ]] && return 0
359 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
365 COMPREPLY+=( $( compgen -W "xlated jited" -- \
370 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
375 # "file" is not compatible with other keywords here
376 if _bpftool_search_list 'file'; then
379 if ! _bpftool_search_list 'linum opcodes visual'; then
380 _bpftool_once_attr 'file'
382 _bpftool_once_attr 'linum opcodes'
383 if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then
384 _bpftool_once_attr 'visual'
391 if [[ $prev == "$command" ]]; then
392 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
401 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
407 _bpftool_get_prog_ids
410 _bpftool_get_prog_names
419 local BPFTOOL_PROG_ATTACH_TYPES='sk_msg_verdict \
420 sk_skb_verdict sk_skb_stream_verdict sk_skb_stream_parser \
422 COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
426 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
435 _bpftool_get_map_names
448 # Propose "load/loadall" to complete "bpftool prog load",
449 # or bash tries to complete "load" as a filename below.
450 if [[ ${#words[@]} -eq 3 ]]; then
451 COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )
455 if [[ ${#words[@]} -lt 6 ]]; then
462 if [[ ${words[-4]} == "map" ]]; then
463 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
466 if [[ ${words[-3]} == "map" ]]; then
467 if [[ ${words[-2]} == "idx" ]]; then
468 _bpftool_get_obj_map_idxs $obj
469 elif [[ ${words[-2]} == "name" ]]; then
470 _bpftool_get_obj_map_names $obj
474 if [[ ${words[-2]} == "map" ]]; then
475 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
481 local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
482 kretprobe classifier flow_dissector \
483 action tracepoint raw_tracepoint \
484 xdp perf_event cgroup/skb cgroup/sock \
485 cgroup/dev lwt_in lwt_out lwt_xmit \
486 lwt_seg6local sockops sk_skb sk_msg lirc_mode2 \
487 cgroup/bind4 cgroup/bind6 \
488 cgroup/connect4 cgroup/connect6 cgroup/connect_unix \
489 cgroup/getpeername4 cgroup/getpeername6 cgroup/getpeername_unix \
490 cgroup/getsockname4 cgroup/getsockname6 cgroup/getsockname_unix \
491 cgroup/sendmsg4 cgroup/sendmsg6 cgroup/sendmsg_unix \
492 cgroup/recvmsg4 cgroup/recvmsg6 cgroup/recvmsg_unix \
493 cgroup/post_bind4 cgroup/post_bind6 \
494 cgroup/sysctl cgroup/getsockopt \
495 cgroup/setsockopt cgroup/sock_release struct_ops \
496 fentry fexit freplace sk_lookup'
497 COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )
505 _bpftool_get_map_names
513 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
514 _bpftool_once_attr 'type pinmaps autoattach'
515 _bpftool_one_of_list 'offload_dev xdpmeta_dev'
526 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
532 _bpftool_get_prog_ids
535 _bpftool_get_prog_names
544 COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
548 [[ $prev == duration ]] && return 0
549 _bpftool_once_attr "$METRIC_TYPE"
555 if [[ ${#words[@]} -eq 4 ]]; then
556 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
561 _bpftool_get_prog_ids
565 _bpftool_get_prog_names
568 data_in|data_out|ctx_in|ctx_out)
572 repeat|data_size_out|ctx_size_out)
576 _bpftool_once_attr 'data_in data_out data_size_out \
577 ctx_in ctx_out ctx_size_out repeat'
583 [[ $prev == $object ]] && \
584 COMPREPLY=( $( compgen -W 'dump help pin attach detach \
585 load loadall show list tracelog run profile' -- "$cur" ) )
590 local STRUCT_OPS_TYPE='id name'
592 show|list|dump|unregister)
595 COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
598 _bpftool_get_map_ids_for_type struct_ops
601 _bpftool_get_map_names_for_type struct_ops
607 [[ $prev == $command ]] && _filedir
611 [[ $prev == $object ]] && \
612 COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
628 _bpftool_get_map_names
634 _bpftool_one_of_list $MAP_TYPE
637 _bpftool_once_attr 'map'
643 [[ $prev == $object ]] && \
644 COMPREPLY=( $( compgen -W 'pin help' \
651 show|list|dump|peek|pop|dequeue|freeze)
654 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
660 _bpftool_get_map_ids_for_type stack
661 _bpftool_get_map_ids_for_type queue
664 _bpftool_get_map_ids_for_type stack
667 _bpftool_get_map_ids_for_type queue
678 _bpftool_get_map_names_for_type stack
679 _bpftool_get_map_names_for_type queue
682 _bpftool_get_map_names_for_type stack
685 _bpftool_get_map_names_for_type queue
688 _bpftool_get_map_names
705 local BPFTOOL_MAP_CREATE_TYPES="$(bpftool feature list_builtins map_types 2>/dev/null | \
707 COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
710 key|value|flags|entries)
714 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
723 _bpftool_get_map_names
731 _bpftool_once_attr 'type key value entries name flags offload_dev'
732 if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then
733 _bpftool_once_attr 'inner_map'
739 lookup|getnext|delete)
742 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
750 _bpftool_get_map_names
754 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
757 case $(_bpftool_map_guess_map_type) in
763 _bpftool_once_attr 'key'
771 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
775 _bpftool_map_update_get_id $command
779 _bpftool_map_update_get_name $command
783 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
786 # We can have bytes, or references to a prog or a
787 # map, depending on the type of the map to update.
788 case "$(_bpftool_map_guess_map_type)" in
789 array_of_maps|hash_of_maps)
790 COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
795 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
800 COMPREPLY+=( $( compgen -W 'hex' \
808 case $(_bpftool_map_guess_map_type) in
810 _bpftool_once_attr 'value'
815 _bpftool_once_attr 'key'
816 local UPDATE_FLAGS='any exist noexist' idx
817 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
818 if [[ ${words[idx]} == 'value' ]]; then
819 # 'value' is present, but is not the last
820 # word i.e. we can now have UPDATE_FLAGS.
821 _bpftool_one_of_list "$UPDATE_FLAGS"
825 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
826 if [[ ${words[idx]} == 'key' ]]; then
827 # 'key' is present, but is not the last
828 # word i.e. we can now have 'value'.
829 _bpftool_once_attr 'value'
841 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
847 _bpftool_get_map_names
855 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
859 _bpftool_get_map_ids_for_type perf_event_array
863 _bpftool_get_map_names_for_type perf_event_array
873 _bpftool_once_attr 'cpu index'
879 [[ $prev == $object ]] && \
880 COMPREPLY=( $( compgen -W 'delete dump getnext help \
881 lookup pin event_pipe show list update create \
882 peek push enqueue pop dequeue freeze' -- \
888 local MAP_TYPE='id pinned name'
893 COMPREPLY+=( $( compgen -W "id map prog file" -- \
898 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
902 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
908 _bpftool_get_prog_ids
922 _bpftool_get_prog_names
925 _bpftool_get_map_names
931 COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
934 COMPREPLY=( $( compgen -W "unsorted" -- "$cur" ) )
940 _bpftool_once_attr 'format'
943 if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
944 COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
946 _bpftool_once_attr 'format'
958 COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
967 [[ $prev == $object ]] && \
968 COMPREPLY=( $( compgen -W 'dump help show list' \
986 _bpftool_once_attr 'name'
998 _bpftool_once_attr 'name'
1008 [[ $prev == $object ]] && \
1009 COMPREPLY=( $( compgen -W 'object skeleton subskeleton help min_core_btf' -- "$cur" ) )
1021 COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
1027 local BPFTOOL_CGROUP_ATTACH_TYPES="$(bpftool feature list_builtins attach_types 2>/dev/null | \
1029 local ATTACH_FLAGS='multi override'
1030 # Check for $prev = $command first
1031 if [ $prev = $command ]; then
1034 # Then check for attach type. This is done outside of the
1035 # "case $prev in" to avoid writing the whole list of attach
1036 # types again as pattern to match (where we cannot reuse
1038 elif [[ $BPFTOOL_CGROUP_ATTACH_TYPES =~ $prev ]]; then
1039 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
1043 # case/esac for the other cases
1046 _bpftool_get_prog_ids
1050 if ! _bpftool_search_list "$BPFTOOL_CGROUP_ATTACH_TYPES"; then
1051 COMPREPLY=( $( compgen -W \
1052 "$BPFTOOL_CGROUP_ATTACH_TYPES" -- "$cur" ) )
1053 elif [[ "$command" == "attach" ]]; then
1054 # We have an attach type on the command line,
1055 # but it is not the previous word, or
1056 # "id|pinned|tag|name" (we already checked for
1057 # that). This should only leave the case when
1058 # we need attach flags for "attach" commamnd.
1059 _bpftool_one_of_list "$ATTACH_FLAGS"
1066 [[ $prev == $object ]] && \
1067 COMPREPLY=( $( compgen -W 'help attach detach \
1068 show list tree' -- "$cur" ) )
1075 [[ $prev == $object ]] && \
1076 COMPREPLY=( $( compgen -W 'help \
1077 show list' -- "$cur" ) )
1082 local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload tcx_ingress tcx_egress'
1085 [[ $prev != "$command" ]] && return 0
1086 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1092 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1096 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1102 _bpftool_get_prog_ids
1105 _bpftool_get_prog_names
1114 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1118 _bpftool_once_attr 'overwrite'
1126 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1130 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1136 [[ $prev == $object ]] && \
1137 COMPREPLY=( $( compgen -W 'help \
1138 show list attach detach' -- "$cur" ) )
1145 [[ $prev == "prefix" ]] && return 0
1146 if _bpftool_search_list 'macros'; then
1147 _bpftool_once_attr 'prefix'
1149 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1151 _bpftool_one_of_list 'kernel dev'
1152 _bpftool_once_attr 'full unprivileged'
1156 [[ $prev != "$command" ]] && return 0
1157 COMPREPLY=( $( compgen -W 'prog_types map_types \
1158 attach_types link_types helpers' -- "$cur" ) )
1161 [[ $prev == $object ]] && \
1162 COMPREPLY=( $( compgen -W 'help list_builtins probe' -- "$cur" ) )
1168 show|list|pin|detach)
1171 _bpftool_get_link_ids
1178 local LINK_TYPE='id pinned'
1181 [[ $prev != "$command" ]] && return 0
1182 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1186 if [[ $prev == "$command" ]]; then
1187 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1188 elif [[ $pprev == "$command" ]]; then
1194 [[ $prev == $object ]] && \
1195 COMPREPLY=( $( compgen -W 'help pin detach show list' -- "$cur" ) )
1201 complete -F _bpftool bpftool
1203 # ex: ts=4 sw=4 et filetype=sh