P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpfProgram.h
1/*
2Copyright 2019 Orange
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_UBPF_UBPFPROGRAM_H_
18#define BACKENDS_UBPF_UBPFPROGRAM_H_
19
20#include "backends/ebpf/ebpfOptions.h"
21#include "backends/ebpf/ebpfProgram.h"
22#include "codeGen.h"
23#include "frontends/p4/evaluator/evaluator.h"
24#include "frontends/p4/typeMap.h"
25#include "ir/ir.h"
26#include "target.h"
27#include "ubpfModel.h"
28
29namespace P4::UBPF {
30
31class UBPFControl;
32class UBPFParser;
33class UBPFDeparser;
34class UBPFTable;
35class UBPFType;
36
38 public:
39 UBPFParser *parser{};
40 UBPFControl *control{};
41 UBPFDeparser *deparser{};
42 UBPFModel &model;
43
44 cstring contextVar, outerHdrOffsetVar, outerHdrLengthVar;
45 cstring stdMetadataVar;
46 cstring packetTruncatedSizeVar;
47 cstring arrayIndexType = "uint32_t"_cs;
48
49 UBPFProgram(const EbpfOptions &options, const IR::P4Program *program, P4::ReferenceMap *refMap,
50 P4::TypeMap *typeMap, const IR::ToplevelBlock *toplevel)
51 : EBPF::EBPFProgram(options, program, refMap, typeMap, toplevel),
52 model(UBPFModel::instance) {
53 packetStartVar = cstring("pkt");
54 offsetVar = cstring("packetOffsetInBits");
55 outerHdrOffsetVar = cstring("outHeaderOffset");
56 outerHdrLengthVar = cstring("outHeaderLength");
57 contextVar = cstring("ctx");
58 lengthVar = cstring("pkt_len");
59 endLabel = cstring("deparser");
60 stdMetadataVar = cstring("std_meta");
61 packetTruncatedSizeVar = cstring("packetTruncatedSize");
62 }
63
64 bool build() override;
65 void emitC(UbpfCodeBuilder *builder, const std::filesystem::path &headerFile);
66 void emitH(EBPF::CodeBuilder *builder, const std::filesystem::path &headerFile) override;
67 void emitPreamble(EBPF::CodeBuilder *builder) override;
68 void emitTypes(EBPF::CodeBuilder *builder) override;
69 void emitTableDefinition(EBPF::CodeBuilder *builder) const;
70 void emitPktVariable(UbpfCodeBuilder *builder) const;
71 void emitPacketLengthVariable(UbpfCodeBuilder *builder) const;
72 void emitHeaderInstances(EBPF::CodeBuilder *builder) override;
73 void emitMetadataInstance(EBPF::CodeBuilder *builder) const;
74 void emitLocalVariables(EBPF::CodeBuilder *builder) override;
75 void emitPipeline(EBPF::CodeBuilder *builder) override;
76
77 bool isLibraryMethod(cstring methodName) override {
78 static std::set<cstring> DEFAULT_METHODS = {
79 "mark_to_drop"_cs, "mark_to_pass"_cs, "ubpf_time_get_ns"_cs, "truncate"_cs,
80 "hash"_cs, "csum_replace2"_cs, "csum_replace4"_cs,
81 };
82 return DEFAULT_METHODS.find(methodName) != DEFAULT_METHODS.end() ||
83 EBPFProgram::isLibraryMethod(methodName);
84 }
85};
86
87} // namespace P4::UBPF
88
89#endif /* BACKENDS_UBPF_UBPFPROGRAM_H_ */
Definition ebpf/codeGen.h:33
Definition ebpfProgram.h:39
Definition ebpfOptions.h:26
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition ubpfControl.h:59
Definition ubpfDeparser.h:64
Definition ubpfModel.h:67
Definition ubpfParser.h:33
Definition ubpfProgram.h:37
bool isLibraryMethod(cstring methodName) override
Definition ubpfProgram.h:77
Definition ubpf/codeGen.h:10
Definition cstring.h:85