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