P4C
The P4 Compiler
Loading...
Searching...
No Matches
ubpfModel.h
1/*
2 * Copyright 2019 Orange
3 * SPDX-FileCopyrightText: 2019 Orange
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_UBPF_UBPFMODEL_H_
9#define BACKENDS_UBPF_UBPFMODEL_H_
10
11#include "frontends/common/model.h"
12#include "frontends/p4/coreLibrary.h"
13#include "ir/ir.h"
14#include "ir/pass_manager.h"
15#include "lib/cstring.h"
16
17namespace P4::UBPF {
18
19using namespace P4::literals;
20
21struct Pipeline_Model : public ::P4::Model::Elem {
22 Pipeline_Model()
23 : Elem("Pipeline"_cs), parser("prs"_cs), control("p"_cs), deparser("dprs"_cs) {}
24
25 ::P4::Model::Elem parser;
26 ::P4::Model::Elem control;
27 ::P4::Model::Elem deparser;
28};
29
30struct Register_Model : public ::P4::Model::Extern_Model {
31 Register_Model()
32 : Extern_Model("Register"_cs),
33 sizeParam("size"_cs),
34 read("read"_cs),
35 write("write"_cs),
36 initial_value("initial_value"_cs),
37 index("index"_cs),
38 value("value"_cs) {}
39
40 ::P4::Model::Elem sizeParam;
43 ::P4::Model::Elem initial_value;
46};
47
48struct Algorithm_Model : public ::P4::Model::Enum_Model {
49 Algorithm_Model() : ::P4::Model::Enum_Model("HashAlgorithm"_cs), lookup3("lookup3"_cs) {}
50
51 ::P4::Model::Elem lookup3;
52};
53
54struct Hash_Model : public ::P4::Model::Elem {
55 Hash_Model() : ::P4::Model::Elem("hash"_cs) {}
56};
57
58class UBPFModel : public ::P4::Model::Model {
59 protected:
60 UBPFModel()
61 : CPacketName("pkt"_cs),
62 packet("packet"_cs, P4::P4CoreLibrary::instance().packetIn, 0),
63 pipeline(),
64 registerModel(),
65 drop("mark_to_drop"_cs),
66 pass("mark_to_pass"_cs),
67 ubpf_time_get_ns("ubpf_time_get_ns"_cs),
68 truncate("truncate"_cs),
69 csum_replace2("csum_replace2"_cs),
70 csum_replace4("csum_replace4"_cs),
71 hashAlgorithm(),
72 hash() {}
73
74 public:
75 static UBPFModel instance;
76 static cstring reservedPrefix;
77
78 ::P4::Model::Elem CPacketName;
80 Pipeline_Model pipeline;
81 Register_Model registerModel;
84 ::P4::Model::Elem ubpf_time_get_ns;
85 ::P4::Model::Elem truncate;
86 ::P4::Model::Extern_Model csum_replace2;
87 ::P4::Model::Extern_Model csum_replace4;
88 Algorithm_Model hashAlgorithm;
89 Hash_Model hash;
90 unsigned version = 20200515;
91
92 static cstring reserved(cstring name) { return reservedPrefix + name; }
93
94 int numberOfParserArguments() const { return version >= 20200515 ? 4 : 3; }
95 int numberOfControlBlockArguments() const { return version >= 20200515 ? 3 : 2; }
96
97 class getUBPFModelVersion : public Inspector {
98 bool preorder(const IR::Declaration_Constant *dc) override {
99 if (dc->name == "__ubpf_model_version") {
100 auto val = dc->initializer->to<IR::Constant>();
101 UBPFModel::instance.version = static_cast<unsigned>(val->value);
102 }
103 return false;
104 }
105 bool preorder(const IR::Declaration *) override { return false; }
106 };
107
108 const IR::P4Program *run(const IR::P4Program *program) {
109 if (program == nullptr) return nullptr;
110
111 PassManager passes({
113 });
114
115 passes.setName("UBPFFrontEnd");
116 passes.setStopOnError(true);
117 const IR::P4Program *result = program->apply(passes);
118 return result;
119 }
120};
121
122} // namespace P4::UBPF
123
124#endif /* BACKENDS_UBPF_UBPFMODEL_H_ */
Definition frontends/common/model.h:64
Definition ir/pass_manager.h:40
Definition cstring.h:85
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:48
Definition ubpfModel.h:54
Definition ubpfModel.h:21
Definition ubpfModel.h:30