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