Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #include <stdio.h>
0003 #include <unistd.h>
0004 #include <errno.h>
0005 #include <string.h>
0006 #include <assert.h>
0007 #include <stdlib.h>
0008 #include <stdarg.h>
0009 #include <time.h>
0010 #include <signal.h>
0011 
0012 #include <linux/types.h>
0013 typedef __u16 __sum16;
0014 #include <arpa/inet.h>
0015 #include <linux/if_ether.h>
0016 #include <linux/if_packet.h>
0017 #include <linux/ip.h>
0018 #include <linux/ipv6.h>
0019 #include <linux/filter.h>
0020 #include <linux/perf_event.h>
0021 #include <linux/socket.h>
0022 #include <linux/unistd.h>
0023 
0024 #include <sys/ioctl.h>
0025 #include <sys/wait.h>
0026 #include <sys/types.h>
0027 #include <sys/time.h>
0028 #include <sys/param.h>
0029 #include <fcntl.h>
0030 #include <pthread.h>
0031 #include <linux/bpf.h>
0032 #include <linux/err.h>
0033 #include <bpf/bpf.h>
0034 #include <bpf/libbpf.h>
0035 
0036 #include "test_iptunnel_common.h"
0037 #include "bpf_util.h"
0038 #include <bpf/bpf_endian.h>
0039 #include "trace_helpers.h"
0040 #include "testing_helpers.h"
0041 
0042 enum verbosity {
0043     VERBOSE_NONE,
0044     VERBOSE_NORMAL,
0045     VERBOSE_VERY,
0046     VERBOSE_SUPER,
0047 };
0048 
0049 struct test_filter {
0050     char *name;
0051     char **subtests;
0052     int subtest_cnt;
0053 };
0054 
0055 struct test_filter_set {
0056     struct test_filter *tests;
0057     int cnt;
0058 };
0059 
0060 struct test_selector {
0061     struct test_filter_set whitelist;
0062     struct test_filter_set blacklist;
0063     bool *num_set;
0064     int num_set_len;
0065 };
0066 
0067 struct subtest_state {
0068     char *name;
0069     size_t log_cnt;
0070     char *log_buf;
0071     int error_cnt;
0072     bool skipped;
0073     bool filtered;
0074 
0075     FILE *stdout;
0076 };
0077 
0078 struct test_state {
0079     bool tested;
0080     bool force_log;
0081 
0082     int error_cnt;
0083     int skip_cnt;
0084     int sub_succ_cnt;
0085 
0086     struct subtest_state *subtest_states;
0087     int subtest_num;
0088 
0089     size_t log_cnt;
0090     char *log_buf;
0091 
0092     FILE *stdout;
0093 };
0094 
0095 struct test_env {
0096     struct test_selector test_selector;
0097     struct test_selector subtest_selector;
0098     bool verifier_stats;
0099     bool debug;
0100     enum verbosity verbosity;
0101 
0102     bool jit_enabled;
0103     bool has_testmod;
0104     bool get_test_cnt;
0105     bool list_test_names;
0106 
0107     struct prog_test_def *test; /* current running test */
0108     struct test_state *test_state; /* current running test state */
0109     struct subtest_state *subtest_state; /* current running subtest state */
0110 
0111     FILE *stdout;
0112     FILE *stderr;
0113     int nr_cpus;
0114 
0115     int succ_cnt; /* successful tests */
0116     int sub_succ_cnt; /* successful sub-tests */
0117     int fail_cnt; /* total failed tests + sub-tests */
0118     int skip_cnt; /* skipped tests */
0119 
0120     int saved_netns_fd;
0121     int workers; /* number of worker process */
0122     int worker_id; /* id number of current worker, main process is -1 */
0123     pid_t *worker_pids; /* array of worker pids */
0124     int *worker_socks; /* array of worker socks */
0125     int *worker_current_test; /* array of current running test for each worker */
0126 };
0127 
0128 #define MAX_LOG_TRUNK_SIZE 8192
0129 #define MAX_SUBTEST_NAME 1024
0130 enum msg_type {
0131     MSG_DO_TEST = 0,
0132     MSG_TEST_DONE = 1,
0133     MSG_TEST_LOG = 2,
0134     MSG_SUBTEST_DONE = 3,
0135     MSG_EXIT = 255,
0136 };
0137 struct msg {
0138     enum msg_type type;
0139     union {
0140         struct {
0141             int num;
0142         } do_test;
0143         struct {
0144             int num;
0145             int sub_succ_cnt;
0146             int error_cnt;
0147             int skip_cnt;
0148             bool have_log;
0149             int subtest_num;
0150         } test_done;
0151         struct {
0152             char log_buf[MAX_LOG_TRUNK_SIZE + 1];
0153             bool is_last;
0154         } test_log;
0155         struct {
0156             int num;
0157             char name[MAX_SUBTEST_NAME + 1];
0158             int error_cnt;
0159             bool skipped;
0160             bool filtered;
0161             bool have_log;
0162         } subtest_done;
0163     };
0164 };
0165 
0166 extern struct test_env env;
0167 
0168 void test__force_log(void);
0169 bool test__start_subtest(const char *name);
0170 void test__end_subtest(void);
0171 void test__skip(void);
0172 void test__fail(void);
0173 int test__join_cgroup(const char *path);
0174 
0175 #define PRINT_FAIL(format...)                                                  \
0176     ({                                                                     \
0177         test__fail();                                                  \
0178         fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
0179         fprintf(stdout, ##format);                                     \
0180     })
0181 
0182 #define _CHECK(condition, tag, duration, format...) ({          \
0183     int __ret = !!(condition);                  \
0184     int __save_errno = errno;                   \
0185     if (__ret) {                            \
0186         test__fail();                       \
0187         fprintf(stdout, "%s:FAIL:%s ", __func__, tag);      \
0188         fprintf(stdout, ##format);              \
0189     } else {                            \
0190         fprintf(stdout, "%s:PASS:%s %d nsec\n",         \
0191                __func__, tag, duration);            \
0192     }                               \
0193     errno = __save_errno;                       \
0194     __ret;                              \
0195 })
0196 
0197 #define CHECK_FAIL(condition) ({                    \
0198     int __ret = !!(condition);                  \
0199     int __save_errno = errno;                   \
0200     if (__ret) {                            \
0201         test__fail();                       \
0202         fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);    \
0203     }                               \
0204     errno = __save_errno;                       \
0205     __ret;                              \
0206 })
0207 
0208 #define CHECK(condition, tag, format...) \
0209     _CHECK(condition, tag, duration, format)
0210 #define CHECK_ATTR(condition, tag, format...) \
0211     _CHECK(condition, tag, tattr.duration, format)
0212 
0213 #define ASSERT_TRUE(actual, name) ({                    \
0214     static int duration = 0;                    \
0215     bool ___ok = (actual);                      \
0216     CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));    \
0217     ___ok;                              \
0218 })
0219 
0220 #define ASSERT_FALSE(actual, name) ({                   \
0221     static int duration = 0;                    \
0222     bool ___ok = !(actual);                     \
0223     CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name)); \
0224     ___ok;                              \
0225 })
0226 
0227 #define ASSERT_EQ(actual, expected, name) ({                \
0228     static int duration = 0;                    \
0229     typeof(actual) ___act = (actual);               \
0230     typeof(expected) ___exp = (expected);               \
0231     bool ___ok = ___act == ___exp;                  \
0232     CHECK(!___ok, (name),                       \
0233           "unexpected %s: actual %lld != expected %lld\n",      \
0234           (name), (long long)(___act), (long long)(___exp));    \
0235     ___ok;                              \
0236 })
0237 
0238 #define ASSERT_NEQ(actual, expected, name) ({               \
0239     static int duration = 0;                    \
0240     typeof(actual) ___act = (actual);               \
0241     typeof(expected) ___exp = (expected);               \
0242     bool ___ok = ___act != ___exp;                  \
0243     CHECK(!___ok, (name),                       \
0244           "unexpected %s: actual %lld == expected %lld\n",      \
0245           (name), (long long)(___act), (long long)(___exp));    \
0246     ___ok;                              \
0247 })
0248 
0249 #define ASSERT_LT(actual, expected, name) ({                \
0250     static int duration = 0;                    \
0251     typeof(actual) ___act = (actual);               \
0252     typeof(expected) ___exp = (expected);               \
0253     bool ___ok = ___act < ___exp;                   \
0254     CHECK(!___ok, (name),                       \
0255           "unexpected %s: actual %lld >= expected %lld\n",      \
0256           (name), (long long)(___act), (long long)(___exp));    \
0257     ___ok;                              \
0258 })
0259 
0260 #define ASSERT_LE(actual, expected, name) ({                \
0261     static int duration = 0;                    \
0262     typeof(actual) ___act = (actual);               \
0263     typeof(expected) ___exp = (expected);               \
0264     bool ___ok = ___act <= ___exp;                  \
0265     CHECK(!___ok, (name),                       \
0266           "unexpected %s: actual %lld > expected %lld\n",       \
0267           (name), (long long)(___act), (long long)(___exp));    \
0268     ___ok;                              \
0269 })
0270 
0271 #define ASSERT_GT(actual, expected, name) ({                \
0272     static int duration = 0;                    \
0273     typeof(actual) ___act = (actual);               \
0274     typeof(expected) ___exp = (expected);               \
0275     bool ___ok = ___act > ___exp;                   \
0276     CHECK(!___ok, (name),                       \
0277           "unexpected %s: actual %lld <= expected %lld\n",      \
0278           (name), (long long)(___act), (long long)(___exp));    \
0279     ___ok;                              \
0280 })
0281 
0282 #define ASSERT_GE(actual, expected, name) ({                \
0283     static int duration = 0;                    \
0284     typeof(actual) ___act = (actual);               \
0285     typeof(expected) ___exp = (expected);               \
0286     bool ___ok = ___act >= ___exp;                  \
0287     CHECK(!___ok, (name),                       \
0288           "unexpected %s: actual %lld < expected %lld\n",       \
0289           (name), (long long)(___act), (long long)(___exp));    \
0290     ___ok;                              \
0291 })
0292 
0293 #define ASSERT_STREQ(actual, expected, name) ({             \
0294     static int duration = 0;                    \
0295     const char *___act = actual;                    \
0296     const char *___exp = expected;                  \
0297     bool ___ok = strcmp(___act, ___exp) == 0;           \
0298     CHECK(!___ok, (name),                       \
0299           "unexpected %s: actual '%s' != expected '%s'\n",      \
0300           (name), ___act, ___exp);                  \
0301     ___ok;                              \
0302 })
0303 
0304 #define ASSERT_STRNEQ(actual, expected, len, name) ({           \
0305     static int duration = 0;                    \
0306     const char *___act = actual;                    \
0307     const char *___exp = expected;                  \
0308     int ___len = len;                       \
0309     bool ___ok = strncmp(___act, ___exp, ___len) == 0;      \
0310     CHECK(!___ok, (name),                       \
0311           "unexpected %s: actual '%.*s' != expected '%.*s'\n",  \
0312           (name), ___len, ___act, ___len, ___exp);          \
0313     ___ok;                              \
0314 })
0315 
0316 #define ASSERT_HAS_SUBSTR(str, substr, name) ({             \
0317     static int duration = 0;                    \
0318     const char *___str = str;                   \
0319     const char *___substr = substr;                 \
0320     bool ___ok = strstr(___str, ___substr) != NULL;         \
0321     CHECK(!___ok, (name),                       \
0322           "unexpected %s: '%s' is not a substring of '%s'\n",   \
0323           (name), ___substr, ___str);               \
0324     ___ok;                              \
0325 })
0326 
0327 #define ASSERT_OK(res, name) ({                     \
0328     static int duration = 0;                    \
0329     long long ___res = (res);                   \
0330     bool ___ok = ___res == 0;                   \
0331     CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",    \
0332           ___res, errno);                       \
0333     ___ok;                              \
0334 })
0335 
0336 #define ASSERT_ERR(res, name) ({                    \
0337     static int duration = 0;                    \
0338     long long ___res = (res);                   \
0339     bool ___ok = ___res < 0;                    \
0340     CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);    \
0341     ___ok;                              \
0342 })
0343 
0344 #define ASSERT_NULL(ptr, name) ({                   \
0345     static int duration = 0;                    \
0346     const void *___res = (ptr);                 \
0347     bool ___ok = !___res;                       \
0348     CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);  \
0349     ___ok;                              \
0350 })
0351 
0352 #define ASSERT_OK_PTR(ptr, name) ({                 \
0353     static int duration = 0;                    \
0354     const void *___res = (ptr);                 \
0355     int ___err = libbpf_get_error(___res);              \
0356     bool ___ok = ___err == 0;                   \
0357     CHECK(!___ok, (name), "unexpected error: %d\n", ___err);    \
0358     ___ok;                              \
0359 })
0360 
0361 #define ASSERT_ERR_PTR(ptr, name) ({                    \
0362     static int duration = 0;                    \
0363     const void *___res = (ptr);                 \
0364     int ___err = libbpf_get_error(___res);              \
0365     bool ___ok = ___err != 0;                   \
0366     CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);  \
0367     ___ok;                              \
0368 })
0369 
0370 static inline __u64 ptr_to_u64(const void *ptr)
0371 {
0372     return (__u64) (unsigned long) ptr;
0373 }
0374 
0375 static inline void *u64_to_ptr(__u64 ptr)
0376 {
0377     return (void *) (unsigned long) ptr;
0378 }
0379 
0380 int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
0381 int compare_map_keys(int map1_fd, int map2_fd);
0382 int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
0383 int extract_build_id(char *build_id, size_t size);
0384 int kern_sync_rcu(void);
0385 int trigger_module_test_read(int read_sz);
0386 int trigger_module_test_write(int write_sz);
0387 
0388 #ifdef __x86_64__
0389 #define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
0390 #elif defined(__s390x__)
0391 #define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
0392 #elif defined(__aarch64__)
0393 #define SYS_NANOSLEEP_KPROBE_NAME "__arm64_sys_nanosleep"
0394 #else
0395 #define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
0396 #endif
0397 
0398 #define BPF_TESTMOD_TEST_FILE "/sys/kernel/bpf_testmod"