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"
32 VLOG_DEFINE_THIS_MODULE(util);
34 COVERAGE_DEFINE(util_xalloc);
36 /* argv[0] without directory names. */
37 const char *program_name;
39 /* Ordinarily "" but set to "monitor" for a monitor process or "worker" for a
41 const char *subprogram_name = "";
43 /* --version option output. */
44 static char *program_version;
47 ovs_assert_failure(const char *where, const char *function,
48 const char *condition)
50 /* Prevent an infinite loop (or stack overflow) in case VLOG_ABORT happens
51 * to trigger an assertion failure of its own. */
52 static int reentry = 0;
56 VLOG_ABORT("%s: assertion %s failed in %s()",
57 where, condition, function);
61 fprintf(stderr, "%s: assertion %s failed in %s()",
62 where, condition, function);
73 ovs_abort(0, "virtual memory exhausted");
77 xcalloc(size_t count, size_t size)
79 void *p = count && size ? calloc(count, size) : malloc(1);
80 COVERAGE_INC(util_xalloc);
90 return xcalloc(1, size);
96 void *p = malloc(size ? size : 1);
97 COVERAGE_INC(util_xalloc);
105 xrealloc(void *p, size_t size)
107 p = realloc(p, size ? size : 1);
108 COVERAGE_INC(util_xalloc);
116 xmemdup(const void *p_, size_t size)
118 void *p = xmalloc(size);
124 xmemdup0(const char *p_, size_t length)
126 char *p = xmalloc(length + 1);
127 memcpy(p, p_, length);
133 xstrdup(const char *s)
135 return xmemdup0(s, strlen(s));
139 xvasprintf(const char *format, va_list args)
145 va_copy(args2, args);
146 needed = vsnprintf(NULL, 0, format, args);
148 s = xmalloc(needed + 1);
150 vsnprintf(s, needed + 1, format, args2);
157 x2nrealloc(void *p, size_t *n, size_t s)
159 *n = *n == 0 ? 1 : 2 * *n;
160 return xrealloc(p, *n * s);
164 xasprintf(const char *format, ...)
169 va_start(args, format);
170 s = xvasprintf(format, args);
176 /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
177 * bytes from 'src' and doesn't return anything. */
179 ovs_strlcpy(char *dst, const char *src, size_t size)
182 size_t len = strnlen(src, size - 1);
183 memcpy(dst, src, len);
188 /* Copies 'src' to 'dst'. Reads no more than 'size - 1' bytes from 'src'.
189 * Always null-terminates 'dst' (if 'size' is nonzero), and writes a zero byte
190 * to every otherwise unused byte in 'dst'.
192 * Except for performance, the following call:
193 * ovs_strzcpy(dst, src, size);
194 * is equivalent to these two calls:
195 * memset(dst, '\0', size);
196 * ovs_strlcpy(dst, src, size);
198 * (Thus, ovs_strzcpy() is similar to strncpy() without some of the pitfalls.)
201 ovs_strzcpy(char *dst, const char *src, size_t size)
204 size_t len = strnlen(src, size - 1);
205 memcpy(dst, src, len);
206 memset(dst + len, '\0', size - len);
210 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
211 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
212 * the message inside parentheses. Then, terminates with abort().
214 * This function is preferred to ovs_fatal() in a situation where it would make
215 * sense for a monitoring process to restart the daemon.
217 * 'format' should not end with a new-line, because this function will add one
220 ovs_abort(int err_no, const char *format, ...)
224 va_start(args, format);
225 ovs_abort_valist(err_no, format, args);
228 /* Same as ovs_abort() except that the arguments are supplied as a va_list. */
230 ovs_abort_valist(int err_no, const char *format, va_list args)
232 ovs_error_valist(err_no, format, args);
236 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
237 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
238 * the message inside parentheses. Then, terminates with EXIT_FAILURE.
240 * 'format' should not end with a new-line, because this function will add one
243 ovs_fatal(int err_no, const char *format, ...)
247 va_start(args, format);
248 ovs_fatal_valist(err_no, format, args);
251 /* Same as ovs_fatal() except that the arguments are supplied as a va_list. */
253 ovs_fatal_valist(int err_no, const char *format, va_list args)
255 ovs_error_valist(err_no, format, args);
259 /* Prints 'format' on stderr, formatting it like printf() does. If 'err_no' is
260 * nonzero, then it is formatted with ovs_retval_to_string() and appended to
261 * the message inside parentheses.
263 * 'format' should not end with a new-line, because this function will add one
266 ovs_error(int err_no, const char *format, ...)
270 va_start(args, format);
271 ovs_error_valist(err_no, format, args);
275 /* Same as ovs_error() except that the arguments are supplied as a va_list. */
277 ovs_error_valist(int err_no, const char *format, va_list args)
279 int save_errno = errno;
281 if (subprogram_name[0]) {
282 fprintf(stderr, "%s(%s): ", program_name, subprogram_name);
284 fprintf(stderr, "%s: ", program_name);
287 vfprintf(stderr, format, args);
289 fprintf(stderr, " (%s)", ovs_retval_to_string(err_no));
296 /* Many OVS functions return an int which is one of:
299 * - EOF: end of file (not necessarily an error; depends on the function called)
301 * Returns the appropriate human-readable string. The caller must copy the
302 * string if it wants to hold onto it, as the storage may be overwritten on
303 * subsequent function calls.
306 ovs_retval_to_string(int retval)
308 static char unknown[48];
314 return strerror(retval);
317 return "End of file";
319 snprintf(unknown, sizeof unknown, "***unknown return value: %d***", retval);
323 /* Sets global "program_name" and "program_version" variables. Should
324 * be called at the beginning of main() with "argv[0]" as the argument
327 * 'version' should contain the version of the caller's program. If 'version'
328 * is the same as the VERSION #define, the caller is assumed to be part of Open
329 * vSwitch. Otherwise, it is assumed to be an external program linking against
330 * the Open vSwitch libraries.
332 * The 'date' and 'time' arguments should likely be called with
333 * "__DATE__" and "__TIME__" to use the time the binary was built.
334 * Alternatively, the "set_program_name" macro may be called to do this
338 set_program_name__(const char *argv0, const char *version, const char *date,
341 const char *slash = strrchr(argv0, '/');
342 program_name = slash ? slash + 1 : argv0;
344 free(program_version);
346 if (!strcmp(version, VERSION)) {
347 program_version = xasprintf("%s (Open vSwitch) "VERSION"\n"
349 program_name, date, time);
351 program_version = xasprintf("%s %s\n"
352 "Open vSwitch Library "VERSION"\n"
354 program_name, version, date, time);
358 /* Returns a pointer to a string describing the program version. The
359 * caller must not modify or free the returned string.
362 get_program_version(void)
364 return program_version;
367 /* Print the version information for the program. */
369 ovs_print_version(uint8_t min_ofp, uint8_t max_ofp)
371 printf("%s", program_version);
372 if (min_ofp || max_ofp) {
373 printf("OpenFlow versions %#x:%#x\n", min_ofp, max_ofp);
377 /* Writes the 'size' bytes in 'buf' to 'stream' as hex bytes arranged 16 per
378 * line. Numeric offsets are also included, starting at 'ofs' for the first
379 * byte in 'buf'. If 'ascii' is true then the corresponding ASCII characters
380 * are also rendered alongside. */
382 ovs_hex_dump(FILE *stream, const void *buf_, size_t size,
383 uintptr_t ofs, bool ascii)
385 const uint8_t *buf = buf_;
386 const size_t per_line = 16; /* Maximum bytes per line. */
390 size_t start, end, n;
393 /* Number of bytes on this line. */
394 start = ofs % per_line;
396 if (end - start > size)
401 fprintf(stream, "%08jx ", (uintmax_t) ROUND_DOWN(ofs, per_line));
402 for (i = 0; i < start; i++)
403 fprintf(stream, " ");
405 fprintf(stream, "%02hhx%c",
406 buf[i - start], i == per_line / 2 - 1? '-' : ' ');
409 for (; i < per_line; i++)
410 fprintf(stream, " ");
411 fprintf(stream, "|");
412 for (i = 0; i < start; i++)
413 fprintf(stream, " ");
414 for (; i < end; i++) {
415 int c = buf[i - start];
416 putc(c >= 32 && c < 127 ? c : '.', stream);
418 for (; i < per_line; i++)
419 fprintf(stream, " ");
420 fprintf(stream, "|");
422 fprintf(stream, "\n");
431 str_to_int(const char *s, int base, int *i)
434 bool ok = str_to_llong(s, base, &ll);
440 str_to_long(const char *s, int base, long *li)
443 bool ok = str_to_llong(s, base, &ll);
449 str_to_llong(const char *s, int base, long long *x)
451 int save_errno = errno;
454 *x = strtoll(s, &tail, base);
455 if (errno == EINVAL || errno == ERANGE || tail == s || *tail != '\0') {
466 str_to_uint(const char *s, int base, unsigned int *u)
468 return str_to_int(s, base, (int *) u);
472 str_to_ulong(const char *s, int base, unsigned long *ul)
474 return str_to_long(s, base, (long *) ul);
478 str_to_ullong(const char *s, int base, unsigned long long *ull)
480 return str_to_llong(s, base, (long long *) ull);
483 /* Converts floating-point string 's' into a double. If successful, stores
484 * the double in '*d' and returns true; on failure, stores 0 in '*d' and
487 * Underflow (e.g. "1e-9999") is not considered an error, but overflow
488 * (e.g. "1e9999)" is. */
490 str_to_double(const char *s, double *d)
492 int save_errno = errno;
495 *d = strtod(s, &tail);
496 if (errno == EINVAL || (errno == ERANGE && *d != 0)
497 || tail == s || *tail != '\0') {
507 /* Returns the value of 'c' as a hexadecimal digit. */
512 case '0': case '1': case '2': case '3': case '4':
513 case '5': case '6': case '7': case '8': case '9':
539 /* Returns the integer value of the 'n' hexadecimal digits starting at 's', or
540 * UINT_MAX if one of those "digits" is not really a hex digit. If 'ok' is
541 * nonnull, '*ok' is set to true if the conversion succeeds or to false if a
542 * non-hex digit is detected. */
544 hexits_value(const char *s, size_t n, bool *ok)
550 for (i = 0; i < n; i++) {
551 int hexit = hexit_value(s[i]);
558 value = (value << 4) + hexit;
566 /* Returns the current working directory as a malloc()'d string, or a null
567 * pointer if the current working directory cannot be determined. */
574 /* Get maximum path length or at least a reasonable estimate. */
575 path_max = pathconf(".", _PC_PATH_MAX);
576 size = (path_max < 0 ? 1024
577 : path_max > 10240 ? 10240
580 /* Get current working directory. */
582 char *buf = xmalloc(size);
583 if (getcwd(buf, size)) {
584 return xrealloc(buf, strlen(buf) + 1);
588 if (error != ERANGE) {
589 VLOG_WARN("getcwd failed (%s)", strerror(error));
598 all_slashes_name(const char *s)
600 return xstrdup(s[0] == '/' && s[1] == '/' && s[2] != '/' ? "//"
605 /* Returns the directory name portion of 'file_name' as a malloc()'d string,
606 * similar to the POSIX dirname() function but thread-safe. */
608 dir_name(const char *file_name)
610 size_t len = strlen(file_name);
611 while (len > 0 && file_name[len - 1] == '/') {
614 while (len > 0 && file_name[len - 1] != '/') {
617 while (len > 0 && file_name[len - 1] == '/') {
620 return len ? xmemdup0(file_name, len) : all_slashes_name(file_name);
623 /* Returns the file name portion of 'file_name' as a malloc()'d string,
624 * similar to the POSIX basename() function but thread-safe. */
626 base_name(const char *file_name)
630 end = strlen(file_name);
631 while (end > 0 && file_name[end - 1] == '/') {
636 return all_slashes_name(file_name);
640 while (start > 0 && file_name[start - 1] != '/') {
644 return xmemdup0(file_name + start, end - start);
647 /* If 'file_name' starts with '/', returns a copy of 'file_name'. Otherwise,
648 * returns an absolute path to 'file_name' considering it relative to 'dir',
649 * which itself must be absolute. 'dir' may be null or the empty string, in
650 * which case the current working directory is used.
652 * Returns a null pointer if 'dir' is null and getcwd() fails. */
654 abs_file_name(const char *dir, const char *file_name)
656 if (file_name[0] == '/') {
657 return xstrdup(file_name);
658 } else if (dir && dir[0]) {
659 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
660 return xasprintf("%s%s%s", dir, separator, file_name);
662 char *cwd = get_cwd();
664 char *abs_name = xasprintf("%s/%s", cwd, file_name);
673 /* Like readlink(), but returns the link name as a null-terminated string in
674 * allocated memory that the caller must eventually free (with free()).
675 * Returns NULL on error, in which case errno is set appropriately. */
677 xreadlink(const char *filename)
681 for (size = 64; ; size *= 2) {
682 char *buf = xmalloc(size);
683 ssize_t retval = readlink(filename, buf, size);
686 if (retval >= 0 && retval < size) {
699 /* Returns a version of 'filename' with symlinks in the final component
700 * dereferenced. This differs from realpath() in that:
702 * - 'filename' need not exist.
704 * - If 'filename' does exist as a symlink, its referent need not exist.
706 * - Only symlinks in the final component of 'filename' are dereferenced.
708 * The caller must eventually free the returned string (with free()). */
710 follow_symlinks(const char *filename)
716 fn = xstrdup(filename);
717 for (i = 0; i < 10; i++) {
721 if (lstat(fn, &s) != 0 || !S_ISLNK(s.st_mode)) {
725 linkname = xreadlink(fn);
727 VLOG_WARN("%s: readlink failed (%s)", filename, strerror(errno));
731 if (linkname[0] == '/') {
732 /* Target of symlink is absolute so use it raw. */
735 /* Target of symlink is relative so add to 'fn''s directory. */
736 char *dir = dir_name(fn);
738 if (!strcmp(dir, ".")) {
741 char *separator = dir[strlen(dir) - 1] == '/' ? "" : "/";
742 next_fn = xasprintf("%s%s%s", dir, separator, linkname);
753 VLOG_WARN("%s: too many levels of symlinks", filename);
755 return xstrdup(filename);
758 /* Pass a value to this function if it is marked with
759 * __attribute__((warn_unused_result)) and you genuinely want to ignore
760 * its return value. (Note that every scalar type can be implicitly
761 * converted to bool.) */
762 void ignore(bool x OVS_UNUSED) { }
764 /* Returns an appropriate delimiter for inserting just before the 0-based item
765 * 'index' in a list that has 'total' items in it. */
767 english_list_delimiter(size_t index, size_t total)
769 return (index == 0 ? ""
770 : index < total - 1 ? ", "
771 : total > 2 ? ", and "
775 /* Given a 32 bit word 'n', calculates floor(log_2('n')). This is equivalent
776 * to finding the bit position of the most significant one bit in 'n'. It is
777 * an error to call this function with 'n' == 0. */
779 log_2_floor(uint32_t n)
783 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
784 #error "Someone screwed up the #includes."
785 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
786 return 31 - __builtin_clz(n);
791 #define BIN_SEARCH_STEP(BITS) \
792 if (n >= (1 << BITS)) { \
801 #undef BIN_SEARCH_STEP
807 /* Given a 32 bit word 'n', calculates ceil(log_2('n')). It is an error to
808 * call this function with 'n' == 0. */
810 log_2_ceil(uint32_t n)
812 return log_2_floor(n) + !is_pow2(n);
815 /* Returns the number of trailing 0-bits in 'n'. Undefined if 'n' == 0. */
816 #if !defined(UINT_MAX) || !defined(UINT32_MAX)
817 #error "Someone screwed up the #includes."
818 #elif __GNUC__ >= 4 && UINT_MAX == UINT32_MAX
819 /* Defined inline in util.h. */
827 #define CTZ_STEP(X) \
844 /* Returns the number of 1-bits in 'x', between 0 and 32 inclusive. */
848 /* In my testing, this implementation is over twice as fast as any other
849 * portable implementation that I tried, including GCC 4.4
850 * __builtin_popcount(), although nonportable asm("popcnt") was over 50%
853 ((((X) & (1 << 0)) != 0) + \
854 (((X) & (1 << 1)) != 0) + \
855 (((X) & (1 << 2)) != 0) + \
856 (((X) & (1 << 3)) != 0) + \
857 (((X) & (1 << 4)) != 0) + \
858 (((X) & (1 << 5)) != 0) + \
859 (((X) & (1 << 6)) != 0) + \
860 (((X) & (1 << 7)) != 0))
861 #define INIT2(X) INIT1(X), INIT1((X) + 1)
862 #define INIT4(X) INIT2(X), INIT2((X) + 2)
863 #define INIT8(X) INIT4(X), INIT4((X) + 4)
864 #define INIT16(X) INIT8(X), INIT8((X) + 8)
865 #define INIT32(X) INIT16(X), INIT16((X) + 16)
866 #define INIT64(X) INIT32(X), INIT32((X) + 32)
868 static const uint8_t popcount8[256] = {
869 INIT64(0), INIT64(64), INIT64(128), INIT64(192)
872 return (popcount8[x & 0xff] +
873 popcount8[(x >> 8) & 0xff] +
874 popcount8[(x >> 16) & 0xff] +
878 /* Returns true if the 'n' bytes starting at 'p' are zeros. */
880 is_all_zeros(const uint8_t *p, size_t n)
884 for (i = 0; i < n; i++) {
892 /* Returns true if the 'n' bytes starting at 'p' are 0xff. */
894 is_all_ones(const uint8_t *p, size_t n)
898 for (i = 0; i < n; i++) {
906 /* Copies 'n_bits' bits starting from bit 'src_ofs' in 'src' to the 'n_bits'
907 * starting from bit 'dst_ofs' in 'dst'. 'src' is 'src_len' bytes long and
908 * 'dst' is 'dst_len' bytes long.
910 * If you consider all of 'src' to be a single unsigned integer in network byte
911 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
912 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
913 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
914 * 2], and so on. Similarly for 'dst'.
916 * Required invariants:
917 * src_ofs + n_bits <= src_len * 8
918 * dst_ofs + n_bits <= dst_len * 8
919 * 'src' and 'dst' must not overlap.
922 bitwise_copy(const void *src_, unsigned int src_len, unsigned int src_ofs,
923 void *dst_, unsigned int dst_len, unsigned int dst_ofs,
926 const uint8_t *src = src_;
929 src += src_len - (src_ofs / 8 + 1);
932 dst += dst_len - (dst_ofs / 8 + 1);
935 if (src_ofs == 0 && dst_ofs == 0) {
936 unsigned int n_bytes = n_bits / 8;
940 memcpy(dst, src, n_bytes);
947 uint8_t mask = (1 << n_bits) - 1;
948 *dst = (*dst & ~mask) | (*src & mask);
952 unsigned int max_copy = 8 - MAX(src_ofs, dst_ofs);
953 unsigned int chunk = MIN(n_bits, max_copy);
954 uint8_t mask = ((1 << chunk) - 1) << dst_ofs;
957 *dst |= ((*src >> src_ofs) << dst_ofs) & mask;
974 /* Zeros the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'. 'dst' is
975 * 'dst_len' bytes long.
977 * If you consider all of 'dst' to be a single unsigned integer in network byte
978 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
979 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
980 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
983 * Required invariant:
984 * dst_ofs + n_bits <= dst_len * 8
987 bitwise_zero(void *dst_, unsigned int dst_len, unsigned dst_ofs,
996 dst += dst_len - (dst_ofs / 8 + 1);
1000 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1002 *dst &= ~(((1 << chunk) - 1) << dst_ofs);
1012 while (n_bits >= 8) {
1018 *dst &= ~((1 << n_bits) - 1);
1022 /* Sets to 1 all of the 'n_bits' bits starting from bit 'dst_ofs' in 'dst'.
1023 * 'dst' is 'dst_len' bytes long.
1025 * If you consider all of 'dst' to be a single unsigned integer in network byte
1026 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1027 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1028 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1031 * Required invariant:
1032 * dst_ofs + n_bits <= dst_len * 8
1035 bitwise_one(void *dst_, unsigned int dst_len, unsigned dst_ofs,
1036 unsigned int n_bits)
1038 uint8_t *dst = dst_;
1044 dst += dst_len - (dst_ofs / 8 + 1);
1048 unsigned int chunk = MIN(n_bits, 8 - dst_ofs);
1050 *dst |= ((1 << chunk) - 1) << dst_ofs;
1060 while (n_bits >= 8) {
1066 *dst |= (1 << n_bits) - 1;
1070 /* Scans the 'n_bits' bits starting from bit 'dst_ofs' in 'dst' for 1-bits.
1071 * Returns false if any 1-bits are found, otherwise true. 'dst' is 'dst_len'
1074 * If you consider all of 'dst' to be a single unsigned integer in network byte
1075 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1076 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1077 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1080 * Required invariant:
1081 * dst_ofs + n_bits <= dst_len * 8
1084 bitwise_is_all_zeros(const void *p_, unsigned int len, unsigned int ofs,
1085 unsigned int n_bits)
1087 const uint8_t *p = p_;
1093 p += len - (ofs / 8 + 1);
1097 unsigned int chunk = MIN(n_bits, 8 - ofs);
1099 if (*p & (((1 << chunk) - 1) << ofs)) {
1111 while (n_bits >= 8) {
1119 if (n_bits && *p & ((1 << n_bits) - 1)) {
1126 /* Copies the 'n_bits' low-order bits of 'value' into the 'n_bits' bits
1127 * starting at bit 'dst_ofs' in 'dst', which is 'dst_len' bytes long.
1129 * If you consider all of 'dst' to be a single unsigned integer in network byte
1130 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1131 * with value 1 in dst[dst_len - 1], bit 1 is the bit with value 2, bit 2 is
1132 * the bit with value 4, ..., bit 8 is the bit with value 1 in dst[dst_len -
1135 * Required invariants:
1136 * dst_ofs + n_bits <= dst_len * 8
1140 bitwise_put(uint64_t value,
1141 void *dst, unsigned int dst_len, unsigned int dst_ofs,
1142 unsigned int n_bits)
1144 ovs_be64 n_value = htonll(value);
1145 bitwise_copy(&n_value, sizeof n_value, 0,
1146 dst, dst_len, dst_ofs,
1150 /* Returns the value of the 'n_bits' bits starting at bit 'src_ofs' in 'src',
1151 * which is 'src_len' bytes long.
1153 * If you consider all of 'src' to be a single unsigned integer in network byte
1154 * order, then bit N is the bit with value 2**N. That is, bit 0 is the bit
1155 * with value 1 in src[src_len - 1], bit 1 is the bit with value 2, bit 2 is
1156 * the bit with value 4, ..., bit 8 is the bit with value 1 in src[src_len -
1159 * Required invariants:
1160 * src_ofs + n_bits <= src_len * 8
1164 bitwise_get(const void *src, unsigned int src_len,
1165 unsigned int src_ofs, unsigned int n_bits)
1167 ovs_be64 value = htonll(0);
1169 bitwise_copy(src, src_len, src_ofs,
1170 &value, sizeof value, 0,
1172 return ntohll(value);