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