19#ifndef BACKENDS_TOFINO_BF_P4C_CONTROL_PLANE_BFRUNTIME_H_
20#define BACKENDS_TOFINO_BF_P4C_CONTROL_PLANE_BFRUNTIME_H_
33#include "control-plane/p4RuntimeSerializer.h"
34#include "lib/big_int_util.h"
36#include "lib/exceptions.h"
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wunused-parameter"
43#pragma GCC diagnostic ignored "-Wpedantic"
44#include "p4/config/v1/p4info.pb.h"
45#include "p4/config/v1/p4types.pb.h"
46#pragma GCC diagnostic pop
51namespace p4configv1 = ::p4::config::v1;
64template <
typename T,
typename R>
65static constexpr P4Id makeBFRuntimeId(T base, R prefix) {
66 return static_cast<P4Id
>((base & 0xffffff) | (prefix << 24));
69static constexpr P4Id getIdPrefix(P4Id
id) {
return ((
id >> 24) & 0xff); }
74static std::string escapeJson(
const std::string &s) {
100 if (
'\x00' <= c && c <=
'\x1f') {
101 o <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
102 <<
static_cast<int>(c);
112static Util::JsonObject *transformAnnotation(
const std::string &annotation) {
116 annotationJson->emplace(
"name"_cs, escapeJson(annotation));
117 return annotationJson;
120template <
typename It>
121static Util::JsonArray *transformAnnotations(
const It &first,
const It &last) {
123 for (
auto it = first; it != last; it++) annotations->append(transformAnnotation(*it));
127static Util::JsonArray *transformAnnotations(
const p4configv1::Preamble &pre) {
128 return transformAnnotations(pre.annotations().begin(), pre.annotations().end());
133static bool isOfType(P4Id
id, T prefix) {
134 return getIdPrefix(
id) ==
static_cast<P4Id
>(prefix);
139template <
typename It>
140static auto findP4InfoObject(
const It &first,
const It &last, P4Id objectId) ->
const
141 typename std::iterator_traits<It>::value_type * {
142 using T =
typename std::iterator_traits<It>::value_type;
143 auto desiredObject = std::find_if(
144 first, last, [&](
const T &
object) {
return object.preamble().id() == objectId; });
145 if (desiredObject == last)
return nullptr;
146 return &*desiredObject;
149static const p4configv1::Table *findTable(
const p4configv1::P4Info &p4info, P4Id tableId) {
150 const auto &tables = p4info.tables();
151 return findP4InfoObject(tables.begin(), tables.end(), tableId);
154const p4configv1::ActionProfile *findActionProf(
const p4configv1::P4Info &p4info,
157const p4configv1::DirectCounter *findDirectCounter(
const p4configv1::P4Info &p4info,
160const p4configv1::DirectMeter *findDirectMeter(
const p4configv1::P4Info &p4info, P4Id meterId);
166template <typename T, typename std::enable_if<std::is_integral<T>::value,
int>::type = 0>
169 typeObj->emplace(
"type"_cs, type);
170 typeObj->emplace(
"default_value"_cs, defaultValue);
171 if (!mask.isNullOrEmpty()) typeObj->emplace(
"mask"_cs, mask);
175Util::JsonObject *makeTypeBool(std::optional<bool> defaultValue = std::nullopt);
177Util::JsonObject *makeTypeBytes(
int width, std::optional<int64_t> defaultValue = std::nullopt,
181 std::optional<cstring> defaultValue = std::nullopt);
186template <
typename It>
187static std::vector<P4Id> collectTableIds(
const p4configv1::P4Info &p4info,
const It &first,
189 std::vector<P4Id> tableIds;
190 for (
auto it = first; it != last; it++) {
191 auto *table = Standard::findTable(p4info, *it);
192 if (table ==
nullptr) {
193 error(
"Invalid table id '%1%'", *it);
196 tableIds.push_back(*it);
212 using Fields = std::vector<Field>;
213 using iterator = Fields::iterator;
214 using const_iterator = Fields::const_iterator;
217 const p4configv1::P4DataTypeSpec &typeSpec,
cstring instanceType,
219 const std::vector<cstring> *fieldNames =
nullptr,
220 cstring prefix =
""_cs,
cstring suffix =
""_cs, P4Id idOffset = 1);
222 iterator begin() {
return fields.begin(); }
223 const_iterator cbegin() {
return fields.cbegin(); }
224 iterator end() {
return fields.end(); }
225 const_iterator cend() {
return fields.cend(); }
228 explicit TypeSpecParser(Fields &&fields) : fields(
std::move(fields)) {}
248 enum TDDataFieldIds : P4Id {
251 TD_DATA_START = (1 << 16),
253 TD_DATA_MATCH_PRIORITY,
256 TD_DATA_ACTION_MEMBER_ID,
257 TD_DATA_SELECTOR_GROUP_ID,
258 TD_DATA_ACTION_MEMBER_STATUS,
259 TD_DATA_MAX_GROUP_SIZE,
262 TD_DATA_ENTRY_HIT_STATE,
264 TD_DATA_METER_SPEC_CIR_KBPS,
265 TD_DATA_METER_SPEC_PIR_KBPS,
266 TD_DATA_METER_SPEC_CBS_KBITS,
267 TD_DATA_METER_SPEC_PBS_KBITS,
269 TD_DATA_METER_SPEC_CIR_PPS,
270 TD_DATA_METER_SPEC_PIR_PPS,
271 TD_DATA_METER_SPEC_CBS_PKTS,
272 TD_DATA_METER_SPEC_PBS_PKTS,
274 TD_DATA_COUNTER_SPEC_BYTES,
275 TD_DATA_COUNTER_SPEC_PKTS,
278 TD_DATA_COUNTER_INDEX,
279 TD_DATA_REGISTER_INDEX,
286 enum Unit { UNSPECIFIED = 0, BYTES = 1, PACKETS = 2, BOTH = 3 };
293 static std::optional<Counter> from(
const p4configv1::Counter &counterInstance);
294 static std::optional<Counter> fromDirect(
const p4configv1::DirectCounter &counterInstance);
299 enum Unit { UNSPECIFIED = 0, BYTES = 1, PACKETS = 2 };
300 enum Type { COLOR_UNAWARE = 0, COLOR_AWARE = 1 };
308 static std::optional<Meter> from(
const p4configv1::Meter &meterInstance);
309 static std::optional<Meter> fromDirect(
const p4configv1::DirectMeter &meterInstance);
318 std::vector<P4Id> tableIds;
320 static P4Id makeActProfId(P4Id implementationId);
321 static std::optional<ActionProf> from(
const p4configv1::P4Info &p4info,
322 const p4configv1::ActionProfile &actionProfile);
329 p4configv1::P4DataTypeSpec typeSpec;
332 static std::optional<Digest> from(
const p4configv1::Digest &digest);
338 std::string dataFieldName;
341 p4configv1::P4DataTypeSpec typeSpec;
350 virtual bool addActionProfIds(
const p4configv1::Table &table,
361 virtual void addDirectResources(
const p4configv1::Table &table,
Util::JsonArray *dataJson,
364 P4Id maxActionParamId = 0)
const;
366 virtual std::optional<bool> actProfHasSelector(P4Id actProfId)
const;
373 P4Id *maxActionParamId =
nullptr)
const;
374 virtual std::optional<Counter> getDirectCounter(P4Id counterId)
const;
375 virtual std::optional<Meter> getDirectMeter(P4Id meterId)
const;
386 const p4configv1::P4DataTypeSpec &typeSpec,
388 const std::vector<cstring> *fieldNames =
nullptr,
390 P4Id idOffset = 1)
const;
401 static void addActionDataField(
Util::JsonArray *dataJson, P4Id
id,
const std::string &name,
420 P4Id idOffset = 1)
const;
422 const p4configv1::P4Info &p4info;
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:233
Util::JsonArray * makeActionSpecs(const p4configv1::Table &table, P4Id *maxActionParamId=nullptr) const
Definition backends/tofino/bf-p4c/control-plane/bfruntime.cpp:661
virtual const Util::JsonObject * genSchema() const
Generates the schema as a Json object for the provided P4Info instance.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.cpp:967
void transformTypeSpecToDataFields(Util::JsonArray *fieldsJson, const p4configv1::P4DataTypeSpec &typeSpec, cstring instanceType, cstring instanceName, const std::vector< cstring > *fieldNames=nullptr, cstring prefix=""_cs, cstring suffix=""_cs, P4Id idOffset=1) const
Definition backends/tofino/bf-p4c/control-plane/bfruntime.cpp:498
void serializeBFRuntimeSchema(std::ostream *destination)
Definition backends/tofino/bf-p4c/control-plane/bfruntime.cpp:988
void addRegisterDataFields(Util::JsonArray *dataJson, const Register ®ister_, P4Id idOffset=1) const
Definition backends/tofino/bf-p4c/control-plane/bfruntime.cpp:512
Common register representation between PSA and other architectures.
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:336
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:204
Definition backends/tofino/bf-p4c/control-plane/bfruntime.h:206
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
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 p4RuntimeSerializer.h:57