aboutsummaryrefslogtreecommitdiff
path: root/lib/util.h
AgeCommit message (Collapse)Author
2013-10-02util: use gcc builtins to better check array sizesFlavio Leitner
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-07-23clang: Fix the alignment warning.Alex Wang
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-22clang: Fix the "expression result unused" warning.Alex Wang
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-17util: New macros ROUND_UP_POW2, ROUND_DOWN_POW2.Ben Pfaff
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-07-12util: Make subprogram_name thread-specific.Ben Pfaff
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-06-28New function ovs_strerror() as a thread-safe replacement for strerror().Ben Pfaff
Signed-off-by: Ben Pfaff <blp@nicira.com>
2013-06-20Create specific types for ofp and odp portAlex Wang
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-05-07bfd: Implement Bidirectional Forwarding Detection.Ethan Jackson
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-04-22change the type of popcount unsignedYAMAMOTO Takashi
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-01-16util: Introduce ovs_assert macro.Ben Pfaff
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>
2012-11-03util: New functions for the index of the leftmost or rightmost 1-bit.Ben Pfaff
These will acquire a user in an upcoming commit. Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-11-03util: Group functions for bitwise tests.Ben Pfaff
This only moves code around for more logical grouping. Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-09-04util: New function raw_ctz().Ben Pfaff
This will acquire a user in an upcoming commit. Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-09-04util: New function popcount().Ben Pfaff
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-04util: New function zero_rightmost_1bit().Ben Pfaff
It's probably easier to understand x = zero_rightmost_1bit(x); than x &= x - 1; Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-08-03util: New macro CONST_CAST.Ben Pfaff
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-01util: New function follow_symlinks().Ben Pfaff
It will acquire its first user in an upcoming commit. Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-07-18vlog: Add VLOG_ABORT() to log and call abort().Ben Pfaff
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-18util: Introduce "subprogram_name" to identify subprocesses and threads.Ben Pfaff
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-05-02Global replace of Nicira Networks.Raju Subramanian
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-04-13util: New function bitwise_is_all_zeros().Ben Pfaff
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-04-13util: New function bitwise_one().Ben Pfaff
It's the obvious counterpart to bitwise_zero(). Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-04-06util: New function set_program_name_version().Ethan Jackson
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-03-07Introduce ofputil_protocol, to abstract the protocol in use on a connection.Ben Pfaff
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-02-01util: New function bitwise_zero().Ben Pfaff
Signed-off-by: Ben Pfaff <blp@nicira.com>
2012-02-01util: Move bitwise_copy() here, add new bitwise functions, add a test.Ben Pfaff
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-01-12multipath: Correctly calculate number of required destination bits.Ben Pfaff
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>
2011-10-14dpif-linux: Fix build with certain 64-bit kernel/userspace combinations.Ben Pfaff
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-09-13Implement new "learn" action.Ben Pfaff
There are a few loose ends here. First, learning actions cause too much flow revalidation. Upcoming commits will fix that problem. The following additional issues have not yet been addressed: * Resource limits: nothing yet limits the maximum number of flows that can be learned. It is possible to exhaust all system memory. * Age reporting: there is no way to find out how soon a learned table entry is due to be evicted. To try this action out, here's a recipe for a very simple-minded MAC learning switch. It uses a 10-second MAC expiration time to make it easier to see what's going on: ovs-vsctl del-controller br0 ovs-ofctl del-flows br0 ovs-ofctl add-flow br0 "table=0 actions=learn(table=1, hard_timeout=10, \ NXM_OF_VLAN_TCI[0..11], NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[], \ output:NXM_OF_IN_PORT[]), resubmit(,1)" ovs-ofctl add-flow br0 "table=1 priority=0 actions=flood" You can then dump the MAC learning table with: ovs-ofctl dump-flows br0 table=1
2011-09-13packets: Add more utility functions for IPv4 and IPv6 addresses.Ben Pfaff
We had these functions scattered around the source tree anyway. packets.h is a good place to centralize them. I do plan to introduce some additional callers.
2011-08-04util: Introduce get_program_version function.Justin Pettit
Useful in an upcoming commit.
2011-07-22util: New function log_2_floor().Ben Pfaff
Calculates the position of the most significant bit in a 32 bit word.
2011-06-06util: New function for forming English lists.Ben Pfaff
This follows the rules I learned in school. Some locales may prefer to omit the comma before "and" in a list of three or more items.
2011-05-16util: Suppress build assertions when building with sparse.Ben Pfaff
sparse simply doesn't like our build assertions on packed structures. It seems that its ideas about struct packing are different from GCC's: ../lib/cfm.h:50:1: error: invalid bitfield width, -1. ../lib/packets.h:206:1: error: invalid bitfield width, -1. ../lib/packets.h:213:1: error: invalid bitfield width, -1. ../lib/packets.h:367:1: error: invalid bitfield width, -1. sparse isn't generating code so we don't really care how it lays out structures. We might as well just skip the assertions, as done here.
2011-05-13poll-loop: Make wakeup logging more portable and easier to understand.Ben Pfaff
Until now, when the poll_loop module's log level was turned up to "debug", it would log a backtrace of the call stack for the event that caused poll() to wake up in poll_block(). This was pretty useful from time to time to find out why ovs-vswitchd was using more CPU than expected, because we could find out what was causing it to wake up. But there were some issues. One is simply that the backtrace was printed as a series of hexadecimal numbers, so GDB or another debugger was needed to translate it into human-readable format. Compiler optimizations meant that even the human-readable backtrace wasn't, in my experience, as helpful as it could have been. And, of course, one needed to have the binary to interpret the backtrace. When the backtrace couldn't be interpreted or wasn't meaningful, there was essentially nothing to fall back on. This commit changes the way that "debug" logging for poll_block() wakeups works. Instead of logging a backtrace, it logs the source code file name and line number of the call to a poll_loop function, using __FILE__ and __LINE__. This is by itself much more meaningful than a sequence of hexadecimal numbers, since no additional interpretation is necessary. It can be useful even if the Open vSwitch version is only approximately known. In addition to the file and line, this commit adds, for wakeups caused by file descriptors, information about the file descriptor itself: what kind of file it is (regular file, directory, socket, etc.), the name of the file (on Linux only), and the local and remote endpoints for socket file descriptors. Here are a few examples of the new output format: 932-ms timeout at ../ofproto/in-band.c:507 [POLLIN] on fd 20 (192.168.0.20:35388<->192.168.0.3:6633) at ../lib/stream-fd.c:149 [POLLIN] on fd 7 (FIFO pipe:[48049]) at ../lib/fatal-signal.c:168
2011-04-04util: New function ovs_fatal_valist().Ben Pfaff
This commit adds a few initial users but more are coming up.
2011-02-24util: Avoid uninitialized pointer complaints from Coverity.Ben Pfaff
2011-02-23util: Make out_of_memory() call abort() instead of exit(EXIT_FAILURE).Ben Pfaff
exit(EXIT_FAILURE) will make a monitoring process (the one created by --monitor) think that it should exit. But the most likely reason for out_of_memory() to be called is a bug: probably, the process is trying to allocate more memory than there is available address space, e.g. something like malloc(-1). So it's better, in my opinion, to call abort() instead, so that the monitor process restarts the daemon and we are more likely to stay alive and, in addition, get a core dump and a useful bug report. I decided to implement a new general-purpose function for this purpose in case we run into other similar situations in the future. (I haven't actually run into this problem in practice. This commit is just speculation about what is better behavior.)
2011-02-22util: New function ovs_strzcpy().Ben Pfaff
Static analyzers hate strncpy(). This new function shares its property of initializing an entire buffer, without its nasty habit of failing to null-terminate long strings. Coverity #10697,10696,10695,10694,10693,10692,10691,10690.
2011-01-30util: New ovs_retval_to_string() function.Andrew Evans
Many OVS functions return 0, EOF, or errno. There are several places in the codebase where a return value is converted to a string. All must decide whether the return value is set, and if it is, whether it is an errno value, EOF, or otherwise invalid. This commit consolidates that code. Reviewed by Ben Pfaff.
2010-12-06util: Introduce ASSIGN_CONTAINER to make iteration macros easier to read.Ben Pfaff
2010-12-03util: Improve type-safety of OBJECT_CONTAINING.Ben Pfaff
2010-11-15util: Add function hexits_value() for parsing multiple hex digits.Ben Pfaff
Suggested-by: Justin Pettit <jpettit@nicira.com>
2010-11-10util: New function base_name().Ben Pfaff
2010-10-01util: New macro OBJECT_CONTAINING.Ben Pfaff
This macro is a variant on CONTAINER_OF that takes an object pointer instead of a type name as its second argument. In the following commit this will simplify many users of CONTAINER_OF.
2010-08-30treewide: Remove trailing whitespaceJoe Perches
Signed-off-by: Joe Perches <joe@perches.com> Acked-by: Simon Horman <horms@verge.net.au> Signed-off-by: Jesse Gross <jesse@nicira.com>
2010-05-07util: Fix GCC false-positive warning for CONTAINER_OF.Ben Pfaff
On sparc, GCC would issue the following warning for every use of CONTAINER_OF: warning: cast increases required alignment of target type This is a false positive: assuming that the data structure that it is applied to was allocated properly, the use of CONTAINER_OF to reach it is valid. And if it was not allocated properly, then code that accesses it other ways will have trouble too.
2010-03-18util: New functions get_cwd(), abs_file_name().Ben Pfaff
These will be used further in an upcoming commit.
2010-02-24Merge "master" into "next".Ben Pfaff
2010-02-12util: Remove unused macros NOT_TESTED, NOT_IMPLEMENTED.Ben Pfaff
Great ideas in theory, but we haven't used them.