trafgen: give note about tc(8) usage
[netsniff-ng.git] / src / built_in.h
blob17bad9f3066f086a3b6f2973f505bb3a2078000e
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2012 Daniel Borkmann.
5 * Parts taken from the Linux kernel, GPL, version 2.
6 * Subject to the GPL, version 2.
7 */
9 #ifndef BUILT_IN_H
10 #define BUILT_IN_H
12 #include <linux/if_packet.h>
13 #include <assert.h>
14 #include <endian.h>
15 #include <byteswap.h>
16 #include <stdint.h>
18 typedef uint64_t u64;
19 typedef uint32_t u32;
20 typedef uint16_t u16;
21 typedef uint8_t u8;
23 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
25 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
26 defined(_M_X64) || defined(__amd64)
27 # define CO_IN_CACHE_SHIFT 7
28 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
29 defined(_M_IX86) || defined(__i386)
30 # define CO_IN_CACHE_SHIFT 7
31 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
32 # define CO_IN_CACHE_SHIFT 6
33 #elif defined(__SPU__)
34 # define CO_IN_CACHE_SHIFT 7
35 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
36 defined(_ARCH_PPC64)
37 # define CO_IN_CACHE_SHIFT 8
38 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
39 defined(_ARCH_PPC)
40 # define CO_IN_CACHE_SHIFT 7
41 #elif defined(__sparcv9__) || defined(__sparcv9)
42 # define CO_IN_CACHE_SHIFT 6
43 #elif defined(__sparc_v8__)
44 # define CO_IN_CACHE_SHIFT 5
45 #elif defined(__sparc__) || defined(__sparc)
46 # define CO_IN_CACHE_SHIFT 5
47 #elif defined(__ARM_EABI__)
48 # define CO_IN_CACHE_SHIFT 5
49 #elif defined(__arm__)
50 # define CO_IN_CACHE_SHIFT 5
51 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
52 # if defined(_ABIO32)
53 # define CO_IN_CACHE_SHIFT 5
54 # elif defined(_ABIN32)
55 # define CO_IN_CACHE_SHIFT 5
56 # else
57 # define CO_IN_CACHE_SHIFT 6
58 # endif
59 #else
60 # define CO_IN_CACHE_SHIFT 5
61 #endif
63 #ifndef CO_CACHE_LINE_SIZE
64 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
65 #endif
67 #ifndef MAX_CPUS
68 # define MAX_CPUS 32
69 #endif
71 #ifndef __aligned_16
72 # define __aligned_16 __attribute__((aligned(16)))
73 #endif
75 #ifndef __cacheline_aligned
76 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
77 #endif
79 #ifndef __aligned_tpacket
80 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
81 #endif
83 #ifndef __align_tpacket
84 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
85 #endif
87 #ifndef __check_format_printf
88 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
89 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
90 #endif
92 #ifndef __packed
93 # define __packed __attribute__((packed))
94 #endif
96 #ifndef round_up
97 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
98 #endif
100 #ifndef round_up_cacheline
101 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
102 #endif
104 #ifndef likely
105 # define likely(x) __builtin_expect(!!(x), 1)
106 #endif
108 #ifndef unlikely
109 # define unlikely(x) __builtin_expect(!!(x), 0)
110 #endif
112 #ifndef constant
113 # define constant(x) __builtin_constant_p(x)
114 #endif
116 #ifndef fmemset
117 # define fmemset __builtin_memset
118 #endif
120 #ifndef fmemcpy
121 # define fmemcpy __builtin_memcpy
122 #endif
124 #ifndef atomic_cmp_swp
125 # define atomic_cmp_swp __sync_val_compare_and_swap
126 #endif
128 #ifndef __deprecated
129 # define __deprecated /* unimplemented */
130 #endif
132 #ifndef EXPORT_SYMBOL
133 # define EXPORT_SYMBOL(x) /* empty, just for readability */
134 #endif
136 #ifndef unreachable
137 # define unreachable() do { } while (1)
138 #endif
140 #ifndef __unused
141 # define __unused __attribute__ ((__unused__))
142 #endif
144 #ifndef noinline
145 # define noinline __attribute__((noinline))
146 #endif
148 #ifndef __always_inline
149 # define __always_inline inline
150 #endif
152 #ifndef __hidden
153 # define __hidden __attribute__((visibility("hidden")))
154 #endif
156 #ifndef __pure
157 # define __pure __attribute__ ((pure))
158 #endif
160 #ifndef __force
161 # define __force /* unimplemented */
162 #endif
164 #ifndef force_cast
165 # define force_cast(type, arg) ((type) (arg))
166 #endif
168 #ifndef access_once
169 # define access_once(x) (*(volatile typeof(x) *) &(x))
170 #endif
172 #ifndef max
173 # define max(a, b) \
174 ({ \
175 typeof (a) _a = (a); \
176 typeof (b) _b = (b); \
177 _a > _b ? _a : _b; \
179 #endif /* max */
181 #ifndef min
182 # define min(a, b) \
183 ({ \
184 typeof (a) _a = (a); \
185 typeof (b) _b = (b); \
186 _a < _b ? _a : _b; \
188 #endif /* min */
190 #ifndef ispow2
191 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
192 #endif
194 #ifndef offsetof
195 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
196 #endif
198 #ifndef container_of
199 # define container_of(ptr, type, member) \
200 ({ \
201 const typeof(((type *) 0)->member) * __mptr = (ptr); \
202 (type *) ((char *) __mptr - offsetof(type, member)); \
204 #endif
206 #ifndef array_size
207 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
208 #endif
210 #ifndef __must_be_array
211 # define __must_be_array(x) \
212 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
213 typeof(&x[0])))
214 #endif
216 #ifndef build_bug_on_zero
217 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
218 #endif
220 #ifndef bug_on
221 # define bug_on(cond) assert(!(cond))
222 #endif
224 #ifndef bug
225 # define bug() assert(0)
226 #endif
228 #define PAGE_SIZE (getpagesize())
229 #define PAGE_MASK (~(PAGE_SIZE - 1))
230 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
232 #if __BYTE_ORDER == __LITTLE_ENDIAN
233 static inline uint64_t htonll(uint64_t x)
235 return bswap_64(x);
238 static inline uint64_t ntohll(uint64_t x)
240 return bswap_64(x);
242 #elif __BYTE_ORDER == __BIG_ENDIAN
243 static inline uint64_t htonll(uint64_t x)
245 return x;
248 static inline uint64_t ntohll(uint64_t x)
250 return x;
252 #else
253 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
254 #endif
255 #ifndef ___constant_swab16
256 # define ___constant_swab16(x) ((__u16)( \
257 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
258 (((__u16)(x) & (__u16)0xff00U) >> 8)))
259 #endif
260 #ifndef ___constant_swab32
261 # define ___constant_swab32(x) ((__u32)( \
262 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
263 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
264 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
265 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
266 #endif
267 #if __BYTE_ORDER == __LITTLE_ENDIAN
268 static inline u16 cpu_to_be16(u16 val)
270 return bswap_16(val);
273 static inline u32 cpu_to_be32(u32 val)
275 return bswap_32(val);
278 static inline u64 cpu_to_be64(u64 val)
280 return bswap_64(val);
283 static inline u16 cpu_to_le16(u16 val)
285 return val;
288 static inline u32 cpu_to_le32(u32 val)
290 return val;
293 static inline u64 cpu_to_le64(u64 val)
295 return val;
298 # ifndef __constant_htonl
299 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
300 # endif
301 # ifndef __constant_ntohl
302 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
303 # endif
304 # ifndef __constant_htons
305 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
306 # endif
307 # ifndef __constant_ntohs
308 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
309 # endif
310 #elif __BYTE_ORDER == __BIG_ENDIAN
311 static inline u16 cpu_to_be16(u16 val)
313 return val;
316 static inline u32 cpu_to_be32(u32 val)
318 return val;
321 static inline u64 cpu_to_be64(u64 val)
323 return val;
326 static inline u16 cpu_to_le16(u16 val)
328 return bswap_16(val);
331 static inline u32 cpu_to_le32(u32 val)
333 return bswap_32(val);
336 static inline u64 cpu_to_le64(u64 val)
338 return bswap_64(val);
341 # ifndef __constant_htonl
342 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
343 # endif
344 # ifndef __constant_ntohl
345 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
346 # endif
347 # ifndef __constant_htons
348 # define __constant_htons(x) ((__force __be16)(__u16)(x))
349 # endif
350 # ifndef __constant_ntohs
351 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
352 # endif
353 #else
354 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
355 #endif
357 #define le64_to_cpu cpu_to_le64
358 #define le32_to_cpu cpu_to_le32
359 #define le16_to_cpu cpu_to_le16
360 #define be64_to_cpu cpu_to_be64
361 #define be32_to_cpu cpu_to_be32
362 #define be16_to_cpu cpu_to_be16
364 #undef memset
365 #undef memcpy
367 #define memset fmemset
368 #define memcpy fmemcpy
370 #endif /* BUILT_IN_H */