P4C
The P4 Compiler
Loading...
Searching...
No Matches
dpdkContext.h
1/*
2 * Copyright 2021 Intel Corp.
3 * SPDX-FileCopyrightText: 2021 Intel Corp.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_DPDK_DPDKCONTEXT_H_
9#define BACKENDS_DPDK_DPDKCONTEXT_H_
10
11#include <regex>
12
13#include "constants.h"
14#include "control-plane/bfruntime.h"
15#include "dpdkProgramStructure.h"
16#include "lib/cstring.h"
17#include "lib/json.h"
18#include "lib/nullstream.h"
19#include "options.h"
20#include "p4/config/v1/p4info.pb.h"
21
22namespace p4configv1 = ::p4::config::v1;
23
28
29namespace P4::DPDK {
30
31using namespace P4::literals;
32
39 unsigned tableHandle;
44 bool is_add_on_miss;
45 bool idle_timeout_with_auto_delete;
46 bool isHidden;
47 unsigned size;
48 cstring controlName;
49 cstring externalName;
50 unsigned default_action_handle;
51 /* Non selector table keys from original P4 program */
52 std::vector<std::pair<cstring, cstring>> tableKeys;
53};
54
58 bool constant_default_action;
59 bool is_compiler_added_action;
60 bool allowed_as_hit_action;
61 bool allowed_as_default_action;
62 unsigned actionHandle;
63 cstring externalName;
65};
66
68 cstring externalName;
69 cstring externType;
70 cstring counterType;
71 unsigned table_id;
72};
73
76 std::string progName;
77 cstring buildDate;
78 cstring compileCommand;
79 cstring compilerVersion;
80 void initTopLevelCtxt(DpdkOptions &options) {
81 buildDate = options.getBuildDate();
82 compileCommand = options.getCompileCommand();
83 progName = options.file.stem();
84 compilerVersion = options.compilerVersion;
85 }
86};
87
90 unsigned max_n_groups;
91 unsigned max_n_members_per_group;
92 unsigned bound_to_action_data_table_handle;
93 void setAttributes(const IR::P4Table *tbl,
94 const std::map<const cstring, struct TableAttributes> &tableAttrmap) {
95 max_n_groups = 0;
96 max_n_members_per_group = 0;
97 auto n_groups = tbl->properties->getProperty("n_groups_max");
98 if (n_groups) {
99 auto n_groups_expr = n_groups->value->to<IR::ExpressionValue>()->expression;
100 max_n_groups = n_groups_expr->to<IR::Constant>()->asInt();
101 }
102 auto n_members = tbl->properties->getProperty("n_members_per_group_max");
103 if (n_members) {
104 auto n_members_expr = n_members->value->to<IR::ExpressionValue>()->expression;
105 max_n_members_per_group = n_members_expr->to<IR::Constant>()->asInt();
106 }
107 // Fetch associated member table handle
108 cstring actionDataTableName = tbl->name.originalName.replace("_sel", "");
109 auto actionTableAttr = ::P4::get(tableAttrmap, actionDataTableName);
110 bound_to_action_data_table_handle = actionTableAttr.tableHandle;
111 }
112};
113
115class DpdkContextGenerator : public Inspector {
116 P4::ReferenceMap *refmap;
117 DpdkProgramStructure *structure;
118 const p4configv1::P4Info &p4info;
119 DpdkOptions &options;
122 std::vector<const IR::Declaration_Instance *> externs;
123
125 std::map<const cstring, struct TableAttributes> tableAttrmap;
126 std::map<cstring, struct actionAttributes> actionAttrMap;
127 std::map<cstring, struct externAttributes> externAttrMap;
128
130 std::map<cstring, size_t> context_handle_map;
131
132 public:
133 DpdkContextGenerator(P4::ReferenceMap *refmap, DpdkProgramStructure *structure,
134 const p4configv1::P4Info &p4info, DpdkOptions &options)
135 : refmap(refmap), structure(structure), p4info(p4info), options(options) {}
136
137 void serializeContextJson(std::ostream *destination);
138 const Util::JsonObject *genContextJsonObject();
139 void addMatchTables(Util::JsonArray *tablesJson);
140 size_t getHandleId(cstring name);
141 void collectHandleId();
142 void addExternInfo(Util::JsonArray *externsJson);
143 Util::JsonObject *initTableCommonJson(const cstring name, const struct TableAttributes &attr);
144 void addKeyField(Util::JsonArray *keyJson, const cstring name, const cstring annon,
145 const IR::KeyElement *key, int position);
146 Util::JsonArray *addActions(const IR::P4Table *table, const cstring ctrlName, bool isMatch);
147 bool addRefTables(const cstring tbl_name, const IR::P4Table **memberTable,
148 Util::JsonObject *tableJson);
149 void addImmediateField(Util::JsonArray *paramJson, const cstring name, int dest_start,
150 int dest_Width);
151 void addActionParam(Util::JsonArray *paramJson, const cstring name, int bitWidth, int position,
152 int byte_array_index);
153 Util::JsonObject *addMatchAttributes(const IR::P4Table *table, const cstring ctrlName);
154 void setActionAttributes(const IR::P4Table *table);
155 void setDefaultActionHandle(const IR::P4Table *table);
157 cstring removePipePrefix(cstring);
158};
159
160} // namespace P4::DPDK
161
162#endif /* BACKENDS_DPDK_DPDKCONTEXT_H_ */
void addActionParam(Util::JsonArray *paramJson, const cstring name, int bitWidth, int position, int byte_array_index)
This function adds a single parameter to the parameters array.
Definition dpdkContext.cpp:360
void addMatchTables(Util::JsonArray *tablesJson)
Add tables to the context json.
Definition dpdkContext.cpp:467
void addExternInfo(Util::JsonArray *externsJson)
Add extern information to the context json.
Definition dpdkContext.cpp:522
void setActionAttributes(const IR::P4Table *table)
This function sets action attributes for actions in a table.
Definition dpdkContext.cpp:226
void addKeyField(Util::JsonArray *keyJson, const cstring name, const cstring annon, const IR::KeyElement *key, int position)
This functions insert a single key field in the match keys array.
Definition dpdkContext.cpp:155
bool addRefTables(const cstring tbl_name, const IR::P4Table **memberTable, Util::JsonObject *tableJson)
This function adds the tables referred by this table.
Definition dpdkContext.cpp:422
Util::JsonArray * addActions(const IR::P4Table *table, const cstring ctrlName, bool isMatch)
This function creates JSON objects for actions within a table.
Definition dpdkContext.cpp:372
Util::JsonObject * initTableCommonJson(const cstring name, const struct TableAttributes &attr)
This function sets the common table properties.
Definition dpdkContext.cpp:181
void addImmediateField(Util::JsonArray *paramJson, const cstring name, int dest_start, int dest_Width)
This functions creates JSON object for immediate fields (action parameters).
Definition dpdkContext.cpp:309
void CollectTablesAndSetAttributes()
Definition dpdkContext.cpp:24
Util::JsonObject * addMatchAttributes(const IR::P4Table *table, const cstring ctrlName)
This functions creates JSON object for match attributes of a table.
Definition dpdkContext.cpp:319
Definition backends/dpdk/options.h:15
Definition indexed_vector.h:31
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition lib/json.h:128
Definition lib/json.h:177
Definition cstring.h:85
Definition dpdk/backend.cpp:26
cstring tableType
Definition dpdkContext.h:43
unsigned tableHandle
Unique ID for the table.
Definition dpdkContext.h:39
cstring direction
Direction of the table, can be ["ingress","egress"].
Definition dpdkContext.h:37
Definition dpdkContext.h:35
Definition dpdkContext.h:57
Definition dpdkContext.h:67
Selection table attributes.
Definition dpdkContext.h:89
Program level information for context json.
Definition dpdkContext.h:75
Collect information related to P4 programs targeting dpdk.
Definition dpdkProgramStructure.h:22