Implement multiqueue (RX & TX) virtio-net
Subsystems (6)
cover Implement multiqueue (RX & TX) virtio-net
This patchset implements both RX and TX MQ. Patch against virtio-net, vhost and qemu are included.
Changes from rev1:
-------------------
1. vqs are allocated as: rx/tx, rx/tx, rx/tx, etc. Lot of code in guest/host/qemu changes, but code becomes simpler.
2. vhost cache align of vhost_dev correctly.
3. virtio-net: cleanup properly on errors (eg detach buf for vq>0 as pointed out by Micheal).
4. Minor changes:
- Fixed some typos.
- Changed vhost_get_thread_index to use MAX_VHOST_THREADS.
- Removed VIRTIO_MAX_TXQS.
- Changed capability to VIRTIO_NET_F_MULTIQUEUE.
- Modified "numtxqs" in virtnet_info to "num_queue_pairs". virtnet_info still has numtxqs as it is more convenient.
- Moved code for VIRTIO_NET_F_CTRL_VLAN into probe function.
- Improve check for return value of virtio_config_val().
- Removed cache align directives in guest as it was redundant.
5. "If we have a wrapper to init all vqs, pls add a wrapper to clean up all vqs as well": I haven't done this as some errors are very specific to the failure location (and what was initialized till then). So only those errors are cleaned up using goto's like the rest of the code. I can change in next version if you feel this is still required.
6. "I think we should have free_unused_bufs that handles a single queue, and call it in a loop": I haven't done this as I think the caller wants all rx/tx queues to be cleaned up by calling this function.
TODO's:
--------
1. Reduce vectors for find_vqs().
2. Make vhost changes minimal. For now, I have restricted the number of vhost threads to 4. This can be either made unrestricted; or if the userspace vhost works, it can be removed altogether.
Please review and provide feedback. I am travelling a bit in the next few days but will respond at the earliest.
---
RFC PATCH 1/4 Change virtqueue structure
Move queue_index from virtio_pci_vq_info to virtqueue. This allows callback handlers to figure out the queue number for the vq that needs attention.
--- drivers/virtio/virtio_pci.c | 10 +++------- include/linux/virtio.h | 1 + 2 files changed, 4 insertions(+), 7 deletions(-)
include/linux/virtio.h +1 -0
@@ -22,6 +22,7 @@ struct virtqueue {void (*callback)(struct virtqueue *vq);const char *name;struct virtio_device *vdev; +int queue_index;/* the index of the queue */ void *priv; };
drivers/virtio/virtio_pci.c +3 -7
@@ -75,9 +75,6 @@ struct virtio_pci_vq_info/* the number of entries in the queue */ int num; -/* the index of the queue */ -int queue_index; -/* the virtual address of the ring queue */ void *queue;
@@ -180,11 +177,10 @@ static void vp_reset(struct virtio_devicstatic void vp_notify (struct virtqueue *vq) {struct virtio_pci_device *vp_dev =to_vp_device (vq->vdev); -struct virtio_pci_vq_info *info = vq->priv;/* we write the queue's selector into the notification register to * signal the other end */ -iowrite16 (info->queue_index, vp_dev->ioaddr +VIRTIO_PCI_QUEUE_NOTIFY ); +iowrite16 (vq->queue_index, vp_dev->ioaddr +VIRTIO_PCI_QUEUE_NOTIFY ); }/* Handle a configuration change: Tell driver if it wants to know. */
@@ -380,7 +376,6 @@ static struct virtqueue *setup_vq(structif (!info)return ERR_PTR (-ENOMEM ); - info->queue_index = index; info->num = num; info->msix_vector = msix_vec;
@@ -403,6 +398,7 @@ static struct virtqueue *setup_vq(struct goto out_activate_queue; } + vq->queue_index = index; vq->priv = info; info->vq = vq;
@@ -441,7 +437,7 @@ static void vp_del_vq(struct virtqueue *list_del (&info->node);spin_unlock_irqrestore (&vp_dev->lock, flags); -iowrite16 (info->queue_index, vp_dev->ioaddr +VIRTIO_PCI_QUEUE_SEL ); +iowrite16 (vq->queue_index, vp_dev->ioaddr +VIRTIO_PCI_QUEUE_SEL );if (vp_dev->msix_enabled) { iowrite16(VIRTIO_MSI_NO_VECTOR,
RFC PATCH 2/4 virtio-net changes
In reply to Krishna KumarImplement mq virtio-net driver.
Though struct virtio_net_config changes, it works with the old qemu since the last element is not accessed unless qemu sets VIRTIO_NET_F_MULTIQUEUE.
--- drivers/net/virtio_net.c | 573 ++++++++++++++++++++++++----------- include/linux/virtio_net.h | 3 2 files changed, 408 insertions(+), 168 deletions(-)
include/linux/virtio_net.h +3 -0
@@ -26,6 +26,7 @@#define VIRTIO_NET_F_CTRL_RX 18/* Control channel RX mode support */ #define VIRTIO_NET_F_CTRL_VLAN 19/* Control channel VLAN filtering */ #define VIRTIO_NET_F_CTRL_RX_EXTRA 20/* Extra RX mode control support */ +#define VIRTIO_NET_F_MULTIQUEUE 21/* Device supports multiple TXQ/RXQ */ #define VIRTIO_NET_S_LINK_UP 1/* Link is up */
@@ -34,6 +35,8 @@ struct virtio_net_config {__u8 mac[6 ];/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */ __u16 status; +/* total number of RX/TX queues */ +__u16 num_queue_pairs; } __attribute__((packed));/* This is the first element of the scatter-gather list. If you don't
drivers/net/virtio_net.c +405 -168
959 lines not on this page · raw message
Re: [PATCH 2/4] [RFC rev2] virtio-net changes
Quoted 7 lines · Krishna Kumar
Implement mq virtio-net driver.
Though struct
virtio_net_configchanges, it works with the old qemu since the last element is not accessed unless qemu setsVIRTIO_NET_F_MULTIQUEUE.Signed-off-by Krishna Kumar <[email protected]>
Hi Krishna!
This change looks fairly solid, but I'd prefer it split into a few
stages for clarity.
The first patch should extract out the struct send_queue and struct receive_queue, even though there's still only one. The second patch can then introduce VIRTIO_NET_F_MULTIQUEUE.
You could split into more parts if that makes sense, but I'd prefer to see the mechanical changes separate from the feature addition.
drivers/net/virtio_net.c module_param(gso, bool, 0444); · line 46
Quoted 8 lines
drivers/net/virtio_net.c +4
-4-struct virtnet_info { -struct virtio_device *vdev; -struct virtqueue *rvq, *svq, *cvq; -struct net_device *dev; +/* Internal representation of a send virtqueue */ +struct send_queue { +/* Virtqueue associated with this send _queue */ +struct virtqueue *svq;
You can simply call this vq now it's inside 'send_queue'.
drivers/net/virtio_net.c module_param(gso, bool, 0444); · line 49
Quoted 3 lines
drivers/net/virtio_net.c +3
-0+ +/* TX: fragments + linear part + virtio header */ +struct scatterlist tx_sg[MAX_SKB_FRAGS +2 ];
Similarly, this can just be sg.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 937
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+static void free_receive_bufs (struct virtnet_info *vi) +{ +int i; + +for (i =0 ; i < vi->numtxqs; i++) { +BUG_ON (vi->rq[i] ==NULL ); +while (vi->rq[i]->pages) +__free_pages (get_a_page (vi->rq[i],GFP_KERNEL ),0 ); + } +}
You can skip the BUG_ON(), since the next line will have the same effect.
Quoted 6 lines
drivers/net/virtio_net.c +6
-0+/* Free memory allocated for send and receive queues */ +static void free_rq_sq (struct virtnet_info *vi) +{ +int i; + +if (vi->rq) {
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 954
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+for (i =0 ; i < vi->numtxqs; i++) +kfree (vi->rq[i]); +kfree (vi->rq); + } + +if (vi->sq) { +for (i =0 ; i < vi->numtxqs; i++) +kfree (vi->sq[i]); +kfree (vi->sq); + }
This looks weird, even though it's correct.
I think we need a better name than numtxqs and shorter than num_queue_pairs. Let's just use num_queues; sure, there are both tx and rq queues, but I still think it's pretty clear.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 971
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+for (i =0 ; i < vi->numtxqs; i++) { +struct virtqueue *svq = vi->sq[i]->svq; + +while (1 ) { + buf =virtqueue_detach_unused_buf (svq); +if (!buf) +break ; +dev_kfree_skb (buf); + } + }
I know this isn't your code, but it's ugly :)
while ((buf = virtqueue_detach_unused_buf(svq)) != NULL)
dev_kfree_skb(buf);drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 979
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+for (i =0 ; i < vi->numtxqs; i++) { +struct virtqueue *rvq = vi->rq[i]->rvq; + +while (1 ) { + buf =virtqueue_detach_unused_buf (rvq); +if (!buf) +break ;
Here too...
Quoted 1 line
+#define
MAX_DEVICE_NAME16
This isn't a good idea, see below.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 997
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+static int initialize_vqs (struct virtnet_info *vi,int numtxqs) +{ +vq_callback_t **callbacks; +struct virtqueue **vqs; +int i, err = -ENOMEM ; +int totalvqs; +char **names;
This whole routine is really messy. How about doing find_vqs first, then have routines like setup_rxq(), setup_txq() and setup_controlq() would make this neater:
static int setup_rxq(struct send_queue *sq, char *name);
Also, use kasprintf() instead of kmalloc & sprintf.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 1046
Quoted 2 lines
drivers/net/virtio_net.c +2
-0+#if 1 +/* Allocate/initialize parameters for recv/send virtqueues */
Why is this #if 1'd?
I do prefer the #else method of doing two loops, myself (but use kasprintf).
Cheers,
Rusty.
Re: [PATCH 2/4] [RFC rev2] virtio-net changes
Hi Rusty,
Thanks for your feedback. I agree with all the changes, and will make it and resubmit next.
thanks,
- KK
Rusty Russell <[email protected]> wrote on 04/13/2011 06:58:02 AM:
Quoted 18 lines
Rusty Russell <[email protected]>
04/13/2011 06:58 AMTo
Krishna Kumar2/India/IBM@IBMIN, [email protected], [email protected]
cc
[email protected], [email protected], [email protected], [email protected], [email protected], [email protected], [email protected], Krishna Kumar2/India/IBM@IBMIN
Subject
Re: [PATCH 2/4] [RFC rev2] virtio-net changes
On Tue, 05 Apr 2011 20:38:52 +0530, Krishna Kumar <[email protected]>
Quoted 129 lines · wrote:
Quoted 7 lines
Implement mq virtio-net driver.
Though struct
virtio_net_configchanges, it works with the old qemu since the last element is not accessed unless qemu setsVIRTIO_NET_F_MULTIQUEUE.Signed-off-by Krishna Kumar <[email protected]>Hi Krishna!
This change looks fairly solid, but I'd prefer it split into a few
stages for clarity.The first patch should extract out the struct
send_queueand structreceive_queue, even though there's still only one. The second patch can then introduceVIRTIO_NET_F_MULTIQUEUE.You could split into more parts if that makes sense, but I'd prefer to see the mechanical changes separate from the feature addition.
drivers/net/virtio_net.c module_param(gso, bool, 0444); · line 46
Quoted 8 lines
drivers/net/virtio_net.c +4
-4-struct virtnet_info { -struct virtio_device *vdev; -struct virtqueue *rvq, *svq, *cvq; -struct net_device *dev; +/* Internal representation of a send virtqueue */ +struct send_queue { +/* Virtqueue associated with this send _queue */ +struct virtqueue *svq;You can simply call this vq now it's inside '
send_queue'.drivers/net/virtio_net.c module_param(gso, bool, 0444); · line 49
Quoted 3 lines
drivers/net/virtio_net.c +3
-0+ +/* TX: fragments + linear part + virtio header */ +struct scatterlist tx_sg[MAX_SKB_FRAGS +2 ];Similarly, this can just be sg.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 937
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+static void free_receive_bufs (struct virtnet_info *vi) +{ +int i; + +for (i =0 ; i < vi->numtxqs; i++) { +BUG_ON (vi->rq[i] ==NULL ); +while (vi->rq[i]->pages) +__free_pages (get_a_page (vi->rq[i],GFP_KERNEL ),0 ); + } +}You can skip the
BUG_ON(), since the next line will have the same effect.Quoted 6 lines
drivers/net/virtio_net.c +6
-0+/* Free memory allocated for send and receive queues */ +static void free_rq_sq (struct virtnet_info *vi) +{ +int i; + +if (vi->rq) {drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 954
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+for (i =0 ; i < vi->numtxqs; i++) +kfree (vi->rq[i]); +kfree (vi->rq); + } + +if (vi->sq) { +for (i =0 ; i < vi->numtxqs; i++) +kfree (vi->sq[i]); +kfree (vi->sq); + }This looks weird, even though it's correct.
I think we need a better name than numtxqs and shorter than
num_queue_pairs. Let's just usenum_queues; sure, there are both tx and rq queues, but I still think it's pretty clear.drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 971
Quoted 10 lines
drivers/net/virtio_net.c +10
-0+for (i =0 ; i < vi->numtxqs; i++) { +struct virtqueue *svq = vi->sq[i]->svq; + +while (1 ) { + buf =virtqueue_detach_unused_buf (svq); +if (!buf) +break ; +dev_kfree_skb (buf); + } + }I know this isn't your code, but it's ugly :)
while ((buf = virtqueue_detach_unused_buf(svq)) != NULL) dev_kfree_skb(buf);drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 979
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+for (i =0 ; i < vi->numtxqs; i++) { +struct virtqueue *rvq = vi->rq[i]->rvq; + +while (1 ) { + buf =virtqueue_detach_unused_buf (rvq); +if (!buf) +break ;Here too...
Quoted 1 line
+#define
MAX_DEVICE_NAME16This isn't a good idea, see below.
drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 997
Quoted 7 lines
drivers/net/virtio_net.c +7
-0+static int initialize_vqs (struct virtnet_info *vi,int numtxqs) +{ +vq_callback_t **callbacks; +struct virtqueue **vqs; +int i, err = -ENOMEM ; +int totalvqs; +char **names;This whole routine is really messy. How about doing
find_vqsfirst, then have routines likesetup_rxq(),setup_txq()andsetup_controlq()would make this neater:static int setup_rxq(struct send_queue *sq, char *name);Also, use
kasprintf()instead of kmalloc & sprintf.drivers/net/virtio_net.c static void virtnet_config_changed(struc · line 1046
Quoted 2 lines
drivers/net/virtio_net.c +2
-0+#if 1 +/* Allocate/initialize parameters for recv/send virtqueues */ Why is this #if 1'd?
I do prefer the #else method of doing two loops, myself (but use kasprintf).
Cheers,
Rusty.
RFC PATCH 3/4 vhost changes
In reply to Krishna Kumarvhost_net_open is changed to allocate a vhost_net and return. The remaining initializations are delayed till SET_OWNER. SET_OWNER is changed so that the argument is used to determine how many txqs to use. Unmodified qemu's will pass NULL, so this is recognized and handled as numtxqs=1.
The number of vhost threads is <= #txqs. Threads handle more than one txq when #txqs is more than MAX_VHOST_THREADS (4). The same thread handles both RX and TX - tested with tap/bridge so far (TBD: some changes are needed in macvtap to support the same).
--- drivers/vhost/net.c | 249 +++++++++++++++++++++++++--------------- drivers/vhost/vhost.c | 221 ++++++++++++++++++++++++----------- drivers/vhost/vhost.h | 22 ++- 3 files changed, 332 insertions(+), 160 deletions(-)
drivers/vhost/net.c +161 -88
@@ -32,12 +32,6 @@* Using this limit prevents one virtqueue from starving others. */ #define VHOST_NET_WEIGHT 0x80000 -enum { -VHOST_NET_VQ_RX =0 , -VHOST_NET_VQ_TX =1 , -VHOST_NET_VQ_MAX =2 , -}; -enum vhost_net_poll_state {VHOST_NET_POLL_DISABLED =0 ,VHOST_NET_POLL_STARTED =1 ,
@@ -46,12 +40,13 @@ enum vhost_net_poll_state {struct vhost_net {struct vhost_dev dev; -struct vhost_virtqueue vqs[VHOST_NET_VQ_MAX ]; -struct vhost_poll poll[VHOST_NET_VQ_MAX ]; +struct vhost_virtqueue *vqs; +struct vhost_poll *poll; +struct socket **socks;/* Tells us whether we are polling a socket for TX. * We only do this when socket buffer fills up. * Protected by tx vq lock. */ -enum vhost_net_poll_state tx_poll_state; +enum vhost_net_poll_state *tx_poll_state; };/* Pop first len bytes from iovec. Return number of segments used. */
@@ -93,28 +88,28 @@ static void copy_iovec_hdr(const struct }/* Caller must have TX VQ lock */ -static void tx_poll_stop (struct vhost_net *net) +static void tx_poll_stop (struct vhost_net *net,int qnum) { -if (likely (net->tx_poll_state !=VHOST_NET_POLL_STARTED )) +if (likely (net->tx_poll_state[qnum /2 ] !=VHOST_NET_POLL_STARTED ))return ; -vhost_poll_stop (net->poll +VHOST_NET_VQ_TX ); - net->tx_poll_state =VHOST_NET_POLL_STOPPED ; +vhost_poll_stop (&net->poll[qnum]); + net->tx_poll_state[qnum /2 ] =VHOST_NET_POLL_STOPPED ; }/* Caller must have TX VQ lock */ -static void tx_poll_start (struct vhost_net *net,struct socket *sock) +static void tx_poll_start (struct vhost_net *net,struct socket *sock,int qnum) { -if (unlikely (net->tx_poll_state !=VHOST_NET_POLL_STOPPED )) +if (unlikely (net->tx_poll_state[qnum /2 ] !=VHOST_NET_POLL_STOPPED ))return ; -vhost_poll_start (net->poll +VHOST_NET_VQ_TX , sock->file); - net->tx_poll_state =VHOST_NET_POLL_STARTED ; +vhost_poll_start (&net->poll[qnum], sock->file); + net->tx_poll_state[qnum /2 ] =VHOST_NET_POLL_STARTED ; }/* Expects to be always run from workqueue - which acts as * read-size critical section for our kind of RCU. */ -static void handle_tx (struct vhost_net *net) +static void handle_tx (struct vhost_virtqueue *vq) { -struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX ]; + struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);unsigned out, in, s;int head;struct msghdr msg = {
@@ -138,7 +133,7 @@ static void handle_tx(struct vhost_net * wmem =atomic_read (&sock->sk->sk_wmem_alloc);if (wmem >= sock->sk->sk_sndbuf) {mutex_lock (&vq->mutex); -tx_poll_start (net, sock); +tx_poll_start (net, sock, vq->qnum);mutex_unlock (&vq->mutex);return ; }
@@ -147,7 +142,7 @@ static void handle_tx(struct vhost_net *vhost_disable_notify (vq);if (wmem < sock->sk->sk_sndbuf /2 ) -tx_poll_stop (net); +tx_poll_stop (net, vq->qnum); hdr_size = vq->vhost_hlen;for (;;) {
@@ -162,7 +157,7 @@ static void handle_tx(struct vhost_net *if (head == vq->num) { wmem =atomic_read (&sock->sk->sk_wmem_alloc);if (wmem >= sock->sk->sk_sndbuf *3 /4 ) { -tx_poll_start (net, sock); +tx_poll_start (net, sock, vq->qnum);set_bit (SOCK_ASYNC_NOSPACE , &sock->flags);break ; }
@@ -192,7 +187,7 @@ static void handle_tx(struct vhost_net * err = sock->ops->sendmsg (NULL , sock, &msg, len);if (unlikely (err <0 )) {vhost_discard_vq_desc (vq,1 ); -tx_poll_start (net, sock); +tx_poll_start (net, sock, vq->qnum);break ; }if (err != len)
@@ -287,9 +282,9 @@ err:/* Expects to be always run from workqueue - which acts as * read-size critical section for our kind of RCU. */ -static void handle_rx (struct vhost_net *net) +static void handle_rx (struct vhost_virtqueue *vq) { -struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX ]; + struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);unsigned uninitialized_var (in ), log;struct vhost_log *vq_log;struct msghdr msg = {
@@ -398,87 +393,155 @@ static void handle_tx_kick(struct vhost_ { struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, poll.work); - struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); -handle_tx (net); +handle_tx (vq); }static void handle_rx_kick (struct vhost_work *work) { struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue, poll.work); - struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev); -handle_rx (net); +handle_rx (vq); }static void handle_tx_net (struct vhost_work *work) { - struct vhost_net *net = container_of(work, struct vhost_net, - poll[VHOST_NET_VQ_TX ].work); -handle_tx (net); + struct vhost_virtqueue *vq = container_of(work, struct vhost_poll, + work)->vq; + +handle_tx (vq); }static void handle_rx_net (struct vhost_work *work) { - struct vhost_net *net = container_of(work, struct vhost_net, - poll[VHOST_NET_VQ_RX ].work); -handle_rx (net); + struct vhost_virtqueue *vq = container_of(work, struct vhost_poll, + work)->vq; + +handle_rx (vq); } -static int vhost_net_open (struct inode *inode,struct file *f) +void vhost_free_vqs (struct vhost_dev *dev) { -struct vhost_net *n =kmalloc (sizeof *n,GFP_KERNEL ); -struct vhost_dev *dev; -int r; + struct vhost_net *n = container_of(dev, struct vhost_net, dev); -if (!n) -return -ENOMEM ; +if (!n->vqs) +return ; - dev = &n->dev; - n->vqs[VHOST_NET_VQ_TX ].handle_kick = handle_tx_kick; - n->vqs[VHOST_NET_VQ_RX ].handle_kick = handle_rx_kick; - r =vhost_dev_init (dev, n->vqs,VHOST_NET_VQ_MAX ); -if (r <0 ) { -kfree (n); -return r; +kfree (n->socks); +kfree (n->tx_poll_state); +kfree (n->poll); +kfree (n->vqs); + +/* +* Reset so that vhost_net_release (which gets called when +* vhost_dev_set_owner() call fails) will notice. +*/ + n->vqs =NULL ; +} + +int vhost_setup_vqs (struct vhost_dev *dev,int numtxqs) +{ + struct vhost_net *n = container_of(dev, struct vhost_net, dev); +int i, nvqs; +int ret = -ENOMEM ; + +if (numtxqs <0 ) +return -EINVAL ; + +if (numtxqs ==0 ) { +/* Old qemu doesn't pass arguments to set_owner, use 1 txq */ + numtxqs =1 ; + } + +/* Get total number of virtqueues */ + nvqs = numtxqs *2 ; + + n->vqs =kmalloc (nvqs *sizeof (*n->vqs),GFP_KERNEL ); + n->poll =kmalloc (nvqs *sizeof (*n->poll),GFP_KERNEL ); + n->socks =kmalloc (nvqs *sizeof (*n->socks),GFP_KERNEL ); + n->tx_poll_state =kmalloc (numtxqs *sizeof (*n->tx_poll_state), +GFP_KERNEL ); +if (!n->vqs || !n->poll || !n->socks || !n->tx_poll_state) + goto err; + +/* RX followed by TX queues */ +for (i =0 ; i < nvqs; i +=2 ) { + n->vqs[i].handle_kick = handle_rx_kick; + n->vqs[i +1 ].handle_kick = handle_tx_kick; } -vhost_poll_init (n->poll +VHOST_NET_VQ_TX , handle_tx_net,POLLOUT , dev); -vhost_poll_init (n->poll +VHOST_NET_VQ_RX , handle_rx_net,POLLIN , dev); - n->tx_poll_state =VHOST_NET_POLL_DISABLED ; + ret =vhost_dev_init (dev, n->vqs, nvqs); +if (ret <0 ) + goto err; - f->private_data = n; +for (i =0 ; i < nvqs; i +=2 ) { +vhost_poll_init (&n->poll[i], handle_rx_net,POLLIN , + &n->vqs[i]); +vhost_poll_init (&n->poll[i+1 ], handle_tx_net,POLLOUT , + &n->vqs[i+1 ]); +if (i /2 < numtxqs) + n->tx_poll_state[i/2 ] =VHOST_NET_POLL_DISABLED ; + }return 0 ; + +err: +/* Free all pointers that may have been allocated */ +vhost_free_vqs (dev); + +return ret; +} + +static int vhost_net_open (struct inode *inode,struct file *f) +{ +struct vhost_net *n =kzalloc (sizeof *n,GFP_KERNEL ); +int ret = -ENOMEM ; + +if (n) { +struct vhost_dev *dev = &n->dev; + + f->private_data = n; +mutex_init (&dev->mutex); + +/* Defer all other initialization till user does SET_OWNER */ + ret =0 ; + } + +return ret; }static void vhost_net_disable_vq (struct vhost_net *n,struct vhost_virtqueue *vq) { +int qnum = vq->qnum; +if (!vq->private_data)return ; -if (vq == n->vqs +VHOST_NET_VQ_TX ) { -tx_poll_stop (n); - n->tx_poll_state =VHOST_NET_POLL_DISABLED ; - }else -vhost_poll_stop (n->poll +VHOST_NET_VQ_RX ); +if (qnum &1 ) {/* Odd qnum -> TX */ +tx_poll_stop (n, qnum); + n->tx_poll_state[qnum /2 ] =VHOST_NET_POLL_DISABLED ; + }else {/* Even qnum -> RX */ +vhost_poll_stop (&n->poll[qnum]); + } }static void vhost_net_enable_vq (struct vhost_net *n,struct vhost_virtqueue *vq) {struct socket *sock; +int qnum = vq->qnum; sock =rcu_dereference_protected (vq->private_data,lockdep_is_held (&vq->mutex));if (!sock)return ; -if (vq == n->vqs +VHOST_NET_VQ_TX ) { - n->tx_poll_state =VHOST_NET_POLL_STOPPED ; -tx_poll_start (n, sock); - }else -vhost_poll_start (n->poll +VHOST_NET_VQ_RX , sock->file); +if (qnum &1 ) {/* Odd qnum -> TX */ + n->tx_poll_state[qnum /2 ] =VHOST_NET_POLL_STOPPED ; +tx_poll_start (n, sock, qnum); + }else {/* Even qnum -> RX */ +vhost_poll_start (&n->poll[qnum], sock->file); + } } static struct socket *vhost_net_stop_vq(struct vhost_net *n,
@@ -495,11 +558,12 @@ static struct socket *vhost_net_stop_vq(return sock; } -static void vhost_net_stop (struct vhost_net *n,struct socket **tx_sock, -struct socket **rx_sock) +static void vhost_net_stop (struct vhost_net *n) { - *tx_sock =vhost_net_stop_vq (n, n->vqs +VHOST_NET_VQ_TX ); - *rx_sock =vhost_net_stop_vq (n, n->vqs +VHOST_NET_VQ_RX ); +int i; + +for (i =0 ; i < n->dev.nvqs; i++) + n->socks[i] =vhost_net_stop_vq (n, &n->vqs[i]); }static void vhost_net_flush_vq (struct vhost_net *n,int index)
@@ -510,26 +574,33 @@ static void vhost_net_flush_vq(struct vhstatic void vhost_net_flush (struct vhost_net *n) { -vhost_net_flush_vq (n,VHOST_NET_VQ_TX ); -vhost_net_flush_vq (n,VHOST_NET_VQ_RX ); +int i; + +for (i =0 ; i < n->dev.nvqs; i++) +vhost_net_flush_vq (n, i); }static int vhost_net_release (struct inode *inode,struct file *f) {struct vhost_net *n = f->private_data; -struct socket *tx_sock; -struct socket *rx_sock; +struct vhost_dev *dev = &n->dev; +int i; -vhost_net_stop (n, &tx_sock, &rx_sock); +vhost_net_stop (n);vhost_net_flush (n); -vhost_dev_cleanup (&n->dev); -if (tx_sock) -fput (tx_sock->file); -if (rx_sock) -fput (rx_sock->file); +vhost_dev_cleanup (dev); + +for (i =0 ; i < n->dev.nvqs; i++) +if (n->socks[i]) +fput (n->socks[i]->file); +/* We do an extra flush before freeing memory, * since jobs can re-queue themselves. */ vhost_net_flush (n); + +/* Free all old pointers */ +vhost_free_vqs (dev); +kfree (n);return 0 ; }
@@ -610,7 +681,7 @@ static long vhost_net_set_backend(structif (r) goto err; -if (index >=VHOST_NET_VQ_MAX ) { +if (index >= n->dev.nvqs) { r = -ENOBUFS ; goto err; }
@@ -656,23 +727,25 @@ err:static long vhost_net_reset_owner (struct vhost_net *n) { -struct socket *tx_sock =NULL ; -struct socket *rx_sock =NULL ;long err; +int i;mutex_lock (&n->dev.mutex); err =vhost_dev_check_owner (&n->dev); -if (err) - goto done; -vhost_net_stop (n, &tx_sock, &rx_sock); +if (err) { +mutex_unlock (&n->dev.mutex); +return err; + } + +vhost_net_stop (n);vhost_net_flush (n); err =vhost_dev_reset_owner (&n->dev); -done:mutex_unlock (&n->dev.mutex); -if (tx_sock) -fput (tx_sock->file); -if (rx_sock) -fput (rx_sock->file); + +for (i =0 ; i < n->dev.nvqs; i++) +if (n->socks[i]) +fput (n->socks[i]->file); +return err; }
@@ -701,7 +774,7 @@ static int vhost_net_set_features(struct } n->dev.acked_features = features;smp_wmb (); -for (i =0 ; i <VHOST_NET_VQ_MAX ; ++i) { +for (i =0 ; i < n->dev.nvqs; ++i) {mutex_lock (&n->vqs[i].mutex); n->vqs[i].vhost_hlen = vhost_hlen; n->vqs[i].sock_hlen = sock_hlen;
drivers/vhost/vhost.c +154 -67
365 lines not on this page · raw message
drivers/vhost/vhost.h +17 -5
@@ -35,11 +35,11 @@ struct vhost_poll {wait_queue_t wait;struct vhost_work work;unsigned long mask; -struct vhost_dev *dev; +struct vhost_virtqueue *vq;/* points back to vq */ };void vhost_poll_init (struct vhost_poll *poll,vhost_work_fn_t fn, -unsigned long mask,struct vhost_dev *dev); +unsigned long mask,struct vhost_virtqueue *vq);void vhost_poll_start (struct vhost_poll *poll,struct file *file);void vhost_poll_stop (struct vhost_poll *poll);void vhost_poll_flush (struct vhost_poll *poll);
@@ -108,8 +108,20 @@ struct vhost_virtqueue {/* Log write descriptors */ void __user *log_base;struct vhost_log *log; +struct task_struct *worker;/* worker for this vq */ +spinlock_t *work_lock;/* points to a dev->work_lock[] entry */ +struct list_head *work_list;/* points to a dev->work_list[] entry */ +int qnum;/* 0 for RX, 1 for TX, and so on alternatively */ }; +/* work entry and the lock */ +struct work_lock_list { +spinlock_t work_lock; +struct list_head work_list; +}____cacheline_aligned_in_smp ; + +#define MAX_VHOST_THREADS 4 +struct vhost_dev {/* Readers use RCU to access memory table pointer * log base pointer and features.
@@ -122,11 +134,11 @@ struct vhost_dev {int nvqs;struct file *log_file;struct eventfd_ctx *log_ctx; -spinlock_t work_lock; -struct list_head work_list; -struct task_struct *worker; +struct work_lock_list work[MAX_VHOST_THREADS ]; }; +int vhost_setup_vqs (struct vhost_dev *dev,int numtxqs); +void vhost_free_vqs (struct vhost_dev *dev);long vhost_dev_init (struct vhost_dev *,struct vhost_virtqueue *vqs,int nvqs);long vhost_dev_check_owner (struct vhost_dev *);long vhost_dev_reset_owner (struct vhost_dev *);
RFC PATCH 4/4 qemu changes
In reply to Krishna Kumarhw/vhost.c +4 -2
@@ -581,7 +581,7 @@ static void vhost_virtqueue_cleanup(stru 0, virtio_queue_get_desc_size(vdev, idx)); } -int vhost_dev_init (struct vhost_dev *hdev,int devfd,bool force) +int vhost_dev_init (struct vhost_dev *hdev,int devfd,bool force,int numtxqs) {uint64_t features;int r;
@@ -593,11 +593,13 @@ int vhost_dev_init(struct vhost_dev *hdereturn -errno; } } - r =ioctl (hdev->control,VHOST_SET_OWNER ,NULL ); + r =ioctl (hdev->control,VHOST_SET_OWNER , numtxqs);if (r <0 ) { goto fail; } + hdev->nvqs = numtxqs *2 ; + r =ioctl (hdev->control,VHOST_GET_FEATURES , &features);if (r <0 ) { goto fail;
hw/vhost.h +1 -1
@@ -41,7 +41,7 @@ struct vhost_dev {bool force; }; -int vhost_dev_init (struct vhost_dev *hdev,int devfd,bool force); +int vhost_dev_init (struct vhost_dev *hdev,int devfd,bool force,int numtxqs);void vhost_dev_cleanup (struct vhost_dev *hdev);bool vhost_dev_query (struct vhost_dev *hdev,VirtIODevice *vdev);int vhost_dev_start (struct vhost_dev *hdev,VirtIODevice *vdev);
hw/vhost_net.c +24 -15
@@ -36,8 +36,9 @@struct vhost_net {struct vhost_dev dev; -struct vhost_virtqueue vqs[2 ]; -int backend; +struct vhost_virtqueue *vqs; +int nvqs; +int *backend;VLANClientState *vc; };
@@ -70,11 +71,11 @@ void vhost_net_ack_features(struct vhost } } -static int vhost_net_get_fd (VLANClientState *backend) +static int vhost_net_get_fd (VLANClientState *backend,int index) {switch (backend->info->type) {case NET_CLIENT_TYPE_TAP : -return tap_get_fd (backend); +return tap_get_fd (backend, index);default :fprintf (stderr,"vhost-net requires tap backend\n" );return -EBADFD ;
@@ -82,27 +83,36 @@ static int vhost_net_get_fd(VLANClientSt }struct vhost_net *vhost_net_init (VLANClientState *backend,int devfd, -bool force) +bool force,int numtxqs) { -int r; +int i, r;struct vhost_net *net =qemu_malloc (sizeof *net);if (!backend) {fprintf (stderr,"vhost-net requires backend to be setup\n" ); goto fail; } - r =vhost_net_get_fd (backend); -if (r <0 ) { - goto fail; + + net->backend =qemu_malloc (numtxqs * (sizeof *net->backend)); +for (i =0 ; i < numtxqs; i++) { + r =vhost_net_get_fd (backend, i); +if (r <0 ) { + goto fail; + } + net->backend[i] = r; } + net->vc = backend; net->dev.backend_features =tap_has_vnet_hdr (backend) ?0 : (1 <<VHOST_NET_F_VIRTIO_NET_HDR ); - net->backend = r; - r =vhost_dev_init (&net->dev, devfd, force); + r =vhost_dev_init (&net->dev, devfd, force, numtxqs);if (r <0 ) { goto fail; } + + net->nvqs = numtxqs *2 ; + net->vqs =qemu_malloc (net->nvqs * (sizeof *net->vqs)); +if (!tap_has_vnet_hdr_len (backend,sizeof (struct virtio_net_hdr_mrg_rxbuf ))) { net->dev.features &= ~(1 <<VIRTIO_NET_F_MRG_RXBUF );
@@ -137,7 +147,6 @@ int vhost_net_start(struct vhost_net *ne sizeof(struct virtio_net_hdr_mrg_rxbuf)); } - net->dev.nvqs =2 ; net->dev.vqs = net->vqs; r =vhost_dev_start (&net->dev, dev);if (r <0 ) {
@@ -145,9 +154,9 @@ int vhost_net_start(struct vhost_net *ne } net->vc->info->poll (net->vc, false); -qemu_set_fd_handler (net->backend,NULL ,NULL ,NULL ); - file.fd = net->backend;for (file.index =0 ; file.index < net->dev.nvqs; ++file.index) { +qemu_set_fd_handler (net->backend[file.index/2 ],NULL ,NULL ,NULL ); + file.fd = net->backend[(file.index /2 ) % (net->dev.nvqs /2 )]; r =ioctl (net->dev.control,VHOST_NET_SET_BACKEND , &file);if (r <0 ) { r = -errno;
@@ -195,7 +204,7 @@ void vhost_net_cleanup(struct vhost_net }#else struct vhost_net *vhost_net_init (VLANClientState *backend,int devfd, -bool force) +bool force,int numtxqs) {return NULL ; }
hw/vhost_net.h +2 -1
@@ -6,7 +6,8 @@struct vhost_net ;typedef struct vhost_net VHostNetState ; -VHostNetState *vhost_net_init (VLANClientState *backend,int devfd,bool force); +VHostNetState *vhost_net_init (VLANClientState *backend,int devfd,bool force, +int numtxqs);bool vhost_net_query (VHostNetState *net,VirtIODevice *dev);int vhost_net_start (VHostNetState *net,VirtIODevice *dev);
hw/virtio-net.c +49 -28
219 lines not on this page · raw message
hw/virtio-net.h +2 -0
@@ -44,6 +44,7 @@#define VIRTIO_NET_F_CTRL_RX 18/* Control channel RX mode support */ #define VIRTIO_NET_F_CTRL_VLAN 19/* Control channel VLAN filtering */ #define VIRTIO_NET_F_CTRL_RX_EXTRA 20/* Extra RX mode control support */ +#define VIRTIO_NET_F_MULTIQUEUE 21/* Supports multiple RX/TX queues */ #define VIRTIO_NET_S_LINK_UP 1/* Link is up */
@@ -72,6 +73,7 @@ struct virtio_net_configuint8_t mac[ETH_ALEN ];/* See VIRTIO_NET_F_STATUS and VIRTIO_NET_S_* above */ uint16_t status; +uint16_t num_queue_pairs;/* number of rx+tx queues */ } __attribute__((packed));/* This is the first element of the scatter-gather list. If you don't
hw/virtio-pci.c +2 -0
@@ -103,6 +103,7 @@ typedef struct {uint32_t addr;uint32_t class_code;uint32_t nvectors; +uint32_t mq;BlockConf block;NICConf nic;uint32_t host_features;
@@ -965,6 +966,7 @@ static PCIDeviceInfo virtio_info[] = {DEFINE_PROP_BIT ("ioeventfd" , VirtIOPCIProxy, flags,VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT , false),DEFINE_PROP_UINT32 ("vectors" , VirtIOPCIProxy, nvectors,3 ), +DEFINE_PROP_UINT32 ("mq" , VirtIOPCIProxy, mq,1 ),DEFINE_VIRTIO_NET_FEATURES (VirtIOPCIProxy, host_features),DEFINE_NIC_PROPERTIES (VirtIOPCIProxy, nic), DEFINE_PROP_UINT32("x-txtimer", VirtIOPCIProxy,
net/tap.c +161 -51
379 lines not on this page · raw message
net/tap.h +2 -1
@@ -35,6 +35,7 @@int net_init_tap (QemuOpts *opts,Monitor *mon,const char *name,VLANState *vlan);int tap_open (char *ifname,int ifname_size,int *vnet_hdr,int vnet_hdr_required); +int vtap_open (const char *devname,int *vnet_hdr,int vnet_hdr_required);ssize_t tap_read_packet (int tapfd,uint8_t *buf,int maxlen);
@@ -52,7 +53,7 @@ int tap_probe_has_ufo(int fd);void tap_fd_set_offload (int fd,int csum,int tso4,int tso6,int ecn,int ufo);void tap_fd_set_vnet_hdr_len (int fd,int len); -int tap_get_fd (VLANClientState *vc); +int tap_get_fd (VLANClientState *vc,int index);struct vhost_net ;struct vhost_net *tap_get_vhost_net (VLANClientState *vc);
net/tap-linux.c +42 -0
48 lines not on this page · raw message
net.c +22 -0
@@ -798,6 +798,16 @@ static int net_init_nic(QemuOpts *opts,return -1 ; } +if (nd->netdev->numtxqs >1 && nd->nvectors ==DEV_NVECTORS_UNSPECIFIED ) { +/* +* User specified mq for guest, but no "vectors=", tune +* it automatically to 'numtxqs' TX + 'numtxqs' RX + 1 controlq. +*/ + nd->nvectors = nd->netdev->numtxqs *2 +1 ; +monitor_printf (mon,"nvectors tuned to %d\n" , nd->nvectors); + } + + nd->used =1 ; nb_nics++;
@@ -941,6 +951,18 @@ static const struct { }, #ifndef _WIN32 { + .name = "vtap", + .type = QEMU_OPT_STRING, + .help = "name of macvtap device to use", + }, { + .name = "mq", + .type = QEMU_OPT_BOOL, + .help = "enable multiqueue on network i/f", + }, { + .name = "numtxqs", + .type = QEMU_OPT_NUMBER, + .help = "optional number of RX/TX queues, if mq is enabled", + }, { .name = "fd", .type = QEMU_OPT_STRING, .help = "file descriptor of an already opened tap",
net.h +1 -0
@@ -64,6 +64,7 @@ struct VLANClientState {struct VLANState *vlan;VLANClientState *peer;NetQueue *send_queue; +int numtxqs;char *model;char *name;char info_str[256 ];
Quoted 37 lines · Krishna Kumar
This patchset implements both RX and TX MQ. Patch against virtio-net, vhost and qemu are included.
Changes from rev1:
-------------------
1. vqs are allocated as: rx/tx, rx/tx, rx/tx, etc. Lot of code in guest/host/qemu changes, but code becomes simpler.
2. vhost cache align ofvhost_devcorrectly.
3. virtio-net: cleanup properly on errors (eg detach buf for vq>0 as pointed out by Micheal).
4. Minor changes:
- Fixed some typos.
- Changedvhost_get_thread_indexto useMAX_VHOST_THREADS.
- RemovedVIRTIO_MAX_TXQS.
- Changed capability toVIRTIO_NET_F_MULTIQUEUE.
- Modified "numtxqs" invirtnet_infoto "num_queue_pairs".virtnet_infostill has numtxqs as it is more convenient.
- Moved code forVIRTIO_NET_F_CTRL_VLANinto probe function.
- Improve check for return value ofvirtio_config_val().
- Removed cache align directives in guest as it was redundant.
5. "If we have a wrapper to init all vqs, pls add a wrapper to clean up all vqs as well": I haven't done this as some errors are very specific to the failure location (and what was initialized till then). So only those errors are cleaned up using goto's like the rest of the code. I can change in next version if you feel this is still required.
6. "I think we should havefree_unused_bufsthat handles a single queue, and call it in a loop": I haven't done this as I think the caller wants all rx/tx queues to be cleaned up by calling this function.TODO's:
--------
1. Reduce vectors forfind_vqs().
2. Make vhost changes minimal. For now, I have restricted the number of vhost threads to 4. This can be either made unrestricted; or if the userspace vhost works, it can be removed altogether.
Quoted 2 lines
Please review and provide feedback. I am travelling a bit in the next few days but will respond at the earliest.
Do you have an update to the virtio-pci spec for this?
Signature · 1 line
error compiling committee.c: too many arguments to function
Avi Kivity <[email protected]> wrote on 04/13/2011 05:30:11 PM:
Hi Avi,
Quoted 10 lines
Quoted 8 lines
--------
1. Reduce vectors forfind_vqs().
2. Make vhost changes minimal. For now, I have restricted the number of vhost threads to 4. This can be either made unrestricted; or if the userspace vhost works, it can be removed altogether.Please review and provide feedback. I am travelling a bit in the next few days but will respond at the earliest.
Do you have an update to the virtio-pci spec for this?
Not yet, will keep it in my TODO list.
thanks,
- KK