Commit ae059841 authored by Brenden Blanco's avatar Brenden Blanco

Add support to xdp_drop_count for clsact mode

Signed-off-by: default avatarBrenden Blanco <bblanco@plumgrid.com>
parent 8db68e81
...@@ -8,15 +8,26 @@ ...@@ -8,15 +8,26 @@
# Licensed under the Apache License, Version 2.0 (the "License") # Licensed under the Apache License, Version 2.0 (the "License")
from bcc import BPF from bcc import BPF
import pyroute2
import time import time
import sys import sys
if (len(sys.argv) != 2): if len(sys.argv) != 2:
print("Usage: {0} <ifdev>\n\ne.g.: {0} eth0".format(sys.argv[0])) print("Usage: {0} <ifdev>\n\ne.g.: {0} eth0".format(sys.argv[0]))
exit(1) exit(1)
device = sys.argv[1] device = sys.argv[1]
mode = BPF.XDP
#mode = BPF.SCHED_CLS
if mode == BPF.XDP:
ret = "XDP_DROP"
ctxtype = "xdp_md"
else:
ret = "TC_ACT_SHOT"
ctxtype = "__sk_buff"
# load BPF program # load BPF program
b = BPF(text = """ b = BPF(text = """
#define KBUILD_MODNAME "foo" #define KBUILD_MODNAME "foo"
...@@ -47,7 +58,7 @@ static inline int parse_ipv6(void *data, u64 nh_off, void *data_end) { ...@@ -47,7 +58,7 @@ static inline int parse_ipv6(void *data, u64 nh_off, void *data_end) {
return ip6h->nexthdr; return ip6h->nexthdr;
} }
int xdp_prog1(struct xdp_md *ctx) { int xdp_prog1(struct CTXTYPE *ctx) {
void* data_end = (void*)(long)ctx->data_end; void* data_end = (void*)(long)ctx->data_end;
void* data = (void*)(long)ctx->data; void* data = (void*)(long)ctx->data;
...@@ -55,7 +66,7 @@ int xdp_prog1(struct xdp_md *ctx) { ...@@ -55,7 +66,7 @@ int xdp_prog1(struct xdp_md *ctx) {
struct ethhdr *eth = data; struct ethhdr *eth = data;
// drop packets // drop packets
int rc = XDP_DROP; // let pass XDP_PASS or redirect to tx via XDP_TX int rc = RETURNCODE; // let pass XDP_PASS or redirect to tx via XDP_TX
long *value; long *value;
uint16_t h_proto; uint16_t h_proto;
uint64_t nh_off = 0; uint64_t nh_off = 0;
...@@ -100,12 +111,21 @@ int xdp_prog1(struct xdp_md *ctx) { ...@@ -100,12 +111,21 @@ int xdp_prog1(struct xdp_md *ctx) {
return rc; return rc;
} }
""", cflags=["-w"]) """, cflags=["-w", "-DRETURNCODE=%s" % ret, "-DCTXTYPE=%s" % ctxtype])
xdp_func = b.load_func("xdp_prog1", BPF.XDP) fn = b.load_func("xdp_prog1", mode)
b.attach_xdp(device, xdp_func)
dropcnt = b.get_table("dropcnt")
if mode == BPF.XDP:
b.attach_xdp(device, fn)
else:
ip = pyroute2.IPRoute()
ipdb = pyroute2.IPDB(nl=ip)
idx = ipdb.interfaces[device].index
ip.tc("add", "clsact", idx)
ip.tc("add-filter", "bpf", idx, ":1", fd=fn.fd, name=fn.name,
parent="ffff:fff2", classid=1, direct_action=True)
dropcnt = b.get_table("dropcnt")
prev = [0] * 256 prev = [0] * 256
print("Printing drops per IP protocol-number, hit CTRL+C to stop") print("Printing drops per IP protocol-number, hit CTRL+C to stop")
while 1: while 1:
...@@ -122,4 +142,8 @@ while 1: ...@@ -122,4 +142,8 @@ while 1:
print("Removing filter from device") print("Removing filter from device")
break; break;
b.remove_xdp(device) if mode == BPF.XDP:
b.remove_xdp(device)
else:
ip.tc("del", "clsact", idx)
ipdb.release()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment