test_lru_map.c 15 KB

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