poll-loop: Make poll_fd_wait_event() cross-platform.

This is helpful if we want to wait either on 'fd' for POSIX or
events for Windows.

For Windows, if both 'fd' and 'wevent' is specified, we associate
that event with the 'fd' using WSAEventSelect() in poll_block().
So any 'events' on that 'fd' will wake us up from WaitForMultipleObjects().

WSAEventSelect() does not understand  POLLIN, POLLOUT etc.  Instead the
macros it understands are FD_READ, FD_ACCEPT, FD_CONNECT, FD_CLOSE etc.
So we need to make that transition.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
socket-util: error number to string for sockets.

For winsock2 functions, error number has to be converted to string
using FormatMessage().

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
stream-ssl: Add support for Windows platform.

This commit creates events and through poll_fd_wait_event()
associates them with socket file descriptors to get woken up
from poll_block().

Some other changes:

* Windows does not have sys/fcntl.h but has a fcntl.h
On Linux, there is fctnl.h too.

* include <openssl/applink.c> to handle different C-Runtime linking
of OVS and openssl libraries as suggested at
https://www.openssl.org/support/faq.html#PROG2

The above include will not be needed if we compile Open vSwitch with
/MD compiler option.

* SHUT_RDWR is equivalent to SD_BOTH on Windows.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
tests: Add basic MPLS push test.

Signed-off-by: Joe Stringer <joestringer@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
flow: Copy MPLS label on mpls_push.

OpenFlow 1.3.3 spec (and earlier) specify that the default value for an
MPLS label should be copied from the outer header.

Signed-off-by: Joe Stringer <joestringer@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
tests: Fix MPLS test cases.

The "userspace" MPLS test case was checking the same things as the
"drop" test case, rather than checking to see that packets were being
sent to userspace. This patch makes the testsuite consistent with itself.

Signed-off-by: Joe Stringer <joestringer@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
bond: Change the way of assigning bond slave for unassigned bond entry.

Before this commit, ovs randomly selects a slave for unassigned
bond entry.  If the selected slave is not enabled, the active slave
is chosen instead.  In this commit, the slave is selected from the
list of all enabled slaves in a round-robin fashion.  This helps
improve the consistency of bond behavior when new flows are added.

Signed-off-by: Alex Wang <alexw@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
tunnel: Support all combinations of flow-based and specific tunnel matches.

There are 12 possible ways to specify a tunnel (2 * 2 * 3 == 12):

    - Specific in_key or flow-based (2 choices).

    - Specific ip_dst or flow-based (2 choices).

    - Specific ip_src, wildcarded, or flow-based (3 choices).

Until now, only 6 of the 12 possibilities have been supported.  We
have had a couple of requests to add another.  This commit adds all the
possibilities, so that we won't have to add the other 6 one by one.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Requested-by: Thomas Morin <thomas.morin@orange.com>
Acked-by: pritesh <pritesh.kothari@cisco.com>
meta-flow: Fix setting MFF_IP_FRAG.

mf_set_flow_value() was not setting 'flow->nw_frag' properly.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
netdev-dummy: Fix reconnecting.

The netdev-dummy unit test ran into the reconnect condition on Jarno's
machine. With his test environment, we found and fixed the bugs in
handling reconnect.

Co-authored-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Andy Zhou <azhou@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
netdev-dummy: Reduce reconnect back off timeout

netdev-dummy will mostly be used for testing and debugging over fairly
reliable connection. Reduce reconnect back off timeout in case the first
connect attempt failed.

Signed-off-by: Andy Zhou <azhou@nicira.com>
Acked-by: Jarno Rajahalme <jrajahalme@nicira.com>
ofproto: Lock for vlan splinters only if have them.

Reading the hmap count for determining if it is empty or not is thread
safe, so avoid locking when not necessary.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
ofproto: Optimize the case of a repeated learn action execution.

When the target flow exists already as intended, only the 'modified'
time needs to be updated.  Allow modification without taking the
'ofproto_mutex' by always using the 'rule->mutex' when accessing the
'modified' time.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
ofproto: Move 'rule->used' to the provider.

Rule's 'used' timestamp is updated at the same time with the other
stats.  So far the 'used' has been updated without proper protection,
which may lead to 'tearing' in 32-bit architectures, resulting in an
incorrect 'used' timestamp.  While in practice this is highly
improbable, it is still better to handle this correctly.

This is resolved by moving the 'used' field to the provider's stats,
so that whatever protection is used for updating packet and byte
counts, can be also used for both reading and writing of the 'used'
timestamp.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
datapath: Fix race.

ovs_vport_cmd_dump() did rcu_read_lock() only after getting the
datapath, which could have been deleted in between.  Resolved by
taking rcu_read_lock() before the get_dp() call.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Pravin B Shelar <pshelar@nicira.com>
ovs-thread: Add support for pthread adaptive mutex

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
ofproto-dpif: Use adaptive mutex for stats.

Stats critical sections are held for very short time, so it makes
sense to spin on them for a while rather than sleeping immediately.

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Signed-off-by: Ben Pfaff <blp@nicira.com>
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>
string: strerror_r() for windows.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
util: follow_symlinks() for windows.

Start with not supporting symbolic links for windows.
This is useful for an upcoming commit.

Signed-off-by: Gurucharan Shetty <gshetty@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>