test_progs.h 10 KB

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