P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfPsaTable.h
1/*
2 * SPDX-FileCopyrightText: 2022 Open Networking Foundation
3 * SPDX-FileCopyrightText: 2022 Orange
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7#ifndef BACKENDS_EBPF_PSA_EBPFPSATABLE_H_
8#define BACKENDS_EBPF_PSA_EBPFPSATABLE_H_
9
10#include "backends/ebpf/ebpfTable.h"
11#include "backends/ebpf/psa/externs/ebpfPsaCounter.h"
12#include "backends/ebpf/psa/externs/ebpfPsaMeter.h"
13#include "frontends/p4/methodInstance.h"
14
15namespace P4::EBPF {
16
18
19class EBPFTablePSA : public EBPFTable {
20 private:
21 struct ConstTernaryEntryDesc {
22 const IR::Entry *entry;
23 unsigned priority;
24 };
25 typedef std::vector<ConstTernaryEntryDesc> EntriesGroup_t;
26 typedef std::vector<EntriesGroup_t> EntriesGroupedByMask_t;
27 EntriesGroupedByMask_t getConstEntriesGroupedByMask();
28 bool hasConstEntries();
29 const cstring addPrefixFunctionName = "add_prefix_and_entries"_cs;
30 const cstring tuplesMapName = instanceName + "_tuples_map"_cs;
31 const cstring prefixesMapName = instanceName + "_prefixes"_cs;
32
33 protected:
34 ActionTranslationVisitor *createActionTranslationVisitor(
35 cstring valueName, const EBPFProgram *program) const override;
36
37 void initDirectCounters();
38 void initDirectMeters();
39 void initImplementation();
40
41 bool tableCacheEnabled = false;
42 cstring cacheValueTypeName;
43 cstring cacheTableName;
44 cstring cacheKeyTypeName;
45 void tryEnableTableCache();
46 void createCacheTypeNames(bool isCacheKeyType, bool isCacheValueType);
47
48 void emitTableValue(CodeBuilder *builder, const IR::Expression *expr, cstring valueName);
49 void emitDefaultActionInitializer(CodeBuilder *builder);
50 void emitConstEntriesInitializer(CodeBuilder *builder);
51 void emitTernaryConstEntriesInitializer(CodeBuilder *builder);
52 void emitMapUpdateTraceMsg(CodeBuilder *builder, cstring mapName, cstring returnCode) const;
53 void emitValueMask(CodeBuilder *builder, cstring valueMask, cstring nextMask,
54 int tupleId) const;
55 void emitKeyMasks(CodeBuilder *builder, EntriesGroupedByMask_t &entriesGroupedByMask,
56 std::vector<cstring> &keyMasksNames);
57 void emitKeysAndValues(CodeBuilder *builder, EntriesGroup_t &sameMaskEntries,
58 std::vector<cstring> &keyNames, std::vector<cstring> &valueNames);
59
60 public:
63 std::vector<std::pair<cstring, EBPFCounterPSA *>> counters;
64 std::vector<std::pair<cstring, EBPFMeterPSA *>> meters;
65 EBPFTableImplementationPSA *implementation;
66
67 EBPFTablePSA(const EBPFProgram *program, const IR::TableBlock *table,
68 CodeGenInspector *codeGen);
69 EBPFTablePSA(const EBPFProgram *program, CodeGenInspector *codeGen, cstring name);
70
71 void emitInstance(CodeBuilder *builder) override;
72 void emitTypes(CodeBuilder *builder) override;
73 void emitValueStructStructure(CodeBuilder *builder) override;
74 void emitAction(CodeBuilder *builder, cstring valueName, cstring actionRunVariable) override;
75 void emitInitializer(CodeBuilder *builder) override;
76 void emitDirectValueTypes(CodeBuilder *builder) override;
77 void emitLookupDefault(CodeBuilder *builder, cstring key, cstring value,
78 cstring actionRunVariable) override;
79 bool dropOnNoMatchingEntryFound() const override;
80 static cstring addPrefixFunc(bool trace);
81
82 virtual void emitCacheTypes(CodeBuilder *builder);
83 void emitCacheInstance(CodeBuilder *builder);
84 void emitCacheLookup(CodeBuilder *builder, cstring key, cstring value) override;
85 void emitCacheUpdate(CodeBuilder *builder, cstring key, cstring value) override;
86 const IR::PathExpression *getActionNameExpression(const IR::Expression *expr) const;
87 bool cacheEnabled() override { return tableCacheEnabled; }
88
89 EBPFCounterPSA *getDirectCounter(cstring name) const {
90 auto result = std::find_if(counters.begin(), counters.end(),
91 [name](std::pair<cstring, EBPFCounterPSA *> elem) -> bool {
92 return name == elem.first;
93 });
94 if (result != counters.end()) return result->second;
95 return nullptr;
96 }
97
98 EBPFMeterPSA *getMeter(cstring name) const {
99 auto result = std::find_if(
100 meters.begin(), meters.end(),
101 [name](std::pair<cstring, EBPFMeterPSA *> elem) -> bool { return name == elem.first; });
102 if (result != meters.end()) return result->second;
103 return nullptr;
104 }
105
106 bool isMatchTypeSupported(const IR::Declaration_ID *matchType) override {
107 return EBPFTable::isMatchTypeSupported(matchType) || matchType->name.name == "selector";
108 }
109
110 DECLARE_TYPEINFO(EBPFTablePSA, EBPFTable);
111};
112
113class EBPFTablePsaPropertyVisitor : public Inspector {
114 protected:
115 EBPFTablePSA *table;
116
117 public:
118 explicit EBPFTablePsaPropertyVisitor(EBPFTablePSA *table) : table(table) {}
119
123 bool preorder(const IR::ListExpression *) override { return true; }
124 bool preorder(const IR::Expression *expr) override {
125 ::P4::error(ErrorType::ERR_UNSUPPORTED,
126 "%1%: unsupported expression, expected a named instance", expr);
127 return false;
128 }
129
130 void visitTableProperty(cstring propertyName) {
131 auto property = table->table->container->properties->getProperty(propertyName);
132 if (property != nullptr) property->apply(*this);
133 }
134};
135
136} // namespace P4::EBPF
137
138#endif /* BACKENDS_EBPF_PSA_EBPFPSATABLE_H_ */
Definition ebpfTable.h:26
Definition ebpf/codeGen.h:33
Definition ebpf/codeGen.h:41
Definition ebpfPsaCounter.h:16
Definition ebpfProgram.h:30
Base class for ActionProfile and ActionSelector.
Definition ebpfPsaTableImplementation.h:18
Definition ebpfPsaTable.h:19
std::vector< std::pair< cstring, EBPFCounterPSA * > > counters
Definition ebpfPsaTable.h:63
void emitDirectValueTypes(CodeBuilder *builder) override
Definition ebpfPsaTable.cpp:514
bool dropOnNoMatchingEntryFound() const override
Definition ebpfPsaTable.cpp:655
bool preorder(const IR::ListExpression *) override
Definition ebpfPsaTable.h:123
Definition cstring.h:85
Definition codeGen.cpp:25
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58