• Stars
    star
    203
  • Rank 192,890 (Top 4 %)
  • Language
    C
  • License
    GNU General Publi...
  • Created about 3 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A very basic eBPF Load Balancer in a few lines of C

Network-related demos

  • Load Balancer from Scratch
  • Packetdrop
  • Packet counting

An eBPF Load Balancer from scratch

As seen at eBPF Summit 2021. This is not production ready :-)

This uses libbpf as a git submodule. If you clone this repo you'll need to run git submodule init and git submodule update to get your copy of that repo.

Load Balancer container

In my demo I'm running all the components as containers.

For the Load Balancer component itself you can build a Docker image from Dockerfile.lb, which starts from an ubuntu container with additional dependencies so that it can compile the eBPF code.

docker buildx create --name mybuilder --bootstrap --use
docker buildx build --push --platform linux/arm64,linux/amd64 --tag lizrice/ubuntu-ebpf-lb -f Dockerfile.lb .
sudo apt install clang llvm libelf-dev libpcap-dev gcc-multilib build-essential make linux-tools-common

Note: gcc-multilib is not currently available for ARM architectures on Ubuntu 22.04. I'm adding /usr/include/$(shell uname -m)-linux-gnu into the include path instead. See this thread for more info.

My version of this container image is available at lizrice/ubuntu-ebpf-lb.

Running it as privileged gives it permissions to load eBPF programs:

docker run --rm -it -v ~/lb-from-scratch:/lb-from-scratch --privileged -h lb --name lb --env TERM=xterm-color lizrice/ubuntu-ebpf-lb

Exec into that container, cd lb-from-scratch and then make should build and install the load balancer onto the eth0 interface for that container.

Demo containers

Here's how I started the containers for the two backends and the client:

docker run -d --rm --name backend-A -h backend-A --env TERM=xterm-color nginxdemos/hello:plain-text
docker run -d --rm --name backend-B -h backend-B --env TERM=xterm-color nginxdemos/hello:plain-text
docker run --rm -it -h client --name client --env TERM=xterm-color ubuntu

Exec into one of the backends and install tcpdump with apk add tcpdump if you want to see incoming traffic there.

Run something on the host that tails the output from BPF trace (for example, my hello world eBPF beginners examples) or just sudo cat /sys/kernel/debug/tracing/trace_pipe

IP addresses

The IP addresses for the client, load balancer and two backends are hard-coded at the top of the .c file. You'll likely need to change these to match the addresses assigned to the containers you run.

Packet drop - ping demo

Install ping utils into the ubuntu-working container

apt install iputils-ping

Save off into an image called ubuntu-pingbox: docker commit <running container> ubuntu-pingbox

docker run --rm -it -v ~/ebpf-net-beginners:/ebpf-net-beginners --privileged -h pingbox --name pingbox --env TERM=xterm-color ubuntu-pingbox

Or use the version I pushed to Docker Hub

docker run --rm -it -v ~/lb-from-scratch:/lb-from-scratch --privileged -h pingbox --env TERM=xterm-color lizrice/ubuntu-pingbox

Find its ip address (ip a from inside, or docker inspect pingbox)

Check you can ping it from outside.

cd ebpf-net-beginners

Comment in the packetdrop target in the Makefile and then make to load the program. Edit and make to drop or pass ICMP packets.

Tracing: cat /sys/kernel/debug/tracing/trace_pipe on host

Listen with nc -l 80 Curl from host with curl -v 172.17.0.2 (use verbose to see the response even if it's not valid HTML)

xdp_liz

Counts packets!

More Repositories

1

containers-from-scratch

Writing a container in a few lines of Go code, as seen at DockerCon 2017 and on O'Reilly Safari
Go
1,445
star
2

ebpf-beginners

The beginner's guide to eBPF
Python
1,175
star
3

learning-ebpf

Learning eBPF, published by O'Reilly - out now! Here's where you'll find a VM config for the examples, and more
C
518
star
4

strace-from-scratch

As seen at Gophercon 2017
Go
300
star
5

libbpfgo-beginners

Basic eBPF examples in Golang using libbpfgo
C
188
star
6

debugger-from-scratch

Go
145
star
7

secure-connections

Simple client and server for showing what's happening with certificates during TLS setup
Go
92
star
8

ebpf-networking

The Beginner's Guide to eBPF Programming for Networking
C
83
star
9

container-security

Resources for the O'Reilly Container Security book
78
star
10

running-with-scissors

Resources from my KubeCon + CloudNativeCon keynote
48
star
11

no-meltdown

Resources for "Don't Have A Meltdown"
Go
36
star
12

hello-container-world

Simple examples for containerizing golang code
Go
18
star
13

containers-and-go

An introduction to containers for Go programmers
17
star
14

kubecon-seattle

KubeCon + CloudNativeCon Seattle
Go
14
star
15

dockerregistry

Gets some Docker registry data and has a look at the layers
Python
8
star
16

force-awakens

Config files for KubeCon Amsterdam demo
Shell
5
star
17

katacoda-scenarios

Katacoda Scenarios
Shell
5
star
18

ebpf-advent

Advent of code in eBPF
C
4
star
19

download-shopify-theme

Command line tool to download Liquid files from a Shopify store's theme
Python
3
star
20

whoisinthehouse

Alexa skill for keeping track of the people in the house
Python
2
star
21

hello-cloudevents

A very quick demonstration of CloudEvents
Go
2
star
22

adventofcode

Can't throw them away!
Go
1
star