Drop PROTO macro syntax and introduce alternative
* 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 <bblanco@plumgrid.com>
diff --git a/examples/tc_neighbor_sharing.c b/examples/tc_neighbor_sharing.c
index 87ea0ea..8d1c3bf 100644
--- a/examples/tc_neighbor_sharing.c
+++ b/examples/tc_neighbor_sharing.c
@@ -19,14 +19,16 @@
// 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 @@
// 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;
diff --git a/examples/tunnel_monitor/monitor.c b/examples/tunnel_monitor/monitor.c
index 6401f3f..8b17078 100644
--- a/examples/tunnel_monitor/monitor.c
+++ b/examples/tunnel_monitor/monitor.c
@@ -57,37 +57,43 @@
// 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;
+ 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(ip) {
- skb->cb[CB_SIP] = ip->src;
- skb->cb[CB_DIP] = ip->dst;
- switch (ip->nextp) {
- case 17: goto udp;
- }
- 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(udp) {
- switch (udp->dport) {
- case 4789: goto vxlan;
- }
- goto EOP;
+
+udp: ;
+ struct udp_t *udp = bpf_consume_skb(skb_cursor, sizeof(*udp));
+ switch (udp->dport) {
+ case 4789: goto vxlan;
+ 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;
- }
-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;
}
@@ -100,19 +106,19 @@
.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;
+ 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;
}
- PROTO(ip) {
- key.inner_sip = ip->src;
- key.inner_dip = ip->dst;
- goto EOP;
- }
-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);
diff --git a/examples/vlan_learning.c b/examples/vlan_learning.c
index ae77a8b..74115df 100644
--- a/examples/vlan_learning.c
+++ b/examples/vlan_learning.c
@@ -16,8 +16,9 @@
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 @@
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 @@
bpf_clone_redirect(skb, leaf->out_ifindex, 0);
}
}
-EOP:
return 1;
}