1// Copyright (c) PLUMgrid, Inc.
2// Licensed under the Apache License, Version 2.0 (the "License")
3// test for packet modification
4
5#packed "false"
6
7struct IPKey {
8  u32 dip:32;
9  u32 sip:32;
10};
11struct IPLeaf {
12  u32 xdip:32;
13  u32 xsip:32;
14  u64 xlated_pkts:64;
15};
16Table<IPKey, IPLeaf, FIXED_MATCH, NONE> xlate(1024);
17
18struct skbuff {
19  u32 type:32;
20};
21
22u32 on_packet (struct skbuff *skb) {
23  u32 ret:32 = 1;
24
25  u32 orig_dip:32 = 0;
26  u32 orig_sip:32 = 0;
27  struct IPLeaf *xleaf;
28
29  goto proto::ethernet;
30
31  state proto::ethernet {
32  }
33
34  state proto::dot1q {
35  }
36
37  state proto::ip {
38    orig_dip = $ip.dst;
39    orig_sip = $ip.src;
40    struct IPKey key = {.dip=orig_dip, .sip=orig_sip};
41    xlate.lookup(key, xleaf) {};
42    on_valid(xleaf) {
43      incr_cksum(@ip.hchecksum, orig_dip, xleaf.xdip);
44      incr_cksum(@ip.hchecksum, orig_sip, xleaf.xsip);
45      // the below are equivalent
46      pkt.rewrite_field($ip.dst, xleaf.xdip);
47      $ip.src = xleaf.xsip;
48      atomic_add(xleaf.xlated_pkts, 1);
49    }
50  }
51
52  state proto::udp {
53    on_valid(xleaf) {
54      incr_cksum(@udp.crc, orig_dip, xleaf.xdip, 1);
55      incr_cksum(@udp.crc, orig_sip, xleaf.xsip, 1);
56    }
57  }
58
59  state proto::tcp {
60    on_valid(xleaf) {
61      incr_cksum(@tcp.cksum, orig_dip, xleaf.xdip, 1);
62      incr_cksum(@tcp.cksum, orig_sip, xleaf.xsip, 1);
63    }
64  }
65
66  state proto::vxlan {
67  }
68
69  state proto::gre {
70  }
71
72  state EOP {
73    return ret;
74  }
75}
76