P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
dpdkContext.h
1/*
2Copyright 2021 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_DPDKCONTEXT_H_
18#define BACKENDS_DPDK_DPDKCONTEXT_H_
19
20#include <regex>
21
22#include "constants.h"
23#include "control-plane/bfruntime.h"
24#include "dpdkProgramStructure.h"
25#include "lib/json.h"
26#include "lib/nullstream.h"
27#include "options.h"
28#include "p4/config/v1/p4info.pb.h"
29
30namespace p4configv1 = ::p4::config::v1;
38namespace DPDK {
39
40/* This structure holds table attributes required for context JSON which are not
41 part of P4Table */
43 // Direction of the table, can be ["ingress","egress"]
44 cstring direction;
45 // Unique ID for the table
46 unsigned tableHandle;
47 /* Table type can one of "match", "selection" and "action
48 Match table is a regular P4 table, selection table and action tables are compiler
49 generated tables when psa_implementation is action_selector or action_profile */
50 cstring tableType;
51 bool is_add_on_miss;
52 bool idle_timeout_with_auto_delete;
53 bool isHidden;
54 unsigned size;
55 cstring controlName;
56 cstring externalName;
57 unsigned default_action_handle;
58 /* Non selector table keys from original P4 program */
59 std::vector<std::pair<cstring, cstring>> tableKeys;
60};
61
62/* This structure holds action attributes required for context JSON which are not
63 part of P4Action */
65 bool constant_default_action;
66 bool is_compiler_added_action;
67 bool allowed_as_hit_action;
68 bool allowed_as_default_action;
69 unsigned actionHandle;
70 cstring externalName;
71 IR::IndexedVector<IR::Parameter> *params;
72};
73
75 cstring externalName;
76 cstring externType;
77 cstring counterType;
78 unsigned table_id;
79};
80
81/* Program level information for context json */
83 cstring progName;
84 cstring buildDate;
85 cstring compileCommand;
86 cstring compilerVersion;
87 void initTopLevelCtxt(DpdkOptions &options) {
88 buildDate = options.getBuildDate();
89 compileCommand = options.getCompileCommand();
90 progName = options.file;
91 auto fileName = progName.findlast('/');
92 // Handle the case when input file is in the current working directory.
93 // fileName would be null in that case, hence progName should remain unchanged.
94 if (fileName) progName = fileName;
95 auto fileext = progName.find(".");
96 progName = progName.replace(fileext, "");
97 progName = progName.trim("/\t\n\r");
98 compilerVersion = options.compilerVersion;
99 }
100};
101
102/* Selection table attributes */
104 unsigned max_n_groups;
105 unsigned max_n_members_per_group;
106 unsigned bound_to_action_data_table_handle;
107 void setAttributes(const IR::P4Table *tbl,
108 const std::map<const cstring, struct TableAttributes> &tableAttrmap) {
109 max_n_groups = 0;
110 max_n_members_per_group = 0;
111 auto n_groups = tbl->properties->getProperty("n_groups_max");
112 if (n_groups) {
113 auto n_groups_expr = n_groups->value->to<IR::ExpressionValue>()->expression;
114 max_n_groups = n_groups_expr->to<IR::Constant>()->asInt();
115 }
116 auto n_members = tbl->properties->getProperty("n_members_per_group_max");
117 if (n_members) {
118 auto n_members_expr = n_members->value->to<IR::ExpressionValue>()->expression;
119 max_n_members_per_group = n_members_expr->to<IR::Constant>()->asInt();
120 }
121 // Fetch associated member table handle
122 cstring actionDataTableName = tbl->name.originalName;
123 actionDataTableName = actionDataTableName.replace("_sel", "");
124 auto actionTableAttr = ::get(tableAttrmap, actionDataTableName);
125 bound_to_action_data_table_handle = actionTableAttr.tableHandle;
126 }
127};
128
129// This pass generates context JSON into user specified file
130class DpdkContextGenerator : public Inspector {
131 P4::ReferenceMap *refmap;
132 DpdkProgramStructure *structure;
133 const p4configv1::P4Info &p4info;
134 DpdkOptions &options;
135 // All tables are collected into this vector
136 IR::IndexedVector<IR::Declaration> tables;
137 std::vector<const IR::Declaration_Instance *> externs;
138
139 // Maps holding table, extern and action attributes needed for context JSON
140 std::map<const cstring, struct TableAttributes> tableAttrmap;
141 std::map<cstring, struct actionAttributes> actionAttrMap;
142 std::map<cstring, struct externAttributes> externAttrMap;
143
144 // Running unique ID for tables and actions
145 std::map<cstring, size_t> context_handle_map;
146
147 public:
149 const p4configv1::P4Info &p4info, DpdkOptions &options)
150 : refmap(refmap), structure(structure), p4info(p4info), options(options) {}
151
152 void serializeContextJson(std::ostream *destination);
153 const Util::JsonObject *genContextJsonObject();
154 void addMatchTables(Util::JsonArray *tablesJson);
155 size_t getHandleId(cstring name);
156 void collectHandleId();
157 void addExternInfo(Util::JsonArray *externsJson);
158 Util::JsonObject *initTableCommonJson(const cstring name, const struct TableAttributes &attr);
159 void addKeyField(Util::JsonArray *keyJson, const cstring name, const cstring annon,
160 const IR::KeyElement *key, int position);
161 Util::JsonArray *addActions(const IR::P4Table *table, const cstring ctrlName, bool isMatch);
162 bool addRefTables(const cstring tbl_name, const IR::P4Table **memberTable,
163 Util::JsonObject *tableJson);
164 void addImmediateField(Util::JsonArray *paramJson, const cstring name, int dest_start,
165 int dest_Width);
166 void addActionParam(Util::JsonArray *paramJson, const cstring name, int bitWidth, int position,
167 int byte_array_index);
168 Util::JsonObject *addMatchAttributes(const IR::P4Table *table, const cstring ctrlName);
169 void setActionAttributes(const IR::P4Table *table);
170 void setDefaultActionHandle(const IR::P4Table *table);
171 void CollectTablesAndSetAttributes();
172 cstring removePipePrefix(cstring);
173};
174
175} // namespace DPDK
176
177#endif /* BACKENDS_DPDK_DPDKCONTEXT_H_ */
Definition dpdkContext.h:130
Definition options.h:24
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition json.h:111
Definition json.h:158
Definition cstring.h:72
Definition backend.cpp:35
Definition dpdkContext.h:42
Definition dpdkContext.h:64
Definition dpdkContext.h:74
Definition dpdkContext.h:103
Definition dpdkContext.h:82
Definition dpdkProgramStructure.h:14