Skip to content

Commit fe8dd45

Browse files
jasowangdavem330
authored andcommitted
tun: switch to new type of msg_control
This patch introduces to a new tun/tap specific msg_control: #define TUN_MSG_UBUF 1 #define TUN_MSG_PTR 2 struct tun_msg_ctl { int type; void *ptr; }; This allows us to pass different kinds of msg_control through sendmsg(). The first supported type is ubuf (TUN_MSG_UBUF) which will be used by the existed vhost_net zerocopy code. The second is XDP buff, which allows vhost_net to pass XDP buff to TUN. This could be used to implement accepting an array of XDP buffs from vhost_net in the following patches. Signed-off-by: Jason Wang <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1a09791 commit fe8dd45

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

drivers/net/tap.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ static inline struct sk_buff *tap_alloc_skb(struct sock *sk, size_t prepad,
619619
#define TAP_RESERVE HH_DATA_OFF(ETH_HLEN)
620620

621621
/* Get packet from user space buffer */
622-
static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
622+
static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,
623623
struct iov_iter *from, int noblock)
624624
{
625625
int good_linear = SKB_MAX_HEAD(TAP_RESERVE);
@@ -663,7 +663,7 @@ static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
663663
if (unlikely(len < ETH_HLEN))
664664
goto err;
665665

666-
if (m && m->msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
666+
if (msg_control && sock_flag(&q->sk, SOCK_ZEROCOPY)) {
667667
struct iov_iter i;
668668

669669
copylen = vnet_hdr.hdr_len ?
@@ -724,11 +724,11 @@ static ssize_t tap_get_user(struct tap_queue *q, struct msghdr *m,
724724
tap = rcu_dereference(q->tap);
725725
/* copy skb_ubuf_info for callback when skb has no error */
726726
if (zerocopy) {
727-
skb_shinfo(skb)->destructor_arg = m->msg_control;
727+
skb_shinfo(skb)->destructor_arg = msg_control;
728728
skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
729729
skb_shinfo(skb)->tx_flags |= SKBTX_SHARED_FRAG;
730-
} else if (m && m->msg_control) {
731-
struct ubuf_info *uarg = m->msg_control;
730+
} else if (msg_control) {
731+
struct ubuf_info *uarg = msg_control;
732732
uarg->callback(uarg, false);
733733
}
734734

@@ -1150,7 +1150,13 @@ static int tap_sendmsg(struct socket *sock, struct msghdr *m,
11501150
size_t total_len)
11511151
{
11521152
struct tap_queue *q = container_of(sock, struct tap_queue, sock);
1153-
return tap_get_user(q, m, &m->msg_iter, m->msg_flags & MSG_DONTWAIT);
1153+
struct tun_msg_ctl *ctl = m->msg_control;
1154+
1155+
if (ctl && ctl->type != TUN_MSG_UBUF)
1156+
return -EINVAL;
1157+
1158+
return tap_get_user(q, ctl ? ctl->ptr : NULL, &m->msg_iter,
1159+
m->msg_flags & MSG_DONTWAIT);
11541160
}
11551161

11561162
static int tap_recvmsg(struct socket *sock, struct msghdr *m,

drivers/net/tun.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2431,11 +2431,15 @@ static int tun_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
24312431
int ret;
24322432
struct tun_file *tfile = container_of(sock, struct tun_file, socket);
24332433
struct tun_struct *tun = tun_get(tfile);
2434+
struct tun_msg_ctl *ctl = m->msg_control;
24342435

24352436
if (!tun)
24362437
return -EBADFD;
24372438

2438-
ret = tun_get_user(tun, tfile, m->msg_control, &m->msg_iter,
2439+
if (ctl && ctl->type != TUN_MSG_UBUF)
2440+
return -EINVAL;
2441+
2442+
ret = tun_get_user(tun, tfile, ctl ? ctl->ptr : NULL, &m->msg_iter,
24392443
m->msg_flags & MSG_DONTWAIT,
24402444
m->msg_flags & MSG_MORE);
24412445
tun_put(tun);

drivers/vhost/net.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
620620
.msg_controllen = 0,
621621
.msg_flags = MSG_DONTWAIT,
622622
};
623+
struct tun_msg_ctl ctl;
623624
size_t len, total_len = 0;
624625
int err;
625626
struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
@@ -664,8 +665,10 @@ static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
664665
ubuf->ctx = nvq->ubufs;
665666
ubuf->desc = nvq->upend_idx;
666667
refcount_set(&ubuf->refcnt, 1);
667-
msg.msg_control = ubuf;
668-
msg.msg_controllen = sizeof(ubuf);
668+
msg.msg_control = &ctl;
669+
ctl.type = TUN_MSG_UBUF;
670+
ctl.ptr = ubuf;
671+
msg.msg_controllen = sizeof(ctl);
669672
ubufs = nvq->ubufs;
670673
atomic_inc(&ubufs->refcount);
671674
nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;

include/linux/if_tun.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,23 @@
1616
#define __IF_TUN_H
1717

1818
#include <uapi/linux/if_tun.h>
19+
#include <uapi/linux/virtio_net.h>
1920

2021
#define TUN_XDP_FLAG 0x1UL
2122

23+
#define TUN_MSG_UBUF 1
24+
#define TUN_MSG_PTR 2
25+
struct tun_msg_ctl {
26+
unsigned short type;
27+
unsigned short num;
28+
void *ptr;
29+
};
30+
31+
struct tun_xdp_hdr {
32+
int buflen;
33+
struct virtio_net_hdr gso;
34+
};
35+
2236
#if defined(CONFIG_TUN) || defined(CONFIG_TUN_MODULE)
2337
struct socket *tun_get_socket(struct file *);
2438
struct ptr_ring *tun_get_tx_ring(struct file *file);

0 commit comments

Comments
 (0)