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 | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 34 | bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs) |
| 35 | { |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 36 | return READ_ONCE(xs->rx) && READ_ONCE(xs->umem) && |
| 37 | READ_ONCE(xs->umem->fq); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Maxim Mikityanskiy | d57d764 | 2019-06-26 17:35:24 +0300 | [diff] [blame] | 40 | bool xsk_umem_has_addrs(struct xdp_umem *umem, u32 cnt) |
| 41 | { |
| 42 | return xskq_has_addrs(umem->fq, cnt); |
| 43 | } |
| 44 | EXPORT_SYMBOL(xsk_umem_has_addrs); |
| 45 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 46 | u64 *xsk_umem_peek_addr(struct xdp_umem *umem, u64 *addr) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 47 | { |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 48 | return xskq_peek_addr(umem->fq, addr); |
| 49 | } |
| 50 | EXPORT_SYMBOL(xsk_umem_peek_addr); |
| 51 | |
| 52 | void xsk_umem_discard_addr(struct xdp_umem *umem) |
| 53 | { |
| 54 | xskq_discard_addr(umem->fq); |
| 55 | } |
| 56 | EXPORT_SYMBOL(xsk_umem_discard_addr); |
| 57 | |
| 58 | static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) |
| 59 | { |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 60 | void *to_buf, *from_buf; |
| 61 | u32 metalen; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 62 | u64 addr; |
Björn Töpel | 4e64c83 | 2018-06-04 13:57:11 +0200 | [diff] [blame] | 63 | int err; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 64 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 65 | if (!xskq_peek_addr(xs->umem->fq, &addr) || |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 66 | len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) { |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 67 | xs->rx_dropped++; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 68 | return -ENOSPC; |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 69 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 70 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 71 | addr += xs->umem->headroom; |
| 72 | |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 73 | if (unlikely(xdp_data_meta_unsupported(xdp))) { |
| 74 | from_buf = xdp->data; |
| 75 | metalen = 0; |
| 76 | } else { |
| 77 | from_buf = xdp->data_meta; |
| 78 | metalen = xdp->data - xdp->data_meta; |
| 79 | } |
| 80 | |
| 81 | to_buf = xdp_umem_get_data(xs->umem, addr); |
| 82 | memcpy(to_buf, from_buf, len + metalen); |
| 83 | addr += metalen; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 84 | err = xskq_produce_batch_desc(xs->rx, addr, len); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 85 | if (!err) { |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 86 | xskq_discard_addr(xs->umem->fq); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 87 | xdp_return_buff(xdp); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | xs->rx_dropped++; |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len) |
| 96 | { |
| 97 | int err = xskq_produce_batch_desc(xs->rx, (u64)xdp->handle, len); |
| 98 | |
Jakub Kicinski | 2d55d61 | 2018-07-27 20:20:08 -0700 | [diff] [blame] | 99 | if (err) |
Björn Töpel | a509a95 | 2018-06-04 13:57:12 +0200 | [diff] [blame] | 100 | xs->rx_dropped++; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 101 | |
| 102 | return err; |
| 103 | } |
| 104 | |
| 105 | int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 106 | { |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 107 | u32 len; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 108 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 109 | if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) |
| 110 | return -EINVAL; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 111 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 112 | len = xdp->data_end - xdp->data; |
| 113 | |
| 114 | return (xdp->rxq->mem.type == MEM_TYPE_ZERO_COPY) ? |
| 115 | __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] | 116 | } |
| 117 | |
| 118 | void xsk_flush(struct xdp_sock *xs) |
| 119 | { |
| 120 | xskq_produce_flush_desc(xs->rx); |
| 121 | xs->sk.sk_data_ready(&xs->sk); |
| 122 | } |
| 123 | |
| 124 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 125 | { |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 126 | u32 metalen = xdp->data - xdp->data_meta; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 127 | u32 len = xdp->data_end - xdp->data; |
| 128 | void *buffer; |
| 129 | u64 addr; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 130 | int err; |
| 131 | |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 132 | spin_lock_bh(&xs->rx_lock); |
| 133 | |
| 134 | if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) { |
| 135 | err = -EINVAL; |
| 136 | goto out_unlock; |
| 137 | } |
Björn Töpel | 5d90237 | 2018-06-12 12:02:56 +0200 | [diff] [blame] | 138 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 139 | if (!xskq_peek_addr(xs->umem->fq, &addr) || |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 140 | len > xs->umem->chunk_size_nohr - XDP_PACKET_HEADROOM) { |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 141 | err = -ENOSPC; |
| 142 | goto out_drop; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 143 | } |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 144 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 145 | addr += xs->umem->headroom; |
| 146 | |
| 147 | buffer = xdp_umem_get_data(xs->umem, addr); |
Björn Töpel | 18baed2 | 2018-08-30 15:12:48 +0200 | [diff] [blame] | 148 | memcpy(buffer, xdp->data_meta, len + metalen); |
| 149 | addr += metalen; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 150 | err = xskq_produce_batch_desc(xs->rx, addr, len); |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 151 | if (err) |
| 152 | goto out_drop; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 153 | |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 154 | xskq_discard_addr(xs->umem->fq); |
| 155 | xskq_produce_flush_desc(xs->rx); |
| 156 | |
| 157 | spin_unlock_bh(&xs->rx_lock); |
| 158 | |
| 159 | xs->sk.sk_data_ready(&xs->sk); |
| 160 | return 0; |
| 161 | |
| 162 | out_drop: |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 163 | xs->rx_dropped++; |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 164 | out_unlock: |
| 165 | spin_unlock_bh(&xs->rx_lock); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 166 | return err; |
| 167 | } |
| 168 | |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 169 | void xsk_umem_complete_tx(struct xdp_umem *umem, u32 nb_entries) |
| 170 | { |
| 171 | xskq_produce_flush_addr_n(umem->cq, nb_entries); |
| 172 | } |
| 173 | EXPORT_SYMBOL(xsk_umem_complete_tx); |
| 174 | |
| 175 | void xsk_umem_consume_tx_done(struct xdp_umem *umem) |
| 176 | { |
| 177 | struct xdp_sock *xs; |
| 178 | |
| 179 | rcu_read_lock(); |
| 180 | list_for_each_entry_rcu(xs, &umem->xsk_list, list) { |
| 181 | xs->sk.sk_write_space(&xs->sk); |
| 182 | } |
| 183 | rcu_read_unlock(); |
| 184 | } |
| 185 | EXPORT_SYMBOL(xsk_umem_consume_tx_done); |
| 186 | |
Maxim Mikityanskiy | 4bce4e5 | 2019-06-26 17:35:28 +0300 | [diff] [blame] | 187 | 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] | 188 | { |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 189 | struct xdp_sock *xs; |
| 190 | |
| 191 | rcu_read_lock(); |
| 192 | list_for_each_entry_rcu(xs, &umem->xsk_list, list) { |
Maxim Mikityanskiy | 4bce4e5 | 2019-06-26 17:35:28 +0300 | [diff] [blame] | 193 | if (!xskq_peek_desc(xs->tx, desc)) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 194 | continue; |
| 195 | |
Maxim Mikityanskiy | 4bce4e5 | 2019-06-26 17:35:28 +0300 | [diff] [blame] | 196 | if (xskq_produce_addr_lazy(umem->cq, desc->addr)) |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 197 | goto out; |
| 198 | |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 199 | xskq_discard_desc(xs->tx); |
| 200 | rcu_read_unlock(); |
| 201 | return true; |
| 202 | } |
| 203 | |
| 204 | out: |
| 205 | rcu_read_unlock(); |
| 206 | return false; |
| 207 | } |
| 208 | EXPORT_SYMBOL(xsk_umem_consume_tx); |
| 209 | |
| 210 | static int xsk_zc_xmit(struct sock *sk) |
| 211 | { |
| 212 | struct xdp_sock *xs = xdp_sk(sk); |
| 213 | struct net_device *dev = xs->dev; |
| 214 | |
| 215 | return dev->netdev_ops->ndo_xsk_async_xmit(dev, xs->queue_id); |
| 216 | } |
| 217 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 218 | static void xsk_destruct_skb(struct sk_buff *skb) |
| 219 | { |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 220 | u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 221 | struct xdp_sock *xs = xdp_sk(skb->sk); |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 222 | unsigned long flags; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 223 | |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 224 | spin_lock_irqsave(&xs->tx_completion_lock, flags); |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 225 | WARN_ON_ONCE(xskq_produce_addr(xs->umem->cq, addr)); |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 226 | spin_unlock_irqrestore(&xs->tx_completion_lock, flags); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 227 | |
| 228 | sock_wfree(skb); |
| 229 | } |
| 230 | |
| 231 | static int xsk_generic_xmit(struct sock *sk, struct msghdr *m, |
| 232 | size_t total_len) |
| 233 | { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 234 | u32 max_batch = TX_BATCH_SIZE; |
| 235 | struct xdp_sock *xs = xdp_sk(sk); |
| 236 | bool sent_frame = false; |
| 237 | struct xdp_desc desc; |
| 238 | struct sk_buff *skb; |
| 239 | int err = 0; |
| 240 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 241 | mutex_lock(&xs->mutex); |
| 242 | |
Ilya Maximets | 6757164 | 2019-07-04 17:25:03 +0300 | [diff] [blame] | 243 | if (xs->queue_id >= xs->dev->real_num_tx_queues) |
| 244 | goto out; |
| 245 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 246 | while (xskq_peek_desc(xs->tx, &desc)) { |
| 247 | char *buffer; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 248 | u64 addr; |
| 249 | u32 len; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 250 | |
| 251 | if (max_batch-- == 0) { |
| 252 | err = -EAGAIN; |
| 253 | goto out; |
| 254 | } |
| 255 | |
Magnus Karlsson | 09210c4 | 2018-07-11 10:12:52 +0200 | [diff] [blame] | 256 | len = desc.len; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 257 | skb = sock_alloc_send_skb(sk, len, 1, &err); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 258 | if (unlikely(!skb)) { |
| 259 | err = -EAGAIN; |
| 260 | goto out; |
| 261 | } |
| 262 | |
| 263 | skb_put(skb, len); |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 264 | addr = desc.addr; |
| 265 | buffer = xdp_umem_get_data(xs->umem, addr); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 266 | err = skb_store_bits(skb, 0, buffer, len); |
Ilya Maximets | 6757164 | 2019-07-04 17:25:03 +0300 | [diff] [blame] | 267 | if (unlikely(err) || xskq_reserve_addr(xs->umem->cq)) { |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 268 | kfree_skb(skb); |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | skb->dev = xs->dev; |
| 273 | skb->priority = sk->sk_priority; |
| 274 | skb->mark = sk->sk_mark; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 275 | skb_shinfo(skb)->destructor_arg = (void *)(long)addr; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 276 | skb->destructor = xsk_destruct_skb; |
| 277 | |
| 278 | err = dev_direct_xmit(skb, xs->queue_id); |
Magnus Karlsson | fe58868 | 2018-06-29 09:48:18 +0200 | [diff] [blame] | 279 | xskq_discard_desc(xs->tx); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 280 | /* Ignore NET_XMIT_CN as packet might have been sent */ |
| 281 | if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) { |
Magnus Karlsson | fe58868 | 2018-06-29 09:48:18 +0200 | [diff] [blame] | 282 | /* SKB completed but not sent */ |
| 283 | err = -EBUSY; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 284 | goto out; |
| 285 | } |
| 286 | |
| 287 | sent_frame = true; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | out: |
| 291 | if (sent_frame) |
| 292 | sk->sk_write_space(sk); |
| 293 | |
| 294 | mutex_unlock(&xs->mutex); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) |
| 299 | { |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 300 | bool need_wait = !(m->msg_flags & MSG_DONTWAIT); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 301 | struct sock *sk = sock->sk; |
| 302 | struct xdp_sock *xs = xdp_sk(sk); |
| 303 | |
| 304 | if (unlikely(!xs->dev)) |
| 305 | return -ENXIO; |
| 306 | if (unlikely(!(xs->dev->flags & IFF_UP))) |
| 307 | return -ENETDOWN; |
Magnus Karlsson | 6efb443 | 2018-07-11 10:12:51 +0200 | [diff] [blame] | 308 | if (unlikely(!xs->tx)) |
| 309 | return -ENOBUFS; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 310 | if (need_wait) |
| 311 | return -EOPNOTSUPP; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 312 | |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 313 | return (xs->zc) ? xsk_zc_xmit(sk) : xsk_generic_xmit(sk, m, total_len); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 314 | } |
| 315 | |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 316 | static unsigned int xsk_poll(struct file *file, struct socket *sock, |
| 317 | struct poll_table_struct *wait) |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 318 | { |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 319 | unsigned int mask = datagram_poll(file, sock, wait); |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 320 | struct sock *sk = sock->sk; |
| 321 | struct xdp_sock *xs = xdp_sk(sk); |
| 322 | |
| 323 | if (xs->rx && !xskq_empty_desc(xs->rx)) |
| 324 | mask |= POLLIN | POLLRDNORM; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 325 | if (xs->tx && !xskq_full_desc(xs->tx)) |
| 326 | mask |= POLLOUT | POLLWRNORM; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 327 | |
| 328 | return mask; |
| 329 | } |
| 330 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 331 | static int xsk_init_queue(u32 entries, struct xsk_queue **queue, |
| 332 | bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 333 | { |
| 334 | struct xsk_queue *q; |
| 335 | |
| 336 | if (entries == 0 || *queue || !is_power_of_2(entries)) |
| 337 | return -EINVAL; |
| 338 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 339 | q = xskq_create(entries, umem_queue); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 340 | if (!q) |
| 341 | return -ENOMEM; |
| 342 | |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 343 | /* Make sure queue is ready before it can be seen by others */ |
| 344 | smp_wmb(); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 345 | *queue = q; |
| 346 | return 0; |
| 347 | } |
| 348 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 349 | static void xsk_unbind_dev(struct xdp_sock *xs) |
| 350 | { |
| 351 | struct net_device *dev = xs->dev; |
| 352 | |
| 353 | if (!dev || xs->state != XSK_BOUND) |
| 354 | return; |
| 355 | |
| 356 | xs->state = XSK_UNBOUND; |
| 357 | |
| 358 | /* Wait for driver to stop using the xdp socket. */ |
| 359 | xdp_del_sk_umem(xs->umem, xs); |
| 360 | xs->dev = NULL; |
| 361 | synchronize_net(); |
| 362 | dev_put(dev); |
| 363 | } |
| 364 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 365 | static int xsk_release(struct socket *sock) |
| 366 | { |
| 367 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 368 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 369 | struct net *net; |
| 370 | |
| 371 | if (!sk) |
| 372 | return 0; |
| 373 | |
| 374 | net = sock_net(sk); |
| 375 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 376 | mutex_lock(&net->xdp.lock); |
| 377 | sk_del_node_init_rcu(sk); |
| 378 | mutex_unlock(&net->xdp.lock); |
| 379 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 380 | local_bh_disable(); |
| 381 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 382 | local_bh_enable(); |
| 383 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 384 | xsk_unbind_dev(xs); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 385 | |
Björn Töpel | 541d7fd | 2018-10-05 13:25:15 +0200 | [diff] [blame] | 386 | xskq_destroy(xs->rx); |
| 387 | xskq_destroy(xs->tx); |
| 388 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 389 | sock_orphan(sk); |
| 390 | sock->sk = NULL; |
| 391 | |
| 392 | sk_refcnt_debug_release(sk); |
| 393 | sock_put(sk); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 398 | static struct socket *xsk_lookup_xsk_from_fd(int fd) |
| 399 | { |
| 400 | struct socket *sock; |
| 401 | int err; |
| 402 | |
| 403 | sock = sockfd_lookup(fd, &err); |
| 404 | if (!sock) |
| 405 | return ERR_PTR(-ENOTSOCK); |
| 406 | |
| 407 | if (sock->sk->sk_family != PF_XDP) { |
| 408 | sockfd_put(sock); |
| 409 | return ERR_PTR(-ENOPROTOOPT); |
| 410 | } |
| 411 | |
| 412 | return sock; |
| 413 | } |
| 414 | |
| 415 | static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 416 | { |
| 417 | struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr; |
| 418 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 419 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame] | 420 | struct net_device *dev; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 421 | u32 flags, qid; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 422 | int err = 0; |
| 423 | |
| 424 | if (addr_len < sizeof(struct sockaddr_xdp)) |
| 425 | return -EINVAL; |
| 426 | if (sxdp->sxdp_family != AF_XDP) |
| 427 | return -EINVAL; |
| 428 | |
Björn Töpel | f54ba39 | 2019-03-08 08:57:26 +0100 | [diff] [blame] | 429 | flags = sxdp->sxdp_flags; |
| 430 | if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY)) |
| 431 | return -EINVAL; |
| 432 | |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame^] | 433 | rtnl_lock(); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 434 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 435 | if (xs->state != XSK_READY) { |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame] | 436 | err = -EBUSY; |
| 437 | goto out_release; |
| 438 | } |
| 439 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 440 | dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex); |
| 441 | if (!dev) { |
| 442 | err = -ENODEV; |
| 443 | goto out_release; |
| 444 | } |
| 445 | |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 446 | if (!xs->rx && !xs->tx) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 447 | err = -EINVAL; |
| 448 | goto out_unlock; |
| 449 | } |
| 450 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 451 | qid = sxdp->sxdp_queue_id; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 452 | |
| 453 | if (flags & XDP_SHARED_UMEM) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 454 | struct xdp_sock *umem_xs; |
| 455 | struct socket *sock; |
| 456 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 457 | if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY)) { |
| 458 | /* Cannot specify flags for shared sockets. */ |
| 459 | err = -EINVAL; |
| 460 | goto out_unlock; |
| 461 | } |
| 462 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 463 | if (xs->umem) { |
| 464 | /* We have already our own. */ |
| 465 | err = -EINVAL; |
| 466 | goto out_unlock; |
| 467 | } |
| 468 | |
| 469 | sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd); |
| 470 | if (IS_ERR(sock)) { |
| 471 | err = PTR_ERR(sock); |
| 472 | goto out_unlock; |
| 473 | } |
| 474 | |
| 475 | umem_xs = xdp_sk(sock->sk); |
| 476 | if (!umem_xs->umem) { |
| 477 | /* No umem to inherit. */ |
| 478 | err = -EBADF; |
| 479 | sockfd_put(sock); |
| 480 | goto out_unlock; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 481 | } else if (umem_xs->dev != dev || umem_xs->queue_id != qid) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 482 | err = -EINVAL; |
| 483 | sockfd_put(sock); |
| 484 | goto out_unlock; |
| 485 | } |
| 486 | |
| 487 | xdp_get_umem(umem_xs->umem); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 488 | xs->umem = umem_xs->umem; |
| 489 | sockfd_put(sock); |
| 490 | } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) { |
| 491 | err = -EINVAL; |
| 492 | goto out_unlock; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 493 | } else { |
| 494 | /* This xsk has its own umem. */ |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 495 | xskq_set_umem(xs->umem->fq, xs->umem->size, |
| 496 | xs->umem->chunk_mask); |
| 497 | xskq_set_umem(xs->umem->cq, xs->umem->size, |
| 498 | xs->umem->chunk_mask); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 499 | |
| 500 | err = xdp_umem_assign_dev(xs->umem, dev, qid, flags); |
| 501 | if (err) |
| 502 | goto out_unlock; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 503 | } |
| 504 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 505 | xs->dev = dev; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 506 | xs->zc = xs->umem->zc; |
| 507 | xs->queue_id = qid; |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 508 | xskq_set_umem(xs->rx, xs->umem->size, xs->umem->chunk_mask); |
| 509 | xskq_set_umem(xs->tx, xs->umem->size, xs->umem->chunk_mask); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 510 | xdp_add_sk_umem(xs->umem, xs); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 511 | |
| 512 | out_unlock: |
| 513 | if (err) |
| 514 | dev_put(dev); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 515 | else |
| 516 | xs->state = XSK_BOUND; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 517 | out_release: |
| 518 | mutex_unlock(&xs->mutex); |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame^] | 519 | rtnl_unlock(); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 520 | return err; |
| 521 | } |
| 522 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 523 | static int xsk_setsockopt(struct socket *sock, int level, int optname, |
| 524 | char __user *optval, unsigned int optlen) |
| 525 | { |
| 526 | struct sock *sk = sock->sk; |
| 527 | struct xdp_sock *xs = xdp_sk(sk); |
| 528 | int err; |
| 529 | |
| 530 | if (level != SOL_XDP) |
| 531 | return -ENOPROTOOPT; |
| 532 | |
| 533 | switch (optname) { |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 534 | case XDP_RX_RING: |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 535 | case XDP_TX_RING: |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 536 | { |
| 537 | struct xsk_queue **q; |
| 538 | int entries; |
| 539 | |
| 540 | if (optlen < sizeof(entries)) |
| 541 | return -EINVAL; |
| 542 | if (copy_from_user(&entries, optval, sizeof(entries))) |
| 543 | return -EFAULT; |
| 544 | |
| 545 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 546 | if (xs->state != XSK_READY) { |
| 547 | mutex_unlock(&xs->mutex); |
| 548 | return -EBUSY; |
| 549 | } |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 550 | q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 551 | err = xsk_init_queue(entries, q, false); |
| 552 | mutex_unlock(&xs->mutex); |
| 553 | return err; |
| 554 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 555 | case XDP_UMEM_REG: |
| 556 | { |
| 557 | struct xdp_umem_reg mr; |
| 558 | struct xdp_umem *umem; |
| 559 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 560 | if (copy_from_user(&mr, optval, sizeof(mr))) |
| 561 | return -EFAULT; |
| 562 | |
| 563 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 564 | if (xs->state != XSK_READY || xs->umem) { |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 565 | mutex_unlock(&xs->mutex); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 566 | return -EBUSY; |
| 567 | } |
| 568 | |
| 569 | umem = xdp_umem_create(&mr); |
| 570 | if (IS_ERR(umem)) { |
| 571 | mutex_unlock(&xs->mutex); |
| 572 | return PTR_ERR(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* Make sure umem is ready before it can be seen by others */ |
| 576 | smp_wmb(); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 577 | xs->umem = umem; |
| 578 | mutex_unlock(&xs->mutex); |
| 579 | return 0; |
| 580 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 581 | case XDP_UMEM_FILL_RING: |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 582 | case XDP_UMEM_COMPLETION_RING: |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 583 | { |
| 584 | struct xsk_queue **q; |
| 585 | int entries; |
| 586 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 587 | if (copy_from_user(&entries, optval, sizeof(entries))) |
| 588 | return -EFAULT; |
| 589 | |
| 590 | mutex_lock(&xs->mutex); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 591 | if (xs->state != XSK_READY) { |
| 592 | mutex_unlock(&xs->mutex); |
| 593 | return -EBUSY; |
| 594 | } |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 595 | if (!xs->umem) { |
| 596 | mutex_unlock(&xs->mutex); |
| 597 | return -EINVAL; |
| 598 | } |
| 599 | |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 600 | q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq : |
| 601 | &xs->umem->cq; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 602 | err = xsk_init_queue(entries, q, true); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 603 | mutex_unlock(&xs->mutex); |
| 604 | return err; |
| 605 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 606 | default: |
| 607 | break; |
| 608 | } |
| 609 | |
| 610 | return -ENOPROTOOPT; |
| 611 | } |
| 612 | |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 613 | static int xsk_getsockopt(struct socket *sock, int level, int optname, |
| 614 | char __user *optval, int __user *optlen) |
| 615 | { |
| 616 | struct sock *sk = sock->sk; |
| 617 | struct xdp_sock *xs = xdp_sk(sk); |
| 618 | int len; |
| 619 | |
| 620 | if (level != SOL_XDP) |
| 621 | return -ENOPROTOOPT; |
| 622 | |
| 623 | if (get_user(len, optlen)) |
| 624 | return -EFAULT; |
| 625 | if (len < 0) |
| 626 | return -EINVAL; |
| 627 | |
| 628 | switch (optname) { |
| 629 | case XDP_STATISTICS: |
| 630 | { |
| 631 | struct xdp_statistics stats; |
| 632 | |
| 633 | if (len < sizeof(stats)) |
| 634 | return -EINVAL; |
| 635 | |
| 636 | mutex_lock(&xs->mutex); |
| 637 | stats.rx_dropped = xs->rx_dropped; |
| 638 | stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx); |
| 639 | stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx); |
| 640 | mutex_unlock(&xs->mutex); |
| 641 | |
| 642 | if (copy_to_user(optval, &stats, sizeof(stats))) |
| 643 | return -EFAULT; |
| 644 | if (put_user(sizeof(stats), optlen)) |
| 645 | return -EFAULT; |
| 646 | |
| 647 | return 0; |
| 648 | } |
Björn Töpel | b3a9e0b | 2018-05-22 09:34:59 +0200 | [diff] [blame] | 649 | case XDP_MMAP_OFFSETS: |
| 650 | { |
| 651 | struct xdp_mmap_offsets off; |
| 652 | |
| 653 | if (len < sizeof(off)) |
| 654 | return -EINVAL; |
| 655 | |
| 656 | off.rx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer); |
| 657 | off.rx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer); |
| 658 | off.rx.desc = offsetof(struct xdp_rxtx_ring, desc); |
| 659 | off.tx.producer = offsetof(struct xdp_rxtx_ring, ptrs.producer); |
| 660 | off.tx.consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer); |
| 661 | off.tx.desc = offsetof(struct xdp_rxtx_ring, desc); |
| 662 | |
| 663 | off.fr.producer = offsetof(struct xdp_umem_ring, ptrs.producer); |
| 664 | off.fr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer); |
| 665 | off.fr.desc = offsetof(struct xdp_umem_ring, desc); |
| 666 | off.cr.producer = offsetof(struct xdp_umem_ring, ptrs.producer); |
| 667 | off.cr.consumer = offsetof(struct xdp_umem_ring, ptrs.consumer); |
| 668 | off.cr.desc = offsetof(struct xdp_umem_ring, desc); |
| 669 | |
| 670 | len = sizeof(off); |
| 671 | if (copy_to_user(optval, &off, len)) |
| 672 | return -EFAULT; |
| 673 | if (put_user(len, optlen)) |
| 674 | return -EFAULT; |
| 675 | |
| 676 | return 0; |
| 677 | } |
Maxim Mikityanskiy | 2640d3c | 2019-06-26 17:35:25 +0300 | [diff] [blame] | 678 | case XDP_OPTIONS: |
| 679 | { |
| 680 | struct xdp_options opts = {}; |
| 681 | |
| 682 | if (len < sizeof(opts)) |
| 683 | return -EINVAL; |
| 684 | |
| 685 | mutex_lock(&xs->mutex); |
| 686 | if (xs->zc) |
| 687 | opts.flags |= XDP_OPTIONS_ZEROCOPY; |
| 688 | mutex_unlock(&xs->mutex); |
| 689 | |
| 690 | len = sizeof(opts); |
| 691 | if (copy_to_user(optval, &opts, len)) |
| 692 | return -EFAULT; |
| 693 | if (put_user(len, optlen)) |
| 694 | return -EFAULT; |
| 695 | |
| 696 | return 0; |
| 697 | } |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 698 | default: |
| 699 | break; |
| 700 | } |
| 701 | |
| 702 | return -EOPNOTSUPP; |
| 703 | } |
| 704 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 705 | static int xsk_mmap(struct file *file, struct socket *sock, |
| 706 | struct vm_area_struct *vma) |
| 707 | { |
Geert Uytterhoeven | a5a16e4 | 2018-06-07 15:37:34 +0200 | [diff] [blame] | 708 | loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 709 | unsigned long size = vma->vm_end - vma->vm_start; |
| 710 | struct xdp_sock *xs = xdp_sk(sock->sk); |
| 711 | struct xsk_queue *q = NULL; |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 712 | struct xdp_umem *umem; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 713 | unsigned long pfn; |
| 714 | struct page *qpg; |
| 715 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 716 | if (xs->state != XSK_READY) |
| 717 | return -EBUSY; |
| 718 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 719 | if (offset == XDP_PGOFF_RX_RING) { |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 720 | q = READ_ONCE(xs->rx); |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 721 | } else if (offset == XDP_PGOFF_TX_RING) { |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 722 | q = READ_ONCE(xs->tx); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 723 | } else { |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 724 | umem = READ_ONCE(xs->umem); |
| 725 | if (!umem) |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 726 | return -EINVAL; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 727 | |
Magnus Karlsson | e6762c8 | 2019-02-08 14:13:50 +0100 | [diff] [blame] | 728 | /* Matches the smp_wmb() in XDP_UMEM_REG */ |
| 729 | smp_rmb(); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 730 | if (offset == XDP_UMEM_PGOFF_FILL_RING) |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 731 | q = READ_ONCE(umem->fq); |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 732 | else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING) |
Björn Töpel | 37b0769 | 2018-05-22 09:35:01 +0200 | [diff] [blame] | 733 | q = READ_ONCE(umem->cq); |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 734 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 735 | |
| 736 | if (!q) |
| 737 | return -EINVAL; |
| 738 | |
Magnus Karlsson | e6762c8 | 2019-02-08 14:13:50 +0100 | [diff] [blame] | 739 | /* Matches the smp_wmb() in xsk_init_queue */ |
| 740 | smp_rmb(); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 741 | qpg = virt_to_head_page(q->ring); |
| 742 | if (size > (PAGE_SIZE << compound_order(qpg))) |
| 743 | return -EINVAL; |
| 744 | |
| 745 | pfn = virt_to_phys(q->ring) >> PAGE_SHIFT; |
| 746 | return remap_pfn_range(vma, vma->vm_start, pfn, |
| 747 | size, vma->vm_page_prot); |
| 748 | } |
| 749 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 750 | static int xsk_notifier(struct notifier_block *this, |
| 751 | unsigned long msg, void *ptr) |
| 752 | { |
| 753 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 754 | struct net *net = dev_net(dev); |
| 755 | struct sock *sk; |
| 756 | |
| 757 | switch (msg) { |
| 758 | case NETDEV_UNREGISTER: |
| 759 | mutex_lock(&net->xdp.lock); |
| 760 | sk_for_each(sk, &net->xdp.list) { |
| 761 | struct xdp_sock *xs = xdp_sk(sk); |
| 762 | |
| 763 | mutex_lock(&xs->mutex); |
| 764 | if (xs->dev == dev) { |
| 765 | sk->sk_err = ENETDOWN; |
| 766 | if (!sock_flag(sk, SOCK_DEAD)) |
| 767 | sk->sk_error_report(sk); |
| 768 | |
| 769 | xsk_unbind_dev(xs); |
| 770 | |
| 771 | /* Clear device references in umem. */ |
| 772 | xdp_umem_clear_dev(xs->umem); |
| 773 | } |
| 774 | mutex_unlock(&xs->mutex); |
| 775 | } |
| 776 | mutex_unlock(&net->xdp.lock); |
| 777 | break; |
| 778 | } |
| 779 | return NOTIFY_DONE; |
| 780 | } |
| 781 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 782 | static struct proto xsk_proto = { |
| 783 | .name = "XDP", |
| 784 | .owner = THIS_MODULE, |
| 785 | .obj_size = sizeof(struct xdp_sock), |
| 786 | }; |
| 787 | |
| 788 | static const struct proto_ops xsk_proto_ops = { |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 789 | .family = PF_XDP, |
| 790 | .owner = THIS_MODULE, |
| 791 | .release = xsk_release, |
| 792 | .bind = xsk_bind, |
| 793 | .connect = sock_no_connect, |
| 794 | .socketpair = sock_no_socketpair, |
| 795 | .accept = sock_no_accept, |
| 796 | .getname = sock_no_getname, |
Linus Torvalds | a11e1d4 | 2018-06-28 09:43:44 -0700 | [diff] [blame] | 797 | .poll = xsk_poll, |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 798 | .ioctl = sock_no_ioctl, |
| 799 | .listen = sock_no_listen, |
| 800 | .shutdown = sock_no_shutdown, |
| 801 | .setsockopt = xsk_setsockopt, |
| 802 | .getsockopt = xsk_getsockopt, |
| 803 | .sendmsg = xsk_sendmsg, |
| 804 | .recvmsg = sock_no_recvmsg, |
| 805 | .mmap = xsk_mmap, |
| 806 | .sendpage = sock_no_sendpage, |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 807 | }; |
| 808 | |
Björn Töpel | 11fe926 | 2019-02-21 13:07:38 +0100 | [diff] [blame] | 809 | static void xsk_destruct(struct sock *sk) |
| 810 | { |
| 811 | struct xdp_sock *xs = xdp_sk(sk); |
| 812 | |
| 813 | if (!sock_flag(sk, SOCK_DEAD)) |
| 814 | return; |
| 815 | |
| 816 | xdp_put_umem(xs->umem); |
| 817 | |
| 818 | sk_refcnt_debug_dec(sk); |
| 819 | } |
| 820 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 821 | static int xsk_create(struct net *net, struct socket *sock, int protocol, |
| 822 | int kern) |
| 823 | { |
| 824 | struct sock *sk; |
| 825 | struct xdp_sock *xs; |
| 826 | |
| 827 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) |
| 828 | return -EPERM; |
| 829 | if (sock->type != SOCK_RAW) |
| 830 | return -ESOCKTNOSUPPORT; |
| 831 | |
| 832 | if (protocol) |
| 833 | return -EPROTONOSUPPORT; |
| 834 | |
| 835 | sock->state = SS_UNCONNECTED; |
| 836 | |
| 837 | sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern); |
| 838 | if (!sk) |
| 839 | return -ENOBUFS; |
| 840 | |
| 841 | sock->ops = &xsk_proto_ops; |
| 842 | |
| 843 | sock_init_data(sock, sk); |
| 844 | |
| 845 | sk->sk_family = PF_XDP; |
| 846 | |
Björn Töpel | 11fe926 | 2019-02-21 13:07:38 +0100 | [diff] [blame] | 847 | sk->sk_destruct = xsk_destruct; |
| 848 | sk_refcnt_debug_inc(sk); |
| 849 | |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 850 | sock_set_flag(sk, SOCK_RCU_FREE); |
| 851 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 852 | xs = xdp_sk(sk); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 853 | xs->state = XSK_READY; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 854 | mutex_init(&xs->mutex); |
Ilya Maximets | bf0bdd1 | 2019-07-03 15:09:16 +0300 | [diff] [blame] | 855 | spin_lock_init(&xs->rx_lock); |
Magnus Karlsson | a9744f7 | 2018-06-29 09:48:20 +0200 | [diff] [blame] | 856 | spin_lock_init(&xs->tx_completion_lock); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 857 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 858 | mutex_lock(&net->xdp.lock); |
| 859 | sk_add_node_rcu(sk, &net->xdp.list); |
| 860 | mutex_unlock(&net->xdp.lock); |
| 861 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 862 | local_bh_disable(); |
| 863 | sock_prot_inuse_add(net, &xsk_proto, 1); |
| 864 | local_bh_enable(); |
| 865 | |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | static const struct net_proto_family xsk_family_ops = { |
| 870 | .family = PF_XDP, |
| 871 | .create = xsk_create, |
| 872 | .owner = THIS_MODULE, |
| 873 | }; |
| 874 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 875 | static struct notifier_block xsk_netdev_notifier = { |
| 876 | .notifier_call = xsk_notifier, |
| 877 | }; |
| 878 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 879 | static int __net_init xsk_net_init(struct net *net) |
| 880 | { |
| 881 | mutex_init(&net->xdp.lock); |
| 882 | INIT_HLIST_HEAD(&net->xdp.list); |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static void __net_exit xsk_net_exit(struct net *net) |
| 887 | { |
| 888 | WARN_ON_ONCE(!hlist_empty(&net->xdp.list)); |
| 889 | } |
| 890 | |
| 891 | static struct pernet_operations xsk_net_ops = { |
| 892 | .init = xsk_net_init, |
| 893 | .exit = xsk_net_exit, |
| 894 | }; |
| 895 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 896 | static int __init xsk_init(void) |
| 897 | { |
| 898 | int err; |
| 899 | |
| 900 | err = proto_register(&xsk_proto, 0 /* no slab */); |
| 901 | if (err) |
| 902 | goto out; |
| 903 | |
| 904 | err = sock_register(&xsk_family_ops); |
| 905 | if (err) |
| 906 | goto out_proto; |
| 907 | |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 908 | err = register_pernet_subsys(&xsk_net_ops); |
| 909 | if (err) |
| 910 | goto out_sk; |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 911 | |
| 912 | err = register_netdevice_notifier(&xsk_netdev_notifier); |
| 913 | if (err) |
| 914 | goto out_pernet; |
| 915 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 916 | return 0; |
| 917 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 918 | out_pernet: |
| 919 | unregister_pernet_subsys(&xsk_net_ops); |
Björn Töpel | 1d0dc06 | 2019-01-24 19:59:37 +0100 | [diff] [blame] | 920 | out_sk: |
| 921 | sock_unregister(PF_XDP); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 922 | out_proto: |
| 923 | proto_unregister(&xsk_proto); |
| 924 | out: |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | fs_initcall(xsk_init); |