Skip to content

Instantly share code, notes, and snippets.

@williamtu
williamtu / .sh
Last active December 29, 2023 07:01
OVS AFXDP script
rm -f /usr/local/etc/openvswitch/conf.db
ovsdb-tool create /usr/local/etc/openvswitch/conf.db /root/ovs/vswitchd/vswitch.ovsschema
ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock \
--remote=db:Open_vSwitch,Open_vSwitch,manager_options \
--pidfile --detach
if [ "$1" == "gdb" ]; then
gdb -ex=r --args ovs-vswitchd --no-chdir --pidfile --log-file=/root/ovs/ovs-vswitchd.log -vvconn -vofproto_dpif -vunixctl --disable-system
@williamtu
williamtu / gist:438477daedc3823ac8882f9110fb2cc4
Last active September 6, 2023 13:35
create tun/tap device and read/write
cat tunclient.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
@williamtu
williamtu / gist:b9ac87419a48ba6d793e490b49b967e7
Last active August 15, 2018 23:14
OOM kill ovs with af-xdp
[64701.441424] xsk_generic_xmit call direct xmit to ovs-p0
[64701.441440] xsk_generic_xmit done
[64717.199367] ovs-vswitchd invoked oom-killer: gfp_mask=0x6200ca(GFP_HIGHUSER_MOVABLE), nodemask=(null), order=0, oom_score_adj=0
[64717.199369] ovs-vswitchd cpuset=/ mems_allowed=0
[64717.199373] CPU: 2 PID: 1412 Comm: ovs-vswitchd Tainted: G L 4.18.0-rc6+ #156
[64717.199373] Hardware name: VMware, Inc. VMware Virtual Platform/440BX Desktop Reference Platform, BIOS 6.00 05/19/2017
[64717.199374] Call Trace:
[64717.200092] dump_stack+0x5c/0x7b
[64717.200228] dump_header+0x6b/0x27c
[64717.200830] oom_kill_process+0x239/0x440
@williamtu
williamtu / failed case
Last active April 19, 2017 22:29
BPF array initialization
make[1]: Leaving directory '/root/net-next'
bpf_load_program() err=13
0: (bf) r6 = r1
1: (30) r0 = *(u8 *)skb[23]
2: (63) *(u32 *)(r10 -4) = r0
3: (18) r1 = 0x9ffe7000
5: (79) r2 = *(u64 *)(r1 +8)
R1 invalid mem access 'map_ptr'
---
@williamtu
williamtu / volatile-xdp9.c
Last active April 14, 2017 18:41
xdp9.c (max BPF stack > 512), see 595: r5 = *(u64 *)(r10 - 600)
diff --git a/tmp/xdp9.c b/tests/xdp9.c
index 0845fd5..9964d15 100644
--- a/tmp/xdp9.c
+++ b/tests/xdp9.c
@@ -1,4 +1,4 @@
-/* Automatically generated by p4c-xdp from xdp9.p4 on Fri Apr 14 10:34:12 2017
+/* Automatically generated by p4c-xdp from xdp9.p4 on Fri Apr 14 11:14:09 2017
*/
#include "xdp9.h"
#define KBUILD_MODNAME "xdptest"
@williamtu
williamtu / xdp12.c
Created February 17, 2017 16:39
xdp12 Verifier: cannot add integer value with 0 upper zero bits to ptr_to_packet
/* Automatically generated by p4c-xdp from xdp12.p4 on Fri Feb 17 08:32:10 2017
*/
#include "xdp12.h"
#define KBUILD_MODNAME "xdptest"
#include <linux/bpf.h>
#include "bpf_helpers.h"
#define load_byte(data, b) (*(((u8*)(data)) + (b)))
#define load_half(data, b) __constant_ntohs(*(u16 *)((u8*)(data) + (b)))
#define load_word(data, b) __constant_ntohl(*(u32 *)((u8*)(data) + (b)))
@williamtu
williamtu / xdp_tx_tunnel_kern.c
Last active February 14, 2017 02:03
xdp tx tunnel fails verifier
root@osboxes:~/net-next/samples/bpf# ./xdp_tx_iptunnel -i 2 -a 10.1.1.1 -p 10.2.2.2 -s 10.2.2.9 -d 10.1.2.3 -m 1:2:3:4:5:6
bpf_load_program() err=13
0: (b7) r2 = 1
1: (bf) r3 = r1
2: (07) r3 += 4
3: (bf) r4 = r1
4: (bf) r5 = r1
5: (61) r0 = *(u32 *)(r1 +4)
6: (61) r1 = *(u32 *)(r1 +0)
7: (bf) r6 = r1
@williamtu
williamtu / xdp7.S
Created February 8, 2017 20:20
xdp7.c fails at 451: (7b) *(u64 *)(r10 -528) = r0 invalid stack off=-528 size=8
xdp7.o: file format ELF64-BPF
Disassembly of section prog:
ebpf_filter:
; int ebpf_filter(struct xdp_md* skb){
0: r6 = r1
; void* ebpf_packetEnd = ((void*)(long)skb->data_end);
1: r3 = *(u32 *)(r6 + 4)
; void* ebpf_packetStart = ((void*)(long)skb->data);
Map 'ebpf_outTable' loaded as pinned!
Map 'ebpf_outTable' (5) injected into prog section 'prog' at offset 295!
Prog section 'prog' loaded (6)!
- Type: 6
- Instructions: 304 (0 over limit)
- License: GPL
Verifier analysis:
@williamtu
williamtu / xdp3.S
Created February 3, 2017 03:15
xdp3.S
xdp3.o: file format ELF64-BPF
Disassembly of section prog:
ebpf_filter:
; int ebpf_filter(struct xdp_md* skb){
0: r6 = r1
; void* ebpf_packetEnd = ((void*)(long)skb->data_end);
1: r4 = *(u32 *)(r6 + 4)
; void* ebpf_packetStart = ((void*)(long)skb->data);