vcap_api_kunit.c 67 KB

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