P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpfModel.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_UBPFMODEL_H_
18#define BACKENDS_UBPF_UBPFMODEL_H_
19
20#include "frontends/common/model.h"
21#include "frontends/p4/coreLibrary.h"
22#include "ir/ir.h"
23#include "ir/pass_manager.h"
24#include "lib/cstring.h"
25
26namespace P4::UBPF {
27
28using namespace P4::literals;
29
32 : Elem("Pipeline"_cs), parser("prs"_cs), control("p"_cs), deparser("dprs"_cs) {}
33
34 ::P4::Model::Elem parser;
35 ::P4::Model::Elem control;
36 ::P4::Model::Elem deparser;
37};
38
41 : Extern_Model("Register"_cs),
42 sizeParam("size"_cs),
43 read("read"_cs),
44 write("write"_cs),
45 initial_value("initial_value"_cs),
46 index("index"_cs),
47 value("value"_cs) {}
48
49 ::P4::Model::Elem sizeParam;
52 ::P4::Model::Elem initial_value;
55};
56
58 Algorithm_Model() : ::P4::Model::Enum_Model("HashAlgorithm"_cs), lookup3("lookup3"_cs) {}
59
60 ::P4::Model::Elem lookup3;
61};
62
64 Hash_Model() : ::P4::Model::Elem("hash"_cs) {}
65};
66
68 protected:
69 UBPFModel()
70 : CPacketName("pkt"_cs),
71 packet("packet"_cs, P4::P4CoreLibrary::instance().packetIn, 0),
72 pipeline(),
73 registerModel(),
74 drop("mark_to_drop"_cs),
75 pass("mark_to_pass"_cs),
76 ubpf_time_get_ns("ubpf_time_get_ns"_cs),
77 truncate("truncate"_cs),
78 csum_replace2("csum_replace2"_cs),
79 csum_replace4("csum_replace4"_cs),
80 hashAlgorithm(),
81 hash() {}
82
83 public:
84 static UBPFModel instance;
85 static cstring reservedPrefix;
86
87 ::P4::Model::Elem CPacketName;
89 Pipeline_Model pipeline;
90 Register_Model registerModel;
93 ::P4::Model::Elem ubpf_time_get_ns;
94 ::P4::Model::Elem truncate;
95 ::P4::Model::Extern_Model csum_replace2;
96 ::P4::Model::Extern_Model csum_replace4;
97 Algorithm_Model hashAlgorithm;
98 Hash_Model hash;
99 unsigned version = 20200515;
100
101 static cstring reserved(cstring name) { return reservedPrefix + name; }
102
103 int numberOfParserArguments() const { return version >= 20200515 ? 4 : 3; }
104 int numberOfControlBlockArguments() const { return version >= 20200515 ? 3 : 2; }
105
107 bool preorder(const IR::Declaration_Constant *dc) override {
108 if (dc->name == "__ubpf_model_version") {
109 auto val = dc->initializer->to<IR::Constant>();
110 UBPFModel::instance.version = static_cast<unsigned>(val->value);
111 }
112 return false;
113 }
114 bool preorder(const IR::Declaration *) override { return false; }
115 };
116
117 const IR::P4Program *run(const IR::P4Program *program) {
118 if (program == nullptr) return nullptr;
119
120 PassManager passes({
122 });
123
124 passes.setName("UBPFFrontEnd");
125 passes.setStopOnError(true);
126 const IR::P4Program *result = program->apply(passes);
127 return result;
128 }
129};
130
131} // namespace P4::UBPF
132
133#endif /* BACKENDS_UBPF_UBPFMODEL_H_ */
Definition visitor.h:400
Definition frontends/common/model.h:64
Definition ir/pass_manager.h:40
Definition ubpfModel.h:67
Definition cstring.h:85
Definition cstring.h:80
Definition frontends/common/model.h:28
Enum_Model : Type_Model.
Definition frontends/common/model.h:47
Extern_Model : Type_Model.
Definition frontends/common/model.h:52
Param_Model : Elem.
Definition frontends/common/model.h:57
Definition ubpfModel.h:57
Definition ubpfModel.h:63
Definition ubpfModel.h:30
Definition ubpfModel.h:39