Page 1 of 1
How do I support virtio-net?
Posted: Fri Oct 02, 2015 12:42 pm
by mariuszp
I attempted to google how VirtualBox's virtio-net is to be programmed but I cannot find any answer - just people looking for pre-made drivers. I remember seeing someone on this forum saying that their OS supports virtio-net. Where can I obtain the necessary information needed to support it in my OS?
Re: How do I support virtio-net?
Posted: Fri Oct 02, 2015 5:10 pm
by Nable
Re: How do I support virtio-net?
Posted: Sat Oct 03, 2015 8:10 am
by mariuszp
OK I think I get most things, but I don't seem to understand what happens when a packet is received. Do I receive an interrupt and then have to allocate that empty buffer and pass it to the virtio-net device to get the incoming packet?
Re: How do I support virtio-net?
Posted: Sat Oct 03, 2015 2:22 pm
by Nable
As I understand, you have to allocate virtio queues (buffers + some structures) at driver startup and tell the virtual device their addresses. When interrupt comes, you disable RX interrupt, consume packets from queues (copy data + remove entries from list) and enable RX interrupt again. Strictly speaking, you don't have to disable interrupts, it's just to reduce the number of VM exits.
Re: How do I support virtio-net?
Posted: Sat Oct 03, 2015 8:15 pm
by SpyderTL
Yes, you need to give the VirtIO device at least one buffer to use to store incoming network packets before you enable it.
Virtio network packets have a small header before the start of the Ethernet header. Some virtual machines assume that this header is in its own, separate buffer, even though the specs don't mention anything about separating the VirtIO header. I found this out the hard way.
Also, keep in mind that a VirtIO buffer can be read-only or write-only, from the devices point of view, but it can't be both. So you have to split up your buffers into readable segments and writable segments, as well.
When a packet is received, the VirtIO device will fill the next available buffer, and then move the buffer from the available queue to the DONE queue. You can just check the done queue whenever you get an interrupt, and process any buffers that you find in the done queue.
Let us know if you run into any problems.