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/bpf/bpf_iteratorsmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/zh_TW/bpf/bpf_iteratorsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/it_IT/bpf/bpf_iteratorsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ja_JP/bpf/bpf_iteratorsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/ko_KR/bpf/bpf_iteratorsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget%/translations/sp_SP/bpf/bpf_iteratorsmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhsection)}(hhh](htitle)}(h BPF Iteratorsh]h BPF Iterators}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhh?/var/lib/git/docbuild/linux/Documentation/bpf/bpf_iterators.rsthKubh)}(hhh](h)}(h Motivationh]h Motivation}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh paragraph)}(hXWThere are a few existing ways to dump kernel data into user space. The most popular one is the ``/proc`` system. For example, ``cat /proc/net/tcp6`` dumps all tcp6 sockets in the system, and ``cat /proc/net/netlink`` dumps all netlink sockets in the system. However, their output format tends to be fixed, and if users want more information about these sockets, they have to patch the kernel, which often takes time to publish upstream and release. The same is true for popular tools like `ss `_ where any additional information needs a kernel patch.h](h_There are a few existing ways to dump kernel data into user space. The most popular one is the }(hhhhhNhNubhliteral)}(h ``/proc``h]h/proc}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh system. For example, }(hhhhhNhNubh)}(h``cat /proc/net/tcp6``h]hcat /proc/net/tcp6}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh+ dumps all tcp6 sockets in the system, and }(hhhhhNhNubh)}(h``cat /proc/net/netlink``h]hcat /proc/net/netlink}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubhX dumps all netlink sockets in the system. However, their output format tends to be fixed, and if users want more information about these sockets, they have to patch the kernel, which often takes time to publish upstream and release. The same is true for popular tools like }(hhhhhNhNubh reference)}(h7`ss `_h]hss}(hj hhhNhNubah}(h]h ]h"]h$]h&]namessrefuri/https://man7.org/linux/man-pages/man8/ss.8.htmluh1j hhubhtarget)}(h2 h]h}(h]ssah ]h"]ssah$]h&]refurijuh1j referencedKhhubh7 where any additional information needs a kernel patch.}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hXTo solve this problem, the `drgn `_ tool is often used to dig out the kernel data with no kernel change. However, the main drawback for drgn is performance, as it cannot do pointer tracing inside the kernel. In addition, drgn cannot validate a pointer value and may read invalid data if the pointer becomes invalid inside the kernel.h](hTo solve this problem, the }(hj8hhhNhNubj )}(h>`drgn `_h]hdrgn}(hj@hhhNhNubah}(h]h ]h"]h$]h&]namedrgnj4https://www.kernel.org/doc/html/latest/bpf/drgn.htmluh1j hj8ubj)}(h7 h]h}(h]drgnah ]h"]drgnah$]h&]refurijPuh1jj-Khj8ubhX* tool is often used to dig out the kernel data with no kernel change. However, the main drawback for drgn is performance, as it cannot do pointer tracing inside the kernel. In addition, drgn cannot validate a pointer value and may read invalid data if the pointer becomes invalid inside the kernel.}(hj8hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hThe BPF iterator solves the above problem by providing flexibility on what data (e.g., tasks, bpf_maps, etc.) to collect by calling BPF programs for each kernel data object.h]hThe BPF iterator solves the above problem by providing flexibility on what data (e.g., tasks, bpf_maps, etc.) to collect by calling BPF programs for each kernel data object.}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhhhubeh}(h] motivationah ]h"] motivationah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hHow BPF Iterators Workh]hHow BPF Iterators Work}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj~hhhhhK ubh)}(hXxA BPF iterator is a type of BPF program that allows users to iterate over specific types of kernel objects. Unlike traditional BPF tracing programs that allow users to define callbacks that are invoked at particular points of execution in the kernel, BPF iterators allow users to define callbacks that should be executed for every entry in a variety of kernel data structures.h]hXxA BPF iterator is a type of BPF program that allows users to iterate over specific types of kernel objects. Unlike traditional BPF tracing programs that allow users to define callbacks that are invoked at particular points of execution in the kernel, BPF iterators allow users to define callbacks that should be executed for every entry in a variety of kernel data structures.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK"hj~hhubh)}(hX(For example, users can define a BPF iterator that iterates over every task on the system and dumps the total amount of CPU runtime currently used by each of them. Another BPF task iterator may instead dump the cgroup information for each task. Such flexibility is the core value of BPF iterators.h]hX(For example, users can define a BPF iterator that iterates over every task on the system and dumps the total amount of CPU runtime currently used by each of them. Another BPF task iterator may instead dump the cgroup information for each task. Such flexibility is the core value of BPF iterators.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK(hj~hhubh)}(hXA BPF program is always loaded into the kernel at the behest of a user space process. A user space process loads a BPF program by opening and initializing the program skeleton as required and then invoking a syscall to have the BPF program verified and loaded by the kernel.h]hXA BPF program is always loaded into the kernel at the behest of a user space process. A user space process loads a BPF program by opening and initializing the program skeleton as required and then invoking a syscall to have the BPF program verified and loaded by the kernel.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK-hj~hhubh)}(hXIn traditional tracing programs, a program is activated by having user space obtain a ``bpf_link`` to the program with ``bpf_program__attach()``. Once activated, the program callback will be invoked whenever the tracepoint is triggered in the main kernel. For BPF iterator programs, a ``bpf_link`` to the program is obtained using ``bpf_link_create()``, and the program callback is invoked by issuing system calls from user space.h](hVIn traditional tracing programs, a program is activated by having user space obtain a }(hjhhhNhNubh)}(h ``bpf_link``h]hbpf_link}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh to the program with }(hjhhhNhNubh)}(h``bpf_program__attach()``h]hbpf_program__attach()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh. Once activated, the program callback will be invoked whenever the tracepoint is triggered in the main kernel. For BPF iterator programs, a }(hjhhhNhNubh)}(h ``bpf_link``h]hbpf_link}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh" to the program is obtained using }(hjhhhNhNubh)}(h``bpf_link_create()``h]hbpf_link_create()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhN, and the program callback is invoked by issuing system calls from user space.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK2hj~hhubh)}(hZNext, let us see how you can use the iterators to iterate on kernel objects and read data.h]hZNext, let us see how you can use the iterators to iterate on kernel objects and read data.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK9hj~hhubeh}(h]how-bpf-iterators-workah ]h"]how bpf iterators workah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(hHow to Use BPF iteratorsh]hHow to Use BPF iterators}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj%hhhhhK>ubh)}(hXBPF selftests are a great resource to illustrate how to use the iterators. In this section, we’ll walk through a BPF selftest which shows how to load and use a BPF iterator program. To begin, we’ll look at `bpf_iter.c `_, which illustrates how to load and trigger BPF iterators on the user space side. Later, we’ll look at a BPF program that runs in kernel space.h](hBPF selftests are a great resource to illustrate how to use the iterators. In this section, we’ll walk through a BPF selftest which shows how to load and use a BPF iterator program. To begin, we’ll look at }(hj6hhhNhNubj )}(h`bpf_iter.c `_h]h bpf_iter.c}(hj>hhhNhNubah}(h]h ]h"]h$]h&]name bpf_iter.cjwhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/prog_tests/bpf_iter.cuh1j hj6ubj)}(hz h]h}(h] bpf-iter-cah ]h"] bpf_iter.cah$]h&]refurijNuh1jj-Khj6ubh, which illustrates how to load and trigger BPF iterators on the user space side. Later, we’ll look at a BPF program that runs in kernel space.}(hj6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK@hj%hhubh)}(h\Loading a BPF iterator in the kernel from user space typically involves the following steps:h]h\Loading a BPF iterator in the kernel from user space typically involves the following steps:}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKGhj%hhubh bullet_list)}(hhh](h list_item)}(hThe BPF program is loaded into the kernel through ``libbpf``. Once the kernel has verified and loaded the program, it returns a file descriptor (fd) to user space.h]h)}(hThe BPF program is loaded into the kernel through ``libbpf``. Once the kernel has verified and loaded the program, it returns a file descriptor (fd) to user space.h](h2The BPF program is loaded into the kernel through }(hjhhhNhNubh)}(h ``libbpf``h]hlibbpf}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhg. Once the kernel has verified and loaded the program, it returns a file descriptor (fd) to user space.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKJhj{ubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubjz)}(hObtain a ``link_fd`` to the BPF program by calling the ``bpf_link_create()`` specified with the BPF program file descriptor received from the kernel.h]h)}(hObtain a ``link_fd`` to the BPF program by calling the ``bpf_link_create()`` specified with the BPF program file descriptor received from the kernel.h](h Obtain a }(hjhhhNhNubh)}(h ``link_fd``h]hlink_fd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh# to the BPF program by calling the }(hjhhhNhNubh)}(h``bpf_link_create()``h]hbpf_link_create()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhI specified with the BPF program file descriptor received from the kernel.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKMhjubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubjz)}(hNext, obtain a BPF iterator file descriptor (``bpf_iter_fd``) by calling the ``bpf_iter_create()`` specified with the ``bpf_link`` received from Step 2.h]h)}(hNext, obtain a BPF iterator file descriptor (``bpf_iter_fd``) by calling the ``bpf_iter_create()`` specified with the ``bpf_link`` received from Step 2.h](h-Next, obtain a BPF iterator file descriptor (}(hjhhhNhNubh)}(h``bpf_iter_fd``h]h bpf_iter_fd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh) by calling the }(hjhhhNhNubh)}(h``bpf_iter_create()``h]hbpf_iter_create()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh specified with the }(hjhhhNhNubh)}(h ``bpf_link``h]hbpf_link}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh received from Step 2.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKOhjubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubjz)}(hRTrigger the iteration by calling ``read(bpf_iter_fd)`` until no data is available.h]h)}(hRTrigger the iteration by calling ``read(bpf_iter_fd)`` until no data is available.h](h!Trigger the iteration by calling }(hj3hhhNhNubh)}(h``read(bpf_iter_fd)``h]hread(bpf_iter_fd)}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj3ubh until no data is available.}(hj3hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKQhj/ubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubjz)}(h3Close the iterator fd using ``close(bpf_iter_fd)``.h]h)}(hj[h](hClose the iterator fd using }(hj]hhhNhNubh)}(h``close(bpf_iter_fd)``h]hclose(bpf_iter_fd)}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj]ubh.}(hj]hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKShjYubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubjz)}(hOIf needed to reread the data, get a new ``bpf_iter_fd`` and do the read again. h]h)}(hNIf needed to reread the data, get a new ``bpf_iter_fd`` and do the read again.h](h(If needed to reread the data, get a new }(hjhhhNhNubh)}(h``bpf_iter_fd``h]h bpf_iter_fd}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh and do the read again.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKThjubah}(h]h ]h"]h$]h&]uh1jyhjvhhhhhNubeh}(h]h ]h"]h$]h&]bullet*uh1jthhhKJhj%hhubh)}(hCThe following are a few examples of selftest BPF iterator programs:h]hCThe following are a few examples of selftest BPF iterator programs:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKVhj%hhubju)}(hhh](jz)}(h`bpf_iter_tcp4.c `_h]h)}(hjh](j )}(hjh]hbpf_iter_tcp4.c}(hjhhhNhNubah}(h]h ]h"]h$]h&]namebpf_iter_tcp4.cjwhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_tcp4.cuh1j hjubj)}(hz h]h}(h]bpf-iter-tcp4-cah ]h"]bpf_iter_tcp4.cah$]h&]refurijuh1jj-Khjubeh}(h]h ]h"]h$]h&]uh1hhhhKXhjubah}(h]h ]h"]h$]h&]uh1jyhjhhhhhNubjz)}(h`bpf_iter_task_vmas.c `_h]h)}(hjh](j )}(hjh]hbpf_iter_task_vmas.c}(hjhhhNhNubah}(h]h ]h"]h$]h&]namebpf_iter_task_vmas.cj|https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_task_vmas.cuh1j hjubj)}(h h]h}(h]bpf-iter-task-vmas-cah ]h"]bpf_iter_task_vmas.cah$]h&]refurij uh1jj-Khjubeh}(h]h ]h"]h$]h&]uh1hhhhKYhjubah}(h]h ]h"]h$]h&]uh1jyhjhhhhhNubjz)}(h`bpf_iter_task_file.c `_ h]h)}(h`bpf_iter_task_file.c `_h](j )}(hj+h]hbpf_iter_task_file.c}(hj-hhhNhNubah}(h]h ]h"]h$]h&]namebpf_iter_task_file.cj|https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_task_file.cuh1j hj)ubj)}(h h]h}(h]bpf-iter-task-file-cah ]h"]bpf_iter_task_file.cah$]h&]refurij<uh1jj-Khj)ubeh}(h]h ]h"]h$]h&]uh1hhhhKZhj%ubah}(h]h ]h"]h$]h&]uh1jyhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1jthhhKXhj%hhubh)}(hDLet us look at ``bpf_iter_task_file.c``, which runs in kernel space:h](hLet us look at }(hj\hhhNhNubh)}(h``bpf_iter_task_file.c``h]hbpf_iter_task_file.c}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj\ubh, which runs in kernel space:}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK\hj%hhubh)}(hX@Here is the definition of ``bpf_iter__task_file`` in `vmlinux.h `_. Any struct name in ``vmlinux.h`` in the format ``bpf_iter__`` represents a BPF iterator. The suffix ```` represents the type of iterator.h](hHere is the definition of }(hj|hhhNhNubh)}(h``bpf_iter__task_file``h]hbpf_iter__task_file}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|ubh in }(hj|hhhNhNubj )}(hj`vmlinux.h `_h]h vmlinux.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]name vmlinux.hj[https://facebookmicrosites.github.io/bpf/blog/2020/02/19/bpf-portability-and-co-re.html#btfuh1j hj|ubj)}(h^ h]h}(h] vmlinux-hah ]h"] vmlinux.hah$]h&]refurijuh1jj-Khj|ubh. Any struct name in }(hj|hhhNhNubh)}(h ``vmlinux.h``h]h vmlinux.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|ubh in the format }(hj|hhhNhNubh)}(h``bpf_iter__``h]hbpf_iter__}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|ubh' represents a BPF iterator. The suffix }(hj|hhhNhNubh)}(h````h]h }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj|ubh! represents the type of iterator.}(hj|hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK^hj%hhubh literal_block)}(hstruct bpf_iter__task_file { union { struct bpf_iter_meta *meta; }; union { struct task_struct *task; }; u32 fd; union { struct file *file; }; };h]hstruct bpf_iter__task_file { union { struct bpf_iter_meta *meta; }; union { struct task_struct *task; }; u32 fd; union { struct file *file; }; };}hjsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1jhhhKfhj%hhubh)}(hXIn the above code, the field 'meta' contains the metadata, which is the same for all BPF iterator programs. The rest of the fields are specific to different iterators. For example, for task_file iterators, the kernel layer provides the 'task', 'fd' and 'file' field values. The 'task' and 'file' are `reference counted `_, so they won't go away when the BPF program runs.h](hXDIn the above code, the field ‘meta’ contains the metadata, which is the same for all BPF iterator programs. The rest of the fields are specific to different iterators. For example, for task_file iterators, the kernel layer provides the ‘task’, ‘fd’ and ‘file’ field values. The ‘task’ and ‘file’ are }(hjhhhNhNubj )}(h`reference counted `_h]hreference counted}(hjhhhNhNubah}(h]h ]h"]h$]h&]namereference countedjuhttps://facebookmicrosites.github.io/bpf/blog/2018/08/31/object-lifetime.html#file-descriptors-and-reference-countersuh1j hjubj)}(hx h]h}(h]reference-countedah ]h"]reference countedah$]h&]refurijuh1jj-Khjubh4, so they won’t go away when the BPF program runs.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKshj%hhubh)}(h:Here is a snippet from the ``bpf_iter_task_file.c`` file:h](hHere is a snippet from the }(hj6hhhNhNubh)}(h``bpf_iter_task_file.c``h]hbpf_iter_task_file.c}(hj>hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj6ubh file:}(hj6hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK{hj%hhubj)}(hXSEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { struct seq_file *seq = ctx->meta->seq; struct task_struct *task = ctx->task; struct file *file = ctx->file; __u32 fd = ctx->fd; if (task == NULL || file == NULL) return 0; if (ctx->meta->seq_num == 0) { count = 0; BPF_SEQ_PRINTF(seq, " tgid gid fd file\n"); } if (tgid == task->tgid && task->tgid != task->pid) count++; if (last_tgid != task->tgid) { last_tgid = task->tgid; unique_tgid_count++; } BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }h]hXSEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { struct seq_file *seq = ctx->meta->seq; struct task_struct *task = ctx->task; struct file *file = ctx->file; __u32 fd = ctx->fd; if (task == NULL || file == NULL) return 0; if (ctx->meta->seq_num == 0) { count = 0; BPF_SEQ_PRINTF(seq, " tgid gid fd file\n"); } if (tgid == task->tgid && task->tgid != task->pid) count++; if (last_tgid != task->tgid) { last_tgid = task->tgid; unique_tgid_count++; } BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }}hjVsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj%hhubh)}(hIn the above example, the section name ``SEC(iter/task_file)``, indicates that the program is a BPF iterator program to iterate all files from all tasks. The context of the program is ``bpf_iter__task_file`` struct.h](h'In the above example, the section name }(hjdhhhNhNubh)}(h``SEC(iter/task_file)``h]hSEC(iter/task_file)}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjdubhz, indicates that the program is a BPF iterator program to iterate all files from all tasks. The context of the program is }(hjdhhhNhNubh)}(h``bpf_iter__task_file``h]hbpf_iter__task_file}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjdubh struct.}(hjdhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubh)}(hXThe user space program invokes the BPF iterator program running in the kernel by issuing a ``read()`` syscall. Once invoked, the BPF program can export data to user space using a variety of BPF helper functions. You can use either ``bpf_seq_printf()`` (and BPF_SEQ_PRINTF helper macro) or ``bpf_seq_write()`` function based on whether you need formatted output or just binary data, respectively. For binary-encoded data, the user space applications can process the data from ``bpf_seq_write()`` as needed. For the formatted data, you can use ``cat `` to print the results similar to ``cat /proc/net/netlink`` after pinning the BPF iterator to the bpffs mount. Later, use ``rm -f `` to remove the pinned iterator.h](h[The user space program invokes the BPF iterator program running in the kernel by issuing a }(hjhhhNhNubh)}(h ``read()``h]hread()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh syscall. Once invoked, the BPF program can export data to user space using a variety of BPF helper functions. You can use either }(hjhhhNhNubh)}(h``bpf_seq_printf()``h]hbpf_seq_printf()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh& (and BPF_SEQ_PRINTF helper macro) or }(hjhhhNhNubh)}(h``bpf_seq_write()``h]hbpf_seq_write()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh function based on whether you need formatted output or just binary data, respectively. For binary-encoded data, the user space applications can process the data from }(hjhhhNhNubh)}(h``bpf_seq_write()``h]hbpf_seq_write()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0 as needed. For the formatted data, you can use }(hjhhhNhNubh)}(h``cat ``h]h cat }(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh! to print the results similar to }(hjhhhNhNubh)}(h``cat /proc/net/netlink``h]hcat /proc/net/netlink}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh@ after pinning the BPF iterator to the bpffs mount. Later, use }(hjhhhNhNubh)}(h``rm -f ``h]h rm -f }(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh to remove the pinned iterator.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubh)}(hFor example, you can use the following command to create a BPF iterator from the ``bpf_iter_ipv6_route.o`` object file and pin it to the ``/sys/fs/bpf/my_route`` path:h](hQFor example, you can use the following command to create a BPF iterator from the }(hj"hhhNhNubh)}(h``bpf_iter_ipv6_route.o``h]hbpf_iter_ipv6_route.o}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"ubh object file and pin it to the }(hj"hhhNhNubh)}(h``/sys/fs/bpf/my_route``h]h/sys/fs/bpf/my_route}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"ubh path:}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubj)}(h@$ bpftool iter pin ./bpf_iter_ipv6_route.o /sys/fs/bpf/my_routeh]h@$ bpftool iter pin ./bpf_iter_ipv6_route.o /sys/fs/bpf/my_route}hjTsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj%hhubh)}(h;And then print out the results using the following command:h]h;And then print out the results using the following command:}(hjbhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj%hhubj)}(h$ cat /sys/fs/bpf/my_routeh]h$ cat /sys/fs/bpf/my_route}hjpsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhj%hhubeh}(h]how-to-use-bpf-iteratorsah ]h"]how to use bpf iteratorsah$]h&]uh1hhhhhhhhK>ubh)}(hhh](h)}(h7Implement Kernel Support for BPF Iterator Program Typesh]h7Implement Kernel Support for BPF Iterator Program Types}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hTo implement a BPF iterator in the kernel, the developer must make a one-time change to the following key data structure defined in the `bpf.h `_ file.h](hTo implement a BPF iterator in the kernel, the developer must make a one-time change to the following key data structure defined in the }(hjhhhNhNubj )}(hd`bpf.h `_h]hbpf.h}(hjhhhNhNubah}(h]h ]h"]h$]h&]namebpf.hjYhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/include/linux/bpf.huh1j hjubj)}(h\ h]h}(h]bpf-hah ]h"]bpf.hah$]h&]refurijuh1jj-Khjubh file.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubj)}(hXstruct bpf_iter_reg { const char *target; bpf_iter_attach_target_t attach_target; bpf_iter_detach_target_t detach_target; bpf_iter_show_fdinfo_t show_fdinfo; bpf_iter_fill_link_info_t fill_link_info; bpf_iter_get_func_proto_t get_func_proto; u32 ctx_arg_info_size; u32 feature; struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX]; const struct bpf_iter_seq_info *seq_info; };h]hXstruct bpf_iter_reg { const char *target; bpf_iter_attach_target_t attach_target; bpf_iter_detach_target_t detach_target; bpf_iter_show_fdinfo_t show_fdinfo; bpf_iter_fill_link_info_t fill_link_info; bpf_iter_get_func_proto_t get_func_proto; u32 ctx_arg_info_size; u32 feature; struct bpf_ctx_arg_aux ctx_arg_info[BPF_ITER_CTX_ARG_MAX]; const struct bpf_iter_seq_info *seq_info; };}hjsbah}(h]h ]h"]h$]h&]jjuh1jhhhKhjhhubh)}(hAfter filling the data structure fields, call ``bpf_iter_reg_target()`` to register the iterator to the main BPF iterator subsystem.h](h.After filling the data structure fields, call }(hjhhhNhNubh)}(h``bpf_iter_reg_target()``h]hbpf_iter_reg_target()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh= to register the iterator to the main BPF iterator subsystem.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hIThe following is the breakdown for each field in struct ``bpf_iter_reg``.h](h8The following is the breakdown for each field in struct }(hjhhhNhNubh)}(h``bpf_iter_reg``h]h bpf_iter_reg}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubhtable)}(hhh]htgroup)}(hhh](hcolspec)}(hhh]h}(h]h ]h"]h$]h&]colwidthKuh1jhjubj )}(hhh]h}(h]h ]h"]h$]h&]j*K2uh1jhjubhthead)}(hhh]hrow)}(hhh](hentry)}(hhh]h)}(hFieldsh]hFields}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj@ubah}(h]h ]h"]h$]h&]uh1j>hj;ubj?)}(hhh]h)}(h Descriptionh]h Description}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjWubah}(h]h ]h"]h$]h&]uh1j>hj;ubeh}(h]h ]h"]h$]h&]uh1j9hj6ubah}(h]h ]h"]h$]h&]uh1j4hjubhtbody)}(hhh](j:)}(hhh](j?)}(hhh]h)}(htargeth]htarget}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubj?)}(hhh]h)}(hSpecifies the name of the BPF iterator. For example: ``bpf_map``, ``bpf_map_elem``. The name should be different from other ``bpf_iter`` target names in the kernel.h](h5Specifies the name of the BPF iterator. For example: }(hjhhhNhNubh)}(h ``bpf_map``h]hbpf_map}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh, }(hjhhhNhNubh)}(h``bpf_map_elem``h]h bpf_map_elem}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh*. The name should be different from other }(hjhhhNhNubh)}(h ``bpf_iter``h]hbpf_iter}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh target names in the kernel.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(hattach_target and detach_targeth]hattach_target and detach_target}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubj?)}(hhh]h)}(hAllows for target specific ``link_create`` action since some targets may need special processing. Called during the user space link_create stage.h](hAllows for target specific }(hj hhhNhNubh)}(h``link_create``h]h link_create}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhg action since some targets may need special processing. Called during the user space link_create stage.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(hshow_fdinfo and fill_link_infoh]hshow_fdinfo and fill_link_info}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj8ubah}(h]h ]h"]h$]h&]uh1j>hj5ubj?)}(hhh]h)}(hiCalled to fill target specific information when user tries to get link info associated with the iterator.h]hiCalled to fill target specific information when user tries to get link info associated with the iterator.}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjOubah}(h]h ]h"]h$]h&]uh1j>hj5ubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(hget_func_protoh]hget_func_proto}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjoubah}(h]h ]h"]h$]h&]uh1j>hjlubj?)}(hhh]h)}(hFPermits a BPF iterator to access BPF helpers specific to the iterator.h]hFPermits a BPF iterator to access BPF helpers specific to the iterator.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjlubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(h"ctx_arg_info_size and ctx_arg_infoh]h"ctx_arg_info_size and ctx_arg_info}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubj?)}(hhh]h)}(hYSpecifies the verifier states for BPF program arguments associated with the bpf iterator.h]hYSpecifies the verifier states for BPF program arguments associated with the bpf iterator.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(hfeatureh]hfeature}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubj?)}(hhh]h)}(hSpecifies certain action requests in the kernel BPF iterator infrastructure. Currently, only BPF_ITER_RESCHED is supported. This means that the kernel function cond_resched() is called to avoid other kernel subsystem (e.g., rcu) misbehaving.h]hSpecifies certain action requests in the kernel BPF iterator infrastructure. Currently, only BPF_ITER_RESCHED is supported. This means that the kernel function cond_resched() is called to avoid other kernel subsystem (e.g., rcu) misbehaving.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1j>hjubeh}(h]h ]h"]h$]h&]uh1j9hj|ubj:)}(hhh](j?)}(hhh]h)}(hseq_infoh]hseq_info}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1j>hj ubj?)}(hhh]h)}(hSpecifies the set of seq operations for the BPF iterator and helpers to initialize/free the private data for the corresponding ``seq_file``.h](hSpecifies the set of seq operations for the BPF iterator and helpers to initialize/free the private data for the corresponding }(hj. hhhNhNubh)}(h ``seq_file``h]hseq_file}(hj6 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj. ubh.}(hj. hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj+ ubah}(h]h ]h"]h$]h&]uh1j>hj ubeh}(h]h ]h"]h$]h&]uh1j9hj|ubeh}(h]h ]h"]h$]h&]uh1jzhjubeh}(h]h ]h"]h$]h&]colsKuh1jhjubah}(h]h ]colwidths-givenah"]h$]h&]uh1jhjhhhNhNubh)}(h`Click here `_ to see an implementation of the ``task_vma`` BPF iterator in the kernel.h](j )}(hY`Click here `_h]h Click here}(hjr hhhNhNubah}(h]h ]h"]h$]h&]name Click herejIhttps://lore.kernel.org/bpf/20210212183107.50963-2-songliubraving@fb.com/uh1j hjn ubj)}(hL h]h}(h] click-hereah ]h"] click hereah$]h&]refurij uh1jj-Khjn ubh! to see an implementation of the }(hjn hhhNhNubh)}(h ``task_vma``h]htask_vma}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjn ubh BPF iterator in the kernel.}(hjn hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]7implement-kernel-support-for-bpf-iterator-program-typesah ]h"]7implement kernel support for bpf iterator program typesah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h!Parameterizing BPF Task Iteratorsh]h!Parameterizing BPF Task Iterators}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubh)}(hXBy default, BPF iterators walk through all the objects of the specified types (processes, cgroups, maps, etc.) across the entire system to read relevant kernel data. But often, there are cases where we only care about a much smaller subset of iterable kernel objects, such as only iterating tasks within a specific process. Therefore, BPF iterator programs support filtering out objects from iteration by allowing user space to configure the iterator program when it is attached.h]hXBy default, BPF iterators walk through all the objects of the specified types (processes, cgroups, maps, etc.) across the entire system to read relevant kernel data. But often, there are cases where we only care about a much smaller subset of iterable kernel objects, such as only iterating tasks within a specific process. Therefore, BPF iterator programs support filtering out objects from iteration by allowing user space to configure the iterator program when it is attached.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]!parameterizing-bpf-task-iteratorsah ]h"]!parameterizing bpf task iteratorsah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(hBPF Task Iterator Programh]hBPF Task Iterator Program}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hThe following code is a BPF iterator program to print files and task information through the ``seq_file`` of the iterator. It is a standard BPF iterator program that visits every file of an iterator. We will use this BPF program in our example later.h](h]The following code is a BPF iterator program to print files and task information through the }(hj hhhNhNubh)}(h ``seq_file``h]hseq_file}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh of the iterator. It is a standard BPF iterator program that visits every file of an iterator. We will use this BPF program in our example later.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hX~#include #include char _license[] SEC("license") = "GPL"; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { struct seq_file *seq = ctx->meta->seq; struct task_struct *task = ctx->task; struct file *file = ctx->file; __u32 fd = ctx->fd; if (task == NULL || file == NULL) return 0; if (ctx->meta->seq_num == 0) { BPF_SEQ_PRINTF(seq, " tgid pid fd file\n"); } BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }h]hX~#include #include char _license[] SEC("license") = "GPL"; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { struct seq_file *seq = ctx->meta->seq; struct task_struct *task = ctx->task; struct file *file = ctx->file; __u32 fd = ctx->fd; if (task == NULL || file == NULL) return 0; if (ctx->meta->seq_num == 0) { BPF_SEQ_PRINTF(seq, " tgid pid fd file\n"); } BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj hhubeh}(h]bpf-task-iterator-programah ]h"]bpf task iterator programah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(h(Creating a File Iterator with Parametersh]h(Creating a File Iterator with Parameters}(hj% hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj" hhhhhM'ubh)}(hTNow, let us look at how to create an iterator that includes only files of a process.h]hTNow, let us look at how to create an iterator that includes only files of a process.}(hj3 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM)hj" hhubh)}(h@First, fill the ``bpf_iter_attach_opts`` struct as shown below:h](hFirst, fill the }(hjA hhhNhNubh)}(h``bpf_iter_attach_opts``h]hbpf_iter_attach_opts}(hjI hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjA ubh struct as shown below:}(hjA hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM,hj" hhubj)}(hLIBBPF_OPTS(bpf_iter_attach_opts, opts); union bpf_iter_link_info linfo; memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo);h]hLIBBPF_OPTS(bpf_iter_attach_opts, opts); union bpf_iter_link_info linfo; memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo);}hja sbah}(h]h ]h"]h$]h&]jjuh1jhhhM0hj" hhubh)}(hX``linfo.task.pid``, if it is non-zero, directs the kernel to create an iterator that only includes opened files for the process with the specified ``pid``. In this example, we will only be iterating files for our process. If ``linfo.task.pid`` is zero, the iterator will visit every opened file of every process. Similarly, ``linfo.task.tid`` directs the kernel to create an iterator that visits opened files of a specific thread, not a process. In this example, ``linfo.task.tid`` is different from ``linfo.task.pid`` only if the thread has a separate file descriptor table. In most circumstances, all process threads share a single file descriptor table.h](h)}(h``linfo.task.pid``h]hlinfo.task.pid}(hjs hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh, if it is non-zero, directs the kernel to create an iterator that only includes opened files for the process with the specified }(hjo hhhNhNubh)}(h``pid``h]hpid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubhG. In this example, we will only be iterating files for our process. If }(hjo hhhNhNubh)}(h``linfo.task.pid``h]hlinfo.task.pid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubhQ is zero, the iterator will visit every opened file of every process. Similarly, }(hjo hhhNhNubh)}(h``linfo.task.tid``h]hlinfo.task.tid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubhy directs the kernel to create an iterator that visits opened files of a specific thread, not a process. In this example, }(hjo hhhNhNubh)}(h``linfo.task.tid``h]hlinfo.task.tid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh is different from }(hjo hhhNhNubh)}(h``linfo.task.pid``h]hlinfo.task.pid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjo ubh only if the thread has a separate file descriptor table. In most circumstances, all process threads share a single file descriptor table.}(hjo hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM7hj" hhubh)}(h`Now, in the userspace program, pass the pointer of struct to the ``bpf_program__attach_iter()``.h](hANow, in the userspace program, pass the pointer of struct to the }(hj hhhNhNubh)}(h``bpf_program__attach_iter()``h]hbpf_program__attach_iter()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMAhj" hhubj)}(h\link = bpf_program__attach_iter(prog, &opts); iter_fd = bpf_iter_create(bpf_link__fd(link));h]h\link = bpf_program__attach_iter(prog, &opts); iter_fd = bpf_iter_create(bpf_link__fd(link));}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMFhj" hhubh)}(hXIf both *tid* and *pid* are zero, an iterator created from this struct ``bpf_iter_attach_opts`` will include every opened file of every task in the system (in the namespace, actually.) It is the same as passing a NULL as the second argument to ``bpf_program__attach_iter()``.h](hIf both }(hj hhhNhNubhemphasis)}(h*tid*h]htid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh and }(hj hhhNhNubj )}(h*pid*h]hpid}(hj/ hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh0 are zero, an iterator created from this struct }(hj hhhNhNubh)}(h``bpf_iter_attach_opts``h]hbpf_iter_attach_opts}(hjA hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh will include every opened file of every task in the system (in the namespace, actually.) It is the same as passing a NULL as the second argument to }(hj hhhNhNubh)}(h``bpf_program__attach_iter()``h]hbpf_program__attach_iter()}(hjS hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMIhj" hhubh)}(h0The whole program looks like the following code:h]h0The whole program looks like the following code:}(hjk hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMNhj" hhubj)}(hXf#include #include #include #include #include "bpf_iter_task_ex.skel.h" static int do_read_opts(struct bpf_program *prog, struct bpf_iter_attach_opts *opts) { struct bpf_link *link; char buf[16] = {}; int iter_fd = -1, len; int ret = 0; link = bpf_program__attach_iter(prog, opts); if (!link) { fprintf(stderr, "bpf_program__attach_iter() fails\n"); return -1; } iter_fd = bpf_iter_create(bpf_link__fd(link)); if (iter_fd < 0) { fprintf(stderr, "bpf_iter_create() fails\n"); ret = -1; goto free_link; } /* not check contents, but ensure read() ends without error */ while ((len = read(iter_fd, buf, sizeof(buf) - 1)) > 0) { buf[len] = 0; printf("%s", buf); } printf("\n"); free_link: if (iter_fd >= 0) close(iter_fd); bpf_link__destroy(link); return 0; } static void test_task_file(void) { LIBBPF_OPTS(bpf_iter_attach_opts, opts); struct bpf_iter_task_ex *skel; union bpf_iter_link_info linfo; skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); printf("PID %d\n", getpid()); do_read_opts(skel->progs.dump_task_file, &opts); bpf_iter_task_ex__destroy(skel); } int main(int argc, const char * const * argv) { test_task_file(); return 0; }h]hXf#include #include #include #include #include "bpf_iter_task_ex.skel.h" static int do_read_opts(struct bpf_program *prog, struct bpf_iter_attach_opts *opts) { struct bpf_link *link; char buf[16] = {}; int iter_fd = -1, len; int ret = 0; link = bpf_program__attach_iter(prog, opts); if (!link) { fprintf(stderr, "bpf_program__attach_iter() fails\n"); return -1; } iter_fd = bpf_iter_create(bpf_link__fd(link)); if (iter_fd < 0) { fprintf(stderr, "bpf_iter_create() fails\n"); ret = -1; goto free_link; } /* not check contents, but ensure read() ends without error */ while ((len = read(iter_fd, buf, sizeof(buf) - 1)) > 0) { buf[len] = 0; printf("%s", buf); } printf("\n"); free_link: if (iter_fd >= 0) close(iter_fd); bpf_link__destroy(link); return 0; } static void test_task_file(void) { LIBBPF_OPTS(bpf_iter_attach_opts, opts); struct bpf_iter_task_ex *skel; union bpf_iter_link_info linfo; skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); printf("PID %d\n", getpid()); do_read_opts(skel->progs.dump_task_file, &opts); bpf_iter_task_ex__destroy(skel); } int main(int argc, const char * const * argv) { test_task_file(); return 0; }}hjy sbah}(h]h ]h"]h$]h&]jjuh1jhhhMRhj" hhubh)}(h5The following lines are the output of the program. ::h]h2The following lines are the output of the program.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj" hhubj)}(hXPID 1859 tgid pid fd file 1859 1859 0 ffffffff82270aa0 1859 1859 1 ffffffff82270aa0 1859 1859 2 ffffffff82270aa0 1859 1859 3 ffffffff82272980 1859 1859 4 ffffffff8225e120 1859 1859 5 ffffffff82255120 1859 1859 6 ffffffff82254f00 1859 1859 7 ffffffff82254d80 1859 1859 8 ffffffff8225abe0h]hXPID 1859 tgid pid fd file 1859 1859 0 ffffffff82270aa0 1859 1859 1 ffffffff82270aa0 1859 1859 2 ffffffff82270aa0 1859 1859 3 ffffffff82272980 1859 1859 4 ffffffff8225e120 1859 1859 5 ffffffff82255120 1859 1859 6 ffffffff82254f00 1859 1859 7 ffffffff82254d80 1859 1859 8 ffffffff8225abe0}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj" hhubeh}(h](creating-a-file-iterator-with-parametersah ]h"](creating a file iterator with parametersah$]h&]uh1hhhhhhhhM'ubh)}(hhh](h)}(hWithout Parametersh]hWithout Parameters}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXeLet us look at how a BPF iterator without parameters skips files of other processes in the system. In this case, the BPF program has to check the pid or the tid of tasks, or it will receive every opened file in the system (in the current *pid* namespace, actually). So, we usually add a global variable in the BPF program to pass a *pid* to the BPF program.h](hLet us look at how a BPF iterator without parameters skips files of other processes in the system. In this case, the BPF program has to check the pid or the tid of tasks, or it will receive every opened file in the system (in the current }(hj hhhNhNubj )}(h*pid*h]hpid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubhY namespace, actually). So, we usually add a global variable in the BPF program to pass a }(hj hhhNhNubj )}(h*pid*h]hpid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh to the BPF program.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h4The BPF program would look like the following block.h]h4The BPF program would look like the following block.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh block_quote)}(hX:: ...... int target_pid = 0; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { ...... if (task->tgid != target_pid) /* Check task->pid instead to check thread IDs */ return 0; BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; } h]j)}(hXg...... int target_pid = 0; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { ...... if (task->tgid != target_pid) /* Check task->pid instead to check thread IDs */ return 0; BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }h]hXg...... int target_pid = 0; SEC("iter/task_file") int dump_task_file(struct bpf_iter__task_file *ctx) { ...... if (task->tgid != target_pid) /* Check task->pid instead to check thread IDs */ return 0; BPF_SEQ_PRINTF(seq, "%8d %8d %8d %lx\n", task->tgid, task->pid, fd, (long)file->f_op); return 0; }}hj sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj ubah}(h]h ]h"]h$]h&]uh1j hhhMhj hhubh)}(h;The user space program would look like the following block:h]h;The user space program would look like the following block:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj )}(hX:: ...... static void test_task_file(void) { ...... skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; skel->bss->target_pid = getpid(); /* process ID. For thread id, use gettid() */ memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); ...... } h]j)}(hX...... static void test_task_file(void) { ...... skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; skel->bss->target_pid = getpid(); /* process ID. For thread id, use gettid() */ memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); ...... }h]hX...... static void test_task_file(void) { ...... skel = bpf_iter_task_ex__open_and_load(); if (skel == NULL) return; skel->bss->target_pid = getpid(); /* process ID. For thread id, use gettid() */ memset(&linfo, 0, sizeof(linfo)); linfo.task.pid = getpid(); opts.link_info = &linfo; opts.link_info_len = sizeof(linfo); ...... }}hj( sbah}(h]h ]h"]h$]h&]jjuh1jhhhMhj$ ubah}(h]h ]h"]h$]h&]uh1j hhhMhj hhubh)}(hX5``target_pid`` is a global variable in the BPF program. The user space program should initialize the variable with a process ID to skip opened files of other processes in the BPF program. When you parametrize a BPF iterator, the iterator calls the BPF program fewer times which can save significant resources.h](h)}(h``target_pid``h]h target_pid}(hj@ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj< ubhX' is a global variable in the BPF program. The user space program should initialize the variable with a process ID to skip opened files of other processes in the BPF program. When you parametrize a BPF iterator, the iterator calls the BPF program fewer times which can save significant resources.}(hj< hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]without-parametersah ]h"]without parametersah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hParametrizing VMA Iteratorsh]hParametrizing VMA Iterators}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj` hhhhhMubh)}(hX#By default, a BPF VMA iterator includes every VMA in every process. However, you can still specify a process or a thread to include only its VMAs. Unlike files, a thread can not have a separate address space (since Linux 2.6.0-test6). Here, using *tid* makes no difference from using *pid*.h](hBy default, a BPF VMA iterator includes every VMA in every process. However, you can still specify a process or a thread to include only its VMAs. Unlike files, a thread can not have a separate address space (since Linux 2.6.0-test6). Here, using }(hjq hhhNhNubj )}(h*tid*h]htid}(hjy hhhNhNubah}(h]h ]h"]h$]h&]uh1j hjq ubh makes no difference from using }(hjq hhhNhNubj )}(h*pid*h]hpid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hjq ubh.}(hjq hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj` hhubeh}(h]parametrizing-vma-iteratorsah ]h"]parametrizing vma iteratorsah$]h&]uh1hhhhhhhhMubh)}(hhh](h)}(hParametrizing Task Iteratorsh]hParametrizing Task Iterators}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hA BPF task iterator with *pid* includes all tasks (threads) of a process. The BPF program receives these tasks one after another. You can specify a BPF task iterator with *tid* parameter to include only the tasks that match the given *tid*.h](hA BPF task iterator with }(hj hhhNhNubj )}(h*pid*h]hpid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh includes all tasks (threads) of a process. The BPF program receives these tasks one after another. You can specify a BPF task iterator with }(hj hhhNhNubj )}(h*tid*h]htid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh: parameter to include only the tasks that match the given }(hj hhhNhNubj )}(h*tid*h]htid}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]parametrizing-task-iteratorsah ]h"]parametrizing task iteratorsah$]h&]uh1hhhhhhhhMubeh}(h] bpf-iteratorsah ]h"] bpf iteratorsah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksj>footnote_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_handlerj2 error_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}refids}nameids}(j j j{jxj)j&jZjWj"jjjjXjUjjjjjFjCjjj(j%j j jjj j j j j j j j j] jZ j j j j u nametypes}(j j{j)jZj"jjXjjjFjj(j jj j j j j] j j uh}(j hjxhj&j jWjQjj~jj%jUjOjjjj jCj=jjj%jj jjjj j j j j j j j" jZ j j j` j j u footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}Rparse_messages]hsystem_message)}(hhh]h)}(hfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.h]hhPossible title underline, too short for the title. Treating it as ordinary text because it’s so short.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubah}(h]h ]h"]h$]h&]levelKtypeINFOlineMsourcehuh1j hj" hhhhhMubatransform_messages6] transformerN include_log] decorationNhhub.