P4C
The P4 Compiler
Loading...
Searching...
No Matches
tc/backend.h
1/*
2Copyright (C) 2023 Intel Corporation
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
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing,
11software distributed 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
14and limitations under the License.
15*/
16
17#ifndef BACKENDS_TC_BACKEND_H_
18#define BACKENDS_TC_BACKEND_H_
19
20#include <deque>
21
22#include "backends/ebpf/psa/ebpfPsaGen.h"
23#include "control-plane/p4RuntimeArchHandler.h"
24#include "ebpfCodeGen.h"
25#include "frontends/p4/evaluator/evaluator.h"
26#include "frontends/p4/parseAnnotations.h"
27#include "frontends/p4/parserCallGraph.h"
28#include "introspection.h"
29#include "ir/ir.h"
30#include "lib/error.h"
31#include "lib/nullstream.h"
32#include "lib/stringify.h"
33#include "options.h"
34#include "pnaProgramStructure.h"
35#include "tcAnnotations.h"
36#include "tc_defines.h"
37
38namespace P4::TC {
39
40extern cstring PnaMainParserInputMetaFields[TC::MAX_PNA_PARSER_META];
41extern cstring PnaMainInputMetaFields[TC::MAX_PNA_INPUT_META];
42extern cstring PnaMainOutputMetaFields[TC::MAX_PNA_OUTPUT_META];
43
45
49class ConvertToBackendIR : public Inspector {
50 public:
52 cstring instance_name;
53 unsigned instance_id;
54 bool is_num_elements;
55 int num_elements;
56 };
57 struct ExternBlock {
58 cstring externId;
59 cstring control_name;
60 unsigned no_of_instances;
61 cstring permissions;
63 };
64 enum CounterType { PACKETS, BYTES, PACKETS_AND_BYTES };
65 const IR::ToplevelBlock *tlb;
66 IR::TCPipeline *tcPipeline;
67 P4::ReferenceMap *refMap;
68 P4::TypeMap *typeMap;
69 TCOptions &options;
70 unsigned int tableCount = 0;
71 unsigned int actionCount = 0;
72 unsigned int metadataCount = 0;
73 unsigned int labelCount = 0;
74 unsigned int externCount = 0;
75 cstring pipelineName = nullptr;
76 cstring mainParserName = nullptr;
80 ordered_map<unsigned, unsigned> tableKeysizeList;
81 safe_vector<const IR::P4Table *> add_on_miss_tables;
85
86 public:
87 ConvertToBackendIR(const IR::ToplevelBlock *tlb, IR::TCPipeline *pipe, P4::ReferenceMap *refMap,
88 P4::TypeMap *typeMap, TCOptions &options)
89 : tlb(tlb), tcPipeline(pipe), refMap(refMap), typeMap(typeMap), options(options) {}
90 void setPipelineName();
91 cstring getPipelineName() { return pipelineName; };
92 bool preorder(const IR::P4Program *p) override;
93 void postorder(const IR::P4Action *a) override;
94 void postorder(const IR::P4Table *t) override;
95 void postorder(const IR::P4Program *p) override;
96 void postorder(const IR::Declaration_Instance *d) override;
97 void postorder(const IR::Type_Struct *ts) override;
98 safe_vector<const IR::TCKey *> processExternConstructor(const IR::Type_Extern *extn,
99 const IR::Declaration_Instance *decl,
100 struct ExternInstance *instance);
101 safe_vector<const IR::TCKey *> processExternControlPath(const IR::Type_Extern *extn,
102 const IR::Declaration_Instance *decl,
103 cstring eName);
104 cstring getControlPathKeyAnnotation(const IR::StructField *field);
105 unsigned GetAccessNumericValue(std::string_view access);
106 bool isDuplicateAction(const IR::P4Action *action);
107 bool isDuplicateOrNoAction(const IR::P4Action *action);
108 void updateDefaultHitAction(const IR::P4Table *t, IR::TCTable *tdef);
109 void updateDefaultMissAction(const IR::P4Table *t, IR::TCTable *tdef);
110 void updateConstEntries(const IR::P4Table *t, IR::TCTable *tdef);
111 void updateMatchType(const IR::P4Table *t, IR::TCTable *tabledef);
112 void updateTimerProfiles(IR::TCTable *tabledef);
113 void updatePnaDirectCounter(const IR::P4Table *t, IR::TCTable *tabledef, unsigned tentries);
114 bool isPnaParserMeta(const IR::Member *mem);
115 bool isPnaMainInputMeta(const IR::Member *mem);
116 bool isPnaMainOutputMeta(const IR::Member *mem);
117 unsigned int findMappedKernelMeta(const IR::Member *mem);
118 const IR::Expression *ExtractExpFromCast(const IR::Expression *exp);
119 unsigned getTcType(const IR::StringLiteral *sl);
120 unsigned getTableId(cstring tableName) const;
121 unsigned getActionId(cstring actionName) const;
122 cstring getExternId(cstring externName) const;
123 unsigned getExternInstanceId(cstring externName, cstring instanceName) const;
124 cstring processExternPermission(const IR::Type_Extern *ext);
125 unsigned getTableKeysize(unsigned tableId) const;
126 cstring externalName(const IR::IDeclaration *declaration) const;
127 cstring HandleTableAccessPermission(const IR::P4Table *t);
128 std::pair<cstring, cstring> *GetAnnotatedAccessPath(const IR::Annotation *anno);
129 void updateAddOnMissTable(const IR::P4Table *t);
130 bool checkParameterDirection(const IR::TCAction *tcAction);
131 bool hasExecuteMethod(const IR::Type_Extern *extn);
132 safe_vector<const IR::TCKey *> HandleTypeNameStructField(const IR::StructField *field,
133 const IR::Type_Extern *extn,
134 const IR::Declaration_Instance *decl,
135 int &kId, cstring annoName);
136 safe_vector<const IR::TCKey *> processCounterControlPathKeys(
137 const IR::Type_Struct *extern_control_path, const IR::Type_Extern *extn,
138 const IR::Declaration_Instance *decl);
139 CounterType toCounterType(const int type);
140};
141
142class Extern {
143 public:
144 static const cstring dropPacket;
145 static const cstring sendToPort;
146};
147
148class Backend : public PassManager {
149 public:
150 const IR::ToplevelBlock *toplevel;
151 P4::ReferenceMap *refMap;
152 P4::TypeMap *typeMap;
153 TCOptions &options;
154 IR::TCPipeline *pipeline = new IR::TCPipeline();
157 TC::ParseTCAnnotations *parseTCAnno;
158 const IR::ToplevelBlock *top = nullptr;
159 EbpfOptions ebpfOption;
160 EBPF::Target *target;
161 const PNAEbpfGenerator *ebpf_program;
162
163 public:
164 explicit Backend(const IR::ToplevelBlock *toplevel, P4::ReferenceMap *refMap,
165 P4::TypeMap *typeMap, TCOptions &options)
166 : toplevel(toplevel), refMap(refMap), typeMap(typeMap), options(options) {
167 setName("BackEnd");
168 }
169 bool process();
170 bool ebpfCodeGen(P4::ReferenceMap *refMap, P4::TypeMap *typeMap);
171 void serialize() const;
172 bool serializeIntrospectionJson(std::ostream &out) const;
173 bool emitCFile();
174};
175
176} // namespace P4::TC
177
178#endif /* BACKENDS_TC_BACKEND_H_ */
Definition ebpf/target.h:44
Definition ebpfOptions.h:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition tc/backend.h:49
bool isPnaParserMeta(const IR::Member *mem)
Definition tc/backend.cpp:1109
Definition tc/backend.h:57
Definition tc/backend.h:142
This pass generates introspection JSON into user specified file.
Definition introspection.h:146
Definition ebpfCodeGen.h:34
Definition tcAnnotations.h:25
Definition backends/tc/options.h:25
Definition typeMap.h:41
Definition cstring.h:85
Definition ordered_map.h:32
Definition safe_vector.h:27
This file defines functions for the pass to generate the introspection file.
Definition tc/backend.cpp:24