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 P4::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
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;
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 = ::P4::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 = ::P4::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 P4::EBPF
101
102#endif /* BACKENDS_EBPF_EBPFCONTROL_H_ */
Definition methodInstance.h:129
Definition ebpf/codeGen.h:33
Definition ebpf/codeGen.h:41
Definition ebpfControl.h:28
virtual void compileEmitField(const IR::Expression *expr, cstring field, unsigned alignment, EBPFType *type)
Handle the packet_out.emit method.
Definition ebpfControl.cpp:156
Definition ebpfControl.h:57
cstring hitVariable
Replace references to headers with references to parserHeaders.
Definition ebpfControl.h:67
Definition ebpfTable.h:150
Base class for EBPF objects.
Definition ebpfObject.h:31
Definition ebpfProgram.h:39
Definition ebpfTable.h:72
Definition ebpfType.h:58
Base class for EBPF types.
Definition ebpfType.h:29
Definition methodInstance.h:194
Definition methodInstance.h:168
Definition vector.h:59
Definition coreLibrary.h:103
Definition cstring.h:85
Definition codeGen.cpp:25