Skip to content

Commit 8bf5c4e

Browse files
netoptimizerAlexei Starovoitov
authored andcommitted
tun: setup xdp_rxq_info
Driver hook points for xdp_rxq_info: * reg : tun_attach * unreg: __tun_detach I've done some manual testing of this tun driver, but I would appriciate good review and someone else running their use-case tests, as I'm not 100% sure I understand the tfile->detached semantics. V2: Removed the skb_array_cleanup() call from V1 by request from Jason Wang. Cc: Jason Wang <[email protected]> Cc: "Michael S. Tsirkin" <[email protected]> Cc: Willem de Bruijn <[email protected]> Signed-off-by: Jesper Dangaard Brouer <[email protected]> Reviewed-by: Jason Wang <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 27e95e3 commit 8bf5c4e

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

drivers/net/tun.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ struct tun_file {
180180
struct list_head next;
181181
struct tun_struct *detached;
182182
struct skb_array tx_array;
183+
struct xdp_rxq_info xdp_rxq;
183184
};
184185

185186
struct tun_flow_entry {
@@ -687,8 +688,10 @@ static void __tun_detach(struct tun_file *tfile, bool clean)
687688
tun->dev->reg_state == NETREG_REGISTERED)
688689
unregister_netdevice(tun->dev);
689690
}
690-
if (tun)
691+
if (tun) {
691692
skb_array_cleanup(&tfile->tx_array);
693+
xdp_rxq_info_unreg(&tfile->xdp_rxq);
694+
}
692695
sock_put(&tfile->sk);
693696
}
694697
}
@@ -728,11 +731,13 @@ static void tun_detach_all(struct net_device *dev)
728731
tun_napi_del(tun, tfile);
729732
/* Drop read queue */
730733
tun_queue_purge(tfile);
734+
xdp_rxq_info_unreg(&tfile->xdp_rxq);
731735
sock_put(&tfile->sk);
732736
}
733737
list_for_each_entry_safe(tfile, tmp, &tun->disabled, next) {
734738
tun_enable_queue(tfile);
735739
tun_queue_purge(tfile);
740+
xdp_rxq_info_unreg(&tfile->xdp_rxq);
736741
sock_put(&tfile->sk);
737742
}
738743
BUG_ON(tun->numdisabled != 0);
@@ -784,6 +789,22 @@ static int tun_attach(struct tun_struct *tun, struct file *file,
784789

785790
tfile->queue_index = tun->numqueues;
786791
tfile->socket.sk->sk_shutdown &= ~RCV_SHUTDOWN;
792+
793+
if (tfile->detached) {
794+
/* Re-attach detached tfile, updating XDP queue_index */
795+
WARN_ON(!xdp_rxq_info_is_reg(&tfile->xdp_rxq));
796+
797+
if (tfile->xdp_rxq.queue_index != tfile->queue_index)
798+
tfile->xdp_rxq.queue_index = tfile->queue_index;
799+
} else {
800+
/* Setup XDP RX-queue info, for new tfile getting attached */
801+
err = xdp_rxq_info_reg(&tfile->xdp_rxq,
802+
tun->dev, tfile->queue_index);
803+
if (err < 0)
804+
goto out;
805+
err = 0;
806+
}
807+
787808
rcu_assign_pointer(tfile->tun, tun);
788809
rcu_assign_pointer(tun->tfiles[tun->numqueues], tfile);
789810
tun->numqueues++;
@@ -1508,6 +1529,7 @@ static struct sk_buff *tun_build_skb(struct tun_struct *tun,
15081529
xdp.data = buf + pad;
15091530
xdp_set_data_meta_invalid(&xdp);
15101531
xdp.data_end = xdp.data + len;
1532+
xdp.rxq = &tfile->xdp_rxq;
15111533
orig_data = xdp.data;
15121534
act = bpf_prog_run_xdp(xdp_prog, &xdp);
15131535

0 commit comments

Comments
 (0)