P4C
The P4 Compiler
Loading...
Searching...
No Matches
tofino/bf-p4c/control-plane/bfruntime_ext.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_CONTROL_PLANE_BFRUNTIME_EXT_H_
20#define BACKENDS_TOFINO_BF_P4C_CONTROL_PLANE_BFRUNTIME_EXT_H_
21
22#pragma GCC diagnostic push
23#pragma GCC diagnostic ignored "-Wunused-parameter"
24#pragma GCC diagnostic ignored "-Wpedantic"
25#include "barefoot/p4info.pb.h"
26#pragma GCC diagnostic pop
27#include "backends/tofino/bf-p4c/device.h"
28#include "backends/tofino/bf-utils/include/dynamic_hash/bfn_hash_algorithm.h"
29#include "bfruntime.h"
30
31namespace BFN {
32
33namespace BFRT {
34
35using namespace BFN::BFRT;
36
49 public:
50 explicit BFRuntimeSchemaGenerator(const p4configv1::P4Info &p4info)
51 : BFRuntimeGenerator(p4info) {}
52
54 const Util::JsonObject *genSchema() const override;
55
56 private:
57 // TODO: these values may need to be available to the BF-RT
58 // implementation as well, if they want to expose them as enums.
59
60 // To avoid potential clashes with P4 names, we prefix the names of "fixed"
61 // data field with a '$'. For example, for TD_DATA_ACTION_MEMBER_ID, we
62 // use the name $ACTION_MEMBER_ID.
63 enum BFRuntimeDataFieldIds : P4Id {
64 // ids for fixed data fields must not collide with the auto-generated
65 // ids for P4 fields (e.g. match key fields). Snapshot tables include
66 // ALL the fields defined in the P4 program so we need to ensure that
67 // this BF_RT_DATA_START offset is quite large.
68 // PSA Ids are between TD_DATA_START = (1 << 16) and TD_DATA_END,
69 BF_RT_DATA_START = TD_DATA_END,
70
71 BF_RT_DATA_MATCH_PRIORITY,
72
73 BF_RT_DATA_LPF_INDEX,
74 BF_RT_DATA_WRED_INDEX,
75
76 BF_RT_DATA_ACTION_MEMBER_ID,
77 BF_RT_DATA_SELECTOR_GROUP_ID,
78 BF_RT_DATA_ACTION_MEMBER_STATUS,
79 BF_RT_DATA_MAX_GROUP_SIZE,
80 BF_RT_DATA_ADT_OFFSET,
81
82 BF_RT_DATA_LPF_SPEC_TYPE,
83 BF_RT_DATA_LPF_SPEC_GAIN_TIME_CONSTANT_NS,
84 BF_RT_DATA_LPF_SPEC_DECAY_TIME_CONSTANT_NS,
85 BF_RT_DATA_LPF_SPEC_OUT_SCALE_DOWN_FACTOR,
86
87 BF_RT_DATA_WRED_SPEC_TIME_CONSTANT_NS,
88 BF_RT_DATA_WRED_SPEC_MIN_THRESH_CELLS,
89 BF_RT_DATA_WRED_SPEC_MAX_THRESH_CELLS,
90 BF_RT_DATA_WRED_SPEC_MAX_PROBABILITY,
91
92 BF_RT_DATA_PORT_METADATA_PORT,
93 BF_RT_DATA_PORT_METADATA_DEFAULT_FIELD,
94
95 BF_RT_DATA_HASH_ALGORITHM_ROTATE,
96 BF_RT_DATA_HASH_ALGORITHM_SEED,
97 BF_RT_DATA_HASH_ALGORITHM_MSB,
98 BF_RT_DATA_HASH_ALGORITHM_EXTEND,
99 BF_RT_DATA_HASH_ALGORITHM_PRE_DEFINED,
100 BF_RT_DATA_HASH_ALGORITHM_USER_DEFINED,
101 BF_RT_DATA_HASH_VALUE,
102 BF_RT_DATA_HASH_CONFIGURE_START_BIT,
103 BF_RT_DATA_HASH_CONFIGURE_LENGTH,
104 BF_RT_DATA_HASH_CONFIGURE_ORDER,
105
106 BF_RT_DATA_SNAPSHOT_START_STAGE,
107 BF_RT_DATA_SNAPSHOT_END_STAGE,
108 BF_RT_DATA_SNAPSHOT_ENABLE,
109 BF_RT_DATA_SNAPSHOT_TRIGGER_STATE,
110 BF_RT_DATA_SNAPSHOT_TIMER_ENABLE,
111 BF_RT_DATA_SNAPSHOT_TIMER_TRIGGER,
112 BF_RT_DATA_SNAPSHOT_TIMER_VALUE_USECS,
113 BF_RT_DATA_SNAPSHOT_FIELD_INFO,
114 BF_RT_DATA_SNAPSHOT_CONTROL_INFO,
115 BF_RT_DATA_SNAPSHOT_TABLE_INFO,
116 BF_RT_DATA_SNAPSHOT_METER_ALU_INFO,
117 BF_RT_DATA_SNAPSHOT_STAGE_ID,
118 BF_RT_DATA_SNAPSHOT_PREV_STAGE_TRIGGER,
119 BF_RT_DATA_SNAPSHOT_INGRESS_TRIGGER_MODE,
120 BF_RT_DATA_SNAPSHOT_LOCAL_STAGE_TRIGGER,
121 BF_RT_DATA_SNAPSHOT_NEXT_TABLE_ID,
122 BF_RT_DATA_SNAPSHOT_NEXT_TABLE_NAME,
123 BF_RT_DATA_SNAPSHOT_ENABLED_NEXT_TABLES,
124 BF_RT_DATA_SNAPSHOT_GLOBAL_EXECUTE_TABLES,
125 BF_RT_DATA_SNAPSHOT_ENABLED_GLOBAL_EXECUTE_TABLES,
126 BF_RT_DATA_SNAPSHOT_LONG_BRANCH_TABLES,
127 BF_RT_DATA_SNAPSHOT_ENABLED_LONG_BRANCH_TABLES,
128 BF_RT_DATA_SNAPSHOT_DEPARSER_ERROR,
129 BF_RT_DATA_SNAPSHOT_TABLE_ID,
130 BF_RT_DATA_SNAPSHOT_TABLE_NAME,
131 BF_RT_DATA_SNAPSHOT_METER_TABLE_NAME,
132 BF_RT_DATA_SNAPSHOT_METER_ALU_OPERATION_TYPE,
133 BF_RT_DATA_SNAPSHOT_MATCH_HIT_HANDLE,
134 BF_RT_DATA_SNAPSHOT_TABLE_HIT,
135 BF_RT_DATA_SNAPSHOT_TABLE_INHIBITED,
136 BF_RT_DATA_SNAPSHOT_TABLE_EXECUTED,
137 BF_RT_DATA_SNAPSHOT_LIVENESS_FIELD_NAME,
138 BF_RT_DATA_SNAPSHOT_LIVENESS_VALID_STAGES,
139 BF_RT_DATA_SNAPSHOT_THREAD,
140 BF_RT_DATA_SNAPSHOT_INGRESS_CAPTURE,
141 BF_RT_DATA_SNAPSHOT_EGRESS_CAPTURE,
142 BF_RT_DATA_SNAPSHOT_GHOST_CAPTURE,
143
144 BF_RT_DATA_PARSER_INSTANCE,
145 BF_RT_DATA_PARSER_NAME,
146
147 BF_RT_DATA_DEBUG_CNT_TABLE_NAME,
148 BF_RT_DATA_DEBUG_CNT_TYPE,
149 BF_RT_DATA_DEBUG_CNT_VALUE,
150 };
151 // Externs only for TNA architectures
152 struct Lpf;
153 struct Wred;
154 struct PortMetadata;
155 struct Snapshot;
156 struct DynHash;
157 struct ParserChoices;
158 struct RegisterParam;
159 struct ValueSet;
160 struct ActionSelector;
161
162 static void addLpfDataFields(Util::JsonArray *dataJson);
163 static void addWredDataFields(Util::JsonArray *dataJson);
164
165 void addDebugCounterTable(Util::JsonArray *tablesJson) const;
166 void addDynHash(Util::JsonArray *tablesJson, const DynHash &dynHash) const;
167 void addDynHashAlgorithm(Util::JsonArray *tablesJson, const DynHash &dynHash) const;
168 void addDynHashCompute(Util::JsonArray *tablesJson, const DynHash &dynHash) const;
169 void addDynHashConfig(Util::JsonArray *tablesJson, const DynHash &dynHash) const;
170 void addLpf(Util::JsonArray *tablesJson, const Lpf &lpf) const;
171 void addParserChoices(Util::JsonArray *tablesJson, const ParserChoices &parserChoices) const;
172 void addPortMetadata(Util::JsonArray *tablesJson, const PortMetadata &portMetadata) const;
173 void addPortMetadataDefault(Util::JsonArray *tablesJson) const;
174 void addPortMetadataExtern(Util::JsonArray *tablesJson) const;
175 void addRegisterParam(Util::JsonArray *tablesJson, const RegisterParam &) const;
179 void addRegisterParamDataFields(Util::JsonArray *dataJson, const RegisterParam &register_param_,
180 P4Id idOffset = 1) const;
181 void addSnapshot(Util::JsonArray *tablesJson, const Snapshot &snapshot) const;
182 void addSnapshotLiveness(Util::JsonArray *tablesJson, const Snapshot &snapshot) const;
183 void addTNAExterns(Util::JsonArray *tablesJson, Util::JsonArray *learnFiltersJson) const;
184 void addWred(Util::JsonArray *tablesJson, const Wred &wred) const;
185 void addDirectResources(const p4configv1::Table &table, Util::JsonArray *dataJson,
186 Util::JsonArray *operationsJson, Util::JsonArray *attributesJson,
187 P4Id maxActionParamId) const override;
188 void addValueSet(Util::JsonArray *tablesJson, const ValueSet &valueSet) const;
189 void addActionSelectorCommon(Util::JsonArray *tablesJson,
190 const ActionSelector &actionProf) const;
191 void addActionSelectorGetMemberCommon(Util::JsonArray *tablesJson,
192 const ActionSelector &actionProf) const;
193 void addActionProfs(Util::JsonArray *tablesJson) const override;
194 bool addActionProfIds(const p4configv1::Table &table,
195 Util::JsonObject *tableJson) const override;
196
197 std::optional<bool> actProfHasSelector(P4Id actProfId) const override;
198
199 std::optional<Lpf> getDirectLpf(P4Id lpfId) const;
200 std::optional<Wred> getDirectWred(P4Id wredId) const;
201 std::optional<Counter> getDirectCounter(P4Id counterId) const override;
202 std::optional<Meter> getDirectMeter(P4Id meterId) const override;
203 std::optional<Register> getDirectRegister(P4Id registerId) const;
204
205 static std::optional<ActionProf> fromTNAActionProfile(
206 const p4configv1::P4Info &p4info, const p4configv1::ExternInstance &externInstance) {
207 const auto &pre = externInstance.preamble();
208 ::barefoot::ActionProfile actionProfile;
209 if (!externInstance.info().UnpackTo(&actionProfile)) {
210 error("Extern instance %1% does not pack an ActionProfile object", pre.name());
211 return std::nullopt;
212 }
213 auto tableIds = collectTableIds(p4info, actionProfile.table_ids().begin(),
214 actionProfile.table_ids().end());
215 return ActionProf{pre.name(), pre.id(), actionProfile.size(), tableIds,
216 transformAnnotations(pre)};
217 };
218
219 static std::optional<Counter> fromTNACounter(const p4configv1::ExternInstance &externInstance) {
220 const auto &pre = externInstance.preamble();
221 ::barefoot::Counter counter;
222 if (!externInstance.info().UnpackTo(&counter)) {
223 error("Extern instance %1% does not pack a Counter object", pre.name());
224 return std::nullopt;
225 }
226 auto unit = static_cast<Counter::Unit>(counter.spec().unit());
227 return Counter{pre.name(), pre.id(), counter.size(), unit, transformAnnotations(pre)};
228 }
229
230 static std::optional<Counter> fromTNADirectCounter(
231 const p4configv1::ExternInstance &externInstance) {
232 const auto &pre = externInstance.preamble();
233 ::barefoot::DirectCounter counter;
234 if (!externInstance.info().UnpackTo(&counter)) {
235 error("Extern instance %1% does not pack a DirectCounter object", pre.name());
236 return std::nullopt;
237 }
238 auto unit = static_cast<Counter::Unit>(counter.spec().unit());
239 return Counter{pre.name(), pre.id(), 0, unit, transformAnnotations(pre)};
240 }
241
242 static std::optional<Meter> fromTNAMeter(const p4configv1::ExternInstance &externInstance) {
243 const auto &pre = externInstance.preamble();
244 ::barefoot::Meter meter;
245 if (!externInstance.info().UnpackTo(&meter)) {
246 error("Extern instance %1% does not pack a Meter object", pre.name());
247 return std::nullopt;
248 }
249 auto unit = static_cast<Meter::Unit>(meter.spec().unit());
250 auto type = static_cast<Meter::Type>(meter.spec().type());
251 return Meter{pre.name(), pre.id(), meter.size(), unit, type, transformAnnotations(pre)};
252 }
253
254 static std::optional<Meter> fromTNADirectMeter(
255 const p4configv1::ExternInstance &externInstance) {
256 const auto &pre = externInstance.preamble();
257 ::barefoot::DirectMeter meter;
258 if (!externInstance.info().UnpackTo(&meter)) {
259 error("Extern instance %1% does not pack a Meter object", pre.name());
260 return std::nullopt;
261 }
262 auto unit = static_cast<Meter::Unit>(meter.spec().unit());
263 auto type = static_cast<Meter::Type>(meter.spec().type());
264 return Meter{pre.name(), pre.id(), 0, unit, type, transformAnnotations(pre)};
265 }
266
267 static std::optional<Digest> fromTNADigest(const p4configv1::ExternInstance &externInstance) {
268 const auto &pre = externInstance.preamble();
269 ::barefoot::Digest digest;
270 if (!externInstance.info().UnpackTo(&digest)) {
271 error("Extern instance %1% does not pack a Digest object", pre.name());
272 return std::nullopt;
273 }
274 return Digest{pre.name(), pre.id(), digest.type_spec(), transformAnnotations(pre)};
275 }
276
277 static std::optional<Register> fromTNARegister(
278 const p4configv1::ExternInstance &externInstance) {
279 const auto &pre = externInstance.preamble();
280 ::barefoot::Register register_;
281 if (!externInstance.info().UnpackTo(&register_)) {
282 error("Extern instance %1% does not pack a Register object", pre.name());
283 return std::nullopt;
284 }
285 return Register{pre.name(), register_.data_field_name(), pre.id(),
286 register_.size(), register_.type_spec(), transformAnnotations(pre)};
287 }
288
289 static std::optional<Register> fromTNADirectRegister(
290 const p4configv1::ExternInstance &externInstance) {
291 const auto &pre = externInstance.preamble();
292 ::barefoot::DirectRegister register_;
293 if (!externInstance.info().UnpackTo(&register_)) {
294 error("Extern instance %1% does not pack a Register object", pre.name());
295 return std::nullopt;
296 }
297 return Register{pre.name(), register_.data_field_name(), pre.id(),
298 0, register_.type_spec(), transformAnnotations(pre)};
299 }
300};
301
302} // namespace BFRT
303
304} // namespace BFN
305
306#endif // BACKENDS_TOFINO_BF_P4C_CONTROL_PLANE_BFRUNTIME_EXT_H_
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:233
Common register representation between PSA and other architectures.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:336
Definition tofino/bf-p4c/control-plane/bfruntime_ext.h:48
const Util::JsonObject * genSchema() const override
Generates the schema as a Json object for the provided P4Info instance.
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:612
Definition json.h:115
Definition json.h:164
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:314
Common counter representation between PSA and other architectures.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:285
Common digest representation between PSA and other architectures.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:326
Common meter representation between PSA and other architectures.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:298
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:68
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:224
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:152
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:321
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:205
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:179
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:281
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:130
Definition tofino/bf-p4c/control-plane/bfruntime_ext.cpp:350