tun: Unify vnet implementation
Subsystems (7)
v3 cover tun: Unify vnet implementation
When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().
This series fills the uninitialized hole. More concretely, the num_buffers field will be initialized with 1, and the other fields will be inialized with 0. Setting the num_buffers field to 1 is mandated by virtio 1.0 [2].
The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/
---
Changes in v3:
- Dropped changes to fill the vnet header.
- Splitted patch "tun: Unify vnet implementation".
- Reverted spurious changes in patch "tun: Unify vnet implementation".
- Merged tun_vnet.c into TAP.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Fixed num_buffers endian.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Akihiko Odaki (9):
tun: Refactor CONFIG_TUN_VNET_CROSS_LE
tun: Avoid double-tracking iov_iter length changes
tun: Keep hdr_len in tun_get_user()
tun: Decouple vnet from tun_struct
tun: Decouple vnet handling
tun: Extract the vnet handling code
tap: Avoid double-tracking iov_iter length changes
tap: Keep hdr_len in tap_get_user()
tap: Use tun's vnet-related code
MAINTAINERS | 2 +- drivers/net/Kconfig | 1 + drivers/net/Makefile | 3 +- drivers/net/tap.c | 172 ++++++------------------------------------ drivers/net/tun.c | 200 +++++++------------------------------------------ drivers/net/tun_vnet.c | 180 ++++++++++++++++++++++++++++++++++++++++++++ drivers/net/tun_vnet.h | 25 +++++++ 7 files changed, 260 insertions(+), 323 deletions(-)
---
base-commit: a32e14f8aef69b42826cf0998b068a43d486a9e9
change-id: 20241230-tun-66e10a49b0c7
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
PATCH v3 1/9 tun: Refactor CONFIG_TUN_VNET_CROSS_LE
Check IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE) to save some lines and make future changes easier.
--- drivers/net/tun.c | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-)
drivers/net/tun.c +8 -18
@@ -298,10 +298,10 @@ static bool tun_napi_frags_enabled(const struct tun_file *tfile)return tfile->napi_frags_enabled; } -#ifdef CONFIG_TUN_VNET_CROSS_LE static inline bool tun_legacy_is_little_endian (struct tun_struct *tun) { -return tun->flags &TUN_VNET_BE ? false : +return !(IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ) && + (tun->flags &TUN_VNET_BE )) &&virtio_legacy_is_little_endian (); }
@@ -309,6 +309,9 @@ static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp) {int be = !!(tun->flags &TUN_VNET_BE ); +if (!IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE )) +return -EINVAL ; +if (put_user (be, argp))return -EFAULT ;
@@ -319,6 +322,9 @@ static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp) {int be; +if (!IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE )) +return -EINVAL ; +if (get_user (be, argp))return -EFAULT ;
@@ -329,22 +335,6 @@ static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)return 0 ; } -#else -static inline bool tun_legacy_is_little_endian (struct tun_struct *tun) -{ -return virtio_legacy_is_little_endian (); -} - -static long tun_get_vnet_be (struct tun_struct *tun,int __user *argp) -{ -return -EINVAL ; -} - -static long tun_set_vnet_be (struct tun_struct *tun,int __user *argp) -{ -return -EINVAL ; -} -#endif /* CONFIG_TUN_VNET_CROSS_LE */ static inline bool tun_is_little_endian (struct tun_struct *tun) {
Signature · 1 line
2.47.1
Re: [PATCH net v3 1/9] tun: Refactor CONFIG_TUN_VNET_CROSS_LE
Quoted 4 lines · Akihiko Odaki
Check
IS_ENABLED(CONFIG_TUN_VNET_CROSS_LE)to save some lines and make future changes easier.Signed-off-by Akihiko Odaki <[email protected]>
PATCH v3 2/9 tun: Avoid double-tracking iov_iter length changes
In reply to Akihiko Odakitun_get_user() used to track the length of iov_iter with another variable. We can use iov_iter_count() to determine the current length to avoid such chores.
--- drivers/net/tun.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/tun.c +6 -6
@@ -1742,7 +1742,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,struct tun_pi pi = {0 ,cpu_to_be16 (ETH_P_IP ) };struct sk_buff *skb;size_t total_len =iov_iter_count (from); -size_t len = total_len, align = tun->align, linear; +size_t len, align = tun->align, linear;struct virtio_net_hdr gso = {0 };int good_linear;int copylen;
@@ -1754,9 +1754,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,enum skb_drop_reason drop_reason =SKB_DROP_REASON_NOT_SPECIFIED ;if (!(tun->flags &IFF_NO_PI )) { -if (len <sizeof (pi)) +if (iov_iter_count (from) <sizeof (pi))return -EINVAL ; - len -=sizeof (pi);if (!copy_from_iter_full (&pi,sizeof (pi), from))return -EFAULT ;
@@ -1765,9 +1764,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if (tun->flags &IFF_VNET_HDR ) {int vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); -if (len < vnet_hdr_sz) +if (iov_iter_count (from) < vnet_hdr_sz)return -EINVAL ; - len -= vnet_hdr_sz;if (!copy_from_iter_full (&gso,sizeof (gso), from))return -EFAULT ;
@@ -1776,11 +1774,13 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2 > tun16_to_cpu(tun, gso.hdr_len)) gso.hdr_len = cpu_to_tun16(tun, tun16_to_cpu(tun, gso.csum_start) + tun16_to_cpu(tun, gso.csum_offset) + 2); -if (tun16_to_cpu (tun, gso.hdr_len) > len) +if (tun16_to_cpu (tun, gso.hdr_len) >iov_iter_count (from))return -EINVAL ;iov_iter_advance (from, vnet_hdr_sz -sizeof (gso)); } + len =iov_iter_count (from); +if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) { align +=NET_IP_ALIGN ; if (unlikely(len < ETH_HLEN ||
Signature · 1 line
2.47.1
Re: [PATCH net v3 2/9] tun: Avoid double-tracking iov_iter length changes
Quoted 5 lines · Akihiko Odaki
tun_get_user()used to track the length ofiov_iterwith another variable. We can useiov_iter_count()to determine the current length to avoid such chores.Signed-off-by Akihiko Odaki <[email protected]>
PATCH v3 3/9 tun: Keep hdr_len in tun_get_user()
In reply to Akihiko Odakihdr_len is repeatedly used so keep it in a local variable.
--- drivers/net/tun.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
drivers/net/tun.c +5 -9
@@ -1746,6 +1746,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,struct virtio_net_hdr gso = {0 };int good_linear;int copylen; +int hdr_len =0 ;bool zerocopy = false;int err;u32 rxhash =0 ;
@@ -1776,6 +1777,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if (tun16_to_cpu (tun, gso.hdr_len) >iov_iter_count (from))return -EINVAL ; + hdr_len =tun16_to_cpu (tun, gso.hdr_len);iov_iter_advance (from, vnet_hdr_sz -sizeof (gso)); }
@@ -1783,8 +1785,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if ((tun->flags &TUN_TYPE_MASK ) ==IFF_TAP ) { align +=NET_IP_ALIGN ; -if (unlikely (len <ETH_HLEN || - (gso.hdr_len &&tun16_to_cpu (tun, gso.hdr_len) <ETH_HLEN ))) +if (unlikely (len <ETH_HLEN || (hdr_len && hdr_len <ETH_HLEN )))return -EINVAL ; }
@@ -1797,9 +1798,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,* enough room for skb expand head in case it is used. * The rest of the buffer is mapped from userspace. */ - copylen = gso.hdr_len ?tun16_to_cpu (tun, gso.hdr_len) :GOODCOPY_LEN ; -if (copylen > good_linear) - copylen = good_linear; + copylen =min (hdr_len ? hdr_len :GOODCOPY_LEN , good_linear); linear = copylen;iov_iter_advance (&i, copylen);if (iov_iter_npages (&i,INT_MAX ) <=MAX_SKB_FRAGS )
@@ -1820,10 +1819,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, }else {if (!zerocopy) { copylen = len; -if (tun16_to_cpu (tun, gso.hdr_len) > good_linear) - linear = good_linear; -else - linear =tun16_to_cpu (tun, gso.hdr_len); + linear =min (hdr_len, good_linear); }if (frags) {
Signature · 1 line
2.47.1
Re: [PATCH net v3 3/9] tun: Keep hdr_len in tun_get_user()
Quoted 3 lines · Akihiko Odaki
hdr_lenis repeatedly used so keep it in a local variable.Signed-off-by Akihiko Odaki <[email protected]>
PATCH v3 4/9 tun: Decouple vnet from tun_struct
In reply to Akihiko OdakiDecouple vnet-related functions from tun_struct so that we can reuse them for tap in the future.
--- drivers/net/tun.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-)
drivers/net/tun.c +27 -26
@@ -298,16 +298,16 @@ static bool tun_napi_frags_enabled(const struct tun_file *tfile)return tfile->napi_frags_enabled; } -static inline bool tun_legacy_is_little_endian (struct tun_struct *tun) +static inline bool tun_legacy_is_little_endian (unsigned int flags) {return !(IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ) && - (tun->flags &TUN_VNET_BE )) && + (flags &TUN_VNET_BE )) &&virtio_legacy_is_little_endian (); } -static long tun_get_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_get_vnet_be (unsigned int flags,int __user *argp) { -int be = !!(tun->flags &TUN_VNET_BE ); +int be = !!(flags &TUN_VNET_BE );if (!IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ))return -EINVAL ;
@@ -318,7 +318,7 @@ static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)return 0 ; } -static long tun_set_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_set_vnet_be (unsigned int *flags,int __user *argp) {int be;
@@ -329,27 +329,26 @@ static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)return -EFAULT ;if (be) - tun->flags |=TUN_VNET_BE ; + *flags |=TUN_VNET_BE ;else - tun->flags &= ~TUN_VNET_BE ; + *flags &= ~TUN_VNET_BE ;return 0 ; } -static inline bool tun_is_little_endian (struct tun_struct *tun) +static inline bool tun_is_little_endian (unsigned int flags) { -return tun->flags &TUN_VNET_LE || -tun_legacy_is_little_endian (tun); +return flags &TUN_VNET_LE ||tun_legacy_is_little_endian (flags); } -static inline u16 tun16_to_cpu (struct tun_struct *tun,__virtio16 val) +static inline u16 tun16_to_cpu (unsigned int flags,__virtio16 val) { -return __virtio16_to_cpu (tun_is_little_endian (tun), val); +return __virtio16_to_cpu (tun_is_little_endian (flags), val); } -static inline __virtio16 cpu_to_tun16 (struct tun_struct *tun,u16 val) +static inline __virtio16 cpu_to_tun16 (unsigned int flags,u16 val) { -return __cpu_to_virtio16 (tun_is_little_endian (tun), val); +return __cpu_to_virtio16 (tun_is_little_endian (flags), val); }static inline u32 tun_hashfn (u32 rxhash)
@@ -1764,6 +1763,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if (tun->flags &IFF_VNET_HDR ) {int vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); +int flags = tun->flags;if (iov_iter_count (from) < vnet_hdr_sz)return -EINVAL ;
@@ -1772,12 +1772,12 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,return -EFAULT ;if ((gso.flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) && -tun16_to_cpu (tun, gso.csum_start) +tun16_to_cpu (tun, gso.csum_offset) +2 >tun16_to_cpu (tun, gso.hdr_len)) - gso.hdr_len =cpu_to_tun16 (tun,tun16_to_cpu (tun, gso.csum_start) +tun16_to_cpu (tun, gso.csum_offset) +2 ); +tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 >tun16_to_cpu (flags, gso.hdr_len)) + gso.hdr_len =cpu_to_tun16 (flags,tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 ); -if (tun16_to_cpu (tun, gso.hdr_len) >iov_iter_count (from)) +if (tun16_to_cpu (flags, gso.hdr_len) >iov_iter_count (from))return -EINVAL ; - hdr_len =tun16_to_cpu (tun, gso.hdr_len); + hdr_len =tun16_to_cpu (flags, gso.hdr_len);iov_iter_advance (from, vnet_hdr_sz -sizeof (gso)); }
@@ -1854,7 +1854,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, } } -if (virtio_net_hdr_to_skb (skb, &gso,tun_is_little_endian (tun))) { +if (virtio_net_hdr_to_skb (skb, &gso,tun_is_little_endian (tun->flags))) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb;
@@ -2108,23 +2108,24 @@ static ssize_t tun_put_user(struct tun_struct *tun,if (vnet_hdr_sz) {struct virtio_net_hdr gso; +int flags = tun->flags;if (iov_iter_count (iter) < vnet_hdr_sz)return -EINVAL ;if (virtio_net_hdr_from_skb (skb, &gso, -tun_is_little_endian (tun), true, +tun_is_little_endian (flags), true, vlan_hlen)) {struct skb_shared_info *sinfo =skb_shinfo (skb);if (net_ratelimit ()) {netdev_err (tun->dev,"unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n" , - sinfo->gso_type,tun16_to_cpu (tun, gso.gso_size), -tun16_to_cpu (tun, gso.hdr_len)); + sinfo->gso_type,tun16_to_cpu (flags, gso.gso_size), +tun16_to_cpu (flags, gso.hdr_len));print_hex_dump (KERN_ERR ,"tun: " ,DUMP_PREFIX_NONE ,16 ,1 , skb->head, -min ((int )tun16_to_cpu (tun, gso.hdr_len),64 ), true); +min ((int )tun16_to_cpu (flags, gso.hdr_len),64 ), true); }WARN_ON_ONCE (1 );return -EINVAL ;
@@ -2493,7 +2494,7 @@ static int tun_xdp_one(struct tun_struct *tun,skb_reserve (skb, xdp->data - xdp->data_hard_start);skb_put (skb, xdp->data_end - xdp->data); -if (virtio_net_hdr_to_skb (skb, gso,tun_is_little_endian (tun))) { +if (virtio_net_hdr_to_skb (skb, gso,tun_is_little_endian (tun->flags))) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ;
@@ -3322,11 +3323,11 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,break ;case TUNGETVNETBE : - ret =tun_get_vnet_be (tun, argp); + ret =tun_get_vnet_be (tun->flags, argp);break ;case TUNSETVNETBE : - ret =tun_set_vnet_be (tun, argp); + ret =tun_set_vnet_be (&tun->flags, argp);break ;case TUNATTACHFILTER :
Signature · 1 line
2.47.1
Willem de Bruijn
maintainerRe: [PATCH net v3 4/9] tun: Decouple vnet from tun_struct
Quoted 79 lines · Akihiko Odaki
Decouple vnet-related functions from
tun_structso that we can reuse them for tap in the future.Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/tun.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-)drivers/net/tun.c +13
-14@@ -298,16 +298,16 @@ static bool tun_napi_frags_enabled(const struct tun_file *tfile)return tfile->napi_frags_enabled; } -static inline bool tun_legacy_is_little_endian (struct tun_struct *tun) +static inline bool tun_legacy_is_little_endian (unsigned int flags) {return !(IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ) && - (tun->flags &TUN_VNET_BE )) && + (flags &TUN_VNET_BE )) &&virtio_legacy_is_little_endian (); } -static long tun_get_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_get_vnet_be (unsigned int flags,int __user *argp) { -int be = !!(tun->flags &TUN_VNET_BE ); +int be = !!(flags &TUN_VNET_BE );if (!IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ))return -EINVAL ;@@ -318,7 +318,7 @@ static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)return 0 ; } -static long tun_set_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_set_vnet_be (unsigned int *flags,int __user *argp) {int be;@@ -329,27 +329,26 @@ static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)return -EFAULT ;if (be) - tun->flags |=TUN_VNET_BE ; + *flags |=TUN_VNET_BE ;else - tun->flags &= ~TUN_VNET_BE ; + *flags &= ~TUN_VNET_BE ;return 0 ; } -static inline bool tun_is_little_endian (struct tun_struct *tun) +static inline bool tun_is_little_endian (unsigned int flags) { -return tun->flags &TUN_VNET_LE || -tun_legacy_is_little_endian (tun); +return flags &TUN_VNET_LE ||tun_legacy_is_little_endian (flags); } -static inline u16 tun16_to_cpu (struct tun_struct *tun,__virtio16 val) +static inline u16 tun16_to_cpu (unsigned int flags,__virtio16 val) { -return __virtio16_to_cpu (tun_is_little_endian (tun), val); +return __virtio16_to_cpu (tun_is_little_endian (flags), val); } -static inline __virtio16 cpu_to_tun16 (struct tun_struct *tun,u16 val) +static inline __virtio16 cpu_to_tun16 (unsigned int flags,u16 val) { -return __cpu_to_virtio16 (tun_is_little_endian (tun), val); +return __cpu_to_virtio16 (tun_is_little_endian (flags), val); }static inline u32 tun_hashfn (u32 rxhash)
drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1766
Quoted 4 lines
drivers/net/tun.c +1
-0@@ -1764,6 +1763,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if (tun->flags &IFF_VNET_HDR ) {int vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); +int flags = tun->flags;
Here and elsewhere: instead of passing around and repeatedly parsing flags, have a variable is_little_endian (or is_le)?
Quoted 85 lines
drivers/net/tun.c +13
-12if (iov_iter_count (from) < vnet_hdr_sz)return -EINVAL ;@@ -1772,12 +1772,12 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,return -EFAULT ;if ((gso.flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) && -tun16_to_cpu (tun, gso.csum_start) +tun16_to_cpu (tun, gso.csum_offset) +2 >tun16_to_cpu (tun, gso.hdr_len)) - gso.hdr_len =cpu_to_tun16 (tun,tun16_to_cpu (tun, gso.csum_start) +tun16_to_cpu (tun, gso.csum_offset) +2 ); +tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 >tun16_to_cpu (flags, gso.hdr_len)) + gso.hdr_len =cpu_to_tun16 (flags,tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 ); -if (tun16_to_cpu (tun, gso.hdr_len) >iov_iter_count (from)) +if (tun16_to_cpu (flags, gso.hdr_len) >iov_iter_count (from))return -EINVAL ; - hdr_len =tun16_to_cpu (tun, gso.hdr_len); + hdr_len =tun16_to_cpu (flags, gso.hdr_len);iov_iter_advance (from, vnet_hdr_sz -sizeof (gso)); }@@ -1854,7 +1854,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, } } -if (virtio_net_hdr_to_skb (skb, &gso,tun_is_little_endian (tun))) { +if (virtio_net_hdr_to_skb (skb, &gso,tun_is_little_endian (tun->flags))) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb;@@ -2108,23 +2108,24 @@ static ssize_t tun_put_user(struct tun_struct *tun,if (vnet_hdr_sz) {struct virtio_net_hdr gso; +int flags = tun->flags;if (iov_iter_count (iter) < vnet_hdr_sz)return -EINVAL ;if (virtio_net_hdr_from_skb (skb, &gso, -tun_is_little_endian (tun), true, +tun_is_little_endian (flags), true, vlan_hlen)) {struct skb_shared_info *sinfo =skb_shinfo (skb);if (net_ratelimit ()) {netdev_err (tun->dev,"unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n" , - sinfo->gso_type,tun16_to_cpu (tun, gso.gso_size), -tun16_to_cpu (tun, gso.hdr_len)); + sinfo->gso_type,tun16_to_cpu (flags, gso.gso_size), +tun16_to_cpu (flags, gso.hdr_len));print_hex_dump (KERN_ERR ,"tun: " ,DUMP_PREFIX_NONE ,16 ,1 , skb->head, -min ((int )tun16_to_cpu (tun, gso.hdr_len),64 ), true); +min ((int )tun16_to_cpu (flags, gso.hdr_len),64 ), true); }WARN_ON_ONCE (1 );return -EINVAL ;@@ -2493,7 +2494,7 @@ static int tun_xdp_one(struct tun_struct *tun,skb_reserve (skb, xdp->data - xdp->data_hard_start);skb_put (skb, xdp->data_end - xdp->data); -if (virtio_net_hdr_to_skb (skb, gso,tun_is_little_endian (tun))) { +if (virtio_net_hdr_to_skb (skb, gso,tun_is_little_endian (tun->flags))) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ;@@ -3322,11 +3323,11 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,break ;case TUNGETVNETBE : - ret =tun_get_vnet_be (tun, argp); + ret =tun_get_vnet_be (tun->flags, argp);break ;case TUNSETVNETBE : - ret =tun_set_vnet_be (tun, argp); + ret =tun_set_vnet_be (&tun->flags, argp);break ;case TUNATTACHFILTER :Signature · 2 lines
-- 2.47.1
Willem de Bruijn
maintainerRe: [PATCH net v3 4/9] tun: Decouple vnet from tun_struct
Quoted 81 lines · Willem de Bruijn
Quoted 80 lines · Akihiko Odaki
Decouple vnet-related functions from
tun_structso that we can reuse them for tap in the future.Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/tun.c | 53 +++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 26 deletions(-)drivers/net/tun.c +13
-14@@ -298,16 +298,16 @@ static bool tun_napi_frags_enabled(const struct tun_file *tfile)return tfile->napi_frags_enabled; } -static inline bool tun_legacy_is_little_endian (struct tun_struct *tun) +static inline bool tun_legacy_is_little_endian (unsigned int flags) {return !(IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ) && - (tun->flags &TUN_VNET_BE )) && + (flags &TUN_VNET_BE )) &&virtio_legacy_is_little_endian (); } -static long tun_get_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_get_vnet_be (unsigned int flags,int __user *argp) { -int be = !!(tun->flags &TUN_VNET_BE ); +int be = !!(flags &TUN_VNET_BE );if (!IS_ENABLED (CONFIG_TUN_VNET_CROSS_LE ))return -EINVAL ;@@ -318,7 +318,7 @@ static long tun_get_vnet_be(struct tun_struct *tun, int __user *argp)return 0 ; } -static long tun_set_vnet_be (struct tun_struct *tun,int __user *argp) +static long tun_set_vnet_be (unsigned int *flags,int __user *argp) {int be;@@ -329,27 +329,26 @@ static long tun_set_vnet_be(struct tun_struct *tun, int __user *argp)return -EFAULT ;if (be) - tun->flags |=TUN_VNET_BE ; + *flags |=TUN_VNET_BE ;else - tun->flags &= ~TUN_VNET_BE ; + *flags &= ~TUN_VNET_BE ;return 0 ; } -static inline bool tun_is_little_endian (struct tun_struct *tun) +static inline bool tun_is_little_endian (unsigned int flags) { -return tun->flags &TUN_VNET_LE || -tun_legacy_is_little_endian (tun); +return flags &TUN_VNET_LE ||tun_legacy_is_little_endian (flags); } -static inline u16 tun16_to_cpu (struct tun_struct *tun,__virtio16 val) +static inline u16 tun16_to_cpu (unsigned int flags,__virtio16 val) { -return __virtio16_to_cpu (tun_is_little_endian (tun), val); +return __virtio16_to_cpu (tun_is_little_endian (flags), val); } -static inline __virtio16 cpu_to_tun16 (struct tun_struct *tun,u16 val) +static inline __virtio16 cpu_to_tun16 (unsigned int flags,u16 val) { -return __cpu_to_virtio16 (tun_is_little_endian (tun), val); +return __cpu_to_virtio16 (tun_is_little_endian (flags), val); }static inline u32 tun_hashfn (u32 rxhash)@@ -1764,6 +1763,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,
Quoted 6 lines
drivers/net/tun.c static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, · line 1766
Quoted 3 lines
drivers/net/tun.c +1
-0if (tun->flags &IFF_VNET_HDR ) {int vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); +int flags = tun->flags;Here and elsewhere: instead of passing around and repeatedly parsing flags, have a variable
is_little_endian(oris_le)?
I guess this will not work everywhere, because endianness is not a boolean flag.. Code checks both TUN_VNET_LE and TUN_VNET_BE.
PATCH v3 5/9 tun: Decouple vnet handling
In reply to Akihiko OdakiDecouple the vnet handling code so that we can reuse it for tap.
--- drivers/net/tun.c | 229 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 133 insertions(+), 96 deletions(-)
drivers/net/tun.c +133 -96
@@ -351,6 +351,122 @@ static inline __virtio16 cpu_to_tun16(unsigned int flags, u16 val)return __cpu_to_virtio16 (tun_is_little_endian (flags), val); } +static long tun_vnet_ioctl (int *sz,unsigned int *flags, +unsigned int cmd,int __user *sp) +{ +int s; + +switch (cmd) { +case TUNGETVNETHDRSZ : + s = *sz; +if (put_user (s, sp)) +return -EFAULT ; +return 0 ; + +case TUNSETVNETHDRSZ : +if (get_user (s, sp)) +return -EFAULT ; +if (s < (int )sizeof (struct virtio_net_hdr )) +return -EINVAL ; + + *sz = s; +return 0 ; + +case TUNGETVNETLE : + s = !!(*flags &TUN_VNET_LE ); +if (put_user (s, sp)) +return -EFAULT ; +return 0 ; + +case TUNSETVNETLE : +if (get_user (s, sp)) +return -EFAULT ; +if (s) + *flags |=TUN_VNET_LE ; +else + *flags &= ~TUN_VNET_LE ; +return 0 ; + +case TUNGETVNETBE : +return tun_get_vnet_be (*flags, sp); + +case TUNSETVNETBE : +return tun_set_vnet_be (flags, sp); + +default : +return -EINVAL ; + } +} + +static int tun_vnet_hdr_get (int sz,unsigned int flags,struct iov_iter *from, +struct virtio_net_hdr *hdr) +{ +if (iov_iter_count (from) < sz) +return -EINVAL ; + +if (!copy_from_iter_full (hdr,sizeof (*hdr), from)) +return -EFAULT ; + +if ((hdr->flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) && +tun16_to_cpu (flags, hdr->csum_start) +tun16_to_cpu (flags, hdr->csum_offset) +2 >tun16_to_cpu (flags, hdr->hdr_len)) + hdr->hdr_len =cpu_to_tun16 (flags,tun16_to_cpu (flags, hdr->csum_start) +tun16_to_cpu (flags, hdr->csum_offset) +2 ); + +if (tun16_to_cpu (flags, hdr->hdr_len) >iov_iter_count (from)) +return -EINVAL ; + +iov_iter_advance (from, sz -sizeof (*hdr)); + +return tun16_to_cpu (flags, hdr->hdr_len); +} + +static int tun_vnet_hdr_put (int sz,struct iov_iter *iter, +const struct virtio_net_hdr *hdr) +{ +if (unlikely (iov_iter_count (iter) < sz)) +return -EINVAL ; + +if (unlikely (copy_to_iter (hdr,sizeof (*hdr), iter) !=sizeof (*hdr))) +return -EFAULT ; + +iov_iter_advance (iter, sz -sizeof (*hdr)); + +return 0 ; +} + +static int tun_vnet_hdr_to_skb (unsigned int flags,struct sk_buff *skb, +const struct virtio_net_hdr *hdr) +{ +return virtio_net_hdr_to_skb (skb, hdr,tun_is_little_endian (flags)); +} + +static int tun_vnet_hdr_from_skb (unsigned int flags, +const struct net_device *dev, +const struct sk_buff *skb, +struct virtio_net_hdr *hdr) +{ +int vlan_hlen =skb_vlan_tag_present (skb) ?VLAN_HLEN :0 ; + +if (virtio_net_hdr_from_skb (skb, hdr, +tun_is_little_endian (flags), true, + vlan_hlen)) { +struct skb_shared_info *sinfo =skb_shinfo (skb); + +if (net_ratelimit ()) { +netdev_err (dev,"unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n" , + sinfo->gso_type,tun16_to_cpu (flags, hdr->gso_size), +tun16_to_cpu (flags, hdr->hdr_len)); +print_hex_dump (KERN_ERR ,"tun: " , +DUMP_PREFIX_NONE , +16 ,1 , skb->head, +min (tun16_to_cpu (flags, hdr->hdr_len),64 ), true); + } +WARN_ON_ONCE (1 ); +return -EINVAL ; + } + +return 0 ; +} +static inline u32 tun_hashfn (u32 rxhash) {return rxhash &TUN_MASK_FLOW_ENTRIES ;
@@ -1763,22 +1879,10 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile,if (tun->flags &IFF_VNET_HDR ) {int vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); -int flags = tun->flags; - -if (iov_iter_count (from) < vnet_hdr_sz) -return -EINVAL ; - -if (!copy_from_iter_full (&gso,sizeof (gso), from)) -return -EFAULT ; -if ((gso.flags &VIRTIO_NET_HDR_F_NEEDS_CSUM ) && -tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 >tun16_to_cpu (flags, gso.hdr_len)) - gso.hdr_len =cpu_to_tun16 (flags,tun16_to_cpu (flags, gso.csum_start) +tun16_to_cpu (flags, gso.csum_offset) +2 ); - -if (tun16_to_cpu (flags, gso.hdr_len) >iov_iter_count (from)) -return -EINVAL ; - hdr_len =tun16_to_cpu (flags, gso.hdr_len); -iov_iter_advance (from, vnet_hdr_sz -sizeof (gso)); + hdr_len =tun_vnet_hdr_get (vnet_hdr_sz, tun->flags, from, &gso); +if (hdr_len <0 ) +return hdr_len; } len =iov_iter_count (from);
@@ -1854,7 +1958,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, } } -if (virtio_net_hdr_to_skb (skb, &gso,tun_is_little_endian (tun->flags))) { +if (tun_vnet_hdr_to_skb (tun->flags, skb, &gso)) {atomic_long_inc (&tun->rx_frame_errors); err = -EINVAL ; goto free_skb;
@@ -2049,18 +2153,15 @@ static ssize_t tun_put_user_xdp(struct tun_struct *tun, {int vnet_hdr_sz =0 ;size_t size = xdp_frame->len; -size_t ret; +ssize_t ret;if (tun->flags &IFF_VNET_HDR ) {struct virtio_net_hdr gso = {0 }; vnet_hdr_sz =READ_ONCE (tun->vnet_hdr_sz); -if (unlikely (iov_iter_count (iter) < vnet_hdr_sz)) -return -EINVAL ; -if (unlikely (copy_to_iter (&gso,sizeof (gso), iter) != -sizeof (gso))) -return -EFAULT ; -iov_iter_advance (iter, vnet_hdr_sz -sizeof (gso)); + ret =tun_vnet_hdr_put (vnet_hdr_sz, iter, &gso); +if (ret) +return ret; } ret =copy_to_iter (xdp_frame->data, size, iter) + vnet_hdr_sz;
@@ -2083,6 +2184,7 @@ static ssize_t tun_put_user(struct tun_struct *tun,int vlan_offset =0 ;int vlan_hlen =0 ;int vnet_hdr_sz =0 ; +int ret;if (skb_vlan_tag_present (skb)) vlan_hlen =VLAN_HLEN ;
@@ -2108,33 +2210,14 @@ static ssize_t tun_put_user(struct tun_struct *tun,if (vnet_hdr_sz) {struct virtio_net_hdr gso; -int flags = tun->flags; - -if (iov_iter_count (iter) < vnet_hdr_sz) -return -EINVAL ; - -if (virtio_net_hdr_from_skb (skb, &gso, -tun_is_little_endian (flags), true, - vlan_hlen)) { -struct skb_shared_info *sinfo =skb_shinfo (skb); - -if (net_ratelimit ()) { -netdev_err (tun->dev,"unexpected GSO type: 0x%x, gso_size %d, hdr_len %d\n" , - sinfo->gso_type,tun16_to_cpu (flags, gso.gso_size), -tun16_to_cpu (flags, gso.hdr_len)); -print_hex_dump (KERN_ERR ,"tun: " , -DUMP_PREFIX_NONE , -16 ,1 , skb->head, -min ((int )tun16_to_cpu (flags, gso.hdr_len),64 ), true); - } -WARN_ON_ONCE (1 ); -return -EINVAL ; - } -if (copy_to_iter (&gso,sizeof (gso), iter) !=sizeof (gso)) -return -EFAULT ; + ret =tun_vnet_hdr_from_skb (tun->flags, tun->dev, skb, &gso); +if (ret) +return ret; -iov_iter_advance (iter, vnet_hdr_sz -sizeof (gso)); + ret =tun_vnet_hdr_put (vnet_hdr_sz, iter, &gso); +if (ret) +return ret; }if (vlan_hlen) {
@@ -2494,7 +2577,7 @@ static int tun_xdp_one(struct tun_struct *tun,skb_reserve (skb, xdp->data - xdp->data_hard_start);skb_put (skb, xdp->data_end - xdp->data); -if (virtio_net_hdr_to_skb (skb, gso,tun_is_little_endian (tun->flags))) { +if (tun_vnet_hdr_to_skb (tun->flags, skb, gso)) {atomic_long_inc (&tun->rx_frame_errors);kfree_skb (skb); ret = -EINVAL ;
@@ -3078,8 +3161,6 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,kgid_t group;int ifindex;int sndbuf; -int vnet_hdr_sz; -int le;int ret;bool do_notify = false;
@@ -3286,50 +3367,6 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,tun_set_sndbuf (tun);break ; -case TUNGETVNETHDRSZ : - vnet_hdr_sz = tun->vnet_hdr_sz; -if (copy_to_user (argp, &vnet_hdr_sz,sizeof (vnet_hdr_sz))) - ret = -EFAULT ; -break ; - -case TUNSETVNETHDRSZ : -if (copy_from_user (&vnet_hdr_sz, argp,sizeof (vnet_hdr_sz))) { - ret = -EFAULT ; -break ; - } -if (vnet_hdr_sz < (int )sizeof (struct virtio_net_hdr )) { - ret = -EINVAL ; -break ; - } - - tun->vnet_hdr_sz = vnet_hdr_sz; -break ; - -case TUNGETVNETLE : - le = !!(tun->flags &TUN_VNET_LE ); -if (put_user (le, (int __user *)argp)) - ret = -EFAULT ; -break ; - -case TUNSETVNETLE : -if (get_user (le, (int __user *)argp)) { - ret = -EFAULT ; -break ; - } -if (le) - tun->flags |=TUN_VNET_LE ; -else - tun->flags &= ~TUN_VNET_LE ; -break ; - -case TUNGETVNETBE : - ret =tun_get_vnet_be (tun->flags, argp); -break ; - -case TUNSETVNETBE : - ret =tun_set_vnet_be (&tun->flags, argp); -break ; -case TUNATTACHFILTER :/* Can be set only for TAPs */ ret = -EINVAL ;
@@ -3385,7 +3422,7 @@ static long __tun_chr_ioctl(struct file *file, unsigned int cmd,break ;default : - ret = -EINVAL ; + ret =tun_vnet_ioctl (&tun->vnet_hdr_sz, &tun->flags, cmd, argp);break ; }
Signature · 1 line
2.47.1
Willem de Bruijn
maintainerRe: [PATCH net v3 5/9] tun: Decouple vnet handling
Quoted 12 lines · Akihiko Odaki
Decouple the vnet handling code so that we can reuse it for tap.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/tun.c | 229 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 133 insertions(+), 96 deletions(-)drivers/net/tun.c +0
-0
drivers/net/tun.c static inline __virtio16 cpu_to_tun16(unsigned int flags, u16 val) · line 356
Quoted 6 lines
drivers/net/tun.c +3
-0@@ -351,6 +351,122 @@ static inline __virtio16 cpu_to_tun16(unsigned int flags, u16 val)return __cpu_to_virtio16 (tun_is_little_endian (flags), val); } +static long tun_vnet_ioctl (int *sz,unsigned int *flags, +unsigned int cmd,int __user *sp) +{
please use vnet_hdr_len_sz instead of sz. It's a bit too cryptic for a casual reader to understand the meaning.
PATCH v3 6/9 tun: Extract the vnet handling code
In reply to Akihiko OdakiThe vnet handling code will be reused by tap.
--- MAINTAINERS | 2 +- drivers/net/Makefile | 3 +- drivers/net/tun.c | 174 +----------------------------------------------- drivers/net/tun_vnet.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ drivers/net/tun_vnet.h | 25 +++++++ 5 files changed, 205 insertions(+), 174 deletions(-)
MAINTAINERS +1 -1
@@ -23902,7 +23902,7 @@ W: http://vtun.sourceforge.net/tun F: Documentation/networking/tuntap.rst F: arch/um/os-Linux/drivers/ F: drivers/net/tap.c -F: drivers/net/tun.c +F: drivers/net/tun* TURBOCHANNEL SUBSYSTEM M: "Maciej W. Rozycki" <[email protected]>
drivers/net/Makefile +2 -1
@@ -29,7 +29,8 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun.o +obj-$(CONFIG_TUN) += tun-drv.o +tun-drv-y := tun.o tun_vnet.o obj-$(CONFIG_TAP) += tap.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
drivers/net/tun.c +2 -172
192 lines not on this page · raw message
drivers/net/tun_vnet.c new file +175 -0
175 lines not on this page · raw message
drivers/net/tun_vnet.h new file +25 -0
@@ -0,0 +1,25 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef TUN_VNET_H +#define TUN_VNET_H + +#include <linux/if_tun.h> +#include <linux/virtio_net.h> + +long tun_vnet_ioctl (int *sz,unsigned int *flags, +unsigned int cmd,int __user *sp); + +int tun_vnet_hdr_get (int sz,unsigned int flags,struct iov_iter *from, +struct virtio_net_hdr *hdr); + +int tun_vnet_hdr_put (int sz,struct iov_iter *iter, +const struct virtio_net_hdr *hdr); + +int tun_vnet_hdr_to_skb (unsigned int flags,struct sk_buff *skb, +const struct virtio_net_hdr *hdr); + +int tun_vnet_hdr_from_skb (unsigned int flags, +const struct net_device *dev, +const struct sk_buff *skb, +struct virtio_net_hdr *hdr); + +#endif /* TUN_VNET_H */
Signature · 1 line
2.47.1
Willem de Bruijn
maintainerRe: [PATCH net v3 6/9] tun: Extract the vnet handling code
Quoted 29 lines · Akihiko Odaki
The vnet handling code will be reused by tap.
Signed-off-by Akihiko Odaki <[email protected]>--- MAINTAINERS | 2 +- drivers/net/Makefile | 3 +- drivers/net/tun.c | 174 +----------------------------------------------- drivers/net/tun_vnet.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++++ drivers/net/tun_vnet.h | 25 +++++++ 5 files changed, 205 insertions(+), 174 deletions(-)MAINTAINERS +1
-1@@ -23902,7 +23902,7 @@ W: http://vtun.sourceforge.net/tun F: Documentation/networking/tuntap.rst F: arch/um/os-Linux/drivers/ F: drivers/net/tap.c -F: drivers/net/tun.c +F: drivers/net/tun* TURBOCHANNEL SUBSYSTEM M: "Maciej W. Rozycki" <[email protected]>drivers/net/Makefile +0
-0
drivers/net/Makefile obj-y += mdio/ · line 36
Quoted 9 lines
drivers/net/Makefile +2
-1@@ -29,7 +29,8 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun.o +obj-$(CONFIG_TUN) += tun-drv.o +tun-drv-y := tun.o tun_vnet.o obj-$(CONFIG_TAP) += tap.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
Let's figure out how to do this. See also comment in 9/9.
Otherwise the rest of the patch looks fine.
PATCH v3 7/9 tap: Avoid double-tracking iov_iter length changes
In reply to Akihiko Odakitap_get_user() used to track the length of iov_iter with another variable. We can use iov_iter_count() to determine the current length to avoid such chores.
--- drivers/net/tap.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/tap.c +5 -4
@@ -641,7 +641,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,struct sk_buff *skb;struct tap_dev *tap;unsigned long total_len =iov_iter_count (from); -unsigned long len = total_len; +unsigned long len;int err;struct virtio_net_hdr vnet_hdr = {0 };int vnet_hdr_len =0 ;
@@ -655,9 +655,8 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, vnet_hdr_len =READ_ONCE (q->vnet_hdr_sz); err = -EINVAL ; -if (len < vnet_hdr_len) +if (iov_iter_count (from) < vnet_hdr_len) goto err; - len -= vnet_hdr_len; err = -EFAULT ;if (!copy_from_iter_full (&vnet_hdr,sizeof (vnet_hdr), from))
@@ -671,10 +670,12 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,tap16_to_cpu (q, vnet_hdr.csum_start) + tap16_to_cpu(q, vnet_hdr.csum_offset) + 2); err = -EINVAL ; -if (tap16_to_cpu (q, vnet_hdr.hdr_len) > len) +if (tap16_to_cpu (q, vnet_hdr.hdr_len) >iov_iter_count (from)) goto err; } + len =iov_iter_count (from); + err = -EINVAL ;if (unlikely (len <ETH_HLEN )) goto err;
Signature · 1 line
2.47.1
PATCH v3 8/9 tap: Keep hdr_len in tap_get_user()
In reply to Akihiko Odakihdr_len is repeatedly used so keep it in a local variable.
--- drivers/net/tap.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/net/tap.c +7 -10
@@ -645,6 +645,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,int err;struct virtio_net_hdr vnet_hdr = {0 };int vnet_hdr_len =0 ; +int hdr_len =0 ;int copylen =0 ;int depth;bool zerocopy = false;
@@ -672,6 +673,7 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control, err = -EINVAL ;if (tap16_to_cpu (q, vnet_hdr.hdr_len) >iov_iter_count (from)) goto err; + hdr_len =tap16_to_cpu (q, vnet_hdr.hdr_len); } len =iov_iter_count (from);
@@ -683,11 +685,8 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (msg_control &&sock_flag (&q->sk,SOCK_ZEROCOPY )) {struct iov_iter i; - copylen = vnet_hdr.hdr_len ? -tap16_to_cpu (q, vnet_hdr.hdr_len) :GOODCOPY_LEN ; -if (copylen > good_linear) - copylen = good_linear; -else if (copylen <ETH_HLEN ) + copylen =min (hdr_len ? hdr_len :GOODCOPY_LEN , good_linear); +if (copylen <ETH_HLEN ) copylen =ETH_HLEN ; linear = copylen; i = *from;
@@ -698,11 +697,9 @@ static ssize_t tap_get_user(struct tap_queue *q, void *msg_control,if (!zerocopy) { copylen = len; - linear =tap16_to_cpu (q, vnet_hdr.hdr_len); -if (linear > good_linear) - linear = good_linear; -else if (linear <ETH_HLEN ) - linear =ETH_HLEN ; + linear =min (hdr_len, good_linear); +if (copylen <ETH_HLEN ) + copylen =ETH_HLEN ; } skb = tap_alloc_skb(&q->sk, TAP_RESERVE, copylen,
Signature · 1 line
2.47.1
PATCH v3 9/9 tap: Use tun's vnet-related code
In reply to Akihiko Odakitun and tap implements the same vnet-related features so reuse the code.
--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)
drivers/net/Kconfig +1 -0
@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernet
drivers/net/Makefile +3 -3
@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.o +obj-$(CONFIG_TAP) += tap-drv.o +tap-drv-y := tap.o tun_vnet.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VXLAN) += vxlan/
drivers/net/tap.c +15 -137
201 lines not on this page · raw message
drivers/net/tun_vnet.c +5 -0
@@ -104,6 +104,7 @@ long tun_vnet_ioctl(int *sz, unsigned int *flags,return -EINVAL ; } } +EXPORT_SYMBOL_GPL (tun_vnet_ioctl);int tun_vnet_hdr_get (int sz,unsigned int flags,struct iov_iter *from,struct virtio_net_hdr *hdr)
@@ -125,6 +126,7 @@ int tun_vnet_hdr_get(int sz, unsigned int flags, struct iov_iter *from,return tun16_to_cpu (flags, hdr->hdr_len); } +EXPORT_SYMBOL_GPL (tun_vnet_hdr_get);int tun_vnet_hdr_put (int sz,struct iov_iter *iter,const struct virtio_net_hdr *hdr)
@@ -139,12 +141,14 @@ int tun_vnet_hdr_put(int sz, struct iov_iter *iter,return 0 ; } +EXPORT_SYMBOL_GPL (tun_vnet_hdr_put);int tun_vnet_hdr_to_skb (unsigned int flags,struct sk_buff *skb,const struct virtio_net_hdr *hdr) {return virtio_net_hdr_to_skb (skb, hdr,tun_is_little_endian (flags)); } +EXPORT_SYMBOL_GPL (tun_vnet_hdr_to_skb); int tun_vnet_hdr_from_skb(unsigned int flags, const struct net_device *dev,
@@ -173,3 +177,4 @@ int tun_vnet_hdr_from_skb(unsigned int flags,return 0 ; } +EXPORT_SYMBOL_GPL (tun_vnet_hdr_from_skb);
Signature · 1 line
2.47.1
Willem de Bruijn
maintainerRe: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0
drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.o
Is reversing the previous changes to tun.ko intentional?
Perhaps the previous approach with a new CONFIG_TUN_VNET is preferable over this. In particular over making TUN select TAP, a new dependency.
Quoted 5 lines
drivers/net/Makefile +2
-0+obj-$(CONFIG_TAP) += tap-drv.o +tap-drv-y := tap.o tun_vnet.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VXLAN) += vxlan/
Re: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 38 lines · Willem de Bruijn
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.oIs reversing the previous changes to tun.ko intentional?
Quoted 2 lines
Perhaps the previous approach with a new
CONFIG_TUN_VNETis preferable over this. In particular over making TUN select TAP, a new dependency.
Jason, you also commented about CONFIG_TUN_VNET for the previous version. Do you prefer the old approach, or the new one? (Or if you have another idea, please tell me.)
Quoted 6 lines
Quoted 6 lines
drivers/net/Makefile +2
-0+obj-$(CONFIG_TAP) += tap-drv.o +tap-drv-y := tap.o tun_vnet.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VXLAN) += vxlan/
Re: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 40 lines · Akihiko Odaki
Quoted 38 lines · Willem de Bruijn
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.oIs reversing the previous changes to tun.ko intentional?
Quoted 6 lines
Quoted 2 lines
Perhaps the previous approach with a new
CONFIG_TUN_VNETis preferable over this. In particular over making TUN select TAP, a new dependency.Jason, you also commented about
CONFIG_TUN_VNETfor the previous version. Do you prefer the old approach, or the new one? (Or if you have another idea, please tell me.)
Ideally, if we can make TUN select TAP that would be better. But there are some subtle differences in the multi queue implementation. We will end up with some useless code for TUN unless we can unify the multi queue logic. It might not be worth it to change the TUN's multi queue logic so having a new file seems to be better.
Thanks
Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
drivers/net/Makefile +2
-0+obj-$(CONFIG_TAP) += tap-drv.o +tap-drv-y := tap.o tun_vnet.o obj-$(CONFIG_VETH) += veth.o obj-$(CONFIG_VIRTIO_NET) += virtio_net.o obj-$(CONFIG_VXLAN) += vxlan/
Re: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 49 lines · Jason Wang
Quoted 40 lines · Akihiko Odaki
Quoted 38 lines · Willem de Bruijn
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.oIs reversing the previous changes to tun.ko intentional?
Quoted 6 lines
Quoted 2 lines
Perhaps the previous approach with a new
CONFIG_TUN_VNETis preferable over this. In particular over making TUN select TAP, a new dependency.Jason, you also commented about
CONFIG_TUN_VNETfor the previous version. Do you prefer the old approach, or the new one? (Or if you have another idea, please tell me.)
Quoted 5 lines
Ideally, if we can make TUN select TAP that would be better. But there are some subtle differences in the multi queue implementation. We will end up with some useless code for TUN unless we can unify the multi queue logic. It might not be worth it to change the TUN's multi queue logic so having a new file seems to be better.
+1 on deduplicating further. But this series is complex enough. Let's not expand that.
The latest approach with a separate .o file may have some performance cost by converting likely inlined code into real function calls. Another option is to move it all into tun_vnet.h. That also resolves the Makefile issues.
Re: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 56 lines · Willem de Bruijn
Quoted 49 lines · Jason Wang
Quoted 40 lines · Akihiko Odaki
Quoted 38 lines · Willem de Bruijn
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.oIs reversing the previous changes to tun.ko intentional?
Quoted 6 lines
Quoted 2 lines
Perhaps the previous approach with a new
CONFIG_TUN_VNETis preferable over this. In particular over making TUN select TAP, a new dependency.Jason, you also commented about
CONFIG_TUN_VNETfor the previous version. Do you prefer the old approach, or the new one? (Or if you have another idea, please tell me.)Quoted 5 lines
Ideally, if we can make TUN select TAP that would be better. But there are some subtle differences in the multi queue implementation. We will end up with some useless code for TUN unless we can unify the multi queue logic. It might not be worth it to change the TUN's multi queue logic so having a new file seems to be better.
Quoted 7 lines
+1 on deduplicating further. But this series is complex enough. Let's not expand that.
The latest approach with a separate .o file may have some performance cost by converting likely inlined code into real function calls. Another option is to move it all into
tun_vnet.h. That also resolves theMakefileissues.
I measured the size difference between the latest inlining approaches. The numbers may vary depending on the system configuration of course, but they should be useful for reference.
The below shows sizes when having a separate module: 106496 bytes in total
# lsmod Module Size Used by tap 28672 0 tun 61440 0 tun_vnet 16384 2 tun,tap
The below shows sizes when inlining: 102400 bytes in total
# lsmod Module Size Used by tap 32768 0 tun 69632 0
So having a separate module costs 4096 bytes more.
These two approaches should have similar tendency for run-time and compile-time performance; the code is so trivial that the overhead of having one additional module is dominant.
The only downside of having all in tun_vnet.h is that it will expose its internal macros and functions, which I think tolerable.
Willem de Bruijn
maintainerRe: [PATCH net v3 9/9] tap: Use tun's vnet-related code
Quoted 84 lines · Akihiko Odaki
Quoted 56 lines · Willem de Bruijn
Quoted 49 lines · Jason Wang
Quoted 40 lines · Akihiko Odaki
Quoted 38 lines · Willem de Bruijn
Quoted 27 lines · Akihiko Odaki
tun and tap implements the same vnet-related features so reuse the code.
Signed-off-by Akihiko Odaki <[email protected]>--- drivers/net/Kconfig | 1 + drivers/net/Makefile | 6 +- drivers/net/tap.c | 152 +++++-------------------------------------------- drivers/net/tun_vnet.c | 5 ++ 4 files changed, 24 insertions(+), 140 deletions(-)drivers/net/Kconfig +1
-0@@ -395,6 +395,7 @@ config TUN tristate "Universal TUN/TAP device driver support" depends on INET select CRC32 + select TAP help TUN/TAP provides packet reception and transmission for user space programs. It can be viewed as a simple Point-to-Point or Ethernetdrivers/net/Makefile +0
-0drivers/net/Makefile obj-y += mdio/ · line 32
Quoted 7 lines
drivers/net/Makefile +1
-3@@ -29,9 +29,9 @@ obj-y += mdio/ obj-y += pcs/ obj-$(CONFIG_RIONET) += rionet.o obj-$(CONFIG_NET_TEAM) += team/ -obj-$(CONFIG_TUN) += tun-drv.o -tun-drv-y := tun.o tun_vnet.o -obj-$(CONFIG_TAP) += tap.o +obj-$(CONFIG_TUN) += tun.oIs reversing the previous changes to tun.ko intentional?
Quoted 6 lines
Quoted 2 lines
Perhaps the previous approach with a new
CONFIG_TUN_VNETis preferable over this. In particular over making TUN select TAP, a new dependency.Jason, you also commented about
CONFIG_TUN_VNETfor the previous version. Do you prefer the old approach, or the new one? (Or if you have another idea, please tell me.)Quoted 5 lines
Ideally, if we can make TUN select TAP that would be better. But there are some subtle differences in the multi queue implementation. We will end up with some useless code for TUN unless we can unify the multi queue logic. It might not be worth it to change the TUN's multi queue logic so having a new file seems to be better.
Quoted 7 lines
+1 on deduplicating further. But this series is complex enough. Let's not expand that.
The latest approach with a separate .o file may have some performance cost by converting likely inlined code into real function calls. Another option is to move it all into
tun_vnet.h. That also resolves theMakefileissues.I measured the size difference between the latest inlining approaches. The numbers may vary depending on the system configuration of course, but they should be useful for reference.
The below shows sizes when having a separate module: 106496 bytes in total
# lsmod Module Size Used by tap 28672 0 tun 61440 0 tun_vnet 16384 2 tun,tapThe below shows sizes when inlining: 102400 bytes in total
# lsmod Module Size Used by tap 32768 0 tun 69632 0
Quoted 5 lines
So having a separate module costs 4096 bytes more.
These two approaches should have similar tendency for run-time and compile-time performance; the code is so trivial that the overhead of having one additional module is dominant.
The concern raised was not about object size, but about inlined vs true calls in the hot path.
Quoted 2 lines
The only downside of having all in
tun_vnet.his that it will expose its internal macros and functions, which I think tolerable.
As long as only included by tun.c and tap.c, I think that's okay. The slow path code (ioctl) could remain in a .c file. But it's simpler to just have the header file.
Michael S. Tsirkin
maintainerQuoted 10 lines · Akihiko Odaki
When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().
This series fills the uninitialized hole. More concretely, the
num_buffersfield will be initialized with 1, and the other fields will be inialized with 0. Setting thenum_buffersfield to 1 is mandated by virtio 1.0 [2].
Quoted 7 lines
The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/Signed-off-by Akihiko Odaki <[email protected]>
Will review. But this does not look like net material to me. Not really a bugfix. More like net-next.
Quoted 39 lines
---
Changes in v3:
- Dropped changes to fill the vnet header.
- Splitted patch "tun: Unify vnet implementation".
- Reverted spurious changes in patch "tun: Unify vnet implementation".
- Mergedtun_vnet.cinto TAP.
- Link to v2: https://lore.kernel.org/r/[email protected]Changes in v2:
- Fixednum_buffersendian.
- Link to v1: https://lore.kernel.org/r/[email protected]---
Akihiko Odaki (9):
tun: RefactorCONFIG_TUN_VNET_CROSS_LE
tun: Avoid double-trackingiov_iterlength changes
tun: Keephdr_lenintun_get_user()
tun: Decouple vnet fromtun_struct
tun: Decouple vnet handling
tun: Extract the vnet handling code
tap: Avoid double-trackingiov_iterlength changes
tap: Keephdr_lenintap_get_user()
tap: Use tun's vnet-related codeMAINTAINERS | 2 +- drivers/net/Kconfig | 1 + drivers/net/Makefile | 3 +- drivers/net/tap.c | 172 ++++++------------------------------------ drivers/net/tun.c | 200 +++++++------------------------------------------ drivers/net/tun_vnet.c | 180 ++++++++++++++++++++++++++++++++++++++++++++ drivers/net/tun_vnet.h | 25 +++++++ 7 files changed, 260 insertions(+), 323 deletions(-)---
base-commit:a32e14f8aef69b42826cf0998b068a43d486a9e9
change-id: 20241230-tun-66e10a49b0c7Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
Willem de Bruijn
maintainerQuoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Akihiko Odaki
When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().
This series fills the uninitialized hole. More concretely, the
num_buffersfield will be initialized with 1, and the other fields will be inialized with 0. Setting thenum_buffersfield to 1 is mandated by virtio 1.0 [2].The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.
Quoted 7 lines
Quoted 4 lines
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/Signed-off-by Akihiko Odaki <[email protected]>Will review. But this does not look like net material to me. Not really a bugfix. More like net-next.
+1. I said basically the same in v2.
Perhaps the field initialization is net material, though not critical until hashing is merged, so not stable.
The deduplication does not belong in net.
IMHO it should all go to net-next.
Quoted 23 lines · Willem de Bruijn
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Akihiko Odaki
When I implemented virtio's hash-related features to tun/tap [1], I found tun/tap does not fill the entire region reserved for the virtio header, leaving some uninitialized hole in the middle of the buffer after read()/recvmesg().
This series fills the uninitialized hole. More concretely, the
num_buffersfield will be initialized with 1, and the other fields will be inialized with 0. Setting thenum_buffersfield to 1 is mandated by virtio 1.0 [2].The change to virtio header is preceded by another change that refactors tun and tap to unify their virtio-related code.
Quoted 7 lines
Quoted 4 lines
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/[email protected]/Signed-off-by Akihiko Odaki <[email protected]>Will review. But this does not look like net material to me. Not really a bugfix. More like net-next.
Quoted 8 lines
+1. I said basically the same in v2.
Perhaps the field initialization is net material, though not critical until hashing is merged, so not stable.
The deduplication does not belong in net.
IMHO it should all go to net-next.
I will post the future version for net-next. I also intend to post the field initialization for net-next too.