Just for fun, here is some simple Snabb Switch example code to retransmit
every packet that is received on a set of Ethernet ports. It’s part of
the basic selftest code in
port.lua
.
The coding style is starting to settle down lately and so it’s more
tempting for me to share little snippets of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Reasonably easy to understand, I hope?
The features of this program that I would like to draw attention to are:
- It is written in a high-level language, LuaJIT.
- On one Xeon core it handles 12 million packets per second spread across 20 x 10GbE network ports. This loop runs in about 85 nanoseconds per packet. And there is heaps of room for optimization left, both in the single-core and multi-core contexts.
- It uses device drivers directly. The
input
andoutput
objects are each one of our drivers for Intel 1G, Intel 10G, or Virtio ethernet devices. The “buffers” are blocks of physical memory that are directly used for hardware DMA. - It’s written in a pretty natural style and yet doesn’t need to garbage collect. The only objects being frequently allocated and freed are the packet buffers, and those are straightforwardly reference counted with
buffer.ref()
andbuffer.deref()
. - There are no confusing concepts like abrupt processor interrupts, threads, locks, or special memory allocation flags. It’s plain and simple high-level user space code just like, say, the Javascript on a web page.
This my friends is an idea for how we could write high-speed packet networking code over the next decade or so. I hope to win you over to this way of thinking :-)
If you want to know more then check out the project homepage, browse the code on github, or browse an early draft of the book.