| 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
| 2 | /* |
| 3 | * Copyright (C) 2021-2025 Linutronix GmbH |
| 4 | * Author Kurt Kanzenbach <[email protected]> |
| 5 | */ |
| 6 | |
| 7 | #ifndef _XDP_H_ |
| 8 | #define _XDP_H_ |
| 9 | |
| 10 | #include <stdbool.h> |
| 11 | #include <stddef.h> |
| 12 | #include <stdint.h> |
| 13 | |
| 14 | #include <linux/if_xdp.h> |
| 15 | |
| 16 | #include "app_config.h" |
| 17 | |
| 18 | #include <bpf/bpf.h> |
| 19 | #include <bpf/libbpf.h> |
| 20 | |
| 21 | #include <xdp/libxdp.h> |
| 22 | #include <xdp/xsk.h> |
| 23 | |
| 24 | #include "security.h" |
| 25 | #include "stat.h" |
| 26 | |
| 27 | #undef XSK_RING_CONS__DEFAULT_NUM_DESCS |
| 28 | #undef XSK_RING_PROD__DEFAULT_NUM_DESCS |
| 29 | #define XSK_RING_CONS__DEFAULT_NUM_DESCS 4096 |
| 30 | #define XSK_RING_PROD__DEFAULT_NUM_DESCS 4096 |
| 31 | |
| 32 | #define XDP_BATCH_SIZE 512 |
| 33 | #define XDP_NUM_FRAMES (XSK_RING_CONS__DEFAULT_NUM_DESCS + XSK_RING_PROD__DEFAULT_NUM_DESCS) |
| 34 | #define XDP_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE |
| 35 | |
| 36 | struct xsk_umem_info { |
| 37 | struct xsk_ring_prod fq; |
| 38 | struct xsk_ring_cons cq; |
| 39 | struct xsk_umem *umem; |
| 40 | void *buffer; |
| 41 | }; |
| 42 | |
| 43 | struct xdp_tx_hwts { |
| 44 | /* Store HW timestamps from AF_XDP completion queue */ |
| 45 | struct round_trip_context *rtt; |
| 46 | /* Sequence number for TX HW timestamp (available one cycle later) */ |
| 47 | int64_t seq_lagged; |
| 48 | /* Counter for tracking TX HW timestamp completions in current cycle (max 2: first+last) */ |
| 49 | uint32_t count; |
| 50 | /* Total number of frames per cycle (needed to identify last packet) */ |
| 51 | uint32_t frames_per_cycle; |
| 52 | }; |
| 53 | |
| 54 | struct xdp_socket { |
| 55 | uint64_t outstanding_tx; |
| 56 | struct xsk_ring_cons rx; |
| 57 | struct xsk_ring_prod tx; |
| 58 | struct xsk_umem_info umem; |
| 59 | struct xsk_socket *xsk; |
| 60 | struct xdp_program *prog; |
| 61 | /* TX hardware timestamping context */ |
| 62 | struct xdp_tx_hwts tx_hwts; |
| 63 | int fd; |
| 64 | bool busy_poll_mode; |
| 65 | bool tx_time_mode; |
| 66 | bool tx_hwtstamp_mode; |
| 67 | }; |
| 68 | |
| 69 | struct xdp_tx_time { |
| 70 | const char *traffic_class; |
| 71 | uint64_t sequence_counter_begin; |
| 72 | uint64_t duration; |
| 73 | uint64_t tx_time_offset; |
| 74 | size_t num_frames_per_cycle; |
| 75 | }; |
| 76 | |
| 77 | struct xdp_gen_config { |
| 78 | enum security_mode mode; |
| 79 | struct security_context *security_context; |
| 80 | const unsigned char *iv_prefix; |
| 81 | const unsigned char *payload_pattern; |
| 82 | size_t payload_pattern_length; |
| 83 | size_t frame_length; |
| 84 | size_t num_frames_per_cycle; |
| 85 | uint32_t *frame_number; |
| 86 | uint64_t sequence_counter_begin; |
| 87 | uint32_t meta_data_offset; |
| 88 | enum stat_frame_type frame_type; |
| 89 | const struct xdp_tx_time *tx_time; |
| 90 | }; |
| 91 | |
| 92 | struct xdp_socket *xdp_open_socket(const char *interface, const char *xdp_program, int queue, |
| 93 | bool skb_mode, bool zero_copy_mode, bool wakeup_mode, |
| 94 | bool busy_poll_mode, bool tx_time_mode, bool tx_hwtstamp_mode); |
| 95 | void xdp_close_socket(struct xdp_socket *xsk, const char *interface, bool skb_mode); |
| 96 | void xdp_complete_tx_only(struct xdp_socket *xsk); |
| 97 | void xdp_complete_tx(struct xdp_socket *xsk); |
| 98 | void xdp_gen_and_send_frames(struct xdp_socket *xsk, const struct xdp_gen_config *xdp); |
| 99 | unsigned int xdp_receive_frames(struct xdp_socket *xsk, size_t frame_length, bool mirror_enabled, |
| 100 | int (*receive_function)(void *data, unsigned char *, size_t), |
| 101 | void *data, const struct xdp_tx_time *tx_time); |
| 102 | void xdp_get_timestamp_metadata(void *data, uint64_t *rx_hw_ts, uint64_t *rx_sw_ts); |
| 103 | |
| 104 | #endif /* _XDP_H_ */ |