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