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_prog_ids()
64 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
65 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
68 _bpftool_get_prog_tags()
70 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
71 command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
74 _bpftool_get_obj_map_names()
80 maps=$(objdump -j maps -t $obj 2>/dev/null | \
81 command awk '/g . maps/ {print $NF}')
83 COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
86 _bpftool_get_obj_map_idxs()
92 nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
94 COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
99 COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
103 # Retrieve type of the map that we are operating on.
104 _bpftool_map_guess_map_type()
107 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
108 case "${words[$((idx-2))]}" in
110 keyword=${words[$((idx-1))]}
111 ref=${words[$((idx))]}
123 [[ -z $ref ]] && return 0
126 type=$(bpftool -jp map show $keyword $ref | \
127 command sed -n 's/.*"type": "\(.*\)",$/\1/p')
128 [[ -n $type ]] && printf $type
131 _bpftool_map_update_get_id()
135 # Is it the map to update, or a map to insert into the map to update?
136 # Search for "value" keyword.
138 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
139 if [[ ${words[idx]} == "value" ]]; then
144 if [[ $value -eq 0 ]]; then
147 _bpftool_get_map_ids_for_type stack
150 _bpftool_get_map_ids_for_type queue
159 # Id to complete is for a value. It can be either prog id or map id. This
160 # depends on the type of the map to update.
161 local type=$(_bpftool_map_guess_map_type)
163 array_of_maps|hash_of_maps)
168 _bpftool_get_prog_ids
179 local cur prev words objword
180 _init_completion || return
183 if [[ ${words[cword]} == -* ]]; then
184 local c='--version --json --pretty --bpffs --mapcompat'
185 COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
189 # Deal with simplest keywords
191 help|hex|opcodes|visual|linum)
195 _bpftool_get_prog_tags
203 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
208 # Remove all options so completions don't have to deal with them.
210 for (( i=1; i < ${#words[@]}; )); do
211 if [[ ${words[i]::1} == - ]]; then
212 words=( "${words[@]:0:i}" "${words[@]:i+1}" )
213 [[ $i -le $cword ]] && cword=$(( cword - 1 ))
219 prev=${words[cword - 1]}
220 pprev=${words[cword - 2]}
222 local object=${words[1]} command=${words[2]}
224 if [[ -z $object || $cword -eq 1 ]]; then
227 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
229 -e '/OBJECT := /!d' \
232 -e 's/|//g' )" -- "$cur" ) )
233 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
239 [[ $command == help ]] && return 0
241 # Completion depends on object and command in use
244 # Complete id, only for subcommands that use prog (but no map) ids
249 _bpftool_get_prog_ids
256 local PROG_TYPE='id pinned tag'
257 local MAP_TYPE='id pinned'
260 [[ $prev != "$command" ]] && return 0
261 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
267 COMPREPLY+=( $( compgen -W "xlated jited" -- \
272 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
277 _bpftool_once_attr 'file'
278 if _bpftool_search_list 'xlated'; then
279 COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
282 COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
290 if [[ $prev == "$command" ]]; then
291 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
300 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
306 _bpftool_get_prog_ids
315 COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
316 stream_parser flow_dissector' -- "$cur" ) )
320 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
339 if [[ ${#words[@]} -lt 6 ]]; then
346 if [[ ${words[-4]} == "map" ]]; then
347 COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
350 if [[ ${words[-3]} == "map" ]]; then
351 if [[ ${words[-2]} == "idx" ]]; then
352 _bpftool_get_obj_map_idxs $obj
353 elif [[ ${words[-2]} == "name" ]]; then
354 _bpftool_get_obj_map_names $obj
358 if [[ ${words[-2]} == "map" ]]; then
359 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
365 COMPREPLY=( $( compgen -W "socket kprobe \
366 kretprobe classifier flow_dissector \
367 action tracepoint raw_tracepoint \
368 xdp perf_event cgroup/skb cgroup/sock \
369 cgroup/dev lwt_in lwt_out lwt_xmit \
370 lwt_seg6local sockops sk_skb sk_msg \
371 lirc_mode2 cgroup/bind4 cgroup/bind6 \
372 cgroup/connect4 cgroup/connect6 \
373 cgroup/sendmsg4 cgroup/sendmsg6 \
374 cgroup/post_bind4 cgroup/post_bind6 \
392 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
393 _bpftool_once_attr 'type'
394 _bpftool_once_attr 'dev'
395 _bpftool_once_attr 'pinmaps'
404 [[ $prev == $object ]] && \
405 COMPREPLY=( $( compgen -W 'dump help pin attach detach load \
406 show list tracelog' -- "$cur" ) )
411 local MAP_TYPE='id pinned'
413 show|list|dump|peek|pop|dequeue)
416 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
422 _bpftool_get_map_ids_for_type stack
423 _bpftool_get_map_ids_for_type queue
426 _bpftool_get_map_ids_for_type stack
429 _bpftool_get_map_ids_for_type queue
449 COMPREPLY=( $( compgen -W 'hash array prog_array \
450 perf_event_array percpu_hash percpu_array \
451 stack_trace cgroup_array lru_hash \
452 lru_percpu_hash lpm_trie array_of_maps \
453 hash_of_maps devmap sockmap cpumap xskmap \
454 sockhash cgroup_storage reuseport_sockarray \
455 percpu_cgroup_storage queue stack' -- \
459 key|value|flags|name|entries)
467 _bpftool_once_attr 'type'
468 _bpftool_once_attr 'key'
469 _bpftool_once_attr 'value'
470 _bpftool_once_attr 'entries'
471 _bpftool_once_attr 'name'
472 _bpftool_once_attr 'flags'
473 _bpftool_once_attr 'dev'
478 lookup|getnext|delete)
481 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
489 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
492 case $(_bpftool_map_guess_map_type) in
498 _bpftool_once_attr 'key'
506 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
510 _bpftool_map_update_get_id $command
514 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
517 # We can have bytes, or references to a prog or a
518 # map, depending on the type of the map to update.
519 case "$(_bpftool_map_guess_map_type)" in
520 array_of_maps|hash_of_maps)
521 local MAP_TYPE='id pinned'
522 COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
527 local PROG_TYPE='id pinned tag'
528 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
533 COMPREPLY+=( $( compgen -W 'hex' \
541 case $(_bpftool_map_guess_map_type) in
543 _bpftool_once_attr 'value'
548 _bpftool_once_attr 'key'
549 local UPDATE_FLAGS='any exist noexist'
550 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
551 if [[ ${words[idx]} == 'value' ]]; then
552 # 'value' is present, but is not the last
553 # word i.e. we can now have UPDATE_FLAGS.
554 _bpftool_one_of_list "$UPDATE_FLAGS"
558 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
559 if [[ ${words[idx]} == 'key' ]]; then
560 # 'key' is present, but is not the last
561 # word i.e. we can now have 'value'.
562 _bpftool_once_attr 'value'
572 if [[ $prev == "$command" ]]; then
573 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
582 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
586 _bpftool_get_map_ids_for_type perf_event_array
596 _bpftool_once_attr 'cpu'
597 _bpftool_once_attr 'index'
603 [[ $prev == $object ]] && \
604 COMPREPLY=( $( compgen -W 'delete dump getnext help \
605 lookup pin event_pipe show list update create \
606 peek push enqueue pop dequeue' -- \
612 local PROG_TYPE='id pinned tag'
613 local MAP_TYPE='id pinned'
618 COMPREPLY+=( $( compgen -W "id map prog file" -- \
623 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
627 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
633 _bpftool_get_prog_ids
642 if [[ $cword == 6 ]] && [[ ${words[3]} == "map" ]]; then
643 COMPREPLY+=( $( compgen -W 'key value kv all' -- \
651 [[ $prev == $object ]] && \
652 COMPREPLY=( $( compgen -W 'dump help' -- "$cur" ) )
667 local ATTACH_TYPES='ingress egress sock_create sock_ops \
668 device bind4 bind6 post_bind4 post_bind6 connect4 \
669 connect6 sendmsg4 sendmsg6 sysctl'
670 local ATTACH_FLAGS='multi override'
671 local PROG_TYPE='id pinned tag'
677 ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
678 post_bind4|post_bind6|connect4|connect6|sendmsg4|\
680 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
685 _bpftool_get_prog_ids
689 if ! _bpftool_search_list "$ATTACH_TYPES"; then
690 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
692 elif [[ "$command" == "attach" ]]; then
693 # We have an attach type on the command line,
694 # but it is not the previous word, or
695 # "id|pinned|tag" (we already checked for
696 # that). This should only leave the case when
697 # we need attach flags for "attach" commamnd.
698 _bpftool_one_of_list "$ATTACH_FLAGS"
705 [[ $prev == $object ]] && \
706 COMPREPLY=( $( compgen -W 'help attach detach \
707 show list tree' -- "$cur" ) )
714 [[ $prev == $object ]] && \
715 COMPREPLY=( $( compgen -W 'help \
716 show list' -- "$cur" ) )
723 [[ $prev == $object ]] && \
724 COMPREPLY=( $( compgen -W 'help \
725 show list' -- "$cur" ) )
732 [[ $prev == "dev" ]] && _sysfs_get_netdevs && return 0
733 [[ $prev == "prefix" ]] && return 0
734 if _bpftool_search_list 'macros'; then
735 COMPREPLY+=( $( compgen -W 'prefix' -- "$cur" ) )
737 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
739 _bpftool_one_of_list 'kernel dev'
743 [[ $prev == $object ]] && \
744 COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
750 complete -F _bpftool bpftool
752 # ex: ts=4 sw=4 et filetype=sh