Skip to content

Commit 33be96e

Browse files
haiyangzdavem330
authored andcommitted
net/hyperv: Add flow control based on hi/low watermark
In the existing code, we only stop queue when the ringbuffer is full, so the current packet has to be dropped or retried from upper layer. This patch stops the tx queue when available ringbuffer is below the low watermark. So the ringbuffer still has small amount of space available for the current packet. This will reduce the overhead of retries on sending. Signed-off-by: Haiyang Zhang <haiyangz@microsoft.com> Reviewed-by: K. Y. Srinivasan <kys@microsoft.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent ede7193 commit 33be96e

4 files changed

Lines changed: 69 additions & 36 deletions

File tree

‎drivers/hv/ring_buffer.c‎

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,6 @@
3030
#include "hyperv_vmbus.h"
3131

3232

33-
/* #defines */
34-
35-
36-
/* Amount of space to write to */
37-
#define BYTES_AVAIL_TO_WRITE(r, w, z) \
38-
((w) >= (r)) ? ((z) - ((w) - (r))) : ((r) - (w))
39-
40-
41-
/*
42-
*
43-
* hv_get_ringbuffer_availbytes()
44-
*
45-
* Get number of bytes available to read and to write to
46-
* for the specified ring buffer
47-
*/
48-
static inline void
49-
hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
50-
u32 *read, u32 *write)
51-
{
52-
u32 read_loc, write_loc;
53-
54-
smp_read_barrier_depends();
55-
56-
/* Capture the read/write indices before they changed */
57-
read_loc = rbi->ring_buffer->read_index;
58-
write_loc = rbi->ring_buffer->write_index;
59-
60-
*write = BYTES_AVAIL_TO_WRITE(read_loc, write_loc, rbi->ring_datasize);
61-
*read = rbi->ring_datasize - *write;
62-
}
63-
6433
/*
6534
* hv_get_next_write_location()
6635
*

‎drivers/net/hyperv/netvsc.c‎

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,24 @@ int netvsc_device_remove(struct hv_device *device)
428428
return 0;
429429
}
430430

431+
432+
#define RING_AVAIL_PERCENT_HIWATER 20
433+
#define RING_AVAIL_PERCENT_LOWATER 10
434+
435+
/*
436+
* Get the percentage of available bytes to write in the ring.
437+
* The return value is in range from 0 to 100.
438+
*/
439+
static inline u32 hv_ringbuf_avail_percent(
440+
struct hv_ring_buffer_info *ring_info)
441+
{
442+
u32 avail_read, avail_write;
443+
444+
hv_get_ringbuffer_availbytes(ring_info, &avail_read, &avail_write);
445+
446+
return avail_write * 100 / ring_info->ring_datasize;
447+
}
448+
431449
static void netvsc_send_completion(struct hv_device *device,
432450
struct vmpacket_descriptor *packet)
433451
{
@@ -455,6 +473,8 @@ static void netvsc_send_completion(struct hv_device *device,
455473
complete(&net_device->channel_init_wait);
456474
} else if (nvsp_packet->hdr.msg_type ==
457475
NVSP_MSG1_TYPE_SEND_RNDIS_PKT_COMPLETE) {
476+
int num_outstanding_sends;
477+
458478
/* Get the send context */
459479
nvsc_packet = (struct hv_netvsc_packet *)(unsigned long)
460480
packet->trans_id;
@@ -463,10 +483,14 @@ static void netvsc_send_completion(struct hv_device *device,
463483
nvsc_packet->completion.send.send_completion(
464484
nvsc_packet->completion.send.send_completion_ctx);
465485

466-
atomic_dec(&net_device->num_outstanding_sends);
486+
num_outstanding_sends =
487+
atomic_dec_return(&net_device->num_outstanding_sends);
467488

468-
if (netif_queue_stopped(ndev) && !net_device->start_remove)
469-
netif_wake_queue(ndev);
489+
if (netif_queue_stopped(ndev) && !net_device->start_remove &&
490+
(hv_ringbuf_avail_percent(&device->channel->outbound)
491+
> RING_AVAIL_PERCENT_HIWATER ||
492+
num_outstanding_sends < 1))
493+
netif_wake_queue(ndev);
470494
} else {
471495
netdev_err(ndev, "Unknown send completion packet type- "
472496
"%d received!!\n", nvsp_packet->hdr.msg_type);
@@ -519,10 +543,19 @@ int netvsc_send(struct hv_device *device,
519543

520544
if (ret == 0) {
521545
atomic_inc(&net_device->num_outstanding_sends);
546+
if (hv_ringbuf_avail_percent(&device->channel->outbound) <
547+
RING_AVAIL_PERCENT_LOWATER) {
548+
netif_stop_queue(ndev);
549+
if (atomic_read(&net_device->
550+
num_outstanding_sends) < 1)
551+
netif_wake_queue(ndev);
552+
}
522553
} else if (ret == -EAGAIN) {
523554
netif_stop_queue(ndev);
524-
if (atomic_read(&net_device->num_outstanding_sends) < 1)
555+
if (atomic_read(&net_device->num_outstanding_sends) < 1) {
525556
netif_wake_queue(ndev);
557+
ret = -ENOSPC;
558+
}
526559
} else {
527560
netdev_err(ndev, "Unable to send packet %p ret %d\n",
528561
packet, ret);

‎drivers/net/hyperv/netvsc_drv.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,13 @@ static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
224224
net->stats.tx_packets++;
225225
} else {
226226
kfree(packet);
227+
if (ret != -EAGAIN) {
228+
dev_kfree_skb_any(skb);
229+
net->stats.tx_dropped++;
230+
}
227231
}
228232

229-
return ret ? NETDEV_TX_BUSY : NETDEV_TX_OK;
233+
return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
230234
}
231235

232236
/*

‎include/linux/hyperv.h‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,33 @@ struct hv_ring_buffer_debug_info {
274274
u32 bytes_avail_towrite;
275275
};
276276

277+
278+
/*
279+
*
280+
* hv_get_ringbuffer_availbytes()
281+
*
282+
* Get number of bytes available to read and to write to
283+
* for the specified ring buffer
284+
*/
285+
static inline void
286+
hv_get_ringbuffer_availbytes(struct hv_ring_buffer_info *rbi,
287+
u32 *read, u32 *write)
288+
{
289+
u32 read_loc, write_loc, dsize;
290+
291+
smp_read_barrier_depends();
292+
293+
/* Capture the read/write indices before they changed */
294+
read_loc = rbi->ring_buffer->read_index;
295+
write_loc = rbi->ring_buffer->write_index;
296+
dsize = rbi->ring_datasize;
297+
298+
*write = write_loc >= read_loc ? dsize - (write_loc - read_loc) :
299+
read_loc - write_loc;
300+
*read = dsize - *write;
301+
}
302+
303+
277304
/*
278305
* We use the same version numbering for all Hyper-V modules.
279306
*

0 commit comments

Comments
 (0)