Skip to contentSkip to conversation

virtio_net: add link status handling

PATCH In review Mark McLoughlin 3 participants 9 messages Dec 9, 2008 – Jan 9, 2009

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_net: add link status handling

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

---
 drivers/net/virtio_net.c   |   40 +++++++++++++++++++++++++++++++++++++++-
 include/linux/virtio_net.h |    5 +++++
 2 files changed, 44 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +39 -1
index 71ca29c..128c38d 100644
@@ -42,6 +42,7 @@ struct virtnet_info
 	struct virtqueue *rvq, *svq;
 	struct net_device *dev;
 	struct napi_struct napi;
+	unsigned int status;
 
 	/* The skb we couldn't send because buffers were full. */
 	struct sk_buff *last_xmit_skb;
@@ -611,6 +612,7 @@ static struct ethtool_ops virtnet_ethtool_ops = {
 	.set_tx_csum = virtnet_set_tx_csum,
 	.set_sg = ethtool_op_set_sg,
 	.set_tso = ethtool_op_set_tso,
+	.get_link = ethtool_op_get_link,
 };
 
 #define MIN_MTU 68
@@ -624,6 +626,38 @@ static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
 	return 0;
 }
 
+static void virtnet_update_status(struct virtnet_info *vi)
+{
+	u16 v;
+
+	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
+		return;
+
+        vi->vdev->config->get(vi->vdev,
+                              offsetof(struct virtio_net_config, status),
+                              &v, sizeof(v));
+
+	if (vi->status == v)
+		return;
+
+	vi->status = v;
+
+	if (vi->status & VIRTIO_NET_S_LINK_UP) {
+		netif_carrier_on(vi->dev);
+		netif_wake_queue(vi->dev);
+	} else {
+		netif_carrier_off(vi->dev);
+		netif_stop_queue(vi->dev);
+	}
+}
+
+static void virtnet_config_changed(struct virtio_device *vdev)
+{
+	struct virtnet_info *vi = vdev->priv;
+
+	virtnet_update_status(vi);
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int err;
@@ -732,6 +766,9 @@ static int virtnet_probe(struct virtio_device *vdev)
 		goto unregister;
 	}
 
+	vi->status = VIRTIO_NET_S_LINK_UP;
+	virtnet_update_status(vi);
+
 	pr_debug("virtnet: registered device %s\n", dev->name);
 	return 0;
 
@@ -787,7 +824,7 @@ static unsigned int features[] = {
 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
 	VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
-	VIRTIO_NET_F_MRG_RXBUF,
+	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS,
 	VIRTIO_F_NOTIFY_ON_EMPTY,
 };
 
@@ -799,6 +836,7 @@ static struct virtio_driver virtio_net = {
 	.id_table =	id_table,
 	.probe =	virtnet_probe,
 	.remove =	__devexit_p(virtnet_remove),
+	.config_changed = virtnet_config_changed,
 };
 
 static int __init init(void)
include/linux/virtio_net.h +5 -0
index 5cdd0aa..f76bd4a 100644
@@ -21,11 +21,16 @@
 #define VIRTIO_NET_F_HOST_ECN	13	/* Host can handle TSO[6] w/ ECN in. */
 #define VIRTIO_NET_F_HOST_UFO	14	/* Host can handle UFO in. */
 #define VIRTIO_NET_F_MRG_RXBUF	15	/* Host can merge receive buffers. */
+#define VIRTIO_NET_F_STATUS	16	/* virtio_net_config.status available */
+
+#define VIRTIO_NET_S_LINK_UP	1	/* Link is up */
 
 struct virtio_net_config
 {
 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
 	__u8 mac[6];
+	/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
+	__u16 status;
 } __attribute__((packed));
 
 /* This is the first element of the scatter-gather list.  If you don't
Signature · 1 line
1.6.0.3
#1 lore raw
Quoted 8 lines · Mark McLoughlin

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

I'm thinking along the lines of doing live migration between two end points that are not on the same subnet. It would be nice to be able to signal link status down, have the guest acknowledge that it received it, do the live migration, then do link status up so the guest reconnects it's networking.

Regards,

Anthony Liguori

#2 lore raw
Quoted 5 lines · On Wednesday 10 December 2008 08:02:14 Anthony Liguori wrote:
Quoted 4 lines · Mark McLoughlin

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

Quoted 7 lines
Quoted 4 lines

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

How about of every status change event? ie. a generic virtio_pci solution?

Rusty.

#8 lore raw
In reply to Mark McLoughlin
Quoted 3 lines · On Tuesday 09 December 2008 20:49:33 Mark McLoughlin wrote:

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This has been a TODO for a while, thanks!

drivers/net/virtio_net.c static int virtnet_change_mtu(struct net_device *dev, int new_mtu) · line 651

Quoted 12 lines
drivers/net/virtio_net.c +12 -0
+	if (vi->status == v)
+		return;
+
+	vi->status = v;
+
+	if (vi->status & VIRTIO_NET_S_LINK_UP) {
+		netif_carrier_on(vi->dev);
+		netif_wake_queue(vi->dev);
+	} else {
+		netif_carrier_off(vi->dev);
+		netif_stop_queue(vi->dev);
+	}

New status bits will screw this logic unless we count on the host not to set them. I suggest:

	/* Ignore unknown (future) status bits */
	v &= VIRTIO_NET_S_LINK_UP;

Cheers,
Rusty.

#3 lore raw
In reply to Rusty Russell
Quoted 13 lines · Rusty Russell
Quoted 11 lines · On Wednesday 10 December 2008 08:02:14 Anthony Liguori wrote:
Quoted 9 lines · Mark McLoughlin

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

Quoted 5 lines
Quoted 2 lines

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

How about of every status change event? ie. a generic virtio_pci solution?

A really simple way to do it would just be to have another status field that was the guest's status (verses the host requested status which the current field is). All config reads/writes result in exits so it's easy to track.

Adding YA virtio event may be a little overkill.

Regards,

Anthony Liguori

Quoted 2 lines

Rusty.

#4 lore raw
Quoted 21 lines · Anthony Liguori
Quoted 13 lines · Rusty Russell
Quoted 11 lines · On Wednesday 10 December 2008 08:02:14 Anthony Liguori wrote:
Quoted 9 lines · Mark McLoughlin

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

Quoted 6 lines
Quoted 3 lines

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

How about of every status change event? ie. a generic virtio_pci solution?

Quoted 6 lines

A really simple way to do it would just be to have another status field that was the guest's status (verses the host requested status which the current field is). All config reads/writes result in exits so it's easy to track.

Adding YA virtio event may be a little overkill.

Sounds very reasonable; that and Rusty's "mask out unknown bits" suggestion in the version below.

Cheers,
Mark.

From: Mark McLoughlin <[email protected]>
Subject: [PATCH] virtio_net: add link status handling

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

---
 drivers/net/virtio_net.c   |   49 +++++++++++++++++++++++++++++++++++++++++++-
 include/linux/virtio_net.h |    8 +++++++
 2 files changed, 56 insertions(+), 1 deletions(-)
drivers/net/virtio_net.c +48 -1
index 71ca29c..40f02a1 100644
@@ -42,6 +42,7 @@ struct virtnet_info
 	struct virtqueue *rvq, *svq;
 	struct net_device *dev;
 	struct napi_struct napi;
+	unsigned int status;
 
 	/* The skb we couldn't send because buffers were full. */
 	struct sk_buff *last_xmit_skb;
@@ -611,6 +612,7 @@ static struct ethtool_ops virtnet_ethtool_ops = {
 	.set_tx_csum = virtnet_set_tx_csum,
 	.set_sg = ethtool_op_set_sg,
 	.set_tso = ethtool_op_set_tso,
+	.get_link = ethtool_op_get_link,
 };
 
 #define MIN_MTU 68
@@ -624,6 +626,47 @@ static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
 	return 0;
 }
 
+static void virtnet_update_status(struct virtnet_info *vi)
+{
+	u16 v;
+
+	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
+		return;
+
+        vi->vdev->config->get(vi->vdev,
+                              offsetof(struct virtio_net_config, status),
+                              &v, sizeof(v));
+
+	/* Ignore unknown (future) status bits */
+	v &= VIRTIO_NET_S_LINK_UP;
+
+	if (vi->status == v)
+		return;
+
+	vi->status = v;
+
+	if (vi->status & VIRTIO_NET_S_LINK_UP) {
+		netif_carrier_on(vi->dev);
+		netif_wake_queue(vi->dev);
+	} else {
+		netif_carrier_off(vi->dev);
+		netif_stop_queue(vi->dev);
+	}
+
+	/* acknowledge the status change */
+        vi->vdev->config->set(vi->vdev,
+                              offsetof(struct virtio_net_config, status_ack),
+                              &vi->status, sizeof(vi->status));
+
+}
+
+static void virtnet_config_changed(struct virtio_device *vdev)
+{
+	struct virtnet_info *vi = vdev->priv;
+
+	virtnet_update_status(vi);
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int err;
@@ -732,6 +775,9 @@ static int virtnet_probe(struct virtio_device *vdev)
 		goto unregister;
 	}
 
+	vi->status = VIRTIO_NET_S_LINK_UP;
+	virtnet_update_status(vi);
+
 	pr_debug("virtnet: registered device %s\n", dev->name);
 	return 0;
 
@@ -787,7 +833,7 @@ static unsigned int features[] = {
 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
 	VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
-	VIRTIO_NET_F_MRG_RXBUF,
+	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS,
 	VIRTIO_F_NOTIFY_ON_EMPTY,
 };
 
@@ -799,6 +845,7 @@ static struct virtio_driver virtio_net = {
 	.id_table =	id_table,
 	.probe =	virtnet_probe,
 	.remove =	__devexit_p(virtnet_remove),
+	.config_changed = virtnet_config_changed,
 };
 
 static int __init init(void)
include/linux/virtio_net.h +8 -0
index 5cdd0aa..67a37f8 100644
@@ -21,11 +21,19 @@
 #define VIRTIO_NET_F_HOST_ECN	13	/* Host can handle TSO[6] w/ ECN in. */
 #define VIRTIO_NET_F_HOST_UFO	14	/* Host can handle UFO in. */
 #define VIRTIO_NET_F_MRG_RXBUF	15	/* Host can merge receive buffers. */
+#define VIRTIO_NET_F_STATUS	16	/* virtio_net_config.status available */
+
+#define VIRTIO_NET_S_LINK_UP	1	/* Link is up */
 
 struct virtio_net_config
 {
 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
 	__u8 mac[6];
+	/* Status supplied by host; see VIRTIO_NET_F_STATUS and VIRTIO_NET_S_*
+	 * bits above */
+	__u16 status;
+	/* The guest acks the status by writing it back here */
+	__u16 status_ack;
 } __attribute__((packed));
 
 /* This is the first element of the scatter-gather list.  If you don't
Signature · 1 line
1.6.0.3
#5 lore raw
Quoted 13 lines · On Thursday 11 December 2008 05:04:44 Mark McLoughlin wrote:
Quoted 12 lines · Anthony Liguori
Quoted 5 lines · Rusty Russell
Quoted 2 lines · On Wednesday 10 December 2008 08:02:14 Anthony Liguori wrote:

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

How about of every status change event? ie. a generic virtio_pci solution?

A really simple way to do it would just be to have another status field that was the guest's status (verses the host requested status which the current field is). All config reads/writes result in exits so it's easy to track.

Quoted 4 lines
Quoted 1 line

Adding YA virtio event may be a little overkill.

Sounds very reasonable; that and Rusty's "mask out unknown bits" suggestion in the version below.

Not quite what I was after. I've taken the original patch, added the masking change. I'll test here and feed to DaveM.

As far as Anthony's live migrate race, I've decided to declare with Canutian arrogance that it Will Never Happen.

Cheers,
Rusty.

#6 lore raw

Hi Rusty,

Quoted 14 lines · Rusty Russell
Quoted 13 lines · On Thursday 11 December 2008 05:04:44 Mark McLoughlin wrote:
Quoted 12 lines · Anthony Liguori
Quoted 5 lines · Rusty Russell
Quoted 2 lines · On Wednesday 10 December 2008 08:02:14 Anthony Liguori wrote:

It would be nice if the virtio-net card wrote some acknowledgement that it has received the link status down/up events.

How about of every status change event? ie. a generic virtio_pci solution?

A really simple way to do it would just be to have another status field that was the guest's status (verses the host requested status which the current field is). All config reads/writes result in exits so it's easy to track.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

Adding YA virtio event may be a little overkill.

Sounds very reasonable; that and Rusty's "mask out unknown bits" suggestion in the version below.

Not quite what I was after. I've taken the original patch, added the masking change. I'll test here and feed to DaveM.

This never got pushed to davem, did it? What you've got in your queue looks fine to me ...

Cheers,
Mark.

#7 lore raw

Hi Dave,

Quoted 6 lines · Mark McLoughlin
Quoted 2 lines · Rusty Russell

Not quite what I was after. I've taken the original patch, added the masking change. I'll test here and feed to DaveM.

This never got pushed to davem, did it? What you've got in your queue looks fine to me ...

Here's a virtio-net patch from Rusty's queue which appears just to have fallen through the cracks.

Thanks,
Mark.

Subject: virtio_net: add link status handling
Date: Tue, 09 Dec 2008 10:19:33 +0000
From: Mark McLoughlin <[email protected]>

Allow the host to inform us that the link is down by adding a VIRTIO_NET_F_STATUS which indicates that device status is available in virtio_net config.

This is currently useful for simulating link down conditions (e.g. using proposed qemu 'set_link' monitor command) but would also be needed if we were to support device assignment via virtio.

---
 drivers/net/virtio_net.c   |   43 ++++++++++++++++++++++++++++++++++++++++++-
 include/linux/virtio_net.h |    5 +++++
 2 files changed, 47 insertions(+), 1 deletion(-)
drivers/net/virtio_net.c +42 -1
index 71ca29c..128c38d 100644
@@ -42,6 +42,7 @@ struct virtnet_info
 	struct virtqueue *rvq, *svq;
 	struct net_device *dev;
 	struct napi_struct napi;
+	unsigned int status;
 
 	/* The skb we couldn't send because buffers were full. */
 	struct sk_buff *last_xmit_skb;
@@ -611,6 +612,7 @@ static struct ethtool_ops virtnet_ethtool_ops = {
 	.set_tx_csum = virtnet_set_tx_csum,
 	.set_sg = ethtool_op_set_sg,
 	.set_tso = ethtool_op_set_tso,
+	.get_link = ethtool_op_get_link,
 };
 
 #define MIN_MTU 68
@@ -624,6 +626,41 @@ static int virtnet_change_mtu(struct net_device *dev, int new_mtu)
 	return 0;
 }
 
+static void virtnet_update_status(struct virtnet_info *vi)
+{
+	u16 v;
+
+	if (!virtio_has_feature(vi->vdev, VIRTIO_NET_F_STATUS))
+		return;
+
+	vi->vdev->config->get(vi->vdev,
+			      offsetof(struct virtio_net_config, status),
+			      &v, sizeof(v));
+
+	/* Ignore unknown (future) status bits */
+	v &= VIRTIO_NET_S_LINK_UP;
+
+	if (vi->status == v)
+		return;
+
+	vi->status = v;
+
+	if (vi->status & VIRTIO_NET_S_LINK_UP) {
+		netif_carrier_on(vi->dev);
+		netif_wake_queue(vi->dev);
+	} else {
+		netif_carrier_off(vi->dev);
+		netif_stop_queue(vi->dev);
+	}
+}
+
+static void virtnet_config_changed(struct virtio_device *vdev)
+{
+	struct virtnet_info *vi = vdev->priv;
+
+	virtnet_update_status(vi);
+}
+
 static int virtnet_probe(struct virtio_device *vdev)
 {
 	int err;
@@ -732,6 +766,9 @@ static int virtnet_probe(struct virtio_device *vdev)
 		goto unregister;
 	}
 
+	vi->status = VIRTIO_NET_S_LINK_UP;
+	virtnet_update_status(vi);
+
 	pr_debug("virtnet: registered device %s\n", dev->name);
 	return 0;
 
@@ -787,7 +824,7 @@ static unsigned int features[] = {
 	VIRTIO_NET_F_HOST_TSO4, VIRTIO_NET_F_HOST_UFO, VIRTIO_NET_F_HOST_TSO6,
 	VIRTIO_NET_F_HOST_ECN, VIRTIO_NET_F_GUEST_TSO4, VIRTIO_NET_F_GUEST_TSO6,
 	VIRTIO_NET_F_GUEST_ECN, /* We don't yet handle UFO input. */
-	VIRTIO_NET_F_MRG_RXBUF,
+	VIRTIO_NET_F_MRG_RXBUF, VIRTIO_NET_F_STATUS,
 	VIRTIO_F_NOTIFY_ON_EMPTY,
 };
 
@@ -799,6 +836,7 @@ static struct virtio_driver virtio_net = {
 	.id_table =	id_table,
 	.probe =	virtnet_probe,
 	.remove =	__devexit_p(virtnet_remove),
+	.config_changed = virtnet_config_changed,
 };
 
 static int __init init(void)
include/linux/virtio_net.h +5 -0
index 5cdd0aa..f76bd4a 100644
@@ -21,11 +21,16 @@
 #define VIRTIO_NET_F_HOST_ECN	13	/* Host can handle TSO[6] w/ ECN in. */
 #define VIRTIO_NET_F_HOST_UFO	14	/* Host can handle UFO in. */
 #define VIRTIO_NET_F_MRG_RXBUF	15	/* Host can merge receive buffers. */
+#define VIRTIO_NET_F_STATUS	16	/* virtio_net_config.status available */
+
+#define VIRTIO_NET_S_LINK_UP	1	/* Link is up */
 
 struct virtio_net_config
 {
 	/* The config defining mac address (if VIRTIO_NET_F_MAC) */
 	__u8 mac[6];
+	/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */
+	__u16 status;
 } __attribute__((packed));
 
 /* This is the first element of the scatter-gather list.  If you don't