Skip to content

Instantly share code, notes, and snippets.

@summerwind
Created May 30, 2018 08:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save summerwind/9dcd3b059f5087fa579afb73ba935eb8 to your computer and use it in GitHub Desktop.
Save summerwind/9dcd3b059f5087fa579afb73ba935eb8 to your computer and use it in GitHub Desktop.
Generic XDP on Ubuntu 18.04

Generic XDP on Ubuntu 18.04

Start VM instance with Vagrant.

$ vagrant up
$ vagrant ssh

Install packages.

$ sudo apt update
$ sudo apt install clang iproute2

Prevent 'asm/types.h' file not found error.

$ sudo ln -s /usr/include/x86_64-linux-gnu/asm/ /usr/include/asm

Compile XDP program with clang.

$ clang -O2 -Wall -target bpf -c xdp_drop.c -o xdp_drop.o

Attach XDP program to enp0s8.

$ sudo ip link set dev enp0s8 xdp obj xdp_drop.o
$ sudo ip link show dev enp0s8

Detach XDP program from enp0s8.

$ sudo ip link set dev enp0s8 xdp off
Vagrant.configure('2') do |config|
config.vm.hostname = "xdp"
config.vm.box = "ubuntu/bionic64"
config.vm.network :private_network, ip: "192.168.10.10"
config.vm.synced_folder "./", "/vagrant"
config.vm.provider :virtualbox do |v|
v.name = "xdp"
v.cpus = 2
v.memory = 1024
v.customize ["modifyvm", :id, "--uartmode1", "disconnected"]
end
end
#include <linux/bpf.h>
#define SEC(NAME) __attribute__((section(NAME), used))
SEC("prog")
int xdp_drop(struct xdp_md *ctx)
{
return XDP_DROP;
}
char __license[] SEC("license") = "GPL";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment