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
44class PNAEbpfGenerator;
45
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 void updatePnaDirectMeter(const IR::P4Table *t, IR::TCTable *tabledef, unsigned tentries);
115 bool isPnaParserMeta(const IR::Member *mem);
116 bool isPnaMainInputMeta(const IR::Member *mem);
117 bool isPnaMainOutputMeta(const IR::Member *mem);
118 unsigned int findMappedKernelMeta(const IR::Member *mem);
119 const IR::Expression *ExtractExpFromCast(const IR::Expression *exp);
120 unsigned getTcType(const IR::StringLiteral *sl);
121 unsigned getTableId(cstring tableName) const;
122 unsigned getActionId(cstring actionName) const;
123 cstring getExternId(cstring externName) const;
124 unsigned getExternInstanceId(cstring externName, cstring instanceName) const;
125 cstring processExternPermission(const IR::Type_Extern *ext);
126 unsigned getTableKeysize(unsigned tableId) const;
127 cstring externalName(const IR::IDeclaration *declaration) const;
128 cstring HandleTableAccessPermission(const IR::P4Table *t);
129 std::pair<cstring, cstring> *GetAnnotatedAccessPath(const IR::Annotation *anno);
130 void updateAddOnMissTable(const IR::P4Table *t);
131 bool checkParameterDirection(const IR::TCAction *tcAction);
132 bool hasExecuteMethod(const IR::Type_Extern *extn);
133 void addExternTypeInstance(const IR::Declaration_Instance *decl,
134 IR::TCExternInstance *tcExternInstance, cstring eName);
135 safe_vector<const IR::TCKey *> HandleTypeNameStructField(const IR::StructField *field,
136 const IR::Type_Extern *extn,
137 const IR::Declaration_Instance *decl,
138 int &kId, cstring annoName);
139 safe_vector<const IR::TCKey *> processCounterControlPathKeys(
140 const IR::Type_Struct *extern_control_path, const IR::Type_Extern *extn,
141 const IR::Declaration_Instance *decl);
142 CounterType toCounterType(const int type);
143};
144
145class Extern {
146 public:
147 static const cstring dropPacket;
148 static const cstring sendToPort;
149};
150
151class Backend : public PassManager {
152 public:
153 const IR::ToplevelBlock *toplevel;
154 P4::ReferenceMap *refMap;
155 P4::TypeMap *typeMap;
156 TCOptions &options;
157 IR::TCPipeline *pipeline = new IR::TCPipeline();
160 TC::ParseTCAnnotations *parseTCAnno;
161 const IR::ToplevelBlock *top = nullptr;
162 EbpfOptions ebpfOption;
163 EBPF::Target *target;
164 const PNAEbpfGenerator *ebpf_program;
165
166 public:
167 explicit Backend(const IR::ToplevelBlock *toplevel, P4::ReferenceMap *refMap,
168 P4::TypeMap *typeMap, TCOptions &options)
169 : toplevel(toplevel), refMap(refMap), typeMap(typeMap), options(options) {
170 setName("BackEnd");
171 }
172 bool process();
173 bool ebpfCodeGen(P4::ReferenceMap *refMap, P4::TypeMap *typeMap);
174 void serialize() const;
175 bool serializeIntrospectionJson(std::ostream &out) const;
176 bool emitCFile();
177};
178
179} // namespace P4::TC
180
181#endif /* BACKENDS_TC_BACKEND_H_ */
Definition ebpf/target.h:44
Definition ebpfOptions.h:26
Definition visitor.h:400
Definition ir/pass_manager.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition tc/backend.h:151
Definition tc/backend.h:49
bool isPnaParserMeta(const IR::Member *mem)
Definition tc/backend.cpp:1185
Definition tc/backend.h:57
Definition tc/backend.h:145
This pass generates introspection JSON into user specified file.
Definition introspection.h:148
Definition ebpfCodeGen.h:35
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