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