test_progs.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __TEST_PROGS_H
  3. #define __TEST_PROGS_H
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <string.h>
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <time.h>
  12. #include <signal.h>
  13. #include <linux/types.h>
  14. typedef __u16 __sum16;
  15. #include <arpa/inet.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_packet.h>
  18. #include <linux/ip.h>
  19. #include <linux/ipv6.h>
  20. #include <linux/filter.h>
  21. #include <linux/perf_event.h>
  22. #include <linux/socket.h>
  23. #include <linux/unistd.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/wait.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/param.h>
  29. #include <fcntl.h>
  30. #include <pthread.h>
  31. #include <linux/bpf.h>
  32. #include <linux/err.h>
  33. #include <bpf/bpf.h>
  34. #include <bpf/libbpf.h>
  35. #include "test_iptunnel_common.h"
  36. #include "bpf_util.h"
  37. #include <bpf/bpf_endian.h>
  38. #include "trace_helpers.h"
  39. #include "testing_helpers.h"
  40. enum verbosity {
  41. VERBOSE_NONE,
  42. VERBOSE_NORMAL,
  43. VERBOSE_VERY,
  44. VERBOSE_SUPER,
  45. };
  46. struct test_filter {
  47. char *name;
  48. char **subtests;
  49. int subtest_cnt;
  50. };
  51. struct test_filter_set {
  52. struct test_filter *tests;
  53. int cnt;
  54. };
  55. struct test_selector {
  56. struct test_filter_set whitelist;
  57. struct test_filter_set blacklist;
  58. bool *num_set;
  59. int num_set_len;
  60. };
  61. struct subtest_state {
  62. char *name;
  63. size_t log_cnt;
  64. char *log_buf;
  65. int error_cnt;
  66. bool skipped;
  67. bool filtered;
  68. bool should_tmon;
  69. FILE *stdout_saved;
  70. };
  71. struct test_state {
  72. bool tested;
  73. bool force_log;
  74. int error_cnt;
  75. int skip_cnt;
  76. int sub_succ_cnt;
  77. struct subtest_state *subtest_states;
  78. int subtest_num;
  79. size_t log_cnt;
  80. char *log_buf;
  81. FILE *stdout_saved;
  82. };
  83. extern int env_verbosity;
  84. struct test_env {
  85. struct test_selector test_selector;
  86. struct test_selector subtest_selector;
  87. struct test_selector tmon_selector;
  88. bool verifier_stats;
  89. bool debug;
  90. enum verbosity verbosity;
  91. bool jit_enabled;
  92. bool has_testmod;
  93. bool get_test_cnt;
  94. bool list_test_names;
  95. struct prog_test_def *test; /* current running test */
  96. struct test_state *test_state; /* current running test state */
  97. struct subtest_state *subtest_state; /* current running subtest state */
  98. FILE *stdout_saved;
  99. FILE *stderr_saved;
  100. int nr_cpus;
  101. FILE *json;
  102. int succ_cnt; /* successful tests */
  103. int sub_succ_cnt; /* successful sub-tests */
  104. int fail_cnt; /* total failed tests + sub-tests */
  105. int skip_cnt; /* skipped tests */
  106. int saved_netns_fd;
  107. int workers; /* number of worker process */
  108. int worker_id; /* id number of current worker, main process is -1 */
  109. pid_t *worker_pids; /* array of worker pids */
  110. int *worker_socks; /* array of worker socks */
  111. int *worker_current_test; /* array of current running test for each worker */
  112. };
  113. #define MAX_LOG_TRUNK_SIZE 8192
  114. #define MAX_SUBTEST_NAME 1024
  115. enum msg_type {
  116. MSG_DO_TEST = 0,
  117. MSG_TEST_DONE = 1,
  118. MSG_TEST_LOG = 2,
  119. MSG_SUBTEST_DONE = 3,
  120. MSG_EXIT = 255,
  121. };
  122. struct msg {
  123. enum msg_type type;
  124. union {
  125. struct {
  126. int num;
  127. } do_test;
  128. struct {
  129. int num;
  130. int sub_succ_cnt;
  131. int error_cnt;
  132. int skip_cnt;
  133. bool have_log;
  134. int subtest_num;
  135. } test_done;
  136. struct {
  137. char log_buf[MAX_LOG_TRUNK_SIZE + 1];
  138. bool is_last;
  139. } test_log;
  140. struct {
  141. int num;
  142. char name[MAX_SUBTEST_NAME + 1];
  143. int error_cnt;
  144. bool skipped;
  145. bool filtered;
  146. bool have_log;
  147. } subtest_done;
  148. };
  149. };
  150. extern struct test_env env;
  151. void test__force_log(void);
  152. bool test__start_subtest(const char *name);
  153. void test__end_subtest(void);
  154. void test__skip(void);
  155. void test__fail(void);
  156. int test__join_cgroup(const char *path);
  157. #define PRINT_FAIL(format...) \
  158. ({ \
  159. test__fail(); \
  160. fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__); \
  161. fprintf(stdout, ##format); \
  162. })
  163. #define _CHECK(condition, tag, duration, format...) ({ \
  164. int __ret = !!(condition); \
  165. int __save_errno = errno; \
  166. if (__ret) { \
  167. test__fail(); \
  168. fprintf(stdout, "%s:FAIL:%s ", __func__, tag); \
  169. fprintf(stdout, ##format); \
  170. } else { \
  171. fprintf(stdout, "%s:PASS:%s %d nsec\n", \
  172. __func__, tag, duration); \
  173. } \
  174. errno = __save_errno; \
  175. __ret; \
  176. })
  177. #define CHECK_FAIL(condition) ({ \
  178. int __ret = !!(condition); \
  179. int __save_errno = errno; \
  180. if (__ret) { \
  181. test__fail(); \
  182. fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__); \
  183. } \
  184. errno = __save_errno; \
  185. __ret; \
  186. })
  187. #define CHECK(condition, tag, format...) \
  188. _CHECK(condition, tag, duration, format)
  189. #define CHECK_ATTR(condition, tag, format...) \
  190. _CHECK(condition, tag, tattr.duration, format)
  191. #define ASSERT_FAIL(fmt, args...) ({ \
  192. static int duration = 0; \
  193. CHECK(false, "", fmt"\n", ##args); \
  194. false; \
  195. })
  196. #define ASSERT_TRUE(actual, name) ({ \
  197. static int duration = 0; \
  198. bool ___ok = (actual); \
  199. CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name)); \
  200. ___ok; \
  201. })
  202. #define ASSERT_FALSE(actual, name) ({ \
  203. static int duration = 0; \
  204. bool ___ok = !(actual); \
  205. CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
  206. ___ok; \
  207. })
  208. #define ASSERT_EQ(actual, expected, name) ({ \
  209. static int duration = 0; \
  210. typeof(actual) ___act = (actual); \
  211. typeof(expected) ___exp = (expected); \
  212. bool ___ok = ___act == ___exp; \
  213. CHECK(!___ok, (name), \
  214. "unexpected %s: actual %lld != expected %lld\n", \
  215. (name), (long long)(___act), (long long)(___exp)); \
  216. ___ok; \
  217. })
  218. #define ASSERT_NEQ(actual, expected, name) ({ \
  219. static int duration = 0; \
  220. typeof(actual) ___act = (actual); \
  221. typeof(expected) ___exp = (expected); \
  222. bool ___ok = ___act != ___exp; \
  223. CHECK(!___ok, (name), \
  224. "unexpected %s: actual %lld == expected %lld\n", \
  225. (name), (long long)(___act), (long long)(___exp)); \
  226. ___ok; \
  227. })
  228. #define ASSERT_LT(actual, expected, name) ({ \
  229. static int duration = 0; \
  230. typeof(actual) ___act = (actual); \
  231. typeof(expected) ___exp = (expected); \
  232. bool ___ok = ___act < ___exp; \
  233. CHECK(!___ok, (name), \
  234. "unexpected %s: actual %lld >= expected %lld\n", \
  235. (name), (long long)(___act), (long long)(___exp)); \
  236. ___ok; \
  237. })
  238. #define ASSERT_LE(actual, expected, name) ({ \
  239. static int duration = 0; \
  240. typeof(actual) ___act = (actual); \
  241. typeof(expected) ___exp = (expected); \
  242. bool ___ok = ___act <= ___exp; \
  243. CHECK(!___ok, (name), \
  244. "unexpected %s: actual %lld > expected %lld\n", \
  245. (name), (long long)(___act), (long long)(___exp)); \
  246. ___ok; \
  247. })
  248. #define ASSERT_GT(actual, expected, name) ({ \
  249. static int duration = 0; \
  250. typeof(actual) ___act = (actual); \
  251. typeof(expected) ___exp = (expected); \
  252. bool ___ok = ___act > ___exp; \
  253. CHECK(!___ok, (name), \
  254. "unexpected %s: actual %lld <= expected %lld\n", \
  255. (name), (long long)(___act), (long long)(___exp)); \
  256. ___ok; \
  257. })
  258. #define ASSERT_GE(actual, expected, name) ({ \
  259. static int duration = 0; \
  260. typeof(actual) ___act = (actual); \
  261. typeof(expected) ___exp = (expected); \
  262. bool ___ok = ___act >= ___exp; \
  263. CHECK(!___ok, (name), \
  264. "unexpected %s: actual %lld < expected %lld\n", \
  265. (name), (long long)(___act), (long long)(___exp)); \
  266. ___ok; \
  267. })
  268. #define ASSERT_STREQ(actual, expected, name) ({ \
  269. static int duration = 0; \
  270. const char *___act = actual; \
  271. const char *___exp = expected; \
  272. bool ___ok = strcmp(___act, ___exp) == 0; \
  273. CHECK(!___ok, (name), \
  274. "unexpected %s: actual '%s' != expected '%s'\n", \
  275. (name), ___act, ___exp); \
  276. ___ok; \
  277. })
  278. #define ASSERT_STRNEQ(actual, expected, len, name) ({ \
  279. static int duration = 0; \
  280. const char *___act = actual; \
  281. const char *___exp = expected; \
  282. int ___len = len; \
  283. bool ___ok = strncmp(___act, ___exp, ___len) == 0; \
  284. CHECK(!___ok, (name), \
  285. "unexpected %s: actual '%.*s' != expected '%.*s'\n", \
  286. (name), ___len, ___act, ___len, ___exp); \
  287. ___ok; \
  288. })
  289. #define ASSERT_HAS_SUBSTR(str, substr, name) ({ \
  290. static int duration = 0; \
  291. const char *___str = str; \
  292. const char *___substr = substr; \
  293. bool ___ok = strstr(___str, ___substr) != NULL; \
  294. CHECK(!___ok, (name), \
  295. "unexpected %s: '%s' is not a substring of '%s'\n", \
  296. (name), ___substr, ___str); \
  297. ___ok; \
  298. })
  299. #define ASSERT_OK(res, name) ({ \
  300. static int duration = 0; \
  301. long long ___res = (res); \
  302. bool ___ok = ___res == 0; \
  303. CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n", \
  304. ___res, errno); \
  305. ___ok; \
  306. })
  307. #define ASSERT_ERR(res, name) ({ \
  308. static int duration = 0; \
  309. long long ___res = (res); \
  310. bool ___ok = ___res < 0; \
  311. CHECK(!___ok, (name), "unexpected success: %lld\n", ___res); \
  312. ___ok; \
  313. })
  314. #define ASSERT_NULL(ptr, name) ({ \
  315. static int duration = 0; \
  316. const void *___res = (ptr); \
  317. bool ___ok = !___res; \
  318. CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
  319. ___ok; \
  320. })
  321. #define ASSERT_OK_PTR(ptr, name) ({ \
  322. static int duration = 0; \
  323. const void *___res = (ptr); \
  324. int ___err = libbpf_get_error(___res); \
  325. bool ___ok = ___err == 0; \
  326. CHECK(!___ok, (name), "unexpected error: %d\n", ___err); \
  327. ___ok; \
  328. })
  329. #define ASSERT_ERR_PTR(ptr, name) ({ \
  330. static int duration = 0; \
  331. const void *___res = (ptr); \
  332. int ___err = libbpf_get_error(___res); \
  333. bool ___ok = ___err != 0; \
  334. CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res); \
  335. ___ok; \
  336. })
  337. #define ASSERT_OK_FD(fd, name) ({ \
  338. static int duration = 0; \
  339. int ___fd = (fd); \
  340. bool ___ok = ___fd >= 0; \
  341. CHECK(!___ok, (name), "unexpected fd: %d (errno %d)\n", \
  342. ___fd, errno); \
  343. ___ok; \
  344. })
  345. #define SYS(goto_label, fmt, ...) \
  346. ({ \
  347. char cmd[1024]; \
  348. snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
  349. if (!ASSERT_OK(system(cmd), cmd)) \
  350. goto goto_label; \
  351. })
  352. #define ALL_TO_DEV_NULL " >/dev/null 2>&1"
  353. #define SYS_NOFAIL(fmt, ...) \
  354. ({ \
  355. char cmd[1024]; \
  356. int n; \
  357. n = snprintf(cmd, sizeof(cmd), fmt, ##__VA_ARGS__); \
  358. if (n < sizeof(cmd) && sizeof(cmd) - n >= sizeof(ALL_TO_DEV_NULL)) \
  359. strcat(cmd, ALL_TO_DEV_NULL); \
  360. system(cmd); \
  361. })
  362. int start_libbpf_log_capture(void);
  363. char *stop_libbpf_log_capture(void);
  364. static inline __u64 ptr_to_u64(const void *ptr)
  365. {
  366. return (__u64) (unsigned long) ptr;
  367. }
  368. static inline void *u64_to_ptr(__u64 ptr)
  369. {
  370. return (void *) (unsigned long) ptr;
  371. }
  372. int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
  373. int compare_map_keys(int map1_fd, int map2_fd);
  374. int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
  375. int trigger_module_test_read(int read_sz);
  376. int trigger_module_test_write(int write_sz);
  377. int write_sysctl(const char *sysctl, const char *value);
  378. int get_bpf_max_tramp_links_from(struct btf *btf);
  379. int get_bpf_max_tramp_links(void);
  380. struct netns_obj;
  381. struct netns_obj *netns_new(const char *name, bool open);
  382. void netns_free(struct netns_obj *netns);
  383. #ifdef __x86_64__
  384. #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
  385. #elif defined(__s390x__)
  386. #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
  387. #elif defined(__aarch64__)
  388. #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
  389. #elif defined(__riscv)
  390. #define SYS_NANOSLEEP_KPROBE_NAME "__riscv_sys_nanosleep"
  391. #else
  392. #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
  393. #endif
  394. #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"
  395. typedef int (*pre_execution_cb)(struct bpf_object *obj);
  396. struct test_loader {
  397. char *log_buf;
  398. size_t log_buf_sz;
  399. pre_execution_cb pre_execution_cb;
  400. struct bpf_object *obj;
  401. };
  402. static inline void test_loader__set_pre_execution_cb(struct test_loader *tester,
  403. pre_execution_cb cb)
  404. {
  405. tester->pre_execution_cb = cb;
  406. }
  407. typedef const void *(*skel_elf_bytes_fn)(size_t *sz);
  408. extern void test_loader__run_subtests(struct test_loader *tester,
  409. const char *skel_name,
  410. skel_elf_bytes_fn elf_bytes_factory);
  411. extern void test_loader_fini(struct test_loader *tester);
  412. #define RUN_TESTS(skel) ({ \
  413. struct test_loader tester = {}; \
  414. \
  415. test_loader__run_subtests(&tester, #skel, skel##__elf_bytes); \
  416. test_loader_fini(&tester); \
  417. })
  418. #endif /* __TEST_PROGS_H */