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
259 _init_completion || return
262 if [[ ${words[cword]} == -* ]]; then
263 local c='--version --json --pretty --bpffs --mapcompat --debug'
264 COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
268 # Deal with simplest keywords
270 help|hex|opcodes|visual|linum)
274 _bpftool_get_prog_tags
286 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
291 # Remove all options so completions don't have to deal with them.
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 ))
302 prev=${words[cword - 1]}
303 pprev=${words[cword - 2]}
305 local object=${words[1]} command=${words[2]}
307 if [[ -z $object || $cword -eq 1 ]]; then
310 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
312 -e '/OBJECT := /!d' \
315 -e 's/|//g' )" -- "$cur" ) )
316 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
322 [[ $command == help ]] && return 0
324 # Completion depends on object and command in use
327 # Complete id and name, only for subcommands that use prog (but no
333 _bpftool_get_prog_ids
337 _bpftool_get_prog_names
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'
349 [[ $prev != "$command" ]] && return 0
350 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
356 COMPREPLY+=( $( compgen -W "xlated jited" -- \
361 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
366 _bpftool_once_attr 'file'
367 if _bpftool_search_list 'xlated'; then
368 COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
371 COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
379 if [[ $prev == "$command" ]]; then
380 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
389 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
395 _bpftool_get_prog_ids
398 _bpftool_get_prog_names
407 COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
408 stream_parser flow_dissector' -- "$cur" ) )
412 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
421 _bpftool_get_map_names
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" ) )
441 if [[ ${#words[@]} -lt 6 ]]; then
448 if [[ ${words[-4]} == "map" ]]; then
449 COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
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
460 if [[ ${words[-2]} == "map" ]]; then
461 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
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 cgroup/sock_release struct_ops \
482 fentry fexit freplace sk_lookup" -- \
491 _bpftool_get_map_names
499 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
500 _bpftool_once_attr 'type'
501 _bpftool_once_attr 'dev'
502 _bpftool_once_attr 'pinmaps'
513 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
519 _bpftool_get_prog_ids
522 _bpftool_get_prog_names
531 COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
540 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
547 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
553 if [[ ${#words[@]} -eq 4 ]]; then
554 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
559 _bpftool_get_prog_ids
563 _bpftool_get_prog_names
566 data_in|data_out|ctx_in|ctx_out)
570 repeat|data_size_out|ctx_size_out)
574 _bpftool_once_attr 'data_in data_out data_size_out \
575 ctx_in ctx_out ctx_size_out repeat'
581 [[ $prev == $object ]] && \
582 COMPREPLY=( $( compgen -W 'dump help pin attach detach \
583 load loadall show list tracelog run profile' -- "$cur" ) )
588 local STRUCT_OPS_TYPE='id name'
590 show|list|dump|unregister)
593 COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
596 _bpftool_get_map_ids_for_type struct_ops
599 _bpftool_get_map_names_for_type struct_ops
609 [[ $prev == $object ]] && \
610 COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
626 _bpftool_get_map_names
632 _bpftool_one_of_list $MAP_TYPE
638 [[ $prev == $object ]] && \
639 COMPREPLY=( $( compgen -W 'pin help' \
645 local MAP_TYPE='id pinned name'
647 show|list|dump|peek|pop|dequeue|freeze)
650 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
656 _bpftool_get_map_ids_for_type stack
657 _bpftool_get_map_ids_for_type queue
660 _bpftool_get_map_ids_for_type stack
663 _bpftool_get_map_ids_for_type queue
674 _bpftool_get_map_names_for_type stack
675 _bpftool_get_map_names_for_type queue
678 _bpftool_get_map_names_for_type stack
681 _bpftool_get_map_names_for_type queue
684 _bpftool_get_map_names
701 COMPREPLY=( $( compgen -W 'hash array prog_array \
702 perf_event_array percpu_hash percpu_array \
703 stack_trace cgroup_array lru_hash \
704 lru_percpu_hash lpm_trie array_of_maps \
705 hash_of_maps devmap devmap_hash sockmap cpumap \
706 xskmap sockhash cgroup_storage reuseport_sockarray \
707 percpu_cgroup_storage queue stack sk_storage \
708 struct_ops inode_storage task_storage' -- \
712 key|value|flags|entries)
716 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
725 _bpftool_get_map_names
733 _bpftool_once_attr 'type'
734 _bpftool_once_attr 'key'
735 _bpftool_once_attr 'value'
736 _bpftool_once_attr 'entries'
737 _bpftool_once_attr 'name'
738 _bpftool_once_attr 'flags'
739 if _bpftool_search_list 'array_of_maps' 'hash_of_maps'; then
740 _bpftool_once_attr 'inner_map'
742 _bpftool_once_attr 'dev'
747 lookup|getnext|delete)
750 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
758 _bpftool_get_map_names
762 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
765 case $(_bpftool_map_guess_map_type) in
771 _bpftool_once_attr 'key'
779 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
783 _bpftool_map_update_get_id $command
787 _bpftool_map_update_get_name $command
791 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
794 # We can have bytes, or references to a prog or a
795 # map, depending on the type of the map to update.
796 case "$(_bpftool_map_guess_map_type)" in
797 array_of_maps|hash_of_maps)
798 local MAP_TYPE='id pinned name'
799 COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
804 local PROG_TYPE='id pinned tag name'
805 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
810 COMPREPLY+=( $( compgen -W 'hex' \
818 case $(_bpftool_map_guess_map_type) in
820 _bpftool_once_attr 'value'
825 _bpftool_once_attr 'key'
826 local UPDATE_FLAGS='any exist noexist'
827 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
828 if [[ ${words[idx]} == 'value' ]]; then
829 # 'value' is present, but is not the last
830 # word i.e. we can now have UPDATE_FLAGS.
831 _bpftool_one_of_list "$UPDATE_FLAGS"
835 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
836 if [[ ${words[idx]} == 'key' ]]; then
837 # 'key' is present, but is not the last
838 # word i.e. we can now have 'value'.
839 _bpftool_once_attr 'value'
851 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
857 _bpftool_get_map_names
865 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
869 _bpftool_get_map_ids_for_type perf_event_array
873 _bpftool_get_map_names_for_type perf_event_array
883 _bpftool_once_attr 'cpu'
884 _bpftool_once_attr 'index'
890 [[ $prev == $object ]] && \
891 COMPREPLY=( $( compgen -W 'delete dump getnext help \
892 lookup pin event_pipe show list update create \
893 peek push enqueue pop dequeue freeze' -- \
899 local PROG_TYPE='id pinned tag name'
900 local MAP_TYPE='id pinned name'
905 COMPREPLY+=( $( compgen -W "id map prog file" -- \
910 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
914 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
920 _bpftool_get_prog_ids
934 _bpftool_get_prog_names
937 _bpftool_get_map_names
943 COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
949 _bpftool_once_attr 'format'
952 if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
953 COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
955 _bpftool_once_attr 'format'
967 COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
976 [[ $prev == $object ]] && \
977 COMPREPLY=( $( compgen -W 'dump help show list' \
995 _bpftool_once_attr 'name'
1001 [[ $prev == $object ]] && \
1002 COMPREPLY=( $( compgen -W 'object skeleton help' -- "$cur" ) )
1014 COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
1020 local ATTACH_TYPES='ingress egress sock_create sock_ops \
1021 device bind4 bind6 post_bind4 post_bind6 connect4 connect6 \
1022 getpeername4 getpeername6 getsockname4 getsockname6 \
1023 sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl getsockopt \
1024 setsockopt sock_release'
1025 local ATTACH_FLAGS='multi override'
1026 local PROG_TYPE='id pinned tag name'
1032 ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
1033 post_bind4|post_bind6|connect4|connect6|getpeername4|\
1034 getpeername6|getsockname4|getsockname6|sendmsg4|sendmsg6|\
1035 recvmsg4|recvmsg6|sysctl|getsockopt|setsockopt|sock_release)
1036 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
1041 _bpftool_get_prog_ids
1045 if ! _bpftool_search_list "$ATTACH_TYPES"; then
1046 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
1048 elif [[ "$command" == "attach" ]]; then
1049 # We have an attach type on the command line,
1050 # but it is not the previous word, or
1051 # "id|pinned|tag|name" (we already checked for
1052 # that). This should only leave the case when
1053 # we need attach flags for "attach" commamnd.
1054 _bpftool_one_of_list "$ATTACH_FLAGS"
1061 [[ $prev == $object ]] && \
1062 COMPREPLY=( $( compgen -W 'help attach detach \
1063 show list tree' -- "$cur" ) )
1070 [[ $prev == $object ]] && \
1071 COMPREPLY=( $( compgen -W 'help \
1072 show list' -- "$cur" ) )
1077 local PROG_TYPE='id pinned tag name'
1078 local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
1081 [[ $prev != "$command" ]] && return 0
1082 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1088 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1092 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1098 _bpftool_get_prog_ids
1101 _bpftool_get_prog_names
1110 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1114 _bpftool_once_attr 'overwrite'
1122 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1126 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1132 [[ $prev == $object ]] && \
1133 COMPREPLY=( $( compgen -W 'help \
1134 show list attach detach' -- "$cur" ) )
1141 [[ $prev == "prefix" ]] && return 0
1142 if _bpftool_search_list 'macros'; then
1143 _bpftool_once_attr 'prefix'
1145 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1147 _bpftool_one_of_list 'kernel dev'
1148 _bpftool_once_attr 'full unprivileged'
1152 [[ $prev == $object ]] && \
1153 COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
1159 show|list|pin|detach)
1162 _bpftool_get_link_ids
1169 local LINK_TYPE='id pinned'
1172 [[ $prev != "$command" ]] && return 0
1173 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1177 if [[ $prev == "$command" ]]; then
1178 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1185 [[ $prev == $object ]] && \
1186 COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
1192 complete -F _bpftool bpftool
1194 # ex: ts=4 sw=4 et filetype=sh