P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfProgram.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
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
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed 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 and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_EBPF_EBPFPROGRAM_H_
18#define BACKENDS_EBPF_EBPFPROGRAM_H_
19
20#include "codeGen.h"
21#include "ebpfModel.h"
22#include "ebpfObject.h"
23#include "ebpfOptions.h"
24#include "frontends/common/options.h"
25#include "frontends/p4/evaluator/evaluator.h"
26#include "frontends/p4/typeMap.h"
27#include "ir/ir.h"
28#include "target.h"
29
30namespace P4::EBPF {
31
32class EBPFProgram;
33class EBPFParser;
34class EBPFControl;
35class EBPFDeparser;
36class EBPFTable;
37class EBPFType;
38
39class EBPFProgram : public EBPFObject {
40 public:
41 // The builder->target defines either TC or XDP target,
42 // while for PSA-eBPF we may use both of them interchangeably.
43 // This field stores the Target object that is unique per eBPF program (pipeline).
44 const Target *progTarget;
45 const EbpfOptions &options;
46 const IR::P4Program *program;
47 const IR::ToplevelBlock *toplevel;
48 P4::ReferenceMap *refMap;
49 P4::TypeMap *typeMap;
50 EBPFParser *parser;
51 EBPFControl *control;
52 EBPFModel &model;
55
56 cstring endLabel, offsetVar, lengthVar, headerStartVar;
57 cstring zeroKey, functionName, errorVar;
58 cstring packetStartVar, packetEndVar, byteVar;
59 cstring errorEnum;
60 cstring license = "GPL"_cs;
62
63 virtual bool build();
64
65 EBPFProgram(const EbpfOptions &options, const IR::P4Program *program, P4::ReferenceMap *refMap,
66 P4::TypeMap *typeMap, const IR::ToplevelBlock *toplevel)
67 : progTarget(nullptr),
68 options(options),
69 program(program),
70 toplevel(toplevel),
71 refMap(refMap),
72 typeMap(typeMap),
73 parser(nullptr),
74 control(nullptr),
75 model(EBPFModel::instance),
76 deparser(nullptr) {
77 // NB: offsetVar not used in eBPF backend - uBPF and TC only
78 offsetVar = EBPFModel::reserved("packetOffsetInBits"_cs);
79 zeroKey = EBPFModel::reserved("zero"_cs);
80 functionName = EBPFModel::reserved("filter"_cs);
81 errorVar = EBPFModel::reserved("errorCode"_cs);
82 packetStartVar = EBPFModel::reserved("packetStart"_cs);
83 packetEndVar = EBPFModel::reserved("packetEnd"_cs);
84 headerStartVar = EBPFModel::reserved("headerStart"_cs);
85 lengthVar = EBPFModel::reserved("pkt_len"_cs);
86 byteVar = EBPFModel::reserved("byte"_cs);
87 endLabel = EBPFModel::reserved("end"_cs);
88 errorEnum = EBPFModel::reserved("errorCodes"_cs);
89 }
90
91 protected:
92 virtual void emitPreamble(CodeBuilder *builder);
93 virtual void emitTypes(CodeBuilder *builder);
94 virtual void emitHeaderInstances(CodeBuilder *builder);
95 virtual void emitLocalVariables(CodeBuilder *builder);
96 virtual void emitPipeline(CodeBuilder *builder);
97
101 virtual bool isLibraryMethod(cstring methodName);
102
103 public:
104 virtual void emitCommonPreamble(CodeBuilder *builder);
105 virtual void emitGeneratedComment(CodeBuilder *builder);
106 virtual void emitH(CodeBuilder *builder,
107 const std::filesystem::path &headerFile); // emits C headers
108 virtual void emitC(CodeBuilder *builder,
109 const std::filesystem::path &headerFile); // emits C program
110
111 DECLARE_TYPEINFO(EBPFProgram, EBPFObject);
112};
113
114} // namespace P4::EBPF
115
116#endif /* BACKENDS_EBPF_EBPFPROGRAM_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfControl.h:57
Definition ebpfDeparser.h:63
Keep this in sync with ebpf_model.p4 and xdp_model.p4.
Definition ebpfModel.h:66
Base class for EBPF objects.
Definition ebpfObject.h:31
Definition ebpfParser.h:79
Definition ebpfProgram.h:39
EBPFProgram(const EbpfOptions &options, const IR::P4Program *program, P4::ReferenceMap *refMap, P4::TypeMap *typeMap, const IR::ToplevelBlock *toplevel)
return 'true' on success
Definition ebpfProgram.h:65
cstring arrayIndexType
TODO: this should be a compiler option probably.
Definition ebpfProgram.h:61
virtual bool isLibraryMethod(cstring methodName)
Definition ebpfProgram.cpp:348
EBPFDeparser * deparser
Deparser may be NULL if not supported (e.g. ebpfFilter package).
Definition ebpfProgram.h:54
Definition ebpf/target.h:44
Definition ebpfOptions.h:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition cstring.h:85
Definition codeGen.cpp:25