Björn Töpel | dac09149 | 2018-05-18 14:00:21 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | /* AF_XDP internal functions |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 3 | * Copyright(c) 2018 Intel Corporation. |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #ifndef _LINUX_XDP_SOCK_H |
| 7 | #define _LINUX_XDP_SOCK_H |
| 8 | |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
| 10 | #include <linux/if_xdp.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 13 | #include <linux/mm.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 14 | #include <net/sock.h> |
| 15 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 16 | struct net_device; |
| 17 | struct xsk_queue; |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 18 | struct xdp_buff; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 19 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 20 | /* Masks for xdp_umem_page flags. |
| 21 | * The low 12-bits of the addr will be 0 since this is the page address, so we |
| 22 | * can use them for flags. |
| 23 | */ |
| 24 | #define XSK_NEXT_PG_CONTIG_SHIFT 0 |
| 25 | #define XSK_NEXT_PG_CONTIG_MASK (1ULL << XSK_NEXT_PG_CONTIG_SHIFT) |
| 26 | |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 27 | struct xdp_umem_page { |
| 28 | void *addr; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 29 | dma_addr_t dma; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 30 | }; |
| 31 | |
Jakub Kicinski | f5bd913 | 2018-09-07 10:18:46 +0200 | [diff] [blame] | 32 | struct xdp_umem_fq_reuse { |
| 33 | u32 nentries; |
| 34 | u32 length; |
| 35 | u64 handles[]; |
| 36 | }; |
| 37 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 38 | /* Flags for the umem flags field. |
| 39 | * |
| 40 | * The NEED_WAKEUP flag is 1 due to the reuse of the flags field for public |
| 41 | * flags. See inlude/uapi/include/linux/if_xdp.h. |
| 42 | */ |
| 43 | #define XDP_UMEM_USES_NEED_WAKEUP (1 << 1) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 44 | |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 45 | struct xdp_umem { |
| 46 | struct xsk_queue *fq; |
| 47 | struct xsk_queue *cq; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 48 | struct xdp_umem_page *pages; |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 49 | u64 chunk_mask; |
| 50 | u64 size; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 51 | u32 headroom; |
| 52 | u32 chunk_size_nohr; |
| 53 | struct user_struct *user; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 54 | refcount_t users; |
| 55 | struct work_struct work; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 56 | struct page **pgs; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 57 | u32 npgs; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 58 | u16 queue_id; |
| 59 | u8 need_wakeup; |
| 60 | u8 flags; |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 61 | int id; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 62 | struct net_device *dev; |
Jakub Kicinski | f5bd913 | 2018-09-07 10:18:46 +0200 | [diff] [blame] | 63 | struct xdp_umem_fq_reuse *fq_reuse; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 64 | bool zc; |
Magnus Karlsson | e4e5aef | 2020-05-04 15:33:51 +0200 | [diff] [blame] | 65 | spinlock_t xsk_tx_list_lock; |
| 66 | struct list_head xsk_tx_list; |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 67 | }; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 68 | |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 69 | struct xsk_map { |
| 70 | struct bpf_map map; |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 71 | spinlock_t lock; /* Synchronize map updates */ |
| 72 | struct xdp_sock *xsk_map[]; |
| 73 | }; |
| 74 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 75 | struct xdp_sock { |
| 76 | /* struct sock must be the first member of struct xdp_sock */ |
| 77 | struct sock sk; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 78 | struct xsk_queue *rx; |
| 79 | struct net_device *dev; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 80 | struct xdp_umem *umem; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 81 | struct list_head flush_node; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 82 | u16 queue_id; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 83 | bool zc; |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 84 | enum { |
| 85 | XSK_READY = 0, |
| 86 | XSK_BOUND, |
| 87 | XSK_UNBOUND, |
| 88 | } state; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 89 | /* Protects multiple processes in the control path */ |
| 90 | struct mutex mutex; |
Jonathan Lemon | fada7fd | 2019-06-06 13:59:40 -0700 | [diff] [blame] | 91 | struct xsk_queue *tx ____cacheline_aligned_in_smp; |
| 92 | struct list_head list; |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 93 | /* Mutual exclusion of NAPI TX thread and sendmsg error paths |
| 94 | * in the SKB destructor callback. |
| 95 | */ |
| 96 | spinlock_t tx_completion_lock; |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 97 | /* Protects generic receive. */ |
| 98 | spinlock_t rx_lock; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 99 | u64 rx_dropped; |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 100 | struct list_head map_list; |
| 101 | /* Protects map_list */ |
| 102 | spinlock_t map_list_lock; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 103 | }; |
| 104 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 105 | #ifdef CONFIG_XDP_SOCKETS |
Björn Töpel | 9025403 | 2018-08-28 14:44:27 +0200 | [diff] [blame] | 106 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 107 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp); |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 108 | int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp); |
| 109 | void __xsk_map_flush(void); |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 110 | |
| 111 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 112 | u32 key) |
| 113 | { |
| 114 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 115 | struct xdp_sock *xs; |
| 116 | |
| 117 | if (key >= map->max_entries) |
| 118 | return NULL; |
| 119 | |
| 120 | xs = READ_ONCE(m->xsk_map[key]); |
| 121 | return xs; |
| 122 | } |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 123 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 124 | static inline u64 xsk_umem_extract_addr(u64 addr) |
| 125 | { |
| 126 | return addr & XSK_UNALIGNED_BUF_ADDR_MASK; |
| 127 | } |
| 128 | |
| 129 | static inline u64 xsk_umem_extract_offset(u64 addr) |
| 130 | { |
| 131 | return addr >> XSK_UNALIGNED_BUF_OFFSET_SHIFT; |
| 132 | } |
| 133 | |
| 134 | static inline u64 xsk_umem_add_offset_to_addr(u64 addr) |
| 135 | { |
| 136 | return xsk_umem_extract_addr(addr) + xsk_umem_extract_offset(addr); |
| 137 | } |
| 138 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 139 | #else |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 140 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 141 | static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 142 | { |
| 143 | return -ENOTSUPP; |
| 144 | } |
| 145 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 146 | static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp) |
Maxim Mikityanskiy | d57d764 | 2019-06-26 17:35:24 +0300 | [diff] [blame] | 147 | { |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 148 | return -EOPNOTSUPP; |
Maxim Mikityanskiy | d57d764 | 2019-06-26 17:35:24 +0300 | [diff] [blame] | 149 | } |
| 150 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 151 | static inline void __xsk_map_flush(void) |
Björn Töpel | 9025403 | 2018-08-28 14:44:27 +0200 | [diff] [blame] | 152 | { |
| 153 | } |
| 154 | |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame^] | 155 | static inline struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, |
| 156 | u32 key) |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 157 | { |
| 158 | return NULL; |
| 159 | } |
| 160 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 161 | static inline u64 xsk_umem_extract_addr(u64 addr) |
| 162 | { |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static inline u64 xsk_umem_extract_offset(u64 addr) |
| 167 | { |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | static inline u64 xsk_umem_add_offset_to_addr(u64 addr) |
| 172 | { |
| 173 | return 0; |
| 174 | } |
| 175 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 176 | #endif /* CONFIG_XDP_SOCKETS */ |
| 177 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 178 | #endif /* _LINUX_XDP_SOCK_H */ |