]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/util.h
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <arpa/inet.h>
31 #include "openvswitch/types.h"
32 #include "openvswitch/util.h"
36 #define va_copy __va_copy
38 #define va_copy(dst, src) ((dst) = (src))
42 extern char *program_name
;
44 #define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0]))
46 /* return 0 for array types, 1 otherwise */
47 #define __ARRAY_CHECK(ARRAY) \
48 !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0]))
50 /* compile-time fail if not array */
51 #define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)]))
52 #define __ARRAY_SIZE(ARRAY) \
53 __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \
54 __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY))
56 #define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY)
59 /* Returns the number of elements in ARRAY. */
60 #define ARRAY_SIZE(ARRAY) __ARRAY_SIZE(ARRAY)
62 /* Returns X / Y, rounding up. X must be nonnegative to round correctly. */
63 #define DIV_ROUND_UP(X, Y) (((X) + ((Y) - 1)) / (Y))
65 /* Returns X rounded up to the nearest multiple of Y. */
66 #define ROUND_UP(X, Y) (DIV_ROUND_UP(X, Y) * (Y))
68 /* Returns the least number that, when added to X, yields a multiple of Y. */
69 #define PAD_SIZE(X, Y) (ROUND_UP(X, Y) - (X))
71 /* Returns X rounded down to the nearest multiple of Y. */
72 #define ROUND_DOWN(X, Y) ((X) / (Y) * (Y))
74 /* Returns true if X is a power of 2, otherwise false. */
75 #define IS_POW2(X) ((X) && !((X) & ((X) - 1)))
83 /* Returns X rounded up to a power of 2. X must be a constant expression. */
84 #define ROUND_UP_POW2(X) RUP2__(X)
85 #define RUP2__(X) (RUP2_1(X) + 1)
86 #define RUP2_1(X) (RUP2_2(X) | (RUP2_2(X) >> 16))
87 #define RUP2_2(X) (RUP2_3(X) | (RUP2_3(X) >> 8))
88 #define RUP2_3(X) (RUP2_4(X) | (RUP2_4(X) >> 4))
89 #define RUP2_4(X) (RUP2_5(X) | (RUP2_5(X) >> 2))
90 #define RUP2_5(X) (RUP2_6(X) | (RUP2_6(X) >> 1))
91 #define RUP2_6(X) ((X) - 1)
93 /* Returns X rounded down to a power of 2. X must be a constant expression. */
94 #define ROUND_DOWN_POW2(X) RDP2__(X)
95 #define RDP2__(X) (RDP2_1(X) - (RDP2_1(X) >> 1))
96 #define RDP2_1(X) (RDP2_2(X) | (RDP2_2(X) >> 16))
97 #define RDP2_2(X) (RDP2_3(X) | (RDP2_3(X) >> 8))
98 #define RDP2_3(X) (RDP2_4(X) | (RDP2_4(X) >> 4))
99 #define RDP2_4(X) (RDP2_5(X) | (RDP2_5(X) >> 2))
100 #define RDP2_5(X) ( (X) | ( (X) >> 1))
102 /* This system's cache line size, in bytes.
103 * Being wrong hurts performance but not correctness. */
104 #define CACHE_LINE_SIZE 64
105 BUILD_ASSERT_DECL(IS_POW2(CACHE_LINE_SIZE
));
108 ovs_prefetch_range(const void *start
, size_t size
)
110 const char *addr
= (const char *)start
;
113 for (ofs
= 0; ofs
< size
; ofs
+= CACHE_LINE_SIZE
) {
114 OVS_PREFETCH(addr
+ ofs
);
119 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
123 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
126 #define OVS_NOT_REACHED() abort()
128 /* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
129 * (void *). This is to suppress the alignment warning issued by clang. */
130 #define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
132 /* Use "%"PRIuSIZE to format size_t with printf(). */
134 #define PRIdSIZE "Id"
135 #define PRIiSIZE "Ii"
136 #define PRIoSIZE "Io"
137 #define PRIuSIZE "Iu"
138 #define PRIxSIZE "Ix"
139 #define PRIXSIZE "IX"
141 #define PRIdSIZE "zd"
142 #define PRIiSIZE "zi"
143 #define PRIoSIZE "zo"
144 #define PRIuSIZE "zu"
145 #define PRIxSIZE "zx"
146 #define PRIXSIZE "zX"
150 typedef uint32_t HANDLE
;
157 #define set_program_name(name) \
158 ovs_set_program_name(name, OVS_PACKAGE_VERSION)
160 const char *get_subprogram_name(void);
161 void set_subprogram_name(const char *);
163 void ovs_print_version(uint8_t min_ofp
, uint8_t max_ofp
);
165 OVS_NO_RETURN
void out_of_memory(void);
166 void *xmalloc(size_t) MALLOC_LIKE
;
167 void *xcalloc(size_t, size_t) MALLOC_LIKE
;
168 void *xzalloc(size_t) MALLOC_LIKE
;
169 void *xrealloc(void *, size_t);
170 void *xmemdup(const void *, size_t) MALLOC_LIKE
;
171 char *xmemdup0(const char *, size_t) MALLOC_LIKE
;
172 char *xstrdup(const char *) MALLOC_LIKE
;
173 char *xasprintf(const char *format
, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE
;
174 char *xvasprintf(const char *format
, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE
;
175 void *x2nrealloc(void *p
, size_t *n
, size_t s
);
177 void *xmalloc_cacheline(size_t) MALLOC_LIKE
;
178 void *xzalloc_cacheline(size_t) MALLOC_LIKE
;
179 void free_cacheline(void *);
181 void ovs_strlcpy(char *dst
, const char *src
, size_t size
);
182 void ovs_strzcpy(char *dst
, const char *src
, size_t size
);
184 OVS_NO_RETURN
void ovs_abort(int err_no
, const char *format
, ...)
185 OVS_PRINTF_FORMAT(2, 3);
186 OVS_NO_RETURN
void ovs_abort_valist(int err_no
, const char *format
, va_list)
187 OVS_PRINTF_FORMAT(2, 0);
188 OVS_NO_RETURN
void ovs_fatal(int err_no
, const char *format
, ...)
189 OVS_PRINTF_FORMAT(2, 3);
190 OVS_NO_RETURN
void ovs_fatal_valist(int err_no
, const char *format
, va_list)
191 OVS_PRINTF_FORMAT(2, 0);
192 void ovs_error(int err_no
, const char *format
, ...) OVS_PRINTF_FORMAT(2, 3);
193 void ovs_error_valist(int err_no
, const char *format
, va_list)
194 OVS_PRINTF_FORMAT(2, 0);
195 const char *ovs_retval_to_string(int);
196 const char *ovs_strerror(int);
197 void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset
, bool ascii
);
199 bool str_to_int(const char *, int base
, int *);
200 bool str_to_long(const char *, int base
, long *);
201 bool str_to_llong(const char *, int base
, long long *);
202 bool str_to_uint(const char *, int base
, unsigned int *);
204 bool ovs_scan(const char *s
, const char *format
, ...) OVS_SCANF_FORMAT(2, 3);
205 bool ovs_scan_len(const char *s
, int *n
, const char *format
, ...);
207 bool str_to_double(const char *, double *);
209 int hexit_value(int c
);
210 uintmax_t hexits_value(const char *s
, size_t n
, bool *ok
);
212 int parse_int_string(const char *s
, uint8_t *valuep
, int field_width
,
215 const char *english_list_delimiter(size_t index
, size_t total
);
219 char *dir_name(const char *file_name
);
220 char *base_name(const char *file_name
);
222 char *abs_file_name(const char *dir
, const char *file_name
);
224 char *follow_symlinks(const char *filename
);
226 void ignore(bool x OVS_UNUSED
);
230 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
235 /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
236 * a plain __builtin_ctzll() here always generates an out-of-line function
237 * call. The test below helps it to emit a single 'bsf' instruction. */
238 return (__builtin_constant_p(n
<= UINT32_MAX
) && n
<= UINT32_MAX
240 : __builtin_ctzll(n
));
244 raw_clz64(uint64_t n
)
246 return __builtin_clzll(n
);
254 _BitScanForward64(&r
, n
);
257 unsigned long low
= n
, high
, r
= 0;
258 if (_BitScanForward(&r
, low
)) {
262 _BitScanForward(&r
, high
);
268 raw_clz64(uint64_t n
)
272 _BitScanReverse64(&r
, n
);
275 unsigned long low
, high
= n
>> 32, r
= 0;
276 if (_BitScanReverse(&r
, high
)) {
280 _BitScanReverse(&r
, low
);
285 /* Defined in util.c. */
286 int raw_ctz(uint64_t n
);
287 int raw_clz64(uint64_t n
);
290 /* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */
294 return n
? raw_ctz(n
) : 32;
297 /* Returns the number of trailing 0-bits in 'n', or 64 if 'n' is 0. */
301 return n
? raw_ctz(n
) : 64;
304 /* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */
308 return n
? raw_clz64(n
) - 32 : 32;
311 /* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */
315 return n
? raw_clz64(n
) : 64;
318 /* Given a word 'n', calculates floor(log_2('n')). This is equivalent
319 * to finding the bit position of the most significant one bit in 'n'. It is
320 * an error to call this function with 'n' == 0. */
322 log_2_floor(uint64_t n
)
324 return 63 - raw_clz64(n
);
327 /* Given a word 'n', calculates ceil(log_2('n')). It is an error to
328 * call this function with 'n' == 0. */
330 log_2_ceil(uint64_t n
)
332 return log_2_floor(n
) + !is_pow2(n
);
335 /* unsigned int count_1bits(uint64_t x):
337 * Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */
338 #if UINTPTR_MAX == UINT64_MAX
339 static inline unsigned int
340 count_1bits(uint64_t x
)
342 #if __GNUC__ >= 4 && __POPCNT__
343 return __builtin_popcountll(x
);
345 /* This portable implementation is the fastest one we know of for 64
346 * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */
347 const uint64_t h55
= UINT64_C(0x5555555555555555);
348 const uint64_t h33
= UINT64_C(0x3333333333333333);
349 const uint64_t h0F
= UINT64_C(0x0F0F0F0F0F0F0F0F);
350 const uint64_t h01
= UINT64_C(0x0101010101010101);
351 x
-= (x
>> 1) & h55
; /* Count of each 2 bits in-place. */
352 x
= (x
& h33
) + ((x
>> 2) & h33
); /* Count of each 4 bits in-place. */
353 x
= (x
+ (x
>> 4)) & h0F
; /* Count of each 8 bits in-place. */
354 return (x
* h01
) >> 56; /* Sum of all bytes. */
357 #else /* Not 64-bit. */
358 #if __GNUC__ >= 4 && __POPCNT__
359 static inline unsigned int
360 count_1bits_32__(uint32_t x
)
362 return __builtin_popcount(x
);
365 #define NEED_COUNT_1BITS_8 1
366 extern const uint8_t count_1bits_8
[256];
367 static inline unsigned int
368 count_1bits_32__(uint32_t x
)
370 /* This portable implementation is the fastest one we know of for 32 bits,
371 * and faster than GCC __builtin_popcount(). */
372 return (count_1bits_8
[x
& 0xff] +
373 count_1bits_8
[(x
>> 8) & 0xff] +
374 count_1bits_8
[(x
>> 16) & 0xff] +
375 count_1bits_8
[x
>> 24]);
378 static inline unsigned int
379 count_1bits(uint64_t x
)
381 return count_1bits_32__(x
) + count_1bits_32__(x
>> 32);
385 /* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x'
387 static inline uintmax_t
388 rightmost_1bit(uintmax_t x
)
393 /* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 =>
394 * 01010000), or 0 if 'x' is 0. */
395 static inline uintmax_t
396 zero_rightmost_1bit(uintmax_t x
)
401 /* Returns the index of the rightmost 1-bit in 'x' (e.g. 01011000 => 3), or an
402 * undefined value if 'x' is 0. */
404 rightmost_1bit_idx(uint64_t x
)
409 /* Returns the index of the leftmost 1-bit in 'x' (e.g. 01011000 => 6), or an
410 * undefined value if 'x' is 0. */
411 static inline uint32_t
412 leftmost_1bit_idx(uint64_t x
)
414 return log_2_floor(x
);
417 /* Return a ovs_be32 prefix in network byte order with 'plen' highest bits set.
418 * Shift with 32 is undefined behavior, but we rather use 64-bit shift than
420 static inline ovs_be32
be32_prefix_mask(int plen
)
422 return htonl((uint64_t)UINT32_MAX
<< (32 - plen
));
425 bool is_all_zeros(const void *, size_t);
426 bool is_all_ones(const void *, size_t);
427 void bitwise_copy(const void *src
, unsigned int src_len
, unsigned int src_ofs
,
428 void *dst
, unsigned int dst_len
, unsigned int dst_ofs
,
429 unsigned int n_bits
);
430 void bitwise_zero(void *dst_
, unsigned int dst_len
, unsigned dst_ofs
,
431 unsigned int n_bits
);
432 void bitwise_one(void *dst_
, unsigned int dst_len
, unsigned dst_ofs
,
433 unsigned int n_bits
);
434 bool bitwise_is_all_zeros(const void *, unsigned int len
, unsigned int ofs
,
435 unsigned int n_bits
);
436 unsigned int bitwise_scan(const void *, unsigned int len
,
437 bool target
, unsigned int start
, unsigned int end
);
438 int bitwise_rscan(const void *, unsigned int len
, bool target
,
440 void bitwise_put(uint64_t value
,
441 void *dst
, unsigned int dst_len
, unsigned int dst_ofs
,
442 unsigned int n_bits
);
443 uint64_t bitwise_get(const void *src
, unsigned int src_len
,
444 unsigned int src_ofs
, unsigned int n_bits
);
445 bool bitwise_get_bit(const void *src
, unsigned int len
, unsigned int ofs
);
446 void bitwise_put0(void *dst
, unsigned int len
, unsigned int ofs
);
447 void bitwise_put1(void *dst
, unsigned int len
, unsigned int ofs
);
448 void bitwise_put_bit(void *dst
, unsigned int len
, unsigned int ofs
, bool);
449 void bitwise_toggle_bit(void *dst
, unsigned int len
, unsigned int ofs
);
451 /* Returns non-zero if the parameters have equal value. */
453 ovs_u128_equals(const ovs_u128
*a
, const ovs_u128
*b
)
455 return (a
->u64
.hi
== b
->u64
.hi
) && (a
->u64
.lo
== b
->u64
.lo
);
458 /* Returns true if 'val' is 0. */
460 ovs_u128_is_zero(const ovs_u128
*val
)
462 return !(val
->u64
.hi
|| val
->u64
.lo
);
465 /* Returns true if 'val' is all ones. */
467 ovs_u128_is_ones(const ovs_u128
*val
)
469 return ovs_u128_equals(val
, &OVS_U128_MAX
);
472 /* Returns non-zero if the parameters have equal value. */
474 ovs_be128_equals(const ovs_be128
*a
, const ovs_be128
*b
)
476 return (a
->be64
.hi
== b
->be64
.hi
) && (a
->be64
.lo
== b
->be64
.lo
);
479 /* Returns true if 'val' is 0. */
481 ovs_be128_is_zero(const ovs_be128
*val
)
483 return !(val
->be64
.hi
|| val
->be64
.lo
);
486 void xsleep(unsigned int seconds
);
488 bool is_stdout_a_tty(void);
492 char *ovs_format_message(int error
);
493 char *ovs_lasterror_to_string(void);
494 int ftruncate(int fd
, off_t length
);