Commit 87697aea authored by 4ast's avatar 4ast

Merge pull request #78 from iovisor/bblanco_dev

Drop PROTO macro syntax and introduce alternative
parents 307fff0f f2ea631f
......@@ -19,14 +19,16 @@ int pass(struct __sk_buff *skb) {
// returns: > 0 when an IP is known
// = 0 when an IP is not known, or non-IP traffic
int classify_wan(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
u8 *cursor = 0;
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
case ETH_P_IP: goto ip;
default: goto EOP;
}
goto EOP;
}
PROTO(ip) {
ip: {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
u32 dip = ip->dst;
struct ipkey key = {.client_ip=dip};
int *val = learned_ips.lookup(&key);
......@@ -42,14 +44,16 @@ EOP:
// Mark the inserted entry with a non-zero value to be used by the classify_wan
// lookup.
int classify_neighbor(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
u8 *cursor = 0;
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
case ETH_P_IP: goto ip;
default: goto EOP;
}
goto EOP;
}
PROTO(ip) {
ip: {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
u32 sip = ip->src;
struct ipkey key = {.client_ip=sip};
int val = 1;
......
......@@ -57,37 +57,43 @@ int handle_egress(struct __sk_buff *skb) {
// parse the outer vxlan frame
int handle_outer(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
// filter bcast/mcast from the stats
if (ethernet->dst & (1ull << 40))
return 1;
switch (ethernet->type) {
case 0x0800: goto ip;
}
goto EOP;
}
PROTO(ip) {
skb->cb[CB_SIP] = ip->src;
skb->cb[CB_DIP] = ip->dst;
switch (ip->nextp) {
case 17: goto udp;
}
goto EOP;
u8 *cursor = 0;
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
// filter bcast/mcast from the stats
if (ethernet->dst & (1ull << 40))
goto finish;
switch (ethernet->type) {
case 0x0800: goto ip;
default: goto finish;
}
PROTO(udp) {
switch (udp->dport) {
case 4789: goto vxlan;
}
goto EOP;
ip: ;
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
skb->cb[CB_SIP] = ip->src;
skb->cb[CB_DIP] = ip->dst;
switch (ip->nextp) {
case 17: goto udp;
default: goto finish;
}
PROTO(vxlan) {
skb->cb[CB_VNI] = vxlan->key;
skb->cb[CB_OFFSET] = (u64)vxlan + sizeof(*vxlan);
parser.call(skb, 2);
goto EOP;
udp: ;
struct udp_t *udp = cursor_advance(cursor, sizeof(*udp));
switch (udp->dport) {
case 4789: goto vxlan;
default: goto finish;
}
EOP:
vxlan: ;
struct vxlan_t *vxlan = cursor_advance(cursor, sizeof(*vxlan));
skb->cb[CB_VNI] = vxlan->key;
skb->cb[CB_OFFSET] = (u64)vxlan + sizeof(*vxlan);
parser.call(skb, 2);
finish:
return 1;
}
......@@ -100,19 +106,19 @@ int handle_inner(struct __sk_buff *skb) {
.outer_sip = skb->cb[CB_SIP],
.outer_dip = skb->cb[CB_DIP]
};
BEGIN_OFFSET(ethernet, skb->cb[CB_OFFSET]);
PROTO(ethernet) {
switch (ethernet->type) {
case 0x0800: goto ip;
}
goto EOP;
}
PROTO(ip) {
key.inner_sip = ip->src;
key.inner_dip = ip->dst;
goto EOP;
u8 *cursor = (u8 *)(u64)skb->cb[CB_OFFSET];
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
default: goto finish;
}
EOP:
ip: ;
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
key.inner_sip = ip->src;
key.inner_dip = ip->dst;
finish:
// consistent ordering
if (key.outer_dip < key.outer_sip)
swap_ipkey(&key);
......
......@@ -16,8 +16,9 @@ BPF_TABLE("hash", int, struct ifindex_leaf_t, egress, 4096);
BPF_TABLE("hash", u64, struct ifindex_leaf_t, ingress, 4096);
int handle_phys2virt(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
u8 *cursor = 0;
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
u64 src_mac = ethernet->src;
struct ifindex_leaf_t *leaf = ingress.lookup(&src_mac);
if (leaf) {
......@@ -33,13 +34,13 @@ int handle_phys2virt(struct __sk_buff *skb) {
bpf_clone_redirect(skb, leaf->out_ifindex, 0);
}
}
EOP:
return 1;
}
int handle_virt2phys(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
u8 *cursor = 0;
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
int src_ifindex = skb->ifindex;
struct ifindex_leaf_t *leaf = egress.lookup(&src_ifindex);
if (leaf) {
......@@ -48,6 +49,5 @@ int handle_virt2phys(struct __sk_buff *skb) {
bpf_clone_redirect(skb, leaf->out_ifindex, 0);
}
}
EOP:
return 1;
}
......@@ -381,6 +381,18 @@ bool BTypeVisitor::VisitVarDecl(VarDecl *Decl) {
return false;
}
tables_[Decl->getName()] = std::move(table);
} else if (const PointerType *P = Decl->getType()->getAs<PointerType>()) {
// if var is a pointer to a packet type, clone the annotation into the var
// decl so that the packet dext/dins rewriter can catch it
if (const RecordType *RT = P->getPointeeType()->getAs<RecordType>()) {
if (const RecordDecl *RD = RT->getDecl()->getDefinition()) {
if (DeprecatedAttr *DA = RD->getAttr<DeprecatedAttr>()) {
if (DA->getMessage() == "packet") {
Decl->addAttr(DA->clone(C));
}
}
}
}
}
return true;
}
......
......@@ -42,18 +42,8 @@ __attribute__((section("maps/" _table_type))) \
struct _name##_table_t _name
// packet parsing state machine helpers
#define BEGIN(next) \
u64 _parse_cursor = 0; \
goto next
#define BEGIN_OFFSET(next, _base_offset) \
u64 _parse_cursor = (_base_offset); \
goto next
#define PROTO(name) \
goto EOP; \
name: ; \
struct name##_t *name __attribute__((deprecated("packet"))) = (void *)_parse_cursor; \
_parse_cursor += sizeof(*name);
#define cursor_advance(_cursor, _len) \
({ void *_tmp = _cursor; _cursor += _len; _tmp; })
char _license[4] SEC("license") = "GPL";
......
......@@ -14,18 +14,22 @@
* limitations under the License.
*/
#include <uapi/linux/if_ether.h>
#define BPF_PACKET_HEADER __attribute__((packed)) __attribute__((deprecated("packet")))
struct ethernet_t {
unsigned long long dst:48;
unsigned long long src:48;
unsigned int type:16;
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct dot1q_t {
unsigned short pri:3;
unsigned short cfi:1;
unsigned short vlanid:12;
unsigned short type;
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct arp_t {
unsigned short htype;
......@@ -37,7 +41,7 @@ struct arp_t {
unsigned long long spa:32;
unsigned long long tha:48;
unsigned int tpa;
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct ip_t {
unsigned char ver:4; // byte 0
......@@ -54,14 +58,14 @@ struct ip_t {
unsigned short hchecksum;
unsigned int src; // byte 12
unsigned int dst; // byte 16
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct udp_t {
unsigned short sport;
unsigned short dport;
unsigned short length;
unsigned short crc;
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct tcp_t {
unsigned short src_port; // byte 0
......@@ -81,7 +85,7 @@ struct tcp_t {
unsigned short rcv_wnd;
unsigned short cksum; // byte 16
unsigned short urg_ptr;
} __attribute__((packed));
} BPF_PACKET_HEADER;
struct vxlan_t {
unsigned int rsv1:4;
......@@ -90,4 +94,4 @@ struct vxlan_t {
unsigned int rsv3:24;
unsigned int key:24;
unsigned int rsv4:8;
} __attribute__((packed));
} BPF_PACKET_HEADER;
......@@ -106,6 +106,7 @@ int pem(struct __sk_buff *skb) {
static int br_common(struct __sk_buff *skb, int which_br) __attribute__((always_inline));
static int br_common(struct __sk_buff *skb, int which_br) {
u8 *cursor = 0;
bpf_metadata_t meta = {};
u16 proto;
u16 arpop;
......@@ -126,8 +127,8 @@ static int br_common(struct __sk_buff *skb, int which_br) {
meta.rx_port_id = skb->cb[1];
}
BEGIN(ethernet);
PROTO(ethernet) {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
{
dmac.addr = ethernet->dst;
if (meta.prog_id != 0) {
/* send to the router */
......@@ -152,22 +153,24 @@ static int br_common(struct __sk_buff *skb, int which_br) {
}
switch (ethernet->type) {
case 0x0800: goto ip;
case 0x0806: goto arp;
case 0x8100: goto dot1q;
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
case ETH_P_8021Q: goto dot1q;
default: goto EOP;
}
goto EOP;
}
PROTO(dot1q) {
dot1q: {
struct dot1q_t *dot1q = cursor_advance(cursor, sizeof(*dot1q));
switch(dot1q->type) {
case 0x0806: goto arp;
case 0x0800: goto ip;
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
default: goto EOP;
}
goto EOP;
}
PROTO(arp) {
arp: {
struct arp_t *arp = cursor_advance(cursor, sizeof(*arp));
/* mac learning */
arpop = arp->oper;
if (arpop == 2) {
......@@ -180,7 +183,7 @@ static int br_common(struct __sk_buff *skb, int which_br) {
__u32 ifindex = *rtrif_p;
eth_addr_t smac;
smac.addr = ethernet->src;
smac.addr = ethernet->src;
if (which_br == 1)
br1_mac_ifindex.update(&smac, &ifindex);
else
......@@ -190,7 +193,8 @@ static int br_common(struct __sk_buff *skb, int which_br) {
goto xmit;
}
PROTO(ip) {
ip: {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
goto xmit;
}
......
......@@ -13,12 +13,10 @@ class TestClang(TestCase):
text = """
#include <bcc/proto.h>
int handle_packet(void *ctx) {
BEGIN(ethernet);
PROTO(ethernet) {
bpf_trace_printk("ethernet->dst = %llx, ethernet->src = %llx\\n",
ethernet->dst, ethernet->src);
}
EOP:
u8 *cursor = 0;
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
bpf_trace_printk("ethernet->dst = %llx, ethernet->src = %llx\\n",
ethernet->dst, ethernet->src);
return 0;
}
"""
......
......@@ -42,6 +42,7 @@ BPF_TABLE("hash", struct SlaveKey, struct SlaveLeaf, slave_map, 10);
int handle_packet(struct __sk_buff *skb) {
int ret = 0;
u8 *cursor = 0;
if (skb->pkt_type == 0) {
// tx
......@@ -70,26 +71,25 @@ int handle_packet(struct __sk_buff *skb) {
ret = 0;
}
BEGIN(ethernet);
PROTO(ethernet) {
switch (ethernet->type) {
case 0x0806: goto arp;
case 0x0800: goto ip;
case 0x8100: goto dot1q;
}
goto EOP;
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
case ETH_P_8021Q: goto dot1q;
default: goto EOP;
}
PROTO(dot1q) {
dot1q: {
struct dot1q_t *dot1q = cursor_advance(cursor, sizeof(*dot1q));
switch (dot1q->type) {
case 0x0806: goto arp;
case 0x0800: goto ip;
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
default: goto EOP;
}
goto EOP;
}
PROTO(arp) {
arp: {
struct arp_t *arp = cursor_advance(cursor, sizeof(*arp));
if (skb->pkt_type) {
if (arp->oper == 1) {
struct MacaddrKey mac_key = {.ip=arp->spa};
......@@ -100,17 +100,20 @@ int handle_packet(struct __sk_buff *skb) {
goto EOP;
}
PROTO(ip) {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
ip: {
switch (ip->nextp) {
case 6: goto tcp;
case 17: goto udp;
default: goto EOP;
}
goto EOP;
}
PROTO(tcp) {
tcp: {
struct tcp_t *tcp = cursor_advance(cursor, sizeof(*tcp));
goto EOP;
}
PROTO(udp) {
udp: {
struct udp_t *udp = cursor_advance(cursor, sizeof(*udp));
if (udp->dport != 5000) {
goto EOP;
}
......
......@@ -15,20 +15,26 @@ struct IPLeaf {
BPF_TABLE("hash", struct IPKey, struct IPLeaf, stats, 256);
int on_packet(struct __sk_buff *skb) {
BEGIN(ethernet);
PROTO(ethernet) {
u8 *cursor = 0;
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
case 0x8100: goto dot1q;
case ETH_P_IP: goto ip;
case ETH_P_8021Q: goto dot1q;
default: goto EOP;
}
}
PROTO(dot1q) {
dot1q: {
struct dot1q_t *dot1q = cursor_advance(cursor, sizeof(*dot1q));
switch (dot1q->type) {
case 0x0800: goto ip;
case ETH_P_8021Q: goto ip;
default: goto EOP;
}
}
PROTO(ip) {
ip: {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
int rx = 0, tx = 0;
struct IPKey key;
if (ip->dst > ip->src) {
......@@ -45,6 +51,7 @@ int on_packet(struct __sk_buff *skb) {
lock_xadd(&leaf->rx_pkts, rx);
lock_xadd(&leaf->tx_pkts, tx);
}
EOP:
return 0;
}
......@@ -14,29 +14,33 @@ struct IPLeaf {
BPF_TABLE("hash", struct IPKey, struct IPLeaf, xlate, 1024);
int on_packet(struct __sk_buff *skb) {
u8 *cursor = 0;
u32 orig_dip = 0;
u32 orig_sip = 0;
struct IPLeaf *xleaf;
BEGIN(ethernet);
PROTO(ethernet) {
ethernet: {
struct ethernet_t *ethernet = cursor_advance(cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
case 0x0806: goto arp;
case 0x8100: goto dot1q;
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
case ETH_P_8021Q: goto dot1q;
default: goto EOP;
}
goto EOP;
}
PROTO(dot1q) {
dot1q: {
struct dot1q_t *dot1q = cursor_advance(cursor, sizeof(*dot1q));
switch (dot1q->type) {
case 0x0806: goto arp;
case 0x0800: goto ip;
case ETH_P_IP: goto ip;
case ETH_P_ARP: goto arp;
default: goto EOP;
}
goto EOP;
}
PROTO(arp) {
arp: {
struct arp_t *arp = cursor_advance(cursor, sizeof(*arp));
orig_dip = arp->tpa;
orig_sip = arp->spa;
struct IPKey key = {.dip=orig_dip, .sip=orig_sip};
......@@ -49,7 +53,8 @@ int on_packet(struct __sk_buff *skb) {
goto EOP;
}
PROTO(ip) {
ip: {
struct ip_t *ip = cursor_advance(cursor, sizeof(*ip));
orig_dip = ip->dst;
orig_sip = ip->src;
struct IPKey key = {.dip=orig_dip, .sip=orig_sip};
......@@ -64,11 +69,12 @@ int on_packet(struct __sk_buff *skb) {
switch (ip->nextp) {
case 6: goto tcp;
case 17: goto udp;
default: goto EOP;
}
goto EOP;
}
PROTO(udp) {
udp: {
struct udp_t *udp = cursor_advance(cursor, sizeof(*udp));
if (xleaf) {
incr_cksum_l4(&udp->crc, orig_dip, xleaf->xdip, 1);
incr_cksum_l4(&udp->crc, orig_sip, xleaf->xsip, 1);
......@@ -76,7 +82,8 @@ int on_packet(struct __sk_buff *skb) {
goto EOP;
}
PROTO(tcp) {
tcp: {
struct tcp_t *tcp = cursor_advance(cursor, sizeof(*tcp));
if (xleaf) {
incr_cksum_l4(&tcp->cksum, orig_dip, xleaf->xdip, 1);
incr_cksum_l4(&tcp->cksum, orig_sip, xleaf->xsip, 1);
......
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