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]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}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhubh& 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)}(hjh](h)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh& was introduced in kernel version 4.18}(hjhhhNhNubeh}(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](h)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh and }(hj@hhhNhNubh)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh 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 }(hj@hhhNhNubh)}(h``bpf_sk_redirect_map()``h]hbpf_sk_redirect_map()}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh, }(hj@hhhNhNubh)}(h``bpf_sk_redirect_hash()``h]hbpf_sk_redirect_hash()}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh, }(hj@hhhNhNubh)}(h``bpf_msg_redirect_map()``h]hbpf_msg_redirect_map()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh and }hj@sbh)}(h``bpf_msg_redirect_hash()``h]hbpf_msg_redirect_hash()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@ubh.}(hj@hhhNhNubeh}(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](h)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhY is backed by an array that uses an integer key as the index to look up a reference to a }(hjhhhNhNubh)}(h``struct sock``h]h struct sock}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh4. The map values are socket descriptors. Similarly, }(hjhhhNhNubh)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhX 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 }(hjhhhNhNubh)}(h``struct sock *``h]h struct sock *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh= that the map holds to user-space is neither safe nor useful.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(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 }(hj hhhNhNubh)}(h ``__SK_DROP``h]h __SK_DROP}(hj(hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh, }(hj hhhNhNubh)}(h ``__SK_PASS``h]h __SK_PASS}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh, or }(hj hhhNhNubh)}(h``__SK_REDIRECT``h]h __SK_REDIRECT}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh.}(hj hhhNhNubeh}(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 }(hjdhhhNhNubh)}(h``struct sk_psock``h]hstruct sk_psock}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjdubh' is attached to it. Additionally, this }(hjdhhhNhNubh)}(h ``sk_psock``h]hsk_psock}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjdubh4 inherits the programs that are attached to the map.}(hjdhhhNhNubeh}(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&]hhforcelanguagechighlight_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 }(hjhhhNhNubh)}(h``stream_verdict``h]hstream_verdict}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh and }(hjhhhNhNubh)}(h``skb_verdict``h]h skb_verdict}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh 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)}(hjh](h)}(h``msg_parser``h]h msg_parser}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh program - }(hjhhhNhNubh)}(h``BPF_SK_MSG_VERDICT``h]hBPF_SK_MSG_VERDICT}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK;hjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubh)}(h9``stream_parser`` program - ``BPF_SK_SKB_STREAM_PARSER``.h]h)}(hjMh](h)}(h``stream_parser``h]h stream_parser}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjOubh program - }(hjOhhhNhNubh)}(h``BPF_SK_SKB_STREAM_PARSER``h]hBPF_SK_SKB_STREAM_PARSER}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjOubh.}(hjOhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubeh}(h]h ]h"]h$]h&]j8j9uh1hhhhK;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: }(hjhhhNhNubh)}(h``bpf_msg_apply_bytes()``h]hbpf_msg_apply_bytes()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh and }(hjhhhNhNubh)}(h``bpf_msg_cork_bytes()``h]hbpf_msg_cork_bytes()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh. With }(hjhhhNhNubh)}(h``bpf_msg_apply_bytes()``h]hbpf_msg_apply_bytes()}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhg BPF programs can tell the infrastructure how many bytes the given verdict should apply to. The helper }(hjhhhNhNubh)}(h``bpf_msg_cork_bytes()``h]hbpf_msg_cork_bytes()}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh 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.}(hjhhhNhNubeh}(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 }(hjMhhhNhNubh)}(h``bpf_msg_pull_data()``h]hbpf_msg_pull_data()}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMubh and }(hjMhhhNhNubh)}(h``bpf_msg_push_data()``h]hbpf_msg_push_data()}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMubh are available to }(hjMhhhNhNubh)}(h``BPF_PROG_TYPE_SK_MSG``h]hBPF_PROG_TYPE_SK_MSG}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMubhk BPF programs to pull in data and set the start and end pointers to given values or to add metadata to the }(hjMhhhNhNubh)}(h``struct sk_msg_buff *msg``h]hstruct sk_msg_buff *msg}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjMubh.}(hjMhhhNhNubeh}(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 }(hjhhhNhNubh)}(h``msg``h]hmsg}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh> is allowed to pass (i.e., if the verdict BPF program returns }(hjhhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh+), redirect it to the socket referenced by }(hjhhhNhNubh)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh (of type }(hjhhhNhNubh)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ) at index }(hjhhhNhNubh)}(h``key``h]hkey}(hjChhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhF. Both ingress and egress interfaces can be used for redirection. The }(hjhhhNhNubh)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh value in }(hjhhhNhNubh)}(h ``flags``h]hflags}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhs 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 }(hjhhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh on success, or }(hjhhhNhNubh)}(h ``SK_DROP``h]hSK_DROP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh 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 }(hjhhhNhNubh)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh (of type }(hjhhhNhNubh)}(h``BPF_MAP_TYPE_SOCKMAP``h]hBPF_MAP_TYPE_SOCKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh ) at index }(hjhhhNhNubh)}(h``key``h]hkey}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhF. Both ingress and egress interfaces can be used for redirection. The }(hjhhhNhNubh)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh value in }(hjhhhNhNubh)}(h ``flags``h]hflags}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhs 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 }(hjAhhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjAubh on success, or }(hjAhhhNhNubh)}(h ``SK_DROP``h]hSK_DROP}(hj[hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjAubh on error.}(hjAhhhNhNubeh}(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()}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj{hhhhhKrubj)}(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}uh1jhhhKshj{hhubh)}(hesocket entries of type ``struct sock *`` can be retrieved using the ``bpf_map_lookup_elem()`` helper.h](hsocket entries of type }(hjhhhNhNubh)}(h``struct sock *``h]h struct sock *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh can be retrieved using the }(hjhhhNhNubh)}(h``bpf_map_lookup_elem()``h]hbpf_map_lookup_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh helper.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKwhj{hhubeh}(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 }(hjhhhNhNubh)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh referencing sockets. The }(hjhhhNhNubh)}(h ``skops``h]hskops}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh4 is used as a new value for the entry associated to }(hjhhhNhNubh)}(h``key``h]hkey}(hj"hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh. The }(hjhhhNhNubh)}(h ``flags``h]hflags}(hj4hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh& argument can be one of the following:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hhh](h)}(h@``BPF_ANY``: Create a new element or update an existing element.h]h)}(hjQh](h)}(h ``BPF_ANY``h]hBPF_ANY}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjSubh5: Create a new element or update an existing element.}(hjShhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjOubah}(h]h ]h"]h$]h&]uh1hhjLhhhhhNubh)}(h?``BPF_NOEXIST``: Create a new element only if it did not exist.h]h)}(hjvh](h)}(h``BPF_NOEXIST``h]h BPF_NOEXIST}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjxubh0: Create a new element only if it did not exist.}(hjxhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjtubah}(h]h ]h"]h$]h&]uh1hhjLhhhhhNubh)}(h+``BPF_EXIST``: Update an existing element. h]h)}(h*``BPF_EXIST``: Update an existing element.h](h)}(h ``BPF_EXIST``h]h BPF_EXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh: Update an existing element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjLhhhhhNubeh}(h]h ]h"]h$]h&]j8j9uh1hhhhKhjhhubh)}(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 }(hjhhhNhNubh)}(h``map``h]hmap}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh 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 }(hjhhhNhNubh)}(h``map``h]hmap}(hj#hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh referencing sockets. The }(hjhhhNhNubh)}(h ``skops``h]hskops}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh4 is used as a new value for the entry associated to }(hjhhhNhNubh)}(h``key``h]hkey}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(h3The ``flags`` argument can be one of the following:h](hThe }(hj_hhhNhNubh)}(h ``flags``h]hflags}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1hhj_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)}(hjh](h)}(h ``BPF_ANY``h]hBPF_ANY}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh5: 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](h)}(h``BPF_NOEXIST``h]h BPF_NOEXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh0: 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](h)}(h ``BPF_EXIST``h]h BPF_EXIST}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh: Update an existing element.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubeh}(h]h ]h"]h$]h&]j8j9uh1hhhhKhjhhubh)}(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 }(hjhhhNhNubh)}(h``map``h]hmap}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh 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.}(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()}(hj1 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj. 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)}hj? sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhKhj. 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 }(hjN hhhNhNubh)}(h``msg``h]hmsg}(hjV hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubh> is allowed to pass (i.e., if the verdict BPF program returns }(hjN hhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hjh hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubh+), redirect it to the socket referenced by }(hjN hhhNhNubh)}(h``map``h]hmap}(hjz hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubh (of type }(hjN hhhNhNubh)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubh ) using hash }(hjN hhhNhNubh)}(h``key``h]hkey}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubhF. Both ingress and egress interfaces can be used for redirection. The }(hjN hhhNhNubh)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubh value in }(hjN hhhNhNubh)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjN ubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hjN hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj. hhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hj hhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh on success, or }(hj hhhNhNubh)}(h ``SK_DROP``h]hSK_DROP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh on error.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj. 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)}hj% 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 }(hj4 hhhNhNubh)}(h``skb``h]hskb}(hj< hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubh> is allowed to pass (i.e., if the verdict BPF program returns }(hj4 hhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hjN hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubh+), redirect it to the socket referenced by }(hj4 hhhNhNubh)}(h``map``h]hmap}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubh (of type }(hj4 hhhNhNubh)}(h``BPF_MAP_TYPE_SOCKHASH``h]hBPF_MAP_TYPE_SOCKHASH}(hjr hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubh ) using hash }(hj4 hhhNhNubh)}(h``key``h]hkey}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubhF. Both ingress and egress interfaces can be used for redirection. The }(hj4 hhhNhNubh)}(h``BPF_F_INGRESS``h]h BPF_F_INGRESS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubh value in }(hj4 hhhNhNubh)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj4 ubhs is used to select the ingress path otherwise the egress path is selected. This is the only flag supported for now.}(hj4 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(h8Returns ``SK_PASS`` on success, or ``SK_DROP`` on error.h](hReturns }(hj hhhNhNubh)}(h ``SK_PASS``h]hSK_PASS}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh on success, or }(hj hhhNhNubh)}(h ``SK_DROP``h]hSK_DROP}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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 }(hjS hhhNhNubh)}(h ``sendmsg()``h]h sendmsg()}(hj[ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjS ubh or }(hjS hhhNhNubh)}(h``sendfile()``h]h sendfile()}(hjm hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjS ubh system call contains multiple logical messages that the BPF program is supposed to read and for which it should apply a verdict.}(hjS hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjO ubah}(h]h ]h"]h$]h&]uh1hhjL 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 hhhNhNubh)}(h ``bytes``h]hbytes}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh of a }(hj hhhNhNubh)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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&]uh1hhjL hhhhhNubeh}(h]h ]h"]h$]h&]j8j9uh1hhhhKhj 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 hhhNhNubh)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh until the number of }(hj hhhNhNubh)}(h ``bytes``h]hbytes}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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 }(hj5 hhhNhNubh)}(h ``sendmsg()``h]h sendmsg()}(hj= hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj5 ubh or }(hj5 hhhNhNubh)}(h``sendfile()``h]h sendfile()}(hjO hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj5 ubh calls.}(hj5 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj hhubh)}(h Returns 0h]h Returns 0}(hjg 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 hhhNhNubh)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and set pointers }(hj hhhNhNubh)}(h ``msg->data``h]h msg->data}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and }(hj hhhNhNubh)}(h``msg->data_end``h]h msg->data_end}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh to }(hj hhhNhNubh)}(h ``start``h]hstart}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh and }hj sbh)}(h``end``h]hend}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh bytes offsets into }(hj hhhNhNubh)}(h``msg``h]hmsg}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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 hhhNhNubh)}(h``BPF_PROG_TYPE_SK_MSG``h]hBPF_PROG_TYPE_SK_MSG}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh is run on a }(hj hhhNhNubh)}(h``msg``h]hmsg}(hj1 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh" it can only parse data that the (}(hj hhhNhNubh)}(h``data``h]hdata}(hjC hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh, }(hj hhhNhNubh)}(h ``data_end``h]hdata_end}(hjU hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh&) pointers have already consumed. For }(hj hhhNhNubh)}(h ``sendmsg()``h]h sendmsg()}(hjg hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubhf hooks this is likely the first scatterlist element. But for calls relying on MSG_SPLICE_PAGES (e.g., }(hj hhhNhNubh)}(h``sendfile()``h]h sendfile()}(hjy hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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 hhhNhNubh)}(h ``flags``h]hflags}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj 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()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubj)}(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}uh1jhhhMhj hhubh)}(h6Look up a socket entry in the sockmap or sockhash map.h]h6Look up a socket entry in the sockmap or sockhash map.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hj hhubh)}(hNReturns the socket entry associated to ``key``, or NULL if no entry was found.h](h'Returns the socket entry associated to }(hj)hhhNhNubh)}(h``key``h]hkey}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj)ubh , or NULL if no entry was found.}(hj)hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]id1ah ]h"]h$]jah&]uh1hhjhhhhhMjKubh)}(hhh](h)}(hbpf_map_update_elem()h]hbpf_map_update_elem()}(hjShhhNhNubah}(h]h ]h"]h$]h&]uh1hhjPhhhhhMubj)}(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)}hjasbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjPhhubh)}(h6Add or update a socket entry in a sockmap or sockhash.h]h6Add or update a socket entry in a sockmap or sockhash.}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjPhhubh)}(h/The flags argument can be one of the following:h]h/The flags argument can be one of the following:}(hj~hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjPhhubh)}(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.}(hjhhhNhNubah}(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.}(hjhhhNhNubah}(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}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj=hhhhhM+ubh)}(hhh](h)}(hbpf_map_update_elem()h]hbpf_map_update_elem()}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjNhhhhhM-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)}hj_sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhM.hjNhhubh)}(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 }(hjnhhhNhNubh)}(h``bpf_map_update_elem()``h]hbpf_map_update_elem()}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjnubh function. The }(hjnhhhNhNubh)}(h``key``h]hkey}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjnubh< parameter is the index value of the sockmap array. And the }(hjnhhhNhNubh)}(h ``value``h]hvalue}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjnubh* parameter is the FD value of that socket.}(hjnhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM2hjNhhubh)}(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&]uh1hhhhM6hjNhhubh)}(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&]uh1hhhhM9hjNhhubh)}(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}uh1jhhhMohjhhubh)}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMwhjhhubj)}(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; }}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMzhjhhubh)}(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&]uh1hhhhMhjhhubj)}(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");}hj:sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjhhubh)}(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.}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(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); }}hjWsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjhhubeh}(h]id5ah ]h"]h$]j;ah&]uh1hhjhhhhhMYjKubh)}(hhh](h)}(h User spaceh]h User space}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1hhjmhhhhhMubh)}(hj;jJjGu nametypes}(jZjjjjxjjj+ j j j jz j jjjj(jRjjjjj&j2j>jJuh}(jWhjjj7jjjjujjj{jjj( jj j. j j j j jw j j j} jKj jjPj/jjj=jjNj}j$jjj%jjhjjjmjOj+jjjjj jjjj#jj/j)j;j5jGjAu footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jKsRparse_messages](hsystem_message)}(hhh]h)}(h8Duplicate implicit target name: "bpf_map_lookup_elem()".h]h