2
0
mirror of https://github.com/openvswitch/ovs synced 2025-08-28 12:58:00 +00:00

81 Commits

Author SHA1 Message Date
Ben Pfaff
2fec66dbb5 util: New functions for allocating memory while avoiding false sharing.
This factors code out of fat-rwlock, making it easily usable by other code.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-03-18 16:41:09 -07:00
Ben Pfaff
e205100808 util: Move CACHE_LINE_SIZE here.
It will come in handy elsewhere in upcoming commits.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-03-13 12:45:15 -07:00
Ben Pfaff
f6e984d794 util: New macro PAD_SIZE.
PAD_SIZE(x,y) is a little shorter and may have a more obvious meaning
than ROUND_UP(x,y) - x.

I intend to add more users in an upcoming comment.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Andy Zhou <azhou@nicira.com>
2014-03-13 12:45:12 -07:00
Gurucharan Shetty
daa04db864 ovsdb-server: Truncate file for Windows.
There is no ftruncate() in visual studio. There is a _chsize_s()
which has a similar functionality.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-03-12 09:14:54 -07:00
Gurucharan Shetty
315ea327a6 util: Pre-allocate buffer for ovs_lasterror_to_string().
This lets us call ovs_lasterror_to_string() and not having
to do an extra call of LocalFree() on the returned string.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-02-14 08:46:05 -08:00
Gurucharan Shetty
ac01d085e1 util: Make xreadlink a static function.
It is only used in util.c

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-02-13 14:09:30 -08:00
Gurucharan Shetty
06f14c92f6 util: A generic function to convert error to string for windows.
More users will be added in an upcoming commit.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2014-01-28 13:08:13 -08:00
Harold Lim
428b2eddc9 Rename NOT_REACHED to OVS_NOT_REACHED
This allows other libraries to use util.h that has already
defined NOT_REACHED.

Signed-off-by: Harold Lim <haroldl@vmware.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-12-17 13:16:39 -08:00
Harold Lim
037821cf99 Update openvswitch to allow linking from C++ projects
The input variable of ovs_scan is changed from 'template' to
'format'. template is a keyword in C++.

Signed-off-by: Harold Lim <haroldl@vmware.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-12-17 13:14:03 -08:00
Helmut Schaa
42efb1dc45 lib/util: Make some functions in util.c inline
str_to_uint, str_to_ulong and str_to_ullong are just wrappers
around the corresponding signed functions. Move these to util.h
and make them inline saving some library exports and letting
the compiler do some more magic.

Signed-off-by: Helmut Schaa <helmut.schaa@googlemail.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-12-13 09:09:18 -08:00
Jarno Rajahalme
381657b3ea lib/util: More portable use of builtin popcnt.
- Use the GCC predefined macro __POPCNT__ to detect the availability
  of fast __builtin_popcnt function.
- Use portable preprocessor macros to detect 64-bit build.
- Only define the 32-bit parts when needed and declare the
  count_1bits_8 at file scope to silence a warning.

This time I have tested all code paths to make sure no warnigns are
generated.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Reviewed-by: Simon Horman <horms@verge.net.au>
2013-12-12 08:27:41 -08:00
Ben Pfaff
4c2ed3450d util: Fix sparse warning.
Without this commit, sparse warns:

    lib/util.c:921:15: warning: symbol 'count_1bits_8' was not declared.
    Should it be static?

Introduced by commit c3cc4d2dd2658 (util: Better count_1bits().).

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
2013-12-10 17:40:33 -08:00
Jarno Rajahalme
c3cc4d2dd2 util: Better count_1bits().
Inline, use another well-known algorithm for 64-bit builds, and use
builtins when they are known to be fast at compile time.  A 32-bit
version of the alternate algorithm is slower than the existing
implementation, so the old one is used for 32-bit builds.  Inline
assembler would be a bit faster on 32-bit i7 build, but we use the GCC
builtin for portability.

It should be stressed builds for specific CPUs do not work on others
CPUs, and that OVS build system or runtime does not currently support
CPU detection.

Speed improvement v.s. existing implementation / GCC 4.7
__builtin_popcountll():

i386:         64%  (inlining)                         / 380%
i386 on i7:   240% (inlining + builtin)               / 820%
x86_64:       59%  (inlining + different algorithm)   / 190%
x86_64 on i7: 370% (inlining + builtin)               / 0%

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2013-12-06 12:55:27 -08:00
Jarno Rajahalme
8c94790303 lib/util: Add clz32() and clz64().
Count leading zeroes using builtin if available.

Make log_2_floor() use raw_clz() and inline log_2_floor() and
log_2_ceil().

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2013-12-03 14:53:15 -08:00
Jarno Rajahalme
d578065e7d lib/util: Rename ctz() as ctz32().
ctz() returns 32 for zero input, and we already have ctz64(),
so it makes sense to rename ctz() as ctz32().

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2013-12-03 14:32:18 -08:00
Alin Serdean
34582733d9 Avoid printf type modifiers not supported by MSVC C runtime library.
The MSVC C library printf() implementation does not support the 'z', 't',
'j', or 'hh' format specifiers.  This commit changes the Open vSwitch code
to avoid those format specifiers, switching to standard macros from
<inttypes.h> where available and inventing new macros resembling them
where necessary.  It also updates CodingStyle to specify the macros' use
and adds a Makefile rule to report violations.

Signed-off-by: Alin Serdean <aserdean@cloudbasesolutions.com>
Co-authored-by: Ben Pfaff <blp@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-11-25 23:38:59 -08:00
Ben Pfaff
fb9aefa3c4 util: Rename popcount to count_1bits
This avoids a conflict with NetBSD's strings.h/libc.
(http://netbsd.gw.com/cgi-bin/man-cgi?popcount++NetBSD-current)

The new name is suggested by Ben Pfaff.

Signed-off-by: YAMAMOTO Takashi <yamamoto@valinux.co.jp>
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@gmail.com>
2013-11-19 10:59:10 -08:00
Ben Pfaff
2a4ca27c26 util: Make popcount() handle 64-bit integers, not separate popcount64().
Having a single function that can do popcount() on any integer type is
easier for callers to get right.  The implementation is probably slower
if the caller actually provides a 32-bit (or shorter) integer, but the
only existing callers always provide a full 64-bit integer so this seems
unimportant for now.

This also restores use, in practice, of the optimized implementation of
population count.  (As the comment on popcount32() says, this version is
2x faster than __builtin_popcount().)

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
2013-11-18 14:35:53 -08:00
Ben Pfaff
d43d314e62 util: Make raw_ctz() accept 64-bit integers.
Having a single function that can do raw_ctz() on any integer type is
easier for callers to get right, and there is no real downside in the
implementation.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
2013-11-18 14:35:21 -08:00
Jarno Rajahalme
cc4c738e12 lib/util: Add ctz64() and popcount64().
Add raw_ctz64(), ctz64(), and popcount64() using builtins when
available.

Signed-off By: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
2013-11-18 09:31:48 -08:00
Ben Pfaff
ed2232fc77 util: New function ovs_scan().
This new function is essentially an implementation of sscanf() with
slightly different behavior (see the comment) that is more convenient for
Open vSwitch internal use.  Also, this implementation ought to work out of
the box on Windows, which has a defective sscanf() that lacks the 'hh'
modifier required to scan into a char variable.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-11-15 08:54:56 -08:00
Ben Pfaff
d710edc4c2 util: Allow set_subprogram_name() to take a printf() format string.
This will be convenient in an upcoming commit.

I had to add -Wno-format-zero-length to suppress a GCC warning about a
zero-length format string in this monitor_daemon() call:
    set_subprogram_name("");
I don't know what that warning is good for anyway, and I guess the Clang
developers don't either because Clang didn't warn.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ethan Jackson <ethan@nicira.com>
2013-11-02 06:59:07 -07:00
Flavio Leitner
878f197290 util: use gcc builtins to better check array sizes
GCC provides two useful builtin functions that can help
to improve array size checking during compilation.

This patch contains no functional changes, but it makes
it easier to detect mistakes.

Signed-off-by: Flavio Leitner <fbl@redhat.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-10-02 16:45:38 -07:00
Alex Wang
db5a101931 clang: Fix the alignment warning.
This commit fixes the warning issued by 'clang' when pointer is casted
to one with greater alignment.

Signed-off-by: Alex Wang <alexw@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-07-23 12:34:41 -07:00
Alex Wang
33e191a01b clang: Fix the "expression result unused" warning.
This commit makes macro function "ASSIGN_CONTAINER()" evaluates
to "(void)0". This is to avoid the 'clang' warning: "expression
result unused", since most of time, the final evaluated value
is not used.

Signed-off-by: Alex Wang <alexw@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-07-22 12:47:59 -07:00
Ben Pfaff
e93ab5531c util: New macros ROUND_UP_POW2, ROUND_DOWN_POW2.
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-07-17 13:32:36 -07:00
Ben Pfaff
bc9fb3a9cd util: Make subprogram_name thread-specific.
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-07-12 14:24:29 -07:00
Ben Pfaff
5fcbed7479 New function ovs_strerror() as a thread-safe replacement for strerror().
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-06-28 16:09:37 -07:00
Alex Wang
4e022ec09e Create specific types for ofp and odp port
Until now, datapath ports and openflow ports were both represented by
unsigned integers of various sizes. With implicit conversions, etc., it is
easy to mix them up and use one where the other is expected.  This commit
creates two typedefs, ofp_port_t and odp_port_t.  Both of these two types
are marked by "__attribute__((bitwise))" so that sparse can be used to
detect any misuse.

Signed-off-by: Alex Wang <alexw@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-06-20 10:42:37 -07:00
Ethan Jackson
ccc096898c bfd: Implement Bidirectional Forwarding Detection.
Traditionally, Open vSwitch has used a variant of 802.1ag "CFM" for
interface liveness detection.  This has served us well until now,
but has several serious drawbacks which have steadily become more
inconvenient.  First, the 802.1ag standard does not implement
several useful features forcing us to (optionally) break
compatibility.  Second, 802.1.ag is not particularly popular
outside of carrier grade networking equipment.  Third, 802.1ag is
simply quite awkward.

In an effort to solve the aforementioned problems, this patch
implements BFD which is ubiquitous, well designed, straight
forward, and implements required features in a standard way.  The
initial cut of the protocol focuses on getting the basics of the
specification correct, leaving performance optimizations, and
advanced features as future work.  The protocol should be
considered experimental pending future testing.

Signed-off-by: Ethan Jackson <ethan@nicira.com>
2013-05-07 16:31:14 -07:00
YAMAMOTO Takashi
4ed6a64c61 change the type of popcount unsigned
it's a natural choice and compatible with a version found in NetBSD libc.

Signed-off-by: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-04-22 08:51:22 -07:00
Ben Pfaff
4749f73d12 util: Introduce ovs_assert macro.
An occasionally significant problem with the standard "assert" macro is
that it writes the failure message to stderr.  In our daemons, stderr is
generally redirected to /dev/null.  It's more useful to write the failure
message to the log, which is what the new ovs_assert macro introduced in
this patch does.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ethan Jackson <ethan@nicira.com>
2013-01-16 16:03:03 -08:00
Ben Pfaff
35bedb6171 util: New functions for the index of the leftmost or rightmost 1-bit.
These will acquire a user in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-11-03 21:13:08 -07:00
Ben Pfaff
fe9d089882 util: Group functions for bitwise tests.
This only moves code around for more logical grouping.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-11-03 21:13:08 -07:00
Ben Pfaff
0ee140fb69 util: New function raw_ctz().
This will acquire a user in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-09-04 12:24:27 -07:00
Ben Pfaff
a656cb7736 util: New function popcount().
This is the fastest portable implementation among the ones below, as
measured with GCC 4.4 on a Xeon X3430.  The measeured times were, in
seconds:

popcount1    25.6
popcount2     6.9 (but is not portable)
popcount3    31.4
popcount4    25.6
popcount5    61.6 (and is buggy)
popcount6    64.6
popcount7    32.3
popcount8    11.2

int
popcount1(unsigned int x)
{
    return __builtin_popcount(x);
}

int
popcount2(unsigned int x)
{
    unsigned int y;
    asm("popcnt %1, %0" : "=r" (y) : "g" (x));
    return y;
}

int
popcount3(unsigned int x)
{
    unsigned int n;

    n = (x >> 1) & 033333333333;
    x -= n;
    n = (n >> 1) & 033333333333;
    x -= n;
    x = (x + (x >> 3)) & 030707070707;
    return x % 63;
}

int
popcount4(unsigned int x)
{
    x -= (x >> 1) & 0x55555555;
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x + (x >> 4)) & 0x0f0f0f0f;
    x += x >> 8;
    x += x >> 16;
    return x & 0x3f;
}

int
popcount5(unsigned int x)
{
    int n;

    n = 0;
    while (x) {
        if (x & 0xf) {
            n += ((0xe9949440 >> (x & 0xf)) & 3) + 1;
        }
        x >>= 4;
    }
    return n;
}

int
popcount6(unsigned int x)
{
    int n;

    n = 0;
    while (x) {
        n += (0xe994 >> (x & 7)) & 3;
        x >>= 3;
    }
    return n;
}

int
popcount7(unsigned int x)
{
    static const int table[16] = {
        0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4
    };

    return (table[x & 0xf]
            + table[(x >> 4) & 0xf]
            + table[(x >> 8) & 0xf]
            + table[(x >> 12) & 0xf]
            + table[(x >> 16) & 0xf]
            + table[(x >> 20) & 0xf]
            + table[(x >> 24) & 0xf]
            + table[x >> 28]);
}

static int
popcount8(unsigned int x)
{
    ((((X) & (1 << 0)) != 0) +                  \
     (((X) & (1 << 1)) != 0) +                  \
     (((X) & (1 << 2)) != 0) +                  \
     (((X) & (1 << 3)) != 0) +                  \
     (((X) & (1 << 4)) != 0) +                  \
     (((X) & (1 << 5)) != 0) +                  \
     (((X) & (1 << 6)) != 0) +                  \
     (((X) & (1 << 7)) != 0))

    static const uint8_t popcount8[256] = {
        INIT64(0), INIT64(64), INIT64(128), INIT64(192)
    };

    return (popcount8[x & 0xff] +
            popcount8[(x >> 8) & 0xff] +
            popcount8[(x >> 16) & 0xff] +
            popcount8[x >> 24]);
}

int
main(void)
{
    unsigned long long int x;
    int n;

    n = 0;
    for (x = 0; x <= UINT32_MAX; x++) {
        n += popcount8(x);
    }
    printf("%d\n", n);

    return 0;
}

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-09-04 11:19:17 -07:00
Ben Pfaff
8472a3cecc util: New function zero_rightmost_1bit().
It's probably easier to understand
	x = zero_rightmost_1bit(x);
than
	x &= x - 1;

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-09-04 11:19:17 -07:00
Ben Pfaff
ebc56baa41 util: New macro CONST_CAST.
Casts are sometimes necessary.  One common reason that they are necessary
is for discarding a "const" qualifier.  However, this can impede
maintenance: if the type of the expression being cast changes, then the
presence of the cast can hide a necessary change in the code that does the
cast.  Using CONST_CAST, instead of a bare cast, makes these changes
visible.

Inspired by my own work elsewhere:
http://git.savannah.gnu.org/cgit/pspp.git/tree/src/libpspp/cast.h#n80

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-08-03 13:33:13 -07:00
Ben Pfaff
fee0c96314 util: New function follow_symlinks().
It will acquire its first user in an upcoming commit.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-08-01 10:55:57 -07:00
Ben Pfaff
d41d4b714d vlog: Add VLOG_ABORT() to log and call abort().
Whereas VLOG_FATAL() eventually calls exit(1), VLOG_ABORT()
eventually calls abort().  The key difference is that abort()
will cause a "monitor" process to restart, where exit(1) will
cause it to exit along with the monitored process.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-07-18 10:30:49 -07:00
Ben Pfaff
781dee0835 util: Introduce "subprogram_name" to identify subprocesses and threads.
This will be more useful later when we introduces "worker" subprocesses.
I don't have any current plans to introduce threading, but I can't
think of a disadvantage to wording this in a general manner.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-07-18 10:30:47 -07:00
Raju Subramanian
e0edde6fee Global replace of Nicira Networks.
Replaced all instances of Nicira Networks(, Inc) to Nicira, Inc.

Feature #10593
Signed-off-by: Raju Subramanian <rsubramanian@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-05-02 17:08:02 -07:00
Ben Pfaff
79a010aa80 util: New function bitwise_is_all_zeros().
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-04-13 21:12:48 -07:00
Ben Pfaff
c2dd49322f util: New function bitwise_one().
It's the obvious counterpart to bitwise_zero().

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-04-13 20:43:37 -07:00
Ethan Jackson
e385ef551a util: New function set_program_name_version().
With this function, users of the Open vSwitch libraries which
should not have the same version as Open vSwitch will have their
version displayed in unixctl and at the command line.

Signed-off-by: Ethan Jackson <ethan@nicira.com>
2012-04-06 12:46:15 -07:00
Ben Pfaff
27527aa09c Introduce ofputil_protocol, to abstract the protocol in use on a connection.
Open vSwitch already handles a few different protocol variations, but it
does so in a nonuniform manner:

  - OpenFlow 1.0 and NXM flow formats are distinguished using the NXFF_*
    constant values from nicira-ext.h.

  - The "flow_mod_table_id" feature setting is maintained in ofproto as
    part of an OpenFlow connection's (ofconn's) state.

There's no way to easily communicate this state among components.  It's
not much of a problem yet, but as more protocol support is added it seems
better to have an abstract, uniform way to represent protocol versions and
variants.  This commit implements that by introducing a new type
"enum ofputil_protocol".  Each ofputil_protocol value represents a variant
of a protocol version.  Each value is a separate bit, so a single enum
can also represent a set of protocols, which is often useful as well.

Reviewed-by: Simon Horman <horms@verge.net.au>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-03-07 13:59:02 -08:00
Ben Pfaff
6cc7ea5ea0 util: New function bitwise_zero().
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-02-01 14:15:11 -08:00
Ben Pfaff
ddc4f8e27f util: Move bitwise_copy() here, add new bitwise functions, add a test.
bitwise_copy() is generally useful so make it a general utility function.
Also add a comment.

Upcoming commits will introduce users for the new functions.

Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-02-01 14:15:07 -08:00
Ben Pfaff
300c69464c multipath: Correctly calculate number of required destination bits.
The previous calculation was wrong when n_links was a power of 2.

Reported-by: Paul Ingram <paul@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-01-12 16:51:29 -08:00
Ben Pfaff
320232ec7f dpif-linux: Fix build with certain 64-bit kernel/userspace combinations.
Unix 64-bit ABIs have two 64-bit types: "long" and "long long".  Either of
these is a reasonable choice for uint64_t (the userspace type) and for
__u64 (the kernel type).  Unfortunately, kernel and userspace don't
necessarily agree on the choice, and in fact the choice varies across
kernel versions and architectures.

Now that OVS is actually using kernel types in its kernel header, this
can make a difference: when __u64 and uint64_t differ, passing a pointer
to __u64 to OVS function get_unaligned_u64() yields a compiler warning
or error.

This commit fixes up the problems of this type found in OVS, by making
get_unaligned_u64() accept all 64-bit unsigned integer types, not just
whichever one happens to be uint64_t.  I didn't do the same thing for
put_unaligned_u64() because it is less likely to be a problem in
practice: usually, when userspace writes to kernel data structures it
does so with copies that it knows to be aligned, so that it's not
necessary to use put_unaligned_u64().

This problem won't occur for uint8_t, uint16_t, or uint32_t, since there is
only one reasonable choice of type for each.  It won't occur for ovs_be<N>
because OVS always defines those as aliases for the kernel's __be<N> types
when those are available.

This compiled cleanly for me in Scientific Linux 6.0 x86-64.

Reported-by: Pravin Shelar <pshelar@nicira.com>
2011-10-14 09:39:48 -07:00