bpftool 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. # bpftool(8) bash completion -*- shell-script -*-
  2. #
  3. # Copyright (C) 2017-2018 Netronome Systems, Inc.
  4. #
  5. # This software is dual licensed under the GNU General License
  6. # Version 2, June 1991 as shown in the file COPYING in the top-level
  7. # directory of this source tree or the BSD 2-Clause License provided
  8. # below. You have the option to license this software under the
  9. # complete terms of either license.
  10. #
  11. # The BSD 2-Clause License:
  12. #
  13. # Redistribution and use in source and binary forms, with or
  14. # without modification, are permitted provided that the following
  15. # conditions are met:
  16. #
  17. # 1. Redistributions of source code must retain the above
  18. # copyright notice, this list of conditions and the following
  19. # disclaimer.
  20. #
  21. # 2. Redistributions in binary form must reproduce the above
  22. # copyright notice, this list of conditions and the following
  23. # disclaimer in the documentation and/or other materials
  24. # provided with the distribution.
  25. #
  26. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. # SOFTWARE.
  34. #
  35. # Author: Quentin Monnet <quentin.monnet@netronome.com>
  36. # Takes a list of words in argument; each one of them is added to COMPREPLY if
  37. # it is not already present on the command line. Returns no value.
  38. _bpftool_once_attr()
  39. {
  40. local w idx found
  41. for w in $*; do
  42. found=0
  43. for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
  44. if [[ $w == ${words[idx]} ]]; then
  45. found=1
  46. break
  47. fi
  48. done
  49. [[ $found -eq 0 ]] && \
  50. COMPREPLY+=( $( compgen -W "$w" -- "$cur" ) )
  51. done
  52. }
  53. # Takes a list of words as argument; if any of those words is present on the
  54. # command line, return 0. Otherwise, return 1.
  55. _bpftool_search_list()
  56. {
  57. local w idx
  58. for w in $*; do
  59. for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
  60. [[ $w == ${words[idx]} ]] && return 0
  61. done
  62. done
  63. return 1
  64. }
  65. # Takes a list of words in argument; adds them all to COMPREPLY if none of them
  66. # is already present on the command line. Returns no value.
  67. _bpftool_one_of_list()
  68. {
  69. _bpftool_search_list $* && return 1
  70. COMPREPLY+=( $( compgen -W "$*" -- "$cur" ) )
  71. }
  72. _bpftool_get_map_ids()
  73. {
  74. COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
  75. command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
  76. }
  77. _bpftool_get_perf_map_ids()
  78. {
  79. COMPREPLY+=( $( compgen -W "$( bpftool -jp map 2>&1 | \
  80. command grep -C2 perf_event_array | \
  81. command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
  82. }
  83. _bpftool_get_prog_ids()
  84. {
  85. COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
  86. command sed -n 's/.*"id": \(.*\),$/\1/p' )" -- "$cur" ) )
  87. }
  88. _bpftool_get_prog_tags()
  89. {
  90. COMPREPLY+=( $( compgen -W "$( bpftool -jp prog 2>&1 | \
  91. command sed -n 's/.*"tag": "\(.*\)",$/\1/p' )" -- "$cur" ) )
  92. }
  93. _bpftool_get_obj_map_names()
  94. {
  95. local obj
  96. obj=$1
  97. maps=$(objdump -j maps -t $obj 2>/dev/null | \
  98. command awk '/g . maps/ {print $NF}')
  99. COMPREPLY+=( $( compgen -W "$maps" -- "$cur" ) )
  100. }
  101. _bpftool_get_obj_map_idxs()
  102. {
  103. local obj
  104. obj=$1
  105. nmaps=$(objdump -j maps -t $obj 2>/dev/null | grep -c 'g . maps')
  106. COMPREPLY+=( $( compgen -W "$(seq 0 $((nmaps - 1)))" -- "$cur" ) )
  107. }
  108. _sysfs_get_netdevs()
  109. {
  110. COMPREPLY+=( $( compgen -W "$( ls /sys/class/net 2>/dev/null )" -- \
  111. "$cur" ) )
  112. }
  113. # For bpftool map update: retrieve type of the map to update.
  114. _bpftool_map_update_map_type()
  115. {
  116. local keyword ref
  117. for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
  118. if [[ ${words[$((idx-2))]} == "update" ]]; then
  119. keyword=${words[$((idx-1))]}
  120. ref=${words[$((idx))]}
  121. fi
  122. done
  123. [[ -z $ref ]] && return 0
  124. local type
  125. type=$(bpftool -jp map show $keyword $ref | \
  126. command sed -n 's/.*"type": "\(.*\)",$/\1/p')
  127. [[ -n $type ]] && printf $type
  128. }
  129. _bpftool_map_update_get_id()
  130. {
  131. # Is it the map to update, or a map to insert into the map to update?
  132. # Search for "value" keyword.
  133. local idx value
  134. for (( idx=7; idx < ${#words[@]}-1; idx++ )); do
  135. if [[ ${words[idx]} == "value" ]]; then
  136. value=1
  137. break
  138. fi
  139. done
  140. [[ $value -eq 0 ]] && _bpftool_get_map_ids && return 0
  141. # Id to complete is for a value. It can be either prog id or map id. This
  142. # depends on the type of the map to update.
  143. local type=$(_bpftool_map_update_map_type)
  144. case $type in
  145. array_of_maps|hash_of_maps)
  146. _bpftool_get_map_ids
  147. return 0
  148. ;;
  149. prog_array)
  150. _bpftool_get_prog_ids
  151. return 0
  152. ;;
  153. *)
  154. return 0
  155. ;;
  156. esac
  157. }
  158. _bpftool()
  159. {
  160. local cur prev words objword
  161. _init_completion || return
  162. # Deal with options
  163. if [[ ${words[cword]} == -* ]]; then
  164. local c='--version --json --pretty --bpffs'
  165. COMPREPLY=( $( compgen -W "$c" -- "$cur" ) )
  166. return 0
  167. fi
  168. # Deal with simplest keywords
  169. case $prev in
  170. help|hex|opcodes|visual)
  171. return 0
  172. ;;
  173. tag)
  174. _bpftool_get_prog_tags
  175. return 0
  176. ;;
  177. file|pinned)
  178. _filedir
  179. return 0
  180. ;;
  181. batch)
  182. COMPREPLY=( $( compgen -W 'file' -- "$cur" ) )
  183. return 0
  184. ;;
  185. esac
  186. # Remove all options so completions don't have to deal with them.
  187. local i
  188. for (( i=1; i < ${#words[@]}; )); do
  189. if [[ ${words[i]::1} == - ]]; then
  190. words=( "${words[@]:0:i}" "${words[@]:i+1}" )
  191. [[ $i -le $cword ]] && cword=$(( cword - 1 ))
  192. else
  193. i=$(( ++i ))
  194. fi
  195. done
  196. cur=${words[cword]}
  197. prev=${words[cword - 1]}
  198. local object=${words[1]} command=${words[2]}
  199. if [[ -z $object || $cword -eq 1 ]]; then
  200. case $cur in
  201. *)
  202. COMPREPLY=( $( compgen -W "$( bpftool help 2>&1 | \
  203. command sed \
  204. -e '/OBJECT := /!d' \
  205. -e 's/.*{//' \
  206. -e 's/}.*//' \
  207. -e 's/|//g' )" -- "$cur" ) )
  208. COMPREPLY+=( $( compgen -W 'batch help' -- "$cur" ) )
  209. return 0
  210. ;;
  211. esac
  212. fi
  213. [[ $command == help ]] && return 0
  214. # Completion depends on object and command in use
  215. case $object in
  216. prog)
  217. if [[ $command != "load" ]]; then
  218. case $prev in
  219. id)
  220. _bpftool_get_prog_ids
  221. return 0
  222. ;;
  223. esac
  224. fi
  225. local PROG_TYPE='id pinned tag'
  226. case $command in
  227. show|list)
  228. [[ $prev != "$command" ]] && return 0
  229. COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
  230. return 0
  231. ;;
  232. dump)
  233. case $prev in
  234. $command)
  235. COMPREPLY+=( $( compgen -W "xlated jited" -- \
  236. "$cur" ) )
  237. return 0
  238. ;;
  239. xlated|jited)
  240. COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
  241. "$cur" ) )
  242. return 0
  243. ;;
  244. *)
  245. _bpftool_once_attr 'file'
  246. if _bpftool_search_list 'xlated'; then
  247. COMPREPLY+=( $( compgen -W 'opcodes visual' -- \
  248. "$cur" ) )
  249. else
  250. COMPREPLY+=( $( compgen -W 'opcodes' -- \
  251. "$cur" ) )
  252. fi
  253. return 0
  254. ;;
  255. esac
  256. ;;
  257. pin)
  258. if [[ $prev == "$command" ]]; then
  259. COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
  260. else
  261. _filedir
  262. fi
  263. return 0
  264. ;;
  265. load)
  266. local obj
  267. if [[ ${#words[@]} -lt 6 ]]; then
  268. _filedir
  269. return 0
  270. fi
  271. obj=${words[3]}
  272. if [[ ${words[-4]} == "map" ]]; then
  273. COMPREPLY=( $( compgen -W "id pinned" -- "$cur" ) )
  274. return 0
  275. fi
  276. if [[ ${words[-3]} == "map" ]]; then
  277. if [[ ${words[-2]} == "idx" ]]; then
  278. _bpftool_get_obj_map_idxs $obj
  279. elif [[ ${words[-2]} == "name" ]]; then
  280. _bpftool_get_obj_map_names $obj
  281. fi
  282. return 0
  283. fi
  284. if [[ ${words[-2]} == "map" ]]; then
  285. COMPREPLY=( $( compgen -W "idx name" -- "$cur" ) )
  286. return 0
  287. fi
  288. case $prev in
  289. type)
  290. COMPREPLY=( $( compgen -W "socket kprobe kretprobe classifier action tracepoint raw_tracepoint xdp perf_event cgroup/skb cgroup/sock cgroup/dev lwt_in lwt_out lwt_xmit lwt_seg6local sockops sk_skb sk_msg lirc_mode2 cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 cgroup/sendmsg6 cgroup/post_bind4 cgroup/post_bind6" -- \
  291. "$cur" ) )
  292. return 0
  293. ;;
  294. id)
  295. _bpftool_get_map_ids
  296. return 0
  297. ;;
  298. pinned)
  299. _filedir
  300. return 0
  301. ;;
  302. dev)
  303. _sysfs_get_netdevs
  304. return 0
  305. ;;
  306. *)
  307. COMPREPLY=( $( compgen -W "map" -- "$cur" ) )
  308. _bpftool_once_attr 'type'
  309. _bpftool_once_attr 'dev'
  310. return 0
  311. ;;
  312. esac
  313. ;;
  314. *)
  315. [[ $prev == $object ]] && \
  316. COMPREPLY=( $( compgen -W 'dump help pin load \
  317. show list' -- "$cur" ) )
  318. ;;
  319. esac
  320. ;;
  321. map)
  322. local MAP_TYPE='id pinned'
  323. case $command in
  324. show|list|dump)
  325. case $prev in
  326. $command)
  327. COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
  328. return 0
  329. ;;
  330. id)
  331. _bpftool_get_map_ids
  332. return 0
  333. ;;
  334. *)
  335. return 0
  336. ;;
  337. esac
  338. ;;
  339. lookup|getnext|delete)
  340. case $prev in
  341. $command)
  342. COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
  343. return 0
  344. ;;
  345. id)
  346. _bpftool_get_map_ids
  347. return 0
  348. ;;
  349. key)
  350. COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
  351. ;;
  352. *)
  353. _bpftool_once_attr 'key'
  354. return 0
  355. ;;
  356. esac
  357. ;;
  358. update)
  359. case $prev in
  360. $command)
  361. COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
  362. return 0
  363. ;;
  364. id)
  365. _bpftool_map_update_get_id
  366. return 0
  367. ;;
  368. key)
  369. COMPREPLY+=( $( compgen -W 'hex' -- "$cur" ) )
  370. ;;
  371. value)
  372. # We can have bytes, or references to a prog or a
  373. # map, depending on the type of the map to update.
  374. case $(_bpftool_map_update_map_type) in
  375. array_of_maps|hash_of_maps)
  376. local MAP_TYPE='id pinned'
  377. COMPREPLY+=( $( compgen -W "$MAP_TYPE" \
  378. -- "$cur" ) )
  379. return 0
  380. ;;
  381. prog_array)
  382. local PROG_TYPE='id pinned tag'
  383. COMPREPLY+=( $( compgen -W "$PROG_TYPE" \
  384. -- "$cur" ) )
  385. return 0
  386. ;;
  387. *)
  388. COMPREPLY+=( $( compgen -W 'hex' \
  389. -- "$cur" ) )
  390. return 0
  391. ;;
  392. esac
  393. return 0
  394. ;;
  395. *)
  396. _bpftool_once_attr 'key'
  397. local UPDATE_FLAGS='any exist noexist'
  398. for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
  399. if [[ ${words[idx]} == 'value' ]]; then
  400. # 'value' is present, but is not the last
  401. # word i.e. we can now have UPDATE_FLAGS.
  402. _bpftool_one_of_list "$UPDATE_FLAGS"
  403. return 0
  404. fi
  405. done
  406. for (( idx=3; idx < ${#words[@]}-1; idx++ )); do
  407. if [[ ${words[idx]} == 'key' ]]; then
  408. # 'key' is present, but is not the last
  409. # word i.e. we can now have 'value'.
  410. _bpftool_once_attr 'value'
  411. return 0
  412. fi
  413. done
  414. return 0
  415. ;;
  416. esac
  417. ;;
  418. pin)
  419. if [[ $prev == "$command" ]]; then
  420. COMPREPLY=( $( compgen -W "$PROG_TYPE" -- "$cur" ) )
  421. else
  422. _filedir
  423. fi
  424. return 0
  425. ;;
  426. event_pipe)
  427. case $prev in
  428. $command)
  429. COMPREPLY=( $( compgen -W "$MAP_TYPE" -- "$cur" ) )
  430. return 0
  431. ;;
  432. id)
  433. _bpftool_get_perf_map_ids
  434. return 0
  435. ;;
  436. cpu)
  437. return 0
  438. ;;
  439. index)
  440. return 0
  441. ;;
  442. *)
  443. _bpftool_once_attr 'cpu'
  444. _bpftool_once_attr 'index'
  445. return 0
  446. ;;
  447. esac
  448. ;;
  449. *)
  450. [[ $prev == $object ]] && \
  451. COMPREPLY=( $( compgen -W 'delete dump getnext help \
  452. lookup pin event_pipe show list update' -- \
  453. "$cur" ) )
  454. ;;
  455. esac
  456. ;;
  457. cgroup)
  458. case $command in
  459. show|list)
  460. _filedir
  461. return 0
  462. ;;
  463. tree)
  464. _filedir
  465. return 0
  466. ;;
  467. attach|detach)
  468. local ATTACH_TYPES='ingress egress sock_create sock_ops \
  469. device bind4 bind6 post_bind4 post_bind6 connect4 \
  470. connect6 sendmsg4 sendmsg6'
  471. local ATTACH_FLAGS='multi override'
  472. local PROG_TYPE='id pinned tag'
  473. case $prev in
  474. $command)
  475. _filedir
  476. return 0
  477. ;;
  478. ingress|egress|sock_create|sock_ops|device|bind4|bind6|\
  479. post_bind4|post_bind6|connect4|connect6|sendmsg4|\
  480. sendmsg6)
  481. COMPREPLY=( $( compgen -W "$PROG_TYPE" -- \
  482. "$cur" ) )
  483. return 0
  484. ;;
  485. id)
  486. _bpftool_get_prog_ids
  487. return 0
  488. ;;
  489. *)
  490. if ! _bpftool_search_list "$ATTACH_TYPES"; then
  491. COMPREPLY=( $( compgen -W "$ATTACH_TYPES" -- \
  492. "$cur" ) )
  493. elif [[ "$command" == "attach" ]]; then
  494. # We have an attach type on the command line,
  495. # but it is not the previous word, or
  496. # "id|pinned|tag" (we already checked for
  497. # that). This should only leave the case when
  498. # we need attach flags for "attach" commamnd.
  499. _bpftool_one_of_list "$ATTACH_FLAGS"
  500. fi
  501. return 0
  502. ;;
  503. esac
  504. ;;
  505. *)
  506. [[ $prev == $object ]] && \
  507. COMPREPLY=( $( compgen -W 'help attach detach \
  508. show list tree' -- "$cur" ) )
  509. ;;
  510. esac
  511. ;;
  512. perf)
  513. case $command in
  514. *)
  515. [[ $prev == $object ]] && \
  516. COMPREPLY=( $( compgen -W 'help \
  517. show list' -- "$cur" ) )
  518. ;;
  519. esac
  520. ;;
  521. esac
  522. } &&
  523. complete -F _bpftool bpftool
  524. # ex: ts=4 sw=4 et filetype=sh