Home | History | Annotate | Line # | Download | only in dist
      1 /*
      2  * xdp-server.h -- integration of AF_XDP into nsd
      3  *
      4  * Copyright (c) 2024, NLnet Labs. All rights reserved.
      5  *
      6  * See LICENSE for the license.
      7  *
      8  */
      9 
     10 #ifndef XDP_SERVER_H
     11 #define XDP_SERVER_H
     12 
     13 #include <stdint.h>
     14 #include <xdp/xsk.h>
     15 
     16 #include "region-allocator.h"
     17 
     18 /* TODO: check if number is sensible */
     19 #define XDP_NUM_FRAMES 8192*2
     20 #define XDP_FRAME_SIZE XSK_UMEM__DEFAULT_FRAME_SIZE // 4096 bytes as of Oct 2024
     21 #define XDP_BUFFER_SIZE XDP_NUM_FRAMES * XDP_FRAME_SIZE
     22 #define XDP_RX_BATCH_SIZE 64
     23 #define XDP_INVALID_UMEM_FRAME UINT64_MAX
     24 /* ring sizes need tweeking */
     25 #define XSK_RING_PROD__NUM_DESCS 8192
     26 #define XSK_RING_CONS__NUM_DESCS 8192
     27 #define XSK_UMEM_FRAME_HEADROOM XSK_UMEM__DEFAULT_FRAME_HEADROOM
     28 #define XSK_UMEM_FLAGS XSK_UMEM__DEFAULT_FLAGS
     29 
     30 struct nsd; /* avoid recursive header include */
     31 
     32 struct xsk_umem_info {
     33 	struct xsk_ring_prod fq;
     34 	struct xsk_ring_cons cq;
     35 	struct xsk_umem *umem;
     36 	void *buffer;
     37 
     38 	uint64_t umem_frame_addr[XDP_NUM_FRAMES];
     39 	uint32_t umem_frame_free;
     40 };
     41 
     42 struct xsk_socket_info {
     43 	struct xsk_ring_cons rx;
     44 	struct xsk_ring_prod tx;
     45 	struct xsk_umem_info *umem;
     46 	struct xsk_socket *xsk;
     47 
     48 	uint32_t outstanding_tx;
     49 };
     50 
     51 struct xdp_ip_address {
     52 	struct xdp_ip_address *next;
     53 	struct sockaddr_storage addr;
     54 };
     55 
     56 struct xdp_server {
     57 	/* NSD global settings */
     58 	region_type *region;
     59 	char const *interface_name;
     60 	char const *bpf_prog_filename;
     61 	char const *bpf_bpffs_path;
     62 	int bpf_prog_should_load;
     63 	int force_copy;
     64 
     65 	/* track bpf objects and file descriptors */
     66 	int xsk_map_fd;
     67 	int bpf_prog_fd;
     68 	uint32_t bpf_prog_id;
     69 	struct bpf_map *xsk_map;
     70 	struct xdp_program *bpf_prog;
     71 
     72 	uint32_t interface_index;
     73 	uint32_t queue_count;
     74 	uint32_t queue_index;
     75 
     76 	struct xdp_ip_address *ip_addresses;
     77 
     78 	struct query **queries;
     79 	struct nsd *nsd;
     80 
     81 	/* these items/arrays are shared between processes */
     82 	/* the number of sockets corresponds to the queue_count */
     83 	/* these are allocated using mmap and are automatically unmapped on exit */
     84 	struct xsk_umem_info *umems;
     85 	struct xsk_socket_info *xsks;
     86 };
     87 
     88 /*
     89  * Handle reading and writing packets via XDP
     90  */
     91 void xdp_handle_recv_and_send(struct xdp_server *xdp);
     92 
     93 /*
     94  * Initialize NSD global XDP settings
     95  *
     96  *	- load XDP program if configured
     97  *	- set limits
     98  */
     99 int xdp_server_init(struct xdp_server *xdp);
    100 
    101 /*
    102  * Cleanup NSD global XDP settings
    103  *
    104  *	- unload XDP program if loaded by NSD
    105  *	- unpin BPF map if pinned and loaded by NSD
    106  */
    107 void xdp_server_cleanup(struct xdp_server *xdp);
    108 
    109 #endif /* XDP_SERVER_H */
    110