P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfPsaGen.h
1/*
2Copyright 2022-present Orange
3Copyright 2022-present Open Networking Foundation
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17#ifndef BACKENDS_EBPF_PSA_EBPFPSAGEN_H_
18#define BACKENDS_EBPF_PSA_EBPFPSAGEN_H_
19
20#include "backends/ebpf/codeGen.h"
21#include "backends/ebpf/ebpfObject.h"
22#include "backends/ebpf/ebpfOptions.h"
23#include "ebpfPipeline.h"
24#include "ebpfPsaParser.h"
25#include "xdpHelpProgram.h"
26
27namespace P4::EBPF {
28
29enum pipeline_type { TC_INGRESS, TC_EGRESS, XDP_INGRESS, XDP_EGRESS, TC_TRAFFIC_MANAGER };
30
32 public:
33 const EbpfOptions &options;
34 std::vector<EBPF::EBPFType *> ebpfTypes;
35
36 EbpfCodeGenerator(const EbpfOptions &options, std::vector<EBPF::EBPFType *> &ebpfTypes)
37 : options(options), ebpfTypes(ebpfTypes) {}
38
39 virtual void emitCommonPreamble(EBPF::CodeBuilder *builder) const = 0;
40 virtual void emitPreamble(CodeBuilder *builder) const = 0;
41 virtual void emitInternalStructures(EBPF::CodeBuilder *pBuilder) const = 0;
42 virtual void emitTypes(EBPF::CodeBuilder *builder) const = 0;
43 virtual void emitGlobalHeadersMetadata(EBPF::CodeBuilder *builder) const = 0;
44 virtual void emitPipelineInstances(EBPF::CodeBuilder *builder) const = 0;
45};
46
48 public:
49 static constexpr unsigned MaxClones = 64;
50 static constexpr unsigned MaxCloneSessions = 1024;
51
52 EBPFPipeline *ingress;
53 EBPFPipeline *egress;
54
55 PSAEbpfGenerator(const EbpfOptions &options, std::vector<EBPFType *> &ebpfTypes,
56 EBPFPipeline *ingress, EBPFPipeline *egress)
57 : EbpfCodeGenerator(options, ebpfTypes), ingress(ingress), egress(egress) {}
58
59 virtual void emit(CodeBuilder *builder) const = 0;
60 virtual void emitInstances(EBPF::CodeBuilder *builder) const = 0;
61
62 void emitPSAIncludes(CodeBuilder *builder) const;
63 void emitPreamble(CodeBuilder *builder) const override;
64 void emitCommonPreamble(CodeBuilder *builder) const override;
65 void emitInternalStructures(CodeBuilder *pBuilder) const override;
66 void emitTypes(CodeBuilder *builder) const override;
67 void emitGlobalHeadersMetadata(CodeBuilder *builder) const override;
68 void emitPacketReplicationTables(CodeBuilder *builder) const;
69 void emitPipelineInstances(CodeBuilder *builder) const override;
70 void emitInitializer(CodeBuilder *builder) const;
71 virtual void emitInitializerSection(CodeBuilder *builder) const = 0;
72 void emitHelperFunctions(CodeBuilder *builder) const;
73
75 void emitCRC32LookupTableTypes(CodeBuilder *builder) const;
76 void emitCRC32LookupTableInitializer(CodeBuilder *builder) const;
77 void emitCRC32LookupTableInstance(CodeBuilder *builder) const;
78};
79
81 public:
82 XDPHelpProgram *xdp;
83
84 PSAArchTC(const EbpfOptions &options, std::vector<EBPFType *> &ebpfTypes, XDPHelpProgram *xdp,
85 EBPFPipeline *tcIngress, EBPFPipeline *tcEgress)
86 : PSAEbpfGenerator(options, ebpfTypes, tcIngress, tcEgress), xdp(xdp) {}
87
88 void emit(CodeBuilder *builder) const override;
89
90 void emitInstances(CodeBuilder *builder) const override;
91 void emitInitializerSection(CodeBuilder *builder) const override;
92};
93
95 public:
100 static constexpr unsigned egressDevmapSize = 256;
101
102 PSAArchXDP(const EbpfOptions &options, std::vector<EBPFType *> &ebpfTypes,
103 EBPFPipeline *xdpIngress, EBPFPipeline *xdpEgress, EBPFPipeline *tcTrafficManager,
104 EBPFPipeline *tcEgress)
105 : PSAEbpfGenerator(options, ebpfTypes, xdpIngress, xdpEgress),
106 tcIngressForXDP(tcTrafficManager),
107 tcEgressForXDP(tcEgress) {}
108
109 void emit(CodeBuilder *builder) const override;
110
111 void emitPreamble(CodeBuilder *builder) const override;
112 void emitInstances(CodeBuilder *builder) const override;
113 void emitInitializerSection(CodeBuilder *builder) const override;
114
115 void emitXDP2TCInternalStructures(CodeBuilder *builder) const;
116 void emitDummyProgram(CodeBuilder *builder) const;
117};
118
120 const EbpfOptions &options;
121 P4::TypeMap *typemap;
122 P4::ReferenceMap *refmap;
123 const PSAEbpfGenerator *ebpf_psa_arch;
124
125 public:
126 ConvertToEbpfPSA(const EbpfOptions &options, P4::ReferenceMap *refmap, P4::TypeMap *typemap)
127 : options(options), typemap(typemap), refmap(refmap), ebpf_psa_arch(nullptr) {}
128
129 const PSAEbpfGenerator *build(const IR::ToplevelBlock *prog);
130 const IR::Node *preorder(IR::ToplevelBlock *p) override;
131
132 const PSAEbpfGenerator *getPSAArchForEBPF() { return ebpf_psa_arch; }
133};
134
136 const cstring name;
137 const pipeline_type type;
138 const EbpfOptions &options;
139 const IR::ParserBlock *parserBlock;
140 const IR::ControlBlock *controlBlock;
141 const IR::ControlBlock *deparserBlock;
142 P4::TypeMap *typemap;
143 P4::ReferenceMap *refmap;
144 EBPFPipeline *pipeline;
145
146 public:
147 ConvertToEbpfPipeline(cstring name, pipeline_type type, const EbpfOptions &options,
148 const IR::ParserBlock *parserBlock, const IR::ControlBlock *controlBlock,
149 const IR::ControlBlock *deparserBlock, P4::ReferenceMap *refmap,
150 P4::TypeMap *typemap)
151 : name(name),
152 type(type),
153 options(options),
154 parserBlock(parserBlock),
155 controlBlock(controlBlock),
156 deparserBlock(deparserBlock),
157 typemap(typemap),
158 refmap(refmap),
159 pipeline(nullptr) {}
160
161 bool preorder(const IR::PackageBlock *block) override;
162 EBPFPipeline *getEbpfPipeline() { return pipeline; }
163};
164
166 EBPF::EBPFProgram *program;
167 pipeline_type type;
168
169 P4::TypeMap *typemap;
170 EBPF::EBPFPsaParser *parser;
171
172 public:
173 ConvertToEBPFParserPSA(EBPF::EBPFProgram *program, P4::TypeMap *typemap, pipeline_type type)
174 : program(program), type(type), typemap(typemap), parser(nullptr) {}
175
176 bool preorder(const IR::ParserBlock *prsr) override;
177 bool preorder(const IR::P4ValueSet *pvs) override;
178 EBPF::EBPFParser *getEBPFParser() { return parser; }
179};
180
182 EBPF::EBPFProgram *program;
183 pipeline_type type;
184 EBPF::EBPFControlPSA *control;
185
186 const IR::Parameter *parserHeaders;
187 P4::ReferenceMap *refmap;
188
189 public:
190 ConvertToEBPFControlPSA(EBPF::EBPFProgram *program, const IR::Parameter *parserHeaders,
191 P4::ReferenceMap *refmap, pipeline_type type)
192 : program(program),
193 type(type),
194 control(nullptr),
195 parserHeaders(parserHeaders),
196 refmap(refmap) {}
197
198 bool preorder(const IR::TableBlock *) override;
199 bool preorder(const IR::ControlBlock *) override;
200 bool preorder(const IR::Declaration_Variable *) override;
201 bool preorder(const IR::Member *m) override;
202 bool preorder(const IR::IfStatement *a) override;
203 bool preorder(const IR::ExternBlock *instance) override;
204
205 EBPF::EBPFControlPSA *getEBPFControl() { return control; }
206};
207
209 EBPF::EBPFProgram *program;
210 pipeline_type pipelineType;
211
212 const IR::Parameter *parserHeaders;
213 const IR::Parameter *istd;
214 EBPF::EBPFDeparserPSA *deparser;
215
216 public:
217 ConvertToEBPFDeparserPSA(EBPFProgram *program, const IR::Parameter *parserHeaders,
218 const IR::Parameter *istd, pipeline_type type)
219 : program(program),
220 pipelineType(type),
221 parserHeaders(parserHeaders),
222 istd(istd),
223 deparser(nullptr) {}
224
225 bool preorder(const IR::ControlBlock *) override;
226 bool preorder(const IR::Declaration_Instance *) override;
227 EBPF::EBPFDeparserPSA *getEBPFDeparser() { return deparser; }
228};
229
230} // namespace P4::EBPF
231
232#endif /* BACKENDS_EBPF_PSA_EBPFPSAGEN_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfPsaGen.h:181
Definition ebpfPsaGen.h:208
Definition ebpfPsaGen.h:165
Definition ebpfPsaGen.h:119
Definition ebpfPsaGen.h:135
Definition ebpfPsaControl.h:58
Definition ebpfPsaDeparser.h:39
Definition ebpfParser.h:79
EBPFPipeline represents a single eBPF program in the TC/XDP hook.
Definition ebpfPipeline.h:28
Definition ebpfProgram.h:39
Definition ebpfPsaParser.h:40
Definition ebpfPsaGen.h:31
Definition ebpfPsaGen.h:80
Definition ebpfPsaGen.h:94
EBPFPipeline * tcIngressForXDP
TC Ingress program used to support packet cloning in the XDP mode.
Definition ebpfPsaGen.h:97
EBPFPipeline * tcEgressForXDP
If the XDP mode is used, we need to have TC Egress pipeline to handle cloned packets.
Definition ebpfPsaGen.h:99
Definition ebpfPsaGen.h:47
void emitCRC32LookupTableTypes(CodeBuilder *builder) const
TODO: move them to the externs/ebpfPsaHashAlgorithm.cpp file.
Definition ebpfPsaGen.cpp:322
void emitTypes(CodeBuilder *builder) const override
Generate headers and structs in p4 prog.
Definition ebpfPsaGen.cpp:117
Definition xdpHelpProgram.h:24
Definition ebpfOptions.h:26
Definition node.h:95
Definition visitor.h:400
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeMap.h:41
Definition cstring.h:85
Definition codeGen.cpp:25