-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathdigest.p4
154 lines (130 loc) · 3.44 KB
/
digest.p4
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* -*- P4_16 -*- */
#include <core.p4>
#include <tc/pna.p4>
#define L3_TABLE_SIZE 2048
/*
* CONST VALUES FOR TYPES
*/
const bit<8> IP_PROTO_TCP = 0x06;
const bit<16> ETHERTYPE_IPV4 = 0x0800;
/*
* Standard ethernet header
*/
header ethernet_t {
@tc_type("macaddr") bit<48> dstAddr;
@tc_type("macaddr") bit<48> srcAddr;
bit<16> etherType;
}
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
@tc_type("ipv4") bit<32> srcAddr;
@tc_type("ipv4") bit<32> dstAddr;
}
struct my_ingress_headers_t {
ethernet_t ethernet;
ipv4_t ipv4;
}
/****** G L O B A L I N G R E S S M E T A D A T A *********/
struct my_ingress_metadata_t {
@tc_type("dev") PortId_t ingress_port;
bool send_digest;
}
/*********************** P A R S E R **************************/
parser Ingress_Parser(
packet_in pkt,
out my_ingress_headers_t hdr,
inout my_ingress_metadata_t meta,
in pna_main_parser_input_metadata_t istd)
{
state start {
transition parse_ethernet;
}
state parse_ethernet {
pkt.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
ETHERTYPE_IPV4: parse_ipv4;
default: reject;
}
}
state parse_ipv4 {
pkt.extract(hdr.ipv4);
transition select(hdr.ipv4.protocol) {
IP_PROTO_TCP : accept;
default: reject;
}
}
}
struct mac_learn_digest_t {
@tc_type("macaddr") bit<48> srcAddr;
@tc_type("dev") PortId_t ingress_port;
};
/***************** M A T C H - A C T I O N *********************/
control ingress(
inout my_ingress_headers_t hdr,
inout my_ingress_metadata_t meta,
in pna_main_input_metadata_t istd,
inout pna_main_output_metadata_t ostd
)
{
action send_nh(@tc_type("dev") PortId_t port, @tc_type("macaddr") bit<48> srcMac, @tc_type("macaddr") bit<48> dstMac) {
hdr.ethernet.srcAddr = srcMac;
hdr.ethernet.dstAddr = dstMac;
meta.ingress_port = istd.input_port;
meta.send_digest = true;
send_to_port(port);
}
action drop() {
drop_packet();
}
table nh_table {
key = {
hdr.ipv4.dstAddr : exact @tc_type("ipv4") @name("dstAddr");
}
actions = {
send_nh;
drop;
}
size = L3_TABLE_SIZE;
const default_action = drop;
}
apply {
meta.send_digest = false;
if (hdr.ipv4.isValid()) {
nh_table.apply();
}
}
}
/********************* D E P A R S E R ************************/
control Ingress_Deparser(
packet_out pkt,
inout my_ingress_headers_t hdr,
in my_ingress_metadata_t meta,
in pna_main_output_metadata_t ostd)
{
Digest<mac_learn_digest_t>() digest_inst;
apply {
if (meta.send_digest) {
mac_learn_digest_t mac_learn_digest;
mac_learn_digest.srcAddr = hdr.ethernet.srcAddr;
mac_learn_digest.ingress_port = meta.ingress_port;
digest_inst.pack(mac_learn_digest);
}
pkt.emit(hdr.ethernet);
pkt.emit(hdr.ipv4);
}
}
/************ F I N A L P A C K A G E ******************************/
PNA_NIC(
Ingress_Parser(),
ingress(),
Ingress_Deparser()
) main;