]>
git.proxmox.com Git - mirror_ovs.git/blob - lib/util.h
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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 <sys/types.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
31 #include "openvswitch/util.h"
32 #if defined(__aarch64__) && __GNUC__ >= 6
36 extern char *program_name
;
38 #define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0]))
39 #if __GNUC__ && !defined(__cplusplus)
40 /* return 0 for array types, 1 otherwise */
41 #define __ARRAY_CHECK(ARRAY) \
42 !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0]))
44 /* compile-time fail if not array */
45 #define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)]))
46 #define __ARRAY_SIZE(ARRAY) \
47 __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \
48 __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY))
49 #elif defined(__cplusplus)
50 #define __ARRAY_SIZE(ARRAY) ( \
51 0 * sizeof(reinterpret_cast<const ::Bad_arg_to_ARRAY_SIZE *>(ARRAY)) + \
52 0 * sizeof(::Bad_arg_to_ARRAY_SIZE::check_type((ARRAY), &(ARRAY))) + \
53 sizeof(ARRAY) / sizeof((ARRAY)[0]) )
55 struct Bad_arg_to_ARRAY_SIZE
{
59 static Is_pointer
check_type(const T
*, const T
* const *);
60 static Is_array
check_type(const void *, const void *);
63 #define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY)
67 /* This system's cache line size, in bytes.
68 * Being wrong hurts performance but not correctness. */
69 #define CACHE_LINE_SIZE 64
70 BUILD_ASSERT_DECL(IS_POW2(CACHE_LINE_SIZE
));
72 /* Cacheline marking is typically done using zero-sized array.
73 * However MSVC doesn't like zero-sized array in struct/union.
74 * C4200: https://msdn.microsoft.com/en-us/library/79wf64bc.aspx
76 typedef uint8_t OVS_CACHE_LINE_MARKER
[1];
79 ovs_prefetch_range(const void *start
, size_t size
)
81 const char *addr
= (const char *)start
;
84 for (ofs
= 0; ofs
< size
; ofs
+= CACHE_LINE_SIZE
) {
85 OVS_PREFETCH(addr
+ ofs
);
90 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
94 #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
97 /* Comparisons for ints with modular arithmetic */
98 #define INT_MOD_LT(a,b) ((int) ((a)-(b)) < 0)
99 #define INT_MOD_LEQ(a,b) ((int) ((a)-(b)) <= 0)
100 #define INT_MOD_GT(a,b) ((int) ((a)-(b)) > 0)
101 #define INT_MOD_GEQ(a,b) ((int) ((a)-(b)) >= 0)
103 #define INT_MOD_MIN(a, b) ((INT_MOD_LT(a, b)) ? (a) : (b))
104 #define INT_MOD_MAX(a, b) ((INT_MOD_GT(a, b)) ? (a) : (b))
106 #define OVS_NOT_REACHED() abort()
108 /* Use "%"PRIuSIZE to format size_t with printf(). */
110 #define PRIdSIZE "Id"
111 #define PRIiSIZE "Ii"
112 #define PRIoSIZE "Io"
113 #define PRIuSIZE "Iu"
114 #define PRIxSIZE "Ix"
115 #define PRIXSIZE "IX"
117 #define PRIdSIZE "zd"
118 #define PRIiSIZE "zi"
119 #define PRIoSIZE "zo"
120 #define PRIuSIZE "zu"
121 #define PRIxSIZE "zx"
122 #define PRIXSIZE "zX"
126 typedef uint32_t HANDLE
;
133 #define set_program_name(name) \
134 ovs_set_program_name(name, OVS_PACKAGE_VERSION)
136 const char *get_subprogram_name(void);
137 void set_subprogram_name(const char *);
139 unsigned int get_page_size(void);
140 long long int get_boot_time(void);
142 void ctl_timeout_setup(unsigned int secs
);
144 void ovs_print_version(uint8_t min_ofp
, uint8_t max_ofp
);
146 void set_memory_locked(void);
147 bool memory_locked(void);
149 OVS_NO_RETURN
void out_of_memory(void);
150 void *xmalloc(size_t) MALLOC_LIKE
;
151 void *xcalloc(size_t, size_t) MALLOC_LIKE
;
152 void *xzalloc(size_t) MALLOC_LIKE
;
153 void *xrealloc(void *, size_t);
154 void *xmemdup(const void *, size_t) MALLOC_LIKE
;
155 char *xmemdup0(const char *, size_t) MALLOC_LIKE
;
156 char *xstrdup(const char *) MALLOC_LIKE
;
157 char *nullable_xstrdup(const char *) MALLOC_LIKE
;
158 bool nullable_string_is_equal(const char *a
, const char *b
);
159 char *xasprintf(const char *format
, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE
;
160 char *xvasprintf(const char *format
, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE
;
161 void *x2nrealloc(void *p
, size_t *n
, size_t s
);
163 void *xmalloc_cacheline(size_t) MALLOC_LIKE
;
164 void *xzalloc_cacheline(size_t) MALLOC_LIKE
;
165 void free_cacheline(void *);
167 void ovs_strlcpy(char *dst
, const char *src
, size_t size
);
168 void ovs_strzcpy(char *dst
, const char *src
, size_t size
);
170 int string_ends_with(const char *str
, const char *suffix
);
172 void *xmalloc_pagealign(size_t) MALLOC_LIKE
;
173 void free_pagealign(void *);
174 void *xmalloc_size_align(size_t, size_t) MALLOC_LIKE
;
175 void free_size_align(void *);
177 /* The C standards say that neither the 'dst' nor 'src' argument to
178 * memcpy() may be null, even if 'n' is zero. This wrapper tolerates
181 nullable_memcpy(void *dst
, const void *src
, size_t n
)
188 /* The C standards say that the 'dst' argument to memset may not be
189 * null, even if 'n' is zero. This wrapper tolerates the null case. */
191 nullable_memset(void *dst
, int c
, size_t n
)
198 /* Copy string SRC to DST, but no more bytes than the shorter of DST or SRC.
199 * DST and SRC must both be char arrays, not pointers, and with GNU C, this
200 * raises a compiler error if either DST or SRC is a pointer instead of an
202 #define ovs_strlcpy_arrays(DST, SRC) \
203 ovs_strlcpy(DST, SRC, MIN(ARRAY_SIZE(DST), ARRAY_SIZE(SRC)))
205 OVS_NO_RETURN
void ovs_abort(int err_no
, const char *format
, ...)
206 OVS_PRINTF_FORMAT(2, 3);
207 OVS_NO_RETURN
void ovs_abort_valist(int err_no
, const char *format
, va_list)
208 OVS_PRINTF_FORMAT(2, 0);
209 OVS_NO_RETURN
void ovs_fatal(int err_no
, const char *format
, ...)
210 OVS_PRINTF_FORMAT(2, 3);
211 OVS_NO_RETURN
void ovs_fatal_valist(int err_no
, const char *format
, va_list)
212 OVS_PRINTF_FORMAT(2, 0);
213 void ovs_error(int err_no
, const char *format
, ...) OVS_PRINTF_FORMAT(2, 3);
214 void ovs_error_valist(int err_no
, const char *format
, va_list)
215 OVS_PRINTF_FORMAT(2, 0);
216 const char *ovs_retval_to_string(int);
217 const char *ovs_strerror(int);
218 void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset
, bool ascii
);
220 bool str_to_int(const char *, int base
, int *);
221 bool str_to_long(const char *, int base
, long *);
222 bool str_to_llong(const char *, int base
, long long *);
223 bool str_to_llong_with_tail(const char *, char **, int base
, long long *);
224 bool str_to_uint(const char *, int base
, unsigned int *);
225 bool str_to_ullong(const char *, int base
, unsigned long long *);
226 bool str_to_llong_range(const char *, int base
, long long *, long long *);
228 bool ovs_scan(const char *s
, const char *format
, ...) OVS_SCANF_FORMAT(2, 3);
229 bool ovs_scan_len(const char *s
, int *n
, const char *format
, ...);
231 bool str_to_double(const char *, double *);
233 int hexit_value(unsigned char c
);
234 uintmax_t hexits_value(const char *s
, size_t n
, bool *ok
);
236 int parse_int_string(const char *s
, uint8_t *valuep
, int field_width
,
239 const char *english_list_delimiter(size_t index
, size_t total
);
243 char *dir_name(const char *file_name
);
244 char *base_name(const char *file_name
);
246 char *abs_file_name(const char *dir
, const char *file_name
);
247 bool is_file_name_absolute(const char *);
249 char *follow_symlinks(const char *filename
);
251 void ignore(bool x OVS_UNUSED
);
255 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
260 /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
261 * a plain __builtin_ctzll() here always generates an out-of-line function
262 * call. The test below helps it to emit a single 'bsf' instruction. */
263 return (__builtin_constant_p(n
<= UINT32_MAX
) && n
<= UINT32_MAX
265 : __builtin_ctzll(n
));
269 raw_clz64(uint64_t n
)
271 return __builtin_clzll(n
);
279 _BitScanForward64(&r
, n
);
282 unsigned long low
= n
, high
, r
= 0;
283 if (_BitScanForward(&r
, low
)) {
287 _BitScanForward(&r
, high
);
293 raw_clz64(uint64_t n
)
297 _BitScanReverse64(&r
, n
);
300 unsigned long low
, high
= n
>> 32, r
= 0;
301 if (_BitScanReverse(&r
, high
)) {
305 _BitScanReverse(&r
, low
);
310 /* Defined in util.c. */
311 int raw_ctz(uint64_t n
);
312 int raw_clz64(uint64_t n
);
315 /* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */
319 return n
? raw_ctz(n
) : 32;
322 /* Returns the number of trailing 0-bits in 'n', or 64 if 'n' is 0. */
326 return n
? raw_ctz(n
) : 64;
329 /* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */
333 return n
? raw_clz64(n
) - 32 : 32;
336 /* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */
340 return n
? raw_clz64(n
) : 64;
343 /* Given a word 'n', calculates floor(log_2('n')). This is equivalent
344 * to finding the bit position of the most significant one bit in 'n'. It is
345 * an error to call this function with 'n' == 0. */
347 log_2_floor(uint64_t n
)
349 return 63 - raw_clz64(n
);
352 /* Given a word 'n', calculates ceil(log_2('n')). It is an error to
353 * call this function with 'n' == 0. */
355 log_2_ceil(uint64_t n
)
357 return log_2_floor(n
) + !is_pow2(n
);
360 /* unsigned int count_1bits(uint64_t x):
362 * Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */
363 #if UINTPTR_MAX == UINT64_MAX
364 static inline unsigned int
365 count_1bits(uint64_t x
)
367 #if (__GNUC__ >= 4 && __POPCNT__) || (defined(__aarch64__) && __GNUC__ >= 7)
368 return __builtin_popcountll(x
);
369 #elif defined(__aarch64__) && __GNUC__ >= 6
370 return vaddv_u8(vcnt_u8(vcreate_u8(x
)));
372 /* This portable implementation is the fastest one we know of for 64
373 * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */
374 const uint64_t h55
= UINT64_C(0x5555555555555555);
375 const uint64_t h33
= UINT64_C(0x3333333333333333);
376 const uint64_t h0F
= UINT64_C(0x0F0F0F0F0F0F0F0F);
377 const uint64_t h01
= UINT64_C(0x0101010101010101);
378 x
-= (x
>> 1) & h55
; /* Count of each 2 bits in-place. */
379 x
= (x
& h33
) + ((x
>> 2) & h33
); /* Count of each 4 bits in-place. */
380 x
= (x
+ (x
>> 4)) & h0F
; /* Count of each 8 bits in-place. */
381 return (x
* h01
) >> 56; /* Sum of all bytes. */
384 #else /* Not 64-bit. */
385 #if __GNUC__ >= 4 && __POPCNT__
386 static inline unsigned int
387 count_1bits_32__(uint32_t x
)
389 return __builtin_popcount(x
);
392 #define NEED_COUNT_1BITS_8 1
393 extern const uint8_t count_1bits_8
[256];
394 static inline unsigned int
395 count_1bits_32__(uint32_t x
)
397 /* This portable implementation is the fastest one we know of for 32 bits,
398 * and faster than GCC __builtin_popcount(). */
399 return (count_1bits_8
[x
& 0xff] +
400 count_1bits_8
[(x
>> 8) & 0xff] +
401 count_1bits_8
[(x
>> 16) & 0xff] +
402 count_1bits_8
[x
>> 24]);
405 static inline unsigned int
406 count_1bits(uint64_t x
)
408 return count_1bits_32__(x
) + count_1bits_32__(x
>> 32);
412 /* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x'
414 static inline uintmax_t
415 rightmost_1bit(uintmax_t x
)
420 /* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 =>
421 * 01010000), or 0 if 'x' is 0. */
422 static inline uintmax_t
423 zero_rightmost_1bit(uintmax_t x
)
428 /* Returns the index of the rightmost 1-bit in 'x' (e.g. 01011000 => 3), or an
429 * undefined value if 'x' is 0. */
431 rightmost_1bit_idx(uint64_t x
)
436 /* Returns the index of the leftmost 1-bit in 'x' (e.g. 01011000 => 6), or an
437 * undefined value if 'x' is 0. */
438 static inline uint32_t
439 leftmost_1bit_idx(uint64_t x
)
441 return log_2_floor(x
);
444 /* Return a ovs_be32 prefix in network byte order with 'plen' highest bits set.
445 * Shift with 32 is undefined behavior, but we rather use 64-bit shift than
447 static inline ovs_be32
be32_prefix_mask(int plen
)
449 return htonl((uint64_t)UINT32_MAX
<< (32 - plen
));
452 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
453 * false otherwise. */
455 uint_is_superset(uintmax_t super
, uintmax_t sub
)
457 return (super
& sub
) == sub
;
460 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
461 * false otherwise. */
463 be16_is_superset(ovs_be16 super
, ovs_be16 sub
)
465 return (super
& sub
) == sub
;
468 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
469 * false otherwise. */
471 be32_is_superset(ovs_be32 super
, ovs_be32 sub
)
473 return (super
& sub
) == sub
;
476 /* Returns true if the 1-bits in 'super' are a superset of the 1-bits in 'sub',
477 * false otherwise. */
479 be64_is_superset(ovs_be64 super
, ovs_be64 sub
)
481 return (super
& sub
) == sub
;
484 bool is_all_zeros(const void *, size_t);
485 bool is_all_ones(const void *, size_t);
486 bool is_all_byte(const void *, size_t, uint8_t byte
);
487 void or_bytes(void *dst
, const void *src
, size_t n
);
488 void bitwise_copy(const void *src
, unsigned int src_len
, unsigned int src_ofs
,
489 void *dst
, unsigned int dst_len
, unsigned int dst_ofs
,
490 unsigned int n_bits
);
491 void bitwise_zero(void *dst_
, unsigned int dst_len
, unsigned dst_ofs
,
492 unsigned int n_bits
);
493 void bitwise_one(void *dst_
, unsigned int dst_len
, unsigned dst_ofs
,
494 unsigned int n_bits
);
495 bool bitwise_is_all_zeros(const void *, unsigned int len
, unsigned int ofs
,
496 unsigned int n_bits
);
497 unsigned int bitwise_scan(const void *, unsigned int len
,
498 bool target
, unsigned int start
, unsigned int end
);
499 int bitwise_rscan(const void *, unsigned int len
, bool target
,
501 void bitwise_put(uint64_t value
,
502 void *dst
, unsigned int dst_len
, unsigned int dst_ofs
,
503 unsigned int n_bits
);
504 uint64_t bitwise_get(const void *src
, unsigned int src_len
,
505 unsigned int src_ofs
, unsigned int n_bits
);
506 bool bitwise_get_bit(const void *src
, unsigned int len
, unsigned int ofs
);
507 void bitwise_put0(void *dst
, unsigned int len
, unsigned int ofs
);
508 void bitwise_put1(void *dst
, unsigned int len
, unsigned int ofs
);
509 void bitwise_put_bit(void *dst
, unsigned int len
, unsigned int ofs
, bool);
510 void bitwise_toggle_bit(void *dst
, unsigned int len
, unsigned int ofs
);
512 /* Returns non-zero if the parameters have equal value. */
514 ovs_u128_equals(const ovs_u128 a
, const ovs_u128 b
)
516 return (a
.u64
.hi
== b
.u64
.hi
) && (a
.u64
.lo
== b
.u64
.lo
);
519 /* Returns true if 'val' is 0. */
521 ovs_u128_is_zero(const ovs_u128 val
)
523 return !(val
.u64
.hi
|| val
.u64
.lo
);
526 /* Returns true if 'val' is all ones. */
528 ovs_u128_is_ones(const ovs_u128 val
)
530 return ovs_u128_equals(val
, OVS_U128_MAX
);
533 /* Returns non-zero if the parameters have equal value. */
535 ovs_be128_equals(const ovs_be128 a
, const ovs_be128 b
)
537 return (a
.be64
.hi
== b
.be64
.hi
) && (a
.be64
.lo
== b
.be64
.lo
);
540 /* Returns true if 'val' is 0. */
542 ovs_be128_is_zero(const ovs_be128 val
)
544 return !(val
.be64
.hi
|| val
.be64
.lo
);
547 static inline ovs_u128
548 ovs_u128_and(const ovs_u128 a
, const ovs_u128 b
)
552 dst
.u64
.hi
= a
.u64
.hi
& b
.u64
.hi
;
553 dst
.u64
.lo
= a
.u64
.lo
& b
.u64
.lo
;
559 ovs_be128_is_superset(ovs_be128 super
, ovs_be128 sub
)
561 return (be64_is_superset(super
.be64
.hi
, sub
.be64
.hi
) &&
562 be64_is_superset(super
.be64
.lo
, sub
.be64
.lo
));
566 ovs_u128_is_superset(ovs_u128 super
, ovs_u128 sub
)
568 return (uint_is_superset(super
.u64
.hi
, sub
.u64
.hi
) &&
569 uint_is_superset(super
.u64
.lo
, sub
.u64
.lo
));
572 void xsleep(unsigned int seconds
);
573 void xnanosleep(uint64_t nanoseconds
);
575 bool is_stdout_a_tty(void);
579 char *ovs_format_message(int error
);
580 char *ovs_lasterror_to_string(void);
581 int ftruncate(int fd
, off_t length
);