test_lru_map.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright (c) 2016 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #define _GNU_SOURCE
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <errno.h>
  12. #include <string.h>
  13. #include <assert.h>
  14. #include <sched.h>
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <sys/wait.h>
  18. #include <sys/resource.h>
  19. #include <bpf/bpf.h>
  20. #include "bpf_util.h"
  21. #define LOCAL_FREE_TARGET (128)
  22. #define PERCPU_FREE_TARGET (16)
  23. static int nr_cpus;
  24. static int create_map(int map_type, int map_flags, unsigned int size)
  25. {
  26. int map_fd;
  27. map_fd = bpf_create_map(map_type, sizeof(unsigned long long),
  28. sizeof(unsigned long long), size, map_flags);
  29. if (map_fd == -1)
  30. perror("bpf_create_map");
  31. return map_fd;
  32. }
  33. static int map_subset(int map0, int map1)
  34. {
  35. unsigned long long next_key = 0;
  36. unsigned long long value0[nr_cpus], value1[nr_cpus];
  37. int ret;
  38. while (!bpf_map_get_next_key(map1, &next_key, &next_key)) {
  39. assert(!bpf_map_lookup_elem(map1, &next_key, value1));
  40. ret = bpf_map_lookup_elem(map0, &next_key, value0);
  41. if (ret) {
  42. printf("key:%llu not found from map. %s(%d)\n",
  43. next_key, strerror(errno), errno);
  44. return 0;
  45. }
  46. if (value0[0] != value1[0]) {
  47. printf("key:%llu value0:%llu != value1:%llu\n",
  48. next_key, value0[0], value1[0]);
  49. return 0;
  50. }
  51. }
  52. return 1;
  53. }
  54. static int map_equal(int lru_map, int expected)
  55. {
  56. return map_subset(lru_map, expected) && map_subset(expected, lru_map);
  57. }
  58. static int sched_next_online(int pid, int *next_to_try)
  59. {
  60. cpu_set_t cpuset;
  61. int next = *next_to_try;
  62. int ret = -1;
  63. while (next < nr_cpus) {
  64. CPU_ZERO(&cpuset);
  65. CPU_SET(next++, &cpuset);
  66. if (!sched_setaffinity(pid, sizeof(cpuset), &cpuset)) {
  67. ret = 0;
  68. break;
  69. }
  70. }
  71. *next_to_try = next;
  72. return ret;
  73. }
  74. /* Size of the LRU amp is 2
  75. * Add key=1 (+1 key)
  76. * Add key=2 (+1 key)
  77. * Lookup Key=1
  78. * Add Key=3
  79. * => Key=2 will be removed by LRU
  80. * Iterate map. Only found key=1 and key=3
  81. */
  82. static void test_lru_sanity0(int map_type, int map_flags)
  83. {
  84. unsigned long long key, value[nr_cpus];
  85. int lru_map_fd, expected_map_fd;
  86. int next_cpu = 0;
  87. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  88. map_flags);
  89. assert(sched_next_online(0, &next_cpu) != -1);
  90. if (map_flags & BPF_F_NO_COMMON_LRU)
  91. lru_map_fd = create_map(map_type, map_flags, 2 * nr_cpus);
  92. else
  93. lru_map_fd = create_map(map_type, map_flags, 2);
  94. assert(lru_map_fd != -1);
  95. expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0, 2);
  96. assert(expected_map_fd != -1);
  97. value[0] = 1234;
  98. /* insert key=1 element */
  99. key = 1;
  100. assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
  101. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  102. BPF_NOEXIST));
  103. /* BPF_NOEXIST means: add new element if it doesn't exist */
  104. assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST) == -1
  105. /* key=1 already exists */
  106. && errno == EEXIST);
  107. assert(bpf_map_update_elem(lru_map_fd, &key, value, -1) == -1 &&
  108. errno == EINVAL);
  109. /* insert key=2 element */
  110. /* check that key=2 is not found */
  111. key = 2;
  112. assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
  113. errno == ENOENT);
  114. /* BPF_EXIST means: update existing element */
  115. assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_EXIST) == -1 &&
  116. /* key=2 is not there */
  117. errno == ENOENT);
  118. assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
  119. /* insert key=3 element */
  120. /* check that key=3 is not found */
  121. key = 3;
  122. assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1 &&
  123. errno == ENOENT);
  124. /* check that key=1 can be found and mark the ref bit to
  125. * stop LRU from removing key=1
  126. */
  127. key = 1;
  128. assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
  129. assert(value[0] == 1234);
  130. key = 3;
  131. assert(!bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
  132. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  133. BPF_NOEXIST));
  134. /* key=2 has been removed from the LRU */
  135. key = 2;
  136. assert(bpf_map_lookup_elem(lru_map_fd, &key, value) == -1);
  137. assert(map_equal(lru_map_fd, expected_map_fd));
  138. close(expected_map_fd);
  139. close(lru_map_fd);
  140. printf("Pass\n");
  141. }
  142. /* Size of the LRU map is 1.5*tgt_free
  143. * Insert 1 to tgt_free (+tgt_free keys)
  144. * Lookup 1 to tgt_free/2
  145. * Insert 1+tgt_free to 2*tgt_free (+tgt_free keys)
  146. * => 1+tgt_free/2 to LOCALFREE_TARGET will be removed by LRU
  147. */
  148. static void test_lru_sanity1(int map_type, int map_flags, unsigned int tgt_free)
  149. {
  150. unsigned long long key, end_key, value[nr_cpus];
  151. int lru_map_fd, expected_map_fd;
  152. unsigned int batch_size;
  153. unsigned int map_size;
  154. int next_cpu = 0;
  155. if (map_flags & BPF_F_NO_COMMON_LRU)
  156. /* Ther percpu lru list (i.e each cpu has its own LRU
  157. * list) does not have a local free list. Hence,
  158. * it will only free old nodes till there is no free
  159. * from the LRU list. Hence, this test does not apply
  160. * to BPF_F_NO_COMMON_LRU
  161. */
  162. return;
  163. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  164. map_flags);
  165. assert(sched_next_online(0, &next_cpu) != -1);
  166. batch_size = tgt_free / 2;
  167. assert(batch_size * 2 == tgt_free);
  168. map_size = tgt_free + batch_size;
  169. lru_map_fd = create_map(map_type, map_flags, map_size);
  170. assert(lru_map_fd != -1);
  171. expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0, map_size);
  172. assert(expected_map_fd != -1);
  173. value[0] = 1234;
  174. /* Insert 1 to tgt_free (+tgt_free keys) */
  175. end_key = 1 + tgt_free;
  176. for (key = 1; key < end_key; key++)
  177. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  178. BPF_NOEXIST));
  179. /* Lookup 1 to tgt_free/2 */
  180. end_key = 1 + batch_size;
  181. for (key = 1; key < end_key; key++) {
  182. assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
  183. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  184. BPF_NOEXIST));
  185. }
  186. /* Insert 1+tgt_free to 2*tgt_free
  187. * => 1+tgt_free/2 to LOCALFREE_TARGET will be
  188. * removed by LRU
  189. */
  190. key = 1 + tgt_free;
  191. end_key = key + tgt_free;
  192. for (; key < end_key; key++) {
  193. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  194. BPF_NOEXIST));
  195. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  196. BPF_NOEXIST));
  197. }
  198. assert(map_equal(lru_map_fd, expected_map_fd));
  199. close(expected_map_fd);
  200. close(lru_map_fd);
  201. printf("Pass\n");
  202. }
  203. /* Size of the LRU map 1.5 * tgt_free
  204. * Insert 1 to tgt_free (+tgt_free keys)
  205. * Update 1 to tgt_free/2
  206. * => The original 1 to tgt_free/2 will be removed due to
  207. * the LRU shrink process
  208. * Re-insert 1 to tgt_free/2 again and do a lookup immeidately
  209. * Insert 1+tgt_free to tgt_free*3/2
  210. * Insert 1+tgt_free*3/2 to tgt_free*5/2
  211. * => Key 1+tgt_free to tgt_free*3/2
  212. * will be removed from LRU because it has never
  213. * been lookup and ref bit is not set
  214. */
  215. static void test_lru_sanity2(int map_type, int map_flags, unsigned int tgt_free)
  216. {
  217. unsigned long long key, value[nr_cpus];
  218. unsigned long long end_key;
  219. int lru_map_fd, expected_map_fd;
  220. unsigned int batch_size;
  221. unsigned int map_size;
  222. int next_cpu = 0;
  223. if (map_flags & BPF_F_NO_COMMON_LRU)
  224. /* Ther percpu lru list (i.e each cpu has its own LRU
  225. * list) does not have a local free list. Hence,
  226. * it will only free old nodes till there is no free
  227. * from the LRU list. Hence, this test does not apply
  228. * to BPF_F_NO_COMMON_LRU
  229. */
  230. return;
  231. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  232. map_flags);
  233. assert(sched_next_online(0, &next_cpu) != -1);
  234. batch_size = tgt_free / 2;
  235. assert(batch_size * 2 == tgt_free);
  236. map_size = tgt_free + batch_size;
  237. if (map_flags & BPF_F_NO_COMMON_LRU)
  238. lru_map_fd = create_map(map_type, map_flags,
  239. map_size * nr_cpus);
  240. else
  241. lru_map_fd = create_map(map_type, map_flags, map_size);
  242. assert(lru_map_fd != -1);
  243. expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0, map_size);
  244. assert(expected_map_fd != -1);
  245. value[0] = 1234;
  246. /* Insert 1 to tgt_free (+tgt_free keys) */
  247. end_key = 1 + tgt_free;
  248. for (key = 1; key < end_key; key++)
  249. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  250. BPF_NOEXIST));
  251. /* Any bpf_map_update_elem will require to acquire a new node
  252. * from LRU first.
  253. *
  254. * The local list is running out of free nodes.
  255. * It gets from the global LRU list which tries to
  256. * shrink the inactive list to get tgt_free
  257. * number of free nodes.
  258. *
  259. * Hence, the oldest key 1 to tgt_free/2
  260. * are removed from the LRU list.
  261. */
  262. key = 1;
  263. if (map_type == BPF_MAP_TYPE_LRU_PERCPU_HASH) {
  264. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  265. BPF_NOEXIST));
  266. assert(!bpf_map_delete_elem(lru_map_fd, &key));
  267. } else {
  268. assert(bpf_map_update_elem(lru_map_fd, &key, value,
  269. BPF_EXIST));
  270. }
  271. /* Re-insert 1 to tgt_free/2 again and do a lookup
  272. * immeidately.
  273. */
  274. end_key = 1 + batch_size;
  275. value[0] = 4321;
  276. for (key = 1; key < end_key; key++) {
  277. assert(bpf_map_lookup_elem(lru_map_fd, &key, value));
  278. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  279. BPF_NOEXIST));
  280. assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
  281. assert(value[0] == 4321);
  282. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  283. BPF_NOEXIST));
  284. }
  285. value[0] = 1234;
  286. /* Insert 1+tgt_free to tgt_free*3/2 */
  287. end_key = 1 + tgt_free + batch_size;
  288. for (key = 1 + tgt_free; key < end_key; key++)
  289. /* These newly added but not referenced keys will be
  290. * gone during the next LRU shrink.
  291. */
  292. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  293. BPF_NOEXIST));
  294. /* Insert 1+tgt_free*3/2 to tgt_free*5/2 */
  295. end_key = key + tgt_free;
  296. for (; key < end_key; key++) {
  297. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  298. BPF_NOEXIST));
  299. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  300. BPF_NOEXIST));
  301. }
  302. assert(map_equal(lru_map_fd, expected_map_fd));
  303. close(expected_map_fd);
  304. close(lru_map_fd);
  305. printf("Pass\n");
  306. }
  307. /* Size of the LRU map is 2*tgt_free
  308. * It is to test the active/inactive list rotation
  309. * Insert 1 to 2*tgt_free (+2*tgt_free keys)
  310. * Lookup key 1 to tgt_free*3/2
  311. * Add 1+2*tgt_free to tgt_free*5/2 (+tgt_free/2 keys)
  312. * => key 1+tgt_free*3/2 to 2*tgt_free are removed from LRU
  313. */
  314. static void test_lru_sanity3(int map_type, int map_flags, unsigned int tgt_free)
  315. {
  316. unsigned long long key, end_key, value[nr_cpus];
  317. int lru_map_fd, expected_map_fd;
  318. unsigned int batch_size;
  319. unsigned int map_size;
  320. int next_cpu = 0;
  321. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  322. map_flags);
  323. assert(sched_next_online(0, &next_cpu) != -1);
  324. batch_size = tgt_free / 2;
  325. assert(batch_size * 2 == tgt_free);
  326. map_size = tgt_free * 2;
  327. if (map_flags & BPF_F_NO_COMMON_LRU)
  328. lru_map_fd = create_map(map_type, map_flags,
  329. map_size * nr_cpus);
  330. else
  331. lru_map_fd = create_map(map_type, map_flags, map_size);
  332. assert(lru_map_fd != -1);
  333. expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0, map_size);
  334. assert(expected_map_fd != -1);
  335. value[0] = 1234;
  336. /* Insert 1 to 2*tgt_free (+2*tgt_free keys) */
  337. end_key = 1 + (2 * tgt_free);
  338. for (key = 1; key < end_key; key++)
  339. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  340. BPF_NOEXIST));
  341. /* Lookup key 1 to tgt_free*3/2 */
  342. end_key = tgt_free + batch_size;
  343. for (key = 1; key < end_key; key++) {
  344. assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
  345. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  346. BPF_NOEXIST));
  347. }
  348. /* Add 1+2*tgt_free to tgt_free*5/2
  349. * (+tgt_free/2 keys)
  350. */
  351. key = 2 * tgt_free + 1;
  352. end_key = key + batch_size;
  353. for (; key < end_key; key++) {
  354. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  355. BPF_NOEXIST));
  356. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  357. BPF_NOEXIST));
  358. }
  359. assert(map_equal(lru_map_fd, expected_map_fd));
  360. close(expected_map_fd);
  361. close(lru_map_fd);
  362. printf("Pass\n");
  363. }
  364. /* Test deletion */
  365. static void test_lru_sanity4(int map_type, int map_flags, unsigned int tgt_free)
  366. {
  367. int lru_map_fd, expected_map_fd;
  368. unsigned long long key, value[nr_cpus];
  369. unsigned long long end_key;
  370. int next_cpu = 0;
  371. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  372. map_flags);
  373. assert(sched_next_online(0, &next_cpu) != -1);
  374. if (map_flags & BPF_F_NO_COMMON_LRU)
  375. lru_map_fd = create_map(map_type, map_flags,
  376. 3 * tgt_free * nr_cpus);
  377. else
  378. lru_map_fd = create_map(map_type, map_flags, 3 * tgt_free);
  379. assert(lru_map_fd != -1);
  380. expected_map_fd = create_map(BPF_MAP_TYPE_HASH, 0,
  381. 3 * tgt_free);
  382. assert(expected_map_fd != -1);
  383. value[0] = 1234;
  384. for (key = 1; key <= 2 * tgt_free; key++)
  385. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  386. BPF_NOEXIST));
  387. key = 1;
  388. assert(bpf_map_update_elem(lru_map_fd, &key, value, BPF_NOEXIST));
  389. for (key = 1; key <= tgt_free; key++) {
  390. assert(!bpf_map_lookup_elem(lru_map_fd, &key, value));
  391. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  392. BPF_NOEXIST));
  393. }
  394. for (; key <= 2 * tgt_free; key++) {
  395. assert(!bpf_map_delete_elem(lru_map_fd, &key));
  396. assert(bpf_map_delete_elem(lru_map_fd, &key));
  397. }
  398. end_key = key + 2 * tgt_free;
  399. for (; key < end_key; key++) {
  400. assert(!bpf_map_update_elem(lru_map_fd, &key, value,
  401. BPF_NOEXIST));
  402. assert(!bpf_map_update_elem(expected_map_fd, &key, value,
  403. BPF_NOEXIST));
  404. }
  405. assert(map_equal(lru_map_fd, expected_map_fd));
  406. close(expected_map_fd);
  407. close(lru_map_fd);
  408. printf("Pass\n");
  409. }
  410. static void do_test_lru_sanity5(unsigned long long last_key, int map_fd)
  411. {
  412. unsigned long long key, value[nr_cpus];
  413. /* Ensure the last key inserted by previous CPU can be found */
  414. assert(!bpf_map_lookup_elem(map_fd, &last_key, value));
  415. value[0] = 1234;
  416. key = last_key + 1;
  417. assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
  418. assert(!bpf_map_lookup_elem(map_fd, &key, value));
  419. /* Cannot find the last key because it was removed by LRU */
  420. assert(bpf_map_lookup_elem(map_fd, &last_key, value));
  421. }
  422. /* Test map with only one element */
  423. static void test_lru_sanity5(int map_type, int map_flags)
  424. {
  425. unsigned long long key, value[nr_cpus];
  426. int next_cpu = 0;
  427. int map_fd;
  428. if (map_flags & BPF_F_NO_COMMON_LRU)
  429. return;
  430. printf("%s (map_type:%d map_flags:0x%X): ", __func__, map_type,
  431. map_flags);
  432. map_fd = create_map(map_type, map_flags, 1);
  433. assert(map_fd != -1);
  434. value[0] = 1234;
  435. key = 0;
  436. assert(!bpf_map_update_elem(map_fd, &key, value, BPF_NOEXIST));
  437. while (sched_next_online(0, &next_cpu) != -1) {
  438. pid_t pid;
  439. pid = fork();
  440. if (pid == 0) {
  441. do_test_lru_sanity5(key, map_fd);
  442. exit(0);
  443. } else if (pid == -1) {
  444. printf("couldn't spawn process to test key:%llu\n",
  445. key);
  446. exit(1);
  447. } else {
  448. int status;
  449. assert(waitpid(pid, &status, 0) == pid);
  450. assert(status == 0);
  451. key++;
  452. }
  453. }
  454. close(map_fd);
  455. /* At least one key should be tested */
  456. assert(key > 0);
  457. printf("Pass\n");
  458. }
  459. int main(int argc, char **argv)
  460. {
  461. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  462. int map_types[] = {BPF_MAP_TYPE_LRU_HASH,
  463. BPF_MAP_TYPE_LRU_PERCPU_HASH};
  464. int map_flags[] = {0, BPF_F_NO_COMMON_LRU};
  465. int t, f;
  466. setbuf(stdout, NULL);
  467. assert(!setrlimit(RLIMIT_MEMLOCK, &r));
  468. nr_cpus = bpf_num_possible_cpus();
  469. assert(nr_cpus != -1);
  470. printf("nr_cpus:%d\n\n", nr_cpus);
  471. for (f = 0; f < sizeof(map_flags) / sizeof(*map_flags); f++) {
  472. unsigned int tgt_free = (map_flags[f] & BPF_F_NO_COMMON_LRU) ?
  473. PERCPU_FREE_TARGET : LOCAL_FREE_TARGET;
  474. for (t = 0; t < sizeof(map_types) / sizeof(*map_types); t++) {
  475. test_lru_sanity0(map_types[t], map_flags[f]);
  476. test_lru_sanity1(map_types[t], map_flags[f], tgt_free);
  477. test_lru_sanity2(map_types[t], map_flags[f], tgt_free);
  478. test_lru_sanity3(map_types[t], map_flags[f], tgt_free);
  479. test_lru_sanity4(map_types[t], map_flags[f], tgt_free);
  480. test_lru_sanity5(map_types[t], map_flags[f]);
  481. printf("\n");
  482. }
  483. }
  484. return 0;
  485. }