Skip to content

Commit a827a16

Browse files
qmonnetborkmann
authored andcommitted
tools: bpftool: add bash completion for cgroup commands
Add bash completion for "bpftool cgroup" command family. While at it, also fix the formatting of some keywords in the man page for cgroups. Fixes: 5ccda64 ("bpftool: implement cgroup bpf operations") Signed-off-by: Quentin Monnet <[email protected]> Reviewed-by: Jakub Kicinski <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 55f3538 commit a827a16

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

tools/bpf/bpftool/Documentation/bpftool-cgroup.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ MAP COMMANDS
2626
| **bpftool** **cgroup help**
2727
|
2828
| *PROG* := { **id** *PROG_ID* | **pinned** *FILE* | **tag** *PROG_TAG* }
29-
| *ATTACH_TYPE* := { *ingress* | *egress* | *sock_create* | *sock_ops* | *device* }
30-
| *ATTACH_FLAGS* := { *multi* | *override* }
29+
| *ATTACH_TYPE* := { **ingress** | **egress** | **sock_create** | **sock_ops** | **device** }
30+
| *ATTACH_FLAGS* := { **multi** | **override** }
3131
3232
DESCRIPTION
3333
===========

tools/bpf/bpftool/bash-completion/bpftool

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,24 @@ _bpftool_once_attr()
5252
done
5353
}
5454

55-
# Takes a list of words in argument; adds them all to COMPREPLY if none of them
56-
# is already present on the command line. Returns no value.
57-
_bpftool_one_of_list()
55+
# Takes a list of words as argument; if any of those words is present on the
56+
# command line, return 0. Otherwise, return 1.
57+
_bpftool_search_list()
5858
{
5959
local w idx
6060
for w in $*; do
6161
for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
62-
[[ $w == ${words[idx]} ]] && return 1
62+
[[ $w == ${words[idx]} ]] && return 0
6363
done
6464
done
65+
return 1
66+
}
67+
68+
# Takes a list of words in argument; adds them all to COMPREPLY if none of them
69+
# is already present on the command line. Returns no value.
70+
_bpftool_one_of_list()
71+
{
72+
_bpftool_search_list $* && return 1
6573
COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
6674
}
6775

@@ -351,6 +359,54 @@ _bpftool()
351359
;;
352360
esac
353361
;;
362+
cgroup)
363+
case $command in
364+
show|list)
365+
_filedir
366+
return 0
367+
;;
368+
attach|detach)
369+
local ATTACH_TYPES='ingress egress sock_create sock_ops \
370+
device'
371+
local ATTACH_FLAGS='multi override'
372+
local PROG_TYPE='id pinned tag'
373+
case $prev in
374+
$command)
375+
_filedir
376+
return 0
377+
;;
378+
ingress|egress|sock_create|sock_ops|device)
379+
COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
380+
"$cur" ) )
381+
return 0
382+
;;
383+
id)
384+
_bpftool_get_prog_ids
385+
return 0
386+
;;
387+
*)
388+
if ! _bpftool_search_list "$ATTACH_TYPES"; then
389+
COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
390+
"$cur" ) )
391+
elif [[ "$command" == "attach" ]]; then
392+
# We have an attach type on the command line,
393+
# but it is not the previous word, or
394+
# "id|pinned|tag" (we already checked for
395+
# that). This should only leave the case when
396+
# we need attach flags for "attach" commamnd.
397+
_bpftool_one_of_list "$ATTACH_FLAGS"
398+
fi
399+
return 0
400+
;;
401+
esac
402+
;;
403+
*)
404+
[[ $prev == $object ]] && \
405+
COMPREPLY=( $( compgen -W 'help attach detach \
406+
show list' -- "$cur" ) )
407+
;;
408+
esac
409+
;;
354410
esac
355411
} &&
356412
complete -F _bpftool bpftool

0 commit comments

Comments
 (0)