]> git.proxmox.com Git - mirror_ovs.git/blame - lib/util.h
datapath-windows: Correct endianness for deleting zone.
[mirror_ovs.git] / lib / util.h
CommitLineData
064af421 1/*
f9ac0f03 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 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>
91883334 23#include <stdarg.h>
064af421 24#include <stdio.h>
ccc09689 25#include <stdlib.h>
064af421
BP
26#include <string.h>
27#include "compiler.h"
ee89ea7b 28#include "util.h"
b0248b2a 29#include "openvswitch/util.h"
064af421 30
91e12f0d 31extern char *program_name;
064af421 32
878f1972 33#define __ARRAY_SIZE_NOCHECK(ARRAY) (sizeof(ARRAY) / sizeof((ARRAY)[0]))
274cd1f1 34#if __GNUC__ && !defined(__cplusplus)
878f1972
FL
35/* return 0 for array types, 1 otherwise */
36#define __ARRAY_CHECK(ARRAY) \
37 !__builtin_types_compatible_p(typeof(ARRAY), typeof(&ARRAY[0]))
38
39/* compile-time fail if not array */
40#define __ARRAY_FAIL(ARRAY) (sizeof(char[-2*!__ARRAY_CHECK(ARRAY)]))
41#define __ARRAY_SIZE(ARRAY) \
42 __builtin_choose_expr(__ARRAY_CHECK(ARRAY), \
43 __ARRAY_SIZE_NOCHECK(ARRAY), __ARRAY_FAIL(ARRAY))
274cd1f1
YHW
44#elif defined(__cplusplus)
45#define __ARRAY_SIZE(ARRAY) ( \
46 0 * sizeof(reinterpret_cast<const ::Bad_arg_to_ARRAY_SIZE *>(ARRAY)) + \
47 0 * sizeof(::Bad_arg_to_ARRAY_SIZE::check_type((ARRAY), &(ARRAY))) + \
48 sizeof(ARRAY) / sizeof((ARRAY)[0]) )
49
50struct Bad_arg_to_ARRAY_SIZE {
51 class Is_pointer;
52 class Is_array {};
53 template <typename T>
54 static Is_pointer check_type(const T *, const T * const *);
55 static Is_array check_type(const void *, const void *);
56};
878f1972
FL
57#else
58#define __ARRAY_SIZE(ARRAY) __ARRAY_SIZE_NOCHECK(ARRAY)
59#endif
60
e93ab553 61
e2051008
BP
62/* This system's cache line size, in bytes.
63 * Being wrong hurts performance but not correctness. */
64#define CACHE_LINE_SIZE 64
65BUILD_ASSERT_DECL(IS_POW2(CACHE_LINE_SIZE));
66
73e713fc
BB
67/* Cacheline marking is typically done using zero-sized array.
68 * However MSVC doesn't like zero-sized array in struct/union.
69 * C4200: https://msdn.microsoft.com/en-us/library/79wf64bc.aspx
70 */
71typedef uint8_t OVS_CACHE_LINE_MARKER[1];
72
124f09c9
JR
73static inline void
74ovs_prefetch_range(const void *start, size_t size)
75{
76 const char *addr = (const char *)start;
77 size_t ofs;
78
79 for (ofs = 0; ofs < size; ofs += CACHE_LINE_SIZE) {
80 OVS_PREFETCH(addr + ofs);
81 }
82}
83
064af421
BP
84#ifndef MIN
85#define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
86#endif
87
88#ifndef MAX
89#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
90#endif
91
a489b168
DDP
92/* Comparisons for ints with modular arithmetic */
93#define INT_MOD_LT(a,b) ((int) ((a)-(b)) < 0)
94#define INT_MOD_LEQ(a,b) ((int) ((a)-(b)) <= 0)
95#define INT_MOD_GT(a,b) ((int) ((a)-(b)) > 0)
96#define INT_MOD_GEQ(a,b) ((int) ((a)-(b)) >= 0)
97
98#define INT_MOD_MIN(a, b) ((INT_MOD_LT(a, b)) ? (a) : (b))
99#define INT_MOD_MAX(a, b) ((INT_MOD_GT(a, b)) ? (a) : (b))
100
428b2edd 101#define OVS_NOT_REACHED() abort()
064af421 102
34582733
AS
103/* Use "%"PRIuSIZE to format size_t with printf(). */
104#ifdef _WIN32
105#define PRIdSIZE "Id"
106#define PRIiSIZE "Ii"
107#define PRIoSIZE "Io"
108#define PRIuSIZE "Iu"
109#define PRIxSIZE "Ix"
110#define PRIXSIZE "IX"
111#else
112#define PRIdSIZE "zd"
113#define PRIiSIZE "zi"
114#define PRIoSIZE "zo"
115#define PRIuSIZE "zu"
116#define PRIxSIZE "zx"
117#define PRIXSIZE "zX"
118#endif
119
b57c6aaa
BP
120#ifndef _WIN32
121typedef uint32_t HANDLE;
122#endif
123
064af421
BP
124#ifdef __cplusplus
125extern "C" {
126#endif
127
55d5bb44 128#define set_program_name(name) \
b0248b2a 129 ovs_set_program_name(name, OVS_PACKAGE_VERSION)
064af421 130
bc9fb3a9 131const char *get_subprogram_name(void);
40e7cf56 132 void set_subprogram_name(const char *);
bc9fb3a9 133
ff1d2c16
BB
134unsigned int get_page_size(void);
135long long int get_boot_time(void);
136
55d5bb44 137void ovs_print_version(uint8_t min_ofp, uint8_t max_ofp);
064af421 138
cab50449 139OVS_NO_RETURN void out_of_memory(void);
064af421
BP
140void *xmalloc(size_t) MALLOC_LIKE;
141void *xcalloc(size_t, size_t) MALLOC_LIKE;
ec6fde61 142void *xzalloc(size_t) MALLOC_LIKE;
064af421
BP
143void *xrealloc(void *, size_t);
144void *xmemdup(const void *, size_t) MALLOC_LIKE;
145char *xmemdup0(const char *, size_t) MALLOC_LIKE;
146char *xstrdup(const char *) MALLOC_LIKE;
2225c0b9 147char *nullable_xstrdup(const char *) MALLOC_LIKE;
aacf18c3 148bool nullable_string_is_equal(const char *a, const char *b);
cab50449
TG
149char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
150char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
064af421
BP
151void *x2nrealloc(void *p, size_t *n, size_t s);
152
2fec66db
BP
153void *xmalloc_cacheline(size_t) MALLOC_LIKE;
154void *xzalloc_cacheline(size_t) MALLOC_LIKE;
155void free_cacheline(void *);
156
064af421 157void ovs_strlcpy(char *dst, const char *src, size_t size);
71d7c22f 158void ovs_strzcpy(char *dst, const char *src, size_t size);
064af421 159
316d0932
LR
160/* The C standards say that neither the 'dst' nor 'src' argument to
161 * memcpy() may be null, even if 'n' is zero. This wrapper tolerates
162 * the null case. */
163static inline void
164nullable_memcpy(void *dst, const void *src, size_t n)
165{
166 if (n) {
167 memcpy(dst, src, n);
168 }
169}
170
171/* The C standards say that the 'dst' argument to memset may not be
172 * null, even if 'n' is zero. This wrapper tolerates the null case. */
173static inline void
174nullable_memset(void *dst, int c, size_t n)
175{
176 if (n) {
177 memset(dst, c, n);
178 }
179}
180
f9ac0f03
BP
181/* Copy string SRC to DST, but no more bytes than the shorter of DST or SRC.
182 * DST and SRC must both be char arrays, not pointers, and with GNU C, this
183 * raises a compiler error if either DST or SRC is a pointer instead of an
184 * array. */
185#define ovs_strlcpy_arrays(DST, SRC) \
186 ovs_strlcpy(DST, SRC, MIN(ARRAY_SIZE(DST), ARRAY_SIZE(SRC)))
187
cab50449
TG
188OVS_NO_RETURN void ovs_abort(int err_no, const char *format, ...)
189 OVS_PRINTF_FORMAT(2, 3);
190OVS_NO_RETURN void ovs_abort_valist(int err_no, const char *format, va_list)
191 OVS_PRINTF_FORMAT(2, 0);
192OVS_NO_RETURN void ovs_fatal(int err_no, const char *format, ...)
193 OVS_PRINTF_FORMAT(2, 3);
194OVS_NO_RETURN void ovs_fatal_valist(int err_no, const char *format, va_list)
195 OVS_PRINTF_FORMAT(2, 0);
196void ovs_error(int err_no, const char *format, ...) OVS_PRINTF_FORMAT(2, 3);
c1c8308a 197void ovs_error_valist(int err_no, const char *format, va_list)
cab50449 198 OVS_PRINTF_FORMAT(2, 0);
c18ea70d 199const char *ovs_retval_to_string(int);
5fcbed74 200const char *ovs_strerror(int);
064af421
BP
201void ovs_hex_dump(FILE *, const void *, size_t, uintptr_t offset, bool ascii);
202
203bool str_to_int(const char *, int base, int *);
204bool str_to_long(const char *, int base, long *);
205bool str_to_llong(const char *, int base, long long *);
1ab39058 206bool str_to_llong_with_tail(const char *, char **, int base, long long *);
4c21aa06 207bool str_to_uint(const char *, int base, unsigned int *);
c24b3458 208bool str_to_ullong(const char *, int base, unsigned long long *);
1ab39058 209bool str_to_llong_range(const char *, int base, long long *, long long *);
064af421 210
cab50449 211bool ovs_scan(const char *s, const char *format, ...) OVS_SCANF_FORMAT(2, 3);
f071cbba 212bool ovs_scan_len(const char *s, int *n, const char *format, ...);
ed2232fc 213
f38b84ea
BP
214bool str_to_double(const char *, double *);
215
216int hexit_value(int c);
0429d959 217uintmax_t hexits_value(const char *s, size_t n, bool *ok);
f38b84ea 218
e7ae59f9
JG
219int parse_int_string(const char *s, uint8_t *valuep, int field_width,
220 char **tail);
221
44b4d050
BP
222const char *english_list_delimiter(size_t index, size_t total);
223
daf03c53 224char *get_cwd(void);
3c1150ce 225#ifndef _WIN32
29d4af60 226char *dir_name(const char *file_name);
e1aff6f9 227char *base_name(const char *file_name);
3c1150ce 228#endif
daf03c53 229char *abs_file_name(const char *dir, const char *file_name);
29d4af60 230
fee0c963
BP
231char *follow_symlinks(const char *filename);
232
c69ee87c 233void ignore(bool x OVS_UNUSED);
fe9d0898
BP
234\f
235/* Bitwise tests. */
0ee140fb 236
d43d314e
BP
237/* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
238#if __GNUC__ >= 4
0ee140fb 239static inline int
d43d314e 240raw_ctz(uint64_t n)
0ee140fb 241{
d43d314e
BP
242 /* With GCC 4.7 on 32-bit x86, if a 32-bit integer is passed as 'n', using
243 * a plain __builtin_ctzll() here always generates an out-of-line function
244 * call. The test below helps it to emit a single 'bsf' instruction. */
245 return (__builtin_constant_p(n <= UINT32_MAX) && n <= UINT32_MAX
246 ? __builtin_ctz(n)
247 : __builtin_ctzll(n));
0ee140fb 248}
8c947903
JR
249
250static inline int
251raw_clz64(uint64_t n)
252{
253 return __builtin_clzll(n);
254}
25f45143
GS
255#elif _MSC_VER
256static inline int
257raw_ctz(uint64_t n)
258{
259#ifdef _WIN64
99970e95 260 unsigned long r = 0;
25f45143
GS
261 _BitScanForward64(&r, n);
262 return r;
263#else
99970e95 264 unsigned long low = n, high, r = 0;
25f45143
GS
265 if (_BitScanForward(&r, low)) {
266 return r;
267 }
268 high = n >> 32;
269 _BitScanForward(&r, high);
270 return r + 32;
271#endif
272}
273
274static inline int
275raw_clz64(uint64_t n)
276{
277#ifdef _WIN64
99970e95 278 unsigned long r = 0;
25f45143
GS
279 _BitScanReverse64(&r, n);
280 return 63 - r;
281#else
99970e95 282 unsigned long low, high = n >> 32, r = 0;
25f45143
GS
283 if (_BitScanReverse(&r, high)) {
284 return 31 - r;
285 }
286 low = n;
287 _BitScanReverse(&r, low);
288 return 63 - r;
289#endif
290}
0ee140fb
BP
291#else
292/* Defined in util.c. */
d43d314e 293int raw_ctz(uint64_t n);
8c947903 294int raw_clz64(uint64_t n);
0ee140fb
BP
295#endif
296
297/* Returns the number of trailing 0-bits in 'n', or 32 if 'n' is 0. */
298static inline int
d578065e 299ctz32(uint32_t n)
0ee140fb
BP
300{
301 return n ? raw_ctz(n) : 32;
302}
303
cc4c738e
JR
304/* Returns the number of trailing 0-bits in 'n', or 64 if 'n' is 0. */
305static inline int
306ctz64(uint64_t n)
307{
d43d314e 308 return n ? raw_ctz(n) : 64;
cc4c738e 309}
18b9283b 310
8c947903
JR
311/* Returns the number of leading 0-bits in 'n', or 32 if 'n' is 0. */
312static inline int
313clz32(uint32_t n)
314{
315 return n ? raw_clz64(n) - 32 : 32;
316}
317
318/* Returns the number of leading 0-bits in 'n', or 64 if 'n' is 0. */
319static inline int
320clz64(uint64_t n)
321{
322 return n ? raw_clz64(n) : 64;
323}
324
325/* Given a word 'n', calculates floor(log_2('n')). This is equivalent
326 * to finding the bit position of the most significant one bit in 'n'. It is
327 * an error to call this function with 'n' == 0. */
328static inline int
329log_2_floor(uint64_t n)
330{
331 return 63 - raw_clz64(n);
332}
333
334/* Given a word 'n', calculates ceil(log_2('n')). It is an error to
335 * call this function with 'n' == 0. */
336static inline int
337log_2_ceil(uint64_t n)
338{
339 return log_2_floor(n) + !is_pow2(n);
340}
341
381657b3
JR
342/* unsigned int count_1bits(uint64_t x):
343 *
344 * Returns the number of 1-bits in 'x', between 0 and 64 inclusive. */
345#if UINTPTR_MAX == UINT64_MAX
346static inline unsigned int
347count_1bits(uint64_t x)
348{
349#if __GNUC__ >= 4 && __POPCNT__
350 return __builtin_popcountll(x);
351#else
352 /* This portable implementation is the fastest one we know of for 64
353 * bits, and about 3x faster than GCC 4.7 __builtin_popcountll(). */
354 const uint64_t h55 = UINT64_C(0x5555555555555555);
355 const uint64_t h33 = UINT64_C(0x3333333333333333);
356 const uint64_t h0F = UINT64_C(0x0F0F0F0F0F0F0F0F);
357 const uint64_t h01 = UINT64_C(0x0101010101010101);
358 x -= (x >> 1) & h55; /* Count of each 2 bits in-place. */
359 x = (x & h33) + ((x >> 2) & h33); /* Count of each 4 bits in-place. */
360 x = (x + (x >> 4)) & h0F; /* Count of each 8 bits in-place. */
361 return (x * h01) >> 56; /* Sum of all bytes. */
362#endif
363}
364#else /* Not 64-bit. */
365#if __GNUC__ >= 4 && __POPCNT__
c3cc4d2d 366static inline unsigned int
381657b3 367count_1bits_32__(uint32_t x)
c3cc4d2d 368{
c3cc4d2d 369 return __builtin_popcount(x);
381657b3 370}
c3cc4d2d 371#else
381657b3
JR
372#define NEED_COUNT_1BITS_8 1
373extern const uint8_t count_1bits_8[256];
374static inline unsigned int
375count_1bits_32__(uint32_t x)
376{
c3cc4d2d
JR
377 /* This portable implementation is the fastest one we know of for 32 bits,
378 * and faster than GCC __builtin_popcount(). */
379 return (count_1bits_8[x & 0xff] +
380 count_1bits_8[(x >> 8) & 0xff] +
381 count_1bits_8[(x >> 16) & 0xff] +
382 count_1bits_8[x >> 24]);
c3cc4d2d 383}
381657b3 384#endif
c3cc4d2d
JR
385static inline unsigned int
386count_1bits(uint64_t x)
387{
381657b3 388 return count_1bits_32__(x) + count_1bits_32__(x >> 32);
c3cc4d2d 389}
381657b3 390#endif
8c947903 391
fe9d0898
BP
392/* Returns the rightmost 1-bit in 'x' (e.g. 01011000 => 00001000), or 0 if 'x'
393 * is 0. */
394static inline uintmax_t
395rightmost_1bit(uintmax_t x)
396{
397 return x & -x;
398}
399
400/* Returns 'x' with its rightmost 1-bit changed to a zero (e.g. 01011000 =>
401 * 01010000), or 0 if 'x' is 0. */
402static inline uintmax_t
403zero_rightmost_1bit(uintmax_t x)
404{
405 return x & (x - 1);
406}
35bedb61 407
4ed47144
BP
408/* Returns the index of the rightmost 1-bit in 'x' (e.g. 01011000 => 3), or an
409 * undefined value if 'x' is 0. */
795b3288 410static inline int
4ed47144 411rightmost_1bit_idx(uint64_t x)
35bedb61 412{
4ed47144 413 return ctz64(x);
35bedb61
BP
414}
415
4ed47144
BP
416/* Returns the index of the leftmost 1-bit in 'x' (e.g. 01011000 => 6), or an
417 * undefined value if 'x' is 0. */
35bedb61 418static inline uint32_t
4ed47144 419leftmost_1bit_idx(uint64_t x)
35bedb61 420{
4ed47144 421 return log_2_floor(x);
35bedb61 422}
86f35fb5
JR
423
424/* Return a ovs_be32 prefix in network byte order with 'plen' highest bits set.
425 * Shift with 32 is undefined behavior, but we rather use 64-bit shift than
426 * compare. */
427static inline ovs_be32 be32_prefix_mask(int plen)
428{
429 return htonl((uint64_t)UINT32_MAX << (32 - plen));
430}
fe9d0898 431\f
53cb9c3e
JR
432bool is_all_zeros(const void *, size_t);
433bool is_all_ones(const void *, size_t);
df1a62cb 434bool is_all_byte(const void *, size_t, uint8_t byte);
ddc4f8e2
BP
435void bitwise_copy(const void *src, unsigned int src_len, unsigned int src_ofs,
436 void *dst, unsigned int dst_len, unsigned int dst_ofs,
437 unsigned int n_bits);
6cc7ea5e
BP
438void bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
439 unsigned int n_bits);
c2dd4932
BP
440void bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
441 unsigned int n_bits);
79a010aa
BP
442bool bitwise_is_all_zeros(const void *, unsigned int len, unsigned int ofs,
443 unsigned int n_bits);
099c06e3
BP
444unsigned int bitwise_scan(const void *, unsigned int len,
445 bool target, unsigned int start, unsigned int end);
95a92d5a
BP
446int bitwise_rscan(const void *, unsigned int len, bool target,
447 int start, int end);
ddc4f8e2
BP
448void bitwise_put(uint64_t value,
449 void *dst, unsigned int dst_len, unsigned int dst_ofs,
450 unsigned int n_bits);
451uint64_t bitwise_get(const void *src, unsigned int src_len,
452 unsigned int src_ofs, unsigned int n_bits);
95a92d5a
BP
453bool bitwise_get_bit(const void *src, unsigned int len, unsigned int ofs);
454void bitwise_put0(void *dst, unsigned int len, unsigned int ofs);
455void bitwise_put1(void *dst, unsigned int len, unsigned int ofs);
456void bitwise_put_bit(void *dst, unsigned int len, unsigned int ofs, bool);
457void bitwise_toggle_bit(void *dst, unsigned int len, unsigned int ofs);
75a75043 458
bdd7ecf5
JS
459/* Returns non-zero if the parameters have equal value. */
460static inline int
2ff8484b 461ovs_u128_equals(const ovs_u128 a, const ovs_u128 b)
bdd7ecf5 462{
2ff8484b 463 return (a.u64.hi == b.u64.hi) && (a.u64.lo == b.u64.lo);
bdd7ecf5
JS
464}
465
557344e3
JS
466/* Returns true if 'val' is 0. */
467static inline bool
2ff8484b 468ovs_u128_is_zero(const ovs_u128 val)
557344e3 469{
2ff8484b 470 return !(val.u64.hi || val.u64.lo);
557344e3
JS
471}
472
473/* Returns true if 'val' is all ones. */
474static inline bool
2ff8484b 475ovs_u128_is_ones(const ovs_u128 val)
557344e3 476{
2ff8484b 477 return ovs_u128_equals(val, OVS_U128_MAX);
557344e3
JS
478}
479
480/* Returns non-zero if the parameters have equal value. */
481static inline int
2ff8484b 482ovs_be128_equals(const ovs_be128 a, const ovs_be128 b)
557344e3 483{
2ff8484b 484 return (a.be64.hi == b.be64.hi) && (a.be64.lo == b.be64.lo);
557344e3
JS
485}
486
487/* Returns true if 'val' is 0. */
488static inline bool
2ff8484b 489ovs_be128_is_zero(const ovs_be128 val)
557344e3 490{
2ff8484b 491 return !(val.be64.hi || val.be64.lo);
557344e3
JS
492}
493
f2d105b5
JS
494static inline ovs_u128
495ovs_u128_and(const ovs_u128 a, const ovs_u128 b)
496{
497 ovs_u128 dst;
498
499 dst.u64.hi = a.u64.hi & b.u64.hi;
500 dst.u64.lo = a.u64.lo & b.u64.lo;
501
502 return dst;
503}
504
5fd2f418 505void xsleep(unsigned int seconds);
ca3cc1aa 506void xnanosleep(uint64_t nanoseconds);
124f09c9 507
20174b74
QM
508bool is_stdout_a_tty(void);
509
06f14c92
GS
510#ifdef _WIN32
511\f
315ea327 512char *ovs_format_message(int error);
06f14c92 513char *ovs_lasterror_to_string(void);
daa04db8 514int ftruncate(int fd, off_t length);
06f14c92
GS
515#endif
516
064af421
BP
517#ifdef __cplusplus
518}
519#endif
520
521#endif /* util.h */