P4C
The P4 Compiler
Loading...
Searching...
No Matches
ebpfPsaHashAlgorithm.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_EXTERNS_EBPFPSAHASHALGORITHM_H_
18#define BACKENDS_EBPF_PSA_EXTERNS_EBPFPSAHASHALGORITHM_H_
19
20#include "backends/ebpf/ebpfObject.h"
21
22namespace P4::EBPF {
23
24class EBPFProgram;
25
27 public:
28 typedef std::vector<const IR::Expression *> ArgumentsList;
29
30 protected:
31 cstring baseName;
32 const EBPFProgram *program;
33 Visitor *visitor;
34
35 ArgumentsList unpackArguments(const IR::MethodCallExpression *expr, int dataPos);
36
37 public:
40 IDENTITY,
41 CRC32,
42 CRC32_CUSTOM,
43 CRC16,
44 CRC16_CUSTOM,
45 ONES_COMPLEMENT16, // aka InternetChecksum
46 TARGET_DEFAULT
47 };
48
49 EBPFHashAlgorithmPSA(const EBPFProgram *program, cstring name)
50 : baseName(name), program(program), visitor(nullptr) {}
51
52 void setVisitor(Visitor *instance) { this->visitor = instance; }
53
54 virtual unsigned getOutputWidth() const { return 0; }
55
57 virtual void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl) = 0;
58
59 virtual void emitClear(CodeBuilder *builder) = 0;
60 virtual void emitAddData(CodeBuilder *builder, int dataPos,
61 const IR::MethodCallExpression *expr);
62 virtual void emitAddData(CodeBuilder *builder, const ArgumentsList &arguments) = 0;
63 virtual void emitGet(CodeBuilder *builder) = 0;
64
65 virtual void emitSubtractData(CodeBuilder *builder, int dataPos,
66 const IR::MethodCallExpression *expr);
67 virtual void emitSubtractData(CodeBuilder *builder, const ArgumentsList &arguments) = 0;
68
69 virtual void emitGetInternalState(CodeBuilder *builder) = 0;
70 virtual void emitSetInternalState(CodeBuilder *builder,
71 const IR::MethodCallExpression *expr) = 0;
72};
73
75 protected:
76 cstring registerVar;
77 cstring initialValue;
78 cstring updateMethod;
79 cstring finalizeMethod;
80 cstring polynomial;
81 const int crcWidth;
82
83 public:
84 CRCChecksumAlgorithm(const EBPFProgram *program, cstring name, int width)
85 : EBPFHashAlgorithmPSA(program, name), crcWidth(width) {}
86
87 unsigned getOutputWidth() const override { return crcWidth; }
88
89 static void emitUpdateMethod(CodeBuilder *builder, int crcWidth);
90
91 void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl) override;
92
93 void emitClear(CodeBuilder *builder) override;
94 void emitAddData(CodeBuilder *builder, const ArgumentsList &arguments) override;
95 void emitGet(CodeBuilder *builder) override;
96
97 void emitSubtractData(CodeBuilder *builder, const ArgumentsList &arguments) override;
98
99 void emitGetInternalState(CodeBuilder *builder) override;
100 void emitSetInternalState(CodeBuilder *builder, const IR::MethodCallExpression *expr) override;
101};
102
110 public:
111 CRC16ChecksumAlgorithm(const EBPFProgram *program, cstring name)
112 : CRCChecksumAlgorithm(program, name, 16) {
113 initialValue = "0"_cs;
114 // We use a 0x8005 polynomial.
115 // 0xA001 comes from 0x8005 value bits reflection.
116 polynomial = "0xA001"_cs;
117 updateMethod = "crc16_update"_cs;
118 finalizeMethod = "crc16_finalize"_cs;
119 }
120
121 static void emitGlobals(CodeBuilder *builder);
122};
123
132 public:
133 CRC32ChecksumAlgorithm(const EBPFProgram *program, cstring name)
134 : CRCChecksumAlgorithm(program, name, 32) {
135 initialValue = "0xffffffff"_cs;
136 // We use a 0x04C11DB7 polynomial.
137 // 0xEDB88320 comes from 0x04C11DB7 value bits reflection.
138 polynomial = "0xEDB88320"_cs;
139 updateMethod = "crc32_update"_cs;
140 finalizeMethod = "crc32_finalize"_cs;
141 }
142
143 static void emitGlobals(CodeBuilder *builder);
144};
145
147 protected:
148 cstring stateVar;
149
150 void updateChecksum(CodeBuilder *builder, const ArgumentsList &arguments, bool addData);
151
152 public:
153 InternetChecksumAlgorithm(const EBPFProgram *program, cstring name)
154 : EBPFHashAlgorithmPSA(program, name) {}
155
156 unsigned getOutputWidth() const override { return 16; }
157
158 static void emitGlobals(CodeBuilder *builder);
159
160 void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl) override;
161
162 void emitClear(CodeBuilder *builder) override;
163 void emitAddData(CodeBuilder *builder, const ArgumentsList &arguments) override;
164 void emitGet(CodeBuilder *builder) override;
165
166 void emitSubtractData(CodeBuilder *builder, const ArgumentsList &arguments) override;
167
168 void emitGetInternalState(CodeBuilder *builder) override;
169 void emitSetInternalState(CodeBuilder *builder, const IR::MethodCallExpression *expr) override;
170};
171
173 public:
174 static EBPFHashAlgorithmTypeFactoryPSA *instance() {
175 static EBPFHashAlgorithmTypeFactoryPSA factory;
176 return &factory;
177 }
178
179 EBPFHashAlgorithmPSA *create(int type, const EBPFProgram *program, cstring name) {
180 if (type == EBPFHashAlgorithmPSA::HashAlgorithm::CRC32)
181 return new CRC32ChecksumAlgorithm(program, name);
182 else if (type == EBPFHashAlgorithmPSA::HashAlgorithm::CRC16)
183 return new CRC16ChecksumAlgorithm(program, name);
184 else if (type == EBPFHashAlgorithmPSA::HashAlgorithm::ONES_COMPLEMENT16 ||
185 type == EBPFHashAlgorithmPSA::HashAlgorithm::TARGET_DEFAULT)
186 return new InternetChecksumAlgorithm(program, name);
187
188 return nullptr;
189 }
190
191 void emitGlobals(CodeBuilder *builder) {
192 CRC16ChecksumAlgorithm::emitGlobals(builder);
193 CRC32ChecksumAlgorithm::emitGlobals(builder);
194 InternetChecksumAlgorithm::emitGlobals(builder);
195 }
196};
197
198} // namespace P4::EBPF
199
200#endif /* BACKENDS_EBPF_PSA_EXTERNS_EBPFPSAHASHALGORITHM_H_ */
Definition ebpfPsaHashAlgorithm.h:109
Definition ebpfPsaHashAlgorithm.h:131
Definition ebpfPsaHashAlgorithm.h:74
void emitAddData(CodeBuilder *builder, const ArgumentsList &arguments) override
Definition ebpfPsaHashAlgorithm.cpp:231
void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl) override
decl might be a null pointer
Definition ebpfPsaHashAlgorithm.cpp:176
Definition ebpf/codeGen.h:33
Definition ebpfPsaHashAlgorithm.h:26
virtual void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl)=0
decl might be a null pointer
HashAlgorithm
Keep this enum in sync with psa.p4 file.
Definition ebpfPsaHashAlgorithm.h:39
Definition ebpfPsaHashAlgorithm.h:172
Base class for EBPF objects.
Definition ebpfObject.h:31
Definition ebpfProgram.h:39
Definition ebpfPsaHashAlgorithm.h:146
void emitVariables(CodeBuilder *builder, const IR::Declaration_Instance *decl) override
decl might be a null pointer
Definition ebpfPsaHashAlgorithm.cpp:486
Definition visitor.h:75
Definition cstring.h:85
Definition codeGen.cpp:25