Lines Matching refs:tfile

238 	struct tun_file *tfile = container_of(napi, struct tun_file, napi);  in tun_napi_receive()  local
239 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_napi_receive()
276 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, in tun_napi_init() argument
279 tfile->napi_enabled = napi_en; in tun_napi_init()
280 tfile->napi_frags_enabled = napi_en && napi_frags; in tun_napi_init()
282 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, in tun_napi_init()
284 napi_enable(&tfile->napi); in tun_napi_init()
288 static void tun_napi_enable(struct tun_file *tfile) in tun_napi_enable() argument
290 if (tfile->napi_enabled) in tun_napi_enable()
291 napi_enable(&tfile->napi); in tun_napi_enable()
294 static void tun_napi_disable(struct tun_file *tfile) in tun_napi_disable() argument
296 if (tfile->napi_enabled) in tun_napi_disable()
297 napi_disable(&tfile->napi); in tun_napi_disable()
300 static void tun_napi_del(struct tun_file *tfile) in tun_napi_del() argument
302 if (tfile->napi_enabled) in tun_napi_del()
303 netif_napi_del(&tfile->napi); in tun_napi_del()
306 static bool tun_napi_frags_enabled(const struct tun_file *tfile) in tun_napi_frags_enabled() argument
308 return tfile->napi_frags_enabled; in tun_napi_frags_enabled()
486 struct tun_file *tfile) in tun_flow_update() argument
491 u16 queue_index = tfile->queue_index; in tun_flow_update()
605 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
607 tfile->detached = tun; in tun_disable_queue()
608 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
612 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) in tun_enable_queue() argument
614 struct tun_struct *tun = tfile->detached; in tun_enable_queue()
616 tfile->detached = NULL; in tun_enable_queue()
617 list_del_init(&tfile->next); in tun_enable_queue()
636 static void tun_queue_purge(struct tun_file *tfile) in tun_queue_purge() argument
640 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) in tun_queue_purge()
643 skb_queue_purge(&tfile->sk.sk_write_queue); in tun_queue_purge()
644 skb_queue_purge(&tfile->sk.sk_error_queue); in tun_queue_purge()
647 static void __tun_detach(struct tun_file *tfile, bool clean) in __tun_detach() argument
652 tun = rtnl_dereference(tfile->tun); in __tun_detach()
655 if (!tfile->detached) in __tun_detach()
656 tun_napi_disable(tfile); in __tun_detach()
657 tun_napi_del(tfile); in __tun_detach()
660 if (tun && !tfile->detached) { in __tun_detach()
661 u16 index = tfile->queue_index; in __tun_detach()
673 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
674 sock_put(&tfile->sk); in __tun_detach()
676 tun_disable_queue(tun, tfile); in __tun_detach()
677 tun_napi_disable(tfile); in __tun_detach()
683 tun_queue_purge(tfile); in __tun_detach()
685 } else if (tfile->detached && clean) { in __tun_detach()
686 tun = tun_enable_queue(tfile); in __tun_detach()
687 sock_put(&tfile->sk); in __tun_detach()
699 xdp_rxq_info_unreg(&tfile->xdp_rxq); in __tun_detach()
700 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); in __tun_detach()
704 static void tun_detach(struct tun_file *tfile, bool clean) in tun_detach() argument
710 tun = rtnl_dereference(tfile->tun); in tun_detach()
712 __tun_detach(tfile, clean); in tun_detach()
718 sock_put(&tfile->sk); in tun_detach()
724 struct tun_file *tfile, *tmp; in tun_detach_all() local
728 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
729 BUG_ON(!tfile); in tun_detach_all()
730 tun_napi_disable(tfile); in tun_detach_all()
731 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
732 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
733 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
736 list_for_each_entry(tfile, &tun->disabled, next) { in tun_detach_all()
737 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
738 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
739 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
745 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
746 tun_napi_del(tfile); in tun_detach_all()
748 tun_queue_purge(tfile); in tun_detach_all()
749 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
750 sock_put(&tfile->sk); in tun_detach_all()
752 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { in tun_detach_all()
753 tun_napi_del(tfile); in tun_detach_all()
754 tun_enable_queue(tfile); in tun_detach_all()
755 tun_queue_purge(tfile); in tun_detach_all()
756 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
757 sock_put(&tfile->sk); in tun_detach_all()
769 struct tun_file *tfile = file->private_data; in tun_attach() local
773 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
778 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
786 if (!tfile->detached && in tun_attach()
794 lock_sock(tfile->socket.sk); in tun_attach()
795 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
796 release_sock(tfile->socket.sk); in tun_attach()
801 if (!tfile->detached && in tun_attach()
802 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, in tun_attach()
808 tfile->queue_index = tun->numqueues; in tun_attach()
809 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; in tun_attach()
811 if (tfile->detached) { in tun_attach()
813 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); in tun_attach()
815 if (tfile->xdp_rxq.queue_index != tfile->queue_index) in tun_attach()
816 tfile->xdp_rxq.queue_index = tfile->queue_index; in tun_attach()
819 err = xdp_rxq_info_reg(&tfile->xdp_rxq, in tun_attach()
820 tun->dev, tfile->queue_index); in tun_attach()
823 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, in tun_attach()
826 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_attach()
832 if (tfile->detached) { in tun_attach()
833 tun_enable_queue(tfile); in tun_attach()
834 tun_napi_enable(tfile); in tun_attach()
836 sock_hold(&tfile->sk); in tun_attach()
837 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
841 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_attach()
852 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
853 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
860 static struct tun_struct *tun_get(struct tun_file *tfile) in tun_get() argument
865 tun = rcu_dereference(tfile->tun); in tun_get()
1087 struct tun_file *tfile; in tun_net_xmit() local
1091 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1094 if (!tfile) in tun_net_xmit()
1108 if (tfile->socket.sk->sk_filter && in tun_net_xmit()
1109 sk_filter(tfile->socket.sk, skb)) in tun_net_xmit()
1128 if (ptr_ring_produce(&tfile->tx_ring, skb)) in tun_net_xmit()
1136 if (tfile->flags & TUN_FASYNC) in tun_net_xmit()
1137 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in tun_net_xmit()
1138 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_net_xmit()
1218 struct tun_file *tfile; in tun_xdp_set() local
1228 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1230 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1232 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1234 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1236 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1238 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1282 static void __tun_xdp_flush_tfile(struct tun_file *tfile) in __tun_xdp_flush_tfile() argument
1285 if (tfile->flags & TUN_FASYNC) in __tun_xdp_flush_tfile()
1286 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in __tun_xdp_flush_tfile()
1287 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in __tun_xdp_flush_tfile()
1294 struct tun_file *tfile; in tun_xdp_xmit() local
1312 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1314 if (unlikely(!tfile)) in tun_xdp_xmit()
1317 spin_lock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1325 if (__ptr_ring_produce(&tfile->tx_ring, frame)) { in tun_xdp_xmit()
1331 spin_unlock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1334 __tun_xdp_flush_tfile(tfile); in tun_xdp_xmit()
1427 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1429 struct sock *sk = tfile->socket.sk; in tun_sock_writeable()
1439 struct tun_file *tfile = file->private_data; in tun_chr_poll() local
1440 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll()
1447 sk = tfile->socket.sk; in tun_chr_poll()
1451 if (!ptr_ring_empty(&tfile->tx_ring)) in tun_chr_poll()
1459 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1461 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1471 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, in tun_napi_alloc_frags() argument
1485 skb = napi_get_frags(&tfile->napi); in tun_napi_alloc_frags()
1521 napi_free_frags(&tfile->napi); in tun_napi_alloc_frags()
1527 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, in tun_alloc_skb() argument
1531 struct sock *sk = tfile->socket.sk; in tun_alloc_skb()
1552 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1555 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_rx_batched()
1562 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1583 skb_record_rx_queue(nskb, tfile->queue_index); in tun_rx_batched()
1586 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1592 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1598 if (tfile->socket.sk->sk_sndbuf != INT_MAX) in tun_can_build_skb()
1614 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, in __tun_build_skb() argument
1625 skb_set_owner_w(skb, tfile->socket.sk); in __tun_build_skb()
1666 struct tun_file *tfile, in tun_build_skb() argument
1703 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, in tun_build_skb()
1720 xdp.rxq = &tfile->xdp_rxq; in tun_build_skb()
1746 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); in tun_build_skb()
1755 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1771 bool frags = tun_napi_frags_enabled(tfile); in tun_get_user()
1826 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1831 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1848 mutex_lock(&tfile->napi_mutex); in tun_get_user()
1849 skb = tun_napi_alloc_frags(tfile, copylen, from); in tun_get_user()
1856 skb = tun_alloc_skb(tfile, align, copylen, linear, in tun_get_user()
1864 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1879 tfile->napi.skb = NULL; in tun_get_user()
1880 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1891 tfile->napi.skb = NULL; in tun_get_user()
1892 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1942 skb_record_rx_queue(skb, tfile->queue_index); in tun_get_user()
1957 tfile->napi.skb = NULL; in tun_get_user()
1958 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1972 !tfile->detached) in tun_get_user()
1995 napi_free_frags(&tfile->napi); in tun_get_user()
1997 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
2001 if (likely(napi_schedule_prep(&tfile->napi))) { in tun_get_user()
2003 napi_gro_frags(&tfile->napi); in tun_get_user()
2004 napi_complete(&tfile->napi); in tun_get_user()
2010 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
2011 } else if (tfile->napi_enabled) { in tun_get_user()
2012 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_get_user()
2021 napi_schedule(&tfile->napi); in tun_get_user()
2025 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
2039 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
2047 struct tun_file *tfile = file->private_data; in tun_chr_write_iter() local
2048 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter()
2058 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
2065 struct tun_file *tfile, in tun_put_user_xdp() argument
2100 struct tun_file *tfile, in tun_put_user() argument
2193 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) in tun_ring_recv() argument
2199 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2207 add_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2211 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2218 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { in tun_ring_recv()
2227 remove_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2234 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2248 ptr = tun_ring_recv(tfile, noblock, &err); in tun_do_read()
2256 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2261 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2274 struct tun_file *tfile = file->private_data; in tun_chr_read_iter() local
2275 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter()
2285 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2431 struct tun_file *tfile; in tun_sock_write_space() local
2445 tfile = container_of(sk, struct tun_file, sk); in tun_sock_write_space()
2446 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); in tun_sock_write_space()
2456 struct tun_file *tfile, in tun_xdp_one() argument
2479 xdp->rxq = &tfile->xdp_rxq; in tun_xdp_one()
2530 skb_record_rx_queue(skb, tfile->queue_index); in tun_xdp_one()
2539 !tfile->detached) in tun_xdp_one()
2554 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2563 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_sendmsg() local
2564 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg()
2584 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2599 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2610 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_recvmsg() local
2611 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg()
2629 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2661 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_peek_len() local
2665 tun = tun_get(tfile); in tun_peek_len()
2669 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); in tun_peek_len()
2738 struct tun_file *tfile = file->private_data; in tun_set_iff() local
2742 if (tfile->detached) in tun_set_iff()
2830 dev->ifindex = tfile->ifindex; in tun_set_iff()
2841 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2858 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2922 struct tun_file *tfile; in tun_detach_filter() local
2925 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2926 lock_sock(tfile->socket.sk); in tun_detach_filter()
2927 sk_detach_filter(tfile->socket.sk); in tun_detach_filter()
2928 release_sock(tfile->socket.sk); in tun_detach_filter()
2937 struct tun_file *tfile; in tun_attach_filter() local
2940 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2941 lock_sock(tfile->socket.sk); in tun_attach_filter()
2942 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2943 release_sock(tfile->socket.sk); in tun_attach_filter()
2956 struct tun_file *tfile; in tun_set_sndbuf() local
2960 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2961 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2967 struct tun_file *tfile = file->private_data; in tun_set_queue() local
2974 tun = tfile->detached; in tun_set_queue()
2985 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2986 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2989 __tun_detach(tfile, false); in tun_set_queue()
3063 struct tun_file *tfile = file->private_data; in __tun_chr_ioctl() local
3064 struct net *net = sock_net(&tfile->sk); in __tun_chr_ioctl()
3102 tun = tun_get(tfile); in __tun_chr_ioctl()
3129 tfile->ifindex = ifindex; in __tun_chr_ioctl()
3145 if (tfile->detached) in __tun_chr_ioctl()
3147 if (!tfile->socket.sk->sk_filter) in __tun_chr_ioctl()
3251 sndbuf = tfile->socket.sk->sk_sndbuf; in __tun_chr_ioctl()
3420 struct tun_file *tfile = file->private_data; in tun_chr_fasync() local
3423 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) in tun_chr_fasync()
3428 tfile->flags |= TUN_FASYNC; in tun_chr_fasync()
3430 tfile->flags &= ~TUN_FASYNC; in tun_chr_fasync()
3439 struct tun_file *tfile; in tun_chr_open() local
3441 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, in tun_chr_open()
3443 if (!tfile) in tun_chr_open()
3445 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { in tun_chr_open()
3446 sk_free(&tfile->sk); in tun_chr_open()
3450 mutex_init(&tfile->napi_mutex); in tun_chr_open()
3451 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3452 tfile->flags = 0; in tun_chr_open()
3453 tfile->ifindex = 0; in tun_chr_open()
3455 init_waitqueue_head(&tfile->socket.wq.wait); in tun_chr_open()
3457 tfile->socket.file = file; in tun_chr_open()
3458 tfile->socket.ops = &tun_socket_ops; in tun_chr_open()
3460 sock_init_data(&tfile->socket, &tfile->sk); in tun_chr_open()
3462 tfile->sk.sk_write_space = tun_sock_write_space; in tun_chr_open()
3463 tfile->sk.sk_sndbuf = INT_MAX; in tun_chr_open()
3465 file->private_data = tfile; in tun_chr_open()
3466 INIT_LIST_HEAD(&tfile->next); in tun_chr_open()
3468 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); in tun_chr_open()
3475 struct tun_file *tfile = file->private_data; in tun_chr_close() local
3477 tun_detach(tfile, true); in tun_chr_close()
3485 struct tun_file *tfile = file->private_data; in tun_chr_show_fdinfo() local
3492 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3631 struct tun_file *tfile; in tun_queue_resize() local
3641 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3642 rings[i] = &tfile->tx_ring; in tun_queue_resize()
3644 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3645 rings[i++] = &tfile->tx_ring; in tun_queue_resize()
3672 struct tun_file *tfile; in tun_device_event() local
3674 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()
3675 tfile->socket.sk->sk_write_space(tfile->socket.sk); in tun_device_event()
3736 struct tun_file *tfile; in tun_get_socket() local
3739 tfile = file->private_data; in tun_get_socket()
3740 if (!tfile) in tun_get_socket()
3742 return &tfile->socket; in tun_get_socket()
3748 struct tun_file *tfile; in tun_get_tx_ring() local
3752 tfile = file->private_data; in tun_get_tx_ring()
3753 if (!tfile) in tun_get_tx_ring()
3755 return &tfile->tx_ring; in tun_get_tx_ring()