-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathtest_extern.cpp
406 lines (330 loc) · 12.4 KB
/
test_extern.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/* Copyright 2013-present Barefoot Networks, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Antonin Bas ([email protected])
*
*/
#include <gtest/gtest.h>
#include <bm/bm_sim/extern.h>
#include <bm/bm_sim/P4Objects.h>
#include <string>
#include <utility>
#include <vector>
#include <cassert>
using namespace bm;
class ExternCounter : public ExternType {
public:
static constexpr unsigned int PACKETS = 0;
static constexpr unsigned int BYTES = 1;
BM_EXTERN_ATTRIBUTES {
BM_EXTERN_ATTRIBUTE_ADD(init_count);
BM_EXTERN_ATTRIBUTE_ADD(type);
}
void reset() {
count = init_count_;
}
void init() override {
init_count_ = init_count.get<size_t>();
type_ = type.get<unsigned int>();
reset();
}
void increment() {
if (type_ == PACKETS) {
count++;
} else if (type_ == BYTES) {
count += get_packet().get_ingress_length();
} else {
assert(0);
}
}
void increment_by(const Data &d) {
count += d.get<size_t>();
}
size_t get() const {
return count;
}
private:
// declared attributes
Data init_count{0};
Data type{PACKETS};
// implementation members
unsigned int type_{0};
size_t init_count_{0};
size_t count{0};
};
// do not put these inside an anonymous namespace or some compilers may complain
BM_REGISTER_EXTERN(ExternCounter);
BM_REGISTER_EXTERN_METHOD(ExternCounter, increment);
BM_REGISTER_EXTERN_METHOD(ExternCounter, increment_by, const Data &);
BM_REGISTER_EXTERN_METHOD(ExternCounter, reset);
BM_REGISTER_EXTERN_METHOD(ExternCounter, get);
BM_REGISTER_EXTERN_W_NAME(counter2, ExternCounter);
BM_REGISTER_EXTERN_W_NAME_METHOD(counter2, ExternCounter, increment);
BM_REGISTER_EXTERN_W_NAME_METHOD(counter2, ExternCounter,
increment_by, const Data &);
BM_REGISTER_EXTERN_W_NAME_METHOD(counter2, ExternCounter, reset);
BM_REGISTER_EXTERN_W_NAME_METHOD(counter2, ExternCounter, get);
constexpr unsigned int ExternCounter::PACKETS;
constexpr unsigned int ExternCounter::BYTES;
// this demonstrates how p4objects can be used in an extern for more advanced
// use cases
class ExternP4Objects : public ExternType {
public:
BM_EXTERN_ATTRIBUTES {
BM_EXTERN_ATTRIBUTE_ADD(register_name);
}
void init() override {
register_array = get_p4objects().get_register_array(register_name);
}
size_t read(const Data &idx) const {
const auto idx_v = idx.get<size_t>();
return register_array->at(idx_v).get<size_t>();
}
// not exposed as an extern method, for testing
RegisterArray *get_register_array() const {
return register_array;
}
private:
// declared attributes
std::string register_name{};
// implementation members
RegisterArray *register_array{nullptr};
};
BM_REGISTER_EXTERN(ExternP4Objects);
BM_REGISTER_EXTERN_METHOD(ExternP4Objects, read, const Data &);
// this demonstrates how expressions can be used as extern attributes
// this extern type has 2 attributes: an expression and one integral value
// (var1); it has one additional integral value which is not an attribute but
// can be set by a method (var2). The execute method evaluates the expression,
// using var1 and var2 as locals.
class ExternExpression : public ExternType {
public:
BM_EXTERN_ATTRIBUTES {
BM_EXTERN_ATTRIBUTE_ADD(expr);
BM_EXTERN_ATTRIBUTE_ADD(var1);
}
void init() override {
vars.push_back(var1);
vars.emplace_back(0);
}
void set_var2(const Data &v) {
vars.at(1) = v;
}
void execute(Data &dest) const {
auto phv = get_packet().get_phv();
// 'vars' passed as locals to evaluate expression
expr.eval_arith(*phv, &dest, vars);
}
private:
// declared attributes
Expression expr{};
Data var1{0};
// implementation members
std::vector<Data> vars{};
};
BM_REGISTER_EXTERN(ExternExpression);
BM_REGISTER_EXTERN_METHOD(ExternExpression, set_var2, const Data &);
BM_REGISTER_EXTERN_METHOD(ExternExpression, execute, Data &);
void set_field1(Field &v) {
v.set(1);
}
void set_field2(Field &v) {
v.set(2);
}
void set_field3(Field &v) {
v.set(3);
}
BM_REGISTER_EXTERN_FUNCTION(set_field1, Field &);
BM_REGISTER_EXTERN_FUNCTION_W_NAME(set_field2_test, set_field2, Field &);
BM_REGISTER_EXTERN_FUNCTION_W_NAME(BM_EXTERN_ADD_EXTERN_PREFIX(set_field3),
set_field3, Field &);
namespace {
// Google Test fixture for extern tests
class ExternTest : public ::testing::Test {
protected:
PHVFactory phv_factory;
PHV *phv{nullptr};
HeaderType testHeaderType;
header_id_t testHeader1{0}, testHeader2{1};
ActionFn testActionFn;
ActionFnEntry testActionFnEntry;
std::unique_ptr<PHVSourceIface> phv_source{nullptr};
// pkt needs to be destroyed BEFORE phv_source, otherwise segfault
// not a very clean design...
std::unique_ptr<Packet> pkt{nullptr};
ExternTest()
: testHeaderType("test_t", 0),
testActionFn("test_action", 0, 0),
testActionFnEntry(&testActionFn),
phv_source(PHVSourceIface::make_phv_source()) {
testHeaderType.push_back_field("f32", 32);
testHeaderType.push_back_field("f48", 48);
testHeaderType.push_back_field("f8", 8);
testHeaderType.push_back_field("f16", 16);
testHeaderType.push_back_field("f128", 128);
phv_factory.push_back_header("test1", testHeader1, testHeaderType);
phv_factory.push_back_header("test2", testHeader2, testHeaderType);
}
static std::unique_ptr<ActionPrimitive_> get_extern_primitive(
const std::string &extern_name, const std::string &method_name) {
return ActionOpcodesMap::get_instance()->get_primitive(
"_" + extern_name + "_" + method_name);
}
static std::unique_ptr<ActionPrimitive_> get_extern_primitive(
const std::string &extern_name) {
return ActionOpcodesMap::get_instance()->get_primitive(extern_name);
}
virtual void SetUp() {
phv_source->set_phv_factory(0, &phv_factory);
pkt = std::unique_ptr<Packet>(new Packet(
Packet::make_new(phv_source.get())));
phv = pkt->get_phv();
}
// virtual void TearDown() { }
};
TEST_F(ExternTest, ExternCounterExecute) {
auto extern_instance = ExternFactoryMap::get_instance()->get_extern_instance(
"ExternCounter");
extern_instance->_register_attributes();
extern_instance->init();
auto counter_instance = dynamic_cast<ExternCounter *>(extern_instance.get());
ASSERT_NE(nullptr, counter_instance);
auto primitive = get_extern_primitive("ExternCounter", "increment_by");
size_t increment_value = 44u;
testActionFn.push_back_primitive(primitive.get());
testActionFn.parameter_push_back_extern_instance(extern_instance.get());
testActionFn.parameter_push_back_const(Data(increment_value));
ASSERT_EQ(0u, counter_instance->get());
testActionFnEntry(pkt.get());
ASSERT_EQ(increment_value, counter_instance->get());
counter_instance->reset();
ASSERT_EQ(0u, counter_instance->get());
testActionFnEntry(pkt.get());
ASSERT_EQ(increment_value, counter_instance->get());
}
TEST_F(ExternTest, ExternCounterSetAttribute) {
size_t init_value = 999u;
auto extern_instance = ExternFactoryMap::get_instance()->get_extern_instance(
"ExternCounter");
extern_instance->_register_attributes();
ASSERT_TRUE(extern_instance->_has_attribute("init_count"));
ASSERT_FALSE(extern_instance->_has_attribute("bad_attribute_name"));
extern_instance->_set_attribute<Data>("init_count", Data(init_value));
extern_instance->init();
auto counter_instance = dynamic_cast<ExternCounter *>(extern_instance.get());
ASSERT_NE(nullptr, counter_instance);
auto primitive = get_extern_primitive("ExternCounter", "increment_by");
size_t increment_value = 44u;
testActionFn.push_back_primitive(primitive.get());
testActionFn.parameter_push_back_extern_instance(extern_instance.get());
testActionFn.parameter_push_back_const(Data(increment_value));
testActionFnEntry(pkt.get());
ASSERT_EQ(increment_value + init_value, counter_instance->get());
}
TEST_F(ExternTest, NameAndId) {
auto extern_instance = ExternFactoryMap::get_instance()->get_extern_instance(
"ExternCounter");
const std::string name = "my_extern_instance";
const p4object_id_t id = 22;
extern_instance->_set_name_and_id(name, id);
extern_instance->_register_attributes();
extern_instance->init();
ASSERT_EQ(name, extern_instance->get_name());
ASSERT_EQ(id, extern_instance->get_id());
}
TEST_F(ExternTest, ExternP4Objects) {
auto extern_instance = ExternFactoryMap::get_instance()->get_extern_instance(
"ExternP4Objects");
auto typed_instance = dynamic_cast<ExternP4Objects *>(extern_instance.get());
ASSERT_NE(nullptr, typed_instance);
extern_instance->_register_attributes();
ASSERT_TRUE(extern_instance->_has_attribute("register_name"));
extern_instance->_set_attribute<std::string>("register_name", "my_register");
// build a P4Objects instance with just one register array for testing
std::stringstream is;
is << "{\"register_arrays\":[{\"name\":\"my_register\",\"id\":0,"
<< "\"size\":1024,\"bitwidth\":32}]}";
P4Objects objects;
LookupStructureFactory factory;
ASSERT_EQ(0, objects.init_objects(&is, &factory));
auto register_array = objects.get_register_array("my_register");
ASSERT_NE(nullptr, register_array);
extern_instance->_set_p4objects(&objects);
extern_instance->init();
ASSERT_EQ(register_array, typed_instance->get_register_array());
}
TEST_F(ExternTest, ExternExpression) {
auto extern_instance = ExternFactoryMap::get_instance()->get_extern_instance(
"ExternExpression");
extern_instance->_register_attributes();
Data var1(77), var2(55);
extern_instance->_set_attribute<Data>("var1", var1);
Expression expr;
expr.push_back_load_local(0);
expr.push_back_load_local(1);
expr.push_back_op(ExprOpcode::ADD);
expr.build();
extern_instance->_set_attribute<Expression>("expr", expr);
extern_instance->init();
auto primitive_1 = get_extern_primitive("ExternExpression", "set_var2");
auto primitive_2 = get_extern_primitive("ExternExpression", "execute");
testActionFn.push_back_primitive(primitive_1.get());
testActionFn.parameter_push_back_extern_instance(extern_instance.get());
testActionFn.parameter_push_back_const(var2);
testActionFn.push_back_primitive(primitive_2.get());
testActionFn.parameter_push_back_extern_instance(extern_instance.get());
testActionFn.parameter_push_back_field(testHeader1, 0); // f32
testActionFnEntry(pkt.get());
const auto &dst = pkt->get_phv()->get_field(testHeader1, 0);
const auto expected = var1.get<int>() + var2.get<int>();
ASSERT_EQ(expected, dst.get<int>());
}
TEST_F(ExternTest, ExternCounterRename) {
// check the objects are both retrievable
auto check_name = [](const std::string &name) {
auto extern_instance =
ExternFactoryMap::get_instance()->get_extern_instance(name);
extern_instance->_register_attributes();
extern_instance->init();
auto counter_instance =
dynamic_cast<ExternCounter *>(extern_instance.get());
ASSERT_NE(nullptr, counter_instance);
auto primitive = get_extern_primitive(name, "increment_by");
ASSERT_NE(nullptr, primitive.get());
};
check_name("ExternCounter");
check_name("counter2");
}
class ExternTestTestRegister : public ExternTest,
public ::testing::WithParamInterface<std::pair<const std::string,
const int> > { };
TEST_P(ExternTestTestRegister, TestRegister) {
auto param = GetParam();
auto name = param.first;
auto expected = param.second;
auto primitive = get_extern_primitive(name);
ASSERT_NE(nullptr, primitive.get());
testActionFn.push_back_primitive(primitive.get());
testActionFn.parameter_push_back_field(testHeader1, 0);
testActionFnEntry(pkt.get());
const auto &dst = pkt->get_phv()->get_field(testHeader1, 0);
ASSERT_EQ(expected, dst.get<int>());
}
INSTANTIATE_TEST_SUITE_P(RegisterExternFunction, ExternTestTestRegister,
::testing::Values(
std::make_pair("set_field1", 1),
std::make_pair("set_field2_test", 2),
std::make_pair("__extern_set_field3", 3)));
} // namespace