2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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.
28 #include "byte-order.h"
30 #include "openvswitch/types.h"
33 VLOG_DEFINE_THIS_MODULE(util);
35 COVERAGE_DEFINE(util_xalloc);
37 /* argv[0] without directory names. */
38 const char *program_name;
40 /* Ordinarily "" but set to "monitor" for a monitor process or "worker" for a
42 const char *subprogram_name = "";
44 /* --version option output. */
45 static char *program_version;
48 ovs_assert_failure(const char *where, const char *function,
49 const char *condition)
51 /* Prevent an infinite loop (or stack overflow) in case VLOG_ABORT happens
52 * to trigger an assertion failure of its own. */
53 static int reentry = 0;
57 VLOG_ABORT("%s: assertion %s failed in %s()",
58 where, condition, function);
62 fprintf(stderr, "%s: assertion %s failed in %s()",
63 where, condition, function);
74 ovs_abort(0, "virtual memory exhausted");
78 xcalloc(size_t count, size_t size)
80 void *p = count && size ? calloc(count, size) : malloc(1);
81 COVERAGE_INC(util_xalloc);
91 return xcalloc(1, size);
97 void *p = malloc(size ? size : 1);
98 COVERAGE_INC(util_xalloc);
106 xrealloc(void *p, size_t size)
108 p = realloc(p, size ? size : 1);
109 COVERAGE_INC(util_xalloc);
117 xmemdup(const void *p_, size_t size)
119 void *p = xmalloc(size);
125 xmemdup0(const char *p_, size_t length)
127 char *p = xmalloc(length + 1);
128 memcpy(p, p_, length);
134 xstrdup(const char *s)
136 return xmemdup0(s, strlen(s));
140 xvasprintf(const char *format, va_list args)
146 va_copy(args2, args);
147 needed = vsnprintf(NULL, 0, format, args);
149 s = xmalloc(needed + 1);
151 vsnprintf(s, needed + 1, format, args2);
158 x2nrealloc(void *p, size_t *n, size_t s)
160 *n = *n == 0 ? 1 : 2 * *n;
161 return xrealloc(p, *n * s);
165 xasprintf(const char *format, ...)
170 va_start(args, format);
171 s = xvasprintf(format, args);
177 /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
178 * bytes from 'src' and doesn't return anything. */
180 ovs_strlcpy(char *dst, const char *src, size_t size)
183 size_t len = strnlen(src, size - 1);
184 memcpy(dst, src, len);
189 /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'.
190 * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte
191 * to every otherwise unused byte in 'dst'.
193 * Except for performance, the following call:
194 * ovs_strzcpy(dst, src, size);
195 * is equivalent to these two calls:
196 * memset(dst, '\0', size);
197 * ovs_strlcpy(dst, src, size);
199 * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.)
202 ovs_strzcpy(char *dst, const char *src, size_t size)
205 size_t len = strnlen(src, size - 1);
206 memcpy(dst, src, len);
207 memset(dst + len, '\0', size - len);
211 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
212 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
213 * the message inside parentheses. Then, terminates with abort().
215 * This function is preferred to ovs_fatal() in a situation where it would make
216 * sense for a monitoring process to restart the daemon.
218 * 'format' should not end with a new-line, because this function will add one
221 ovs_abort(int err_no, const char *format, ...)
225 va_start(args, format);
226 ovs_abort_valist(err_no, format, args);
229 /* Same as ovs_abort() except that the arguments are supplied as a va_list. */
231 ovs_abort_valist(int err_no, const char *format, va_list args)
233 ovs_error_valist(err_no, format, args);
237 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
238 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
239 * the message inside parentheses. Then, terminates with EXIT_FAILURE.
241 * 'format' should not end with a new-line, because this function will add one
244 ovs_fatal(int err_no, const char *format, ...)
248 va_start(args, format);
249 ovs_fatal_valist(err_no, format, args);
252 /* Same as ovs_fatal() except that the arguments are supplied as a va_list. */
254 ovs_fatal_valist(int err_no, const char *format, va_list args)
256 ovs_error_valist(err_no, format, args);
260 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
261 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
262 * the message inside parentheses.
264 * 'format' should not end with a new-line, because this function will add one
267 ovs_error(int err_no, const char *format, ...)
271 va_start(args, format);
272 ovs_error_valist(err_no, format, args);
276 /* Same as ovs_error() except that the arguments are supplied as a va_list. */
278 ovs_error_valist(int err_no, const char *format, va_list args)
280 int save_errno = errno;
282 if (subprogram_name[0]) {
283 fprintf(stderr, "%s(%s): ", program_name, subprogram_name);
285 fprintf(stderr, "%s: ", program_name);
288 vfprintf(stderr, format, args);
290 fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
297 /* Many OVS functions return an int which is one of:
300 * - EOF: end of file (not necessarily an error; depends on the function called)
302 * Returns the appropriate human-readable string. The caller must copy the
303 * string if it wants to hold onto it, as the storage may be overwritten on
304 * subsequent function calls.
307 ovs_retval_to_string(int retval)
309 static char unknown[48];
315 return strerror(retval);
318 return "End of file";
320 snprintf(unknown, sizeof unknown, "***unknown return value: %d***", retval);
324 /* Sets global "program_name" and "program_version" variables. Should
325 * be called at the beginning of main() with "argv[0]" as the argument
328 * 'version' should contain the version of the caller's program. If 'version'
329 * is the same as the VERSION #define, the caller is assumed to be part of Open
330 * vSwitch. Otherwise, it is assumed to be an external program linking against
331 * the Open vSwitch libraries.
333 * The 'date' and 'time' arguments should likely be called with
334 * "__DATE__" and "__TIME__" to use the time the binary was built.
335 * Alternatively, the "set_program_name" macro may be called to do this
339 set_program_name__(const char *argv0, const char *version, const char *date,
342 const char *slash = strrchr(argv0, '/');
343 program_name = slash ? slash + 1 : argv0;
345 free(program_version);
347 if (!strcmp(version, VERSION)) {
348 program_version = xasprintf("%s (Open vSwitch) "VERSION"\n"
350 program_name, date, time);
352 program_version = xasprintf("%s %s\n"
353 "Open vSwitch Library "VERSION"\n"
355 program_name, version, date, time);
359 /* Returns a pointer to a string describing the program version. The
360 * caller must not modify or free the returned string.
363 get_program_version(void)
365 return program_version;
368 /* Print the version information for the program. */
370 ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
372 printf("%s", program_version);
373 if (min_ofp || max_ofp) {
374 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
378 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
379 * line. Numeric offsets are also included, starting at 'ofs' for the first
380 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
381 * are also rendered alongside. */
383 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
384 uintptr_t ofs, bool ascii)
386 const uint8_t *buf = buf_;
387 const size_t per_line = 16; /* Maximum bytes per line. */
391 size_t start, end, n;
394 /* Number of bytes on this line. */
395 start = ofs % per_line;
397 if (end - start > size)
402 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
403 for (i = 0; i < start; i++)
404 fprintf(stream, " ");
406 fprintf(stream, "%02hhx%c",
407 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
410 for (; i < per_line; i++)
411 fprintf(stream, " ");
412 fprintf(stream, "|");
413 for (i = 0; i < start; i++)
414 fprintf(stream, " ");
415 for (; i < end; i++) {
416 int c = buf[i - start];
417 putc(c >= 32 && c < 127 ? c : '.', stream);
419 for (; i < per_line; i++)
420 fprintf(stream, " ");
421 fprintf(stream, "|");
423 fprintf(stream, "\n");
432 str_to_int(const char *s, int base, int *i)
435 bool ok = str_to_llong(s, base, &ll);
441 str_to_long(const char *s, int base, long *li)
444 bool ok = str_to_llong(s, base, &ll);
450 str_to_llong(const char *s, int base, long long *x)
452 int save_errno = errno;
455 *x = strtoll(s, &tail, base);
456 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
467 str_to_uint(const char *s, int base, unsigned int *u)
469 return str_to_int(s, base, (int *) u);
473 str_to_ulong(const char *s, int base, unsigned long *ul)
475 return str_to_long(s, base, (long *) ul);
479 str_to_ullong(const char *s, int base, unsigned long long *ull)
481 return str_to_llong(s, base, (long long *) ull);
484 /* Converts floating-point string 's' into a double. If successful, stores
485 * the double in '*d' and returns true; on failure, stores 0 in '*d' and
488 * Underflow (e.g. "1e-9999") is not considered an error, but overflow
489 * (e.g. "1e9999)" is. */
491 str_to_double(const char *s, double *d)
493 int save_errno = errno;
496 *d = strtod(s, &tail);
497 if (errno == EINVAL || (errno == ERANGE && *d != 0)
498 || tail == s || *tail != '\0') {
508 /* Returns the value of 'c' as a hexadecimal digit. */
513 case '0': case '1': case '2': case '3': case '4':
514 case '5': case '6': case '7': case '8': case '9':
540 /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
541 * UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is
542 * nonnull, '*ok' is set to true if the conversion succeeds or to false if a
543 * non-hex digit is detected. */
545 hexits_value(const char *s, size_t n, bool *ok)
551 for (i = 0; i < n; i++) {
552 int hexit = hexit_value(s[i]);
559 value = (value << 4) + hexit;
567 /* Returns the current working directory as a malloc()'d string, or a null
568 * pointer if the current working directory cannot be determined. */
575 /* Get maximum path length or at least a reasonable estimate. */
576 path_max = pathconf(".", _PC_PATH_MAX);
577 size = (path_max < 0 ? 1024
578 : path_max > 10240 ? 10240
581 /* Get current working directory. */
583 char *buf = xmalloc(size);
584 if (getcwd(buf, size)) {
585 return xrealloc(buf, strlen(buf) + 1);
589 if (error != ERANGE) {
590 VLOG_WARN("getcwd failed (%s)", strerror(error));
599 all_slashes_name(const char *s)
601 return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
606 /* Returns the directory name portion of 'file_name' as a malloc()'d string,
607 * similar to the POSIX dirname() function but thread-safe. */
609 dir_name(const char *file_name)
611 size_t len = strlen(file_name);
612 while (len > 0 && file_name[len - 1] == '/') {
615 while (len > 0 && file_name[len - 1] != '/') {
618 while (len > 0 && file_name[len - 1] == '/') {
621 return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
624 /* Returns the file name portion of 'file_name' as a malloc()'d string,
625 * similar to the POSIX basename() function but thread-safe. */
627 base_name(const char *file_name)
631 end = strlen(file_name);
632 while (end > 0 && file_name[end - 1] == '/') {
637 return all_slashes_name(file_name);
641 while (start > 0 && file_name[start - 1] != '/') {
645 return xmemdup0(file_name + start, end - start);
648 /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,
649 * returns an absolute path to 'file_name' considering it relative to 'dir',
650 * which itself must be absolute. 'dir' may be null or the empty string, in
651 * which case the current working directory is used.
653 * Returns a null pointer if 'dir' is null and getcwd() fails. */
655 abs_file_name(const char *dir, const char *file_name)
657 if (file_name[0] == '/') {
658 return xstrdup(file_name);
659 } else if (dir && dir[0]) {
660 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
661 return xasprintf("%s%s%s", dir, separator, file_name);
663 char *cwd = get_cwd();
665 char *abs_name = xasprintf("%s/%s", cwd, file_name);
674 /* Like readlink(), but returns the link name as a null-terminated string in
675 * allocated memory that the caller must eventually free (with free()).
676 * Returns NULL on error, in which case errno is set appropriately. */
678 xreadlink(const char *filename)
682 for (size = 64; ; size *= 2) {
683 char *buf = xmalloc(size);
684 ssize_t retval = readlink(filename, buf, size);
687 if (retval >= 0 && retval < size) {
700 /* Returns a version of 'filename' with symlinks in the final component
701 * dereferenced. This differs from realpath() in that:
703 * - 'filename' need not exist.
705 * - If 'filename' does exist as a symlink, its referent need not exist.
707 * - Only symlinks in the final component of 'filename' are dereferenced.
709 * The caller must eventually free the returned string (with free()). */
711 follow_symlinks(const char *filename)
717 fn = xstrdup(filename);
718 for (i = 0; i < 10; i++) {
722 if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) {
726 linkname = xreadlink(fn);
728 VLOG_WARN("%s: readlink failed (%s)", filename, strerror(errno));
732 if (linkname[0] == '/') {
733 /* Target of symlink is absolute so use it raw. */
736 /* Target of symlink is relative so add to 'fn''s directory. */
737 char *dir = dir_name(fn);
739 if (!strcmp(dir, ".")) {
742 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
743 next_fn = xasprintf("%s%s%s", dir, separator, linkname);
754 VLOG_WARN("%s: too many levels of symlinks", filename);
756 return xstrdup(filename);
759 /* Pass a value to this function if it is marked with
760 * __attribute__((warn_unused_result)) and you genuinely want to ignore
761 * its return value. (Note that every scalar type can be implicitly
762 * converted to bool.) */
763 void ignore(bool x OVS_UNUSED) { }
765 /* Returns an appropriate delimiter for inserting just before the 0-based item
766 * 'index' in a list that has 'total' items in it. */
768 english_list_delimiter(size_t index, size_t total)
770 return (index == 0 ? ""
771 : index < total - 1 ? ", "
772 : total > 2 ? ", and "
776 /* Given a 32 bit word 'n', calculates floor(log_2('n')). This is equivalent
777 * to finding the bit position of the most significant one bit in 'n'. It is
778 * an error to call this function with 'n' == 0. */
780 log_2_floor(uint32_t n)
784 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
785 #error "Someone screwed up the #includes."
786 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
787 return 31 - __builtin_clz(n);
792 #define BIN_SEARCH_STEP(BITS) \
793 if (n >= (1 << BITS)) { \
802 #undef BIN_SEARCH_STEP
808 /* Given a 32 bit word 'n', calculates ceil(log_2('n')). It is an error to
809 * call this function with 'n' == 0. */
811 log_2_ceil(uint32_t n)
813 return log_2_floor(n) + !IS_POW2(n);
816 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
817 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
818 #error "Someone screwed up the #includes."
819 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
820 /* Defined inline in util.h. */
828 #define CTZ_STEP(X) \
845 /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
849 /* In my testing, this implementation is over twice as fast as any other
850 * portable implementation that I tried, including GCC 4.4
851 * __builtin_popcount(), although nonportable asm("popcnt") was over 50%
854 ((((X) & (1 << 0)) != 0) + \
855 (((X) & (1 << 1)) != 0) + \
856 (((X) & (1 << 2)) != 0) + \
857 (((X) & (1 << 3)) != 0) + \
858 (((X) & (1 << 4)) != 0) + \
859 (((X) & (1 << 5)) != 0) + \
860 (((X) & (1 << 6)) != 0) + \
861 (((X) & (1 << 7)) != 0))
862 #define INIT2(X) INIT1(X), INIT1((X) + 1)
863 #define INIT4(X) INIT2(X), INIT2((X) + 2)
864 #define INIT8(X) INIT4(X), INIT4((X) + 4)
865 #define INIT16(X) INIT8(X), INIT8((X) + 8)
866 #define INIT32(X) INIT16(X), INIT16((X) + 16)
867 #define INIT64(X) INIT32(X), INIT32((X) + 32)
869 static const uint8_t popcount8[256] = {
870 INIT64(0), INIT64(64), INIT64(128), INIT64(192)
873 return (popcount8[x & 0xff] +
874 popcount8[(x >> 8) & 0xff] +
875 popcount8[(x >> 16) & 0xff] +
879 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
881 is_all_zeros(const uint8_t *p, size_t n)
885 for (i = 0; i < n; i++) {
893 /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
895 is_all_ones(const uint8_t *p, size_t n)
899 for (i = 0; i < n; i++) {
907 /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
908 * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and
909 * 'dst' is 'dst_len' bytes long.
911 * If you consider all of 'src' to be a single unsigned integer in network byte
912 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
913 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
914 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
915 * 2], and so on. Similarly for 'dst'.
917 * Required invariants:
918 * src_ofs + n_bits <= src_len * 8
919 * dst_ofs + n_bits <= dst_len * 8
920 * 'src' and 'dst' must not overlap.
923 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
924 void *dst_, unsigned int dst_len, unsigned int dst_ofs,
927 const uint8_t *src = src_;
930 src += src_len - (src_ofs / 8 + 1);
933 dst += dst_len - (dst_ofs / 8 + 1);
936 if (src_ofs == 0 && dst_ofs == 0) {
937 unsigned int n_bytes = n_bits / 8;
941 memcpy(dst, src, n_bytes);
948 uint8_t mask = (1 << n_bits) - 1;
949 *dst = (*dst & ~mask) | (*src & mask);
953 unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
954 unsigned int chunk = MIN(n_bits, max_copy);
955 uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
958 *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
975 /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is
976 * 'dst_len' bytes long.
978 * If you consider all of 'dst' to be a single unsigned integer in network byte
979 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
980 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
981 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
984 * Required invariant:
985 * dst_ofs + n_bits <= dst_len * 8
988 bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
997 dst += dst_len - (dst_ofs / 8 + 1);
1001 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1003 *dst &= ~(((1 << chunk) - 1) << dst_ofs);
1013 while (n_bits >= 8) {
1019 *dst &= ~((1 << n_bits) - 1);
1023 /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.
1024 * 'dst' is 'dst_len' bytes long.
1026 * If you consider all of 'dst' to be a single unsigned integer in network byte
1027 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1028 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1029 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1032 * Required invariant:
1033 * dst_ofs + n_bits <= dst_len * 8
1036 bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1037 unsigned int n_bits)
1039 uint8_t *dst = dst_;
1045 dst += dst_len - (dst_ofs / 8 + 1);
1049 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1051 *dst |= ((1 << chunk) - 1) << dst_ofs;
1061 while (n_bits >= 8) {
1067 *dst |= (1 << n_bits) - 1;
1071 /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits.
1072 * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len'
1075 * If you consider all of 'dst' to be a single unsigned integer in network byte
1076 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1077 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1078 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1081 * Required invariant:
1082 * dst_ofs + n_bits <= dst_len * 8
1085 bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
1086 unsigned int n_bits)
1088 const uint8_t *p = p_;
1094 p += len - (ofs / 8 + 1);
1098 unsigned int chunk = MIN(n_bits, 8 - ofs);
1100 if (*p & (((1 << chunk) - 1) << ofs)) {
1112 while (n_bits >= 8) {
1120 if (n_bits && *p & ((1 << n_bits) - 1)) {
1127 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
1128 * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
1130 * If you consider all of 'dst' to be a single unsigned integer in network byte
1131 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1132 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1133 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1136 * Required invariants:
1137 * dst_ofs + n_bits <= dst_len * 8
1141 bitwise_put(uint64_t value,
1142 void *dst, unsigned int dst_len, unsigned int dst_ofs,
1143 unsigned int n_bits)
1145 ovs_be64 n_value = htonll(value);
1146 bitwise_copy(&n_value, sizeof n_value, 0,
1147 dst, dst_len, dst_ofs,
1151 /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src',
1152 * which is 'src_len' bytes long.
1154 * If you consider all of 'src' to be a single unsigned integer in network byte
1155 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1156 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1157 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1160 * Required invariants:
1161 * src_ofs + n_bits <= src_len * 8
1165 bitwise_get(const void *src, unsigned int src_len,
1166 unsigned int src_ofs, unsigned int n_bits)
1168 ovs_be64 value = htonll(0);
1170 bitwise_copy(src, src_len, src_ofs,
1171 &value, sizeof value, 0,
1173 return ntohll(value);