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 "ovs-thread.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, '/');
344 assert_single_threaded();
346 program_name = slash ? slash + 1 : argv0;
348 free(program_version);
350 if (!strcmp(version, VERSION)) {
351 program_version = xasprintf("%s (Open vSwitch) "VERSION"\n"
353 program_name, date, time);
355 program_version = xasprintf("%s %s\n"
356 "Open vSwitch Library "VERSION"\n"
358 program_name, version, date, time);
362 /* Returns a pointer to a string describing the program version. The
363 * caller must not modify or free the returned string.
366 get_program_version(void)
368 return program_version;
371 /* Print the version information for the program. */
373 ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
375 printf("%s", program_version);
376 if (min_ofp || max_ofp) {
377 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
381 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
382 * line. Numeric offsets are also included, starting at 'ofs' for the first
383 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
384 * are also rendered alongside. */
386 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
387 uintptr_t ofs, bool ascii)
389 const uint8_t *buf = buf_;
390 const size_t per_line = 16; /* Maximum bytes per line. */
394 size_t start, end, n;
397 /* Number of bytes on this line. */
398 start = ofs % per_line;
400 if (end - start > size)
405 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
406 for (i = 0; i < start; i++)
407 fprintf(stream, " ");
409 fprintf(stream, "%02hhx%c",
410 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
413 for (; i < per_line; i++)
414 fprintf(stream, " ");
415 fprintf(stream, "|");
416 for (i = 0; i < start; i++)
417 fprintf(stream, " ");
418 for (; i < end; i++) {
419 int c = buf[i - start];
420 putc(c >= 32 && c < 127 ? c : '.', stream);
422 for (; i < per_line; i++)
423 fprintf(stream, " ");
424 fprintf(stream, "|");
426 fprintf(stream, "\n");
435 str_to_int(const char *s, int base, int *i)
438 bool ok = str_to_llong(s, base, &ll);
444 str_to_long(const char *s, int base, long *li)
447 bool ok = str_to_llong(s, base, &ll);
453 str_to_llong(const char *s, int base, long long *x)
455 int save_errno = errno;
458 *x = strtoll(s, &tail, base);
459 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
470 str_to_uint(const char *s, int base, unsigned int *u)
472 return str_to_int(s, base, (int *) u);
476 str_to_ulong(const char *s, int base, unsigned long *ul)
478 return str_to_long(s, base, (long *) ul);
482 str_to_ullong(const char *s, int base, unsigned long long *ull)
484 return str_to_llong(s, base, (long long *) ull);
487 /* Converts floating-point string 's' into a double. If successful, stores
488 * the double in '*d' and returns true; on failure, stores 0 in '*d' and
491 * Underflow (e.g. "1e-9999") is not considered an error, but overflow
492 * (e.g. "1e9999)" is. */
494 str_to_double(const char *s, double *d)
496 int save_errno = errno;
499 *d = strtod(s, &tail);
500 if (errno == EINVAL || (errno == ERANGE && *d != 0)
501 || tail == s || *tail != '\0') {
511 /* Returns the value of 'c' as a hexadecimal digit. */
516 case '0': case '1': case '2': case '3': case '4':
517 case '5': case '6': case '7': case '8': case '9':
543 /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
544 * UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is
545 * nonnull, '*ok' is set to true if the conversion succeeds or to false if a
546 * non-hex digit is detected. */
548 hexits_value(const char *s, size_t n, bool *ok)
554 for (i = 0; i < n; i++) {
555 int hexit = hexit_value(s[i]);
562 value = (value << 4) + hexit;
570 /* Returns the current working directory as a malloc()'d string, or a null
571 * pointer if the current working directory cannot be determined. */
578 /* Get maximum path length or at least a reasonable estimate. */
579 path_max = pathconf(".", _PC_PATH_MAX);
580 size = (path_max < 0 ? 1024
581 : path_max > 10240 ? 10240
584 /* Get current working directory. */
586 char *buf = xmalloc(size);
587 if (getcwd(buf, size)) {
588 return xrealloc(buf, strlen(buf) + 1);
592 if (error != ERANGE) {
593 VLOG_WARN("getcwd failed (%s)", strerror(error));
602 all_slashes_name(const char *s)
604 return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
609 /* Returns the directory name portion of 'file_name' as a malloc()'d string,
610 * similar to the POSIX dirname() function but thread-safe. */
612 dir_name(const char *file_name)
614 size_t len = strlen(file_name);
615 while (len > 0 && file_name[len - 1] == '/') {
618 while (len > 0 && file_name[len - 1] != '/') {
621 while (len > 0 && file_name[len - 1] == '/') {
624 return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
627 /* Returns the file name portion of 'file_name' as a malloc()'d string,
628 * similar to the POSIX basename() function but thread-safe. */
630 base_name(const char *file_name)
634 end = strlen(file_name);
635 while (end > 0 && file_name[end - 1] == '/') {
640 return all_slashes_name(file_name);
644 while (start > 0 && file_name[start - 1] != '/') {
648 return xmemdup0(file_name + start, end - start);
651 /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,
652 * returns an absolute path to 'file_name' considering it relative to 'dir',
653 * which itself must be absolute. 'dir' may be null or the empty string, in
654 * which case the current working directory is used.
656 * Returns a null pointer if 'dir' is null and getcwd() fails. */
658 abs_file_name(const char *dir, const char *file_name)
660 if (file_name[0] == '/') {
661 return xstrdup(file_name);
662 } else if (dir && dir[0]) {
663 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
664 return xasprintf("%s%s%s", dir, separator, file_name);
666 char *cwd = get_cwd();
668 char *abs_name = xasprintf("%s/%s", cwd, file_name);
677 /* Like readlink(), but returns the link name as a null-terminated string in
678 * allocated memory that the caller must eventually free (with free()).
679 * Returns NULL on error, in which case errno is set appropriately. */
681 xreadlink(const char *filename)
685 for (size = 64; ; size *= 2) {
686 char *buf = xmalloc(size);
687 ssize_t retval = readlink(filename, buf, size);
690 if (retval >= 0 && retval < size) {
703 /* Returns a version of 'filename' with symlinks in the final component
704 * dereferenced. This differs from realpath() in that:
706 * - 'filename' need not exist.
708 * - If 'filename' does exist as a symlink, its referent need not exist.
710 * - Only symlinks in the final component of 'filename' are dereferenced.
712 * The caller must eventually free the returned string (with free()). */
714 follow_symlinks(const char *filename)
720 fn = xstrdup(filename);
721 for (i = 0; i < 10; i++) {
725 if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) {
729 linkname = xreadlink(fn);
731 VLOG_WARN("%s: readlink failed (%s)", filename, strerror(errno));
735 if (linkname[0] == '/') {
736 /* Target of symlink is absolute so use it raw. */
739 /* Target of symlink is relative so add to 'fn''s directory. */
740 char *dir = dir_name(fn);
742 if (!strcmp(dir, ".")) {
745 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
746 next_fn = xasprintf("%s%s%s", dir, separator, linkname);
757 VLOG_WARN("%s: too many levels of symlinks", filename);
759 return xstrdup(filename);
762 /* Pass a value to this function if it is marked with
763 * __attribute__((warn_unused_result)) and you genuinely want to ignore
764 * its return value. (Note that every scalar type can be implicitly
765 * converted to bool.) */
766 void ignore(bool x OVS_UNUSED) { }
768 /* Returns an appropriate delimiter for inserting just before the 0-based item
769 * 'index' in a list that has 'total' items in it. */
771 english_list_delimiter(size_t index, size_t total)
773 return (index == 0 ? ""
774 : index < total - 1 ? ", "
775 : total > 2 ? ", and "
779 /* Given a 32 bit word 'n', calculates floor(log_2('n')). This is equivalent
780 * to finding the bit position of the most significant one bit in 'n'. It is
781 * an error to call this function with 'n' == 0. */
783 log_2_floor(uint32_t n)
787 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
788 #error "Someone screwed up the #includes."
789 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
790 return 31 - __builtin_clz(n);
795 #define BIN_SEARCH_STEP(BITS) \
796 if (n >= (1 << BITS)) { \
805 #undef BIN_SEARCH_STEP
811 /* Given a 32 bit word 'n', calculates ceil(log_2('n')). It is an error to
812 * call this function with 'n' == 0. */
814 log_2_ceil(uint32_t n)
816 return log_2_floor(n) + !is_pow2(n);
819 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
820 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
821 #error "Someone screwed up the #includes."
822 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
823 /* Defined inline in util.h. */
831 #define CTZ_STEP(X) \
848 /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
852 /* In my testing, this implementation is over twice as fast as any other
853 * portable implementation that I tried, including GCC 4.4
854 * __builtin_popcount(), although nonportable asm("popcnt") was over 50%
857 ((((X) & (1 << 0)) != 0) + \
858 (((X) & (1 << 1)) != 0) + \
859 (((X) & (1 << 2)) != 0) + \
860 (((X) & (1 << 3)) != 0) + \
861 (((X) & (1 << 4)) != 0) + \
862 (((X) & (1 << 5)) != 0) + \
863 (((X) & (1 << 6)) != 0) + \
864 (((X) & (1 << 7)) != 0))
865 #define INIT2(X) INIT1(X), INIT1((X) + 1)
866 #define INIT4(X) INIT2(X), INIT2((X) + 2)
867 #define INIT8(X) INIT4(X), INIT4((X) + 4)
868 #define INIT16(X) INIT8(X), INIT8((X) + 8)
869 #define INIT32(X) INIT16(X), INIT16((X) + 16)
870 #define INIT64(X) INIT32(X), INIT32((X) + 32)
872 static const uint8_t popcount8[256] = {
873 INIT64(0), INIT64(64), INIT64(128), INIT64(192)
876 return (popcount8[x & 0xff] +
877 popcount8[(x >> 8) & 0xff] +
878 popcount8[(x >> 16) & 0xff] +
882 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
884 is_all_zeros(const uint8_t *p, size_t n)
888 for (i = 0; i < n; i++) {
896 /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
898 is_all_ones(const uint8_t *p, size_t n)
902 for (i = 0; i < n; i++) {
910 /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
911 * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and
912 * 'dst' is 'dst_len' bytes long.
914 * If you consider all of 'src' to be a single unsigned integer in network byte
915 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
916 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
917 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
918 * 2], and so on. Similarly for 'dst'.
920 * Required invariants:
921 * src_ofs + n_bits <= src_len * 8
922 * dst_ofs + n_bits <= dst_len * 8
923 * 'src' and 'dst' must not overlap.
926 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
927 void *dst_, unsigned int dst_len, unsigned int dst_ofs,
930 const uint8_t *src = src_;
933 src += src_len - (src_ofs / 8 + 1);
936 dst += dst_len - (dst_ofs / 8 + 1);
939 if (src_ofs == 0 && dst_ofs == 0) {
940 unsigned int n_bytes = n_bits / 8;
944 memcpy(dst, src, n_bytes);
951 uint8_t mask = (1 << n_bits) - 1;
952 *dst = (*dst & ~mask) | (*src & mask);
956 unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
957 unsigned int chunk = MIN(n_bits, max_copy);
958 uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
961 *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
978 /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is
979 * 'dst_len' bytes long.
981 * If you consider all of 'dst' to be a single unsigned integer in network byte
982 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
983 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
984 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
987 * Required invariant:
988 * dst_ofs + n_bits <= dst_len * 8
991 bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1000 dst += dst_len - (dst_ofs / 8 + 1);
1004 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1006 *dst &= ~(((1 << chunk) - 1) << dst_ofs);
1016 while (n_bits >= 8) {
1022 *dst &= ~((1 << n_bits) - 1);
1026 /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.
1027 * 'dst' is 'dst_len' bytes long.
1029 * If you consider all of 'dst' to be a single unsigned integer in network byte
1030 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1031 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1032 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1035 * Required invariant:
1036 * dst_ofs + n_bits <= dst_len * 8
1039 bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1040 unsigned int n_bits)
1042 uint8_t *dst = dst_;
1048 dst += dst_len - (dst_ofs / 8 + 1);
1052 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1054 *dst |= ((1 << chunk) - 1) << dst_ofs;
1064 while (n_bits >= 8) {
1070 *dst |= (1 << n_bits) - 1;
1074 /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits.
1075 * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len'
1078 * If you consider all of 'dst' to be a single unsigned integer in network byte
1079 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1080 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1081 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1084 * Required invariant:
1085 * dst_ofs + n_bits <= dst_len * 8
1088 bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
1089 unsigned int n_bits)
1091 const uint8_t *p = p_;
1097 p += len - (ofs / 8 + 1);
1101 unsigned int chunk = MIN(n_bits, 8 - ofs);
1103 if (*p & (((1 << chunk) - 1) << ofs)) {
1115 while (n_bits >= 8) {
1123 if (n_bits && *p & ((1 << n_bits) - 1)) {
1130 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
1131 * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
1133 * If you consider all of 'dst' to be a single unsigned integer in network byte
1134 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1135 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1136 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1139 * Required invariants:
1140 * dst_ofs + n_bits <= dst_len * 8
1144 bitwise_put(uint64_t value,
1145 void *dst, unsigned int dst_len, unsigned int dst_ofs,
1146 unsigned int n_bits)
1148 ovs_be64 n_value = htonll(value);
1149 bitwise_copy(&n_value, sizeof n_value, 0,
1150 dst, dst_len, dst_ofs,
1154 /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src',
1155 * which is 'src_len' bytes long.
1157 * If you consider all of 'src' to be a single unsigned integer in network byte
1158 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1159 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1160 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1163 * Required invariants:
1164 * src_ofs + n_bits <= src_len * 8
1168 bitwise_get(const void *src, unsigned int src_len,
1169 unsigned int src_ofs, unsigned int n_bits)
1171 ovs_be64 value = htonll(0);
1173 bitwise_copy(src, src_len, src_ofs,
1174 &value, sizeof value, 0,
1176 return ntohll(value);