Skip to contentSkip to conversation

macvtap: Correctly set tap features when IFF_VNET_HDR is disabled.

PATCH Superseded Vlad Yasevich 3 participants 6 messages Aug 13, 2013 – Aug 14, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 macvtap: Correctly set tap features when IFF_VNET_HDR is disabled.

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

---
 drivers/net/macvtap.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +19 -8
index a98fb0e..076b9e7 100644
@@ -1019,6 +1019,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 	struct macvlan_dev *vlan;
 	netdev_features_t features;
 	netdev_features_t feature_mask = 0;
+	netdev_features_t tap_mask = TUN_OFFLOADS;
 
 	vlan = rtnl_dereference(q->vlan);
 	if (!vlan)
@@ -1026,7 +1027,12 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 
 	features = vlan->dev->features;
 
-	if (arg & TUN_F_CSUM) {
+	if (!(q->flags & IFF_VNET_HDR)) {
+		/* Turn off all checsum offloading also if user does
+		 * not user vnet_hdr.
+		 */
+		tap_mask |= NETIF_F_ALL_CSUM;
+	} else if (arg & TUN_F_CSUM) {
 		feature_mask = NETIF_F_HW_CSUM;
 
 		if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
@@ -1058,8 +1064,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 	/* tap_features are the same as features on tun/tap and
 	 * reflect user expectations.
 	 */
-	vlan->tap_features = vlan->dev->features &
-			    (feature_mask | ~TUN_OFFLOADS);
+	vlan->tap_features = vlan->dev->features & (feature_mask | ~tap_mask);
 	vlan->set_features = features;
 	netdev_update_features(vlan->dev);
 
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
 		if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
 		    (IFF_NO_PI | IFF_TAP))
 			ret = -EINVAL;
-		else
+		else {
+			if ((q->flags ^ u) & IFF_VNET_HDR) {
+				/* vnet_hdr support impacts the offloads,
+				 * so we need to run throught the offload
+				 * change.
+				 */
+				rtnl_lock();
+				ret = set_offload(q, 0);
+				rtnl_unlock();
+			}
 			q->flags = u;
+		}
 
 		return ret;
 
@@ -1155,10 +1170,6 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
 			    TUN_F_TSO_ECN | TUN_F_UFO))
 			return -EINVAL;
 
-		/* TODO: only accept frames with the features that
-			 got enabled for forwarded frames */
-		if (!(q->flags & IFF_VNET_HDR))
-			return  -EINVAL;
 		rtnl_lock();
 		ret = set_offload(q, arg);
 		rtnl_unlock();
Signature · 1 line
1.8.1.4
#1 lore raw

Hello.

Quoted 20 lines · Vlad Yasevich

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

---
  drivers/net/macvtap.c | 27 +++++++++++++++++++--------
  1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +0 -0
index a98fb0e..076b9e7 100644

[...]

Quoted 7 lines
drivers/net/macvtap.c +2 -1
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
  		if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  		    (IFF_NO_PI | IFF_TAP))
  			ret = -EINVAL;
-		else
+		else {
+			if ((q->flags ^ u) & IFF_VNET_HDR) {

drivers/net/macvtap.c static long macvtap_ioctl(struct file *file, unsigned int cmd, · line 1111

Quoted 10 lines
drivers/net/macvtap.c +9 -0
+				/* vnet_hdr support impacts the offloads,
+				 * so we need to run throught the offload
+				 * change.
+				 */
+				rtnl_lock();
+				ret = set_offload(q, 0);
+				rtnl_unlock();
+			}
  			q->flags = u;
+		}

According to Documentation/CodingStyle, both arms of the *if* statement
should have {} if one arm has it.

WBR, Sergei

#2 lore raw
In reply to Vlad Yasevich
Quoted 5 lines · Vlad Yasevich

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

Quoted 7 lines

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

Hmm, this seems to make it even messier.
So
set TUNSETOFFLOAD
clear IFF_VNET_HDR
set IFF_VNET_HDR

offloads are now clear?

I'm also not sure userspace that sets offloads but clears vnet hdr knows what it's doing.

How about we fail attempts to clear vnet hdr
unless offloads are disabled?

Quoted 17 lines
---
 drivers/net/macvtap.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +1 -0
index a98fb0e..076b9e7 100644
@@ -1019,6 +1019,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 	struct macvlan_dev *vlan;
 	netdev_features_t features;
 	netdev_features_t feature_mask = 0;
+	netdev_features_t tap_mask = TUN_OFFLOADS;
 
 	vlan = rtnl_dereference(q->vlan);
 	if (!vlan)

drivers/net/macvtap.c static int set_offload(struct macvtap_queue *q, unsigned long arg) · line 1032

Quoted 7 lines
drivers/net/macvtap.c +3 -1
@@ -1026,7 +1027,12 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 
 	features = vlan->dev->features;
 
-	if (arg & TUN_F_CSUM) {
+	if (!(q->flags & IFF_VNET_HDR)) {
+		/* Turn off all checsum offloading also if user does
+		 * not user vnet_hdr.

Does not set?

Quoted 17 lines
drivers/net/macvtap.c +4 -2
+		 */
+		tap_mask |= NETIF_F_ALL_CSUM;
+	} else if (arg & TUN_F_CSUM) {
 		feature_mask = NETIF_F_HW_CSUM;
 
 		if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
@@ -1058,8 +1064,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 	/* tap_features are the same as features on tun/tap and
 	 * reflect user expectations.
 	 */
-	vlan->tap_features = vlan->dev->features &
-			    (feature_mask | ~TUN_OFFLOADS);
+	vlan->tap_features = vlan->dev->features & (feature_mask | ~tap_mask);
 	vlan->set_features = features;
 	netdev_update_features(vlan->dev);
 

drivers/net/macvtap.c static long macvtap_ioctl(struct file *file, unsigned int cmd, · line 1103

Quoted 8 lines
drivers/net/macvtap.c +4 -1
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
 		if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
 		    (IFF_NO_PI | IFF_TAP))
 			ret = -EINVAL;
-		else
+		else {
+			if ((q->flags ^ u) & IFF_VNET_HDR) {
+				/* vnet_hdr support impacts the offloads,
+				 * so we need to run throught the offload

throught ?

Quoted 24 lines
drivers/net/macvtap.c +7 -4
+				 * change.
+				 */
+				rtnl_lock();
+				ret = set_offload(q, 0);
+				rtnl_unlock();
+			}
 			q->flags = u;
+		}
 
 		return ret;
 
@@ -1155,10 +1170,6 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
 			    TUN_F_TSO_ECN | TUN_F_UFO))
 			return -EINVAL;
 
-		/* TODO: only accept frames with the features that
-			 got enabled for forwarded frames */
-		if (!(q->flags & IFF_VNET_HDR))
-			return  -EINVAL;
 		rtnl_lock();
 		ret = set_offload(q, arg);
 		rtnl_unlock();
Signature · 1 line
1.8.1.4
#3 lore raw
Quoted 20 lines · Michael S. Tsirkin
Quoted 5 lines · Vlad Yasevich

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

Quoted 7 lines

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

Hmm, this seems to make it even messier.
So
set TUNSETOFFLOAD
clear IFF_VNET_HDR
set IFF_VNET_HDR

Quoted 8 lines

offloads are now clear?

I'm also not sure userspace that sets offloads but clears vnet hdr knows what it's doing.

How about we fail attempts to clear vnet hdr
unless offloads are disabled?

OK. That makes it simpler...

-vlad

Quoted 82 lines
Quoted 20 lines


---
  drivers/net/macvtap.c | 27 +++++++++++++++++++--------
  1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +1 -0
index a98fb0e..076b9e7 100644
@@ -1019,6 +1019,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
  	struct macvlan_dev *vlan;
  	netdev_features_t features;
  	netdev_features_t feature_mask = 0;
+	netdev_features_t tap_mask = TUN_OFFLOADS;
 
  	vlan = rtnl_dereference(q->vlan);
  	if (!vlan)

drivers/net/macvtap.c static int set_offload(struct macvtap_queue *q, unsigned long arg) · line 1032

Quoted 7 lines
drivers/net/macvtap.c +3 -1
@@ -1026,7 +1027,12 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
 
  	features = vlan->dev->features;
 
-	if (arg & TUN_F_CSUM) {
+	if (!(q->flags & IFF_VNET_HDR)) {
+		/* Turn off all checsum offloading also if user does
+		 * not user vnet_hdr.

Does not set?

Quoted 17 lines
drivers/net/macvtap.c +4 -2
+		 */
+		tap_mask |= NETIF_F_ALL_CSUM;
+	} else if (arg & TUN_F_CSUM) {
  		feature_mask = NETIF_F_HW_CSUM;
 
  		if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
@@ -1058,8 +1064,7 @@ static int set_offload(struct macvtap_queue *q, unsigned long arg)
  	/* tap_features are the same as features on tun/tap and
  	 * reflect user expectations.
  	 */
-	vlan->tap_features = vlan->dev->features &
-			    (feature_mask | ~TUN_OFFLOADS);
+	vlan->tap_features = vlan->dev->features & (feature_mask | ~tap_mask);
  	vlan->set_features = features;
  	netdev_update_features(vlan->dev);
 

drivers/net/macvtap.c static long macvtap_ioctl(struct file *file, unsigned int cmd, · line 1103

Quoted 8 lines
drivers/net/macvtap.c +4 -1
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
  		if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
  		    (IFF_NO_PI | IFF_TAP))
  			ret = -EINVAL;
-		else
+		else {
+			if ((q->flags ^ u) & IFF_VNET_HDR) {
+				/* vnet_hdr support impacts the offloads,
+				 * so we need to run throught the offload

throught ?

Quoted 24 lines
drivers/net/macvtap.c +7 -5
+				 * change.
+				 */
+				rtnl_lock();
+				ret = set_offload(q, 0);
+				rtnl_unlock();
+			}
  			q->flags = u;
+		}
 
  		return ret;
 
@@ -1155,10 +1170,6 @@ static long macvtap_ioctl(struct file *file, unsigned int cmd,
  			    TUN_F_TSO_ECN | TUN_F_UFO))
  			return -EINVAL;
 
-		/* TODO: only accept frames with the features that
-			 got enabled for forwarded frames */
-		if (!(q->flags & IFF_VNET_HDR))
-			return  -EINVAL;
  		rtnl_lock();
  		ret = set_offload(q, arg);
  		rtnl_unlock();
--

1.8.1.4

#4 lore raw
Quoted 23 lines · Vlad Yasevich
Quoted 22 lines · Michael S. Tsirkin
Quoted 5 lines · Vlad Yasevich

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

Quoted 7 lines

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

Hmm, this seems to make it even messier.
So
set TUNSETOFFLOAD
clear IFF_VNET_HDR
set IFF_VNET_HDR

offloads are now clear?

Quoted 8 lines
Quoted 6 lines

I'm also not sure userspace that sets offloads but clears vnet hdr knows what it's doing.

How about we fail attempts to clear vnet hdr
unless offloads are disabled?

OK. That makes it simpler...

Spoke too soon.

What happens is that libvirt is typically the one that turns off IFF_VNET_HDR, and qemu controls the offloads.

So, if we forbid turning off vnet_hdr while offloads are set, that will break libvirt (this is because macvtap defaults to tap offloads being enabled).

-vlad

Quoted 92 lines

-vlad

Quoted 88 lines
Quoted 21 lines


---
  drivers/net/macvtap.c | 27 +++++++++++++++++++--------
  1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +1 -0
index a98fb0e..076b9e7 100644
@@ -1019,6 +1019,7 @@ static int set_offload(struct macvtap_queue *q,
 unsigned long arg)
      struct macvlan_dev *vlan;
      netdev_features_t features;
      netdev_features_t feature_mask = 0;
+    netdev_features_t tap_mask = TUN_OFFLOADS;
 
      vlan = rtnl_dereference(q->vlan);
      if (!vlan)

drivers/net/macvtap.c static int set_offload(struct macvtap_queue · line 1033

Quoted 8 lines
drivers/net/macvtap.c +3 -1
@@ -1026,7 +1027,12 @@ static int set_offload(struct macvtap_queue
 *q, unsigned long arg)
 
      features = vlan->dev->features;
 
-    if (arg & TUN_F_CSUM) {
+    if (!(q->flags & IFF_VNET_HDR)) {
+        /* Turn off all checsum offloading also if user does
+         * not user vnet_hdr.

Does not set?

Quoted 19 lines
drivers/net/macvtap.c +3 -0
+         */
+        tap_mask |= NETIF_F_ALL_CSUM;
+    } else if (arg & TUN_F_CSUM) {
          feature_mask = NETIF_F_HW_CSUM;
 
          if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
drivers/net/macvtap.c +1 -2
@@ -1058,8 +1064,7 @@ static int set_offload(struct macvtap_queue *q,
 unsigned long arg)
      /* tap_features are the same as features on tun/tap and
       * reflect user expectations.
       */
-    vlan->tap_features = vlan->dev->features &
-                (feature_mask | ~TUN_OFFLOADS);
+    vlan->tap_features = vlan->dev->features & (feature_mask |
 ~tap_mask);
      vlan->set_features = features;
      netdev_update_features(vlan->dev);

drivers/net/macvtap.c static long macvtap_ioctl(struct file *file, · line 1104

Quoted 9 lines
drivers/net/macvtap.c +4 -1
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file,
 unsigned int cmd,
          if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
              (IFF_NO_PI | IFF_TAP))
              ret = -EINVAL;
-        else
+        else {
+            if ((q->flags ^ u) & IFF_VNET_HDR) {
+                /* vnet_hdr support impacts the offloads,
+                 * so we need to run throught the offload

throught ?

Quoted 25 lines
drivers/net/macvtap.c +7 -4
+                 * change.
+                 */
+                rtnl_lock();
+                ret = set_offload(q, 0);
+                rtnl_unlock();
+            }
              q->flags = u;
+        }
 
          return ret;
@@ -1155,10 +1170,6 @@ static long macvtap_ioctl(struct file *file,
 unsigned int cmd,
                  TUN_F_TSO_ECN | TUN_F_UFO))
              return -EINVAL;
 
-        /* TODO: only accept frames with the features that
-             got enabled for forwarded frames */
-        if (!(q->flags & IFF_VNET_HDR))
-            return  -EINVAL;
          rtnl_lock();
          ret = set_offload(q, arg);
          rtnl_unlock();

--
1.8.1.4

#5 lore raw
Quoted 35 lines · Vlad Yasevich
Quoted 23 lines · Vlad Yasevich
Quoted 22 lines · Michael S. Tsirkin
Quoted 5 lines · Vlad Yasevich

When the user turns off IFF_VNET_HDR flag, attempts to change offload features via TUNSETOFFLOAD do not work. This could cause GSO packets to be delivered to the user when the user is not prepared to handle them.

Quoted 7 lines

To solve, allow processing of TUNSETOFFLOAD when IFF_VNET_HDR is disabled and make sure to turn off all offloads in this case. Also, when IFF_VNET_HDR is disabled, run throught the offload change as well to make sure that the functionality is not dependent on the order of the ioclt() calls.

Hmm, this seems to make it even messier.
So
set TUNSETOFFLOAD
clear IFF_VNET_HDR
set IFF_VNET_HDR

offloads are now clear?

Quoted 8 lines
Quoted 6 lines

I'm also not sure userspace that sets offloads but clears vnet hdr knows what it's doing.

How about we fail attempts to clear vnet hdr
unless offloads are disabled?

OK. That makes it simpler...

Spoke too soon.

Quoted 8 lines

What happens is that libvirt is typically the one that turns off IFF_VNET_HDR, and qemu controls the offloads.

So, if we forbid turning off vnet_hdr while offloads are set, that will break libvirt (this is because macvtap defaults to tap offloads being enabled).

-vlad

Well what prevents libvirt from disabling offloads? Anyway, another approach is to say that this is up to user - if user disables hdr but not offloads, user will get packets without a checksum.

Quoted 92 lines
Quoted 92 lines

-vlad

Quoted 88 lines
Quoted 21 lines


---
 drivers/net/macvtap.c | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)
drivers/net/macvtap.c +1 -0
index a98fb0e..076b9e7 100644
@@ -1019,6 +1019,7 @@ static int set_offload(struct macvtap_queue *q,
 unsigned long arg)
     struct macvlan_dev *vlan;
     netdev_features_t features;
     netdev_features_t feature_mask = 0;
+    netdev_features_t tap_mask = TUN_OFFLOADS;
 
     vlan = rtnl_dereference(q->vlan);
     if (!vlan)

drivers/net/macvtap.c static int set_offload(struct macvtap_queue · line 1033

Quoted 8 lines
drivers/net/macvtap.c +3 -1
@@ -1026,7 +1027,12 @@ static int set_offload(struct macvtap_queue
 *q, unsigned long arg)
 
     features = vlan->dev->features;
 
-    if (arg & TUN_F_CSUM) {
+    if (!(q->flags & IFF_VNET_HDR)) {
+        /* Turn off all checsum offloading also if user does
+         * not user vnet_hdr.

Does not set?

Quoted 19 lines
drivers/net/macvtap.c +3 -0
+         */
+        tap_mask |= NETIF_F_ALL_CSUM;
+    } else if (arg & TUN_F_CSUM) {
         feature_mask = NETIF_F_HW_CSUM;
 
         if (arg & (TUN_F_TSO4 | TUN_F_TSO6)) {
drivers/net/macvtap.c +1 -2
@@ -1058,8 +1064,7 @@ static int set_offload(struct macvtap_queue *q,
 unsigned long arg)
     /* tap_features are the same as features on tun/tap and
      * reflect user expectations.
      */
-    vlan->tap_features = vlan->dev->features &
-                (feature_mask | ~TUN_OFFLOADS);
+    vlan->tap_features = vlan->dev->features & (feature_mask |
 ~tap_mask);
     vlan->set_features = features;
     netdev_update_features(vlan->dev);

drivers/net/macvtap.c static long macvtap_ioctl(struct file *file, · line 1104

Quoted 9 lines
drivers/net/macvtap.c +4 -1
@@ -1092,8 +1097,18 @@ static long macvtap_ioctl(struct file *file,
 unsigned int cmd,
         if ((u & ~(IFF_VNET_HDR | IFF_MULTI_QUEUE)) !=
             (IFF_NO_PI | IFF_TAP))
             ret = -EINVAL;
-        else
+        else {
+            if ((q->flags ^ u) & IFF_VNET_HDR) {
+                /* vnet_hdr support impacts the offloads,
+                 * so we need to run throught the offload

throught ?

Quoted 25 lines
drivers/net/macvtap.c +7 -4
+                 * change.
+                 */
+                rtnl_lock();
+                ret = set_offload(q, 0);
+                rtnl_unlock();
+            }
             q->flags = u;
+        }
 
         return ret;
@@ -1155,10 +1170,6 @@ static long macvtap_ioctl(struct file *file,
 unsigned int cmd,
                 TUN_F_TSO_ECN | TUN_F_UFO))
             return -EINVAL;
 
-        /* TODO: only accept frames with the features that
-             got enabled for forwarded frames */
-        if (!(q->flags & IFF_VNET_HDR))
-            return  -EINVAL;
         rtnl_lock();
         ret = set_offload(q, arg);
         rtnl_unlock();

--
1.8.1.4