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 */
133 # define unreachable() do { } while (1)
136 #ifndef __maybe_unused
137 # define __maybe_unused __attribute__ ((__unused__))
141 # define noinline __attribute__((noinline))
144 #ifndef __always_inline
145 # define __always_inline inline
149 # define __hidden __attribute__((visibility("hidden")))
153 # define __pure __attribute__ ((pure))
157 # define __force /* unimplemented */
161 # define force_cast(type, arg) ((type) (arg))
165 # define access_once(x) (*(volatile typeof(x) *) &(x))
171 typeof (a) _a = (a); \
172 typeof (b) _b = (b); \
180 typeof (a) _a = (a); \
181 typeof (b) _b = (b); \
187 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
191 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
195 # define container_of(ptr, type, member) \
197 const typeof(((type *) 0)->member) * __mptr = (ptr); \
198 (type *) ((char *) __mptr - offsetof(type, member)); \
203 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
206 #ifndef __must_be_array
207 # define __must_be_array(x) \
208 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
212 #ifndef build_bug_on_zero
213 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
217 # define bug_on(cond) assert(!(cond))
221 # define bug() assert(0)
224 #define PAGE_SIZE (getpagesize())
225 #define PAGE_MASK (~(PAGE_SIZE - 1))
226 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
228 #if __BYTE_ORDER == __LITTLE_ENDIAN
229 static inline uint64_t htonll(uint64_t x
)
234 static inline uint64_t ntohll(uint64_t x
)
238 #elif __BYTE_ORDER == __BIG_ENDIAN
239 static inline uint64_t htonll(uint64_t x
)
244 static inline uint64_t ntohll(uint64_t x
)
249 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
251 #ifndef ___constant_swab16
252 # define ___constant_swab16(x) ((__u16)( \
253 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
254 (((__u16)(x) & (__u16)0xff00U) >> 8)))
256 #ifndef ___constant_swab32
257 # define ___constant_swab32(x) ((__u32)( \
258 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
259 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
260 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
261 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
263 #if __BYTE_ORDER == __LITTLE_ENDIAN
264 static inline u16
cpu_to_be16(u16 val
)
266 return bswap_16(val
);
269 static inline u32
cpu_to_be32(u32 val
)
271 return bswap_32(val
);
274 static inline u64
cpu_to_be64(u64 val
)
276 return bswap_64(val
);
279 static inline u16
cpu_to_le16(u16 val
)
284 static inline u32
cpu_to_le32(u32 val
)
289 static inline u64
cpu_to_le64(u64 val
)
294 # ifndef __constant_htonl
295 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
297 # ifndef __constant_ntohl
298 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
300 # ifndef __constant_htons
301 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
303 # ifndef __constant_ntohs
304 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
306 #elif __BYTE_ORDER == __BIG_ENDIAN
307 static inline u16
cpu_to_be16(u16 val
)
312 static inline u32
cpu_to_be32(u32 val
)
317 static inline u64
cpu_to_be64(u64 val
)
322 static inline u16
cpu_to_le16(u16 val
)
324 return bswap_16(val
);
327 static inline u32
cpu_to_le32(u32 val
)
329 return bswap_32(val
);
332 static inline u64
cpu_to_le64(u64 val
)
334 return bswap_64(val
);
337 # ifndef __constant_htonl
338 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
340 # ifndef __constant_ntohl
341 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
343 # ifndef __constant_htons
344 # define __constant_htons(x) ((__force __be16)(__u16)(x))
346 # ifndef __constant_ntohs
347 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
350 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
353 #define le64_to_cpu cpu_to_le64
354 #define le32_to_cpu cpu_to_le32
355 #define le16_to_cpu cpu_to_le16
356 #define be64_to_cpu cpu_to_be64
357 #define be32_to_cpu cpu_to_be32
358 #define be16_to_cpu cpu_to_be16
363 #define memset fmemset
364 #define memcpy fmemcpy
366 #endif /* BUILT_IN_H */