P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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 "ebpfCodeGen.h"
24#include "frontends/p4/evaluator/evaluator.h"
25#include "frontends/p4/parseAnnotations.h"
26#include "frontends/p4/parserCallGraph.h"
27#include "introspection.h"
28#include "ir/ir.h"
29#include "lib/error.h"
30#include "lib/nullstream.h"
31#include "lib/stringify.h"
32#include "options.h"
33#include "pnaProgramStructure.h"
34#include "tcAnnotations.h"
35#include "tc_defines.h"
36
37namespace TC {
38
39extern cstring PnaMainParserInputMetaFields[TC::MAX_PNA_PARSER_META];
40extern cstring PnaMainInputMetaFields[TC::MAX_PNA_INPUT_META];
41extern cstring PnaMainOutputMetaFields[TC::MAX_PNA_OUTPUT_META];
42
43class PNAEbpfGenerator;
44
48class ConvertToBackendIR : public Inspector {
49 public:
50 const IR::ToplevelBlock *tlb;
51 IR::TCPipeline *tcPipeline;
52 P4::ReferenceMap *refMap;
53 P4::TypeMap *typeMap;
54 TCOptions &options;
55 unsigned int tableCount = 0;
56 unsigned int actionCount = 0;
57 unsigned int metadataCount = 0;
58 unsigned int labelCount = 0;
59 cstring pipelineName = nullptr;
60 cstring mainParserName = nullptr;
64 ordered_map<unsigned, unsigned> tableKeysizeList;
65
66 public:
67 ConvertToBackendIR(const IR::ToplevelBlock *tlb, IR::TCPipeline *pipe, P4::ReferenceMap *refMap,
68 P4::TypeMap *typeMap, TCOptions &options)
69 : tlb(tlb), tcPipeline(pipe), refMap(refMap), typeMap(typeMap), options(options) {}
70 void setPipelineName();
71 cstring getPipelineName() { return pipelineName; };
72 bool preorder(const IR::P4Program *p) override;
73 void postorder(const IR::P4Action *a) override;
74 void postorder(const IR::P4Table *t) override;
75 void postorder(const IR::P4Program *p) override;
76 bool isDuplicateOrNoAction(const IR::P4Action *action);
77 void updateDefaultHitAction(const IR::P4Table *t, IR::TCTable *tdef);
78 void updateDefaultMissAction(const IR::P4Table *t, IR::TCTable *tdef);
79 void updateConstEntries(const IR::P4Table *t, IR::TCTable *tdef);
80 void updateMatchType(const IR::P4Table *t, IR::TCTable *tabledef);
81 void updateTimerProfiles(IR::TCTable *tabledef);
82 bool isPnaParserMeta(const IR::Member *mem);
83 bool isPnaMainInputMeta(const IR::Member *mem);
84 bool isPnaMainOutputMeta(const IR::Member *mem);
85 unsigned int findMappedKernelMeta(const IR::Member *mem);
86 const IR::Expression *ExtractExpFromCast(const IR::Expression *exp);
87 unsigned getTcType(const IR::StringLiteral *sl);
88 unsigned getTableId(cstring tableName) const;
89 unsigned getActionId(cstring actionName) const;
90 unsigned getTableKeysize(unsigned tableId) const;
91 cstring externalName(const IR::IDeclaration *declaration) const;
92 void updateAddOnMissTable(cstring tblname) const {
93 for (auto table : tcPipeline->tableDefs) {
94 if (table->tableName == tblname) {
95 ((IR::TCTable *)table)->setTableAddOnMiss();
96 }
97 }
98 }
99};
100
101class Extern {
102 public:
103 static const cstring dropPacket;
104 static const cstring sendToPort;
105};
106
107class Backend : public PassManager {
108 public:
109 const IR::ToplevelBlock *toplevel;
110 P4::ReferenceMap *refMap;
111 P4::TypeMap *typeMap;
112 TCOptions &options;
113 IR::TCPipeline *pipeline = new IR::TCPipeline();
116 TC::ParseTCAnnotations *parseTCAnno;
117 const IR::ToplevelBlock *top = nullptr;
118 EbpfOptions ebpfOption;
119 EBPF::Target *target;
120 const PNAEbpfGenerator *ebpf_program;
121
122 public:
123 explicit Backend(const IR::ToplevelBlock *toplevel, P4::ReferenceMap *refMap,
124 P4::TypeMap *typeMap, TCOptions &options)
125 : toplevel(toplevel), refMap(refMap), typeMap(typeMap), options(options) {
126 setName("BackEnd");
127 }
128 bool process();
129 bool ebpfCodeGen(P4::ReferenceMap *refMap, P4::TypeMap *typeMap);
130 void serialize() const;
131 bool serializeIntrospectionJson(std::ostream &out) const;
132 bool emitCFile();
133};
134
135} // namespace TC
136
137#endif /* BACKENDS_TC_BACKEND_H_ */
Definition target.h:40
Definition ebpfOptions.h:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition backend.h:107
Definition backend.h:48
bool isPnaParserMeta(const IR::Member *mem)
Definition backend.cpp:603
Definition backend.h:101
This pass generates introspection JSON into user specified file.
Definition introspection.h:118
Definition ebpfCodeGen.h:29
Definition tcAnnotations.h:25
Definition options.h:26
Definition cstring.h:72
Definition ordered_map.h:30
This file defines functions for the pass to generate the introspection file.
Definition backend.cpp:24