test_lpm_map.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Randomized tests for eBPF longest-prefix-match maps
  4. *
  5. * This program runs randomized tests against the lpm-bpf-map. It implements a
  6. * "Trivial Longest Prefix Match" (tlpm) based on simple, linear, singly linked
  7. * lists. The implementation should be pretty straightforward.
  8. *
  9. * Based on tlpm, this inserts randomized data into bpf-lpm-maps and verifies
  10. * the trie-based bpf-map implementation behaves the same way as tlpm.
  11. */
  12. #include <assert.h>
  13. #include <errno.h>
  14. #include <inttypes.h>
  15. #include <linux/bpf.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <arpa/inet.h>
  23. #include <sys/time.h>
  24. #include <bpf/bpf.h>
  25. #include "bpf_util.h"
  26. #include "bpf_rlimit.h"
  27. struct tlpm_node {
  28. struct tlpm_node *next;
  29. size_t n_bits;
  30. uint8_t key[];
  31. };
  32. static struct tlpm_node *tlpm_match(struct tlpm_node *list,
  33. const uint8_t *key,
  34. size_t n_bits);
  35. static struct tlpm_node *tlpm_add(struct tlpm_node *list,
  36. const uint8_t *key,
  37. size_t n_bits)
  38. {
  39. struct tlpm_node *node;
  40. size_t n;
  41. n = (n_bits + 7) / 8;
  42. /* 'overwrite' an equivalent entry if one already exists */
  43. node = tlpm_match(list, key, n_bits);
  44. if (node && node->n_bits == n_bits) {
  45. memcpy(node->key, key, n);
  46. return list;
  47. }
  48. /* add new entry with @key/@n_bits to @list and return new head */
  49. node = malloc(sizeof(*node) + n);
  50. assert(node);
  51. node->next = list;
  52. node->n_bits = n_bits;
  53. memcpy(node->key, key, n);
  54. return node;
  55. }
  56. static void tlpm_clear(struct tlpm_node *list)
  57. {
  58. struct tlpm_node *node;
  59. /* free all entries in @list */
  60. while ((node = list)) {
  61. list = list->next;
  62. free(node);
  63. }
  64. }
  65. static struct tlpm_node *tlpm_match(struct tlpm_node *list,
  66. const uint8_t *key,
  67. size_t n_bits)
  68. {
  69. struct tlpm_node *best = NULL;
  70. size_t i;
  71. /* Perform longest prefix-match on @key/@n_bits. That is, iterate all
  72. * entries and match each prefix against @key. Remember the "best"
  73. * entry we find (i.e., the longest prefix that matches) and return it
  74. * to the caller when done.
  75. */
  76. for ( ; list; list = list->next) {
  77. for (i = 0; i < n_bits && i < list->n_bits; ++i) {
  78. if ((key[i / 8] & (1 << (7 - i % 8))) !=
  79. (list->key[i / 8] & (1 << (7 - i % 8))))
  80. break;
  81. }
  82. if (i >= list->n_bits) {
  83. if (!best || i > best->n_bits)
  84. best = list;
  85. }
  86. }
  87. return best;
  88. }
  89. static struct tlpm_node *tlpm_delete(struct tlpm_node *list,
  90. const uint8_t *key,
  91. size_t n_bits)
  92. {
  93. struct tlpm_node *best = tlpm_match(list, key, n_bits);
  94. struct tlpm_node *node;
  95. if (!best || best->n_bits != n_bits)
  96. return list;
  97. if (best == list) {
  98. node = best->next;
  99. free(best);
  100. return node;
  101. }
  102. for (node = list; node; node = node->next) {
  103. if (node->next == best) {
  104. node->next = best->next;
  105. free(best);
  106. return list;
  107. }
  108. }
  109. /* should never get here */
  110. assert(0);
  111. return list;
  112. }
  113. static void test_lpm_basic(void)
  114. {
  115. struct tlpm_node *list = NULL, *t1, *t2;
  116. /* very basic, static tests to verify tlpm works as expected */
  117. assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  118. t1 = list = tlpm_add(list, (uint8_t[]){ 0xff }, 8);
  119. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  120. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
  121. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0x00 }, 16));
  122. assert(!tlpm_match(list, (uint8_t[]){ 0x7f }, 8));
  123. assert(!tlpm_match(list, (uint8_t[]){ 0xfe }, 8));
  124. assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 7));
  125. t2 = list = tlpm_add(list, (uint8_t[]){ 0xff, 0xff }, 16);
  126. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  127. assert(t2 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
  128. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 15));
  129. assert(!tlpm_match(list, (uint8_t[]){ 0x7f, 0xff }, 16));
  130. list = tlpm_delete(list, (uint8_t[]){ 0xff, 0xff }, 16);
  131. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  132. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
  133. list = tlpm_delete(list, (uint8_t[]){ 0xff }, 8);
  134. assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  135. tlpm_clear(list);
  136. }
  137. static void test_lpm_order(void)
  138. {
  139. struct tlpm_node *t1, *t2, *l1 = NULL, *l2 = NULL;
  140. size_t i, j;
  141. /* Verify the tlpm implementation works correctly regardless of the
  142. * order of entries. Insert a random set of entries into @l1, and copy
  143. * the same data in reverse order into @l2. Then verify a lookup of
  144. * random keys will yield the same result in both sets.
  145. */
  146. for (i = 0; i < (1 << 12); ++i)
  147. l1 = tlpm_add(l1, (uint8_t[]){
  148. rand() % 0xff,
  149. rand() % 0xff,
  150. }, rand() % 16 + 1);
  151. for (t1 = l1; t1; t1 = t1->next)
  152. l2 = tlpm_add(l2, t1->key, t1->n_bits);
  153. for (i = 0; i < (1 << 8); ++i) {
  154. uint8_t key[] = { rand() % 0xff, rand() % 0xff };
  155. t1 = tlpm_match(l1, key, 16);
  156. t2 = tlpm_match(l2, key, 16);
  157. assert(!t1 == !t2);
  158. if (t1) {
  159. assert(t1->n_bits == t2->n_bits);
  160. for (j = 0; j < t1->n_bits; ++j)
  161. assert((t1->key[j / 8] & (1 << (7 - j % 8))) ==
  162. (t2->key[j / 8] & (1 << (7 - j % 8))));
  163. }
  164. }
  165. tlpm_clear(l1);
  166. tlpm_clear(l2);
  167. }
  168. static void test_lpm_map(int keysize)
  169. {
  170. size_t i, j, n_matches, n_matches_after_delete, n_nodes, n_lookups;
  171. struct tlpm_node *t, *list = NULL;
  172. struct bpf_lpm_trie_key *key;
  173. uint8_t *data, *value;
  174. int r, map;
  175. /* Compare behavior of tlpm vs. bpf-lpm. Create a randomized set of
  176. * prefixes and insert it into both tlpm and bpf-lpm. Then run some
  177. * randomized lookups and verify both maps return the same result.
  178. */
  179. n_matches = 0;
  180. n_matches_after_delete = 0;
  181. n_nodes = 1 << 8;
  182. n_lookups = 1 << 16;
  183. data = alloca(keysize);
  184. memset(data, 0, keysize);
  185. value = alloca(keysize + 1);
  186. memset(value, 0, keysize + 1);
  187. key = alloca(sizeof(*key) + keysize);
  188. memset(key, 0, sizeof(*key) + keysize);
  189. map = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  190. sizeof(*key) + keysize,
  191. keysize + 1,
  192. 4096,
  193. BPF_F_NO_PREALLOC);
  194. assert(map >= 0);
  195. for (i = 0; i < n_nodes; ++i) {
  196. for (j = 0; j < keysize; ++j)
  197. value[j] = rand() & 0xff;
  198. value[keysize] = rand() % (8 * keysize + 1);
  199. list = tlpm_add(list, value, value[keysize]);
  200. key->prefixlen = value[keysize];
  201. memcpy(key->data, value, keysize);
  202. r = bpf_map_update_elem(map, key, value, 0);
  203. assert(!r);
  204. }
  205. for (i = 0; i < n_lookups; ++i) {
  206. for (j = 0; j < keysize; ++j)
  207. data[j] = rand() & 0xff;
  208. t = tlpm_match(list, data, 8 * keysize);
  209. key->prefixlen = 8 * keysize;
  210. memcpy(key->data, data, keysize);
  211. r = bpf_map_lookup_elem(map, key, value);
  212. assert(!r || errno == ENOENT);
  213. assert(!t == !!r);
  214. if (t) {
  215. ++n_matches;
  216. assert(t->n_bits == value[keysize]);
  217. for (j = 0; j < t->n_bits; ++j)
  218. assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
  219. (value[j / 8] & (1 << (7 - j % 8))));
  220. }
  221. }
  222. /* Remove the first half of the elements in the tlpm and the
  223. * corresponding nodes from the bpf-lpm. Then run the same
  224. * large number of random lookups in both and make sure they match.
  225. * Note: we need to count the number of nodes actually inserted
  226. * since there may have been duplicates.
  227. */
  228. for (i = 0, t = list; t; i++, t = t->next)
  229. ;
  230. for (j = 0; j < i / 2; ++j) {
  231. key->prefixlen = list->n_bits;
  232. memcpy(key->data, list->key, keysize);
  233. r = bpf_map_delete_elem(map, key);
  234. assert(!r);
  235. list = tlpm_delete(list, list->key, list->n_bits);
  236. assert(list);
  237. }
  238. for (i = 0; i < n_lookups; ++i) {
  239. for (j = 0; j < keysize; ++j)
  240. data[j] = rand() & 0xff;
  241. t = tlpm_match(list, data, 8 * keysize);
  242. key->prefixlen = 8 * keysize;
  243. memcpy(key->data, data, keysize);
  244. r = bpf_map_lookup_elem(map, key, value);
  245. assert(!r || errno == ENOENT);
  246. assert(!t == !!r);
  247. if (t) {
  248. ++n_matches_after_delete;
  249. assert(t->n_bits == value[keysize]);
  250. for (j = 0; j < t->n_bits; ++j)
  251. assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
  252. (value[j / 8] & (1 << (7 - j % 8))));
  253. }
  254. }
  255. close(map);
  256. tlpm_clear(list);
  257. /* With 255 random nodes in the map, we are pretty likely to match
  258. * something on every lookup. For statistics, use this:
  259. *
  260. * printf(" nodes: %zu\n"
  261. * " lookups: %zu\n"
  262. * " matches: %zu\n"
  263. * "matches(delete): %zu\n",
  264. * n_nodes, n_lookups, n_matches, n_matches_after_delete);
  265. */
  266. }
  267. /* Test the implementation with some 'real world' examples */
  268. static void test_lpm_ipaddr(void)
  269. {
  270. struct bpf_lpm_trie_key *key_ipv4;
  271. struct bpf_lpm_trie_key *key_ipv6;
  272. size_t key_size_ipv4;
  273. size_t key_size_ipv6;
  274. int map_fd_ipv4;
  275. int map_fd_ipv6;
  276. __u64 value;
  277. key_size_ipv4 = sizeof(*key_ipv4) + sizeof(__u32);
  278. key_size_ipv6 = sizeof(*key_ipv6) + sizeof(__u32) * 4;
  279. key_ipv4 = alloca(key_size_ipv4);
  280. key_ipv6 = alloca(key_size_ipv6);
  281. map_fd_ipv4 = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  282. key_size_ipv4, sizeof(value),
  283. 100, BPF_F_NO_PREALLOC);
  284. assert(map_fd_ipv4 >= 0);
  285. map_fd_ipv6 = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  286. key_size_ipv6, sizeof(value),
  287. 100, BPF_F_NO_PREALLOC);
  288. assert(map_fd_ipv6 >= 0);
  289. /* Fill data some IPv4 and IPv6 address ranges */
  290. value = 1;
  291. key_ipv4->prefixlen = 16;
  292. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  293. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  294. value = 2;
  295. key_ipv4->prefixlen = 24;
  296. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  297. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  298. value = 3;
  299. key_ipv4->prefixlen = 24;
  300. inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
  301. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  302. value = 5;
  303. key_ipv4->prefixlen = 24;
  304. inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
  305. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  306. value = 4;
  307. key_ipv4->prefixlen = 23;
  308. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  309. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  310. value = 0xdeadbeef;
  311. key_ipv6->prefixlen = 64;
  312. inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
  313. assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);
  314. /* Set tprefixlen to maximum for lookups */
  315. key_ipv4->prefixlen = 32;
  316. key_ipv6->prefixlen = 128;
  317. /* Test some lookups that should come back with a value */
  318. inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
  319. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
  320. assert(value == 3);
  321. inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
  322. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
  323. assert(value == 2);
  324. inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
  325. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
  326. assert(value == 0xdeadbeef);
  327. inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
  328. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
  329. assert(value == 0xdeadbeef);
  330. /* Test some lookups that should not match any entry */
  331. inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
  332. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
  333. errno == ENOENT);
  334. inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
  335. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
  336. errno == ENOENT);
  337. inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
  338. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
  339. errno == ENOENT);
  340. close(map_fd_ipv4);
  341. close(map_fd_ipv6);
  342. }
  343. static void test_lpm_delete(void)
  344. {
  345. struct bpf_lpm_trie_key *key;
  346. size_t key_size;
  347. int map_fd;
  348. __u64 value;
  349. key_size = sizeof(*key) + sizeof(__u32);
  350. key = alloca(key_size);
  351. map_fd = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  352. key_size, sizeof(value),
  353. 100, BPF_F_NO_PREALLOC);
  354. assert(map_fd >= 0);
  355. /* Add nodes:
  356. * 192.168.0.0/16 (1)
  357. * 192.168.0.0/24 (2)
  358. * 192.168.128.0/24 (3)
  359. * 192.168.1.0/24 (4)
  360. *
  361. * (1)
  362. * / \
  363. * (IM) (3)
  364. * / \
  365. * (2) (4)
  366. */
  367. value = 1;
  368. key->prefixlen = 16;
  369. inet_pton(AF_INET, "192.168.0.0", key->data);
  370. assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
  371. value = 2;
  372. key->prefixlen = 24;
  373. inet_pton(AF_INET, "192.168.0.0", key->data);
  374. assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
  375. value = 3;
  376. key->prefixlen = 24;
  377. inet_pton(AF_INET, "192.168.128.0", key->data);
  378. assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
  379. value = 4;
  380. key->prefixlen = 24;
  381. inet_pton(AF_INET, "192.168.1.0", key->data);
  382. assert(bpf_map_update_elem(map_fd, key, &value, 0) == 0);
  383. /* remove non-existent node */
  384. key->prefixlen = 32;
  385. inet_pton(AF_INET, "10.0.0.1", key->data);
  386. assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
  387. errno == ENOENT);
  388. key->prefixlen = 30; // unused prefix so far
  389. inet_pton(AF_INET, "192.255.0.0", key->data);
  390. assert(bpf_map_delete_elem(map_fd, key) == -1 &&
  391. errno == ENOENT);
  392. key->prefixlen = 16; // same prefix as the root node
  393. inet_pton(AF_INET, "192.255.0.0", key->data);
  394. assert(bpf_map_delete_elem(map_fd, key) == -1 &&
  395. errno == ENOENT);
  396. /* assert initial lookup */
  397. key->prefixlen = 32;
  398. inet_pton(AF_INET, "192.168.0.1", key->data);
  399. assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
  400. assert(value == 2);
  401. /* remove leaf node */
  402. key->prefixlen = 24;
  403. inet_pton(AF_INET, "192.168.0.0", key->data);
  404. assert(bpf_map_delete_elem(map_fd, key) == 0);
  405. key->prefixlen = 32;
  406. inet_pton(AF_INET, "192.168.0.1", key->data);
  407. assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
  408. assert(value == 1);
  409. /* remove leaf (and intermediary) node */
  410. key->prefixlen = 24;
  411. inet_pton(AF_INET, "192.168.1.0", key->data);
  412. assert(bpf_map_delete_elem(map_fd, key) == 0);
  413. key->prefixlen = 32;
  414. inet_pton(AF_INET, "192.168.1.1", key->data);
  415. assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
  416. assert(value == 1);
  417. /* remove root node */
  418. key->prefixlen = 16;
  419. inet_pton(AF_INET, "192.168.0.0", key->data);
  420. assert(bpf_map_delete_elem(map_fd, key) == 0);
  421. key->prefixlen = 32;
  422. inet_pton(AF_INET, "192.168.128.1", key->data);
  423. assert(bpf_map_lookup_elem(map_fd, key, &value) == 0);
  424. assert(value == 3);
  425. /* remove last node */
  426. key->prefixlen = 24;
  427. inet_pton(AF_INET, "192.168.128.0", key->data);
  428. assert(bpf_map_delete_elem(map_fd, key) == 0);
  429. key->prefixlen = 32;
  430. inet_pton(AF_INET, "192.168.128.1", key->data);
  431. assert(bpf_map_lookup_elem(map_fd, key, &value) == -1 &&
  432. errno == ENOENT);
  433. close(map_fd);
  434. }
  435. static void test_lpm_get_next_key(void)
  436. {
  437. struct bpf_lpm_trie_key *key_p, *next_key_p;
  438. size_t key_size;
  439. __u32 value = 0;
  440. int map_fd;
  441. key_size = sizeof(*key_p) + sizeof(__u32);
  442. key_p = alloca(key_size);
  443. next_key_p = alloca(key_size);
  444. map_fd = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE, key_size, sizeof(value),
  445. 100, BPF_F_NO_PREALLOC);
  446. assert(map_fd >= 0);
  447. /* empty tree. get_next_key should return ENOENT */
  448. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == -1 &&
  449. errno == ENOENT);
  450. /* get and verify the first key, get the second one should fail. */
  451. key_p->prefixlen = 16;
  452. inet_pton(AF_INET, "192.168.0.0", key_p->data);
  453. assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
  454. memset(key_p, 0, key_size);
  455. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  456. assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
  457. key_p->data[1] == 168);
  458. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
  459. errno == ENOENT);
  460. /* no exact matching key should get the first one in post order. */
  461. key_p->prefixlen = 8;
  462. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  463. assert(key_p->prefixlen == 16 && key_p->data[0] == 192 &&
  464. key_p->data[1] == 168);
  465. /* add one more element (total two) */
  466. key_p->prefixlen = 24;
  467. inet_pton(AF_INET, "192.168.128.0", key_p->data);
  468. assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
  469. memset(key_p, 0, key_size);
  470. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  471. assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
  472. key_p->data[1] == 168 && key_p->data[2] == 128);
  473. memset(next_key_p, 0, key_size);
  474. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  475. assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
  476. next_key_p->data[1] == 168);
  477. memcpy(key_p, next_key_p, key_size);
  478. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
  479. errno == ENOENT);
  480. /* Add one more element (total three) */
  481. key_p->prefixlen = 24;
  482. inet_pton(AF_INET, "192.168.0.0", key_p->data);
  483. assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
  484. memset(key_p, 0, key_size);
  485. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  486. assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
  487. key_p->data[1] == 168 && key_p->data[2] == 0);
  488. memset(next_key_p, 0, key_size);
  489. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  490. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  491. next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
  492. memcpy(key_p, next_key_p, key_size);
  493. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  494. assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
  495. next_key_p->data[1] == 168);
  496. memcpy(key_p, next_key_p, key_size);
  497. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
  498. errno == ENOENT);
  499. /* Add one more element (total four) */
  500. key_p->prefixlen = 24;
  501. inet_pton(AF_INET, "192.168.1.0", key_p->data);
  502. assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
  503. memset(key_p, 0, key_size);
  504. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  505. assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
  506. key_p->data[1] == 168 && key_p->data[2] == 0);
  507. memset(next_key_p, 0, key_size);
  508. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  509. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  510. next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
  511. memcpy(key_p, next_key_p, key_size);
  512. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  513. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  514. next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
  515. memcpy(key_p, next_key_p, key_size);
  516. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  517. assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
  518. next_key_p->data[1] == 168);
  519. memcpy(key_p, next_key_p, key_size);
  520. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
  521. errno == ENOENT);
  522. /* Add one more element (total five) */
  523. key_p->prefixlen = 28;
  524. inet_pton(AF_INET, "192.168.1.128", key_p->data);
  525. assert(bpf_map_update_elem(map_fd, key_p, &value, 0) == 0);
  526. memset(key_p, 0, key_size);
  527. assert(bpf_map_get_next_key(map_fd, NULL, key_p) == 0);
  528. assert(key_p->prefixlen == 24 && key_p->data[0] == 192 &&
  529. key_p->data[1] == 168 && key_p->data[2] == 0);
  530. memset(next_key_p, 0, key_size);
  531. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  532. assert(next_key_p->prefixlen == 28 && next_key_p->data[0] == 192 &&
  533. next_key_p->data[1] == 168 && next_key_p->data[2] == 1 &&
  534. next_key_p->data[3] == 128);
  535. memcpy(key_p, next_key_p, key_size);
  536. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  537. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  538. next_key_p->data[1] == 168 && next_key_p->data[2] == 1);
  539. memcpy(key_p, next_key_p, key_size);
  540. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  541. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  542. next_key_p->data[1] == 168 && next_key_p->data[2] == 128);
  543. memcpy(key_p, next_key_p, key_size);
  544. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  545. assert(next_key_p->prefixlen == 16 && next_key_p->data[0] == 192 &&
  546. next_key_p->data[1] == 168);
  547. memcpy(key_p, next_key_p, key_size);
  548. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == -1 &&
  549. errno == ENOENT);
  550. /* no exact matching key should return the first one in post order */
  551. key_p->prefixlen = 22;
  552. inet_pton(AF_INET, "192.168.1.0", key_p->data);
  553. assert(bpf_map_get_next_key(map_fd, key_p, next_key_p) == 0);
  554. assert(next_key_p->prefixlen == 24 && next_key_p->data[0] == 192 &&
  555. next_key_p->data[1] == 168 && next_key_p->data[2] == 0);
  556. close(map_fd);
  557. }
  558. #define MAX_TEST_KEYS 4
  559. struct lpm_mt_test_info {
  560. int cmd; /* 0: update, 1: delete, 2: lookup, 3: get_next_key */
  561. int iter;
  562. int map_fd;
  563. struct {
  564. __u32 prefixlen;
  565. __u32 data;
  566. } key[MAX_TEST_KEYS];
  567. };
  568. static void *lpm_test_command(void *arg)
  569. {
  570. int i, j, ret, iter, key_size;
  571. struct lpm_mt_test_info *info = arg;
  572. struct bpf_lpm_trie_key *key_p;
  573. key_size = sizeof(struct bpf_lpm_trie_key) + sizeof(__u32);
  574. key_p = alloca(key_size);
  575. for (iter = 0; iter < info->iter; iter++)
  576. for (i = 0; i < MAX_TEST_KEYS; i++) {
  577. /* first half of iterations in forward order,
  578. * and second half in backward order.
  579. */
  580. j = (iter < (info->iter / 2)) ? i : MAX_TEST_KEYS - i - 1;
  581. key_p->prefixlen = info->key[j].prefixlen;
  582. memcpy(key_p->data, &info->key[j].data, sizeof(__u32));
  583. if (info->cmd == 0) {
  584. __u32 value = j;
  585. /* update must succeed */
  586. assert(bpf_map_update_elem(info->map_fd, key_p, &value, 0) == 0);
  587. } else if (info->cmd == 1) {
  588. ret = bpf_map_delete_elem(info->map_fd, key_p);
  589. assert(ret == 0 || errno == ENOENT);
  590. } else if (info->cmd == 2) {
  591. __u32 value;
  592. ret = bpf_map_lookup_elem(info->map_fd, key_p, &value);
  593. assert(ret == 0 || errno == ENOENT);
  594. } else {
  595. struct bpf_lpm_trie_key *next_key_p = alloca(key_size);
  596. ret = bpf_map_get_next_key(info->map_fd, key_p, next_key_p);
  597. assert(ret == 0 || errno == ENOENT || errno == ENOMEM);
  598. }
  599. }
  600. // Pass successful exit info back to the main thread
  601. pthread_exit((void *)info);
  602. }
  603. static void setup_lpm_mt_test_info(struct lpm_mt_test_info *info, int map_fd)
  604. {
  605. info->iter = 2000;
  606. info->map_fd = map_fd;
  607. info->key[0].prefixlen = 16;
  608. inet_pton(AF_INET, "192.168.0.0", &info->key[0].data);
  609. info->key[1].prefixlen = 24;
  610. inet_pton(AF_INET, "192.168.0.0", &info->key[1].data);
  611. info->key[2].prefixlen = 24;
  612. inet_pton(AF_INET, "192.168.128.0", &info->key[2].data);
  613. info->key[3].prefixlen = 24;
  614. inet_pton(AF_INET, "192.168.1.0", &info->key[3].data);
  615. }
  616. static void test_lpm_multi_thread(void)
  617. {
  618. struct lpm_mt_test_info info[4];
  619. size_t key_size, value_size;
  620. pthread_t thread_id[4];
  621. int i, map_fd;
  622. void *ret;
  623. /* create a trie */
  624. value_size = sizeof(__u32);
  625. key_size = sizeof(struct bpf_lpm_trie_key) + value_size;
  626. map_fd = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE, key_size, value_size,
  627. 100, BPF_F_NO_PREALLOC);
  628. /* create 4 threads to test update, delete, lookup and get_next_key */
  629. setup_lpm_mt_test_info(&info[0], map_fd);
  630. for (i = 0; i < 4; i++) {
  631. if (i != 0)
  632. memcpy(&info[i], &info[0], sizeof(info[i]));
  633. info[i].cmd = i;
  634. assert(pthread_create(&thread_id[i], NULL, &lpm_test_command, &info[i]) == 0);
  635. }
  636. for (i = 0; i < 4; i++)
  637. assert(pthread_join(thread_id[i], &ret) == 0 && ret == (void *)&info[i]);
  638. close(map_fd);
  639. }
  640. int main(void)
  641. {
  642. int i;
  643. /* we want predictable, pseudo random tests */
  644. srand(0xf00ba1);
  645. test_lpm_basic();
  646. test_lpm_order();
  647. /* Test with 8, 16, 24, 32, ... 128 bit prefix length */
  648. for (i = 1; i <= 16; ++i)
  649. test_lpm_map(i);
  650. test_lpm_ipaddr();
  651. test_lpm_delete();
  652. test_lpm_get_next_key();
  653. test_lpm_multi_thread();
  654. printf("test_lpm: OK\n");
  655. return 0;
  656. }