netsniff-ng: remove set_sockopt_hwtimestamp call
[netsniff-ng.git] / built_in.h
blobee97c749581784f0ef2906645d513b595cc0cbc9
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2009-2012 Daniel Borkmann.
4 * Parts taken from the Linux kernel, GPL, version 2.
5 * Subject to the GPL, version 2.
6 */
8 #ifndef BUILT_IN_H
9 #define BUILT_IN_H
11 #include <linux/if_packet.h>
12 #include <assert.h>
13 #include <endian.h>
14 #include <byteswap.h>
15 #include <asm/byteorder.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 #ifndef CO_CACHE_LINE_SIZE
24 # define CO_CACHE_LINE_SIZE (1 << CO_IN_CACHE_SHIFT)
25 #endif
27 #ifndef MAX_CPUS
28 # define MAX_CPUS 32
29 #endif
31 #ifndef __aligned_16
32 # define __aligned_16 __attribute__((aligned(16)))
33 #endif
35 #ifndef __cacheline_aligned
36 # define __cacheline_aligned __attribute__((aligned(CO_CACHE_LINE_SIZE)))
37 #endif
39 #ifndef __aligned_tpacket
40 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
41 #endif
43 #ifndef __align_tpacket
44 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
45 #endif
47 #ifndef __check_format_printf
48 # define __check_format_printf(pos_fmtstr, pos_fmtargs) \
49 __attribute__ ((format (printf, (pos_fmtstr), (pos_fmtargs))))
50 #endif
52 #ifndef __packed
53 # define __packed __attribute__((packed))
54 #endif
56 #ifndef round_up
57 # define round_up(x, alignment) (((x) + (alignment) - 1) & ~((alignment) - 1))
58 #endif
60 #ifndef round_up_cacheline
61 # define round_up_cacheline(x) round_up((x), CO_CACHE_LINE_SIZE)
62 #endif
64 #ifndef likely
65 # define likely(x) __builtin_expect(!!(x), 1)
66 #endif
68 #ifndef unlikely
69 # define unlikely(x) __builtin_expect(!!(x), 0)
70 #endif
72 #ifndef constant
73 # define constant(x) __builtin_constant_p(x)
74 #endif
76 #ifndef fmemset
77 # define fmemset __builtin_memset
78 #endif
80 #ifndef fmemcpy
81 # define fmemcpy __builtin_memcpy
82 #endif
84 #ifndef __maybe_unused
85 # define __maybe_unused __attribute__ ((__unused__))
86 #endif
88 #ifndef noinline
89 # define noinline __attribute__((noinline))
90 #endif
92 #ifndef __hidden
93 # define __hidden __attribute__((visibility("hidden")))
94 #endif
96 #ifndef __pure
97 # define __pure __attribute__ ((pure))
98 #endif
100 #ifndef __force
101 # define __force /* unimplemented */
102 #endif
104 #ifndef force_cast
105 # define force_cast(type, arg) ((type) (arg))
106 #endif
108 #ifndef max
109 # define max(a, b) \
110 ({ \
111 typeof (a) _a = (a); \
112 typeof (b) _b = (b); \
113 _a > _b ? _a : _b; \
115 #endif /* max */
117 #ifndef min
118 # define min(a, b) \
119 ({ \
120 typeof (a) _a = (a); \
121 typeof (b) _b = (b); \
122 _a < _b ? _a : _b; \
124 #endif /* min */
126 #ifndef ispow2
127 # define ispow2(x) ({ !!((x) && !((x) & ((x) - 1))); })
128 #endif
130 #ifndef offsetof
131 # define offsetof(type, member) ((size_t) &((type *) 0)->member)
132 #endif
134 #ifndef container_of
135 # define container_of(ptr, type, member) \
136 ({ \
137 const typeof(((type *) 0)->member) * __mptr = (ptr); \
138 (type *) ((char *) __mptr - offsetof(type, member)); \
140 #endif
142 #ifndef array_size
143 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
144 #endif
146 #ifndef __must_be_array
147 # define __must_be_array(x) \
148 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), \
149 typeof(&x[0])))
150 #endif
152 #ifndef build_bug_on_zero
153 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
154 #endif
156 #ifndef bug_on
157 # define bug_on(cond) assert(!(cond))
158 #endif
160 #ifndef bug
161 # define bug() assert(0)
162 #endif
164 #define PAGE_SIZE (getpagesize())
165 #define PAGE_MASK (~(PAGE_SIZE - 1))
166 #define PAGE_ALIGN(addr) (((addr) + PAGE_SIZE - 1) & PAGE_MASK)
168 #if __BYTE_ORDER == __LITTLE_ENDIAN
169 static inline uint64_t htonll(uint64_t x)
171 return bswap_64(x);
174 static inline uint64_t ntohll(uint64_t x)
176 return bswap_64(x);
178 #elif __BYTE_ORDER == __BIG_ENDIAN
179 static inline uint64_t htonll(uint64_t x)
181 return x;
184 static inline uint64_t ntohll(uint64_t x)
186 return x;
188 #else
189 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
190 #endif
191 #ifndef ___constant_swab16
192 # define ___constant_swab16(x) ((__u16)( \
193 (((__u16)(x) & (__u16)0x00ffU) << 8) | \
194 (((__u16)(x) & (__u16)0xff00U) >> 8)))
195 #endif
196 #ifndef ___constant_swab32
197 # define ___constant_swab32(x) ((__u32)( \
198 (((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
199 (((__u32)(x) & (__u32)0x0000ff00UL) << 8) | \
200 (((__u32)(x) & (__u32)0x00ff0000UL) >> 8) | \
201 (((__u32)(x) & (__u32)0xff000000UL) >> 24)))
202 #endif
203 #if __BYTE_ORDER == __LITTLE_ENDIAN
204 static inline u16 cpu_to_be16(u16 val)
206 return bswap_16(val);
209 static inline u32 cpu_to_be32(u32 val)
211 return bswap_32(val);
214 static inline u64 cpu_to_be64(u64 val)
216 return bswap_64(val);
219 static inline u16 cpu_to_le16(u16 val)
221 return val;
224 static inline u32 cpu_to_le32(u32 val)
226 return val;
229 static inline u64 cpu_to_le64(u64 val)
231 return val;
234 # ifndef __constant_htonl
235 # define __constant_htonl(x) ((__force __be32)___constant_swab32((x)))
236 # endif
237 # ifndef __constant_ntohl
238 # define __constant_ntohl(x) ___constant_swab32((__force __be32)(x))
239 # endif
240 # ifndef __constant_htons
241 # define __constant_htons(x) ((__force __be16)___constant_swab16((x)))
242 # endif
243 # ifndef __constant_ntohs
244 # define __constant_ntohs(x) ___constant_swab16((__force __be16)(x))
245 # endif
246 #elif __BYTE_ORDER == __BIG_ENDIAN
247 static inline u16 cpu_to_be16(u16 val)
249 return val;
252 static inline u32 cpu_to_be32(u32 val)
254 return val;
257 static inline u64 cpu_to_be64(u64 val)
259 return val;
262 static inline u16 cpu_to_le16(u16 val)
264 return bswap_16(val);
267 static inline u32 cpu_to_le32(u32 val)
269 return bswap_32(val);
272 static inline u64 cpu_to_le64(u64 val)
274 return bswap_64(val);
277 # ifndef __constant_htonl
278 # define __constant_htonl(x) ((__force __be32)(__u32)(x))
279 # endif
280 # ifndef __constant_ntohl
281 # define __constant_ntohl(x) ((__force __u32)(__be32)(x))
282 # endif
283 # ifndef __constant_htons
284 # define __constant_htons(x) ((__force __be16)(__u16)(x))
285 # endif
286 # ifndef __constant_ntohs
287 # define __constant_ntohs(x) ((__force __u16)(__be16)(x))
288 # endif
289 #else
290 # error __BYTE_ORDER is neither __LITTLE_ENDIAN nor __BIG_ENDIAN
291 #endif
293 #define le64_to_cpu cpu_to_le64
294 #define le32_to_cpu cpu_to_le32
295 #define le16_to_cpu cpu_to_le16
296 #define be64_to_cpu cpu_to_be64
297 #define be32_to_cpu cpu_to_be32
298 #define be16_to_cpu cpu_to_be16
300 #undef memset
301 #undef memcpy
303 #define memset fmemset
304 #define memcpy fmemcpy
306 #if defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
307 defined(_M_X64) || defined(__amd64)
308 # define CO_IN_CACHE_SHIFT 7
309 #elif defined(__i386__) || defined(__x86__) || defined(__X86__) || \
310 defined(_M_IX86) || defined(__i386)
311 # define CO_IN_CACHE_SHIFT 7
312 #elif defined(__ia64__) || defined(__IA64__) || defined(__M_IA64)
313 # define CO_IN_CACHE_SHIFT 6
314 #elif defined(__SPU__)
315 # define CO_IN_CACHE_SHIFT 7
316 #elif defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__) || \
317 defined(_ARCH_PPC64)
318 # define CO_IN_CACHE_SHIFT 8
319 #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) || \
320 defined(_ARCH_PPC)
321 # define CO_IN_CACHE_SHIFT 7
322 #elif defined(__sparcv9__) || defined(__sparcv9)
323 # define CO_IN_CACHE_SHIFT 6
324 #elif defined(__sparc_v8__)
325 # define CO_IN_CACHE_SHIFT 5
326 #elif defined(__sparc__) || defined(__sparc)
327 # define CO_IN_CACHE_SHIFT 5
328 #elif defined(__ARM_EABI__)
329 # define CO_IN_CACHE_SHIFT 5
330 #elif defined(__arm__)
331 # define CO_IN_CACHE_SHIFT 5
332 #elif defined(__mips__) || defined(__mips) || defined(__MIPS__)
333 # if defined(_ABIO32)
334 # define CO_IN_CACHE_SHIFT 5
335 # elif defined(_ABIN32)
336 # define CO_IN_CACHE_SHIFT 5
337 # else
338 # define CO_IN_CACHE_SHIFT 6
339 # endif
340 #else
341 # define CO_IN_CACHE_SHIFT 5
342 #endif
344 #endif /* BUILT_IN_H */