Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries.
 * Microchip VCAP API kunit test suite
 */

#include <kunit/test.h>
#include "vcap_api.h"
#include "vcap_api_client.h"
#include "vcap_api_debugfs.h"
#include "vcap_model_kunit.h"

/* First we have the test infrastructure that emulates the platform
 * implementation
 */
#define TEST_BUF_CNT 100
#define TEST_BUF_SZ  350
#define STREAMWSIZE 64

static u32 test_updateaddr[STREAMWSIZE] = {};
static int test_updateaddridx;
static int test_cache_erase_count;
static u32 test_init_start;
static u32 test_init_count;
static u32 test_hw_counter_id;
static struct vcap_cache_data test_hw_cache;
static struct net_device test_netdev = {};
static int test_move_addr;
static int test_move_offset;
static int test_move_count;
static char test_pr_buffer[TEST_BUF_CNT][TEST_BUF_SZ];
static int test_pr_bufferidx;
static int test_pr_idx;

/* Callback used by the VCAP API */
static enum vcap_keyfield_set test_val_keyset(struct net_device *ndev,
					      struct vcap_admin *admin,
					      struct vcap_rule *rule,
					      struct vcap_keyset_list *kslist,
					      u16 l3_proto)
{
	int idx;

	if (kslist->cnt > 0) {
		switch (admin->vtype) {
		case VCAP_TYPE_IS0:
			for (idx = 0; idx < kslist->cnt; idx++) {
				if (kslist->keysets[idx] == VCAP_KFS_ETAG)
					return kslist->keysets[idx];
				if (kslist->keysets[idx] ==
				    VCAP_KFS_PURE_5TUPLE_IP4)
					return kslist->keysets[idx];
				if (kslist->keysets[idx] ==
				    VCAP_KFS_NORMAL_5TUPLE_IP4)
					return kslist->keysets[idx];
				if (kslist->keysets[idx] ==
				    VCAP_KFS_NORMAL_7TUPLE)
					return kslist->keysets[idx];
			}
			break;
		case VCAP_TYPE_IS2:
			for (idx = 0; idx < kslist->cnt; idx++) {
				if (kslist->keysets[idx] == VCAP_KFS_MAC_ETYPE)
					return kslist->keysets[idx];
				if (kslist->keysets[idx] == VCAP_KFS_ARP)
					return kslist->keysets[idx];
				if (kslist->keysets[idx] == VCAP_KFS_IP_7TUPLE)
					return kslist->keysets[idx];
			}
			break;
		default:
			pr_info("%s:%d: no validation for VCAP %d\n",
				__func__, __LINE__, admin->vtype);
			break;
		}
	}
	return -EINVAL;
}

/* Callback used by the VCAP API */
static void test_add_def_fields(struct net_device *ndev,
				struct vcap_admin *admin,
				struct vcap_rule *rule)
{
	if (admin->vinst == 0 || admin->vinst == 2)
		vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS,
				      VCAP_BIT_1);
	else
		vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS,
				      VCAP_BIT_0);
}

/* Callback used by the VCAP API */
static void test_cache_erase(struct vcap_admin *admin)
{
	if (test_cache_erase_count) {
		memset(admin->cache.keystream, 0, test_cache_erase_count);
		memset(admin->cache.maskstream, 0, test_cache_erase_count);
		memset(admin->cache.actionstream, 0, test_cache_erase_count);
		test_cache_erase_count = 0;
	}
}

/* Callback used by the VCAP API */
static void test_cache_init(struct net_device *ndev, struct vcap_admin *admin,
			    u32 start, u32 count)
{
	test_init_start = start;
	test_init_count = count;
}

/* Callback used by the VCAP API */
static void test_cache_read(struct net_device *ndev, struct vcap_admin *admin,
			    enum vcap_selection sel, u32 start, u32 count)
{
	u32 *keystr, *mskstr, *actstr;
	int idx;

	pr_debug("%s:%d: %d %d\n", __func__, __LINE__, start, count);
	switch (sel) {
	case VCAP_SEL_ENTRY:
		keystr = &admin->cache.keystream[start];
		mskstr = &admin->cache.maskstream[start];
		for (idx = 0; idx < count; ++idx) {
			pr_debug("%s:%d: keydata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, keystr[idx]);
		}
		for (idx = 0; idx < count; ++idx) {
			/* Invert the mask before decoding starts */
			mskstr[idx] = ~mskstr[idx];
			pr_debug("%s:%d: mskdata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, mskstr[idx]);
		}
		break;
	case VCAP_SEL_ACTION:
		actstr = &admin->cache.actionstream[start];
		for (idx = 0; idx < count; ++idx) {
			pr_debug("%s:%d: actdata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, actstr[idx]);
		}
		break;
	case VCAP_SEL_COUNTER:
		pr_debug("%s:%d\n", __func__, __LINE__);
		test_hw_counter_id = start;
		admin->cache.counter = test_hw_cache.counter;
		admin->cache.sticky = test_hw_cache.sticky;
		break;
	case VCAP_SEL_ALL:
		pr_debug("%s:%d\n", __func__, __LINE__);
		break;
	}
}

/* Callback used by the VCAP API */
static void test_cache_write(struct net_device *ndev, struct vcap_admin *admin,
			     enum vcap_selection sel, u32 start, u32 count)
{
	u32 *keystr, *mskstr, *actstr;
	int idx;

	switch (sel) {
	case VCAP_SEL_ENTRY:
		keystr = &admin->cache.keystream[start];
		mskstr = &admin->cache.maskstream[start];
		for (idx = 0; idx < count; ++idx) {
			pr_debug("%s:%d: keydata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, keystr[idx]);
		}
		for (idx = 0; idx < count; ++idx) {
			/* Invert the mask before encoding starts */
			mskstr[idx] = ~mskstr[idx];
			pr_debug("%s:%d: mskdata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, mskstr[idx]);
		}
		break;
	case VCAP_SEL_ACTION:
		actstr = &admin->cache.actionstream[start];
		for (idx = 0; idx < count; ++idx) {
			pr_debug("%s:%d: actdata[%02d]: 0x%08x\n", __func__,
				 __LINE__, start + idx, actstr[idx]);
		}
		break;
	case VCAP_SEL_COUNTER:
		pr_debug("%s:%d\n", __func__, __LINE__);
		test_hw_counter_id = start;
		test_hw_cache.counter = admin->cache.counter;
		test_hw_cache.sticky = admin->cache.sticky;
		break;
	case VCAP_SEL_ALL:
		pr_err("%s:%d: cannot write all streams at once\n",
		       __func__, __LINE__);
		break;
	}
}

/* Callback used by the VCAP API */
static void test_cache_update(struct net_device *ndev, struct vcap_admin *admin,
			      enum vcap_command cmd,
			      enum vcap_selection sel, u32 addr)
{
	if (test_updateaddridx < ARRAY_SIZE(test_updateaddr))
		test_updateaddr[test_updateaddridx] = addr;
	else
		pr_err("%s:%d: overflow: %d\n", __func__, __LINE__,
		       test_updateaddridx);
	test_updateaddridx++;
}

static void test_cache_move(struct net_device *ndev, struct vcap_admin *admin,
			    u32 addr, int offset, int count)
{
	test_move_addr = addr;
	test_move_offset = offset;
	test_move_count = count;
}

/* Provide port information via a callback interface */
static int vcap_test_port_info(struct net_device *ndev,
			       struct vcap_admin *admin,
			       struct vcap_output_print *out)
{
	return 0;
}

static const struct vcap_operations test_callbacks = {
	.validate_keyset = test_val_keyset,
	.add_default_fields = test_add_def_fields,
	.cache_erase = test_cache_erase,
	.cache_write = test_cache_write,
	.cache_read = test_cache_read,
	.init = test_cache_init,
	.update = test_cache_update,
	.move = test_cache_move,
	.port_info = vcap_test_port_info,
};

static struct vcap_control test_vctrl = {
	.vcaps = kunit_test_vcaps,
	.stats = &kunit_test_vcap_stats,
	.ops = &test_callbacks,
};

static void vcap_test_api_init(struct vcap_admin *admin)
{
	/* Initialize the shared objects */
	INIT_LIST_HEAD(&test_vctrl.list);
	INIT_LIST_HEAD(&admin->list);
	INIT_LIST_HEAD(&admin->rules);
	INIT_LIST_HEAD(&admin->enabled);
	mutex_init(&admin->lock);
	list_add_tail(&admin->list, &test_vctrl.list);
	memset(test_updateaddr, 0, sizeof(test_updateaddr));
	test_updateaddridx = 0;
	test_pr_bufferidx = 0;
	test_pr_idx = 0;
}

/* callback used by the show_admin function */
static __printf(2, 3)
int test_prf(void *out, const char *fmt, ...)
{
	static char test_buffer[TEST_BUF_SZ];
	va_list args;
	int idx, cnt;

	if (test_pr_bufferidx >= TEST_BUF_CNT) {
		pr_err("%s:%d: overflow: %d\n", __func__, __LINE__,
		       test_pr_bufferidx);
		return 0;
	}

	va_start(args, fmt);
	cnt = vscnprintf(test_buffer, TEST_BUF_SZ, fmt, args);
	va_end(args);

	for (idx = 0; idx < cnt; ++idx) {
		test_pr_buffer[test_pr_bufferidx][test_pr_idx] =
			test_buffer[idx];
		if (test_buffer[idx] == '\n') {
			test_pr_buffer[test_pr_bufferidx][++test_pr_idx] = 0;
			test_pr_idx = 0;
			test_pr_bufferidx++;
		} else {
			++test_pr_idx;
		}
	}

	return cnt;
}

/* Define the test cases. */

static void vcap_api_addr_keyset_test(struct kunit *test)
{
	u32 keydata[12] = {
		0x40450042, 0x000feaf3, 0x00000003, 0x00050600,
		0x10203040, 0x00075880, 0x633c6864, 0x00040003,
		0x00000020, 0x00000008, 0x00000240, 0x00000000,
	};
	u32 mskdata[12] = {
		0x0030ff80, 0xfff00000, 0xfffffffc, 0xfff000ff,
		0x00000000, 0xfff00000, 0x00000000, 0xfff3fffc,
		0xffffffc0, 0xffffffff, 0xfffffc03, 0xffffffff,
	};
	u32 actdata[12] = {};
	struct vcap_admin admin = {
		.vtype = VCAP_TYPE_IS2,
		.cache = {
			.keystream = keydata,
			.maskstream = mskdata,
			.actionstream = actdata,
		},
	};
	enum vcap_keyfield_set keysets[10];
	struct vcap_keyset_list matches;
	int ret, idx, addr;

	vcap_test_api_init(&admin);

	/* Go from higher to lower addresses searching for a keyset */
	matches.keysets = keysets;
	matches.cnt = 0;
	matches.max = ARRAY_SIZE(keysets);
	for (idx = ARRAY_SIZE(keydata) - 1, addr = 799; idx > 0;
	     --idx, --addr) {
		admin.cache.keystream = &keydata[idx];
		admin.cache.maskstream = &mskdata[idx];
		ret = vcap_addr_keysets(&test_vctrl, &test_netdev, &admin,
					addr, &matches);
		KUNIT_EXPECT_EQ(test, -EINVAL, ret);
	}

	/* Finally we hit the start of the rule */
	admin.cache.keystream = &keydata[idx];
	admin.cache.maskstream = &mskdata[idx];
	matches.cnt = 0;
	ret = vcap_addr_keysets(&test_vctrl, &test_netdev, &admin,
				addr, &matches);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_EQ(test, matches.cnt, 1);
	KUNIT_EXPECT_EQ(test, matches.keysets[0], VCAP_KFS_MAC_ETYPE);
}

static void vcap_api_show_admin_raw_test(struct kunit *test)
{
	u32 keydata[4] = {
		0x40450042, 0x000feaf3, 0x00000003, 0x00050600,
	};
	u32 mskdata[4] = {
		0x0030ff80, 0xfff00000, 0xfffffffc, 0xfff000ff,
	};
	u32 actdata[12] = {};
	struct vcap_admin admin = {
		.vtype = VCAP_TYPE_IS2,
		.cache = {
			.keystream = keydata,
			.maskstream = mskdata,
			.actionstream = actdata,
		},
		.first_valid_addr = 786,
		.last_valid_addr = 788,
	};
	struct vcap_rule_internal ri = {
		.ndev = &test_netdev,
	};
	struct vcap_output_print out = {
		.prf = (void *)test_prf,
	};
	const char *test_expected =
		"  addr: 786, X6 rule, keysets: VCAP_KFS_MAC_ETYPE\n";
	int ret;

	vcap_test_api_init(&admin);
	list_add_tail(&ri.list, &admin.rules);

	ret = vcap_show_admin_raw(&test_vctrl, &admin, &out);
	KUNIT_EXPECT_EQ(test, 0, ret);
	KUNIT_EXPECT_STREQ(test, test_expected, test_pr_buffer[0]);
}

static const char * const test_admin_info_expect[] = {
	"name: is2\n",
	"rows: 256\n",
	"sw_count: 12\n",
	"sw_width: 52\n",
	"sticky_width: 1\n",
	"act_width: 110\n",
	"default_cnt: 73\n",
	"require_cnt_dis: 0\n",
	"version: 1\n",
	"vtype: 4\n",
	"vinst: 0\n",
	"ingress: 1\n",
	"first_cid: 10000\n",
	"last_cid: 19999\n",
	"lookups: 4\n",
	"first_valid_addr: 0\n",
	"last_valid_addr: 3071\n",
	"last_used_addr: 794\n",
};

static void vcap_api_show_admin_test(struct kunit *test)
{
	struct vcap_admin admin = {
		.vtype = VCAP_TYPE_IS2,
		.first_cid = 10000,
		.last_cid = 19999,
		.lookups = 4,
		.last_valid_addr = 3071,
		.first_valid_addr = 0,
		.last_used_addr = 794,
		.ingress = true,
	};
	struct vcap_output_print out = {
		.prf = (void *)test_prf,
	};
	int idx;

	vcap_test_api_init(&admin);

	vcap_show_admin_info(&test_vctrl, &admin, &out);
	for (idx = 0; idx < test_pr_bufferidx; ++idx) {
		/* pr_info("log[%02d]: %s", idx, test_pr_buffer[idx]); */
		KUNIT_EXPECT_STREQ(test, test_admin_info_expect[idx],
				   test_pr_buffer[idx]);
	}
}

static const char * const test_admin_expect[] = {
	"name: is2\n",
	"rows: 256\n",
	"sw_count: 12\n",
	"sw_width: 52\n",
	"sticky_width: 1\n",
	"act_width: 110\n",
	"default_cnt: 73\n",
	"require_cnt_dis: 0\n",
	"version: 1\n",
	"vtype: 4\n",
	"vinst: 0\n",
	"ingress: 1\n",
	"first_cid: 8000000\n",
	"last_cid: 8199999\n",
	"lookups: 4\n",
	"first_valid_addr: 0\n",
	"last_valid_addr: 3071\n",
	"last_used_addr: 794\n",
	"\n",
	"rule: 100, addr: [794,799], X6, ctr[0]: 0, hit: 0\n",
	"  chain_id: 0\n",
	"  user: 0\n",
	"  priority: 0\n",
	"  state: permanent\n",
	"  keysets: VCAP_KFS_MAC_ETYPE\n",
	"  keyset_sw: 6\n",
	"  keyset_sw_regs: 2\n",
	"    ETYPE_LEN_IS: W1: 1/1\n",
	"    IF_IGR_PORT_MASK: W32: 0xffabcd01/0xffffffff\n",
	"    IF_IGR_PORT_MASK_RNG: W4: 5/15\n",
	"    L2_DMAC: W48: 01:02:03:04:05:06/ff:ff:ff:ff:ff:ff\n",
	"    L2_PAYLOAD_ETYPE: W64: 0x9000002000000081/0xff000000000000ff\n",
	"    L2_SMAC: W48: b1:9e:34:32:75:88/ff:ff:ff:ff:ff:ff\n",
	"    LOOKUP_FIRST_IS: W1: 1/1\n",
	"    TYPE: W4: 0/15\n",
	"  actionset: VCAP_AFS_BASE_TYPE\n",
	"  actionset_sw: 3\n",
	"  actionset_sw_regs: 4\n",
	"    CNT_ID: W12: 100\n",
	"    MATCH_ID: W16: 1\n",
	"    MATCH_ID_MASK: W16: 1\n",
	"    POLICE_ENA: W1: 1\n",
	"    PORT_MASK: W68: 0x0514670115f3324589\n",
};

static void vcap_api_show_admin_rule_test(struct kunit *test)
{
	u32 keydata[] = {
		0x40450042, 0x000feaf3, 0x00000003, 0x00050600,
		0x10203040, 0x00075880, 0x633c6864, 0x00040003,
		0x00000020, 0x00000008, 0x00000240, 0x00000000,
	};
	u32 mskdata[] = {
		0x0030ff80, 0xfff00000, 0xfffffffc, 0xfff000ff,
		0x00000000, 0xfff00000, 0x00000000, 0xfff3fffc,
		0xffffffc0, 0xffffffff, 0xfffffc03, 0xffffffff,
	};
	u32 actdata[] = {
		0x00040002, 0xf3324589, 0x14670115, 0x00000005,
		0x00000000, 0x00100000, 0x06400010, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000,
		0x00000000, 0x00000000, 0x00000000, 0x00000000,
	};
	struct vcap_admin admin = {
		.vtype = VCAP_TYPE_IS2,
		.first_cid = 8000000,
		.last_cid = 8199999,
		.lookups = 4,
		.last_valid_addr = 3071,
		.first_valid_addr = 0,
		.last_used_addr = 794,
		.ingress = true,
		.cache = {
			.keystream = keydata,
			.maskstream = mskdata,
			.actionstream = actdata,
		},
	};
	struct vcap_rule_internal ri = {
		.admin = &admin,
		.data = {
			.id = 100,
			.keyset = VCAP_KFS_MAC_ETYPE,
			.actionset = VCAP_AFS_BASE_TYPE,
		},
		.size = 6,
		.keyset_sw = 6,
		.keyset_sw_regs = 2,
		.actionset_sw = 3,
		.actionset_sw_regs = 4,
		.addr = 794,
		.vctrl = &test_vctrl,
	};
	struct vcap_output_print out = {
		.prf = (void *)test_prf,
	};
	int ret, idx;

	vcap_test_api_init(&admin);
	list_add_tail(&ri.list, &admin.rules);

	ret = vcap_show_admin(&test_vctrl, &admin, &out);
	KUNIT_EXPECT_EQ(test, 0, ret);
	for (idx = 0; idx < test_pr_bufferidx; ++idx) {
		/* pr_info("log[%02d]: %s", idx, test_pr_buffer[idx]); */
		KUNIT_EXPECT_STREQ(test, test_admin_expect[idx],
				   test_pr_buffer[idx]);
	}
}

static struct kunit_case vcap_api_debugfs_test_cases[] = {
	KUNIT_CASE(vcap_api_addr_keyset_test),
	KUNIT_CASE(vcap_api_show_admin_raw_test),
	KUNIT_CASE(vcap_api_show_admin_test),
	KUNIT_CASE(vcap_api_show_admin_rule_test),
	{}
};

static struct kunit_suite vcap_api_debugfs_test_suite = {
	.name = "VCAP_API_DebugFS_Testsuite",
	.test_cases = vcap_api_debugfs_test_cases,
};

kunit_test_suite(vcap_api_debugfs_test_suite);