eBPF (XDP) Network Programming.

Recently I made an XDP based program that hooks into the network interface and gets called for every network packet that arrives at my system. depending on where it got attached it can run in driver itself or it will run in kernel(little slower). well there is another mode where the program runs in the hardware itself but those network devices are pretty rare to obtain so we can’t really use it in general.

running a piece of code like this is very dangerous without any supervisor. because this code is run in the kernel space. you get it ? a crash could lead to kernel crash. imagine you just send one packet to yourself and bam your kernel just crashed. well in order to prevent this thing the Linux kernel has a methodology that’s called eBPF. Extended Berkeley Packet Filter. It’s basically a way to run code that can change the behaviour of the kernel safely. the design of this system came from BSD where in kernel there was a vm that could process the network packets coming to the machine. that vm executed a program on each packet. Linux picked the same idea long time ago and extended it. eBPF can do a lot more than just processing network packets as of today.

Since I’m also a beginner in this, I thought why not just do some project in it to understand this. so I did. which is called stint. Which is a rate limiter attached to the network interface. It has more features like auto/manual ip banning. But it’s a very small program. It checks every incoming ethernet packet and finds the ip by looking inside it and then records it to a data structure. then we do the regular token bucket rate limiting algorithm on that data. well the system is very simple.

So first of all in XDP you get a context, and inside that lives the ethernet packet. you look into the headers of that and you check if it’s an IPv4/IPv6 packet. then you look into the IP packet and take out the actual source ip. well if you need the source port you need to go one level deeper into the ip packet where you can see the tcp/udp header for port.

eBPF programs are run in a virtual machine inside the kernel. In order to run your program, first you write a normal program in a high level language, for example C, Rust etc. Then you compile that to eBPF bytecode. clang supports this. after that you load the program and attach it to a hook. when you load it, the eBPF verifier checks your program for correctness. It will ensure that your program is safe. after loading it needs to be attached to some hook. one of the hook types is XDP which stands for eXpress Data Path. simply put, if an ethernet packet arrives at your network interface your hook triggers the program to run. there are several types of hooks, this is the reason why I said eBPF has more features. but the design is the same. it’s an event based execution no matter what. something should trigger it. so after you hook it you get a link. if the link gets destroyed your program gets deallocated so you usually pin it to a file.

Since I told you eBPF runs inside a kernel VM. The VM has a 512-byte stack per program. normal C types are supported only inside that. but what about some state that should outlive a program run? in that case you have one choice which is MAP. though it’s called map it’s definitely not a simple hashmap. here map can be said as a data structure that supports different types of storage. an array, a hashmap, an LRU hash, a ring buffer, an LPM trie etc. So these data structures outlive a program.

Now suppose you want to read a state of the program or provide some state to it from user space, how will we do it? the mechanism is very trivial: you pin the map to a file, open an fd to it and interact with that storage.

That’s it for the eBPF basics. I will write more about it in upcoming days. until then happy hacking <3.