Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.15.
   1// SPDX-License-Identifier: BSD-3-Clause
   2/* Copyright (C) 2022 Microchip Technology Inc. and its subsidiaries.
   3 * Microchip VCAP API kunit test suite
   4 */
   5
   6#include <kunit/test.h>
   7#include "vcap_api.h"
   8#include "vcap_api_client.h"
   9#include "vcap_model_kunit.h"
  10
  11/* First we have the test infrastructure that emulates the platform
  12 * implementation
  13 */
  14#define TEST_BUF_CNT 100
  15#define TEST_BUF_SZ  350
  16#define STREAMWSIZE 64
  17
  18static u32 test_updateaddr[STREAMWSIZE] = {};
  19static int test_updateaddridx;
  20static int test_cache_erase_count;
  21static u32 test_init_start;
  22static u32 test_init_count;
  23static u32 test_hw_counter_id;
  24static struct vcap_cache_data test_hw_cache;
  25static struct net_device test_netdev = {};
  26static int test_move_addr;
  27static int test_move_offset;
  28static int test_move_count;
  29
  30/* Callback used by the VCAP API */
  31static enum vcap_keyfield_set test_val_keyset(struct net_device *ndev,
  32					      struct vcap_admin *admin,
  33					      struct vcap_rule *rule,
  34					      struct vcap_keyset_list *kslist,
  35					      u16 l3_proto)
  36{
  37	int idx;
  38
  39	if (kslist->cnt > 0) {
  40		switch (admin->vtype) {
  41		case VCAP_TYPE_IS0:
  42			for (idx = 0; idx < kslist->cnt; idx++) {
  43				if (kslist->keysets[idx] == VCAP_KFS_ETAG)
  44					return kslist->keysets[idx];
  45				if (kslist->keysets[idx] == VCAP_KFS_PURE_5TUPLE_IP4)
  46					return kslist->keysets[idx];
  47				if (kslist->keysets[idx] == VCAP_KFS_NORMAL_5TUPLE_IP4)
  48					return kslist->keysets[idx];
  49				if (kslist->keysets[idx] == VCAP_KFS_NORMAL_7TUPLE)
  50					return kslist->keysets[idx];
  51			}
  52			break;
  53		case VCAP_TYPE_IS2:
  54			for (idx = 0; idx < kslist->cnt; idx++) {
  55				if (kslist->keysets[idx] == VCAP_KFS_MAC_ETYPE)
  56					return kslist->keysets[idx];
  57				if (kslist->keysets[idx] == VCAP_KFS_ARP)
  58					return kslist->keysets[idx];
  59				if (kslist->keysets[idx] == VCAP_KFS_IP_7TUPLE)
  60					return kslist->keysets[idx];
  61			}
  62			break;
  63		default:
  64			pr_info("%s:%d: no validation for VCAP %d\n",
  65				__func__, __LINE__, admin->vtype);
  66			break;
  67		}
  68	}
  69	return -EINVAL;
  70}
  71
  72/* Callback used by the VCAP API */
  73static void test_add_def_fields(struct net_device *ndev,
  74				struct vcap_admin *admin,
  75				struct vcap_rule *rule)
  76{
  77	if (admin->vinst == 0 || admin->vinst == 2)
  78		vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS, VCAP_BIT_1);
  79	else
  80		vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS, VCAP_BIT_0);
  81}
  82
  83/* Callback used by the VCAP API */
  84static void test_cache_erase(struct vcap_admin *admin)
  85{
  86	if (test_cache_erase_count) {
  87		memset(admin->cache.keystream, 0, test_cache_erase_count);
  88		memset(admin->cache.maskstream, 0, test_cache_erase_count);
  89		memset(admin->cache.actionstream, 0, test_cache_erase_count);
  90		test_cache_erase_count = 0;
  91	}
  92}
  93
  94/* Callback used by the VCAP API */
  95static void test_cache_init(struct net_device *ndev, struct vcap_admin *admin,
  96			    u32 start, u32 count)
  97{
  98	test_init_start = start;
  99	test_init_count = count;
 100}
 101
 102/* Callback used by the VCAP API */
 103static void test_cache_read(struct net_device *ndev, struct vcap_admin *admin,
 104			    enum vcap_selection sel, u32 start, u32 count)
 105{
 106	u32 *keystr, *mskstr, *actstr;
 107	int idx;
 108
 109	pr_debug("%s:%d: %d %d\n", __func__, __LINE__, start, count);
 110	switch (sel) {
 111	case VCAP_SEL_ENTRY:
 112		keystr = &admin->cache.keystream[start];
 113		mskstr = &admin->cache.maskstream[start];
 114		for (idx = 0; idx < count; ++idx) {
 115			pr_debug("%s:%d: keydata[%02d]: 0x%08x\n", __func__,
 116				 __LINE__, start + idx, keystr[idx]);
 117		}
 118		for (idx = 0; idx < count; ++idx) {
 119			/* Invert the mask before decoding starts */
 120			mskstr[idx] = ~mskstr[idx];
 121			pr_debug("%s:%d: mskdata[%02d]: 0x%08x\n", __func__,
 122				 __LINE__, start + idx, mskstr[idx]);
 123		}
 124		break;
 125	case VCAP_SEL_ACTION:
 126		actstr = &admin->cache.actionstream[start];
 127		for (idx = 0; idx < count; ++idx) {
 128			pr_debug("%s:%d: actdata[%02d]: 0x%08x\n", __func__,
 129				 __LINE__, start + idx, actstr[idx]);
 130		}
 131		break;
 132	case VCAP_SEL_COUNTER:
 133		pr_debug("%s:%d\n", __func__, __LINE__);
 134		test_hw_counter_id = start;
 135		admin->cache.counter = test_hw_cache.counter;
 136		admin->cache.sticky = test_hw_cache.sticky;
 137		break;
 138	case VCAP_SEL_ALL:
 139		pr_debug("%s:%d\n", __func__, __LINE__);
 140		break;
 141	}
 142}
 143
 144/* Callback used by the VCAP API */
 145static void test_cache_write(struct net_device *ndev, struct vcap_admin *admin,
 146			     enum vcap_selection sel, u32 start, u32 count)
 147{
 148	u32 *keystr, *mskstr, *actstr;
 149	int idx;
 150
 151	switch (sel) {
 152	case VCAP_SEL_ENTRY:
 153		keystr = &admin->cache.keystream[start];
 154		mskstr = &admin->cache.maskstream[start];
 155		for (idx = 0; idx < count; ++idx) {
 156			pr_debug("%s:%d: keydata[%02d]: 0x%08x\n", __func__,
 157				 __LINE__, start + idx, keystr[idx]);
 158		}
 159		for (idx = 0; idx < count; ++idx) {
 160			/* Invert the mask before encoding starts */
 161			mskstr[idx] = ~mskstr[idx];
 162			pr_debug("%s:%d: mskdata[%02d]: 0x%08x\n", __func__,
 163				 __LINE__, start + idx, mskstr[idx]);
 164		}
 165		break;
 166	case VCAP_SEL_ACTION:
 167		actstr = &admin->cache.actionstream[start];
 168		for (idx = 0; idx < count; ++idx) {
 169			pr_debug("%s:%d: actdata[%02d]: 0x%08x\n", __func__,
 170				 __LINE__, start + idx, actstr[idx]);
 171		}
 172		break;
 173	case VCAP_SEL_COUNTER:
 174		pr_debug("%s:%d\n", __func__, __LINE__);
 175		test_hw_counter_id = start;
 176		test_hw_cache.counter = admin->cache.counter;
 177		test_hw_cache.sticky = admin->cache.sticky;
 178		break;
 179	case VCAP_SEL_ALL:
 180		pr_err("%s:%d: cannot write all streams at once\n",
 181		       __func__, __LINE__);
 182		break;
 183	}
 184}
 185
 186/* Callback used by the VCAP API */
 187static void test_cache_update(struct net_device *ndev, struct vcap_admin *admin,
 188			      enum vcap_command cmd,
 189			      enum vcap_selection sel, u32 addr)
 190{
 191	if (test_updateaddridx < ARRAY_SIZE(test_updateaddr))
 192		test_updateaddr[test_updateaddridx] = addr;
 193	else
 194		pr_err("%s:%d: overflow: %d\n", __func__, __LINE__, test_updateaddridx);
 195	test_updateaddridx++;
 196}
 197
 198static void test_cache_move(struct net_device *ndev, struct vcap_admin *admin,
 199			    u32 addr, int offset, int count)
 200{
 201	test_move_addr = addr;
 202	test_move_offset = offset;
 203	test_move_count = count;
 204}
 205
 206/* Provide port information via a callback interface */
 207static int vcap_test_port_info(struct net_device *ndev,
 208			       struct vcap_admin *admin,
 209			       struct vcap_output_print *out)
 210{
 211	return 0;
 212}
 213
 214static const struct vcap_operations test_callbacks = {
 215	.validate_keyset = test_val_keyset,
 216	.add_default_fields = test_add_def_fields,
 217	.cache_erase = test_cache_erase,
 218	.cache_write = test_cache_write,
 219	.cache_read = test_cache_read,
 220	.init = test_cache_init,
 221	.update = test_cache_update,
 222	.move = test_cache_move,
 223	.port_info = vcap_test_port_info,
 224};
 225
 226static struct vcap_control test_vctrl = {
 227	.vcaps = kunit_test_vcaps,
 228	.stats = &kunit_test_vcap_stats,
 229	.ops = &test_callbacks,
 230};
 231
 232static void vcap_test_api_init(struct vcap_admin *admin)
 233{
 234	/* Initialize the shared objects */
 235	INIT_LIST_HEAD(&test_vctrl.list);
 236	INIT_LIST_HEAD(&admin->list);
 237	INIT_LIST_HEAD(&admin->rules);
 238	INIT_LIST_HEAD(&admin->enabled);
 239	mutex_init(&admin->lock);
 240	list_add_tail(&admin->list, &test_vctrl.list);
 241	memset(test_updateaddr, 0, sizeof(test_updateaddr));
 242	test_updateaddridx = 0;
 243}
 244
 245/* Helper function to create a rule of a specific size */
 246static void test_vcap_xn_rule_creator(struct kunit *test, int cid,
 247				      enum vcap_user user, u16 priority,
 248				      int id, int size, int expected_addr)
 249{
 250	struct vcap_rule *rule;
 251	struct vcap_rule_internal *ri;
 252	enum vcap_keyfield_set keyset = VCAP_KFS_NO_VALUE;
 253	enum vcap_actionfield_set actionset = VCAP_AFS_NO_VALUE;
 254	int ret;
 255
 256	/* init before testing */
 257	memset(test_updateaddr, 0, sizeof(test_updateaddr));
 258	test_updateaddridx = 0;
 259	test_move_addr = 0;
 260	test_move_offset = 0;
 261	test_move_count = 0;
 262
 263	switch (size) {
 264	case 2:
 265		keyset = VCAP_KFS_ETAG;
 266		actionset = VCAP_AFS_CLASS_REDUCED;
 267		break;
 268	case 3:
 269		keyset = VCAP_KFS_PURE_5TUPLE_IP4;
 270		actionset = VCAP_AFS_CLASSIFICATION;
 271		break;
 272	case 6:
 273		keyset = VCAP_KFS_NORMAL_5TUPLE_IP4;
 274		actionset = VCAP_AFS_CLASSIFICATION;
 275		break;
 276	case 12:
 277		keyset = VCAP_KFS_NORMAL_7TUPLE;
 278		actionset = VCAP_AFS_FULL;
 279		break;
 280	default:
 281		break;
 282	}
 283
 284	/* Check that a valid size was used */
 285	KUNIT_ASSERT_NE(test, VCAP_KFS_NO_VALUE, keyset);
 286
 287	/* Allocate the rule */
 288	rule = vcap_alloc_rule(&test_vctrl, &test_netdev, cid, user, priority,
 289			       id);
 290	KUNIT_EXPECT_PTR_NE(test, NULL, rule);
 291
 292	ri = (struct vcap_rule_internal *)rule;
 293
 294	/* Override rule keyset */
 295	ret = vcap_set_rule_set_keyset(rule, keyset);
 296
 297	/* Add rule actions : there must be at least one action */
 298	ret = vcap_rule_add_action_u32(rule, VCAP_AF_ISDX_VAL, 0);
 299
 300	/* Override rule actionset */
 301	ret = vcap_set_rule_set_actionset(rule, actionset);
 302
 303	ret = vcap_val_rule(rule, ETH_P_ALL);
 304	KUNIT_EXPECT_EQ(test, 0, ret);
 305	KUNIT_EXPECT_EQ(test, keyset, rule->keyset);
 306	KUNIT_EXPECT_EQ(test, actionset, rule->actionset);
 307	KUNIT_EXPECT_EQ(test, size, ri->size);
 308
 309	/* Add rule with write callback */
 310	ret = vcap_add_rule(rule);
 311	KUNIT_EXPECT_EQ(test, 0, ret);
 312	KUNIT_EXPECT_EQ(test, expected_addr, ri->addr);
 313	vcap_free_rule(rule);
 314}
 315
 316/* Prepare testing rule deletion */
 317static void test_init_rule_deletion(void)
 318{
 319	test_move_addr = 0;
 320	test_move_offset = 0;
 321	test_move_count = 0;
 322	test_init_start = 0;
 323	test_init_count = 0;
 324}
 325
 326/* Define the test cases. */
 327
 328static void vcap_api_set_bit_1_test(struct kunit *test)
 329{
 330	struct vcap_stream_iter iter = {
 331		.offset = 35,
 332		.sw_width = 52,
 333		.reg_idx = 1,
 334		.reg_bitpos = 20,
 335		.tg = NULL,
 336	};
 337	u32 stream[2] = {0};
 338
 339	vcap_set_bit(stream, &iter, 1);
 340
 341	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[0]);
 342	KUNIT_EXPECT_EQ(test, (u32)BIT(20), stream[1]);
 343}
 344
 345static void vcap_api_set_bit_0_test(struct kunit *test)
 346{
 347	struct vcap_stream_iter iter = {
 348		.offset = 35,
 349		.sw_width = 52,
 350		.reg_idx = 2,
 351		.reg_bitpos = 11,
 352		.tg = NULL,
 353	};
 354	u32 stream[3] = {~0, ~0, ~0};
 355
 356	vcap_set_bit(stream, &iter, 0);
 357
 358	KUNIT_EXPECT_EQ(test, (u32)~0, stream[0]);
 359	KUNIT_EXPECT_EQ(test, (u32)~0, stream[1]);
 360	KUNIT_EXPECT_EQ(test, (u32)~BIT(11), stream[2]);
 361}
 362
 363static void vcap_api_iterator_init_test(struct kunit *test)
 364{
 365	struct vcap_stream_iter iter;
 366	struct vcap_typegroup typegroups[] = {
 367		{ .offset = 0, .width = 2, .value = 2, },
 368		{ .offset = 156, .width = 1, .value = 0, },
 369		{ }
 370	};
 371	struct vcap_typegroup typegroups2[] = {
 372		{ .offset = 0, .width = 3, .value = 4, },
 373		{ .offset = 49, .width = 2, .value = 0, },
 374		{ .offset = 98, .width = 2, .value = 0, },
 375		{ }
 376	};
 377
 378	vcap_iter_init(&iter, 52, typegroups, 86);
 379
 380	KUNIT_EXPECT_EQ(test, 52, iter.sw_width);
 381	KUNIT_EXPECT_EQ(test, 86 + 2, iter.offset);
 382	KUNIT_EXPECT_EQ(test, 3, iter.reg_idx);
 383	KUNIT_EXPECT_EQ(test, 4, iter.reg_bitpos);
 384
 385	vcap_iter_init(&iter, 49, typegroups2, 134);
 386
 387	KUNIT_EXPECT_EQ(test, 49, iter.sw_width);
 388	KUNIT_EXPECT_EQ(test, 134 + 7, iter.offset);
 389	KUNIT_EXPECT_EQ(test, 5, iter.reg_idx);
 390	KUNIT_EXPECT_EQ(test, 11, iter.reg_bitpos);
 391}
 392
 393static void vcap_api_iterator_next_test(struct kunit *test)
 394{
 395	struct vcap_stream_iter iter;
 396	struct vcap_typegroup typegroups[] = {
 397		{ .offset = 0, .width = 4, .value = 8, },
 398		{ .offset = 49, .width = 1, .value = 0, },
 399		{ .offset = 98, .width = 2, .value = 0, },
 400		{ .offset = 147, .width = 3, .value = 0, },
 401		{ .offset = 196, .width = 2, .value = 0, },
 402		{ .offset = 245, .width = 1, .value = 0, },
 403		{ }
 404	};
 405	int idx;
 406
 407	vcap_iter_init(&iter, 49, typegroups, 86);
 408
 409	KUNIT_EXPECT_EQ(test, 49, iter.sw_width);
 410	KUNIT_EXPECT_EQ(test, 86 + 5, iter.offset);
 411	KUNIT_EXPECT_EQ(test, 3, iter.reg_idx);
 412	KUNIT_EXPECT_EQ(test, 10, iter.reg_bitpos);
 413
 414	vcap_iter_next(&iter);
 415
 416	KUNIT_EXPECT_EQ(test, 91 + 1, iter.offset);
 417	KUNIT_EXPECT_EQ(test, 3, iter.reg_idx);
 418	KUNIT_EXPECT_EQ(test, 11, iter.reg_bitpos);
 419
 420	for (idx = 0; idx < 6; idx++)
 421		vcap_iter_next(&iter);
 422
 423	KUNIT_EXPECT_EQ(test, 92 + 6 + 2, iter.offset);
 424	KUNIT_EXPECT_EQ(test, 4, iter.reg_idx);
 425	KUNIT_EXPECT_EQ(test, 2, iter.reg_bitpos);
 426}
 427
 428static void vcap_api_encode_typegroups_test(struct kunit *test)
 429{
 430	u32 stream[12] = {0};
 431	struct vcap_typegroup typegroups[] = {
 432		{ .offset = 0, .width = 4, .value = 8, },
 433		{ .offset = 49, .width = 1, .value = 1, },
 434		{ .offset = 98, .width = 2, .value = 3, },
 435		{ .offset = 147, .width = 3, .value = 5, },
 436		{ .offset = 196, .width = 2, .value = 2, },
 437		{ .offset = 245, .width = 5, .value = 27, },
 438		{ }
 439	};
 440
 441	vcap_encode_typegroups(stream, 49, typegroups, false);
 442
 443	KUNIT_EXPECT_EQ(test, (u32)0x8, stream[0]);
 444	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[1]);
 445	KUNIT_EXPECT_EQ(test, (u32)0x1, stream[2]);
 446	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[3]);
 447	KUNIT_EXPECT_EQ(test, (u32)0x3, stream[4]);
 448	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[5]);
 449	KUNIT_EXPECT_EQ(test, (u32)0x5, stream[6]);
 450	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[7]);
 451	KUNIT_EXPECT_EQ(test, (u32)0x2, stream[8]);
 452	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[9]);
 453	KUNIT_EXPECT_EQ(test, (u32)27, stream[10]);
 454	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[11]);
 455}
 456
 457static void vcap_api_encode_bit_test(struct kunit *test)
 458{
 459	struct vcap_stream_iter iter;
 460	u32 stream[4] = {0};
 461	struct vcap_typegroup typegroups[] = {
 462		{ .offset = 0, .width = 4, .value = 8, },
 463		{ .offset = 49, .width = 1, .value = 1, },
 464		{ .offset = 98, .width = 2, .value = 3, },
 465		{ .offset = 147, .width = 3, .value = 5, },
 466		{ .offset = 196, .width = 2, .value = 2, },
 467		{ .offset = 245, .width = 1, .value = 0, },
 468		{ }
 469	};
 470
 471	vcap_iter_init(&iter, 49, typegroups, 44);
 472
 473	KUNIT_EXPECT_EQ(test, 48, iter.offset);
 474	KUNIT_EXPECT_EQ(test, 1, iter.reg_idx);
 475	KUNIT_EXPECT_EQ(test, 16, iter.reg_bitpos);
 476
 477	vcap_encode_bit(stream, &iter, 1);
 478
 479	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[0]);
 480	KUNIT_EXPECT_EQ(test, (u32)BIT(16), stream[1]);
 481	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[2]);
 482}
 483
 484static void vcap_api_encode_field_test(struct kunit *test)
 485{
 486	struct vcap_stream_iter iter;
 487	u32 stream[16] = {0};
 488	struct vcap_typegroup typegroups[] = {
 489		{ .offset = 0, .width = 4, .value = 8, },
 490		{ .offset = 49, .width = 1, .value = 1, },
 491		{ .offset = 98, .width = 2, .value = 3, },
 492		{ .offset = 147, .width = 3, .value = 5, },
 493		{ .offset = 196, .width = 2, .value = 2, },
 494		{ .offset = 245, .width = 5, .value = 27, },
 495		{ }
 496	};
 497	struct vcap_field rf = {
 498		.type = VCAP_FIELD_U32,
 499		.offset = 86,
 500		.width = 4,
 501	};
 502	u8 value[] = {0x5};
 503
 504	vcap_iter_init(&iter, 49, typegroups, rf.offset);
 505
 506	KUNIT_EXPECT_EQ(test, 91, iter.offset);
 507	KUNIT_EXPECT_EQ(test, 3, iter.reg_idx);
 508	KUNIT_EXPECT_EQ(test, 10, iter.reg_bitpos);
 509
 510	vcap_encode_field(stream, &iter, rf.width, value);
 511
 512	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[0]);
 513	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[1]);
 514	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[2]);
 515	KUNIT_EXPECT_EQ(test, (u32)(0x5 << 10), stream[3]);
 516	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[4]);
 517
 518	vcap_encode_typegroups(stream, 49, typegroups, false);
 519
 520	KUNIT_EXPECT_EQ(test, (u32)0x8, stream[0]);
 521	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[1]);
 522	KUNIT_EXPECT_EQ(test, (u32)0x1, stream[2]);
 523	KUNIT_EXPECT_EQ(test, (u32)(0x5 << 10), stream[3]);
 524	KUNIT_EXPECT_EQ(test, (u32)0x3, stream[4]);
 525	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[5]);
 526	KUNIT_EXPECT_EQ(test, (u32)0x5, stream[6]);
 527	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[7]);
 528	KUNIT_EXPECT_EQ(test, (u32)0x2, stream[8]);
 529	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[9]);
 530	KUNIT_EXPECT_EQ(test, (u32)27, stream[10]);
 531	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[11]);
 532}
 533
 534/* In this testcase the subword is smaller than a register */
 535static void vcap_api_encode_short_field_test(struct kunit *test)
 536{
 537	struct vcap_stream_iter iter;
 538	int sw_width = 21;
 539	u32 stream[6] = {0};
 540	struct vcap_typegroup tgt[] = {
 541		{ .offset = 0, .width = 3, .value = 7, },
 542		{ .offset = 21, .width = 2, .value = 3, },
 543		{ .offset = 42, .width = 1, .value = 1, },
 544		{ }
 545	};
 546	struct vcap_field rf = {
 547		.type = VCAP_FIELD_U32,
 548		.offset = 25,
 549		.width = 4,
 550	};
 551	u8 value[] = {0x5};
 552
 553	vcap_iter_init(&iter, sw_width, tgt, rf.offset);
 554
 555	KUNIT_EXPECT_EQ(test, 1, iter.regs_per_sw);
 556	KUNIT_EXPECT_EQ(test, 21, iter.sw_width);
 557	KUNIT_EXPECT_EQ(test, 25 + 3 + 2, iter.offset);
 558	KUNIT_EXPECT_EQ(test, 1, iter.reg_idx);
 559	KUNIT_EXPECT_EQ(test, 25 + 3 + 2 - sw_width, iter.reg_bitpos);
 560
 561	vcap_encode_field(stream, &iter, rf.width, value);
 562
 563	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[0]);
 564	KUNIT_EXPECT_EQ(test, (u32)(0x5 << (25 + 3 + 2 - sw_width)), stream[1]);
 565	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[2]);
 566	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[3]);
 567	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[4]);
 568	KUNIT_EXPECT_EQ(test, (u32)0x0, stream[5]);
 569
 570	vcap_encode_typegroups(stream, sw_width, tgt, false);
 571
 572	KUNIT_EXPECT_EQ(test, (u32)7, stream[0]);
 573	KUNIT_EXPECT_EQ(test, (u32)((0x5 << (25 + 3 + 2 - sw_width)) + 3), stream[1]);
 574	KUNIT_EXPECT_EQ(test, (u32)1, stream[2]);
 575	KUNIT_EXPECT_EQ(test, (u32)0, stream[3]);
 576	KUNIT_EXPECT_EQ(test, (u32)0, stream[4]);
 577	KUNIT_EXPECT_EQ(test, (u32)0, stream[5]);
 578}
 579
 580static void vcap_api_encode_keyfield_test(struct kunit *test)
 581{
 582	u32 keywords[16] = {0};
 583	u32 maskwords[16] = {0};
 584	struct vcap_admin admin = {
 585		.vtype = VCAP_TYPE_IS2,
 586		.cache = {
 587			.keystream = keywords,
 588			.maskstream = maskwords,
 589			.actionstream = keywords,
 590		},
 591	};
 592	struct vcap_rule_internal rule = {
 593		.admin = &admin,
 594		.data = {
 595			.keyset = VCAP_KFS_MAC_ETYPE,
 596		},
 597		.vctrl = &test_vctrl,
 598	};
 599	struct vcap_client_keyfield ckf = {
 600		.ctrl.list = {},
 601		.ctrl.key = VCAP_KF_ISDX_CLS,
 602		.ctrl.type = VCAP_FIELD_U32,
 603		.data.u32.value = 0xeef014a1,
 604		.data.u32.mask = 0xfff,
 605	};
 606	struct vcap_field rf = {
 607		.type = VCAP_FIELD_U32,
 608		.offset = 56,
 609		.width = 12,
 610	};
 611	struct vcap_typegroup tgt[] = {
 612		{ .offset = 0, .width = 2, .value = 2, },
 613		{ .offset = 156, .width = 1, .value = 1, },
 614		{ }
 615	};
 616
 617	vcap_test_api_init(&admin);
 618	vcap_encode_keyfield(&rule, &ckf, &rf, tgt);
 619
 620	/* Key */
 621	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[0]);
 622	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[1]);
 623	KUNIT_EXPECT_EQ(test, (u32)(0x04a1 << 6), keywords[2]);
 624	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[3]);
 625	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[4]);
 626	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[5]);
 627	KUNIT_EXPECT_EQ(test, (u32)0x0, keywords[6]);
 628
 629	/* Mask */
 630	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[0]);
 631	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[1]);
 632	KUNIT_EXPECT_EQ(test, (u32)(0x0fff << 6), maskwords[2]);
 633	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[3]);
 634	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[4]);
 635	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[5]);
 636	KUNIT_EXPECT_EQ(test, (u32)0x0, maskwords[6]);
 637}
 638
 639static void vcap_api_encode_max_keyfield_test(struct kunit *test)
 640{
 641	int idx;
 642	u32 keywords[6] = {0};
 643	u32 maskwords[6] = {0};
 644	struct vcap_admin admin = {
 645		.vtype = VCAP_TYPE_IS2,
 646		/* IS2 sw_width = 52 bit */
 647		.cache = {
 648			.keystream = keywords,
 649			.maskstream = maskwords,
 650			.actionstream = keywords,
 651		},
 652	};
 653	struct vcap_rule_internal rule = {
 654		.admin = &admin,
 655		.data = {
 656			.keyset = VCAP_KFS_IP_7TUPLE,
 657		},
 658		.vctrl = &test_vctrl,
 659	};
 660	struct vcap_client_keyfield ckf = {
 661		.ctrl.list = {},
 662		.ctrl.key = VCAP_KF_L3_IP6_DIP,
 663		.ctrl.type = VCAP_FIELD_U128,
 664		.data.u128.value = { 0xa1, 0xa2, 0xa3, 0xa4, 0, 0, 0x43, 0,
 665			0, 0, 0, 0, 0, 0, 0x78, 0x8e, },
 666		.data.u128.mask =  { 0xff, 0xff, 0xff, 0xff, 0, 0, 0xff, 0,
 667			0, 0, 0, 0, 0, 0, 0xff, 0xff },
 668	};
 669	struct vcap_field rf = {
 670		.type = VCAP_FIELD_U128,
 671		.offset = 0,
 672		.width = 128,
 673	};
 674	struct vcap_typegroup tgt[] = {
 675		{ .offset = 0, .width = 2, .value = 2, },
 676		{ .offset = 156, .width = 1, .value = 1, },
 677		{ }
 678	};
 679	u32 keyres[] = {
 680		0x928e8a84,
 681		0x000c0002,
 682		0x00000010,
 683		0x00000000,
 684		0x0239e000,
 685		0x00000000,
 686	};
 687	u32 mskres[] = {
 688		0xfffffffc,
 689		0x000c0003,
 690		0x0000003f,
 691		0x00000000,
 692		0x03fffc00,
 693		0x00000000,
 694	};
 695
 696	vcap_encode_keyfield(&rule, &ckf, &rf, tgt);
 697
 698	/* Key */
 699	for (idx = 0; idx < ARRAY_SIZE(keyres); ++idx)
 700		KUNIT_EXPECT_EQ(test, keyres[idx], keywords[idx]);
 701	/* Mask */
 702	for (idx = 0; idx < ARRAY_SIZE(mskres); ++idx)
 703		KUNIT_EXPECT_EQ(test, mskres[idx], maskwords[idx]);
 704}
 705
 706static void vcap_api_encode_actionfield_test(struct kunit *test)
 707{
 708	u32 actwords[16] = {0};
 709	int sw_width = 21;
 710	struct vcap_admin admin = {
 711		.vtype = VCAP_TYPE_ES2, /* act_width = 21 */
 712		.cache = {
 713			.actionstream = actwords,
 714		},
 715	};
 716	struct vcap_rule_internal rule = {
 717		.admin = &admin,
 718		.data = {
 719			.actionset = VCAP_AFS_BASE_TYPE,
 720		},
 721		.vctrl = &test_vctrl,
 722	};
 723	struct vcap_client_actionfield caf = {
 724		.ctrl.list = {},
 725		.ctrl.action = VCAP_AF_POLICE_IDX,
 726		.ctrl.type = VCAP_FIELD_U32,
 727		.data.u32.value = 0x67908032,
 728	};
 729	struct vcap_field rf = {
 730		.type = VCAP_FIELD_U32,
 731		.offset = 35,
 732		.width = 6,
 733	};
 734	struct vcap_typegroup tgt[] = {
 735		{ .offset = 0, .width = 2, .value = 2, },
 736		{ .offset = 21, .width = 1, .value = 1, },
 737		{ .offset = 42, .width = 1, .value = 0, },
 738		{ }
 739	};
 740
 741	vcap_encode_actionfield(&rule, &caf, &rf, tgt);
 742
 743	/* Action */
 744	KUNIT_EXPECT_EQ(test, (u32)0x0, actwords[0]);
 745	KUNIT_EXPECT_EQ(test, (u32)((0x32 << (35 + 2 + 1 - sw_width)) & 0x1fffff), actwords[1]);
 746	KUNIT_EXPECT_EQ(test, (u32)((0x32 >> ((2 * sw_width) - 38 - 1))), actwords[2]);
 747	KUNIT_EXPECT_EQ(test, (u32)0x0, actwords[3]);
 748	KUNIT_EXPECT_EQ(test, (u32)0x0, actwords[4]);
 749	KUNIT_EXPECT_EQ(test, (u32)0x0, actwords[5]);
 750	KUNIT_EXPECT_EQ(test, (u32)0x0, actwords[6]);
 751}
 752
 753static void vcap_api_keyfield_typegroup_test(struct kunit *test)
 754{
 755	const struct vcap_typegroup *tg;
 756
 757	tg = vcap_keyfield_typegroup(&test_vctrl, VCAP_TYPE_IS2, VCAP_KFS_MAC_ETYPE);
 758	KUNIT_EXPECT_PTR_NE(test, NULL, tg);
 759	KUNIT_EXPECT_EQ(test, 0, tg[0].offset);
 760	KUNIT_EXPECT_EQ(test, 2, tg[0].width);
 761	KUNIT_EXPECT_EQ(test, 2, tg[0].value);
 762	KUNIT_EXPECT_EQ(test, 156, tg[1].offset);
 763	KUNIT_EXPECT_EQ(test, 1, tg[1].width);
 764	KUNIT_EXPECT_EQ(test, 0, tg[1].value);
 765	KUNIT_EXPECT_EQ(test, 0, tg[2].offset);
 766	KUNIT_EXPECT_EQ(test, 0, tg[2].width);
 767	KUNIT_EXPECT_EQ(test, 0, tg[2].value);
 768
 769	tg = vcap_keyfield_typegroup(&test_vctrl, VCAP_TYPE_ES2, VCAP_KFS_LL_FULL);
 770	KUNIT_EXPECT_PTR_EQ(test, NULL, tg);
 771}
 772
 773static void vcap_api_actionfield_typegroup_test(struct kunit *test)
 774{
 775	const struct vcap_typegroup *tg;
 776
 777	tg = vcap_actionfield_typegroup(&test_vctrl, VCAP_TYPE_IS0, VCAP_AFS_FULL);
 778	KUNIT_EXPECT_PTR_NE(test, NULL, tg);
 779	KUNIT_EXPECT_EQ(test, 0, tg[0].offset);
 780	KUNIT_EXPECT_EQ(test, 3, tg[0].width);
 781	KUNIT_EXPECT_EQ(test, 4, tg[0].value);
 782	KUNIT_EXPECT_EQ(test, 110, tg[1].offset);
 783	KUNIT_EXPECT_EQ(test, 2, tg[1].width);
 784	KUNIT_EXPECT_EQ(test, 0, tg[1].value);
 785	KUNIT_EXPECT_EQ(test, 220, tg[2].offset);
 786	KUNIT_EXPECT_EQ(test, 2, tg[2].width);
 787	KUNIT_EXPECT_EQ(test, 0, tg[2].value);
 788	KUNIT_EXPECT_EQ(test, 0, tg[3].offset);
 789	KUNIT_EXPECT_EQ(test, 0, tg[3].width);
 790	KUNIT_EXPECT_EQ(test, 0, tg[3].value);
 791
 792	tg = vcap_actionfield_typegroup(&test_vctrl, VCAP_TYPE_IS2, VCAP_AFS_CLASSIFICATION);
 793	KUNIT_EXPECT_PTR_EQ(test, NULL, tg);
 794}
 795
 796static void vcap_api_vcap_keyfields_test(struct kunit *test)
 797{
 798	const struct vcap_field *ft;
 799
 800	ft = vcap_keyfields(&test_vctrl, VCAP_TYPE_IS2, VCAP_KFS_MAC_ETYPE);
 801	KUNIT_EXPECT_PTR_NE(test, NULL, ft);
 802
 803	/* Keyset that is not available and within the maximum keyset enum value */
 804	ft = vcap_keyfields(&test_vctrl, VCAP_TYPE_ES2, VCAP_KFS_PURE_5TUPLE_IP4);
 805	KUNIT_EXPECT_PTR_EQ(test, NULL, ft);
 806
 807	/* Keyset that is not available and beyond the maximum keyset enum value */
 808	ft = vcap_keyfields(&test_vctrl, VCAP_TYPE_ES2, VCAP_KFS_LL_FULL);
 809	KUNIT_EXPECT_PTR_EQ(test, NULL, ft);
 810}
 811
 812static void vcap_api_vcap_actionfields_test(struct kunit *test)
 813{
 814	const struct vcap_field *ft;
 815
 816	ft = vcap_actionfields(&test_vctrl, VCAP_TYPE_IS0, VCAP_AFS_FULL);
 817	KUNIT_EXPECT_PTR_NE(test, NULL, ft);
 818
 819	ft = vcap_actionfields(&test_vctrl, VCAP_TYPE_IS2, VCAP_AFS_FULL);
 820	KUNIT_EXPECT_PTR_EQ(test, NULL, ft);
 821
 822	ft = vcap_actionfields(&test_vctrl, VCAP_TYPE_IS2, VCAP_AFS_CLASSIFICATION);
 823	KUNIT_EXPECT_PTR_EQ(test, NULL, ft);
 824}
 825
 826static void vcap_api_encode_rule_keyset_test(struct kunit *test)
 827{
 828	u32 keywords[16] = {0};
 829	u32 maskwords[16] = {0};
 830	struct vcap_admin admin = {
 831		.vtype = VCAP_TYPE_IS2,
 832		.cache = {
 833			.keystream = keywords,
 834			.maskstream = maskwords,
 835		},
 836	};
 837	struct vcap_rule_internal rule = {
 838		.admin = &admin,
 839		.data = {
 840			.keyset = VCAP_KFS_MAC_ETYPE,
 841		},
 842		.vctrl = &test_vctrl,
 843	};
 844	struct vcap_client_keyfield ckf[] = {
 845		{
 846			.ctrl.key = VCAP_KF_TYPE,
 847			.ctrl.type = VCAP_FIELD_U32,
 848			.data.u32.value = 0x00,
 849			.data.u32.mask = 0x0f,
 850		},
 851		{
 852			.ctrl.key = VCAP_KF_LOOKUP_FIRST_IS,
 853			.ctrl.type = VCAP_FIELD_BIT,
 854			.data.u1.value = 0x01,
 855			.data.u1.mask = 0x01,
 856		},
 857		{
 858			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK_L3,
 859			.ctrl.type = VCAP_FIELD_BIT,
 860			.data.u1.value = 0x00,
 861			.data.u1.mask = 0x01,
 862		},
 863		{
 864			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK_RNG,
 865			.ctrl.type = VCAP_FIELD_U32,
 866			.data.u32.value = 0x00,
 867			.data.u32.mask = 0x0f,
 868		},
 869		{
 870			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK,
 871			.ctrl.type = VCAP_FIELD_U72,
 872			.data.u72.value = {0x0, 0x00, 0x00, 0x00},
 873			.data.u72.mask = {0xfd, 0xff, 0xff, 0xff},
 874		},
 875		{
 876			.ctrl.key = VCAP_KF_L2_DMAC,
 877			.ctrl.type = VCAP_FIELD_U48,
 878			/* Opposite endianness */
 879			.data.u48.value = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
 880			.data.u48.mask = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
 881		},
 882		{
 883			.ctrl.key = VCAP_KF_ETYPE_LEN_IS,
 884			.ctrl.type = VCAP_FIELD_BIT,
 885			.data.u1.value = 0x01,
 886			.data.u1.mask = 0x01,
 887		},
 888		{
 889			.ctrl.key = VCAP_KF_ETYPE,
 890			.ctrl.type = VCAP_FIELD_U32,
 891			.data.u32.value = 0xaabb,
 892			.data.u32.mask = 0xffff,
 893		},
 894	};
 895	int idx;
 896	int ret;
 897
 898	/* Empty entry list */
 899	INIT_LIST_HEAD(&rule.data.keyfields);
 900	ret = vcap_encode_rule_keyset(&rule);
 901	KUNIT_EXPECT_EQ(test, -EINVAL, ret);
 902
 903	for (idx = 0; idx < ARRAY_SIZE(ckf); idx++)
 904		list_add_tail(&ckf[idx].ctrl.list, &rule.data.keyfields);
 905	ret = vcap_encode_rule_keyset(&rule);
 906	KUNIT_EXPECT_EQ(test, 0, ret);
 907
 908	/* The key and mask values below are from an actual Sparx5 rule config */
 909	/* Key */
 910	KUNIT_EXPECT_EQ(test, (u32)0x00000042, keywords[0]);
 911	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[1]);
 912	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[2]);
 913	KUNIT_EXPECT_EQ(test, (u32)0x00020100, keywords[3]);
 914	KUNIT_EXPECT_EQ(test, (u32)0x60504030, keywords[4]);
 915	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[5]);
 916	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[6]);
 917	KUNIT_EXPECT_EQ(test, (u32)0x0002aaee, keywords[7]);
 918	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[8]);
 919	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[9]);
 920	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[10]);
 921	KUNIT_EXPECT_EQ(test, (u32)0x00000000, keywords[11]);
 922
 923	/* Mask: they will be inverted when applied to the register */
 924	KUNIT_EXPECT_EQ(test, (u32)~0x00b07f80, maskwords[0]);
 925	KUNIT_EXPECT_EQ(test, (u32)~0xfff00000, maskwords[1]);
 926	KUNIT_EXPECT_EQ(test, (u32)~0xfffffffc, maskwords[2]);
 927	KUNIT_EXPECT_EQ(test, (u32)~0xfff000ff, maskwords[3]);
 928	KUNIT_EXPECT_EQ(test, (u32)~0x00000000, maskwords[4]);
 929	KUNIT_EXPECT_EQ(test, (u32)~0xfffffff0, maskwords[5]);
 930	KUNIT_EXPECT_EQ(test, (u32)~0xfffffffe, maskwords[6]);
 931	KUNIT_EXPECT_EQ(test, (u32)~0xfffc0001, maskwords[7]);
 932	KUNIT_EXPECT_EQ(test, (u32)~0xffffffff, maskwords[8]);
 933	KUNIT_EXPECT_EQ(test, (u32)~0xffffffff, maskwords[9]);
 934	KUNIT_EXPECT_EQ(test, (u32)~0xffffffff, maskwords[10]);
 935	KUNIT_EXPECT_EQ(test, (u32)~0xffffffff, maskwords[11]);
 936}
 937
 938static void vcap_api_encode_rule_actionset_test(struct kunit *test)
 939{
 940	u32 actwords[16] = {0};
 941	struct vcap_admin admin = {
 942		.vtype = VCAP_TYPE_IS2,
 943		.cache = {
 944			.actionstream = actwords,
 945		},
 946	};
 947	struct vcap_rule_internal rule = {
 948		.admin = &admin,
 949		.data = {
 950			.actionset = VCAP_AFS_BASE_TYPE,
 951		},
 952		.vctrl = &test_vctrl,
 953	};
 954	struct vcap_client_actionfield caf[] = {
 955		{
 956			.ctrl.action = VCAP_AF_MATCH_ID,
 957			.ctrl.type = VCAP_FIELD_U32,
 958			.data.u32.value = 0x01,
 959		},
 960		{
 961			.ctrl.action = VCAP_AF_MATCH_ID_MASK,
 962			.ctrl.type = VCAP_FIELD_U32,
 963			.data.u32.value = 0x01,
 964		},
 965		{
 966			.ctrl.action = VCAP_AF_CNT_ID,
 967			.ctrl.type = VCAP_FIELD_U32,
 968			.data.u32.value = 0x64,
 969		},
 970	};
 971	int idx;
 972	int ret;
 973
 974	/* Empty entry list */
 975	INIT_LIST_HEAD(&rule.data.actionfields);
 976	ret = vcap_encode_rule_actionset(&rule);
 977	/* We allow rules with no actions */
 978	KUNIT_EXPECT_EQ(test, 0, ret);
 979
 980	for (idx = 0; idx < ARRAY_SIZE(caf); idx++)
 981		list_add_tail(&caf[idx].ctrl.list, &rule.data.actionfields);
 982	ret = vcap_encode_rule_actionset(&rule);
 983	KUNIT_EXPECT_EQ(test, 0, ret);
 984
 985	/* The action values below are from an actual Sparx5 rule config */
 986	KUNIT_EXPECT_EQ(test, (u32)0x00000002, actwords[0]);
 987	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[1]);
 988	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[2]);
 989	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[3]);
 990	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[4]);
 991	KUNIT_EXPECT_EQ(test, (u32)0x00100000, actwords[5]);
 992	KUNIT_EXPECT_EQ(test, (u32)0x06400010, actwords[6]);
 993	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[7]);
 994	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[8]);
 995	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[9]);
 996	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[10]);
 997	KUNIT_EXPECT_EQ(test, (u32)0x00000000, actwords[11]);
 998}
 999
1000static void vcap_free_ckf(struct vcap_rule *rule)
1001{
1002	struct vcap_client_keyfield *ckf, *next_ckf;
1003
1004	list_for_each_entry_safe(ckf, next_ckf, &rule->keyfields, ctrl.list) {
1005		list_del(&ckf->ctrl.list);
1006		kfree(ckf);
1007	}
1008}
1009
1010static void vcap_api_rule_add_keyvalue_test(struct kunit *test)
1011{
1012	struct vcap_admin admin = {
1013		.vtype = VCAP_TYPE_IS2,
1014	};
1015	struct vcap_rule_internal ri = {
1016		.admin = &admin,
1017		.data = {
1018			.keyset = VCAP_KFS_NO_VALUE,
1019		},
1020		.vctrl = &test_vctrl,
1021	};
1022	struct vcap_rule *rule = (struct vcap_rule *)&ri;
1023	struct vcap_client_keyfield *kf;
1024	int ret;
1025	struct vcap_u128_key dip = {
1026		.value = {0x17, 0x26, 0x35, 0x44, 0x63, 0x62, 0x71},
1027		.mask = {0xf1, 0xf2, 0xf3, 0xf4, 0x4f, 0x3f, 0x2f, 0x1f},
1028	};
1029	int idx;
1030
1031	INIT_LIST_HEAD(&rule->keyfields);
1032	ret = vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS, VCAP_BIT_0);
1033	KUNIT_EXPECT_EQ(test, 0, ret);
1034	ret = list_empty(&rule->keyfields);
1035	KUNIT_EXPECT_EQ(test, false, ret);
1036	kf = list_first_entry(&rule->keyfields, struct vcap_client_keyfield,
1037			      ctrl.list);
1038	KUNIT_EXPECT_EQ(test, VCAP_KF_LOOKUP_FIRST_IS, kf->ctrl.key);
1039	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, kf->ctrl.type);
1040	KUNIT_EXPECT_EQ(test, 0x0, kf->data.u1.value);
1041	KUNIT_EXPECT_EQ(test, 0x1, kf->data.u1.mask);
1042	vcap_free_ckf(rule);
1043
1044	INIT_LIST_HEAD(&rule->keyfields);
1045	ret = vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS, VCAP_BIT_1);
1046	KUNIT_EXPECT_EQ(test, 0, ret);
1047	ret = list_empty(&rule->keyfields);
1048	KUNIT_EXPECT_EQ(test, false, ret);
1049	kf = list_first_entry(&rule->keyfields, struct vcap_client_keyfield,
1050			      ctrl.list);
1051	KUNIT_EXPECT_EQ(test, VCAP_KF_LOOKUP_FIRST_IS, kf->ctrl.key);
1052	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, kf->ctrl.type);
1053	KUNIT_EXPECT_EQ(test, 0x1, kf->data.u1.value);
1054	KUNIT_EXPECT_EQ(test, 0x1, kf->data.u1.mask);
1055	vcap_free_ckf(rule);
1056
1057	INIT_LIST_HEAD(&rule->keyfields);
1058	ret = vcap_rule_add_key_bit(rule, VCAP_KF_LOOKUP_FIRST_IS,
1059				    VCAP_BIT_ANY);
1060	KUNIT_EXPECT_EQ(test, 0, ret);
1061	ret = list_empty(&rule->keyfields);
1062	KUNIT_EXPECT_EQ(test, false, ret);
1063	kf = list_first_entry(&rule->keyfields, struct vcap_client_keyfield,
1064			      ctrl.list);
1065	KUNIT_EXPECT_EQ(test, VCAP_KF_LOOKUP_FIRST_IS, kf->ctrl.key);
1066	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, kf->ctrl.type);
1067	KUNIT_EXPECT_EQ(test, 0x0, kf->data.u1.value);
1068	KUNIT_EXPECT_EQ(test, 0x0, kf->data.u1.mask);
1069	vcap_free_ckf(rule);
1070
1071	INIT_LIST_HEAD(&rule->keyfields);
1072	ret = vcap_rule_add_key_u32(rule, VCAP_KF_TYPE, 0x98765432, 0xff00ffab);
1073	KUNIT_EXPECT_EQ(test, 0, ret);
1074	ret = list_empty(&rule->keyfields);
1075	KUNIT_EXPECT_EQ(test, false, ret);
1076	kf = list_first_entry(&rule->keyfields, struct vcap_client_keyfield,
1077			      ctrl.list);
1078	KUNIT_EXPECT_EQ(test, VCAP_KF_TYPE, kf->ctrl.key);
1079	KUNIT_EXPECT_EQ(test, VCAP_FIELD_U32, kf->ctrl.type);
1080	KUNIT_EXPECT_EQ(test, 0x98765432, kf->data.u32.value);
1081	KUNIT_EXPECT_EQ(test, 0xff00ffab, kf->data.u32.mask);
1082	vcap_free_ckf(rule);
1083
1084	INIT_LIST_HEAD(&rule->keyfields);
1085	ret = vcap_rule_add_key_u128(rule, VCAP_KF_L3_IP6_SIP, &dip);
1086	KUNIT_EXPECT_EQ(test, 0, ret);
1087	ret = list_empty(&rule->keyfields);
1088	KUNIT_EXPECT_EQ(test, false, ret);
1089	kf = list_first_entry(&rule->keyfields, struct vcap_client_keyfield,
1090			      ctrl.list);
1091	KUNIT_EXPECT_EQ(test, VCAP_KF_L3_IP6_SIP, kf->ctrl.key);
1092	KUNIT_EXPECT_EQ(test, VCAP_FIELD_U128, kf->ctrl.type);
1093	for (idx = 0; idx < ARRAY_SIZE(dip.value); ++idx)
1094		KUNIT_EXPECT_EQ(test, dip.value[idx], kf->data.u128.value[idx]);
1095	for (idx = 0; idx < ARRAY_SIZE(dip.mask); ++idx)
1096		KUNIT_EXPECT_EQ(test, dip.mask[idx], kf->data.u128.mask[idx]);
1097	vcap_free_ckf(rule);
1098}
1099
1100static void vcap_free_caf(struct vcap_rule *rule)
1101{
1102	struct vcap_client_actionfield *caf, *next_caf;
1103
1104	list_for_each_entry_safe(caf, next_caf,
1105				 &rule->actionfields, ctrl.list) {
1106		list_del(&caf->ctrl.list);
1107		kfree(caf);
1108	}
1109}
1110
1111static void vcap_api_rule_add_actionvalue_test(struct kunit *test)
1112{
1113	struct vcap_admin admin = {
1114		.vtype = VCAP_TYPE_IS2,
1115	};
1116	struct vcap_rule_internal ri = {
1117		.admin = &admin,
1118		.data = {
1119			.actionset = VCAP_AFS_NO_VALUE,
1120		},
1121	};
1122	struct vcap_rule *rule = (struct vcap_rule *)&ri;
1123	struct vcap_client_actionfield *af;
1124	int ret;
1125
1126	INIT_LIST_HEAD(&rule->actionfields);
1127	ret = vcap_rule_add_action_bit(rule, VCAP_AF_POLICE_ENA, VCAP_BIT_0);
1128	KUNIT_EXPECT_EQ(test, 0, ret);
1129	ret = list_empty(&rule->actionfields);
1130	KUNIT_EXPECT_EQ(test, false, ret);
1131	af = list_first_entry(&rule->actionfields,
1132			      struct vcap_client_actionfield, ctrl.list);
1133	KUNIT_EXPECT_EQ(test, VCAP_AF_POLICE_ENA, af->ctrl.action);
1134	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, af->ctrl.type);
1135	KUNIT_EXPECT_EQ(test, 0x0, af->data.u1.value);
1136	vcap_free_caf(rule);
1137
1138	INIT_LIST_HEAD(&rule->actionfields);
1139	ret = vcap_rule_add_action_bit(rule, VCAP_AF_POLICE_ENA, VCAP_BIT_1);
1140	KUNIT_EXPECT_EQ(test, 0, ret);
1141	ret = list_empty(&rule->actionfields);
1142	KUNIT_EXPECT_EQ(test, false, ret);
1143	af = list_first_entry(&rule->actionfields,
1144			      struct vcap_client_actionfield, ctrl.list);
1145	KUNIT_EXPECT_EQ(test, VCAP_AF_POLICE_ENA, af->ctrl.action);
1146	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, af->ctrl.type);
1147	KUNIT_EXPECT_EQ(test, 0x1, af->data.u1.value);
1148	vcap_free_caf(rule);
1149
1150	INIT_LIST_HEAD(&rule->actionfields);
1151	ret = vcap_rule_add_action_bit(rule, VCAP_AF_POLICE_ENA, VCAP_BIT_ANY);
1152	KUNIT_EXPECT_EQ(test, 0, ret);
1153	ret = list_empty(&rule->actionfields);
1154	KUNIT_EXPECT_EQ(test, false, ret);
1155	af = list_first_entry(&rule->actionfields,
1156			      struct vcap_client_actionfield, ctrl.list);
1157	KUNIT_EXPECT_EQ(test, VCAP_AF_POLICE_ENA, af->ctrl.action);
1158	KUNIT_EXPECT_EQ(test, VCAP_FIELD_BIT, af->ctrl.type);
1159	KUNIT_EXPECT_EQ(test, 0x0, af->data.u1.value);
1160	vcap_free_caf(rule);
1161
1162	INIT_LIST_HEAD(&rule->actionfields);
1163	ret = vcap_rule_add_action_u32(rule, VCAP_AF_TYPE, 0x98765432);
1164	KUNIT_EXPECT_EQ(test, 0, ret);
1165	ret = list_empty(&rule->actionfields);
1166	KUNIT_EXPECT_EQ(test, false, ret);
1167	af = list_first_entry(&rule->actionfields,
1168			      struct vcap_client_actionfield, ctrl.list);
1169	KUNIT_EXPECT_EQ(test, VCAP_AF_TYPE, af->ctrl.action);
1170	KUNIT_EXPECT_EQ(test, VCAP_FIELD_U32, af->ctrl.type);
1171	KUNIT_EXPECT_EQ(test, 0x98765432, af->data.u32.value);
1172	vcap_free_caf(rule);
1173
1174	INIT_LIST_HEAD(&rule->actionfields);
1175	ret = vcap_rule_add_action_u32(rule, VCAP_AF_MASK_MODE, 0xaabbccdd);
1176	KUNIT_EXPECT_EQ(test, 0, ret);
1177	ret = list_empty(&rule->actionfields);
1178	KUNIT_EXPECT_EQ(test, false, ret);
1179	af = list_first_entry(&rule->actionfields,
1180			      struct vcap_client_actionfield, ctrl.list);
1181	KUNIT_EXPECT_EQ(test, VCAP_AF_MASK_MODE, af->ctrl.action);
1182	KUNIT_EXPECT_EQ(test, VCAP_FIELD_U32, af->ctrl.type);
1183	KUNIT_EXPECT_EQ(test, 0xaabbccdd, af->data.u32.value);
1184	vcap_free_caf(rule);
1185}
1186
1187static void vcap_api_rule_find_keyset_basic_test(struct kunit *test)
1188{
1189	struct vcap_keyset_list matches = {};
1190	struct vcap_admin admin = {
1191		.vtype = VCAP_TYPE_IS2,
1192	};
1193	struct vcap_rule_internal ri = {
1194		.admin = &admin,
1195		.vctrl = &test_vctrl,
1196	};
1197	struct vcap_client_keyfield ckf[] = {
1198		{
1199			.ctrl.key = VCAP_KF_TYPE,
1200		}, {
1201			.ctrl.key = VCAP_KF_LOOKUP_FIRST_IS,
1202		}, {
1203			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK_L3,
1204		}, {
1205			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK_RNG,
1206		}, {
1207			.ctrl.key = VCAP_KF_IF_IGR_PORT_MASK,
1208		}, {
1209			.ctrl.key = VCAP_KF_L2_DMAC,
1210		}, {
1211			.ctrl.key = VCAP_KF_ETYPE_LEN_IS,
1212		}, {
1213			.ctrl.key = VCAP_KF_ETYPE,
1214		},
1215	};
1216	int idx;
1217	bool ret;
1218	enum vcap_keyfield_set keysets[10] = {};
1219
1220	matches.keysets = keysets;
1221	matches.max = ARRAY_SIZE(keysets);
1222
1223	INIT_LIST_HEAD(&ri.data.keyfields);
1224	for (idx = 0; idx < ARRAY_SIZE(ckf); idx++)
1225		list_add_tail(&ckf[idx].ctrl.list, &ri.data.keyfields);
1226
1227	ret = vcap_rule_find_keysets(&ri.data, &matches);
1228
1229	KUNIT_EXPECT_EQ(test, true, ret);
1230	KUNIT_EXPECT_EQ(test, 1, matches.cnt);
1231	KUNIT_EXPECT_EQ(test, VCAP_KFS_MAC_ETYPE, matches.keysets[0]);
1232}
1233
1234static void vcap_api_rule_find_keyset_failed_test(struct kunit *test)
1235{
1236	struct vcap_keyset_list matches = {};
1237	struct vcap_admin admin = {
1238		.vtype = VCAP_TYPE_IS2,
1239	};
1240	struct vcap_rule_internal ri = {
1241		.admin = &admin,
1242		.vctrl = &test_vctrl,
1243	};
1244	struct vcap_client_keyfield ckf[] = {
1245		{
1246			.ctrl.key = VCAP_KF_TYPE,
1247		}, {
1248			.ctrl.key = VCAP_KF_LOOKUP_FIRST_IS,
1249		}, {
1250			.ctrl.key = VCAP_KF_ARP_OPCODE,
1251		}, {
1252			.ctrl.key = VCAP_KF_L3_IP4_SIP,
1253		}, {
1254			.ctrl.key = VCAP_KF_L3_IP4_DIP,
1255		}, {
1256			.ctrl.key = VCAP_KF_8021Q_PCP_CLS,
1257		}, {
1258			.ctrl.key = VCAP_KF_ETYPE_LEN_IS, /* Not with ARP */
1259		}, {
1260			.ctrl.key = VCAP_KF_ETYPE, /* Not with ARP */
1261		},
1262	};
1263	int idx;
1264	bool ret;
1265	enum vcap_keyfield_set keysets[10] = {};
1266
1267	matches.keysets = keysets;
1268	matches.max = ARRAY_SIZE(keysets);
1269
1270	INIT_LIST_HEAD(&ri.data.keyfields);
1271	for (idx = 0; idx < ARRAY_SIZE(ckf); idx++)
1272		list_add_tail(&ckf[idx].ctrl.list, &ri.data.keyfields);
1273
1274	ret = vcap_rule_find_keysets(&ri.data, &matches);
1275
1276	KUNIT_EXPECT_EQ(test, false, ret);
1277	KUNIT_EXPECT_EQ(test, 0, matches.cnt);
1278	KUNIT_EXPECT_EQ(test, VCAP_KFS_NO_VALUE, matches.keysets[0]);
1279}
1280
1281static void vcap_api_rule_find_keyset_many_test(struct kunit *test)
1282{
1283	struct vcap_keyset_list matches = {};
1284	struct vcap_admin admin = {
1285		.vtype = VCAP_TYPE_IS2,
1286	};
1287	struct vcap_rule_internal ri = {
1288		.admin = &admin,
1289		.vctrl = &test_vctrl,
1290	};
1291	struct vcap_client_keyfield ckf[] = {
1292		{
1293			.ctrl.key = VCAP_KF_TYPE,
1294		}, {
1295			.ctrl.key = VCAP_KF_LOOKUP_FIRST_IS,
1296		}, {
1297			.ctrl.key = VCAP_KF_8021Q_DEI_CLS,
1298		}, {
1299			.ctrl.key = VCAP_KF_8021Q_PCP_CLS,
1300		}, {
1301			.ctrl.key = VCAP_KF_8021Q_VID_CLS,
1302		}, {
1303			.ctrl.key = VCAP_KF_ISDX_CLS,
1304		}, {
1305			.ctrl.key = VCAP_KF_L2_MC_IS,
1306		}, {
1307			.ctrl.key = VCAP_KF_L2_BC_IS,
1308		},
1309	};
1310	int idx;
1311	bool ret;
1312	enum vcap_keyfield_set keysets[10] = {};
1313
1314	matches.keysets = keysets;
1315	matches.max = ARRAY_SIZE(keysets);
1316
1317	INIT_LIST_HEAD(&ri.data.keyfields);
1318	for (idx = 0; idx < ARRAY_SIZE(ckf); idx++)
1319		list_add_tail(&ckf[idx].ctrl.list, &ri.data.keyfields);
1320
1321	ret = vcap_rule_find_keysets(&ri.data, &matches);
1322
1323	KUNIT_EXPECT_EQ(test, true, ret);
1324	KUNIT_EXPECT_EQ(test, 6, matches.cnt);
1325	KUNIT_EXPECT_EQ(test, VCAP_KFS_ARP, matches.keysets[0]);
1326	KUNIT_EXPECT_EQ(test, VCAP_KFS_IP4_OTHER, matches.keysets[1]);
1327	KUNIT_EXPECT_EQ(test, VCAP_KFS_IP4_TCP_UDP, matches.keysets[2]);
1328	KUNIT_EXPECT_EQ(test, VCAP_KFS_IP6_STD, matches.keysets[3]);
1329	KUNIT_EXPECT_EQ(test, VCAP_KFS_IP_7TUPLE, matches.keysets[4]);
1330	KUNIT_EXPECT_EQ(test, VCAP_KFS_MAC_ETYPE, matches.keysets[5]);
1331}
1332
1333static void vcap_api_encode_rule_test(struct kunit *test)
1334{
1335	/* Data used by VCAP Library callback */
1336	static u32 keydata[32] = {};
1337	static u32 mskdata[32] = {};
1338	static u32 actdata[32] = {};
1339
1340	struct vcap_admin is2_admin = {
1341		.vtype = VCAP_TYPE_IS2,
1342		.first_cid = 8000000,
1343		.last_cid = 8099999,
1344		.lookups = 4,
1345		.last_valid_addr = 3071,
1346		.first_valid_addr = 0,
1347		.last_used_addr = 800,
1348		.cache = {
1349			.keystream = keydata,
1350			.maskstream = mskdata,
1351			.actionstream = actdata,
1352		},
1353	};
1354	struct vcap_rule *rule;
1355	struct vcap_rule_internal *ri;
1356	int vcap_chain_id = 8000000;
1357	enum vcap_user user = VCAP_USER_VCAP_UTIL;
1358	u16 priority = 10;
1359	int id = 100;
1360	int ret;
1361	struct vcap_u48_key smac = {
1362		.value = { 0x88, 0x75, 0x32, 0x34, 0x9e, 0xb1 },
1363		.mask = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
1364	};
1365	struct vcap_u48_key dmac = {
1366		.value = { 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 },
1367		.mask = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }
1368	};
1369	u32 port_mask_rng_value = 0x05;
1370	u32 port_mask_rng_mask = 0x0f;
1371	u32 igr_port_mask_value = 0xffabcd01;
1372	u32 igr_port_mask_mask = ~0;
1373	/* counter is written as the first operation */
1374	u32 expwriteaddr[] = {792, 792, 793, 794, 795, 796, 797};
1375	int idx;
1376
1377	vcap_test_api_init(&is2_admin);
1378
1379	/* Allocate the rule */
1380	rule = vcap_alloc_rule(&test_vctrl, &test_netdev, vcap_chain_id, user,
1381			       priority, id);
1382	KUNIT_EXPECT_PTR_NE(test, NULL, rule);
1383	ri = (struct vcap_rule_internal *)rule;
1384
1385	/* Add rule keys */
1386	ret = vcap_rule_add_key_u48(rule, VCAP_KF_L2_DMAC, &dmac);
1387	KUNIT_EXPECT_EQ(test, 0, ret);
1388	ret = vcap_rule_add_key_u48(rule, VCAP_KF_L2_SMAC, &smac);
1389	KUNIT_EXPECT_EQ(test, 0, ret);
1390	ret = vcap_rule_add_key_bit(rule, VCAP_KF_ETYPE_LEN_IS, VCAP_BIT_1);
1391	KUNIT_EXPECT_EQ(test, 0, ret);
1392	/* Cannot add the same field twice */
1393	ret = vcap_rule_add_key_bit(rule, VCAP_KF_ETYPE_LEN_IS, VCAP_BIT_1);
1394	KUNIT_EXPECT_EQ(test, -EINVAL, ret);
1395	ret = vcap_rule_add_key_bit(rule, VCAP_KF_IF_IGR_PORT_MASK_L3,
1396				    VCAP_BIT_ANY);
1397	KUNIT_EXPECT_EQ(test, 0, ret);
1398	ret = vcap_rule_add_key_u32(rule, VCAP_KF_IF_IGR_PORT_MASK_RNG,
1399				    port_mask_rng_value, port_mask_rng_mask);
1400	KUNIT_EXPECT_EQ(test, 0, ret);
1401	ret = vcap_rule_add_key_u32(rule, VCAP_KF_IF_IGR_PORT_MASK,
1402				    igr_port_mask_value, igr_port_mask_mask);
1403	KUNIT_EXPECT_EQ(test, 0, ret);
1404
1405	/* Add rule actions */
1406	ret = vcap_rule_add_action_bit(rule, VCAP_AF_POLICE_ENA, VCAP_BIT_1);
1407	KUNIT_EXPECT_EQ(test, 0, ret);
1408	ret = vcap_rule_add_action_u32(rule, VCAP_AF_CNT_ID, id);
1409	KUNIT_EXPECT_EQ(test, 0, ret);
1410	ret = vcap_rule_add_action_u32(rule, VCAP_AF_MATCH_ID, 1);
1411	KUNIT_EXPECT_EQ(test, 0, ret);
1412	ret = vcap_rule_add_action_u32(rule, VCAP_AF_MATCH_ID_MASK, 1);
1413	KUNIT_EXPECT_EQ(test, 0, ret);
1414
1415	/* For now the actionset is hardcoded */
1416	ret = vcap_set_rule_set_actionset(rule, VCAP_AFS_BASE_TYPE);
1417	KUNIT_EXPECT_EQ(test, 0, ret);
1418
1419	/* Validation with validate keyset callback */
1420	ret = vcap_val_rule(rule, ETH_P_ALL);
1421	KUNIT_EXPECT_EQ(test, 0, ret);
1422	KUNIT_EXPECT_EQ(test, VCAP_KFS_MAC_ETYPE, rule->keyset);
1423	KUNIT_EXPECT_EQ(test, VCAP_AFS_BASE_TYPE, rule->actionset);
1424	KUNIT_EXPECT_EQ(test, 6, ri->size);
1425	KUNIT_EXPECT_EQ(test, 2, ri->keyset_sw_regs);
1426	KUNIT_EXPECT_EQ(test, 4, ri->actionset_sw_regs);
1427
1428	/* Enable lookup, so the rule will be written */
1429	ret = vcap_enable_lookups(&test_vctrl, &test_netdev, 0,
1430				  rule->vcap_chain_id, rule->cookie, true);
1431	KUNIT_EXPECT_EQ(test, 0, ret);
1432
1433	/* Add rule with write callback */
1434	ret = vcap_add_rule(rule);
1435	KUNIT_EXPECT_EQ(test, 0, ret);
1436	KUNIT_EXPECT_EQ(test, 792, is2_admin.last_used_addr);
1437	for (idx = 0; idx < ARRAY_SIZE(expwriteaddr); ++idx)
1438		KUNIT_EXPECT_EQ(test, expwriteaddr[idx], test_updateaddr[idx]);
1439
1440	/* Check that the rule has been added */
1441	ret = list_empty(&is2_admin.rules);
1442	KUNIT_EXPECT_EQ(test, false, ret);
1443	KUNIT_EXPECT_EQ(test, 0, ret);
1444
1445	vcap_enable_lookups(&test_vctrl, &test_netdev, 0, 0,
1446			    rule->cookie, false);
1447
1448	ret = vcap_del_rule(&test_vctrl, &test_netdev, id);
1449	KUNIT_EXPECT_EQ(test, 0, ret);
1450
1451	vcap_free_rule(rule);
1452}
1453
1454static void vcap_api_set_rule_counter_test(struct kunit *test)
1455{
1456	struct vcap_admin is2_admin = {
1457		.cache = {
1458			.counter = 100,
1459			.sticky = true,
1460		},
1461	};
1462	struct vcap_rule_internal ri = {
1463		.data = {
1464			.id = 1001,
1465		},
1466		.addr = 600,
1467		.admin = &is2_admin,
1468		.counter_id = 1002,
1469		.vctrl = &test_vctrl,
1470	};
1471	struct vcap_rule_internal ri2 = {
1472		.data = {
1473			.id = 2001,
1474		},
1475		.addr = 700,
1476		.admin = &is2_admin,
1477		.counter_id = 2002,
1478		.vctrl = &test_vctrl,
1479	};
1480	struct vcap_counter ctr = { .value = 0, .sticky = false};
1481	struct vcap_counter ctr2 = { .value = 101, .sticky = true};
1482	int ret;
1483
1484	vcap_test_api_init(&is2_admin);
1485	list_add_tail(&ri.list, &is2_admin.rules);
1486	list_add_tail(&ri2.list, &is2_admin.rules);
1487
1488	pr_info("%s:%d\n", __func__, __LINE__);
1489	ret = vcap_rule_set_counter(&ri.data, &ctr);
1490	pr_info("%s:%d\n", __func__, __LINE__);
1491	KUNIT_EXPECT_EQ(test, 0, ret);
1492
1493	KUNIT_EXPECT_EQ(test, 1002, test_hw_counter_id);
1494	KUNIT_EXPECT_EQ(test, 0, test_hw_cache.counter);
1495	KUNIT_EXPECT_EQ(test, false, test_hw_cache.sticky);
1496	KUNIT_EXPECT_EQ(test, 600, test_updateaddr[0]);
1497
1498	ret = vcap_rule_set_counter(&ri2.data, &ctr2);
1499	KUNIT_EXPECT_EQ(test, 0, ret);
1500
1501	KUNIT_EXPECT_EQ(test, 2002, test_hw_counter_id);
1502	KUNIT_EXPECT_EQ(test, 101, test_hw_cache.counter);
1503	KUNIT_EXPECT_EQ(test, true, test_hw_cache.sticky);
1504	KUNIT_EXPECT_EQ(test, 700, test_updateaddr[1]);
1505}
1506
1507static void vcap_api_get_rule_counter_test(struct kunit *test)
1508{
1509	struct vcap_admin is2_admin = {
1510		.cache = {
1511			.counter = 100,
1512			.sticky = true,
1513		},
1514	};
1515	struct vcap_rule_internal ri = {
1516		.data = {
1517			.id = 1010,
1518		},
1519		.addr = 400,
1520		.admin = &is2_admin,
1521		.counter_id = 1011,
1522		.vctrl = &test_vctrl,
1523	};
1524	struct vcap_rule_internal ri2 = {
1525		.data = {
1526			.id = 2011,
1527		},
1528		.addr = 300,
1529		.admin = &is2_admin,
1530		.counter_id = 2012,
1531		.vctrl = &test_vctrl,
1532	};
1533	struct vcap_counter ctr = {};
1534	struct vcap_counter ctr2 = {};
1535	int ret;
1536
1537	vcap_test_api_init(&is2_admin);
1538	test_hw_cache.counter = 55;
1539	test_hw_cache.sticky = true;
1540
1541	list_add_tail(&ri.list, &is2_admin.rules);
1542	list_add_tail(&ri2.list, &is2_admin.rules);
1543
1544	ret = vcap_rule_get_counter(&ri.data, &ctr);
1545	KUNIT_EXPECT_EQ(test, 0, ret);
1546
1547	KUNIT_EXPECT_EQ(test, 1011, test_hw_counter_id);
1548	KUNIT_EXPECT_EQ(test, 55, ctr.value);
1549	KUNIT_EXPECT_EQ(test, true, ctr.sticky);
1550	KUNIT_EXPECT_EQ(test, 400, test_updateaddr[0]);
1551
1552	test_hw_cache.counter = 22;
1553	test_hw_cache.sticky = false;
1554
1555	ret = vcap_rule_get_counter(&ri2.data, &ctr2);
1556	KUNIT_EXPECT_EQ(test, 0, ret);
1557
1558	KUNIT_EXPECT_EQ(test, 2012, test_hw_counter_id);
1559	KUNIT_EXPECT_EQ(test, 22, ctr2.value);
1560	KUNIT_EXPECT_EQ(test, false, ctr2.sticky);
1561	KUNIT_EXPECT_EQ(test, 300, test_updateaddr[1]);
1562}
1563
1564static void vcap_api_rule_insert_in_order_test(struct kunit *test)
1565{
1566	/* Data used by VCAP Library callback */
1567	static u32 keydata[32] = {};
1568	static u32 mskdata[32] = {};
1569	static u32 actdata[32] = {};
1570
1571	struct vcap_admin admin = {
1572		.vtype = VCAP_TYPE_IS0,
1573		.first_cid = 10000,
1574		.last_cid = 19999,
1575		.lookups = 4,
1576		.last_valid_addr = 3071,
1577		.first_valid_addr = 0,
1578		.last_used_addr = 800,
1579		.cache = {
1580			.keystream = keydata,
1581			.maskstream = mskdata,
1582			.actionstream = actdata,
1583		},
1584	};
1585
1586	vcap_test_api_init(&admin);
1587
1588	/* Create rules with different sizes and check that they are placed
1589	 * at the correct address in the VCAP according to size
1590	 */
1591	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 10, 500, 12, 780);
1592	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 20, 400, 6, 774);
1593	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 30, 300, 3, 771);
1594	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 40, 200, 2, 768);
1595
1596	vcap_del_rule(&test_vctrl, &test_netdev, 200);
1597	vcap_del_rule(&test_vctrl, &test_netdev, 300);
1598	vcap_del_rule(&test_vctrl, &test_netdev, 400);
1599	vcap_del_rule(&test_vctrl, &test_netdev, 500);
1600}
1601
1602static void vcap_api_rule_insert_reverse_order_test(struct kunit *test)
1603{
1604	/* Data used by VCAP Library callback */
1605	static u32 keydata[32] = {};
1606	static u32 mskdata[32] = {};
1607	static u32 actdata[32] = {};
1608
1609	struct vcap_admin admin = {
1610		.vtype = VCAP_TYPE_IS0,
1611		.first_cid = 10000,
1612		.last_cid = 19999,
1613		.lookups = 4,
1614		.last_valid_addr = 3071,
1615		.first_valid_addr = 0,
1616		.last_used_addr = 800,
1617		.cache = {
1618			.keystream = keydata,
1619			.maskstream = mskdata,
1620			.actionstream = actdata,
1621		},
1622	};
1623	struct vcap_rule_internal *elem;
1624	u32 exp_addr[] = {780, 774, 771, 768, 767};
1625	int idx;
1626
1627	vcap_test_api_init(&admin);
1628
1629	/* Create rules with different sizes and check that they are placed
1630	 * at the correct address in the VCAP according to size
1631	 */
1632	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 20, 200, 2, 798);
1633	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1634	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1635	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1636
1637	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 30, 300, 3, 795);
1638	KUNIT_EXPECT_EQ(test, 6, test_move_offset);
1639	KUNIT_EXPECT_EQ(test, 3, test_move_count);
1640	KUNIT_EXPECT_EQ(test, 798, test_move_addr);
1641
1642	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 40, 400, 6, 792);
1643	KUNIT_EXPECT_EQ(test, 6, test_move_offset);
1644	KUNIT_EXPECT_EQ(test, 6, test_move_count);
1645	KUNIT_EXPECT_EQ(test, 792, test_move_addr);
1646
1647	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 50, 500, 12, 780);
1648	KUNIT_EXPECT_EQ(test, 18, test_move_offset);
1649	KUNIT_EXPECT_EQ(test, 12, test_move_count);
1650	KUNIT_EXPECT_EQ(test, 786, test_move_addr);
1651
1652	idx = 0;
1653	list_for_each_entry(elem, &admin.rules, list) {
1654		KUNIT_EXPECT_EQ(test, exp_addr[idx], elem->addr);
1655		++idx;
1656	}
1657	KUNIT_EXPECT_EQ(test, 768, admin.last_used_addr);
1658
1659	vcap_del_rule(&test_vctrl, &test_netdev, 500);
1660	vcap_del_rule(&test_vctrl, &test_netdev, 400);
1661	vcap_del_rule(&test_vctrl, &test_netdev, 300);
1662	vcap_del_rule(&test_vctrl, &test_netdev, 200);
1663}
1664
1665static void vcap_api_rule_remove_at_end_test(struct kunit *test)
1666{
1667	/* Data used by VCAP Library callback */
1668	static u32 keydata[32] = {};
1669	static u32 mskdata[32] = {};
1670	static u32 actdata[32] = {};
1671
1672	struct vcap_admin admin = {
1673		.vtype = VCAP_TYPE_IS0,
1674		.first_cid = 10000,
1675		.last_cid = 19999,
1676		.lookups = 4,
1677		.last_valid_addr = 3071,
1678		.first_valid_addr = 0,
1679		.last_used_addr = 800,
1680		.cache = {
1681			.keystream = keydata,
1682			.maskstream = mskdata,
1683			.actionstream = actdata,
1684		},
1685	};
1686	int ret;
1687
1688	vcap_test_api_init(&admin);
1689	test_init_rule_deletion();
1690
1691	/* Create rules with different sizes and check that they are placed
1692	 * at the correct address in the VCAP according to size
1693	 */
1694	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 10, 500, 12, 780);
1695	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 20, 400, 6, 774);
1696	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 30, 300, 3, 771);
1697	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 40, 200, 2, 768);
1698
1699	/* Remove rules again from the end */
1700	ret = vcap_del_rule(&test_vctrl, &test_netdev, 200);
1701	KUNIT_EXPECT_EQ(test, 0, ret);
1702	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1703	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1704	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1705	KUNIT_EXPECT_EQ(test, 768, test_init_start);
1706	KUNIT_EXPECT_EQ(test, 2, test_init_count);
1707	KUNIT_EXPECT_EQ(test, 771, admin.last_used_addr);
1708
1709	ret = vcap_del_rule(&test_vctrl, &test_netdev, 300);
1710	KUNIT_EXPECT_EQ(test, ret, 0);
1711	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1712	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1713	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1714	KUNIT_EXPECT_EQ(test, 771, test_init_start);
1715	KUNIT_EXPECT_EQ(test, 3, test_init_count);
1716	KUNIT_EXPECT_EQ(test, 774, admin.last_used_addr);
1717
1718	ret = vcap_del_rule(&test_vctrl, &test_netdev, 400);
1719	KUNIT_EXPECT_EQ(test, ret, 0);
1720	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1721	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1722	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1723	KUNIT_EXPECT_EQ(test, 774, test_init_start);
1724	KUNIT_EXPECT_EQ(test, 6, test_init_count);
1725	KUNIT_EXPECT_EQ(test, 780, admin.last_used_addr);
1726
1727	ret = vcap_del_rule(&test_vctrl, &test_netdev, 500);
1728	KUNIT_EXPECT_EQ(test, ret, 0);
1729	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1730	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1731	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1732	KUNIT_EXPECT_EQ(test, 780, test_init_start);
1733	KUNIT_EXPECT_EQ(test, 12, test_init_count);
1734	KUNIT_EXPECT_EQ(test, 3072, admin.last_used_addr);
1735}
1736
1737static void vcap_api_rule_remove_in_middle_test(struct kunit *test)
1738{
1739	/* Data used by VCAP Library callback */
1740	static u32 keydata[32] = {};
1741	static u32 mskdata[32] = {};
1742	static u32 actdata[32] = {};
1743
1744	struct vcap_admin admin = {
1745		.vtype = VCAP_TYPE_IS0,
1746		.first_cid = 10000,
1747		.last_cid = 19999,
1748		.lookups = 4,
1749		.first_valid_addr = 0,
1750		.last_used_addr = 800,
1751		.last_valid_addr = 800 - 1,
1752		.cache = {
1753			.keystream = keydata,
1754			.maskstream = mskdata,
1755			.actionstream = actdata,
1756		},
1757	};
1758	int ret;
1759
1760	vcap_test_api_init(&admin);
1761
1762	/* Create rules with different sizes and check that they are placed
1763	 * at the correct address in the VCAP according to size
1764	 */
1765	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 10, 500, 12, 780);
1766	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 20, 400, 6, 774);
1767	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 30, 300, 3, 771);
1768	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 40, 200, 2, 768);
1769
1770	/* Remove rules in the middle */
1771	test_init_rule_deletion();
1772	ret = vcap_del_rule(&test_vctrl, &test_netdev, 400);
1773	KUNIT_EXPECT_EQ(test, 0, ret);
1774	KUNIT_EXPECT_EQ(test, 768, test_move_addr);
1775	KUNIT_EXPECT_EQ(test, -6, test_move_offset);
1776	KUNIT_EXPECT_EQ(test, 6, test_move_count);
1777	KUNIT_EXPECT_EQ(test, 768, test_init_start);
1778	KUNIT_EXPECT_EQ(test, 6, test_init_count);
1779	KUNIT_EXPECT_EQ(test, 774, admin.last_used_addr);
1780
1781	test_init_rule_deletion();
1782	ret = vcap_del_rule(&test_vctrl, &test_netdev, 300);
1783	KUNIT_EXPECT_EQ(test, 0, ret);
1784	KUNIT_EXPECT_EQ(test, 774, test_move_addr);
1785	KUNIT_EXPECT_EQ(test, -4, test_move_offset);
1786	KUNIT_EXPECT_EQ(test, 2, test_move_count);
1787	KUNIT_EXPECT_EQ(test, 774, test_init_start);
1788	KUNIT_EXPECT_EQ(test, 4, test_init_count);
1789	KUNIT_EXPECT_EQ(test, 778, admin.last_used_addr);
1790
1791	test_init_rule_deletion();
1792	ret = vcap_del_rule(&test_vctrl, &test_netdev, 500);
1793	KUNIT_EXPECT_EQ(test, 0, ret);
1794	KUNIT_EXPECT_EQ(test, 778, test_move_addr);
1795	KUNIT_EXPECT_EQ(test, -20, test_move_offset);
1796	KUNIT_EXPECT_EQ(test, 2, test_move_count);
1797	KUNIT_EXPECT_EQ(test, 778, test_init_start);
1798	KUNIT_EXPECT_EQ(test, 20, test_init_count);
1799	KUNIT_EXPECT_EQ(test, 798, admin.last_used_addr);
1800
1801	test_init_rule_deletion();
1802	ret = vcap_del_rule(&test_vctrl, &test_netdev, 200);
1803	KUNIT_EXPECT_EQ(test, 0, ret);
1804	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1805	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1806	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1807	KUNIT_EXPECT_EQ(test, 798, test_init_start);
1808	KUNIT_EXPECT_EQ(test, 2, test_init_count);
1809	KUNIT_EXPECT_EQ(test, 800, admin.last_used_addr);
1810}
1811
1812static void vcap_api_rule_remove_in_front_test(struct kunit *test)
1813{
1814	/* Data used by VCAP Library callback */
1815	static u32 keydata[32] = {};
1816	static u32 mskdata[32] = {};
1817	static u32 actdata[32] = {};
1818
1819	struct vcap_admin admin = {
1820		.vtype = VCAP_TYPE_IS0,
1821		.first_cid = 10000,
1822		.last_cid = 19999,
1823		.lookups = 4,
1824		.first_valid_addr = 0,
1825		.last_used_addr = 800,
1826		.last_valid_addr = 800 - 1,
1827		.cache = {
1828			.keystream = keydata,
1829			.maskstream = mskdata,
1830			.actionstream = actdata,
1831		},
1832	};
1833	int ret;
1834
1835	vcap_test_api_init(&admin);
1836
1837	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 10, 500, 12, 780);
1838	KUNIT_EXPECT_EQ(test, 780, admin.last_used_addr);
1839
1840	test_init_rule_deletion();
1841	ret = vcap_del_rule(&test_vctrl, &test_netdev, 500);
1842	KUNIT_EXPECT_EQ(test, 0, ret);
1843	KUNIT_EXPECT_EQ(test, 0, test_move_addr);
1844	KUNIT_EXPECT_EQ(test, 0, test_move_offset);
1845	KUNIT_EXPECT_EQ(test, 0, test_move_count);
1846	KUNIT_EXPECT_EQ(test, 780, test_init_start);
1847	KUNIT_EXPECT_EQ(test, 12, test_init_count);
1848	KUNIT_EXPECT_EQ(test, 800, admin.last_used_addr);
1849
1850	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 20, 400, 6, 792);
1851	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 30, 300, 3, 789);
1852	test_vcap_xn_rule_creator(test, 10000, VCAP_USER_QOS, 40, 200, 2, 786);
1853
1854	test_init_rule_deletion();
1855	ret = vcap_del_rule(&test_vctrl, &test_netdev, 400);
1856	KUNIT_EXPECT_EQ(test, 0, ret);
1857	KUNIT_EXPECT_EQ(test, 786, test_move_addr);
1858	KUNIT_EXPECT_EQ(test, -8, test_move_offset);
1859	KUNIT_EXPECT_EQ(test, 6, test_move_count);
1860	KUNIT_EXPECT_EQ(test, 786, test_init_start);
1861	KUNIT_EXPECT_EQ(test, 8, test_init_count);
1862	KUNIT_EXPECT_EQ(test, 794, admin.last_used_addr);
1863
1864	vcap_del_rule(&test_vctrl, &test_netdev, 200);
1865	vcap_del_rule(&test_vctrl, &test_netdev, 300);
1866}
1867
1868static struct kunit_case vcap_api_rule_remove_test_cases[] = {
1869	KUNIT_CASE(vcap_api_rule_remove_at_end_test),
1870	KUNIT_CASE(vcap_api_rule_remove_in_middle_test),
1871	KUNIT_CASE(vcap_api_rule_remove_in_front_test),
1872	{}
1873};
1874
1875static void vcap_api_next_lookup_basic_test(struct kunit *test)
1876{
1877	struct vcap_admin admin1 = {
1878		.vtype = VCAP_TYPE_IS2,
1879		.vinst = 0,
1880		.first_cid = 8000000,
1881		.last_cid = 8199999,
1882		.lookups = 4,
1883		.lookups_per_instance = 2,
1884	};
1885	struct vcap_admin admin2 = {
1886		.vtype = VCAP_TYPE_IS2,
1887		.vinst = 1,
1888		.first_cid = 8200000,
1889		.last_cid = 8399999,
1890		.lookups = 4,
1891		.lookups_per_instance = 2,
1892	};
1893	bool ret;
1894
1895	vcap_test_api_init(&admin1);
1896	list_add_tail(&admin2.list, &test_vctrl.list);
1897
1898	ret = vcap_is_next_lookup(&test_vctrl, 8000000, 1001000);
1899	KUNIT_EXPECT_EQ(test, false, ret);
1900	ret = vcap_is_next_lookup(&test_vctrl, 8000000, 8001000);
1901	KUNIT_EXPECT_EQ(test, false, ret);
1902	ret = vcap_is_next_lookup(&test_vctrl, 8000000, 8101000);
1903	KUNIT_EXPECT_EQ(test, true, ret);
1904
1905	ret = vcap_is_next_lookup(&test_vctrl, 8100000, 8101000);
1906	KUNIT_EXPECT_EQ(test, false, ret);
1907	ret = vcap_is_next_lookup(&test_vctrl, 8100000, 8201000);
1908	KUNIT_EXPECT_EQ(test, true, ret);
1909
1910	ret = vcap_is_next_lookup(&test_vctrl, 8200000, 8201000);
1911	KUNIT_EXPECT_EQ(test, false, ret);
1912	ret = vcap_is_next_lookup(&test_vctrl, 8200000, 8301000);
1913	KUNIT_EXPECT_EQ(test, true, ret);
1914
1915	ret = vcap_is_next_lookup(&test_vctrl, 8300000, 8301000);
1916	KUNIT_EXPECT_EQ(test, false, ret);
1917	ret = vcap_is_next_lookup(&test_vctrl, 8300000, 8401000);
1918	KUNIT_EXPECT_EQ(test, false, ret);
1919}
1920
1921static void vcap_api_next_lookup_advanced_test(struct kunit *test)
1922{
1923	struct vcap_admin admin[] = {
1924	{
1925		.vtype = VCAP_TYPE_IS0,
1926		.vinst = 0,
1927		.first_cid = 1000000,
1928		.last_cid =  1199999,
1929		.lookups = 6,
1930		.lookups_per_instance = 2,
1931	}, {
1932		.vtype = VCAP_TYPE_IS0,
1933		.vinst = 1,
1934		.first_cid = 1200000,
1935		.last_cid =  1399999,
1936		.lookups = 6,
1937		.lookups_per_instance = 2,
1938	}, {
1939		.vtype = VCAP_TYPE_IS0,
1940		.vinst = 2,
1941		.first_cid = 1400000,
1942		.last_cid =  1599999,
1943		.lookups = 6,
1944		.lookups_per_instance = 2,
1945	}, {
1946		.vtype = VCAP_TYPE_IS2,
1947		.vinst = 0,
1948		.first_cid = 8000000,
1949		.last_cid = 8199999,
1950		.lookups = 4,
1951		.lookups_per_instance = 2,
1952	}, {
1953		.vtype = VCAP_TYPE_IS2,
1954		.vinst = 1,
1955		.first_cid = 8200000,
1956		.last_cid = 8399999,
1957		.lookups = 4,
1958		.lookups_per_instance = 2,
1959	}
1960	};
1961	bool ret;
1962
1963	vcap_test_api_init(&admin[0]);
1964	list_add_tail(&admin[1].list, &test_vctrl.list);
1965	list_add_tail(&admin[2].list, &test_vctrl.list);
1966	list_add_tail(&admin[3].list, &test_vctrl.list);
1967	list_add_tail(&admin[4].list, &test_vctrl.list);
1968
1969	ret = vcap_is_next_lookup(&test_vctrl, 1000000, 1001000);
1970	KUNIT_EXPECT_EQ(test, false, ret);
1971	ret = vcap_is_next_lookup(&test_vctrl, 1000000, 1101000);
1972	KUNIT_EXPECT_EQ(test, true, ret);
1973
1974	ret = vcap_is_next_lookup(&test_vctrl, 1100000, 1201000);
1975	KUNIT_EXPECT_EQ(test, true, ret);
1976	ret = vcap_is_next_lookup(&test_vctrl, 1100000, 1301000);
1977	KUNIT_EXPECT_EQ(test, true, ret);
1978	ret = vcap_is_next_lookup(&test_vctrl, 1100000, 8101000);
1979	KUNIT_EXPECT_EQ(test, true, ret);
1980	ret = vcap_is_next_lookup(&test_vctrl, 1300000, 1401000);
1981	KUNIT_EXPECT_EQ(test, true, ret);
1982	ret = vcap_is_next_lookup(&test_vctrl, 1400000, 1501000);
1983	KUNIT_EXPECT_EQ(test, true, ret);
1984	ret = vcap_is_next_lookup(&test_vctrl, 1500000, 8001000);
1985	KUNIT_EXPECT_EQ(test, true, ret);
1986
1987	ret = vcap_is_next_lookup(&test_vctrl, 8000000, 8001000);
1988	KUNIT_EXPECT_EQ(test, false, ret);
1989	ret = vcap_is_next_lookup(&test_vctrl, 8000000, 8101000);
1990	KUNIT_EXPECT_EQ(test, true, ret);
1991
1992	ret = vcap_is_next_lookup(&test_vctrl, 8300000, 8301000);
1993	KUNIT_EXPECT_EQ(test, false, ret);
1994	ret = vcap_is_next_lookup(&test_vctrl, 8300000, 8401000);
1995	KUNIT_EXPECT_EQ(test, false, ret);
1996}
1997
1998static void vcap_api_filter_unsupported_keys_test(struct kunit *test)
1999{
2000	struct vcap_admin admin = {
2001		.vtype = VCAP_TYPE_IS2,
2002	};
2003	struct vcap_rule_internal ri = {
2004		.admin = &admin,
2005		.vctrl = &test_vctrl,
2006		.data.keyset = VCAP_KFS_MAC_ETYPE,
2007	};
2008	enum vcap_key_field keylist[] = {
2009		VCAP_KF_TYPE,
2010		VCAP_KF_LOOKUP_FIRST_IS,
2011		VCAP_KF_ARP_ADDR_SPACE_OK_IS,  /* arp keys are not in keyset */
2012		VCAP_KF_ARP_PROTO_SPACE_OK_IS,
2013		VCAP_KF_ARP_LEN_OK_IS,
2014		VCAP_KF_ARP_TGT_MATCH_IS,
2015		VCAP_KF_ARP_SENDER_MATCH_IS,
2016		VCAP_KF_ARP_OPCODE_UNKNOWN_IS,
2017		VCAP_KF_ARP_OPCODE,
2018		VCAP_KF_8021Q_DEI_CLS,
2019		VCAP_KF_8021Q_PCP_CLS,
2020		VCAP_KF_8021Q_VID_CLS,
2021		VCAP_KF_L2_MC_IS,
2022		VCAP_KF_L2_BC_IS,
2023	};
2024	enum vcap_key_field expected[] = {
2025		VCAP_KF_TYPE,
2026		VCAP_KF_LOOKUP_FIRST_IS,
2027		VCAP_KF_8021Q_DEI_CLS,
2028		VCAP_KF_8021Q_PCP_CLS,
2029		VCAP_KF_8021Q_VID_CLS,
2030		VCAP_KF_L2_MC_IS,
2031		VCAP_KF_L2_BC_IS,
2032	};
2033	struct vcap_client_keyfield *ckf, *next;
2034	bool ret;
2035	int idx;
2036
2037	/* Add all keys to the rule */
2038	INIT_LIST_HEAD(&ri.data.keyfields);
2039	for (idx = 0; idx < ARRAY_SIZE(keylist); idx++) {
2040		ckf = kzalloc(sizeof(*ckf), GFP_KERNEL);
2041		if (ckf) {
2042			ckf->ctrl.key = keylist[idx];
2043			list_add_tail(&ckf->ctrl.list, &ri.data.keyfields);
2044		}
2045	}
2046
2047	KUNIT_EXPECT_EQ(test, 14, ARRAY_SIZE(keylist));
2048
2049	/* Drop unsupported keys from the rule */
2050	ret = vcap_filter_rule_keys(&ri.data, NULL, 0, true);
2051
2052	KUNIT_EXPECT_EQ(test, 0, ret);
2053
2054	/* Check remaining keys in the rule */
2055	idx = 0;
2056	list_for_each_entry_safe(ckf, next, &ri.data.keyfields, ctrl.list) {
2057		KUNIT_EXPECT_EQ(test, expected[idx], ckf->ctrl.key);
2058		list_del(&ckf->ctrl.list);
2059		kfree(ckf);
2060		++idx;
2061	}
2062	KUNIT_EXPECT_EQ(test, 7, idx);
2063}
2064
2065static void vcap_api_filter_keylist_test(struct kunit *test)
2066{
2067	struct vcap_admin admin = {
2068		.vtype = VCAP_TYPE_IS0,
2069	};
2070	struct vcap_rule_internal ri = {
2071		.admin = &admin,
2072		.vctrl = &test_vctrl,
2073		.data.keyset = VCAP_KFS_NORMAL_7TUPLE,
2074	};
2075	enum vcap_key_field keylist[] = {
2076		VCAP_KF_TYPE,
2077		VCAP_KF_LOOKUP_FIRST_IS,
2078		VCAP_KF_LOOKUP_GEN_IDX_SEL,
2079		VCAP_KF_LOOKUP_GEN_IDX,
2080		VCAP_KF_IF_IGR_PORT_MASK_SEL,
2081		VCAP_KF_IF_IGR_PORT_MASK,
2082		VCAP_KF_L2_MC_IS,
2083		VCAP_KF_L2_BC_IS,
2084		VCAP_KF_8021Q_VLAN_TAGS,
2085		VCAP_KF_8021Q_TPID0,
2086		VCAP_KF_8021Q_PCP0,
2087		VCAP_KF_8021Q_DEI0,
2088		VCAP_KF_8021Q_VID0,
2089		VCAP_KF_8021Q_TPID1,
2090		VCAP_KF_8021Q_PCP1,
2091		VCAP_KF_8021Q_DEI1,
2092		VCAP_KF_8021Q_VID1,
2093		VCAP_KF_8021Q_TPID2,
2094		VCAP_KF_8021Q_PCP2,
2095		VCAP_KF_8021Q_DEI2,
2096		VCAP_KF_8021Q_VID2,
2097		VCAP_KF_L2_DMAC,
2098		VCAP_KF_L2_SMAC,
2099		VCAP_KF_IP_MC_IS,
2100		VCAP_KF_ETYPE_LEN_IS,
2101		VCAP_KF_ETYPE,
2102		VCAP_KF_IP_SNAP_IS,
2103		VCAP_KF_IP4_IS,
2104		VCAP_KF_L3_FRAGMENT_TYPE,
2105		VCAP_KF_L3_FRAG_INVLD_L4_LEN,
2106		VCAP_KF_L3_OPTIONS_IS,
2107		VCAP_KF_L3_DSCP,
2108		VCAP_KF_L3_IP6_DIP,
2109		VCAP_KF_L3_IP6_SIP,
2110		VCAP_KF_TCP_UDP_IS,
2111		VCAP_KF_TCP_IS,
2112		VCAP_KF_L4_SPORT,
2113		VCAP_KF_L4_RNG,
2114	};
2115	enum vcap_key_field droplist[] = {
2116		VCAP_KF_8021Q_TPID1,
2117		VCAP_KF_8021Q_PCP1,
2118		VCAP_KF_8021Q_DEI1,
2119		VCAP_KF_8021Q_VID1,
2120		VCAP_KF_8021Q_TPID2,
2121		VCAP_KF_8021Q_PCP2,
2122		VCAP_KF_8021Q_DEI2,
2123		VCAP_KF_8021Q_VID2,
2124		VCAP_KF_L3_IP6_DIP,
2125		VCAP_KF_L3_IP6_SIP,
2126		VCAP_KF_L4_SPORT,
2127		VCAP_KF_L4_RNG,
2128	};
2129	enum vcap_key_field expected[] = {
2130		VCAP_KF_TYPE,
2131		VCAP_KF_LOOKUP_FIRST_IS,
2132		VCAP_KF_LOOKUP_GEN_IDX_SEL,
2133		VCAP_KF_LOOKUP_GEN_IDX,
2134		VCAP_KF_IF_IGR_PORT_MASK_SEL,
2135		VCAP_KF_IF_IGR_PORT_MASK,
2136		VCAP_KF_L2_MC_IS,
2137		VCAP_KF_L2_BC_IS,
2138		VCAP_KF_8021Q_VLAN_TAGS,
2139		VCAP_KF_8021Q_TPID0,
2140		VCAP_KF_8021Q_PCP0,
2141		VCAP_KF_8021Q_DEI0,
2142		VCAP_KF_8021Q_VID0,
2143		VCAP_KF_L2_DMAC,
2144		VCAP_KF_L2_SMAC,
2145		VCAP_KF_IP_MC_IS,
2146		VCAP_KF_ETYPE_LEN_IS,
2147		VCAP_KF_ETYPE,
2148		VCAP_KF_IP_SNAP_IS,
2149		VCAP_KF_IP4_IS,
2150		VCAP_KF_L3_FRAGMENT_TYPE,
2151		VCAP_KF_L3_FRAG_INVLD_L4_LEN,
2152		VCAP_KF_L3_OPTIONS_IS,
2153		VCAP_KF_L3_DSCP,
2154		VCAP_KF_TCP_UDP_IS,
2155		VCAP_KF_TCP_IS,
2156	};
2157	struct vcap_client_keyfield *ckf, *next;
2158	bool ret;
2159	int idx;
2160
2161	/* Add all keys to the rule */
2162	INIT_LIST_HEAD(&ri.data.keyfields);
2163	for (idx = 0; idx < ARRAY_SIZE(keylist); idx++) {
2164		ckf = kzalloc(sizeof(*ckf), GFP_KERNEL);
2165		if (ckf) {
2166			ckf->ctrl.key = keylist[idx];
2167			list_add_tail(&ckf->ctrl.list, &ri.data.keyfields);
2168		}
2169	}
2170
2171	KUNIT_EXPECT_EQ(test, 38, ARRAY_SIZE(keylist));
2172
2173	/* Drop listed keys from the rule */
2174	ret = vcap_filter_rule_keys(&ri.data, droplist, ARRAY_SIZE(droplist),
2175				    false);
2176
2177	KUNIT_EXPECT_EQ(test, 0, ret);
2178
2179	/* Check remaining keys in the rule */
2180	idx = 0;
2181	list_for_each_entry_safe(ckf, next, &ri.data.keyfields, ctrl.list) {
2182		KUNIT_EXPECT_EQ(test, expected[idx], ckf->ctrl.key);
2183		list_del(&ckf->ctrl.list);
2184		kfree(ckf);
2185		++idx;
2186	}
2187	KUNIT_EXPECT_EQ(test, 26, idx);
2188}
2189
2190static void vcap_api_rule_chain_path_test(struct kunit *test)
2191{
2192	struct vcap_admin admin1 = {
2193		.vtype = VCAP_TYPE_IS0,
2194		.vinst = 0,
2195		.first_cid = 1000000,
2196		.last_cid =  1199999,
2197		.lookups = 6,
2198		.lookups_per_instance = 2,
2199	};
2200	struct vcap_enabled_port eport3 = {
2201		.ndev = &test_netdev,
2202		.cookie = 0x100,
2203		.src_cid = 0,
2204		.dst_cid = 1000000,
2205	};
2206	struct vcap_enabled_port eport2 = {
2207		.ndev = &test_netdev,
2208		.cookie = 0x200,
2209		.src_cid = 1000000,
2210		.dst_cid = 1100000,
2211	};
2212	struct vcap_enabled_port eport1 = {
2213		.ndev = &test_netdev,
2214		.cookie = 0x300,
2215		.src_cid = 1100000,
2216		.dst_cid = 8000000,
2217	};
2218	bool ret;
2219	int chain;
2220
2221	vcap_test_api_init(&admin1);
2222	list_add_tail(&eport1.list, &admin1.enabled);
2223	list_add_tail(&eport2.list, &admin1.enabled);
2224	list_add_tail(&eport3.list, &admin1.enabled);
2225
2226	ret = vcap_path_exist(&test_vctrl, &test_netdev, 1000000);
2227	KUNIT_EXPECT_EQ(test, true, ret);
2228
2229	ret = vcap_path_exist(&test_vctrl, &test_netdev, 1100000);
2230	KUNIT_EXPECT_EQ(test, true, ret);
2231
2232	ret = vcap_path_exist(&test_vctrl, &test_netdev, 1200000);
2233	KUNIT_EXPECT_EQ(test, false, ret);
2234
2235	chain = vcap_get_next_chain(&test_vctrl, &test_netdev, 0);
2236	KUNIT_EXPECT_EQ(test, 1000000, chain);
2237
2238	chain = vcap_get_next_chain(&test_vctrl, &test_netdev, 1000000);
2239	KUNIT_EXPECT_EQ(test, 1100000, chain);
2240
2241	chain = vcap_get_next_chain(&test_vctrl, &test_netdev, 1100000);
2242	KUNIT_EXPECT_EQ(test, 8000000, chain);
2243}
2244
2245static struct kunit_case vcap_api_rule_enable_test_cases[] = {
2246	KUNIT_CASE(vcap_api_rule_chain_path_test),
2247	{}
2248};
2249
2250static struct kunit_suite vcap_api_rule_enable_test_suite = {
2251	.name = "VCAP_API_Rule_Enable_Testsuite",
2252	.test_cases = vcap_api_rule_enable_test_cases,
2253};
2254
2255static struct kunit_suite vcap_api_rule_remove_test_suite = {
2256	.name = "VCAP_API_Rule_Remove_Testsuite",
2257	.test_cases = vcap_api_rule_remove_test_cases,
2258};
2259
2260static struct kunit_case vcap_api_rule_insert_test_cases[] = {
2261	KUNIT_CASE(vcap_api_rule_insert_in_order_test),
2262	KUNIT_CASE(vcap_api_rule_insert_reverse_order_test),
2263	{}
2264};
2265
2266static struct kunit_suite vcap_api_rule_insert_test_suite = {
2267	.name = "VCAP_API_Rule_Insert_Testsuite",
2268	.test_cases = vcap_api_rule_insert_test_cases,
2269};
2270
2271static struct kunit_case vcap_api_rule_counter_test_cases[] = {
2272	KUNIT_CASE(vcap_api_set_rule_counter_test),
2273	KUNIT_CASE(vcap_api_get_rule_counter_test),
2274	{}
2275};
2276
2277static struct kunit_suite vcap_api_rule_counter_test_suite = {
2278	.name = "VCAP_API_Rule_Counter_Testsuite",
2279	.test_cases = vcap_api_rule_counter_test_cases,
2280};
2281
2282static struct kunit_case vcap_api_support_test_cases[] = {
2283	KUNIT_CASE(vcap_api_next_lookup_basic_test),
2284	KUNIT_CASE(vcap_api_next_lookup_advanced_test),
2285	KUNIT_CASE(vcap_api_filter_unsupported_keys_test),
2286	KUNIT_CASE(vcap_api_filter_keylist_test),
2287	{}
2288};
2289
2290static struct kunit_suite vcap_api_support_test_suite = {
2291	.name = "VCAP_API_Support_Testsuite",
2292	.test_cases = vcap_api_support_test_cases,
2293};
2294
2295static struct kunit_case vcap_api_full_rule_test_cases[] = {
2296	KUNIT_CASE(vcap_api_rule_find_keyset_basic_test),
2297	KUNIT_CASE(vcap_api_rule_find_keyset_failed_test),
2298	KUNIT_CASE(vcap_api_rule_find_keyset_many_test),
2299	KUNIT_CASE(vcap_api_encode_rule_test),
2300	{}
2301};
2302
2303static struct kunit_suite vcap_api_full_rule_test_suite = {
2304	.name = "VCAP_API_Full_Rule_Testsuite",
2305	.test_cases = vcap_api_full_rule_test_cases,
2306};
2307
2308static struct kunit_case vcap_api_rule_value_test_cases[] = {
2309	KUNIT_CASE(vcap_api_rule_add_keyvalue_test),
2310	KUNIT_CASE(vcap_api_rule_add_actionvalue_test),
2311	{}
2312};
2313
2314static struct kunit_suite vcap_api_rule_value_test_suite = {
2315	.name = "VCAP_API_Rule_Value_Testsuite",
2316	.test_cases = vcap_api_rule_value_test_cases,
2317};
2318
2319static struct kunit_case vcap_api_encoding_test_cases[] = {
2320	KUNIT_CASE(vcap_api_set_bit_1_test),
2321	KUNIT_CASE(vcap_api_set_bit_0_test),
2322	KUNIT_CASE(vcap_api_iterator_init_test),
2323	KUNIT_CASE(vcap_api_iterator_next_test),
2324	KUNIT_CASE(vcap_api_encode_typegroups_test),
2325	KUNIT_CASE(vcap_api_encode_bit_test),
2326	KUNIT_CASE(vcap_api_encode_field_test),
2327	KUNIT_CASE(vcap_api_encode_short_field_test),
2328	KUNIT_CASE(vcap_api_encode_keyfield_test),
2329	KUNIT_CASE(vcap_api_encode_max_keyfield_test),
2330	KUNIT_CASE(vcap_api_encode_actionfield_test),
2331	KUNIT_CASE(vcap_api_keyfield_typegroup_test),
2332	KUNIT_CASE(vcap_api_actionfield_typegroup_test),
2333	KUNIT_CASE(vcap_api_vcap_keyfields_test),
2334	KUNIT_CASE(vcap_api_vcap_actionfields_test),
2335	KUNIT_CASE(vcap_api_encode_rule_keyset_test),
2336	KUNIT_CASE(vcap_api_encode_rule_actionset_test),
2337	{}
2338};
2339
2340static struct kunit_suite vcap_api_encoding_test_suite = {
2341	.name = "VCAP_API_Encoding_Testsuite",
2342	.test_cases = vcap_api_encoding_test_cases,
2343};
2344
2345kunit_test_suite(vcap_api_rule_enable_test_suite);
2346kunit_test_suite(vcap_api_rule_remove_test_suite);
2347kunit_test_suite(vcap_api_rule_insert_test_suite);
2348kunit_test_suite(vcap_api_rule_counter_test_suite);
2349kunit_test_suite(vcap_api_support_test_suite);
2350kunit_test_suite(vcap_api_full_rule_test_suite);
2351kunit_test_suite(vcap_api_rule_value_test_suite);
2352kunit_test_suite(vcap_api_encoding_test_suite);