Skip to contentSkip to conversation

virtio_net: ethtool tx napi configuration

PATCH Superseded Jason Wang 3 participants 10 messages Sep 13, 2018 – Sep 14, 2018

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH v2 1/1 virtio_net: ethtool tx napi configuration

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
 drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +63 -1
index 765920905226..6e70864f5899 100644
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
 #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+
 static const unsigned long guest_offloads[] = {
 	VIRTIO_NET_F_GUEST_TSO4,
 	VIRTIO_NET_F_GUEST_TSO6,
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
 	virtqueue_napi_complete(napi, sq->vq, 0);
 
-	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+	/* Check napi.weight to avoid tx stall since it could be set
+	 * to zero by ethtool after skb_xmit_done().
+	 */
+	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
 		netif_tx_wake_queue(txq);
 
 	return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
 	return 0;
 }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_SCOALESCE,
+		.rx_max_coalesced_frames = 1,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+	int i, napi_weight;
+
+	if (ec->tx_max_coalesced_frames > 1)
+		return -EINVAL;
+
+	ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+	/* disallow changes to fields not explicitly tested above */
+	if (memcmp(ec, &ec_default, sizeof(ec_default)))
+		return -EINVAL;
+
+	if (napi_weight ^ vi->sq[0].napi.weight) {
+		for (i = 0; i < vi->max_queue_pairs; i++) {
+			struct netdev_queue *txq =
+			       netdev_get_tx_queue(vi->dev, i);
+
+			virtnet_napi_tx_disable(&vi->sq[i].napi);
+			__netif_tx_lock_bh(txq);
+			vi->sq[i].napi.weight = napi_weight;
+			__netif_tx_unlock_bh(txq);
+			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+					       &vi->sq[i].napi);
+		}
+	}
+
+	return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_GCOALESCE,
+		.rx_max_coalesced_frames = 1,
+		.tx_max_coalesced_frames = 0,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+
+	memcpy(ec, &ec_default, sizeof(ec_default));
+
+	if (vi->sq[0].napi.weight)
+		ec->tx_max_coalesced_frames = 1;
+
+	return 0;
+}
+
 static void virtnet_init_settings(struct net_device *dev)
 {
 	struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
 	.get_ts_info = ethtool_op_get_ts_info,
 	.get_link_ksettings = virtnet_get_link_ksettings,
 	.set_link_ksettings = virtnet_set_link_ksettings,
+	.set_coalesce = virtnet_set_coalesce,
+	.get_coalesce = virtnet_get_coalesce,
 };
 
 static void virtnet_freeze_down(struct virtio_device *vdev)
Signature · 1 line
2.17.1
author
#1 lore raw
Quoted 77 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
  drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +23 -1
index 765920905226..6e70864f5899 100644
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
  #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+
  static const unsigned long guest_offloads[] = {
  	VIRTIO_NET_F_GUEST_TSO4,
  	VIRTIO_NET_F_GUEST_TSO6,
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
  	virtqueue_napi_complete(napi, sq->vq, 0);
 
-	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+	/* Check napi.weight to avoid tx stall since it could be set
+	 * to zero by ethtool after skb_xmit_done().
+	 */
+	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
  		netif_tx_wake_queue(txq);
 
  	return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
  	return 0;
  }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_SCOALESCE,
+		.rx_max_coalesced_frames = 1,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+	int i, napi_weight;
+
+	if (ec->tx_max_coalesced_frames > 1)
+		return -EINVAL;
+
+	ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+	/* disallow changes to fields not explicitly tested above */

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2215

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+	if (memcmp(ec, &ec_default, sizeof(ec_default)))
+		return -EINVAL;
+
+	if (napi_weight ^ vi->sq[0].napi.weight) {
+		for (i = 0; i < vi->max_queue_pairs; i++) {
+			struct netdev_queue *txq =
+			       netdev_get_tx_queue(vi->dev, i);
+
+			virtnet_napi_tx_disable(&vi->sq[i].napi);
+			__netif_tx_lock_bh(txq);

Need to check netif_running() before disabling napi. Otherwise we may have a infinite loop here.

The discussion is still ongoing, if we decide to go set_coalesce, I will post a V3.

Thanks

Quoted 40 lines
drivers/net/virtio_net.c +30 -0
+			vi->sq[i].napi.weight = napi_weight;
+			__netif_tx_unlock_bh(txq);
+			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+					       &vi->sq[i].napi);
+		}
+	}
+
+	return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_GCOALESCE,
+		.rx_max_coalesced_frames = 1,
+		.tx_max_coalesced_frames = 0,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+
+	memcpy(ec, &ec_default, sizeof(ec_default));
+
+	if (vi->sq[0].napi.weight)
+		ec->tx_max_coalesced_frames = 1;
+
+	return 0;
+}
+
  static void virtnet_init_settings(struct net_device *dev)
  {
  	struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
  	.get_ts_info = ethtool_op_get_ts_info,
  	.get_link_ksettings = virtnet_get_link_ksettings,
  	.set_link_ksettings = virtnet_set_link_ksettings,
+	.set_coalesce = virtnet_set_coalesce,
+	.get_coalesce = virtnet_get_coalesce,
  };
 
  static void virtnet_freeze_down(struct virtio_device *vdev)
author
#2 lore raw
In reply to Jason Wang
Quoted 77 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
  drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +23 -1
index 765920905226..6e70864f5899 100644
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
  #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+
  static const unsigned long guest_offloads[] = {
  	VIRTIO_NET_F_GUEST_TSO4,
  	VIRTIO_NET_F_GUEST_TSO6,
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
  	virtqueue_napi_complete(napi, sq->vq, 0);
 
-	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+	/* Check napi.weight to avoid tx stall since it could be set
+	 * to zero by ethtool after skb_xmit_done().
+	 */
+	if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
  		netif_tx_wake_queue(txq);
 
  	return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
  	return 0;
  }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_SCOALESCE,
+		.rx_max_coalesced_frames = 1,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+	int i, napi_weight;
+
+	if (ec->tx_max_coalesced_frames > 1)
+		return -EINVAL;
+
+	ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+	napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+	/* disallow changes to fields not explicitly tested above */

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2215

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+	if (memcmp(ec, &ec_default, sizeof(ec_default)))
+		return -EINVAL;
+
+	if (napi_weight ^ vi->sq[0].napi.weight) {
+		for (i = 0; i < vi->max_queue_pairs; i++) {
+			struct netdev_queue *txq =
+			       netdev_get_tx_queue(vi->dev, i);
+
+			virtnet_napi_tx_disable(&vi->sq[i].napi);
+			__netif_tx_lock_bh(txq);

Need to check netif_running() before disabling napi. Otherwise we may have a infinite loop here.

The discussion is still ongoing, if we decide to go set_coalesce, I will post a V3.

Thanks

Quoted 32 lines
drivers/net/virtio_net.c +28 -0
+			vi->sq[i].napi.weight = napi_weight;
+			__netif_tx_unlock_bh(txq);
+			virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+					       &vi->sq[i].napi);
+		}
+	}
+
+	return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
+				struct ethtool_coalesce *ec)
+{
+	struct ethtool_coalesce ec_default = {
+		.cmd = ETHTOOL_GCOALESCE,
+		.rx_max_coalesced_frames = 1,
+		.tx_max_coalesced_frames = 0,
+	};
+	struct virtnet_info *vi = netdev_priv(dev);
+
+	memcpy(ec, &ec_default, sizeof(ec_default));
+
+	if (vi->sq[0].napi.weight)
+		ec->tx_max_coalesced_frames = 1;
+
+	return 0;
+}
+
  static void virtnet_init_settings(struct net_device *dev)
  {
  	struct virtnet_info *vi = netdev_priv(dev);

drivers/net/virtio_net.c static const struct ethtool_ops virtnet_ethtool_ops = { · line 2286

Quoted 8 lines
drivers/net/virtio_net.c +2 -0
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
  	.get_ts_info = ethtool_op_get_ts_info,
  	.get_link_ksettings = virtnet_get_link_ksettings,
  	.set_link_ksettings = virtnet_set_link_ksettings,
+	.set_coalesce = virtnet_set_coalesce,
+	.get_coalesce = virtnet_get_coalesce,
  };
 
  static void virtnet_freeze_down(struct virtio_device *vdev)

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
In reply to Jason Wang
Quoted 37 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
 drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 765920905226..6e70864f5899 100644

drivers/net/virtio_net.c DECLARE_EWMA(pkt_len, 0, 64) · line 70

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
 #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+

This is no longer needed

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1453

Quoted 13 lines
drivers/net/virtio_net.c +4 -1
 static const unsigned long guest_offloads[] = {
        VIRTIO_NET_F_GUEST_TSO4,
        VIRTIO_NET_F_GUEST_TSO6,
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
        virtqueue_napi_complete(napi, sq->vq, 0);
 
-       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+       /* Check napi.weight to avoid tx stall since it could be set
+        * to zero by ethtool after skb_xmit_done().
+        */
+       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
                netif_tx_wake_queue(txq);

I see. This assumes that the napi handler will always be called on conversion from napi to no-napi mode.

That is safe to assume because if it isn't called (and will not call netif_tx_wake_queue) that implies that napi was not scheduled, and thus the tx interrupt was not suppressed and thus there was no tx completion work to be scheduled?

Quoted 40 lines
drivers/net/virtio_net.c +34 -0
        return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
        return 0;
 }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_SCOALESCE,
+               .rx_max_coalesced_frames = 1,
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+       int i, napi_weight;
+
+       if (ec->tx_max_coalesced_frames > 1)
+               return -EINVAL;
+
+       ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+       /* disallow changes to fields not explicitly tested above */
+       if (memcmp(ec, &ec_default, sizeof(ec_default)))
+               return -EINVAL;
+
+       if (napi_weight ^ vi->sq[0].napi.weight) {
+               for (i = 0; i < vi->max_queue_pairs; i++) {
+                       struct netdev_queue *txq =
+                              netdev_get_tx_queue(vi->dev, i);
+
+                       virtnet_napi_tx_disable(&vi->sq[i].napi);
+                       __netif_tx_lock_bh(txq);
+                       vi->sq[i].napi.weight = napi_weight;
+                       __netif_tx_unlock_bh(txq);
+                       virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+                                              &vi->sq[i].napi);
+               }
+       }
+

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2232

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_GCOALESCE,
+               .rx_max_coalesced_frames = 1,
+               .tx_max_coalesced_frames = 0,

no need to explicitly initialize to 0 (unless you did this for documentation purposes, which is fine).

Quoted 26 lines
drivers/net/virtio_net.c +13 -1
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+
+       memcpy(ec, &ec_default, sizeof(ec_default));
+
+       if (vi->sq[0].napi.weight)
+               ec->tx_max_coalesced_frames = 1;
+
+       return 0;
+}
+
 static void virtnet_init_settings(struct net_device *dev)
 {
        struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
        .get_ts_info = ethtool_op_get_ts_info,
        .get_link_ksettings = virtnet_get_link_ksettings,
        .set_link_ksettings = virtnet_set_link_ksettings,
+       .set_coalesce = virtnet_set_coalesce,
+       .get_coalesce = virtnet_get_coalesce,
 };
 
 static void virtnet_freeze_down(struct virtio_device *vdev)
--

2.17.1

#6 lore raw
In reply to Jason Wang
Quoted 37 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
 drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 765920905226..6e70864f5899 100644

drivers/net/virtio_net.c DECLARE_EWMA(pkt_len, 0, 64) · line 70

Quoted 5 lines
drivers/net/virtio_net.c +2 -0
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
 #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+

This is no longer needed

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1453

Quoted 13 lines
drivers/net/virtio_net.c +4 -1
 static const unsigned long guest_offloads[] = {
        VIRTIO_NET_F_GUEST_TSO4,
        VIRTIO_NET_F_GUEST_TSO6,
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
        virtqueue_napi_complete(napi, sq->vq, 0);
 
-       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+       /* Check napi.weight to avoid tx stall since it could be set
+        * to zero by ethtool after skb_xmit_done().
+        */
+       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
                netif_tx_wake_queue(txq);

I see. This assumes that the napi handler will always be called on conversion from napi to no-napi mode.

That is safe to assume because if it isn't called (and will not call netif_tx_wake_queue) that implies that napi was not scheduled, and thus the tx interrupt was not suppressed and thus there was no tx completion work to be scheduled?

Quoted 40 lines
drivers/net/virtio_net.c +34 -0
        return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
        return 0;
 }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_SCOALESCE,
+               .rx_max_coalesced_frames = 1,
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+       int i, napi_weight;
+
+       if (ec->tx_max_coalesced_frames > 1)
+               return -EINVAL;
+
+       ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+       /* disallow changes to fields not explicitly tested above */
+       if (memcmp(ec, &ec_default, sizeof(ec_default)))
+               return -EINVAL;
+
+       if (napi_weight ^ vi->sq[0].napi.weight) {
+               for (i = 0; i < vi->max_queue_pairs; i++) {
+                       struct netdev_queue *txq =
+                              netdev_get_tx_queue(vi->dev, i);
+
+                       virtnet_napi_tx_disable(&vi->sq[i].napi);
+                       __netif_tx_lock_bh(txq);
+                       vi->sq[i].napi.weight = napi_weight;
+                       __netif_tx_unlock_bh(txq);
+                       virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+                                              &vi->sq[i].napi);
+               }
+       }
+

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2232

Quoted 10 lines
drivers/net/virtio_net.c +10 -0
+       return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_GCOALESCE,
+               .rx_max_coalesced_frames = 1,
+               .tx_max_coalesced_frames = 0,

no need to explicitly initialize to 0 (unless you did this for documentation purposes, which is fine).

Quoted 26 lines
drivers/net/virtio_net.c +13 -1
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+
+       memcpy(ec, &ec_default, sizeof(ec_default));
+
+       if (vi->sq[0].napi.weight)
+               ec->tx_max_coalesced_frames = 1;
+
+       return 0;
+}
+
 static void virtnet_init_settings(struct net_device *dev)
 {
        struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
        .get_ts_info = ethtool_op_get_ts_info,
        .get_link_ksettings = virtnet_get_link_ksettings,
        .set_link_ksettings = virtnet_set_link_ksettings,
+       .set_coalesce = virtnet_set_coalesce,
+       .get_coalesce = virtnet_get_coalesce,
 };
 
 static void virtnet_freeze_down(struct virtio_device *vdev)
--

2.17.1

maintainer
#7 lore raw
In reply to Jason Wang

From: Jason Wang <[email protected]>
Date: Thu, 13 Sep 2018 13:35:45 +0800

Quoted 1 line

Toggle tx napi through a bit in tx-frames.

This is not what the code implements as the interface any more.

Please fix the commit message to match the code.

Thanks.

maintainer
#9 lore raw
In reply to Jason Wang

From: Jason Wang <[email protected]>
Date: Thu, 13 Sep 2018 13:35:45 +0800

Quoted 1 line

Toggle tx napi through a bit in tx-frames.

This is not what the code implements as the interface any more.

Please fix the commit message to match the code.

Thanks.

author
#8 lore raw
In reply to David Miller
Quoted 9 lines · David Miller

From: Jason Wang <[email protected]>
Date: Thu, 13 Sep 2018 13:35:45 +0800

Quoted 1 line

Toggle tx napi through a bit in tx-frames.

This is not what the code implements as the interface any more.

Please fix the commit message to match the code.

Thanks.

Will fix this.

Thanks
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#4 lore raw
In reply to Willem de Bruijn
Quoted 38 lines · Willem de Bruijn
Quoted 37 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
  drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 765920905226..6e70864f5899 100644
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
Quoted 5 lines

drivers/net/virtio_net.c DECLARE_EWMA(pkt_len, 0, 64) · line 70

Quoted 4 lines
drivers/net/virtio_net.c +2 -0
  #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+

This is no longer needed

Yes, will remove this.

Quoted 17 lines
Quoted 5 lines
drivers/net/virtio_net.c +0 -0
  static const unsigned long guest_offloads[] = {
         VIRTIO_NET_F_GUEST_TSO4,
         VIRTIO_NET_F_GUEST_TSO6,

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1453

Quoted 9 lines
drivers/net/virtio_net.c +4 -1
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
         virtqueue_napi_complete(napi, sq->vq, 0);
 
-       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+       /* Check napi.weight to avoid tx stall since it could be set
+        * to zero by ethtool after skb_xmit_done().
+        */
+       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
                 netif_tx_wake_queue(txq);

I see. This assumes that the napi handler will always be called on conversion from napi to no-napi mode.

Quoted 4 lines

That is safe to assume because if it isn't called (and will not call netif_tx_wake_queue) that implies that napi was not scheduled, and thus the tx interrupt was not suppressed and thus there was no tx completion work to be scheduled?

If it isn't called it means skb_xmit_done() wakeup tx directly instead of schedule tx. This could be a little bit early since there may be still lots of pending tx packets. But it doesn't harm, start_xmit() can handle this by re enable a delayed tx interrupt and disable TX.

But there's a bug, look like I need remove the check of (!sq->napi.weight) in the beginning of the function.

Quoted 44 lines
Quoted 44 lines
drivers/net/virtio_net.c +38 -0
         return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
         return 0;
  }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_SCOALESCE,
+               .rx_max_coalesced_frames = 1,
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+       int i, napi_weight;
+
+       if (ec->tx_max_coalesced_frames > 1)
+               return -EINVAL;
+
+       ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+       /* disallow changes to fields not explicitly tested above */
+       if (memcmp(ec, &ec_default, sizeof(ec_default)))
+               return -EINVAL;
+
+       if (napi_weight ^ vi->sq[0].napi.weight) {
+               for (i = 0; i < vi->max_queue_pairs; i++) {
+                       struct netdev_queue *txq =
+                              netdev_get_tx_queue(vi->dev, i);
+
+                       virtnet_napi_tx_disable(&vi->sq[i].napi);
+                       __netif_tx_lock_bh(txq);
+                       vi->sq[i].napi.weight = napi_weight;
+                       __netif_tx_unlock_bh(txq);
+                       virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+                                              &vi->sq[i].napi);
+               }
+       }
+
+       return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
Quoted 8 lines

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2232

Quoted 6 lines
drivers/net/virtio_net.c +6 -0
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_GCOALESCE,
+               .rx_max_coalesced_frames = 1,
+               .tx_max_coalesced_frames = 0,

no need to explicitly initialize to 0 (unless you did this for documentation purposes, which is fine).

Yes.

Thanks

Quoted 26 lines
Quoted 26 lines
drivers/net/virtio_net.c +13 -1
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+
+       memcpy(ec, &ec_default, sizeof(ec_default));
+
+       if (vi->sq[0].napi.weight)
+               ec->tx_max_coalesced_frames = 1;
+
+       return 0;
+}
+
  static void virtnet_init_settings(struct net_device *dev)
  {
         struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
         .get_ts_info = ethtool_op_get_ts_info,
         .get_link_ksettings = virtnet_get_link_ksettings,
         .set_link_ksettings = virtnet_set_link_ksettings,
+       .set_coalesce = virtnet_set_coalesce,
+       .get_coalesce = virtnet_get_coalesce,
  };
 
  static void virtnet_freeze_down(struct virtio_device *vdev)
--

2.17.1

author
#5 lore raw
In reply to Willem de Bruijn
Quoted 38 lines · Willem de Bruijn
Quoted 37 lines · Jason Wang

Implement ethtool .set_coalesce (-C) and .get_coalesce (-c) handlers. Interrupt moderation is currently not supported, so these accept and display the default settings of 0 usec and 1 frame.

Toggle tx napi through a bit in tx-frames. So as to not interfere with possible future interrupt moderation, value 1 means tx napi while value 0 means not.

To properly synchronize with the data path, tx napi is disabled and tx lock is held when changing the value of napi weight. And two more places that can access tx napi weight:

- speculative tx polling in rx napi, we can leave it as is since it not a must for correctness.
- skb_xmit_done(), one more check of napi weight is added before trying to enable tx to avoid tx to be disabled forever if napi is disabled after skb_xmit_done() but before the napi

---
Changes from V1:
- try to synchronize with datapath to allow changing mode when interface is up.
- use tx-frames 0 as to disable tx napi while tx-frames 1 to enable tx napi

---
  drivers/net/virtio_net.c | 64 +++++++++++++++++++++++++++++++++++++++-
  1 file changed, 63 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +0 -0
index 765920905226..6e70864f5899 100644
@@ -66,6 +66,8 @@ DECLARE_EWMA(pkt_len, 0, 64)
 
Quoted 5 lines

drivers/net/virtio_net.c DECLARE_EWMA(pkt_len, 0, 64) · line 70

Quoted 4 lines
drivers/net/virtio_net.c +2 -0
  #define VIRTNET_DRIVER_VERSION "1.0.0"
 
+static const u32 ethtool_coalesce_napi_mask = (1UL << 10);
+

This is no longer needed

Yes, will remove this.

Quoted 17 lines
Quoted 5 lines
drivers/net/virtio_net.c +0 -0
  static const unsigned long guest_offloads[] = {
         VIRTIO_NET_F_GUEST_TSO4,
         VIRTIO_NET_F_GUEST_TSO6,

drivers/net/virtio_net.c static int virtnet_poll_tx(struct napi_struct *napi, int budget) · line 1453

Quoted 9 lines
drivers/net/virtio_net.c +4 -1
@@ -1444,7 +1446,10 @@ static int virtnet_poll_tx(struct napi_struct *napi, int budget)
 
         virtqueue_napi_complete(napi, sq->vq, 0);
 
-       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS)
+       /* Check napi.weight to avoid tx stall since it could be set
+        * to zero by ethtool after skb_xmit_done().
+        */
+       if (sq->vq->num_free >= 2 + MAX_SKB_FRAGS || !sq->napi.weight)
                 netif_tx_wake_queue(txq);

I see. This assumes that the napi handler will always be called on conversion from napi to no-napi mode.

Quoted 4 lines

That is safe to assume because if it isn't called (and will not call netif_tx_wake_queue) that implies that napi was not scheduled, and thus the tx interrupt was not suppressed and thus there was no tx completion work to be scheduled?

If it isn't called it means skb_xmit_done() wakeup tx directly instead of schedule tx. This could be a little bit early since there may be still lots of pending tx packets. But it doesn't harm, start_xmit() can handle this by re enable a delayed tx interrupt and disable TX.

But there's a bug, look like I need remove the check of (!sq->napi.weight) in the beginning of the function.

Quoted 44 lines
Quoted 44 lines
drivers/net/virtio_net.c +38 -0
         return 0;
@@ -2181,6 +2186,61 @@ static int virtnet_get_link_ksettings(struct net_device *dev,
         return 0;
  }
 
+static int virtnet_set_coalesce(struct net_device *dev,
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_SCOALESCE,
+               .rx_max_coalesced_frames = 1,
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+       int i, napi_weight;
+
+       if (ec->tx_max_coalesced_frames > 1)
+               return -EINVAL;
+
+       ec_default.tx_max_coalesced_frames = ec->tx_max_coalesced_frames;
+       napi_weight = ec->tx_max_coalesced_frames ? NAPI_POLL_WEIGHT : 0;
+
+       /* disallow changes to fields not explicitly tested above */
+       if (memcmp(ec, &ec_default, sizeof(ec_default)))
+               return -EINVAL;
+
+       if (napi_weight ^ vi->sq[0].napi.weight) {
+               for (i = 0; i < vi->max_queue_pairs; i++) {
+                       struct netdev_queue *txq =
+                              netdev_get_tx_queue(vi->dev, i);
+
+                       virtnet_napi_tx_disable(&vi->sq[i].napi);
+                       __netif_tx_lock_bh(txq);
+                       vi->sq[i].napi.weight = napi_weight;
+                       __netif_tx_unlock_bh(txq);
+                       virtnet_napi_tx_enable(vi, vi->sq[i].vq,
+                                              &vi->sq[i].napi);
+               }
+       }
+
+       return 0;
+}
+
+static int virtnet_get_coalesce(struct net_device *dev,
Quoted 8 lines

drivers/net/virtio_net.c static int virtnet_get_link_ksettings(struct net_device *dev, · line 2232

Quoted 6 lines
drivers/net/virtio_net.c +6 -0
+                               struct ethtool_coalesce *ec)
+{
+       struct ethtool_coalesce ec_default = {
+               .cmd = ETHTOOL_GCOALESCE,
+               .rx_max_coalesced_frames = 1,
+               .tx_max_coalesced_frames = 0,

no need to explicitly initialize to 0 (unless you did this for documentation purposes, which is fine).

Yes.

Thanks

Quoted 22 lines
Quoted 22 lines
drivers/net/virtio_net.c +13 -0
+       };
+       struct virtnet_info *vi = netdev_priv(dev);
+
+       memcpy(ec, &ec_default, sizeof(ec_default));
+
+       if (vi->sq[0].napi.weight)
+               ec->tx_max_coalesced_frames = 1;
+
+       return 0;
+}
+
  static void virtnet_init_settings(struct net_device *dev)
  {
         struct virtnet_info *vi = netdev_priv(dev);
@@ -2219,6 +2279,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = {
         .get_ts_info = ethtool_op_get_ts_info,
         .get_link_ksettings = virtnet_get_link_ksettings,
         .set_link_ksettings = virtnet_set_link_ksettings,
+       .set_coalesce = virtnet_set_coalesce,
+       .get_coalesce = virtnet_get_coalesce,
  };
 
Quoted 4 lines
Quoted 4 lines
drivers/net/virtio_net.c +0 -1
  static void virtnet_freeze_down(struct virtio_device *vdev)
--

2.17.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization