Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2015 Mellanox Technologies Ltd. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. Neither the name of the copyright holder nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 21 | * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 23 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #ifndef _IBVERBS_COMMON_H_ |
| 31 | #define _IBVERBS_COMMON_H_ |
Leon Romanovsky | 43f2205 | 2015-11-10 11:09:31 +0200 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
| 33 | #include "config.h" |
| 34 | #endif |
| 35 | |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 36 | #include <stdio.h> |
| 37 | #include <errno.h> |
Artemy Kovalyov | 36eea22 | 2017-10-04 01:27:27 +0300 | [diff] [blame] | 38 | #define __STDC_LIMIT_MACROS |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 39 | #include <stdint.h> |
| 40 | #define __STDC_FORMAT_MACROS |
| 41 | #include <inttypes.h> /* printf PRItn */ |
| 42 | #include <unistd.h> |
| 43 | #include <sys/time.h> |
| 44 | #include <sys/socket.h> |
| 45 | #include <sys/ioctl.h> |
| 46 | #include <sys/types.h> |
| 47 | #include <netinet/in.h> |
| 48 | #include <arpa/inet.h> |
| 49 | #include <pthread.h> |
| 50 | #include <inttypes.h> /* printf PRItn */ |
| 51 | #include <fcntl.h> |
| 52 | #include <poll.h> |
| 53 | #include <ctype.h> |
| 54 | #include <malloc.h> |
| 55 | #include <math.h> |
| 56 | #include <complex.h> |
| 57 | |
| 58 | #include "gtest.h" |
| 59 | |
| 60 | #define INLINE __inline |
| 61 | |
| 62 | #ifndef UNREFERENCED_PARAMETER |
| 63 | #define UNREFERENCED_PARAMETER(P) ((void)P) |
| 64 | #endif |
| 65 | |
| 66 | #define QUOTE(name) #name |
| 67 | #define STR(macro) QUOTE(macro) |
| 68 | |
| 69 | #define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0])) |
| 70 | |
| 71 | |
| 72 | /* Platform specific 16-byte alignment macro switch. |
| 73 | On Visual C++ it would substitute __declspec(align(16)). |
| 74 | On GCC it substitutes __attribute__((aligned (16))). |
| 75 | */ |
| 76 | |
| 77 | #if defined(_MSC_VER) |
| 78 | #define ALIGN(x) __declspec(align(x)) |
| 79 | #else |
| 80 | #define ALIGN(x) __attribute__((aligned (x))) |
| 81 | #endif |
| 82 | |
| 83 | #pragma pack( push, 1 ) |
| 84 | typedef struct _DATA128_TYPE |
| 85 | { |
| 86 | uint64_t field1; |
| 87 | uint64_t field2; |
| 88 | } DATA128_TYPE; |
| 89 | #pragma pack( pop ) |
| 90 | |
| 91 | |
| 92 | #if !defined( EOK ) |
| 93 | # define EOK 0 /* no error */ |
| 94 | #endif |
| 95 | |
| 96 | enum { |
Artemy Kovalyov | 8d83098 | 2017-03-09 12:22:04 +0200 | [diff] [blame] | 97 | GTEST_LOG_ERR = 1 << 0, |
| 98 | GTEST_LOG_NOTICE = 1 << 1, |
| 99 | GTEST_LOG_INFO = 1 << 2, |
| 100 | GTEST_LOG_TRACE = 1 << 3 |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | extern uint32_t gtest_debug_mask; |
| 104 | extern char *gtest_dev_name; |
| 105 | |
| 106 | |
Artemy Kovalyov | 8d83098 | 2017-03-09 12:22:04 +0200 | [diff] [blame] | 107 | #define VERBS_PRINT(level, color, fmt, ...) \ |
| 108 | do { \ |
| 109 | if (gtest_debug_mask & GTEST_LOG_ ## level) \ |
| 110 | printf("\033[0;3%dm" "[ %-8s ] " fmt "\033[m", \ |
| 111 | color, #level, ##__VA_ARGS__); \ |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 112 | } while(0) |
| 113 | |
Artemy Kovalyov | 8d83098 | 2017-03-09 12:22:04 +0200 | [diff] [blame] | 114 | #define VERBS_ERR(fmt, ...) \ |
| 115 | VERBS_PRINT(ERR, 1, fmt, ##__VA_ARGS__) |
| 116 | |
| 117 | #define VERBS_NOTICE(fmt, ...) \ |
| 118 | VERBS_PRINT(NOTICE, 3, fmt, ##__VA_ARGS__) |
| 119 | |
| 120 | #define VERBS_INFO(fmt, ...) \ |
| 121 | VERBS_PRINT(INFO, 4, fmt, ##__VA_ARGS__) |
| 122 | |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 123 | #define VERBS_TRACE(fmt, ...) \ |
Artemy Kovalyov | 8d83098 | 2017-03-09 12:22:04 +0200 | [diff] [blame] | 124 | VERBS_PRINT(TRACE, 7, fmt, ##__VA_ARGS__) |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 125 | |
Leon Romanovsky | 97a3b94 | 2015-11-10 12:03:44 +0200 | [diff] [blame] | 126 | #define CHECK_TEST_OR_SKIP(FEATURE_NAME) \ |
| 127 | do{\ |
| 128 | if(this->skip_this_test) {\ |
| 129 | std::cout << "[ SKIPPED ] Feature " << #FEATURE_NAME << " is not supported" << std::endl;\ |
Artemy Kovalyov | 207d074 | 2016-09-13 12:54:47 +0300 | [diff] [blame] | 130 | ::testing::UnitTest::GetInstance()->runtime_skip(); \ |
Leon Romanovsky | 97a3b94 | 2015-11-10 12:03:44 +0200 | [diff] [blame] | 131 | return;\ |
| 132 | }\ |
| 133 | } while(0) |
| 134 | |
| 135 | |
Leon Romanovsky | 5194084 | 2015-11-08 14:39:44 +0200 | [diff] [blame] | 136 | void sys_hexdump(void *ptr, int buflen); |
| 137 | uint32_t sys_inet_addr(char* ip); |
| 138 | |
| 139 | |
| 140 | static INLINE void sys_getenv(void) |
| 141 | { |
| 142 | char *env; |
| 143 | |
| 144 | env = getenv("IBV_TEST_MASK"); |
| 145 | if (env) |
| 146 | gtest_debug_mask = strtol(env, NULL, 0); |
| 147 | |
| 148 | env = getenv("IBV_TEST_DEV"); |
| 149 | gtest_dev_name = strdup(env ? env : "mlx"); |
| 150 | } |
| 151 | |
| 152 | static INLINE int sys_is_big_endian(void) |
| 153 | { |
| 154 | return( htonl(1) == 1 ); |
| 155 | } |
| 156 | |
| 157 | static INLINE double sys_gettime(void) |
| 158 | { |
| 159 | struct timeval tv; |
| 160 | gettimeofday(&tv, 0); |
| 161 | return (double)(tv.tv_sec * 1000000 + tv.tv_usec); |
| 162 | } |
| 163 | |
| 164 | static INLINE uint64_t sys_rdtsc(void) |
| 165 | { |
| 166 | unsigned long long int result=0; |
| 167 | |
| 168 | #if defined(__i386__) |
| 169 | __asm volatile(".byte 0x0f, 0x31" : "=A" (result) : ); |
| 170 | |
| 171 | #elif defined(__x86_64__) |
| 172 | unsigned hi, lo; |
| 173 | __asm volatile("rdtsc" : "=a"(lo), "=d"(hi)); |
| 174 | result = hi; |
| 175 | result = result<<32; |
| 176 | result = result|lo; |
| 177 | |
| 178 | #elif defined(__powerpc__) |
| 179 | unsigned long int hi, lo, tmp; |
| 180 | __asm volatile( |
| 181 | "0: \n\t" |
| 182 | "mftbu %0 \n\t" |
| 183 | "mftb %1 \n\t" |
| 184 | "mftbu %2 \n\t" |
| 185 | "cmpw %2,%0 \n\t" |
| 186 | "bne 0b \n" |
| 187 | : "=r"(hi),"=r"(lo),"=r"(tmp) |
| 188 | ); |
| 189 | result = hi; |
| 190 | result = result<<32; |
| 191 | result = result|lo; |
| 192 | |
| 193 | #endif |
| 194 | |
| 195 | return (result); |
| 196 | } |
| 197 | #endif //_IBVERBS_COMMON_H_ |