File: test_call1.c

package info (click to toggle)
bpfcc 0.18.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,368 kB
  • sloc: ansic: 132,727; python: 36,226; cpp: 26,973; sh: 710; yacc: 525; makefile: 141; lex: 94
file content (59 lines) | stat: -rw-r--r-- 1,118 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright (c) PLUMgrid, Inc.
// Licensed under the Apache License, Version 2.0 (the "License")

BPF_PROG_ARRAY(jump, 64);
BPF_ARRAY(stats, u64, 64);

enum states {
  S_EOP = 1,
  S_ETHER,
  S_ARP,
  S_IP
};

int parse_ether(struct __sk_buff *skb) {
  size_t cur = 0;
  size_t next = cur + 14;

  int key = S_ETHER;
  u64 *leaf = stats.lookup(&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 1;
}

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.lookup(&key);
  if (leaf) (*leaf)++;

  jump.call(skb, S_EOP);
  return 1;
}

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.lookup(&key);
  if (leaf) (*leaf)++;

  jump.call(skb, S_EOP);
  return 1;
}

int eop(struct __sk_buff *skb) {
  int key = S_EOP;
  u64 *leaf = stats.lookup(&key);
  if (leaf) (*leaf)++;
  return 1;
}