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