Lines Matching refs:tfile

221 	struct tun_file *tfile = container_of(napi, struct tun_file, napi);  in tun_napi_receive()  local
222 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_napi_receive()
259 static void tun_napi_init(struct tun_struct *tun, struct tun_file *tfile, in tun_napi_init() argument
262 tfile->napi_enabled = napi_en; in tun_napi_init()
263 tfile->napi_frags_enabled = napi_en && napi_frags; in tun_napi_init()
265 netif_tx_napi_add(tun->dev, &tfile->napi, tun_napi_poll, in tun_napi_init()
267 napi_enable(&tfile->napi); in tun_napi_init()
271 static void tun_napi_disable(struct tun_file *tfile) in tun_napi_disable() argument
273 if (tfile->napi_enabled) in tun_napi_disable()
274 napi_disable(&tfile->napi); in tun_napi_disable()
277 static void tun_napi_del(struct tun_file *tfile) in tun_napi_del() argument
279 if (tfile->napi_enabled) in tun_napi_del()
280 netif_napi_del(&tfile->napi); in tun_napi_del()
283 static bool tun_napi_frags_enabled(const struct tun_file *tfile) in tun_napi_frags_enabled() argument
285 return tfile->napi_frags_enabled; in tun_napi_frags_enabled()
463 struct tun_file *tfile) in tun_flow_update() argument
468 u16 queue_index = tfile->queue_index; in tun_flow_update()
582 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
584 tfile->detached = tun; in tun_disable_queue()
585 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
589 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) in tun_enable_queue() argument
591 struct tun_struct *tun = tfile->detached; in tun_enable_queue()
593 tfile->detached = NULL; in tun_enable_queue()
594 list_del_init(&tfile->next); in tun_enable_queue()
613 static void tun_queue_purge(struct tun_file *tfile) in tun_queue_purge() argument
617 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) in tun_queue_purge()
620 skb_queue_purge(&tfile->sk.sk_write_queue); in tun_queue_purge()
621 skb_queue_purge(&tfile->sk.sk_error_queue); in tun_queue_purge()
624 static void __tun_detach(struct tun_file *tfile, bool clean) in __tun_detach() argument
629 tun = rtnl_dereference(tfile->tun); in __tun_detach()
632 tun_napi_disable(tfile); in __tun_detach()
633 tun_napi_del(tfile); in __tun_detach()
636 if (tun && !tfile->detached) { in __tun_detach()
637 u16 index = tfile->queue_index; in __tun_detach()
649 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
650 sock_put(&tfile->sk); in __tun_detach()
652 tun_disable_queue(tun, tfile); in __tun_detach()
657 tun_queue_purge(tfile); in __tun_detach()
659 } else if (tfile->detached && clean) { in __tun_detach()
660 tun = tun_enable_queue(tfile); in __tun_detach()
661 sock_put(&tfile->sk); in __tun_detach()
673 xdp_rxq_info_unreg(&tfile->xdp_rxq); in __tun_detach()
674 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); in __tun_detach()
675 sock_put(&tfile->sk); in __tun_detach()
679 static void tun_detach(struct tun_file *tfile, bool clean) in tun_detach() argument
685 tun = rtnl_dereference(tfile->tun); in tun_detach()
687 __tun_detach(tfile, clean); in tun_detach()
696 struct tun_file *tfile, *tmp; in tun_detach_all() local
700 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
701 BUG_ON(!tfile); in tun_detach_all()
702 tun_napi_disable(tfile); in tun_detach_all()
703 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
704 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
705 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
708 list_for_each_entry(tfile, &tun->disabled, next) { in tun_detach_all()
709 tfile->socket.sk->sk_shutdown = RCV_SHUTDOWN; in tun_detach_all()
710 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_detach_all()
711 RCU_INIT_POINTER(tfile->tun, NULL); in tun_detach_all()
717 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_all()
718 tun_napi_del(tfile); in tun_detach_all()
720 tun_queue_purge(tfile); in tun_detach_all()
721 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
722 sock_put(&tfile->sk); in tun_detach_all()
724 list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) { in tun_detach_all()
725 tun_enable_queue(tfile); in tun_detach_all()
726 tun_queue_purge(tfile); in tun_detach_all()
727 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
728 sock_put(&tfile->sk); in tun_detach_all()
740 struct tun_file *tfile = file->private_data; in tun_attach() local
744 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
749 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
757 if (!tfile->detached && in tun_attach()
765 lock_sock(tfile->socket.sk); in tun_attach()
766 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
767 release_sock(tfile->socket.sk); in tun_attach()
772 if (!tfile->detached && in tun_attach()
773 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, in tun_attach()
779 tfile->queue_index = tun->numqueues; in tun_attach()
780 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; in tun_attach()
782 if (tfile->detached) { in tun_attach()
784 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); in tun_attach()
786 if (tfile->xdp_rxq.queue_index != tfile->queue_index) in tun_attach()
787 tfile->xdp_rxq.queue_index = tfile->queue_index; in tun_attach()
790 err = xdp_rxq_info_reg(&tfile->xdp_rxq, in tun_attach()
791 tun->dev, tfile->queue_index, 0); in tun_attach()
794 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, in tun_attach()
797 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_attach()
803 if (tfile->detached) { in tun_attach()
804 tun_enable_queue(tfile); in tun_attach()
806 sock_hold(&tfile->sk); in tun_attach()
807 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
811 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_attach()
822 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
823 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
830 static struct tun_struct *tun_get(struct tun_file *tfile) in tun_get() argument
835 tun = rcu_dereference(tfile->tun); in tun_get()
1013 struct tun_file *tfile; in tun_net_xmit() local
1017 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1020 if (!tfile) in tun_net_xmit()
1034 if (tfile->socket.sk->sk_filter && in tun_net_xmit()
1035 sk_filter(tfile->socket.sk, skb)) in tun_net_xmit()
1054 if (ptr_ring_produce(&tfile->tx_ring, skb)) in tun_net_xmit()
1058 if (tfile->flags & TUN_FASYNC) in tun_net_xmit()
1059 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in tun_net_xmit()
1060 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_net_xmit()
1115 struct tun_file *tfile; in tun_xdp_set() local
1125 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1127 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1129 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1131 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1133 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1135 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1178 static void __tun_xdp_flush_tfile(struct tun_file *tfile) in __tun_xdp_flush_tfile() argument
1181 if (tfile->flags & TUN_FASYNC) in __tun_xdp_flush_tfile()
1182 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in __tun_xdp_flush_tfile()
1183 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in __tun_xdp_flush_tfile()
1190 struct tun_file *tfile; in tun_xdp_xmit() local
1207 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1209 if (unlikely(!tfile)) in tun_xdp_xmit()
1212 spin_lock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1220 if (__ptr_ring_produce(&tfile->tx_ring, frame)) { in tun_xdp_xmit()
1226 spin_unlock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1229 __tun_xdp_flush_tfile(tfile); in tun_xdp_xmit()
1325 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1327 struct sock *sk = tfile->socket.sk; in tun_sock_writeable()
1337 struct tun_file *tfile = file->private_data; in tun_chr_poll() local
1338 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll()
1345 sk = tfile->socket.sk; in tun_chr_poll()
1349 if (!ptr_ring_empty(&tfile->tx_ring)) in tun_chr_poll()
1357 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1359 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1369 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, in tun_napi_alloc_frags() argument
1382 skb = napi_get_frags(&tfile->napi); in tun_napi_alloc_frags()
1418 napi_free_frags(&tfile->napi); in tun_napi_alloc_frags()
1424 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, in tun_alloc_skb() argument
1428 struct sock *sk = tfile->socket.sk; in tun_alloc_skb()
1449 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1452 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_rx_batched()
1459 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1480 skb_record_rx_queue(nskb, tfile->queue_index); in tun_rx_batched()
1483 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1489 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1495 if (tfile->socket.sk->sk_sndbuf != INT_MAX) in tun_can_build_skb()
1511 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, in __tun_build_skb() argument
1522 skb_set_owner_w(skb, tfile->socket.sk); in __tun_build_skb()
1563 struct tun_file *tfile, in tun_build_skb() argument
1600 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, in tun_build_skb()
1613 xdp_init_buff(&xdp, buflen, &tfile->xdp_rxq); in tun_build_skb()
1639 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); in tun_build_skb()
1648 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1663 bool frags = tun_napi_frags_enabled(tfile); in tun_get_user()
1718 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1723 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1740 mutex_lock(&tfile->napi_mutex); in tun_get_user()
1741 skb = tun_napi_alloc_frags(tfile, copylen, from); in tun_get_user()
1748 skb = tun_alloc_skb(tfile, align, copylen, linear, in tun_get_user()
1756 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1771 tfile->napi.skb = NULL; in tun_get_user()
1772 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1783 tfile->napi.skb = NULL; in tun_get_user()
1784 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1832 skb_record_rx_queue(skb, tfile->queue_index); in tun_get_user()
1847 tfile->napi.skb = NULL; in tun_get_user()
1848 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1862 !tfile->detached) in tun_get_user()
1882 napi_free_frags(&tfile->napi); in tun_get_user()
1884 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1890 napi_gro_frags(&tfile->napi); in tun_get_user()
1892 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1893 } else if (tfile->napi_enabled) { in tun_get_user()
1894 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_get_user()
1903 napi_schedule(&tfile->napi); in tun_get_user()
1907 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
1918 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
1926 struct tun_file *tfile = file->private_data; in tun_chr_write_iter() local
1927 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter()
1937 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
1944 struct tun_file *tfile, in tun_put_user_xdp() argument
1975 struct tun_file *tfile, in tun_put_user() argument
2064 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) in tun_ring_recv() argument
2070 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2078 add_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2082 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2089 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { in tun_ring_recv()
2098 remove_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2105 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2119 ptr = tun_ring_recv(tfile, noblock, &err); in tun_do_read()
2127 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2132 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2145 struct tun_file *tfile = file->private_data; in tun_chr_read_iter() local
2146 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter()
2156 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2306 struct tun_file *tfile; in tun_sock_write_space() local
2320 tfile = container_of(sk, struct tun_file, sk); in tun_sock_write_space()
2321 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); in tun_sock_write_space()
2331 struct tun_file *tfile, in tun_xdp_one() argument
2353 xdp_init_buff(xdp, buflen, &tfile->xdp_rxq); in tun_xdp_one()
2404 skb_record_rx_queue(skb, tfile->queue_index); in tun_xdp_one()
2413 !tfile->detached) in tun_xdp_one()
2424 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2433 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_sendmsg() local
2434 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg()
2453 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2468 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2479 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_recvmsg() local
2480 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg()
2498 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2530 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_peek_len() local
2534 tun = tun_get(tfile); in tun_peek_len()
2538 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); in tun_peek_len()
2607 struct tun_file *tfile = file->private_data; in tun_set_iff() local
2611 if (tfile->detached) in tun_set_iff()
2699 dev->ifindex = tfile->ifindex; in tun_set_iff()
2710 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2752 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2834 struct tun_file *tfile; in tun_detach_filter() local
2837 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2838 lock_sock(tfile->socket.sk); in tun_detach_filter()
2839 sk_detach_filter(tfile->socket.sk); in tun_detach_filter()
2840 release_sock(tfile->socket.sk); in tun_detach_filter()
2849 struct tun_file *tfile; in tun_attach_filter() local
2852 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2853 lock_sock(tfile->socket.sk); in tun_attach_filter()
2854 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2855 release_sock(tfile->socket.sk); in tun_attach_filter()
2868 struct tun_file *tfile; in tun_set_sndbuf() local
2872 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2873 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2879 struct tun_file *tfile = file->private_data; in tun_set_queue() local
2886 tun = tfile->detached; in tun_set_queue()
2897 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2898 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2901 __tun_detach(tfile, false); in tun_set_queue()
2975 struct tun_file *tfile = file->private_data; in __tun_chr_ioctl() local
2976 struct net *net = sock_net(&tfile->sk); in __tun_chr_ioctl()
3013 tun = tun_get(tfile); in __tun_chr_ioctl()
3040 tfile->ifindex = ifindex; in __tun_chr_ioctl()
3056 if (tfile->detached) in __tun_chr_ioctl()
3058 if (!tfile->socket.sk->sk_filter) in __tun_chr_ioctl()
3171 sndbuf = tfile->socket.sk->sk_sndbuf; in __tun_chr_ioctl()
3340 struct tun_file *tfile = file->private_data; in tun_chr_fasync() local
3343 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) in tun_chr_fasync()
3348 tfile->flags |= TUN_FASYNC; in tun_chr_fasync()
3350 tfile->flags &= ~TUN_FASYNC; in tun_chr_fasync()
3359 struct tun_file *tfile; in tun_chr_open() local
3361 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, in tun_chr_open()
3363 if (!tfile) in tun_chr_open()
3365 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { in tun_chr_open()
3366 sk_free(&tfile->sk); in tun_chr_open()
3370 mutex_init(&tfile->napi_mutex); in tun_chr_open()
3371 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3372 tfile->flags = 0; in tun_chr_open()
3373 tfile->ifindex = 0; in tun_chr_open()
3375 init_waitqueue_head(&tfile->socket.wq.wait); in tun_chr_open()
3377 tfile->socket.file = file; in tun_chr_open()
3378 tfile->socket.ops = &tun_socket_ops; in tun_chr_open()
3380 sock_init_data(&tfile->socket, &tfile->sk); in tun_chr_open()
3382 tfile->sk.sk_write_space = tun_sock_write_space; in tun_chr_open()
3383 tfile->sk.sk_sndbuf = INT_MAX; in tun_chr_open()
3385 file->private_data = tfile; in tun_chr_open()
3386 INIT_LIST_HEAD(&tfile->next); in tun_chr_open()
3388 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); in tun_chr_open()
3395 struct tun_file *tfile = file->private_data; in tun_chr_close() local
3397 tun_detach(tfile, true); in tun_chr_close()
3405 struct tun_file *tfile = file->private_data; in tun_chr_show_fdinfo() local
3412 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3555 struct tun_file *tfile; in tun_queue_resize() local
3565 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3566 rings[i] = &tfile->tx_ring; in tun_queue_resize()
3568 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3569 rings[i++] = &tfile->tx_ring; in tun_queue_resize()
3596 struct tun_file *tfile; in tun_device_event() local
3598 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()
3599 tfile->socket.sk->sk_write_space(tfile->socket.sk); in tun_device_event()
3660 struct tun_file *tfile; in tun_get_socket() local
3663 tfile = file->private_data; in tun_get_socket()
3664 if (!tfile) in tun_get_socket()
3666 return &tfile->socket; in tun_get_socket()
3672 struct tun_file *tfile; in tun_get_tx_ring() local
3676 tfile = file->private_data; in tun_get_tx_ring()
3677 if (!tfile) in tun_get_tx_ring()
3679 return &tfile->tx_ring; in tun_get_tx_ring()