P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
dpdkProgram.h
1/*
2Copyright 2020 Intel Corp.
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_DPDK_DPDKPROGRAM_H_
18#define BACKENDS_DPDK_DPDKPROGRAM_H_
19
20#include "dpdkArch.h"
21#include "dpdkProgramStructure.h"
22#include "frontends/common/constantFolding.h"
23#include "frontends/common/resolveReferences/referenceMap.h"
24#include "frontends/p4/coreLibrary.h"
25#include "frontends/p4/enumInstance.h"
26#include "frontends/p4/evaluator/evaluator.h"
27#include "frontends/p4/methodInstance.h"
28#include "frontends/p4/simplify.h"
29#include "frontends/p4/typeMap.h"
30#include "frontends/p4/unusedDeclarations.h"
31#include "ir/ir.h"
32#include "lib/big_int_util.h"
33#include "lib/json.h"
34#include "options.h"
35
36namespace DPDK {
37
38class ConvertToDpdkProgram : public Transform {
39 P4::TypeMap *typemap;
40 P4::ReferenceMap *refmap;
41 DpdkProgramStructure *structure;
42 DpdkOptions &options;
43 const IR::DpdkAsmProgram *dpdk_program;
44
45 public:
47 DpdkProgramStructure *structure, DpdkOptions &options)
48 : typemap(typemap), refmap(refmap), structure(structure), options(options) {}
49
50 const IR::DpdkAsmProgram *create(IR::P4Program *prog);
51 IR::IndexedVector<IR::DpdkAsmStatement> create_pna_preamble();
52 IR::IndexedVector<IR::DpdkAsmStatement> create_psa_preamble();
53 IR::IndexedVector<IR::DpdkAsmStatement> create_pna_postamble();
54 IR::IndexedVector<IR::DpdkAsmStatement> create_psa_postamble();
55 const IR::Node *preorder(IR::P4Program *p) override;
56 const IR::DpdkAsmProgram *getDpdkProgram() { return dpdk_program; }
57 IR::IndexedVector<IR::DpdkStructType> UpdateHeaderMetadata(IR::P4Program *prog,
58 IR::Type_Struct *metadata);
59};
60
61class ConvertToDpdkParser : public Inspector {
62 IR::IndexedVector<IR::DpdkAsmStatement> instructions;
63 P4::ReferenceMap *refmap;
64 P4::TypeMap *typemap;
65 DpdkProgramStructure *structure;
66 IR::Type_Struct *metadataStruct;
67
68 public:
70 DpdkProgramStructure *structure, IR::Type_Struct *metadataStruct)
71 : refmap(refmap), typemap(typemap), structure(structure), metadataStruct(metadataStruct) {}
72 IR::IndexedVector<IR::DpdkAsmStatement> getInstructions() { return instructions; }
73
74 bool preorder(const IR::P4Parser *a) override;
75 bool preorder(const IR::ParserState *s) override;
76 void add_instr(const IR::DpdkAsmStatement *s) { instructions.push_back(s); }
77 cstring append_parser_name(const IR::P4Parser *p, cstring);
78 IR::Declaration_Variable *addNewTmpVarToMetadata(cstring name, const IR::Type *type);
79 void handleTupleExpression(const IR::ListExpression *cl, const IR::ListExpression *input,
80 int inputSize, cstring trueLabel, cstring falseLabel);
81 void getCondVars(const IR::Expression *sv, const IR::Expression *ce, IR::Expression **leftExpr,
82 IR::Expression **rightExpr);
83};
84
85class ConvertToDpdkControl : public Inspector {
86 P4::TypeMap *typemap;
87 P4::ReferenceMap *refmap;
88 DpdkProgramStructure *structure;
89 IR::Type_Struct *metadataStruct;
90 IR::IndexedVector<IR::DpdkAsmStatement> instructions;
91 IR::IndexedVector<IR::DpdkTable> tables;
92 IR::IndexedVector<IR::DpdkSelector> selectors;
93 IR::IndexedVector<IR::DpdkLearner> learners;
94 IR::IndexedVector<IR::DpdkAction> actions;
95 std::set<cstring> unique_actions;
96 bool deparser;
97
98 public:
100 DpdkProgramStructure *structure, IR::Type_Struct *metadataStruct,
101 bool deparser = false)
102 : typemap(typemap),
103 refmap(refmap),
104 structure(structure),
105 metadataStruct(metadataStruct),
106 deparser(deparser) {}
107
108 IR::IndexedVector<IR::DpdkTable> &getTables() { return tables; }
109 IR::IndexedVector<IR::DpdkSelector> &getSelectors() { return selectors; }
110 IR::IndexedVector<IR::DpdkLearner> &getLearners() { return learners; }
111 IR::IndexedVector<IR::DpdkAction> &getActions() { return actions; }
112 IR::IndexedVector<IR::DpdkAsmStatement> &getInstructions() { return instructions; }
113
114 bool preorder(const IR::P4Action *a) override;
115 bool preorder(const IR::P4Table *a) override;
116 bool preorder(const IR::P4Control *) override;
117 bool checkTableValid(const IR::P4Table *a);
118
119 void add_inst(const IR::DpdkAsmStatement *s) { instructions.push_back(s); }
120 void add_table(const IR::DpdkTable *t) { tables.push_back(t); }
121 void add_table(const IR::DpdkSelector *s) { selectors.push_back(s); }
122 void add_table(const IR::DpdkLearner *s) { learners.push_back(s); }
123 void add_action(const IR::DpdkAction *a) { actions.push_back(a); }
124
125 std::optional<const IR::Member *> getMemExprFromProperty(const IR::P4Table *, cstring);
126 std::optional<int> getNumberFromProperty(const IR::P4Table *, cstring);
127};
128
129class CollectActionUses : public Inspector {
130 ordered_set<cstring> &actions;
131
132 public:
133 explicit CollectActionUses(ordered_set<cstring> &a) : actions(a) {}
134 bool preorder(const IR::ActionListElement *ale) {
135 if (auto mce = ale->expression->to<IR::MethodCallExpression>()) {
136 if (auto path = mce->method->to<IR::PathExpression>()) {
137 if (path->path->name.originalName == "NoAction")
138 actions.insert("NoAction");
139 else
140 actions.insert(path->path->name.name);
141 }
142 }
143 return false;
144 }
145};
146
147class ElimUnusedActions : public Transform {
148 const ordered_set<cstring> &used_actions;
149 std::set<cstring> kept_actions;
150
151 public:
152 explicit ElimUnusedActions(const ordered_set<cstring> &a) : used_actions(a) {}
153 const IR::Node *postorder(IR::DpdkAction *a) override {
154 if (kept_actions.count(a->name.name) != 0) return nullptr;
155 if (used_actions.find(a->name.name) != used_actions.end()) {
156 kept_actions.insert(a->name.name);
157 return a;
158 }
159 return nullptr;
160 }
161};
162
163// frontend generates multiple copies of the same action in the localizeActions pass,
164// they are not useful for dpdk datapath. Removed the duplicated actions in
165// this pass.
166class EliminateUnusedAction : public PassManager {
167 ordered_set<cstring> actions;
168
169 public:
171 addPasses({new CollectActionUses(actions), new ElimUnusedActions(actions)});
172 }
173};
174
175} // namespace DPDK
176#endif /* BACKENDS_DPDK_DPDKPROGRAM_H_ */
Definition dpdkProgram.h:129
Definition dpdkProgram.h:85
Definition dpdkProgram.h:61
Definition dpdkProgram.h:38
const IR::DpdkAsmProgram * create(IR::P4Program *prog)
Definition dpdkProgram.cpp:96
Definition options.h:24
Definition dpdkProgram.h:147
Definition dpdkProgram.h:166
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition cstring.h:72
Definition ordered_set.h:30
Definition backend.cpp:35
Definition dpdkProgramStructure.h:14