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>
17 /* /sys/devices/system/cpu/cpuX/cache/indexX/coherency_line_size */
19 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
20 defined(_M_X64) || defined(__amd64)
21 # define CO_IN_CACHE_SHIFT 7
22 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
23 defined(_M_IX86) || defined(__i386)
24 # define CO_IN_CACHE_SHIFT 7
25 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
26 # define CO_IN_CACHE_SHIFT 6
27 #elif defined(__SPU__)
28 # define CO_IN_CACHE_SHIFT 7
29 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
31 # define CO_IN_CACHE_SHIFT 8
32 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
34 # define CO_IN_CACHE_SHIFT 7
35 #elif defined(__sparcv9__) || defined(__sparcv9)
36 # define CO_IN_CACHE_SHIFT 6
37 #elif defined(__sparc_v8__)
38 # define CO_IN_CACHE_SHIFT 5
39 #elif defined(__sparc__) || defined(__sparc)
40 # define CO_IN_CACHE_SHIFT 5
41 #elif defined(__ARM_EABI__)
42 # define CO_IN_CACHE_SHIFT 5
43 #elif defined(__arm__)
44 # define CO_IN_CACHE_SHIFT 5
45 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
47 # define CO_IN_CACHE_SHIFT 5
48 # elif defined(_ABIN32)
49 # define CO_IN_CACHE_SHIFT 5
51 # define CO_IN_CACHE_SHIFT 6
54 # define CO_IN_CACHE_SHIFT 5
57 #ifndef CO_CACHE_LINE_SIZE
58 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
62 # define __aligned_16 __attribute__((aligned(16)))
65 #ifndef __cacheline_aligned
66 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
69 #ifndef __aligned_tpacket
70 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
74 # define __packed __attribute__((packed))
78 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
81 #ifndef round_up_cacheline
82 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
86 # define likely(x) __builtin_expect(!!(x), 1)
90 # define unlikely(x) __builtin_expect(!!(x), 0)
93 #ifndef prefetch_rd_hi
94 # define prefetch_rd_hi(addr) __builtin_prefetch(addr, 0, 3)
97 #ifndef prefetch_rd_lo
98 # define prefetch_rd_lo(addr) __builtin_prefetch(addr, 0, 0)
101 #ifndef prefetch_wr_hi
102 # define prefetch_wr_hi(addr) __builtin_prefetch(addr, 1, 3)
105 #ifndef prefetch_wr_lo
106 # define prefetch_wr_lo(addr) __builtin_prefetch(addr, 1, 0)
110 # define fmemset __builtin_memset
114 # define fmemcpy __builtin_memcpy
117 #ifndef atomic_cmp_swp
118 # define atomic_cmp_swp __sync_val_compare_and_swap
122 # define __deprecated /* unimplemented */
126 # define __export /* unimplemented */
130 # define __import extern
134 # define unreachable() do { } while (1)
137 #ifndef __read_mostly
138 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
142 # define __unused __attribute__ ((__unused__))
146 # define noinline __attribute__((noinline))
149 #ifndef __always_inline
150 # define __always_inline inline
154 # define __hidden __attribute__((visibility("hidden")))
158 # define __pure __attribute__ ((pure))
162 # define force_cast(type, arg) ((type) (arg))
166 # define access_once(x) (*(volatile typeof(x) *) &(x))
172 typeof (a) _a = (a); \
173 typeof (b) _b = (b); \
181 typeof (a) _a = (a); \
182 typeof (b) _b = (b); \
188 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
192 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
196 # define container_of(ptr, type, member) \
198 const typeof(((type *) 0)->member) * __mptr = (ptr); \
199 (type *) ((char *) __mptr - offsetof(type, member)); \
204 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
207 #ifndef __must_be_array
208 # define __must_be_array(x) \
209 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
213 #ifndef build_bug_on_zero
214 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
218 # define bug_on(cond) assert(!(cond))
222 # define bug assert(0)
225 #if __BYTE_ORDER == __LITTLE_ENDIAN
226 static inline uint64_t htonll(uint64_t x
)
231 static inline uint64_t ntohll(uint64_t x
)
235 #elif __BYTE_ORDER == __BIG_ENDIAN
236 static inline uint64_t htonll(uint64_t x
)
241 static inline uint64_t ntohll(uint64_t x
)
246 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
249 #endif /* BUILT_IN_H */