How do I support virtio-net?
How do I support virtio-net?
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?
I think you can start here: http://wiki.osdev.org/Virtio
If it's not enough, you can dig into the specification: http://lxr.free-electrons.com/source/Do ... .txt?v=3.4 (plain text) or http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf (PDF).
Upd: + paper with pictures: http://ozlabs.org/~rusty/virtio-spec/virtio-paper.pdf
If it's not enough, you can dig into the specification: http://lxr.free-electrons.com/source/Do ... .txt?v=3.4 (plain text) or http://ozlabs.org/~rusty/virtio-spec/virtio-0.9.5.pdf (PDF).
Upd: + paper with pictures: http://ozlabs.org/~rusty/virtio-spec/virtio-paper.pdf
Re: How do I support virtio-net?
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?
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?
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.
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.
Project: OZone
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott
Source: GitHub
Current Task: LIB/OBJ file support
"The more they overthink the plumbing, the easier it is to stop up the drain." - Montgomery Scott

