Virtio draft IV
Subsystems (7)
PATCH 1/3 Virtio draft IV
In response to Avi's excellent analysis, I've updated virtio as promised (apologies for the delay, travel got in the way).
===
This attempts to implement a "virtual I/O" layer which should allow common drivers to be efficiently used across most virtual I/O mechanisms. It will no-doubt need further enhancement.
The details of probing the device are left to hypervisor-specific
code: it simple constructs the "struct virtio_device" and hands it to the probe function (eg. virtnet_probe() or virtblk_probe()).
The virtio drivers add and get I/O buffers; as the buffers are consumed the driver "interrupt" callbacks are invoked.
I have written two virtio device drivers (net and block) and two virtio implementations (for lguest): a read-write socket-style implementation, and a more efficient descriptor-based implementation.
--- include/linux/virtio.h | 64 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+)
===================================================================
include/linux/virtio.h new file +64 -0
@@ -0,0 +1,64 @@ +#ifndef _LINUX_VIRTIO_H +#define _LINUX_VIRTIO_H +#include <linux/types.h> +#include <linux/scatterlist.h> +#include <linux/spinlock.h> + +/** +* virtqueue - queue for virtual I/O +* @ops: the operations for this virtqueue. +* @cb: set by the driver for callbacks. +* @priv: private pointer for the driver to use. +*/ +struct virtqueue { +struct virtqueue_ops *ops; +bool (*cb)(struct virtqueue *vq); +void *priv; +}; + +/** +* virtqueue_ops - operations for virtqueue abstraction layer +* @add_buf: expose buffer to other end +* vq: the struct virtqueue we're talking about. +* sg: the description of the buffer(s). +* out_num: the number of sg readable by other side +* in_num: the number of sg which are writable (after readable ones) +* data: the token identifying the buffer. +* Returns 0 or an error. +* @sync: update after add_buf +* vq: the struct virtqueue +* After one or more add_buf calls, invoke this to kick the virtio layer. +* @get_buf: get the next used buffer +* vq: the struct virtqueue we're talking about. +* len: the length written into the buffer +* Returns NULL or the "data" token handed to add_buf. +* @detach_buf: "unadd" an unused buffer. +* vq: the struct virtqueue we're talking about. +* data: the buffer identifier. +* This is usually used for shutdown, returnes 0 or -ENOENT. +* @restart: restart callbacks ater callback returned false. +* vq: the struct virtqueue we're talking about. +* This returns "false" (and doesn't re-enable) if there are pending +* buffers in thq queue, to avoid a race. +* +* Locking rules are straightforward: the driver is responsible for +* locking. No two operations may be invoked simultaneously. +* +* All operations can be called in any context. +*/ +struct virtqueue_ops { +int (*add_buf)(struct virtqueue *vq, +struct scatterlist sg[], +unsigned int out_num, +unsigned int in_num, +void *data); + +void (*sync)(struct virtqueue *vq); + +void *(*get_buf)(struct virtqueue *vq,unsigned int *len); + +int (*detach_buf)(struct virtqueue *vq,void *data); + +bool (*restart)(struct virtqueue *vq); +}; +#endif /* _LINUX_VIRTIO_H */
PATCH 2/3 Virtio draft IV: the block driver
The block driver uses scatter-gather lists with sg[0] being the request information (struct virtio_blk_outhdr) with the type, sector and inbuf id. The next N sg entries are the bio itself, then the last sg is the status byte. Whether the N entries are in or out depends on whether it's a read or a write.
We accept the normal (SCSI) ioctls: they get handed through to the other side which can then handle it or reply that it's unsupported. It's not clear that this actually works in general, since I don't know if blk_pc_request() requests have an accurate rq_data_dir().
Although we try to reply -ENOTTY on unsupported commands, the block layer in its infinite wisdom suppressed the error so ioctl(fd, CDROMEJECT) returns success to userspace.
--- drivers/block/Makefile | 1 drivers/block/virtio_blk.c | 253 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/Kbuild | 1 include/linux/virtio_blk.h | 45 +++++++ 4 files changed, 300 insertions(+)
===================================================================
drivers/block/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_BLK_CPQ_CISS_DA) += cciss. obj-$(CONFIG_BLK_CPQ_CISS_DA) += cciss.o obj-$(CONFIG_BLK_DEV_DAC960) += DAC960.o obj-$(CONFIG_CDROM_PKTCDVD) += pktcdvd.o +obj-y += virtio_blk.o obj-$(CONFIG_BLK_DEV_UMEM) += umem.o obj-$(CONFIG_BLK_DEV_NBD) += nbd.o
===================================================================
drivers/block/virtio_blk.c new file +253 -0
253 lines not on this page · raw message
===================================================================
include/linux/Kbuild +1 -0
@@ -341,6 +341,7 @@ unifdef-y += utsname.h unifdef-y += utsname.h unifdef-y += videodev2.h unifdef-y += videodev.h +unifdef-y += virtio_blk.h unifdef-y += wait.h unifdef-y += wanrouter.h unifdef-y += watchdog.h
===================================================================
include/linux/virtio_blk.h new file +45 -0
45 lines not on this page · raw message
PATCH 3/3 Virtio draft IV: the net driver
The network driver uses *two* virtqueues: one for input packets and one for output packets. This has nice locking properties (ie. we don't do any for recv vs send).
TODO:
1) GSO.
2) Checksum options.
3) Big packets.
4) Multi-client devices (maybe separate driver?).
--- drivers/net/Makefile | 2 drivers/net/virtio_net.c | 276 ++++++++++++++++++++++++++++++++++++++++++++ include/linux/virtio_net.h | 15 ++ 3 files changed, 292 insertions(+), 1 deletion(-)
===================================================================
drivers/net/Makefile +1 -1
@@ -37,7 +37,7 @@ obj-$(CONFIG_CASSINI) += cassini.o obj-$(CONFIG_MACE) += mace.o obj-$(CONFIG_BMAC) += bmac.o - +obj-y += virtio_net.o obj-$(CONFIG_DGRS) += dgrs.o obj-$(CONFIG_VORTEX) += 3c59x.o obj-$(CONFIG_TYPHOON) += typhoon.o
===================================================================
drivers/net/virtio_net.c new file +276 -0
276 lines not on this page · raw message
===================================================================
include/linux/virtio_net.h new file +15 -0
@@ -0,0 +1,15 @@ +#ifndef _LINUX_VIRTIO_NET_H +#define _LINUX_VIRTIO_NET_H +#include <linux/types.h> +#include <linux/etherdevice.h> +struct device ; +struct net_device ; +struct virtqueue ; + +struct net_device *virtnet_probe (struct virtqueue *vq_recv, +struct virtqueue *vq_send, +struct device *dev, +const u8 mac[ETH_ALEN ]); +void virtnet_remove (struct net_device *dev); + +#endif /* _LINUX_VIRTIO_NET_H */
Re: [PATCH 3/3] Virtio draft IV: the net driver
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 13 lines
drivers/net/virtio_net.c new file +13
-0+static void try_fill_recv (struct virtnet_info *vi) +{ +struct sk_buff *skb; +struct scatterlist sg[MAX_SKB_FRAGS ]; +int num, err; + +for (;;) { + skb =netdev_alloc_skb (vi->ndev,MAX_PACKET_LEN ); +if (unlikely (!skb)) +break ; + +skb_put (skb,MAX_PACKET_LEN ); + num =skb_to_sgvec (skb, sg,0 , skb->len);
drivers/net/virtio_net.c line 94
Quoted 10 lines
drivers/net/virtio_net.c new file +10
-0+skb_queue_head (&vi->recv, skb); + + err = vi->vq_recv->ops->add_buf (vi->vq_recv, sg,0 , num, skb); +if (err) { +skb_unlink (skb, &vi->recv); +kfree_skb (skb); +break ; + } + vi->num++; + }
Hmm, so it allocates skbs until oom or until add_buf fails, right?
Do you expect the add_buf call to fail if we have enough buffers? Who defines the amount of buffers we can add via add_buf?
Signature · 5 lines
IBM Deutschland Entwicklung GmbH Vorsitzender des Aufsichtsrats: Martin Jetter Geschäftsführung: Herbert Kircher Sitz der Gesellschaft: Böblingen Registergericht: Amtsgericht Stuttgart, HRB 243294
Re: [PATCH 3/3] Virtio draft IV: the net driver
Quoted 4 lines · Christian Borntraeger
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void
try_fill_recv(struct virtnet_info *vi)Hmm, so it allocates skbs until oom or until
add_buffails, right?
Yep.
Quoted 2 lines
Do you expect the
add_bufcall to fail if we have enough buffers? Who defines the amount of buffers we can add viaadd_buf?
There will be some internal limit on how many buffers the virtio implementation supports, but depends on that implementation. It could be a number of buffers or a total number of descriptors.
Cheers,
Rusty.
Re: [PATCH 3/3] Virtio draft IV: the net driver
Am Mittwoch, 11. Juli 2007 schrieb Rusty Russell:
Quoted 3 lines
There will be some internal limit on how many buffers the virtio implementation supports, but depends on that implementation. It could be a number of buffers or a total number of descriptors.
I would suggest to implement a limit in the device driver as well. Otherwise the network driver could allocate a huge amount of guest memory if the virtio implementation accepts a large amount of buffers. This memory is not swappable and reclaimable by the memory management, so we should be careful.
So what about something like this:
+ for (;vi->num < MAX_BUFS;) {
+ skb = netdev_alloc_skb(vi->ndev, MAX_PACKET_LEN);
+ if (unlikely(!skb))
+ break;
+
+ skb_put(skb, MAX_PACKET_LEN);
+ num = skb_to_sgvec(skb, sg, 0, skb->len);
+ skb_queue_head(&vi->recv, skb);
+
+ err = vi->vq_recv->ops->add_buf(vi->vq_recv, sg, 0, num, skb);
+ if (err) {
+ skb_unlink(skb, &vi->recv);
+ kfree_skb(skb);
+ break;+ }
+ vi->num++
+ }
Christian
Re: [PATCH 3/3] Virtio draft IV: the net driver
Quoted 9 lines · Christian Borntraeger
Am Mittwoch, 11. Juli 2007 schrieb Rusty Russell:
Quoted 3 lines
There will be some internal limit on how many buffers the virtio implementation supports, but depends on that implementation. It could be a number of buffers or a total number of descriptors.
I would suggest to implement a limit in the device driver as well. Otherwise the network driver could allocate a huge amount of guest memory if the virtio implementation accepts a large amount of buffers. This memory is not swappable and reclaimable by the memory management, so we should be careful.
Hi Christian,
I this this would be premature, but easy enough to do later if we have
such a high-limit virtio implementation. If we did do it, it would
make sense to have it a variable passed into the net driver.
Thanks,
Rusty.
RE: [PATCH 3/3] Virtio draft IV: the net driver
In reply to Rusty RussellQuoted 8 lines · [email protected] wrote:
Quoted 4 lines · Christian Borntraeger
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void
try_fill_recv(struct virtnet_info *vi)Hmm, so it allocates skbs until oom or until
add_buffails, right?Yep.
Quoted 8 lines
Quoted 2 lines
Do you expect the
add_bufcall to fail if we have enough buffers? Who defines the amount of buffers we can add viaadd_buf?There will be some internal limit on how many buffers the virtio implementation supports, but depends on that implementation. It could be a number of buffers or a total number of descriptors.
I think one of the key tradeoffs here is simplicity versus flexibility. A QP style interface is very explicit about the size of queues (requested
and actual) and the size of queue entries (request maximum and actual maximum), current depths, etc.
My reading of the proposed interface is that is it much much simpler than all that. This takes flexibility away from the application, that in theory *could* have adapted its behavior to different queue sizes, and gives it to the specific implmentation (that can now do things like variable-length work queue entries if it wants to).
The one thing I'd recommend avoiding is starting with a simple interface and then tacking on one bell or whistle at a time. We should either keep
it simple or shift to a QP style interface and lean on all the research and negotiations on what attributes are needed that has already taken place
for RDMA related networking interfaces. It is also a pain for implementations
to have to deal with multiple *detailed* queue interface requirements that
all try to accomplish the same thing but insist on doing it differently. If things are vague for one interface you have the flexibility to re-use what another interface forced you to do.
But unless there are *specific* consumers who say they want specific bells
and whistles my hunch would be to stick with the simple interface. Which translates as "if you need to add a buffer, add it, let us worry about it,
if you've gone too far we'll tell you."
Re: [PATCH 3/3] Virtio draft IV: the net driver
In reply to Rusty RussellAm Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void receive_skb(struct
net_device*dev, structsk_buff*skb,
[...]
drivers/net/virtio_net.c line 69
Quoted 1 line
drivers/net/virtio_net.c new file +1
-0+netif_rx (skb);
In the NAPI case, we should use netif_receive_skb, no?
Christian
Re: [PATCH 3/3] Virtio draft IV: the net driver
Quoted 6 lines · Christian Borntraeger
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void receive_skb(struct
net_device*dev, structsk_buff*skb,[...]
drivers/net/virtio_net.c line 69
Quoted 1 line
drivers/net/virtio_net.c new file +1
-0+netif_rx (skb);In the NAPI case, we should use
netif_receive_skb, no?
Oops, well spotted!
I'm working on GSO and checksum offload support at the moment, will roll that fix into the next release.
Thanks!
Rusty.
David Miller
maintainerRe: [PATCH 3/3] Virtio draft IV: the net driver
In reply to Christian BorntraegerFrom: Christian Borntraeger <[email protected]>
Date: Wed, 11 Jul 2007 12:45:40 +0200
Quoted 6 lines
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void receive_skb(struct
net_device*dev, structsk_buff*skb,[...]
drivers/net/virtio_net.c line 69
Quoted 1 line
drivers/net/virtio_net.c new file +1
-0+netif_rx (skb);In the NAPI case, we should use
netif_receive_skb, no?
NAPI doesn't make sense for virtual devices, my Sun LDOM nework driver won't use NAPI either.
It's also too cumbersome to use NAPI with the way virtualized network drivers work (multiple ports, each with an interrupt source, not just one) until the NAPI split patches are ported and applied upstream and that won't be for a while.
Re: [PATCH 3/3] Virtio draft IV: the net driver
Quoted 10 lines · David Miller
From: Christian Borntraeger <[email protected]>
Date: Wed, 11 Jul 2007 12:45:40 +0200Quoted 6 lines
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 1 line
+static void receive_skb(struct
net_device*dev, structsk_buff*skb,[...]
drivers/net/virtio_net.c line 69
Quoted 1 line
drivers/net/virtio_net.c new file +1
-0+netif_rx (skb);In the NAPI case, we should use
netif_receive_skb, no?
Quoted 7 lines
NAPI doesn't make sense for virtual devices, my Sun LDOM nework driver won't use NAPI either.
It's also too cumbersome to use NAPI with the way virtualized network drivers work (multiple ports, each with an interrupt source, not just one) until the NAPI split patches are ported and applied upstream and that won't be for a while.
Dave, I think you're the only one (so far?) with multiple irqs.
It's not clear that guest-controlled interrupt mitigation is the best approach for virtual devices, but at the moment it doesn't hurt.
Cheers,
Rusty.
David Miller
maintainerRe: [PATCH 3/3] Virtio draft IV: the net driver
From: Rusty Russell <[email protected]>
Date: Thu, 12 Jul 2007 12:21:33 +1000
Quoted 1 line
Dave, I think you're the only one (so far?) with multiple irqs.
Luckily there are known hw implementations with that issue so I won't be weird for long :)
Quoted 2 lines
It's not clear that guest-controlled interrupt mitigation is the best approach for virtual devices, but at the moment it doesn't hurt.
It would be nice for consistency's sake, once it is easy to do so.
Re: [PATCH 2/3] Virtio draft IV: the block driver
In reply to Rusty RussellAm Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 3 lines
drivers/block/virtio_blk.c new file +3
-0+ vbr =mempool_alloc (vblk->pool,GFP_ATOMIC ); +if (!vbr) + goto stop;
[...]
Quoted 4 lines
drivers/block/virtio_blk.c new file +4
-0+BUG_ON (req->nr_phys_segments >ARRAY_SIZE (vblk->sg)); + vbr->req = req; +if (!do_req (q, vblk, vbr)) + goto stop;
[...]
drivers/block/virtio_blk.c line 158
Quoted 4 lines
drivers/block/virtio_blk.c new file +4
-0+stop: +/* Queue full? Wait. */ +blk_stop_queue (q); +mempool_free (vbr, vblk->pool);
Hmm, can mempool_free really handle NULL as its first argument? (first goto). As far as I can see we have a problem here. Our pool has preallocated 1 element. So what about:
- first alloc fails in mempool_alloc fails -> we get the pre-allocated element, no more elements left
- second alloc fails in mempool_alloc fails -> we get NULL -> we return NULL to mempool_free which adds NULL back to the pool as the pool is empty. Now the pool has an NULL entry, no?
Christian
Re: [PATCH 2/3] Virtio draft IV: the block driver
Quoted 10 lines · Christian Borntraeger
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 3 lines
(quoted diff) +3
-0+ vbr = mempool_alloc(vblk->pool, GFP_ATOMIC); + if (!vbr) + goto stop;[...]
Quoted 4 lines
(quoted diff) +4
-0+ BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg)); + vbr->req = req; + if (!do_req(q, vblk, vbr)) + goto stop;[...]
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+stop: + /* Queue full? Wait. */ + blk_stop_queue(q); + mempool_free(vbr, vblk->pool);Hmm, can
mempool_freereally handle NULL as its first argument? (first goto).
Good point. Any objections to fixing that?
Cheers,
Rusty.
===
Christian Borntraeger points out that mempool_free() doesn't noop when handed NULL. This is inconsistent with the other free-like functions in the kernel.
mm/mempool.c +3 -0
@@ -263,6 +263,9 @@ void mempool_free(void *element, mempool {unsigned long flags; +if (unlikely (element ==NULL )) +return ; +smp_mb ();if (pool->curr_nr < pool->min_nr) {spin_lock_irqsave (&pool->lock, flags);
Re: [PATCH 2/3] Virtio draft IV: the block driver
In reply to Christian BorntraegerQuoted 10 lines · Christian Borntraeger
Am Mittwoch, 4. Juli 2007 schrieb Rusty Russell:
Quoted 3 lines
(quoted diff) +3
-0+ vbr = mempool_alloc(vblk->pool, GFP_ATOMIC); + if (!vbr) + goto stop;[...]
Quoted 4 lines
(quoted diff) +4
-0+ BUG_ON(req->nr_phys_segments > ARRAY_SIZE(vblk->sg)); + vbr->req = req; + if (!do_req(q, vblk, vbr)) + goto stop;[...]
Quoted 7 lines
Review
Quoted 4 lines
(quoted diff) +4
-0+stop: + /* Queue full? Wait. */ + blk_stop_queue(q); + mempool_free(vbr, vblk->pool);Hmm, can
mempool_freereally handle NULL as its first argument? (first goto).
Good point. Any objections to fixing that?
Cheers,
Rusty.
===
Christian Borntraeger points out that mempool_free() doesn't noop when handed NULL. This is inconsistent with the other free-like functions in the kernel.
mm/mempool.c +3 -0
@@ -263,6 +263,9 @@ void mempool_free(void *element, mempool {unsigned long flags; +if (unlikely (element ==NULL )) +return ; +smp_mb ();if (pool->curr_nr < pool->min_nr) {spin_lock_irqsave (&pool->lock, flags);
Re: [PATCH 2/3] Virtio draft IV: the block driver
In reply to Rusty RussellReview
Quoted 5 lines
(quoted diff) +5
-0+#ifdef __KERNEL__ +struct gendisk; +struct virtio_device; + +struct gendisk *virtblk_probe(struct virtqueue *vq);
Hello Rusty,
I am currently do some prototyping with virtio IV and found tis small issue in the block device. virtblk_probe requires a declaration of virtqueue, not virtio_device.
--- include/linux/virtio_blk.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/virtio_blk.h +1 -1
@@ -37,7 +37,7 @@ struct virtio_blk_inhdr#ifdef __KERNEL__struct gendisk ; -struct virtio_device ; +struct virtqueue ;struct gendisk *virtblk_probe (struct virtqueue *vq);void virtblk_remove (struct gendisk *disk);
Re: [PATCH 2/3] Virtio draft IV: the block driver
Quoted 11 lines · Christian Borntraeger
Review
Quoted 5 lines
(quoted diff) +5
-0+#ifdef __KERNEL__ +struct gendisk; +struct virtio_device; + +struct gendisk *virtblk_probe(struct virtqueue *vq);Hello Rusty,
I am currently do some prototyping with virtio IV and found tis small issue in the block device.
virtblk_proberequires a declaration of virtqueue, notvirtio_device.
Oops, thanks Christian!
I'll roll this in, thanks!
Rusty.
PS. 2.6.23-rc1 broke the block driver anyway, so will publish new versions soon.