Lines Matching refs:tfile
227 struct tun_file *tfile = container_of(napi, struct tun_file, napi); in tun_napi_receive() local
228 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_napi_receive()
265 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, in tun_napi_init() argument
268 tfile->napi_enabled = napi_en; in tun_napi_init()
269 tfile->napi_frags_enabled = napi_en && napi_frags; in tun_napi_init()
271 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, in tun_napi_init()
273 napi_enable(&tfile->napi); in tun_napi_init()
277 static void tun_napi_enable(struct tun_file *tfile) in tun_napi_enable() argument
279 if (tfile->napi_enabled) in tun_napi_enable()
280 napi_enable(&tfile->napi); in tun_napi_enable()
283 static void tun_napi_disable(struct tun_file *tfile) in tun_napi_disable() argument
285 if (tfile->napi_enabled) in tun_napi_disable()
286 napi_disable(&tfile->napi); in tun_napi_disable()
289 static void tun_napi_del(struct tun_file *tfile) in tun_napi_del() argument
291 if (tfile->napi_enabled) in tun_napi_del()
292 netif_napi_del(&tfile->napi); in tun_napi_del()
295 static bool tun_napi_frags_enabled(const struct tun_file *tfile) in tun_napi_frags_enabled() argument
297 return tfile->napi_frags_enabled; in tun_napi_frags_enabled()
475 struct tun_file *tfile) in tun_flow_update() argument
480 u16 queue_index = tfile->queue_index; in tun_flow_update()
594 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
596 tfile->detached = tun; in tun_disable_queue()
597 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
601 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) in tun_enable_queue() argument
603 struct tun_struct *tun = tfile->detached; in tun_enable_queue()
605 tfile->detached = NULL; in tun_enable_queue()
606 list_del_init(&tfile->next); in tun_enable_queue()
625 static void tun_queue_purge(struct tun_file *tfile) in tun_queue_purge() argument
629 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) in tun_queue_purge()
632 skb_queue_purge(&tfile->sk.sk_write_queue); in tun_queue_purge()
633 skb_queue_purge(&tfile->sk.sk_error_queue); in tun_queue_purge()
636 static void __tun_detach(struct tun_file *tfile, bool clean) in __tun_detach() argument
641 tun = rtnl_dereference(tfile->tun); in __tun_detach()
644 if (!tfile->detached) in __tun_detach()
645 tun_napi_disable(tfile); in __tun_detach()
646 tun_napi_del(tfile); in __tun_detach()
649 if (tun && !tfile->detached) { in __tun_detach()
650 u16 index = tfile->queue_index; in __tun_detach()
662 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
663 sock_put(&tfile->sk); in __tun_detach()
665 tun_disable_queue(tun, tfile); in __tun_detach()
666 tun_napi_disable(tfile); in __tun_detach()
672 tun_queue_purge(tfile); in __tun_detach()
674 } else if (tfile->detached && clean) { in __tun_detach()
675 tun = tun_enable_queue(tfile); in __tun_detach()
676 sock_put(&tfile->sk); in __tun_detach()
688 xdp_rxq_info_unreg(&tfile->xdp_rxq); in __tun_detach()
689 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); in __tun_detach()
693 static void tun_detach(struct tun_file *tfile, bool clean) in tun_detach() argument
699 tun = rtnl_dereference(tfile->tun); in tun_detach()
701 __tun_detach(tfile, clean); in tun_detach()
707 sock_put(&tfile->sk); in tun_detach()
713 struct tun_file *tfile, *tmp; in tun_detach_all() local
717 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
718 BUG_ON(!tfile); in tun_detach_all()
719 tun_napi_disable(tfile); in tun_detach_all()
720 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
721 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
722 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
725 list_for_each_entry(tfile, &tun->disabled, next) { in tun_detach_all()
726 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
727 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
728 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
734 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
735 tun_napi_del(tfile); in tun_detach_all()
737 tun_queue_purge(tfile); in tun_detach_all()
738 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
739 sock_put(&tfile->sk); in tun_detach_all()
741 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { in tun_detach_all()
742 tun_napi_del(tfile); in tun_detach_all()
743 tun_enable_queue(tfile); in tun_detach_all()
744 tun_queue_purge(tfile); in tun_detach_all()
745 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
746 sock_put(&tfile->sk); in tun_detach_all()
758 struct tun_file *tfile = file->private_data; in tun_attach() local
762 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
767 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
775 if (!tfile->detached && in tun_attach()
783 lock_sock(tfile->socket.sk); in tun_attach()
784 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
785 release_sock(tfile->socket.sk); in tun_attach()
790 if (!tfile->detached && in tun_attach()
791 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, in tun_attach()
797 tfile->queue_index = tun->numqueues; in tun_attach()
798 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; in tun_attach()
800 if (tfile->detached) { in tun_attach()
802 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); in tun_attach()
804 if (tfile->xdp_rxq.queue_index != tfile->queue_index) in tun_attach()
805 tfile->xdp_rxq.queue_index = tfile->queue_index; in tun_attach()
808 err = xdp_rxq_info_reg(&tfile->xdp_rxq, in tun_attach()
809 tun->dev, tfile->queue_index, 0); in tun_attach()
812 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, in tun_attach()
815 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_attach()
821 if (tfile->detached) { in tun_attach()
822 tun_enable_queue(tfile); in tun_attach()
823 tun_napi_enable(tfile); in tun_attach()
825 sock_hold(&tfile->sk); in tun_attach()
826 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
830 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_attach()
841 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
842 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
849 static struct tun_struct *tun_get(struct tun_file *tfile) in tun_get() argument
854 tun = rcu_dereference(tfile->tun); in tun_get()
1076 struct tun_file *tfile; in tun_net_xmit() local
1080 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1083 if (!tfile) in tun_net_xmit()
1097 if (tfile->socket.sk->sk_filter && in tun_net_xmit()
1098 sk_filter(tfile->socket.sk, skb)) in tun_net_xmit()
1120 if (ptr_ring_produce(&tfile->tx_ring, skb)) in tun_net_xmit()
1128 if (tfile->flags & TUN_FASYNC) in tun_net_xmit()
1129 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in tun_net_xmit()
1130 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_net_xmit()
1185 struct tun_file *tfile; in tun_xdp_set() local
1195 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1197 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1199 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1201 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1203 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1205 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1249 static void __tun_xdp_flush_tfile(struct tun_file *tfile) in __tun_xdp_flush_tfile() argument
1252 if (tfile->flags & TUN_FASYNC) in __tun_xdp_flush_tfile()
1253 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in __tun_xdp_flush_tfile()
1254 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in __tun_xdp_flush_tfile()
1261 struct tun_file *tfile; in tun_xdp_xmit() local
1278 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1280 if (unlikely(!tfile)) in tun_xdp_xmit()
1283 spin_lock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1291 if (__ptr_ring_produce(&tfile->tx_ring, frame)) { in tun_xdp_xmit()
1297 spin_unlock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1300 __tun_xdp_flush_tfile(tfile); in tun_xdp_xmit()
1397 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1399 struct sock *sk = tfile->socket.sk; in tun_sock_writeable()
1409 struct tun_file *tfile = file->private_data; in tun_chr_poll() local
1410 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll()
1417 sk = tfile->socket.sk; in tun_chr_poll()
1421 if (!ptr_ring_empty(&tfile->tx_ring)) in tun_chr_poll()
1429 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1431 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1441 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, in tun_napi_alloc_frags() argument
1455 skb = napi_get_frags(&tfile->napi); in tun_napi_alloc_frags()
1491 napi_free_frags(&tfile->napi); in tun_napi_alloc_frags()
1497 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, in tun_alloc_skb() argument
1501 struct sock *sk = tfile->socket.sk; in tun_alloc_skb()
1522 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1525 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_rx_batched()
1532 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1553 skb_record_rx_queue(nskb, tfile->queue_index); in tun_rx_batched()
1556 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1562 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1568 if (tfile->socket.sk->sk_sndbuf != INT_MAX) in tun_can_build_skb()
1584 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, in __tun_build_skb() argument
1595 skb_set_owner_w(skb, tfile->socket.sk); in __tun_build_skb()
1636 struct tun_file *tfile, in tun_build_skb() argument
1673 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, in tun_build_skb()
1686 xdp_init_buff(&xdp, buflen, &tfile->xdp_rxq); in tun_build_skb()
1712 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); in tun_build_skb()
1721 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1736 bool frags = tun_napi_frags_enabled(tfile); in tun_get_user()
1791 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1796 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1813 mutex_lock(&tfile->napi_mutex); in tun_get_user()
1814 skb = tun_napi_alloc_frags(tfile, copylen, from); in tun_get_user()
1821 skb = tun_alloc_skb(tfile, align, copylen, linear, in tun_get_user()
1829 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1844 tfile->napi.skb = NULL; in tun_get_user()
1845 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1856 tfile->napi.skb = NULL; in tun_get_user()
1857 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1905 skb_record_rx_queue(skb, tfile->queue_index); in tun_get_user()
1920 tfile->napi.skb = NULL; in tun_get_user()
1921 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1935 !tfile->detached) in tun_get_user()
1958 napi_free_frags(&tfile->napi); in tun_get_user()
1960 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1964 if (likely(napi_schedule_prep(&tfile->napi))) { in tun_get_user()
1966 napi_gro_frags(&tfile->napi); in tun_get_user()
1967 napi_complete(&tfile->napi); in tun_get_user()
1973 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1974 } else if (tfile->napi_enabled) { in tun_get_user()
1975 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_get_user()
1984 napi_schedule(&tfile->napi); in tun_get_user()
1988 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
1999 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
2007 struct tun_file *tfile = file->private_data; in tun_chr_write_iter() local
2008 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter()
2018 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
2025 struct tun_file *tfile, in tun_put_user_xdp() argument
2056 struct tun_file *tfile, in tun_put_user() argument
2145 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) in tun_ring_recv() argument
2151 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2159 add_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2163 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2170 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { in tun_ring_recv()
2179 remove_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2186 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2200 ptr = tun_ring_recv(tfile, noblock, &err); in tun_do_read()
2208 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2213 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2226 struct tun_file *tfile = file->private_data; in tun_chr_read_iter() local
2227 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter()
2237 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2382 struct tun_file *tfile; in tun_sock_write_space() local
2396 tfile = container_of(sk, struct tun_file, sk); in tun_sock_write_space()
2397 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); in tun_sock_write_space()
2407 struct tun_file *tfile, in tun_xdp_one() argument
2429 xdp_init_buff(xdp, buflen, &tfile->xdp_rxq); in tun_xdp_one()
2480 skb_record_rx_queue(skb, tfile->queue_index); in tun_xdp_one()
2489 !tfile->detached) in tun_xdp_one()
2500 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2509 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_sendmsg() local
2510 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg()
2530 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2545 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2556 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_recvmsg() local
2557 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg()
2575 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2607 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_peek_len() local
2611 tun = tun_get(tfile); in tun_peek_len()
2615 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); in tun_peek_len()
2684 struct tun_file *tfile = file->private_data; in tun_set_iff() local
2688 if (tfile->detached) in tun_set_iff()
2776 dev->ifindex = tfile->ifindex; in tun_set_iff()
2787 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2804 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2868 struct tun_file *tfile; in tun_detach_filter() local
2871 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2872 lock_sock(tfile->socket.sk); in tun_detach_filter()
2873 sk_detach_filter(tfile->socket.sk); in tun_detach_filter()
2874 release_sock(tfile->socket.sk); in tun_detach_filter()
2883 struct tun_file *tfile; in tun_attach_filter() local
2886 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2887 lock_sock(tfile->socket.sk); in tun_attach_filter()
2888 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2889 release_sock(tfile->socket.sk); in tun_attach_filter()
2902 struct tun_file *tfile; in tun_set_sndbuf() local
2906 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2907 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2913 struct tun_file *tfile = file->private_data; in tun_set_queue() local
2920 tun = tfile->detached; in tun_set_queue()
2931 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2932 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2935 __tun_detach(tfile, false); in tun_set_queue()
3009 struct tun_file *tfile = file->private_data; in __tun_chr_ioctl() local
3010 struct net *net = sock_net(&tfile->sk); in __tun_chr_ioctl()
3048 tun = tun_get(tfile); in __tun_chr_ioctl()
3077 tfile->ifindex = ifindex; in __tun_chr_ioctl()
3093 if (tfile->detached) in __tun_chr_ioctl()
3095 if (!tfile->socket.sk->sk_filter) in __tun_chr_ioctl()
3208 sndbuf = tfile->socket.sk->sk_sndbuf; in __tun_chr_ioctl()
3377 struct tun_file *tfile = file->private_data; in tun_chr_fasync() local
3380 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) in tun_chr_fasync()
3385 tfile->flags |= TUN_FASYNC; in tun_chr_fasync()
3387 tfile->flags &= ~TUN_FASYNC; in tun_chr_fasync()
3396 struct tun_file *tfile; in tun_chr_open() local
3398 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, in tun_chr_open()
3400 if (!tfile) in tun_chr_open()
3402 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { in tun_chr_open()
3403 sk_free(&tfile->sk); in tun_chr_open()
3407 mutex_init(&tfile->napi_mutex); in tun_chr_open()
3408 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3409 tfile->flags = 0; in tun_chr_open()
3410 tfile->ifindex = 0; in tun_chr_open()
3412 init_waitqueue_head(&tfile->socket.wq.wait); in tun_chr_open()
3414 tfile->socket.file = file; in tun_chr_open()
3415 tfile->socket.ops = &tun_socket_ops; in tun_chr_open()
3417 sock_init_data_uid(&tfile->socket, &tfile->sk, current_fsuid()); in tun_chr_open()
3419 tfile->sk.sk_write_space = tun_sock_write_space; in tun_chr_open()
3420 tfile->sk.sk_sndbuf = INT_MAX; in tun_chr_open()
3422 file->private_data = tfile; in tun_chr_open()
3423 INIT_LIST_HEAD(&tfile->next); in tun_chr_open()
3425 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); in tun_chr_open()
3432 struct tun_file *tfile = file->private_data; in tun_chr_close() local
3434 tun_detach(tfile, true); in tun_chr_close()
3442 struct tun_file *tfile = file->private_data; in tun_chr_show_fdinfo() local
3449 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3592 struct tun_file *tfile; in tun_queue_resize() local
3602 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3603 rings[i] = &tfile->tx_ring; in tun_queue_resize()
3605 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3606 rings[i++] = &tfile->tx_ring; in tun_queue_resize()
3633 struct tun_file *tfile; in tun_device_event() local
3635 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()
3636 tfile->socket.sk->sk_write_space(tfile->socket.sk); in tun_device_event()
3697 struct tun_file *tfile; in tun_get_socket() local
3700 tfile = file->private_data; in tun_get_socket()
3701 if (!tfile) in tun_get_socket()
3703 return &tfile->socket; in tun_get_socket()
3709 struct tun_file *tfile; in tun_get_tx_ring() local
3713 tfile = file->private_data; in tun_get_tx_ring()
3714 if (!tfile) in tun_get_tx_ring()
3716 return &tfile->tx_ring; in tun_get_tx_ring()