Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* XDP sockets |
| 3 | * |
| 4 | * AF_XDP sockets allows a channel between XDP programs and userspace |
| 5 | * applications. |
| 6 | * Copyright(c) 2018 Intel Corporation. |
| 7 | * |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 8 | * Author(s): Björn Töpel <bjorn.topel@intel.com> |
| 9 | * Magnus Karlsson <magnus.karlsson@intel.com> |
| 10 | */ |
| 11 | |
| 12 | #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__ |
| 13 | |
| 14 | #include <linux/if_xdp.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/sched/mm.h> |
| 17 | #include <linux/sched/signal.h> |
| 18 | #include <linux/sched/task.h> |
| 19 | #include <linux/socket.h> |
| 20 | #include <linux/file.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/net.h> |
| 23 | #include <linux/netdevice.h> |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 24 | #include <linux/rculist.h> |
Magnus Karlsson | a71506a | 2020-05-20 21:20:51 +0200 | [diff] [blame] | 25 | #include <net/xdp_sock_drv.h> |
Björn Töpel | a073195 | 2020-11-30 19:52:00 +0100 | [diff] [blame] | 26 | #include <net/busy_poll.h> |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 27 | #include <net/xdp.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 28 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 29 | #include "xsk_queue.h" |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 30 | #include "xdp_umem.h" |
Björn Töpel | a36b38aa | 2019-01-24 19:59:39 +0100 | [diff] [blame] | 31 | #include "xsk.h" |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 32 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 33 | #define TX_BATCH_SIZE 16 |
| 34 | |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 35 | static DEFINE_PER_CPU(struct list_head, xskmap_flush_list); |
| 36 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 37 | void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 38 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 39 | if (pool->cached_need_wakeup & XDP_WAKEUP_RX) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 40 | return; |
| 41 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 42 | pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP; |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 43 | pool->cached_need_wakeup |= XDP_WAKEUP_RX; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 44 | } |
| 45 | EXPORT_SYMBOL(xsk_set_rx_need_wakeup); |
| 46 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 47 | void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 48 | { |
| 49 | struct xdp_sock *xs; |
| 50 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 51 | if (pool->cached_need_wakeup & XDP_WAKEUP_TX) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 52 | return; |
| 53 | |
| 54 | rcu_read_lock(); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 55 | list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 56 | xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP; |
| 57 | } |
| 58 | rcu_read_unlock(); |
| 59 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 60 | pool->cached_need_wakeup |= XDP_WAKEUP_TX; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 61 | } |
| 62 | EXPORT_SYMBOL(xsk_set_tx_need_wakeup); |
| 63 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 64 | void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 65 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 66 | if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX)) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 67 | return; |
| 68 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 69 | pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP; |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 70 | pool->cached_need_wakeup &= ~XDP_WAKEUP_RX; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 71 | } |
| 72 | EXPORT_SYMBOL(xsk_clear_rx_need_wakeup); |
| 73 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 74 | void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 75 | { |
| 76 | struct xdp_sock *xs; |
| 77 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 78 | if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX)) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 79 | return; |
| 80 | |
| 81 | rcu_read_lock(); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 82 | list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 83 | xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP; |
| 84 | } |
| 85 | rcu_read_unlock(); |
| 86 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 87 | pool->cached_need_wakeup &= ~XDP_WAKEUP_TX; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 88 | } |
| 89 | EXPORT_SYMBOL(xsk_clear_tx_need_wakeup); |
| 90 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 91 | bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool) |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 92 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 93 | return pool->uses_need_wakeup; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 94 | } |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 95 | EXPORT_SYMBOL(xsk_uses_need_wakeup); |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 96 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 97 | struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev, |
| 98 | u16 queue_id) |
| 99 | { |
| 100 | if (queue_id < dev->real_num_rx_queues) |
| 101 | return dev->_rx[queue_id].pool; |
| 102 | if (queue_id < dev->real_num_tx_queues) |
| 103 | return dev->_tx[queue_id].pool; |
| 104 | |
| 105 | return NULL; |
| 106 | } |
| 107 | EXPORT_SYMBOL(xsk_get_pool_from_qid); |
| 108 | |
| 109 | void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id) |
| 110 | { |
Maxim Mikityanskiy | b425e24 | 2021-01-18 18:03:33 +0200 | [diff] [blame] | 111 | if (queue_id < dev->num_rx_queues) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 112 | dev->_rx[queue_id].pool = NULL; |
Maxim Mikityanskiy | b425e24 | 2021-01-18 18:03:33 +0200 | [diff] [blame] | 113 | if (queue_id < dev->num_tx_queues) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 114 | dev->_tx[queue_id].pool = NULL; |
| 115 | } |
| 116 | |
| 117 | /* The buffer pool is stored both in the _rx struct and the _tx struct as we do |
| 118 | * not know if the device has more tx queues than rx, or the opposite. |
| 119 | * This might also change during run time. |
| 120 | */ |
| 121 | int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool, |
| 122 | u16 queue_id) |
| 123 | { |
| 124 | if (queue_id >= max_t(unsigned int, |
| 125 | dev->real_num_rx_queues, |
| 126 | dev->real_num_tx_queues)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | if (queue_id < dev->real_num_rx_queues) |
| 130 | dev->_rx[queue_id].pool = pool; |
| 131 | if (queue_id < dev->real_num_tx_queues) |
| 132 | dev->_tx[queue_id].pool = pool; |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 137 | void xp_release(struct xdp_buff_xsk *xskb) |
| 138 | { |
| 139 | xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb; |
| 140 | } |
| 141 | |
| 142 | static u64 xp_get_handle(struct xdp_buff_xsk *xskb) |
| 143 | { |
| 144 | u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start; |
| 145 | |
| 146 | offset += xskb->pool->headroom; |
| 147 | if (!xskb->pool->unaligned) |
| 148 | return xskb->orig_addr + offset; |
| 149 | return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT); |
| 150 | } |
| 151 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 152 | static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 153 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 154 | struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp); |
| 155 | u64 addr; |
| 156 | int err; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 157 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 158 | addr = xp_get_handle(xskb); |
| 159 | err = xskq_prod_reserve_desc(xs->rx, addr, len); |
| 160 | if (err) { |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 161 | xs->rx_queue_full++; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 162 | return err; |
| 163 | } |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 164 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 165 | xp_release(xskb); |
| 166 | return 0; |
| 167 | } |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 168 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 169 | static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len) |
| 170 | { |
| 171 | void *from_buf, *to_buf; |
| 172 | u32 metalen; |
| 173 | |
| 174 | if (unlikely(xdp_data_meta_unsupported(from))) { |
| 175 | from_buf = from->data; |
| 176 | to_buf = to->data; |
| 177 | metalen = 0; |
| 178 | } else { |
| 179 | from_buf = from->data_meta; |
| 180 | metalen = from->data - from->data_meta; |
| 181 | to_buf = to->data - metalen; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | memcpy(to_buf, from_buf, len + metalen); |
| 185 | } |
| 186 | |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 187 | static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 188 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 189 | struct xdp_buff *xsk_xdp; |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame] | 190 | int err; |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 191 | u32 len; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 192 | |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 193 | len = xdp->data_end - xdp->data; |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 194 | if (len > xsk_pool_get_rx_frame_size(xs->pool)) { |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 195 | xs->rx_dropped++; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 196 | return -ENOSPC; |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 197 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 198 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 199 | xsk_xdp = xsk_buff_alloc(xs->pool); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 200 | if (!xsk_xdp) { |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 201 | xs->rx_dropped++; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 202 | return -ENOSPC; |
| 203 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 204 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 205 | xsk_copy_xdp(xsk_xdp, xdp, len); |
| 206 | err = __xsk_rcv_zc(xs, xsk_xdp, len); |
| 207 | if (err) { |
| 208 | xsk_buff_free(xsk_xdp); |
| 209 | return err; |
| 210 | } |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 211 | return 0; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 212 | } |
| 213 | |
Xuan Zhuo | 3413f04 | 2020-12-01 21:56:58 +0800 | [diff] [blame] | 214 | static bool xsk_tx_writeable(struct xdp_sock *xs) |
| 215 | { |
| 216 | if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2) |
| 217 | return false; |
| 218 | |
| 219 | return true; |
| 220 | } |
| 221 | |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 222 | static bool xsk_is_bound(struct xdp_sock *xs) |
| 223 | { |
| 224 | if (READ_ONCE(xs->state) == XSK_BOUND) { |
| 225 | /* Matches smp_wmb() in bind(). */ |
| 226 | smp_rmb(); |
| 227 | return true; |
| 228 | } |
| 229 | return false; |
| 230 | } |
| 231 | |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 232 | static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 233 | { |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 234 | if (!xsk_is_bound(xs)) |
| 235 | return -EINVAL; |
| 236 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 237 | if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) |
| 238 | return -EINVAL; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 239 | |
Björn Töpel | b02e5a0 | 2020-11-30 19:52:01 +0100 | [diff] [blame] | 240 | sk_mark_napi_id_once_xdp(&xs->sk, xdp); |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 241 | return 0; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 244 | static void xsk_flush(struct xdp_sock *xs) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 245 | { |
Magnus Karlsson | 59e35e5 | 2019-12-19 13:39:23 +0100 | [diff] [blame] | 246 | xskq_prod_submit(xs->rx); |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 247 | __xskq_cons_release(xs->pool->fq); |
Björn Töpel | 43a825a | 2020-01-20 10:29:17 +0100 | [diff] [blame] | 248 | sock_def_readable(&xs->sk); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 252 | { |
| 253 | int err; |
| 254 | |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 255 | spin_lock_bh(&xs->rx_lock); |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 256 | err = xsk_rcv_check(xs, xdp); |
| 257 | if (!err) { |
| 258 | err = __xsk_rcv(xs, xdp); |
| 259 | xsk_flush(xs); |
| 260 | } |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 261 | spin_unlock_bh(&xs->rx_lock); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 262 | return err; |
| 263 | } |
| 264 | |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 265 | static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 266 | { |
| 267 | int err; |
| 268 | u32 len; |
| 269 | |
| 270 | err = xsk_rcv_check(xs, xdp); |
| 271 | if (err) |
| 272 | return err; |
| 273 | |
| 274 | if (xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL) { |
| 275 | len = xdp->data_end - xdp->data; |
| 276 | return __xsk_rcv_zc(xs, xdp, len); |
| 277 | } |
| 278 | |
| 279 | err = __xsk_rcv(xs, xdp); |
| 280 | if (!err) |
| 281 | xdp_return_buff(xdp); |
| 282 | return err; |
| 283 | } |
| 284 | |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 285 | int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp) |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 286 | { |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 287 | struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list); |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 288 | int err; |
| 289 | |
Björn Töpel | 458f727 | 2021-01-22 11:53:49 +0100 | [diff] [blame] | 290 | err = xsk_rcv(xs, xdp); |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 291 | if (err) |
| 292 | return err; |
| 293 | |
| 294 | if (!xs->flush_node.prev) |
| 295 | list_add(&xs->flush_node, flush_list); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 300 | void __xsk_map_flush(void) |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 301 | { |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 302 | struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list); |
Björn Töpel | d817991 | 2019-11-01 12:03:46 +0100 | [diff] [blame] | 303 | struct xdp_sock *xs, *tmp; |
| 304 | |
| 305 | list_for_each_entry_safe(xs, tmp, flush_list, flush_node) { |
| 306 | xsk_flush(xs); |
| 307 | __list_del_clearprev(&xs->flush_node); |
| 308 | } |
| 309 | } |
| 310 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 311 | void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 312 | { |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 313 | xskq_prod_submit_n(pool->cq, nb_entries); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 314 | } |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 315 | EXPORT_SYMBOL(xsk_tx_completed); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 316 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 317 | void xsk_tx_release(struct xsk_buff_pool *pool) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 318 | { |
| 319 | struct xdp_sock *xs; |
| 320 | |
| 321 | rcu_read_lock(); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 322 | list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { |
Magnus Karlsson | 30744a6 | 2020-02-10 16:27:12 +0100 | [diff] [blame] | 323 | __xskq_cons_release(xs->tx); |
Xuan Zhuo | 3413f04 | 2020-12-01 21:56:58 +0800 | [diff] [blame] | 324 | if (xsk_tx_writeable(xs)) |
| 325 | xs->sk.sk_write_space(&xs->sk); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 326 | } |
| 327 | rcu_read_unlock(); |
| 328 | } |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 329 | EXPORT_SYMBOL(xsk_tx_release); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 330 | |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 331 | bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 332 | { |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 333 | struct xdp_sock *xs; |
| 334 | |
| 335 | rcu_read_lock(); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 336 | list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 337 | if (!xskq_cons_peek_desc(xs->tx, desc, pool)) { |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 338 | xs->tx->queue_empty_descs++; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 339 | continue; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 340 | } |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 341 | |
Tobias Klauser | 0a05861 | 2020-04-22 01:29:27 +0200 | [diff] [blame] | 342 | /* This is the backpressure mechanism for the Tx path. |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 343 | * Reserve space in the completion queue and only proceed |
| 344 | * if there is space in it. This avoids having to implement |
| 345 | * any buffering in the Tx path. |
| 346 | */ |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 347 | if (xskq_prod_reserve_addr(pool->cq, desc->addr)) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 348 | goto out; |
| 349 | |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 350 | xskq_cons_release(xs->tx); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 351 | rcu_read_unlock(); |
| 352 | return true; |
| 353 | } |
| 354 | |
| 355 | out: |
| 356 | rcu_read_unlock(); |
| 357 | return false; |
| 358 | } |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 359 | EXPORT_SYMBOL(xsk_tx_peek_desc); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 360 | |
Magnus Karlsson | 9349eb3 | 2020-11-16 12:12:46 +0100 | [diff] [blame] | 361 | static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, struct xdp_desc *descs, |
| 362 | u32 max_entries) |
| 363 | { |
| 364 | u32 nb_pkts = 0; |
| 365 | |
| 366 | while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts])) |
| 367 | nb_pkts++; |
| 368 | |
| 369 | xsk_tx_release(pool); |
| 370 | return nb_pkts; |
| 371 | } |
| 372 | |
| 373 | u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, struct xdp_desc *descs, |
| 374 | u32 max_entries) |
| 375 | { |
| 376 | struct xdp_sock *xs; |
| 377 | u32 nb_pkts; |
| 378 | |
| 379 | rcu_read_lock(); |
| 380 | if (!list_is_singular(&pool->xsk_tx_list)) { |
| 381 | /* Fallback to the non-batched version */ |
| 382 | rcu_read_unlock(); |
| 383 | return xsk_tx_peek_release_fallback(pool, descs, max_entries); |
| 384 | } |
| 385 | |
| 386 | xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list); |
| 387 | if (!xs) { |
| 388 | nb_pkts = 0; |
| 389 | goto out; |
| 390 | } |
| 391 | |
| 392 | nb_pkts = xskq_cons_peek_desc_batch(xs->tx, descs, pool, max_entries); |
| 393 | if (!nb_pkts) { |
| 394 | xs->tx->queue_empty_descs++; |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | /* This is the backpressure mechanism for the Tx path. Try to |
| 399 | * reserve space in the completion queue for all packets, but |
| 400 | * if there are fewer slots available, just process that many |
| 401 | * packets. This avoids having to implement any buffering in |
| 402 | * the Tx path. |
| 403 | */ |
| 404 | nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, descs, nb_pkts); |
| 405 | if (!nb_pkts) |
| 406 | goto out; |
| 407 | |
| 408 | xskq_cons_release_n(xs->tx, nb_pkts); |
| 409 | __xskq_cons_release(xs->tx); |
| 410 | xs->sk.sk_write_space(&xs->sk); |
| 411 | |
| 412 | out: |
| 413 | rcu_read_unlock(); |
| 414 | return nb_pkts; |
| 415 | } |
| 416 | EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch); |
| 417 | |
Maxim Mikityanskiy | 0687068 | 2019-12-17 16:20:42 +0000 | [diff] [blame] | 418 | static int xsk_wakeup(struct xdp_sock *xs, u8 flags) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 419 | { |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 420 | struct net_device *dev = xs->dev; |
Maxim Mikityanskiy | 0687068 | 2019-12-17 16:20:42 +0000 | [diff] [blame] | 421 | int err; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 422 | |
Maxim Mikityanskiy | 0687068 | 2019-12-17 16:20:42 +0000 | [diff] [blame] | 423 | rcu_read_lock(); |
| 424 | err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags); |
| 425 | rcu_read_unlock(); |
| 426 | |
| 427 | return err; |
| 428 | } |
| 429 | |
| 430 | static int xsk_zc_xmit(struct xdp_sock *xs) |
| 431 | { |
| 432 | return xsk_wakeup(xs, XDP_WAKEUP_TX); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 433 | } |
| 434 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 435 | static void xsk_destruct_skb(struct sk_buff *skb) |
| 436 | { |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 437 | u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 438 | struct xdp_sock *xs = xdp_sk(skb->sk); |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 439 | unsigned long flags; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 440 | |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 441 | spin_lock_irqsave(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 442 | xskq_prod_submit_addr(xs->pool->cq, addr); |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 443 | spin_unlock_irqrestore(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 444 | |
| 445 | sock_wfree(skb); |
| 446 | } |
| 447 | |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 448 | static int xsk_generic_xmit(struct sock *sk) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 449 | { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 450 | struct xdp_sock *xs = xdp_sk(sk); |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 451 | u32 max_batch = TX_BATCH_SIZE; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 452 | bool sent_frame = false; |
| 453 | struct xdp_desc desc; |
| 454 | struct sk_buff *skb; |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 455 | unsigned long flags; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 456 | int err = 0; |
| 457 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 458 | mutex_lock(&xs->mutex); |
| 459 | |
Ilya Maximets | 6757164 | 2019-07-04 17:25:03 +0300 | [diff] [blame] | 460 | if (xs->queue_id >= xs->dev->real_num_tx_queues) |
| 461 | goto out; |
| 462 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 463 | while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 464 | char *buffer; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 465 | u64 addr; |
| 466 | u32 len; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 467 | |
| 468 | if (max_batch-- == 0) { |
| 469 | err = -EAGAIN; |
| 470 | goto out; |
| 471 | } |
| 472 | |
Magnus Karlsson | 09210c4 | 2018-07-11 10:12:52 +0200 | [diff] [blame] | 473 | len = desc.len; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 474 | skb = sock_alloc_send_skb(sk, len, 1, &err); |
Li RongQing | aa2cad0 | 2020-06-11 13:11:06 +0800 | [diff] [blame] | 475 | if (unlikely(!skb)) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 476 | goto out; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 477 | |
| 478 | skb_put(skb, len); |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 479 | addr = desc.addr; |
Magnus Karlsson | c465576 | 2020-08-28 10:26:16 +0200 | [diff] [blame] | 480 | buffer = xsk_buff_raw_get_data(xs->pool, addr); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 481 | err = skb_store_bits(skb, 0, buffer, len); |
Tobias Klauser | 0a05861 | 2020-04-22 01:29:27 +0200 | [diff] [blame] | 482 | /* This is the backpressure mechanism for the Tx path. |
Magnus Karlsson | 15d8c91 | 2019-12-19 13:39:30 +0100 | [diff] [blame] | 483 | * Reserve space in the completion queue and only proceed |
| 484 | * if there is space in it. This avoids having to implement |
| 485 | * any buffering in the Tx path. |
| 486 | */ |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 487 | spin_lock_irqsave(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 488 | if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) { |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 489 | spin_unlock_irqrestore(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 490 | kfree_skb(skb); |
| 491 | goto out; |
| 492 | } |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 493 | spin_unlock_irqrestore(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 494 | |
| 495 | skb->dev = xs->dev; |
| 496 | skb->priority = sk->sk_priority; |
| 497 | skb->mark = sk->sk_mark; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 498 | skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 499 | skb->destructor = xsk_destruct_skb; |
| 500 | |
Björn Töpel | 36ccdf8 | 2020-11-23 18:56:00 +0100 | [diff] [blame] | 501 | err = __dev_direct_xmit(skb, xs->queue_id); |
Magnus Karlsson | 642e450 | 2020-09-16 14:00:25 +0200 | [diff] [blame] | 502 | if (err == NETDEV_TX_BUSY) { |
| 503 | /* Tell user-space to retry the send */ |
| 504 | skb->destructor = sock_wfree; |
Magnus Karlsson | b1b95cb | 2020-12-18 14:45:25 +0100 | [diff] [blame] | 505 | spin_lock_irqsave(&xs->pool->cq_lock, flags); |
| 506 | xskq_prod_cancel(xs->pool->cq); |
| 507 | spin_unlock_irqrestore(&xs->pool->cq_lock, flags); |
Magnus Karlsson | 642e450 | 2020-09-16 14:00:25 +0200 | [diff] [blame] | 508 | /* Free skb without triggering the perf drop trace */ |
| 509 | consume_skb(skb); |
| 510 | err = -EAGAIN; |
| 511 | goto out; |
| 512 | } |
| 513 | |
Magnus Karlsson | c5ed924b | 2019-12-19 13:39:26 +0100 | [diff] [blame] | 514 | xskq_cons_release(xs->tx); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 515 | /* Ignore NET_XMIT_CN as packet might have been sent */ |
Magnus Karlsson | 642e450 | 2020-09-16 14:00:25 +0200 | [diff] [blame] | 516 | if (err == NET_XMIT_DROP) { |
Magnus Karlsson | fe58868 | 2018-06-29 09:48:18 +0200 | [diff] [blame] | 517 | /* SKB completed but not sent */ |
| 518 | err = -EBUSY; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 519 | goto out; |
| 520 | } |
| 521 | |
| 522 | sent_frame = true; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 523 | } |
| 524 | |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 525 | xs->tx->queue_empty_descs++; |
| 526 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 527 | out: |
| 528 | if (sent_frame) |
Xuan Zhuo | 3413f04 | 2020-12-01 21:56:58 +0800 | [diff] [blame] | 529 | if (xsk_tx_writeable(xs)) |
| 530 | sk->sk_write_space(sk); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 531 | |
| 532 | mutex_unlock(&xs->mutex); |
| 533 | return err; |
| 534 | } |
| 535 | |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 536 | static int __xsk_sendmsg(struct sock *sk) |
| 537 | { |
| 538 | struct xdp_sock *xs = xdp_sk(sk); |
| 539 | |
| 540 | if (unlikely(!(xs->dev->flags & IFF_UP))) |
| 541 | return -ENETDOWN; |
| 542 | if (unlikely(!xs->tx)) |
| 543 | return -ENOBUFS; |
| 544 | |
| 545 | return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk); |
| 546 | } |
| 547 | |
Björn Töpel | a073195 | 2020-11-30 19:52:00 +0100 | [diff] [blame] | 548 | static bool xsk_no_wakeup(struct sock *sk) |
| 549 | { |
| 550 | #ifdef CONFIG_NET_RX_BUSY_POLL |
| 551 | /* Prefer busy-polling, skip the wakeup. */ |
| 552 | return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) && |
| 553 | READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID; |
| 554 | #else |
| 555 | return false; |
| 556 | #endif |
| 557 | } |
| 558 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 559 | static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) |
| 560 | { |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 561 | bool need_wait = !(m->msg_flags & MSG_DONTWAIT); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 562 | struct sock *sk = sock->sk; |
| 563 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 564 | struct xsk_buff_pool *pool; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 565 | |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 566 | if (unlikely(!xsk_is_bound(xs))) |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 567 | return -ENXIO; |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 568 | if (unlikely(need_wait)) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 569 | return -EOPNOTSUPP; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 570 | |
Björn Töpel | a073195 | 2020-11-30 19:52:00 +0100 | [diff] [blame] | 571 | if (sk_can_busy_loop(sk)) |
| 572 | sk_busy_loop(sk, 1); /* only support non-blocking sockets */ |
| 573 | |
| 574 | if (xsk_no_wakeup(sk)) |
| 575 | return 0; |
| 576 | |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 577 | pool = xs->pool; |
| 578 | if (pool->cached_need_wakeup & XDP_WAKEUP_TX) |
| 579 | return __xsk_sendmsg(sk); |
| 580 | return 0; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 581 | } |
| 582 | |
Björn Töpel | 45a8668 | 2020-11-30 19:51:58 +0100 | [diff] [blame] | 583 | static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags) |
| 584 | { |
| 585 | bool need_wait = !(flags & MSG_DONTWAIT); |
| 586 | struct sock *sk = sock->sk; |
| 587 | struct xdp_sock *xs = xdp_sk(sk); |
| 588 | |
Björn Töpel | 3546b9b | 2020-12-07 09:20:08 +0100 | [diff] [blame] | 589 | if (unlikely(!xsk_is_bound(xs))) |
| 590 | return -ENXIO; |
Björn Töpel | 45a8668 | 2020-11-30 19:51:58 +0100 | [diff] [blame] | 591 | if (unlikely(!(xs->dev->flags & IFF_UP))) |
| 592 | return -ENETDOWN; |
| 593 | if (unlikely(!xs->rx)) |
| 594 | return -ENOBUFS; |
Björn Töpel | 45a8668 | 2020-11-30 19:51:58 +0100 | [diff] [blame] | 595 | if (unlikely(need_wait)) |
| 596 | return -EOPNOTSUPP; |
| 597 | |
Björn Töpel | a073195 | 2020-11-30 19:52:00 +0100 | [diff] [blame] | 598 | if (sk_can_busy_loop(sk)) |
| 599 | sk_busy_loop(sk, 1); /* only support non-blocking sockets */ |
| 600 | |
| 601 | if (xsk_no_wakeup(sk)) |
| 602 | return 0; |
| 603 | |
Björn Töpel | 45a8668 | 2020-11-30 19:51:58 +0100 | [diff] [blame] | 604 | if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc) |
| 605 | return xsk_wakeup(xs, XDP_WAKEUP_RX); |
| 606 | return 0; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 607 | } |
| 608 | |
Luc Van Oostenryck | 5d946c5 | 2019-11-20 01:10:42 +0100 | [diff] [blame] | 609 | static __poll_t xsk_poll(struct file *file, struct socket *sock, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 610 | struct poll_table_struct *wait) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 611 | { |
Xuan Zhuo | f5da541 | 2020-12-01 21:56:57 +0800 | [diff] [blame] | 612 | __poll_t mask = 0; |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 613 | struct sock *sk = sock->sk; |
| 614 | struct xdp_sock *xs = xdp_sk(sk); |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 615 | struct xsk_buff_pool *pool; |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 616 | |
Xuan Zhuo | f5da541 | 2020-12-01 21:56:57 +0800 | [diff] [blame] | 617 | sock_poll_wait(file, sock, wait); |
| 618 | |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 619 | if (unlikely(!xsk_is_bound(xs))) |
| 620 | return mask; |
| 621 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 622 | pool = xs->pool; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 623 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 624 | if (pool->cached_need_wakeup) { |
Maxim Mikityanskiy | 0687068 | 2019-12-17 16:20:42 +0000 | [diff] [blame] | 625 | if (xs->zc) |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 626 | xsk_wakeup(xs, pool->cached_need_wakeup); |
Magnus Karlsson | df55105 | 2019-10-02 08:31:59 +0200 | [diff] [blame] | 627 | else |
| 628 | /* Poll needs to drive Tx also in copy mode */ |
| 629 | __xsk_sendmsg(sk); |
| 630 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 631 | |
Magnus Karlsson | 59e35e5 | 2019-12-19 13:39:23 +0100 | [diff] [blame] | 632 | if (xs->rx && !xskq_prod_is_empty(xs->rx)) |
Luc Van Oostenryck | 5d946c5 | 2019-11-20 01:10:42 +0100 | [diff] [blame] | 633 | mask |= EPOLLIN | EPOLLRDNORM; |
Xuan Zhuo | 3413f04 | 2020-12-01 21:56:58 +0800 | [diff] [blame] | 634 | if (xs->tx && xsk_tx_writeable(xs)) |
Luc Van Oostenryck | 5d946c5 | 2019-11-20 01:10:42 +0100 | [diff] [blame] | 635 | mask |= EPOLLOUT | EPOLLWRNORM; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 636 | |
| 637 | return mask; |
| 638 | } |
| 639 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 640 | static int xsk_init_queue(u32 entries, struct xsk_queue **queue, |
| 641 | bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 642 | { |
| 643 | struct xsk_queue *q; |
| 644 | |
| 645 | if (entries == 0 || *queue || !is_power_of_2(entries)) |
| 646 | return -EINVAL; |
| 647 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 648 | q = xskq_create(entries, umem_queue); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 649 | if (!q) |
| 650 | return -ENOMEM; |
| 651 | |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 652 | /* Make sure queue is ready before it can be seen by others */ |
| 653 | smp_wmb(); |
Björn Töpel | 94a9976 | 2019-09-04 13:49:10 +0200 | [diff] [blame] | 654 | WRITE_ONCE(*queue, q); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 658 | static void xsk_unbind_dev(struct xdp_sock *xs) |
| 659 | { |
| 660 | struct net_device *dev = xs->dev; |
| 661 | |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 662 | if (xs->state != XSK_BOUND) |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 663 | return; |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 664 | WRITE_ONCE(xs->state, XSK_UNBOUND); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 665 | |
| 666 | /* Wait for driver to stop using the xdp socket. */ |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 667 | xp_del_xsk(xs->pool, xs); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 668 | xs->dev = NULL; |
| 669 | synchronize_net(); |
| 670 | dev_put(dev); |
| 671 | } |
| 672 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 673 | static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs, |
| 674 | struct xdp_sock ***map_entry) |
| 675 | { |
| 676 | struct xsk_map *map = NULL; |
| 677 | struct xsk_map_node *node; |
| 678 | |
| 679 | *map_entry = NULL; |
| 680 | |
| 681 | spin_lock_bh(&xs->map_list_lock); |
| 682 | node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node, |
| 683 | node); |
| 684 | if (node) { |
Zhu Yanjun | bb1b25c | 2020-11-26 23:03:18 +0800 | [diff] [blame] | 685 | bpf_map_inc(&node->map->map); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 686 | map = node->map; |
| 687 | *map_entry = node->map_entry; |
| 688 | } |
| 689 | spin_unlock_bh(&xs->map_list_lock); |
| 690 | return map; |
| 691 | } |
| 692 | |
| 693 | static void xsk_delete_from_maps(struct xdp_sock *xs) |
| 694 | { |
| 695 | /* This function removes the current XDP socket from all the |
| 696 | * maps it resides in. We need to take extra care here, due to |
| 697 | * the two locks involved. Each map has a lock synchronizing |
| 698 | * updates to the entries, and each socket has a lock that |
| 699 | * synchronizes access to the list of maps (map_list). For |
| 700 | * deadlock avoidance the locks need to be taken in the order |
| 701 | * "map lock"->"socket map list lock". We start off by |
| 702 | * accessing the socket map list, and take a reference to the |
| 703 | * map to guarantee existence between the |
| 704 | * xsk_get_map_list_entry() and xsk_map_try_sock_delete() |
| 705 | * calls. Then we ask the map to remove the socket, which |
| 706 | * tries to remove the socket from the map. Note that there |
| 707 | * might be updates to the map between |
| 708 | * xsk_get_map_list_entry() and xsk_map_try_sock_delete(). |
| 709 | */ |
| 710 | struct xdp_sock **map_entry = NULL; |
| 711 | struct xsk_map *map; |
| 712 | |
| 713 | while ((map = xsk_get_map_list_entry(xs, &map_entry))) { |
| 714 | xsk_map_try_sock_delete(map, xs, map_entry); |
Zhu Yanjun | bb1b25c | 2020-11-26 23:03:18 +0800 | [diff] [blame] | 715 | bpf_map_put(&map->map); |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 719 | static int xsk_release(struct socket *sock) |
| 720 | { |
| 721 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 722 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 723 | struct net *net; |
| 724 | |
| 725 | if (!sk) |
| 726 | return 0; |
| 727 | |
| 728 | net = sock_net(sk); |
| 729 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 730 | mutex_lock(&net->xdp.lock); |
| 731 | sk_del_node_init_rcu(sk); |
| 732 | mutex_unlock(&net->xdp.lock); |
| 733 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 734 | local_bh_disable(); |
| 735 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 736 | local_bh_enable(); |
| 737 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 738 | xsk_delete_from_maps(xs); |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 739 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 740 | xsk_unbind_dev(xs); |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 741 | mutex_unlock(&xs->mutex); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 742 | |
Björn Töpel | 541d7fd | 2018-10-05 13:25:15 +0200 | [diff] [blame] | 743 | xskq_destroy(xs->rx); |
| 744 | xskq_destroy(xs->tx); |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 745 | xskq_destroy(xs->fq_tmp); |
| 746 | xskq_destroy(xs->cq_tmp); |
Björn Töpel | 541d7fd | 2018-10-05 13:25:15 +0200 | [diff] [blame] | 747 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 748 | sock_orphan(sk); |
| 749 | sock->sk = NULL; |
| 750 | |
| 751 | sk_refcnt_debug_release(sk); |
| 752 | sock_put(sk); |
| 753 | |
| 754 | return 0; |
| 755 | } |
| 756 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 757 | static struct socket *xsk_lookup_xsk_from_fd(int fd) |
| 758 | { |
| 759 | struct socket *sock; |
| 760 | int err; |
| 761 | |
| 762 | sock = sockfd_lookup(fd, &err); |
| 763 | if (!sock) |
| 764 | return ERR_PTR(-ENOTSOCK); |
| 765 | |
| 766 | if (sock->sk->sk_family != PF_XDP) { |
| 767 | sockfd_put(sock); |
| 768 | return ERR_PTR(-ENOPROTOOPT); |
| 769 | } |
| 770 | |
| 771 | return sock; |
| 772 | } |
| 773 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 774 | static bool xsk_validate_queues(struct xdp_sock *xs) |
| 775 | { |
| 776 | return xs->fq_tmp && xs->cq_tmp; |
| 777 | } |
| 778 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 779 | static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 780 | { |
| 781 | struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr; |
| 782 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 783 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame] | 784 | struct net_device *dev; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 785 | u32 flags, qid; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 786 | int err = 0; |
| 787 | |
| 788 | if (addr_len < sizeof(struct sockaddr_xdp)) |
| 789 | return -EINVAL; |
| 790 | if (sxdp->sxdp_family != AF_XDP) |
| 791 | return -EINVAL; |
| 792 | |
Björn Töpel | f54ba39 | 2019-03-08 08:57:26 +0100 | [diff] [blame] | 793 | flags = sxdp->sxdp_flags; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 794 | if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY | |
| 795 | XDP_USE_NEED_WAKEUP)) |
Björn Töpel | f54ba39 | 2019-03-08 08:57:26 +0100 | [diff] [blame] | 796 | return -EINVAL; |
| 797 | |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 798 | rtnl_lock(); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 799 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 800 | if (xs->state != XSK_READY) { |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame] | 801 | err = -EBUSY; |
| 802 | goto out_release; |
| 803 | } |
| 804 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 805 | dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex); |
| 806 | if (!dev) { |
| 807 | err = -ENODEV; |
| 808 | goto out_release; |
| 809 | } |
| 810 | |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 811 | if (!xs->rx && !xs->tx) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 812 | err = -EINVAL; |
| 813 | goto out_unlock; |
| 814 | } |
| 815 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 816 | qid = sxdp->sxdp_queue_id; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 817 | |
| 818 | if (flags & XDP_SHARED_UMEM) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 819 | struct xdp_sock *umem_xs; |
| 820 | struct socket *sock; |
| 821 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 822 | if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) || |
| 823 | (flags & XDP_USE_NEED_WAKEUP)) { |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 824 | /* Cannot specify flags for shared sockets. */ |
| 825 | err = -EINVAL; |
| 826 | goto out_unlock; |
| 827 | } |
| 828 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 829 | if (xs->umem) { |
| 830 | /* We have already our own. */ |
| 831 | err = -EINVAL; |
| 832 | goto out_unlock; |
| 833 | } |
| 834 | |
| 835 | sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd); |
| 836 | if (IS_ERR(sock)) { |
| 837 | err = PTR_ERR(sock); |
| 838 | goto out_unlock; |
| 839 | } |
| 840 | |
| 841 | umem_xs = xdp_sk(sock->sk); |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 842 | if (!xsk_is_bound(umem_xs)) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 843 | err = -EBADF; |
| 844 | sockfd_put(sock); |
| 845 | goto out_unlock; |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 846 | } |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 847 | |
Magnus Karlsson | a113243 | 2020-08-28 10:26:26 +0200 | [diff] [blame] | 848 | if (umem_xs->queue_id != qid || umem_xs->dev != dev) { |
| 849 | /* Share the umem with another socket on another qid |
| 850 | * and/or device. |
| 851 | */ |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 852 | xs->pool = xp_create_and_assign_umem(xs, |
| 853 | umem_xs->umem); |
| 854 | if (!xs->pool) { |
Magnus Karlsson | 1fd17c8c | 2020-09-26 11:26:13 +0200 | [diff] [blame] | 855 | err = -ENOMEM; |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 856 | sockfd_put(sock); |
| 857 | goto out_unlock; |
| 858 | } |
| 859 | |
| 860 | err = xp_assign_dev_shared(xs->pool, umem_xs->umem, |
| 861 | dev, qid); |
| 862 | if (err) { |
| 863 | xp_destroy(xs->pool); |
Magnus Karlsson | 83cf5c6 | 2020-09-02 09:36:04 +0200 | [diff] [blame] | 864 | xs->pool = NULL; |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 865 | sockfd_put(sock); |
| 866 | goto out_unlock; |
| 867 | } |
| 868 | } else { |
| 869 | /* Share the buffer pool with the other socket. */ |
| 870 | if (xs->fq_tmp || xs->cq_tmp) { |
| 871 | /* Do not allow setting your own fq or cq. */ |
| 872 | err = -EINVAL; |
| 873 | sockfd_put(sock); |
| 874 | goto out_unlock; |
| 875 | } |
| 876 | |
| 877 | xp_get_pool(umem_xs->pool); |
| 878 | xs->pool = umem_xs->pool; |
| 879 | } |
| 880 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 881 | xdp_get_umem(umem_xs->umem); |
Björn Töpel | 9764f4b | 2019-09-04 13:49:11 +0200 | [diff] [blame] | 882 | WRITE_ONCE(xs->umem, umem_xs->umem); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 883 | sockfd_put(sock); |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 884 | } else if (!xs->umem || !xsk_validate_queues(xs)) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 885 | err = -EINVAL; |
| 886 | goto out_unlock; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 887 | } else { |
| 888 | /* This xsk has its own umem. */ |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 889 | xs->pool = xp_create_and_assign_umem(xs, xs->umem); |
| 890 | if (!xs->pool) { |
| 891 | err = -ENOMEM; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 892 | goto out_unlock; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | err = xp_assign_dev(xs->pool, dev, qid, flags); |
| 896 | if (err) { |
| 897 | xp_destroy(xs->pool); |
| 898 | xs->pool = NULL; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 899 | goto out_unlock; |
| 900 | } |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Magnus Karlsson | 8bee683 | 2020-12-14 09:51:27 +0100 | [diff] [blame] | 903 | /* FQ and CQ are now owned by the buffer pool and cleaned up with it. */ |
| 904 | xs->fq_tmp = NULL; |
| 905 | xs->cq_tmp = NULL; |
| 906 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 907 | xs->dev = dev; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 908 | xs->zc = xs->umem->zc; |
| 909 | xs->queue_id = qid; |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 910 | xp_add_xsk(xs->pool, xs); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 911 | |
| 912 | out_unlock: |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 913 | if (err) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 914 | dev_put(dev); |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 915 | } else { |
| 916 | /* Matches smp_rmb() in bind() for shared umem |
| 917 | * sockets, and xsk_is_bound(). |
| 918 | */ |
| 919 | smp_wmb(); |
| 920 | WRITE_ONCE(xs->state, XSK_BOUND); |
| 921 | } |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 922 | out_release: |
| 923 | mutex_unlock(&xs->mutex); |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 924 | rtnl_unlock(); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 925 | return err; |
| 926 | } |
| 927 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 928 | struct xdp_umem_reg_v1 { |
| 929 | __u64 addr; /* Start of packet data area */ |
| 930 | __u64 len; /* Length of packet data area */ |
| 931 | __u32 chunk_size; |
| 932 | __u32 headroom; |
| 933 | }; |
| 934 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 935 | static int xsk_setsockopt(struct socket *sock, int level, int optname, |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 936 | sockptr_t optval, unsigned int optlen) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 937 | { |
| 938 | struct sock *sk = sock->sk; |
| 939 | struct xdp_sock *xs = xdp_sk(sk); |
| 940 | int err; |
| 941 | |
| 942 | if (level != SOL_XDP) |
| 943 | return -ENOPROTOOPT; |
| 944 | |
| 945 | switch (optname) { |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 946 | case XDP_RX_RING: |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 947 | case XDP_TX_RING: |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 948 | { |
| 949 | struct xsk_queue **q; |
| 950 | int entries; |
| 951 | |
| 952 | if (optlen < sizeof(entries)) |
| 953 | return -EINVAL; |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 954 | if (copy_from_sockptr(&entries, optval, sizeof(entries))) |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 955 | return -EFAULT; |
| 956 | |
| 957 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 958 | if (xs->state != XSK_READY) { |
| 959 | mutex_unlock(&xs->mutex); |
| 960 | return -EBUSY; |
| 961 | } |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 962 | q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 963 | err = xsk_init_queue(entries, q, false); |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 964 | if (!err && optname == XDP_TX_RING) |
| 965 | /* Tx needs to be explicitly woken up the first time */ |
| 966 | xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 967 | mutex_unlock(&xs->mutex); |
| 968 | return err; |
| 969 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 970 | case XDP_UMEM_REG: |
| 971 | { |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 972 | size_t mr_size = sizeof(struct xdp_umem_reg); |
| 973 | struct xdp_umem_reg mr = {}; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 974 | struct xdp_umem *umem; |
| 975 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 976 | if (optlen < sizeof(struct xdp_umem_reg_v1)) |
| 977 | return -EINVAL; |
| 978 | else if (optlen < sizeof(mr)) |
| 979 | mr_size = sizeof(struct xdp_umem_reg_v1); |
| 980 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 981 | if (copy_from_sockptr(&mr, optval, mr_size)) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 982 | return -EFAULT; |
| 983 | |
| 984 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 985 | if (xs->state != XSK_READY || xs->umem) { |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 986 | mutex_unlock(&xs->mutex); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 987 | return -EBUSY; |
| 988 | } |
| 989 | |
| 990 | umem = xdp_umem_create(&mr); |
| 991 | if (IS_ERR(umem)) { |
| 992 | mutex_unlock(&xs->mutex); |
| 993 | return PTR_ERR(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* Make sure umem is ready before it can be seen by others */ |
| 997 | smp_wmb(); |
Björn Töpel | 9764f4b | 2019-09-04 13:49:11 +0200 | [diff] [blame] | 998 | WRITE_ONCE(xs->umem, umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 999 | mutex_unlock(&xs->mutex); |
| 1000 | return 0; |
| 1001 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1002 | case XDP_UMEM_FILL_RING: |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 1003 | case XDP_UMEM_COMPLETION_RING: |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1004 | { |
| 1005 | struct xsk_queue **q; |
| 1006 | int entries; |
| 1007 | |
Christoph Hellwig | a7b75c5 | 2020-07-23 08:09:07 +0200 | [diff] [blame] | 1008 | if (copy_from_sockptr(&entries, optval, sizeof(entries))) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1009 | return -EFAULT; |
| 1010 | |
| 1011 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1012 | if (xs->state != XSK_READY) { |
| 1013 | mutex_unlock(&xs->mutex); |
| 1014 | return -EBUSY; |
| 1015 | } |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 1016 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 1017 | q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp : |
| 1018 | &xs->cq_tmp; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 1019 | err = xsk_init_queue(entries, q, true); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1020 | mutex_unlock(&xs->mutex); |
| 1021 | return err; |
| 1022 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1023 | default: |
| 1024 | break; |
| 1025 | } |
| 1026 | |
| 1027 | return -ENOPROTOOPT; |
| 1028 | } |
| 1029 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1030 | static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring) |
| 1031 | { |
| 1032 | ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer); |
| 1033 | ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer); |
| 1034 | ring->desc = offsetof(struct xdp_rxtx_ring, desc); |
| 1035 | } |
| 1036 | |
| 1037 | static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring) |
| 1038 | { |
| 1039 | ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer); |
| 1040 | ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer); |
| 1041 | ring->desc = offsetof(struct xdp_umem_ring, desc); |
| 1042 | } |
| 1043 | |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1044 | struct xdp_statistics_v1 { |
| 1045 | __u64 rx_dropped; |
| 1046 | __u64 rx_invalid_descs; |
| 1047 | __u64 tx_invalid_descs; |
| 1048 | }; |
| 1049 | |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1050 | static int xsk_getsockopt(struct socket *sock, int level, int optname, |
| 1051 | char __user *optval, int __user *optlen) |
| 1052 | { |
| 1053 | struct sock *sk = sock->sk; |
| 1054 | struct xdp_sock *xs = xdp_sk(sk); |
| 1055 | int len; |
| 1056 | |
| 1057 | if (level != SOL_XDP) |
| 1058 | return -ENOPROTOOPT; |
| 1059 | |
| 1060 | if (get_user(len, optlen)) |
| 1061 | return -EFAULT; |
| 1062 | if (len < 0) |
| 1063 | return -EINVAL; |
| 1064 | |
| 1065 | switch (optname) { |
| 1066 | case XDP_STATISTICS: |
| 1067 | { |
Peilin Ye | 3c4f850 | 2020-07-28 01:36:04 -0400 | [diff] [blame] | 1068 | struct xdp_statistics stats = {}; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1069 | bool extra_stats = true; |
| 1070 | size_t stats_size; |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1071 | |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1072 | if (len < sizeof(struct xdp_statistics_v1)) { |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1073 | return -EINVAL; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1074 | } else if (len < sizeof(stats)) { |
| 1075 | extra_stats = false; |
| 1076 | stats_size = sizeof(struct xdp_statistics_v1); |
| 1077 | } else { |
| 1078 | stats_size = sizeof(stats); |
| 1079 | } |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1080 | |
| 1081 | mutex_lock(&xs->mutex); |
| 1082 | stats.rx_dropped = xs->rx_dropped; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1083 | if (extra_stats) { |
| 1084 | stats.rx_ring_full = xs->rx_queue_full; |
| 1085 | stats.rx_fill_ring_empty_descs = |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 1086 | xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1087 | stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx); |
| 1088 | } else { |
| 1089 | stats.rx_dropped += xs->rx_queue_full; |
| 1090 | } |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1091 | stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx); |
| 1092 | stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx); |
| 1093 | mutex_unlock(&xs->mutex); |
| 1094 | |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1095 | if (copy_to_user(optval, &stats, stats_size)) |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1096 | return -EFAULT; |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 1097 | if (put_user(stats_size, optlen)) |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1098 | return -EFAULT; |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1102 | case XDP_MMAP_OFFSETS: |
| 1103 | { |
| 1104 | struct xdp_mmap_offsets off; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1105 | struct xdp_mmap_offsets_v1 off_v1; |
| 1106 | bool flags_supported = true; |
| 1107 | void *to_copy; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1108 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1109 | if (len < sizeof(off_v1)) |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1110 | return -EINVAL; |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1111 | else if (len < sizeof(off)) |
| 1112 | flags_supported = false; |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1113 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1114 | if (flags_supported) { |
| 1115 | /* xdp_ring_offset is identical to xdp_ring_offset_v1 |
| 1116 | * except for the flags field added to the end. |
| 1117 | */ |
| 1118 | xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *) |
| 1119 | &off.rx); |
| 1120 | xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *) |
| 1121 | &off.tx); |
| 1122 | xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *) |
| 1123 | &off.fr); |
| 1124 | xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *) |
| 1125 | &off.cr); |
| 1126 | off.rx.flags = offsetof(struct xdp_rxtx_ring, |
| 1127 | ptrs.flags); |
| 1128 | off.tx.flags = offsetof(struct xdp_rxtx_ring, |
| 1129 | ptrs.flags); |
| 1130 | off.fr.flags = offsetof(struct xdp_umem_ring, |
| 1131 | ptrs.flags); |
| 1132 | off.cr.flags = offsetof(struct xdp_umem_ring, |
| 1133 | ptrs.flags); |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1134 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1135 | len = sizeof(off); |
| 1136 | to_copy = &off; |
| 1137 | } else { |
| 1138 | xsk_enter_rxtx_offsets(&off_v1.rx); |
| 1139 | xsk_enter_rxtx_offsets(&off_v1.tx); |
| 1140 | xsk_enter_umem_offsets(&off_v1.fr); |
| 1141 | xsk_enter_umem_offsets(&off_v1.cr); |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1142 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 1143 | len = sizeof(off_v1); |
| 1144 | to_copy = &off_v1; |
| 1145 | } |
| 1146 | |
| 1147 | if (copy_to_user(optval, to_copy, len)) |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 1148 | return -EFAULT; |
| 1149 | if (put_user(len, optlen)) |
| 1150 | return -EFAULT; |
| 1151 | |
| 1152 | return 0; |
| 1153 | } |
Maxim Mikityanskiy | 2640d3c | 2019-06-26 17:35:25 +0300 | [diff] [blame] | 1154 | case XDP_OPTIONS: |
| 1155 | { |
| 1156 | struct xdp_options opts = {}; |
| 1157 | |
| 1158 | if (len < sizeof(opts)) |
| 1159 | return -EINVAL; |
| 1160 | |
| 1161 | mutex_lock(&xs->mutex); |
| 1162 | if (xs->zc) |
| 1163 | opts.flags |= XDP_OPTIONS_ZEROCOPY; |
| 1164 | mutex_unlock(&xs->mutex); |
| 1165 | |
| 1166 | len = sizeof(opts); |
| 1167 | if (copy_to_user(optval, &opts, len)) |
| 1168 | return -EFAULT; |
| 1169 | if (put_user(len, optlen)) |
| 1170 | return -EFAULT; |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 1174 | default: |
| 1175 | break; |
| 1176 | } |
| 1177 | |
| 1178 | return -EOPNOTSUPP; |
| 1179 | } |
| 1180 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1181 | static int xsk_mmap(struct file *file, struct socket *sock, |
| 1182 | struct vm_area_struct *vma) |
| 1183 | { |
Geert Uytterhoeven | a5a16e4 | 2018-06-07 15:37:34 +0200 | [diff] [blame] | 1184 | loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1185 | unsigned long size = vma->vm_end - vma->vm_start; |
| 1186 | struct xdp_sock *xs = xdp_sk(sock->sk); |
| 1187 | struct xsk_queue *q = NULL; |
| 1188 | unsigned long pfn; |
| 1189 | struct page *qpg; |
| 1190 | |
Björn Töpel | 42fddcc | 2019-09-04 13:49:12 +0200 | [diff] [blame] | 1191 | if (READ_ONCE(xs->state) != XSK_READY) |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1192 | return -EBUSY; |
| 1193 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 1194 | if (offset == XDP_PGOFF_RX_RING) { |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 1195 | q = READ_ONCE(xs->rx); |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 1196 | } else if (offset == XDP_PGOFF_TX_RING) { |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 1197 | q = READ_ONCE(xs->tx); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 1198 | } else { |
Magnus Karlsson | e6762c8 | 2019-02-08 14:13:50 +0100 | [diff] [blame] | 1199 | /* Matches the smp_wmb() in XDP_UMEM_REG */ |
| 1200 | smp_rmb(); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 1201 | if (offset == XDP_UMEM_PGOFF_FILL_RING) |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 1202 | q = READ_ONCE(xs->fq_tmp); |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 1203 | else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING) |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 1204 | q = READ_ONCE(xs->cq_tmp); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 1205 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1206 | |
| 1207 | if (!q) |
| 1208 | return -EINVAL; |
| 1209 | |
Magnus Karlsson | e6762c8 | 2019-02-08 14:13:50 +0100 | [diff] [blame] | 1210 | /* Matches the smp_wmb() in xsk_init_queue */ |
| 1211 | smp_rmb(); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1212 | qpg = virt_to_head_page(q->ring); |
Matthew Wilcox (Oracle) | a50b854 | 2019-09-23 15:34:25 -0700 | [diff] [blame] | 1213 | if (size > page_size(qpg)) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 1214 | return -EINVAL; |
| 1215 | |
| 1216 | pfn = virt_to_phys(q->ring) >> PAGE_SHIFT; |
| 1217 | return remap_pfn_range(vma, vma->vm_start, pfn, |
| 1218 | size, vma->vm_page_prot); |
| 1219 | } |
| 1220 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1221 | static int xsk_notifier(struct notifier_block *this, |
| 1222 | unsigned long msg, void *ptr) |
| 1223 | { |
| 1224 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1225 | struct net *net = dev_net(dev); |
| 1226 | struct sock *sk; |
| 1227 | |
| 1228 | switch (msg) { |
| 1229 | case NETDEV_UNREGISTER: |
| 1230 | mutex_lock(&net->xdp.lock); |
| 1231 | sk_for_each(sk, &net->xdp.list) { |
| 1232 | struct xdp_sock *xs = xdp_sk(sk); |
| 1233 | |
| 1234 | mutex_lock(&xs->mutex); |
| 1235 | if (xs->dev == dev) { |
| 1236 | sk->sk_err = ENETDOWN; |
| 1237 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1238 | sk->sk_error_report(sk); |
| 1239 | |
| 1240 | xsk_unbind_dev(xs); |
| 1241 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 1242 | /* Clear device references. */ |
| 1243 | xp_clear_dev(xs->pool); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1244 | } |
| 1245 | mutex_unlock(&xs->mutex); |
| 1246 | } |
| 1247 | mutex_unlock(&net->xdp.lock); |
| 1248 | break; |
| 1249 | } |
| 1250 | return NOTIFY_DONE; |
| 1251 | } |
| 1252 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1253 | static struct proto xsk_proto = { |
| 1254 | .name = "XDP", |
| 1255 | .owner = THIS_MODULE, |
| 1256 | .obj_size = sizeof(struct xdp_sock), |
| 1257 | }; |
| 1258 | |
| 1259 | static const struct proto_ops xsk_proto_ops = { |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 1260 | .family = PF_XDP, |
| 1261 | .owner = THIS_MODULE, |
| 1262 | .release = xsk_release, |
| 1263 | .bind = xsk_bind, |
| 1264 | .connect = sock_no_connect, |
| 1265 | .socketpair = sock_no_socketpair, |
| 1266 | .accept = sock_no_accept, |
| 1267 | .getname = sock_no_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 1268 | .poll = xsk_poll, |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 1269 | .ioctl = sock_no_ioctl, |
| 1270 | .listen = sock_no_listen, |
| 1271 | .shutdown = sock_no_shutdown, |
| 1272 | .setsockopt = xsk_setsockopt, |
| 1273 | .getsockopt = xsk_getsockopt, |
| 1274 | .sendmsg = xsk_sendmsg, |
Björn Töpel | 45a8668 | 2020-11-30 19:51:58 +0100 | [diff] [blame] | 1275 | .recvmsg = xsk_recvmsg, |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 1276 | .mmap = xsk_mmap, |
| 1277 | .sendpage = sock_no_sendpage, |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1278 | }; |
| 1279 | |
Björn Töpel | 11fe926 | 2019-02-21 13:07:38 +0100 | [diff] [blame] | 1280 | static void xsk_destruct(struct sock *sk) |
| 1281 | { |
| 1282 | struct xdp_sock *xs = xdp_sk(sk); |
| 1283 | |
| 1284 | if (!sock_flag(sk, SOCK_DEAD)) |
| 1285 | return; |
| 1286 | |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 1287 | if (!xp_put_pool(xs->pool)) |
Magnus Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 1288 | xdp_put_umem(xs->umem, !xs->pool); |
Björn Töpel | 11fe926 | 2019-02-21 13:07:38 +0100 | [diff] [blame] | 1289 | |
| 1290 | sk_refcnt_debug_dec(sk); |
| 1291 | } |
| 1292 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1293 | static int xsk_create(struct net *net, struct socket *sock, int protocol, |
| 1294 | int kern) |
| 1295 | { |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1296 | struct xdp_sock *xs; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 1297 | struct sock *sk; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1298 | |
| 1299 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) |
| 1300 | return -EPERM; |
| 1301 | if (sock->type != SOCK_RAW) |
| 1302 | return -ESOCKTNOSUPPORT; |
| 1303 | |
| 1304 | if (protocol) |
| 1305 | return -EPROTONOSUPPORT; |
| 1306 | |
| 1307 | sock->state = SS_UNCONNECTED; |
| 1308 | |
| 1309 | sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern); |
| 1310 | if (!sk) |
| 1311 | return -ENOBUFS; |
| 1312 | |
| 1313 | sock->ops = &xsk_proto_ops; |
| 1314 | |
| 1315 | sock_init_data(sock, sk); |
| 1316 | |
| 1317 | sk->sk_family = PF_XDP; |
| 1318 | |
Björn Töpel | 11fe926 | 2019-02-21 13:07:38 +0100 | [diff] [blame] | 1319 | sk->sk_destruct = xsk_destruct; |
| 1320 | sk_refcnt_debug_inc(sk); |
| 1321 | |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 1322 | sock_set_flag(sk, SOCK_RCU_FREE); |
| 1323 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1324 | xs = xdp_sk(sk); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1325 | xs->state = XSK_READY; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1326 | mutex_init(&xs->mutex); |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 1327 | spin_lock_init(&xs->rx_lock); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1328 | |
Björn Töpel | 0402acd | 2019-08-15 11:30:13 +0200 | [diff] [blame] | 1329 | INIT_LIST_HEAD(&xs->map_list); |
| 1330 | spin_lock_init(&xs->map_list_lock); |
| 1331 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 1332 | mutex_lock(&net->xdp.lock); |
| 1333 | sk_add_node_rcu(sk, &net->xdp.list); |
| 1334 | mutex_unlock(&net->xdp.lock); |
| 1335 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1336 | local_bh_disable(); |
| 1337 | sock_prot_inuse_add(net, &xsk_proto, 1); |
| 1338 | local_bh_enable(); |
| 1339 | |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
| 1343 | static const struct net_proto_family xsk_family_ops = { |
| 1344 | .family = PF_XDP, |
| 1345 | .create = xsk_create, |
| 1346 | .owner = THIS_MODULE, |
| 1347 | }; |
| 1348 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1349 | static struct notifier_block xsk_netdev_notifier = { |
| 1350 | .notifier_call = xsk_notifier, |
| 1351 | }; |
| 1352 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 1353 | static int __net_init xsk_net_init(struct net *net) |
| 1354 | { |
| 1355 | mutex_init(&net->xdp.lock); |
| 1356 | INIT_HLIST_HEAD(&net->xdp.list); |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | static void __net_exit xsk_net_exit(struct net *net) |
| 1361 | { |
| 1362 | WARN_ON_ONCE(!hlist_empty(&net->xdp.list)); |
| 1363 | } |
| 1364 | |
| 1365 | static struct pernet_operations xsk_net_ops = { |
| 1366 | .init = xsk_net_init, |
| 1367 | .exit = xsk_net_exit, |
| 1368 | }; |
| 1369 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1370 | static int __init xsk_init(void) |
| 1371 | { |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 1372 | int err, cpu; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1373 | |
| 1374 | err = proto_register(&xsk_proto, 0 /* no slab */); |
| 1375 | if (err) |
| 1376 | goto out; |
| 1377 | |
| 1378 | err = sock_register(&xsk_family_ops); |
| 1379 | if (err) |
| 1380 | goto out_proto; |
| 1381 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 1382 | err = register_pernet_subsys(&xsk_net_ops); |
| 1383 | if (err) |
| 1384 | goto out_sk; |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1385 | |
| 1386 | err = register_netdevice_notifier(&xsk_netdev_notifier); |
| 1387 | if (err) |
| 1388 | goto out_pernet; |
| 1389 | |
Björn Töpel | e312b9e | 2019-12-19 07:10:02 +0100 | [diff] [blame] | 1390 | for_each_possible_cpu(cpu) |
| 1391 | INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu)); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1392 | return 0; |
| 1393 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 1394 | out_pernet: |
| 1395 | unregister_pernet_subsys(&xsk_net_ops); |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 1396 | out_sk: |
| 1397 | sock_unregister(PF_XDP); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1398 | out_proto: |
| 1399 | proto_unregister(&xsk_proto); |
| 1400 | out: |
| 1401 | return err; |
| 1402 | } |
| 1403 | |
| 1404 | fs_initcall(xsk_init); |