test_lpm_map.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Randomized tests for eBPF longest-prefix-match maps
  3. *
  4. * This program runs randomized tests against the lpm-bpf-map. It implements a
  5. * "Trivial Longest Prefix Match" (tlpm) based on simple, linear, singly linked
  6. * lists. The implementation should be pretty straightforward.
  7. *
  8. * Based on tlpm, this inserts randomized data into bpf-lpm-maps and verifies
  9. * the trie-based bpf-map implementation behaves the same way as tlpm.
  10. */
  11. #include <assert.h>
  12. #include <errno.h>
  13. #include <inttypes.h>
  14. #include <linux/bpf.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <time.h>
  19. #include <unistd.h>
  20. #include <arpa/inet.h>
  21. #include <sys/time.h>
  22. #include <sys/resource.h>
  23. #include <bpf/bpf.h>
  24. #include "bpf_util.h"
  25. struct tlpm_node {
  26. struct tlpm_node *next;
  27. size_t n_bits;
  28. uint8_t key[];
  29. };
  30. static struct tlpm_node *tlpm_add(struct tlpm_node *list,
  31. const uint8_t *key,
  32. size_t n_bits)
  33. {
  34. struct tlpm_node *node;
  35. size_t n;
  36. /* add new entry with @key/@n_bits to @list and return new head */
  37. n = (n_bits + 7) / 8;
  38. node = malloc(sizeof(*node) + n);
  39. assert(node);
  40. node->next = list;
  41. node->n_bits = n_bits;
  42. memcpy(node->key, key, n);
  43. return node;
  44. }
  45. static void tlpm_clear(struct tlpm_node *list)
  46. {
  47. struct tlpm_node *node;
  48. /* free all entries in @list */
  49. while ((node = list)) {
  50. list = list->next;
  51. free(node);
  52. }
  53. }
  54. static struct tlpm_node *tlpm_match(struct tlpm_node *list,
  55. const uint8_t *key,
  56. size_t n_bits)
  57. {
  58. struct tlpm_node *best = NULL;
  59. size_t i;
  60. /* Perform longest prefix-match on @key/@n_bits. That is, iterate all
  61. * entries and match each prefix against @key. Remember the "best"
  62. * entry we find (i.e., the longest prefix that matches) and return it
  63. * to the caller when done.
  64. */
  65. for ( ; list; list = list->next) {
  66. for (i = 0; i < n_bits && i < list->n_bits; ++i) {
  67. if ((key[i / 8] & (1 << (7 - i % 8))) !=
  68. (list->key[i / 8] & (1 << (7 - i % 8))))
  69. break;
  70. }
  71. if (i >= list->n_bits) {
  72. if (!best || i > best->n_bits)
  73. best = list;
  74. }
  75. }
  76. return best;
  77. }
  78. static void test_lpm_basic(void)
  79. {
  80. struct tlpm_node *list = NULL, *t1, *t2;
  81. /* very basic, static tests to verify tlpm works as expected */
  82. assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  83. t1 = list = tlpm_add(list, (uint8_t[]){ 0xff }, 8);
  84. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  85. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
  86. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0x00 }, 16));
  87. assert(!tlpm_match(list, (uint8_t[]){ 0x7f }, 8));
  88. assert(!tlpm_match(list, (uint8_t[]){ 0xfe }, 8));
  89. assert(!tlpm_match(list, (uint8_t[]){ 0xff }, 7));
  90. t2 = list = tlpm_add(list, (uint8_t[]){ 0xff, 0xff }, 16);
  91. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff }, 8));
  92. assert(t2 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 16));
  93. assert(t1 == tlpm_match(list, (uint8_t[]){ 0xff, 0xff }, 15));
  94. assert(!tlpm_match(list, (uint8_t[]){ 0x7f, 0xff }, 16));
  95. tlpm_clear(list);
  96. }
  97. static void test_lpm_order(void)
  98. {
  99. struct tlpm_node *t1, *t2, *l1 = NULL, *l2 = NULL;
  100. size_t i, j;
  101. /* Verify the tlpm implementation works correctly regardless of the
  102. * order of entries. Insert a random set of entries into @l1, and copy
  103. * the same data in reverse order into @l2. Then verify a lookup of
  104. * random keys will yield the same result in both sets.
  105. */
  106. for (i = 0; i < (1 << 12); ++i)
  107. l1 = tlpm_add(l1, (uint8_t[]){
  108. rand() % 0xff,
  109. rand() % 0xff,
  110. }, rand() % 16 + 1);
  111. for (t1 = l1; t1; t1 = t1->next)
  112. l2 = tlpm_add(l2, t1->key, t1->n_bits);
  113. for (i = 0; i < (1 << 8); ++i) {
  114. uint8_t key[] = { rand() % 0xff, rand() % 0xff };
  115. t1 = tlpm_match(l1, key, 16);
  116. t2 = tlpm_match(l2, key, 16);
  117. assert(!t1 == !t2);
  118. if (t1) {
  119. assert(t1->n_bits == t2->n_bits);
  120. for (j = 0; j < t1->n_bits; ++j)
  121. assert((t1->key[j / 8] & (1 << (7 - j % 8))) ==
  122. (t2->key[j / 8] & (1 << (7 - j % 8))));
  123. }
  124. }
  125. tlpm_clear(l1);
  126. tlpm_clear(l2);
  127. }
  128. static void test_lpm_map(int keysize)
  129. {
  130. size_t i, j, n_matches, n_nodes, n_lookups;
  131. struct tlpm_node *t, *list = NULL;
  132. struct bpf_lpm_trie_key *key;
  133. uint8_t *data, *value;
  134. int r, map;
  135. /* Compare behavior of tlpm vs. bpf-lpm. Create a randomized set of
  136. * prefixes and insert it into both tlpm and bpf-lpm. Then run some
  137. * randomized lookups and verify both maps return the same result.
  138. */
  139. n_matches = 0;
  140. n_nodes = 1 << 8;
  141. n_lookups = 1 << 16;
  142. data = alloca(keysize);
  143. memset(data, 0, keysize);
  144. value = alloca(keysize + 1);
  145. memset(value, 0, keysize + 1);
  146. key = alloca(sizeof(*key) + keysize);
  147. memset(key, 0, sizeof(*key) + keysize);
  148. map = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  149. sizeof(*key) + keysize,
  150. keysize + 1,
  151. 4096,
  152. BPF_F_NO_PREALLOC);
  153. assert(map >= 0);
  154. for (i = 0; i < n_nodes; ++i) {
  155. for (j = 0; j < keysize; ++j)
  156. value[j] = rand() & 0xff;
  157. value[keysize] = rand() % (8 * keysize + 1);
  158. list = tlpm_add(list, value, value[keysize]);
  159. key->prefixlen = value[keysize];
  160. memcpy(key->data, value, keysize);
  161. r = bpf_map_update_elem(map, key, value, 0);
  162. assert(!r);
  163. }
  164. for (i = 0; i < n_lookups; ++i) {
  165. for (j = 0; j < keysize; ++j)
  166. data[j] = rand() & 0xff;
  167. t = tlpm_match(list, data, 8 * keysize);
  168. key->prefixlen = 8 * keysize;
  169. memcpy(key->data, data, keysize);
  170. r = bpf_map_lookup_elem(map, key, value);
  171. assert(!r || errno == ENOENT);
  172. assert(!t == !!r);
  173. if (t) {
  174. ++n_matches;
  175. assert(t->n_bits == value[keysize]);
  176. for (j = 0; j < t->n_bits; ++j)
  177. assert((t->key[j / 8] & (1 << (7 - j % 8))) ==
  178. (value[j / 8] & (1 << (7 - j % 8))));
  179. }
  180. }
  181. close(map);
  182. tlpm_clear(list);
  183. /* With 255 random nodes in the map, we are pretty likely to match
  184. * something on every lookup. For statistics, use this:
  185. *
  186. * printf(" nodes: %zu\n"
  187. * "lookups: %zu\n"
  188. * "matches: %zu\n", n_nodes, n_lookups, n_matches);
  189. */
  190. }
  191. /* Test the implementation with some 'real world' examples */
  192. static void test_lpm_ipaddr(void)
  193. {
  194. struct bpf_lpm_trie_key *key_ipv4;
  195. struct bpf_lpm_trie_key *key_ipv6;
  196. size_t key_size_ipv4;
  197. size_t key_size_ipv6;
  198. int map_fd_ipv4;
  199. int map_fd_ipv6;
  200. __u64 value;
  201. key_size_ipv4 = sizeof(*key_ipv4) + sizeof(__u32);
  202. key_size_ipv6 = sizeof(*key_ipv6) + sizeof(__u32) * 4;
  203. key_ipv4 = alloca(key_size_ipv4);
  204. key_ipv6 = alloca(key_size_ipv6);
  205. map_fd_ipv4 = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  206. key_size_ipv4, sizeof(value),
  207. 100, BPF_F_NO_PREALLOC);
  208. assert(map_fd_ipv4 >= 0);
  209. map_fd_ipv6 = bpf_create_map(BPF_MAP_TYPE_LPM_TRIE,
  210. key_size_ipv6, sizeof(value),
  211. 100, BPF_F_NO_PREALLOC);
  212. assert(map_fd_ipv6 >= 0);
  213. /* Fill data some IPv4 and IPv6 address ranges */
  214. value = 1;
  215. key_ipv4->prefixlen = 16;
  216. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  217. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  218. value = 2;
  219. key_ipv4->prefixlen = 24;
  220. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  221. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  222. value = 3;
  223. key_ipv4->prefixlen = 24;
  224. inet_pton(AF_INET, "192.168.128.0", key_ipv4->data);
  225. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  226. value = 5;
  227. key_ipv4->prefixlen = 24;
  228. inet_pton(AF_INET, "192.168.1.0", key_ipv4->data);
  229. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  230. value = 4;
  231. key_ipv4->prefixlen = 23;
  232. inet_pton(AF_INET, "192.168.0.0", key_ipv4->data);
  233. assert(bpf_map_update_elem(map_fd_ipv4, key_ipv4, &value, 0) == 0);
  234. value = 0xdeadbeef;
  235. key_ipv6->prefixlen = 64;
  236. inet_pton(AF_INET6, "2a00:1450:4001:814::200e", key_ipv6->data);
  237. assert(bpf_map_update_elem(map_fd_ipv6, key_ipv6, &value, 0) == 0);
  238. /* Set tprefixlen to maximum for lookups */
  239. key_ipv4->prefixlen = 32;
  240. key_ipv6->prefixlen = 128;
  241. /* Test some lookups that should come back with a value */
  242. inet_pton(AF_INET, "192.168.128.23", key_ipv4->data);
  243. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
  244. assert(value == 3);
  245. inet_pton(AF_INET, "192.168.0.1", key_ipv4->data);
  246. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == 0);
  247. assert(value == 2);
  248. inet_pton(AF_INET6, "2a00:1450:4001:814::", key_ipv6->data);
  249. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
  250. assert(value == 0xdeadbeef);
  251. inet_pton(AF_INET6, "2a00:1450:4001:814::1", key_ipv6->data);
  252. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == 0);
  253. assert(value == 0xdeadbeef);
  254. /* Test some lookups that should not match any entry */
  255. inet_pton(AF_INET, "10.0.0.1", key_ipv4->data);
  256. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
  257. errno == ENOENT);
  258. inet_pton(AF_INET, "11.11.11.11", key_ipv4->data);
  259. assert(bpf_map_lookup_elem(map_fd_ipv4, key_ipv4, &value) == -1 &&
  260. errno == ENOENT);
  261. inet_pton(AF_INET6, "2a00:ffff::", key_ipv6->data);
  262. assert(bpf_map_lookup_elem(map_fd_ipv6, key_ipv6, &value) == -1 &&
  263. errno == ENOENT);
  264. close(map_fd_ipv4);
  265. close(map_fd_ipv6);
  266. }
  267. int main(void)
  268. {
  269. struct rlimit limit = { RLIM_INFINITY, RLIM_INFINITY };
  270. int i, ret;
  271. /* we want predictable, pseudo random tests */
  272. srand(0xf00ba1);
  273. /* allow unlimited locked memory */
  274. ret = setrlimit(RLIMIT_MEMLOCK, &limit);
  275. if (ret < 0)
  276. perror("Unable to lift memlock rlimit");
  277. test_lpm_basic();
  278. test_lpm_order();
  279. /* Test with 8, 16, 24, 32, ... 128 bit prefix length */
  280. for (i = 1; i <= 16; ++i)
  281. test_lpm_map(i);
  282. test_lpm_ipaddr();
  283. printf("test_lpm: OK\n");
  284. return 0;
  285. }