]>
Commit | Line | Data |
---|---|---|
064af421 | 1 | /* |
b54d69df | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc. |
064af421 | 3 | * |
a14bc59f BP |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. | |
6 | * You may obtain a copy of the License at: | |
064af421 | 7 | * |
a14bc59f BP |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * | |
10 | * Unless required by applicable law or agreed to in writing, software | |
11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | * See the License for the specific language governing permissions and | |
14 | * limitations under the License. | |
064af421 BP |
15 | */ |
16 | ||
17 | #ifndef UTIL_H | |
18 | #define UTIL_H 1 | |
19 | ||
b2fc6a74 | 20 | #include <arpa/inet.h> |
437d0d22 | 21 | #include <inttypes.h> |
a656cb77 | 22 | #include <limits.h> |
064af421 | 23 | #include <stdio.h> |
ccc09689 | 24 | #include <stdlib.h> |
064af421 BP |
25 | #include <string.h> |
26 | #include "compiler.h" | |
b0248b2a | 27 | #include "openvswitch/util.h" |
064af421 | 28 | |
91e12f0d | 29 | extern char *program_name; |
064af421 | 30 | |
878f1972 FL |
31 | #define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0])) |
32 | #ifdef __GNUC__ | |
33 | /* return 0 for array types, 1 otherwise */ | |
34 | #define __ARRAY_CHECK(ARRAY) \ | |
35 | !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0])) | |
36 | ||
37 | /* compile-time fail if not array */ | |
38 | #define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)])) | |
39 | #define __ARRAY_SIZE(ARRAY) \ | |
40 | __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \ | |
41 | __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY)) | |
42 | #else | |
43 | #define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY) | |
44 | #endif | |
45 | ||
e93ab553 | 46 | |
e2051008 BP |
47 | /* This system's cache line size, in bytes. |
48 | * Being wrong hurts performance but not correctness. */ | |
49 | #define CACHE_LINE_SIZE 64 | |
50 | BUILD_ASSERT_DECL(IS_POW2(CACHE_LINE_SIZE)); | |
51 | ||
124f09c9 JR |
52 | static inline void |
53 | ovs_prefetch_range(const void *start, size_t size) | |
54 | { | |
55 | const char *addr = (const char *)start; | |
56 | size_t ofs; | |
57 | ||
58 | for (ofs = 0; ofs < size; ofs += CACHE_LINE_SIZE) { | |
59 | OVS_PREFETCH(addr + ofs); | |
60 | } | |
61 | } | |
62 | ||
064af421 BP |
63 | #ifndef MIN |
64 | #define MIN(X, Y) ((X) < (Y) ? (X) : (Y)) | |
65 | #endif | |
66 | ||
67 | #ifndef MAX | |
68 | #define MAX(X, Y) ((X) > (Y) ? (X) : (Y)) | |
69 | #endif | |
70 | ||
428b2edd | 71 | #define OVS_NOT_REACHED() abort() |
064af421 | 72 | |
34582733 AS |
73 | /* Use "%"PRIuSIZE to format size_t with printf(). */ |
74 | #ifdef _WIN32 | |
75 | #define PRIdSIZE "Id" | |
76 | #define PRIiSIZE "Ii" | |
77 | #define PRIoSIZE "Io" | |
78 | #define PRIuSIZE "Iu" | |
79 | #define PRIxSIZE "Ix" | |
80 | #define PRIXSIZE "IX" | |
81 | #else | |
82 | #define PRIdSIZE "zd" | |
83 | #define PRIiSIZE "zi" | |
84 | #define PRIoSIZE "zo" | |
85 | #define PRIuSIZE "zu" | |
86 | #define PRIxSIZE "zx" | |
87 | #define PRIXSIZE "zX" | |
88 | #endif | |
89 | ||
b57c6aaa BP |
90 | #ifndef _WIN32 |
91 | typedef uint32_t HANDLE; | |
92 | #endif | |
93 | ||
064af421 BP |
94 | #ifdef __cplusplus |
95 | extern "C" { | |
96 | #endif | |
97 | ||
55d5bb44 | 98 | #define set_program_name(name) \ |
b0248b2a | 99 | ovs_set_program_name(name, OVS_PACKAGE_VERSION) |
064af421 | 100 | |
bc9fb3a9 | 101 | const char *get_subprogram_name(void); |
40e7cf56 | 102 | void set_subprogram_name(const char *); |
bc9fb3a9 | 103 | |
55d5bb44 | 104 | void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp); |
064af421 | 105 | |
cab50449 | 106 | OVS_NO_RETURN void out_of_memory(void); |
064af421 BP |
107 | void *xmalloc(size_t) MALLOC_LIKE; |
108 | void *xcalloc(size_t, size_t) MALLOC_LIKE; | |
ec6fde61 | 109 | void *xzalloc(size_t) MALLOC_LIKE; |
064af421 BP |
110 | void *xrealloc(void *, size_t); |
111 | void *xmemdup(const void *, size_t) MALLOC_LIKE; | |
112 | char *xmemdup0(const char *, size_t) MALLOC_LIKE; | |
113 | char *xstrdup(const char *) MALLOC_LIKE; | |
cab50449 TG |
114 | char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE; |
115 | char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE; | |
064af421 BP |
116 | void *x2nrealloc(void *p, size_t *n, size_t s); |
117 | ||
2fec66db BP |
118 | void *xmalloc_cacheline(size_t) MALLOC_LIKE; |
119 | void *xzalloc_cacheline(size_t) MALLOC_LIKE; | |
120 | void free_cacheline(void *); | |
121 | ||
064af421 | 122 | void ovs_strlcpy(char *dst, const char *src, size_t size); |
71d7c22f | 123 | void ovs_strzcpy(char *dst, const char *src, size_t size); |
064af421 | 124 | |
cab50449 TG |
125 | OVS_NO_RETURN void ovs_abort(int err_no, const char *format, ...) |
126 | OVS_PRINTF_FORMAT(2, 3); | |
127 | OVS_NO_RETURN void ovs_abort_valist(int err_no, const char *format, va_list) | |
128 | OVS_PRINTF_FORMAT(2, 0); | |
129 | OVS_NO_RETURN void ovs_fatal(int err_no, const char *format, ...) | |
130 | OVS_PRINTF_FORMAT(2, 3); | |
131 | OVS_NO_RETURN void ovs_fatal_valist(int err_no, const char *format, va_list) | |
132 | OVS_PRINTF_FORMAT(2, 0); | |
133 | void ovs_error(int err_no, const char *format, ...) OVS_PRINTF_FORMAT(2, 3); | |
c1c8308a | 134 | void ovs_error_valist(int err_no, const char *format, va_list) |
cab50449 | 135 | OVS_PRINTF_FORMAT(2, 0); |
c18ea70d | 136 | const char *ovs_retval_to_string(int); |
5fcbed74 | 137 | const char *ovs_strerror(int); |
064af421 BP |
138 | void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii); |
139 | ||
140 | bool str_to_int(const char *, int base, int *); | |
141 | bool str_to_long(const char *, int base, long *); | |
142 | bool str_to_llong(const char *, int base, long long *); | |
4c21aa06 | 143 | bool str_to_uint(const char *, int base, unsigned int *); |
064af421 | 144 | |
cab50449 | 145 | bool ovs_scan(const char *s, const char *format, ...) OVS_SCANF_FORMAT(2, 3); |
f071cbba | 146 | bool ovs_scan_len(const char *s, int *n, const char *format, ...); |
ed2232fc | 147 | |
f38b84ea BP |
148 | bool str_to_double(const char *, double *); |
149 | ||
150 | int hexit_value(int c); | |
0429d959 | 151 | uintmax_t hexits_value(const char *s, size_t n, bool *ok); |
f38b84ea | 152 | |
e7ae59f9 JG |
153 | int parse_int_string(const char *s, uint8_t *valuep, int field_width, |
154 | char **tail); | |
155 | ||
44b4d050 BP |
156 | const char *english_list_delimiter(size_t index, size_t total); |
157 | ||
daf03c53 | 158 | char *get_cwd(void); |
3c1150ce | 159 | #ifndef _WIN32 |
29d4af60 | 160 | char *dir_name(const char *file_name); |
e1aff6f9 | 161 | char *base_name(const char *file_name); |
3c1150ce | 162 | #endif |
daf03c53 | 163 | char *abs_file_name(const char *dir, const char *file_name); |
29d4af60 | 164 | |
fee0c963 BP |
165 | char *follow_symlinks(const char *filename); |
166 | ||
c69ee87c | 167 | void ignore(bool x OVS_UNUSED); |
fe9d0898 BP |
168 | \f |
169 | /* Bitwise tests. */ | |
0ee140fb | 170 | |
d43d314e BP |
171 | /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */ |
172 | #if __GNUC__ >= 4 | |
0ee140fb | 173 | static inline int |
d43d314e | 174 | raw_ctz(uint64_t n) |
0ee140fb | 175 | { |
d43d314e BP |
176 | /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using |
177 | * a plain __builtin_ctzll() here always generates an out-of-line function | |
178 | * call. The test below helps it to emit a single 'bsf' instruction. */ | |
179 | return (__builtin_constant_p(n <= UINT32_MAX) && n <= UINT32_MAX | |
180 | ? __builtin_ctz(n) | |
181 | : __builtin_ctzll(n)); | |
0ee140fb | 182 | } |
8c947903 JR |
183 | |
184 | static inline int | |
185 | raw_clz64(uint64_t n) | |
186 | { | |
187 | return __builtin_clzll(n); | |
188 | } | |
25f45143 GS |
189 | #elif _MSC_VER |
190 | static inline int | |
191 | raw_ctz(uint64_t n) | |
192 | { | |
193 | #ifdef _WIN64 | |
99970e95 | 194 | unsigned long r = 0; |
25f45143 GS |
195 | _BitScanForward64(&r, n); |
196 | return r; | |
197 | #else | |
99970e95 | 198 | unsigned long low = n, high, r = 0; |
25f45143 GS |
199 | if (_BitScanForward(&r, low)) { |
200 | return r; | |
201 | } | |
202 | high = n >> 32; | |
203 | _BitScanForward(&r, high); | |
204 | return r + 32; | |
205 | #endif | |
206 | } | |
207 | ||
208 | static inline int | |
209 | raw_clz64(uint64_t n) | |
210 | { | |
211 | #ifdef _WIN64 | |
99970e95 | 212 | unsigned long r = 0; |
25f45143 GS |
213 | _BitScanReverse64(&r, n); |
214 | return 63 - r; | |
215 | #else | |
99970e95 | 216 | unsigned long low, high = n >> 32, r = 0; |
25f45143 GS |
217 | if (_BitScanReverse(&r, high)) { |
218 | return 31 - r; | |
219 | } | |
220 | low = n; | |
221 | _BitScanReverse(&r, low); | |
222 | return 63 - r; | |
223 | #endif | |
224 | } | |
0ee140fb BP |
225 | #else |
226 | /* Defined in util.c. */ | |
d43d314e | 227 | int raw_ctz(uint64_t n); |
8c947903 | 228 | int raw_clz64(uint64_t n); |
0ee140fb BP |
229 | #endif |
230 | ||
231 | /* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */ | |
232 | static inline int | |
d578065e | 233 | ctz32(uint32_t n) |
0ee140fb BP |
234 | { |
235 | return n ? raw_ctz(n) : 32; | |
236 | } | |
237 | ||
cc4c738e JR |
238 | /* Returns the number of trailing 0-bits in 'n', or 64 if 'n' is 0. */ |
239 | static inline int | |
240 | ctz64(uint64_t n) | |
241 | { | |
d43d314e | 242 | return n ? raw_ctz(n) : 64; |
cc4c738e | 243 | } |
18b9283b | 244 | |
8c947903 JR |
245 | /* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */ |
246 | static inline int | |
247 | clz32(uint32_t n) | |
248 | { | |
249 | return n ? raw_clz64(n) - 32 : 32; | |
250 | } | |
251 | ||
252 | /* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */ | |
253 | static inline int | |
254 | clz64(uint64_t n) | |
255 | { | |
256 | return n ? raw_clz64(n) : 64; | |
257 | } | |
258 | ||
259 | /* Given a word 'n', calculates floor(log_2('n')). This is equivalent | |
260 | * to finding the bit position of the most significant one bit in 'n'. It is | |
261 | * an error to call this function with 'n' == 0. */ | |
262 | static inline int | |
263 | log_2_floor(uint64_t n) | |
264 | { | |
265 | return 63 - raw_clz64(n); | |
266 | } | |
267 | ||
268 | /* Given a word 'n', calculates ceil(log_2('n')). It is an error to | |
269 | * call this function with 'n' == 0. */ | |
270 | static inline int | |
271 | log_2_ceil(uint64_t n) | |
272 | { | |
273 | return log_2_floor(n) + !is_pow2(n); | |
274 | } | |
275 | ||
381657b3 JR |
276 | /* unsigned int count_1bits(uint64_t x): |
277 | * | |
278 | * Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */ | |
279 | #if UINTPTR_MAX == UINT64_MAX | |
280 | static inline unsigned int | |
281 | count_1bits(uint64_t x) | |
282 | { | |
283 | #if __GNUC__ >= 4 && __POPCNT__ | |
284 | return __builtin_popcountll(x); | |
285 | #else | |
286 | /* This portable implementation is the fastest one we know of for 64 | |
287 | * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */ | |
288 | const uint64_t h55 = UINT64_C(0x5555555555555555); | |
289 | const uint64_t h33 = UINT64_C(0x3333333333333333); | |
290 | const uint64_t h0F = UINT64_C(0x0F0F0F0F0F0F0F0F); | |
291 | const uint64_t h01 = UINT64_C(0x0101010101010101); | |
292 | x -= (x >> 1) & h55; /* Count of each 2 bits in-place. */ | |
293 | x = (x & h33) + ((x >> 2) & h33); /* Count of each 4 bits in-place. */ | |
294 | x = (x + (x >> 4)) & h0F; /* Count of each 8 bits in-place. */ | |
295 | return (x * h01) >> 56; /* Sum of all bytes. */ | |
296 | #endif | |
297 | } | |
298 | #else /* Not 64-bit. */ | |
299 | #if __GNUC__ >= 4 && __POPCNT__ | |
c3cc4d2d | 300 | static inline unsigned int |
381657b3 | 301 | count_1bits_32__(uint32_t x) |
c3cc4d2d | 302 | { |
c3cc4d2d | 303 | return __builtin_popcount(x); |
381657b3 | 304 | } |
c3cc4d2d | 305 | #else |
381657b3 JR |
306 | #define NEED_COUNT_1BITS_8 1 |
307 | extern const uint8_t count_1bits_8[256]; | |
308 | static inline unsigned int | |
309 | count_1bits_32__(uint32_t x) | |
310 | { | |
c3cc4d2d JR |
311 | /* This portable implementation is the fastest one we know of for 32 bits, |
312 | * and faster than GCC __builtin_popcount(). */ | |
313 | return (count_1bits_8[x & 0xff] + | |
314 | count_1bits_8[(x >> 8) & 0xff] + | |
315 | count_1bits_8[(x >> 16) & 0xff] + | |
316 | count_1bits_8[x >> 24]); | |
c3cc4d2d | 317 | } |
381657b3 | 318 | #endif |
c3cc4d2d JR |
319 | static inline unsigned int |
320 | count_1bits(uint64_t x) | |
321 | { | |
381657b3 | 322 | return count_1bits_32__(x) + count_1bits_32__(x >> 32); |
c3cc4d2d | 323 | } |
381657b3 | 324 | #endif |
8c947903 | 325 | |
fe9d0898 BP |
326 | /* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x' |
327 | * is 0. */ | |
328 | static inline uintmax_t | |
329 | rightmost_1bit(uintmax_t x) | |
330 | { | |
331 | return x & -x; | |
332 | } | |
333 | ||
334 | /* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 => | |
335 | * 01010000), or 0 if 'x' is 0. */ | |
336 | static inline uintmax_t | |
337 | zero_rightmost_1bit(uintmax_t x) | |
338 | { | |
339 | return x & (x - 1); | |
340 | } | |
35bedb61 | 341 | |
4ed47144 BP |
342 | /* Returns the index of the rightmost 1-bit in 'x' (e.g. 01011000 => 3), or an |
343 | * undefined value if 'x' is 0. */ | |
795b3288 | 344 | static inline int |
4ed47144 | 345 | rightmost_1bit_idx(uint64_t x) |
35bedb61 | 346 | { |
4ed47144 | 347 | return ctz64(x); |
35bedb61 BP |
348 | } |
349 | ||
4ed47144 BP |
350 | /* Returns the index of the leftmost 1-bit in 'x' (e.g. 01011000 => 6), or an |
351 | * undefined value if 'x' is 0. */ | |
35bedb61 | 352 | static inline uint32_t |
4ed47144 | 353 | leftmost_1bit_idx(uint64_t x) |
35bedb61 | 354 | { |
4ed47144 | 355 | return log_2_floor(x); |
35bedb61 | 356 | } |
86f35fb5 JR |
357 | |
358 | /* Return a ovs_be32 prefix in network byte order with 'plen' highest bits set. | |
359 | * Shift with 32 is undefined behavior, but we rather use 64-bit shift than | |
360 | * compare. */ | |
361 | static inline ovs_be32 be32_prefix_mask(int plen) | |
362 | { | |
363 | return htonl((uint64_t)UINT32_MAX << (32 - plen)); | |
364 | } | |
fe9d0898 | 365 | \f |
53cb9c3e JR |
366 | bool is_all_zeros(const void *, size_t); |
367 | bool is_all_ones(const void *, size_t); | |
ddc4f8e2 BP |
368 | void bitwise_copy(const void *src, unsigned int src_len, unsigned int src_ofs, |
369 | void *dst, unsigned int dst_len, unsigned int dst_ofs, | |
370 | unsigned int n_bits); | |
6cc7ea5e BP |
371 | void bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs, |
372 | unsigned int n_bits); | |
c2dd4932 BP |
373 | void bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs, |
374 | unsigned int n_bits); | |
79a010aa BP |
375 | bool bitwise_is_all_zeros(const void *, unsigned int len, unsigned int ofs, |
376 | unsigned int n_bits); | |
099c06e3 BP |
377 | unsigned int bitwise_scan(const void *, unsigned int len, |
378 | bool target, unsigned int start, unsigned int end); | |
95a92d5a BP |
379 | int bitwise_rscan(const void *, unsigned int len, bool target, |
380 | int start, int end); | |
ddc4f8e2 BP |
381 | void bitwise_put(uint64_t value, |
382 | void *dst, unsigned int dst_len, unsigned int dst_ofs, | |
383 | unsigned int n_bits); | |
384 | uint64_t bitwise_get(const void *src, unsigned int src_len, | |
385 | unsigned int src_ofs, unsigned int n_bits); | |
95a92d5a BP |
386 | bool bitwise_get_bit(const void *src, unsigned int len, unsigned int ofs); |
387 | void bitwise_put0(void *dst, unsigned int len, unsigned int ofs); | |
388 | void bitwise_put1(void *dst, unsigned int len, unsigned int ofs); | |
389 | void bitwise_put_bit(void *dst, unsigned int len, unsigned int ofs, bool); | |
390 | void bitwise_toggle_bit(void *dst, unsigned int len, unsigned int ofs); | |
75a75043 | 391 | |
bdd7ecf5 JS |
392 | /* Returns non-zero if the parameters have equal value. */ |
393 | static inline int | |
394 | ovs_u128_equals(const ovs_u128 *a, const ovs_u128 *b) | |
395 | { | |
396 | return (a->u64.hi == b->u64.hi) && (a->u64.lo == b->u64.lo); | |
397 | } | |
398 | ||
557344e3 JS |
399 | /* Returns true if 'val' is 0. */ |
400 | static inline bool | |
401 | ovs_u128_is_zero(const ovs_u128 *val) | |
402 | { | |
403 | return !(val->u64.hi || val->u64.lo); | |
404 | } | |
405 | ||
406 | /* Returns true if 'val' is all ones. */ | |
407 | static inline bool | |
408 | ovs_u128_is_ones(const ovs_u128 *val) | |
409 | { | |
f3d2a295 | 410 | return ovs_u128_equals(val, &OVS_U128_MAX); |
557344e3 JS |
411 | } |
412 | ||
413 | /* Returns non-zero if the parameters have equal value. */ | |
414 | static inline int | |
415 | ovs_be128_equals(const ovs_be128 *a, const ovs_be128 *b) | |
416 | { | |
417 | return (a->be64.hi == b->be64.hi) && (a->be64.lo == b->be64.lo); | |
418 | } | |
419 | ||
420 | /* Returns true if 'val' is 0. */ | |
421 | static inline bool | |
422 | ovs_be128_is_zero(const ovs_be128 *val) | |
423 | { | |
424 | return !(val->be64.hi || val->be64.lo); | |
425 | } | |
426 | ||
f2d105b5 JS |
427 | static inline ovs_u128 |
428 | ovs_u128_and(const ovs_u128 a, const ovs_u128 b) | |
429 | { | |
430 | ovs_u128 dst; | |
431 | ||
432 | dst.u64.hi = a.u64.hi & b.u64.hi; | |
433 | dst.u64.lo = a.u64.lo & b.u64.lo; | |
434 | ||
435 | return dst; | |
436 | } | |
437 | ||
5fd2f418 | 438 | void xsleep(unsigned int seconds); |
124f09c9 | 439 | |
20174b74 QM |
440 | bool is_stdout_a_tty(void); |
441 | ||
06f14c92 GS |
442 | #ifdef _WIN32 |
443 | \f | |
315ea327 | 444 | char *ovs_format_message(int error); |
06f14c92 | 445 | char *ovs_lasterror_to_string(void); |
daa04db8 | 446 | int ftruncate(int fd, off_t length); |
06f14c92 GS |
447 | #endif |
448 | ||
064af421 BP |
449 | #ifdef __cplusplus |
450 | } | |
451 | #endif | |
452 | ||
453 | #endif /* util.h */ |