test_lpm_map.c 23 KB

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