How do I support virtio-net?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
mariuszp
Member
Member
Posts: 593
Joined: Sat Oct 16, 2010 3:38 pm

How do I support virtio-net?

Post 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?
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: How do I support virtio-net?

Post by Nable »

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
mariuszp
Member
Member
Posts: 593
Joined: Sat Oct 16, 2010 3:38 pm

Re: How do I support virtio-net?

Post 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?
Nable
Member
Member
Posts: 453
Joined: Tue Nov 08, 2011 11:35 am

Re: How do I support virtio-net?

Post 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.
User avatar
SpyderTL
Member
Member
Posts: 1074
Joined: Sun Sep 19, 2010 10:05 pm

Re: How do I support virtio-net?

Post 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.
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
Post Reply