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