Luke Gorrie's blog

21st Century Network Hacking

Echoing Packets With Snabb Switch

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
function Port:echo ()
   local inputs, outputs = self.inputs, self.outputs
   repeat
      for i = 1,#inputs do
         local input, output = inputs[i], outputs[i]
         input:sync_receive()
         while input:can_receive() and output:can_transmit() do
            local buf = input:receive()
            output:transmit(buf)
            buffer.deref(buf)
         end
         while input:can_add_receive_buffer() do
            input:add_receive_buffer(buffer.allocate())
         end
         output:sync_transmit()
      end
      C.usleep(1)
   until coroutine.yield("echo") == nil
end

Reasonably easy to understand, I hope?

The features of this program that I would like to draw attention to are:

  1. It is written in a high-level language, LuaJIT.
  2. 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.
  3. It uses device drivers directly. The input and output 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.
  4. 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() and buffer.deref().
  5. 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.