test_lru_map.c 16 KB

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