2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2009-2012 Daniel Borkmann.
5 * Subject to the GPL, version 2.
11 #include <linux/if_packet.h>
14 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
16 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
17 defined(_M_X64) || defined(__amd64)
18 # define CO_IN_CACHE_SHIFT 7
19 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
20 defined(_M_IX86) || defined(__i386)
21 # define CO_IN_CACHE_SHIFT 7
22 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
23 # define CO_IN_CACHE_SHIFT 6
24 #elif defined(__SPU__)
25 # define CO_IN_CACHE_SHIFT 7
26 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
28 # define CO_IN_CACHE_SHIFT 8
29 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
31 # define CO_IN_CACHE_SHIFT 7
32 #elif defined(__sparcv9__) || defined(__sparcv9)
33 # define CO_IN_CACHE_SHIFT 6
34 #elif defined(__sparc_v8__)
35 # define CO_IN_CACHE_SHIFT 5
36 #elif defined(__sparc__) || defined(__sparc)
37 # define CO_IN_CACHE_SHIFT 5
38 #elif defined(__ARM_EABI__)
39 # define CO_IN_CACHE_SHIFT 5
40 #elif defined(__arm__)
41 # define CO_IN_CACHE_SHIFT 5
42 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
44 # define CO_IN_CACHE_SHIFT 5
45 # elif defined(_ABIN32)
46 # define CO_IN_CACHE_SHIFT 5
48 # define CO_IN_CACHE_SHIFT 6
51 # define CO_IN_CACHE_SHIFT 5
54 #ifndef CO_CACHE_LINE_SIZE
55 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
59 # define __aligned_16 __attribute__((aligned(16)))
62 #ifndef __cacheline_aligned
63 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
66 #ifndef __aligned_tpacket
67 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
71 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
74 #ifndef round_up_cacheline
75 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
79 # define likely(x) __builtin_expect(!!(x), 1)
83 # define unlikely(x) __builtin_expect(!!(x), 0)
86 #ifndef prefetch_rd_hi
87 # define prefetch_rd_hi(addr) __builtin_prefetch(addr, 0, 3)
90 #ifndef prefetch_rd_lo
91 # define prefetch_rd_lo(addr) __builtin_prefetch(addr, 0, 0)
94 #ifndef prefetch_wr_hi
95 # define prefetch_wr_hi(addr) __builtin_prefetch(addr, 1, 3)
98 #ifndef prefetch_wr_lo
99 # define prefetch_wr_lo(addr) __builtin_prefetch(addr, 1, 0)
103 # define fmemset __builtin_memset
107 # define fmemcpy __builtin_memcpy
110 #ifndef atomic_cmp_swp
111 # define atomic_cmp_swp __sync_val_compare_and_swap
115 # define __deprecated /* unimplemented */
119 # define unreachable() do { } while (1)
122 #ifndef __read_mostly
123 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
127 # define noinline __attribute__((noinline))
130 #ifndef __always_inline
131 # define __always_inline inline
135 # define __hidden __attribute__((visibility("hidden")))
139 # define __pure __attribute__ ((pure))
143 # define force_cast(type, arg) ((type) (arg))
147 # define access_once(x) (*(volatile typeof(x) *) &(x))
153 typeof (a) _a = (a); \
154 typeof (b) _b = (b); \
162 typeof (a) _a = (a); \
163 typeof (b) _b = (b); \
169 #define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
173 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
177 # define container_of(ptr, type, member) \
179 const typeof(((type *) 0)->member) * __mptr = (ptr); \
180 (type *) ((char *) __mptr - offsetof(type, member)); \
185 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
188 #ifndef __must_be_array
189 # define __must_be_array(x) \
190 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
194 #ifndef build_bug_on_zero
195 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
199 # define bug_on(cond) assert(!(cond))
203 # define bug assert(0)
206 #endif /* BUILT_IN_H */