sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget%/translations/zh_CN/networking/filtermodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/zh_TW/networking/filtermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/it_IT/networking/filtermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ja_JP/networking/filtermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ko_KR/networking/filtermodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/sp_SP/networking/filtermodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h SPDX-License-Identifier: GPL-2.0h]h SPDX-License-Identifier: GPL-2.0}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhh?/var/lib/git/docbuild/linux/Documentation/networking/filter.rsthKubhtarget)}(h.. _networking-filter:h]h}(h]h ]h"]h$]h&]refidnetworking-filteruh1hhKhhhhhhubhsection)}(hhh](htitle)}(h7Linux Socket Filtering aka Berkeley Packet Filter (BPF)h]h7Linux Socket Filtering aka Berkeley Packet Filter (BPF)}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hNoticeh]hNotice}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhK ubh paragraph)}(hThis file used to document the eBPF format and mechanisms even when not related to socket filtering. The ../bpf/index.rst has more details on eBPF.h]hThis file used to document the eBPF format and mechanisms even when not related to socket filtering. The ../bpf/index.rst has more details on eBPF.}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hhhhubeh}(h]noticeah ]h"]noticeah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(h Introductionh]h Introduction}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXLinux Socket Filtering (LSF) is derived from the Berkeley Packet Filter. Though there are some distinct differences between the BSD and Linux Kernel filtering, but when we speak of BPF or LSF in Linux context, we mean the very same mechanism of filtering in the Linux kernel.h]hXLinux Socket Filtering (LSF) is derived from the Berkeley Packet Filter. Though there are some distinct differences between the BSD and Linux Kernel filtering, but when we speak of BPF or LSF in Linux context, we mean the very same mechanism of filtering in the Linux kernel.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXBPF allows a user-space program to attach a filter onto any socket and allow or disallow certain types of data to come through the socket. LSF follows exactly the same filter code structure as BSD's BPF, so referring to the BSD bpf.4 manpage is very helpful in creating filters.h]hXBPF allows a user-space program to attach a filter onto any socket and allow or disallow certain types of data to come through the socket. LSF follows exactly the same filter code structure as BSD’s BPF, so referring to the BSD bpf.4 manpage is very helpful in creating filters.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hX5On Linux, BPF is much simpler than on BSD. One does not have to worry about devices or anything like that. You simply create your filter code, send it to the kernel via the SO_ATTACH_FILTER option and if your filter code passes the kernel check on it, you then immediately begin filtering data on that socket.h]hX5On Linux, BPF is much simpler than on BSD. One does not have to worry about devices or anything like that. You simply create your filter code, send it to the kernel via the SO_ATTACH_FILTER option and if your filter code passes the kernel check on it, you then immediately begin filtering data on that socket.}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXYou can also detach filters from your socket via the SO_DETACH_FILTER option. This will probably not be used much since when you close a socket that has a filter on it the filter is automagically removed. The other less common case may be adding a different filter on the same socket where you had another filter that is still running: the kernel takes care of removing the old one and placing your new one in its place, assuming your filter has passed the checks, otherwise if it fails the old filter will remain on that socket.h]hXYou can also detach filters from your socket via the SO_DETACH_FILTER option. This will probably not be used much since when you close a socket that has a filter on it the filter is automagically removed. The other less common case may be adding a different filter on the same socket where you had another filter that is still running: the kernel takes care of removing the old one and placing your new one in its place, assuming your filter has passed the checks, otherwise if it fails the old filter will remain on that socket.}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK#hjhhubh)}(hXSO_LOCK_FILTER option allows to lock the filter attached to a socket. Once set, a filter cannot be removed or changed. This allows one process to setup a socket, attach a filter, lock it then drop privileges and be assured that the filter will be kept until the socket is closed.h]hXSO_LOCK_FILTER option allows to lock the filter attached to a socket. Once set, a filter cannot be removed or changed. This allows one process to setup a socket, attach a filter, lock it then drop privileges and be assured that the filter will be kept until the socket is closed.}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK,hjhhubh)}(hXQThe biggest user of this construct might be libpcap. Issuing a high-level filter command like `tcpdump -i em1 port 22` passes through the libpcap internal compiler that generates a structure that can eventually be loaded via SO_ATTACH_FILTER to the kernel. `tcpdump -i em1 port 22 -ddd` displays what is being placed into this structure.h](h^The biggest user of this construct might be libpcap. Issuing a high-level filter command like }(hjWhhhNhNubhtitle_reference)}(h`tcpdump -i em1 port 22`h]htcpdump -i em1 port 22}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1j_hjWubh passes through the libpcap internal compiler that generates a structure that can eventually be loaded via SO_ATTACH_FILTER to the kernel. }(hjWhhhNhNubj`)}(h`tcpdump -i em1 port 22 -ddd`h]htcpdump -i em1 port 22 -ddd}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1j_hjWubh3 displays what is being placed into this structure.}(hjWhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK1hjhhubh)}(hXAlthough we were only speaking about sockets here, BPF in Linux is used in many more places. There's xt_bpf for netfilter, cls_bpf in the kernel qdisc layer, SECCOMP-BPF (SECure COMPuting [1]_), and lots of other places such as team driver, PTP code, etc where BPF is being used.h](hAlthough we were only speaking about sockets here, BPF in Linux is used in many more places. There’s xt_bpf for netfilter, cls_bpf in the kernel qdisc layer, SECCOMP-BPF (SECure COMPuting }(hjhhhNhNubhfootnote_reference)}(h[1]_h]h1}(hjhhhNhNubah}(h]id1ah ]h"]h$]h&]hid2docnamenetworking/filteruh1jhjresolvedKubhW), and lots of other places such as team driver, PTP code, etc where BPF is being used.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK7hjhhubhfootnote)}(h/Documentation/userspace-api/seccomp_filter.rst h](hlabel)}(h1h]h1}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh)}(h.Documentation/userspace-api/seccomp_filter.rsth]h.Documentation/userspace-api/seccomp_filter.rst}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hXFSteven McCanne and Van Jacobson. 1993. The BSD packet filter: a new architecture for user-level packet capture. In Proceedings of the USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993 Conference Proceedings (USENIX'93). USENIX Association, Berkeley, CA, USA, 2-2. [http://www.tcpdump.org/papers/bpf-usenix93.pdf]h](hXSteven McCanne and Van Jacobson. 1993. The BSD packet filter: a new architecture for user-level packet capture. In Proceedings of the USENIX Winter 1993 Conference Proceedings on USENIX Winter 1993 Conference Proceedings (USENIX’93). USENIX Association, Berkeley, CA, USA, 2-2. [}(hjhhhNhNubh reference)}(h.http://www.tcpdump.org/papers/bpf-usenix93.pdfh]h.http://www.tcpdump.org/papers/bpf-usenix93.pdf}(hjhhhNhNubah}(h]h ]h"]h$]h&]refurijuh1jhjubh]}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK@hjhhubeh}(h] introductionah ]h"] introductionah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h Structureh]h Structure}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKGubh)}(hcUser space applications include which contains the following relevant structures::h]hbUser space applications include which contains the following relevant structures:}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKIhjhhubh literal_block)}(hstruct sock_filter { /* Filter block */ __u16 code; /* Actual filter code */ __u8 jt; /* Jump true */ __u8 jf; /* Jump false */ __u32 k; /* Generic multiuse field */ };h]hstruct sock_filter { /* Filter block */ __u16 code; /* Actual filter code */ __u8 jt; /* Jump true */ __u8 jf; /* Jump false */ __u32 k; /* Generic multiuse field */ };}hj7sbah}(h]h ]h"]h$]h&]hhuh1j5hhhKLhjhhubh)}(hSuch a structure is assembled as an array of 4-tuples, that contains a code, jt, jf and k value. jt and jf are jump offsets and k a generic value to be used for a provided code::h]hSuch a structure is assembled as an array of 4-tuples, that contains a code, jt, jf and k value. jt and jf are jump offsets and k a generic value to be used for a provided code:}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKShjhhubj6)}(hstruct sock_fprog { /* Required for SO_ATTACH_FILTER. */ unsigned short len; /* Number of filter blocks */ struct sock_filter __user *filter; };h]hstruct sock_fprog { /* Required for SO_ATTACH_FILTER. */ unsigned short len; /* Number of filter blocks */ struct sock_filter __user *filter; };}hjSsbah}(h]h ]h"]h$]h&]hhuh1j5hhhKWhjhhubh)}(hFor socket filtering, a pointer to this structure (as shown in follow-up example) is being passed to the kernel through setsockopt(2).h]hFor socket filtering, a pointer to this structure (as shown in follow-up example) is being passed to the kernel through setsockopt(2).}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK\hjhhubeh}(h] structureah ]h"] structureah$]h&]uh1hhhhhhhhKGubh)}(hhh](h)}(hExampleh]hExample}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjwhhhhhK`ubj6)}(hX#include #include #include #include /* ... */ /* From the example above: tcpdump -i em1 port 22 -dd */ struct sock_filter code[] = { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 8, 0x000086dd }, { 0x30, 0, 0, 0x00000014 }, { 0x15, 2, 0, 0x00000084 }, { 0x15, 1, 0, 0x00000006 }, { 0x15, 0, 17, 0x00000011 }, { 0x28, 0, 0, 0x00000036 }, { 0x15, 14, 0, 0x00000016 }, { 0x28, 0, 0, 0x00000038 }, { 0x15, 12, 13, 0x00000016 }, { 0x15, 0, 12, 0x00000800 }, { 0x30, 0, 0, 0x00000017 }, { 0x15, 2, 0, 0x00000084 }, { 0x15, 1, 0, 0x00000006 }, { 0x15, 0, 8, 0x00000011 }, { 0x28, 0, 0, 0x00000014 }, { 0x45, 6, 0, 0x00001fff }, { 0xb1, 0, 0, 0x0000000e }, { 0x48, 0, 0, 0x0000000e }, { 0x15, 2, 0, 0x00000016 }, { 0x48, 0, 0, 0x00000010 }, { 0x15, 0, 1, 0x00000016 }, { 0x06, 0, 0, 0x0000ffff }, { 0x06, 0, 0, 0x00000000 }, }; struct sock_fprog bpf = { .len = ARRAY_SIZE(code), .filter = code, }; sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sock < 0) /* ... bail out ... */ ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)); if (ret < 0) /* ... bail out ... */ /* ... */ close(sock);h]hX#include #include #include #include /* ... */ /* From the example above: tcpdump -i em1 port 22 -dd */ struct sock_filter code[] = { { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 8, 0x000086dd }, { 0x30, 0, 0, 0x00000014 }, { 0x15, 2, 0, 0x00000084 }, { 0x15, 1, 0, 0x00000006 }, { 0x15, 0, 17, 0x00000011 }, { 0x28, 0, 0, 0x00000036 }, { 0x15, 14, 0, 0x00000016 }, { 0x28, 0, 0, 0x00000038 }, { 0x15, 12, 13, 0x00000016 }, { 0x15, 0, 12, 0x00000800 }, { 0x30, 0, 0, 0x00000017 }, { 0x15, 2, 0, 0x00000084 }, { 0x15, 1, 0, 0x00000006 }, { 0x15, 0, 8, 0x00000011 }, { 0x28, 0, 0, 0x00000014 }, { 0x45, 6, 0, 0x00001fff }, { 0xb1, 0, 0, 0x0000000e }, { 0x48, 0, 0, 0x0000000e }, { 0x15, 2, 0, 0x00000016 }, { 0x48, 0, 0, 0x00000010 }, { 0x15, 0, 1, 0x00000016 }, { 0x06, 0, 0, 0x0000ffff }, { 0x06, 0, 0, 0x00000000 }, }; struct sock_fprog bpf = { .len = ARRAY_SIZE(code), .filter = code, }; sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); if (sock < 0) /* ... bail out ... */ ret = setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &bpf, sizeof(bpf)); if (ret < 0) /* ... bail out ... */ /* ... */ close(sock);}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhKdhjwhhubh)}(hThe above example code attaches a socket filter for a PF_PACKET socket in order to let all IPv4/IPv6 packets with port 22 pass. The rest will be dropped for this socket.h]hThe above example code attaches a socket filter for a PF_PACKET socket in order to let all IPv4/IPv6 packets with port 22 pass. The rest will be dropped for this socket.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(hThe setsockopt(2) call to SO_DETACH_FILTER doesn't need any arguments and SO_LOCK_FILTER for preventing the filter to be detached, takes an integer value with 0 or 1.h]hThe setsockopt(2) call to SO_DETACH_FILTER doesn’t need any arguments and SO_LOCK_FILTER for preventing the filter to be detached, takes an integer value with 0 or 1.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(huNote that socket filters are not restricted to PF_PACKET sockets only, but can also be used on other socket families.h]huNote that socket filters are not restricted to PF_PACKET sockets only, but can also be used on other socket families.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(hSummary of system calls:h]hSummary of system calls:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh block_quote)}(h* setsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &val, sizeof(val)); * setsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &val, sizeof(val)); * setsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &val, sizeof(val)); h]h bullet_list)}(hhh](h list_item)}(hDsetsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &val, sizeof(val));h]h)}(hjh]hDsetsockopt(sockfd, SOL_SOCKET, SO_ATTACH_FILTER, &val, sizeof(val));}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hDsetsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &val, sizeof(val));h]h)}(hjh]hDsetsockopt(sockfd, SOL_SOCKET, SO_DETACH_FILTER, &val, sizeof(val));}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hEsetsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &val, sizeof(val)); h]h)}(hDsetsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &val, sizeof(val));h]hDsetsockopt(sockfd, SOL_SOCKET, SO_LOCK_FILTER, &val, sizeof(val));}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhKhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjwhhubh)}(hNormally, most use cases for socket filtering on packet sockets will be covered by libpcap in high-level syntax, so as an application developer you should stick to that. libpcap wraps its own layer around all that.h]hNormally, most use cases for socket filtering on packet sockets will be covered by libpcap in high-level syntax, so as an application developer you should stick to that. libpcap wraps its own layer around all that.}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubh)}(hXUnless i) using/linking to libpcap is not an option, ii) the required BPF filters use Linux extensions that are not supported by libpcap's compiler, iii) a filter might be more complex and not cleanly implementable with libpcap's compiler, or iv) particular filter codes should be optimized differently than libpcap's internal compiler does; then in such cases writing such a filter "by hand" can be of an alternative. For example, xt_bpf and cls_bpf users might have requirements that could result in more complex filter code, or one that cannot be expressed with libpcap (e.g. different return codes for various code paths). Moreover, BPF JIT implementors may wish to manually write test cases and thus need low-level access to BPF code as well.h]hXUnless i) using/linking to libpcap is not an option, ii) the required BPF filters use Linux extensions that are not supported by libpcap’s compiler, iii) a filter might be more complex and not cleanly implementable with libpcap’s compiler, or iv) particular filter codes should be optimized differently than libpcap’s internal compiler does; then in such cases writing such a filter “by hand” can be of an alternative. For example, xt_bpf and cls_bpf users might have requirements that could result in more complex filter code, or one that cannot be expressed with libpcap (e.g. different return codes for various code paths). Moreover, BPF JIT implementors may wish to manually write test cases and thus need low-level access to BPF code as well.}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwhhubeh}(h]exampleah ]h"]exampleah$]h&]uh1hhhhhhhhK`ubh)}(hhh](h)}(hBPF engine and instruction seth]hBPF engine and instruction set}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjShhhhhKubh)}(hXUnder tools/bpf/ there's a small helper tool called bpf_asm which can be used to write low-level filters for example scenarios mentioned in the previous section. Asm-like syntax mentioned here has been implemented in bpf_asm and will be used for further explanations (instead of dealing with less readable opcodes directly, principles are the same). The syntax is closely modelled after Steven McCanne's and Van Jacobson's BPF paper.h]hXUnder tools/bpf/ there’s a small helper tool called bpf_asm which can be used to write low-level filters for example scenarios mentioned in the previous section. Asm-like syntax mentioned here has been implemented in bpf_asm and will be used for further explanations (instead of dealing with less readable opcodes directly, principles are the same). The syntax is closely modelled after Steven McCanne’s and Van Jacobson’s BPF paper.}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjShhubh)}(h>The BPF architecture consists of the following basic elements:h]h>The BPF architecture consists of the following basic elements:}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjShhubj)}(hX======= ==================================================== Element Description ======= ==================================================== A 32 bit wide accumulator X 32 bit wide X register M[] 16 x 32 bit wide misc registers aka "scratch memory store", addressable from 0 to 15 ======= ==================================================== h]htable)}(hhh]htgroup)}(hhh](hcolspec)}(hhh]h}(h]h ]h"]h$]h&]colwidthKuh1jhjubj)}(hhh]h}(h]h ]h"]h$]h&]colwidthK4uh1jhjubhthead)}(hhh]hrow)}(hhh](hentry)}(hhh]h)}(hElementh]hElement}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h Descriptionh]h Description}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhjubhtbody)}(hhh](j)}(hhh](j)}(hhh]h)}(hAh]hA}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h32 bit wide accumulatorh]h32 bit wide accumulator}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh](j)}(hhh]h)}(hXh]hX}(hj,hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj)ubah}(h]h ]h"]h$]h&]uh1jhj&ubj)}(hhh]h)}(h32 bit wide X registerh]h32 bit wide X register}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj@ubah}(h]h ]h"]h$]h&]uh1jhj&ubeh}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh](j)}(hhh]h)}(hM[]h]hM[]}(hjchhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj`ubah}(h]h ]h"]h$]h&]uh1jhj]ubj)}(hhh]h)}(hT16 x 32 bit wide misc registers aka "scratch memory store", addressable from 0 to 15h]hX16 x 32 bit wide misc registers aka “scratch memory store”, addressable from 0 to 15}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjwubah}(h]h ]h"]h$]h&]uh1jhj]ubeh}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]colsKuh1jhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjShhubh)}(hA program, that is translated by bpf_asm into "opcodes" is an array that consists of the following elements (as already mentioned)::h]hA program, that is translated by bpf_asm into “opcodes” is an array that consists of the following elements (as already mentioned):}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjShhubj6)}(hop:16, jt:8, jf:8, k:32h]hop:16, jt:8, jf:8, k:32}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhKhjShhubh)}(hXIThe element op is a 16 bit wide opcode that has a particular instruction encoded. jt and jf are two 8 bit wide jump targets, one for condition "jump if true", the other one "jump if false". Eventually, element k contains a miscellaneous argument that can be interpreted in different ways depending on the given instruction in op.h]hXQThe element op is a 16 bit wide opcode that has a particular instruction encoded. jt and jf are two 8 bit wide jump targets, one for condition “jump if true”, the other one “jump if false”. Eventually, element k contains a miscellaneous argument that can be interpreted in different ways depending on the given instruction in op.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjShhubh)}(hX The instruction set consists of load, store, branch, alu, miscellaneous and return instructions that are also represented in bpf_asm syntax. This table lists all bpf_asm instructions available resp. what their underlying opcodes as defined in linux/filter.h stand for:h]hX The instruction set consists of load, store, branch, alu, miscellaneous and return instructions that are also represented in bpf_asm syntax. This table lists all bpf_asm instructions available resp. what their underlying opcodes as defined in linux/filter.h stand for:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjShhubj)}(hX=========== =================== ===================== Instruction Addressing mode Description =========== =================== ===================== ld 1, 2, 3, 4, 12 Load word into A ldi 4 Load word into A ldh 1, 2 Load half-word into A ldb 1, 2 Load byte into A ldx 3, 4, 5, 12 Load word into X ldxi 4 Load word into X ldxb 5 Load byte into X st 3 Store A into M[] stx 3 Store X into M[] jmp 6 Jump to label ja 6 Jump to label jeq 7, 8, 9, 10 Jump on A == jneq 9, 10 Jump on A != jne 9, 10 Jump on A != jlt 9, 10 Jump on A < jle 9, 10 Jump on A <= jgt 7, 8, 9, 10 Jump on A > jge 7, 8, 9, 10 Jump on A >= jset 7, 8, 9, 10 Jump on A & add 0, 4 A + sub 0, 4 A - mul 0, 4 A * div 0, 4 A / mod 0, 4 A % neg !A and 0, 4 A & or 0, 4 A | xor 0, 4 A ^ lsh 0, 4 A << rsh 0, 4 A >> tax Copy A into X txa Copy X into A ret 4, 11 Return =========== =================== ===================== h]j)}(hhh]j)}(hhh](j)}(hhh]h}(h]h ]h"]h$]h&]colwidthK uh1jhjubj)}(hhh]h}(h]h ]h"]h$]h&]colwidthKuh1jhjubj)}(hhh]h}(h]h ]h"]h$]h&]colwidthKuh1jhjubj)}(hhh]j)}(hhh](j)}(hhh]h)}(h Instructionh]h Instruction}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hAddressing modeh]hAddressing mode}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj*ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h Descriptionh]h Description}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjAubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh](j)}(hhh](j)}(hhh]h)}(hldh]hld}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjjubah}(h]h ]h"]h$]h&]uh1jhjgubj)}(hhh]h)}(h1, 2, 3, 4, 12h]h1, 2, 3, 4, 12}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjgubj)}(hhh]h)}(hLoad word into Ah]hLoad word into A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjgubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldih]hldi}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h4h]h4}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hLoad word into Ah]hLoad word into A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldhh]hldh}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h1, 2h]h1, 2}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hLoad half-word into Ah]hLoad half-word into A}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj4ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldbh]hldb}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjTubah}(h]h ]h"]h$]h&]uh1jhjQubj)}(hhh]h)}(h1, 2h]h1, 2}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjkubah}(h]h ]h"]h$]h&]uh1jhjQubj)}(hhh]h)}(hLoad byte into Ah]hLoad byte into A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjQubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldxh]hldx}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h 3, 4, 5, 12h]h 3, 4, 5, 12}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hLoad word into Xh]hLoad word into X}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldxih]hldxi}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hjh]h4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hLoad word into Xh]hLoad word into X}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hldxbh]hldxb}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj=ubah}(h]h ]h"]h$]h&]uh1jhj:ubj)}(hhh]h)}(h5h]h5}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjTubah}(h]h ]h"]h$]h&]uh1jhj:ubj)}(hhh]h)}(hLoad byte into Xh]hLoad byte into X}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjkubah}(h]h ]h"]h$]h&]uh1jhj:ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hsth]hst}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h3h]h3}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hStore A into M[]h]hStore A into M[]}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hstxh]hstx}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hjh]h3}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hStore X into M[]h]hStore X into M[]}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjmph]hjmp}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj&ubah}(h]h ]h"]h$]h&]uh1jhj#ubj)}(hhh]h)}(h6h]h6}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj=ubah}(h]h ]h"]h$]h&]uh1jhj#ubj)}(hhh]h)}(h Jump to labelh]h Jump to label}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjTubah}(h]h ]h"]h$]h&]uh1jhj#ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjah]hja}(hjwhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjtubah}(h]h ]h"]h$]h&]uh1jhjqubj)}(hhh]h)}(hjBh]h6}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjqubj)}(hhh]h)}(h Jump to labelh]h Jump to label}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjqubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjeqh]hjeq}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h 7, 8, 9, 10h]h 7, 8, 9, 10}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hJump on A == h]hJump on A == }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjneqh]hjneq}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h9, 10h]h9, 10}(hj) hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj& ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hJump on A != h]hJump on A != }(hj@ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj= ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjneh]hjne}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj] ubah}(h]h ]h"]h$]h&]uh1jhjZ ubj)}(hhh]h)}(h9, 10h]h9, 10}(hjw hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjt ubah}(h]h ]h"]h$]h&]uh1jhjZ ubj)}(hhh]h)}(hJump on A != h]hJump on A != }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjZ ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjlth]hjlt}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h9, 10h]h9, 10}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hJump on A < h]hJump on A < }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjleh]hjle}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h9, 10h]h9, 10}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hJump on A <= h]hJump on A <= }(hj* hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj' ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjgth]hjgt}(hjJ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjG ubah}(h]h ]h"]h$]h&]uh1jhjD ubj)}(hhh]h)}(h 7, 8, 9, 10h]h 7, 8, 9, 10}(hja hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj^ ubah}(h]h ]h"]h$]h&]uh1jhjD ubj)}(hhh]h)}(hJump on A > h]hJump on A > }(hjx hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhju ubah}(h]h ]h"]h$]h&]uh1jhjD ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjgeh]hjge}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h 7, 8, 9, 10h]h 7, 8, 9, 10}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hJump on A >= h]hJump on A >= }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hjseth]hjset}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h 7, 8, 9, 10h]h 7, 8, 9, 10}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hJump on A & h]hJump on A & }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(haddh]hadd}(hj4 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj1 ubah}(h]h ]h"]h$]h&]uh1jhj. ubj)}(hhh]h)}(h0, 4h]h0, 4}(hjK hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjH ubah}(h]h ]h"]h$]h&]uh1jhj. ubj)}(hhh]h)}(hA + h]hA + }(hjb hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj_ ubah}(h]h ]h"]h$]h&]uh1jhj. ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hsubh]hsub}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj| ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj| ubj)}(hhh]h)}(hA - h]hA - }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj| ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hmulh]hmul}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hA * h]hA * }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hdivh]hdiv}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj5 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj2 ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hA / h]hA / }(hjL hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjI ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hmodh]hmod}(hjl hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhji ubah}(h]h ]h"]h$]h&]uh1jhjf ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjf ubj)}(hhh]h)}(hA % h]hA % }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhjf ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hnegh]hneg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h!Ah]h!A}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(handh]hand}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hA & h]hA & }(hj( hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj% ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(horh]hor}(hjH hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjE ubah}(h]h ]h"]h$]h&]uh1jhjB ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj_ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj\ ubah}(h]h ]h"]h$]h&]uh1jhjB ubj)}(hhh]h)}(hA | h]hA | }(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjs ubah}(h]h ]h"]h$]h&]uh1jhjB ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hxorh]hxor}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hA ^ h]hA ^ }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hlshh]hlsh}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(h0, 4h]h0, 4}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hA << h]hA << }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hrshh]hrsh}(hj2hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj/ubah}(h]h ]h"]h$]h&]uh1jhj,ubj)}(hhh]h)}(h0, 4h]h0, 4}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjFubah}(h]h ]h"]h$]h&]uh1jhj,ubj)}(hhh]h)}(hA >> h]hA >> }(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj]ubah}(h]h ]h"]h$]h&]uh1jhj,ubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(htaxh]htax}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj}ubah}(h]h ]h"]h$]h&]uh1jhjzubj)}(hhh]h}(h]h ]h"]h$]h&]uh1jhjzubj)}(hhh]h)}(h Copy A into Xh]h Copy A into X}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjzubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(htxah]htxa}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h Copy X into Ah]h Copy X into A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubj)}(hhh](j)}(hhh]h)}(hreth]hret}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h4, 11h]h4, 11}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hReturnh]hReturn}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj+ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjdubeh}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]colsKuh1jhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhKhjShhubh)}(hlen proto skb->protocol type skb->pkt_type poff Payload start offset ifidx skb->dev->ifindex nla Netlink attribute of type X with offset A nlan Nested Netlink attribute of type X with offset A mark skb->mark queue skb->queue_mapping hatype skb->dev->type rxhash skb->hash cpu raw_smp_processor_id() vlan_tci skb_vlan_tag_get(skb) vlan_avail skb_vlan_tag_present(skb) vlan_tpid skb->vlan_proto rand get_random_u32() =================================== ================================================= h]j)}(hhh]j)}(hhh](j)}(hhh]h}(h]h ]h"]h$]h&]colwidthK#uh1jhjubj)}(hhh]h}(h]h ]h"]h$]h&]colwidthK1uh1jhjubj)}(hhh]j)}(hhh](j)}(hhh]h)}(h Extensionh]h Extension}(hj?hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM#hj<ubah}(h]h ]h"]h$]h&]uh1jhj9ubj)}(hhh]h)}(h Descriptionh]h Description}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM#hjSubah}(h]h ]h"]h$]h&]uh1jhj9ubeh}(h]h ]h"]h$]h&]uh1jhj6ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh](j)}(hhh](j)}(hhh]h)}(hlenh]hlen}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hj|ubah}(h]h ]h"]h$]h&]uh1jhjyubj)}(hhh]h)}(hskb->lenh]hskb->len}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM%hjubah}(h]h ]h"]h$]h&]uh1jhjyubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hprotoh]hproto}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM&hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h skb->protocolh]h skb->protocol}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM&hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(htypeh]htype}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM'hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h skb->pkt_typeh]h skb->pkt_type}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM'hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hpoffh]hpoff}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM(hj!ubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hPayload start offseth]hPayload start offset}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM(hj8ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hifidxh]hifidx}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM)hjXubah}(h]h ]h"]h$]h&]uh1jhjUubj)}(hhh]h)}(hskb->dev->ifindexh]hskb->dev->ifindex}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM)hjoubah}(h]h ]h"]h$]h&]uh1jhjUubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hnlah]hnla}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM*hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h)Netlink attribute of type X with offset Ah]h)Netlink attribute of type X with offset A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM*hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hnlanh]hnlan}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h0Nested Netlink attribute of type X with offset Ah]h0Nested Netlink attribute of type X with offset A}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM+hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hmarkh]hmark}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM,hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h skb->markh]h skb->mark}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM,hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hqueueh]hqueue}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM-hj4ubah}(h]h ]h"]h$]h&]uh1jhj1ubj)}(hhh]h)}(hskb->queue_mappingh]hskb->queue_mapping}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM-hjKubah}(h]h ]h"]h$]h&]uh1jhj1ubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hhatypeh]hhatype}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM.hjkubah}(h]h ]h"]h$]h&]uh1jhjhubj)}(hhh]h)}(hskb->dev->typeh]hskb->dev->type}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM.hjubah}(h]h ]h"]h$]h&]uh1jhjhubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hrxhashh]hrxhash}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM/hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(h skb->hashh]h skb->hash}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM/hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hcpuh]hcpu}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM0hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hraw_smp_processor_id()h]hraw_smp_processor_id()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM0hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hvlan_tcih]hvlan_tci}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hjubah}(h]h ]h"]h$]h&]uh1jhj ubj)}(hhh]h)}(hskb_vlan_tag_get(skb)h]hskb_vlan_tag_get(skb)}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hj'ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(h vlan_availh]h vlan_avail}(hjJhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM2hjGubah}(h]h ]h"]h$]h&]uh1jhjDubj)}(hhh]h)}(hskb_vlan_tag_present(skb)h]hskb_vlan_tag_present(skb)}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM2hj^ubah}(h]h ]h"]h$]h&]uh1jhjDubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(h vlan_tpidh]h vlan_tpid}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM3hj~ubah}(h]h ]h"]h$]h&]uh1jhj{ubj)}(hhh]h)}(hskb->vlan_protoh]hskb->vlan_proto}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM3hjubah}(h]h ]h"]h$]h&]uh1jhj{ubeh}(h]h ]h"]h$]h&]uh1jhjvubj)}(hhh](j)}(hhh]h)}(hrandh]hrand}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hjubah}(h]h ]h"]h$]h&]uh1jhjubj)}(hhh]h)}(hget_random_u32()h]hget_random_u32()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM4hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjvubeh}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]colsKuh1jhjubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhhhM"hjShhubh)}(hKThese extensions can also be prefixed with '#'. Examples for low-level BPF:h]hOThese extensions can also be prefixed with ‘#’. Examples for low-level BPF:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM7hjShhubh)}(h**ARP packets**::h](hstrong)}(h**ARP packets**h]h ARP packets}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM:hjShhubj6)}(h.ldh [12] jne #0x806, drop ret #-1 drop: ret #0h]h.ldh [12] jne #0x806, drop ret #-1 drop: ret #0}hj.sbah}(h]h ]h"]h$]h&]hhuh1j5hhhM<hjShhubh)}(h**IPv4 TCP packets**::h](j)}(h**IPv4 TCP packets**h]hIPv4 TCP packets}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj<ubh:}(hj<hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMAhjShhubj6)}(hEldh [12] jne #0x800, drop ldb [23] jneq #6, drop ret #-1 drop: ret #0h]hEldh [12] jne #0x800, drop ldb [23] jneq #6, drop ret #-1 drop: ret #0}hjXsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMChjShhubh)}(h)**icmp random packet sampling, 1 in 4**::h](j)}(h'**icmp random packet sampling, 1 in 4**h]h#icmp random packet sampling, 1 in 4}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjfubh:}(hjfhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMJhjShhubj6)}(hldh [12] jne #0x800, drop ldb [23] jneq #1, drop # get a random uint32 number ld rand mod #4 jneq #1, drop ret #-1 drop: ret #0h]hldh [12] jne #0x800, drop ldb [23] jneq #1, drop # get a random uint32 number ld rand mod #4 jneq #1, drop ret #-1 drop: ret #0}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMLhjShhubh)}(h**SECCOMP filter example**::h](j)}(h**SECCOMP filter example**h]hSECCOMP filter example}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMWhjShhubj6)}(hXld [4] /* offsetof(struct seccomp_data, arch) */ jne #0xc000003e, bad /* AUDIT_ARCH_X86_64 */ ld [0] /* offsetof(struct seccomp_data, nr) */ jeq #15, good /* __NR_rt_sigreturn */ jeq #231, good /* __NR_exit_group */ jeq #60, good /* __NR_exit */ jeq #0, good /* __NR_read */ jeq #1, good /* __NR_write */ jeq #5, good /* __NR_fstat */ jeq #9, good /* __NR_mmap */ jeq #14, good /* __NR_rt_sigprocmask */ jeq #13, good /* __NR_rt_sigaction */ jeq #35, good /* __NR_nanosleep */ bad: ret #0 /* SECCOMP_RET_KILL_THREAD */ good: ret #0x7fff0000 /* SECCOMP_RET_ALLOW */h]hXld [4] /* offsetof(struct seccomp_data, arch) */ jne #0xc000003e, bad /* AUDIT_ARCH_X86_64 */ ld [0] /* offsetof(struct seccomp_data, nr) */ jeq #15, good /* __NR_rt_sigreturn */ jeq #231, good /* __NR_exit_group */ jeq #60, good /* __NR_exit */ jeq #0, good /* __NR_read */ jeq #1, good /* __NR_write */ jeq #5, good /* __NR_fstat */ jeq #9, good /* __NR_mmap */ jeq #14, good /* __NR_rt_sigprocmask */ jeq #13, good /* __NR_rt_sigaction */ jeq #35, good /* __NR_nanosleep */ bad: ret #0 /* SECCOMP_RET_KILL_THREAD */ good: ret #0x7fff0000 /* SECCOMP_RET_ALLOW */}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMYhjShhubh)}(h%Examples for low-level BPF extension:h]h%Examples for low-level BPF extension:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMihjShhubh)}(h#**Packet for interface index 13**::h](j)}(h!**Packet for interface index 13**h]hPacket for interface index 13}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMkhjShhubj6)}(h,ld ifidx jneq #13, drop ret #-1 drop: ret #0h]h,ld ifidx jneq #13, drop ret #-1 drop: ret #0}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMmhjShhubh)}(h!**(Accelerated) VLAN w/ id 10**::h](j)}(h**(Accelerated) VLAN w/ id 10**h]h(Accelerated) VLAN w/ id 10}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMrhjShhubj6)}(h/ld vlan_tci jneq #10, drop ret #-1 drop: ret #0h]h/ld vlan_tci jneq #10, drop ret #-1 drop: ret #0}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMthjShhubh)}(hThe above example code can be placed into a file (here called "foo"), and then be passed to the bpf_asm tool for generating opcodes, output that xt_bpf and cls_bpf understands and can directly be loaded with. Example with above ARP code::h]hThe above example code can be placed into a file (here called “foo”), and then be passed to the bpf_asm tool for generating opcodes, output that xt_bpf and cls_bpf understands and can directly be loaded with. Example with above ARP code:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMyhjShhubj6)}(hA$ ./bpf_asm foo 4,40 0 0 12,21 0 1 2054,6 0 0 4294967295,6 0 0 0,h]hA$ ./bpf_asm foo 4,40 0 0 12,21 0 1 2054,6 0 0 4294967295,6 0 0 0,}hj*sbah}(h]h ]h"]h$]h&]hhuh1j5hhhM~hjShhubh)}(h!In copy and paste C-like output::h]h In copy and paste C-like output:}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjShhubj6)}(h$ ./bpf_asm -c foo { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 1, 0x00000806 }, { 0x06, 0, 0, 0xffffffff }, { 0x06, 0, 0, 0000000000 },h]h$ ./bpf_asm -c foo { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 1, 0x00000806 }, { 0x06, 0, 0, 0xffffffff }, { 0x06, 0, 0, 0000000000 },}hjFsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjShhubh)}(hXIn particular, as usage with xt_bpf or cls_bpf can result in more complex BPF filters that might not be obvious at first, it's good to test filters before attaching to a live system. For that purpose, there's a small tool called bpf_dbg under tools/bpf/ in the kernel source directory. This debugger allows for testing BPF filters against given pcap files, single stepping through the BPF code on the pcap's packets and to do BPF machine register dumps.h]hXIn particular, as usage with xt_bpf or cls_bpf can result in more complex BPF filters that might not be obvious at first, it’s good to test filters before attaching to a live system. For that purpose, there’s a small tool called bpf_dbg under tools/bpf/ in the kernel source directory. This debugger allows for testing BPF filters against given pcap files, single stepping through the BPF code on the pcap’s packets and to do BPF machine register dumps.}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjShhubh)}(h7Starting bpf_dbg is trivial and just requires issuing::h]h6Starting bpf_dbg is trivial and just requires issuing:}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjShhubj6)}(h # ./bpf_dbgh]h # ./bpf_dbg}hjpsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjShhubh)}(hIn case input and output do not equal stdin/stdout, bpf_dbg takes an alternative stdin source as a first argument, and an alternative stdout sink as a second one, e.g. `./bpf_dbg test_in.txt test_out.txt`.h](hIn case input and output do not equal stdin/stdout, bpf_dbg takes an alternative stdin source as a first argument, and an alternative stdout sink as a second one, e.g. }(hj~hhhNhNubj`)}(h$`./bpf_dbg test_in.txt test_out.txt`h]h"./bpf_dbg test_in.txt test_out.txt}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j_hj~ubh.}(hj~hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjShhubh)}(hOther than that, a particular libreadline configuration can be set via file "~/.bpf_dbg_init" and the command history is stored in the file "~/.bpf_dbg_history".h]hOther than that, a particular libreadline configuration can be set via file “~/.bpf_dbg_init” and the command history is stored in the file “~/.bpf_dbg_history”.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjShhubh)}(hInteraction in bpf_dbg happens through a shell that also has auto-completion support (follow-up example commands starting with '>' denote bpf_dbg shell). The usual workflow would be to ...h]hInteraction in bpf_dbg happens through a shell that also has auto-completion support (follow-up example commands starting with ‘>’ denote bpf_dbg shell). The usual workflow would be to ...}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjShhubj)}(hhh](j)}(hXqload bpf 6,40 0 0 12,21 0 3 2048,48 0 0 23,21 0 1 1,6 0 0 65535,6 0 0 0 Loads a BPF filter from standard output of bpf_asm, or transformed via e.g. ``tcpdump -iem1 -ddd port 22 | tr '\n' ','``. Note that for JIT debugging (next section), this command creates a temporary socket and loads the BPF code into the kernel. Thus, this will also be useful for JIT developers. h]h)}(hXpload bpf 6,40 0 0 12,21 0 3 2048,48 0 0 23,21 0 1 1,6 0 0 65535,6 0 0 0 Loads a BPF filter from standard output of bpf_asm, or transformed via e.g. ``tcpdump -iem1 -ddd port 22 | tr '\n' ','``. Note that for JIT debugging (next section), this command creates a temporary socket and loads the BPF code into the kernel. Thus, this will also be useful for JIT developers.h](hload bpf 6,40 0 0 12,21 0 3 2048,48 0 0 23,21 0 1 1,6 0 0 65535,6 0 0 0 Loads a BPF filter from standard output of bpf_asm, or transformed via e.g. }(hjhhhNhNubhliteral)}(h,``tcpdump -iem1 -ddd port 22 | tr '\n' ','``h]h(tcpdump -iem1 -ddd port 22 | tr '\n' ','}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh. Note that for JIT debugging (next section), this command creates a temporary socket and loads the BPF code into the kernel. Thus, this will also be useful for JIT developers.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h6load pcap foo.pcap Loads standard tcpdump pcap file. h](h)}(hload pcap foo.pcaph]hload pcap foo.pcap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(h!Loads standard tcpdump pcap file.h]h!Loads standard tcpdump pcap file.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h run [] h]h)}(h run []h]h run []}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]j'j(uh1jhhhMhjShhubhdefinition_list)}(hhh]hdefinition_list_item)}(hbpf passes:1 fails:9 Runs through all packets from a pcap to account how many passes and fails the filter will generate. A limit of packets to traverse can be given. h](hterm)}(hbpf passes:1 fails:9h]hbpf passes:1 fails:9}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1j8hhhMhj4ubh definition)}(hhh]h)}(hRuns through all packets from a pcap to account how many passes and fails the filter will generate. A limit of packets to traverse can be given.h]hRuns through all packets from a pcap to account how many passes and fails the filter will generate. A limit of packets to traverse can be given.}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjJubah}(h]h ]h"]h$]h&]uh1jHhj4ubeh}(h]h ]h"]h$]h&]uh1j2hhhMhj/ubah}(h]h ]h"]h$]h&]uh1j-hjShhhhhNubj)}(hhh](j)}(hdisassemble:: l0: ldh [12] l1: jeq #0x800, l2, l5 l2: ldb [23] l3: jeq #0x1, l4, l5 l4: ret #0xffff l5: ret #0 Prints out BPF code disassembly. h](h)}(h disassemble::h]h disassemble:}(hjthhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjpubj6)}(hxl0: ldh [12] l1: jeq #0x800, l2, l5 l2: ldb [23] l3: jeq #0x1, l4, l5 l4: ret #0xffff l5: ret #0h]hxl0: ldh [12] l1: jeq #0x800, l2, l5 l2: ldb [23] l3: jeq #0x1, l4, l5 l4: ret #0xffff l5: ret #0}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjpubh)}(h Prints out BPF code disassembly.h]h Prints out BPF code disassembly.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjpubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubj)}(hX"dump:: /* { op, jt, jf, k }, */ { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 3, 0x00000800 }, { 0x30, 0, 0, 0x00000017 }, { 0x15, 0, 1, 0x00000001 }, { 0x06, 0, 0, 0x0000ffff }, { 0x06, 0, 0, 0000000000 }, Prints out C-style BPF code dump. h](h)}(hdump::h]hdump:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubj6)}(h/* { op, jt, jf, k }, */ { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 3, 0x00000800 }, { 0x30, 0, 0, 0x00000017 }, { 0x15, 0, 1, 0x00000001 }, { 0x06, 0, 0, 0x0000ffff }, { 0x06, 0, 0, 0000000000 },h]h/* { op, jt, jf, k }, */ { 0x28, 0, 0, 0x0000000c }, { 0x15, 0, 3, 0x00000800 }, { 0x30, 0, 0, 0x00000017 }, { 0x15, 0, 1, 0x00000001 }, { 0x06, 0, 0, 0x0000ffff }, { 0x06, 0, 0, 0000000000 },}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjubh)}(h!Prints out C-style BPF code dump.h]h!Prints out C-style BPF code dump.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubj)}(h7breakpoint 0:: breakpoint at: l0: ldh [12] h](h)}(hbreakpoint 0::h]h breakpoint 0:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubj6)}(h breakpoint at: l0: ldh [12]h]h breakpoint at: l0: ldh [12]}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubj)}(hXbreakpoint 1:: breakpoint at: l1: jeq #0x800, l2, l5 ... Sets breakpoints at particular BPF instructions. Issuing a `run` command will walk through the pcap file continuing from the current packet and break when a breakpoint is being hit (another `run` will continue from the currently active breakpoint executing next instructions): * run:: -- register dump -- pc: [0] <-- program counter code: [40] jt[0] jf[0] k[12] <-- plain BPF code of current instruction curr: l0: ldh [12] <-- disassembly of current instruction A: [00000000][0] <-- content of A (hex, decimal) X: [00000000][0] <-- content of X (hex, decimal) M[0,15]: [00000000][0] <-- folded content of M (hex, decimal) -- packet dump -- <-- Current packet from pcap (hex) len: 42 0: 00 19 cb 55 55 a4 00 14 a4 43 78 69 08 06 00 01 16: 08 00 06 04 00 01 00 14 a4 43 78 69 0a 3b 01 26 32: 00 00 00 00 00 00 0a 3b 01 01 (breakpoint) > * breakpoint:: breakpoints: 0 1 Prints currently set breakpoints. h](h)}(hbreakpoint 1::h]h breakpoint 1:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubj6)}(h*breakpoint at: l1: jeq #0x800, l2, l5h]h*breakpoint at: l1: jeq #0x800, l2, l5}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjubh)}(h...h]h...}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(hXSets breakpoints at particular BPF instructions. Issuing a `run` command will walk through the pcap file continuing from the current packet and break when a breakpoint is being hit (another `run` will continue from the currently active breakpoint executing next instructions):h](h;Sets breakpoints at particular BPF instructions. Issuing a }(hj,hhhNhNubj`)}(h`run`h]hrun}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1j_hj,ubh~ command will walk through the pcap file continuing from the current packet and break when a breakpoint is being hit (another }(hj,hhhNhNubj`)}(h`run`h]hrun}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1j_hj,ubhQ will continue from the currently active breakpoint executing next instructions):}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubj)}(hhh](j)}(hXrun:: -- register dump -- pc: [0] <-- program counter code: [40] jt[0] jf[0] k[12] <-- plain BPF code of current instruction curr: l0: ldh [12] <-- disassembly of current instruction A: [00000000][0] <-- content of A (hex, decimal) X: [00000000][0] <-- content of X (hex, decimal) M[0,15]: [00000000][0] <-- folded content of M (hex, decimal) -- packet dump -- <-- Current packet from pcap (hex) len: 42 0: 00 19 cb 55 55 a4 00 14 a4 43 78 69 08 06 00 01 16: 08 00 06 04 00 01 00 14 a4 43 78 69 0a 3b 01 26 32: 00 00 00 00 00 00 0a 3b 01 01 (breakpoint) > h](h)}(hrun::h]hrun:}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjaubj6)}(hX-- register dump -- pc: [0] <-- program counter code: [40] jt[0] jf[0] k[12] <-- plain BPF code of current instruction curr: l0: ldh [12] <-- disassembly of current instruction A: [00000000][0] <-- content of A (hex, decimal) X: [00000000][0] <-- content of X (hex, decimal) M[0,15]: [00000000][0] <-- folded content of M (hex, decimal) -- packet dump -- <-- Current packet from pcap (hex) len: 42 0: 00 19 cb 55 55 a4 00 14 a4 43 78 69 08 06 00 01 16: 08 00 06 04 00 01 00 14 a4 43 78 69 0a 3b 01 26 32: 00 00 00 00 00 00 0a 3b 01 01 (breakpoint) >h]hX-- register dump -- pc: [0] <-- program counter code: [40] jt[0] jf[0] k[12] <-- plain BPF code of current instruction curr: l0: ldh [12] <-- disassembly of current instruction A: [00000000][0] <-- content of A (hex, decimal) X: [00000000][0] <-- content of X (hex, decimal) M[0,15]: [00000000][0] <-- folded content of M (hex, decimal) -- packet dump -- <-- Current packet from pcap (hex) len: 42 0: 00 19 cb 55 55 a4 00 14 a4 43 78 69 08 06 00 01 16: 08 00 06 04 00 01 00 14 a4 43 78 69 0a 3b 01 26 32: 00 00 00 00 00 00 0a 3b 01 01 (breakpoint) >}hjssbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjaubeh}(h]h ]h"]h$]h&]uh1jhj^ubj)}(hFbreakpoint:: breakpoints: 0 1 Prints currently set breakpoints. h](h)}(h breakpoint::h]h breakpoint:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubj6)}(hbreakpoints: 0 1h]hbreakpoints: 0 1}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjubh)}(h!Prints currently set breakpoints.h]h!Prints currently set breakpoints.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhj^ubeh}(h]h ]h"]h$]h&]j'j(uh1jhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjmhhhNhNubj)}(hX.step [-, +] Performs single stepping through the BPF program from the current pc offset. Thus, on each step invocation, above register dump is issued. This can go forwards and backwards in time, a plain `step` will break on the next BPF instruction, thus +1. (No `run` needs to be issued here.) h](h)}(hstep [-, +]h]hstep [-, +]}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(hXPerforms single stepping through the BPF program from the current pc offset. Thus, on each step invocation, above register dump is issued. This can go forwards and backwards in time, a plain `step` will break on the next BPF instruction, thus +1. (No `run` needs to be issued here.)h](hPerforms single stepping through the BPF program from the current pc offset. Thus, on each step invocation, above register dump is issued. This can go forwards and backwards in time, a plain }(hjhhhNhNubj`)}(h`step`h]hstep}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j_hjubh6 will break on the next BPF instruction, thus +1. (No }(hjhhhNhNubj`)}(h`run`h]hrun}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j_hjubh needs to be issued here.)}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubj)}(hselect Selects a given packet from the pcap file to continue from. Thus, on the next `run` or `step`, the BPF program is being evaluated against the user pre-selected packet. Numbering starts just as in Wireshark with index 1. h](h)}(h select h]h select }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubh)}(hSelects a given packet from the pcap file to continue from. Thus, on the next `run` or `step`, the BPF program is being evaluated against the user pre-selected packet. Numbering starts just as in Wireshark with index 1.h](hNSelects a given packet from the pcap file to continue from. Thus, on the next }(hj#hhhNhNubj`)}(h`run`h]hrun}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1j_hj#ubh or }(hj#hhhNhNubj`)}(h`step`h]hstep}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1j_hj#ubh~, the BPF program is being evaluated against the user pre-selected packet. Numbering starts just as in Wireshark with index 1.}(hj#hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubj)}(hquit Exits bpf_dbg. h](h)}(hquith]hquit}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj[ubh)}(hExits bpf_dbg.h]hExits bpf_dbg.}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj[ubeh}(h]h ]h"]h$]h&]uh1jhjmhhhhhNubeh}(h]h ]h"]h$]h&]j'j(uh1jhhhMhjShhubeh}(h]bpf-engine-and-instruction-setah ]h"]bpf engine and instruction setah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h JIT compilerh]h JIT compiler}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hX;The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC, ARM, ARM64, MIPS, RISC-V, s390, and ARC and can be enabled through CONFIG_BPF_JIT. The JIT compiler is transparently invoked for each attached filter from user space or for internal kernel users if it has been previously enabled by root::h]hX:The Linux kernel has a built-in BPF JIT compiler for x86_64, SPARC, PowerPC, ARM, ARM64, MIPS, RISC-V, s390, and ARC and can be enabled through CONFIG_BPF_JIT. The JIT compiler is transparently invoked for each attached filter from user space or for internal kernel users if it has been previously enabled by root:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj6)}(h*echo 1 > /proc/sys/net/core/bpf_jit_enableh]h*echo 1 > /proc/sys/net/core/bpf_jit_enable}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhM hjhhubh)}(hvFor JIT developers, doing audits etc, each compile run can output the generated opcode image into the kernel log via::h]huFor JIT developers, doing audits etc, each compile run can output the generated opcode image into the kernel log via:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubj6)}(h*echo 2 > /proc/sys/net/core/bpf_jit_enableh]h*echo 2 > /proc/sys/net/core/bpf_jit_enable}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjhhubh)}(hExample output from dmesg::h]hExample output from dmesg:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj6)}(hX[ 3389.935842] flen=6 proglen=70 pass=3 image=ffffffffa0069c8f [ 3389.935847] JIT code: 00000000: 55 48 89 e5 48 83 ec 60 48 89 5d f8 44 8b 4f 68 [ 3389.935849] JIT code: 00000010: 44 2b 4f 6c 4c 8b 87 d8 00 00 00 be 0c 00 00 00 [ 3389.935850] JIT code: 00000020: e8 1d 94 ff e0 3d 00 08 00 00 75 16 be 17 00 00 [ 3389.935851] JIT code: 00000030: 00 e8 28 94 ff e0 83 f8 01 75 07 b8 ff ff 00 00 [ 3389.935852] JIT code: 00000040: eb 02 31 c0 c9 c3h]hX[ 3389.935842] flen=6 proglen=70 pass=3 image=ffffffffa0069c8f [ 3389.935847] JIT code: 00000000: 55 48 89 e5 48 83 ec 60 48 89 5d f8 44 8b 4f 68 [ 3389.935849] JIT code: 00000010: 44 2b 4f 6c 4c 8b 87 d8 00 00 00 be 0c 00 00 00 [ 3389.935850] JIT code: 00000020: e8 1d 94 ff e0 3d 00 08 00 00 75 16 be 17 00 00 [ 3389.935851] JIT code: 00000030: 00 e8 28 94 ff e0 83 f8 01 75 07 b8 ff ff 00 00 [ 3389.935852] JIT code: 00000040: eb 02 31 c0 c9 c3}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjhhubh)}(hXvWhen CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1 and setting any other value than that will return in failure. This is even the case for setting bpf_jit_enable to 2, since dumping the final JIT image into the kernel log is discouraged and introspection through bpftool (under tools/bpf/bpftool/) is the generally recommended approach instead.h]hXvWhen CONFIG_BPF_JIT_ALWAYS_ON is enabled, bpf_jit_enable is permanently set to 1 and setting any other value than that will return in failure. This is even the case for setting bpf_jit_enable to 2, since dumping the final JIT image into the kernel log is discouraged and introspection through bpftool (under tools/bpf/bpftool/) is the generally recommended approach instead.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hIn the kernel source tree under tools/bpf/, there's bpf_jit_disasm for generating disassembly out of the kernel log's hexdump::h]hIn the kernel source tree under tools/bpf/, there’s bpf_jit_disasm for generating disassembly out of the kernel log’s hexdump:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj6)}(hX# ./bpf_jit_disasm 70 bytes emitted from JIT compiler (pass:3, flen:6) ffffffffa0069c8f + : 0: push %rbp 1: mov %rsp,%rbp 4: sub $0x60,%rsp 8: mov %rbx,-0x8(%rbp) c: mov 0x68(%rdi),%r9d 10: sub 0x6c(%rdi),%r9d 14: mov 0xd8(%rdi),%r8 1b: mov $0xc,%esi 20: callq 0xffffffffe0ff9442 25: cmp $0x800,%eax 2a: jne 0x0000000000000042 2c: mov $0x17,%esi 31: callq 0xffffffffe0ff945e 36: cmp $0x1,%eax 39: jne 0x0000000000000042 3b: mov $0xffff,%eax 40: jmp 0x0000000000000044 42: xor %eax,%eax 44: leaveq 45: retq Issuing option `-o` will "annotate" opcodes to resulting assembler instructions, which can be very useful for JIT developers: # ./bpf_jit_disasm -o 70 bytes emitted from JIT compiler (pass:3, flen:6) ffffffffa0069c8f + : 0: push %rbp 55 1: mov %rsp,%rbp 48 89 e5 4: sub $0x60,%rsp 48 83 ec 60 8: mov %rbx,-0x8(%rbp) 48 89 5d f8 c: mov 0x68(%rdi),%r9d 44 8b 4f 68 10: sub 0x6c(%rdi),%r9d 44 2b 4f 6c 14: mov 0xd8(%rdi),%r8 4c 8b 87 d8 00 00 00 1b: mov $0xc,%esi be 0c 00 00 00 20: callq 0xffffffffe0ff9442 e8 1d 94 ff e0 25: cmp $0x800,%eax 3d 00 08 00 00 2a: jne 0x0000000000000042 75 16 2c: mov $0x17,%esi be 17 00 00 00 31: callq 0xffffffffe0ff945e e8 28 94 ff e0 36: cmp $0x1,%eax 83 f8 01 39: jne 0x0000000000000042 75 07 3b: mov $0xffff,%eax b8 ff ff 00 00 40: jmp 0x0000000000000044 eb 02 42: xor %eax,%eax 31 c0 44: leaveq c9 45: retq c3h]hX# ./bpf_jit_disasm 70 bytes emitted from JIT compiler (pass:3, flen:6) ffffffffa0069c8f + : 0: push %rbp 1: mov %rsp,%rbp 4: sub $0x60,%rsp 8: mov %rbx,-0x8(%rbp) c: mov 0x68(%rdi),%r9d 10: sub 0x6c(%rdi),%r9d 14: mov 0xd8(%rdi),%r8 1b: mov $0xc,%esi 20: callq 0xffffffffe0ff9442 25: cmp $0x800,%eax 2a: jne 0x0000000000000042 2c: mov $0x17,%esi 31: callq 0xffffffffe0ff945e 36: cmp $0x1,%eax 39: jne 0x0000000000000042 3b: mov $0xffff,%eax 40: jmp 0x0000000000000044 42: xor %eax,%eax 44: leaveq 45: retq Issuing option `-o` will "annotate" opcodes to resulting assembler instructions, which can be very useful for JIT developers: # ./bpf_jit_disasm -o 70 bytes emitted from JIT compiler (pass:3, flen:6) ffffffffa0069c8f + : 0: push %rbp 55 1: mov %rsp,%rbp 48 89 e5 4: sub $0x60,%rsp 48 83 ec 60 8: mov %rbx,-0x8(%rbp) 48 89 5d f8 c: mov 0x68(%rdi),%r9d 44 8b 4f 68 10: sub 0x6c(%rdi),%r9d 44 2b 4f 6c 14: mov 0xd8(%rdi),%r8 4c 8b 87 d8 00 00 00 1b: mov $0xc,%esi be 0c 00 00 00 20: callq 0xffffffffe0ff9442 e8 1d 94 ff e0 25: cmp $0x800,%eax 3d 00 08 00 00 2a: jne 0x0000000000000042 75 16 2c: mov $0x17,%esi be 17 00 00 00 31: callq 0xffffffffe0ff945e e8 28 94 ff e0 36: cmp $0x1,%eax 83 f8 01 39: jne 0x0000000000000042 75 07 3b: mov $0xffff,%eax b8 ff ff 00 00 40: jmp 0x0000000000000044 eb 02 42: xor %eax,%eax 31 c0 44: leaveq c9 45: retq c3}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhM"hjhhubh)}(hFor BPF JIT developers, bpf_jit_disasm, bpf_asm and bpf_dbg provides a useful toolchain for developing and testing the kernel's JIT compiler.h]hFor BPF JIT developers, bpf_jit_disasm, bpf_asm and bpf_dbg provides a useful toolchain for developing and testing the kernel’s JIT compiler.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMihjhhubeh}(h] jit-compilerah ]h"] jit compilerah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hBPF kernel internalsh]hBPF kernel internals}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4hhhhhMmubh)}(hXxInternally, for the kernel interpreter, a different instruction set format with similar underlying principles from BPF described in previous paragraphs is being used. However, the instruction set format is modelled closer to the underlying architecture to mimic native instruction sets, so that a better performance can be achieved (more details later). This new ISA is called eBPF. See the ../bpf/index.rst for details. (Note: eBPF which originates from [e]xtended BPF is not the same as BPF extensions! While eBPF is an ISA, BPF extensions date back to classic BPF's 'overloading' of BPF_LD | BPF_{B,H,W} | BPF_ABS instruction.)h]hX~Internally, for the kernel interpreter, a different instruction set format with similar underlying principles from BPF described in previous paragraphs is being used. However, the instruction set format is modelled closer to the underlying architecture to mimic native instruction sets, so that a better performance can be achieved (more details later). This new ISA is called eBPF. See the ../bpf/index.rst for details. (Note: eBPF which originates from [e]xtended BPF is not the same as BPF extensions! While eBPF is an ISA, BPF extensions date back to classic BPF’s ‘overloading’ of BPF_LD | BPF_{B,H,W} | BPF_ABS instruction.)}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMnhj4hhubh)}(hX5The new instruction set was originally designed with the possible goal in mind to write programs in "restricted C" and compile into eBPF with a optional GCC/LLVM backend, so that it can just-in-time map to modern 64-bit CPUs with minimal performance overhead over two steps, that is, C -> eBPF -> native code.h]hX9The new instruction set was originally designed with the possible goal in mind to write programs in “restricted C” and compile into eBPF with a optional GCC/LLVM backend, so that it can just-in-time map to modern 64-bit CPUs with minimal performance overhead over two steps, that is, C -> eBPF -> native code.}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMxhj4hhubh)}(hXCurrently, the new format is being used for running user BPF programs, which includes seccomp BPF, classic socket filters, cls_bpf traffic classifier, team driver's classifier for its load-balancing mode, netfilter's xt_bpf extension, PTP dissector/classifier, and much more. They are all internally converted by the kernel into the new instruction set representation and run in the eBPF interpreter. For in-kernel handlers, this all works transparently by using bpf_prog_create() for setting up the filter, resp. bpf_prog_destroy() for destroying it. The function bpf_prog_run(filter, ctx) transparently invokes eBPF interpreter or JITed code to run the filter. 'filter' is a pointer to struct bpf_prog that we got from bpf_prog_create(), and 'ctx' the given context (e.g. skb pointer). All constraints and restrictions from bpf_check_classic() apply before a conversion to the new layout is being done behind the scenes!h]hXCurrently, the new format is being used for running user BPF programs, which includes seccomp BPF, classic socket filters, cls_bpf traffic classifier, team driver’s classifier for its load-balancing mode, netfilter’s xt_bpf extension, PTP dissector/classifier, and much more. They are all internally converted by the kernel into the new instruction set representation and run in the eBPF interpreter. For in-kernel handlers, this all works transparently by using bpf_prog_create() for setting up the filter, resp. bpf_prog_destroy() for destroying it. The function bpf_prog_run(filter, ctx) transparently invokes eBPF interpreter or JITed code to run the filter. ‘filter’ is a pointer to struct bpf_prog that we got from bpf_prog_create(), and ‘ctx’ the given context (e.g. skb pointer). All constraints and restrictions from bpf_check_classic() apply before a conversion to the new layout is being done behind the scenes!}(hjahhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM}hj4hhubh)}(hCurrently, the classic BPF format is being used for JITing on most 32-bit architectures, whereas x86-64, aarch64, s390x, powerpc64, sparc64, arm32, riscv64, riscv32, loongarch64, arc perform JIT compilation from eBPF instruction set.h]hCurrently, the classic BPF format is being used for JITing on most 32-bit architectures, whereas x86-64, aarch64, s390x, powerpc64, sparc64, arm32, riscv64, riscv32, loongarch64, arc perform JIT compilation from eBPF instruction set.}(hjohhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj4hhubeh}(h]bpf-kernel-internalsah ]h"]bpf kernel internalsah$]h&]uh1hhhhhhhhMmubh)}(hhh](h)}(hTestingh]hTesting}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hNext to the BPF toolchain, the kernel also ships a test module that contains various test cases for classic and eBPF that can be executed against the BPF interpreter and JIT compiler. It can be found in lib/test_bpf.c and enabled via Kconfig::h]hNext to the BPF toolchain, the kernel also ships a test module that contains various test cases for classic and eBPF that can be executed against the BPF interpreter and JIT compiler. It can be found in lib/test_bpf.c and enabled via Kconfig:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj6)}(hCONFIG_TEST_BPF=mh]hCONFIG_TEST_BPF=m}hjsbah}(h]h ]h"]h$]h&]hhuh1j5hhhMhjhhubh)}(hAfter the module has been built and installed, the test suite can be executed via insmod or modprobe against 'test_bpf' module. Results of the test cases including timings in nsec can be found in the kernel log (dmesg).h]hAfter the module has been built and installed, the test suite can be executed via insmod or modprobe against ‘test_bpf’ module. Results of the test cases including timings in nsec can be found in the kernel log (dmesg).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]testingah ]h"]testingah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hMisch]hMisc}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hdAlso trinity, the Linux syscall fuzzer, has built-in support for BPF and SECCOMP-BPF kernel fuzzing.h]hdAlso trinity, the Linux syscall fuzzer, has built-in support for BPF and SECCOMP-BPF kernel fuzzing.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]miscah ]h"]miscah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(h Written byh]h Written by}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hThe document was written in the hope that it is found useful and in order to give potential BPF hackers or security auditors a better overview of the underlying architecture.h]hThe document was written in the hope that it is found useful and in order to give potential BPF hackers or security auditors a better overview of the underlying architecture.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hhh](j)}(h Jay Schulist h]h)}(hjh](hJay Schulist <}(hjhhhNhNubj)}(hjschlst@samba.orgh]hjschlst@samba.org}(hjhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:jschlst@samba.orguh1jhjubh>}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h&Daniel Borkmann h]h)}(hj>h](hDaniel Borkmann <}(hj@hhhNhNubj)}(hdaniel@iogearbox.neth]hdaniel@iogearbox.net}(hjGhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:daniel@iogearbox.netuh1jhj@ubh>}(hj@hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj<ubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubj)}(h#Alexei Starovoitov h]h)}(hjih](hAlexei Starovoitov <}(hjkhhhNhNubj)}(hast@kernel.orgh]hast@kernel.org}(hjrhhhNhNubah}(h]h ]h"]h$]h&]refurimailto:ast@kernel.orguh1jhjkubh>}(hjkhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjgubah}(h]h ]h"]h$]h&]uh1jhjhhhhhNubeh}(h]h ]h"]h$]h&]j'-uh1jhhhMhjhhubeh}(h] written-byah ]h"] written byah$]h&]uh1hhhhhhhhMubeh}(h](5linux-socket-filtering-aka-berkeley-packet-filter-bpfheh ]h"](7linux socket filtering aka berkeley packet filter (bpf)networking-filtereh$]h&]uh1hhhhhhhhKexpect_referenced_by_name}jhsexpect_referenced_by_id}hhsubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksjfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}1]jasrefids}h]hasnameids}(jhjjhhjjjjjtjqjPjMjjj1j.jjjjjjjju nametypes}(jjhjjjtjPjj1jjjjuh}(hhjhhhjjjjjjjqjjMjwjjSj.jjj4jjjjjju footnote_refs}j]jas citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes]ja citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jKsRparse_messages]hsystem_message)}(hhh]h)}(heUnexpected possible title overline or transition. Treating it as ordinary text because it's so short.h]hgUnexpected possible title overline or transition. Treating it as ordinary text because it’s so short.}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj8ubah}(h]h ]h"]h$]h&]levelKtypeINFOlineMsourcehuh1j6hjubatransform_messages]j7)}(hhh]h)}(hhh]h7Hyperlink target "networking-filter" is not referenced.}hjYsbah}(h]h ]h"]h$]h&]uh1hhjVubah}(h]h ]h"]h$]h&]levelKtypejQsourcehlineKuh1j6uba transformerN include_log] decorationNhhub.