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