Skip to contentSkip to conversation

virtio-net: Drop net_virtio_info.can_receive

PATCH In review Fam Zheng 4 participants 15 messages Jun 30, 2015 – Jul 13, 2015

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

PATCH 1/1 virtio-net: Drop net_virtio_info.can_receive

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,
Signature · 1 line
2.4.4
#1 lore raw
Quoted 18 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
hw/net/virtio-net.c +0 -0
index d728233..dbef0d0 100644

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 7 lines
hw/net/virtio-net.c +0 -1
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Reviewed-by
#2 lore raw
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 15 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Thoughts?

#3 lore raw
Quoted 42 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 15 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Quoted 7 lines

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Thoughts?

OK, but we do have transient out of buffers states too, and virtio_net_can_receive checks these as well.

To me, it looks like we should change virtio_net_can_receive to avoid checking link down state, etc.
But I don't see why we should remove it completely.

Signature · 1 line
MST
author
#4 lore raw
Quoted 49 lines · Michael S. Tsirkin
Quoted 48 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 15 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Thoughts?

Quoted 8 lines
Quoted 1 line

OK, but we do have transient out of buffers states too, and virtio_net_can_receive checks these as well.

To me, it looks like we should change virtio_net_can_receive to avoid checking link down state, etc.
But I don't see why we should remove it completely.

OK, it makse sense to check buffer state.

Fam

author
#5 lore raw
Quoted 48 lines · Fam Zheng
Quoted 47 lines · Michael S. Tsirkin
Quoted 46 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 15 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 6 lines
Quoted 6 lines
Quoted 3 lines

Thoughts?

OK, but we do have transient out of buffers states too, and virtio_net_can_receive checks these as well.

I'm a bit confused, do you mean virtio_queue_ready()? That is buffer presence check, and the free buffer check virtio_net_has_buffers() is only in virtio_net_receive(). Am I missing things?

Fam

Quoted 9 lines
Quoted 4 lines

To me, it looks like we should change virtio_net_can_receive to avoid checking link down state, etc.
But I don't see why we should remove it completely.

OK, it makse sense to check buffer state.

Fam

#6 lore raw
Quoted 53 lines · Fam Zheng
Quoted 52 lines · Fam Zheng
Quoted 51 lines · Michael S. Tsirkin
Quoted 49 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 15 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Thoughts?

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines

OK, but we do have transient out of buffers states too, and virtio_net_can_receive checks these as well.

I'm a bit confused, do you mean virtio_queue_ready()? That is buffer presence check, and the free buffer check virtio_net_has_buffers() is only in virtio_net_receive(). Am I missing things?

Fam

Interesting. You are right.

See also commit cdd5cc12ba8cf0c068da319370bdd3ba45eaf7ac.

Quoted 9 lines
Quoted 9 lines
Quoted 4 lines

To me, it looks like we should change virtio_net_can_receive to avoid checking link down state, etc.
But I don't see why we should remove it completely.

OK, it makse sense to check buffer state.

Fam

#7 lore raw
In reply to Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 11 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

Quoted 2 lines
#8 lore raw
Quoted 50 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 10 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 6 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Stefan

#9 lore raw
Quoted 58 lines · Stefan Hajnoczi
Quoted 50 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 10 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 6 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

Quoted 7 lines

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Stefan

Frankly, I don't see the point of the patch. Is this supposed to be a bugfix? If so, there's should be a description about how to trigger the bug. Is this an optimization? If so there should be some numbers showing a gain.

Signature · 1 line
MST
author
#10 lore raw
Quoted 65 lines · Michael S. Tsirkin
Quoted 64 lines · Stefan Hajnoczi
Quoted 50 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 10 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 6 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Quoted 8 lines
Quoted 1 line

Stefan

Frankly, I don't see the point of the patch. Is this supposed to be a bugfix? If so, there's should be a description about how to trigger the bug. Is this an optimization? If so there should be some numbers showing a gain.

It's a bug fix, we are not flushing the queue when DIRVER_OK is being set or when buffer is becoming available (the virtio_net_can_receive conditions). Not an issue before a90a7425cf but since that the semantics is enforced.

Fam

#11 lore raw
Quoted 75 lines · Fam Zheng
Quoted 65 lines · Michael S. Tsirkin
Quoted 64 lines · Stefan Hajnoczi
Quoted 50 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 10 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 6 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Quoted 8 lines
Quoted 1 line

Stefan

Frankly, I don't see the point of the patch. Is this supposed to be a bugfix? If so, there's should be a description about how to trigger the bug. Is this an optimization? If so there should be some numbers showing a gain.

Quoted 5 lines

It's a bug fix, we are not flushing the queue when DIRVER_OK is being set or when buffer is becoming available (the virtio_net_can_receive conditions). Not an issue before a90a7425cf but since that the semantics is enforced.

Fam

I think the safest and obvious fix is to flush on DRIVER_OK then (unless vhost started). That might be 2.4 material.

Signature · 1 line
MST
#12 lore raw
In reply to Stefan Hajnoczi
Quoted 48 lines · Stefan Hajnoczi
Quoted 47 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 9 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

But it was the choice of guest.

Quoted 5 lines

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Stefan

I think there're two problems for virtio-net:

1) mitigation method when guest driver is ok. For tx, we have either timer or bh, for rx and only for tap, we have 756ae78b. We probably need fixes for other backends.

2) when driver is not ok, the point is we should not poll the backend at all (I believe this is one of the main objects of main loop). Something like tap_can_send() and the commit that drops tap_can_send() all follow this rule. But this patch does not, we end up with:

- driver is not ok or no driver, qemu keep reading and dropping packets.
- driver is ok but not enough rx buffer, qemu will disable tap read poll.

Which looks conflicted.

We need fix this either in 2.4 or later and also for other NICs.

#13 lore raw
Quoted 65 lines · Jason Wang
Quoted 48 lines · Stefan Hajnoczi
Quoted 47 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 9 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

But it was the choice of guest.

Quoted 4 lines

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Quoted 7 lines
Quoted 1 line

Stefan

I think there're two problems for virtio-net:

1) mitigation method when guest driver is ok. For tx, we have either timer or bh, for rx and only for tap, we have 756ae78b. We probably need fixes for other backends.

I agree. It's not directly related to the change Fam is proposing though.

Quoted 6 lines

2) when driver is not ok, the point is we should not poll the backend at all (I believe this is one of the main objects of main loop). Something like tap_can_send() and the commit that drops tap_can_send() all follow this rule. But this patch does not, we end up with:

- driver is not ok or no driver, qemu keep reading and dropping packets.

It's correct from a functionality perspective. When rx is disabled or the link is down, packets should be dropped.

From a performance perspective I agree it would be better to drop packets earlier in the stack.

If you want tap to drop packets efficiently during link down/receive disabled/etc:

What's the best way to disable tap rx so the host kernel drops packets instead of queuing them?

Quoted 1 line

- driver is ok but not enough rx buffer, qemu will disable tap read poll.

QEMU relies on the kernel's socket receive buffer or netif receive queue to hold incoming packets. When QEMU enables tap read poll again it receives the queued packets.

Queuing is necessary so USB net emulation (the rx buffer is only 1 packet!) and maybe slirp work.

I think real NICs drop packets when the rx ring is exhausted, but we need to keep the queuing behavior unless someone proposes a way to eliminate it.

#14 lore raw
Quoted 80 lines · Stefan Hajnoczi
Quoted 61 lines · Jason Wang
Quoted 47 lines · Stefan Hajnoczi
Quoted 46 lines · Jason Wang
Quoted 29 lines · Stefan Hajnoczi
Quoted 14 lines · Jason Wang
Quoted 13 lines · Fam Zheng

virtio_net_receive still does the check by calling virtio_net_can_receive, if the device or driver is not ready, the packet is dropped.

This is necessary because returning false from can_receive complicates
things: the peer would disable sending until we explicitly flush the queue.

---
 hw/net/virtio-net.c | 1 -
 1 file changed, 1 deletion(-)
Quoted 14 lines

hw/net/virtio-net.c static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f, · line 1508

Quoted 12 lines
hw/net/virtio-net.c +0 -1
index d728233..dbef0d0 100644
@@ -1503,7 +1503,6 @@ static int virtio_net_load_device(VirtIODevice *vdev, QEMUFile *f,
 static NetClientInfo net_virtio_info = {
     .type = NET_CLIENT_OPTIONS_KIND_NIC,
     .size = sizeof(NICState),
-    .can_receive = virtio_net_can_receive,
     .receive = virtio_net_receive,
     .link_status_changed = virtio_net_set_link_status,
     .query_rx_filter = virtio_net_query_rxfilter,

A side effect of this patch is it will read and then drop packet is guest driver is no ok.

Quoted 5 lines

I think that the semantics of .can_receive() and .receive() return values are currently incorrect in many NICs. They have .can_receive() functions that return false for conditions where .receive() would discard the packet. So what happens is that packets get queued when they should actually be discarded.

Yes, but they are bugs more or less.

Quoted 9 lines

The purpose of the flow control (queuing) mechanism is to tell the sender to hold off until the receiver has more rx buffers available. It's a short-term thing that doesn't included link down, rx disable, or NIC reset states.

Therefore, I think this patch will not introduce a regression. It is adjusting the code to stop queuing packets when they should actually be dropped.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Thoughts?

I agree there's no functional issue. But it cause wasting of cpu cycles (consider guest is being flooded). Sometime it maybe even dangerous. For tap, we're probably ok since we have 756ae78b but for other backend, we don't.

If the guest uses iptables rules or other mechanisms to drop bogus packets the cost is even higher than discarding them at the QEMU layer.

But it was the choice of guest.

Quoted 3 lines

What's more is that if you're using link down as a DoS mitigation strategy then you might as well hot unplug the NIC.

Quoted 6 lines
Quoted 1 line

Stefan

I think there're two problems for virtio-net:

1) mitigation method when guest driver is ok. For tx, we have either timer or bh, for rx and only for tap, we have 756ae78b. We probably need fixes for other backends.

I agree. It's not directly related to the change Fam is proposing though.

Quoted 6 lines

2) when driver is not ok, the point is we should not poll the backend at all (I believe this is one of the main objects of main loop). Something like tap_can_send() and the commit that drops tap_can_send() all follow this rule. But this patch does not, we end up with:

- driver is not ok or no driver, qemu keep reading and dropping packets.

It's correct from a functionality perspective. When rx is disabled or the link is down, packets should be dropped.

Quoted 8 lines

From a performance perspective I agree it would be better to drop packets earlier in the stack.

If you want tap to drop packets efficiently during link down/receive disabled/etc:

What's the best way to disable tap rx so the host kernel drops packets instead of queuing them?

There's no such API currently especially for a unprivileged qemu. But tuntap will drop the packet if it exceeds tx_queue_length. This looks still much better than current behaviour.

Quoted 8 lines
Quoted 2 lines

- driver is ok but not enough rx buffer, qemu will disable tap read poll.

QEMU relies on the kernel's socket receive buffer or netif receive queue to hold incoming packets. When QEMU enables tap read poll again it receives the queued packets.

Queuing is necessary so USB net emulation (the rx buffer is only 1 packet!) and maybe slirp work.

Yes.

Quoted 4 lines

I think real NICs drop packets when the rx ring is exhausted, but we need to keep the queuing behavior unless someone proposes a way to eliminate it.

Yes, queueing seems useless if backend can do it.

So I'm ok with the patch, and we can optimize in the future.

Thanks