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_disable(struct tun_file *tfile) in tun_napi_disable() argument
290 if (tfile->napi_enabled) in tun_napi_disable()
291 napi_disable(&tfile->napi); in tun_napi_disable()
294 static void tun_napi_del(struct tun_file *tfile) in tun_napi_del() argument
296 if (tfile->napi_enabled) in tun_napi_del()
297 netif_napi_del(&tfile->napi); in tun_napi_del()
300 static bool tun_napi_frags_enabled(const struct tun_file *tfile) in tun_napi_frags_enabled() argument
302 return tfile->napi_frags_enabled; in tun_napi_frags_enabled()
480 struct tun_file *tfile) in tun_flow_update() argument
485 u16 queue_index = tfile->queue_index; in tun_flow_update()
599 static void tun_disable_queue(struct tun_struct *tun, struct tun_file *tfile) in tun_disable_queue() argument
601 tfile->detached = tun; in tun_disable_queue()
602 list_add_tail(&tfile->next, &tun->disabled); in tun_disable_queue()
606 static struct tun_struct *tun_enable_queue(struct tun_file *tfile) in tun_enable_queue() argument
608 struct tun_struct *tun = tfile->detached; in tun_enable_queue()
610 tfile->detached = NULL; in tun_enable_queue()
611 list_del_init(&tfile->next); in tun_enable_queue()
630 static void tun_queue_purge(struct tun_file *tfile) in tun_queue_purge() argument
634 while ((ptr = ptr_ring_consume(&tfile->tx_ring)) != NULL) in tun_queue_purge()
637 skb_queue_purge(&tfile->sk.sk_write_queue); in tun_queue_purge()
638 skb_queue_purge(&tfile->sk.sk_error_queue); in tun_queue_purge()
641 static void __tun_detach(struct tun_file *tfile, bool clean) in __tun_detach() argument
646 tun = rtnl_dereference(tfile->tun); in __tun_detach()
649 tun_napi_disable(tfile); in __tun_detach()
650 tun_napi_del(tfile); in __tun_detach()
653 if (tun && !tfile->detached) { in __tun_detach()
654 u16 index = tfile->queue_index; in __tun_detach()
666 RCU_INIT_POINTER(tfile->tun, NULL); in __tun_detach()
667 sock_put(&tfile->sk); in __tun_detach()
669 tun_disable_queue(tun, tfile); in __tun_detach()
674 tun_queue_purge(tfile); in __tun_detach()
676 } else if (tfile->detached && clean) { in __tun_detach()
677 tun = tun_enable_queue(tfile); in __tun_detach()
678 sock_put(&tfile->sk); in __tun_detach()
690 xdp_rxq_info_unreg(&tfile->xdp_rxq); in __tun_detach()
691 ptr_ring_cleanup(&tfile->tx_ring, tun_ptr_free); in __tun_detach()
692 sock_put(&tfile->sk); in __tun_detach()
696 static void tun_detach(struct tun_file *tfile, bool clean) in tun_detach() argument
702 tun = rtnl_dereference(tfile->tun); in tun_detach()
704 __tun_detach(tfile, clean); 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_enable_queue(tfile); in tun_detach_all()
743 tun_queue_purge(tfile); in tun_detach_all()
744 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_detach_all()
745 sock_put(&tfile->sk); in tun_detach_all()
757 struct tun_file *tfile = file->private_data; in tun_attach() local
761 err = security_tun_dev_attach(tfile->socket.sk, tun->security); in tun_attach()
766 if (rtnl_dereference(tfile->tun) && !tfile->detached) in tun_attach()
774 if (!tfile->detached && in tun_attach()
782 lock_sock(tfile->socket.sk); in tun_attach()
783 err = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach()
784 release_sock(tfile->socket.sk); in tun_attach()
789 if (!tfile->detached && in tun_attach()
790 ptr_ring_resize(&tfile->tx_ring, dev->tx_queue_len, in tun_attach()
796 tfile->queue_index = tun->numqueues; in tun_attach()
797 tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN; in tun_attach()
799 if (tfile->detached) { in tun_attach()
801 WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq)); in tun_attach()
803 if (tfile->xdp_rxq.queue_index != tfile->queue_index) in tun_attach()
804 tfile->xdp_rxq.queue_index = tfile->queue_index; in tun_attach()
807 err = xdp_rxq_info_reg(&tfile->xdp_rxq, in tun_attach()
808 tun->dev, tfile->queue_index); in tun_attach()
811 err = xdp_rxq_info_reg_mem_model(&tfile->xdp_rxq, in tun_attach()
814 xdp_rxq_info_unreg(&tfile->xdp_rxq); in tun_attach()
820 if (tfile->detached) { in tun_attach()
821 tun_enable_queue(tfile); in tun_attach()
823 sock_hold(&tfile->sk); in tun_attach()
824 tun_napi_init(tun, tfile, napi, napi_frags); in tun_attach()
828 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_attach()
839 rcu_assign_pointer(tfile->tun, tun); in tun_attach()
840 rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile); in tun_attach()
847 static struct tun_struct *tun_get(struct tun_file *tfile) in tun_get() argument
852 tun = rcu_dereference(tfile->tun); in tun_get()
1074 struct tun_file *tfile; in tun_net_xmit() local
1078 tfile = rcu_dereference(tun->tfiles[txq]); in tun_net_xmit()
1081 if (!tfile) in tun_net_xmit()
1095 if (tfile->socket.sk->sk_filter && in tun_net_xmit()
1096 sk_filter(tfile->socket.sk, skb)) in tun_net_xmit()
1115 if (ptr_ring_produce(&tfile->tx_ring, skb)) in tun_net_xmit()
1123 if (tfile->flags & TUN_FASYNC) in tun_net_xmit()
1124 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in tun_net_xmit()
1125 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in tun_net_xmit()
1205 struct tun_file *tfile; in tun_xdp_set() local
1215 tfile = rtnl_dereference(tun->tfiles[i]); in tun_xdp_set()
1217 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1219 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1221 list_for_each_entry(tfile, &tun->disabled, next) { in tun_xdp_set()
1223 sock_set_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1225 sock_reset_flag(&tfile->sk, SOCK_XDP); in tun_xdp_set()
1269 static void __tun_xdp_flush_tfile(struct tun_file *tfile) in __tun_xdp_flush_tfile() argument
1272 if (tfile->flags & TUN_FASYNC) in __tun_xdp_flush_tfile()
1273 kill_fasync(&tfile->fasync, SIGIO, POLL_IN); in __tun_xdp_flush_tfile()
1274 tfile->socket.sk->sk_data_ready(tfile->socket.sk); in __tun_xdp_flush_tfile()
1281 struct tun_file *tfile; in tun_xdp_xmit() local
1299 tfile = rcu_dereference(tun->tfiles[smp_processor_id() % in tun_xdp_xmit()
1301 if (unlikely(!tfile)) in tun_xdp_xmit()
1304 spin_lock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1312 if (__ptr_ring_produce(&tfile->tx_ring, frame)) { in tun_xdp_xmit()
1318 spin_unlock(&tfile->tx_ring.producer_lock); in tun_xdp_xmit()
1321 __tun_xdp_flush_tfile(tfile); in tun_xdp_xmit()
1414 static bool tun_sock_writeable(struct tun_struct *tun, struct tun_file *tfile) in tun_sock_writeable() argument
1416 struct sock *sk = tfile->socket.sk; in tun_sock_writeable()
1426 struct tun_file *tfile = file->private_data; in tun_chr_poll() local
1427 struct tun_struct *tun = tun_get(tfile); in tun_chr_poll()
1434 sk = tfile->socket.sk; in tun_chr_poll()
1438 if (!ptr_ring_empty(&tfile->tx_ring)) in tun_chr_poll()
1446 if (tun_sock_writeable(tun, tfile) || in tun_chr_poll()
1448 tun_sock_writeable(tun, tfile))) in tun_chr_poll()
1458 static struct sk_buff *tun_napi_alloc_frags(struct tun_file *tfile, in tun_napi_alloc_frags() argument
1472 skb = napi_get_frags(&tfile->napi); in tun_napi_alloc_frags()
1508 napi_free_frags(&tfile->napi); in tun_napi_alloc_frags()
1514 static struct sk_buff *tun_alloc_skb(struct tun_file *tfile, in tun_alloc_skb() argument
1518 struct sock *sk = tfile->socket.sk; in tun_alloc_skb()
1539 static void tun_rx_batched(struct tun_struct *tun, struct tun_file *tfile, in tun_rx_batched() argument
1542 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_rx_batched()
1549 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1570 skb_record_rx_queue(nskb, tfile->queue_index); in tun_rx_batched()
1573 skb_record_rx_queue(skb, tfile->queue_index); in tun_rx_batched()
1579 static bool tun_can_build_skb(struct tun_struct *tun, struct tun_file *tfile, in tun_can_build_skb() argument
1585 if (tfile->socket.sk->sk_sndbuf != INT_MAX) in tun_can_build_skb()
1601 static struct sk_buff *__tun_build_skb(struct tun_file *tfile, in __tun_build_skb() argument
1612 skb_set_owner_w(skb, tfile->socket.sk); in __tun_build_skb()
1653 struct tun_file *tfile, in tun_build_skb() argument
1690 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, in tun_build_skb()
1707 xdp.rxq = &tfile->xdp_rxq; in tun_build_skb()
1733 return __tun_build_skb(tfile, alloc_frag, buf, buflen, len, pad); in tun_build_skb()
1742 static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, in tun_get_user() argument
1758 bool frags = tun_napi_frags_enabled(tfile); in tun_get_user()
1813 if (!frags && tun_can_build_skb(tun, tfile, len, noblock, zerocopy)) { in tun_get_user()
1818 skb = tun_build_skb(tun, tfile, from, &gso, len, &skb_xdp); in tun_get_user()
1835 mutex_lock(&tfile->napi_mutex); in tun_get_user()
1836 skb = tun_napi_alloc_frags(tfile, copylen, from); in tun_get_user()
1843 skb = tun_alloc_skb(tfile, align, copylen, linear, in tun_get_user()
1851 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1866 tfile->napi.skb = NULL; in tun_get_user()
1867 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1878 tfile->napi.skb = NULL; in tun_get_user()
1879 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1929 skb_record_rx_queue(skb, tfile->queue_index); in tun_get_user()
1944 tfile->napi.skb = NULL; in tun_get_user()
1945 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1959 !tfile->detached) in tun_get_user()
1979 napi_free_frags(&tfile->napi); in tun_get_user()
1981 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1987 napi_gro_frags(&tfile->napi); in tun_get_user()
1989 mutex_unlock(&tfile->napi_mutex); in tun_get_user()
1990 } else if (tfile->napi_enabled) { in tun_get_user()
1991 struct sk_buff_head *queue = &tfile->sk.sk_write_queue; in tun_get_user()
2000 napi_schedule(&tfile->napi); in tun_get_user()
2004 tun_rx_batched(tun, tfile, skb, more); in tun_get_user()
2018 tun_flow_update(tun, rxhash, tfile); in tun_get_user()
2026 struct tun_file *tfile = file->private_data; in tun_chr_write_iter() local
2027 struct tun_struct *tun = tun_get(tfile); in tun_chr_write_iter()
2037 result = tun_get_user(tun, tfile, NULL, from, noblock, false); in tun_chr_write_iter()
2044 struct tun_file *tfile, in tun_put_user_xdp() argument
2079 struct tun_file *tfile, in tun_put_user() argument
2172 static void *tun_ring_recv(struct tun_file *tfile, int noblock, int *err) in tun_ring_recv() argument
2178 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2186 add_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2190 ptr = ptr_ring_consume(&tfile->tx_ring); in tun_ring_recv()
2197 if (tfile->socket.sk->sk_shutdown & RCV_SHUTDOWN) { in tun_ring_recv()
2206 remove_wait_queue(&tfile->socket.wq.wait, &wait); in tun_ring_recv()
2213 static ssize_t tun_do_read(struct tun_struct *tun, struct tun_file *tfile, in tun_do_read() argument
2227 ptr = tun_ring_recv(tfile, noblock, &err); in tun_do_read()
2235 ret = tun_put_user_xdp(tun, tfile, xdpf, to); in tun_do_read()
2240 ret = tun_put_user(tun, tfile, skb, to); in tun_do_read()
2253 struct tun_file *tfile = file->private_data; in tun_chr_read_iter() local
2254 struct tun_struct *tun = tun_get(tfile); in tun_chr_read_iter()
2264 ret = tun_do_read(tun, tfile, to, noblock, NULL); in tun_chr_read_iter()
2410 struct tun_file *tfile; in tun_sock_write_space() local
2424 tfile = container_of(sk, struct tun_file, sk); in tun_sock_write_space()
2425 kill_fasync(&tfile->fasync, SIGIO, POLL_OUT); in tun_sock_write_space()
2435 struct tun_file *tfile, in tun_xdp_one() argument
2458 xdp->rxq = &tfile->xdp_rxq; in tun_xdp_one()
2509 skb_record_rx_queue(skb, tfile->queue_index); in tun_xdp_one()
2518 !tfile->detached) in tun_xdp_one()
2533 tun_flow_update(tun, rxhash, tfile); in tun_xdp_one()
2542 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_sendmsg() local
2543 struct tun_struct *tun = tun_get(tfile); in tun_sendmsg()
2562 tun_xdp_one(tun, tfile, xdp, &flush, &tpage); in tun_sendmsg()
2577 ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter, in tun_sendmsg()
2588 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_recvmsg() local
2589 struct tun_struct *tun = tun_get(tfile); in tun_recvmsg()
2607 ret = tun_do_read(tun, tfile, &m->msg_iter, flags & MSG_DONTWAIT, ptr); in tun_recvmsg()
2639 struct tun_file *tfile = container_of(sock, struct tun_file, socket); in tun_peek_len() local
2643 tun = tun_get(tfile); in tun_peek_len()
2647 ret = PTR_RING_PEEK_CALL(&tfile->tx_ring, tun_ptr_peek_len); in tun_peek_len()
2716 struct tun_file *tfile = file->private_data; in tun_set_iff() local
2720 if (tfile->detached) in tun_set_iff()
2808 dev->ifindex = tfile->ifindex; in tun_set_iff()
2819 tun->sndbuf = tfile->socket.sk->sk_sndbuf; in tun_set_iff()
2836 rcu_assign_pointer(tfile->tun, tun); in tun_set_iff()
2900 struct tun_file *tfile; in tun_detach_filter() local
2903 tfile = rtnl_dereference(tun->tfiles[i]); in tun_detach_filter()
2904 lock_sock(tfile->socket.sk); in tun_detach_filter()
2905 sk_detach_filter(tfile->socket.sk); in tun_detach_filter()
2906 release_sock(tfile->socket.sk); in tun_detach_filter()
2915 struct tun_file *tfile; in tun_attach_filter() local
2918 tfile = rtnl_dereference(tun->tfiles[i]); in tun_attach_filter()
2919 lock_sock(tfile->socket.sk); in tun_attach_filter()
2920 ret = sk_attach_filter(&tun->fprog, tfile->socket.sk); in tun_attach_filter()
2921 release_sock(tfile->socket.sk); in tun_attach_filter()
2934 struct tun_file *tfile; in tun_set_sndbuf() local
2938 tfile = rtnl_dereference(tun->tfiles[i]); in tun_set_sndbuf()
2939 tfile->socket.sk->sk_sndbuf = tun->sndbuf; in tun_set_sndbuf()
2945 struct tun_file *tfile = file->private_data; in tun_set_queue() local
2952 tun = tfile->detached; in tun_set_queue()
2963 tun = rtnl_dereference(tfile->tun); in tun_set_queue()
2964 if (!tun || !(tun->flags & IFF_MULTI_QUEUE) || tfile->detached) in tun_set_queue()
2967 __tun_detach(tfile, false); in tun_set_queue()
3041 struct tun_file *tfile = file->private_data; in __tun_chr_ioctl() local
3042 struct net *net = sock_net(&tfile->sk); in __tun_chr_ioctl()
3080 tun = tun_get(tfile); in __tun_chr_ioctl()
3107 tfile->ifindex = ifindex; in __tun_chr_ioctl()
3123 if (tfile->detached) in __tun_chr_ioctl()
3125 if (!tfile->socket.sk->sk_filter) in __tun_chr_ioctl()
3229 sndbuf = tfile->socket.sk->sk_sndbuf; in __tun_chr_ioctl()
3398 struct tun_file *tfile = file->private_data; in tun_chr_fasync() local
3401 if ((ret = fasync_helper(fd, file, on, &tfile->fasync)) < 0) in tun_chr_fasync()
3406 tfile->flags |= TUN_FASYNC; in tun_chr_fasync()
3408 tfile->flags &= ~TUN_FASYNC; in tun_chr_fasync()
3417 struct tun_file *tfile; in tun_chr_open() local
3419 tfile = (struct tun_file *)sk_alloc(net, AF_UNSPEC, GFP_KERNEL, in tun_chr_open()
3421 if (!tfile) in tun_chr_open()
3423 if (ptr_ring_init(&tfile->tx_ring, 0, GFP_KERNEL)) { in tun_chr_open()
3424 sk_free(&tfile->sk); in tun_chr_open()
3428 mutex_init(&tfile->napi_mutex); in tun_chr_open()
3429 RCU_INIT_POINTER(tfile->tun, NULL); in tun_chr_open()
3430 tfile->flags = 0; in tun_chr_open()
3431 tfile->ifindex = 0; in tun_chr_open()
3433 init_waitqueue_head(&tfile->socket.wq.wait); in tun_chr_open()
3435 tfile->socket.file = file; in tun_chr_open()
3436 tfile->socket.ops = &tun_socket_ops; in tun_chr_open()
3438 sock_init_data_uid(&tfile->socket, &tfile->sk, current_fsuid()); in tun_chr_open()
3440 tfile->sk.sk_write_space = tun_sock_write_space; in tun_chr_open()
3441 tfile->sk.sk_sndbuf = INT_MAX; in tun_chr_open()
3443 file->private_data = tfile; in tun_chr_open()
3444 INIT_LIST_HEAD(&tfile->next); in tun_chr_open()
3446 sock_set_flag(&tfile->sk, SOCK_ZEROCOPY); in tun_chr_open()
3453 struct tun_file *tfile = file->private_data; in tun_chr_close() local
3455 tun_detach(tfile, true); in tun_chr_close()
3463 struct tun_file *tfile = file->private_data; in tun_chr_show_fdinfo() local
3470 tun = tun_get(tfile); in tun_chr_show_fdinfo()
3609 struct tun_file *tfile; in tun_queue_resize() local
3619 tfile = rtnl_dereference(tun->tfiles[i]); in tun_queue_resize()
3620 rings[i] = &tfile->tx_ring; in tun_queue_resize()
3622 list_for_each_entry(tfile, &tun->disabled, next) in tun_queue_resize()
3623 rings[i++] = &tfile->tx_ring; in tun_queue_resize()
3650 struct tun_file *tfile; in tun_device_event() local
3652 tfile = rtnl_dereference(tun->tfiles[i]); in tun_device_event()
3653 tfile->socket.sk->sk_write_space(tfile->socket.sk); in tun_device_event()
3714 struct tun_file *tfile; in tun_get_socket() local
3717 tfile = file->private_data; in tun_get_socket()
3718 if (!tfile) in tun_get_socket()
3720 return &tfile->socket; in tun_get_socket()
3726 struct tun_file *tfile; in tun_get_tx_ring() local
3730 tfile = file->private_data; in tun_get_tx_ring()
3731 if (!tfile) in tun_get_tx_ring()
3733 return &tfile->tx_ring; in tun_get_tx_ring()