xref: /aosp_15_r20/external/bcc/tests/python/test_call1.c (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1 // Copyright (c) PLUMgrid, Inc.
2 // Licensed under the Apache License, Version 2.0 (the "License")
3 
4 BPF_PROG_ARRAY(jump, 64);
5 BPF_ARRAY(stats, u64, 64);
6 
7 enum states {
8   S_EOP = 1,
9   S_ETHER,
10   S_ARP,
11   S_IP
12 };
13 
parse_ether(struct __sk_buff * skb)14 int parse_ether(struct __sk_buff *skb) {
15   size_t cur = 0;
16   size_t next = cur + 14;
17 
18   int key = S_ETHER;
19   u64 *leaf = stats.lookup(&key);
20   if (leaf) (*leaf)++;
21 
22   switch (bpf_dext_pkt(skb, cur + 12, 0, 16)) {
23     case 0x0800: jump.call(skb, S_IP);
24     case 0x0806: jump.call(skb, S_ARP);
25   }
26   jump.call(skb, S_EOP);
27   return 1;
28 }
29 
parse_arp(struct __sk_buff * skb)30 int parse_arp(struct __sk_buff *skb) {
31   size_t cur = 14;  // TODO: get from ctx
32   size_t next = cur + 28;
33 
34   int key = S_ARP;
35   u64 *leaf = stats.lookup(&key);
36   if (leaf) (*leaf)++;
37 
38   jump.call(skb, S_EOP);
39   return 1;
40 }
41 
parse_ip(struct __sk_buff * skb)42 int parse_ip(struct __sk_buff *skb) {
43   size_t cur = 14;  // TODO: get from ctx
44   size_t next = cur + 20;
45 
46   int key = S_IP;
47   u64 *leaf = stats.lookup(&key);
48   if (leaf) (*leaf)++;
49 
50   jump.call(skb, S_EOP);
51   return 1;
52 }
53 
eop(struct __sk_buff * skb)54 int eop(struct __sk_buff *skb) {
55   int key = S_EOP;
56   u64 *leaf = stats.lookup(&key);
57   if (leaf) (*leaf)++;
58   return 1;
59 }
60