P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
ebpfDeparser.h
1/*
2Copyright 2022-present Orange
3Copyright 2022-present Open Networking Foundation
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17#ifndef BACKENDS_EBPF_EBPFDEPARSER_H_
18#define BACKENDS_EBPF_EBPFDEPARSER_H_
19
20#include "ebpfControl.h"
21
22namespace EBPF {
23
24class EBPFDeparser;
25
26// this translator emits deparser externs
28 protected:
29 const EBPFDeparser *deparser;
30
31 public:
32 explicit DeparserBodyTranslator(const EBPFDeparser *deparser);
33
34 bool preorder(const IR::MethodCallExpression *expression) override;
35};
36
37// this translator emits buffer preparation (eg. which headers will be emitted)
39 protected:
40 const EBPFDeparser *deparser;
41
42 public:
43 explicit DeparserPrepareBufferTranslator(const EBPFDeparser *deparser);
44
45 void processMethod(const P4::ExternMethod *method) override;
46 bool preorder(const IR::BlockStatement *s) override;
47 bool preorder(const IR::MethodCallExpression *expression) override;
48};
49
50// this translator emits headers
52 protected:
53 const EBPFDeparser *deparser;
54
55 public:
56 explicit DeparserHdrEmitTranslator(const EBPFDeparser *deparser);
57
58 void processMethod(const P4::ExternMethod *method) override;
59 void emitField(CodeBuilder *builder, cstring field, const IR::Expression *hdrExpr,
60 unsigned alignment, EBPF::EBPFType *type);
61};
62
63class EBPFDeparser : public EBPFControl {
64 public:
65 const IR::Parameter *packet_out;
66
67 EBPFType *headerType;
68 cstring outerHdrOffsetVar, outerHdrLengthVar;
69 cstring returnCode;
70
71 EBPFDeparser(const EBPFProgram *program, const IR::ControlBlock *control,
72 const IR::Parameter *parserHeaders)
73 : EBPFControl(program, control, parserHeaders) {
74 codeGen = new DeparserBodyTranslator(this);
75 outerHdrOffsetVar = cstring("outHeaderOffset");
76 outerHdrLengthVar = cstring("outHeaderLength");
77 returnCode = cstring("returnCode");
78 }
79
80 bool build() override;
81 void emit(CodeBuilder *builder) override;
82 // A "PreDeparser" is emitted just before a sequence of hdr.emit() functions.
83 // It is useful in the case of resubmit or clone operation, as these operations
84 // require to have an original packet.
85 virtual void emitPreDeparser(CodeBuilder *builder) { (void)builder; }
86
87 virtual void emitDeparserExternCalls(CodeBuilder *builder) { (void)builder; }
88
89 void emitBufferAdjusts(CodeBuilder *builder) const;
90
91 DECLARE_TYPEINFO(EBPFDeparser, EBPFControl);
92};
93
94} // namespace EBPF
95
96#endif /* BACKENDS_EBPF_EBPFDEPARSER_H_ */
Definition codeGen.h:33
Definition ebpfControl.h:28
Definition ebpfDeparser.h:27
Definition ebpfDeparser.h:51
Definition ebpfDeparser.h:38
Definition ebpfControl.h:57
Definition ebpfDeparser.h:63
Definition ebpfProgram.h:39
Definition ebpfType.h:29
Definition methodInstance.h:144
Definition cstring.h:72