17#ifndef BACKENDS_TC_EBPFCODEGEN_H_
18#define BACKENDS_TC_EBPFCODEGEN_H_
26using namespace P4::literals;
41 PNAEbpfGenerator(
const EbpfOptions &options, std::vector<EBPF::EBPFType *> &ebpfTypes,
63class PNAErrorCodesGen :
public Inspector {
69 bool preorder(
const IR::Type_Error *errors)
override {
71 for (
auto decl : errors->members) {
73 if (decl->srcInfo.isValid()) {
74 auto sourceFile = decl->srcInfo.getSourceFile();
76 if (sourceFile.endsWith(
"p4include/core.p4"))
continue;
79 builder->emitIndent();
80 builder->appendFormat(
"static const ParserError_t %v = %d", decl->name,
id);
81 builder->endOfStatement(
true);
86 "%1%: Reached maximum number of possible errors", decl);
96class PNAArchTC :
public PNAEbpfGenerator {
100 PNAArchTC(
const EbpfOptions &options, std::vector<EBPF::EBPFType *> &ebpfTypes,
103 : PNAEbpfGenerator(options, ebpfTypes, pipeline, tcIR), xdp(xdp) {}
132 bool preorder(
const IR::Member *expression)
override;
135 void compileExtractField(
const IR::Expression *expr,
const IR::StructField *field,
137 void compileLookahead(
const IR::Expression *destination)
override;
138 bool preorder(
const IR::SelectCase *selectCase)
override;
139 bool preorder(
const IR::SelectExpression *expression)
override;
140 bool preorder(
const IR::AssignmentStatement *statement)
override;
149 void emitDeclaration(
EBPF::CodeBuilder *builder,
const IR::Declaration *decl)
override;
158 const IR::P4Action *action,
159 bool isDefaultAction)
const;
161 void initDirectCounters();
162 void initDirectMeters();
169 initDirectCounters();
179 bool isMatchTypeSupported(
const IR::Declaration_ID *matchType)
override {
180 if (matchType->name.name ==
"range" || matchType->name.name ==
"rangelist" ||
181 matchType->name.name ==
"optional")
183 return EBPF::EBPFTable::isMatchTypeSupported(matchType);
186 cstring actionRunVariable)
override;
188 cstring p4ActionToActionIDName(
const IR::P4Action *action)
const;
200 bool preorder(
const IR::AssignmentStatement *a)
override;
205 mutable bool touched_skb_metadata;
207 IngressDeparserPNA(
const EBPF::EBPFProgram *program,
const IR::ControlBlock *control,
208 const IR::Parameter *parserHeaders,
const IR::Parameter *istd)
210 touched_skb_metadata(
false) {
214 bool addExternDeclaration =
false;
215 bool build()
override;
218 void emitDeclaration(
EBPF::CodeBuilder *builder,
const IR::Declaration *decl)
override;
221 if (addExternDeclaration) {
222 builder->emitIndent();
223 builder->appendLine(
"struct p4tc_ext_bpf_params ext_params = {};");
231class ConvertToEbpfPNA :
public Transform {
241 : options(options), typemap(typemap), refmap(refmap), ebpf_program(
nullptr), tcIR(tcIR) {}
244 const IR::Node *preorder(IR::ToplevelBlock *p)
override;
250class ConvertToEbpfPipelineTC :
public Inspector {
252 const EBPF::pipeline_type type;
254 const IR::ParserBlock *parserBlock;
255 const IR::ControlBlock *controlBlock;
256 const IR::ControlBlock *deparserBlock;
261 std::vector<EBPF::EBPFType *> ebpfTypes;
264 ConvertToEbpfPipelineTC(
cstring name, EBPF::pipeline_type type,
const EbpfOptions &options,
265 const IR::ParserBlock *parserBlock,
266 const IR::ControlBlock *controlBlock,
269 std::vector<EBPF::EBPFType *> ebpfTypes)
273 parserBlock(parserBlock),
274 controlBlock(controlBlock),
275 deparserBlock(deparserBlock),
280 ebpfTypes(ebpfTypes) {}
282 bool preorder(
const IR::PackageBlock *block)
override;
288class ConvertToEBPFParserPNA :
public Inspector {
295 : program(program), typemap(typemap), parser(
nullptr) {}
297 bool preorder(
const IR::ParserBlock *prsr)
override;
298 bool preorder(
const IR::P4ValueSet *pvs)
override;
304 bool addExternDeclaration =
false;
305 std::map<cstring, EBPFRegisterPNA *> pna_registers;
306 std::map<cstring, EBPFHashPNA *> pna_hashes;
307 std::map<cstring, EBPFRandomPNA *> pna_randoms;
309 mutable bool touched_skb_metadata;
311 EBPFControlPNA(
const EBPF::EBPFProgram *program,
const IR::ControlBlock *control,
312 const IR::Parameter *parserHeaders)
316 auto result = ::P4::get(pna_registers, name);
317 BUG_CHECK(result !=
nullptr,
"No register named %1%", name);
321 auto result = ::P4::get(pna_hashes, name);
325 auto result = ::P4::get(pna_randoms, name);
329 if (addExternDeclaration) {
330 builder->emitIndent();
331 builder->appendLine(
"struct p4tc_ext_bpf_params ext_params = {};");
332 builder->emitIndent();
333 builder->appendLine(
"struct p4tc_ext_bpf_val ext_val = {};");
334 builder->emitIndent();
335 builder->appendLine(
"struct p4tc_ext_bpf_val *ext_val_ptr;");
338 void emitTableTypes(
EBPF::CodeBuilder *builder) { EBPF::EBPFControl::emitTableTypes(builder); }
344class ConvertToEBPFControlPNA :
public Inspector {
346 EBPF::pipeline_type type;
349 const IR::Parameter *parserHeaders;
355 ConvertToEBPFControlPNA(
EBPF::EBPFProgram *program,
const IR::Parameter *parserHeaders,
361 parserHeaders(parserHeaders),
365 bool preorder(
const IR::TableBlock *)
override;
366 bool preorder(
const IR::ControlBlock *)
override;
367 bool preorder(
const IR::Declaration_Variable *)
override;
368 bool preorder(
const IR::Member *m)
override;
369 bool preorder(
const IR::IfStatement *a)
override;
370 bool preorder(
const IR::ExternBlock *instance)
override;
371 bool checkPnaTimestampMem(
const IR::Member *m);
377class ConvertToEBPFDeparserPNA :
public Inspector {
379 const IR::Parameter *parserHeaders;
380 const IR::Parameter *istd;
382 std::vector<EBPF::EBPFType *> ebpfTypes;
386 ConvertToEBPFDeparserPNA(
EBPF::EBPFProgram *program,
const IR::Parameter *parserHeaders,
388 std::vector<EBPF::EBPFType *> ebpfTypes)
390 parserHeaders(parserHeaders),
393 ebpfTypes(ebpfTypes),
396 bool preorder(
const IR::ControlBlock *)
override;
397 bool preorder(
const IR::Declaration_Instance *)
override;
414 virtual cstring getParamName(
const IR::PathExpression *);
415 bool preorder(
const IR::AssignmentStatement *a)
override;
417 bool preorder(
const IR::Member *)
override;
418 bool IsTableAddOnMiss(
const IR::P4Table *table);
419 const IR::P4Action *GetAddOnMissHitAction(
cstring actionName);
420 void ValidateAddOnMissMissAction(
const IR::P4Action *act);
421 bool arithCommon(
const IR::Operation_Binary *,
const char *,
const char *);
422 bool sarithCommon(
const IR::Operation_Binary *,
const char *);
423 bool preorder(
const IR::Concat *)
override;
424 bool preorder(
const IR::Add *)
override;
425 bool preorder(
const IR::Sub *)
override;
426 bool preorder(
const IR::Mul *)
override;
427 bool preorder(
const IR::Cast *)
override;
428 bool preorder(
const IR::Neg *)
override;
429 bool preorder(
const IR::Cmpl *)
override;
430 bool preorder(
const IR::Shl *)
override;
431 bool preorder(
const IR::Shr *)
override;
432 bool preorder(
const IR::Equ *)
override;
433 bool preorder(
const IR::Neq *)
override;
434 bool preorder(
const IR::Lss *)
override;
435 bool preorder(
const IR::Leq *)
override;
436 bool preorder(
const IR::Grt *)
override;
437 bool preorder(
const IR::Geq *)
override;
438 bool preorder(
const IR::BAnd *)
override;
439 bool preorder(
const IR::BOr *)
override;
440 bool preorder(
const IR::BXor *)
override;
441 bool preorder(
const IR::AddSat *)
override;
442 bool preorder(
const IR::SubSat *)
override;
443 bool preorder(
const IR::Constant *)
override;
444 bool bigXSmallMul(
const IR::Expression *,
const IR::Constant *);
445 void visitHostOrder(
const IR::Expression *);
451 public ControlBodyTranslatorPNA {
454 bool isDefaultAction;
460 const IR::P4Action *act,
bool isDefaultAction);
461 bool preorder(
const IR::PathExpression *pe)
override;
462 bool isActionParameter(
const IR::Expression *expression)
const;
465 cstring getParamInstanceName(
const IR::Expression *expression)
const override;
466 cstring getParamName(
const IR::PathExpression *)
override;
Definition methodInstance.h:129
Definition ebpfTable.h:26
Definition ebpf/codeGen.h:33
Definition ebpf/codeGen.h:41
Definition ebpfControl.h:28
Definition ebpfPsaDeparser.h:31
This translator emits buffer preparation (eg. which headers will be emitted)
Definition ebpfDeparser.h:38
Definition ebpfPsaControl.h:59
Definition ebpfDeparser.h:63
Definition ebpfPsaDeparser.h:39
Definition ebpfPsaHashAlgorithm.h:26
Definition ebpfPsaHashAlgorithm.h:172
Definition ebpfParser.h:81
EBPFPipeline represents a single eBPF program in the TC/XDP hook.
Definition ebpfPipeline.h:28
cstring name
A custom name of eBPF program.
Definition ebpfPipeline.h:31
Definition ebpfProgram.h:39
Definition ebpfPsaParser.h:40
Definition ebpfPsaTable.h:29
Base class for EBPF types.
Definition ebpfType.h:29
Definition ebpfPsaGen.h:31
Definition ebpfPsaParser.h:29
Definition ebpfPipeline.h:199
Definition xdpHelpProgram.h:24
Definition ebpfOptions.h:26
Definition methodInstance.h:194
Definition methodInstance.h:168
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition tc/backend.h:327
Definition ebpfCodeGen.h:195
Definition ebpfCodeGen.h:493
Definition ebpfCodeGen.h:302
Definition ebpfCodeGen.h:483
Definition tcExterns.h:181
Definition ebpfCodeGen.h:143
Definition tcExterns.h:321
Definition tcExterns.h:53
void validateKeys() const override
Definition ebpfCodeGen.cpp:2657
Definition ebpfCodeGen.h:203
void emitPreDeparser(EBPF::CodeBuilder *builder) override
Definition ebpfCodeGen.cpp:1432
void emit(EBPF::CodeBuilder *builder) const override
Definition ebpfCodeGen.cpp:179
void emitParser(EBPF::CodeBuilder *builder) const override
Definition ebpfCodeGen.cpp:277
Definition ebpfCodeGen.h:36
void emitLocalVariables(EBPF::CodeBuilder *builder) override
Generates a set of helper variables that are used during packet processing.
Definition ebpfCodeGen.cpp:600
void emitGlobalMetadataInitializer(EBPF::CodeBuilder *builder) override
Definition ebpfCodeGen.cpp:527
void emitTrafficManager(EBPF::CodeBuilder *builder) override
Definition ebpfCodeGen.cpp:569
This file defines functions for the pass to generate the introspection file.
Definition tc/backend.cpp:27
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58