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> |
| 24 | #include <net/xdp_sock.h> |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 25 | #include <net/xdp.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 26 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 27 | #include "xsk_queue.h" |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 28 | #include "xdp_umem.h" |
| 29 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 30 | #define TX_BATCH_SIZE 16 |
| 31 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 32 | static struct xdp_sock *xdp_sk(struct sock *sk) |
| 33 | { |
| 34 | return (struct xdp_sock *)sk; |
| 35 | } |
| 36 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 37 | bool xsk_is_setup_for_bpf_map(struct xdp_sock *xs) |
| 38 | { |
| 39 | return !!xs->rx; |
| 40 | } |
| 41 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 42 | static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 43 | { |
| 44 | u32 *id, len = xdp->data_end - xdp->data; |
| 45 | void *buffer; |
| 46 | int err = 0; |
| 47 | |
| 48 | if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index) |
| 49 | return -EINVAL; |
| 50 | |
| 51 | id = xskq_peek_id(xs->umem->fq); |
| 52 | if (!id) |
| 53 | return -ENOSPC; |
| 54 | |
| 55 | buffer = xdp_umem_get_data_with_headroom(xs->umem, *id); |
| 56 | memcpy(buffer, xdp->data, len); |
| 57 | err = xskq_produce_batch_desc(xs->rx, *id, len, |
| 58 | xs->umem->frame_headroom); |
| 59 | if (!err) |
| 60 | xskq_discard_id(xs->umem->fq); |
| 61 | |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 66 | { |
| 67 | int err; |
| 68 | |
| 69 | err = __xsk_rcv(xs, xdp); |
| 70 | if (likely(!err)) |
| 71 | xdp_return_buff(xdp); |
| 72 | else |
| 73 | xs->rx_dropped++; |
| 74 | |
| 75 | return err; |
| 76 | } |
| 77 | |
| 78 | void xsk_flush(struct xdp_sock *xs) |
| 79 | { |
| 80 | xskq_produce_flush_desc(xs->rx); |
| 81 | xs->sk.sk_data_ready(&xs->sk); |
| 82 | } |
| 83 | |
| 84 | int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp) |
| 85 | { |
| 86 | int err; |
| 87 | |
| 88 | err = __xsk_rcv(xs, xdp); |
| 89 | if (!err) |
| 90 | xsk_flush(xs); |
| 91 | else |
| 92 | xs->rx_dropped++; |
| 93 | |
| 94 | return err; |
| 95 | } |
| 96 | |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 97 | static void xsk_destruct_skb(struct sk_buff *skb) |
| 98 | { |
| 99 | u32 id = (u32)(long)skb_shinfo(skb)->destructor_arg; |
| 100 | struct xdp_sock *xs = xdp_sk(skb->sk); |
| 101 | |
| 102 | WARN_ON_ONCE(xskq_produce_id(xs->umem->cq, id)); |
| 103 | |
| 104 | sock_wfree(skb); |
| 105 | } |
| 106 | |
| 107 | static int xsk_generic_xmit(struct sock *sk, struct msghdr *m, |
| 108 | size_t total_len) |
| 109 | { |
| 110 | bool need_wait = !(m->msg_flags & MSG_DONTWAIT); |
| 111 | u32 max_batch = TX_BATCH_SIZE; |
| 112 | struct xdp_sock *xs = xdp_sk(sk); |
| 113 | bool sent_frame = false; |
| 114 | struct xdp_desc desc; |
| 115 | struct sk_buff *skb; |
| 116 | int err = 0; |
| 117 | |
| 118 | if (unlikely(!xs->tx)) |
| 119 | return -ENOBUFS; |
| 120 | if (need_wait) |
| 121 | return -EOPNOTSUPP; |
| 122 | |
| 123 | mutex_lock(&xs->mutex); |
| 124 | |
| 125 | while (xskq_peek_desc(xs->tx, &desc)) { |
| 126 | char *buffer; |
| 127 | u32 id, len; |
| 128 | |
| 129 | if (max_batch-- == 0) { |
| 130 | err = -EAGAIN; |
| 131 | goto out; |
| 132 | } |
| 133 | |
| 134 | if (xskq_reserve_id(xs->umem->cq)) { |
| 135 | err = -EAGAIN; |
| 136 | goto out; |
| 137 | } |
| 138 | |
| 139 | len = desc.len; |
| 140 | if (unlikely(len > xs->dev->mtu)) { |
| 141 | err = -EMSGSIZE; |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | skb = sock_alloc_send_skb(sk, len, !need_wait, &err); |
| 146 | if (unlikely(!skb)) { |
| 147 | err = -EAGAIN; |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | skb_put(skb, len); |
| 152 | id = desc.idx; |
| 153 | buffer = xdp_umem_get_data(xs->umem, id) + desc.offset; |
| 154 | err = skb_store_bits(skb, 0, buffer, len); |
| 155 | if (unlikely(err)) { |
| 156 | kfree_skb(skb); |
| 157 | goto out; |
| 158 | } |
| 159 | |
| 160 | skb->dev = xs->dev; |
| 161 | skb->priority = sk->sk_priority; |
| 162 | skb->mark = sk->sk_mark; |
| 163 | skb_shinfo(skb)->destructor_arg = (void *)(long)id; |
| 164 | skb->destructor = xsk_destruct_skb; |
| 165 | |
| 166 | err = dev_direct_xmit(skb, xs->queue_id); |
| 167 | /* Ignore NET_XMIT_CN as packet might have been sent */ |
| 168 | if (err == NET_XMIT_DROP || err == NETDEV_TX_BUSY) { |
| 169 | err = -EAGAIN; |
| 170 | /* SKB consumed by dev_direct_xmit() */ |
| 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | sent_frame = true; |
| 175 | xskq_discard_desc(xs->tx); |
| 176 | } |
| 177 | |
| 178 | out: |
| 179 | if (sent_frame) |
| 180 | sk->sk_write_space(sk); |
| 181 | |
| 182 | mutex_unlock(&xs->mutex); |
| 183 | return err; |
| 184 | } |
| 185 | |
| 186 | static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len) |
| 187 | { |
| 188 | struct sock *sk = sock->sk; |
| 189 | struct xdp_sock *xs = xdp_sk(sk); |
| 190 | |
| 191 | if (unlikely(!xs->dev)) |
| 192 | return -ENXIO; |
| 193 | if (unlikely(!(xs->dev->flags & IFF_UP))) |
| 194 | return -ENETDOWN; |
| 195 | |
| 196 | return xsk_generic_xmit(sk, m, total_len); |
| 197 | } |
| 198 | |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 199 | static unsigned int xsk_poll(struct file *file, struct socket *sock, |
| 200 | struct poll_table_struct *wait) |
| 201 | { |
| 202 | unsigned int mask = datagram_poll(file, sock, wait); |
| 203 | struct sock *sk = sock->sk; |
| 204 | struct xdp_sock *xs = xdp_sk(sk); |
| 205 | |
| 206 | if (xs->rx && !xskq_empty_desc(xs->rx)) |
| 207 | mask |= POLLIN | POLLRDNORM; |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 208 | if (xs->tx && !xskq_full_desc(xs->tx)) |
| 209 | mask |= POLLOUT | POLLWRNORM; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 210 | |
| 211 | return mask; |
| 212 | } |
| 213 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 214 | static int xsk_init_queue(u32 entries, struct xsk_queue **queue, |
| 215 | bool umem_queue) |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 216 | { |
| 217 | struct xsk_queue *q; |
| 218 | |
| 219 | if (entries == 0 || *queue || !is_power_of_2(entries)) |
| 220 | return -EINVAL; |
| 221 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 222 | q = xskq_create(entries, umem_queue); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 223 | if (!q) |
| 224 | return -ENOMEM; |
| 225 | |
| 226 | *queue = q; |
| 227 | return 0; |
| 228 | } |
| 229 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 230 | static int xsk_release(struct socket *sock) |
| 231 | { |
| 232 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 233 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 234 | struct net *net; |
| 235 | |
| 236 | if (!sk) |
| 237 | return 0; |
| 238 | |
| 239 | net = sock_net(sk); |
| 240 | |
| 241 | local_bh_disable(); |
| 242 | sock_prot_inuse_add(net, sk->sk_prot, -1); |
| 243 | local_bh_enable(); |
| 244 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 245 | if (xs->dev) { |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame^] | 246 | /* Wait for driver to stop using the xdp socket. */ |
| 247 | synchronize_net(); |
| 248 | dev_put(xs->dev); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 249 | xs->dev = NULL; |
| 250 | } |
| 251 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 252 | sock_orphan(sk); |
| 253 | sock->sk = NULL; |
| 254 | |
| 255 | sk_refcnt_debug_release(sk); |
| 256 | sock_put(sk); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 261 | static struct socket *xsk_lookup_xsk_from_fd(int fd) |
| 262 | { |
| 263 | struct socket *sock; |
| 264 | int err; |
| 265 | |
| 266 | sock = sockfd_lookup(fd, &err); |
| 267 | if (!sock) |
| 268 | return ERR_PTR(-ENOTSOCK); |
| 269 | |
| 270 | if (sock->sk->sk_family != PF_XDP) { |
| 271 | sockfd_put(sock); |
| 272 | return ERR_PTR(-ENOPROTOOPT); |
| 273 | } |
| 274 | |
| 275 | return sock; |
| 276 | } |
| 277 | |
| 278 | static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 279 | { |
| 280 | struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr; |
| 281 | struct sock *sk = sock->sk; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 282 | struct xdp_sock *xs = xdp_sk(sk); |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame^] | 283 | struct net_device *dev; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 284 | int err = 0; |
| 285 | |
| 286 | if (addr_len < sizeof(struct sockaddr_xdp)) |
| 287 | return -EINVAL; |
| 288 | if (sxdp->sxdp_family != AF_XDP) |
| 289 | return -EINVAL; |
| 290 | |
| 291 | mutex_lock(&xs->mutex); |
Björn Töpel | 959b71d | 2018-05-22 09:34:56 +0200 | [diff] [blame^] | 292 | if (xs->dev) { |
| 293 | err = -EBUSY; |
| 294 | goto out_release; |
| 295 | } |
| 296 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 297 | dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex); |
| 298 | if (!dev) { |
| 299 | err = -ENODEV; |
| 300 | goto out_release; |
| 301 | } |
| 302 | |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 303 | if (!xs->rx && !xs->tx) { |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 304 | err = -EINVAL; |
| 305 | goto out_unlock; |
| 306 | } |
| 307 | |
| 308 | if (sxdp->sxdp_queue_id >= dev->num_rx_queues) { |
| 309 | err = -EINVAL; |
| 310 | goto out_unlock; |
| 311 | } |
| 312 | |
| 313 | if (sxdp->sxdp_flags & XDP_SHARED_UMEM) { |
| 314 | struct xdp_sock *umem_xs; |
| 315 | struct socket *sock; |
| 316 | |
| 317 | if (xs->umem) { |
| 318 | /* We have already our own. */ |
| 319 | err = -EINVAL; |
| 320 | goto out_unlock; |
| 321 | } |
| 322 | |
| 323 | sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd); |
| 324 | if (IS_ERR(sock)) { |
| 325 | err = PTR_ERR(sock); |
| 326 | goto out_unlock; |
| 327 | } |
| 328 | |
| 329 | umem_xs = xdp_sk(sock->sk); |
| 330 | if (!umem_xs->umem) { |
| 331 | /* No umem to inherit. */ |
| 332 | err = -EBADF; |
| 333 | sockfd_put(sock); |
| 334 | goto out_unlock; |
| 335 | } else if (umem_xs->dev != dev || |
| 336 | umem_xs->queue_id != sxdp->sxdp_queue_id) { |
| 337 | err = -EINVAL; |
| 338 | sockfd_put(sock); |
| 339 | goto out_unlock; |
| 340 | } |
| 341 | |
| 342 | xdp_get_umem(umem_xs->umem); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 343 | xs->umem = umem_xs->umem; |
| 344 | sockfd_put(sock); |
| 345 | } else if (!xs->umem || !xdp_umem_validate_queues(xs->umem)) { |
| 346 | err = -EINVAL; |
| 347 | goto out_unlock; |
Björn Töpel | c497176 | 2018-05-02 13:01:27 +0200 | [diff] [blame] | 348 | } else { |
| 349 | /* This xsk has its own umem. */ |
| 350 | xskq_set_umem(xs->umem->fq, &xs->umem->props); |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 351 | xskq_set_umem(xs->umem->cq, &xs->umem->props); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 352 | } |
| 353 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 354 | xs->dev = dev; |
| 355 | xs->queue_id = sxdp->sxdp_queue_id; |
| 356 | |
| 357 | xskq_set_umem(xs->rx, &xs->umem->props); |
Magnus Karlsson | 35fcde7 | 2018-05-02 13:01:34 +0200 | [diff] [blame] | 358 | xskq_set_umem(xs->tx, &xs->umem->props); |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 359 | |
| 360 | out_unlock: |
| 361 | if (err) |
| 362 | dev_put(dev); |
| 363 | out_release: |
| 364 | mutex_unlock(&xs->mutex); |
| 365 | return err; |
| 366 | } |
| 367 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 368 | static int xsk_setsockopt(struct socket *sock, int level, int optname, |
| 369 | char __user *optval, unsigned int optlen) |
| 370 | { |
| 371 | struct sock *sk = sock->sk; |
| 372 | struct xdp_sock *xs = xdp_sk(sk); |
| 373 | int err; |
| 374 | |
| 375 | if (level != SOL_XDP) |
| 376 | return -ENOPROTOOPT; |
| 377 | |
| 378 | switch (optname) { |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 379 | case XDP_RX_RING: |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 380 | case XDP_TX_RING: |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 381 | { |
| 382 | struct xsk_queue **q; |
| 383 | int entries; |
| 384 | |
| 385 | if (optlen < sizeof(entries)) |
| 386 | return -EINVAL; |
| 387 | if (copy_from_user(&entries, optval, sizeof(entries))) |
| 388 | return -EFAULT; |
| 389 | |
| 390 | mutex_lock(&xs->mutex); |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 391 | q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 392 | err = xsk_init_queue(entries, q, false); |
| 393 | mutex_unlock(&xs->mutex); |
| 394 | return err; |
| 395 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 396 | case XDP_UMEM_REG: |
| 397 | { |
| 398 | struct xdp_umem_reg mr; |
| 399 | struct xdp_umem *umem; |
| 400 | |
| 401 | if (xs->umem) |
| 402 | return -EBUSY; |
| 403 | |
| 404 | if (copy_from_user(&mr, optval, sizeof(mr))) |
| 405 | return -EFAULT; |
| 406 | |
| 407 | mutex_lock(&xs->mutex); |
| 408 | err = xdp_umem_create(&umem); |
| 409 | |
| 410 | err = xdp_umem_reg(umem, &mr); |
| 411 | if (err) { |
| 412 | kfree(umem); |
| 413 | mutex_unlock(&xs->mutex); |
| 414 | return err; |
| 415 | } |
| 416 | |
| 417 | /* Make sure umem is ready before it can be seen by others */ |
| 418 | smp_wmb(); |
| 419 | |
| 420 | xs->umem = umem; |
| 421 | mutex_unlock(&xs->mutex); |
| 422 | return 0; |
| 423 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 424 | case XDP_UMEM_FILL_RING: |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 425 | case XDP_UMEM_COMPLETION_RING: |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 426 | { |
| 427 | struct xsk_queue **q; |
| 428 | int entries; |
| 429 | |
| 430 | if (!xs->umem) |
| 431 | return -EINVAL; |
| 432 | |
| 433 | if (copy_from_user(&entries, optval, sizeof(entries))) |
| 434 | return -EFAULT; |
| 435 | |
| 436 | mutex_lock(&xs->mutex); |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 437 | q = (optname == XDP_UMEM_FILL_RING) ? &xs->umem->fq : |
| 438 | &xs->umem->cq; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 439 | err = xsk_init_queue(entries, q, true); |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 440 | mutex_unlock(&xs->mutex); |
| 441 | return err; |
| 442 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 443 | default: |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | return -ENOPROTOOPT; |
| 448 | } |
| 449 | |
Magnus Karlsson | af75d9e | 2018-05-02 13:01:35 +0200 | [diff] [blame] | 450 | static int xsk_getsockopt(struct socket *sock, int level, int optname, |
| 451 | char __user *optval, int __user *optlen) |
| 452 | { |
| 453 | struct sock *sk = sock->sk; |
| 454 | struct xdp_sock *xs = xdp_sk(sk); |
| 455 | int len; |
| 456 | |
| 457 | if (level != SOL_XDP) |
| 458 | return -ENOPROTOOPT; |
| 459 | |
| 460 | if (get_user(len, optlen)) |
| 461 | return -EFAULT; |
| 462 | if (len < 0) |
| 463 | return -EINVAL; |
| 464 | |
| 465 | switch (optname) { |
| 466 | case XDP_STATISTICS: |
| 467 | { |
| 468 | struct xdp_statistics stats; |
| 469 | |
| 470 | if (len < sizeof(stats)) |
| 471 | return -EINVAL; |
| 472 | |
| 473 | mutex_lock(&xs->mutex); |
| 474 | stats.rx_dropped = xs->rx_dropped; |
| 475 | stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx); |
| 476 | stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx); |
| 477 | mutex_unlock(&xs->mutex); |
| 478 | |
| 479 | if (copy_to_user(optval, &stats, sizeof(stats))) |
| 480 | return -EFAULT; |
| 481 | if (put_user(sizeof(stats), optlen)) |
| 482 | return -EFAULT; |
| 483 | |
| 484 | return 0; |
| 485 | } |
| 486 | default: |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | return -EOPNOTSUPP; |
| 491 | } |
| 492 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 493 | static int xsk_mmap(struct file *file, struct socket *sock, |
| 494 | struct vm_area_struct *vma) |
| 495 | { |
| 496 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
| 497 | unsigned long size = vma->vm_end - vma->vm_start; |
| 498 | struct xdp_sock *xs = xdp_sk(sock->sk); |
| 499 | struct xsk_queue *q = NULL; |
| 500 | unsigned long pfn; |
| 501 | struct page *qpg; |
| 502 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 503 | if (offset == XDP_PGOFF_RX_RING) { |
| 504 | q = xs->rx; |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 505 | } else if (offset == XDP_PGOFF_TX_RING) { |
| 506 | q = xs->tx; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 507 | } else { |
| 508 | if (!xs->umem) |
| 509 | return -EINVAL; |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 510 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 511 | if (offset == XDP_UMEM_PGOFF_FILL_RING) |
| 512 | q = xs->umem->fq; |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 513 | else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING) |
| 514 | q = xs->umem->cq; |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 515 | } |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 516 | |
| 517 | if (!q) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | qpg = virt_to_head_page(q->ring); |
| 521 | if (size > (PAGE_SIZE << compound_order(qpg))) |
| 522 | return -EINVAL; |
| 523 | |
| 524 | pfn = virt_to_phys(q->ring) >> PAGE_SHIFT; |
| 525 | return remap_pfn_range(vma, vma->vm_start, pfn, |
| 526 | size, vma->vm_page_prot); |
| 527 | } |
| 528 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 529 | static struct proto xsk_proto = { |
| 530 | .name = "XDP", |
| 531 | .owner = THIS_MODULE, |
| 532 | .obj_size = sizeof(struct xdp_sock), |
| 533 | }; |
| 534 | |
| 535 | static const struct proto_ops xsk_proto_ops = { |
Björn Töpel | c2f4374 | 2018-05-18 14:00:24 +0200 | [diff] [blame] | 536 | .family = PF_XDP, |
| 537 | .owner = THIS_MODULE, |
| 538 | .release = xsk_release, |
| 539 | .bind = xsk_bind, |
| 540 | .connect = sock_no_connect, |
| 541 | .socketpair = sock_no_socketpair, |
| 542 | .accept = sock_no_accept, |
| 543 | .getname = sock_no_getname, |
| 544 | .poll = xsk_poll, |
| 545 | .ioctl = sock_no_ioctl, |
| 546 | .listen = sock_no_listen, |
| 547 | .shutdown = sock_no_shutdown, |
| 548 | .setsockopt = xsk_setsockopt, |
| 549 | .getsockopt = xsk_getsockopt, |
| 550 | .sendmsg = xsk_sendmsg, |
| 551 | .recvmsg = sock_no_recvmsg, |
| 552 | .mmap = xsk_mmap, |
| 553 | .sendpage = sock_no_sendpage, |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 554 | }; |
| 555 | |
| 556 | static void xsk_destruct(struct sock *sk) |
| 557 | { |
| 558 | struct xdp_sock *xs = xdp_sk(sk); |
| 559 | |
| 560 | if (!sock_flag(sk, SOCK_DEAD)) |
| 561 | return; |
| 562 | |
Björn Töpel | b9b6b68 | 2018-05-02 13:01:25 +0200 | [diff] [blame] | 563 | xskq_destroy(xs->rx); |
Magnus Karlsson | f614590 | 2018-05-02 13:01:32 +0200 | [diff] [blame] | 564 | xskq_destroy(xs->tx); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 565 | xdp_put_umem(xs->umem); |
| 566 | |
| 567 | sk_refcnt_debug_dec(sk); |
| 568 | } |
| 569 | |
| 570 | static int xsk_create(struct net *net, struct socket *sock, int protocol, |
| 571 | int kern) |
| 572 | { |
| 573 | struct sock *sk; |
| 574 | struct xdp_sock *xs; |
| 575 | |
| 576 | if (!ns_capable(net->user_ns, CAP_NET_RAW)) |
| 577 | return -EPERM; |
| 578 | if (sock->type != SOCK_RAW) |
| 579 | return -ESOCKTNOSUPPORT; |
| 580 | |
| 581 | if (protocol) |
| 582 | return -EPROTONOSUPPORT; |
| 583 | |
| 584 | sock->state = SS_UNCONNECTED; |
| 585 | |
| 586 | sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern); |
| 587 | if (!sk) |
| 588 | return -ENOBUFS; |
| 589 | |
| 590 | sock->ops = &xsk_proto_ops; |
| 591 | |
| 592 | sock_init_data(sock, sk); |
| 593 | |
| 594 | sk->sk_family = PF_XDP; |
| 595 | |
| 596 | sk->sk_destruct = xsk_destruct; |
| 597 | sk_refcnt_debug_inc(sk); |
| 598 | |
| 599 | xs = xdp_sk(sk); |
| 600 | mutex_init(&xs->mutex); |
| 601 | |
| 602 | local_bh_disable(); |
| 603 | sock_prot_inuse_add(net, &xsk_proto, 1); |
| 604 | local_bh_enable(); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | static const struct net_proto_family xsk_family_ops = { |
| 610 | .family = PF_XDP, |
| 611 | .create = xsk_create, |
| 612 | .owner = THIS_MODULE, |
| 613 | }; |
| 614 | |
| 615 | static int __init xsk_init(void) |
| 616 | { |
| 617 | int err; |
| 618 | |
| 619 | err = proto_register(&xsk_proto, 0 /* no slab */); |
| 620 | if (err) |
| 621 | goto out; |
| 622 | |
| 623 | err = sock_register(&xsk_family_ops); |
| 624 | if (err) |
| 625 | goto out_proto; |
| 626 | |
| 627 | return 0; |
| 628 | |
| 629 | out_proto: |
| 630 | proto_unregister(&xsk_proto); |
| 631 | out: |
| 632 | return err; |
| 633 | } |
| 634 | |
| 635 | fs_initcall(xsk_init); |