test_lpm_map.c 9.3 KB

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