Commit 55c70bff authored by Stanislav Fomichev's avatar Stanislav Fomichev Committed by Daniel Borkmann

bpftool: add bash completion for peek/push/enqueue/pop/dequeue

bpftool map peek id <TAB>      - suggests only queue and stack map ids
bpftool map pop id <TAB>       - suggests only stack map ids
bpftool map dequeue id <TAB>   - suggests only queue map ids

bpftool map push id <TAB>      - suggests only stack map ids
bpftool map enqueue id <TAB>   - suggests only queue map ids

bpftool map push id 1 <TAB>    - suggests 'value', not 'key'
bpftool map enqueue id 2 <TAB> - suggests 'value', not 'key'

bpftool map update id <stack/queue type> - suggests 'value', not 'key'
bpftool map lookup id <stack/queue type> - suggests nothing
Signed-off-by: default avatarStanislav Fomichev <sdf@google.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 74f312ef
......@@ -50,14 +50,15 @@ _bpftool_get_map_ids()
command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
}
_bpftool_get_perf_map_ids()
# Takes map type and adds matching map ids to the list of suggestions.
_bpftool_get_map_ids_for_type()
{
local type="$1"
COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
command grep -C2 perf_event_array | \
command grep -C2 "$type" | \
command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
}
_bpftool_get_prog_ids()
{
COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
......@@ -99,15 +100,25 @@ _sysfs_get_netdevs()
"$cur" ) )
}
# For bpftool map update: retrieve type of the map to update.
_bpftool_map_update_map_type()
# Retrieve type of the map that we are operating on.
_bpftool_map_guess_map_type()
{
local keyword ref
for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
if [[ ${words[$((idx-2))]} == "update" ]]; then
case "${words[$((idx-2))]}" in
lookup|update)
keyword=${words[$((idx-1))]}
ref=${words[$((idx))]}
fi
;;
push)
printf "stack"
return 0
;;
enqueue)
printf "queue"
return 0
;;
esac
done
[[ -z $ref ]] && return 0
......@@ -119,6 +130,8 @@ _bpftool_map_update_map_type()
_bpftool_map_update_get_id()
{
local command="$1"
# Is it the map to update, or a map to insert into the map to update?
# Search for "value" keyword.
local idx value
......@@ -128,11 +141,24 @@ _bpftool_map_update_get_id()
break
fi
done
[[ $value -eq 0 ]] && _bpftool_get_map_ids && return 0
if [[ $value -eq 0 ]]; then
case "$command" in
push)
_bpftool_get_map_ids_for_type stack
;;
enqueue)
_bpftool_get_map_ids_for_type queue
;;
*)
_bpftool_get_map_ids
;;
esac
return 0
fi
# Id to complete is for a value. It can be either prog id or map id. This
# depends on the type of the map to update.
local type=$(_bpftool_map_update_map_type)
local type=$(_bpftool_map_guess_map_type)
case $type in
array_of_maps|hash_of_maps)
_bpftool_get_map_ids
......@@ -382,14 +408,28 @@ _bpftool()
map)
local MAP_TYPE='id pinned'
case $command in
show|list|dump)
show|list|dump|peek|pop|dequeue)
case $prev in
$command)
COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
return 0
;;
id)
case "$command" in
peek)
_bpftool_get_map_ids_for_type stack
_bpftool_get_map_ids_for_type queue
;;
pop)
_bpftool_get_map_ids_for_type stack
;;
dequeue)
_bpftool_get_map_ids_for_type queue
;;
*)
_bpftool_get_map_ids
;;
esac
return 0
;;
*)
......@@ -447,19 +487,25 @@ _bpftool()
COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
;;
*)
case $(_bpftool_map_guess_map_type) in
queue|stack)
return 0
;;
esac
_bpftool_once_attr 'key'
return 0
;;
esac
;;
update)
update|push|enqueue)
case $prev in
$command)
COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
return 0
;;
id)
_bpftool_map_update_get_id
_bpftool_map_update_get_id $command
return 0
;;
key)
......@@ -468,7 +514,7 @@ _bpftool()
value)
# We can have bytes, or references to a prog or a
# map, depending on the type of the map to update.
case $(_bpftool_map_update_map_type) in
case "$(_bpftool_map_guess_map_type)" in
array_of_maps|hash_of_maps)
local MAP_TYPE='id pinned'
COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
......@@ -490,6 +536,13 @@ _bpftool()
return 0
;;
*)
case $(_bpftool_map_guess_map_type) in
queue|stack)
_bpftool_once_attr 'value'
return 0;
;;
esac
_bpftool_once_attr 'key'
local UPDATE_FLAGS='any exist noexist'
for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
......@@ -508,6 +561,7 @@ _bpftool()
return 0
fi
done
return 0
;;
esac
......@@ -527,7 +581,7 @@ _bpftool()
return 0
;;
id)
_bpftool_get_perf_map_ids
_bpftool_get_map_ids_for_type perf_event_array
return 0
;;
cpu)
......@@ -546,7 +600,8 @@ _bpftool()
*)
[[ $prev == $object ]] && \
COMPREPLY=( $( compgen -W 'delete dump getnext help \
lookup pin event_pipe show list update create' -- \
lookup pin event_pipe show list update create \
peek push enqueue pop dequeue' -- \
"$cur" ) )
;;
esac
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment