Commit Graph

421 Commits

Author SHA1 Message Date
Jesper Dangaard Brouer
8703e1a320 AF_XDP-interaction: Explain xsk_btf__read_field in user code
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-11 14:34:43 +01:00
Jesper Dangaard Brouer
a333281166 lib_xsk_extend: Explain xsk_btf__read_field hashmap caching
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 22:10:43 +01:00
Jesper Dangaard Brouer
f61c9be97f lib_xsk_extend: Rename xsk_btf__read_member to xsk_btf__read
Making it the main API.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 21:59:14 +01:00
Jesper Dangaard Brouer
9b36c233fe lib_xsk_extend: Let XSK_BTF_READ_INTO call xsk_btf__read_member
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 21:52:56 +01:00
Jesper Dangaard Brouer
465f069fab lib_xsk_extend: API change introduce xsk_btf__read_field
Rename xsk_btf__read to xsk_btf__read_field.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 20:56:32 +01:00
Jesper Dangaard Brouer
be949736ef AF_XDP-interaction: Also introduce BTF mirror for struct xdp_hints_mark
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 19:39:50 +01:00
Jesper Dangaard Brouer
219ea356a4 AF_XDP-interaction: btf_id zero indicate no BTF info
Zero init xdp_hints_rx_time, which means xdp_hints_rx_time.btf_type_id
is zero if setup_btf_info() didn't find the struct name.

In print_meta_info_via_btf() we also exit if btf_id is zero, as
this isn't a valid BTF type id.

Thus, we don't need to explicitly handle "error" case of a btf_type_id
being zero, it will just naturally not match when userspace could not
find the struct name.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 17:47:11 +01:00
Jesper Dangaard Brouer
1cb72f03df AF_XDP-interaction: introduce struct to mirror kernel-side struct
The 'struct xdp_hints_rx_time' exist both in kernel-side (af_xdp_kern.c)
and userspace program af_xdp_user.c, but are very different.

The kernel-side is the actual XDP-hints memory layout used in metadata area.

The userspace side uses the same struct name, but contains BTF info
that allow us to access the struct members from kernel-side struct.
The BTF info is extracted on userspace startup, from the BPF-prog
ELF data BTF section.

Caching BTF lookups at startup is done, as target application
is a real-time application.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-09 16:50:35 +01:00
Toke Høiland-Jørgensen
54d67979a5 Merge pull request #27 from rst0git/readme
readme: fix typos
2021-11-06 15:38:27 +01:00
Radostin Stoyanov
7f6f9d5a9d readme: fix typos
Signed-off-by: Radostin Stoyanov <radostin@redhat.com>
2021-11-06 12:33:09 +00:00
Jesper Dangaard Brouer
5e93cf0993 AF_XDP-interaction: Code cleanup, remove local BTF code
The local BTF code in af_xdp_user.c was only used for
debugging the BTF structures while developing and testing
the lib_xsk_extend.c code.

If is confusing for other reading the code, so simply
remove thisi, as the lib_xsk_extend.c should hide
these details fpr API users.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-05 15:59:33 +01:00
Jesper Dangaard Brouer
87654aecdd AF_XDP-interaction: code cleanup, remove manual C type-casting approach
This method have been obsoleted by the BTF approach.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-05 15:50:34 +01:00
Jesper Dangaard Brouer
d84ced9730 AF_XDP-interaction: Fix to work on newer kernels supporting bpf_link
Program fails on newer kernels, with error message:

 libbpf: Netlink-based XDP prog detected, please unload it in order to launch AF_XDP prog
 ERROR: Can't setup AF_XDP socket "Invalid argument"

Since kernel v5.13 libbpf version, when bpf_link support is
detected then libbpf/xsk require XDP/BPF programs use this feature.
See kernel commit 10397994d30f ("libbpf: xsk: Use bpf_link").

To continue using our netlink-based XDP attach approach,
instruct libbpf/xsk to not be in change of loading the
BPF-prog.  As our XDP-prog is special it also makes
to control this ourselves.

This is achived with the flag XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD

 xsk_cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD

Remember to update the xskmap manually.

Fixes: 10397994d30f ("libbpf: xsk: Use bpf_link")
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-05 14:54:50 +01:00
Jesper Dangaard Brouer
1807d1024b AF_XDP-interaction: Add README.org
I keep forgetting the API docs, so lets add links in the README.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-05 13:40:20 +01:00
Jesper Dangaard Brouer
b028f24513 lib_xsk_extend: Add comment describing XDP metadata and BTF
Add a large comments to the most central piece of code that
access the right offset into the metadata based on the BTF
info we have extracted.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-05 10:13:42 +01:00
Jesper Dangaard Brouer
f46743a75f AF_XDP-interaction: Enable AF_XDP code that reply to ICMPV6_ECHO_REQUEST
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 15:54:21 +01:00
Jesper Dangaard Brouer
173cc762fd AF_XDP-interaction: Let netstack handle ARP and IPv6 NDP
NDP (Neighbor Discovery Protocol)

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 15:25:49 +01:00
Jesper Dangaard Brouer
556d6fd47b AF_XDP-interaction: Move refill of fill-queue to after RX processing
The root cause of the slowdown on the first packet with timestamps,
as primary related to refilling the fill-queue, which happend before
process_packet.

Primary cause, as first packet still see a slowdown of around 4 usec
while it was around 9 usec before.

In commit 5df5332f23 ("AF_XDP-interaction: config AF_XDP frame_headroom")
the fill-queue size was increase to be larger.  This caused the
first call to handle_receive_packets() to refill too many frames
into the fill-queue.

Patch split out refill into function restock_receive_fill_queue()
which is now called *after* process_packet step, but before
releasing RX packets via xsk_ring_cons__release(&xsk->rx).

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 13:49:20 +01:00
Jesper Dangaard Brouer
6016a13008 AF_XDP-interaction: Usage of xsk_btf__read_member API
This API xsk_btf__read_member() is faster, but it was not the
root cause of the slowdown on the first packet with timestamps.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 12:15:16 +01:00
Jesper Dangaard Brouer
7af928b8a8 lib_xsk_extend: Expose more direct xsk_btf_member API
With the current xsk_btf__read() we are seeing a slowdown on the
first packet with timestamps.  The theory is this is caused
by the allocation for the cached hashmap entry.

meta-time rx_ktime:870394156129518 time_now:870394156139039 diff:9521 ns
meta-time rx_ktime:870396208293894 time_now:870396208295098 diff:1204 ns
meta-time rx_ktime:870398256286553 time_now:870398256287772 diff:1219 ns

Create a new API that can access struct members more directly
via caching the xsk_btf_member offset + size in the C-code API user.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 10:25:16 +01:00
Jesper Dangaard Brouer
6b200aa8d5 lib_xsk_extend: rename xsk_btf_entry to xsk_btf_member
And export xsk_btf_member in header file.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 10:01:30 +01:00
Jesper Dangaard Brouer
a37b82bd9d AF_XDP-interaction: lib_xsk_extend move entry allocation
Avoid doing a malloc in xsk_btf__field_entry().

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 09:44:53 +01:00
Jesper Dangaard Brouer
07617277ab AF_XDP-interaction: Cleanup debugging code in lib_xsk_extend
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-04 09:18:44 +01:00
Jesper Dangaard Brouer
b06ebaf7d9 AF_XDP-interaction: Implement BTF extraction of 'mark'
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 21:59:13 +01:00
Jesper Dangaard Brouer
10626881f1 AF_XDP-interaction: Cleanups for af_xdp_user
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 21:43:11 +01:00
Jesper Dangaard Brouer
778b5e6cdf AF_XDP-interaction: info for using macro XSK_BTF_READ_INTO
The program segfaulted if using the XSK_BTF_READ_INTO macro
with field as a C-string e.g. "rx_ktime".

Add comments to highlight this happens.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 21:06:55 +01:00
Jesper Dangaard Brouer
5080226836 AF_XDP-interaction: Use xsk_btf__read correctly
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 20:55:46 +01:00
Jesper Dangaard Brouer
d758f4f919 AF_XDP-interaction: Fix lib_xsk_extend entry->size bug
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 19:51:52 +01:00
Jesper Dangaard Brouer
3488e02d00 AF_XDP-interaction: Trying to use xsk_btf__read
This doesn't work with packet test.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 19:21:16 +01:00
Jesper Dangaard Brouer
8bb3d3ce3d AF_XDP-interaction: steps towards using BTF id during pkt processing
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 18:14:17 +01:00
Jesper Dangaard Brouer
20a07fa297 AF_XDP-interaction: Adapt lib_xsk_extend while trying to use
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 15:27:09 +01:00
Ederson de Souza
34ef433573 Proposed: Helpers to access XDP hints based on BTF definitions
Based on:
 Subj: libbpf: Helpers to access XDP hints based on BTF definitions
  https://lore.kernel.org/all/20210803010331.39453-15-ederson.desouza@intel.com/
 From: Ederson de Souza <ederson.desouza@intel.com>

Ederson says:

 A new set of functions to help get the BTF definition of XDP hints
 structure and get the information based on it.

 `xsk_umem__btf_id` helps retrieve the BTF id of XDP metadata.
 `xsk_btf__init` sets up a context based on the BTF, including a hashmap,
 so that subsequent queries are faster.
 `xsk_btf__read` returns a pointer to the position in the XDP metadata
 containing a given field.
 `xsk_btf__has_field` checks the presence of a field in the BTF.
 `xsk_btf__free` frees up the context.

 Besides those, a macro `XSK_BTF_READ_INTO` acts as a convenient helper
 to read the field contents into a given variable.

 Note that currently, the hashmap used to speed-up offset location into
 the BTF doesn't use the field name as a string as key to the hashmap. It
 directly uses the pointer value instead, as it is expected that most of
 time, field names will be addressed by a shared constant string residing
 on read-only memory, thus saving some time. If this assumption is not
 entirely true, this optimisation needs to be rethought (or discarded
 altogether).

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 12:23:56 +01:00
Jesper Dangaard Brouer
bb5105e5ca AF_XDP-interaction: Makefile add hashmap to be compiled
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 11:34:58 +01:00
Jesper Dangaard Brouer
4cb152a3c7 AF_XDP-interaction: Using btf__resolve_size for struct members possible
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 11:07:47 +01:00
Andrii Nakryiko
e4168200de Import libbpf: resizable non-thread safe internal hashmap
This is a copy of the kernels/libbpf hashmap
 tools/lib/bpf/hashmap.{h,c}

The plan is to prototype an AF_XDP/xsk userspace API for accessing
BTF information, that should be moved to libbpf (or libxdp).  Thus,
this hashmap code will become avail if successful.

Original kernel commit 553db8ba73df ("libbpf: add resizable non-thread
safe internal hashmap") (Author: Andrii Nakryiko). Thus, giving Andrii
author credit in this git commit.

Andrii Nakryiko said:

    libbpf: add resizable non-thread safe internal hashmap

    There is a need for fast point lookups inside libbpf for multiple use
    cases (e.g., name resolution for BTF-to-C conversion, by-name lookups in
    BTF for upcoming BPF CO-RE relocation support, etc). This patch
    implements simple resizable non-thread safe hashmap using single linked
    list chains.

    Four different insert strategies are supported:
     - HASHMAP_ADD - only add key/value if key doesn't exist yet;
     - HASHMAP_SET - add key/value pair if key doesn't exist yet; otherwise,
       update value;
     - HASHMAP_UPDATE - update value, if key already exists; otherwise, do
       nothing and return -ENOENT;
     - HASHMAP_APPEND - always add key/value pair, even if key already exists.
       This turns hashmap into a multimap by allowing multiple values to be
       associated with the same key. Most useful read API for such hashmap is
       hashmap__for_each_key_entry() iteration. If hashmap__find() is still
       used, it will return last inserted key/value entry (first in a bucket
       chain).

    For HASHMAP_SET and HASHMAP_UPDATE, old key/value pair is returned, so
    that calling code can handle proper memory management, if necessary.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-03 11:01:36 +01:00
Jesper Dangaard Brouer
1a02d53b5f XDP-interaction: BTF try walking struct BTF members
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 16:37:24 +01:00
Jesper Dangaard Brouer
2778646e37 XDP-interaction: Playing with BTF APIs
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 14:52:40 +01:00
Jesper Dangaard Brouer
49c33e775f XDP-interaction: Require loading custom BPF program
Don't allow loading the default xsk BPF-prog if not specifying
any BPF-prog.  We have a need for our own BPf-prog.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 14:34:43 +01:00
Jesper Dangaard Brouer
5245641946 AF_XDP-interaction: Improve comments explaning attributes
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 13:58:39 +01:00
Jesper Dangaard Brouer
c905867f29 AF_XDP-interaction: Make the two meta data types different sizes
On purpose to make it more difficult for userspace side.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 12:40:49 +01:00
Jesper Dangaard Brouer
555aec82a7 AF_XDP-interaction: Two meta data structs
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-11-02 10:46:11 +01:00
Jesper Dangaard Brouer
69c0f60008 AF_XDP-interaction: Make BPF-prog BTF aware
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-29 17:54:09 +02:00
Jesper Dangaard Brouer
858778aa83 AF_XDP-interaction: crude extraction of metadata area
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-29 16:06:42 +02:00
Jesper Dangaard Brouer
1bc02a7470 AF_XDP-interaction: Add --pktinfo option that print info on pkt
To verify the contents of the incomming packets add a
function print_pkt_info() that decode part of the packet
headers and print IP-header src+dst (both IPv4 and IPv6).

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-29 14:53:33 +02:00
Jesper Dangaard Brouer
54d51b06b4 AF_XDP-interaction: Have XDP-prog with something into meta area
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 18:45:41 +02:00
Jesper Dangaard Brouer
5df5332f23 AF_XDP-interaction: config AF_XDP frame_headroom
The default frame_headroom seems to be zero as
define XSK_UMEM__DEFAULT_FRAME_HEADROOM is zero.

This patch does explicit xsk_umem_config setup.
Comment taken from kernel/samples/bpf/xdpsock_user.c.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 18:20:56 +02:00
Jesper Dangaard Brouer
359cc27c72 AF_XDP-interaction: use bpf_xdp_adjust_meta
This only works in skb-mode, other modes fail the bpf_xdp_adjust_meta
call and result in XDP_ABORTED.  This only occurred on mlx5.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 17:42:23 +02:00
Jesper Dangaard Brouer
9ef9104c61 AF_XDP-interaction: Fix strncpy warning in common_user_bpf_xdp.c
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 14:20:14 +02:00
Jesper Dangaard Brouer
45a7e3cab7 lib/Makefile: Enable compiler warnings to USER_TARGETS_OBJS
Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 13:51:09 +02:00
Jesper Dangaard Brouer
6f0a1e3f01 AF_XDP-interaction: Use bpf_program__section_name
BPF program title (bpf_program__title) is getting deprecated
in libbpf use bpf_program__section_name() instead.

Signed-off-by: Jesper Dangaard Brouer <brouer@redhat.com>
2021-10-28 13:35:12 +02:00