-
Notifications
You must be signed in to change notification settings - Fork 0
/
simpleSwitch.cpp
707 lines (670 loc) · 32.5 KB
/
simpleSwitch.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
Copyright 2013-present Barefoot Networks, 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.
*/
/**
* This file implements the simple switch model
*/
#include <algorithm>
#include <cstring>
#include <set>
#include "frontend_sai_model.h"
#include "backends/bmv2/backend.h"
#include "simpleSwitch.h"
using BMV2::mkArrayField;
using BMV2::mkParameters;
using BMV2::mkPrimitive;
using BMV2::nextId;
namespace P4FLX {
V1Model V1Model::instance; // inspired by p4c/frontends/p4/modelinstances.cpp [omers]
// need a class to generate simpleSwitch
cstring jsonMetadataParameterName = "standard_metadata";
void
SimpleSwitch::addToFieldList(const IR::Expression* expr, Util::JsonArray* fl) {
auto typeMap = backend->getTypeMap();
auto conv = backend->getExpressionConverter();
if (expr->is<IR::ListExpression>()) {
auto le = expr->to<IR::ListExpression>();
for (auto e : le->components) {
addToFieldList(e, fl);
}
return;
}
auto type = typeMap->getType(expr, true);
if (type->is<IR::Type_StructLike>()) {
// recursively add all fields
auto st = type->to<IR::Type_StructLike>();
for (auto f : st->fields) {
auto member = new IR::Member(expr, f->name);
typeMap->setType(member, typeMap->getType(f, true));
addToFieldList(member, fl);
}
return;
}
auto j = conv->convert(expr);
fl->append(j);
}
// returns id of created field list
int
SimpleSwitch::createFieldList(const IR::Expression* expr, cstring group,
cstring listName, Util::JsonArray* field_lists) {
auto fl = new Util::JsonObject();
field_lists->append(fl);
int id = nextId(group);
fl->emplace("id", id);
fl->emplace("name", listName);
// TODO(jafingerhut) - add line/col here?
auto elements = mkArrayField(fl, "elements");
addToFieldList(expr, elements);
return id;
}
cstring
SimpleSwitch::convertHashAlgorithm(cstring algorithm) {
cstring result;
if (algorithm == v1model.algorithm.crc32.name)
result = "crc32";
else if (algorithm == v1model.algorithm.crc32_custom.name)
result = "crc32_custom";
else if (algorithm == v1model.algorithm.crc16.name)
result = "crc16";
else if (algorithm == v1model.algorithm.crc16_custom.name)
result = "crc16_custom";
else if (algorithm == v1model.algorithm.random.name)
result = "random";
else if (algorithm == v1model.algorithm.identity.name)
result = "identity";
else
::error("%1%: unexpected algorithm", algorithm);
return result;
}
void
SimpleSwitch::convertExternObjects(Util::JsonArray *result,
const P4::ExternMethod *em,
const IR::MethodCallExpression *mc,
const IR::StatOrDecl *s) {
auto conv = backend->getExpressionConverter();
if (em->originalExternType->name == v1model.counter.name) {
if (em->method->name == v1model.counter.increment.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto primitive = mkPrimitive("count", result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto ctr = new Util::JsonObject();
ctr->emplace("type", "counter_array");
ctr->emplace("value", em->object->controlPlaneName());
parameters->append(ctr);
auto index = conv->convert(mc->arguments->at(0));
parameters->append(index);
}
} else if (em->originalExternType->name == v1model.meter.name) {
if (em->method->name == v1model.meter.executeMeter.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
auto primitive = mkPrimitive("execute_meter", result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto mtr = new Util::JsonObject();
mtr->emplace("type", "meter_array");
mtr->emplace("value", em->object->controlPlaneName());
parameters->append(mtr);
auto index = conv->convert(mc->arguments->at(0));
parameters->append(index);
auto result = conv->convert(mc->arguments->at(1));
parameters->append(result);
}
} else if (em->originalExternType->name == v1model.registers.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
auto reg = new Util::JsonObject();
reg->emplace("type", "register_array");
cstring name = em->object->controlPlaneName();
reg->emplace("value", name);
if (em->method->name == v1model.registers.read.name) {
auto primitive = mkPrimitive("register_read", result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto dest = conv->convert(mc->arguments->at(0));
parameters->append(dest);
parameters->append(reg);
auto index = conv->convert(mc->arguments->at(1));
parameters->append(index);
} else if (em->method->name == v1model.registers.write.name) {
auto primitive = mkPrimitive("register_write", result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
parameters->append(reg);
auto index = conv->convert(mc->arguments->at(0));
parameters->append(index);
auto value = conv->convert(mc->arguments->at(1));
parameters->append(value);
}
} else if (em->originalExternType->name == v1model.directMeter.name) {
if (em->method->name == v1model.directMeter.read.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
auto dest = mc->arguments->at(0);
backend->getMeterMap().setDestination(em->object, dest);
// Do not generate any code for this operation
}
} else if (em->originalExternType->name == v1model.directCounter.name) {
if (em->method->name == v1model.directCounter.count.name) {
BUG_CHECK(mc->arguments->size() == 0, "Expected 0 argument for %1%", mc);
// Do not generate any code for this operation
}
}
}
void
SimpleSwitch::convertExternFunctions(Util::JsonArray *result,
const P4::ExternFunction *ef,
const IR::MethodCallExpression *mc,
const IR::StatOrDecl* s) {
auto refMap = backend->getRefMap();
auto typeMap = backend->getTypeMap();
auto conv = backend->getExpressionConverter();
if (ef->method->name == v1model.clone.name ||
ef->method->name == v1model.clone.clone3.name) {
int id = -1;
if (ef->method->name == v1model.clone.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
cstring name = refMap->newName("fl");
auto emptylist = new IR::ListExpression({});
id = createFieldList(emptylist, "field_lists", name, backend->field_lists);
} else {
BUG_CHECK(mc->arguments->size() == 3, "Expected 3 arguments for %1%", mc);
cstring name = refMap->newName("fl");
id = createFieldList(mc->arguments->at(2), "field_lists", name,
backend->field_lists);
}
auto cloneType = mc->arguments->at(0);
auto ei = P4::EnumInstance::resolve(cloneType, typeMap);
if (ei == nullptr) {
::error("%1%: must be a constant on this target", cloneType);
} else {
cstring prim;
if (ei->name == "IB2E") {
prim = "clone_ingress_bridge_pkt_to_egress";
} else if (ei->name == "IR2E") {
prim = "clone_ingress_router_pkt_to_egress";
} else { //if (ei->name == "ER2E") {
prim = "clone_egress_router_pkt_to_egress";
}
// cstring prim = ei->name == "I2E" ? "clone_ingress_bridge_pkt_to_egress" :
// "clone_egress_bridge_pkt_to_egress";
auto session = conv->convert(mc->arguments->at(1));
auto primitive = mkPrimitive(prim, result);
auto parameters = mkParameters(primitive);
// TODO(jafingerhut):
// primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
parameters->append(session);
if (id >= 0) {
auto cst = new IR::Constant(id);
typeMap->setType(cst, IR::Type_Bits::get(32));
auto jcst = conv->convert(cst);
parameters->append(jcst);
}
}
} else if (ef->method->name == v1model.hash.name) {
static std::set<cstring> supportedHashAlgorithms = {
v1model.algorithm.crc32.name, v1model.algorithm.crc32_custom.name,
v1model.algorithm.crc16.name, v1model.algorithm.crc16_custom.name,
v1model.algorithm.random.name, v1model.algorithm.identity.name };
BUG_CHECK(mc->arguments->size() == 5, "Expected 5 arguments for %1%", mc);
auto primitive = mkPrimitive("modify_field_with_hash_based_offset", result);
auto parameters = mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto dest = conv->convert(mc->arguments->at(0));
parameters->append(dest);
auto base = conv->convert(mc->arguments->at(2));
parameters->append(base);
auto calculation = new Util::JsonObject();
auto ei = P4::EnumInstance::resolve(mc->arguments->at(1), typeMap);
CHECK_NULL(ei);
if (supportedHashAlgorithms.find(ei->name) == supportedHashAlgorithms.end())
::error("%1%: unexpected algorithm", ei->name);
// inlined cstring calcName = createCalculation(ei->name,
// mc->arguments->at(3), calculations);
auto fields = mc->arguments->at(3);
cstring calcName = refMap->newName("calc_");
auto calc = new Util::JsonObject();
calc->emplace("name", calcName);
calc->emplace("id", nextId("calculations"));
calc->emplace("algo", ei->name);
if (!fields->is<IR::ListExpression>()) {
// expand it into a list
auto list = new IR::ListExpression({});
auto type = typeMap->getType(fields, true);
BUG_CHECK(type->is<IR::Type_StructLike>(), "%1%: expected a struct", fields);
for (auto f : type->to<IR::Type_StructLike>()->fields) {
auto e = new IR::Member(fields, f->name);
auto ftype = typeMap->getType(f);
typeMap->setType(e, ftype);
list->push_back(e);
}
fields = list;
typeMap->setType(fields, type);
}
auto jright = conv->convert(fields);
calc->emplace("input", jright);
backend->json->calculations->append(calc);
calculation->emplace("type", "calculation");
calculation->emplace("value", calcName);
parameters->append(calculation);
auto max = conv->convert(mc->arguments->at(4));
parameters->append(max);
} else if (ef->method->name == v1model.digest_receiver.name) {
BUG_CHECK(mc->arguments->size() == 2, "Expected 2 arguments for %1%", mc);
auto primitive = mkPrimitive("generate_digest", result);
auto parameters = mkParameters(primitive);
// TODO(jafingerhut):
// primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto dest = conv->convert(mc->arguments->at(0));
parameters->append(dest);
cstring listName = "digest";
// If we are supplied a type argument that is a named type use
// that for the list name.
if (mc->typeArguments->size() == 1) {
auto typeArg = mc->typeArguments->at(0);
if (typeArg->is<IR::Type_Name>()) {
auto origType = refMap->getDeclaration(
typeArg->to<IR::Type_Name>()->path, true);
BUG_CHECK(origType->is<IR::Type_Struct>(),
"%1%: expected a struct type", origType);
auto st = origType->to<IR::Type_Struct>();
listName = st->controlPlaneName();
}
}
int id = createFieldList(mc->arguments->at(1), "learn_lists",
listName, backend->learn_lists);
auto cst = new IR::Constant(id);
typeMap->setType(cst, IR::Type_Bits::get(32));
auto jcst = conv->convert(cst);
parameters->append(jcst);
} else if (ef->method->name == v1model.resubmit.name ||
ef->method->name == v1model.recirculate.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 argument for %1%", mc);
cstring prim = (ef->method->name == v1model.resubmit.name) ?
"resubmit" : "recirculate";
auto primitive = mkPrimitive(prim, result);
auto parameters = mkParameters(primitive);
// TODO(jafingerhut):
// primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
cstring listName = prim;
// If we are supplied a type argument that is a named type use
// that for the list name.
if (mc->typeArguments->size() == 1) {
auto typeArg = mc->typeArguments->at(0);
if (typeArg->is<IR::Type_Name>()) {
auto origType = refMap->getDeclaration(
typeArg->to<IR::Type_Name>()->path, true);
BUG_CHECK(origType->is<IR::Type_Struct>(),
"%1%: expected a struct type", origType);
auto st = origType->to<IR::Type_Struct>();
listName = st->controlPlaneName();
}
}
int id = createFieldList(mc->arguments->at(0), "field_lists",
listName, backend->field_lists);
auto cst = new IR::Constant(id);
typeMap->setType(cst, IR::Type_Bits::get(32));
auto jcst = conv->convert(cst);
parameters->append(jcst);
} else if (ef->method->name == v1model.drop.name) {
BUG_CHECK(mc->arguments->size() == 0, "Expected 0 arguments for %1%", mc);
auto primitive = mkPrimitive("drop", result);
(void)mkParameters(primitive);
primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
} else if (ef->method->name == v1model.random.name) {
BUG_CHECK(mc->arguments->size() == 3, "Expected 3 arguments for %1%", mc);
auto primitive =
mkPrimitive(v1model.random.modify_field_rng_uniform.name, result);
auto params = mkParameters(primitive);
// TODO(jafingerhut):
// primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto dest = conv->convert(mc->arguments->at(0));
auto lo = conv->convert(mc->arguments->at(1));
auto hi = conv->convert(mc->arguments->at(2));
params->append(dest);
params->append(lo);
params->append(hi);
} else if (ef->method->name == v1model.truncate.name) {
BUG_CHECK(mc->arguments->size() == 1, "Expected 1 arguments for %1%", mc);
auto primitive = mkPrimitive(v1model.truncate.name, result);
auto params = mkParameters(primitive);
// TODO(jafingerhut):
// primitive->emplace_non_null("source_info", s->sourceInfoJsonObj());
auto len = conv->convert(mc->arguments->at(0));
params->append(len);
}
}
void
SimpleSwitch::convertExternInstances(const IR::Declaration *c,
const IR::ExternBlock* eb,
Util::JsonArray* action_profiles,
BMV2::SharedActionSelectorCheck& selector_check) {
CHECK_NULL(backend);
auto conv = backend->getExpressionConverter();
auto inst = c->to<IR::Declaration_Instance>();
cstring name = inst->controlPlaneName();
if (eb->type->name == v1model.counter.name) {
auto jctr = new Util::JsonObject();
jctr->emplace("name", name);
jctr->emplace("id", nextId("counter_arrays"));
jctr->emplace_non_null("source_info", eb->sourceInfoJsonObj());
auto sz = eb->getParameterValue(v1model.counter.sizeParam.name);
CHECK_NULL(sz);
BUG_CHECK(sz->is<IR::Constant>(), "%1%: expected a constant", sz);
jctr->emplace("size", sz->to<IR::Constant>()->value);
jctr->emplace("is_direct", false);
backend->counters->append(jctr);
} else if (eb->type->name == v1model.meter.name) {
auto jmtr = new Util::JsonObject();
jmtr->emplace("name", name);
jmtr->emplace("id", nextId("meter_arrays"));
jmtr->emplace_non_null("source_info", eb->sourceInfoJsonObj());
jmtr->emplace("is_direct", false);
auto sz = eb->getParameterValue(v1model.meter.sizeParam.name);
CHECK_NULL(sz);
BUG_CHECK(sz->is<IR::Constant>(), "%1%: expected a constant", sz);
jmtr->emplace("size", sz->to<IR::Constant>()->value);
jmtr->emplace("rate_count", 2);
auto mkind = eb->getParameterValue(v1model.meter.typeParam.name);
CHECK_NULL(mkind);
BUG_CHECK(mkind->is<IR::Declaration_ID>(), "%1%: expected a member", mkind);
cstring name = mkind->to<IR::Declaration_ID>()->name;
cstring type = "?";
if (name == v1model.meter.meterType.packets.name)
type = "packets";
else if (name == v1model.meter.meterType.bytes.name)
type = "bytes";
else
::error("Unexpected meter type %1%", mkind);
jmtr->emplace("type", type);
backend->meter_arrays->append(jmtr);
} else if (eb->type->name == v1model.registers.name) {
auto jreg = new Util::JsonObject();
jreg->emplace("name", name);
jreg->emplace("id", nextId("register_arrays"));
jreg->emplace_non_null("source_info", eb->sourceInfoJsonObj());
auto sz = eb->getParameterValue(v1model.registers.sizeParam.name);
CHECK_NULL(sz);
BUG_CHECK(sz->is<IR::Constant>(), "%1%: expected a constant", sz);
if (sz->to<IR::Constant>()->value == 0)
error("%1%: direct registers are not supported in bmv2", inst);
jreg->emplace("size", sz->to<IR::Constant>()->value);
BUG_CHECK(eb->instanceType->is<IR::Type_SpecializedCanonical>(),
"%1%: Expected a generic specialized type", eb->instanceType);
auto st = eb->instanceType->to<IR::Type_SpecializedCanonical>();
BUG_CHECK(st->arguments->size() == 1, "%1%: expected 1 type argument");
unsigned width = st->arguments->at(0)->width_bits();
if (width == 0)
::error("%1%: unknown width", st->arguments->at(0));
jreg->emplace("bitwidth", width);
backend->register_arrays->append(jreg);
} else if (eb->type->name == v1model.directCounter.name) {
auto it = backend->getDirectCounterMap().find(name);
if (it == backend->getDirectCounterMap().end()) {
::warning("%1%: Direct counter not used; ignoring", inst);
} else {
auto jctr = new Util::JsonObject();
jctr->emplace("name", name);
jctr->emplace("id", nextId("counter_arrays"));
// TODO(jafingerhut) - add line/col here?
jctr->emplace("is_direct", true);
jctr->emplace("binding", it->second->externalName());
backend->counters->append(jctr);
}
} else if (eb->type->name == v1model.directMeter.name) {
auto info = backend->getMeterMap().getInfo(c);
CHECK_NULL(info);
CHECK_NULL(info->table);
CHECK_NULL(info->destinationField);
auto jmtr = new Util::JsonObject();
jmtr->emplace("name", name);
jmtr->emplace("id", nextId("meter_arrays"));
jmtr->emplace_non_null("source_info", eb->sourceInfoJsonObj());
jmtr->emplace("is_direct", true);
jmtr->emplace("rate_count", 2);
auto mkind = eb->getParameterValue(v1model.directMeter.typeParam.name);
CHECK_NULL(mkind);
BUG_CHECK(mkind->is<IR::Declaration_ID>(), "%1%: expected a member", mkind);
cstring name = mkind->to<IR::Declaration_ID>()->name;
cstring type = "?";
if (name == v1model.meter.meterType.packets.name)
type = "packets";
else if (name == v1model.meter.meterType.bytes.name)
type = "bytes";
else
::error("%1%: unexpected meter type", mkind);
jmtr->emplace("type", type);
jmtr->emplace("size", info->tableSize);
cstring tblname = info->table->controlPlaneName();
jmtr->emplace("binding", tblname);
auto result = conv->convert(info->destinationField);
jmtr->emplace("result_target", result->to<Util::JsonObject>()->get("value"));
backend->meter_arrays->append(jmtr);
} else if (eb->type->name == v1model.action_profile.name ||
eb->type->name == v1model.action_selector.name) {
auto action_profile = new Util::JsonObject();
action_profile->emplace("name", name);
action_profile->emplace("id", nextId("action_profiles"));
// TODO(jafingerhut) - add line/col here?
auto add_size = [&action_profile, &eb](const cstring &pname) {
auto sz = eb->getParameterValue(pname);
BUG_CHECK(sz->is<IR::Constant>(), "%1%: expected a constant", sz);
action_profile->emplace("max_size", sz->to<IR::Constant>()->value);
};
if (eb->type->name == v1model.action_profile.name) {
add_size(v1model.action_profile.sizeParam.name);
} else {
add_size(v1model.action_selector.sizeParam.name);
auto selector = new Util::JsonObject();
auto hash = eb->getParameterValue(
v1model.action_selector.algorithmParam.name);
BUG_CHECK(hash->is<IR::Declaration_ID>(), "%1%: expected a member", hash);
auto algo = convertHashAlgorithm(hash->to<IR::Declaration_ID>()->name);
selector->emplace("algo", algo);
auto input = selector_check.get_selector_input(
c->to<IR::Declaration_Instance>());
if (input == nullptr) {
// the selector is never used by any table, we cannot figure out its
// input and therefore cannot include it in the JSON
::warning("Action selector '%1%' is never referenced by a table "
"and cannot be included in bmv2 JSON", c);
return;
}
auto j_input = mkArrayField(selector, "input");
for (auto expr : *input) {
auto jk = conv->convert(expr);
j_input->append(jk);
}
action_profile->emplace("selector", selector);
}
action_profiles->append(action_profile);
}
}
cstring
SimpleSwitch::createCalculation(cstring algo, const IR::Expression* fields,
Util::JsonArray* calculations, const IR::Node* node) {
auto typeMap = backend->getTypeMap();
auto refMap = backend->getRefMap();
auto conv = backend->getExpressionConverter();
cstring calcName = refMap->newName("calc_");
auto calc = new Util::JsonObject();
calc->emplace("name", calcName);
calc->emplace("id", nextId("calculations"));
calc->emplace_non_null("source_info", node->sourceInfoJsonObj());
calc->emplace("algo", algo);
if (!fields->is<IR::ListExpression>()) {
// expand it into a list
auto list = new IR::ListExpression({});
auto type = typeMap->getType(fields, true);
BUG_CHECK(type->is<IR::Type_StructLike>(), "%1%: expected a struct", fields);
for (auto f : type->to<IR::Type_StructLike>()->fields) {
auto e = new IR::Member(fields, f->name);
auto ftype = typeMap->getType(f);
typeMap->setType(e, ftype);
list->push_back(e);
}
fields = list;
typeMap->setType(fields, type);
}
auto jright = conv->convert(fields);
calc->emplace("input", jright);
calculations->append(calc);
return calcName;
}
void
SimpleSwitch::generateUpdate(const IR::BlockStatement *block,
Util::JsonArray* checksums, Util::JsonArray* calculations) {
auto typeMap = backend->getTypeMap();
auto refMap = backend->getRefMap();
auto conv = backend->getExpressionConverter();
// Currently this is very hacky to target the very limited support
// for checksums in BMv2 This will work much better when BMv2
// offers a checksum extern.
for (auto stat : block->components) {
if (stat->is<IR::IfStatement>()) {
// The way checksums work in Json, they always ignore the condition!
stat = stat->to<IR::IfStatement>()->ifTrue;
}
if (auto blk = stat->to<IR::BlockStatement>()) {
generateUpdate(blk, checksums, calculations);
continue;
} else if (auto assign = stat->to<IR::AssignmentStatement>()) {
if (auto mc = assign->right->to<IR::MethodCallExpression>()) {
auto mi = P4::MethodInstance::resolve(mc, refMap, typeMap);
if (auto em = mi->to<P4::ExternMethod>()) {
if (em->method->name.name == v1model.ck16.get.name &&
em->originalExternType->name.name == v1model.ck16.name) {
BUG_CHECK(mi->expr->arguments->size() == 1,
"%1%: Expected 1 argument", assign->right);
auto cksum = new Util::JsonObject();
cstring calcName = createCalculation("csum16", mi->expr->arguments->at(0),
calculations, mc);
cksum->emplace("name", refMap->newName("cksum_"));
cksum->emplace("id", nextId("checksums"));
// TODO(jafingerhut) - add line/col here?
auto jleft = conv->convert(assign->left);
cksum->emplace("target", jleft->to<Util::JsonObject>()->get("value"));
cksum->emplace("type", "generic");
cksum->emplace("calculation", calcName);
checksums->append(cksum);
continue;
}
}
}
} else if (auto mc = stat->to<IR::MethodCallStatement>()) {
auto mi = P4::MethodInstance::resolve(mc->methodCall, refMap, typeMap, true);
BUG_CHECK(mi && mi->isApply(), "Call of something other than an apply method");
// FIXME -- ignore for now
continue;
}
BUG("%1%: not handled yet", stat);
}
}
void
SimpleSwitch::convertChecksumUpdate(const IR::P4Control* updateControl,
Util::JsonArray* checksums,
Util::JsonArray* calculations) {
generateUpdate(updateControl->body, checksums, calculations);
}
void
SimpleSwitch::setPipelineControls(const IR::ToplevelBlock* toplevel,
std::set<cstring>* controls,
std::map<cstring, cstring>* map) {
auto main = toplevel->getMain();
if (main == nullptr) {
::error("`%1%' module not found for simple switch", IR::P4Program::main);
exit(1);
}
auto ingress_bridge = main->getParameterValue(v1model.sw.ingress_bridge.name);
auto egress_bridge = main->getParameterValue(v1model.sw.egress_bridge.name);
if (ingress_bridge == nullptr || egress_bridge == nullptr ||
!ingress_bridge->is<IR::ControlBlock>() || !egress_bridge->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
auto ingress_bridge_name = ingress_bridge->to<IR::ControlBlock>()->container->name;
auto egress_bridge_name = egress_bridge->to<IR::ControlBlock>()->container->name;
controls->emplace(ingress_bridge_name);
controls->emplace(egress_bridge_name);
map->emplace(ingress_bridge_name, "ingress_bridge");
map->emplace(egress_bridge_name, "egress_bridge");
auto ingress = main->getParameterValue(v1model.sw.ingress.name);
auto ingress_router = main->getParameterValue(v1model.sw.ingress_router.name);
auto egress_router = main->getParameterValue(v1model.sw.egress_router.name);
auto egress = main->getParameterValue(v1model.sw.egress.name);
if (ingress == nullptr || !ingress->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
if (ingress_router == nullptr || !ingress_router->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
if (egress_router == nullptr || !egress_router->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
if (egress == nullptr || !egress->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
auto ingress_name = ingress->to<IR::ControlBlock>()->container->name;
controls->emplace(ingress_name);
map->emplace(ingress_name, "ingress");
auto ingress_router_name = ingress_router->to<IR::ControlBlock>()->container->name;
controls->emplace(ingress_router_name);
map->emplace(ingress_router_name, "ingress_router");
auto egress_router_name = egress_router->to<IR::ControlBlock>()->container->name;
controls->emplace(egress_router_name);
map->emplace(egress_router_name, "egress_router");
auto egress_name = egress->to<IR::ControlBlock>()->container->name;
controls->emplace(egress_name);
map->emplace(egress_name, "egress");
}
void
SimpleSwitch::setNonPipelineControls(const IR::ToplevelBlock* toplevel,
std::set<cstring>* controls) {
auto main = toplevel->getMain();
auto verify = main->getParameterValue(v1model.sw.verify.name);
auto update = main->getParameterValue(v1model.sw.update.name);
auto deparser = main->getParameterValue(v1model.sw.deparser.name);
if (verify == nullptr || update == nullptr || deparser == nullptr ||
!verify->is<IR::ControlBlock>() || !update->is<IR::ControlBlock>() ||
!deparser->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
controls->emplace(verify->to<IR::ControlBlock>()->container->name);
controls->emplace(update->to<IR::ControlBlock>()->container->name);
controls->emplace(deparser->to<IR::ControlBlock>()->container->name);
}
void
SimpleSwitch::setUpdateChecksumControls(const IR::ToplevelBlock* toplevel,
std::set<cstring>* controls) {
auto main = toplevel->getMain();
auto update = main->getParameterValue(v1model.sw.update.name);
if (update == nullptr || !update->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
controls->emplace(update->to<IR::ControlBlock>()->container->name);
}
void
SimpleSwitch::setDeparserControls(const IR::ToplevelBlock* toplevel,
std::set<cstring>* controls) {
auto main = toplevel->getMain();
auto deparser = main->getParameterValue(v1model.sw.deparser.name);
if (deparser == nullptr || !deparser->is<IR::ControlBlock>()) {
BUG("%1%: main package does not match the expected model %2%",
main, v1model.file.toString());
}
controls->emplace(deparser->to<IR::ControlBlock>()->container->name);
}
} // namespace P4FLX