Loading...
1# bpftool(8) bash completion -*- shell-script -*-
2#
3# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
4# Copyright (C) 2017-2018 Netronome Systems, Inc.
5#
6# Author: Quentin Monnet <quentin.monnet@netronome.com>
7
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.
10_bpftool_once_attr()
11{
12 local w idx found
13 for w in $*; do
14 found=0
15 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
16 if [[ $w == ${words[idx]} ]]; then
17 found=1
18 break
19 fi
20 done
21 [[ $found -eq 0 ]] && \
22 COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
23 done
24}
25
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()
29{
30 local w idx
31 for w in $*; do
32 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
33 [[ $w == ${words[idx]} ]] && return 0
34 done
35 done
36 return 1
37}
38
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()
42{
43 _bpftool_search_list $* && return 1
44 COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
45}
46
47_bpftool_get_map_ids()
48{
49 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
50 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
51}
52
53# Takes map type and adds matching map ids to the list of suggestions.
54_bpftool_get_map_ids_for_type()
55{
56 local type="$1"
57 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
58 command grep -C2 "$type" | \
59 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
60}
61
62_bpftool_get_map_names()
63{
64 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
65 command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
66}
67
68# Takes map type and adds matching map names to the list of suggestions.
69_bpftool_get_map_names_for_type()
70{
71 local type="$1"
72 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
73 command grep -C2 "$type" | \
74 command sed -n 's/.*"name": \(.*\),$/\1/p' )" -- "$cur" ) )
75}
76
77_bpftool_get_prog_ids()
78{
79 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
80 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
81}
82
83_bpftool_get_prog_tags()
84{
85 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
86 command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
87}
88
89_bpftool_get_prog_names()
90{
91 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
92 command sed -n 's/.*"name": "\(.*\)",$/\1/p' )" -- "$cur" ) )
93}
94
95_bpftool_get_btf_ids()
96{
97 COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
98 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
99}
100
101_bpftool_get_link_ids()
102{
103 COMPREPLY+=( $( compgen -W "$( bpftool -jp link 2>&1 | \
104 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
105}
106
107_bpftool_get_obj_map_names()
108{
109 local obj
110
111 obj=$1
112
113 maps=$(objdump -j maps -t $obj 2>/dev/null | \
114 command awk '/g . maps/ {print $NF}')
115
116 COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
117}
118
119_bpftool_get_obj_map_idxs()
120{
121 local obj
122
123 obj=$1
124
125 nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
126
127 COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
128}
129
130_sysfs_get_netdevs()
131{
132 COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
133 "$cur" ) )
134}
135
136# Retrieve type of the map that we are operating on.
137_bpftool_map_guess_map_type()
138{
139 local keyword ref
140 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
141 case "${words[$((idx-2))]}" in
142 lookup|update)
143 keyword=${words[$((idx-1))]}
144 ref=${words[$((idx))]}
145 ;;
146 push)
147 printf "stack"
148 return 0
149 ;;
150 enqueue)
151 printf "queue"
152 return 0
153 ;;
154 esac
155 done
156 [[ -z $ref ]] && return 0
157
158 local type
159 type=$(bpftool -jp map show $keyword $ref | \
160 command sed -n 's/.*"type": "\(.*\)",$/\1/p')
161 [[ -n $type ]] && printf $type
162}
163
164_bpftool_map_update_get_id()
165{
166 local command="$1"
167
168 # Is it the map to update, or a map to insert into the map to update?
169 # Search for "value" keyword.
170 local idx value
171 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
172 if [[ ${words[idx]} == "value" ]]; then
173 value=1
174 break
175 fi
176 done
177 if [[ $value -eq 0 ]]; then
178 case "$command" in
179 push)
180 _bpftool_get_map_ids_for_type stack
181 ;;
182 enqueue)
183 _bpftool_get_map_ids_for_type queue
184 ;;
185 *)
186 _bpftool_get_map_ids
187 ;;
188 esac
189 return 0
190 fi
191
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)
195 case $type in
196 array_of_maps|hash_of_maps)
197 _bpftool_get_map_ids
198 return 0
199 ;;
200 prog_array)
201 _bpftool_get_prog_ids
202 return 0
203 ;;
204 *)
205 return 0
206 ;;
207 esac
208}
209
210_bpftool_map_update_get_name()
211{
212 local command="$1"
213
214 # Is it the map to update, or a map to insert into the map to update?
215 # Search for "value" keyword.
216 local idx value
217 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
218 if [[ ${words[idx]} == "value" ]]; then
219 value=1
220 break
221 fi
222 done
223 if [[ $value -eq 0 ]]; then
224 case "$command" in
225 push)
226 _bpftool_get_map_names_for_type stack
227 ;;
228 enqueue)
229 _bpftool_get_map_names_for_type queue
230 ;;
231 *)
232 _bpftool_get_map_names
233 ;;
234 esac
235 return 0
236 fi
237
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)
241 case $type in
242 array_of_maps|hash_of_maps)
243 _bpftool_get_map_names
244 return 0
245 ;;
246 prog_array)
247 _bpftool_get_prog_names
248 return 0
249 ;;
250 *)
251 return 0
252 ;;
253 esac
254}
255
256_bpftool()
257{
258 local cur prev words objword json=0
259 _init_completion || return
260
261 # Deal with options
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" ) )
266 return 0
267 fi
268 if _bpftool_search_list -j --json -p --pretty; then
269 json=1
270 fi
271
272 # Deal with simplest keywords
273 case $prev in
274 help|hex)
275 return 0
276 ;;
277 tag)
278 _bpftool_get_prog_tags
279 return 0
280 ;;
281 dev|offload_dev|xdpmeta_dev)
282 _sysfs_get_netdevs
283 return 0
284 ;;
285 file|pinned|-B|--base-btf)
286 _filedir
287 return 0
288 ;;
289 batch)
290 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
291 return 0
292 ;;
293 esac
294
295 # Remove all options so completions don't have to deal with them.
296 local i
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 ))
302 else
303 i=$(( ++i ))
304 fi
305 done
306 cur=${words[cword]}
307 prev=${words[cword - 1]}
308 pprev=${words[cword - 2]}
309
310 local object=${words[1]} command=${words[2]}
311
312 if [[ -z $object || $cword -eq 1 ]]; then
313 case $cur in
314 *)
315 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
316 command sed \
317 -e '/OBJECT := /!d' \
318 -e 's/.*{//' \
319 -e 's/}.*//' \
320 -e 's/|//g' )" -- "$cur" ) )
321 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
322 return 0
323 ;;
324 esac
325 fi
326
327 [[ $command == help ]] && return 0
328
329 # Completion depends on object and command in use
330 case $object in
331 prog)
332 # Complete id and name, only for subcommands that use prog (but no
333 # map) ids/names.
334 case $command in
335 show|list|dump|pin)
336 case $prev in
337 id)
338 _bpftool_get_prog_ids
339 return 0
340 ;;
341 name)
342 _bpftool_get_prog_names
343 return 0
344 ;;
345 esac
346 ;;
347 esac
348
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'
353 case $command in
354 show|list)
355 [[ $prev != "$command" ]] && return 0
356 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
357 return 0
358 ;;
359 dump)
360 case $prev in
361 $command)
362 COMPREPLY+=( $( compgen -W "xlated jited" -- \
363 "$cur" ) )
364 return 0
365 ;;
366 xlated|jited)
367 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
368 "$cur" ) )
369 return 0
370 ;;
371 *)
372 # "file" is not compatible with other keywords here
373 if _bpftool_search_list 'file'; then
374 return 0
375 fi
376 if ! _bpftool_search_list 'linum opcodes visual'; then
377 _bpftool_once_attr 'file'
378 fi
379 _bpftool_once_attr 'linum opcodes'
380 if _bpftool_search_list 'xlated' && [[ "$json" == 0 ]]; then
381 _bpftool_once_attr 'visual'
382 fi
383 return 0
384 ;;
385 esac
386 ;;
387 pin)
388 if [[ $prev == "$command" ]]; then
389 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
390 else
391 _filedir
392 fi
393 return 0
394 ;;
395 attach|detach)
396 case $cword in
397 3)
398 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
399 return 0
400 ;;
401 4)
402 case $prev in
403 id)
404 _bpftool_get_prog_ids
405 ;;
406 name)
407 _bpftool_get_prog_names
408 ;;
409 pinned)
410 _filedir
411 ;;
412 esac
413 return 0
414 ;;
415 5)
416 local BPFTOOL_PROG_ATTACH_TYPES='sk_msg_verdict \
417 sk_skb_verdict sk_skb_stream_verdict sk_skb_stream_parser \
418 flow_dissector'
419 COMPREPLY=( $( compgen -W "$BPFTOOL_PROG_ATTACH_TYPES" -- "$cur" ) )
420 return 0
421 ;;
422 6)
423 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
424 return 0
425 ;;
426 7)
427 case $prev in
428 id)
429 _bpftool_get_map_ids
430 ;;
431 name)
432 _bpftool_get_map_names
433 ;;
434 pinned)
435 _filedir
436 ;;
437 esac
438 return 0
439 ;;
440 esac
441 ;;
442 load|loadall)
443 local obj
444
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" ) )
449 return 0
450 fi
451
452 if [[ ${#words[@]} -lt 6 ]]; then
453 _filedir
454 return 0
455 fi
456
457 obj=${words[3]}
458
459 if [[ ${words[-4]} == "map" ]]; then
460 COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
461 return 0
462 fi
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
468 fi
469 return 0
470 fi
471 if [[ ${words[-2]} == "map" ]]; then
472 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
473 return 0
474 fi
475
476 case $prev in
477 type)
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" ) )
495 return 0
496 ;;
497 id)
498 _bpftool_get_map_ids
499 return 0
500 ;;
501 name)
502 _bpftool_get_map_names
503 return 0
504 ;;
505 pinned|pinmaps)
506 _filedir
507 return 0
508 ;;
509 *)
510 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
511 _bpftool_once_attr 'type pinmaps autoattach'
512 _bpftool_one_of_list 'offload_dev xdpmeta_dev'
513 return 0
514 ;;
515 esac
516 ;;
517 tracelog)
518 return 0
519 ;;
520 profile)
521 case $cword in
522 3)
523 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
524 return 0
525 ;;
526 4)
527 case $prev in
528 id)
529 _bpftool_get_prog_ids
530 ;;
531 name)
532 _bpftool_get_prog_names
533 ;;
534 pinned)
535 _filedir
536 ;;
537 esac
538 return 0
539 ;;
540 5)
541 COMPREPLY=( $( compgen -W "$METRIC_TYPE duration" -- "$cur" ) )
542 return 0
543 ;;
544 6)
545 case $prev in
546 duration)
547 return 0
548 ;;
549 *)
550 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
551 return 0
552 ;;
553 esac
554 return 0
555 ;;
556 *)
557 COMPREPLY=( $( compgen -W "$METRIC_TYPE" -- "$cur" ) )
558 return 0
559 ;;
560 esac
561 ;;
562 run)
563 if [[ ${#words[@]} -eq 4 ]]; then
564 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
565 return 0
566 fi
567 case $prev in
568 id)
569 _bpftool_get_prog_ids
570 return 0
571 ;;
572 name)
573 _bpftool_get_prog_names
574 return 0
575 ;;
576 data_in|data_out|ctx_in|ctx_out)
577 _filedir
578 return 0
579 ;;
580 repeat|data_size_out|ctx_size_out)
581 return 0
582 ;;
583 *)
584 _bpftool_once_attr 'data_in data_out data_size_out \
585 ctx_in ctx_out ctx_size_out repeat'
586 return 0
587 ;;
588 esac
589 ;;
590 *)
591 [[ $prev == $object ]] && \
592 COMPREPLY=( $( compgen -W 'dump help pin attach detach \
593 load loadall show list tracelog run profile' -- "$cur" ) )
594 ;;
595 esac
596 ;;
597 struct_ops)
598 local STRUCT_OPS_TYPE='id name'
599 case $command in
600 show|list|dump|unregister)
601 case $prev in
602 $command)
603 COMPREPLY=( $( compgen -W "$STRUCT_OPS_TYPE" -- "$cur" ) )
604 ;;
605 id)
606 _bpftool_get_map_ids_for_type struct_ops
607 ;;
608 name)
609 _bpftool_get_map_names_for_type struct_ops
610 ;;
611 esac
612 return 0
613 ;;
614 register)
615 _filedir
616 return 0
617 ;;
618 *)
619 [[ $prev == $object ]] && \
620 COMPREPLY=( $( compgen -W 'register unregister show list dump help' \
621 -- "$cur" ) )
622 ;;
623 esac
624 ;;
625 iter)
626 case $command in
627 pin)
628 case $prev in
629 $command)
630 _filedir
631 ;;
632 id)
633 _bpftool_get_map_ids
634 ;;
635 name)
636 _bpftool_get_map_names
637 ;;
638 pinned)
639 _filedir
640 ;;
641 *)
642 _bpftool_one_of_list $MAP_TYPE
643 ;;
644 esac
645 return 0
646 ;;
647 *)
648 [[ $prev == $object ]] && \
649 COMPREPLY=( $( compgen -W 'pin help' \
650 -- "$cur" ) )
651 ;;
652 esac
653 ;;
654 map)
655 local MAP_TYPE='id pinned name'
656 case $command in
657 show|list|dump|peek|pop|dequeue|freeze)
658 case $prev in
659 $command)
660 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
661 return 0
662 ;;
663 id)
664 case "$command" in
665 peek)
666 _bpftool_get_map_ids_for_type stack
667 _bpftool_get_map_ids_for_type queue
668 ;;
669 pop)
670 _bpftool_get_map_ids_for_type stack
671 ;;
672 dequeue)
673 _bpftool_get_map_ids_for_type queue
674 ;;
675 *)
676 _bpftool_get_map_ids
677 ;;
678 esac
679 return 0
680 ;;
681 name)
682 case "$command" in
683 peek)
684 _bpftool_get_map_names_for_type stack
685 _bpftool_get_map_names_for_type queue
686 ;;
687 pop)
688 _bpftool_get_map_names_for_type stack
689 ;;
690 dequeue)
691 _bpftool_get_map_names_for_type queue
692 ;;
693 *)
694 _bpftool_get_map_names
695 ;;
696 esac
697 return 0
698 ;;
699 *)
700 return 0
701 ;;
702 esac
703 ;;
704 create)
705 case $prev in
706 $command)
707 _filedir
708 return 0
709 ;;
710 type)
711 local BPFTOOL_MAP_CREATE_TYPES="$(bpftool feature list_builtins map_types 2>/dev/null | \
712 grep -v '^unspec$')"
713 COMPREPLY=( $( compgen -W "$BPFTOOL_MAP_CREATE_TYPES" -- "$cur" ) )
714 return 0
715 ;;
716 key|value|flags|entries)
717 return 0
718 ;;
719 inner_map)
720 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
721 return 0
722 ;;
723 id)
724 _bpftool_get_map_ids
725 ;;
726 name)
727 case $pprev in
728 inner_map)
729 _bpftool_get_map_names
730 ;;
731 *)
732 return 0
733 ;;
734 esac
735 ;;
736 *)
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'
740 fi
741 return 0
742 ;;
743 esac
744 ;;
745 lookup|getnext|delete)
746 case $prev in
747 $command)
748 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
749 return 0
750 ;;
751 id)
752 _bpftool_get_map_ids
753 return 0
754 ;;
755 name)
756 _bpftool_get_map_names
757 return 0
758 ;;
759 key)
760 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
761 ;;
762 *)
763 case $(_bpftool_map_guess_map_type) in
764 queue|stack)
765 return 0
766 ;;
767 esac
768
769 _bpftool_once_attr 'key'
770 return 0
771 ;;
772 esac
773 ;;
774 update|push|enqueue)
775 case $prev in
776 $command)
777 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
778 return 0
779 ;;
780 id)
781 _bpftool_map_update_get_id $command
782 return 0
783 ;;
784 name)
785 _bpftool_map_update_get_name $command
786 return 0
787 ;;
788 key)
789 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
790 ;;
791 value)
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" \
798 -- "$cur" ) )
799 return 0
800 ;;
801 prog_array)
802 local PROG_TYPE='id pinned tag name'
803 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
804 -- "$cur" ) )
805 return 0
806 ;;
807 *)
808 COMPREPLY+=( $( compgen -W 'hex' \
809 -- "$cur" ) )
810 return 0
811 ;;
812 esac
813 return 0
814 ;;
815 *)
816 case $(_bpftool_map_guess_map_type) in
817 queue|stack)
818 _bpftool_once_attr 'value'
819 return 0;
820 ;;
821 esac
822
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"
830 return 0
831 fi
832 done
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'
838 return 0
839 fi
840 done
841
842 return 0
843 ;;
844 esac
845 ;;
846 pin)
847 case $prev in
848 $command)
849 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
850 ;;
851 id)
852 _bpftool_get_map_ids
853 ;;
854 name)
855 _bpftool_get_map_names
856 ;;
857 esac
858 return 0
859 ;;
860 event_pipe)
861 case $prev in
862 $command)
863 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
864 return 0
865 ;;
866 id)
867 _bpftool_get_map_ids_for_type perf_event_array
868 return 0
869 ;;
870 name)
871 _bpftool_get_map_names_for_type perf_event_array
872 return 0
873 ;;
874 cpu)
875 return 0
876 ;;
877 index)
878 return 0
879 ;;
880 *)
881 _bpftool_once_attr 'cpu index'
882 return 0
883 ;;
884 esac
885 ;;
886 *)
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' -- \
891 "$cur" ) )
892 ;;
893 esac
894 ;;
895 btf)
896 local PROG_TYPE='id pinned tag name'
897 local MAP_TYPE='id pinned name'
898 case $command in
899 dump)
900 case $prev in
901 $command)
902 COMPREPLY+=( $( compgen -W "id map prog file" -- \
903 "$cur" ) )
904 return 0
905 ;;
906 prog)
907 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
908 return 0
909 ;;
910 map)
911 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
912 return 0
913 ;;
914 id)
915 case $pprev in
916 prog)
917 _bpftool_get_prog_ids
918 ;;
919 map)
920 _bpftool_get_map_ids
921 ;;
922 $command)
923 _bpftool_get_btf_ids
924 ;;
925 esac
926 return 0
927 ;;
928 name)
929 case $pprev in
930 prog)
931 _bpftool_get_prog_names
932 ;;
933 map)
934 _bpftool_get_map_names
935 ;;
936 esac
937 return 0
938 ;;
939 format)
940 COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
941 ;;
942 *)
943 # emit extra options
944 case ${words[3]} in
945 id|file)
946 _bpftool_once_attr 'format'
947 ;;
948 map|prog)
949 if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
950 COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
951 fi
952 _bpftool_once_attr 'format'
953 ;;
954 *)
955 ;;
956 esac
957 return 0
958 ;;
959 esac
960 ;;
961 show|list)
962 case $prev in
963 $command)
964 COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
965 ;;
966 id)
967 _bpftool_get_btf_ids
968 ;;
969 esac
970 return 0
971 ;;
972 *)
973 [[ $prev == $object ]] && \
974 COMPREPLY=( $( compgen -W 'dump help show list' \
975 -- "$cur" ) )
976 ;;
977 esac
978 ;;
979 gen)
980 case $command in
981 object)
982 _filedir
983 return 0
984 ;;
985 skeleton)
986 case $prev in
987 $command)
988 _filedir
989 return 0
990 ;;
991 *)
992 _bpftool_once_attr 'name'
993 return 0
994 ;;
995 esac
996 ;;
997 subskeleton)
998 case $prev in
999 $command)
1000 _filedir
1001 return 0
1002 ;;
1003 *)
1004 _bpftool_once_attr 'name'
1005 return 0
1006 ;;
1007 esac
1008 ;;
1009 min_core_btf)
1010 _filedir
1011 return 0
1012 ;;
1013 *)
1014 [[ $prev == $object ]] && \
1015 COMPREPLY=( $( compgen -W 'object skeleton subskeleton help min_core_btf' -- "$cur" ) )
1016 ;;
1017 esac
1018 ;;
1019 cgroup)
1020 case $command in
1021 show|list|tree)
1022 case $cword in
1023 3)
1024 _filedir
1025 ;;
1026 4)
1027 COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
1028 ;;
1029 esac
1030 return 0
1031 ;;
1032 attach|detach)
1033 local BPFTOOL_CGROUP_ATTACH_TYPES="$(bpftool feature list_builtins attach_types 2>/dev/null | \
1034 grep '^cgroup_')"
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
1039 _filedir
1040 return 0
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
1044 # our variable).
1045 elif [[ $BPFTOOL_CGROUP_ATTACH_TYPES =~ $prev ]]; then
1046 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
1047 "$cur" ) )
1048 return 0
1049 fi
1050 # case/esac for the other cases
1051 case $prev in
1052 id)
1053 _bpftool_get_prog_ids
1054 return 0
1055 ;;
1056 *)
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"
1067 fi
1068 return 0
1069 ;;
1070 esac
1071 ;;
1072 *)
1073 [[ $prev == $object ]] && \
1074 COMPREPLY=( $( compgen -W 'help attach detach \
1075 show list tree' -- "$cur" ) )
1076 ;;
1077 esac
1078 ;;
1079 perf)
1080 case $command in
1081 *)
1082 [[ $prev == $object ]] && \
1083 COMPREPLY=( $( compgen -W 'help \
1084 show list' -- "$cur" ) )
1085 ;;
1086 esac
1087 ;;
1088 net)
1089 local PROG_TYPE='id pinned tag name'
1090 local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
1091 case $command in
1092 show|list)
1093 [[ $prev != "$command" ]] && return 0
1094 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1095 return 0
1096 ;;
1097 attach)
1098 case $cword in
1099 3)
1100 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1101 return 0
1102 ;;
1103 4)
1104 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
1105 return 0
1106 ;;
1107 5)
1108 case $prev in
1109 id)
1110 _bpftool_get_prog_ids
1111 ;;
1112 name)
1113 _bpftool_get_prog_names
1114 ;;
1115 pinned)
1116 _filedir
1117 ;;
1118 esac
1119 return 0
1120 ;;
1121 6)
1122 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1123 return 0
1124 ;;
1125 8)
1126 _bpftool_once_attr 'overwrite'
1127 return 0
1128 ;;
1129 esac
1130 ;;
1131 detach)
1132 case $cword in
1133 3)
1134 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
1135 return 0
1136 ;;
1137 4)
1138 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
1139 return 0
1140 ;;
1141 esac
1142 ;;
1143 *)
1144 [[ $prev == $object ]] && \
1145 COMPREPLY=( $( compgen -W 'help \
1146 show list attach detach' -- "$cur" ) )
1147 ;;
1148 esac
1149 ;;
1150 feature)
1151 case $command in
1152 probe)
1153 [[ $prev == "prefix" ]] && return 0
1154 if _bpftool_search_list 'macros'; then
1155 _bpftool_once_attr 'prefix'
1156 else
1157 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
1158 fi
1159 _bpftool_one_of_list 'kernel dev'
1160 _bpftool_once_attr 'full unprivileged'
1161 return 0
1162 ;;
1163 list_builtins)
1164 [[ $prev != "$command" ]] && return 0
1165 COMPREPLY=( $( compgen -W 'prog_types map_types \
1166 attach_types link_types helpers' -- "$cur" ) )
1167 ;;
1168 *)
1169 [[ $prev == $object ]] && \
1170 COMPREPLY=( $( compgen -W 'help list_builtins probe' -- "$cur" ) )
1171 ;;
1172 esac
1173 ;;
1174 link)
1175 case $command in
1176 show|list|pin|detach)
1177 case $prev in
1178 id)
1179 _bpftool_get_link_ids
1180 return 0
1181 ;;
1182 esac
1183 ;;
1184 esac
1185
1186 local LINK_TYPE='id pinned'
1187 case $command in
1188 show|list)
1189 [[ $prev != "$command" ]] && return 0
1190 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1191 return 0
1192 ;;
1193 pin|detach)
1194 if [[ $prev == "$command" ]]; then
1195 COMPREPLY=( $( compgen -W "$LINK_TYPE" -- "$cur" ) )
1196 else
1197 _filedir
1198 fi
1199 return 0
1200 ;;
1201 *)
1202 [[ $prev == $object ]] && \
1203 COMPREPLY=( $( compgen -W 'help pin show list' -- "$cur" ) )
1204 ;;
1205 esac
1206 ;;
1207 esac
1208} &&
1209complete -F _bpftool bpftool
1210
1211# ex: ts=4 sw=4 et filetype=sh
1# bpftool(8) bash completion -*- shell-script -*-
2#
3# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
4# Copyright (C) 2017-2018 Netronome Systems, Inc.
5#
6# Author: Quentin Monnet <quentin.monnet@netronome.com>
7
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.
10_bpftool_once_attr()
11{
12 local w idx found
13 for w in $*; do
14 found=0
15 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
16 if [[ $w == ${words[idx]} ]]; then
17 found=1
18 break
19 fi
20 done
21 [[ $found -eq 0 ]] && \
22 COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
23 done
24}
25
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()
29{
30 local w idx
31 for w in $*; do
32 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
33 [[ $w == ${words[idx]} ]] && return 0
34 done
35 done
36 return 1
37}
38
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()
42{
43 _bpftool_search_list $* && return 1
44 COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
45}
46
47_bpftool_get_map_ids()
48{
49 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
50 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
51}
52
53# Takes map type and adds matching map ids to the list of suggestions.
54_bpftool_get_map_ids_for_type()
55{
56 local type="$1"
57 COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
58 command grep -C2 "$type" | \
59 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
60}
61
62_bpftool_get_prog_ids()
63{
64 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
65 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
66}
67
68_bpftool_get_prog_tags()
69{
70 COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
71 command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
72}
73
74_bpftool_get_btf_ids()
75{
76 COMPREPLY+=( $( compgen -W "$( bpftool -jp btf 2>&1 | \
77 command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
78}
79
80_bpftool_get_obj_map_names()
81{
82 local obj
83
84 obj=$1
85
86 maps=$(objdump -j maps -t $obj 2>/dev/null | \
87 command awk '/g . maps/ {print $NF}')
88
89 COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
90}
91
92_bpftool_get_obj_map_idxs()
93{
94 local obj
95
96 obj=$1
97
98 nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
99
100 COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
101}
102
103_sysfs_get_netdevs()
104{
105 COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
106 "$cur" ) )
107}
108
109# Retrieve type of the map that we are operating on.
110_bpftool_map_guess_map_type()
111{
112 local keyword ref
113 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
114 case "${words[$((idx-2))]}" in
115 lookup|update)
116 keyword=${words[$((idx-1))]}
117 ref=${words[$((idx))]}
118 ;;
119 push)
120 printf "stack"
121 return 0
122 ;;
123 enqueue)
124 printf "queue"
125 return 0
126 ;;
127 esac
128 done
129 [[ -z $ref ]] && return 0
130
131 local type
132 type=$(bpftool -jp map show $keyword $ref | \
133 command sed -n 's/.*"type": "\(.*\)",$/\1/p')
134 [[ -n $type ]] && printf $type
135}
136
137_bpftool_map_update_get_id()
138{
139 local command="$1"
140
141 # Is it the map to update, or a map to insert into the map to update?
142 # Search for "value" keyword.
143 local idx value
144 for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
145 if [[ ${words[idx]} == "value" ]]; then
146 value=1
147 break
148 fi
149 done
150 if [[ $value -eq 0 ]]; then
151 case "$command" in
152 push)
153 _bpftool_get_map_ids_for_type stack
154 ;;
155 enqueue)
156 _bpftool_get_map_ids_for_type queue
157 ;;
158 *)
159 _bpftool_get_map_ids
160 ;;
161 esac
162 return 0
163 fi
164
165 # Id to complete is for a value. It can be either prog id or map id. This
166 # depends on the type of the map to update.
167 local type=$(_bpftool_map_guess_map_type)
168 case $type in
169 array_of_maps|hash_of_maps)
170 _bpftool_get_map_ids
171 return 0
172 ;;
173 prog_array)
174 _bpftool_get_prog_ids
175 return 0
176 ;;
177 *)
178 return 0
179 ;;
180 esac
181}
182
183_bpftool()
184{
185 local cur prev words objword
186 _init_completion || return
187
188 # Deal with options
189 if [[ ${words[cword]} == -* ]]; then
190 local c='--version --json --pretty --bpffs --mapcompat --debug'
191 COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
192 return 0
193 fi
194
195 # Deal with simplest keywords
196 case $prev in
197 help|hex|opcodes|visual|linum)
198 return 0
199 ;;
200 tag)
201 _bpftool_get_prog_tags
202 return 0
203 ;;
204 dev)
205 _sysfs_get_netdevs
206 return 0
207 ;;
208 file|pinned)
209 _filedir
210 return 0
211 ;;
212 batch)
213 COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
214 return 0
215 ;;
216 esac
217
218 # Remove all options so completions don't have to deal with them.
219 local i
220 for (( i=1; i < ${#words[@]}; )); do
221 if [[ ${words[i]::1} == - ]]; then
222 words=( "${words[@]:0:i}" "${words[@]:i+1}" )
223 [[ $i -le $cword ]] && cword=$(( cword - 1 ))
224 else
225 i=$(( ++i ))
226 fi
227 done
228 cur=${words[cword]}
229 prev=${words[cword - 1]}
230 pprev=${words[cword - 2]}
231
232 local object=${words[1]} command=${words[2]}
233
234 if [[ -z $object || $cword -eq 1 ]]; then
235 case $cur in
236 *)
237 COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
238 command sed \
239 -e '/OBJECT := /!d' \
240 -e 's/.*{//' \
241 -e 's/}.*//' \
242 -e 's/|//g' )" -- "$cur" ) )
243 COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
244 return 0
245 ;;
246 esac
247 fi
248
249 [[ $command == help ]] && return 0
250
251 # Completion depends on object and command in use
252 case $object in
253 prog)
254 # Complete id, only for subcommands that use prog (but no map) ids
255 case $command in
256 show|list|dump|pin)
257 case $prev in
258 id)
259 _bpftool_get_prog_ids
260 return 0
261 ;;
262 esac
263 ;;
264 esac
265
266 local PROG_TYPE='id pinned tag'
267 local MAP_TYPE='id pinned'
268 case $command in
269 show|list)
270 [[ $prev != "$command" ]] && return 0
271 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
272 return 0
273 ;;
274 dump)
275 case $prev in
276 $command)
277 COMPREPLY+=( $( compgen -W "xlated jited" -- \
278 "$cur" ) )
279 return 0
280 ;;
281 xlated|jited)
282 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
283 "$cur" ) )
284 return 0
285 ;;
286 *)
287 _bpftool_once_attr 'file'
288 if _bpftool_search_list 'xlated'; then
289 COMPREPLY+=( $( compgen -W 'opcodes visual linum' -- \
290 "$cur" ) )
291 else
292 COMPREPLY+=( $( compgen -W 'opcodes linum' -- \
293 "$cur" ) )
294 fi
295 return 0
296 ;;
297 esac
298 ;;
299 pin)
300 if [[ $prev == "$command" ]]; then
301 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
302 else
303 _filedir
304 fi
305 return 0
306 ;;
307 attach|detach)
308 case $cword in
309 3)
310 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
311 return 0
312 ;;
313 4)
314 case $prev in
315 id)
316 _bpftool_get_prog_ids
317 ;;
318 pinned)
319 _filedir
320 ;;
321 esac
322 return 0
323 ;;
324 5)
325 COMPREPLY=( $( compgen -W 'msg_verdict stream_verdict \
326 stream_parser flow_dissector' -- "$cur" ) )
327 return 0
328 ;;
329 6)
330 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
331 return 0
332 ;;
333 7)
334 case $prev in
335 id)
336 _bpftool_get_map_ids
337 ;;
338 pinned)
339 _filedir
340 ;;
341 esac
342 return 0
343 ;;
344 esac
345 ;;
346 load|loadall)
347 local obj
348
349 # Propose "load/loadall" to complete "bpftool prog load",
350 # or bash tries to complete "load" as a filename below.
351 if [[ ${#words[@]} -eq 3 ]]; then
352 COMPREPLY=( $( compgen -W "load loadall" -- "$cur" ) )
353 return 0
354 fi
355
356 if [[ ${#words[@]} -lt 6 ]]; then
357 _filedir
358 return 0
359 fi
360
361 obj=${words[3]}
362
363 if [[ ${words[-4]} == "map" ]]; then
364 COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
365 return 0
366 fi
367 if [[ ${words[-3]} == "map" ]]; then
368 if [[ ${words[-2]} == "idx" ]]; then
369 _bpftool_get_obj_map_idxs $obj
370 elif [[ ${words[-2]} == "name" ]]; then
371 _bpftool_get_obj_map_names $obj
372 fi
373 return 0
374 fi
375 if [[ ${words[-2]} == "map" ]]; then
376 COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
377 return 0
378 fi
379
380 case $prev in
381 type)
382 COMPREPLY=( $( compgen -W "socket kprobe \
383 kretprobe classifier flow_dissector \
384 action tracepoint raw_tracepoint \
385 xdp perf_event cgroup/skb cgroup/sock \
386 cgroup/dev lwt_in lwt_out lwt_xmit \
387 lwt_seg6local sockops sk_skb sk_msg \
388 lirc_mode2 cgroup/bind4 cgroup/bind6 \
389 cgroup/connect4 cgroup/connect6 \
390 cgroup/sendmsg4 cgroup/sendmsg6 \
391 cgroup/recvmsg4 cgroup/recvmsg6 \
392 cgroup/post_bind4 cgroup/post_bind6 \
393 cgroup/sysctl cgroup/getsockopt \
394 cgroup/setsockopt" -- \
395 "$cur" ) )
396 return 0
397 ;;
398 id)
399 _bpftool_get_map_ids
400 return 0
401 ;;
402 pinned|pinmaps)
403 _filedir
404 return 0
405 ;;
406 *)
407 COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
408 _bpftool_once_attr 'type'
409 _bpftool_once_attr 'dev'
410 _bpftool_once_attr 'pinmaps'
411 return 0
412 ;;
413 esac
414 ;;
415 tracelog)
416 return 0
417 ;;
418 run)
419 if [[ ${#words[@]} -lt 5 ]]; then
420 _filedir
421 return 0
422 fi
423 case $prev in
424 id)
425 _bpftool_get_prog_ids
426 return 0
427 ;;
428 data_in|data_out|ctx_in|ctx_out)
429 _filedir
430 return 0
431 ;;
432 repeat|data_size_out|ctx_size_out)
433 return 0
434 ;;
435 *)
436 _bpftool_once_attr 'data_in data_out data_size_out \
437 ctx_in ctx_out ctx_size_out repeat'
438 return 0
439 ;;
440 esac
441 ;;
442 *)
443 [[ $prev == $object ]] && \
444 COMPREPLY=( $( compgen -W 'dump help pin attach detach \
445 load loadall show list tracelog run' -- "$cur" ) )
446 ;;
447 esac
448 ;;
449 map)
450 local MAP_TYPE='id pinned'
451 case $command in
452 show|list|dump|peek|pop|dequeue|freeze)
453 case $prev in
454 $command)
455 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
456 return 0
457 ;;
458 id)
459 case "$command" in
460 peek)
461 _bpftool_get_map_ids_for_type stack
462 _bpftool_get_map_ids_for_type queue
463 ;;
464 pop)
465 _bpftool_get_map_ids_for_type stack
466 ;;
467 dequeue)
468 _bpftool_get_map_ids_for_type queue
469 ;;
470 *)
471 _bpftool_get_map_ids
472 ;;
473 esac
474 return 0
475 ;;
476 *)
477 return 0
478 ;;
479 esac
480 ;;
481 create)
482 case $prev in
483 $command)
484 _filedir
485 return 0
486 ;;
487 type)
488 COMPREPLY=( $( compgen -W 'hash array prog_array \
489 perf_event_array percpu_hash percpu_array \
490 stack_trace cgroup_array lru_hash \
491 lru_percpu_hash lpm_trie array_of_maps \
492 hash_of_maps devmap devmap_hash sockmap cpumap \
493 xskmap sockhash cgroup_storage reuseport_sockarray \
494 percpu_cgroup_storage queue stack' -- \
495 "$cur" ) )
496 return 0
497 ;;
498 key|value|flags|name|entries)
499 return 0
500 ;;
501 *)
502 _bpftool_once_attr 'type'
503 _bpftool_once_attr 'key'
504 _bpftool_once_attr 'value'
505 _bpftool_once_attr 'entries'
506 _bpftool_once_attr 'name'
507 _bpftool_once_attr 'flags'
508 _bpftool_once_attr 'dev'
509 return 0
510 ;;
511 esac
512 ;;
513 lookup|getnext|delete)
514 case $prev in
515 $command)
516 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
517 return 0
518 ;;
519 id)
520 _bpftool_get_map_ids
521 return 0
522 ;;
523 key)
524 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
525 ;;
526 *)
527 case $(_bpftool_map_guess_map_type) in
528 queue|stack)
529 return 0
530 ;;
531 esac
532
533 _bpftool_once_attr 'key'
534 return 0
535 ;;
536 esac
537 ;;
538 update|push|enqueue)
539 case $prev in
540 $command)
541 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
542 return 0
543 ;;
544 id)
545 _bpftool_map_update_get_id $command
546 return 0
547 ;;
548 key)
549 COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
550 ;;
551 value)
552 # We can have bytes, or references to a prog or a
553 # map, depending on the type of the map to update.
554 case "$(_bpftool_map_guess_map_type)" in
555 array_of_maps|hash_of_maps)
556 local MAP_TYPE='id pinned'
557 COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
558 -- "$cur" ) )
559 return 0
560 ;;
561 prog_array)
562 local PROG_TYPE='id pinned tag'
563 COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
564 -- "$cur" ) )
565 return 0
566 ;;
567 *)
568 COMPREPLY+=( $( compgen -W 'hex' \
569 -- "$cur" ) )
570 return 0
571 ;;
572 esac
573 return 0
574 ;;
575 *)
576 case $(_bpftool_map_guess_map_type) in
577 queue|stack)
578 _bpftool_once_attr 'value'
579 return 0;
580 ;;
581 esac
582
583 _bpftool_once_attr 'key'
584 local UPDATE_FLAGS='any exist noexist'
585 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
586 if [[ ${words[idx]} == 'value' ]]; then
587 # 'value' is present, but is not the last
588 # word i.e. we can now have UPDATE_FLAGS.
589 _bpftool_one_of_list "$UPDATE_FLAGS"
590 return 0
591 fi
592 done
593 for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
594 if [[ ${words[idx]} == 'key' ]]; then
595 # 'key' is present, but is not the last
596 # word i.e. we can now have 'value'.
597 _bpftool_once_attr 'value'
598 return 0
599 fi
600 done
601
602 return 0
603 ;;
604 esac
605 ;;
606 pin)
607 if [[ $prev == "$command" ]]; then
608 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
609 else
610 _filedir
611 fi
612 return 0
613 ;;
614 event_pipe)
615 case $prev in
616 $command)
617 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
618 return 0
619 ;;
620 id)
621 _bpftool_get_map_ids_for_type perf_event_array
622 return 0
623 ;;
624 cpu)
625 return 0
626 ;;
627 index)
628 return 0
629 ;;
630 *)
631 _bpftool_once_attr 'cpu'
632 _bpftool_once_attr 'index'
633 return 0
634 ;;
635 esac
636 ;;
637 *)
638 [[ $prev == $object ]] && \
639 COMPREPLY=( $( compgen -W 'delete dump getnext help \
640 lookup pin event_pipe show list update create \
641 peek push enqueue pop dequeue freeze' -- \
642 "$cur" ) )
643 ;;
644 esac
645 ;;
646 btf)
647 local PROG_TYPE='id pinned tag'
648 local MAP_TYPE='id pinned'
649 case $command in
650 dump)
651 case $prev in
652 $command)
653 COMPREPLY+=( $( compgen -W "id map prog file" -- \
654 "$cur" ) )
655 return 0
656 ;;
657 prog)
658 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
659 return 0
660 ;;
661 map)
662 COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
663 return 0
664 ;;
665 id)
666 case $pprev in
667 prog)
668 _bpftool_get_prog_ids
669 ;;
670 map)
671 _bpftool_get_map_ids
672 ;;
673 $command)
674 _bpftool_get_btf_ids
675 ;;
676 esac
677 return 0
678 ;;
679 format)
680 COMPREPLY=( $( compgen -W "c raw" -- "$cur" ) )
681 ;;
682 *)
683 # emit extra options
684 case ${words[3]} in
685 id|file)
686 _bpftool_once_attr 'format'
687 ;;
688 map|prog)
689 if [[ ${words[3]} == "map" ]] && [[ $cword == 6 ]]; then
690 COMPREPLY+=( $( compgen -W "key value kv all" -- "$cur" ) )
691 fi
692 _bpftool_once_attr 'format'
693 ;;
694 *)
695 ;;
696 esac
697 return 0
698 ;;
699 esac
700 ;;
701 show|list)
702 case $prev in
703 $command)
704 COMPREPLY+=( $( compgen -W "id" -- "$cur" ) )
705 ;;
706 id)
707 _bpftool_get_btf_ids
708 ;;
709 esac
710 return 0
711 ;;
712 *)
713 [[ $prev == $object ]] && \
714 COMPREPLY=( $( compgen -W 'dump help show list' \
715 -- "$cur" ) )
716 ;;
717 esac
718 ;;
719 cgroup)
720 case $command in
721 show|list|tree)
722 case $cword in
723 3)
724 _filedir
725 ;;
726 4)
727 COMPREPLY=( $( compgen -W 'effective' -- "$cur" ) )
728 ;;
729 esac
730 return 0
731 ;;
732 attach|detach)
733 local ATTACH_TYPES='ingress egress sock_create sock_ops \
734 device bind4 bind6 post_bind4 post_bind6 connect4 \
735 connect6 sendmsg4 sendmsg6 recvmsg4 recvmsg6 sysctl \
736 getsockopt setsockopt'
737 local ATTACH_FLAGS='multi override'
738 local PROG_TYPE='id pinned tag'
739 case $prev in
740 $command)
741 _filedir
742 return 0
743 ;;
744 ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
745 post_bind4|post_bind6|connect4|connect6|sendmsg4|\
746 sendmsg6|recvmsg4|recvmsg6|sysctl|getsockopt|\
747 setsockopt)
748 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
749 "$cur" ) )
750 return 0
751 ;;
752 id)
753 _bpftool_get_prog_ids
754 return 0
755 ;;
756 *)
757 if ! _bpftool_search_list "$ATTACH_TYPES"; then
758 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
759 "$cur" ) )
760 elif [[ "$command" == "attach" ]]; then
761 # We have an attach type on the command line,
762 # but it is not the previous word, or
763 # "id|pinned|tag" (we already checked for
764 # that). This should only leave the case when
765 # we need attach flags for "attach" commamnd.
766 _bpftool_one_of_list "$ATTACH_FLAGS"
767 fi
768 return 0
769 ;;
770 esac
771 ;;
772 *)
773 [[ $prev == $object ]] && \
774 COMPREPLY=( $( compgen -W 'help attach detach \
775 show list tree' -- "$cur" ) )
776 ;;
777 esac
778 ;;
779 perf)
780 case $command in
781 *)
782 [[ $prev == $object ]] && \
783 COMPREPLY=( $( compgen -W 'help \
784 show list' -- "$cur" ) )
785 ;;
786 esac
787 ;;
788 net)
789 local PROG_TYPE='id pinned tag'
790 local ATTACH_TYPES='xdp xdpgeneric xdpdrv xdpoffload'
791 case $command in
792 show|list)
793 [[ $prev != "$command" ]] && return 0
794 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
795 return 0
796 ;;
797 attach)
798 case $cword in
799 3)
800 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
801 return 0
802 ;;
803 4)
804 COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
805 return 0
806 ;;
807 5)
808 case $prev in
809 id)
810 _bpftool_get_prog_ids
811 ;;
812 pinned)
813 _filedir
814 ;;
815 esac
816 return 0
817 ;;
818 6)
819 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
820 return 0
821 ;;
822 8)
823 _bpftool_once_attr 'overwrite'
824 return 0
825 ;;
826 esac
827 ;;
828 detach)
829 case $cword in
830 3)
831 COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- "$cur" ) )
832 return 0
833 ;;
834 4)
835 COMPREPLY=( $( compgen -W 'dev' -- "$cur" ) )
836 return 0
837 ;;
838 esac
839 ;;
840 *)
841 [[ $prev == $object ]] && \
842 COMPREPLY=( $( compgen -W 'help \
843 show list attach detach' -- "$cur" ) )
844 ;;
845 esac
846 ;;
847 feature)
848 case $command in
849 probe)
850 [[ $prev == "prefix" ]] && return 0
851 if _bpftool_search_list 'macros'; then
852 COMPREPLY+=( $( compgen -W 'prefix' -- "$cur" ) )
853 else
854 COMPREPLY+=( $( compgen -W 'macros' -- "$cur" ) )
855 fi
856 _bpftool_one_of_list 'kernel dev'
857 return 0
858 ;;
859 *)
860 [[ $prev == $object ]] && \
861 COMPREPLY=( $( compgen -W 'help probe' -- "$cur" ) )
862 ;;
863 esac
864 ;;
865 esac
866} &&
867complete -F _bpftool bpftool
868
869# ex: ts=4 sw=4 et filetype=sh