€•þŒsphinx.addnodes”Œdocument”“”)�”}”(Œ rawsource”Œ”Œchildren”]”(Œ translations”Œ LanguagesNode”“”)�”}”(hhh]”(hŒ pending_xref”“”)�”}”(hhh]”Œdocutils.nodes”ŒText”“”ŒChinese (Simplified)”…”�”}”Œparent”hsbaŒ attributes”}”(Œids”]”Œclasses”]”Œnames”]”Œdupnames”]”Œbackrefs”]”Œ refdomain”Œstd”Œreftype”Œdoc”Œ reftarget”Œ%/translations/zh_CN/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuŒtagname”hhh ubh)�”}”(hhh]”hŒChinese (Traditional)”…”�”}”hh2sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/zh_TW/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒItalian”…”�”}”hhFsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/it_IT/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒJapanese”…”�”}”hhZsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/ja_JP/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒKorean”…”�”}”hhnsbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/ko_KR/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒPortuguese (Brazilian)”…”�”}”hh‚sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/pt_BR/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubh)�”}”(hhh]”hŒSpanish”…”�”}”hh–sbah}”(h]”h ]”h"]”h$]”h&]”Œ refdomain”h)Œreftype”h+Œ reftarget”Œ%/translations/sp_SP/bpf/bpf_iterators”Œmodname”NŒ classname”NŒ refexplicit”ˆuh1hhh ubeh}”(h]”h ]”h"]”h$]”h&]”Œcurrent_language”ŒEnglish”uh1h hhŒ _document”hŒsource”NŒline”NubhŒsection”“”)�”}”(hhh]”(hŒtitle”“”)�”}”(hŒ BPF Iterators”h]”hŒ BPF Iterators”…”�”}”(hh¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhh·h²hh³Œ?/var/lib/git/docbuild/linux/Documentation/bpf/bpf_iterators.rst”h´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒOverview”h]”hŒOverview”…”�”}”(hhÎh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhhËh²hh³hÊh´KubhŒ paragraph”“”)�”}”(hX?BPF supports two separate entities collectively known as "BPF iterators": BPF iterator *program type* and *open-coded* BPF iterators. The former is a stand-alone BPF program type which, when attached and activated by user, will be called once for each entity (task_struct, cgroup, etc) that is being iterated. The latter is a set of BPF-side APIs implementing iterator functionality and available across multiple BPF program types. Open-coded iterators provide similar functionality to BPF iterator programs, but gives more flexibility and control to all other BPF program types. BPF iterator programs, on the other hand, can be used to implement anonymous or BPF FS-mounted special files, whose contents are generated by attached BPF iterator program, backed by seq_file functionality. Both are useful depending on specific needs.”h]”(hŒ[BPF supports two separate entities collectively known as “BPF iteratorsâ€�: BPF iterator ”…”�”}”(hhÞh²hh³Nh´NubhŒemphasis”“”)�”}”(hŒ*program type*”h]”hŒ program type”…”�”}”(hhèh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhhÞubhŒ and ”…”�”}”(hhÞh²hh³Nh´Nubhç)�”}”(hŒ *open-coded*”h]”hŒ open-coded”…”�”}”(hhúh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhhÞubhXÉ BPF iterators. The former is a stand-alone BPF program type which, when attached and activated by user, will be called once for each entity (task_struct, cgroup, etc) that is being iterated. The latter is a set of BPF-side APIs implementing iterator functionality and available across multiple BPF program types. Open-coded iterators provide similar functionality to BPF iterator programs, but gives more flexibility and control to all other BPF program types. BPF iterator programs, on the other hand, can be used to implement anonymous or BPF FS-mounted special files, whose contents are generated by attached BPF iterator program, backed by seq_file functionality. Both are useful depending on specific needs.”…”�”}”(hhÞh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K hhËh²hubhÝ)�”}”(hXWhen adding a new BPF iterator program, it is expected that similar functionality will be added as open-coded iterator for maximum flexibility. It's also expected that iteration logic and code will be maximally shared and reused between two iterator API surfaces.”h]”hX When adding a new BPF iterator program, it is expected that similar functionality will be added as open-coded iterator for maximum flexibility. It’s also expected that iteration logic and code will be maximally shared and reused between two iterator API surfaces.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KhhËh²hubeh}”(h]”Œoverview”ah ]”h"]”Œoverview”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒOpen-coded BPF Iterators”h]”hŒOpen-coded BPF Iterators”…”�”}”(hj+h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj(h²hh³hÊh´KubhÝ)�”}”(hX;Open-coded BPF iterators are implemented as tightly-coupled trios of kfuncs (constructor, next element fetch, destructor) and iterator-specific type describing on-the-stack iterator state, which is guaranteed by the BPF verifier to not be tampered with outside of the corresponding constructor/destructor/next APIs.”h]”hX;Open-coded BPF iterators are implemented as tightly-coupled trios of kfuncs (constructor, next element fetch, destructor) and iterator-specific type describing on-the-stack iterator state, which is guaranteed by the BPF verifier to not be tampered with outside of the corresponding constructor/destructor/next APIs.”…”�”}”(hj9h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Khj(h²hubhÝ)�”}”(hXzEach kind of open-coded BPF iterator has its own associated struct bpf_iter_, where denotes a specific type of iterator. bpf_iter_ state needs to live on BPF program stack, so make sure it's small enough to fit on BPF stack. For performance reasons its best to avoid dynamic memory allocation for iterator state and size the state struct big enough to fit everything necessary. But if necessary, dynamic memory allocation is a way to bypass BPF stack limitations. Note, state struct size is part of iterator's user-visible API, so changing it will break backwards compatibility, so be deliberate about designing it.”h]”hX~Each kind of open-coded BPF iterator has its own associated struct bpf_iter_, where denotes a specific type of iterator. bpf_iter_ state needs to live on BPF program stack, so make sure it’s small enough to fit on BPF stack. For performance reasons its best to avoid dynamic memory allocation for iterator state and size the state struct big enough to fit everything necessary. But if necessary, dynamic memory allocation is a way to bypass BPF stack limitations. Note, state struct size is part of iterator’s user-visible API, so changing it will break backwards compatibility, so be deliberate about designing it.”…”�”}”(hjGh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K%hj(h²hubhÝ)�”}”(hXeAll kfuncs (constructor, next, destructor) have to be named consistently as bpf_iter__{new,next,destroy}(), respectively. represents iterator type, and iterator state should be represented as a matching `struct bpf_iter_` state type. Also, all iter kfuncs should have a pointer to this `struct bpf_iter_` as the very first argument.”h]”(hŒØAll kfuncs (constructor, next, destructor) have to be named consistently as bpf_iter__{new,next,destroy}(), respectively. represents iterator type, and iterator state should be represented as a matching ”…”�”}”(hjUh²hh³Nh´NubhŒtitle_reference”“”)�”}”(hŒ`struct bpf_iter_`”h]”hŒstruct bpf_iter_”…”�”}”(hj_h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjUubhŒA state type. Also, all iter kfuncs should have a pointer to this ”…”�”}”(hjUh²hh³Nh´Nubj^)�”}”(hŒ`struct bpf_iter_`”h]”hŒstruct bpf_iter_”…”�”}”(hjqh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjUubhŒ as the very first argument.”…”�”}”(hjUh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K/hj(h²hubhŒdefinition_list”“”)�”}”(hhh]”hŒdefinition_list_item”“”)�”}”(hXTAdditionally: - Constructor, i.e., `bpf_iter__new()`, can have arbitrary extra number of arguments. Return type is not enforced either. - Next method, i.e., `bpf_iter__next()`, has to return a pointer type and should have exactly one argument: `struct bpf_iter_ *` (const/volatile/restrict and typedefs are ignored). - Destructor, i.e., `bpf_iter__destroy()`, should return void and should have exactly one argument, similar to the next method. - `struct bpf_iter_` size is enforced to be positive and a multiple of 8 bytes (to fit stack slots correctly). ”h]”(hŒterm”“”)�”}”(hŒ Additionally:”h]”hŒ Additionally:”…”�”}”(hj–h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j”h³hÊh´K>hj�ubhŒ definition”“”)�”}”(hhh]”hŒ bullet_list”“”)�”}”(hhh]”(hŒ list_item”“”)�”}”(hŒ}Constructor, i.e., `bpf_iter__new()`, can have arbitrary extra number of arguments. Return type is not enforced either.”h]”hÝ)�”}”(hŒ}Constructor, i.e., `bpf_iter__new()`, can have arbitrary extra number of arguments. Return type is not enforced either.”h]”(hŒConstructor, i.e., ”…”�”}”(hj´h²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__new()`”h]”hŒbpf_iter__new()”…”�”}”(hj¼h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj´ubhŒS, can have arbitrary extra number of arguments. Return type is not enforced either.”…”�”}”(hj´h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K6hj°ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj«ubj¯)�”}”(hŒ¾Next method, i.e., `bpf_iter__next()`, has to return a pointer type and should have exactly one argument: `struct bpf_iter_ *` (const/volatile/restrict and typedefs are ignored).”h]”hÝ)�”}”(hŒ¾Next method, i.e., `bpf_iter__next()`, has to return a pointer type and should have exactly one argument: `struct bpf_iter_ *` (const/volatile/restrict and typedefs are ignored).”h]”(hŒNext method, i.e., ”…”�”}”(hjÞh²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__next()`”h]”hŒbpf_iter__next()”…”�”}”(hjæh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjÞubhŒE, has to return a pointer type and should have exactly one argument: ”…”�”}”(hjÞh²hh³Nh´Nubj^)�”}”(hŒ`struct bpf_iter_ *`”h]”hŒstruct bpf_iter_ *”…”�”}”(hjøh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjÞubhŒ4 (const/volatile/restrict and typedefs are ignored).”…”�”}”(hjÞh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K8hjÚubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj«ubj¯)�”}”(hŒƒDestructor, i.e., `bpf_iter__destroy()`, should return void and should have exactly one argument, similar to the next method.”h]”hÝ)�”}”(hŒƒDestructor, i.e., `bpf_iter__destroy()`, should return void and should have exactly one argument, similar to the next method.”h]”(hŒDestructor, i.e., ”…”�”}”(hjh²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__destroy()`”h]”hŒbpf_iter__destroy()”…”�”}”(hj"h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjubhŒV, should return void and should have exactly one argument, similar to the next method.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K;hjubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj«ubj¯)�”}”(hŒs`struct bpf_iter_` size is enforced to be positive and a multiple of 8 bytes (to fit stack slots correctly). ”h]”hÝ)�”}”(hŒr`struct bpf_iter_` size is enforced to be positive and a multiple of 8 bytes (to fit stack slots correctly).”h]”(j^)�”}”(hŒ`struct bpf_iter_`”h]”hŒstruct bpf_iter_”…”�”}”(hjHh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjDubhŒZ size is enforced to be positive and a multiple of 8 bytes (to fit stack slots correctly).”…”�”}”(hjDh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K=hj@ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj«ubeh}”(h]”h ]”h"]”h$]”h&]”Œbullet”Œ-”uh1j©h³hÊh´K6hj¦ubah}”(h]”h ]”h"]”h$]”h&]”uh1j¤hj�ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jŽh³hÊh´K>hj‹ubah}”(h]”h ]”h"]”h$]”h&]”uh1j‰hj(h²hh³Nh´NubhÝ)�”}”(hXSuch strictness and consistency allows to build generic helpers abstracting important, but boilerplate, details to be able to use open-coded iterators effectively and ergonomically (see libbpf's bpf_for_each() macro). This is enforced at kfunc registration point by the kernel.”h]”hXSuch strictness and consistency allows to build generic helpers abstracting important, but boilerplate, details to be able to use open-coded iterators effectively and ergonomically (see libbpf’s bpf_for_each() macro). This is enforced at kfunc registration point by the kernel.”…”�”}”(hj€h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K@hj(h²hubjŠ)�”}”(hhh]”j�)�”}”(hX§Constructor/next/destructor implementation contract is as follows: - constructor, `bpf_iter__new()`, always initializes iterator state on the stack. If any of the input arguments are invalid, constructor should make sure to still initialize it such that subsequent next() calls will return NULL. I.e., on error, *return error and construct empty iterator*. Constructor kfunc is marked with KF_ITER_NEW flag. - next method, `bpf_iter__next()`, accepts pointer to iterator state and produces an element. Next method should always return a pointer. The contract between BPF verifier is that next method *guarantees* that it will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls *should keep returning NULL*. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as NULL-returning kfunc, of course). - destructor, `bpf_iter__destroy()`, is always called once. Even if constructor failed or next returned nothing. Destructor frees up any resources and marks stack space used by `struct bpf_iter_` as usable for something else. Destructor is marked with KF_ITER_DESTROY flag. ”h]”(j•)�”}”(hŒBConstructor/next/destructor implementation contract is as follows:”h]”hŒBConstructor/next/destructor implementation contract is as follows:”…”�”}”(hj•h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j”h³hÊh´KWhj‘ubj¥)�”}”(hhh]”jª)�”}”(hhh]”(j¯)�”}”(hXYconstructor, `bpf_iter__new()`, always initializes iterator state on the stack. If any of the input arguments are invalid, constructor should make sure to still initialize it such that subsequent next() calls will return NULL. I.e., on error, *return error and construct empty iterator*. Constructor kfunc is marked with KF_ITER_NEW flag. ”h]”hÝ)�”}”(hXXconstructor, `bpf_iter__new()`, always initializes iterator state on the stack. If any of the input arguments are invalid, constructor should make sure to still initialize it such that subsequent next() calls will return NULL. I.e., on error, *return error and construct empty iterator*. Constructor kfunc is marked with KF_ITER_NEW flag.”h]”(hŒ constructor, ”…”�”}”(hj­h²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__new()`”h]”hŒbpf_iter__new()”…”�”}”(hjµh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj­ubhŒÕ, always initializes iterator state on the stack. If any of the input arguments are invalid, constructor should make sure to still initialize it such that subsequent next() calls will return NULL. I.e., on error, ”…”�”}”(hj­h²hh³Nh´Nubhç)�”}”(hŒ+*return error and construct empty iterator*”h]”hŒ)return error and construct empty iterator”…”�”}”(hjÇh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj­ubhŒ4. Constructor kfunc is marked with KF_ITER_NEW flag.”…”�”}”(hj­h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KFhj©ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj¦ubj¯)�”}”(hXÌnext method, `bpf_iter__next()`, accepts pointer to iterator state and produces an element. Next method should always return a pointer. The contract between BPF verifier is that next method *guarantees* that it will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls *should keep returning NULL*. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as NULL-returning kfunc, of course). ”h]”hÝ)�”}”(hXËnext method, `bpf_iter__next()`, accepts pointer to iterator state and produces an element. Next method should always return a pointer. The contract between BPF verifier is that next method *guarantees* that it will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls *should keep returning NULL*. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as NULL-returning kfunc, of course).”h]”(hŒ next method, ”…”�”}”(hjéh²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__next()`”h]”hŒbpf_iter__next()”…”�”}”(hjñh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hjéubhŒŸ, accepts pointer to iterator state and produces an element. Next method should always return a pointer. The contract between BPF verifier is that next method ”…”�”}”(hjéh²hh³Nh´Nubhç)�”}”(hŒ *guarantees*”h]”hŒ guarantees”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjéubhŒo that it will eventually return NULL when elements are exhausted. Once NULL is returned, subsequent next calls ”…”�”}”(hjéh²hh³Nh´Nubhç)�”}”(hŒ*should keep returning NULL*”h]”hŒshould keep returning NULL”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjéubhŒp. Next method is marked with KF_ITER_NEXT (and should also have KF_RET_NULL as NULL-returning kfunc, of course).”…”�”}”(hjéh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KLhjåubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj¦ubj¯)�”}”(hXdestructor, `bpf_iter__destroy()`, is always called once. Even if constructor failed or next returned nothing. Destructor frees up any resources and marks stack space used by `struct bpf_iter_` as usable for something else. Destructor is marked with KF_ITER_DESTROY flag. ”h]”hÝ)�”}”(hXdestructor, `bpf_iter__destroy()`, is always called once. Even if constructor failed or next returned nothing. Destructor frees up any resources and marks stack space used by `struct bpf_iter_` as usable for something else. Destructor is marked with KF_ITER_DESTROY flag.”h]”(hŒ destructor, ”…”�”}”(hj7h²hh³Nh´Nubj^)�”}”(hŒ`bpf_iter__destroy()`”h]”hŒbpf_iter__destroy()”…”�”}”(hj?h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj7ubhŒ�, is always called once. Even if constructor failed or next returned nothing. Destructor frees up any resources and marks stack space used by ”…”�”}”(hj7h²hh³Nh´Nubj^)�”}”(hŒ`struct bpf_iter_`”h]”hŒstruct bpf_iter_”…”�”}”(hjQh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj7ubhŒN as usable for something else. Destructor is marked with KF_ITER_DESTROY flag.”…”�”}”(hj7h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KThj3ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hj¦ubeh}”(h]”h ]”h"]”h$]”h&]”jljmuh1j©h³hÊh´KFhj£ubah}”(h]”h ]”h"]”h$]”h&]”uh1j¤hj‘ubeh}”(h]”h ]”h"]”h$]”h&]”uh1jŽh³hÊh´KWhjŽubah}”(h]”h ]”h"]”h$]”h&]”uh1j‰hj(h²hh³Nh´NubhÝ)�”}”(hX,Any open-coded BPF iterator implementation has to implement at least these three methods. It is enforced that for any given type of iterator only applicable constructor/destructor/next are callable. I.e., verifier ensures you can't pass number iterator state into, say, cgroup iterator's next method.”h]”hX0Any open-coded BPF iterator implementation has to implement at least these three methods. It is enforced that for any given type of iterator only applicable constructor/destructor/next are callable. I.e., verifier ensures you can’t pass number iterator state into, say, cgroup iterator’s next method.”…”�”}”(hj‡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KYhj(h²hubhÝ)�”}”(hX/From a 10,000-feet BPF verification point of view, next methods are the points of forking a verification state, which are conceptually similar to what verifier is doing when validating conditional jumps. Verifier is branching out `call bpf_iter__next` instruction and simulates two outcomes: NULL (iteration is done) and non-NULL (new element is returned). NULL is simulated first and is supposed to reach exit without looping. After that non-NULL case is validated and it either reaches exit (for trivial examples with no real loop), or reaches another `call bpf_iter__next` instruction with the state equivalent to already (partially) validated one. State equivalency at that point means we technically are going to be looping forever without "breaking out" out of established "state envelope" (i.e., subsequent iterations don't add any new knowledge or constraints to the verifier state, so running 1, 2, 10, or a million of them doesn't matter). But taking into account the contract stating that iterator next method *has to* return NULL eventually, we can conclude that loop body is safe and will eventually terminate. Given we validated logic outside of the loop (NULL case), and concluded that loop body is safe (though potentially looping many times), verifier can claim safety of the overall program logic.”h]”(hŒæFrom a 10,000-feet BPF verification point of view, next methods are the points of forking a verification state, which are conceptually similar to what verifier is doing when validating conditional jumps. Verifier is branching out ”…”�”}”(hj•h²hh³Nh´Nubj^)�”}”(hŒ`call bpf_iter__next`”h]”hŒcall bpf_iter__next”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj•ubhX/ instruction and simulates two outcomes: NULL (iteration is done) and non-NULL (new element is returned). NULL is simulated first and is supposed to reach exit without looping. After that non-NULL case is validated and it either reaches exit (for trivial examples with no real loop), or reaches another ”…”�”}”(hj•h²hh³Nh´Nubj^)�”}”(hŒ`call bpf_iter__next`”h]”hŒcall bpf_iter__next”…”�”}”(hj¯h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1j]hj•ubhXÊ instruction with the state equivalent to already (partially) validated one. State equivalency at that point means we technically are going to be looping forever without “breaking outâ€� out of established “state envelopeâ€� (i.e., subsequent iterations don’t add any new knowledge or constraints to the verifier state, so running 1, 2, 10, or a million of them doesn’t matter). But taking into account the contract stating that iterator next method ”…”�”}”(hj•h²hh³Nh´Nubhç)�”}”(hŒ*has to*”h]”hŒhas to”…”�”}”(hjÁh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj•ubhX return NULL eventually, we can conclude that loop body is safe and will eventually terminate. Given we validated logic outside of the loop (NULL case), and concluded that loop body is safe (though potentially looping many times), verifier can claim safety of the overall program logic.”…”�”}”(hj•h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K^hj(h²hubeh}”(h]”Œopen-coded-bpf-iterators”ah ]”h"]”Œopen-coded bpf iterators”ah$]”h&]”uh1hµhh·h²hh³hÊh´Kubh¶)�”}”(hhh]”(h»)�”}”(hŒBPF Iterators Motivation”h]”hŒBPF Iterators Motivation”…”�”}”(hjäh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjáh²hh³hÊh´KsubhÝ)�”}”(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 ”…”�”}”(hjòh²hh³Nh´NubhŒliteral”“”)�”}”(hŒ ``/proc``”h]”hŒ/proc”…”�”}”(hjüh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjòubhŒ system. For example, ”…”�”}”(hjòh²hh³Nh´Nubjû)�”}”(hŒ``cat /proc/net/tcp6``”h]”hŒcat /proc/net/tcp6”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjòubhŒ+ dumps all tcp6 sockets in the system, and ”…”�”}”(hjòh²hh³Nh´Nubjû)�”}”(hŒ``cat /proc/net/netlink``”h]”hŒcat /proc/net/netlink”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjòubhX 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 ”…”�”}”(hjòh²hh³Nh´NubhŒ reference”“”)�”}”(hŒ7`ss `_”h]”hŒss”…”�”}”(hj4h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œss”Œrefuri”Œ/https://man7.org/linux/man-pages/man8/ss.8.html”uh1j2hjòubhŒtarget”“”)�”}”(hŒ2 ”h]”h}”(h]”Œss”ah ]”h"]”Œss”ah$]”h&]”Œrefuri”jEuh1jFŒ referenced”KhjòubhŒ7 where any additional information needs a kernel patch.”…”�”}”(hjòh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Kuhjáh²hubhÝ)�”}”(hXƒTo 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]”(hŒTo solve this problem, the ”…”�”}”(hj`h²hh³Nh´Nubj3)�”}”(hŒ>`drgn `_”h]”hŒdrgn”…”�”}”(hjhh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œdrgn”jDŒ4https://www.kernel.org/doc/html/latest/bpf/drgn.html”uh1j2hj`ubjG)�”}”(hŒ7 ”h]”h}”(h]”Œdrgn”ah ]”h"]”Œdrgn”ah$]”h&]”Œrefuri”jxuh1jFjUKhj`ubhX* 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.”…”�”}”(hj`h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K~hjáh²hubhÝ)�”}”(hŒ­The 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]”hŒ­The 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.”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K…hjáh²hubeh}”(h]”Œbpf-iterators-motivation”ah ]”h"]”Œbpf iterators motivation”ah$]”h&]”uh1hµhh·h²hh³hÊh´Ksubh¶)�”}”(hhh]”(h»)�”}”(hŒHow BPF Iterators Work”h]”hŒHow BPF Iterators Work”…”�”}”(hj©h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj¦h²hh³hÊh´K‹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.”…”�”}”(hj·h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K�hj¦h²hubhÝ)�”}”(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.”…”�”}”(hjÅh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K“hj¦h²hubhÝ)�”}”(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.”…”�”}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K˜hj¦h²hubhÝ)�”}”(hX®In 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]”(hŒVIn traditional tracing programs, a program is activated by having user space obtain a ”…”�”}”(hjáh²hh³Nh´Nubjû)�”}”(hŒ ``bpf_link``”h]”hŒbpf_link”…”�”}”(hjéh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjáubhŒ to the program with ”…”�”}”(hjáh²hh³Nh´Nubjû)�”}”(hŒ``bpf_program__attach()``”h]”hŒbpf_program__attach()”…”�”}”(hjûh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjáubhŒ�. Once activated, the program callback will be invoked whenever the tracepoint is triggered in the main kernel. For BPF iterator programs, a ”…”�”}”(hjáh²hh³Nh´Nubjû)�”}”(hŒ ``bpf_link``”h]”hŒbpf_link”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjáubhŒ" to the program is obtained using ”…”�”}”(hjáh²hh³Nh´Nubjû)�”}”(hŒ``bpf_link_create()``”h]”hŒbpf_link_create()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjáubhŒN, and the program callback is invoked by issuing system calls from user space.”…”�”}”(hjáh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K�hj¦h²hubhÝ)�”}”(hŒZNext, let us see how you can use the iterators to iterate on kernel objects and read data.”h]”hŒZNext, let us see how you can use the iterators to iterate on kernel objects and read data.”…”�”}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¤hj¦h²hubeh}”(h]”Œhow-bpf-iterators-work”ah ]”h"]”Œhow bpf iterators work”ah$]”h&]”uh1hµhh·h²hh³hÊh´K‹ubh¶)�”}”(hhh]”(h»)�”}”(hŒHow to Use BPF iterators”h]”hŒHow to Use BPF iterators”…”�”}”(hjPh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjMh²hh³hÊh´K©ubhÝ)�”}”(hXìBPF 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]”(hŒÔBPF 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 ”…”�”}”(hj^h²hh³Nh´Nubj3)�”}”(hŒ‡`bpf_iter.c `_”h]”hŒ bpf_iter.c”…”�”}”(hjfh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œ bpf_iter.c”jDŒwhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/prog_tests/bpf_iter.c”uh1j2hj^ubjG)�”}”(hŒz ”h]”h}”(h]”Œ bpf-iter-c”ah ]”h"]”Œ bpf_iter.c”ah$]”h&]”Œrefuri”jvuh1jFjUKhj^ubhŒ‘, 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.”…”�”}”(hj^h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K«hjMh²hubhÝ)�”}”(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:”…”�”}”(hjŽh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K²hjMh²hubjª)�”}”(hhh]”(j¯)�”}”(hŒ£The 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Ý)�”}”(hŒ£The 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Œ2The BPF program is loaded into the kernel through ”…”�”}”(hj£h²hh³Nh´Nubjû)�”}”(hŒ ``libbpf``”h]”hŒlibbpf”…”�”}”(hj«h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj£ubhŒg. Once the kernel has verified and loaded the program, it returns a file descriptor (fd) to user space.”…”�”}”(hj£h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KµhjŸubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubj¯)�”}”(hŒ•Obtain 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Ý)�”}”(hŒ•Obtain 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 ”…”�”}”(hjÍh²hh³Nh´Nubjû)�”}”(hŒ ``link_fd``”h]”hŒlink_fd”…”�”}”(hjÕh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjÍubhŒ# to the BPF program by calling the ”…”�”}”(hjÍh²hh³Nh´Nubjû)�”}”(hŒ``bpf_link_create()``”h]”hŒbpf_link_create()”…”�”}”(hjçh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjÍubhŒI specified with the BPF program file descriptor received from the kernel.”…”�”}”(hjÍh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¸hjÉubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubj¯)�”}”(hŒ˜Next, 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Ý)�”}”(hŒ˜Next, 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 (”…”�”}”(hj h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_fd``”h]”hŒ bpf_iter_fd”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj ubhŒ) by calling the ”…”�”}”(hj h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_create()``”h]”hŒbpf_iter_create()”…”�”}”(hj#h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj ubhŒ specified with the ”…”�”}”(hj h²hh³Nh´Nubjû)�”}”(hŒ ``bpf_link``”h]”hŒbpf_link”…”�”}”(hj5h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj ubhŒ received from Step 2.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Kºhjubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubj¯)�”}”(hŒRTrigger the iteration by calling ``read(bpf_iter_fd)`` until no data is available.”h]”hÝ)�”}”(hŒRTrigger the iteration by calling ``read(bpf_iter_fd)`` until no data is available.”h]”(hŒ!Trigger the iteration by calling ”…”�”}”(hjWh²hh³Nh´Nubjû)�”}”(hŒ``read(bpf_iter_fd)``”h]”hŒread(bpf_iter_fd)”…”�”}”(hj_h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjWubhŒ until no data is available.”…”�”}”(hjWh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¼hjSubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubj¯)�”}”(hŒ3Close the iterator fd using ``close(bpf_iter_fd)``.”h]”hÝ)�”}”(hjh]”(hŒClose the iterator fd using ”…”�”}”(hj�h²hh³Nh´Nubjû)�”}”(hŒ``close(bpf_iter_fd)``”h]”hŒclose(bpf_iter_fd)”…”�”}”(hjˆh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj�ubhŒ.”…”�”}”(hj�h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¾hj}ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubj¯)�”}”(hŒOIf needed to reread the data, get a new ``bpf_iter_fd`` and do the read again. ”h]”hÝ)�”}”(hŒNIf 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 ”…”�”}”(hjªh²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_fd``”h]”hŒ bpf_iter_fd”…”�”}”(hj²h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjªubhŒ and do the read again.”…”�”}”(hjªh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´K¿hj¦ubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjœh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”jlŒ*”uh1j©h³hÊh´KµhjMh²hubhÝ)�”}”(hŒCThe following are a few examples of selftest BPF iterator programs:”h]”hŒCThe following are a few examples of selftest BPF iterator programs:”…”�”}”(hj×h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÁhjMh²hubjª)�”}”(hhh]”(j¯)�”}”(hŒŒ`bpf_iter_tcp4.c `_”h]”hÝ)�”}”(hjêh]”(j3)�”}”(hjêh]”hŒbpf_iter_tcp4.c”…”�”}”(hjïh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œbpf_iter_tcp4.c”jDŒwhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_tcp4.c”uh1j2hjìubjG)�”}”(hŒz ”h]”h}”(h]”Œbpf-iter-tcp4-c”ah ]”h"]”Œbpf_iter_tcp4.c”ah$]”h&]”Œrefuri”jþuh1jFjUKhjìubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÃhjèubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjåh²hh³hÊh´Nubj¯)�”}”(hŒ–`bpf_iter_task_vmas.c `_”h]”hÝ)�”}”(hjh]”(j3)�”}”(hjh]”hŒbpf_iter_task_vmas.c”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œbpf_iter_task_vmas.c”jDŒ|https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_task_vmas.c”uh1j2hjubjG)�”}”(hŒ ”h]”h}”(h]”Œbpf-iter-task-vmas-c”ah ]”h"]”Œbpf_iter_task_vmas.c”ah$]”h&]”Œrefuri”j.uh1jFjUKhjubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÄhjubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjåh²hh³hÊh´Nubj¯)�”}”(hŒ—`bpf_iter_task_file.c `_ ”h]”hÝ)�”}”(hŒ–`bpf_iter_task_file.c `_”h]”(j3)�”}”(hjNh]”hŒbpf_iter_task_file.c”…”�”}”(hjPh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œbpf_iter_task_file.c”jDŒ|https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/tools/testing/selftests/bpf/progs/bpf_iter_task_file.c”uh1j2hjLubjG)�”}”(hŒ ”h]”h}”(h]”Œbpf-iter-task-file-c”ah ]”h"]”Œbpf_iter_task_file.c”ah$]”h&]”Œrefuri”j_uh1jFjUKhjLubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÅhjHubah}”(h]”h ]”h"]”h$]”h&]”uh1j®hjåh²hh³hÊh´Nubeh}”(h]”h ]”h"]”h$]”h&]”jljÖuh1j©h³hÊh´KÃhjMh²hubhÝ)�”}”(hŒDLet us look at ``bpf_iter_task_file.c``, which runs in kernel space:”h]”(hŒLet us look at ”…”�”}”(hjh²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_task_file.c``”h]”hŒbpf_iter_task_file.c”…”�”}”(hj‡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjubhŒ, which runs in kernel space:”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÇhjMh²hubhÝ)�”}”(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]”(hŒHere is the definition of ”…”�”}”(hjŸh²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter__task_file``”h]”hŒbpf_iter__task_file”…”�”}”(hj§h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjŸubhŒ in ”…”�”}”(hjŸh²hh³Nh´Nubj3)�”}”(hŒj`vmlinux.h `_”h]”hŒ vmlinux.h”…”�”}”(hj¹h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œ vmlinux.h”jDŒ[https://facebookmicrosites.github.io/bpf/blog/2020/02/19/bpf-portability-and-co-re.html#btf”uh1j2hjŸubjG)�”}”(hŒ^ ”h]”h}”(h]”Œ vmlinux-h”ah ]”h"]”Œ vmlinux.h”ah$]”h&]”Œrefuri”jÉuh1jFjUKhjŸubhŒ. Any struct name in ”…”�”}”(hjŸh²hh³Nh´Nubjû)�”}”(hŒ ``vmlinux.h``”h]”hŒ vmlinux.h”…”�”}”(hjÛh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjŸubhŒ in the format ”…”�”}”(hjŸh²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter__``”h]”hŒbpf_iter__”…”�”}”(hjíh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjŸubhŒ' represents a BPF iterator. The suffix ”…”�”}”(hjŸh²hh³Nh´Nubjû)�”}”(hŒ````”h]”hŒ ”…”�”}”(hjÿh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjŸubhŒ! represents the type of iterator.”…”�”}”(hjŸh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÉhjMh²hubhŒ literal_block”“”)�”}”(hŒístruct bpf_iter__task_file { union { struct bpf_iter_meta *meta; }; union { struct task_struct *task; }; u32 fd; union { struct file *file; }; };”h]”hŒístruct 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:space”Œpreserve”uh1jh³hÊh´KÑhjMh²hubhÝ)�”}”(hXêIn 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 ”…”�”}”(hj)h²hh³Nh´Nubj3)�”}”(hŒŒ`reference counted `_”h]”hŒreference counted”…”�”}”(hj1h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œreference counted”jDŒuhttps://facebookmicrosites.github.io/bpf/blog/2018/08/31/object-lifetime.html#file-descriptors-and-reference-counters”uh1j2hj)ubjG)�”}”(hŒx ”h]”h}”(h]”Œreference-counted”ah ]”h"]”Œreference counted”ah$]”h&]”Œrefuri”jAuh1jFjUKhj)ubhŒ4, so they won’t go away when the BPF program runs.”…”�”}”(hj)h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KÞhjMh²hubhÝ)�”}”(hŒ:Here is a snippet from the ``bpf_iter_task_file.c`` file:”h]”(hŒHere is a snippet from the ”…”�”}”(hjYh²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_task_file.c``”h]”hŒbpf_iter_task_file.c”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjYubhŒ file:”…”�”}”(hjYh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´KæhjMh²hubj)�”}”(hXŽ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) { 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]”hXŽ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) { 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; }”…”�”}”hjysbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´KêhjMh²hubhÝ)�”}”(hŒ×In 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 ”…”�”}”(hj‡h²hh³Nh´Nubjû)�”}”(hŒ``SEC(iter/task_file)``”h]”hŒSEC(iter/task_file)”…”�”}”(hj�h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj‡ubhŒz, indicates that the program is a BPF iterator program to iterate all files from all tasks. The context of the program is ”…”�”}”(hj‡h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter__task_file``”h]”hŒbpf_iter__task_file”…”�”}”(hj¡h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj‡ubhŒ struct.”…”�”}”(hj‡h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjMh²hubhÝ)�”}”(hXÕThe 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 ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ ``read()``”h]”hŒread()”…”�”}”(hjÁh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ‚ syscall. Once invoked, the BPF program can export data to user space using a variety of BPF helper functions. You can use either ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``bpf_seq_printf()``”h]”hŒbpf_seq_printf()”…”�”}”(hjÓh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ& (and BPF_SEQ_PRINTF helper macro) or ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``bpf_seq_write()``”h]”hŒbpf_seq_write()”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ§ 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 ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``bpf_seq_write()``”h]”hŒbpf_seq_write()”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ0 as needed. For the formatted data, you can use ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``cat ``”h]”hŒ cat ”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ! to print the results similar to ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``cat /proc/net/netlink``”h]”hŒcat /proc/net/netlink”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ@ after pinning the BPF iterator to the bpffs mount. Later, use ”…”�”}”(hj¹h²hh³Nh´Nubjû)�”}”(hŒ``rm -f ``”h]”hŒ rm -f ”…”�”}”(hj- h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¹ubhŒ to remove the pinned iterator.”…”�”}”(hj¹h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M hjMh²hubhÝ)�”}”(hŒ§For 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]”(hŒQFor example, you can use the following command to create a BPF iterator from the ”…”�”}”(hjE h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_ipv6_route.o``”h]”hŒbpf_iter_ipv6_route.o”…”�”}”(hjM h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjE ubhŒ object file and pin it to the ”…”�”}”(hjE h²hh³Nh´Nubjû)�”}”(hŒ``/sys/fs/bpf/my_route``”h]”hŒ/sys/fs/bpf/my_route”…”�”}”(hj_ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjE ubhŒ path:”…”�”}”(hjE h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjMh²hubj)�”}”(hŒ@$ bpftool iter pin ./bpf_iter_ipv6_route.o /sys/fs/bpf/my_route”h]”hŒ@$ bpftool iter pin ./bpf_iter_ipv6_route.o /sys/fs/bpf/my_route”…”�”}”hjw sbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´MhjMh²hubhÝ)�”}”(hŒ;And then print out the results using the following command:”h]”hŒ;And then print out the results using the following command:”…”�”}”(hj… h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjMh²hubj)�”}”(hŒ$ cat /sys/fs/bpf/my_route”h]”hŒ$ cat /sys/fs/bpf/my_route”…”�”}”hj“ sbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´M"hjMh²hubeh}”(h]”Œhow-to-use-bpf-iterators”ah ]”h"]”Œhow to use bpf iterators”ah$]”h&]”uh1hµhh·h²hh³hÊh´K©ubh¶)�”}”(hhh]”(h»)�”}”(hŒ7Implement Kernel Support for BPF Iterator Program Types”h]”hŒ7Implement Kernel Support for BPF Iterator Program Types”…”�”}”(hj¬ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj© h²hh³hÊh´M'ubhÝ)�”}”(hŒòTo 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]”(hŒˆTo implement a BPF iterator in the kernel, the developer must make a one-time change to the following key data structure defined in the ”…”�”}”(hjº h²hh³Nh´Nubj3)�”}”(hŒd`bpf.h `_”h]”hŒbpf.h”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œbpf.h”jDŒYhttps://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git/tree/include/linux/bpf.h”uh1j2hjº ubjG)�”}”(hŒ\ ”h]”h}”(h]”Œbpf-h”ah ]”h"]”Œbpf.h”ah$]”h&]”Œrefuri”jÒ uh1jFjUKhjº ubhŒ file.”…”�”}”(hjº h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M)hj© h²hubj)�”}”(hXástruct 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]”hXástruct 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; };”…”�”}”hjê sbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´M0hj© h²hubhÝ)�”}”(hŒ„After 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 ”…”�”}”(hjø h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_reg_target()``”h]”hŒbpf_iter_reg_target()”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjø ubhŒ= to register the iterator to the main BPF iterator subsystem.”…”�”}”(hjø h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M=hj© h²hubhÝ)�”}”(hŒIThe following is the breakdown for each field in struct ``bpf_iter_reg``.”h]”(hŒ8The following is the breakdown for each field in struct ”…”�”}”(hj h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_reg``”h]”hŒ bpf_iter_reg”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj ubhŒ.”…”�”}”(hj h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M@hj© h²hubhŒtable”“”)�”}”(hhh]”hŒtgroup”“”)�”}”(hhh]”(hŒcolspec”“”)�”}”(hhh]”h}”(h]”h ]”h"]”h$]”h&]”Œcolwidth”Kuh1jB hj? ubjC )�”}”(hhh]”h}”(h]”h ]”h"]”h$]”h&]”jM K2uh1jB hj? ubhŒthead”“”)�”}”(hhh]”hŒrow”“”)�”}”(hhh]”(hŒentry”“”)�”}”(hhh]”hÝ)�”}”(hŒFields”h]”hŒFields”…”�”}”(hjf h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MFhjc ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj^ ubjb )�”}”(hhh]”hÝ)�”}”(hŒ Description”h]”hŒ Description”…”�”}”(hj} h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MGhjz ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj^ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjY ubah}”(h]”h ]”h"]”h$]”h&]”uh1jW hj? ubhŒtbody”“”)�”}”(hhh]”(j] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒtarget”h]”hŒtarget”…”�”}”(hj¨ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MHhj¥ ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj¢ ubjb )�”}”(hhh]”hÝ)�”}”(hŒ¤Specifies 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]”(hŒ5Specifies the name of the BPF iterator. For example: ”…”�”}”(hj¿ h²hh³Nh´Nubjû)�”}”(hŒ ``bpf_map``”h]”hŒbpf_map”…”�”}”(hjÇ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¿ ubhŒ, ”…”�”}”(hj¿ h²hh³Nh´Nubjû)�”}”(hŒ``bpf_map_elem``”h]”hŒ bpf_map_elem”…”�”}”(hjÙ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¿ ubhŒ*. The name should be different from other ”…”�”}”(hj¿ h²hh³Nh´Nubjû)�”}”(hŒ ``bpf_iter``”h]”hŒbpf_iter”…”�”}”(hjë h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj¿ ubhŒ target names in the kernel.”…”�”}”(hj¿ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MIhj¼ ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj¢ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒattach_target and detach_target”h]”hŒattach_target and detach_target”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MKhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj ubjb )�”}”(hhh]”hÝ)�”}”(hŒ‘Allows for target specific ``link_create`` action since some targets may need special processing. Called during the user space link_create stage.”h]”(hŒAllows for target specific ”…”�”}”(hj, h²hh³Nh´Nubjû)�”}”(hŒ``link_create``”h]”hŒ link_create”…”�”}”(hj4 h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj, ubhŒg action since some targets may need special processing. Called during the user space link_create stage.”…”�”}”(hj, h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MLhj) ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒshow_fdinfo and fill_link_info”h]”hŒshow_fdinfo and fill_link_info”…”�”}”(hj^ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MNhj[ ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjX ubjb )�”}”(hhh]”hÝ)�”}”(hŒiCalled to fill target specific information when user tries to get link info associated with the iterator.”h]”hŒiCalled to fill target specific information when user tries to get link info associated with the iterator.”…”�”}”(hju h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MOhjr ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjX ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒget_func_proto”h]”hŒget_func_proto”…”�”}”(hj• h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MQhj’ ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj� ubjb )�”}”(hhh]”hÝ)�”}”(hŒFPermits a BPF iterator to access BPF helpers specific to the iterator.”h]”hŒFPermits a BPF iterator to access BPF helpers specific to the iterator.”…”�”}”(hj¬ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MRhj© ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj� ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒ"ctx_arg_info_size and ctx_arg_info”h]”hŒ"ctx_arg_info_size and ctx_arg_info”…”�”}”(hjÌ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MShjÉ ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjÆ ubjb )�”}”(hhh]”hÝ)�”}”(hŒYSpecifies the verifier states for BPF program arguments associated with the bpf iterator.”h]”hŒYSpecifies the verifier states for BPF program arguments associated with the bpf iterator.”…”�”}”(hjã h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MThjà ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjÆ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒfeature”h]”hŒfeature”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MVhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjý ubjb )�”}”(hhh]”hÝ)�”}”(hŒñSpecifies 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]”hŒñSpecifies 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.”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MWhj ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hjý ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubj] )�”}”(hhh]”(jb )�”}”(hhh]”hÝ)�”}”(hŒseq_info”h]”hŒseq_info”…”�”}”(hj: h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M[hj7 ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj4 ubjb )�”}”(hhh]”hÝ)�”}”(hŒŒSpecifies the set of seq operations for the BPF iterator and helpers to initialize/free the private data for the corresponding ``seq_file``.”h]”(hŒSpecifies the set of seq operations for the BPF iterator and helpers to initialize/free the private data for the corresponding ”…”�”}”(hjQ h²hh³Nh´Nubjû)�”}”(hŒ ``seq_file``”h]”hŒseq_file”…”�”}”(hjY h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjQ ubhŒ.”…”�”}”(hjQ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M\hjN ubah}”(h]”h ]”h"]”h$]”h&]”uh1ja hj4 ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j\ hjŸ ubeh}”(h]”h ]”h"]”h$]”h&]”uh1j� hj? ubeh}”(h]”h ]”h"]”h$]”h&]”Œcols”Kuh1j= hj: ubah}”(h]”h ]”Œcolwidths-given”ah"]”h$]”h&]”uh1j8 hj© h²hh³Nh´NubhÝ)�”}”(hŒ¢`Click here `_ to see an implementation of the ``task_vma`` BPF iterator in the kernel.”h]”(j3)�”}”(hŒY`Click here `_”h]”hŒ Click here”…”�”}”(hj• h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”Œname”Œ Click here”jDŒIhttps://lore.kernel.org/bpf/20210212183107.50963-2-songliubraving@fb.com/”uh1j2hj‘ ubjG)�”}”(hŒL ”h]”h}”(h]”Œ click-here”ah ]”h"]”Œ click here”ah$]”h&]”Œrefuri”j¥ uh1jFjUKhj‘ ubhŒ! to see an implementation of the ”…”�”}”(hj‘ h²hh³Nh´Nubjû)�”}”(hŒ ``task_vma``”h]”hŒtask_vma”…”�”}”(hj· h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj‘ ubhŒ BPF iterator in the kernel.”…”�”}”(hj‘ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M_hj© h²hubeh}”(h]”Œ7implement-kernel-support-for-bpf-iterator-program-types”ah ]”h"]”Œ7implement kernel support for bpf iterator program types”ah$]”h&]”uh1hµhh·h²hh³hÊh´M'ubh¶)�”}”(hhh]”(h»)�”}”(hŒ!Parameterizing BPF Task Iterators”h]”hŒ!Parameterizing BPF Task Iterators”…”�”}”(hjÚ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj× h²hh³hÊh´MeubhÝ)�”}”(hXßBy 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]”hXßBy 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è h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mghj× h²hubeh}”(h]”Œ!parameterizing-bpf-task-iterators”ah ]”h"]”Œ!parameterizing bpf task iterators”ah$]”h&]”uh1hµhh·h²hh³hÊh´Meubh¶)�”}”(hhh]”(h»)�”}”(hŒBPF Task Iterator Program”h]”hŒBPF Task Iterator Program”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjþ h²hh³hÊh´MqubhÝ)�”}”(hŒúThe 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 h²hh³Nh´Nubjû)�”}”(hŒ ``seq_file``”h]”hŒseq_file”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj 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 h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´Mshjþ h²hubj)�”}”(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; }”•ñUh]”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&]”j'j(uh1jh³hÊh´Mzhjþ h²hubeh}”(h]”Œbpf-task-iterator-program”ah ]”h"]”Œbpf task iterator program”ah$]”h&]”uh1hµhh·h²hh³hÊh´Mqubh¶)�”}”(hhh]”(h»)�”}”(hŒ(Creating a File Iterator with Parameters”h]”hŒ(Creating a File Iterator with Parameters”…”�”}”(hjH h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjE h²hh³hÊh´M’ubhÝ)�”}”(hŒTNow, let us look at how to create an iterator that includes only files of a process.”h]”hŒTNow, let us look at how to create an iterator that includes only files of a process.”…”�”}”(hjV h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M”hjE h²hubhÝ)�”}”(hŒ@First, fill the ``bpf_iter_attach_opts`` struct as shown below:”h]”(hŒFirst, fill the ”…”�”}”(hjd h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_attach_opts``”h]”hŒbpf_iter_attach_opts”…”�”}”(hjl h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjd ubhŒ struct as shown below:”…”�”}”(hjd h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M—hjE h²hubj)�”}”(hŒÂLIBBPF_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]”hŒÂLIBBPF_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);”…”�”}”hj„ sbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´M›hjE h²hubhÝ)�”}”(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]”(jû)�”}”(hŒ``linfo.task.pid``”h]”hŒlinfo.task.pid”…”�”}”(hj– h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒ�, if it is non-zero, directs the kernel to create an iterator that only includes opened files for the process with the specified ”…”�”}”(hj’ h²hh³Nh´Nubjû)�”}”(hŒ``pid``”h]”hŒpid”…”�”}”(hj¨ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒG. In this example, we will only be iterating files for our process. If ”…”�”}”(hj’ h²hh³Nh´Nubjû)�”}”(hŒ``linfo.task.pid``”h]”hŒlinfo.task.pid”…”�”}”(hjº h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒQ is zero, the iterator will visit every opened file of every process. Similarly, ”…”�”}”(hj’ h²hh³Nh´Nubjû)�”}”(hŒ``linfo.task.tid``”h]”hŒlinfo.task.tid”…”�”}”(hjÌ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒy directs the kernel to create an iterator that visits opened files of a specific thread, not a process. In this example, ”…”�”}”(hj’ h²hh³Nh´Nubjû)�”}”(hŒ``linfo.task.tid``”h]”hŒlinfo.task.tid”…”�”}”(hjÞ h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒ is different from ”…”�”}”(hj’ h²hh³Nh´Nubjû)�”}”(hŒ``linfo.task.pid``”h]”hŒlinfo.task.pid”…”�”}”(hjð h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj’ ubhŒŠ only if the thread has a separate file descriptor table. In most circumstances, all process threads share a single file descriptor table.”…”�”}”(hj’ h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¢hjE h²hubhÝ)�”}”(hŒ`Now, in the userspace program, pass the pointer of struct to the ``bpf_program__attach_iter()``.”h]”(hŒANow, in the userspace program, pass the pointer of struct to the ”…”�”}”(hjh²hh³Nh´Nubjû)�”}”(hŒ``bpf_program__attach_iter()``”h]”hŒbpf_program__attach_iter()”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhjubhŒ.”…”�”}”(hjh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¬hjE h²hubj)�”}”(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&]”j'j(uh1jh³hÊh´M±hjE h²hubhÝ)�”}”(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]”(hŒIf both ”…”�”}”(hj6h²hh³Nh´Nubhç)�”}”(hŒ*tid*”h]”hŒtid”…”�”}”(hj>h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj6ubhŒ and ”…”�”}”(hj6h²hh³Nh´Nubhç)�”}”(hŒ*pid*”h]”hŒpid”…”�”}”(hjPh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj6ubhŒ0 are zero, an iterator created from this struct ”…”�”}”(hj6h²hh³Nh´Nubjû)�”}”(hŒ``bpf_iter_attach_opts``”h]”hŒbpf_iter_attach_opts”…”�”}”(hjbh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj6ubhŒ• 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 ”…”�”}”(hj6h²hh³Nh´Nubjû)�”}”(hŒ``bpf_program__attach_iter()``”h]”hŒbpf_program__attach_iter()”…”�”}”(hjth²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj6ubhŒ.”…”�”}”(hj6h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M´hjE h²hubhÝ)�”}”(hŒ0The whole program looks like the following code:”h]”hŒ0The whole program looks like the following code:”…”�”}”(hjŒh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M¹hjE h²hubj)�”}”(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; }”…”�”}”hjšsbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´M½hjE h²hubhÝ)�”}”(hŒ5The following lines are the output of the program. ::”h]”hŒ2The following lines are the output of the program.”…”�”}”(hj¨h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MùhjE h²hubj)�”}”(hX°PID 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”h]”hX°PID 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&]”j'j(uh1jh³hÊh´MühjE h²hubeh}”(h]”Œ(creating-a-file-iterator-with-parameters”ah ]”h"]”Œ(creating a file iterator with parameters”ah$]”h&]”uh1hµhh·h²hh³hÊh´M’ubh¶)�”}”(hhh]”(h»)�”}”(hŒWithout Parameters”h]”hŒWithout Parameters”…”�”}”(hjÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjÌh²hh³hÊh´M ubhÝ)�”}”(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]”(hŒîLet 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Ýh²hh³Nh´Nubhç)�”}”(hŒ*pid*”h]”hŒpid”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjÝubhŒY namespace, actually). So, we usually add a global variable in the BPF program to pass a ”…”�”}”(hjÝh²hh³Nh´Nubhç)�”}”(hŒ*pid*”h]”hŒpid”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjÝubhŒ to the BPF program.”…”�”}”(hjÝh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M hjÌh²hubhÝ)�”}”(hŒ4The BPF program would look like the following block.”h]”hŒ4The BPF program would look like the following block.”…”�”}”(hjh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MhjÌh²hubhŒ 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&]”j'j(uh1jh³hÊh´Mhjubah}”(h]”h ]”h"]”h$]”h&]”uh1jh³hÊh´MhjÌh²hubhÝ)�”}”(hŒ;The user space program would look like the following block:”h]”hŒ;The user space program would look like the following block:”…”�”}”(hj7h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M%hjÌh²hubj)�”}”(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); ...... }”…”�”}”hjIsbah}”(h]”h ]”h"]”h$]”h&]”j'j(uh1jh³hÊh´M)hjEubah}”(h]”h ]”h"]”h$]”h&]”uh1jh³hÊh´M'hjÌh²hubhÝ)�”}”(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]”(jû)�”}”(hŒ``target_pid``”h]”hŒ target_pid”…”�”}”(hjah²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1júhj]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]h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´M8hjÌh²hubeh}”(h]”Œwithout-parameters”ah ]”h"]”Œwithout parameters”ah$]”h&]”uh1hµhh·h²hh³hÊh´M ubh¶)�”}”(hhh]”(h»)�”}”(hŒParametrizing VMA Iterators”h]”hŒParametrizing VMA Iterators”…”�”}”(hj„h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhj�h²hh³hÊh´M?ubhÝ)�”}”(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]”(hŒø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 ”…”�”}”(hj’h²hh³Nh´Nubhç)�”}”(hŒ*tid*”h]”hŒtid”…”�”}”(hjšh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj’ubhŒ makes no difference from using ”…”�”}”(hj’h²hh³Nh´Nubhç)�”}”(hŒ*pid*”h]”hŒpid”…”�”}”(hj¬h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhj’ubhŒ.”…”�”}”(hj’h²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MAhj�h²hubeh}”(h]”Œparametrizing-vma-iterators”ah ]”h"]”Œparametrizing vma iterators”ah$]”h&]”uh1hµhh·h²hh³hÊh´M?ubh¶)�”}”(hhh]”(h»)�”}”(hŒParametrizing Task Iterators”h]”hŒParametrizing Task Iterators”…”�”}”(hjÏh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hºhjÌh²hh³hÊh´MHubhÝ)�”}”(hŒðA 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]”(hŒA BPF task iterator with ”…”�”}”(hjÝh²hh³Nh´Nubhç)�”}”(hŒ*pid*”h]”hŒpid”…”�”}”(hjåh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæ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Ýh²hh³Nh´Nubhç)�”}”(hŒ*tid*”h]”hŒtid”…”�”}”(hj÷h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjÝubhŒ: parameter to include only the tasks that match the given ”…”�”}”(hjÝh²hh³Nh´Nubhç)�”}”(hŒ*tid*”h]”hŒtid”…”�”}”(hj h²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hæhjÝubhŒ.”…”�”}”(hjÝh²hh³Nh´Nubeh}”(h]”h ]”h"]”h$]”h&]”uh1hÜh³hÊh´MJhjÌh²hubeh}”(h]”Œparametrizing-task-iterators”ah ]”h"]”Œparametrizing task iterators”ah$]”h&]”uh1hµhh·h²hh³hÊh´MHubeh}”(h]”Œ bpf-iterators”ah ]”h"]”Œ bpf iterators”ah$]”h&]”uh1hµhhh²hh³hÊh´Kubeh}”(h]”h ]”h"]”h$]”h&]”Œsource”hÊuh1hŒcurrent_source”NŒ current_line”NŒsettings”Œdocutils.frontend”ŒValues”“”)�”}”(hºNŒ generator”NŒ datestamp”NŒ source_link”NŒ source_url”NŒ toc_backlinks”ja Œfootnote_backlinks”KŒ sectnum_xform”KŒstrip_comments”NŒstrip_elements_with_classes”NŒ strip_classes”NŒ report_level”KŒ halt_level”KŒexit_status_level”KŒdebug”NŒwarning_stream”NŒ traceback”ˆŒinput_encoding”Œ utf-8-sig”Œinput_encoding_error_handler”Œstrict”Œoutput_encoding”Œutf-8”Œoutput_encoding_error_handler”jSŒerror_encoding”Œutf-8”Œerror_encoding_error_handler”Œbackslashreplace”Œ language_code”Œen”Œrecord_dependencies”NŒconfig”NŒ id_prefix”hŒauto_id_prefix”Œid”Œ dump_settings”NŒdump_internals”NŒdump_transforms”NŒdump_pseudo_xml”NŒexpose_internals”NŒstrict_visitor”NŒ_disable_config”NŒ_source”hÊŒ _destination”NŒ _config_files”]”Œ7/var/lib/git/docbuild/linux/Documentation/docutils.conf”aŒfile_insertion_enabled”ˆŒ raw_enabled”KŒline_length_limit”M'Œpep_references”NŒ pep_base_url”Œhttps://peps.python.org/”Œpep_file_url_template”Œpep-%04d”Œrfc_references”NŒ rfc_base_url”Œ&https://datatracker.ietf.org/doc/html/”Œ tab_width”KŒtrim_footnote_reference_space”‰Œsyntax_highlight”Œlong”Œ smart_quotes”ˆŒsmartquotes_locales”]”Œcharacter_level_inline_markup”‰Œdoctitle_xform”‰Œ docinfo_xform”KŒsectsubtitle_xform”‰Œ image_loading”Œlink”Œembed_stylesheet”‰Œcloak_email_addresses”ˆŒsection_self_link”‰Œenv”NubŒreporter”NŒindirect_targets”]”Œsubstitution_defs”}”Œsubstitution_names”}”Œrefnames”}”Œrefids”}”Œnameids”}”(j.j+j%j"jÞjÛj£j jQjNj‚jjJjGj¦ j£ j€j}jjj8j5jijfjÓjÐjKjHjÔ jÑ jÜ jÙ j¯ j¬ jû jø jB j? jÉjÆj~j{jÉjÆj&j#uŒ nametypes”}”(j.‰j%‰jÞ‰j£‰jQˆj‚ˆjJ‰j¦ ‰j€ˆjˆj8ˆjiˆjÓˆjKˆjÔ ‰jÜ ˆj¯ ˆjû ‰jB ‰jɉj~‰jɉj&‰uh}”(j+h·j"hËjÛj(j jájNjHjjyjGj¦j£ jMj}jwjjÿj5j/jfj`jÐjÊjHjBjÑ j© jÙ jÓ j¬ j¦ jø j× j? jþ jÆjE j{jÌjÆj�j#jÌuŒ footnote_refs”}”Œ citation_refs”}”Œ autofootnotes”]”Œautofootnote_refs”]”Œsymbol_footnotes”]”Œsymbol_footnote_refs”]”Œ footnotes”]”Œ citations”]”Œautofootnote_start”KŒsymbol_footnote_start”KŒ id_counter”Œ collections”ŒCounter”“”}”…”R”Œparse_messages”]”hŒsystem_message”“”)�”}”(hhh]”hÝ)�”}”(hŒfPossible title underline, too short for the title. Treating it as ordinary text because it's so short.”h]”hŒhPossible title underline, too short for the title. Treating it as ordinary text because it’s so short.”…”�”}”(hjºh²hh³Nh´Nubah}”(h]”h ]”h"]”h$]”h&]”uh1hÜhj·ubah}”(h]”h ]”h"]”h$]”h&]”Œlevel”KŒtype”ŒINFO”Œline”MúŒsource”hÊuh1jµhjE h²hh³hÊh´MúubaŒtransform_messages”]”Œ transformer”NŒ include_log”]”Œ decoration”Nh²hub.