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.
6 # Author: Quentin Monnet <quentin.monnet@netronome.com>
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()
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 objword json=0
259 _init_completion || return
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" ) )
268 if _bpftool_search_list -j --json -p --pretty; then
272 # Deal with simplest keywords
278 _bpftool_get_prog_tags
281 dev|offload_dev|xdpmeta_dev)
285 file|pinned|-B|--base-btf)
290 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
295 # Remove all options so completions don't have to deal with them.
297 for (( i=1; i < ${#words[@]}; )); do
298 if [[ ${words[i]::1} == - ]] &&
299 [[ ${words[i]} != "-B" ]] && [[ ${words[i]} != "--base-btf" ]]; then
300 words=( "${words[@]:0:i}" "${words[@]:i+1}" )
301 [[ $i -le $cword ]] && cword=$(( cword - 1 ))
307 prev=${words[cword - 1]}
308 pprev=${words[cword - 2]}
310 local object=${words[1]} command=${words[2]}
312 if [[ -z $object || $cword -eq 1 ]]; then
315 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
317 -e '/OBJECT := /!d' \
320 -e 's/|//g' )" -- "$cur" ) )
321 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
327 [[ $command == help ]] && return 0
329 # Completion depends on object and command in use
332 # Complete id and name, only for subcommands that use prog (but no
338 _bpftool_get_prog_ids
342 _bpftool_get_prog_names
349 local PROG_TYPE='id pinned tag name'
350 local MAP_TYPE='id pinned name'
351 local METRIC_TYPE='cycles instructions l1d_loads llc_misses \
352 itlb_misses dtlb_misses'
355 [[ $prev != "$command" ]] && return 0
356 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
362 COMPREPLY+=( $( compgen -W "xlated jited" -- \
367 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
372 # "file" is not compatible with other keywords here
373 if _bpftool_search_list 'file'; then
376 if ! _bpftool_search_list 'linum opcodes visual'; then
377 _bpftool_once_attr 'file'
379 _bpftool_once_attr 'linum opcodes'
380 if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then
381 _bpftool_once_attr 'visual'
388 if [[ $prev == "$command" ]]; then
389 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
398 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
404 _bpftool_get_prog_ids
407 _bpftool_get_prog_names
416 local BPFTOOL_PROG_ATTACH_TYPES='sk_msg_verdict \
417 sk_skb_verdict sk_skb_stream_verdict sk_skb_stream_parser \
419 COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
423 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
432 _bpftool_get_map_names
445 # Propose "load/loadall" to complete "bpftool prog load",
446 # or bash tries to complete "load" as a filename below.
447 if [[ ${#words[@]} -eq 3 ]]; then
448 COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )
452 if [[ ${#words[@]} -lt 6 ]]; then
459 if [[ ${words[-4]} == "map" ]]; then
460 COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
463 if [[ ${words[-3]} == "map" ]]; then
464 if [[ ${words[-2]} == "idx" ]]; then
465 _bpftool_get_obj_map_idxs $obj
466 elif [[ ${words[-2]} == "name" ]]; then
467 _bpftool_get_obj_map_names $obj
471 if [[ ${words[-2]} == "map" ]]; then
472 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
478 local BPFTOOL_PROG_LOAD_TYPES='socket kprobe \
479 kretprobe classifier flow_dissector \
480 action tracepoint raw_tracepoint \
481 xdp perf_event cgroup/skb cgroup/sock \
482 cgroup/dev lwt_in lwt_out lwt_xmit \
483 lwt_seg6local sockops sk_skb sk_msg lirc_mode2 \
484 cgroup/bind4 cgroup/bind6 \
485 cgroup/connect4 cgroup/connect6 cgroup/connect_unix \
486 cgroup/getpeername4 cgroup/getpeername6 cgroup/getpeername_unix \
487 cgroup/getsockname4 cgroup/getsockname6 cgroup/getsockname_unix \
488 cgroup/sendmsg4 cgroup/sendmsg6 cgroup/sendmsg_unix \
489 cgroup/recvmsg4 cgroup/recvmsg6 cgroup/recvmsg_unix \
490 cgroup/post_bind4 cgroup/post_bind6 \
491 cgroup/sysctl cgroup/getsockopt \
492 cgroup/setsockopt cgroup/sock_release struct_ops \
493 fentry fexit freplace sk_lookup'
494 COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_LOAD_TYPES" -- "$cur" ) )
502 _bpftool_get_map_names
510 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
511 _bpftool_once_attr 'type pinmaps autoattach'
512 _bpftool_one_of_list 'offload_dev xdpmeta_dev'
523 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
529 _bpftool_get_prog_ids
532 _bpftool_get_prog_names
541 COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
550 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
557 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
563 if [[ ${#words[@]} -eq 4 ]]; then
564 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
569 _bpftool_get_prog_ids
573 _bpftool_get_prog_names
576 data_in|data_out|ctx_in|ctx_out)
580 repeat|data_size_out|ctx_size_out)
584 _bpftool_once_attr 'data_in data_out data_size_out \
585 ctx_in ctx_out ctx_size_out repeat'
591 [[ $prev == $object ]] && \
592 COMPREPLY=( $( compgen -W 'dump help pin attach detach \
593 load loadall show list tracelog run profile' -- "$cur" ) )
598 local STRUCT_OPS_TYPE='id name'
600 show|list|dump|unregister)
603 COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
606 _bpftool_get_map_ids_for_type struct_ops
609 _bpftool_get_map_names_for_type struct_ops
619 [[ $prev == $object ]] && \
620 COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
636 _bpftool_get_map_names
642 _bpftool_one_of_list $MAP_TYPE
648 [[ $prev == $object ]] && \
649 COMPREPLY=( $( compgen -W 'pin help' \
655 local MAP_TYPE='id pinned name'
657 show|list|dump|peek|pop|dequeue|freeze)
660 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
666 _bpftool_get_map_ids_for_type stack
667 _bpftool_get_map_ids_for_type queue
670 _bpftool_get_map_ids_for_type stack
673 _bpftool_get_map_ids_for_type queue
684 _bpftool_get_map_names_for_type stack
685 _bpftool_get_map_names_for_type queue
688 _bpftool_get_map_names_for_type stack
691 _bpftool_get_map_names_for_type queue
694 _bpftool_get_map_names
711 local BPFTOOL_MAP_CREATE_TYPES="$(bpftool feature list_builtins map_types 2>/dev/null | \
713 COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
716 key|value|flags|entries)
720 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
729 _bpftool_get_map_names
737 _bpftool_once_attr 'type key value entries name flags offload_dev'
738 if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then
739 _bpftool_once_attr 'inner_map'
745 lookup|getnext|delete)
748 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
756 _bpftool_get_map_names
760 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
763 case $(_bpftool_map_guess_map_type) in
769 _bpftool_once_attr 'key'
777 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
781 _bpftool_map_update_get_id $command
785 _bpftool_map_update_get_name $command
789 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
792 # We can have bytes, or references to a prog or a
793 # map, depending on the type of the map to update.
794 case "$(_bpftool_map_guess_map_type)" in
795 array_of_maps|hash_of_maps)
796 local MAP_TYPE='id pinned name'
797 COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
802 local PROG_TYPE='id pinned tag name'
803 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
808 COMPREPLY+=( $( compgen -W 'hex' \
816 case $(_bpftool_map_guess_map_type) in
818 _bpftool_once_attr 'value'
823 _bpftool_once_attr 'key'
824 local UPDATE_FLAGS='any exist noexist'
825 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
826 if [[ ${words[idx]} == 'value' ]]; then
827 # 'value' is present, but is not the last
828 # word i.e. we can now have UPDATE_FLAGS.
829 _bpftool_one_of_list "$UPDATE_FLAGS"
833 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
834 if [[ ${words[idx]} == 'key' ]]; then
835 # 'key' is present, but is not the last
836 # word i.e. we can now have 'value'.
837 _bpftool_once_attr 'value'
849 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
855 _bpftool_get_map_names
863 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
867 _bpftool_get_map_ids_for_type perf_event_array
871 _bpftool_get_map_names_for_type perf_event_array
881 _bpftool_once_attr 'cpu index'
887 [[ $prev == $object ]] && \
888 COMPREPLY=( $( compgen -W 'delete dump getnext help \
889 lookup pin event_pipe show list update create \
890 peek push enqueue pop dequeue freeze' -- \
896 local PROG_TYPE='id pinned tag name'
897 local MAP_TYPE='id pinned name'
902 COMPREPLY+=( $( compgen -W "id map prog file" -- \
907 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
911 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
917 _bpftool_get_prog_ids
931 _bpftool_get_prog_names
934 _bpftool_get_map_names
940 COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
946 _bpftool_once_attr 'format'
949 if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
950 COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
952 _bpftool_once_attr 'format'
964 COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
973 [[ $prev == $object ]] && \
974 COMPREPLY=( $( compgen -W 'dump help show list' \
992 _bpftool_once_attr 'name'
1004 _bpftool_once_attr 'name'
1014 [[ $prev == $object ]] && \
1015 COMPREPLY=( $( compgen -W 'object skeleton subskeleton help min_core_btf' -- "$cur" ) )
1027 COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
1033 local BPFTOOL_CGROUP_ATTACH_TYPES="$(bpftool feature list_builtins attach_types 2>/dev/null | \
1035 local ATTACH_FLAGS='multi override'
1036 local PROG_TYPE='id pinned tag name'
1037 # Check for $prev = $command first
1038 if [ $prev = $command ]; then
1041 # Then check for attach type. This is done outside of the
1042 # "case $prev in" to avoid writing the whole list of attach
1043 # types again as pattern to match (where we cannot reuse
1045 elif [[ $BPFTOOL_CGROUP_ATTACH_TYPES =~ $prev ]]; then
1046 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
1050 # case/esac for the other cases
1053 _bpftool_get_prog_ids
1057 if ! _bpftool_search_list "$BPFTOOL_CGROUP_ATTACH_TYPES"; then
1058 COMPREPLY=( $( compgen -W \
1059 "$BPFTOOL_CGROUP_ATTACH_TYPES" -- "$cur" ) )
1060 elif [[ "$command" == "attach" ]]; then
1061 # We have an attach type on the command line,
1062 # but it is not the previous word, or
1063 # "id|pinned|tag|name" (we already checked for
1064 # that). This should only leave the case when
1065 # we need attach flags for "attach" commamnd.
1066 _bpftool_one_of_list "$ATTACH_FLAGS"
1073 [[ $prev == $object ]] && \
1074 COMPREPLY=( $( compgen -W 'help attach detach \
1075 show list tree' -- "$cur" ) )
1082 [[ $prev == $object ]] && \
1083 COMPREPLY=( $( compgen -W 'help \
1084 show list' -- "$cur" ) )
1089 local PROG_TYPE='id pinned tag name'
1090 local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
1093 [[ $prev != "$command" ]] && return 0
1094 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1100 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1104 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1110 _bpftool_get_prog_ids
1113 _bpftool_get_prog_names
1122 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1126 _bpftool_once_attr 'overwrite'
1134 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1138 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1144 [[ $prev == $object ]] && \
1145 COMPREPLY=( $( compgen -W 'help \
1146 show list attach detach' -- "$cur" ) )
1153 [[ $prev == "prefix" ]] && return 0
1154 if _bpftool_search_list 'macros'; then
1155 _bpftool_once_attr 'prefix'
1157 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1159 _bpftool_one_of_list 'kernel dev'
1160 _bpftool_once_attr 'full unprivileged'
1164 [[ $prev != "$command" ]] && return 0
1165 COMPREPLY=( $( compgen -W 'prog_types map_types \
1166 attach_types link_types helpers' -- "$cur" ) )
1169 [[ $prev == $object ]] && \
1170 COMPREPLY=( $( compgen -W 'help list_builtins probe' -- "$cur" ) )
1176 show|list|pin|detach)
1179 _bpftool_get_link_ids
1186 local LINK_TYPE='id pinned'
1189 [[ $prev != "$command" ]] && return 0
1190 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1194 if [[ $prev == "$command" ]]; then
1195 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1202 [[ $prev == $object ]] && \
1203 COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
1209 complete -F _bpftool bpftool
1211 # ex: ts=4 sw=4 et filetype=sh