aboutsummaryrefslogtreecommitdiffstats
path: root/Documentation/bpf
diff options
context:
space:
mode:
authorAndrii Nakryiko <andriin@fb.com>2019-02-28 20:12:19 -0500
committerDaniel Borkmann <daniel@iogearbox.net>2019-03-01 18:40:04 -0500
commit5efc529fb428e042c08a598b9afc5c5e2c600d74 (patch)
treef8961a67e3c9c31bedccbd9e3e09b75cb8df3911 /Documentation/bpf
parent4b9113045b1745ec8512d6743680809edca6a74e (diff)
docs/btf: fix typos, improve wording
Fix various typos, some of the formatting and wording for Documentation/btf.rst. Signed-off-by: Andrii Nakryiko <andriin@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Diffstat (limited to 'Documentation/bpf')
-rw-r--r--Documentation/bpf/btf.rst108
1 files changed, 53 insertions, 55 deletions
diff --git a/Documentation/bpf/btf.rst b/Documentation/bpf/btf.rst
index 1d434c3a268d..1d761f1c5b2b 100644
--- a/Documentation/bpf/btf.rst
+++ b/Documentation/bpf/btf.rst
@@ -5,7 +5,7 @@ BPF Type Format (BTF)
51. Introduction 51. Introduction
6*************** 6***************
7 7
8BTF (BPF Type Format) is the meta data format which 8BTF (BPF Type Format) is the metadata format which
9encodes the debug info related to BPF program/map. 9encodes the debug info related to BPF program/map.
10The name BTF was used initially to describe 10The name BTF was used initially to describe
11data types. The BTF was later extended to include 11data types. The BTF was later extended to include
@@ -40,8 +40,8 @@ details in :ref:`BTF_Type_String`.
402. BTF Type and String Encoding 402. BTF Type and String Encoding
41******************************* 41*******************************
42 42
43The file ``include/uapi/linux/btf.h`` provides high 43The file ``include/uapi/linux/btf.h`` provides high-level
44level definition on how types/strings are encoded. 44definition of how types/strings are encoded.
45 45
46The beginning of data blob must be:: 46The beginning of data blob must be::
47 47
@@ -59,23 +59,23 @@ The beginning of data blob must be::
59 }; 59 };
60 60
61The magic is ``0xeB9F``, which has different encoding for big and little 61The magic is ``0xeB9F``, which has different encoding for big and little
62endian system, and can be used to test whether BTF is generated for 62endian systems, and can be used to test whether BTF is generated for
63big or little endian target. 63big- or little-endian target.
64The btf_header is designed to be extensible with hdr_len equal to 64The ``btf_header`` is designed to be extensible with ``hdr_len`` equal to
65``sizeof(struct btf_header)`` when the data blob is generated. 65``sizeof(struct btf_header)`` when a data blob is generated.
66 66
672.1 String Encoding 672.1 String Encoding
68=================== 68===================
69 69
70The first string in the string section must be a null string. 70The first string in the string section must be a null string.
71The rest of string table is a concatenation of other null-treminated 71The rest of string table is a concatenation of other null-terminated
72strings. 72strings.
73 73
742.2 Type Encoding 742.2 Type Encoding
75================= 75=================
76 76
77The type id ``0`` is reserved for ``void`` type. 77The type id ``0`` is reserved for ``void`` type.
78The type section is parsed sequentially and the type id is assigned to 78The type section is parsed sequentially and type id is assigned to
79each recognized type starting from id ``1``. 79each recognized type starting from id ``1``.
80Currently, the following types are supported:: 80Currently, the following types are supported::
81 81
@@ -122,9 +122,9 @@ Each type contains the following common data::
122 }; 122 };
123 }; 123 };
124 124
125For certain kinds, the common data are followed by kind specific data. 125For certain kinds, the common data are followed by kind-specific data.
126The ``name_off`` in ``struct btf_type`` specifies the offset in the string table. 126The ``name_off`` in ``struct btf_type`` specifies the offset in the string
127The following details encoding of each kind. 127table. The following sections detail encoding of each kind.
128 128
1292.2.1 BTF_KIND_INT 1292.2.1 BTF_KIND_INT
130~~~~~~~~~~~~~~~~~~ 130~~~~~~~~~~~~~~~~~~
@@ -136,7 +136,7 @@ The following details encoding of each kind.
136 * ``info.vlen``: 0 136 * ``info.vlen``: 0
137 * ``size``: the size of the int type in bytes. 137 * ``size``: the size of the int type in bytes.
138 138
139``btf_type`` is followed by a ``u32`` with following bits arrangement:: 139``btf_type`` is followed by a ``u32`` with the following bits arrangement::
140 140
141 #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24) 141 #define BTF_INT_ENCODING(VAL) (((VAL) & 0x0f000000) >> 24)
142 #define BTF_INT_OFFSET(VAL) (((VAL & 0x00ff0000)) >> 16) 142 #define BTF_INT_OFFSET(VAL) (((VAL & 0x00ff0000)) >> 16)
@@ -148,7 +148,7 @@ The ``BTF_INT_ENCODING`` has the following attributes::
148 #define BTF_INT_CHAR (1 << 1) 148 #define BTF_INT_CHAR (1 << 1)
149 #define BTF_INT_BOOL (1 << 2) 149 #define BTF_INT_BOOL (1 << 2)
150 150
151The ``BTF_INT_ENCODING()`` provides extra information, signness, 151The ``BTF_INT_ENCODING()`` provides extra information: signedness,
152char, or bool, for the int type. The char and bool encoding 152char, or bool, for the int type. The char and bool encoding
153are mostly useful for pretty print. At most one encoding can 153are mostly useful for pretty print. At most one encoding can
154be specified for the int type. 154be specified for the int type.
@@ -161,8 +161,7 @@ The maximum value of ``BTF_INT_BITS()`` is 128.
161 161
162The ``BTF_INT_OFFSET()`` specifies the starting bit offset to 162The ``BTF_INT_OFFSET()`` specifies the starting bit offset to
163calculate values for this int. For example, a bitfield struct 163calculate values for this int. For example, a bitfield struct
164member has 164member has:
165
166 * btf member bit offset 100 from the start of the structure, 165 * btf member bit offset 100 from the start of the structure,
167 * btf member pointing to an int type, 166 * btf member pointing to an int type,
168 * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4`` 167 * the int type has ``BTF_INT_OFFSET() = 2`` and ``BTF_INT_BITS() = 4``
@@ -179,7 +178,7 @@ access the same bits as the above:
179 178
180The original intention of ``BTF_INT_OFFSET()`` is to provide 179The original intention of ``BTF_INT_OFFSET()`` is to provide
181flexibility of bitfield encoding. 180flexibility of bitfield encoding.
182Currently, both llvm and pahole generates ``BTF_INT_OFFSET() = 0`` 181Currently, both llvm and pahole generate ``BTF_INT_OFFSET() = 0``
183for all int types. 182for all int types.
184 183
1852.2.2 BTF_KIND_PTR 1842.2.2 BTF_KIND_PTR
@@ -204,7 +203,7 @@ No additional type data follow ``btf_type``.
204 * ``info.vlen``: 0 203 * ``info.vlen``: 0
205 * ``size/type``: 0, not used 204 * ``size/type``: 0, not used
206 205
207btf_type is followed by one "struct btf_array":: 206``btf_type`` is followed by one ``struct btf_array``::
208 207
209 struct btf_array { 208 struct btf_array {
210 __u32 type; 209 __u32 type;
@@ -217,27 +216,26 @@ The ``struct btf_array`` encoding:
217 * ``index_type``: the index type 216 * ``index_type``: the index type
218 * ``nelems``: the number of elements for this array (``0`` is also allowed). 217 * ``nelems``: the number of elements for this array (``0`` is also allowed).
219 218
220The ``index_type`` can be any regular int types 219The ``index_type`` can be any regular int type
221(u8, u16, u32, u64, unsigned __int128). 220(``u8``, ``u16``, ``u32``, ``u64``, ``unsigned __int128``).
222The original design of including ``index_type`` follows dwarf 221The original design of including ``index_type`` follows DWARF,
223which has a ``index_type`` for its array type. 222which has an ``index_type`` for its array type.
224Currently in BTF, beyond type verification, the ``index_type`` is not used. 223Currently in BTF, beyond type verification, the ``index_type`` is not used.
225 224
226The ``struct btf_array`` allows chaining through element type to represent 225The ``struct btf_array`` allows chaining through element type to represent
227multiple dimensional arrays. For example, ``int a[5][6]``, the following 226multidimensional arrays. For example, for ``int a[5][6]``, the following
228type system illustrates the chaining: 227type information illustrates the chaining:
229 228
230 * [1]: int 229 * [1]: int
231 * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6`` 230 * [2]: array, ``btf_array.type = [1]``, ``btf_array.nelems = 6``
232 * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5`` 231 * [3]: array, ``btf_array.type = [2]``, ``btf_array.nelems = 5``
233 232
234Currently, both pahole and llvm collapse multiple dimensional array 233Currently, both pahole and llvm collapse multidimensional array
235into one dimensional array, e.g., ``a[5][6]``, the btf_array.nelems 234into one-dimensional array, e.g., for ``a[5][6]``, the ``btf_array.nelems``
236equal to ``30``. This is because the original use case is map pretty 235is equal to ``30``. This is because the original use case is map pretty
237print where the whole array is dumped out so one dimensional array 236print where the whole array is dumped out so one-dimensional array
238is enough. As more BTF usage is explored, pahole and llvm can be 237is enough. As more BTF usage is explored, pahole and llvm can be
239changed to generate proper chained representation for 238changed to generate proper chained representation for multidimensional arrays.
240multiple dimensional arrays.
241 239
2422.2.4 BTF_KIND_STRUCT 2402.2.4 BTF_KIND_STRUCT
243~~~~~~~~~~~~~~~~~~~~~ 241~~~~~~~~~~~~~~~~~~~~~
@@ -382,7 +380,7 @@ No additional type data follow ``btf_type``.
382 380
383No additional type data follow ``btf_type``. 381No additional type data follow ``btf_type``.
384 382
385A BTF_KIND_FUNC defines, not a type, but a subprogram (function) whose 383A BTF_KIND_FUNC defines not a type, but a subprogram (function) whose
386signature is defined by ``type``. The subprogram is thus an instance of 384signature is defined by ``type``. The subprogram is thus an instance of
387that type. The BTF_KIND_FUNC may in turn be referenced by a func_info in 385that type. The BTF_KIND_FUNC may in turn be referenced by a func_info in
388the :ref:`BTF_Ext_Section` (ELF) or in the arguments to 386the :ref:`BTF_Ext_Section` (ELF) or in the arguments to
@@ -459,10 +457,10 @@ The workflow typically looks like:
4593.1 BPF_BTF_LOAD 4573.1 BPF_BTF_LOAD
460================ 458================
461 459
462Load a blob of BTF data into kernel. A blob of data 460Load a blob of BTF data into kernel. A blob of data,
463described in :ref:`BTF_Type_String` 461described in :ref:`BTF_Type_String`,
464can be directly loaded into the kernel. 462can be directly loaded into the kernel.
465A ``btf_fd`` returns to userspace. 463A ``btf_fd`` is returned to a userspace.
466 464
4673.2 BPF_MAP_CREATE 4653.2 BPF_MAP_CREATE
468================== 466==================
@@ -487,7 +485,7 @@ In libbpf, the map can be defined with extra annotation like below:
487Here, the parameters for macro BPF_ANNOTATE_KV_PAIR are map name, 485Here, the parameters for macro BPF_ANNOTATE_KV_PAIR are map name,
488key and value types for the map. 486key and value types for the map.
489During ELF parsing, libbpf is able to extract key/value type_id's 487During ELF parsing, libbpf is able to extract key/value type_id's
490and assigned them to BPF_MAP_CREATE attributes automatically. 488and assign them to BPF_MAP_CREATE attributes automatically.
491 489
492.. _BPF_Prog_Load: 490.. _BPF_Prog_Load:
493 491
@@ -532,7 +530,7 @@ Below are requirements for func_info:
532 bpf func boundaries. 530 bpf func boundaries.
533 531
534Below are requirements for line_info: 532Below are requirements for line_info:
535 * the first insn in each func must points to a line_info record. 533 * the first insn in each func must have a line_info record pointing to it.
536 * the line_info insn_off is in strictly increasing order. 534 * the line_info insn_off is in strictly increasing order.
537 535
538For line_info, the line number and column number are defined as below: 536For line_info, the line number and column number are defined as below:
@@ -544,26 +542,26 @@ For line_info, the line number and column number are defined as below:
5443.4 BPF_{PROG,MAP}_GET_NEXT_ID 5423.4 BPF_{PROG,MAP}_GET_NEXT_ID
545 543
546In kernel, every loaded program, map or btf has a unique id. 544In kernel, every loaded program, map or btf has a unique id.
547The id won't change during the life time of the program, map or btf. 545The id won't change during the lifetime of a program, map, or btf.
548 546
549The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID 547The bpf syscall command BPF_{PROG,MAP}_GET_NEXT_ID
550returns all id's, one for each command, to user space, for bpf 548returns all id's, one for each command, to user space, for bpf
551program or maps, 549program or maps, respectively,
552so the inspection tool can inspect all programs and maps. 550so an inspection tool can inspect all programs and maps.
553 551
5543.5 BPF_{PROG,MAP}_GET_FD_BY_ID 5523.5 BPF_{PROG,MAP}_GET_FD_BY_ID
555 553
556The introspection tool cannot use id to get details about program or maps. 554An introspection tool cannot use id to get details about program or maps.
557A file descriptor needs to be obtained first for reference counting purpose. 555A file descriptor needs to be obtained first for reference-counting purpose.
558 556
5593.6 BPF_OBJ_GET_INFO_BY_FD 5573.6 BPF_OBJ_GET_INFO_BY_FD
560========================== 558==========================
561 559
562Once a program/map fd is acquired, the introspection tool can 560Once a program/map fd is acquired, an introspection tool can
563get the detailed information from kernel about this fd, 561get the detailed information from kernel about this fd,
564some of which is btf related. For example, 562some of which are BTF-related. For example,
565``bpf_map_info`` returns ``btf_id``, key/value type id. 563``bpf_map_info`` returns ``btf_id`` and key/value type ids.
566``bpf_prog_info`` returns ``btf_id``, func_info and line info 564``bpf_prog_info`` returns ``btf_id``, func_info, and line info
567for translated bpf byte codes, and jited_line_info. 565for translated bpf byte codes, and jited_line_info.
568 566
5693.7 BPF_BTF_GET_FD_BY_ID 5673.7 BPF_BTF_GET_FD_BY_ID
@@ -574,9 +572,9 @@ bpf syscall command BPF_BTF_GET_FD_BY_ID can retrieve a btf fd.
574Then, with command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally 572Then, with command BPF_OBJ_GET_INFO_BY_FD, the btf blob, originally
575loaded into the kernel with BPF_BTF_LOAD, can be retrieved. 573loaded into the kernel with BPF_BTF_LOAD, can be retrieved.
576 574
577With the btf blob, ``bpf_map_info`` and ``bpf_prog_info``, the introspection 575With the btf blob, ``bpf_map_info``, and ``bpf_prog_info``, an introspection
578tool has full btf knowledge and is able to pretty print map key/values, 576tool has full btf knowledge and is able to pretty print map key/values,
579dump func signatures, dump line info along with byte/jit codes. 577dump func signatures and line info, along with byte/jit codes.
580 578
5814. ELF File Format Interface 5794. ELF File Format Interface
582**************************** 580****************************
@@ -625,8 +623,8 @@ The func_info is organized as below.::
625 ... 623 ...
626 624
627``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure 625``func_info_rec_size`` specifies the size of ``bpf_func_info`` structure
628when .BTF.ext is generated. btf_ext_info_sec, defined below, is 626when .BTF.ext is generated. ``btf_ext_info_sec``, defined below, is
629the func_info for each specific ELF section.:: 627a collection of func_info for each specific ELF section.::
630 628
631 struct btf_ext_info_sec { 629 struct btf_ext_info_sec {
632 __u32 sec_name_off; /* offset to section name */ 630 __u32 sec_name_off; /* offset to section name */
@@ -661,7 +659,7 @@ from the beginning of section (``btf_ext_info_sec->sec_name_off``).
661 659
662With BTF, the map key/value can be printed based on fields rather than 660With BTF, the map key/value can be printed based on fields rather than
663simply raw bytes. This is especially 661simply raw bytes. This is especially
664valuable for large structure or if you data structure 662valuable for large structure or if your data structure
665has bitfields. For example, for the following map,:: 663has bitfields. For example, for the following map,::
666 664
667 enum A { A1, A2, A3, A4, A5 }; 665 enum A { A1, A2, A3, A4, A5 };
@@ -702,8 +700,8 @@ bpftool is able to pretty print like below:
7025.2 bpftool prog dump 7005.2 bpftool prog dump
703===================== 701=====================
704 702
705The following is an example to show func_info and line_info 703The following is an example showing how func_info and line_info
706can help prog dump with better kernel symbol name, function prototype 704can help prog dump with better kernel symbol names, function prototypes
707and line information.:: 705and line information.::
708 706
709 $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv 707 $ bpftool prog dump jited pinned /sys/fs/bpf/test_btf_haskv
@@ -733,10 +731,10 @@ and line information.::
733 ; counts = bpf_map_lookup_elem(&btf_map, &key); 731 ; counts = bpf_map_lookup_elem(&btf_map, &key);
734 [...] 732 [...]
735 733
7365.3 verifier log 7345.3 Verifier Log
737================ 735================
738 736
739The following is an example how line_info can help verifier failure debug.:: 737The following is an example of how line_info can help debugging verification failure.::
740 738
741 /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c 739 /* The code at tools/testing/selftests/bpf/test_xdp_noinline.c
742 * is modified as below. 740 * is modified as below.
@@ -867,4 +865,4 @@ The assembly code (-S) is able to show the BTF encoding in assembly format.::
8677. Testing 8657. Testing
868********** 866**********
869 867
870Kernel bpf selftest `test_btf.c` provides extensive set of BTF related tests. 868Kernel bpf selftest `test_btf.c` provides extensive set of BTF-related tests.