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 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;
53 // Deparser may be NULL if not supported (e.g. ebpfFilter package)
54 EBPFDeparser *deparser;
55
56 cstring endLabel, offsetVar, lengthVar, headerStartVar;
57 cstring zeroKey, functionName, errorVar;
58 cstring packetStartVar, packetEndVar, byteVar;
59 cstring errorEnum;
60 cstring license = "GPL"; // TODO: this should be a compiler option probably
61 cstring arrayIndexType = "u32";
62
63 virtual bool build(); // return 'true' on success
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");
79 zeroKey = EBPFModel::reserved("zero");
80 functionName = EBPFModel::reserved("filter");
81 errorVar = EBPFModel::reserved("errorCode");
82 packetStartVar = EBPFModel::reserved("packetStart");
83 packetEndVar = EBPFModel::reserved("packetEnd");
84 headerStartVar = EBPFModel::reserved("headerStart");
85 lengthVar = EBPFModel::reserved("pkt_len");
86 byteVar = EBPFModel::reserved("byte");
87 endLabel = EBPFModel::reserved("end");
88 errorEnum = EBPFModel::reserved("errorCodes");
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
98 public:
99 virtual void emitCommonPreamble(CodeBuilder *builder);
100 virtual void emitGeneratedComment(CodeBuilder *builder);
101 virtual void emitH(CodeBuilder *builder, cstring headerFile); // emits C headers
102 virtual void emitC(CodeBuilder *builder, cstring headerFile); // emits C program
103
104 DECLARE_TYPEINFO(EBPFProgram, EBPFObject);
105};
106
107} // namespace EBPF
108
109#endif /* BACKENDS_EBPF_EBPFPROGRAM_H_ */
Definition codeGen.h:33
Definition ebpfControl.h:57
Definition ebpfDeparser.h:63
Definition ebpfModel.h:64
Definition ebpfObject.h:31
Definition ebpfParser.h:79
Definition ebpfProgram.h:39
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 cstring.h:72