-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from plumgrid/issue16_cleanups
Address comments and add tail_call example
- Loading branch information
Showing
11 changed files
with
138 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "../../src/cc/bpf_helpers.h" | ||
|
||
BPF_TABLE("prog", int, int, jump, 64); | ||
BPF_TABLE("array", int, u64, stats, 64); | ||
|
||
enum states { | ||
S_EOP = 1, | ||
S_ETHER, | ||
S_ARP, | ||
S_IP | ||
}; | ||
|
||
BPF_EXPORT(parse_ether) | ||
int parse_ether(struct __sk_buff *skb) { | ||
size_t cur = 0; | ||
size_t next = cur + 14; | ||
|
||
int key = S_ETHER; | ||
u64 *leaf = stats.get(&key); | ||
if (leaf) (*leaf)++; | ||
|
||
switch (bpf_dext_pkt(skb, cur + 12, 0, 16)) { | ||
case 0x0800: jump.call(skb, S_IP); | ||
case 0x0806: jump.call(skb, S_ARP); | ||
} | ||
jump.call(skb, S_EOP); | ||
return 0; | ||
} | ||
|
||
BPF_EXPORT(parse_arp) | ||
int parse_arp(struct __sk_buff *skb) { | ||
size_t cur = 14; // TODO: get from ctx | ||
size_t next = cur + 28; | ||
|
||
int key = S_ARP; | ||
u64 *leaf = stats.get(&key); | ||
if (leaf) (*leaf)++; | ||
|
||
jump.call(skb, S_EOP); | ||
return 0; | ||
} | ||
|
||
BPF_EXPORT(parse_ip) | ||
int parse_ip(struct __sk_buff *skb) { | ||
size_t cur = 14; // TODO: get from ctx | ||
size_t next = cur + 20; | ||
|
||
int key = S_IP; | ||
u64 *leaf = stats.get(&key); | ||
if (leaf) (*leaf)++; | ||
|
||
jump.call(skb, S_EOP); | ||
return 0; | ||
} | ||
|
||
BPF_EXPORT(eop) | ||
int eop(struct __sk_buff *skb) { | ||
int key = S_EOP; | ||
u64 *leaf = stats.get(&key); | ||
if (leaf) (*leaf)++; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#!/usr/bin/env python | ||
|
||
from ctypes import c_ushort, c_int, c_ulonglong | ||
from netaddr import IPAddress | ||
from bpf import BPF | ||
from socket import socket, AF_INET, SOCK_DGRAM | ||
import sys | ||
from time import sleep | ||
from unittest import main, TestCase | ||
|
||
arg1 = sys.argv.pop(1) | ||
|
||
S_EOP = 1 | ||
S_ETHER = 2 | ||
S_ARP = 3 | ||
S_IP = 4 | ||
|
||
class TestBPFSocket(TestCase): | ||
def setUp(self): | ||
b = BPF(dp_file=arg1, debug=0) | ||
ether_fn = b.load_func("parse_ether", BPF.SCHED_CLS) | ||
arp_fn = b.load_func("parse_arp", BPF.SCHED_CLS) | ||
ip_fn = b.load_func("parse_ip", BPF.SCHED_CLS) | ||
eop_fn = b.load_func("eop", BPF.SCHED_CLS) | ||
BPF.attach_classifier(ether_fn, "eth0") | ||
self.jump = b.get_table("jump", c_int, c_int) | ||
self.jump.put(c_int(S_ARP), c_int(arp_fn.fd)) | ||
self.jump.put(c_int(S_IP), c_int(ip_fn.fd)) | ||
self.jump.put(c_int(S_EOP), c_int(eop_fn.fd)) | ||
self.stats = b.get_table("stats", c_int, c_ulonglong) | ||
|
||
def test_jumps(self): | ||
udp = socket(AF_INET, SOCK_DGRAM) | ||
udp.sendto(b"a" * 10, ("172.16.1.1", 5000)) | ||
self.assertGreater(self.stats.get(c_int(S_IP)).value, 0) | ||
self.assertGreater(self.stats.get(c_int(S_ARP)).value, 0) | ||
self.assertGreater(self.stats.get(c_int(S_EOP)).value, 1) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters