-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter_lab.p4
executable file
·182 lines (154 loc) · 3.41 KB
/
meter_lab.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (C) 2016, Netronome Systems, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#define ETHERTYPE_VLAN 0x8100
#define ETHERTYPE_IPV4 0x0800
/*
* Header declarations
*/
header_type ethernet_t {
fields {
dstAddr : 48;
srcAddr : 48;
etherType : 16;
}
}
header_type vlan_t {
fields {
pcp : 3;
cfi : 1;
vid : 12;
etherType : 16;
}
}
header_type ipv4_t {
fields {
version : 4;
ihl : 4;
diffserv : 8;
totalLen : 16;
identification : 16;
flags : 3;
fragOffset : 13;
ttl : 8;
protocol : 8;
hdrChecksum : 16;
srcAddr : 32;
dstAddr: 32;
}
}
header_type extended_meta_t {
fields {
vf_meter_color : 2;
}
}
header ethernet_t ethernet;
header vlan_t vlan;
header ipv4_t ipv4;
metadata extended_meta_t extended_meta;
/*
* Parser
*/
parser start {
set_metadata(extended_meta.vf_meter_color, 0); /* precolour green */
return parse_ethernet;
}
parser parse_ethernet {
extract(ethernet);
return select(latest.etherType) {
ETHERTYPE_VLAN : parse_vlan;
ETHERTYPE_IPV4 : parse_ipv4;
default: ingress;
}
}
parser parse_vlan {
extract(vlan);
return select(latest.etherType) {
ETHERTYPE_IPV4 : parse_ipv4;
default: ingress;
}
}
parser parse_ipv4 {
extract(ipv4);
return select(latest.protocol) {
default: ingress;
}
}
/*
* Ingress
*/
/* attach a pragma that will automatically drop the packet
* when the meter returns red
*/
@pragma netro meter_drop_red
meter vf_meters {
type: bytes; /* bytes or packets */
result: extended_meta.vf_meter_color;
instance_count: 4;
}
action do_throttle(meter_idx) {
execute_meter(vf_meters, meter_idx, extended_meta.vf_meter_color);
}
action do_forward(espec) {
modify_field(standard_metadata.egress_spec, espec);
}
action drop_act() {
drop();
}
table forward {
reads {
standard_metadata.ingress_port : exact;
}
actions {
do_forward;
drop_act;
}
}
table forward_vlan {
reads {
vlan.vid: exact;
}
actions {
do_throttle;
}
}
control ingress {
if (valid(vlan)) {
apply(forward_vlan); /* do throttle */
}
apply(forward); /* destination port */
}
/*
* Egress
*/
action translate_vlan(new_vid, new_espec) {
modify_field(vlan.vid, new_vid);
modify_field(standard_metadata.egress_spec, new_espec);
}
table manipulate_vlan {
reads {
vlan.vid: exact;
standard_metadata.ingress_port : exact;
}
actions {
translate_vlan;
}
}
control egress {
if (valid(vlan)) {
apply(manipulate_vlan);
}
}