P4C
The P4 Compiler
Loading...
Searching...
No Matches
pnaNic.h
1/*
2Copyright 2024 Marvell Technology, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_BMV2_PNA_NIC_PNANIC_H_
18#define BACKENDS_BMV2_PNA_NIC_PNANIC_H_
19
20#include "backends/bmv2/portable_common/portable.h"
21#include "pnaProgramStructure.h"
22
23namespace P4::BMV2 {
24
26 public:
28 P4::ProgramStructure *structure, cstring scalarsName)
29 : BMV2::ExpressionConverter(refMap, typeMap, structure, scalarsName) {}
30
31 void modelError(const char *format, const cstring field) {
32 ::P4::error(ErrorType::ERR_MODEL,
33 (cstring(format) + "\nInvalid metadata parameter value for PNA").c_str(),
34 field);
35 }
36
37 Util::IJson *convertParam(UNUSED const IR::Parameter *param, cstring fieldName) override {
38 cstring ptName = param->type->toString();
39 if (PnaProgramStructure::isCounterMetadata(ptName)) { // check if its counter metadata
40 auto jsn = new Util::JsonObject();
41 jsn->emplace("name"_cs, param->toString());
42 jsn->emplace("type"_cs, "hexstr");
43 auto bitwidth = param->type->width_bits();
44
45 // encode the counter type from enum -> int
46 if (fieldName == "BYTES") {
47 cstring repr = BMV2::stringRepr(0, ROUNDUP(bitwidth, 32));
48 jsn->emplace("value"_cs, repr);
49 } else if (fieldName == "PACKETS") {
50 cstring repr = BMV2::stringRepr(1, ROUNDUP(bitwidth, 32));
51 jsn->emplace("value"_cs, repr);
52 } else if (fieldName == "PACKETS_AND_BYTES") {
53 cstring repr = BMV2::stringRepr(2, ROUNDUP(bitwidth, 32));
54 jsn->emplace("value"_cs, repr);
55 } else {
56 modelError("%1%: Exptected a PNA_CounterType_t", fieldName);
57 return nullptr;
58 }
59 return jsn;
60 } else if (PnaProgramStructure::isStandardMetadata(ptName)) { // check if its pna metadata
61 auto jsn = new Util::JsonObject();
62
63 // encode the metadata type and field in json
64 jsn->emplace("type"_cs, "field");
65 auto a = mkArrayField(jsn, "value"_cs);
66 a->append(ptName.exceptLast(2));
67 a->append(fieldName);
68 return jsn;
69 } else {
70 // not a special type
71 return nullptr;
72 }
73 return nullptr;
74 }
75};
76
78 public:
79 // PnaCodeGenerator(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
80 // : PortableCodeGenerator(refMap, typeMap) {}
81
82 void create(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
83 void createParsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
84 void createControls(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
85 void createDeparsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
86};
87
89 public:
90 P4::ReferenceMap *refMap;
91 P4::TypeMap *typeMap;
92 const IR::ToplevelBlock *toplevel;
93 JsonObjects *json;
94 PnaProgramStructure *structure;
95 PnaCodeGenerator *codeGenerator;
96
98 const IR::ToplevelBlock *toplevel, JsonObjects *json,
99 PnaProgramStructure *structure)
100 : refMap(refMap), typeMap(typeMap), toplevel(toplevel), json(json), structure(structure) {
101 CHECK_NULL(refMap);
102 CHECK_NULL(typeMap);
103 CHECK_NULL(toplevel);
104 CHECK_NULL(json);
105 CHECK_NULL(structure);
106 codeGenerator = new PnaCodeGenerator();
107 CHECK_NULL(codeGenerator);
108 }
109
110 void postorder(UNUSED const IR::P4Program *program) override {
111 cstring scalarsName = "scalars"_cs;
112 // This visitor is used in multiple passes to convert expression to json
113 auto conv = new PnaNicExpressionConverter(refMap, typeMap, structure, scalarsName);
114 auto ctxt = new ConversionContext(refMap, typeMap, toplevel, structure, conv, json);
115 codeGenerator->create(ctxt, structure);
116 }
117};
118
119class PnaNicBackend : public Backend {
120 BMV2Options &options;
121
122 public:
123 void convert(const IR::ToplevelBlock *tlb) override;
124 PnaNicBackend(BMV2Options &options, P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
125 P4::ConvertEnums::EnumMapping *enumMap)
126 : Backend(options, refMap, typeMap, enumMap), options(options) {}
127};
128
129EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Hash)
130EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(InternetChecksum)
131EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Register)
132
133} // namespace P4::BMV2
134
135#endif /* BACKENDS_BMV2_PNA_NIC_PNANIC_H_ */
Definition backends/bmv2/common/options.h:26
Backend is a the base class for SimpleSwitchBackend and PortableSwitchBackend.
Definition bmv2/common/backend.h:60
Definition pnaNic.h:88
Definition expression.h:51
Definition JsonObjects.h:27
Definition pnaNic.h:77
Definition pnaNic.h:119
Definition bmv2/pna_nic/pnaProgramStructure.h:31
static bool isStandardMetadata(cstring ptName)
Definition bmv2/pna_nic/pnaProgramStructure.h:46
static bool isCounterMetadata(cstring ptName)
Definition bmv2/pna_nic/pnaProgramStructure.h:42
Definition portable.h:46
Definition visitor.h:400
Definition portableProgramStructure.h:29
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition json.h:40
Definition json.h:164
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
Definition bmv2/common/helpers.h:297