-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcmu_groups.p4_template
351 lines (321 loc) · 15.5 KB
/
cmu_groups.p4_template
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#ifndef _CMU_GROUP_P4_
#define _CMU_GROUP_P4_
#include "headers.p4"
/** Below are all definitions of CMU-Groups */
{%for CMUG in CMUG_GROUP_CONFIGS %}
// Definition for CMU-Group{{CMUG.id}}
control CMU_Group{{CMUG.id}} ( in header_t hdr,
in egress_intrinsic_metadata_t intr_md,
inout egress_metadata_t meta )
{
action no_action(){}
// Definition for Shared Compresstion Stage.
{%if CMUG.type == 1 %}
Hash<bit<16>>(HashAlgorithm_t.CRC32) hash_unit1;
Hash<bit<16>>(HashAlgorithm_t.CRC32) hash_unit2;
Hash<bit<16>>(HashAlgorithm_t.CRC32) hash_unit3;
{% else %}
Hash<bit<32>>(HashAlgorithm_t.CRC32) hash_unit1;
Hash<bit<16>>(HashAlgorithm_t.CRC32) hash_unit2;
{% endif %}
// The hash inputs are fixed here. There are two ways to generate different hash values.
// a) Firstly, use different sub-range in the initialization stage.
// b) Secondly, add salts to hash outputs in the control plane.
action hash1(){
meta.cmu_group{{CMUG.meta_id}}.compressed_key1 = hash_unit1.get({ {{",".join(CMUG.candidate_key_list.keys())}} });
}
action hash2(){
meta.cmu_group{{CMUG.meta_id}}.compressed_key2 = hash_unit2.get({ {{",".join(CMUG.candidate_key_list.keys())}} });
}
{%if CMUG.type == 1 %}
action hash3(){
meta.cmu_group{{CMUG.meta_id}}.compressed_key3 = hash_unit3.get({ {{",".join(CMUG.candidate_key_list.keys())}} });
}
{% endif %}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start}}
{%endif%}
table tbl_hash1{
actions = {
hash1;
}
const default_action = hash1();
size = 1;
}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start}}
{%endif%}
table tbl_hash2{
actions = {
hash2;
}
const default_action = hash2();
size = 1;
}
{%if CMUG.type == 1 %}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start}}
{%endif%}
table tbl_hash3{
actions = {
hash3;
}
const default_action = hash3();
size = 1;
}
{% endif %}
// Definition for Other Stages of Each CMU (SALU).
{%for id in range(CMUG.cmu_num) %}
// Initialization stage of CMU{{id}}.
action set_cmu{{id+1}}_hkey1_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
{%if CMUG.type == 1 %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[15:0];
{% else %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[{{id*8+15}}:{{id*8}}];
{% endif %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey2_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%if CMUG.type == 1 %}
action set_cmu{{id+1}}_hkey3_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key3[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey12_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = (meta.cmu_group{{CMUG.meta_id}}.compressed_key1 ^ meta.cmu_group{{CMUG.meta_id}}.compressed_key2)[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey13_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = (meta.cmu_group{{CMUG.meta_id}}.compressed_key1 ^ meta.cmu_group{{CMUG.meta_id}}.compressed_key3)[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey23_cparam(bit<8> task_id, bit<16> param1, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = (meta.cmu_group{{CMUG.meta_id}}.compressed_key2 ^ meta.cmu_group{{CMUG.meta_id}}.compressed_key3)[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = param1;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%endif%}
action set_cmu{{id+1}}_hkey1_hparam2(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
{%if CMUG.type == 1 %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[15:0];
{% else %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[{{id*8+15}}:{{id*8}}];
{% endif %}
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%if CMUG.type == 1 %}
action set_cmu{{id+1}}_hkey1_hparam3(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key3[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%endif%}
action set_cmu{{id+1}}_hkey2_hparam1(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%if CMUG.type == 1 %}
action set_cmu{{id+1}}_hkey2_hparam3(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key3[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey3_hparam1(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key3[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey3_hparam2(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key3[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{%endif%}
// There are some special (i.e., from standard metadata) params here.
{% for pram_name, param_val in CMUG.std_params.items() %}
action set_cmu{{id+1}}_hkey1_{{pram_name}}(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key1[{{id*8+15}}:{{id*8}}];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = {{param_val}};
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
action set_cmu{{id+1}}_hkey2_{{pram_name}}(bit<8> task_id, bit<16> param2) {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id = task_id;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[15:0] = meta.cmu_group{{CMUG.meta_id}}.compressed_key2[15:0];
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = {{param_val}};
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 = param2;
}
{% endfor %}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start+1}}
{%endif%}
table tbl_cmu{{id+1}}_initialization {
key = {
// Here we use IP Pair as the filter.
hdr.ipv4.src_addr : ternary;
hdr.ipv4.dst_addr : ternary;
// Other fields can be added here.
// We only use the filter in the initialization stage, and select a task id.
// We use task_id to identify tasks to save TCAM resources in the initialization stage.
}
actions = {
set_cmu{{id+1}}_hkey1_cparam;
set_cmu{{id+1}}_hkey2_cparam;
{%if CMUG.type == 1 %}
set_cmu{{id+1}}_hkey3_cparam;
set_cmu{{id+1}}_hkey12_cparam;
set_cmu{{id+1}}_hkey13_cparam;
set_cmu{{id+1}}_hkey23_cparam;
{%endif%}
set_cmu{{id+1}}_hkey1_hparam2;
{%if CMUG.type == 1 %}
set_cmu{{id+1}}_hkey1_hparam3;
{%endif%}
set_cmu{{id+1}}_hkey2_hparam1;
{%if CMUG.type == 1 %}
set_cmu{{id+1}}_hkey2_hparam3;
{%endif%}
{%if CMUG.type == 1 %}
set_cmu{{id+1}}_hkey3_hparam1;
set_cmu{{id+1}}_hkey3_hparam2;
{%endif%}
{% for pram_name, param_val in CMUG.std_params.items() %}
set_cmu{{id+1}}_hkey1_{{pram_name}};
set_cmu{{id+1}}_hkey2_{{pram_name}};
{%if CMUG.type == 1 %}
set_cmu{{id+1}}_hkey3_{{pram_name}};
{%endif%}
{% endfor %}
}
size = 32;
}
// Pre-processing stage of CMU{{id}}.
action process_cmu{{id+1}}_key_param(bit<16> offset, bit<16> code){
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key = meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key + offset; // Implementing '-' by '+' overflow.
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = code;
}
action process_cmu{{id+1}}_key(bit<16> offset){
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key = meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key + offset; // Implementing '-' by '+' overflow.
}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start+2}}
{%endif%}
table tbl_cmu{{id+1}}_preprocessing {
key = {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id : exact;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key : ternary;
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 : ternary;
}
actions = {
no_action;
process_cmu{{id+1}}_key;
process_cmu{{id+1}}_key_param;
}
const default_action = no_action();
size = 1024;
}
// Operation stage of CMU{{id}}.
Register<bit<16>, bit<16>>({{CMUG.cmu_size}}, 0) cmu{{id+1}}_buckets;
RegisterAction<bit<16>, bit<16>, bit<16>>(cmu{{id+1}}_buckets) cmu{{id+1}}_op_cond_add = {
void apply(inout bit<16> value, out bit<16> result) {
result = 0;
if(value < meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2){
value = value |+| meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1;
}
result = value;
}
};
RegisterAction<bit<16>, bit<16>, bit<16>>(cmu{{id+1}}_buckets) cmu{{id+1}}_op_max = {
void apply(inout bit<16> value, out bit<16> result) {
result = 0;
if(value < meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1){
value = meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1;
result = value;
}
}
};
RegisterAction<bit<16>, bit<16>, bit<16>>(cmu{{id+1}}_buckets) cmu{{id+1}}_op_and_or = {
void apply(inout bit<16> value, out bit<16> result) {
if(meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p2 == 1) // bitwise_and
{
value = meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 & value;
}
else{ // bitwise_or
value = meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 | value;
}
result = value;
}
};
//RegisterAction<bit<16>, bit<16>, bit<16>>(cmu{{id+1}}_buckets) cmu{{id+1}}_op_reserved = {
// void apply(inout bit<16> value, out bit<16> result) {
// // Reserved stateful operation.
// result = value;
// }
//};
action op_cmu{{id+1}}_cond_add(){
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = cmu{{id+1}}_op_cond_add.execute((bit<16>)meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[{{CMUG.key_bitw-1}}:0]);
}
action op_cmu{{id+1}}_and_or(){
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = cmu{{id+1}}_op_and_or.execute((bit<16>)meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[{{CMUG.key_bitw-1}}:0]);
}
action op_cmu{{id+1}}_max(){
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = cmu{{id+1}}_op_max.execute((bit<16>)meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[{{CMUG.key_bitw-1}}:0]);
}
//action op_cmu{{id+1}}_reserved(){
// meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.param.p1 = cmu{{id+1}}_op_reserved.execute((bit<16>)meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.key[{{CMUG.key_bitw-1}}:0]);
//}
{%if CMUG.mau_start >= 0%}
@pragma stage {{CMUG.mau_start+3}}
{%endif%}
table tbl_cmu{{id+1}}_operation {
key = {
meta.cmu_group{{CMUG.meta_id}}.cmu{{id+1}}.task_id : exact;
}
actions = {
op_cmu{{id+1}}_cond_add;
op_cmu{{id+1}}_and_or;
op_cmu{{id+1}}_max;
// op_cmu{{id+1}}_reserved;
}
size = 32;
}
{% endfor %}
apply {
// Shared Compression Stage for CMU Group {{CMUG.id}}.
tbl_hash1.apply();
tbl_hash2.apply();
{%if CMUG.type == 1 %}
tbl_hash3.apply();
{% endif %}
{%for id in range(CMUG.cmu_num) %}
// Initialization, Pre-processing, operation stages for CMU{{id+1}} in CMU Group {{CMUG.id}}.
tbl_cmu{{id+1}}_initialization.apply();
tbl_cmu{{id+1}}_preprocessing.apply();
tbl_cmu{{id+1}}_operation.apply();
{% endfor %}
}
}
{% endfor %}
#endif