qsphinx.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_xskmapmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget"/translations/zh_TW/bpf/map_xskmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget"/translations/it_IT/bpf/map_xskmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget"/translations/ja_JP/bpf/map_xskmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget"/translations/ko_KR/bpf/map_xskmapmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget"/translations/sp_SP/bpf/map_xskmapmodnameN 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+-------------+-------------+ | | prog | | 2 | xsk C | | | | | +-------------+ | | | | | | | | | | +---------+ | | | +---------------------------------------------------+h]hX+---------------------------------------------------+ | xsk A | xsk B | xsk C |<---+ User space =========================================================|========== | Queue 0 | Queue 1 | Queue 2 | | Kernel +---------------------------------------------------+ | | Netdev eth0 | | +---------------------------------------------------+ | | +=============+ | | | | key | xsk | | | | +---------+ +=============+ | | | | | | 0 | xsk A | | | | | | +-------------+ | | | | | | 1 | xsk B | | | | | BPF |-- redirect -->+-------------+-------------+ | | prog | | 2 | xsk C | | | | | +-------------+ | | | | | | | | | | +---------+ | | | +---------------------------------------------------+}hjzsbah}(h]h ]h"]h$]h&]hhforcelanguagenonehighlight_args}uh1jxhhhKhhhhubh)}(hX An AF_XDP socket that is bound to a certain will *only* accept XDP frames from that . If an XDP program tries to redirect from a other than what the socket is bound to, the frame will not be received on the socket.h]h)}(hX An AF_XDP socket that is bound to a certain will *only* accept XDP frames from that . If an XDP program tries to redirect from a other than what the socket is bound to, the frame will not be received on the socket.h](hCAn AF_XDP socket that is bound to a certain will }(hjhhhNhNubhemphasis)}(h*only*h]honly}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh accept XDP frames from that . If an XDP program tries to redirect from a other than what the socket is bound to, the frame will not be received on the socket.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK+hjubah}(h]h ]h"]h$]h&]uh1hhhhhhhhNubh)}(hXTypically an XSKMAP is created per netdev. This map contains an array of XSK File Descriptors (FDs). The number of array elements is typically set or adjusted using the ``max_entries`` map parameter. For AF_XDP ``max_entries`` is equal to the number of queues supported by the netdev.h](hTypically an XSKMAP is created per netdev. This map contains an array of XSK File Descriptors (FDs). The number of array elements is typically set or adjusted using the }(hjhhhNhNubh)}(h``max_entries``h]h max_entries}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh map parameter. For AF_XDP }(hjhhhNhNubh)}(h``max_entries``h]h max_entries}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubh: is equal to the number of queues supported by the netdev.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK0hhhhubh)}(h4Both the map key and map value size must be 4 bytes.h]h)}(hjh]h4Both the map key and map value size must be 4 bytes.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK6hjubah}(h]h ]h"]h$]h&]uh1hhhhhhhhNubh)}(hhh](h)}(hUsageh]hUsage}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK9ubh)}(hhh](h)}(h Kernel BPFh]h Kernel BPF}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubjy)}(h>long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)h]h>long bpf_redirect_map(struct bpf_map *map, u32 key, u64 flags)}hj5sbah}(h]h ]h"]h$]h&]hhjjcj}uh1jxhhhK?hj$hhubh)}(hRedirect the packet to the endpoint referenced by ``map`` at index ``key``. For ``BPF_MAP_TYPE_XSKMAP`` this map contains references to XSK FDs for sockets attached to a netdev's queues.h](h2Redirect the packet to the endpoint referenced by }(hjEhhhNhNubh)}(h``map``h]hmap}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjEubh at index }(hjEhhhNhNubh)}(h``key``h]hkey}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjEubh. For }(hjEhhhNhNubh)}(h``BPF_MAP_TYPE_XSKMAP``h]hBPF_MAP_TYPE_XSKMAP}(hjqhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjEubhU this map contains references to XSK FDs for sockets attached to a netdev’s queues.}(hjEhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKChj$hhubh)}(hIf the map is empty at an index, the packet is dropped. This means that it is necessary to have an XDP program loaded with at least one XSK in the XSKMAP to be able to get any traffic to user space through the socket.h]h)}(hIf the map is empty at an index, the packet is dropped. This means that it is necessary to have an XDP program loaded with at least one XSK in the XSKMAP to be able to get any traffic to user space through the socket.h]hIf the map is empty at an index, the packet is dropped. This means that it is necessary to have an XDP program loaded with at least one XSK in the XSKMAP to be able to get any traffic to user space through the socket.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhjubah}(h]h ]h"]h$]h&]uh1hhj$hhhhhNubeh}(h]bpf-redirect-mapah ]h"]bpf_redirect_map()ah$]h&]uh1hhjhhhhhK>ubh)}(hhh](h)}(hbpf_map_lookup_elem()h]hbpf_map_lookup_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKMubjy)}(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&]hhjjjCj}uh1jxhhhKNhjhhubh)}(hoXSK entry references of type ``struct xdp_sock *`` can be retrieved using the ``bpf_map_lookup_elem()`` helper.h](hXSK entry references of type }(hjhhhNhNubh)}(h``struct xdp_sock *``h]hstruct xdp_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&]uh1hhhhKRhjhhubeh}(h]bpf-map-lookup-elemah ]h"]h$]bpf_map_lookup_elem()ah&]uh1hhjhhhhhKM referencedKubeh}(h] kernel-bpfah ]h"] kernel bpfah$]h&]uh1hhjhhhhhKhhhNhNubh)}(h``struct xdp_sock *``h]hstruct xdp_sock *}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj>ubh& or negative error in case of failure.}(hj>hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKuhjhhubeh}(h]id1ah ]h"]h$]jah&]uh1hhj hhhhhKpjKubh)}(hhh](h)}(hbpf_map_delete_elem()h]hbpf_map_delete_elem()}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjehhhhhKxubjy)}(h0int bpf_map_delete_elem(int fd, const void *key)h]h0int bpf_map_delete_elem(int fd, const void *key)}hjvsbah}(h]h ]h"]h$]h&]hhjjjCj}uh1jxhhhKyhjehhubh)}(hXSK entries can be deleted using the ``bpf_map_delete_elem()`` helper. This helper will return 0 on success, or negative error in case of failure.h](h%XSK entries can be deleted using the }(hjhhhNhNubh)}(h``bpf_map_delete_elem()``h]hbpf_map_delete_elem()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjubhT helper. This helper will return 0 on success, or negative error in case of failure.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK}hjehhubh)}(hZWhen `libxdp`_ deletes an XSK it also removes the associated socket entry from the XSKMAP.h]h)}(hZWhen `libxdp`_ deletes an XSK it also removes the associated socket entry from the XSKMAP.h](hWhen }(hjhhhNhNubjZ)}(h `libxdp`_h]hlibxdp}(hjhhhNhNubah}(h]h ]h"]h$]h&]namelibxdpjk?https://github.com/xdp-project/xdp-tools/tree/master/lib/libxdpuh1jYhjjmKubhL deletes an XSK it also removes the associated socket entry from the XSKMAP.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjehhhhhNubeh}(h]bpf-map-delete-elemah ]h"]bpf_map_delete_elem()ah$]h&]uh1hhj hhhhhKxubeh}(h] user-spaceah ]h"]h$] user spaceah&]uh1hhjhhhhhKVjKubeh}(h]usageah ]h"]usageah$]h&]uh1hhhhhhhhK9ubh)}(hhh](h)}(hExamplesh]hExamples}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(hKernelh]hKernel}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hThe following code snippet shows how to declare a ``BPF_MAP_TYPE_XSKMAP`` called ``xsks_map`` and how to redirect packets to an XSK.h](h2The following code snippet shows how to declare a }(hj hhhNhNubh)}(h``BPF_MAP_TYPE_XSKMAP``h]hBPF_MAP_TYPE_XSKMAP}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh called }(hj hhhNhNubh)}(h ``xsks_map``h]hxsks_map}(hj&hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj ubh' and how to redirect packets to an XSK.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjhhubjy)}(hXstruct { __uint(type, BPF_MAP_TYPE_XSKMAP); __type(key, __u32); __type(value, __u32); __uint(max_entries, 64); } xsks_map SEC(".maps"); SEC("xdp") int xsk_redir_prog(struct xdp_md *ctx) { __u32 index = ctx->rx_queue_index; if (bpf_map_lookup_elem(&xsks_map, &index)) return bpf_redirect_map(&xsks_map, index, 0); return XDP_PASS; }h]hXstruct { __uint(type, BPF_MAP_TYPE_XSKMAP); __type(key, __u32); __type(value, __u32); __uint(max_entries, 64); } xsks_map SEC(".maps"); SEC("xdp") int xsk_redir_prog(struct xdp_md *ctx) { __u32 index = ctx->rx_queue_index; if (bpf_map_lookup_elem(&xsks_map, &index)) return bpf_redirect_map(&xsks_map, index, 0); return XDP_PASS; }}hj>sbah}(h]h ]h"]h$]h&]hhjjjCj}uh1jxhhhKhjhhubeh}(h]kernelah ]h"]kernelah$]h&]uh1hhjhhhhhKubh)}(hhh](h)}(h User spaceh]h User space}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjUhhhhhKubh)}(hKThe following code snippet shows how to update an XSKMAP with an XSK entry.h]hKThe following code snippet shows how to update an XSKMAP with an XSK entry.}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjUhhubjy)}(hX-int update_xsks_map(struct bpf_map *xsks_map, int queue_id, int xsk_fd) { int ret; ret = bpf_map_update_elem(bpf_map__fd(xsks_map), &queue_id, &xsk_fd, 0); if (ret < 0) fprintf(stderr, "Failed to update xsks_map: %s\n", strerror(errno)); return ret; }h]hX-int update_xsks_map(struct bpf_map *xsks_map, int queue_id, int xsk_fd) { int ret; ret = bpf_map_update_elem(bpf_map__fd(xsks_map), &queue_id, &xsk_fd, 0); if (ret < 0) fprintf(stderr, "Failed to update xsks_map: %s\n", strerror(errno)); return ret; }}hjtsbah}(h]h ]h"]h$]h&]hhjjjCj}uh1jxhhhKhjUhhubh)}(hFor an example on how create AF_XDP sockets, please see the AF_XDP-example and AF_XDP-forwarding programs in the `bpf-examples`_ directory in the `libxdp`_ repository. For a detailed explanation of the AF_XDP interface please see:h](hqFor an example on how create AF_XDP sockets, please see the AF_XDP-example and AF_XDP-forwarding programs in the }(hjhhhNhNubjZ)}(h`bpf-examples`_h]h bpf-examples}(hjhhhNhNubah}(h]h ]h"]h$]h&]name bpf-examplesjk+https://github.com/xdp-project/bpf-examplesuh1jYhjjmKubh directory in the }(hjhhhNhNubjZ)}(h `libxdp`_h]hlibxdp}(hjhhhNhNubah}(h]h ]h"]h$]h&]namelibxdpjkjuh1jYhjjmKubhK repository. For a detailed explanation of the AF_XDP interface please see:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjUhhubh)}(hhh](h)}(h`libxdp-readme`_.h]h)}(hjh](jZ)}(h`libxdp-readme`_h]h libxdp-readme}(hjhhhNhNubah}(h]h ]h"]h$]h&]name libxdp-readmejkThttps://github.com/xdp-project/xdp-tools/tree/master/lib/libxdp#using-af_xdp-socketsuh1jYhjjmKubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubh)}(h `AF_XDP`_ kernel documentation. h]h)}(h`AF_XDP`_ kernel documentation.h](jZ)}(h `AF_XDP`_h]hAF_XDP}(hjhhhNhNubah}(h]h ]h"]h$]h&]nameAF_XDPjkjluh1jYhjjmKubh kernel documentation.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjhhhhhNubeh}(h]h ]h"]h$]h&]jjuh1hhhhKhjUhhubh)}(hJThe most comprehensive resource for using XSKMAPs and AF_XDP is `libxdp`_.h]h)}(hjh](h@The most comprehensive resource for using XSKMAPs and AF_XDP is }(hjhhhNhNubjZ)}(h `libxdp`_h]hlibxdp}(hjhhhNhNubah}(h]h ]h"]h$]h&]namelibxdpjkjuh1jYhjjmKubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhjubah}(h]h ]h"]h$]h&]uh1hhjUhhhhhNubhtarget)}(hK.. _libxdp: https://github.com/xdp-project/xdp-tools/tree/master/lib/libxdph]h}(h]libxdpah ]h"]libxdpah$]h&]jkjuh1j>hKhjUhhhhjKubj?)}(hI.. _AF_XDP: https://www.kernel.org/doc/html/latest/networking/af_xdp.htmlh]h}(h]af-xdpah ]h"]af_xdpah$]h&]jkjluh1j>hKhjUhhhhjKubj?)}(h=.. _bpf-examples: https://github.com/xdp-project/bpf-examplesh]h}(h] bpf-examplesah ]h"] bpf-examplesah$]h&]jkjuh1j>hKhjUhhhhjKubj?)}(hg.. _libxdp-readme: https://github.com/xdp-project/xdp-tools/tree/master/lib/libxdp#using-af_xdp-socketsh]h}(h] libxdp-readmeah ]h"] libxdp-readmeah$]h&]jkjuh1j>hKhjUhhhhjKubeh}(h]id2ah ]h"]h$]jah&]uh1hhjhhhhhKjKubeh}(h]examplesah ]h"]examplesah$]h&]uh1hhhhhhhhKubeh}(h]bpf-map-type-xskmapah ]h"]bpf_map_type_xskmapah$]h&]uh1hhhhhhhhKubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}(af_xdp](j[jelibxdp](jjje bpf-examples]ja libxdp-readme]jaurefids}nameids}(jjjjj jjjbpf_map_lookup_elem()N user spaceNjjjjj|jyjRjOjIjFjUjRjaj^jmjju nametypes}(jjj jjjjjj|jRjIjUjajmuh}(jhjjjjjj$jjjj jj5j`jjjejyjjOjjrjUjFj@jRjLj^jXjjjdu 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