linux/tools/testing/selftests/bpf/test_progs.h
<<
>>
Prefs
   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
  12#include <linux/types.h>
  13typedef __u16 __sum16;
  14#include <arpa/inet.h>
  15#include <linux/if_ether.h>
  16#include <linux/if_packet.h>
  17#include <linux/ip.h>
  18#include <linux/ipv6.h>
  19#include <linux/filter.h>
  20#include <linux/perf_event.h>
  21#include <linux/socket.h>
  22#include <linux/unistd.h>
  23
  24#include <sys/ioctl.h>
  25#include <sys/wait.h>
  26#include <sys/types.h>
  27#include <sys/time.h>
  28#include <fcntl.h>
  29#include <pthread.h>
  30#include <linux/bpf.h>
  31#include <linux/err.h>
  32#include <bpf/bpf.h>
  33#include <bpf/libbpf.h>
  34
  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#include "flow_dissector_load.h"
  41
  42enum verbosity {
  43        VERBOSE_NONE,
  44        VERBOSE_NORMAL,
  45        VERBOSE_VERY,
  46        VERBOSE_SUPER,
  47};
  48
  49struct str_set {
  50        const char **strs;
  51        int cnt;
  52};
  53
  54struct test_selector {
  55        struct str_set whitelist;
  56        struct str_set blacklist;
  57        bool *num_set;
  58        int num_set_len;
  59};
  60
  61struct test_env {
  62        struct test_selector test_selector;
  63        struct test_selector subtest_selector;
  64        bool verifier_stats;
  65        enum verbosity verbosity;
  66
  67        bool jit_enabled;
  68        bool has_testmod;
  69        bool get_test_cnt;
  70        bool list_test_names;
  71
  72        struct prog_test_def *test;
  73        FILE *stdout;
  74        FILE *stderr;
  75        char *log_buf;
  76        size_t log_cnt;
  77        int nr_cpus;
  78
  79        int succ_cnt; /* successful tests */
  80        int sub_succ_cnt; /* successful sub-tests */
  81        int fail_cnt; /* total failed tests + sub-tests */
  82        int skip_cnt; /* skipped tests */
  83
  84        int saved_netns_fd;
  85};
  86
  87extern struct test_env env;
  88
  89extern void test__force_log();
  90extern bool test__start_subtest(const char *name);
  91extern void test__skip(void);
  92extern void test__fail(void);
  93extern int test__join_cgroup(const char *path);
  94
  95#define PRINT_FAIL(format...)                                                  \
  96        ({                                                                     \
  97                test__fail();                                                  \
  98                fprintf(stdout, "%s:FAIL:%d ", __func__, __LINE__);            \
  99                fprintf(stdout, ##format);                                     \
 100        })
 101
 102#define _CHECK(condition, tag, duration, format...) ({                  \
 103        int __ret = !!(condition);                                      \
 104        int __save_errno = errno;                                       \
 105        if (__ret) {                                                    \
 106                test__fail();                                           \
 107                fprintf(stdout, "%s:FAIL:%s ", __func__, tag);          \
 108                fprintf(stdout, ##format);                              \
 109        } else {                                                        \
 110                fprintf(stdout, "%s:PASS:%s %d nsec\n",                 \
 111                       __func__, tag, duration);                        \
 112        }                                                               \
 113        errno = __save_errno;                                           \
 114        __ret;                                                          \
 115})
 116
 117#define CHECK_FAIL(condition) ({                                        \
 118        int __ret = !!(condition);                                      \
 119        int __save_errno = errno;                                       \
 120        if (__ret) {                                                    \
 121                test__fail();                                           \
 122                fprintf(stdout, "%s:FAIL:%d\n", __func__, __LINE__);    \
 123        }                                                               \
 124        errno = __save_errno;                                           \
 125        __ret;                                                          \
 126})
 127
 128#define CHECK(condition, tag, format...) \
 129        _CHECK(condition, tag, duration, format)
 130#define CHECK_ATTR(condition, tag, format...) \
 131        _CHECK(condition, tag, tattr.duration, format)
 132
 133#define ASSERT_TRUE(actual, name) ({                                    \
 134        static int duration = 0;                                        \
 135        bool ___ok = (actual);                                          \
 136        CHECK(!___ok, (name), "unexpected %s: got FALSE\n", (name));    \
 137        ___ok;                                                          \
 138})
 139
 140#define ASSERT_FALSE(actual, name) ({                                   \
 141        static int duration = 0;                                        \
 142        bool ___ok = !(actual);                                         \
 143        CHECK(!___ok, (name), "unexpected %s: got TRUE\n", (name));     \
 144        ___ok;                                                          \
 145})
 146
 147#define ASSERT_EQ(actual, expected, name) ({                            \
 148        static int duration = 0;                                        \
 149        typeof(actual) ___act = (actual);                               \
 150        typeof(expected) ___exp = (expected);                           \
 151        bool ___ok = ___act == ___exp;                                  \
 152        CHECK(!___ok, (name),                                           \
 153              "unexpected %s: actual %lld != expected %lld\n",          \
 154              (name), (long long)(___act), (long long)(___exp));        \
 155        ___ok;                                                          \
 156})
 157
 158#define ASSERT_NEQ(actual, expected, name) ({                           \
 159        static int duration = 0;                                        \
 160        typeof(actual) ___act = (actual);                               \
 161        typeof(expected) ___exp = (expected);                           \
 162        bool ___ok = ___act != ___exp;                                  \
 163        CHECK(!___ok, (name),                                           \
 164              "unexpected %s: actual %lld == expected %lld\n",          \
 165              (name), (long long)(___act), (long long)(___exp));        \
 166        ___ok;                                                          \
 167})
 168
 169#define ASSERT_LT(actual, expected, name) ({                            \
 170        static int duration = 0;                                        \
 171        typeof(actual) ___act = (actual);                               \
 172        typeof(expected) ___exp = (expected);                           \
 173        bool ___ok = ___act < ___exp;                                   \
 174        CHECK(!___ok, (name),                                           \
 175              "unexpected %s: actual %lld >= expected %lld\n",          \
 176              (name), (long long)(___act), (long long)(___exp));        \
 177        ___ok;                                                          \
 178})
 179
 180#define ASSERT_LE(actual, expected, name) ({                            \
 181        static int duration = 0;                                        \
 182        typeof(actual) ___act = (actual);                               \
 183        typeof(expected) ___exp = (expected);                           \
 184        bool ___ok = ___act <= ___exp;                                  \
 185        CHECK(!___ok, (name),                                           \
 186              "unexpected %s: actual %lld > expected %lld\n",           \
 187              (name), (long long)(___act), (long long)(___exp));        \
 188        ___ok;                                                          \
 189})
 190
 191#define ASSERT_GT(actual, expected, name) ({                            \
 192        static int duration = 0;                                        \
 193        typeof(actual) ___act = (actual);                               \
 194        typeof(expected) ___exp = (expected);                           \
 195        bool ___ok = ___act > ___exp;                                   \
 196        CHECK(!___ok, (name),                                           \
 197              "unexpected %s: actual %lld <= expected %lld\n",          \
 198              (name), (long long)(___act), (long long)(___exp));        \
 199        ___ok;                                                          \
 200})
 201
 202#define ASSERT_GE(actual, expected, name) ({                            \
 203        static int duration = 0;                                        \
 204        typeof(actual) ___act = (actual);                               \
 205        typeof(expected) ___exp = (expected);                           \
 206        bool ___ok = ___act >= ___exp;                                  \
 207        CHECK(!___ok, (name),                                           \
 208              "unexpected %s: actual %lld < expected %lld\n",           \
 209              (name), (long long)(___act), (long long)(___exp));        \
 210        ___ok;                                                          \
 211})
 212
 213#define ASSERT_STREQ(actual, expected, name) ({                         \
 214        static int duration = 0;                                        \
 215        const char *___act = actual;                                    \
 216        const char *___exp = expected;                                  \
 217        bool ___ok = strcmp(___act, ___exp) == 0;                       \
 218        CHECK(!___ok, (name),                                           \
 219              "unexpected %s: actual '%s' != expected '%s'\n",          \
 220              (name), ___act, ___exp);                                  \
 221        ___ok;                                                          \
 222})
 223
 224#define ASSERT_STRNEQ(actual, expected, len, name) ({                   \
 225        static int duration = 0;                                        \
 226        const char *___act = actual;                                    \
 227        const char *___exp = expected;                                  \
 228        int ___len = len;                                               \
 229        bool ___ok = strncmp(___act, ___exp, ___len) == 0;              \
 230        CHECK(!___ok, (name),                                           \
 231              "unexpected %s: actual '%.*s' != expected '%.*s'\n",      \
 232              (name), ___len, ___act, ___len, ___exp);                  \
 233        ___ok;                                                          \
 234})
 235
 236#define ASSERT_OK(res, name) ({                                         \
 237        static int duration = 0;                                        \
 238        long long ___res = (res);                                       \
 239        bool ___ok = ___res == 0;                                       \
 240        CHECK(!___ok, (name), "unexpected error: %lld (errno %d)\n",    \
 241              ___res, errno);                                           \
 242        ___ok;                                                          \
 243})
 244
 245#define ASSERT_ERR(res, name) ({                                        \
 246        static int duration = 0;                                        \
 247        long long ___res = (res);                                       \
 248        bool ___ok = ___res < 0;                                        \
 249        CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);    \
 250        ___ok;                                                          \
 251})
 252
 253#define ASSERT_NULL(ptr, name) ({                                       \
 254        static int duration = 0;                                        \
 255        const void *___res = (ptr);                                     \
 256        bool ___ok = !___res;                                           \
 257        CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);      \
 258        ___ok;                                                          \
 259})
 260
 261#define ASSERT_OK_PTR(ptr, name) ({                                     \
 262        static int duration = 0;                                        \
 263        const void *___res = (ptr);                                     \
 264        int ___err = libbpf_get_error(___res);                          \
 265        bool ___ok = ___err == 0;                                       \
 266        CHECK(!___ok, (name), "unexpected error: %d\n", ___err);        \
 267        ___ok;                                                          \
 268})
 269
 270#define ASSERT_ERR_PTR(ptr, name) ({                                    \
 271        static int duration = 0;                                        \
 272        const void *___res = (ptr);                                     \
 273        int ___err = libbpf_get_error(___res);                          \
 274        bool ___ok = ___err != 0;                                       \
 275        CHECK(!___ok, (name), "unexpected pointer: %p\n", ___res);      \
 276        ___ok;                                                          \
 277})
 278
 279static inline __u64 ptr_to_u64(const void *ptr)
 280{
 281        return (__u64) (unsigned long) ptr;
 282}
 283
 284static inline void *u64_to_ptr(__u64 ptr)
 285{
 286        return (void *) (unsigned long) ptr;
 287}
 288
 289int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
 290int compare_map_keys(int map1_fd, int map2_fd);
 291int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
 292int extract_build_id(char *build_id, size_t size);
 293int kern_sync_rcu(void);
 294
 295#ifdef __x86_64__
 296#define SYS_NANOSLEEP_KPROBE_NAME "__x64_sys_nanosleep"
 297#elif defined(__s390x__)
 298#define SYS_NANOSLEEP_KPROBE_NAME "__s390x_sys_nanosleep"
 299#else
 300#define SYS_NANOSLEEP_KPROBE_NAME "sys_nanosleep"
 301#endif
 302