Lines Matching refs:tfile
224 struct tun_file *tfile = container_of(napi, struct tun_file, napi); in tun_napi_receive() local
225 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_napi_receive()
262 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, in tun_napi_init() argument
265 tfile->napi_enabled = napi_en; in tun_napi_init()
266 tfile->napi_frags_enabled = napi_en && napi_frags; in tun_napi_init()
268 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, in tun_napi_init()
270 napi_enable(&tfile->napi); in tun_napi_init()
274 static void tun_napi_disable(struct tun_file *tfile) in tun_napi_disable() argument
276 if (tfile->napi_enabled) in tun_napi_disable()
277 napi_disable(&tfile->napi); in tun_napi_disable()
280 static void tun_napi_del(struct tun_file *tfile) in tun_napi_del() argument
282 if (tfile->napi_enabled) in tun_napi_del()
283 netif_napi_del(&tfile->napi); in tun_napi_del()
286 static bool tun_napi_frags_enabled(const struct tun_file *tfile) in tun_napi_frags_enabled() argument
288 return tfile->napi_frags_enabled; in tun_napi_frags_enabled()
466 struct tun_file *tfile) in tun_flow_update() argument
471 u16 queue_index = tfile->queue_index; in tun_flow_update()
585 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
587 tfile->detached = tun; in tun_disable_queue()
588 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
592 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) in tun_enable_queue() argument
594 struct tun_struct *tun = tfile->detached; in tun_enable_queue()
596 tfile->detached = NULL; in tun_enable_queue()
597 list_del_init(&tfile->next); in tun_enable_queue()
616 static void tun_queue_purge(struct tun_file *tfile) in tun_queue_purge() argument
620 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) in tun_queue_purge()
623 skb_queue_purge(&tfile->sk.sk_write_queue); in tun_queue_purge()
624 skb_queue_purge(&tfile->sk.sk_error_queue); in tun_queue_purge()
627 static void __tun_detach(struct tun_file *tfile, bool clean) in __tun_detach() argument
632 tun = rtnl_dereference(tfile->tun); in __tun_detach()
635 tun_napi_disable(tfile); in __tun_detach()
636 tun_napi_del(tfile); in __tun_detach()
639 if (tun && !tfile->detached) { in __tun_detach()
640 u16 index = tfile->queue_index; in __tun_detach()
652 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
653 sock_put(&tfile->sk); in __tun_detach()
655 tun_disable_queue(tun, tfile); in __tun_detach()
660 tun_queue_purge(tfile); in __tun_detach()
662 } else if (tfile->detached && clean) { in __tun_detach()
663 tun = tun_enable_queue(tfile); in __tun_detach()
664 sock_put(&tfile->sk); in __tun_detach()
676 xdp_rxq_info_unreg(&tfile->xdp_rxq); in __tun_detach()
677 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); in __tun_detach()
678 sock_put(&tfile->sk); in __tun_detach()
682 static void tun_detach(struct tun_file *tfile, bool clean) in tun_detach() argument
688 tun = rtnl_dereference(tfile->tun); in tun_detach()
690 __tun_detach(tfile, clean); in tun_detach()
699 struct tun_file *tfile, *tmp; in tun_detach_all() local
703 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
704 BUG_ON(!tfile); in tun_detach_all()
705 tun_napi_disable(tfile); in tun_detach_all()
706 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
707 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
708 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
711 list_for_each_entry(tfile, &tun->disabled, next) { in tun_detach_all()
712 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
713 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
714 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
720 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
721 tun_napi_del(tfile); in tun_detach_all()
723 tun_queue_purge(tfile); in tun_detach_all()
724 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
725 sock_put(&tfile->sk); in tun_detach_all()
727 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { in tun_detach_all()
728 tun_enable_queue(tfile); in tun_detach_all()
729 tun_queue_purge(tfile); in tun_detach_all()
730 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
731 sock_put(&tfile->sk); in tun_detach_all()
743 struct tun_file *tfile = file->private_data; in tun_attach() local
747 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
752 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
760 if (!tfile->detached && in tun_attach()
768 lock_sock(tfile->socket.sk); in tun_attach()
769 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
770 release_sock(tfile->socket.sk); in tun_attach()
775 if (!tfile->detached && in tun_attach()
776 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, in tun_attach()
782 tfile->queue_index = tun->numqueues; in tun_attach()
783 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; in tun_attach()
785 if (tfile->detached) { in tun_attach()
787 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); in tun_attach()
789 if (tfile->xdp_rxq.queue_index != tfile->queue_index) in tun_attach()
790 tfile->xdp_rxq.queue_index = tfile->queue_index; in tun_attach()
793 err = xdp_rxq_info_reg(&tfile->xdp_rxq, in tun_attach()
794 tun->dev, tfile->queue_index); in tun_attach()
797 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, in tun_attach()
800 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_attach()
806 if (tfile->detached) { in tun_attach()
807 tun_enable_queue(tfile); in tun_attach()
809 sock_hold(&tfile->sk); in tun_attach()
810 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
814 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_attach()
825 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
826 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
833 static struct tun_struct *tun_get(struct tun_file *tfile) in tun_get() argument
838 tun = rcu_dereference(tfile->tun); in tun_get()
1016 struct tun_file *tfile; in tun_net_xmit() local
1020 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1023 if (!tfile) in tun_net_xmit()
1037 if (tfile->socket.sk->sk_filter && in tun_net_xmit()
1038 sk_filter(tfile->socket.sk, skb)) in tun_net_xmit()
1057 if (ptr_ring_produce(&tfile->tx_ring, skb)) in tun_net_xmit()
1061 if (tfile->flags & TUN_FASYNC) in tun_net_xmit()
1062 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in tun_net_xmit()
1063 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_net_xmit()
1143 struct tun_file *tfile; in tun_xdp_set() local
1153 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1155 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1157 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1159 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1161 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1163 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1206 static void __tun_xdp_flush_tfile(struct tun_file *tfile) in __tun_xdp_flush_tfile() argument
1209 if (tfile->flags & TUN_FASYNC) in __tun_xdp_flush_tfile()
1210 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in __tun_xdp_flush_tfile()
1211 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in __tun_xdp_flush_tfile()
1218 struct tun_file *tfile; in tun_xdp_xmit() local
1236 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1238 if (unlikely(!tfile)) in tun_xdp_xmit()
1241 spin_lock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1249 if (__ptr_ring_produce(&tfile->tx_ring, frame)) { in tun_xdp_xmit()
1255 spin_unlock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1258 __tun_xdp_flush_tfile(tfile); in tun_xdp_xmit()
1350 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1352 struct sock *sk = tfile->socket.sk; in tun_sock_writeable()
1362 struct tun_file *tfile = file->private_data; in tun_chr_poll() local
1363 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll()
1370 sk = tfile->socket.sk; in tun_chr_poll()
1374 if (!ptr_ring_empty(&tfile->tx_ring)) in tun_chr_poll()
1382 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1384 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1394 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, in tun_napi_alloc_frags() argument
1407 skb = napi_get_frags(&tfile->napi); in tun_napi_alloc_frags()
1443 napi_free_frags(&tfile->napi); in tun_napi_alloc_frags()
1449 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, in tun_alloc_skb() argument
1453 struct sock *sk = tfile->socket.sk; in tun_alloc_skb()
1474 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1477 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_rx_batched()
1484 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1505 skb_record_rx_queue(nskb, tfile->queue_index); in tun_rx_batched()
1508 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1514 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1520 if (tfile->socket.sk->sk_sndbuf != INT_MAX) in tun_can_build_skb()
1536 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, in __tun_build_skb() argument
1547 skb_set_owner_w(skb, tfile->socket.sk); in __tun_build_skb()
1588 struct tun_file *tfile, in tun_build_skb() argument
1625 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, in tun_build_skb()
1642 xdp.rxq = &tfile->xdp_rxq; in tun_build_skb()
1668 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); in tun_build_skb()
1677 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1693 bool frags = tun_napi_frags_enabled(tfile); in tun_get_user()
1748 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1753 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1770 mutex_lock(&tfile->napi_mutex); in tun_get_user()
1771 skb = tun_napi_alloc_frags(tfile, copylen, from); in tun_get_user()
1778 skb = tun_alloc_skb(tfile, align, copylen, linear, in tun_get_user()
1786 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1801 tfile->napi.skb = NULL; in tun_get_user()
1802 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1813 tfile->napi.skb = NULL; in tun_get_user()
1814 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1864 skb_record_rx_queue(skb, tfile->queue_index); in tun_get_user()
1879 tfile->napi.skb = NULL; in tun_get_user()
1880 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1894 !tfile->detached) in tun_get_user()
1914 napi_free_frags(&tfile->napi); in tun_get_user()
1916 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1922 napi_gro_frags(&tfile->napi); in tun_get_user()
1924 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1925 } else if (tfile->napi_enabled) { in tun_get_user()
1926 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_get_user()
1935 napi_schedule(&tfile->napi); in tun_get_user()
1939 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
1953 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
1961 struct tun_file *tfile = file->private_data; in tun_chr_write_iter() local
1962 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter()
1972 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
1979 struct tun_file *tfile, in tun_put_user_xdp() argument
2014 struct tun_file *tfile, in tun_put_user() argument
2107 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) in tun_ring_recv() argument
2113 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2121 add_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2125 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2132 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { in tun_ring_recv()
2141 remove_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2148 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2162 ptr = tun_ring_recv(tfile, noblock, &err); in tun_do_read()
2170 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2175 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2188 struct tun_file *tfile = file->private_data; in tun_chr_read_iter() local
2189 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter()
2199 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2349 struct tun_file *tfile; in tun_sock_write_space() local
2363 tfile = container_of(sk, struct tun_file, sk); in tun_sock_write_space()
2364 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); in tun_sock_write_space()
2374 struct tun_file *tfile, in tun_xdp_one() argument
2397 xdp->rxq = &tfile->xdp_rxq; in tun_xdp_one()
2448 skb_record_rx_queue(skb, tfile->queue_index); in tun_xdp_one()
2457 !tfile->detached) in tun_xdp_one()
2472 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2481 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_sendmsg() local
2482 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg()
2501 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2516 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2527 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_recvmsg() local
2528 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg()
2546 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2578 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_peek_len() local
2582 tun = tun_get(tfile); in tun_peek_len()
2586 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); in tun_peek_len()
2655 struct tun_file *tfile = file->private_data; in tun_set_iff() local
2659 if (tfile->detached) in tun_set_iff()
2747 dev->ifindex = tfile->ifindex; in tun_set_iff()
2758 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2800 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2882 struct tun_file *tfile; in tun_detach_filter() local
2885 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2886 lock_sock(tfile->socket.sk); in tun_detach_filter()
2887 sk_detach_filter(tfile->socket.sk); in tun_detach_filter()
2888 release_sock(tfile->socket.sk); in tun_detach_filter()
2897 struct tun_file *tfile; in tun_attach_filter() local
2900 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2901 lock_sock(tfile->socket.sk); in tun_attach_filter()
2902 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2903 release_sock(tfile->socket.sk); in tun_attach_filter()
2916 struct tun_file *tfile; in tun_set_sndbuf() local
2920 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2921 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2927 struct tun_file *tfile = file->private_data; in tun_set_queue() local
2934 tun = tfile->detached; in tun_set_queue()
2945 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2946 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2949 __tun_detach(tfile, false); in tun_set_queue()
2984 struct tun_file *tfile = file->private_data; in __tun_chr_ioctl() local
2985 struct net *net = sock_net(&tfile->sk); in __tun_chr_ioctl()
3023 tun = tun_get(tfile); in __tun_chr_ioctl()
3050 tfile->ifindex = ifindex; in __tun_chr_ioctl()
3066 if (tfile->detached) in __tun_chr_ioctl()
3068 if (!tfile->socket.sk->sk_filter) in __tun_chr_ioctl()
3172 sndbuf = tfile->socket.sk->sk_sndbuf; in __tun_chr_ioctl()
3341 struct tun_file *tfile = file->private_data; in tun_chr_fasync() local
3344 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) in tun_chr_fasync()
3349 tfile->flags |= TUN_FASYNC; in tun_chr_fasync()
3351 tfile->flags &= ~TUN_FASYNC; in tun_chr_fasync()
3360 struct tun_file *tfile; in tun_chr_open() local
3362 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, in tun_chr_open()
3364 if (!tfile) in tun_chr_open()
3366 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { in tun_chr_open()
3367 sk_free(&tfile->sk); in tun_chr_open()
3371 mutex_init(&tfile->napi_mutex); in tun_chr_open()
3372 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3373 tfile->flags = 0; in tun_chr_open()
3374 tfile->ifindex = 0; in tun_chr_open()
3376 init_waitqueue_head(&tfile->socket.wq.wait); in tun_chr_open()
3378 tfile->socket.file = file; in tun_chr_open()
3379 tfile->socket.ops = &tun_socket_ops; in tun_chr_open()
3381 sock_init_data(&tfile->socket, &tfile->sk); in tun_chr_open()
3383 tfile->sk.sk_write_space = tun_sock_write_space; in tun_chr_open()
3384 tfile->sk.sk_sndbuf = INT_MAX; in tun_chr_open()
3386 file->private_data = tfile; in tun_chr_open()
3387 INIT_LIST_HEAD(&tfile->next); in tun_chr_open()
3389 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); in tun_chr_open()
3396 struct tun_file *tfile = file->private_data; in tun_chr_close() local
3398 tun_detach(tfile, true); in tun_chr_close()
3406 struct tun_file *tfile = file->private_data; in tun_chr_show_fdinfo() local
3413 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3552 struct tun_file *tfile; in tun_queue_resize() local
3562 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3563 rings[i] = &tfile->tx_ring; in tun_queue_resize()
3565 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3566 rings[i++] = &tfile->tx_ring; in tun_queue_resize()
3593 struct tun_file *tfile; in tun_device_event() local
3595 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()
3596 tfile->socket.sk->sk_write_space(tfile->socket.sk); in tun_device_event()
3657 struct tun_file *tfile; in tun_get_socket() local
3660 tfile = file->private_data; in tun_get_socket()
3661 if (!tfile) in tun_get_socket()
3663 return &tfile->socket; in tun_get_socket()
3669 struct tun_file *tfile; in tun_get_tx_ring() local
3673 tfile = file->private_data; in tun_get_tx_ring()
3674 if (!tfile) in tun_get_tx_ring()
3676 return &tfile->tx_ring; in tun_get_tx_ring()