%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/map_sockmapmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/zh_TW/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/it_IT/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/ja_JP/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/ko_KR/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hPortuguese (Brazilian)}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/pt_BR/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget#/translations/sp_SP/bpf/map_sockmapmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h%SPDX-License-Identifier: GPL-2.0-onlyh]h%SPDX-License-Identifier: GPL-2.0-only}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhh=/var/lib/git/docbuild/linux/Documentation/bpf/map_sockmap.rsthKubh)}(hCopyright Red Hath]hCopyright Red Hat}hhsbah}(h]h ]h"]h$]h&]hhuh1hhhhhhhhKubhsection)}(hhh](htitle)}(h.BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASHh]h.BPF_MAP_TYPE_SOCKMAP and BPF_MAP_TYPE_SOCKHASH}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubhnote)}(h- ``BPF_MAP_TYPE_SOCKMAP`` was introduced in kernel version 4.14 - ``BPF_MAP_TYPE_SOCKHASH`` was introduced in kernel version 4.18h]h bullet_list)}(hhh](h list_item)}(h>``BPF_MAP_TYPE_SOCKMAP`` was introduced in kernel version 4.14h]h paragraph)}(hhh](hliteral)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhhubh& was introduced in kernel version 4.14}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhubah}(h]h ]h"]h$]h&]uh1hhhubh)}(h?``BPF_MAP_TYPE_SOCKHASH`` was introduced in kernel version 4.18h]h)}(hj#h](j)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj%ubh& was introduced in kernel version 4.18}(hj%hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hj!ubah}(h]h ]h"]h$]h&]uh1hhhubeh}(h]h ]h"]h$]h&]bullet-uh1hhhhK hhubah}(h]h ]h"]h$]h&]uh1hhhhhhNhNubh)}(hXR``BPF_MAP_TYPE_SOCKMAP`` and ``BPF_MAP_TYPE_SOCKHASH`` maps can be used to redirect skbs between sockets or to apply policy at the socket level based on the result of a BPF (verdict) program with the help of the BPF helpers ``bpf_sk_redirect_map()``, ``bpf_sk_redirect_hash()``, ``bpf_msg_redirect_map()`` and ``bpf_msg_redirect_hash()``.h](j)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh and }(hjThhhNhNubj)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh maps can be used to redirect skbs between sockets or to apply policy at the socket level based on the result of a BPF (verdict) program with the help of the BPF helpers }(hjThhhNhNubj)}(h``bpf_sk_redirect_map()``h]hbpf_sk_redirect_map()}(hj|hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh, }(hjThhhNhNubj)}(h``bpf_sk_redirect_hash()``h]hbpf_sk_redirect_hash()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh, }(hjThhhNhNubj)}(h``bpf_msg_redirect_map()``h]hbpf_msg_redirect_map()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh and }hjTsbj)}(h``bpf_msg_redirect_hash()``h]hbpf_msg_redirect_hash()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjTubh.}(hjThhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubh)}(hX%``BPF_MAP_TYPE_SOCKMAP`` is backed by an array that uses an integer key as the index to look up a reference to a ``struct sock``. The map values are socket descriptors. Similarly, ``BPF_MAP_TYPE_SOCKHASH`` is a hash backed BPF map that holds references to sockets via their socket descriptors.h](j)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhY is backed by an array that uses an integer key as the index to look up a reference to a }(hjhhhNhNubj)}(h``struct sock``h]h struct sock}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh4. The map values are socket descriptors. Similarly, }(hjhhhNhNubj)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhX is a hash backed BPF map that holds references to sockets via their socket descriptors.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hThe value type is either __u32 or __u64; the latter (__u64) is to support returning socket cookies to userspace. Returning the ``struct sock *`` that the map holds to user-space is neither safe nor useful.h]h)}(hThe value type is either __u32 or __u64; the latter (__u64) is to support returning socket cookies to userspace. Returning the ``struct sock *`` that the map holds to user-space is neither safe nor useful.h](hThe value type is either __u32 or __u64; the latter (__u64) is to support returning socket cookies to userspace. Returning the }(hjhhhNhNubj)}(h``struct sock *``h]h struct sock *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh= that the map holds to user-space is neither safe nor useful.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1hhhhhhhhNubh)}(hXpThese maps may have BPF programs attached to them, specifically a parser program and a verdict program. The parser program determines how much data has been parsed and therefore how much data needs to be queued to come to a verdict. The verdict program is essentially the redirect program and can return a verdict of ``__SK_DROP``, ``__SK_PASS``, or ``__SK_REDIRECT``.h](hX=These maps may have BPF programs attached to them, specifically a parser program and a verdict program. The parser program determines how much data has been parsed and therefore how much data needs to be queued to come to a verdict. The verdict program is essentially the redirect program and can return a verdict of }(hj4hhhNhNubj)}(h ``__SK_DROP``h]h __SK_DROP}(hj<hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubh, }(hj4hhhNhNubj)}(h ``__SK_PASS``h]h __SK_PASS}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubh, or }(hj4hhhNhNubj)}(h``__SK_REDIRECT``h]h __SK_REDIRECT}(hj`hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubh.}(hj4hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhhhhubh)}(hWhen a socket is inserted into one of these maps, its socket callbacks are replaced and a ``struct sk_psock`` is attached to it. Additionally, this ``sk_psock`` inherits the programs that are attached to the map.h](hZWhen a socket is inserted into one of these maps, its socket callbacks are replaced and a }(hjxhhhNhNubj)}(h``struct sk_psock``h]hstruct sk_psock}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjxubh' is attached to it. Additionally, this }(hjxhhhNhNubj)}(h ``sk_psock``h]hsk_psock}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjxubh4 inherits the programs that are attached to the map.}(hjxhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK"hhhhubh)}(hA sock object may be in multiple maps, but can only inherit a single parse or verdict program. If adding a sock object to a map would result in having multiple parser programs the update will return an EBUSY error.h]hA sock object may be in multiple maps, but can only inherit a single parse or verdict program. If adding a sock object to a map would result in having multiple parser programs the update will return an EBUSY error.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK&hhhhubh)}(h3The supported programs to attach to these maps are:h]h3The supported programs to attach to these maps are:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK*hhhhubh literal_block)}(hstruct sk_psock_progs { struct bpf_prog *msg_parser; struct bpf_prog *stream_parser; struct bpf_prog *stream_verdict; struct bpf_prog *skb_verdict; };h]hstruct sk_psock_progs { struct bpf_prog *msg_parser; struct bpf_prog *stream_parser; struct bpf_prog *stream_verdict; struct bpf_prog *skb_verdict; };}hjsbah}(h]h ]h"]h$]h&]hhƌforcelanguagechighlight_args}uh1jhhhK,hhhhubh)}(h`Users are not allowed to attach ``stream_verdict`` and ``skb_verdict`` programs to the same map.h]h)}(h`Users are not allowed to attach ``stream_verdict`` and ``skb_verdict`` programs to the same map.h](h Users are not allowed to attach }(hjhhhNhNubj)}(h``stream_verdict``h]hstream_verdict}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh and }(hjhhhNhNubj)}(h``skb_verdict``h]h skb_verdict}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh programs to the same map.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK6hjubah}(h]h ]h"]h$]h&]uh1hhhhhhhhNubh)}(h*The attach types for the map programs are:h]h*The attach types for the map programs are:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK9hhhhubh)}(hhh](h)}(h0``msg_parser`` program - ``BPF_SK_MSG_VERDICT``.h]h)}(hj*h](j)}(h``msg_parser``h]h msg_parser}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubh program - }(hj,hhhNhNubj)}(h``BPF_SK_MSG_VERDICT``h]hBPF_SK_MSG_VERDICT}(hjAhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj,ubh.}(hj,hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK;hj(ubah}(h]h ]h"]h$]h&]uh1hhj%hhhhhNubh)}(h9``stream_parser`` program - ``BPF_SK_SKB_STREAM_PARSER``.h]h)}(hjah](j)}(h``stream_parser``h]h stream_parser}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubh program - }(hjchhhNhNubj)}(h``BPF_SK_SKB_STREAM_PARSER``h]hBPF_SK_SKB_STREAM_PARSER}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjcubh.}(hjchhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhj%hhhhhNubeh}(h]h ]h"]h$]h&]jLjMuh1hhhhK;hhhhubh)}(hXThere are additional helpers available to use with the parser and verdict programs: ``bpf_msg_apply_bytes()`` and ``bpf_msg_cork_bytes()``. With ``bpf_msg_apply_bytes()`` BPF programs can tell the infrastructure how many bytes the given verdict should apply to. The helper ``bpf_msg_cork_bytes()`` handles a different case where a BPF program cannot reach a verdict on a msg until it receives more bytes AND the program doesn't want to forward the packet until it is known to be good.h](hTThere are additional helpers available to use with the parser and verdict programs: }(hj hhhNhNubj)}(h``bpf_msg_apply_bytes()``h]hbpf_msg_apply_bytes()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh and }(hj hhhNhNubj)}(h``bpf_msg_cork_bytes()``h]hbpf_msg_cork_bytes()}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh. With }(hj hhhNhNubj)}(h``bpf_msg_apply_bytes()``h]hbpf_msg_apply_bytes()}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubhg BPF programs can tell the infrastructure how many bytes the given verdict should apply to. The helper }(hj hhhNhNubj)}(h``bpf_msg_cork_bytes()``h]hbpf_msg_cork_bytes()}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh handles a different case where a BPF program cannot reach a verdict on a msg until it receives more bytes AND the program doesn’t want to forward the packet until it is known to be good.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK@hhhhubh)}(hFinally, the helpers ``bpf_msg_pull_data()`` and ``bpf_msg_push_data()`` are available to ``BPF_PROG_TYPE_SK_MSG`` BPF programs to pull in data and set the start and end pointers to given values or to add metadata to the ``struct sk_msg_buff *msg``.h](hFinally, the helpers }(hjahhhNhNubj)}(h``bpf_msg_pull_data()``h]hbpf_msg_pull_data()}(hjihhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubh and }(hjahhhNhNubj)}(h``bpf_msg_push_data()``h]hbpf_msg_push_data()}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubh are available to }(hjahhhNhNubj)}(h``BPF_PROG_TYPE_SK_MSG``h]hBPF_PROG_TYPE_SK_MSG}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubhk BPF programs to pull in data and set the start and end pointers to given values or to add metadata to the }(hjahhhNhNubj)}(h``struct sk_msg_buff *msg``h]hstruct sk_msg_buff *msg}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjaubh.}(hjahhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKHhhhhubh)}(h9All these helpers will be described in more detail below.h]h9All these helpers will be described in more detail below.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKMhhhhubh)}(hhh](h)}(hUsageh]hUsage}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKPubh)}(hhh](h)}(h Kernel BPFh]h Kernel BPF}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKRubh)}(hhh](h)}(hbpf_msg_redirect_map()h]hbpf_msg_redirect_map()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKTubj)}(h[long bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags)h]h[long bpf_msg_redirect_map(struct sk_msg_buff *msg, struct bpf_map *map, u32 key, u64 flags)}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKUhjhhubh)}(hXThis helper is used in programs implementing policies at the socket level. If the message ``msg`` is allowed to pass (i.e., if the verdict BPF program returns ``SK_PASS``), redirect it to the socket referenced by ``map`` (of type ``BPF_MAP_TYPE_SOCKMAP``) at index ``key``. Both ingress and egress interfaces can be used for redirection. The ``BPF_F_INGRESS`` value in ``flags`` is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.h](hZThis helper is used in programs implementing policies at the socket level. If the message }(hjhhhNhNubj)}(h``msg``h]hmsg}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh> is allowed to pass (i.e., if the verdict BPF program returns }(hjhhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hj!hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh+), redirect it to the socket referenced by }(hjhhhNhNubj)}(h``map``h]hmap}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh (of type }(hjhhhNhNubj)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh ) at index }(hjhhhNhNubj)}(h``key``h]hkey}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhF. Both ingress and egress interfaces can be used for redirection. The }(hjhhhNhNubj)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hjihhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh value in }(hjhhhNhNubj)}(h ``flags``h]hflags}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKYhjhhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hjhhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh on success, or }(hjhhhNhNubj)}(h ``SK_DROP``h]hSK_DROP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh on error.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKahjhhubeh}(h]bpf-msg-redirect-mapah ]h"]bpf_msg_redirect_map()ah$]h&]uh1hhjhhhhhKTubh)}(hhh](h)}(hbpf_sk_redirect_map()h]hbpf_sk_redirect_map()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKdubj)}(hUlong bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key u64 flags)h]hUlong bpf_sk_redirect_map(struct sk_buff *skb, struct bpf_map *map, u32 key u64 flags)}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKehjhhubh)}(hXHRedirect the packet to the socket referenced by ``map`` (of type ``BPF_MAP_TYPE_SOCKMAP``) at index ``key``. Both ingress and egress interfaces can be used for redirection. The ``BPF_F_INGRESS`` value in ``flags`` is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.h](h0Redirect the packet to the socket referenced by }(hjhhhNhNubj)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh (of type }(hjhhhNhNubj)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh ) at index }(hjhhhNhNubj)}(h``key``h]hkey}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhF. Both ingress and egress interfaces can be used for redirection. The }(hjhhhNhNubj)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hj+hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh value in }(hjhhhNhNubj)}(h ``flags``h]hflags}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKihjhhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hjUhhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh on success, or }(hjUhhhNhNubj)}(h ``SK_DROP``h]hSK_DROP}(hjohhhNhNubah}(h]h ]h"]h$]h&]uh1jhjUubh on error.}(hjUhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKohjhhubeh}(h]bpf-sk-redirect-mapah ]h"]bpf_sk_redirect_map()ah$]h&]uh1hhjhhhhhKdubh)}(hhh](h)}(hbpf_map_lookup_elem()h]hbpf_map_lookup_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKrubj)}(h?void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)h]h?void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKshjhhubh)}(hesocket entries of type ``struct sock *`` can be retrieved using the ``bpf_map_lookup_elem()`` helper.h](hsocket entries of type }(hjhhhNhNubj)}(h``struct sock *``h]h struct sock *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh can be retrieved using the }(hjhhhNhNubj)}(h``bpf_map_lookup_elem()``h]hbpf_map_lookup_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh helper.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKwhjhhubeh}(h]bpf-map-lookup-elemah ]h"]h$]bpf_map_lookup_elem()ah&]uh1hhjhhhhhKr referencedKubh)}(hhh](h)}(hbpf_sock_map_update()h]hbpf_sock_map_update()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK{ubj)}(h_long bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)h]h_long bpf_sock_map_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhK|hjhhubh)}(hAdd an entry to, or update a ``map`` referencing sockets. The ``skops`` is used as a new value for the entry associated to ``key``. The ``flags`` argument can be one of the following:h](hAdd an entry to, or update a }(hj hhhNhNubj)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh referencing sockets. The }(hj hhhNhNubj)}(h ``skops``h]hskops}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh4 is used as a new value for the entry associated to }(hj hhhNhNubj)}(h``key``h]hkey}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh. The }(hj hhhNhNubj)}(h ``flags``h]hflags}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh& argument can be one of the following:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hhh](h)}(h@``BPF_ANY``: Create a new element or update an existing element.h]h)}(hjeh](j)}(h ``BPF_ANY``h]hBPF_ANY}(hjjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjgubh5: Create a new element or update an existing element.}(hjghhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjcubah}(h]h ]h"]h$]h&]uh1hhj`hhhhhNubh)}(h?``BPF_NOEXIST``: Create a new element only if it did not exist.h]h)}(hjh](j)}(h``BPF_NOEXIST``h]h BPF_NOEXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh0: Create a new element only if it did not exist.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhj`hhhhhNubh)}(h+``BPF_EXIST``: Update an existing element. h]h)}(h*``BPF_EXIST``: Update an existing element.h](j)}(h ``BPF_EXIST``h]h BPF_EXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: Update an existing element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhj`hhhhhNubeh}(h]h ]h"]h$]h&]jLjMuh1hhhhKhjhhubh)}(hIf the ``map`` has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.h](hIf the }(hjhhhNhNubj)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h=Returns 0 on success, or a negative error in case of failure.h]h=Returns 0 on success, or a negative error in case of failure.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]bpf-sock-map-updateah ]h"]bpf_sock_map_update()ah$]h&]uh1hhjhhhhhK{ubh)}(hhh](h)}(hbpf_sock_hash_update()h]hbpf_sock_hash_update()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubj)}(h`long bpf_sock_hash_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)h]h`long bpf_sock_hash_update(struct bpf_sock_ops *skops, struct bpf_map *map, void *key, u64 flags)}hj sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhjhhubh)}(hAdd an entry to, or update a sockhash ``map`` referencing sockets. The ``skops`` is used as a new value for the entry associated to ``key``.h](h&Add an entry to, or update a sockhash }(hj/hhhNhNubj)}(h``map``h]hmap}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj/ubh referencing sockets. The }(hj/hhhNhNubj)}(h ``skops``h]hskops}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj/ubh4 is used as a new value for the entry associated to }(hj/hhhNhNubj)}(h``key``h]hkey}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj/ubh.}(hj/hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h3The ``flags`` argument can be one of the following:h](hThe }(hjshhhNhNubj)}(h ``flags``h]hflags}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjsubh& argument can be one of the following:}(hjshhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hhh](h)}(h@``BPF_ANY``: Create a new element or update an existing element.h]h)}(hjh](j)}(h ``BPF_ANY``h]hBPF_ANY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh5: Create a new element or update an existing element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubh)}(h?``BPF_NOEXIST``: Create a new element only if it did not exist.h]h)}(hjh](j)}(h``BPF_NOEXIST``h]h BPF_NOEXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh0: Create a new element only if it did not exist.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubh)}(h+``BPF_EXIST``: Update an existing element. h]h)}(h*``BPF_EXIST``: Update an existing element.h](j)}(h ``BPF_EXIST``h]h BPF_EXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh: Update an existing element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubeh}(h]h ]h"]h$]h&]jLjMuh1hhhhKhjhhubh)}(hIf the ``map`` has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.h](hIf the }(hj hhhNhNubj)}(h``map``h]hmap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh has BPF programs (parser and verdict), those will be inherited by the socket being added. If the socket is already attached to BPF programs, this results in an error.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h=Returns 0 on success, or a negative error in case of failure.h]h=Returns 0 on success, or a negative error in case of failure.}(hj, hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]bpf-sock-hash-updateah ]h"]bpf_sock_hash_update()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_msg_redirect_hash()h]hbpf_msg_redirect_hash()}(hjE hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjB hhhhhKubj)}(h^long bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags)h]h^long bpf_msg_redirect_hash(struct sk_msg_buff *msg, struct bpf_map *map, void *key, u64 flags)}hjS sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhjB hhubh)}(hXThis helper is used in programs implementing policies at the socket level. If the message ``msg`` is allowed to pass (i.e., if the verdict BPF program returns ``SK_PASS``), redirect it to the socket referenced by ``map`` (of type ``BPF_MAP_TYPE_SOCKHASH``) using hash ``key``. Both ingress and egress interfaces can be used for redirection. The ``BPF_F_INGRESS`` value in ``flags`` is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.h](hZThis helper is used in programs implementing policies at the socket level. If the message }(hjb hhhNhNubj)}(h``msg``h]hmsg}(hjj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubh> is allowed to pass (i.e., if the verdict BPF program returns }(hjb hhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hj| hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubh+), redirect it to the socket referenced by }(hjb hhhNhNubj)}(h``map``h]hmap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubh (of type }(hjb hhhNhNubj)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubh ) using hash }(hjb hhhNhNubj)}(h``key``h]hkey}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubhF. Both ingress and egress interfaces can be used for redirection. The }(hjb hhhNhNubj)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubh value in }(hjb hhhNhNubj)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjb ubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hjb hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjB hhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hj hhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh on success, or }(hj hhhNhNubj)}(h ``SK_DROP``h]hSK_DROP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh on error.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjB hhubeh}(h]bpf-msg-redirect-hashah ]h"]bpf_msg_redirect_hash()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_sk_redirect_hash()h]hbpf_sk_redirect_hash()}(hj+ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj( hhhhhKubj)}(hYlong bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags)h]hYlong bpf_sk_redirect_hash(struct sk_buff *skb, struct bpf_map *map, void *key, u64 flags)}hj9 sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhj( hhubh)}(hXThis helper is used in programs implementing policies at the skb socket level. If the sk_buff ``skb`` is allowed to pass (i.e., if the verdict BPF program returns ``SK_PASS``), redirect it to the socket referenced by ``map`` (of type ``BPF_MAP_TYPE_SOCKHASH``) using hash ``key``. Both ingress and egress interfaces can be used for redirection. The ``BPF_F_INGRESS`` value in ``flags`` is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.h](h^This helper is used in programs implementing policies at the skb socket level. If the sk_buff }(hjH hhhNhNubj)}(h``skb``h]hskb}(hjP hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubh> is allowed to pass (i.e., if the verdict BPF program returns }(hjH hhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hjb hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubh+), redirect it to the socket referenced by }(hjH hhhNhNubj)}(h``map``h]hmap}(hjt hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubh (of type }(hjH hhhNhNubj)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubh ) using hash }(hjH hhhNhNubj)}(h``key``h]hkey}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubhF. Both ingress and egress interfaces can be used for redirection. The }(hjH hhhNhNubj)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubh value in }(hjH hhhNhNubj)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjH ubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hjH hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj( hhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hj hhhNhNubj)}(h ``SK_PASS``h]hSK_PASS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh on success, or }(hj hhhNhNubj)}(h ``SK_DROP``h]hSK_DROP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh on error.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj( hhubeh}(h]bpf-sk-redirect-hashah ]h"]bpf_sk_redirect_hash()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_msg_apply_bytes()h]hbpf_msg_apply_bytes()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubj)}(h. For example, this helper can be used in the following cases:}(hj. hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hhh](h)}(hA single ``sendmsg()`` or ``sendfile()`` system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict.h]h)}(hA single ``sendmsg()`` or ``sendfile()`` system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict.h](h A single }(hjg hhhNhNubj)}(h ``sendmsg()``h]h sendmsg()}(hjo hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjg ubh or }(hjg hhhNhNubj)}(h``sendfile()``h]h sendfile()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjg ubh system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict.}(hjg hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjc ubah}(h]h ]h"]h$]h&]uh1hhj` hhhhhNubh)}(hA BPF program only cares to read the first ``bytes`` of a ``msg``. If the message has a large payload, then setting up and calling the BPF program repeatedly for all bytes, even though the verdict is already known, would create unnecessary overhead. h]h)}(hA BPF program only cares to read the first ``bytes`` of a ``msg``. If the message has a large payload, then setting up and calling the BPF program repeatedly for all bytes, even though the verdict is already known, would create unnecessary overhead.h](h+A BPF program only cares to read the first }(hj hhhNhNubj)}(h ``bytes``h]hbytes}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh of a }(hj hhhNhNubj)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh. If the message has a large payload, then setting up and calling the BPF program repeatedly for all bytes, even though the verdict is already known, would create unnecessary overhead.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj ubah}(h]h ]h"]h$]h&]uh1hhj` hhhhhNubeh}(h]h ]h"]h$]h&]jLjMuh1hhhhKhj hhubh)}(h Returns 0h]h Returns 0}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]bpf-msg-apply-bytesah ]h"]bpf_msg_apply_bytes()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_msg_cork_bytes()h]hbpf_msg_cork_bytes()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubj)}(h;long bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes)h]h;long bpf_msg_cork_bytes(struct sk_msg_buff *msg, u32 bytes)}hj sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhj hhubh)}(hFor socket policies, prevent the execution of the verdict BPF program for message ``msg`` until the number of ``bytes`` have been accumulated.h](hRFor socket policies, prevent the execution of the verdict BPF program for message }(hj hhhNhNubj)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh until the number of }(hj hhhNhNubj)}(h ``bytes``h]hbytes}(hj1 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh have been accumulated.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hThis can be used when one needs a specific number of bytes before a verdict can be assigned, even if the data spans multiple ``sendmsg()`` or ``sendfile()`` calls.h](h}This can be used when one needs a specific number of bytes before a verdict can be assigned, even if the data spans multiple }(hjI hhhNhNubj)}(h ``sendmsg()``h]h sendmsg()}(hjQ hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjI ubh or }(hjI hhhNhNubj)}(h``sendfile()``h]h sendfile()}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjI ubh calls.}(hjI hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(h Returns 0h]h Returns 0}(hj{ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]bpf-msg-cork-bytesah ]h"]bpf_msg_cork_bytes()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_msg_pull_data()h]hbpf_msg_pull_data()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKubj)}(hNlong bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags)h]hNlong bpf_msg_pull_data(struct sk_msg_buff *msg, u32 start, u32 end, u64 flags)}hj sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhj hhubh)}(hFor socket policies, pull in non-linear data from user space for ``msg`` and set pointers ``msg->data`` and ``msg->data_end`` to ``start`` and ``end`` bytes offsets into ``msg``, respectively.h](hAFor socket policies, pull in non-linear data from user space for }(hj hhhNhNubj)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh and set pointers }(hj hhhNhNubj)}(h ``msg->data``h]h msg->data}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh and }(hj hhhNhNubj)}(h``msg->data_end``h]h msg->data_end}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh to }(hj hhhNhNubj)}(h ``start``h]hstart}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh and }hj sbj)}(h``end``h]hend}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh bytes offsets into }(hj hhhNhNubj)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh, respectively.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hXIf a program of type ``BPF_PROG_TYPE_SK_MSG`` is run on a ``msg`` it can only parse data that the (``data``, ``data_end``) pointers have already consumed. For ``sendmsg()`` hooks this is likely the first scatterlist element. But for calls relying on MSG_SPLICE_PAGES (e.g., ``sendfile()``) this will be the range (**0**, **0**) because the data is shared with user space and by default the objective is to avoid allowing user space to modify data while (or after) BPF verdict is being decided. This helper can be used to pull in data and to set the start and end pointers to given values. Data will be copied if necessary (i.e., if data was not linear and if start and end pointers do not point to the same chunk).h](hIf a program of type }(hj+ hhhNhNubj)}(h``BPF_PROG_TYPE_SK_MSG``h]hBPF_PROG_TYPE_SK_MSG}(hj3 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh is run on a }(hj+ hhhNhNubj)}(h``msg``h]hmsg}(hjE hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh" it can only parse data that the (}(hj+ hhhNhNubj)}(h``data``h]hdata}(hjW hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh, }(hj+ hhhNhNubj)}(h ``data_end``h]hdata_end}(hji hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh&) pointers have already consumed. For }(hj+ hhhNhNubj)}(h ``sendmsg()``h]h sendmsg()}(hj{ hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubhf hooks this is likely the first scatterlist element. But for calls relying on MSG_SPLICE_PAGES (e.g., }(hj+ hhhNhNubj)}(h``sendfile()``h]h sendfile()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj+ ubh) this will be the range (}(hj+ hhhNhNubhstrong)}(h**0**h]h0}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj+ ubh, }hj+ sbj )}(h**0**h]h0}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj+ ubhX) because the data is shared with user space and by default the objective is to avoid allowing user space to modify data while (or after) BPF verdict is being decided. This helper can be used to pull in data and to set the start and end pointers to given values. Data will be copied if necessary (i.e., if data was not linear and if start and end pointers do not point to the same chunk).}(hj+ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hX A call to this helper is susceptible to change the underlying packet buffer. Therefore, at load time, all checks on pointers previously done by the verifier are invalidated and must be performed again, if the helper is used in combination with direct packet access.h]hX A call to this helper is susceptible to change the underlying packet buffer. Therefore, at load time, all checks on pointers previously done by the verifier are invalidated and must be performed again, if the helper is used in combination with direct packet access.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(hQAll values for ``flags`` are reserved for future usage, and must be left at zero.h](hAll values for }(hj hhhNhNubj)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh9 are reserved for future usage, and must be left at zero.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h=Returns 0 on success, or a negative error in case of failure.h]h=Returns 0 on success, or a negative error in case of failure.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]bpf-msg-pull-dataah ]h"]bpf_msg_pull_data()ah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hbpf_map_lookup_elem()h]hbpf_map_lookup_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubj)}(h?void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)h]h?void *bpf_map_lookup_elem(struct bpf_map *map, const void *key)}hj sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjhhubh)}(h6Look up a socket entry in the sockmap or sockhash map.h]h6Look up a socket entry in the sockmap or sockhash map.}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubh)}(hNReturns the socket entry associated to ``key``, or NULL if no entry was found.h](h'Returns the socket entry associated to }(hj=hhhNhNubj)}(h``key``h]hkey}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj=ubh , or NULL if no entry was found.}(hj=hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]id1ah ]h"]h$]jah&]uh1hhjhhhhhMjKubh)}(hhh](h)}(hbpf_map_update_elem()h]hbpf_map_update_elem()}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhjdhhhhhMubj)}(h\long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)h]h\long bpf_map_update_elem(struct bpf_map *map, const void *key, const void *value, u64 flags)}hjusbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjdhhubh)}(h6Add or update a socket entry in a sockmap or sockhash.h]h6Add or update a socket entry in a sockmap or sockhash.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjdhhubh)}(h/The flags argument can be one of the following:h]h/The flags argument can be one of the following:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjdhhubh)}(hhh](h)}(hlong bpf_map_delete_elem(struct bpf_map *map, const void *key)h]h>long bpf_map_delete_elem(struct bpf_map *map, const void *key)}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhM"hjhhubh)}(h3Delete a socket entry from a sockmap or a sockhash.h]h3Delete a socket entry from a sockmap or a sockhash.}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM&hjhhubh)}(h=Returns 0 on success, or a negative error in case of failure.h]h=Returns 0 on success, or a negative error in case of failure.}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM(hjhhubeh}(h]bpf-map-delete-elemah ]h"]h$]bpf_map_delete_elem()ah&]uh1hhjhhhhhM!jKubeh}(h] kernel-bpfah ]h"]h$] kernel bpfah&]uh1hhjhhhhhKRjKubh)}(hhh](h)}(h User spaceh]h User space}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhjQhhhhhM+ubh)}(hhh](h)}(hbpf_map_update_elem()h]hbpf_map_update_elem()}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1hhjbhhhhhM-ubj)}(hPint bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags)h]hPint bpf_map_update_elem(int fd, const void *key, const void *value, __u64 flags)}hjssbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhM.hjbhhubh)}(hSockmap entries can be added or updated using the ``bpf_map_update_elem()`` function. The ``key`` parameter is the index value of the sockmap array. And the ``value`` parameter is the FD value of that socket.h](h2Sockmap entries can be added or updated using the }(hjhhhNhNubj)}(h``bpf_map_update_elem()``h]hbpf_map_update_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh function. The }(hjhhhNhNubj)}(h``key``h]hkey}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh< parameter is the index value of the sockmap array. And the }(hjhhhNhNubj)}(h ``value``h]hvalue}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh* parameter is the FD value of that socket.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2hjbhhubh)}(h~Under the hood, the sockmap update function uses the socket FD value to retrieve the associated socket and its attached psock.h]h~Under the hood, the sockmap update function uses the socket FD value to retrieve the associated socket and its attached psock.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM6hjbhhubh)}(h/The flags argument can be one of the following:h]h/The flags argument can be one of the following:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM9hjbhhubh)}(hhh](h)}(hlen; }h]hcSEC("sk_skb/stream_parser") int bpf_prog_parser(struct __sk_buff *skb) { return skb->len; }}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMohj hhubh)}(hThe following code snippet shows a simple verdict program that interacts with a sockmap to redirect traffic to another socket based on the local port.h]hThe following code snippet shows a simple verdict program that interacts with a sockmap to redirect traffic to another socket based on the local port.}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMwhj hhubj)}(hXSEC("sk_skb/stream_verdict") int bpf_prog_verdict(struct __sk_buff *skb) { __u32 lport = skb->local_port; __u32 idx = 0; if (lport == 10000) return bpf_sk_redirect_map(skb, &sock_map_rx, idx, 0); return SK_PASS; }h]hXSEC("sk_skb/stream_verdict") int bpf_prog_verdict(struct __sk_buff *skb) { __u32 lport = skb->local_port; __u32 idx = 0; if (lport == 10000) return bpf_sk_redirect_map(skb, &sock_map_rx, idx, 0); return SK_PASS; }}hj1sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMzhj hhubh)}(h?The following code snippet shows how to declare a sockhash map.h]h?The following code snippet shows how to declare a sockhash map.}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hX,struct socket_key { __u32 src_ip; __u32 dst_ip; __u32 src_port; __u32 dst_port; }; struct { __uint(type, BPF_MAP_TYPE_SOCKHASH); __uint(max_entries, 1); __type(key, struct socket_key); __type(value, __u64); } sock_hash_rx SEC(".maps");h]hX,struct socket_key { __u32 src_ip; __u32 dst_ip; __u32 src_port; __u32 dst_port; }; struct { __uint(type, BPF_MAP_TYPE_SOCKHASH); __uint(max_entries, 1); __type(key, struct socket_key); __type(value, __u64); } sock_hash_rx SEC(".maps");}hjNsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhj hhubh)}(hThe following code snippet shows a simple verdict program that interacts with a sockhash to redirect traffic to another socket based on a hash of some of the skb parameters.h]hThe following code snippet shows a simple verdict program that interacts with a sockhash to redirect traffic to another socket based on a hash of some of the skb parameters.}(hj]hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hXstatic inline void extract_socket_key(struct __sk_buff *skb, struct socket_key *key) { key->src_ip = skb->remote_ip4; key->dst_ip = skb->local_ip4; key->src_port = skb->remote_port >> 16; key->dst_port = (bpf_htonl(skb->local_port)) >> 16; } SEC("sk_skb/stream_verdict") int bpf_prog_verdict(struct __sk_buff *skb) { struct socket_key key; extract_socket_key(skb, &key); return bpf_sk_redirect_hash(skb, &sock_hash_rx, &key, 0); }h]hXstatic inline void extract_socket_key(struct __sk_buff *skb, struct socket_key *key) { key->src_ip = skb->remote_ip4; key->dst_ip = skb->local_ip4; key->src_port = skb->remote_port >> 16; key->dst_port = (bpf_htonl(skb->local_port)) >> 16; } SEC("sk_skb/stream_verdict") int bpf_prog_verdict(struct __sk_buff *skb) { struct socket_key key; extract_socket_key(skb, &key); return bpf_sk_redirect_hash(skb, &sock_hash_rx, &key, 0); }<}hjksbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhj hhubeh}(h]id5ah ]h"]h$]jOah&]uh1hhjhhhhhMYjKubh)}(hhh](h)}(h User spaceh]h User space}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h