hw/net: Fix .can_receive() for NICs
v2 cover hw/net: Fix .can_receive() for NICs
v2: Don't use BH in patches 6 and 12. [Jason]
Add Stefan's reviewed-by in all others.
Drop trace point in patch 8 and move function body upward. [Michael]
Since a90a742 "tap: Drop tap_can_send", all nics that returns false from .can_receive() are required to explicitly flush the incoming queue when the status of it is changing back to true, otherwise the backend will sop processing more rx packets.
The purpose of this callback is to tell the peer backend (tap, socket, etc) "hold on until guest consumes old data because my buffer is not ready". More often than not NICs also do this when driver deactivated the card or disabled rx, causing the packets being unnessarily queued, where they should actualy be dropped.
This series adds such missing qemu_flush_queued_packets calls for all NICs, and drops such unnecessary conditions in .can_receive(), so that NICs now:
- return false from .can_receive when guest buffers are busy.
- call qemu_flush_queued_packets when buffers are available again.
- return -1 from .receive when rx is not enabled.
e1000, ne2000, rocker and vmxnet3 are not included because they're fixed by other patches on the list and applied to Stefan's tree.
virtio-net is covered by another series:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07377.html ^U
http://patchwork.ozlabs.org/patch/495379/ (the subject should have v2, my bad.)
All other NICs are okay, as they already do the flush on the state transition points.
Please review.
Fam Zheng (12):
xgmac: Drop packets with eth_can_rx is false.
pcnet: Drop pcnet_can_receive
eepro100: Drop nic_can_receive
usbnet: Drop usbnet_can_receive
etsec: Move etsec_can_receive into etsec_receive
etsec: Flush queue when rx buffer is consumed
mcf_fec: Drop mcf_fec_can_receive
milkymist-minimac2: Flush queued packets when link comes up
mipsnet: Flush queued packets when receiving is enabled
stellaris_enet: Flush queued packets when read done
dp8393x: Flush packets when link comes up
axienet: Flush queued packets when rx is done
hw/net/dp8393x.c | 8 ++++++++ hw/net/eepro100.c | 11 ----------- hw/net/fsl_etsec/etsec.c | 20 ++++++++++---------- hw/net/fsl_etsec/etsec.h | 4 +++- hw/net/fsl_etsec/rings.c | 17 +++++++++++------ hw/net/lance.c | 1 - hw/net/mcf_fec.c | 9 +-------- hw/net/milkymist-minimac2.c | 33 ++++++++++++++++----------------- hw/net/mipsnet.c | 9 +++++++-- hw/net/pcnet-pci.c | 1 - hw/net/pcnet.c | 9 --------- hw/net/pcnet.h | 1 - hw/net/stellaris_enet.c | 14 +++++--------- hw/net/xgmac.c | 8 ++++---- hw/net/xilinx_axienet.c | 17 +++++++++++++---- hw/usb/dev-network.c | 20 ++++---------------- trace-events | 1 - 17 files changed, 82 insertions(+), 101 deletions(-)
Signature · 1 line
2.4.3
PATCH v2 1/12 xgmac: Drop packets with eth_can_rx is false.
--- hw/net/xgmac.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
hw/net/xgmac.c +4 -4
@@ -312,10 +312,8 @@ static const MemoryRegionOps enet_mem_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int eth_can_rx (NetClientState *nc) +static int eth_can_rx (XgmacState *s) { -XgmacState *s =qemu_get_nic_opaque (nc); -/* RX enabled? */ return s->regs[DMA_CONTROL ] &DMA_CONTROL_SR ; }
@@ -329,6 +327,9 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)struct desc bd;ssize_t ret; +if (!eth_can_rx (s)) { +return -1 ; + } unicast = ~buf[0 ] &0x1 ; broadcast =memcmp (buf, sa_bcast,6 ) ==0 ; multicast = !unicast && !broadcast;
@@ -371,7 +372,6 @@ out:static NetClientInfo net_xgmac_enet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = eth_can_rx, .receive = eth_rx, };
Signature · 1 line
2.4.3
PATCH v2 2/12 pcnet: Drop pcnet_can_receive
In reply to Fam Zhengpcnet_receive already checks the conditions and drop packets if false. Due to the new semantics since 6e99c63 ("net/socket: Drop net_socket_can_send"), having .can_receive returning 0 requires us to explicitly flush the queued packets when the conditions are becoming true, but queuing the packets when guest driver is not ready doesn't make much sense.
--- hw/net/lance.c | 1 - hw/net/pcnet-pci.c | 1 - hw/net/pcnet.c | 9 --------- hw/net/pcnet.h | 1 - 4 files changed, 12 deletions(-)
hw/net/lance.c +0 -1
@@ -94,7 +94,6 @@ static const MemoryRegionOps lance_mem_ops = {static NetClientInfo net_lance_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = pcnet_can_receive, .receive = pcnet_receive, .link_status_changed = pcnet_set_link_status, };
hw/net/pcnet-pci.c +0 -1
@@ -273,7 +273,6 @@ static void pci_pcnet_uninit(PCIDevice *dev)static NetClientInfo net_pci_pcnet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = pcnet_can_receive, .receive = pcnet_receive, .link_status_changed = pcnet_set_link_status, };
hw/net/pcnet.c +0 -9
@@ -995,15 +995,6 @@ static int pcnet_tdte_poll(PCNetState *s)return !!(CSR_CXST (s) &0x8000 ); } -int pcnet_can_receive (NetClientState *nc) -{ -PCNetState *s =qemu_get_nic_opaque (nc); -if (CSR_STOP (s) ||CSR_SPND (s)) -return 0 ; - -return sizeof (s->buffer)-16 ; -} -#define MIN_BUF_SIZE 60ssize_t pcnet_receive (NetClientState *nc,const uint8_t *buf,size_t size_)
hw/net/pcnet.h +0 -1
@@ -60,7 +60,6 @@ uint32_t pcnet_ioport_readw(void *opaque, uint32_t addr);void pcnet_ioport_writel (void *opaque,uint32_t addr,uint32_t val);uint32_t pcnet_ioport_readl (void *opaque,uint32_t addr);uint32_t pcnet_bcr_readw (PCNetState *s,uint32_t rap); -int pcnet_can_receive (NetClientState *nc);ssize_t pcnet_receive (NetClientState *nc,const uint8_t *buf,size_t size_);void pcnet_set_link_status (NetClientState *nc);void pcnet_common_init (DeviceState *dev,PCNetState *s,NetClientInfo *info);
Signature · 1 line
2.4.3
PATCH v2 3/12 eepro100: Drop nic_can_receive
In reply to Fam Zhengnic_receive already checks the conditions and drop packets if false. Due to the new semantics since 6e99c63 ("net/socket: Drop net_socket_can_send"), having .can_receive returning 0 requires us to explicitly flush the queued packets when the conditions are becoming true, but queuing the packets when guest driver is not ready doesn't make much sense.
--- hw/net/eepro100.c | 11 ----------- 1 file changed, 11 deletions(-)
hw/net/eepro100.c +0 -11
@@ -1617,16 +1617,6 @@ static const MemoryRegionOps eepro100_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int nic_can_receive (NetClientState *nc) -{ -EEPRO100State *s =qemu_get_nic_opaque (nc); -TRACE (RXTX ,logout ("%p\n" , s)); -return get_ru_state (s) == ru_ready; -#if 0 -return !eepro100_buffer_full (s); -#endif -} -static ssize_t nic_receive (NetClientState *nc,const uint8_t * buf,size_t size) {/* TODO:
@@ -1844,7 +1834,6 @@ static void pci_nic_uninit(PCIDevice *pci_dev)static NetClientInfo net_eepro100_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = nic_can_receive, .receive = nic_receive, };
Signature · 1 line
2.4.3
PATCH v2 4/12 usbnet: Drop usbnet_can_receive
In reply to Fam Zhengusbnet_receive already drops packet if rndis_state is not RNDIS_DATA_INITIALIZED, and queues packet if in buffer is not available. The only difference is s->dev.config but that is similar to rndis_state.
Drop usbnet_can_receive and move these checks to usbnet_receive, so that we don't need to explicitly flush the queue when s->dev.config changes value.
--- hw/usb/dev-network.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-)
hw/usb/dev-network.c +4 -16
@@ -1268,6 +1268,10 @@ static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t sizuint8_t *in_buf = s->in_buf;size_t total_size = size; +if (!s->dev.config) { +return -1 ; + } +if (is_rndis (s)) {if (s->rndis_state !=RNDIS_DATA_INITIALIZED ) {return -1 ;
@@ -1309,21 +1313,6 @@ static ssize_t usbnet_receive(NetClientState *nc, const uint8_t *buf, size_t sizreturn size; } -static int usbnet_can_receive (NetClientState *nc) -{ -USBNetState *s =qemu_get_nic_opaque (nc); - -if (!s->dev.config) { -return 0 ; - } - -if (is_rndis (s) && s->rndis_state !=RNDIS_DATA_INITIALIZED ) { -return 1 ; - } - -return !s->in_len; -} -static void usbnet_cleanup (NetClientState *nc) {USBNetState *s =qemu_get_nic_opaque (nc);
@@ -1343,7 +1332,6 @@ static void usb_net_handle_destroy(USBDevice *dev)static NetClientInfo net_usbnet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = usbnet_can_receive, .receive = usbnet_receive, .cleanup = usbnet_cleanup, };
Signature · 1 line
2.4.3
PATCH v2 5/12 etsec: Move etsec_can_receive into etsec_receive
In reply to Fam ZhengWhen etsec_reset returns 0, peer would queue the packet as if .can_receive returns false. Drop etsec_can_receive and let etsec_receive carry the semantics.
--- hw/net/fsl_etsec/etsec.c | 11 +---------- hw/net/fsl_etsec/etsec.h | 2 +- hw/net/fsl_etsec/rings.c | 14 ++++++++------ 3 files changed, 10 insertions(+), 17 deletions(-)
hw/net/fsl_etsec/etsec.c +1 -10
@@ -338,13 +338,6 @@ static void etsec_reset(DeviceState *d)MII_SR_100X_FD_CAPS |MII_SR_100T4_CAPS ; } -static int etsec_can_receive (NetClientState *nc) -{ -eTSEC *etsec =qemu_get_nic_opaque (nc); - -return etsec->rx_buffer_len ==0 ; -} -static ssize_t etsec_receive (NetClientState *nc,const uint8_t *buf,size_t size)
@@ -355,8 +348,7 @@ static ssize_t etsec_receive(NetClientState *nc,fprintf (stderr,"%s receive size:%d\n" , etsec->nic->nc.name, size);qemu_hexdump (buf, stderr,"" , size);#endif -etsec_rx_ring_write (etsec, buf, size); -return size; +return etsec_rx_ring_write (etsec, buf, size); }
@@ -370,7 +362,6 @@ static void etsec_set_link_status(NetClientState *nc)static NetClientInfo net_etsec_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = etsec_can_receive, .receive = etsec_receive, .link_status_changed = etsec_set_link_status, };
hw/net/fsl_etsec/etsec.h +1 -1
@@ -162,7 +162,7 @@ DeviceState *etsec_create(hwaddr base,void etsec_walk_tx_ring (eTSEC *etsec,int ring_nbr);void etsec_walk_rx_ring (eTSEC *etsec,int ring_nbr); -void etsec_rx_ring_write (eTSEC *etsec,const uint8_t *buf,size_t size); +ssize_t etsec_rx_ring_write (eTSEC *etsec,const uint8_t *buf,size_t size); void etsec_write_miim(eTSEC *etsec, eTSEC_Register *reg,
hw/net/fsl_etsec/rings.c +8 -6
@@ -481,40 +481,42 @@ static void rx_init_frame(eTSEC *etsec, const uint8_t *buf, size_t size) etsec->rx_buffer_len, etsec->rx_padding); } -void etsec_rx_ring_write (eTSEC *etsec,const uint8_t *buf,size_t size) +ssize_t etsec_rx_ring_write (eTSEC *etsec,const uint8_t *buf,size_t size) {int ring_nbr =0 ;/* Always use ring0 (no filer) */ if (etsec->rx_buffer_len !=0 ) {RING_DEBUG ("%s: We can't receive now," " a buffer is already in the pipe\n" , __func__); -return ; +return 0 ; }if (etsec->regs[RSTAT ].value &1 << (23 - ring_nbr)) {RING_DEBUG ("%s: The ring is halted\n" , __func__); -return ; +return -1 ; }if (etsec->regs[DMACTRL ].value &DMACTRL_GRS ) {RING_DEBUG ("%s: Graceful receive stop\n" , __func__); -return ; +return -1 ; }if (!(etsec->regs[MACCFG1 ].value &MACCFG1_RX_EN )) {RING_DEBUG ("%s: MAC Receive not enabled\n" , __func__); -return ; +return -1 ; }if ((etsec->regs[RCTRL ].value &RCTRL_RSF ) && (size <60 )) {/* CRC is not in the packet yet, so short frame is below 60 bytes */ RING_DEBUG ("%s: Drop short frame\n" , __func__); -return ; +return -1 ; }rx_init_frame (etsec, buf, size);etsec_walk_rx_ring (etsec, ring_nbr); + +return size; }void etsec_walk_rx_ring (eTSEC *etsec,int ring_nbr)
Signature · 1 line
2.4.3
PATCH v2 6/12 etsec: Flush queue when rx buffer is consumed
In reply to Fam ZhengThe BH will be scheduled when etsec->rx_buffer_len is becoming 0, which is the condition of queuing.
--- hw/net/fsl_etsec/etsec.c | 11 ++++++++++- hw/net/fsl_etsec/etsec.h | 2 ++ hw/net/fsl_etsec/rings.c | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-)
hw/net/fsl_etsec/etsec.c +10 -1
@@ -342,13 +342,22 @@ static ssize_t etsec_receive(NetClientState *nc,const uint8_t *buf, size_t size) { + ssize_t ret;eTSEC *etsec =qemu_get_nic_opaque (nc);#if defined(HEX_DUMP )fprintf (stderr,"%s receive size:%d\n" , etsec->nic->nc.name, size);qemu_hexdump (buf, stderr,"" , size);#endif -return etsec_rx_ring_write (etsec, buf, size); +/* Flush is unnecessary as are already in receiving path */ + etsec->need_flush = false; + ret =etsec_rx_ring_write (etsec, buf, size); +if (ret ==0 ) { +/* The packet will be queued, let's flush it when buffer is avilable +* again. */ + etsec->need_flush = true; + } +return ret; }
hw/net/fsl_etsec/etsec.h +2 -0
@@ -144,6 +144,8 @@ typedef struct eTSEC {QEMUBH *bh;struct ptimer_state *ptimer; +/* Whether we should flush the rx queue when buffer becomes available. */ +bool need_flush; } eTSEC;#define TYPE_ETSEC_COMMON "eTSEC"
hw/net/fsl_etsec/rings.c +3 -0
@@ -646,6 +646,9 @@ void etsec_walk_rx_ring(eTSEC *etsec, int ring_nbr) }else { etsec->rx_buffer_len =0 ; etsec->rx_buffer =NULL ; +if (etsec->need_flush) { +qemu_flush_queued_packets (qemu_get_queue (etsec->nic)); + } }RING_DEBUG ("eTSEC End of ring_write: remaining_data:%zu\n" , remaining_data);
Signature · 1 line
2.4.3
PATCH v2 7/12 mcf_fec: Drop mcf_fec_can_receive
In reply to Fam ZhengThe semantics of .can_receive requires us to flush the queue explicitly when s->rx_enabled becomes true after it returns 0, but the packet being queued is not meaningful since the guest hasn't activated the card. Let's just drop the packet in this case.
--- hw/net/mcf_fec.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-)
hw/net/mcf_fec.c +1 -8
@@ -351,12 +351,6 @@ static void mcf_fec_write(void *opaque, hwaddr addr,mcf_fec_update (s); } -static int mcf_fec_can_receive (NetClientState *nc) -{ -mcf_fec_state *s =qemu_get_nic_opaque (nc); -return s->rx_enabled; -} -static ssize_t mcf_fec_receive (NetClientState *nc,const uint8_t *buf,size_t size) {mcf_fec_state *s =qemu_get_nic_opaque (nc);
@@ -370,7 +364,7 @@ static ssize_t mcf_fec_receive(NetClientState *nc, const uint8_t *buf, size_t siDPRINTF ("do_rx len %d\n" , size);if (!s->rx_enabled) { -fprintf (stderr,"mcf_fec_receive: Unexpected packet\n" ); +return -1 ; }/* 4 bytes for the CRC. */ size +=4 ;
@@ -442,7 +436,6 @@ static const MemoryRegionOps mcf_fec_ops = {static NetClientInfo net_mcf_fec_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = mcf_fec_can_receive, .receive = mcf_fec_receive, };
Signature · 1 line
2.4.3
PATCH v2 8/12 milkymist-minimac2: Flush queued packets when link comes up
In reply to Fam ZhengDrop .can_receive and move the semantics into minimac2_rx, by returning 0.
That is once minimac2_rx returns 0, incoming packets will be queued until the queue is explicitly flushed. We do this when s->regs[R_STATE0] or s->regs[R_STATE1] is changed in minimac2_write.
Also drop the unused trace point.
--- hw/net/milkymist-minimac2.c | 33 ++++++++++++++++----------------- trace-events | 1 - 2 files changed, 16 insertions(+), 18 deletions(-)
hw/net/milkymist-minimac2.c +16 -17
@@ -303,8 +303,7 @@ static ssize_t minimac2_rx(NetClientState *nc, const uint8_t *buf, size_t size) r_state =R_STATE1 ; rx_buf = s->rx1_buf; }else { -trace_milkymist_minimac2_drop_rx_frame (buf); -return size; +return 0 ; }/* assemble frame */
@@ -354,6 +353,18 @@ minimac2_read(void *opaque, hwaddr addr, unsigned size)return r; } +static int minimac2_can_rx (MilkymistMinimac2State *s) +{ +if (s->regs[R_STATE0 ] ==STATE_LOADED ) { +return 1 ; + } +if (s->regs[R_STATE1 ] ==STATE_LOADED ) { +return 1 ; + } + +return 0 ; +} +static void minimac2_write (void *opaque,hwaddr addr,uint64_t value,unsigned size)
@@ -387,6 +398,9 @@ minimac2_write(void *opaque, hwaddr addr, uint64_t value,case R_STATE1 : s->regs[addr] = value;update_rx_interrupt (s); +if (minimac2_can_rx (s)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + }break ;case R_SETUP :case R_COUNT0 :
@@ -411,20 +425,6 @@ static const MemoryRegionOps minimac2_ops = { .endianness = DEVICE_NATIVE_ENDIAN, }; -static int minimac2_can_rx (NetClientState *nc) -{ -MilkymistMinimac2State *s =qemu_get_nic_opaque (nc); - -if (s->regs[R_STATE0 ] ==STATE_LOADED ) { -return 1 ; - } -if (s->regs[R_STATE1 ] ==STATE_LOADED ) { -return 1 ; - } - -return 0 ; -} -static void milkymist_minimac2_reset (DeviceState *d) {MilkymistMinimac2State *s =MILKYMIST_MINIMAC2 (d);
@@ -445,7 +445,6 @@ static void milkymist_minimac2_reset(DeviceState *d)static NetClientInfo net_milkymist_minimac2_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = minimac2_can_rx, .receive = minimac2_rx, };
trace-events +0 -1
@@ -828,7 +828,6 @@ milkymist_minimac2_mdio_write(uint8_t phy_addr, uint8_t addr, uint16_t value) "p milkymist_minimac2_mdio_read(uint8_t phy_addr, uint8_t addr, uint16_t value) "phy_addr %02x addr %02x value %04x" milkymist_minimac2_tx_frame(uint32_t length) "length %u" milkymist_minimac2_rx_frame(const void *buf, uint32_t length) "buf %p length %u" -milkymist_minimac2_drop_rx_frame(const void *buf) "buf %p" milkymist_minimac2_rx_transfer(const void *buf, uint32_t length) "buf %p length %d" milkymist_minimac2_raise_irq_rx(void) "Raise IRQ RX" milkymist_minimac2_lower_irq_rx(void) "Lower IRQ RX"
Signature · 1 line
2.4.3
PATCH v2 9/12 mipsnet: Flush queued packets when receiving is enabled
In reply to Fam ZhengDrop .can_receive and move the semantics to mipsnet_receive, by returning 0.
After 0 is returned, we must flush the queue explicitly to restart it:
Call qemu_flush_queued_packets when s->busy or s->rx_count is being updated.
--- hw/net/mipsnet.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
hw/net/mipsnet.c +7 -2
@@ -80,7 +80,7 @@ static ssize_t mipsnet_receive(NetClientState *nc, const uint8_t *buf, size_t sitrace_mipsnet_receive (size);if (!mipsnet_can_receive (nc)) -return -1 ; +return 0 ; s->busy =1 ;
@@ -134,6 +134,9 @@ static uint64_t mipsnet_ioport_read(void *opaque, hwaddr addr,if (s->rx_count) { s->rx_count--; ret = s->rx_buffer[s->rx_read++]; +if (mipsnet_can_receive (s->nic->ncs)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } }break ;/* Reads as zero. */
@@ -170,6 +173,9 @@ static void mipsnet_ioport_write(void *opaque, hwaddr addr, } s->busy = !!s->intctl;mipsnet_update_irq (s); +if (mipsnet_can_receive (s->nic->ncs)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + }break ;case MIPSNET_TX_DATA_BUFFER : s->tx_buffer[s->tx_written++] = val;
@@ -214,7 +220,6 @@ static const VMStateDescription vmstate_mipsnet = {static NetClientInfo net_mipsnet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = mipsnet_can_receive, .receive = mipsnet_receive, };
Signature · 1 line
2.4.3
PATCH v2 10/12 stellaris_enet: Flush queued packets when read done
In reply to Fam ZhengIf s->np reaches 31, the queue will be disabled by peer when it sees stellaris_enet_can_receive() returns false, until we explicitly flushes it which notifies the peer. Do this when guest is done reading all existing data.
Move the semantics to stellaris_enet_receive, by returning 0 when the buffer is full, so that new packets will be queued. In stellaris_enet_read, flush and restart the queue when guest has done reading.
--- hw/net/stellaris_enet.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-)
hw/net/stellaris_enet.c +5 -9
@@ -228,8 +228,7 @@ static ssize_t stellaris_enet_receive(NetClientState *nc, const uint8_t *buf, siif ((s->rctl &SE_RCTL_RXEN ) ==0 )return -1 ;if (s->np >=31 ) { -DPRINTF ("Packet dropped\n" ); -return -1 ; +return 0 ; }DPRINTF ("Received packet len=%zu\n" , size);
@@ -260,13 +259,8 @@ static ssize_t stellaris_enet_receive(NetClientState *nc, const uint8_t *buf, sireturn size; } -static int stellaris_enet_can_receive (NetClientState *nc) +static int stellaris_enet_can_receive (stellaris_enet_state *s) { -stellaris_enet_state *s =qemu_get_nic_opaque (nc); - -if ((s->rctl &SE_RCTL_RXEN ) ==0 ) -return 1 ; -return (s->np <31 ); }
@@ -307,6 +301,9 @@ static uint64_t stellaris_enet_read(void *opaque, hwaddr offset, s->next_packet =0 ; s->np--;DPRINTF ("RX done np=%d\n" , s->np); +if (!s->np &&stellaris_enet_can_receive (s)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } }return val; }
@@ -454,7 +451,6 @@ static void stellaris_enet_reset(stellaris_enet_state *s)static NetClientInfo net_stellaris_enet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = stellaris_enet_can_receive, .receive = stellaris_enet_receive, };
Signature · 1 line
2.4.3
PATCH v2 11/12 dp8393x: Flush packets when link comes up
In reply to Fam Zheng.can_receive callback changes semantics that once return 0, backend will try sending again until explicitly flushed, change the device to meet that.
dp8393x_can_receive checks SONIC_CR_RXEN bit in SONIC_CR register and SONIC_ISR_RBE bit in SONIC_ISR register, try flushing the queue when either bit is being updated.
--- hw/net/dp8393x.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/net/dp8393x.c +8 -0
@@ -327,9 +327,14 @@ static void dp8393x_do_stop_timer(dp8393xState *s)dp8393x_update_wt_regs (s); } +static int dp8393x_can_receive (NetClientState *nc); +static void dp8393x_do_receiver_enable (dp8393xState *s) { s->regs[SONIC_CR ] &= ~SONIC_CR_RXDIS ; +if (dp8393x_can_receive (s->nic->ncs)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } }static void dp8393x_do_receiver_disable (dp8393xState *s)
@@ -569,6 +574,9 @@ static void dp8393x_write(void *opaque, hwaddr addr, uint64_t data,dp8393x_do_read_rra (s); }dp8393x_update_irq (s); +if (dp8393x_can_receive (s->nic->ncs)) { +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + }break ;/* Ignore least significant bit */ case SONIC_RSA :
Signature · 1 line
2.4.3
PATCH v2 12/12 axienet: Flush queued packets when rx is done
In reply to Fam Zhengeth_can_rx checks s->rxsize and returns false if it is non-zero. Because of the .can_receive semantics change, this will make the incoming queue disabled by peer, until it is explicitly flushed. So we should flush it when s->rxsize is becoming zero.
Squash eth_can_rx semantics into etx_rx and drop .can_receive() callback, also add flush when rx buffer becomes available again after a packet gets queued.
The other conditions, "!axienet_rx_resetting(s) && axienet_rx_enabled(s)" are OK because enet_write already calls qemu_flush_queued_packets when the register bits are changed.
--- hw/net/xilinx_axienet.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
hw/net/xilinx_axienet.c +13 -4
@@ -401,6 +401,9 @@ struct XilinxAXIEnet {uint8_t rxapp[CONTROL_PAYLOAD_SIZE ];uint32_t rxappsize; + +/* Whether axienet_eth_rx_notify should flush incoming queue. */ +bool need_flush; };static void axienet_rx_reset (XilinxAXIEnet *s)
@@ -658,10 +661,8 @@ static const MemoryRegionOps enet_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int eth_can_rx (NetClientState *nc) +static int eth_can_rx (XilinxAXIEnet *s) { -XilinxAXIEnet *s =qemu_get_nic_opaque (nc); -/* RX enabled? */ return !s->rxsize && !axienet_rx_resetting (s) &&axienet_rx_enabled (s); }
@@ -701,6 +702,10 @@ static void axienet_eth_rx_notify(void *opaque) s->rxpos += ret;if (!s->rxsize) { s->regs[R_IS ] |=IS_RX_COMPLETE ; +if (s->need_flush) { + s->need_flush = false; +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } } }enet_update_irq (s);
@@ -721,6 +726,11 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)DENET (qemu_log ("%s: %zd bytes\n" , __func__, size)); +if (!eth_can_rx (s)) { + s->need_flush = true; +return 0 ; + } + unicast = ~buf[0 ] &0x1 ; broadcast =memcmp (buf, sa_bcast,6 ) ==0 ; multicast = !unicast && !broadcast;
@@ -925,7 +935,6 @@ xilinx_axienet_data_stream_push(StreamSlave *obj, uint8_t *buf, size_t size)static NetClientInfo net_xilinx_enet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = eth_can_rx, .receive = eth_rx, };
Signature · 1 line
2.4.3
Re: [Qemu-devel] [PATCH v2 for-2.4 12/12] axienet: Flush queued packets when rx is done
Quoted 56 lines · Fam Zheng
eth_can_rxcheckss->rxsizeand returns false if it is non-zero. Because of the .can_receive semantics change, this will make the incoming queue disabled by peer, until it is explicitly flushed. So we should flush it whens->rxsizeis becoming zero.Squash
eth_can_rxsemantics intoetx_rxand drop .can_receive() callback, also add flush when rx buffer becomes available again after a packet gets queued.The other conditions, "!axienet_rx_resetting(s) &&
axienet_rx_enabled(s)" are OK becauseenet_writealready callsqemu_flush_queued_packetswhen the register bits are changed.Signed-off-by Fam Zheng <[email protected]>--- hw/net/xilinx_axienet.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)hw/net/xilinx_axienet.c +8
-3@@ -401,6 +401,9 @@ struct XilinxAXIEnet {uint8_t rxapp[CONTROL_PAYLOAD_SIZE ];uint32_t rxappsize; + +/* Whether axienet_eth_rx_notify should flush incoming queue. */ +bool need_flush; };static void axienet_rx_reset (XilinxAXIEnet *s)@@ -658,10 +661,8 @@ static const MemoryRegionOps enet_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int eth_can_rx (NetClientState *nc) +static int eth_can_rx (XilinxAXIEnet *s) { -XilinxAXIEnet *s =qemu_get_nic_opaque (nc); -/* RX enabled? */ return !s->rxsize && !axienet_rx_resetting (s) &&axienet_rx_enabled (s); }@@ -701,6 +702,10 @@ static void axienet_eth_rx_notify(void *opaque) s->rxpos += ret;if (!s->rxsize) { s->regs[R_IS ] |=IS_RX_COMPLETE ; +if (s->need_flush) { + s->need_flush = false; +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } } }enet_update_irq (s);
hw/net/xilinx_axienet.c static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size) · line 733
Quoted 8 lines
hw/net/xilinx_axienet.c +5
-0@@ -721,6 +726,11 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)DENET (qemu_log ("%s: %zd bytes\n" , __func__, size)); +if (!eth_can_rx (s)) { + s->need_flush = true; +return 0 ; + } +
axienet_eth_rx_notify() was only called by eth_rx(). So when s->need_flush is true, we won't ever reach axienet_eth_rx_notify()?
Quoted 11 lines
hw/net/xilinx_axienet.c +0
-1unicast = ~buf[0 ] &0x1 ; broadcast =memcmp (buf, sa_bcast,6 ) ==0 ; multicast = !unicast && !broadcast;@@ -925,7 +935,6 @@ xilinx_axienet_data_stream_push(StreamSlave *obj, uint8_t *buf, size_t size)static NetClientInfo net_xilinx_enet_info = { .type =NET_CLIENT_OPTIONS_KIND_NIC , .size =sizeof (NICState), - .can_receive = eth_can_rx, .receive = eth_rx, };
Re: [Qemu-devel] [PATCH v2 for-2.4 12/12] axienet: Flush queued packets when rx is done
Quoted 62 lines · Jason Wang
Quoted 59 lines · Fam Zheng
eth_can_rxcheckss->rxsizeand returns false if it is non-zero. Because of the .can_receive semantics change, this will make the incoming queue disabled by peer, until it is explicitly flushed. So we should flush it whens->rxsizeis becoming zero.Squash
eth_can_rxsemantics intoetx_rxand drop .can_receive() callback, also add flush when rx buffer becomes available again after a packet gets queued.The other conditions, "!axienet_rx_resetting(s) &&
axienet_rx_enabled(s)" are OK becauseenet_writealready callsqemu_flush_queued_packetswhen the register bits are changed.Signed-off-by Fam Zheng <[email protected]>--- hw/net/xilinx_axienet.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)hw/net/xilinx_axienet.c +8
-3@@ -401,6 +401,9 @@ struct XilinxAXIEnet {uint8_t rxapp[CONTROL_PAYLOAD_SIZE ];uint32_t rxappsize; + +/* Whether axienet_eth_rx_notify should flush incoming queue. */ +bool need_flush; };static void axienet_rx_reset (XilinxAXIEnet *s)@@ -658,10 +661,8 @@ static const MemoryRegionOps enet_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int eth_can_rx (NetClientState *nc) +static int eth_can_rx (XilinxAXIEnet *s) { -XilinxAXIEnet *s =qemu_get_nic_opaque (nc); -/* RX enabled? */ return !s->rxsize && !axienet_rx_resetting (s) &&axienet_rx_enabled (s); }@@ -701,6 +702,10 @@ static void axienet_eth_rx_notify(void *opaque) s->rxpos += ret;if (!s->rxsize) { s->regs[R_IS ] |=IS_RX_COMPLETE ; +if (s->need_flush) { + s->need_flush = false; +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } } }enet_update_irq (s);@@ -721,6 +726,11 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)DENET (qemu_log ("%s: %zd bytes\n" , __func__, size));
Quoted 8 lines
hw/net/xilinx_axienet.c static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size) · line 733
Quoted 5 lines
hw/net/xilinx_axienet.c +5
-0+if (!eth_can_rx (s)) { + s->need_flush = true; +return 0 ; + } +
axienet_eth_rx_notify()was only called byeth_rx(). So whens->need_flushis true, we won't ever reachaxienet_eth_rx_notify()?
We will.
If we are here it measn a previous call to axienet_eth_rx_notify hasn't drained the buffer:
static void axienet_eth_rx_notify(void *opaque)
{
...
while (s->rxsize && stream_can_push(s->tx_data_dev,
axienet_eth_rx_notify, s)) {
size_t ret = stream_push(s->tx_data_dev, (void *)s->rxmem + s->rxpos,
s->rxsize);
s->rxsize -= ret;
s->rxpos += ret;
if (!s->rxsize) {
s->regs[R_IS] |= IS_RX_COMPLETE;
}
}
...
}axienet_eth_rx_notify is passed to stream_can_push so it will be reached again once s->tx_data_dev can receive more data:
typedef struct StreamSlaveClass {
InterfaceClass parent;
/**
* can push - determine if a stream slave is capable of accepting at least
* one byte of data. Returns false if cannot accept. If not implemented, the
* slave is assumed to always be capable of receiving.
* @notify: Optional callback that the slave will call when the slave is
* capable of receiving again. Only called if false is returned.
* @notify_opaque: opaque data to pass to notify call.
*/
bool (*can_push)(StreamSlave *obj, StreamCanPushNotifyFn notify,
void *notify_opaque);
...
Am I missing anything?
Fam
Re: [Qemu-devel] [PATCH v2 for-2.4 12/12] axienet: Flush queued packets when rx is done
Quoted 109 lines · Fam Zheng
Quoted 61 lines · Jason Wang
Quoted 59 lines · Fam Zheng
eth_can_rxcheckss->rxsizeand returns false if it is non-zero. Because of the .can_receive semantics change, this will make the incoming queue disabled by peer, until it is explicitly flushed. So we should flush it whens->rxsizeis becoming zero.Squash
eth_can_rxsemantics intoetx_rxand drop .can_receive() callback, also add flush when rx buffer becomes available again after a packet gets queued.The other conditions, "!axienet_rx_resetting(s) &&
axienet_rx_enabled(s)" are OK becauseenet_writealready callsqemu_flush_queued_packetswhen the register bits are changed.Signed-off-by Fam Zheng <[email protected]>--- hw/net/xilinx_axienet.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)hw/net/xilinx_axienet.c +8
-3@@ -401,6 +401,9 @@ struct XilinxAXIEnet {uint8_t rxapp[CONTROL_PAYLOAD_SIZE ];uint32_t rxappsize; + +/* Whether axienet_eth_rx_notify should flush incoming queue. */ +bool need_flush; };static void axienet_rx_reset (XilinxAXIEnet *s)@@ -658,10 +661,8 @@ static const MemoryRegionOps enet_ops = { .endianness = DEVICE_LITTLE_ENDIAN, }; -static int eth_can_rx (NetClientState *nc) +static int eth_can_rx (XilinxAXIEnet *s) { -XilinxAXIEnet *s =qemu_get_nic_opaque (nc); -/* RX enabled? */ return !s->rxsize && !axienet_rx_resetting (s) &&axienet_rx_enabled (s); }@@ -701,6 +702,10 @@ static void axienet_eth_rx_notify(void *opaque) s->rxpos += ret;if (!s->rxsize) { s->regs[R_IS ] |=IS_RX_COMPLETE ; +if (s->need_flush) { + s->need_flush = false; +qemu_flush_queued_packets (qemu_get_queue (s->nic)); + } } }enet_update_irq (s);@@ -721,6 +726,11 @@ static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)DENET (qemu_log ("%s: %zd bytes\n" , __func__, size));Quoted 7 lines
hw/net/xilinx_axienet.c static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size) · line 733
Quoted 5 lines
hw/net/xilinx_axienet.c +5
-0+if (!eth_can_rx (s)) { + s->need_flush = true; +return 0 ; + } +
axienet_eth_rx_notify()was only called byeth_rx(). So whens->need_flushis true, we won't ever reachaxienet_eth_rx_notify()?We will.
If we are here it measn a previous call to
axienet_eth_rx_notifyhasn't drained the buffer:static void axienet_eth_rx_notify(void *opaque) { ... while (s->rxsize && stream_can_push(s->tx_data_dev, axienet_eth_rx_notify, s)) { size_t ret = stream_push(s->tx_data_dev, (void *)s->rxmem + s->rxpos, s->rxsize); s->rxsize -= ret; s->rxpos += ret; if (!s->rxsize) { s->regs[R_IS] |= IS_RX_COMPLETE; } } ... }
axienet_eth_rx_notifyis passed tostream_can_pushso it will be reached again onces->tx_data_devcan receive more data:typedef struct StreamSlaveClass {
InterfaceClass parent;
/**
* can push - determine if a stream slave is capable of accepting at least
* one byte of data. Returns false if cannot accept. If not implemented, the
* slave is assumed to always be capable of receiving.
* @notify: Optional callback that the slave will call when the slave is
* capable of receiving again. Only called if false is returned.
* @notify_opaque: opaque data to pass to notify call.
*/
bool (*can_push)(StreamSlave *obj, StreamCanPushNotifyFn notify,
void*notify_opaque);
...
Quoted 3 lines
Am I missing anything?
Fam
Probably not. I misses the possible call in axidma_write(). So the patch is ok :)
Quoted 27 lines · Fam Zheng
v2: Don't use BH in patches 6 and 12. [Jason]
Add Stefan's reviewed-by in all others.
Drop trace point in patch 8 and move function body upward. [Michael]Since a90a742 "tap: Drop
tap_can_send", all nics that returns false from .can_receive() are required to explicitly flush the incoming queue when the status of it is changing back to true, otherwise the backend will sop processing more rx packets.The purpose of this callback is to tell the peer backend (tap, socket, etc) "hold on until guest consumes old data because my buffer is not ready". More often than not NICs also do this when driver deactivated the card or disabled rx, causing the packets being unnessarily queued, where they should actualy be dropped.
This series adds such missing
qemu_flush_queued_packetscalls for all NICs, and drops such unnecessary conditions in .can_receive(), so that NICs now:- return false from .can_receive when guest buffers are busy.
- callqemu_flush_queued_packetswhen buffers are available again.
- return -1 from .receive when rx is not enabled.e1000, ne2000, rocker and vmxnet3 are not included because they're fixed by other patches on the list and applied to Stefan's tree.
virtio-net is covered by another series:
Quoted 8 lines
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07377.html ^U
http://patchwork.ozlabs.org/patch/495379/ (the subject should have v2, my bad.)
All other NICs are okay, as they already do the flush on the state transition points.
Please review.
Thanks
Quoted 34 lines
Fam Zheng (12):
xgmac: Drop packets witheth_can_rxis false.
pcnet: Droppcnet_can_receive
eepro100: Dropnic_can_receive
usbnet: Dropusbnet_can_receive
etsec: Moveetsec_can_receiveintoetsec_receive
etsec: Flush queue when rx buffer is consumed
mcf_fec: Dropmcf_fec_can_receive
milkymist-minimac2: Flush queued packets when link comes up
mipsnet: Flush queued packets when receiving is enabled
stellaris_enet: Flush queued packets when read done
dp8393x: Flush packets when link comes up
axienet: Flush queued packets when rx is donehw/net/dp8393x.c | 8 ++++++++ hw/net/eepro100.c | 11 ----------- hw/net/fsl_etsec/etsec.c | 20 ++++++++++---------- hw/net/fsl_etsec/etsec.h | 4 +++- hw/net/fsl_etsec/rings.c | 17 +++++++++++------ hw/net/lance.c | 1 - hw/net/mcf_fec.c | 9 +-------- hw/net/milkymist-minimac2.c | 33 ++++++++++++++++----------------- hw/net/mipsnet.c | 9 +++++++-- hw/net/pcnet-pci.c | 1 - hw/net/pcnet.c | 9 --------- hw/net/pcnet.h | 1 - hw/net/stellaris_enet.c | 14 +++++--------- hw/net/xgmac.c | 8 ++++---- hw/net/xilinx_axienet.c | 17 +++++++++++++---- hw/usb/dev-network.c | 20 ++++---------------- trace-events | 1 - 17 files changed, 82 insertions(+), 101 deletions(-)
Quoted 60 lines · Fam Zheng
v2: Don't use BH in patches 6 and 12. [Jason]
Add Stefan's reviewed-by in all others.
Drop trace point in patch 8 and move function body upward. [Michael]Since a90a742 "tap: Drop
tap_can_send", all nics that returns false from .can_receive() are required to explicitly flush the incoming queue when the status of it is changing back to true, otherwise the backend will sop processing more rx packets.The purpose of this callback is to tell the peer backend (tap, socket, etc) "hold on until guest consumes old data because my buffer is not ready". More often than not NICs also do this when driver deactivated the card or disabled rx, causing the packets being unnessarily queued, where they should actualy be dropped.
This series adds such missing
qemu_flush_queued_packetscalls for all NICs, and drops such unnecessary conditions in .can_receive(), so that NICs now:- return false from .can_receive when guest buffers are busy.
- callqemu_flush_queued_packetswhen buffers are available again.
- return -1 from .receive when rx is not enabled.e1000, ne2000, rocker and vmxnet3 are not included because they're fixed by other patches on the list and applied to Stefan's tree.
virtio-net is covered by another series:
https://lists.gnu.org/archive/html/qemu-devel/2015-06/msg07377.html ^U
http://patchwork.ozlabs.org/patch/495379/ (the subject should have v2, my bad.)
All other NICs are okay, as they already do the flush on the state transition points.
Please review.
Fam Zheng (12):
xgmac: Drop packets witheth_can_rxis false.
pcnet: Droppcnet_can_receive
eepro100: Dropnic_can_receive
usbnet: Dropusbnet_can_receive
etsec: Moveetsec_can_receiveintoetsec_receive
etsec: Flush queue when rx buffer is consumed
mcf_fec: Dropmcf_fec_can_receive
milkymist-minimac2: Flush queued packets when link comes up
mipsnet: Flush queued packets when receiving is enabled
stellaris_enet: Flush queued packets when read done
dp8393x: Flush packets when link comes up
axienet: Flush queued packets when rx is donehw/net/dp8393x.c | 8 ++++++++ hw/net/eepro100.c | 11 ----------- hw/net/fsl_etsec/etsec.c | 20 ++++++++++---------- hw/net/fsl_etsec/etsec.h | 4 +++- hw/net/fsl_etsec/rings.c | 17 +++++++++++------ hw/net/lance.c | 1 - hw/net/mcf_fec.c | 9 +-------- hw/net/milkymist-minimac2.c | 33 ++++++++++++++++----------------- hw/net/mipsnet.c | 9 +++++++-- hw/net/pcnet-pci.c | 1 -
Quoted 8 lines
hw/net/pcnet.c | 9 --------- hw/net/pcnet.h | 1 - hw/net/stellaris_enet.c | 14 +++++--------- hw/net/xgmac.c | 8 ++++---- hw/net/xilinx_axienet.c | 17 +++++++++++++---- hw/usb/dev-network.c | 20 ++++---------------- trace-events | 1 - 17 files changed, 82 insertions(+), 101 deletions(-)
I can't merge this until net/hub.c is fixed. Anything that relies on qemu_can_send_packet() will break if we drop .can_receive() functions.
At this point in the 2.4 release cycle I'm tempted to revert the relevant qemu_set_fd_handler2() refactoring patches and stage this for 2.5 instead. That way we know networking works as before and we can tackle all these fixups in 2.5.