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