P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
ebpfControl.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_EBPFCONTROL_H_
18#define BACKENDS_EBPF_EBPFCONTROL_H_
19
20#include "ebpfObject.h"
21#include "ebpfTable.h"
22#include "ebpfType.h"
23
24namespace EBPF {
25
26class EBPFControl;
27
29 protected:
30 const EBPFControl *control;
31 std::set<const IR::Parameter *> toDereference;
32 std::vector<cstring> saveAction;
33 P4::P4CoreLibrary &p4lib;
34
35 public:
36 explicit ControlBodyTranslator(const EBPFControl *control);
37
38 // handle the packet_out.emit method
39 virtual void compileEmitField(const IR::Expression *expr, cstring field, unsigned alignment,
40 EBPFType *type);
41 virtual void compileEmit(const IR::Vector<IR::Argument> *args);
42 virtual void processMethod(const P4::ExternMethod *method);
43 virtual void processApply(const P4::ApplyMethod *method);
44 virtual void processFunction(const P4::ExternFunction *function);
45 void processCustomExternFunction(const P4::ExternFunction *function,
46 EBPFTypeFactory *typeFactory);
47
48 bool preorder(const IR::PathExpression *expression) override;
49 bool preorder(const IR::MethodCallExpression *expression) override;
50 bool preorder(const IR::ExitStatement *) override;
51 bool preorder(const IR::ReturnStatement *) override;
52 bool preorder(const IR::IfStatement *statement) override;
53 bool preorder(const IR::SwitchStatement *statement) override;
54 bool preorder(const IR::StructExpression *expr) override;
55};
56
57class EBPFControl : public EBPFObject {
58 public:
59 const EBPFProgram *program;
60 const IR::ControlBlock *controlBlock;
61 const IR::Parameter *headers;
62 const IR::Parameter *accept; // only for ebpfFilter progs
63 const IR::Parameter *xdpInputMeta; // only for xdp progs
64 const IR::Parameter *xdpOutputMeta; // only for xdp progs
65 const IR::Parameter *parserHeaders;
66 // replace references to headers with references to parserHeaders
67 cstring hitVariable;
68 ControlBodyTranslator *codeGen;
69 const bool emitExterns;
70
71 std::set<const IR::Parameter *> toDereference;
72 std::map<cstring, EBPFTable *> tables;
73 std::map<cstring, EBPFCounterTable *> counters;
74
75 EBPFControl(const EBPFProgram *program, const IR::ControlBlock *block,
76 const IR::Parameter *parserHeaders);
77 virtual void emit(CodeBuilder *builder);
78 virtual void emitDeclaration(CodeBuilder *builder, const IR::Declaration *decl);
79 virtual void emitTableTypes(CodeBuilder *builder);
80 virtual void emitTableInitializers(CodeBuilder *builder);
81 virtual void emitTableInstances(CodeBuilder *builder);
82 virtual bool build();
83 EBPFTable *getTable(cstring name) const {
84 auto result = ::get(tables, name);
85 BUG_CHECK(result != nullptr, "No table named %1%", name);
86 return result;
87 }
88 EBPFCounterTable *getCounter(cstring name) const {
89 auto result = ::get(counters, name);
90 BUG_CHECK(result != nullptr, "No counter named %1%", name);
91 return result;
92 }
93
94 protected:
95 void scanConstants();
96
97 DECLARE_TYPEINFO(EBPFControl, EBPFObject);
98};
99
100} // namespace EBPF
101
102#endif /* BACKENDS_EBPF_EBPFCONTROL_H_ */
Definition codeGen.h:33
Definition codeGen.h:41
Definition ebpfControl.h:28
Definition ebpfControl.h:57
Definition ebpfTable.h:150
Definition ebpfObject.h:31
Definition ebpfProgram.h:39
Definition ebpfTable.h:72
Definition ebpfType.h:58
Definition ebpfType.h:29
Definition externInstance.h:33
Definition methodInstance.h:124
Definition methodInstance.h:176
Definition methodInstance.h:144
Definition coreLibrary.h:100
Definition cstring.h:72