Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* AF_XDP internal functions |
| 3 | * Copyright(c) 2018 Intel Corporation. |
| 4 | */ |
| 5 | |
| 6 | #ifndef _LINUX_XDP_SOCK_H |
| 7 | #define _LINUX_XDP_SOCK_H |
| 8 | |
| 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/if_xdp.h> |
| 11 | #include <linux/mutex.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <net/sock.h> |
| 15 | |
| 16 | struct net_device; |
| 17 | struct xsk_queue; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 18 | struct xdp_buff; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 19 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | struct xdp_umem { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 21 | void *addrs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 22 | u64 size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | u32 headroom; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 24 | u32 chunk_size; |
| 25 | u32 chunks; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 26 | u32 npgs; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 27 | struct user_struct *user; |
| 28 | refcount_t users; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 29 | u8 flags; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 30 | bool zc; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 31 | struct page **pgs; |
| 32 | int id; |
| 33 | struct list_head xsk_dma_list; |
| 34 | struct work_struct work; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 37 | struct xsk_map { |
| 38 | struct bpf_map map; |
| 39 | spinlock_t lock; /* Synchronize map updates */ |
| 40 | struct xdp_sock *xsk_map[]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 43 | struct xdp_sock { |
| 44 | /* struct sock must be the first member of struct xdp_sock */ |
| 45 | struct sock sk; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 46 | struct xsk_queue *rx ____cacheline_aligned_in_smp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 47 | struct net_device *dev; |
| 48 | struct xdp_umem *umem; |
| 49 | struct list_head flush_node; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 50 | struct xsk_buff_pool *pool; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 51 | u16 queue_id; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 52 | bool zc; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 53 | enum { |
| 54 | XSK_READY = 0, |
| 55 | XSK_BOUND, |
| 56 | XSK_UNBOUND, |
| 57 | } state; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 58 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 59 | struct xsk_queue *tx ____cacheline_aligned_in_smp; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 60 | struct list_head tx_list; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 61 | /* Protects generic receive. */ |
| 62 | spinlock_t rx_lock; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 63 | |
| 64 | /* Statistics */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 65 | u64 rx_dropped; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 66 | u64 rx_queue_full; |
| 67 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 68 | struct list_head map_list; |
| 69 | /* Protects map_list */ |
| 70 | spinlock_t map_list_lock; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 71 | /* Protects multiple processes in the control path */ |
| 72 | struct mutex mutex; |
| 73 | struct xsk_queue *fq_tmp; /* Only as tmp storage before bind */ |
| 74 | struct xsk_queue *cq_tmp; /* Only as tmp storage before bind */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 77 | #ifdef CONFIG_XDP_SOCKETS |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 78 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 79 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 80 | int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp); |
| 81 | void __xsk_map_flush(void); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 82 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 83 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 84 | u32 key) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 85 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 86 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 87 | struct xdp_sock *xs; |
| 88 | |
| 89 | if (key >= map->max_entries) |
| 90 | return NULL; |
| 91 | |
| 92 | xs = READ_ONCE(m->xsk_map[key]); |
| 93 | return xs; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 96 | #else |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 97 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 98 | static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 99 | { |
| 100 | return -ENOTSUPP; |
| 101 | } |
| 102 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 103 | static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 104 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 105 | return -EOPNOTSUPP; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 108 | static inline void __xsk_map_flush(void) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 109 | { |
| 110 | } |
| 111 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 112 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 113 | u32 key) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 114 | { |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 118 | #endif /* CONFIG_XDP_SOCKETS */ |
| 119 | |
| 120 | #endif /* _LINUX_XDP_SOCK_H */ |