Skip to content

Commit

Permalink
Drop PROTO macro syntax and introduce alternative
Browse files Browse the repository at this point in the history
* Feedback given by some external users was that the PROTO syntax was
  opaque and unintuitive. As such, drop that macro and introduce an
  alternative syntax.
* Convert all to use 'cursor_advance' macro API

Signed-off-by: Brenden Blanco <[email protected]>
  • Loading branch information
Brenden Blanco committed Jul 2, 2015
1 parent 307fff0 commit 6ec65e4
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 135 deletions.
24 changes: 14 additions & 10 deletions examples/tc_neighbor_sharing.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -42,14 +44,16 @@ int classify_wan(struct __sk_buff *skb) {
// 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;
Expand Down
86 changes: 46 additions & 40 deletions examples/tunnel_monitor/monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *skb_cursor = 0;

struct ethernet_t *ethernet = bpf_consume_skb(skb_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 = bpf_consume_skb(skb_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 = bpf_consume_skb(skb_cursor, sizeof(*udp));
switch (udp->dport) {
case 4789: goto vxlan;
default: goto finish;
}
EOP:

vxlan: ;
struct vxlan_t *vxlan = bpf_consume_skb(skb_cursor, sizeof(*vxlan));
skb->cb[CB_VNI] = vxlan->key;
skb->cb[CB_OFFSET] = (u64)vxlan + sizeof(*vxlan);
parser.call(skb, 2);

finish:
return 1;
}

Expand All @@ -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 *skb_cursor = (u8 *)(u64)skb->cb[CB_OFFSET];

struct ethernet_t *ethernet = bpf_consume_skb(skb_cursor, sizeof(*ethernet));
switch (ethernet->type) {
case 0x0800: goto ip;
default: goto finish;
}
EOP:
ip: ;
struct ip_t *ip = bpf_consume_skb(skb_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);
Expand Down
12 changes: 6 additions & 6 deletions examples/vlan_learning.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -48,6 +49,5 @@ int handle_virt2phys(struct __sk_buff *skb) {
bpf_clone_redirect(skb, leaf->out_ifindex, 0);
}
}
EOP:
return 1;
}
12 changes: 12 additions & 0 deletions src/cc/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 2 additions & 12 deletions src/cc/export/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
18 changes: 11 additions & 7 deletions src/cc/export/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -90,4 +94,4 @@ struct vxlan_t {
unsigned int rsv3:24;
unsigned int key:24;
unsigned int rsv4:8;
} __attribute__((packed));
} BPF_PACKET_HEADER;
30 changes: 17 additions & 13 deletions tests/cc/test_brb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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;
}

Expand Down
10 changes: 4 additions & 6 deletions tests/cc/test_clang.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ def test_printk(self):
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;
}
"""
Expand Down
Loading

0 comments on commit 6ec65e4

Please sign in to comment.