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.
12 #include <linux/if_packet.h>
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__) || \
37 # define CO_IN_CACHE_SHIFT 8
38 #elif defined(__powerpc__) || defined(__ppc__) || defined(__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__)
53 # define CO_IN_CACHE_SHIFT 5
54 # elif defined(_ABIN32)
55 # define CO_IN_CACHE_SHIFT 5
57 # define CO_IN_CACHE_SHIFT 6
60 # define CO_IN_CACHE_SHIFT 5
63 #ifndef CO_CACHE_LINE_SIZE
64 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
72 # define __aligned_16 __attribute__((aligned(16)))
75 #ifndef __cacheline_aligned
76 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
79 #ifndef __aligned_tpacket
80 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
83 #ifndef __align_tpacket
84 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
87 #ifndef __check_format_printf
88 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
89 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
93 # define __packed __attribute__((packed))
97 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
100 #ifndef round_up_cacheline
101 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
105 # define likely(x) __builtin_expect(!!(x), 1)
109 # define unlikely(x) __builtin_expect(!!(x), 0)
113 # define constant(x) __builtin_constant_p(x)
117 # define fmemset __builtin_memset
121 # define fmemcpy __builtin_memcpy
124 #ifndef atomic_cmp_swp
125 # define atomic_cmp_swp __sync_val_compare_and_swap
129 # define __deprecated /* unimplemented */
132 #ifndef EXPORT_SYMBOL
133 # define EXPORT_SYMBOL(x) /* empty, just for readability */
137 # define unreachable() do { } while (1)
141 # define __unused __attribute__ ((__unused__))
145 # define noinline __attribute__((noinline))
148 #ifndef __always_inline
149 # define __always_inline inline
153 # define __hidden __attribute__((visibility("hidden")))
157 # define __pure __attribute__ ((pure))
161 # define __force /* unimplemented */
165 # define force_cast(type, arg) ((type) (arg))
169 # define access_once(x) (*(volatile typeof(x) *) &(x))
175 typeof (a) _a = (a); \
176 typeof (b) _b = (b); \
184 typeof (a) _a = (a); \
185 typeof (b) _b = (b); \
191 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
195 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
199 # define container_of(ptr, type, member) \
201 const typeof(((type *) 0)->member) * __mptr = (ptr); \
202 (type *) ((char *) __mptr - offsetof(type, member)); \
207 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
210 #ifndef __must_be_array
211 # define __must_be_array(x) \
212 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
216 #ifndef build_bug_on_zero
217 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
221 # define bug_on(cond) assert(!(cond))
225 # define bug() assert(0)
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
)
238 static inline uint64_t ntohll(uint64_t x
)
242 #elif __BYTE_ORDER == __BIG_ENDIAN
243 static inline uint64_t htonll(uint64_t x
)
248 static inline uint64_t ntohll(uint64_t x
)
253 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
255 #ifndef ___constant_swab16
256 # define ___constant_swab16(x) ((__u16)( \
257 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
258 (((__u16)(x) & (__u16)0xff00U) >> 8)))
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)))
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
)
288 static inline u32
cpu_to_le32(u32 val
)
293 static inline u64
cpu_to_le64(u64 val
)
298 # ifndef __constant_htonl
299 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
301 # ifndef __constant_ntohl
302 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
304 # ifndef __constant_htons
305 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
307 # ifndef __constant_ntohs
308 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
310 #elif __BYTE_ORDER == __BIG_ENDIAN
311 static inline u16
cpu_to_be16(u16 val
)
316 static inline u32
cpu_to_be32(u32 val
)
321 static inline u64
cpu_to_be64(u64 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))
344 # ifndef __constant_ntohl
345 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
347 # ifndef __constant_htons
348 # define __constant_htons(x) ((__force __be16)(__u16)(x))
350 # ifndef __constant_ntohs
351 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
354 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
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
367 #define memset fmemset
368 #define memcpy fmemcpy
370 #endif /* BUILT_IN_H */