aboutsummaryrefslogtreecommitdiff
path: root/net/openvswitch
diff options
context:
space:
mode:
authorKevin Lo <kevlo@FreeBSD.org>2015-04-08 07:58:20 +0000
committerKevin Lo <kevlo@FreeBSD.org>2015-04-08 07:58:20 +0000
commit47ab01b62b4c60d293765d253e5ddbf929678f37 (patch)
tree73628864a38eed268d3318b4a7e6bf2562f7a661 /net/openvswitch
parent4edad698c1b73025f16933b80f189999fe670924 (diff)
downloadports-47ab01b62b4c60d293765d253e5ddbf929678f37.tar.gz
ports-47ab01b62b4c60d293765d253e5ddbf929678f37.zip
Notes
Diffstat (limited to 'net/openvswitch')
-rw-r--r--net/openvswitch/Makefile37
-rw-r--r--net/openvswitch/distinfo4
-rw-r--r--net/openvswitch/files/ovs-vswitchd.in6
-rw-r--r--net/openvswitch/files/ovsdb-server.in21
-rw-r--r--net/openvswitch/files/patch-bsd-netdev.diff3190
-rw-r--r--net/openvswitch/files/patch-lib-netdev-bsd.c24
-rw-r--r--net/openvswitch/files/threaded.diff1292
-rw-r--r--net/openvswitch/pkg-plist155
8 files changed, 140 insertions, 4589 deletions
diff --git a/net/openvswitch/Makefile b/net/openvswitch/Makefile
index 61b14a3e9e32..cc2d565303b7 100644
--- a/net/openvswitch/Makefile
+++ b/net/openvswitch/Makefile
@@ -2,43 +2,26 @@
# $FreeBSD$
PORTNAME= openvswitch
-PORTVERSION= 1.7.1
+PORTVERSION= 2.3.1
CATEGORIES= net
MASTER_SITES= http://openvswitch.org/releases/
-MAINTAINER= ports@FreeBSD.org
+MAINTAINER= kevlo@FreeBSD.org
COMMENT= Production quality, multilayer virtual switch
LICENSE= APACHE20
-GNU_CONFIGURE= yes
-CONFIGURE_ARGS= PERL=${PREFIX}/bin/perl \
- SSL_CFLAGS="-I${OPENSSLINC}" \
- SSL_LIBS="-L${OPENSSLLIB} -lssl -lcrypto"
-USES= autoreconf gmake pkgconfig python
+USES= gmake libtool perl5 pkgconfig python
USE_OPENSSL= yes
+USE_LDCONFIG= yes
+GNU_CONFIGURE= yes
+CONFIGURE_ARGS= --enable-shared --localstatedir=/var \
+ --with-dbdir=/var/db/${PORTNAME} \
+ --with-openssl=${OPENSSLBASE}
USE_RC_SUBR= ovs-vswitchd ovsdb-server
-
-OPTIONS_DEFINE= THREADED
-THREADED_DESC= Experimental high-performance threading patch
-
-.include <bsd.port.pre.mk>
-
-.if ${PORT_OPTIONS:MTHREADED}
-CONFIGURE_ARGS+=--enable-threaded=yes
-.endif
-
-post-patch:
-.if ${PORT_OPTIONS:MTHREADED}
- @# We can't use EXTRA_PATCHES, since we need to apply this one
- @# after files/patch-bsd-netdef.diff, not before.
- ${PATCH} ${PATCH_ARGS} < ${FILESDIR}/threaded.diff
-.endif
+LDFLAGS+= -lpthread
post-install:
${INSTALL_DATA} ${WRKSRC}/vswitchd/vswitch.ovsschema ${STAGEDIR}${PREFIX}/share/openvswitch/
-#regression-test: build
-# @cd ${WRKSRC} && ${MAKE_CMD} check
-
-.include <bsd.port.post.mk>
+.include <bsd.port.mk>
diff --git a/net/openvswitch/distinfo b/net/openvswitch/distinfo
index d3f678ceea1c..a63194c9202b 100644
--- a/net/openvswitch/distinfo
+++ b/net/openvswitch/distinfo
@@ -1,2 +1,2 @@
-SHA256 (openvswitch-1.7.1.tar.gz) = a31fdae8400e9a1af89bd9031c83de15d4ba6dc2fb3ff92e5bf772577fa1c882
-SIZE (openvswitch-1.7.1.tar.gz) = 2153423
+SHA256 (openvswitch-2.3.1.tar.gz) = 21174901c311d54703b4a7f498be0c42e5d2cc68bdecb0090dbb555e2af4bcd2
+SIZE (openvswitch-2.3.1.tar.gz) = 3266916
diff --git a/net/openvswitch/files/ovs-vswitchd.in b/net/openvswitch/files/ovs-vswitchd.in
index 0ef893fb0842..250425ef8d6b 100644
--- a/net/openvswitch/files/ovs-vswitchd.in
+++ b/net/openvswitch/files/ovs-vswitchd.in
@@ -11,19 +11,17 @@
name="ovs_vswitchd"
rcvar=ovs_vswitchd_enable
-command="%%PREFIX%%/sbin/ovs-vswitchd"
-pidfile="%%PREFIX%%/var/run/ovs-vswitchd.pid"
+command="/usr/local/sbin/ovs-vswitchd"
start_precmd="ovs_vswitchd_precmd"
load_rc_config $name
: ${ovs_vswitchd_enable="NO"}
-: ${ovsdb_server_sock="%%PREFIX%%/var/run/openvswitch/db.sock"}
ovs_vswitchd_precmd() {
load_kld if_tap || return 1
}
-command_args="--detach --pidfile=$pidfile unix:$ovsdb_server_sock"
+command_args="--detach --pidfile"
run_rc_command "$1"
diff --git a/net/openvswitch/files/ovsdb-server.in b/net/openvswitch/files/ovsdb-server.in
index fecbdc12abc2..c57855010135 100644
--- a/net/openvswitch/files/ovsdb-server.in
+++ b/net/openvswitch/files/ovsdb-server.in
@@ -11,30 +11,29 @@
name="ovsdb_server"
rcvar=ovsdb_server_enable
-command="%%PREFIX%%/sbin/ovsdb-server"
-pidfile="%%PREFIX%%/var/run/ovsdb-server.pid"
+command="/usr/local/sbin/ovsdb-server"
+pidfile="/var/run/ovsdb-server.pid"
start_precmd="ovsdb_server_precmd"
load_rc_config $name
: ${ovsdb_server_enable="NO"}
-: ${ovsdb_server_sock="%%PREFIX%%/var/run/openvswitch/db.sock"}
-: ${ovsdb_server_database="%%PREFIX%%/etc/openvswitch/conf.db"}
+: ${ovsdb_server_sock="/var/run/openvswitch/db.sock"}
+: ${ovsdb_server_database="/var/db/openvswitch/conf.db"}
ovsdb_server_precmd() {
[ -e "`dirname $ovsdb_server_database`" ] || \
mkdir -p "`dirname $ovsdb_server_database`"
[ -e "$ovsdb_server_database" ] || \
- %%PREFIX%%/bin/ovsdb-tool create "$ovsdb_server_database" \
- %%PREFIX%%/share/openvswitch/vswitch.ovsschema
+ /usr/local/bin/ovsdb-tool create "$ovsdb_server_database" \
+ /usr/local/share/openvswitch/vswitch.ovsschema
}
command_args="--detach --pidfile=$pidfile \
--remote=punix:$ovsdb_server_sock \
- --remote=db:Open_vSwitch,manager_options \
- --private-key=db:SSL,private_key \
- --certificate=db:SSL,certificate \
- --bootstrap-ca-cert=db:SSL,ca_cert \
- $ovsdb_server_database"
+ --remote=db:Open_vSwitch,Open_vSwitch,manager_options \
+ --private-key=db:Open_vSwitch,SSL,private_key \
+ --certificate=db:Open_vSwitch,SSL,certificate \
+ --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert"
run_rc_command "$1"
diff --git a/net/openvswitch/files/patch-bsd-netdev.diff b/net/openvswitch/files/patch-bsd-netdev.diff
deleted file mode 100644
index 63c518863ddd..000000000000
--- a/net/openvswitch/files/patch-bsd-netdev.diff
+++ /dev/null
@@ -1,3190 +0,0 @@
-diff --git INSTALL INSTALL
-new file mode 100644
-index 0000000..671c4a4
---- /dev/null
-+++ INSTALL
-@@ -0,0 +1,341 @@
-+ How to Install Open vSwitch on Linux and FreeBSD
-+ ================================================
-+
-+This document describes how to build and install Open vSwitch on a
-+generic Linux or FreeBSD host. If you want to install Open vSwitch on
-+a Citrix XenServer, see INSTALL.XenServer instead.
-+
-+This version of Open vSwitch may be built manually with "configure"
-+and "make", as described below. You may also build Debian packages by
-+running "dpkg-buildpackage".
-+
-+Build Requirements
-+------------------
-+
-+To compile the userspace programs in the Open vSwitch distribution,
-+you will need the following software:
-+
-+ - A make program, e.g. GNU make. BSD make should also work.
-+
-+ - The GNU C compiler. We generally test with version 4.1, 4.2, or
-+ 4.3.
-+
-+ - pkg-config. We test with version 0.22.
-+
-+ - libssl, from OpenSSL, is optional but recommended if you plan to
-+ connect the Open vSwitch to an OpenFlow controller. libssl is
-+ required to establish confidentiality and authenticity in the
-+ connections from an Open vSwitch to an OpenFlow controller. If
-+ libssl is installed, then Open vSwitch will automatically build
-+ with support for it.
-+
-+To compile the kernel module on Linux, you must also install the
-+following. If you cannot build or install the kernel module, you may
-+use the userspace-only implementation, at a cost in performance. The
-+userspace implementation may also lack some features. Refer to
-+INSTALL.userspace for more information.
-+
-+ - A supported Linux kernel version. Please refer to README for a
-+ list of supported versions.
-+
-+ The Open vSwitch datapath requires bridging support
-+ (CONFIG_BRIDGE) to be built as a kernel module. (This is common
-+ in kernels provided by Linux distributions.) The bridge module
-+ must not be loaded or in use. If the bridge module is running
-+ (check with "lsmod | grep bridge"), you must remove it ("rmmod
-+ bridge") before starting the datapath.
-+
-+ For optional support of ingress policing, you must enable kernel
-+ configuration options NET_CLS_BASIC, NET_SCH_INGRESS, and
-+ NET_ACT_POLICE, either built-in or as modules. (NET_CLS_POLICE is
-+ obsolete and not needed.)
-+
-+ If GRE tunneling is being used it is recommended that the kernel
-+ be compiled with IPv6 support (CONFIG_IPV6). This allows for
-+ special handling (such as path MTU discovery) of IPv6 packets.
-+
-+ To configure HTB or HFSC quality of service with Open vSwitch,
-+ you must enable the respective configuration options.
-+
-+ To use Open vSwitch support for TAP devices, you must enable
-+ CONFIG_TUN.
-+
-+ - To build a kernel module, you need the same version of GCC that
-+ was used to build that kernel.
-+
-+ - A kernel build directory corresponding to the Linux kernel image
-+ the module is to run on. Under Debian and Ubuntu, for example,
-+ each linux-image package containing a kernel binary has a
-+ corresponding linux-headers package with the required build
-+ infrastructure.
-+
-+If you are working from a Git tree or snapshot (instead of from a
-+distribution tarball), or if you modify the Open vSwitch build system
-+or the database schema, you will also need the following software:
-+
-+ - Autoconf version 2.64 or later.
-+
-+ - Automake version 1.10 or later.
-+
-+ - Python 2.x, for x >= 4.
-+
-+If you modify the ovsdbmonitor tool, then you will also need the
-+following:
-+
-+ - pyuic4 from PyQt4 (http://www.riverbankcomputing.co.uk).
-+
-+To run the unit tests, you also need:
-+
-+ - Perl. Version 5.10.1 is known to work. Earlier versions should
-+ also work.
-+
-+If you modify the vswitchd database schema, then the E-R diagram in
-+the ovs-vswitchd.conf.db(5) manpage will be updated properly only if
-+you have the following:
-+
-+ - "dot" from graphviz (http://www.graphviz.org/).
-+
-+ - Perl. Version 5.10.1 is known to work. Earlier versions should
-+ also work.
-+
-+ - Python 2.x, for x >= 4.
-+
-+If you are going to extensively modify Open vSwitch, please consider
-+installing the following to obtain better warnings:
-+
-+ - "sparse" version 0.4.4 or later
-+ (http://www.kernel.org/pub/software/devel/sparse/dist/).
-+
-+ - GNU make.
-+
-+Installation Requirements
-+-------------------------
-+
-+The machine on which Open vSwitch is to be installed must have the
-+following software:
-+
-+ - libc compatible with the libc used for build.
-+
-+ - libssl compatible with the libssl used for build, if OpenSSL was
-+ used for the build.
-+
-+ - On Linux, the same kernel version configured as part of the build.
-+
-+ - For optional support of ingress policing on Linux, the "tc" program
-+ from iproute2 (part of all major distributions and available at
-+ http://www.linux-foundation.org/en/Net:Iproute2).
-+
-+On Linux you should ensure that /dev/urandom exists. To support TAP
-+devices, you must also ensure that /dev/net/tun exists.
-+
-+To run the ovsdbmonitor tool, the machine must also have the following
-+software:
-+
-+ - Python 2.x, for x >= 4.
-+
-+ - Python Twisted Conch.
-+
-+ - Python JSON.
-+
-+ - PySide or PyQt4.
-+
-+ - Python Zope interface module.
-+
-+(On Debian "lenny" the above can be installed with "apt-get install
-+python-json python-qt4 python-zopeinterface python-twisted-conch".)
-+
-+Building and Installing Open vSwitch for Linux or FreeBSD
-+=========================================================
-+
-+Once you have installed all the prerequisites listed above in the Base
-+Prerequisites section, follow the procedure below to build.
-+
-+1. If you pulled the sources directly from an Open vSwitch Git tree,
-+ run boot.sh in the top source directory:
-+
-+ % ./boot.sh
-+
-+2. In the top source directory, configure the package by running the
-+ configure script. You can usually invoke configure without any
-+ arguments:
-+
-+ % ./configure
-+
-+ By default all files are installed under /usr/local. If you want
-+ to install into, e.g., /usr and /var instead of /usr/local and
-+ /usr/local/var, add options as shown here:
-+
-+ % ./configure --prefix=/usr --localstatedir=/var
-+
-+ To use a specific C compiler for compiling Open vSwitch user
-+ programs, also specify it on the configure command line, like so:
-+
-+ % ./configure CC=gcc-4.2
-+
-+ To build the Linux kernel module, so that you can run the
-+ kernel-based switch, pass the location of the kernel build
-+ directory on --with-linux. For example, to build for a running
-+ instance of Linux:
-+
-+ % ./configure --with-linux=/lib/modules/`uname -r`/build
-+
-+ If you wish to build the kernel module for an architecture other
-+ than the architecture of the machine used for the build, you may
-+ specify the kernel architecture string using the KARCH variable
-+ when invoking the configure script. For example, to build for MIPS
-+ with Linux:
-+
-+ % ./configure --with-linux=/path/to/linux KARCH=mips
-+
-+ The configure script accepts a number of other options and honors
-+ additional environment variables. For a full list, invoke
-+ configure with the --help option.
-+
-+3. Run make in the top source directory:
-+
-+ % make
-+
-+ On FreeBSD you may need to use GNU make (gmake) or NetBSD make
-+ (bmake) instead of the native make.
-+
-+ For improved warnings if you installed "sparse" (see
-+ "Prerequisites"), add C=1 to the "make" command line.
-+
-+4. Become root by running "su" or another program.
-+
-+5. Run "make install" to install the executables and manpages into the
-+ running system, by default under /usr/local.
-+
-+6. If you built kernel modules, you may load them with "insmod", e.g.:
-+
-+ % insmod datapath/linux/openvswitch.ko
-+
-+ You may need to specify a full path to insmod, e.g. /sbin/insmod.
-+ To verify that the modules have been loaded, run "/sbin/lsmod" and
-+ check that openvswitch is listed.
-+
-+ If the "insmod" operation fails, look at the last few kernel log
-+ messages (e.g. with "dmesg | tail"):
-+
-+ - The message "openvswitch: exports duplicate symbol
-+ br_should_route_hook (owned by bridge)" means that the bridge
-+ module is loaded. Run "/sbin/rmmod bridge" to remove it.
-+
-+ If "/sbin/rmmod bridge" fails with "ERROR: Module bridge does
-+ not exist in /proc/modules", then the bridge is compiled into
-+ the kernel, rather than as a module. Open vSwitch does not
-+ support this configuration (see "Build Requirements", above).
-+
-+ - The message "openvswitch: exports duplicate symbol
-+ dp_ioctl_hook (owned by ofdatapath)" means that the ofdatapath
-+ module from the OpenFlow reference implementation is loaded.
-+ Run "/sbin/rmmod ofdatapath" to remove it. (You might have to
-+ delete any existing datapaths beforehand, using the "dpctl"
-+ program included with the OpenFlow reference implementation.
-+ "ovs-dpctl" will not work.)
-+
-+ - Otherwise, the most likely problem is that Open vSwitch was
-+ built for a kernel different from the one into which you are
-+ trying to load it. Run "modinfo" on openvswitch.ko and on
-+ a module built for the running kernel, e.g.:
-+
-+ % /sbin/modinfo openvswitch.ko
-+ % /sbin/modinfo /lib/modules/`uname -r`/kernel/net/bridge/bridge.ko
-+
-+ Compare the "vermagic" lines output by the two commands. If
-+ they differ, then Open vSwitch was built for the wrong kernel.
-+
-+ - If you decide to report a bug or ask a question related to
-+ module loading, please include the output from the "dmesg" and
-+ "modinfo" commands mentioned above.
-+
-+ There is an optional module parameter to openvswitch.ko called
-+ vlan_tso that enables TCP segmentation offload over VLANs on NICs
-+ that support it. Many drivers do not expose support for TSO on VLANs
-+ in a way that Open vSwitch can use but there is no way to detect
-+ whether this is the case. If you know that your particular driver can
-+ handle it (for example by testing sending large TCP packets over VLANs)
-+ then passing in a value of 1 may improve performance. Modules built for
-+ Linux kernels 2.6.37 and later, as well as specially patched versions
-+ of earlier kernels, do not need this and do not have this parameter. If
-+ you do not understand what this means or do not know if your driver
-+ will work, do not set this.
-+
-+7. Initialize the configuration database using ovsdb-tool, e.g.:
-+
-+ % mkdir -p /usr/local/etc/openvswitch
-+ % ovsdb-tool create /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
-+
-+Startup
-+=======
-+
-+Before starting ovs-vswitchd itself, you need to start its
-+configuration database, ovsdb-server. Each machine on which Open
-+vSwitch is installed should run its own copy of ovsdb-server.
-+Configure it to use the database you created during step 7 of
-+installation, above, to listen on a Unix domain socket, to connect to
-+any managers specified in the database itself, and to use the SSL
-+configuration in the database:
-+
-+ % ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock \
-+ --remote=db:Open_vSwitch,manager_options \
-+ --private-key=db:SSL,private_key \
-+ --certificate=db:SSL,certificate \
-+ --bootstrap-ca-cert=db:SSL,ca_cert \
-+ --pidfile --detach
-+
-+(If you built Open vSwitch without SSL support, then omit
-+--private-key, --certificate, and --bootstrap-ca-cert.)
-+
-+Then initialize the database using ovs-vsctl. This is only
-+necessary the first time after you create the database with
-+ovsdb-tool (but running it at any time is harmless):
-+
-+ % ovs-vsctl --no-wait init
-+
-+Then start the main Open vSwitch daemon, telling it to connect to the
-+same Unix domain socket:
-+
-+ % ovs-vswitchd --pidfile --detach
-+
-+Now you may use ovs-vsctl to set up bridges and other Open vSwitch
-+features. For example, to create a bridge named br0 and add ports
-+eth0 and vif1.0 to it:
-+
-+ % ovs-vsctl add-br br0
-+ % ovs-vsctl add-port br0 eth0
-+ % ovs-vsctl add-port br0 vif1.0
-+
-+Please refer to ovs-vsctl(8) for more details.
-+
-+Upgrading
-+=========
-+
-+When you upgrade Open vSwitch from one version to another, you should
-+also upgrade the database schema:
-+
-+1. Stop the Open vSwitch daemons, e.g.:
-+
-+ % kill `cd /usr/local/var/run/openvswitch && cat ovsdb-server.pid ovs-vswitchd.pid`
-+
-+2. Install the new Open vSwitch release.
-+
-+3. Upgrade the database, in one of the following two ways:
-+
-+ - If there is no important data in your database, then you may
-+ delete the database file and recreate it with ovsdb-tool,
-+ following the instructions under "Building and Installing Open
-+ vSwitch for Linux or FreeBSD".
-+
-+ - If you want to preserve the contents of your database, back it
-+ up first, then use "ovsdb-tool convert" to upgrade it, e.g.:
-+
-+ % ovsdb-tool convert /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
-+
-+4. Start the Open vSwitch daemons as described under "Building and
-+ Installing Open vSwitch for Linux or FreeBSD" above.
-+
-+Bug Reporting
-+-------------
-+
-+Please report problems to bugs@openvswitch.org.
-diff --git INSTALL.KVM INSTALL.KVM
-index 61620e5..18a7bd4 100644
---- INSTALL.KVM
-+++ INSTALL.KVM
-@@ -3,12 +3,12 @@
-
- This document describes how to use Open vSwitch with the Kernel-based
- Virtual Machine (KVM). This document assumes that you have read and
--followed INSTALL.Linux to get Open vSwitch setup on your Linux system.
-+followed INSTALL to get Open vSwitch setup on your Linux system.
-
- Setup
- -----
-
--First, follow the setup instructions in INSTALL.Linux to get a working
-+First, follow the setup instructions in INSTALL to get a working
- Open vSwitch installation.
-
- KVM uses tunctl to handle various bridging modes, which you can
-@@ -43,7 +43,7 @@ switch='br0'
- ovs-vsctl del-port ${switch} $1
- --------------------------------------------------------------------
-
--At the end of INSTALL.Linux, it describes basic usage of creating
-+At the end of INSTALL, it describes basic usage of creating
- bridges and ports. If you haven't already, create a bridge named
- br0 with the following command:
-
-diff --git INSTALL.Libvirt INSTALL.Libvirt
-index 4f31831..1bc45d5 100644
---- INSTALL.Libvirt
-+++ INSTALL.Libvirt
-@@ -4,7 +4,7 @@
- This document describes how to use Open vSwitch with Libvirt 0.9.11 or
- later. The Open vSwitch support in Libvirt 0.9.11 eliminates the need to
- use OVS Linux Bridge compatibility layer (brcompatd) and interface up/down
--scripts. This document assumes that you followed INSTALL.Linux or installed
-+scripts. This document assumes that you followed INSTALL or installed
- Open vSwitch from distribution packaging such as a .deb or .rpm. The Open
- vSwitch support is included by default in Libvirt 0.9.11. Consult
- www.libvirt.org for instructions on how to build the latest Libvirt, if your
-diff --git INSTALL.Linux INSTALL.Linux
-deleted file mode 100644
-index 78152b6..0000000
---- INSTALL.Linux
-+++ /dev/null
-@@ -1,338 +0,0 @@
-- How to Install Open vSwitch on Linux
-- ====================================
--
--This document describes how to build and install Open vSwitch on a
--generic Linux host. If you want to install Open vSwitch on a Citrix
--XenServer, see INSTALL.XenServer instead.
--
--This version of Open vSwitch may be built manually with "configure"
--and "make", as described below. You may also build Debian packages by
--running "dpkg-buildpackage".
--
--Build Requirements
--------------------
--
--To compile the userspace programs in the Open vSwitch distribution,
--you will need the following software:
--
-- - A make program, e.g. GNU make. BSD make should also work.
--
-- - The GNU C compiler. We generally test with version 4.1, 4.2, or
-- 4.3.
--
-- - pkg-config. We test with version 0.22.
--
-- - libssl, from OpenSSL, is optional but recommended if you plan to
-- connect the Open vSwitch to an OpenFlow controller. libssl is
-- required to establish confidentiality and authenticity in the
-- connections from an Open vSwitch to an OpenFlow controller. If
-- libssl is installed, then Open vSwitch will automatically build
-- with support for it.
--
--To compile the kernel module, you must also install the following. If
--you cannot build or install the kernel module, you may use the
--userspace-only implementation, at a cost in performance. The
--userspace implementation may also lack some features. Refer to
--INSTALL.userspace for more information.
--
-- - A supported Linux kernel version. Please refer to README for a
-- list of supported versions.
--
-- The Open vSwitch datapath requires bridging support
-- (CONFIG_BRIDGE) to be built as a kernel module. (This is common
-- in kernels provided by Linux distributions.) The bridge module
-- must not be loaded or in use. If the bridge module is running
-- (check with "lsmod | grep bridge"), you must remove it ("rmmod
-- bridge") before starting the datapath.
--
-- For optional support of ingress policing, you must enable kernel
-- configuration options NET_CLS_BASIC, NET_SCH_INGRESS, and
-- NET_ACT_POLICE, either built-in or as modules. (NET_CLS_POLICE is
-- obsolete and not needed.)
--
-- If GRE tunneling is being used it is recommended that the kernel
-- be compiled with IPv6 support (CONFIG_IPV6). This allows for
-- special handling (such as path MTU discovery) of IPv6 packets.
--
-- To configure HTB or HFSC quality of service with Open vSwitch,
-- you must enable the respective configuration options.
--
-- To use Open vSwitch support for TAP devices, you must enable
-- CONFIG_TUN.
--
-- - To build a kernel module, you need the same version of GCC that
-- was used to build that kernel.
--
-- - A kernel build directory corresponding to the Linux kernel image
-- the module is to run on. Under Debian and Ubuntu, for example,
-- each linux-image package containing a kernel binary has a
-- corresponding linux-headers package with the required build
-- infrastructure.
--
--If you are working from a Git tree or snapshot (instead of from a
--distribution tarball), or if you modify the Open vSwitch build system
--or the database schema, you will also need the following software:
--
-- - Autoconf version 2.64 or later.
--
-- - Automake version 1.10 or later.
--
-- - Python 2.x, for x >= 4.
--
--If you modify the ovsdbmonitor tool, then you will also need the
--following:
--
-- - pyuic4 from PyQt4 (http://www.riverbankcomputing.co.uk).
--
--To run the unit tests, you also need:
--
-- - Perl. Version 5.10.1 is known to work. Earlier versions should
-- also work.
--
--If you modify the vswitchd database schema, then the E-R diagram in
--the ovs-vswitchd.conf.db(5) manpage will be updated properly only if
--you have the following:
--
-- - "dot" from graphviz (http://www.graphviz.org/).
--
-- - Perl. Version 5.10.1 is known to work. Earlier versions should
-- also work.
--
-- - Python 2.x, for x >= 4.
--
--If you are going to extensively modify Open vSwitch, please consider
--installing the following to obtain better warnings:
--
-- - "sparse" version 0.4.4 or later
-- (http://www.kernel.org/pub/software/devel/sparse/dist/).
--
-- - GNU make.
--
--Installation Requirements
---------------------------
--
--The machine on which Open vSwitch is to be installed must have the
--following software:
--
-- - libc compatible with the libc used for build.
--
-- - libssl compatible with the libssl used for build, if OpenSSL was
-- used for the build.
--
-- - The Linux kernel version configured as part of the build.
--
-- - For optional support of ingress policing, the "tc" program from
-- iproute2 (part of all major distributions and available at
-- http://www.linux-foundation.org/en/Net:Iproute2).
--
--You should ensure that /dev/urandom exists. To support TAP devices,
--you must also ensure that /dev/net/tun exists.
--
--To run the ovsdbmonitor tool, the machine must also have the following
--software:
--
-- - Python 2.x, for x >= 4.
--
-- - Python Twisted Conch.
--
-- - Python JSON.
--
-- - PySide or PyQt4.
--
-- - Python Zope interface module.
--
--(On Debian "lenny" the above can be installed with "apt-get install
--python-json python-qt4 python-zopeinterface python-twisted-conch".)
--
--Building and Installing Open vSwitch for Linux
--==============================================
--
--Once you have installed all the prerequisites listed above in the Base
--Prerequisites section, follow the procedure below to build.
--
--1. If you pulled the sources directly from an Open vSwitch Git tree,
-- run boot.sh in the top source directory:
--
-- % ./boot.sh
--
--2. In the top source directory, configure the package by running the
-- configure script. You can usually invoke configure without any
-- arguments:
--
-- % ./configure
--
-- By default all files are installed under /usr/local. If you want
-- to install into, e.g., /usr and /var instead of /usr/local and
-- /usr/local/var, add options as shown here:
--
-- % ./configure --prefix=/usr --localstatedir=/var
--
-- To use a specific C compiler for compiling Open vSwitch user
-- programs, also specify it on the configure command line, like so:
--
-- % ./configure CC=gcc-4.2
--
-- To build the Linux kernel module, so that you can run the
-- kernel-based switch, pass the location of the kernel build
-- directory on --with-linux. For example, to build for a running
-- instance of Linux:
--
-- % ./configure --with-linux=/lib/modules/`uname -r`/build
--
-- If you wish to build the kernel module for an architecture other
-- than the architecture of the machine used for the build, you may
-- specify the kernel architecture string using the KARCH variable
-- when invoking the configure script. For example, to build for MIPS
-- with Linux:
--
-- % ./configure --with-linux=/path/to/linux KARCH=mips
--
-- The configure script accepts a number of other options and honors
-- additional environment variables. For a full list, invoke
-- configure with the --help option.
--
--3. Run make in the top source directory:
--
-- % make
--
-- For improved warnings if you installed "sparse" (see
-- "Prerequisites"), add C=1 to the "make" command line.
--
--4. Become root by running "su" or another program.
--
--5. Run "make install" to install the executables and manpages into the
-- running system, by default under /usr/local.
--
--6. If you built kernel modules, you may load them with "insmod", e.g.:
--
-- % insmod datapath/linux/openvswitch.ko
--
-- You may need to specify a full path to insmod, e.g. /sbin/insmod.
-- To verify that the modules have been loaded, run "/sbin/lsmod" and
-- check that openvswitch is listed.
--
-- If the "insmod" operation fails, look at the last few kernel log
-- messages (e.g. with "dmesg | tail"):
--
-- - The message "openvswitch: exports duplicate symbol
-- br_should_route_hook (owned by bridge)" means that the bridge
-- module is loaded. Run "/sbin/rmmod bridge" to remove it.
--
-- If "/sbin/rmmod bridge" fails with "ERROR: Module bridge does
-- not exist in /proc/modules", then the bridge is compiled into
-- the kernel, rather than as a module. Open vSwitch does not
-- support this configuration (see "Build Requirements", above).
--
-- - The message "openvswitch: exports duplicate symbol
-- dp_ioctl_hook (owned by ofdatapath)" means that the ofdatapath
-- module from the OpenFlow reference implementation is loaded.
-- Run "/sbin/rmmod ofdatapath" to remove it. (You might have to
-- delete any existing datapaths beforehand, using the "dpctl"
-- program included with the OpenFlow reference implementation.
-- "ovs-dpctl" will not work.)
--
-- - Otherwise, the most likely problem is that Open vSwitch was
-- built for a kernel different from the one into which you are
-- trying to load it. Run "modinfo" on openvswitch.ko and on
-- a module built for the running kernel, e.g.:
--
-- % /sbin/modinfo openvswitch.ko
-- % /sbin/modinfo /lib/modules/`uname -r`/kernel/net/bridge/bridge.ko
--
-- Compare the "vermagic" lines output by the two commands. If
-- they differ, then Open vSwitch was built for the wrong kernel.
--
-- - If you decide to report a bug or ask a question related to
-- module loading, please include the output from the "dmesg" and
-- "modinfo" commands mentioned above.
--
-- There is an optional module parameter to openvswitch.ko called
-- vlan_tso that enables TCP segmentation offload over VLANs on NICs
-- that support it. Many drivers do not expose support for TSO on VLANs
-- in a way that Open vSwitch can use but there is no way to detect
-- whether this is the case. If you know that your particular driver can
-- handle it (for example by testing sending large TCP packets over VLANs)
-- then passing in a value of 1 may improve performance. Modules built for
-- Linux kernels 2.6.37 and later, as well as specially patched versions
-- of earlier kernels, do not need this and do not have this parameter. If
-- you do not understand what this means or do not know if your driver
-- will work, do not set this.
--
--7. Initialize the configuration database using ovsdb-tool, e.g.:
--
-- % mkdir -p /usr/local/etc/openvswitch
-- % ovsdb-tool create /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
--
--Startup
--=======
--
--Before starting ovs-vswitchd itself, you need to start its
--configuration database, ovsdb-server. Each machine on which Open
--vSwitch is installed should run its own copy of ovsdb-server.
--Configure it to use the database you created during step 7 of
--installation, above, to listen on a Unix domain socket, to connect to
--any managers specified in the database itself, and to use the SSL
--configuration in the database:
--
-- % ovsdb-server --remote=punix:/usr/local/var/run/openvswitch/db.sock \
-- --remote=db:Open_vSwitch,manager_options \
-- --private-key=db:SSL,private_key \
-- --certificate=db:SSL,certificate \
-- --bootstrap-ca-cert=db:SSL,ca_cert \
-- --pidfile --detach
--
--(If you built Open vSwitch without SSL support, then omit
----private-key, --certificate, and --bootstrap-ca-cert.)
--
--Then initialize the database using ovs-vsctl. This is only
--necessary the first time after you create the database with
--ovsdb-tool (but running it at any time is harmless):
--
-- % ovs-vsctl --no-wait init
--
--Then start the main Open vSwitch daemon, telling it to connect to the
--same Unix domain socket:
--
-- % ovs-vswitchd --pidfile --detach
--
--Now you may use ovs-vsctl to set up bridges and other Open vSwitch
--features. For example, to create a bridge named br0 and add ports
--eth0 and vif1.0 to it:
--
-- % ovs-vsctl add-br br0
-- % ovs-vsctl add-port br0 eth0
-- % ovs-vsctl add-port br0 vif1.0
--
--Please refer to ovs-vsctl(8) for more details.
--
--Upgrading
--=========
--
--When you upgrade Open vSwitch from one version to another, you should
--also upgrade the database schema:
--
--1. Stop the Open vSwitch daemons, e.g.:
--
-- % kill `cd /usr/local/var/run/openvswitch && cat ovsdb-server.pid ovs-vswitchd.pid`
--
--2. Install the new Open vSwitch release.
--
--3. Upgrade the database, in one of the following two ways:
--
-- - If there is no important data in your database, then you may
-- delete the database file and recreate it with ovsdb-tool,
-- following the instructions under "Building and Installing Open
-- vSwitch for Linux".
--
-- - If you want to preserve the contents of your database, back it
-- up first, then use "ovsdb-tool convert" to upgrade it, e.g.:
--
-- % ovsdb-tool convert /usr/local/etc/openvswitch/conf.db vswitchd/vswitch.ovsschema
--
--4. Start the Open vSwitch daemons as described under "Building and
-- Installing Open vSwitch for Linux" above.
--
--Bug Reporting
---------------
--
--Please report problems to bugs@openvswitch.org.
-diff --git INSTALL.RHEL INSTALL.RHEL
-index 039a857..ff79c89 100644
---- INSTALL.RHEL
-+++ INSTALL.RHEL
-@@ -3,7 +3,7 @@
-
- This document describes how to build and install Open vSwitch on a Red
- Hat Enterprise Linux (RHEL) host. If you want to install Open vSwitch
--on a generic Linux host, see INSTALL.Linux instead.
-+on a generic Linux host, see INSTALL instead.
-
- We have tested these instructions with RHEL 5.6 and RHEL 6.0.
-
-diff --git INSTALL.SSL INSTALL.SSL
-index f322b41..8511b69 100644
---- INSTALL.SSL
-+++ INSTALL.SSL
-@@ -7,8 +7,8 @@ with OpenSSL. SSL support ensures integrity and confidentiality of
- the OpenFlow connections, increasing network security.
-
- This file explains how to configure an Open vSwitch to connect to an
--OpenFlow controller over SSL. Refer to INSTALL.Linux for instructions
--on building Open vSwitch with SSL support.
-+OpenFlow controller over SSL. Refer to INSTALL for instructions on
-+building Open vSwitch with SSL support.
-
- Open vSwitch uses TLS version 1.0 or later (TLSv1), as specified by
- RFC 2246, which is very similar to SSL version 3.0. TLSv1 was
-diff --git INSTALL.XenServer INSTALL.XenServer
-index e45d5bc..7a4dd76 100644
---- INSTALL.XenServer
-+++ INSTALL.XenServer
-@@ -3,7 +3,7 @@
-
- This document describes how to build and install Open vSwitch on a
- Citrix XenServer host. If you want to install Open vSwitch on a
--generic Linux host, see INSTALL.Linux instead.
-+generic Linux or BSD host, see INSTALL instead.
-
- These instructions have been tested with XenServer 5.6 FP1.
-
-@@ -19,8 +19,8 @@ RPMs for Citrix XenServer is the DDK VM available from Citrix.
- ./configure; make dist" in the Git tree. You cannot run this in
- the DDK VM, because it lacks tools that are necessary to bootstrap
- the Open vSwitch distribution. Instead, you must run this on a
-- machine that has the tools listed in INSTALL.Linux as prerequisites
-- for building from a Git tree.
-+ machine that has the tools listed in INSTALL as prerequisites for
-+ building from a Git tree.
-
- 2. Copy the distribution tarball into /usr/src/redhat/SOURCES inside
- the DDK VM.
-diff --git INSTALL.bridge INSTALL.bridge
-index dec09d8..af20bff 100644
---- INSTALL.bridge
-+++ INSTALL.bridge
-@@ -25,7 +25,7 @@ that the Open vSwitch kernel modules are loaded before the Linux
- kernel bridge module.
-
- 1. Build, install, and start up the Open vSwitch kernel modules and
-- userspace programs as described in INSTALL.Linux.
-+ userspace programs as described in INSTALL.
-
- It is important to run "make install", because some Open vSwitch
- programs expect to find files in locations selected at installation
-diff --git INSTALL.userspace INSTALL.userspace
-index 6e6fcd4..feb108c 100644
---- INSTALL.userspace
-+++ INSTALL.userspace
-@@ -17,7 +17,7 @@ Building and Installing
- -----------------------
-
- The requirements and procedure for building, installing, and
--configuring Open vSwitch are the same as those given in INSTALL.Linux.
-+configuring Open vSwitch are the same as those given in INSTALL.
- You may omit configuring, building, and installing the kernel module,
- and the related requirements.
-
-@@ -31,6 +31,10 @@ The tun device must also exist as /dev/net/tun. If it does not exist,
- then create /dev/net (if necessary) with "mkdir /dev/net", then create
- /dev/net/tun with "mknod /dev/net/tun c 10 200".
-
-+On FreeBSD, the userspace switch additionally requires the kernel
-+tap(4) driver to be available, either built into the kernel or loaded
-+as a module.
-+
- Using the Userspace Datapath with ovs-vswitchd
- ----------------------------------------------
-
-diff --git Makefile.am Makefile.am
-index 91adf05..cbe94a6 100644
---- Makefile.am
-+++ Makefile.am
-@@ -33,9 +33,9 @@ PYCOV_CLEAN_FILES = build-aux/check-structs,cover
- EXTRA_DIST = \
- CodingStyle \
- DESIGN \
-+ INSTALL \
- INSTALL.KVM \
- INSTALL.Libvirt \
-- INSTALL.Linux \
- INSTALL.RHEL \
- INSTALL.SSL \
- INSTALL.XenServer \
-diff --git NOTICE NOTICE
-index 912d7b8..dafd25f 100644
---- NOTICE
-+++ NOTICE
-@@ -4,6 +4,9 @@ available at http://www.apache.org/licenses/LICENSE-2.0.html
- Open vSwitch
- Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira, Inc.
-
-+Open vSwitch BSD port
-+Copyright (c) 2011 Gaetano Catalli
-+
- Apache Portable Runtime
- Copyright 2008 The Apache Software Foundation.
-
-diff --git README README
-index 251a92e..3ce45b7 100644
---- README
-+++ README
-@@ -88,7 +88,7 @@ Open vSwitch also provides some tools:
- What other documentation is available?
- --------------------------------------
-
--To install Open vSwitch on a regular Linux machine, read INSTALL.Linux.
-+To install Open vSwitch on a regular Linux machine, read INSTALL.
-
- To use Open vSwitch as a drop-in replacement for the Linux bridge,
- read INSTALL.bridge.
-@@ -99,7 +99,7 @@ or resource pool, read INSTALL.XenServer.
- To build RPMs for installing Open vSwitch on a Red Hat Enterprise
- Linux host, read INSTALL.RHEL.
-
--To use Open vSwitch with KVM on Linux, read INSTALL.Linux, then
-+To use Open vSwitch with KVM on Linux, read INSTALL, then
- INSTALL.KVM.
-
- To use Open vSwitch with Libvirt, read INSTALL.Libvirt.
-diff --git acinclude.m4 acinclude.m4
-index d33653d..51e784b 100644
---- acinclude.m4
-+++ acinclude.m4
-@@ -287,6 +287,17 @@ AC_DEFUN([OVS_CHECK_IF_PACKET],
- [Define to 1 if net/if_packet.h is available.])
- fi])
-
-+dnl Checks for net/if_dl.h.
-+AC_DEFUN([OVS_CHECK_IF_DL],
-+ [AC_CHECK_HEADER([net/if_dl.h],
-+ [HAVE_IF_DL=yes],
-+ [HAVE_IF_DL=no])
-+ AM_CONDITIONAL([HAVE_IF_DL], [test "$HAVE_IF_DL" = yes])
-+ if test "$HAVE_IF_DL" = yes; then
-+ AC_DEFINE([HAVE_IF_DL], [1],
-+ [Define to 1 if net/if_dl.h is available.])
-+ fi])
-+
- dnl Checks for buggy strtok_r.
- dnl
- dnl Some versions of glibc 2.7 has a bug in strtok_r when compiling
-diff --git configure.ac configure.ac
-index 1160cdb..5692b86 100644
---- configure.ac
-+++ configure.ac
-@@ -41,6 +41,7 @@ AC_SYS_LARGEFILE
- AC_SEARCH_LIBS([pow], [m])
- AC_SEARCH_LIBS([clock_gettime], [rt])
- AC_SEARCH_LIBS([timer_create], [rt])
-+AC_SEARCH_LIBS([pcap_open_live], [pcap])
-
- OVS_CHECK_COVERAGE
- OVS_CHECK_NDEBUG
-@@ -52,6 +53,7 @@ OVS_CHECK_PYUIC4
- OVS_CHECK_OVSDBMONITOR
- OVS_CHECK_DOT
- OVS_CHECK_IF_PACKET
-+OVS_CHECK_IF_DL
- OVS_CHECK_STRTOK_R
- AC_CHECK_MEMBERS([struct stat.st_mtim.tv_nsec, struct stat.st_mtimensec],
- [], [], [[#include <sys/stat.h>]])
-diff --git debian/copyright.in debian/copyright.in
-index 71aa680..16e5559 100644
---- debian/copyright.in
-+++ debian/copyright.in
-@@ -12,6 +12,7 @@ Upstream Copyright Holders:
- Copyright (c) 2010 Jean Tourrilhes - HP-Labs.
- Copyright (c) 2008,2009,2010 Citrix Systems, Inc.
- and authors listed above.
-+ Copyright (c) 2011 Gaetano Catalli
-
- License:
-
-diff --git lib/automake.mk lib/automake.mk
-index 6217526..13622b3 100644
---- lib/automake.mk
-+++ lib/automake.mk
-@@ -230,6 +230,14 @@ lib_libopenvswitch_a_SOURCES += \
- lib/route-table.h
- endif
-
-+if HAVE_IF_DL
-+lib_libopenvswitch_a_SOURCES += \
-+ lib/netdev-bsd.c \
-+ lib/rtbsd.c \
-+ lib/rtbsd.h \
-+ lib/route-table-bsd.c
-+endif
-+
- if HAVE_OPENSSL
- lib_libopenvswitch_a_SOURCES += lib/stream-ssl.c
- nodist_lib_libopenvswitch_a_SOURCES += lib/dhparams.c
-diff --git lib/netdev-bsd.c lib/netdev-bsd.c
-new file mode 100644
-index 0000000..0b1a37c
---- /dev/null
-+++ lib/netdev-bsd.c
-@@ -0,0 +1,1485 @@
-+/*
-+ * Copyright (c) 2011 Gaetano Catalli.
-+ *
-+ * Licensed under the Apache License, Version 2.0 (the "License");
-+ * you may not use this file except in compliance with the License.
-+ * You may obtain a copy of the License at:
-+ *
-+ * http://www.apache.org/licenses/LICENSE-2.0
-+ *
-+ * Unless required by applicable law or agreed to in writing, software
-+ * distributed under the License is distributed on an "AS IS" BASIS,
-+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-+ * See the License for the specific language governing permissions and
-+ * limitations under the License.
-+ */
-+
-+#include <config.h>
-+
-+#include <stdlib.h>
-+#include <config.h>
-+#include <assert.h>
-+#include <errno.h>
-+#include <fcntl.h>
-+#include <sys/types.h>
-+#include <sys/time.h>
-+#include <sys/ioctl.h>
-+#include <sys/socket.h>
-+#include <sys/sockio.h>
-+#include <ifaddrs.h>
-+#include <pcap/pcap.h>
-+#include <net/if.h>
-+#include <net/if_dl.h>
-+#include <net/if_media.h>
-+#include <net/if_tap.h>
-+#include <netinet/in.h>
-+#include <net/if_mib.h>
-+#include <poll.h>
-+#include <string.h>
-+#include <unistd.h>
-+#include <sys/sysctl.h>
-+
-+#include "rtbsd.h"
-+#include "coverage.h"
-+#include "dynamic-string.h"
-+#include "fatal-signal.h"
-+#include "netdev-provider.h"
-+#include "ofpbuf.h"
-+#include "openflow/openflow.h"
-+#include "packets.h"
-+#include "poll-loop.h"
-+#include "socket-util.h"
-+#include "shash.h"
-+#include "svec.h"
-+#include "vlog.h"
-+
-+VLOG_DEFINE_THIS_MODULE(netdev_bsd);
-+
-+
-+/*
-+ * This file implements objects to access interfaces.
-+ * Externally, interfaces are represented by two structures:
-+ * + struct netdev_dev, representing a network device,
-+ * containing e.g. name and a refcount;
-+ * We can have private variables by embedding the
-+ * struct netdev_dev into our own structure
-+ * (e.g. netdev_dev_bsd)
-+ *
-+ * + struct netdev, representing an instance of an open netdev_dev.
-+ * The structure contains a pointer to the 'struct netdev'
-+ * representing the device. Again, private information
-+ * such as file descriptor etc. are stored in our
-+ * own struct netdev_bsd which includes a struct netdev.
-+ *
-+ * Both 'struct netdev' and 'struct netdev_dev' are referenced
-+ * in containers which hold pointers to the data structures.
-+ * We can reach our own struct netdev_XXX_bsd by putting a
-+ * struct netdev_XXX within our own struct, and using CONTAINER_OF
-+ * to access the parent structure.
-+ */
-+struct netdev_bsd {
-+ struct netdev netdev;
-+
-+ int netdev_fd; /* Selectable file descriptor for the network device.
-+ This descriptor will be used for polling operations */
-+
-+ pcap_t *pcap_handle; /* Packet capture descriptor for a system network
-+ device */
-+};
-+
-+struct netdev_dev_bsd {
-+ struct netdev_dev netdev_dev;
-+ unsigned int cache_valid;
-+ unsigned int change_seq;
-+
-+ int ifindex;
-+ uint8_t etheraddr[ETH_ADDR_LEN];
-+ struct in_addr in4;
-+ struct in6_addr in6;
-+ int mtu;
-+ int carrier;
-+
-+ bool tap_opened;
-+ int tap_fd; /* TAP character device, if any */
-+};
-+
-+
-+enum {
-+ VALID_IFINDEX = 1 << 0,
-+ VALID_ETHERADDR = 1 << 1,
-+ VALID_IN4 = 1 << 2,
-+ VALID_IN6 = 1 << 3,
-+ VALID_MTU = 1 << 4,
-+ VALID_CARRIER = 1 << 5
-+};
-+
-+/* An AF_INET socket (used for ioctl operations). */
-+static int af_inet_sock = -1;
-+
-+#define PCAP_SNAPLEN 2048
-+
-+
-+/*
-+ * Notifier used to invalidate device informations in case of status change.
-+ *
-+ * It will be registered with a 'rtbsd_notifier_register()' when the first
-+ * device will be created with the call of either 'netdev_bsd_tap_create()' or
-+ * 'netdev_bsd_system_create()'.
-+ *
-+ * The callback associated with this notifier ('netdev_bsd_cache_cb()') will
-+ * invalidate cached information about the device.
-+ */
-+static struct rtbsd_notifier netdev_bsd_cache_notifier;
-+static int cache_notifier_refcount;
-+
-+static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
-+
-+static int netdev_bsd_do_ioctl(const struct netdev *, struct ifreq *,
-+ unsigned long cmd, const char *cmd_name);
-+static void destroy_tap(int fd, const char *name);
-+static int get_flags(const struct netdev *, int *flagsp);
-+static int set_flags(struct netdev *, int flags);
-+static int do_set_addr(struct netdev *netdev,
-+ int ioctl_nr, const char *ioctl_name,
-+ struct in_addr addr);
-+static int get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN]);
-+static int set_etheraddr(const char *netdev_name, int hwaddr_family,
-+ int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
-+static int get_ifindex(const struct netdev *, int *ifindexp);
-+
-+static int netdev_bsd_init(void);
-+
-+static bool
-+is_netdev_bsd_class(const struct netdev_class *netdev_class)
-+{
-+ return netdev_class->init == netdev_bsd_init;
-+}
-+
-+static struct netdev_bsd *
-+netdev_bsd_cast(const struct netdev *netdev)
-+{
-+ assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_get_dev(netdev))));
-+ return CONTAINER_OF(netdev, struct netdev_bsd, netdev);
-+}
-+
-+static struct netdev_dev_bsd *
-+netdev_dev_bsd_cast(const struct netdev_dev *netdev_dev)
-+{
-+ assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_dev)));
-+ return CONTAINER_OF(netdev_dev, struct netdev_dev_bsd, netdev_dev);
-+}
-+
-+/* Initialize the AF_INET socket used for ioctl operations */
-+static int
-+netdev_bsd_init(void)
-+{
-+ static int status = -1;
-+
-+ if (status >= 0) { /* already initialized */
-+ return status;
-+ }
-+
-+ af_inet_sock = socket(AF_INET, SOCK_DGRAM, 0);
-+ status = af_inet_sock >= 0 ? 0 : errno;
-+
-+ if (status) {
-+ VLOG_ERR("failed to create inet socket: %s", strerror(status));
-+ }
-+
-+ return status;
-+}
-+
-+/*
-+ * Perform periodic work needed by netdev. In BSD netdevs it checks for any
-+ * interface status changes, and eventually calls all the user callbacks.
-+ */
-+static void
-+netdev_bsd_run(void)
-+{
-+ rtbsd_notifier_run();
-+}
-+
-+/*
-+ * Arranges for poll_block() to wake up if the "run" member function needs to
-+ * be called.
-+ */
-+static void
-+netdev_bsd_wait(void)
-+{
-+ rtbsd_notifier_wait();
-+}
-+
-+static void
-+netdev_dev_bsd_changed(struct netdev_dev_bsd *dev)
-+{
-+ dev->change_seq++;
-+ if (!dev->change_seq) {
-+ dev->change_seq++;
-+ }
-+}
-+
-+/* Invalidate cache in case of interface status change. */
-+static void
-+netdev_bsd_cache_cb(const struct rtbsd_change *change,
-+ void *aux OVS_UNUSED)
-+{
-+ struct netdev_dev_bsd *dev;
-+
-+ if (change) {
-+ struct netdev_dev *base_dev = netdev_dev_from_name(change->if_name);
-+
-+ if (base_dev) {
-+ const struct netdev_class *netdev_class =
-+ netdev_dev_get_class(base_dev);
-+
-+ if (is_netdev_bsd_class(netdev_class)) {
-+ dev = netdev_dev_bsd_cast(base_dev);
-+ dev->cache_valid = 0;
-+ netdev_dev_bsd_changed(dev);
-+ }
-+ }
-+ } else {
-+ /*
-+ * XXX the API is lacking, we should be able to iterate on the list of
-+ * netdevs without having to store the info in a temp shash.
-+ */
-+ struct shash device_shash;
-+ struct shash_node *node;
-+
-+ shash_init(&device_shash);
-+ netdev_dev_get_devices(&netdev_bsd_class, &device_shash);
-+ SHASH_FOR_EACH (node, &device_shash) {
-+ dev = node->data;
-+ dev->cache_valid = 0;
-+ netdev_dev_bsd_changed(dev);
-+ }
-+ shash_destroy(&device_shash);
-+ }
-+}
-+
-+static int
-+cache_notifier_ref(void)
-+{
-+ int ret = 0;
-+
-+ if (!cache_notifier_refcount) {
-+ ret = rtbsd_notifier_register(&netdev_bsd_cache_notifier,
-+ netdev_bsd_cache_cb, NULL);
-+ if (ret) {
-+ return ret;
-+ }
-+ }
-+ cache_notifier_refcount++;
-+ return 0;
-+}
-+
-+static int
-+cache_notifier_unref(void)
-+{
-+ cache_notifier_refcount--;
-+ if (cache_notifier_refcount == 0) {
-+ rtbsd_notifier_unregister(&netdev_bsd_cache_notifier);
-+ }
-+ return 0;
-+}
-+
-+/* Allocate a netdev_dev_bsd structure */
-+static int
-+netdev_bsd_create_system(const struct netdev_class *class, const char *name,
-+ struct netdev_dev **netdev_devp)
-+{
-+ struct netdev_dev_bsd *netdev_dev;
-+ int error;
-+
-+ error = cache_notifier_ref();
-+ if (error) {
-+ return error;
-+ }
-+
-+ netdev_dev = xzalloc(sizeof *netdev_dev);
-+ netdev_dev->change_seq = 1;
-+ netdev_dev_init(&netdev_dev->netdev_dev, name, class);
-+ *netdev_devp = &netdev_dev->netdev_dev;
-+
-+ return 0;
-+}
-+
-+/*
-+ * Allocate a netdev_dev_bsd structure with 'tap' class.
-+ */
-+static int
-+netdev_bsd_create_tap(const struct netdev_class *class, const char *name,
-+ struct netdev_dev **netdev_devp)
-+{
-+ struct netdev_dev_bsd *netdev_dev = NULL;
-+ int error = 0;
-+ struct ifreq ifr;
-+
-+ error = cache_notifier_ref();
-+ if (error) {
-+ goto error;
-+ }
-+
-+ /* allocate the device structure and set the internal flag */
-+ netdev_dev = xzalloc(sizeof *netdev_dev);
-+
-+ memset(&ifr, 0, sizeof(ifr));
-+
-+ /* Create a tap device by opening /dev/tap. The TAPGIFNAME ioctl is used
-+ * to retrieve the name of the tap device. */
-+ netdev_dev->tap_fd = open("/dev/tap", O_RDWR);
-+ netdev_dev->change_seq = 1;
-+ if (netdev_dev->tap_fd < 0) {
-+ error = errno;
-+ VLOG_WARN("opening \"/dev/tap\" failed: %s", strerror(error));
-+ goto error_undef_notifier;
-+ }
-+
-+ /* Retrieve tap name (e.g. tap0) */
-+ if (ioctl(netdev_dev->tap_fd, TAPGIFNAME, &ifr) == -1) {
-+ /* XXX Need to destroy the device? */
-+ error = errno;
-+ goto error_undef_notifier;
-+ }
-+
-+ /* Change the name of the tap device */
-+ ifr.ifr_data = (void *)name;
-+ if (ioctl(af_inet_sock, SIOCSIFNAME, &ifr) == -1) {
-+ error = errno;
-+ destroy_tap(netdev_dev->tap_fd, ifr.ifr_name);
-+ goto error_undef_notifier;
-+ }
-+
-+ /* set non-blocking. */
-+ error = set_nonblocking(netdev_dev->tap_fd);
-+ if (error) {
-+ destroy_tap(netdev_dev->tap_fd, name);
-+ goto error_undef_notifier;
-+ }
-+
-+ /* Turn device UP */
-+ ifr.ifr_flags = (uint16_t)IFF_UP;
-+ ifr.ifr_flagshigh = 0;
-+ strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
-+ if (ioctl(af_inet_sock, SIOCSIFFLAGS, &ifr) == -1) {
-+ error = errno;
-+ destroy_tap(netdev_dev->tap_fd, name);
-+ goto error_undef_notifier;
-+ }
-+
-+ /* initialize the device structure and
-+ * link the structure to its netdev */
-+ netdev_dev_init(&netdev_dev->netdev_dev, name, class);
-+ *netdev_devp = &netdev_dev->netdev_dev;
-+
-+ return 0;
-+
-+error_undef_notifier:
-+ cache_notifier_unref();
-+error:
-+ free(netdev_dev);
-+ return error;
-+}
-+
-+static void
-+netdev_bsd_destroy(struct netdev_dev *netdev_dev_)
-+{
-+ struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_);
-+
-+ cache_notifier_unref();
-+
-+ if (netdev_dev->tap_fd >= 0 &&
-+ !strcmp(netdev_dev_get_type(netdev_dev_), "tap")) {
-+ destroy_tap(netdev_dev->tap_fd, netdev_dev_get_name(netdev_dev_));
-+ }
-+ free(netdev_dev);
-+}
-+
-+
-+static int
-+netdev_bsd_open_system(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
-+{
-+ struct netdev_dev_bsd *netdev_dev = netdev_dev_bsd_cast(netdev_dev_);
-+ struct netdev_bsd *netdev;
-+ int error;
-+ enum netdev_flags flags;
-+
-+ /* Allocate network device. */
-+ netdev = xcalloc(1, sizeof *netdev);
-+ netdev->netdev_fd = -1;
-+ netdev_init(&netdev->netdev, netdev_dev_);
-+
-+ /* Verify that the netdev really exists by attempting to read its flags */
-+ error = netdev_get_flags(&netdev->netdev, &flags);
-+ if (error == ENXIO) {
-+ goto error;
-+ }
-+
-+ /* The first user that opens a tap port(from dpif_create_and_open()) will
-+ * receive the file descriptor associated with the tap device. Instead, the
-+ * following users will open the tap device as a normal 'system' device. */
-+ if (!strcmp(netdev_dev_get_type(netdev_dev_), "tap") &&
-+ !netdev_dev->tap_opened) {
-+ netdev_dev->tap_opened = true;
-+ netdev->netdev_fd = netdev_dev->tap_fd;
-+ }
-+
-+ *netdevp = &netdev->netdev;
-+ return 0;
-+
-+error:
-+ netdev_uninit(&netdev->netdev, true);
-+ return error;
-+}
-+
-+
-+
-+/* Close a 'netdev'. */
-+static void
-+netdev_bsd_close(struct netdev *netdev_)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+
-+ if (netdev->netdev_fd >= 0 && strcmp(netdev_get_type(netdev_), "tap")) {
-+ pcap_close(netdev->pcap_handle);
-+ }
-+
-+ free(netdev);
-+}
-+
-+static int
-+netdev_bsd_listen(struct netdev *netdev_)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+ char errbuf[PCAP_ERRBUF_SIZE];
-+ int error;
-+ int fd;
-+ int one = 1;
-+
-+ if (netdev->netdev_fd >= 0) {
-+ return 0;
-+ }
-+
-+ /* open the pcap device. The device is opened in non-promiscuous mode
-+ * because the interface flags are manually set by the caller. */
-+ errbuf[0] = '\0';
-+ netdev->pcap_handle = pcap_open_live(netdev_get_name(netdev_), PCAP_SNAPLEN,
-+ 0, 1000, errbuf);
-+ if (netdev->pcap_handle == NULL) {
-+ VLOG_ERR("%s: pcap_open_live failed: %s",
-+ netdev_get_name(netdev_), errbuf);
-+ error = EIO;
-+ goto error;
-+ } else if (errbuf[0] != '\0') {
-+ VLOG_WARN("%s: pcap_open_live: %s",
-+ netdev_get_name(netdev_), errbuf);
-+ }
-+
-+ netdev_dev_bsd_changed(netdev_dev_bsd_cast(netdev_get_dev(netdev_)));
-+
-+ /* initialize netdev->netdev_fd */
-+ fd = pcap_get_selectable_fd(netdev->pcap_handle);
-+ if (fd == -1) {
-+ error = errno;
-+ goto error;
-+ }
-+
-+ /* Set non-blocking mode. Also the BIOCIMMEDIATE ioctl must be called
-+ * on the file descriptor returned by pcap_get_selectable_fd to achieve
-+ * a real non-blocking behaviour.*/
-+ error = pcap_setnonblock(netdev->pcap_handle, 1, errbuf);
-+ if (error == -1) {
-+ error = errno;
-+ goto error;
-+ }
-+
-+ /* This call assure that reads return immediately upon packet reception.
-+ * Otherwise, a read will block until either the kernel buffer becomes
-+ * full or a timeout occurs. */
-+ if(ioctl(fd, BIOCIMMEDIATE, &one) < 0 ) {
-+ VLOG_ERR("ioctl(BIOCIMMEDIATE) on %s device failed: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ error = errno;
-+ goto error;
-+ }
-+
-+ /* Capture only incoming packets */
-+ error = pcap_setdirection(netdev->pcap_handle, PCAP_D_IN);
-+ if (error == -1) {
-+ error = errno;
-+ goto error;
-+ }
-+
-+ netdev->netdev_fd = fd;
-+ return 0;
-+
-+error:
-+ if (fd >= 0) {
-+ close(netdev->netdev_fd);
-+ }
-+ return error;
-+}
-+
-+
-+/* The recv callback of the netdev class returns the number of bytes of the
-+ * received packet.
-+ *
-+ * This can be done by the pcap_next() function. Unfortunately pcap_next() does
-+ * not make difference between a missing packet on the capture interface and
-+ * an error during the file capture. We can use the pcap_dispatch() function
-+ * instead, which is able to distinguish between errors and null packet.
-+ *
-+ * To make pcap_dispatch() returns the number of bytes read from the interface
-+ * we need to define the following callback and argument.
-+ */
-+struct pcap_arg {
-+ void *data;
-+ int size;
-+ int retval;
-+};
-+
-+/*
-+ * This callback will be executed on every captured packet.
-+ *
-+ * If the packet captured by pcap_dispatch() does not fit the pcap buffer,
-+ * pcap returns a truncated packet and we follow this behavior.
-+ *
-+ * The argument args->retval is the packet size in bytes.
-+ */
-+static void
-+proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
-+{
-+ struct pcap_arg *args = (struct pcap_arg *)args_;
-+
-+ if (args->size < hdr->len) {
-+ VLOG_WARN_RL(&rl, "packet truncated");
-+ args->retval = args->size;
-+ } else {
-+ args->retval = hdr->len;
-+ }
-+
-+ /* copy the packet to our buffer */
-+ memcpy(args->data, packet, args->retval);
-+}
-+
-+/*
-+ * This function attempts to receive a packet from the specified network
-+ * device. It is assumed that the network device is a system device or a tap
-+ * device opened as a system one. In this case the read operation is performed
-+ * on the 'netdev' pcap descriptor.
-+ */
-+static int
-+netdev_bsd_recv_system(struct netdev_bsd *netdev, void *data, size_t size)
-+{
-+ struct pcap_arg arg;
-+ int ret;
-+
-+ if (netdev->netdev_fd < 0) {
-+ return -EAGAIN;
-+ }
-+
-+ /* prepare the pcap argument to store the packet */
-+ arg.size = size;
-+ arg.data = data;
-+
-+ for (;;) {
-+ ret = pcap_dispatch(netdev->pcap_handle, 1, proc_pkt, (u_char *)&arg);
-+
-+ if (ret > 0) {
-+ return arg.retval; /* arg.retval < 0 is handled in the caller */
-+ }
-+ if (ret == -1) {
-+ if (errno == EINTR) {
-+ continue;
-+ }
-+ }
-+
-+ return -EAGAIN;
-+ }
-+}
-+
-+/*
-+ * This function attempts to receive a packet from the specified network
-+ * device. It is assumed that the network device is a tap device and the
-+ * 'netdev_fd' member of the 'netdev' structure is initialized with the tap
-+ * file descriptor.
-+ */
-+static int
-+netdev_bsd_recv_tap(struct netdev_bsd *netdev, void *data, size_t size)
-+{
-+ if (netdev->netdev_fd < 0) {
-+ return -EAGAIN;
-+ }
-+
-+ for (;;) {
-+ ssize_t retval = read(netdev->netdev_fd, data, size);
-+ if (retval >= 0) {
-+ return retval;
-+ } else if (errno != EINTR) {
-+ if (errno != EAGAIN) {
-+ VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
-+ strerror(errno), netdev->netdev.netdev_dev->name);
-+ }
-+ return -errno;
-+ }
-+ }
-+}
-+
-+
-+/*
-+ * According with the nature of the device a different function must be called.
-+ * If the device is the bridge local port the 'netdev_bsd_recv_tap' function
-+ * must be called, otherwise the 'netdev_bsd_recv_system' function is called.
-+ *
-+ * type!="tap" ---> system device.
-+ * type=="tap" && netdev_fd == tap_fd ---> internal tap device
-+ * type=="tap" && netdev_fd != tap_fd ---> internal tap device
-+ * opened as a system
-+ * device.
-+ */
-+static int
-+netdev_bsd_recv(struct netdev *netdev_, void* data, size_t size)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+ struct netdev_dev_bsd * netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!strcmp(netdev_get_type(netdev_), "tap") &&
-+ netdev->netdev_fd == netdev_dev->tap_fd) {
-+ return netdev_bsd_recv_tap(netdev, data, size);
-+ } else {
-+ return netdev_bsd_recv_system(netdev, data, size);
-+ }
-+}
-+
-+
-+/*
-+ * Registers with the poll loop to wake up from the next call to poll_block()
-+ * when a packet is ready to be received with netdev_recv() on 'netdev'.
-+ */
-+static void
-+netdev_bsd_recv_wait(struct netdev *netdev_)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+
-+ if (netdev->netdev_fd >= 0) {
-+ poll_fd_wait(netdev->netdev_fd, POLLIN);
-+ }
-+}
-+
-+/* Discards all packets waiting to be received from 'netdev'. */
-+static int
-+netdev_bsd_drain(struct netdev *netdev_)
-+{
-+ struct ifreq ifr;
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+
-+ strcpy(ifr.ifr_name, netdev_get_name(netdev_));
-+ if (ioctl(netdev->netdev_fd, BIOCFLUSH, &ifr) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ return errno;
-+ }
-+ return 0;
-+}
-+
-+/*
-+ * Send a packet on the specified network device. The device could be either a
-+ * system or a tap device.
-+ */
-+static int
-+netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+ struct netdev_dev_bsd * netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (netdev->netdev_fd < 0) {
-+ return EPIPE;
-+ }
-+
-+ for (;;) {
-+ ssize_t retval;
-+ if (!strcmp(netdev_get_type(netdev_), "tap") &&
-+ netdev_dev->tap_fd == netdev->netdev_fd) {
-+ retval = write(netdev->netdev_fd, data, size);
-+ } else {
-+ retval = pcap_inject(netdev->pcap_handle, data, size);
-+ }
-+ if (retval < 0) {
-+ if (errno == EINTR) {
-+ continue;
-+ } else if (errno != EAGAIN) {
-+ VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ }
-+ return errno;
-+ } else if (retval != size) {
-+ VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
-+ "%zu) on %s", retval, size,
-+ netdev_get_name(netdev_));
-+ return EMSGSIZE;
-+ } else {
-+ return 0;
-+ }
-+ }
-+}
-+
-+/*
-+ * Registers with the poll loop to wake up from the next call to poll_block()
-+ * when the packet transmission queue has sufficient room to transmit a packet
-+ * with netdev_send().
-+ */
-+static void
-+netdev_bsd_send_wait(struct netdev *netdev_)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+
-+ if (netdev->netdev_fd < 0) { /* Nothing to do. */
-+ return;
-+ }
-+
-+ if (strcmp(netdev_get_type(netdev_), "tap")) {
-+ poll_fd_wait(netdev->netdev_fd, POLLOUT);
-+ } else {
-+ /* TAP device always accepts packets. */
-+ poll_immediate_wake();
-+ }
-+}
-+
-+/*
-+ * Attempts to set 'netdev''s MAC address to 'mac'. Returns 0 if successful,
-+ * otherwise a positive errno value.
-+ */
-+static int
-+netdev_bsd_set_etheraddr(struct netdev *netdev_,
-+ const uint8_t mac[ETH_ADDR_LEN])
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+ int error;
-+
-+ if (!(netdev_dev->cache_valid & VALID_ETHERADDR)
-+ || !eth_addr_equals(netdev_dev->etheraddr, mac)) {
-+ error = set_etheraddr(netdev_get_name(netdev_), AF_LINK, ETH_ADDR_LEN,
-+ mac);
-+ if (!error) {
-+ netdev_dev->cache_valid |= VALID_ETHERADDR;
-+ memcpy(netdev_dev->etheraddr, mac, ETH_ADDR_LEN);
-+ netdev_dev_bsd_changed(netdev_dev);
-+ }
-+ } else {
-+ error = 0;
-+ }
-+ return error;
-+}
-+
-+/*
-+ * Returns a pointer to 'netdev''s MAC address. The caller must not modify or
-+ * free the returned buffer.
-+ */
-+static int
-+netdev_bsd_get_etheraddr(const struct netdev *netdev_,
-+ uint8_t mac[ETH_ADDR_LEN])
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!(netdev_dev->cache_valid & VALID_ETHERADDR)) {
-+ int error = get_etheraddr(netdev_get_name(netdev_),
-+ netdev_dev->etheraddr);
-+ if (error) {
-+ return error;
-+ }
-+ netdev_dev->cache_valid |= VALID_ETHERADDR;
-+ }
-+ memcpy(mac, netdev_dev->etheraddr, ETH_ADDR_LEN);
-+
-+ return 0;
-+}
-+
-+/*
-+ * Returns the maximum size of transmitted (and received) packets on 'netdev',
-+ * in bytes, not including the hardware header; thus, this is typically 1500
-+ * bytes for Ethernet devices.
-+ */
-+static int
-+netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!(netdev_dev->cache_valid & VALID_MTU)) {
-+ struct ifreq ifr;
-+ int error;
-+
-+ error = netdev_bsd_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
-+ if (error) {
-+ return error;
-+ }
-+ netdev_dev->mtu = ifr.ifr_mtu;
-+ netdev_dev->cache_valid |= VALID_MTU;
-+ }
-+
-+ *mtup = netdev_dev->mtu;
-+ return 0;
-+}
-+
-+static int
-+netdev_bsd_get_ifindex(const struct netdev *netdev)
-+{
-+ int ifindex, error;
-+
-+ error = get_ifindex(netdev, &ifindex);
-+ return error ? -error : ifindex;
-+}
-+
-+static int
-+netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!(netdev_dev->cache_valid & VALID_CARRIER)) {
-+ struct ifmediareq ifmr;
-+
-+ memset(&ifmr, 0, sizeof(ifmr));
-+ strncpy(ifmr.ifm_name, netdev_get_name(netdev_), sizeof ifmr.ifm_name);
-+
-+ if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ return errno;
-+ }
-+
-+ netdev_dev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
-+ netdev_dev->cache_valid |= VALID_CARRIER;
-+
-+ /* If the interface doesn't report whether the media is active,
-+ * just assume it is active. */
-+ if ((ifmr.ifm_status & IFM_AVALID) == 0) {
-+ netdev_dev->carrier = true;
-+ }
-+ }
-+ *carrier = netdev_dev->carrier;
-+
-+ return 0;
-+}
-+
-+/* Retrieves current device stats for 'netdev'. */
-+static int
-+netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
-+{
-+ int if_count, i;
-+ int mib[6];
-+ size_t len;
-+ struct ifmibdata ifmd;
-+
-+
-+ mib[0] = CTL_NET;
-+ mib[1] = PF_LINK;
-+ mib[2] = NETLINK_GENERIC;
-+ mib[3] = IFMIB_SYSTEM;
-+ mib[4] = IFMIB_IFCOUNT;
-+
-+ len = sizeof(if_count);
-+
-+ if (sysctl(mib, 5, &if_count, &len, (void *)0, 0) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ return errno;
-+ }
-+
-+ mib[5] = IFDATA_GENERAL;
-+ mib[3] = IFMIB_IFDATA;
-+ len = sizeof(ifmd);
-+ for (i = 1; i <= if_count; i++) {
-+ mib[4] = i; //row
-+ if (sysctl(mib, 6, &ifmd, &len, (void *)0, 0) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: sysctl failed: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ return errno;
-+ } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
-+ stats->rx_packets = ifmd.ifmd_data.ifi_ipackets;
-+ stats->tx_packets = ifmd.ifmd_data.ifi_opackets;
-+ stats->rx_bytes = ifmd.ifmd_data.ifi_ibytes;
-+ stats->tx_bytes = ifmd.ifmd_data.ifi_obytes;
-+ stats->rx_errors = ifmd.ifmd_data.ifi_ierrors;
-+ stats->tx_errors = ifmd.ifmd_data.ifi_oerrors;
-+ stats->rx_dropped = ifmd.ifmd_data.ifi_iqdrops;
-+ stats->tx_dropped = 0;
-+ stats->multicast = ifmd.ifmd_data.ifi_imcasts;
-+ stats->collisions = ifmd.ifmd_data.ifi_collisions;
-+
-+ stats->rx_length_errors = 0;
-+ stats->rx_over_errors = 0;
-+ stats->rx_crc_errors = 0;
-+ stats->rx_frame_errors = 0;
-+ stats->rx_fifo_errors = 0;
-+ stats->rx_missed_errors = 0;
-+
-+ stats->tx_aborted_errors = 0;
-+ stats->tx_carrier_errors = 0;
-+ stats->tx_fifo_errors = 0;
-+ stats->tx_heartbeat_errors = 0;
-+ stats->tx_window_errors = 0;
-+ break;
-+ }
-+ }
-+
-+ return 0;
-+}
-+
-+static uint32_t
-+netdev_bsd_parse_media(int media)
-+{
-+ uint32_t supported = 0;
-+ bool half_duplex = media & IFM_HDX ? true : false;
-+
-+ switch (IFM_SUBTYPE(media)) {
-+ case IFM_10_2:
-+ case IFM_10_5:
-+ case IFM_10_STP:
-+ case IFM_10_T:
-+ supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
-+ supported |= NETDEV_F_COPPER;
-+ break;
-+
-+ case IFM_10_FL:
-+ supported |= half_duplex ? NETDEV_F_10MB_HD : NETDEV_F_10MB_FD;
-+ supported |= NETDEV_F_FIBER;
-+ break;
-+
-+ case IFM_100_T2:
-+ case IFM_100_T4:
-+ case IFM_100_TX:
-+ case IFM_100_VG:
-+ supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
-+ supported |= NETDEV_F_COPPER;
-+ break;
-+
-+ case IFM_100_FX:
-+ supported |= half_duplex ? NETDEV_F_100MB_HD : NETDEV_F_100MB_FD;
-+ supported |= NETDEV_F_FIBER;
-+ break;
-+
-+ case IFM_1000_CX:
-+ case IFM_1000_T:
-+ supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
-+ supported |= NETDEV_F_COPPER;
-+ break;
-+
-+ case IFM_1000_LX:
-+ case IFM_1000_SX:
-+ supported |= half_duplex ? NETDEV_F_1GB_HD : NETDEV_F_1GB_FD;
-+ supported |= NETDEV_F_FIBER;
-+ break;
-+
-+ case IFM_10G_CX4:
-+ supported |= NETDEV_F_10GB_FD;
-+ supported |= NETDEV_F_COPPER;
-+ break;
-+
-+ case IFM_10G_LR:
-+ case IFM_10G_SR:
-+ supported |= NETDEV_F_10GB_FD;
-+ supported |= NETDEV_F_FIBER;
-+ break;
-+
-+ default:
-+ return 0;
-+ }
-+
-+ if (IFM_SUBTYPE(media) == IFM_AUTO) {
-+ supported |= NETDEV_F_AUTONEG;
-+ }
-+ /*
-+ if (media & IFM_ETH_FMASK) {
-+ supported |= NETDEV_F_PAUSE;
-+ }
-+ */
-+
-+ return supported;
-+}
-+
-+/*
-+ * Stores the features supported by 'netdev' into each of '*current',
-+ * '*advertised', '*supported', and '*peer' that are non-null. Each value is a
-+ * bitmap of "enum ofp_port_features" bits, in host byte order. Returns 0 if
-+ * successful, otherwise a positive errno value. On failure, all of the
-+ * passed-in values are set to 0.
-+ */
-+static int
-+netdev_bsd_get_features(const struct netdev *netdev,
-+ enum netdev_features *current, uint32_t *advertised,
-+ enum netdev_features *supported, uint32_t *peer)
-+{
-+ struct ifmediareq ifmr;
-+ int *media_list;
-+ int i;
-+ int error;
-+
-+
-+ /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
-+
-+ memset(&ifmr, 0, sizeof(ifmr));
-+ strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
-+
-+ /* We make two SIOCGIFMEDIA ioctl calls. The first to determine the
-+ * number of supported modes, and a second with a buffer to retrieve
-+ * them. */
-+ if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
-+ netdev_get_name(netdev), strerror(errno));
-+ return errno;
-+ }
-+
-+ media_list = xcalloc(ifmr.ifm_count, sizeof(int));
-+ ifmr.ifm_ulist = media_list;
-+
-+ if (!IFM_TYPE(ifmr.ifm_current) & IFM_ETHER) {
-+ VLOG_DBG_RL(&rl, "%s: doesn't appear to be ethernet",
-+ netdev_get_name(netdev));
-+ error = EINVAL;
-+ goto cleanup;
-+ }
-+
-+ if (ioctl(af_inet_sock, SIOCGIFMEDIA, &ifmr) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
-+ netdev_get_name(netdev), strerror(errno));
-+ error = errno;
-+ goto cleanup;
-+ }
-+
-+ /* Current settings. */
-+ *current = netdev_bsd_parse_media(ifmr.ifm_active);
-+
-+ /* Advertised features. */
-+ *advertised = netdev_bsd_parse_media(ifmr.ifm_current);
-+
-+ /* Supported features. */
-+ *supported = 0;
-+ for (i = 0; i < ifmr.ifm_count; i++) {
-+ *supported |= netdev_bsd_parse_media(ifmr.ifm_ulist[i]);
-+ }
-+
-+ /* Peer advertisements. */
-+ *peer = 0; /* XXX */
-+
-+ error = 0;
-+cleanup:
-+ free(media_list);
-+ return error;
-+}
-+
-+/*
-+ * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
-+ * 'in4' is non-null) and returns true. Otherwise, returns false.
-+ */
-+static int
-+netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
-+ struct in_addr *netmask)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!(netdev_dev->cache_valid & VALID_IN4)) {
-+ const struct sockaddr_in *sin;
-+ struct ifreq ifr;
-+ int error;
-+
-+ ifr.ifr_addr.sa_family = AF_INET;
-+ error = netdev_bsd_do_ioctl(netdev_, &ifr,
-+ SIOCGIFADDR, "SIOCGIFADDR");
-+ if (error) {
-+ return error;
-+ }
-+
-+ sin = (struct sockaddr_in *) &ifr.ifr_addr;
-+ netdev_dev->in4 = sin->sin_addr;
-+ netdev_dev->cache_valid |= VALID_IN4;
-+ error = netdev_bsd_do_ioctl(netdev_, &ifr,
-+ SIOCGIFNETMASK, "SIOCGIFNETMASK");
-+ if (error) {
-+ return error;
-+ }
-+ *netmask = ((struct sockaddr_in*)&ifr.ifr_addr)->sin_addr;
-+ }
-+ *in4 = netdev_dev->in4;
-+
-+ return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
-+}
-+
-+/*
-+ * Assigns 'addr' as 'netdev''s IPv4 address and 'mask' as its netmask. If
-+ * 'addr' is INADDR_ANY, 'netdev''s IPv4 address is cleared. Returns a
-+ * positive errno value.
-+ */
-+static int
-+netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
-+ struct in_addr mask)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+ int error;
-+
-+ error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
-+ if (!error) {
-+ netdev_dev->cache_valid |= VALID_IN4;
-+ netdev_dev->in4 = addr;
-+ if (addr.s_addr != INADDR_ANY) {
-+ error = do_set_addr(netdev_, SIOCSIFNETMASK,
-+ "SIOCSIFNETMASK", mask);
-+ }
-+ netdev_dev_bsd_changed(netdev_dev);
-+ }
-+ return error;
-+}
-+
-+static int
-+netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+ if (!(netdev_dev->cache_valid & VALID_IN6)) {
-+ struct ifaddrs *ifa, *head;
-+ struct sockaddr_in6 *sin6;
-+ const char *netdev_name = netdev_get_name(netdev_);
-+
-+ if (getifaddrs(&head) != 0) {
-+ VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
-+ strerror(errno));
-+ return errno;
-+ }
-+
-+ for (ifa = head; ifa; ifa = ifa->ifa_next) {
-+ if (ifa->ifa_addr->sa_family == AF_INET6 &&
-+ !strcmp(ifa->ifa_name, netdev_name)) {
-+ sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
-+ if (sin6) {
-+ memcpy(&netdev_dev->in6, &sin6->sin6_addr, sin6->sin6_len);
-+ netdev_dev->cache_valid |= VALID_IN6;
-+ *in6 = netdev_dev->in6;
-+ freeifaddrs(head);
-+ return 0;
-+ }
-+ }
-+ }
-+ return EADDRNOTAVAIL;
-+ }
-+ *in6 = netdev_dev->in6;
-+ return 0;
-+}
-+
-+static void
-+make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
-+{
-+ struct sockaddr_in sin;
-+ memset(&sin, 0, sizeof sin);
-+ sin.sin_family = AF_INET;
-+ sin.sin_addr = addr;
-+ sin.sin_port = 0;
-+
-+ memset(sa, 0, sizeof *sa);
-+ memcpy(sa, &sin, sizeof sin);
-+}
-+
-+static int
-+do_set_addr(struct netdev *netdev,
-+ int ioctl_nr, const char *ioctl_name, struct in_addr addr)
-+{
-+ struct ifreq ifr;
-+ make_in4_sockaddr(&ifr.ifr_addr, addr);
-+ return netdev_bsd_do_ioctl(netdev, &ifr, ioctl_nr, ioctl_name);
-+}
-+
-+static int
-+nd_to_iff_flags(enum netdev_flags nd)
-+{
-+ int iff = 0;
-+ if (nd & NETDEV_UP) {
-+ iff |= IFF_UP;
-+ }
-+ if (nd & NETDEV_PROMISC) {
-+ iff |= IFF_PROMISC;
-+ iff |= IFF_PPROMISC;
-+ }
-+ return iff;
-+}
-+
-+static int
-+iff_to_nd_flags(int iff)
-+{
-+ enum netdev_flags nd = 0;
-+ if (iff & IFF_UP) {
-+ nd |= NETDEV_UP;
-+ }
-+ if (iff & IFF_PROMISC) {
-+ nd |= NETDEV_PROMISC;
-+ }
-+ return nd;
-+}
-+
-+static int
-+netdev_bsd_update_flags(struct netdev *netdev, enum netdev_flags off,
-+ enum netdev_flags on, enum netdev_flags *old_flagsp)
-+{
-+ int old_flags, new_flags;
-+ int error;
-+
-+ error = get_flags(netdev, &old_flags);
-+ if (!error) {
-+ *old_flagsp = iff_to_nd_flags(old_flags);
-+ new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
-+ if (new_flags != old_flags) {
-+ error = set_flags(netdev, new_flags);
-+ netdev_dev_bsd_changed(netdev_dev_bsd_cast(netdev_get_dev(netdev)));
-+ }
-+ }
-+ return error;
-+}
-+
-+static unsigned int
-+netdev_bsd_change_seq(const struct netdev *netdev)
-+{
-+ return netdev_dev_bsd_cast(netdev_get_dev(netdev))->change_seq;
-+}
-+
-+
-+const struct netdev_class netdev_bsd_class = {
-+ "system",
-+
-+ netdev_bsd_init,
-+ netdev_bsd_run,
-+ netdev_bsd_wait,
-+ netdev_bsd_create_system,
-+ netdev_bsd_destroy,
-+ NULL, /* get_config */
-+ NULL, /* set_config */
-+ netdev_bsd_open_system,
-+ netdev_bsd_close,
-+
-+ netdev_bsd_listen,
-+
-+ netdev_bsd_recv,
-+ netdev_bsd_recv_wait,
-+ netdev_bsd_drain,
-+
-+ netdev_bsd_send,
-+ netdev_bsd_send_wait,
-+
-+ netdev_bsd_set_etheraddr,
-+ netdev_bsd_get_etheraddr,
-+ netdev_bsd_get_mtu,
-+ NULL, /* set_mtu */
-+ netdev_bsd_get_ifindex,
-+ netdev_bsd_get_carrier,
-+ NULL, /* get_carrier_resets */
-+ NULL, /* set_miimon_interval */
-+ netdev_bsd_get_stats,
-+ NULL, /* set_stats */
-+
-+ netdev_bsd_get_features,
-+ NULL, /* set_advertisement */
-+ NULL, /* set_policing */
-+ NULL, /* get_qos_type */
-+ NULL, /* get_qos_capabilities */
-+ NULL, /* get_qos */
-+ NULL, /* set_qos */
-+ NULL, /* get_queue */
-+ NULL, /* set_queue */
-+ NULL, /* delete_queue */
-+ NULL, /* get_queue_stats */
-+ NULL, /* dump_queue */
-+ NULL, /* dump_queue_stats */
-+
-+ netdev_bsd_get_in4,
-+ netdev_bsd_set_in4,
-+ netdev_bsd_get_in6,
-+ NULL, /* add_router */
-+ NULL, /* get_next_hop */
-+ NULL, /* get_drv_info */
-+ NULL, /* arp_lookup */
-+
-+ netdev_bsd_update_flags,
-+
-+ netdev_bsd_change_seq
-+};
-+
-+const struct netdev_class netdev_tap_class = {
-+ "tap",
-+
-+ netdev_bsd_init,
-+ netdev_bsd_run,
-+ netdev_bsd_wait,
-+ netdev_bsd_create_tap,
-+ netdev_bsd_destroy,
-+ NULL, /* get_config */
-+ NULL, /* set_config */
-+ netdev_bsd_open_system,
-+ netdev_bsd_close,
-+
-+ netdev_bsd_listen,
-+
-+ netdev_bsd_recv,
-+ netdev_bsd_recv_wait,
-+ netdev_bsd_drain,
-+
-+ netdev_bsd_send,
-+ netdev_bsd_send_wait,
-+
-+ netdev_bsd_set_etheraddr,
-+ netdev_bsd_get_etheraddr,
-+ netdev_bsd_get_mtu,
-+ NULL, /* set_mtu */
-+ netdev_bsd_get_ifindex,
-+ netdev_bsd_get_carrier,
-+ NULL, /* get_carrier_resets */
-+ NULL, /* set_miimon_interval */
-+ netdev_bsd_get_stats,
-+ NULL, /* set_stats */
-+
-+ netdev_bsd_get_features,
-+ NULL, /* set_advertisement */
-+ NULL, /* set_policing */
-+ NULL, /* get_qos_type */
-+ NULL, /* get_qos_capabilities */
-+ NULL, /* get_qos */
-+ NULL, /* set_qos */
-+ NULL, /* get_queue */
-+ NULL, /* set_queue */
-+ NULL, /* delete_queue */
-+ NULL, /* get_queue_stats */
-+ NULL, /* dump_queue */
-+ NULL, /* dump_queue_stats */
-+
-+ netdev_bsd_get_in4,
-+ netdev_bsd_set_in4,
-+ netdev_bsd_get_in6,
-+ NULL, /* add_router */
-+ NULL, /* get_next_hop */
-+ NULL, /* get_drv_info */
-+ NULL, /* arp_lookup */
-+
-+ netdev_bsd_update_flags,
-+
-+ netdev_bsd_change_seq
-+};
-+
-+
-+static void
-+destroy_tap(int fd, const char *name)
-+{
-+ struct ifreq ifr;
-+
-+ close(fd);
-+ strcpy(ifr.ifr_name, name);
-+ /* XXX What to do if this call fails? */
-+ ioctl(af_inet_sock, SIOCIFDESTROY, &ifr);
-+}
-+
-+static int
-+get_flags(const struct netdev *netdev, int *flags)
-+{
-+ struct ifreq ifr;
-+ int error;
-+
-+ error = netdev_bsd_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
-+
-+ *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16);
-+ *flags |= 0x0000FFFF & ifr.ifr_flags;
-+
-+ return error;
-+}
-+
-+static int
-+set_flags(struct netdev *netdev, int flags)
-+{
-+ struct ifreq ifr;
-+
-+ ifr.ifr_flags = 0x0000FFFF & flags;
-+ ifr.ifr_flagshigh = (0xFFFF0000 & flags) >> 16;
-+
-+ return netdev_bsd_do_ioctl(netdev, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
-+}
-+
-+static int
-+get_ifindex(const struct netdev *netdev_, int *ifindexp)
-+{
-+ struct netdev_dev_bsd *netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+ *ifindexp = 0;
-+ if (!(netdev_dev->cache_valid & VALID_IFINDEX)) {
-+ int ifindex = if_nametoindex(netdev_get_name(netdev_));
-+ if (ifindex <= 0) {
-+ return errno;
-+ }
-+ netdev_dev->cache_valid |= VALID_IFINDEX;
-+ netdev_dev->ifindex = ifindex;
-+ }
-+ *ifindexp = netdev_dev->ifindex;
-+ return 0;
-+}
-+
-+static int
-+get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
-+{
-+ struct ifaddrs *head;
-+ struct ifaddrs *ifa;
-+ struct sockaddr_dl *sdl;
-+
-+ if (getifaddrs(&head) != 0) {
-+ VLOG_ERR("getifaddrs on %s device failed: %s", netdev_name,
-+ strerror(errno));
-+ return errno;
-+ }
-+
-+ for (ifa = head; ifa; ifa = ifa->ifa_next) {
-+ if (ifa->ifa_addr->sa_family == AF_LINK) {
-+ if (!strcmp(ifa->ifa_name, netdev_name)) {
-+ sdl = (struct sockaddr_dl *)ifa->ifa_addr;
-+ if (sdl) {
-+ memcpy(ea, LLADDR(sdl), sdl->sdl_alen);
-+ freeifaddrs(head);
-+ return 0;
-+ }
-+ }
-+ }
-+ }
-+
-+ VLOG_ERR("could not find ethernet address for %s device", netdev_name);
-+ freeifaddrs(head);
-+ return ENODEV;
-+}
-+
-+static int
-+set_etheraddr(const char *netdev_name, int hwaddr_family,
-+ int hwaddr_len, const uint8_t mac[ETH_ADDR_LEN])
-+{
-+ struct ifreq ifr;
-+
-+ memset(&ifr, 0, sizeof ifr);
-+ strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
-+ ifr.ifr_addr.sa_family = hwaddr_family;
-+ ifr.ifr_addr.sa_len = hwaddr_len;
-+ memcpy(ifr.ifr_addr.sa_data, mac, hwaddr_len);
-+ if (ioctl(af_inet_sock, SIOCSIFLLADDR, &ifr) < 0) {
-+ VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
-+ netdev_name, strerror(errno));
-+ return errno;
-+ }
-+ return 0;
-+}
-+
-+static int
-+netdev_bsd_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
-+ unsigned long cmd, const char *cmd_name)
-+{
-+ strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
-+ if (ioctl(af_inet_sock, cmd, ifr) == -1) {
-+ VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s",
-+ netdev_get_name(netdev), cmd_name, strerror(errno));
-+ return errno;
-+ }
-+ return 0;
-+}
-diff --git lib/netdev-provider.h lib/netdev-provider.h
-index 080b76a..2a91f05 100644
---- lib/netdev-provider.h
-+++ lib/netdev-provider.h
-@@ -596,6 +596,9 @@ const struct netdev_class *netdev_lookup_provider(const char *type);
- extern const struct netdev_class netdev_linux_class;
- extern const struct netdev_class netdev_internal_class;
- extern const struct netdev_class netdev_tap_class;
-+#ifdef __FreeBSD__
-+extern const struct netdev_class netdev_bsd_class;
-+#endif
-
- #ifdef __cplusplus
- }
-diff --git lib/netdev.c lib/netdev.c
-index e4e7ab1..be7cdd2 100644
---- lib/netdev.c
-+++ lib/netdev.c
-@@ -80,6 +80,10 @@ netdev_initialize(void)
- netdev_register_provider(&netdev_tap_class);
- netdev_vport_register();
- #endif
-+#ifdef __FreeBSD__
-+ netdev_register_provider(&netdev_tap_class);
-+ netdev_register_provider(&netdev_bsd_class);
-+#endif
- }
- }
-
-diff --git lib/route-table-bsd.c lib/route-table-bsd.c
-new file mode 100644
-index 0000000..c145091
---- /dev/null
-+++ lib/route-table-bsd.c
-@@ -0,0 +1,131 @@
-+/*
-+ * Copyright (c) 2012 Ed Maste. All rights reserved.
-+ *
-+ * Licensed under the Apache License, Version 2.0 (the "License");
-+ * you may not use this file except in compliance with the License.
-+ * You may obtain a copy of the License at:
-+ *
-+ * http://www.apache.org/licenses/LICENSE-2.0
-+ *
-+ * Unless required by applicable law or agreed to in writing, software
-+ * distributed under the License is distributed on an "AS IS" BASIS,
-+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-+ * See the License for the specific language governing permissions and
-+ * limitations under the License.
-+ */
-+
-+#include <config.h>
-+
-+#include "route-table.h"
-+
-+#include <sys/socket.h>
-+#include <sys/types.h>
-+
-+#include <net/if.h>
-+#include <net/route.h>
-+#include <net/if_dl.h>
-+#include <netinet/in.h>
-+
-+#include <string.h>
-+#include <unistd.h>
-+
-+#include "vlog.h"
-+
-+VLOG_DEFINE_THIS_MODULE(route_table);
-+
-+static int pid;
-+static unsigned int register_count = 0;
-+
-+bool
-+route_table_get_name(ovs_be32 ip, char name[IFNAMSIZ])
-+{
-+ struct {
-+ struct rt_msghdr rtm;
-+ char space[512];
-+ } rtmsg;
-+
-+ struct rt_msghdr *rtm = &rtmsg.rtm;
-+ struct sockaddr_dl *ifp = NULL;
-+ struct sockaddr_in *sin;
-+ struct sockaddr *sa;
-+ static int seq;
-+ int i, len, namelen, rtsock;
-+
-+ rtsock = socket(PF_ROUTE, SOCK_RAW, 0);
-+ if (rtsock < 0)
-+ return false;
-+
-+ memset(&rtmsg, 0, sizeof(rtmsg));
-+
-+ rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in);
-+ rtm->rtm_version = RTM_VERSION;
-+ rtm->rtm_type = RTM_GET;
-+ rtm->rtm_addrs = RTA_DST | RTA_IFP;
-+ rtm->rtm_seq = ++seq;
-+
-+ sin = (struct sockaddr_in *)(rtm + 1);
-+ sin->sin_len = len = sizeof(struct sockaddr_in);
-+ sin->sin_family = AF_INET;
-+ sin->sin_addr.s_addr = ip;
-+
-+ if ((write(rtsock, (char *)&rtmsg, rtm->rtm_msglen)) < 0) {
-+ close(rtsock);
-+ return false;
-+ }
-+
-+ do {
-+ len = read(rtsock, (char *)&rtmsg, sizeof(rtmsg));
-+ } while (len > 0 && (rtmsg.rtm.rtm_seq != seq ||
-+ rtmsg.rtm.rtm_pid != pid));
-+
-+ close(rtsock);
-+
-+ if (len < 0) {
-+ return false;
-+ }
-+
-+ sa = (struct sockaddr *)(rtm + 1);
-+ for (i = 1; i; i <<= 1) {
-+ if (rtm->rtm_addrs & i) {
-+ if (i == RTA_IFP && sa->sa_family == AF_LINK &&
-+ ((struct sockaddr_dl *)sa)->sdl_nlen) {
-+ ifp = (struct sockaddr_dl *)sa;
-+ namelen = ifp->sdl_nlen;
-+ if (namelen > IFNAMSIZ - 1)
-+ namelen = IFNAMSIZ - 1;
-+ memcpy(name, ifp->sdl_data, namelen);
-+ name[namelen] = '\0';
-+ return true;
-+ }
-+ sa = (struct sockaddr *)((char *)sa + SA_SIZE(sa));
-+ }
-+ }
-+ return false;
-+}
-+
-+void
-+route_table_register()
-+{
-+ if (!register_count)
-+ {
-+ pid = getpid();
-+ }
-+
-+ register_count++;
-+}
-+
-+void
-+route_table_unregister()
-+{
-+ register_count--;
-+}
-+
-+void
-+route_table_run(void)
-+{
-+}
-+
-+void
-+route_table_wait(void)
-+{
-+}
-diff --git lib/route-table.h lib/route-table.h
-index 1479249..804cb3f 100644
---- lib/route-table.h
-+++ lib/route-table.h
-@@ -17,6 +17,7 @@
- #ifndef ROUTE_TABLE_H
- #define ROUTE_TABLE_H 1
-
-+#include <sys/socket.h>
- #include <net/if.h>
- #include <stdbool.h>
- #include <stdint.h>
-diff --git lib/rtbsd.c lib/rtbsd.c
-new file mode 100644
-index 0000000..c5fe03a
---- /dev/null
-+++ lib/rtbsd.c
-@@ -0,0 +1,167 @@
-+/*
-+ * Copyright (c) 2011 Gaetano Catalli.
-+ *
-+ * Licensed under the Apache License, Version 2.0 (the "License");
-+ * you may not use this file except in compliance with the License.
-+ * You may obtain a copy of the License at:
-+ *
-+ * http://www.apache.org/licenses/LICENSE-2.0
-+ *
-+ * Unless required by applicable law or agreed to in writing, software
-+ * distributed under the License is distributed on an "AS IS" BASIS,
-+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-+ * See the License for the specific language governing permissions and
-+ * limitations under the License.
-+ */
-+
-+#include <config.h>
-+
-+#include <unistd.h>
-+#include <errno.h>
-+#include <sys/socket.h>
-+#include <net/if.h>
-+#include <net/route.h>
-+#include <poll.h>
-+
-+#include "coverage.h"
-+#include "socket-util.h"
-+#include "poll-loop.h"
-+#include "vlog.h"
-+#include "rtbsd.h"
-+
-+VLOG_DEFINE_THIS_MODULE(rtbsd);
-+COVERAGE_DEFINE(rtbsd_changed);
-+
-+/* PF_ROUTE socket. */
-+static int notify_sock = -1;
-+
-+/* All registered notifiers. */
-+static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
-+
-+static void rtbsd_report_change(const struct if_msghdr *);
-+static void rtbsd_report_notify_error(void);
-+
-+/* Registers 'cb' to be called with auxiliary data 'aux' with network device
-+ * change notifications. The notifier is stored in 'notifier', which the
-+ * caller must not modify or free.
-+ *
-+ * Returns 0 if successful, otherwise a positive errno value. */
-+int
-+rtbsd_notifier_register(struct rtbsd_notifier *notifier,
-+ rtbsd_notify_func *cb, void *aux)
-+{
-+ if (notify_sock < 0) {
-+ int error;
-+ notify_sock = socket(PF_ROUTE, SOCK_RAW, 0);
-+ if (notify_sock < 0) {
-+ VLOG_WARN("could not create PF_ROUTE socket: %s",
-+ strerror(errno));
-+ return errno;
-+ }
-+ error = set_nonblocking(notify_sock);
-+ if (error) {
-+ VLOG_WARN("error set_nonblocking PF_ROUTE socket: %s",
-+ strerror(error));
-+ return error;
-+ }
-+ } else {
-+ /* Catch up on notification work so that the new notifier won't
-+ * receive any stale notifications. XXX*/
-+ rtbsd_notifier_run();
-+ }
-+
-+ list_push_back(&all_notifiers, &notifier->node);
-+ notifier->cb = cb;
-+ notifier->aux = aux;
-+ return 0;
-+}
-+
-+/* Cancels notification on 'notifier', which must have previously been
-+ * registered with rtbsd_notifier_register(). */
-+void
-+rtbsd_notifier_unregister(struct rtbsd_notifier *notifier)
-+{
-+ list_remove(&notifier->node);
-+ if (list_is_empty(&all_notifiers)) {
-+ close(notify_sock);
-+ notify_sock = -1;
-+ }
-+}
-+
-+/* Calls all of the registered notifiers, passing along any as-yet-unreported
-+ * netdev change events. */
-+void
-+rtbsd_notifier_run(void)
-+{
-+ static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-+ struct if_msghdr msg;
-+ if (notify_sock < 0) {
-+ return;
-+ }
-+
-+ for (;;) {
-+ int retval;
-+
-+ msg.ifm_type = RTM_IFINFO;
-+ msg.ifm_version = RTM_VERSION; //XXX check if necessary
-+
-+ /* read from PF_ROUTE socket */
-+ retval = read(notify_sock, (char *)&msg, sizeof(msg));
-+ if (retval >= 0) {
-+ /* received packet from PF_ROUTE socket
-+ * XXX check for bad packets */
-+ if (msg.ifm_type == RTM_IFINFO) {
-+ rtbsd_report_change(&msg);
-+ }
-+ } else if (errno == EAGAIN) {
-+ return;
-+ } else {
-+ if (errno == ENOBUFS) {
-+ VLOG_WARN_RL(&rl, "PF_ROUTE receive buffer overflowed");
-+ } else {
-+ VLOG_WARN_RL(&rl, "error reading PF_ROUTE socket: %s",
-+ strerror(errno));
-+ }
-+ rtbsd_report_notify_error();
-+ }
-+ }
-+}
-+
-+/* Causes poll_block() to wake up when network device change notifications are
-+ * ready. */
-+void
-+rtbsd_notifier_wait(void)
-+{
-+ if (notify_sock >= 0) {
-+ poll_fd_wait(notify_sock, POLLIN);
-+ }
-+}
-+
-+static void
-+rtbsd_report_change(const struct if_msghdr *msg)
-+{
-+ struct rtbsd_notifier *notifier;
-+ struct rtbsd_change change;
-+
-+ COVERAGE_INC(rtbsd_changed);
-+
-+ change.msg_type = msg->ifm_type; //XXX
-+ change.if_index = msg->ifm_index;
-+ if_indextoname(msg->ifm_index, change.if_name);
-+ change.master_ifindex = 0; //XXX
-+
-+ LIST_FOR_EACH (notifier, node, &all_notifiers) {
-+ notifier->cb(&change, notifier->aux);
-+ }
-+}
-+
-+/* If an error occurs the notifiers' callbacks are called with NULL changes */
-+static void
-+rtbsd_report_notify_error(void)
-+{
-+ struct rtbsd_notifier *notifier;
-+
-+ LIST_FOR_EACH (notifier, node, &all_notifiers) {
-+ notifier->cb(NULL, notifier->aux);
-+ }
-+}
-diff --git lib/rtbsd.h lib/rtbsd.h
-new file mode 100644
-index 0000000..60bfae9
---- /dev/null
-+++ lib/rtbsd.h
-@@ -0,0 +1,58 @@
-+/*
-+ * Copyright (c) 2011 Gaetano Catalli.
-+ *
-+ * Licensed under the Apache License, Version 2.0 (the "License");
-+ * you may not use this file except in compliance with the License.
-+ * You may obtain a copy of the License at:
-+ *
-+ * http://www.apache.org/licenses/LICENSE-2.0
-+ *
-+ * Unless required by applicable law or agreed to in writing, software
-+ * distributed under the License is distributed on an "AS IS" BASIS,
-+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-+ * See the License for the specific language governing permissions and
-+ * limitations under the License.
-+ */
-+
-+#ifndef RTBSD_H
-+#define RTBSD_H 1
-+
-+#include "list.h"
-+
-+/*
-+ * A digested version of a message received from a PF_ROUTE socket which
-+ * indicates that a network device has been created or destroyed or changed.
-+ */
-+struct rtbsd_change {
-+ /* Copied from struct if_msghdr. */
-+ int msg_type; /* e.g. XXX. */
-+
-+ /* Copied from struct if_msghdr. */
-+ int if_index; /* Index of network device. */
-+
-+ char if_name[IF_NAMESIZE]; /* Name of network device. */
-+ int master_ifindex; /* Ifindex of datapath master (0 if none). */
-+};
-+
-+/*
-+ * Function called to report that a netdev has changed. 'change' describes the
-+ * specific change. It may be null if the buffer of change information
-+ * overflowed, in which case the function must assume that every device may
-+ * have changed. 'aux' is as specified in the call to
-+ * rtbsd_notifier_register().
-+ */
-+typedef void rtbsd_notify_func(const struct rtbsd_change *, void *aux);
-+
-+struct rtbsd_notifier {
-+ struct list node;
-+ rtbsd_notify_func *cb;
-+ void *aux;
-+};
-+
-+int rtbsd_notifier_register(struct rtbsd_notifier *,
-+ rtbsd_notify_func *, void *aux);
-+void rtbsd_notifier_unregister(struct rtbsd_notifier *);
-+void rtbsd_notifier_run(void);
-+void rtbsd_notifier_wait(void);
-+
-+#endif /* rtbsd.h */
-diff --git lib/socket-util.c lib/socket-util.c
-index 82a33c2..b14eef5 100644
---- lib/socket-util.c
-+++ lib/socket-util.c
-@@ -85,12 +85,14 @@ set_nonblocking(int fd)
- static int
- set_dscp(int fd, uint8_t dscp)
- {
-+ int val;
-+
- if (dscp > 63) {
- return EINVAL;
- }
-
-- dscp = dscp << 2;
-- if (setsockopt(fd, IPPROTO_IP, IP_TOS, &dscp, sizeof dscp)) {
-+ val = dscp << 2;
-+ if (setsockopt(fd, IPPROTO_IP, IP_TOS, &val, sizeof val)) {
- return errno;
- }
-
-diff --git lib/vconn.c lib/vconn.c
-index 5cd708d..dd198bc 100644
---- lib/vconn.c
-+++ lib/vconn.c
-@@ -286,13 +286,7 @@ vconn_open_block(const char *name, int min_version, struct vconn **vconnp)
-
- error = vconn_open(name, min_version, &vconn, DSCP_DEFAULT);
- if (!error) {
-- while ((error = vconn_connect(vconn)) == EAGAIN) {
-- vconn_run(vconn);
-- vconn_run_wait(vconn);
-- vconn_connect_wait(vconn);
-- poll_block();
-- }
-- assert(error != EINPROGRESS);
-+ error = vconn_connect_block(vconn);
- }
-
- if (error) {
-@@ -621,6 +615,24 @@ do_send(struct vconn *vconn, struct ofpbuf *msg)
- return retval;
- }
-
-+/* Same as vconn_connect(), except that it waits until the connection on
-+ * 'vconn' completes or fails. Thus, it will never return EAGAIN. */
-+int
-+vconn_connect_block(struct vconn *vconn)
-+{
-+ int error;
-+
-+ while ((error = vconn_connect(vconn)) == EAGAIN) {
-+ vconn_run(vconn);
-+ vconn_run_wait(vconn);
-+ vconn_connect_wait(vconn);
-+ poll_block();
-+ }
-+ assert(error != EINPROGRESS);
-+
-+ return error;
-+}
-+
- /* Same as vconn_send, except that it waits until 'msg' can be transmitted. */
- int
- vconn_send_block(struct vconn *vconn, struct ofpbuf *msg)
-diff --git lib/vconn.h lib/vconn.h
-index ee4a568..913364e 100644
---- lib/vconn.h
-+++ lib/vconn.h
-@@ -1,5 +1,5 @@
- /*
-- * Copyright (c) 2008, 2009, 2010, 2011 Nicira, Inc.
-+ * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
-@@ -55,6 +55,7 @@ void vconn_run(struct vconn *);
- void vconn_run_wait(struct vconn *);
-
- int vconn_open_block(const char *name, int min_version, struct vconn **);
-+int vconn_connect_block(struct vconn *);
- int vconn_send_block(struct vconn *, struct ofpbuf *);
- int vconn_recv_block(struct vconn *, struct ofpbuf **);
-
-diff --git lib/vlandev.c lib/vlandev.c
-index 77e8e93..ffb8e73 100644
---- lib/vlandev.c
-+++ lib/vlandev.c
-@@ -23,13 +23,13 @@
- #include <sys/stat.h>
-
- #include "hash.h"
--#include "rtnetlink-link.h"
- #include "shash.h"
- #include "vlog.h"
-
- VLOG_DEFINE_THIS_MODULE(vlandev);
-
- #ifdef __linux__
-+#include "rtnetlink-link.h"
- #include <linux/if_vlan.h>
- #include <linux/sockios.h>
- #include "netdev-linux.h"
-diff --git ofproto/ofproto-dpif-sflow.c ofproto/ofproto-dpif-sflow.c
-index 611f89a..23f5498 100644
---- ofproto/ofproto-dpif-sflow.c
-+++ ofproto/ofproto-dpif-sflow.c
-@@ -18,6 +18,7 @@
- #include <config.h>
- #include "ofproto-dpif-sflow.h"
- #include <inttypes.h>
-+#include <sys/socket.h>
- #include <net/if.h>
- #include <stdlib.h>
- #include "collectors.h"
-diff --git tests/ofproto-dpif.at tests/ofproto-dpif.at
-index 9c3e0dc..b3d1947 100644
---- tests/ofproto-dpif.at
-+++ tests/ofproto-dpif.at
-@@ -962,7 +962,7 @@ ovs-appctl -t test-netflow exit
-
- AT_CHECK([[sed -e 's/, uptime [0-9]*//
- s/, now [0-9.]*//
--s/time \([0-9]*\)\.\.\.\1\b/time <moment>/
-+s/time \([0-9]*\)\.\.\.\1$/time <moment>/
- s/time [0-9]*\.\.\.[0-9]*/time <range>/
- ' netflow.log]], [0],
- [header: v5, seq 0, engine 2,1
-diff --git tests/ovs-vsctl.at tests/ovs-vsctl.at
-index 584ee79..9e6b4a9 100644
---- tests/ovs-vsctl.at
-+++ tests/ovs-vsctl.at
-@@ -769,7 +769,7 @@ AT_CHECK(
-
- ])
- m4_define([VSCTL_CHECK_FIND],
-- [AT_CHECK([ovs-vsctl --bare --timeout=5 --no-wait -vreconnect:ANY:emer --db=unix:socket -- --columns=name find bridge '$1' | sort | xargs echo], [0], [$2
-+ [AT_CHECK([echo `ovs-vsctl --bare --timeout=5 --no-wait -vreconnect:ANY:emer --db=unix:socket -- --columns=name find bridge '$1' | sort`], [0], [$2
- ])])
-
- # Arithmetic relational operators without keys.
-diff --git tests/ovsdb-server.at tests/ovsdb-server.at
-index f5db1a8..b81878c 100644
---- tests/ovsdb-server.at
-+++ tests/ovsdb-server.at
-@@ -322,8 +322,8 @@ dnl We can't fully re-check the contents of the database log, because the
- dnl order of the records is not predictable, but there should only be 4 lines
- dnl in it now.
- AT_CAPTURE_FILE([db])
--AT_CHECK([wc -l < db], [0], [4
--], [], [test ! -e pid || kill `cat pid`])
-+AT_CHECK([test `wc -l < db` -eq 4], [0], [], [],
-+ [test ! -e pid || kill `cat pid`])
- dnl And check that the dumped data is the same too:
- AT_CHECK([ovsdb-client dump unix:socket ordinals], [0], [stdout], [ignore],
- [test ! -e pid || kill `cat pid`])
-@@ -348,8 +348,8 @@ AT_CHECK(
- [0], [[[{"count":3}]
- ]], [ignore], [test ! -e pid || kill `cat pid`])
- dnl There should be 6 lines in the log now.
--AT_CHECK([wc -l < db], [0], [6
--], [], [test ! -e pid || kill `cat pid`])
-+AT_CHECK([test `wc -l < db` -eq 6], [0], [], [],
-+ [test ! -e pid || kill `cat pid`])
- dnl Then check that the dumped data is correct.
- AT_CHECK([ovsdb-client dump unix:socket ordinals], [0], [stdout], [ignore],
- [test ! -e pid || kill `cat pid`])
-diff --git tests/ovsdb-tool.at tests/ovsdb-tool.at
-index 2d19b32..87949bb 100644
---- tests/ovsdb-tool.at
-+++ tests/ovsdb-tool.at
-@@ -121,8 +121,7 @@ dnl We can't fully re-check the contents of the database log, because the
- dnl order of the records is not predictable, but there should only be 4 lines
- dnl in it now.
- AT_CAPTURE_FILE([db])
--AT_CHECK([wc -l < db], [0], [4
--])
-+AT_CHECK([test `wc -l < db` -eq 4])
- dnl And check that the dumped data is the same too:
- AT_CHECK([[ovsdb-server --unixctl="`pwd`"/unixctl --remote=punix:socket --run "ovsdb-client dump unix:socket ordinals" db]],
- [0], [stdout], [ignore])
-@@ -185,8 +184,7 @@ dnl We can't fully re-check the contents of the database log, because the
- dnl order of the records is not predictable, but there should only be 4 lines
- dnl in it now.
- AT_CAPTURE_FILE([db])
--AT_CHECK([wc -l < db], [0], [4
--])
-+AT_CHECK([test `wc -l < db` -eq 4])
- dnl And check that the dumped data is the same except for the removed column:
- AT_CHECK([[ovsdb-server --unixctl="`pwd`"/unixctl --remote=punix:socket --run "ovsdb-client dump unix:socket ordinals" db]],
- [0], [stdout], [ignore])
-@@ -248,8 +246,7 @@ dnl We can't fully re-check the contents of the database log, because the
- dnl order of the records is not predictable, but there should only be 4 lines
- dnl in it now.
- AT_CAPTURE_FILE([db])
--AT_CHECK([wc -l < db], [0], [4
--])
-+AT_CHECK([test `wc -l < db` -eq 4])
- dnl And check that the dumped data is the same except for the added column:
- AT_CHECK([[ovsdb-server --unixctl="`pwd`"/unixctl --remote=punix:socket --run "ovsdb-client dump unix:socket ordinals" db]],
- [0], [stdout], [ignore])
-diff --git tests/test-vconn.c tests/test-vconn.c
-index 31451a2..3f57b6d 100644
---- tests/test-vconn.c
-+++ tests/test-vconn.c
-@@ -140,20 +140,26 @@ static void
- test_refuse_connection(int argc OVS_UNUSED, char *argv[])
- {
- const char *type = argv[1];
-- int expected_error;
- struct fake_pvconn fpv;
- struct vconn *vconn;
--
-- expected_error = (!strcmp(type, "unix") ? EPIPE
-- : !strcmp(type, "tcp") ? ECONNRESET
-- : EPROTO);
-+ int error;
-
- fpv_create(type, &fpv);
- CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
- DSCP_DEFAULT), 0);
- fpv_close(&fpv);
- vconn_run(vconn);
-- CHECK_ERRNO(vconn_connect(vconn), expected_error);
-+
-+ error = vconn_connect_block(vconn);
-+ if (!strcmp(type, "tcp")) {
-+ if (error != ECONNRESET && error != EPIPE) {
-+ ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
-+ error, strerror(error));
-+ }
-+ } else {
-+ CHECK_ERRNO(error, !strcmp(type, "unix") ? EPIPE : EPROTO);
-+ }
-+
- vconn_close(vconn);
- fpv_destroy(&fpv);
- }
-@@ -194,6 +200,7 @@ test_read_hello(int argc OVS_UNUSED, char *argv[])
- struct fake_pvconn fpv;
- struct vconn *vconn;
- struct stream *stream;
-+ int error;
-
- fpv_create(type, &fpv);
- CHECK_ERRNO(vconn_open(fpv.vconn_name, OFP10_VERSION, &vconn,
-@@ -223,7 +230,11 @@ test_read_hello(int argc OVS_UNUSED, char *argv[])
- poll_block();
- }
- stream_close(stream);
-- CHECK_ERRNO(vconn_connect(vconn), ECONNRESET);
-+ error = vconn_connect_block(vconn);
-+ if (error != ECONNRESET && error != EPIPE) {
-+ ovs_fatal(0, "unexpected vconn_connect() return value %d (%s)",
-+ error, strerror(error));
-+ }
- vconn_close(vconn);
- }
-
-@@ -313,7 +324,7 @@ test_send_hello(const char *type, const void *out, size_t out_size,
- poll_block();
- }
- stream_close(stream);
-- CHECK_ERRNO(vconn_recv(vconn, &msg), EOF);
-+ CHECK_ERRNO(vconn_recv_block(vconn, &msg), EOF);
- vconn_close(vconn);
- }
-
-diff --git utilities/ovs-pki.in utilities/ovs-pki.in
-index 019ffcf..2dc4060 100755
---- utilities/ovs-pki.in
-+++ utilities/ovs-pki.in
-@@ -24,6 +24,38 @@ batch=no
- log='@LOGDIR@/ovs-pki.log'
- keytype=rsa
- bits=2048
-+
-+# OS-specific compatibility routines
-+case $(uname -s) in
-+FreeBSD)
-+ file_mod_epoch()
-+ {
-+ stat -r "$1" | awk '{print $10}'
-+ }
-+
-+ file_mod_date()
-+ {
-+ stat -f '%Sm' "$1"
-+ }
-+
-+ sha1sum()
-+ {
-+ sha1 "$@"
-+ }
-+ ;;
-+*)
-+ file_mod_epoch()
-+ {
-+ date -r "$1" +%s
-+ }
-+
-+ file_mod_date()
-+ {
-+ date -r "$1"
-+ }
-+ ;;
-+esac
-+
- for option; do
- # This option-parsing mechanism borrowed from a Autoconf-generated
- # configure script under the following license:
-@@ -365,7 +397,7 @@ make_tmpdir() {
- fingerprint() {
- file=$1
- name=${1-$2}
-- date=$(date -r $file)
-+ date=$(file_mod_date "$file")
- if grep -e '-BEGIN CERTIFICATE-' "$file" > /dev/null; then
- fingerprint=$(openssl x509 -noout -in "$file" -fingerprint |
- sed 's/SHA1 Fingerprint=//' | tr -d ':')
-@@ -609,7 +641,7 @@ elif test "$command" = expire; then
- for type in switch controller; do
- cd "$pkidir/${type}ca/incoming" || exit 1
- for file in $(glob "*"); do
-- time=$(date -r "$file" +%s)
-+ time=$(file_mod_epoch "$file")
- if test "$time" -lt "$cutoff"; then
- rm -f "$file"
- fi
-diff --git xenserver/opt_xensource_libexec_interface-reconfigure xenserver/opt_xensource_libexec_interface-reconfigure
-index 6c6de9f..72bd5e4 100755
---- xenserver/opt_xensource_libexec_interface-reconfigure
-+++ xenserver/opt_xensource_libexec_interface-reconfigure
-@@ -1,4 +1,4 @@
--#!/usr/bin/python
-+#!/usr/bin/env python
- #
- # Copyright (c) 2008,2009 Citrix Systems, Inc.
- #
diff --git a/net/openvswitch/files/patch-lib-netdev-bsd.c b/net/openvswitch/files/patch-lib-netdev-bsd.c
new file mode 100644
index 000000000000..d7fcb081a9ac
--- /dev/null
+++ b/net/openvswitch/files/patch-lib-netdev-bsd.c
@@ -0,0 +1,24 @@
+--- lib/netdev-bsd.c.orig 2015-04-08 13:49:06.863643000 +0800
++++ lib/netdev-bsd.c 2015-04-08 13:50:53.973989000 +0800
+@@ -1771,7 +1771,7 @@
+ ifr_get_flags(const struct ifreq *ifr)
+ {
+ #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
+- return (ifr->ifr_flagshigh << 16) | ifr->ifr_flags;
++ return (ifr->ifr_flagshigh << 16) | (ifr->ifr_flags & 0xffff);
+ #else
+ return ifr->ifr_flags;
+ #endif
+@@ -1780,9 +1780,11 @@
+ static void
+ ifr_set_flags(struct ifreq *ifr, int flags)
+ {
+- ifr->ifr_flags = flags;
+ #ifdef HAVE_STRUCT_IFREQ_IFR_FLAGSHIGH
++ ifr->ifr_flags = flags & 0xffff;
+ ifr->ifr_flagshigh = flags >> 16;
++#else
++ ifr->ifr_flags = flags;
+ #endif
+ }
+
diff --git a/net/openvswitch/files/threaded.diff b/net/openvswitch/files/threaded.diff
deleted file mode 100644
index a3bd811ca151..000000000000
--- a/net/openvswitch/files/threaded.diff
+++ /dev/null
@@ -1,1292 +0,0 @@
-diff --git configure.ac configure.ac
-index 5692b86..ff62627 100644
---- configure.ac
-+++ configure.ac
-@@ -43,6 +43,7 @@ AC_SEARCH_LIBS([clock_gettime], [rt])
- AC_SEARCH_LIBS([timer_create], [rt])
- AC_SEARCH_LIBS([pcap_open_live], [pcap])
-
-+OVS_CHECK_THREADED
- OVS_CHECK_COVERAGE
- OVS_CHECK_NDEBUG
- OVS_CHECK_NETLINK
-diff --git lib/automake.mk lib/automake.mk
-index 13622b3..87bdd8d 100644
---- lib/automake.mk
-+++ lib/automake.mk
-@@ -37,6 +37,7 @@ lib_libopenvswitch_a_SOURCES = \
- lib/daemon.c \
- lib/daemon.h \
- lib/dhcp.h \
-+ lib/dispatch.h \
- lib/dummy.c \
- lib/dummy.h \
- lib/dhparams.h \
-diff --git lib/dispatch.h lib/dispatch.h
-new file mode 100644
-index 0000000..80ac9c7
---- /dev/null
-+++ lib/dispatch.h
-@@ -0,0 +1,9 @@
-+#include <sys/types.h>
-+#include "ofpbuf.h"
-+
-+#ifndef DISPATCH_H
-+#define DISPATCH_H 1
-+
-+typedef void (*pkt_handler)(u_char *user, struct ofpbuf* buf);
-+
-+#endif /* DISPATCH_H */
-diff --git lib/dpif-netdev.c lib/dpif-netdev.c
-index cade79e..509e2ef 100644
---- lib/dpif-netdev.c
-+++ lib/dpif-netdev.c
-@@ -32,6 +32,15 @@
- #include <sys/stat.h>
- #include <unistd.h>
-
-+#ifdef THREADED
-+#include <signal.h>
-+#include <pthread.h>
-+
-+#include "socket-util.h"
-+#include "fatal-signal.h"
-+#include "dispatch.h"
-+#endif
-+
- #include "csum.h"
- #include "dpif.h"
- #include "dpif-provider.h"
-@@ -55,6 +64,16 @@
- #include "vlog.h"
-
- VLOG_DEFINE_THIS_MODULE(dpif_netdev);
-+/* We could use these macros instead of using #ifdef and #endif every time we
-+ * need to call the pthread_mutex_lock/unlock.
-+#ifdef THREADED
-+#define LOCK(mutex) pthread_mutex_lock(mutex)
-+#define UNLOCK(mutex) pthread_mutex_unlock(mutex)
-+#else
-+#define LOCK(mutex)
-+#define UNLOCK(mutex)
-+#endif
-+*/
-
- /* Configuration parameters. */
- enum { MAX_PORTS = 256 }; /* Maximum number of ports. */
-@@ -82,6 +101,21 @@ struct dp_netdev {
- int open_cnt;
- bool destroyed;
-
-+#ifdef THREADED
-+ /* The pipe is used to signal the presence of a packet on the queue.
-+ * - dpif_netdev_recv_wait() waits on p[0]
-+ * - dpif_netdev_recv() extract from queue and read p[0]
-+ * - dp_netdev_output_control() send to queue and write p[1]
-+ */
-+
-+ int pipe[2]; /* signal a packet on the queue */
-+ struct pollfd *pipe_fd;
-+
-+ pthread_mutex_t table_mutex; /* mutex for the flow table */
-+ pthread_mutex_t port_list_mutex; /* port list mutex */
-+
-+ /* The access to this queue is protected by the table_mutex mutex */
-+#endif
- struct dp_netdev_queue queues[N_QUEUES];
- struct hmap flow_table; /* Flow table. */
-
-@@ -102,6 +136,9 @@ struct dp_netdev_port {
- struct list node; /* Element in dp_netdev's 'port_list'. */
- struct netdev *netdev;
- char *type; /* Port type as requested by user. */
-+#ifdef THREADED
-+ struct pollfd *poll_fd; /* To manage the poll loop in the thread. */
-+#endif
- };
-
- /* A flow in dp_netdev's 'flow_table'. */
-@@ -127,6 +164,11 @@ struct dpif_netdev {
- unsigned int dp_serial;
- };
-
-+#ifdef THREADED
-+/* XXX global Descriptor of the thread that manages the datapaths. */
-+pthread_t thread_p;
-+#endif
-+
- /* All netdev-based datapaths. */
- static struct shash dp_netdevs = SHASH_INITIALIZER(&dp_netdevs);
-
-@@ -204,6 +246,23 @@ create_dp_netdev(const char *name, const struct dpif_class *class,
- dp->class = class;
- dp->name = xstrdup(name);
- dp->open_cnt = 0;
-+#ifdef THREADED
-+ error = pipe(dp->pipe);
-+ if (error) {
-+ VLOG_ERR("Unable to create datapath thread pipe: %s", strerror(errno));
-+ return errno;
-+ }
-+ if (set_nonblocking(dp->pipe[0]) || set_nonblocking(dp->pipe[1])) {
-+ VLOG_ERR("Unable to set nonblocking on datapath thread pipe: %s",
-+ strerror(errno));
-+ return errno;
-+ }
-+ dp->pipe_fd = NULL;
-+ VLOG_DBG("Datapath thread pipe created (%d, %d)", dp->pipe[0], dp->pipe[1]);
-+
-+ pthread_mutex_init(&dp->table_mutex, NULL);
-+ pthread_mutex_init(&dp->port_list_mutex, NULL);
-+#endif
- for (i = 0; i < N_QUEUES; i++) {
- dp->queues[i].head = dp->queues[i].tail = 0;
- }
-@@ -221,6 +280,38 @@ create_dp_netdev(const char *name, const struct dpif_class *class,
- return 0;
- }
-
-+#ifdef THREADED
-+static void * dp_thread_body(void *args OVS_UNUSED);
-+
-+/* This is the function that is called in response of a fatal signal (e.g.
-+ * SIGTERM) */
-+static void
-+dpif_netdev_exit_hook(void *aux OVS_UNUSED)
-+{
-+ if (pthread_cancel(thread_p) == 0) {
-+ pthread_join(thread_p, NULL);
-+ }
-+}
-+
-+static int
-+dpif_netdev_init(void)
-+{
-+ static int error = -1;
-+
-+ if (error < 0) {
-+ fatal_signal_add_hook(dpif_netdev_exit_hook, NULL, NULL, true);
-+ error = pthread_create(&thread_p, NULL, dp_thread_body, NULL);
-+ if (error != 0) {
-+ VLOG_ERR("Unable to create datapath thread: %s", strerror(errno));
-+ error = errno;
-+ } else {
-+ VLOG_DBG("Datapath thread started");
-+ }
-+ }
-+ return error;
-+}
-+#endif
-+
- static int
- dpif_netdev_open(const struct dpif_class *class, const char *name,
- bool create, struct dpif **dpifp)
-@@ -247,9 +338,14 @@ dpif_netdev_open(const struct dpif_class *class, const char *name,
- }
-
- *dpifp = create_dpif_netdev(dp);
-+#ifdef THREADED
-+ dpif_netdev_init();
-+#endif
- return 0;
- }
-
-+/* table_mutex must be locked in THREADED mode.
-+ */
- static void
- dp_netdev_purge_queues(struct dp_netdev *dp)
- {
-@@ -273,11 +369,23 @@ dp_netdev_free(struct dp_netdev *dp)
- struct dp_netdev_port *port, *next;
-
- dp_netdev_flow_flush(dp);
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->port_list_mutex);
-+#endif
- LIST_FOR_EACH_SAFE (port, next, node, &dp->port_list) {
- do_del_port(dp, port->port_no);
- }
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- dp_netdev_purge_queues(dp);
- hmap_destroy(&dp->flow_table);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+ pthread_mutex_destroy(&dp->table_mutex);
-+ pthread_mutex_destroy(&dp->port_list_mutex);
-+#endif
- free(dp->name);
- free(dp);
- }
-@@ -306,7 +414,13 @@ static int
- dpif_netdev_get_stats(const struct dpif *dpif, struct dpif_dp_stats *stats)
- {
- struct dp_netdev *dp = get_dp_netdev(dpif);
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- stats->n_flows = hmap_count(&dp->flow_table);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- stats->n_hit = dp->n_hit;
- stats->n_missed = dp->n_missed;
- stats->n_lost = dp->n_lost;
-@@ -354,13 +468,22 @@ do_add_port(struct dp_netdev *dp, const char *devname, const char *type,
- port->port_no = port_no;
- port->netdev = netdev;
- port->type = xstrdup(type);
-+#ifdef THREADED
-+ port->poll_fd = NULL;
-+#endif
-
- error = netdev_get_mtu(netdev, &mtu);
- if (!error) {
- max_mtu = mtu;
- }
-
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->port_list_mutex);
-+#endif
- list_push_back(&dp->port_list, &port->node);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+#endif
- dp->ports[port_no] = port;
- dp->serial++;
-
-@@ -448,15 +571,25 @@ get_port_by_name(struct dp_netdev *dp,
- {
- struct dp_netdev_port *port;
-
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->port_list_mutex);
-+#endif
- LIST_FOR_EACH (port, node, &dp->port_list) {
- if (!strcmp(netdev_get_name(port->netdev), devname)) {
- *portp = port;
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+#endif
- return 0;
- }
- }
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+#endif
- return ENOENT;
- }
-
-+/* In THREADED mode, must be called with port_list_mutex held. */
- static int
- do_del_port(struct dp_netdev *dp, uint16_t port_no)
- {
-@@ -531,7 +664,13 @@ dpif_netdev_get_max_ports(const struct dpif *dpif OVS_UNUSED)
- static void
- dp_netdev_free_flow(struct dp_netdev *dp, struct dp_netdev_flow *flow)
- {
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- hmap_remove(&dp->flow_table, &flow->node);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- free(flow->actions);
- free(flow);
- }
-@@ -620,7 +759,11 @@ dpif_netdev_port_poll_wait(const struct dpif *dpif_)
- }
-
- static struct dp_netdev_flow *
--dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
-+#ifdef THREADED
-+dp_netdev_lookup_flow_locked(struct dp_netdev *dp, const struct flow *key)
-+#else
-+dp_netdev_lookup_flow(struct dp_netdev *dp, const struct flow *key)
-+#endif
- {
- struct dp_netdev_flow *flow;
-
-@@ -632,6 +775,19 @@ dp_netdev_lookup_flow(const struct dp_netdev *dp, const struct flow *key)
- return NULL;
- }
-
-+#ifdef THREADED
-+static struct dp_netdev_flow *
-+dp_netdev_lookup_flow(struct dp_netdev *dp, const struct flow *key)
-+{
-+ struct dp_netdev_flow *flow;
-+
-+ pthread_mutex_lock(&dp->table_mutex);
-+ flow = dp_netdev_lookup_flow_locked(dp, key);
-+ pthread_mutex_unlock(&dp->table_mutex);
-+ return flow;
-+}
-+#endif
-+
- static void
- get_dpif_flow_stats(struct dp_netdev_flow *flow, struct dpif_flow_stats *stats)
- {
-@@ -729,7 +885,13 @@ add_flow(struct dpif *dpif, const struct flow *key,
- return error;
- }
-
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- hmap_insert(&dp->flow_table, &flow->node, flow_hash(&flow->key, 0));
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- return 0;
- }
-
-@@ -749,6 +911,7 @@ dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
- struct dp_netdev_flow *flow;
- struct flow key;
- int error;
-+ int n_flows;
-
- error = dpif_netdev_flow_from_nlattrs(put->key, put->key_len, &key);
- if (error) {
-@@ -758,7 +921,14 @@ dpif_netdev_flow_put(struct dpif *dpif, const struct dpif_flow_put *put)
- flow = dp_netdev_lookup_flow(dp, &key);
- if (!flow) {
- if (put->flags & DPIF_FP_CREATE) {
-- if (hmap_count(&dp->flow_table) < MAX_FLOWS) {
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
-+ n_flows = hmap_count(&dp->flow_table);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
-+ if (n_flows < MAX_FLOWS) {
- if (put->stats) {
- memset(put->stats, 0, sizeof *put->stats);
- }
-@@ -843,7 +1013,13 @@ dpif_netdev_flow_dump_next(const struct dpif *dpif, void *state_,
- struct dp_netdev_flow *flow;
- struct hmap_node *node;
-
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- node = hmap_at_position(&dp->flow_table, &state->bucket, &state->offset);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- if (!node) {
- return EOF;
- }
-@@ -949,7 +1125,13 @@ static int
- dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
- struct ofpbuf *buf)
- {
-- struct dp_netdev_queue *q = find_nonempty_queue(dpif);
-+ struct dp_netdev_queue *q;
-+#ifdef THREADED
-+ struct dp_netdev *dp = get_dp_netdev(dpif);
-+ char c;
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
-+ q = find_nonempty_queue(dpif);
- if (q) {
- struct dpif_upcall *u = q->upcalls[q->tail++ & QUEUE_MASK];
- *upcall = *u;
-@@ -958,8 +1140,19 @@ dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
- ofpbuf_uninit(buf);
- *buf = *upcall->packet;
-
-+#ifdef THREADED
-+ /* Read a byte from the pipe to signal that a packet has been
-+ * received. */
-+ if (read(dp->pipe[0], &c, 1) < 0) {
-+ VLOG_ERR("Pipe read error (from datapath): %s", strerror(errno));
-+ }
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- return 0;
- } else {
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- return EAGAIN;
- }
- }
-@@ -967,19 +1160,32 @@ dpif_netdev_recv(struct dpif *dpif, struct dpif_upcall *upcall,
- static void
- dpif_netdev_recv_wait(struct dpif *dpif)
- {
-+#ifdef THREADED
-+ struct dp_netdev *dp = get_dp_netdev(dpif);
-+
-+ poll_fd_wait(dp->pipe[0], POLLIN);
-+#else
- if (find_nonempty_queue(dpif)) {
- poll_immediate_wake();
- } else {
- /* No messages ready to be received, and dp_wait() will ensure that we
- * wake up to queue new messages, so there is nothing to do. */
- }
-+#endif
- }
-
- static void
- dpif_netdev_recv_purge(struct dpif *dpif)
- {
- struct dpif_netdev *dpif_netdev = dpif_netdev_cast(dpif);
-+#ifdef THREADED
-+ struct dp_netdev *dp = get_dp_netdev(dpif);
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- dp_netdev_purge_queues(dpif_netdev->dp);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- }
-
- static void
-@@ -1003,7 +1209,12 @@ dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
- return;
- }
- flow_extract(packet, 0, 0, odp_port_to_ofp_port(port->port_no), &key);
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+ flow = dp_netdev_lookup_flow_locked(dp, &key);
-+#else
- flow = dp_netdev_lookup_flow(dp, &key);
-+#endif
- if (flow) {
- dp_netdev_flow_used(flow, &key, packet);
- dp_netdev_execute_actions(dp, packet, &key,
-@@ -1013,8 +1224,22 @@ dp_netdev_port_input(struct dp_netdev *dp, struct dp_netdev_port *port,
- dp->n_missed++;
- dp_netdev_output_userspace(dp, packet, DPIF_UC_MISS, &key, 0);
- }
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- }
-
-+#ifdef THREADED
-+static void
-+dpif_netdev_run(struct dpif *dpif OVS_UNUSED)
-+{
-+}
-+
-+static void
-+dpif_netdev_wait(struct dpif *dpif OVS_UNUSED)
-+{
-+}
-+#else
- static void
- dpif_netdev_run(struct dpif *dpif)
- {
-@@ -1053,6 +1278,144 @@ dpif_netdev_wait(struct dpif *dpif)
- netdev_recv_wait(port->netdev);
- }
- }
-+#endif
-+
-+#ifdef THREADED
-+/*
-+ * pcap callback argument
-+ */
-+struct dispatch_arg {
-+ struct dp_netdev *dp; /* update statistics */
-+ struct dp_netdev_port *port; /* argument to flow identifier function */
-+};
-+
-+/* Process a packet.
-+ *
-+ * The port_input function will send immediately if it finds a flow match and
-+ * the associated action is ODPAT_OUTPUT or ODPAT_OUTPUT_GROUP.
-+ * If a flow is not found or for the other actions, the packet is copied.
-+ */
-+static void
-+process_pkt(u_char *user, struct ofpbuf *buf)
-+{
-+ struct dispatch_arg *arg = (struct dispatch_arg *)user;
-+
-+ ofpbuf_padto(buf, ETH_TOTAL_MIN);
-+ dp_netdev_port_input(arg->dp, arg->port, buf);
-+}
-+
-+/* Body of the thread that manages the datapaths */
-+static void*
-+dp_thread_body(void *args OVS_UNUSED)
-+{
-+ struct dp_netdev *dp;
-+ struct dp_netdev_port *port;
-+ struct dispatch_arg arg;
-+ int error;
-+ int n_fds;
-+ uint32_t batch = 50; /* max number of pkts processed by the dispatch */
-+ int processed; /* actual number of pkts processed by the dispatch */
-+ char readbuf[1024];
-+
-+ sigset_t sigmask;
-+
-+ /*XXX Since the poll involves all ports of all datapaths, the right fds
-+ * size should be MAX_PORTS * max_number_of_datapaths */
-+ struct pollfd fds[MAX_PORTS + 1];
-+
-+ /* mask the fatal signals. In this way the main thread is delegate to
-+ * manage this them. */
-+ sigemptyset(&sigmask);
-+ sigaddset(&sigmask, SIGTERM);
-+ sigaddset(&sigmask, SIGALRM);
-+ sigaddset(&sigmask, SIGINT);
-+ sigaddset(&sigmask, SIGHUP);
-+
-+ if (pthread_sigmask(SIG_BLOCK, &sigmask, NULL) != 0) {
-+ VLOG_ERR("Error setting thread sigmask: %s", strerror(errno));
-+ }
-+
-+ for(;;) {
-+ struct shash_node *node;
-+ n_fds = 0;
-+ /* build the structure for poll */
-+ SHASH_FOR_EACH(node, &dp_netdevs) {
-+ dp = (struct dp_netdev *)node->data;
-+ fds[n_fds].fd = dp->pipe[1];
-+ fds[n_fds].events = POLLIN;
-+ dp->pipe_fd = &fds[n_fds];
-+ n_fds++;
-+ if (n_fds >= sizeof(fds) / sizeof(fds[0])) {
-+ VLOG_ERR("Too many fds for poll adding pipe_fd");
-+ break;
-+ }
-+ pthread_mutex_lock(&dp->port_list_mutex);
-+ LIST_FOR_EACH (port, node, &dp->port_list) {
-+ /* insert an element in the fds structure */
-+ fds[n_fds].fd = netdev_get_fd(port->netdev);
-+ fds[n_fds].events = POLLIN;
-+ port->poll_fd = &fds[n_fds];
-+ n_fds++;
-+ if (n_fds >= sizeof(fds) / sizeof(fds[0])) {
-+ VLOG_ERR("Too many fds for poll adding port fd");
-+ break;
-+ }
-+ }
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+ }
-+
-+ error = poll(fds, n_fds, 2000);
-+ VLOG_DBG("dp_thread_body poll wakeup with cnt=%d", error);
-+
-+ if (error < 0) {
-+ if (errno == EINTR) {
-+ /* XXX get this case in detach mode */
-+ continue;
-+ }
-+ VLOG_ERR("Datapath thread poll() error: %s\n", strerror(errno));
-+ /* XXX terminating the thread is probably not right */
-+ break;
-+ }
-+ pthread_testcancel();
-+
-+ SHASH_FOR_EACH (node, &dp_netdevs) {
-+ dp = (struct dp_netdev *)node->data;
-+ if (dp->pipe_fd && (dp->pipe_fd->revents & POLLIN)) {
-+ VLOG_DBG("Signalled from main thread");
-+ while ((error = read(dp->pipe[1], readbuf, sizeof(readbuf))) > 0)
-+ ;
-+ if (error < 0 && errno != EAGAIN) {
-+ VLOG_ERR("Pipe read error (to datapath): %s", strerror(errno));
-+ }
-+ }
-+ arg.dp = dp;
-+ pthread_mutex_lock(&dp->port_list_mutex);
-+ LIST_FOR_EACH (port, node, &dp->port_list) {
-+ arg.port = port;
-+ if (port->poll_fd) {
-+ VLOG_DBG("fd %d revents 0x%x", port->poll_fd->fd, port->poll_fd->revents);
-+ }
-+ if (port->poll_fd && (port->poll_fd->revents & POLLIN)) {
-+ /* call the dispatch and process the packet into
-+ * its callback. We process 'batch' packets at time */
-+ processed = netdev_dispatch(port->netdev, batch,
-+ process_pkt, (u_char *)&arg);
-+ if (processed < 0) { /* pcap returns error */
-+ static struct vlog_rate_limit rl =
-+ VLOG_RATE_LIMIT_INIT(1, 5);
-+ VLOG_ERR_RL(&rl,
-+ "error receiving data from XXX \n");
-+ }
-+ } /* end of if poll */
-+ } /* end of port loop */
-+ pthread_mutex_unlock(&dp->port_list_mutex);
-+ } /* end of dp loop */
-+ } /* for ;; */
-+
-+ return NULL;
-+}
-+
-+#endif /* THREADED */
-
- static void
- dp_netdev_set_dl(struct ofpbuf *packet, const struct ovs_key_ethernet *eth_key)
-@@ -1068,11 +1431,19 @@ dp_netdev_output_port(struct dp_netdev *dp, struct ofpbuf *packet,
- uint16_t out_port)
- {
- struct dp_netdev_port *p = dp->ports[out_port];
-+ char c = 0;
-+
- if (p) {
- netdev_send(p->netdev, packet);
-+#ifdef THREADED
-+ if (write(dp->pipe[0], &c, 1) < 0) {
-+ VLOG_ERR("Pipe write error (to datapath): %s", strerror(errno));
-+ }
-+#endif
- }
- }
-
-+/* In THREADED mode, must be called with table_lock_mutex held. */
- static int
- dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
- int queue_no, const struct flow *flow, uint64_t arg)
-@@ -1081,6 +1452,9 @@ dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
- struct dpif_upcall *upcall;
- struct ofpbuf *buf;
- size_t key_len;
-+#ifdef THREADED
-+ char c = 0;
-+#endif
-
- if (q->head - q->tail >= MAX_QUEUE_LEN) {
- dp->n_lost++;
-@@ -1102,6 +1476,12 @@ dp_netdev_output_userspace(struct dp_netdev *dp, const struct ofpbuf *packet,
- upcall->userdata = arg;
-
- q->upcalls[q->head++ & QUEUE_MASK] = upcall;
-+#ifdef THREADED
-+ /* Write a byte on the pipe to advertise that a packet is ready. */
-+ if (write(dp->pipe[1], &c, 1) < 0) {
-+ VLOG_ERR("Pipe write error (from datapath): %s", strerror(errno));
-+ }
-+#endif
-
- return 0;
- }
-@@ -1150,7 +1530,13 @@ dp_netdev_action_userspace(struct dp_netdev *dp,
-
- userdata_attr = nl_attr_find_nested(a, OVS_USERSPACE_ATTR_USERDATA);
- userdata = userdata_attr ? nl_attr_get_u64(userdata_attr) : 0;
-+#ifdef THREADED
-+ pthread_mutex_lock(&dp->table_mutex);
-+#endif
- dp_netdev_output_userspace(dp, packet, DPIF_UC_ACTION, key, userdata);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dp->table_mutex);
-+#endif
- }
-
- static void
-diff --git lib/netdev-bsd.c lib/netdev-bsd.c
-index 0b1a37c..ff79367 100644
---- lib/netdev-bsd.c
-+++ lib/netdev-bsd.c
-@@ -667,6 +667,89 @@ netdev_bsd_recv_wait(struct netdev *netdev_)
- }
- }
-
-+#ifdef THREADED
-+
-+struct dispatch_arg {
-+ pkt_handler h;
-+ u_char *user;
-+};
-+
-+static void
-+dispatch_handler(u_char *user, const struct pcap_pkthdr *phdr, const u_char *pdata)
-+{
-+ struct ofpbuf buf;
-+ struct dispatch_arg *parg = (struct dispatch_arg*)user;
-+
-+ ofpbuf_use_stub(&buf, (void*)pdata, phdr->caplen);
-+ buf.size = phdr->caplen;
-+ (*parg->h)(parg->user, &buf);
-+ ofpbuf_uninit(&buf);
-+}
-+
-+static int
-+netdev_bsd_dispatch_system(struct netdev_bsd *netdev, int batch, pkt_handler h,
-+ u_char *user)
-+{
-+ int ret;
-+ struct dispatch_arg arg;
-+
-+ arg.h = h;
-+ arg.user = user;
-+ ret = pcap_dispatch(netdev->pcap_handle, batch, dispatch_handler, (u_char*)&arg);
-+ return ret;
-+}
-+
-+static int
-+netdev_bsd_dispatch_tap(struct netdev_bsd *netdev, int batch, pkt_handler h,
-+ u_char *user)
-+{
-+ int ret;
-+ int i;
-+ const size_t size = VLAN_HEADER_LEN + ETH_HEADER_LEN + ETH_PAYLOAD_MAX;
-+ OFPBUF_STACK_BUFFER(buf_, size);
-+
-+ struct ofpbuf buf;
-+ ofpbuf_use_stub(&buf, buf_, size);
-+ for (i = 0; i < batch; i++) {
-+ ret = netdev_bsd_recv_tap(netdev, buf.data, ofpbuf_tailroom(&buf));
-+ if (ret >= 0) {
-+ buf.size += ret;
-+ h(user, &buf);
-+ } else if (ret != -EAGAIN) {
-+ return -1;
-+ } else { /* ret = EAGAIN */
-+ break;
-+ }
-+ ofpbuf_clear(&buf);
-+ }
-+ ofpbuf_uninit(&buf);
-+ return i;
-+}
-+
-+static int
-+netdev_bsd_dispatch(struct netdev *netdev_, int batch, pkt_handler h,
-+ u_char *user)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+ struct netdev_dev_bsd * netdev_dev =
-+ netdev_dev_bsd_cast(netdev_get_dev(netdev_));
-+
-+ if (!strcmp(netdev_get_type(netdev_), "tap") &&
-+ netdev->netdev_fd == netdev_dev->tap_fd) {
-+ return netdev_bsd_dispatch_tap(netdev, batch, h, user);
-+ } else {
-+ return netdev_bsd_dispatch_system(netdev, batch, h, user);
-+ }
-+}
-+
-+static int
-+netdev_bsd_get_fd(struct netdev *netdev_)
-+{
-+ struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-+ return netdev->netdev_fd;
-+}
-+#endif
-+
- /* Discards all packets waiting to be received from 'netdev'. */
- static int
- netdev_bsd_drain(struct netdev *netdev_)
-@@ -1263,6 +1346,10 @@ const struct netdev_class netdev_bsd_class = {
-
- netdev_bsd_recv,
- netdev_bsd_recv_wait,
-+#ifdef THREADED
-+ netdev_bsd_dispatch,
-+ netdev_bsd_get_fd,
-+#endif
- netdev_bsd_drain,
-
- netdev_bsd_send,
-@@ -1323,6 +1410,10 @@ const struct netdev_class netdev_tap_class = {
-
- netdev_bsd_recv,
- netdev_bsd_recv_wait,
-+#ifdef THREADED
-+ netdev_bsd_dispatch,
-+ netdev_bsd_get_fd,
-+#endif
- netdev_bsd_drain,
-
- netdev_bsd_send,
-diff --git lib/netdev-dummy.c lib/netdev-dummy.c
-index b8c23c5..4e4801c 100644
---- lib/netdev-dummy.c
-+++ lib/netdev-dummy.c
-@@ -20,6 +20,12 @@
-
- #include <errno.h>
-
-+#ifdef THREADED
-+#include <pthread.h>
-+#include <unistd.h>
-+#include "socket-util.h"
-+#endif
-+
- #include "flow.h"
- #include "list.h"
- #include "netdev-provider.h"
-@@ -51,6 +57,10 @@ struct netdev_dummy {
- struct list node; /* In netdev_dev_dummy's "devs" list. */
- struct list recv_queue;
- bool listening;
-+#ifdef THREADED
-+ pthread_mutex_t queue_mutex;
-+ int s_pipe[2]; /* used to signal packet arrivals */
-+#endif
- };
-
- static struct shash dummy_netdev_devs = SHASH_INITIALIZER(&dummy_netdev_devs);
-@@ -124,11 +134,30 @@ netdev_dummy_open(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
- {
- struct netdev_dev_dummy *netdev_dev = netdev_dev_dummy_cast(netdev_dev_);
- struct netdev_dummy *netdev;
-+#ifdef THREADED
-+ int error;
-+#endif
-
- netdev = xmalloc(sizeof *netdev);
- netdev_init(&netdev->netdev, netdev_dev_);
- list_init(&netdev->recv_queue);
- netdev->listening = false;
-+#ifdef THREADED
-+ error = pipe(netdev->s_pipe);
-+ if (error) {
-+ VLOG_ERR("Unable to create dummy pipe: %s", strerror(errno));
-+ free(netdev);
-+ return errno;
-+ }
-+ if (set_nonblocking(netdev->s_pipe[0]) ||
-+ set_nonblocking(netdev->s_pipe[1])) {
-+ VLOG_ERR("Unable to set nonblocking on dummy pipe: %s",
-+ strerror(errno));
-+ free(netdev);
-+ return errno;
-+ }
-+ pthread_mutex_init(&netdev->queue_mutex, NULL);
-+#endif
-
- *netdevp = &netdev->netdev;
- list_push_back(&netdev_dev->devs, &netdev->node);
-@@ -141,6 +170,13 @@ netdev_dummy_close(struct netdev *netdev_)
- struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
- list_remove(&netdev->node);
- ofpbuf_list_delete(&netdev->recv_queue);
-+#ifdef THREADED
-+ if (netdev->listening) {
-+ close(netdev->s_pipe[0]);
-+ close(netdev->s_pipe[1]);
-+ }
-+ pthread_mutex_destroy(&netdev->queue_mutex);
-+#endif
- free(netdev);
- }
-
-@@ -158,12 +194,29 @@ netdev_dummy_recv(struct netdev *netdev_, void *buffer, size_t size)
- struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
- struct ofpbuf *packet;
- size_t packet_size;
-+#ifdef THREADED
-+ char c;
-+#endif
-
-+#ifdef THREADED
-+ pthread_mutex_lock(&netdev->queue_mutex);
-+#endif
- if (list_is_empty(&netdev->recv_queue)) {
-+#ifdef THREADED
-+ pthread_mutex_unlock(&netdev->queue_mutex);
-+#endif
- return -EAGAIN;
- }
-+#ifdef THREADED
-+ if (read(netdev->s_pipe[0], &c, 1) < 0) {
-+ VLOG_ERR("Error reading dummy pipe: %s", strerror(errno));
-+ }
-+#endif
-
- packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
-+#ifdef THREADED
-+ pthread_mutex_unlock(&netdev->queue_mutex);
-+#endif
- if (packet->size > size) {
- return -EMSGSIZE;
- }
-@@ -179,11 +232,60 @@ static void
- netdev_dummy_recv_wait(struct netdev *netdev_)
- {
- struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
-- if (!list_is_empty(&netdev->recv_queue)) {
-+ int empty;
-+
-+#ifdef THREADED
-+ pthread_mutex_lock(&netdev->queue_mutex);
-+#endif
-+ empty = list_is_empty(&netdev->recv_queue);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&netdev->queue_mutex);
-+#endif
-+ if (!empty) {
- poll_immediate_wake();
- }
- }
-
-+#ifdef THREADED
-+static int
-+netdev_dummy_dispatch(struct netdev *netdev_, int batch, pkt_handler h,
-+ u_char *user)
-+{
-+ int i;
-+ struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
-+ struct ofpbuf *packet;
-+ VLOG_DBG("dispatch %d", batch);
-+
-+ for (i = 0; i < batch; i++) {
-+ char c;
-+ if (read(netdev->s_pipe[0], &c, 1) < 0) {
-+ if (errno == EAGAIN)
-+ break;
-+ VLOG_ERR("%s: error reading from the pipe: %s",
-+ netdev_get_name(netdev_), strerror(errno));
-+ return -1;
-+ }
-+ pthread_mutex_lock(&netdev->queue_mutex);
-+ if (list_is_empty(&netdev->recv_queue)) {
-+ pthread_mutex_unlock(&netdev->queue_mutex);
-+ return -EAGAIN;
-+ }
-+ packet = ofpbuf_from_list(list_pop_front(&netdev->recv_queue));
-+ pthread_mutex_unlock(&netdev->queue_mutex);
-+ h(user, packet);
-+ ofpbuf_delete(packet);
-+ }
-+ return i;
-+}
-+
-+static int
-+netdev_dummy_get_fd(struct netdev *netdev_)
-+{
-+ struct netdev_dummy *netdev = netdev_dummy_cast(netdev_);
-+ return netdev->s_pipe[0];
-+}
-+#endif
-+
- static int
- netdev_dummy_drain(struct netdev *netdev_)
- {
-@@ -316,6 +418,10 @@ static const struct netdev_class dummy_class = {
- netdev_dummy_listen,
- netdev_dummy_recv,
- netdev_dummy_recv_wait,
-+#ifdef THREADED
-+ netdev_dummy_dispatch, /* dispatch */
-+ netdev_dummy_get_fd, /* get_fd */
-+#endif
- netdev_dummy_drain,
-
- NULL, /* send */
-@@ -407,6 +513,9 @@ netdev_dummy_receive(struct unixctl_conn *conn,
- struct netdev_dev_dummy *dummy_dev;
- int n_listeners;
- int i;
-+#ifdef THREADED
-+ char c = 0;
-+#endif
-
- dummy_dev = shash_find_data(&dummy_netdev_devs, argv[1]);
- if (!dummy_dev) {
-@@ -414,6 +523,7 @@ netdev_dummy_receive(struct unixctl_conn *conn,
- return;
- }
-
-+
- n_listeners = 0;
- for (i = 2; i < argc; i++) {
- struct netdev_dummy *dev;
-@@ -429,7 +539,16 @@ netdev_dummy_receive(struct unixctl_conn *conn,
- LIST_FOR_EACH (dev, node, &dummy_dev->devs) {
- if (dev->listening) {
- struct ofpbuf *copy = ofpbuf_clone(packet);
-+#ifdef THREADED
-+ pthread_mutex_lock(&dev->queue_mutex);
-+#endif
- list_push_back(&dev->recv_queue, &copy->list_node);
-+#ifdef THREADED
-+ pthread_mutex_unlock(&dev->queue_mutex);
-+ if (write(dev->s_pipe[1], &c, 1) < 0) {
-+ VLOG_ERR("Error writing dummy pipe: %s", strerror(errno));
-+ }
-+#endif
- n_listeners++;
- }
- }
-diff --git lib/netdev-linux.c lib/netdev-linux.c
-index 4d2f3ac..c33a801 100644
---- lib/netdev-linux.c
-+++ lib/netdev-linux.c
-@@ -891,6 +891,43 @@ netdev_linux_recv_wait(struct netdev *netdev_)
- }
- }
-
-+#ifdef THREADED
-+static int
-+netdev_linux_dispatch(struct netdev *netdev_, int batch, pkt_handler h,
-+ u_char *user)
-+{
-+ int ret;
-+ int i;
-+ const size_t size = VLAN_HEADER_LEN + ETH_HEADER_LEN + ETH_PAYLOAD_MAX;
-+ OFPBUF_STACK_BUFFER(buf_, size);
-+ struct ofpbuf buf;
-+ VLOG_DBG("dispatch %d", batch);
-+
-+ ofpbuf_use_stub(&buf, buf_, size);
-+ for (i = 0; i < batch; i++) {
-+ ret = netdev_linux_recv(netdev_, buf.data, ofpbuf_tailroom(&buf));
-+ if (ret >= 0) {
-+ buf.size += ret;
-+ h(user, &buf);
-+ } else if (ret != -EAGAIN) {
-+ return -1;
-+ } else {
-+ break;
-+ }
-+ ofpbuf_clear(&buf);
-+ }
-+ ofpbuf_uninit(&buf);
-+ return i;
-+}
-+
-+static int
-+netdev_linux_get_fd(struct netdev *netdev_)
-+{
-+ struct netdev_linux *netdev = netdev_linux_cast(netdev_);
-+ return netdev->fd;
-+}
-+#endif
-+
- /* Discards all packets waiting to be received from 'netdev'. */
- static int
- netdev_linux_drain(struct netdev *netdev_)
-@@ -2376,6 +2413,12 @@ netdev_linux_change_seq(const struct netdev *netdev)
- return netdev_dev_linux_cast(netdev_get_dev(netdev))->change_seq;
- }
-
-+#ifdef THREADED
-+#define THREADED_NULL NULL, NULL,
-+#else
-+#define THREADED_NULL
-+#endif
-+
- #define NETDEV_LINUX_CLASS(NAME, CREATE, GET_STATS, SET_STATS, \
- GET_FEATURES, GET_STATUS) \
- { \
-@@ -2396,6 +2439,7 @@ netdev_linux_change_seq(const struct netdev *netdev)
- netdev_linux_listen, \
- netdev_linux_recv, \
- netdev_linux_recv_wait, \
-+ THREADED_NULL \
- netdev_linux_drain, \
- \
- netdev_linux_send, \
-diff --git lib/netdev-provider.h lib/netdev-provider.h
-index 2a91f05..ee4e757 100644
---- lib/netdev-provider.h
-+++ lib/netdev-provider.h
-@@ -24,6 +24,9 @@
- #include "netdev.h"
- #include "list.h"
- #include "shash.h"
-+#ifdef THREADED
-+#include "dispatch.h"
-+#endif
-
- #ifdef __cplusplus
- extern "C" {
-@@ -190,6 +193,22 @@ struct netdev_class {
- * implement packet reception through the 'recv' member function. */
- void (*recv_wait)(struct netdev *netdev);
-
-+#ifdef THREADED
-+ /* Attempts to receive 'batch' packets from 'netdev' and process them
-+ * through the 'handler' callback. This function is used in the 'THREADED'
-+ * version in order to optimize the forwarding process, since it permits to
-+ * process packets directly in the netdev memory.
-+ *
-+ * Returns the number of packets processed on success; this can be 0 if no
-+ * packets are available to be read. Returns -1 if an error occurred.
-+ */
-+ int (*dispatch)(struct netdev *netdev, int batch, pkt_handler handler,
-+ u_char *user);
-+
-+ /* Return the file descriptor of the device */
-+ int (*get_fd)(struct netdev *netdev);
-+#endif
-+
- /* Discards all packets waiting to be received from 'netdev'.
- *
- * May be null if not needed, such as for a network device that does not
-diff --git lib/netdev-vport.c lib/netdev-vport.c
-index 1721f6b..39b26de 100644
---- lib/netdev-vport.c
-+++ lib/netdev-vport.c
-@@ -889,6 +889,13 @@ unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
- return 0;
- }
-
-+
-+#ifdef THREADED
-+# define THREADED_NULL NULL, NULL,
-+#else
-+# define THREADED_NULL
-+#endif
-+
- #define VPORT_FUNCTIONS(GET_STATUS) \
- NULL, \
- netdev_vport_run, \
-@@ -905,6 +912,7 @@ unparse_patch_config(const char *name OVS_UNUSED, const char *type OVS_UNUSED,
- NULL, /* listen */ \
- NULL, /* recv */ \
- NULL, /* recv_wait */ \
-+ THREADED_NULL \
- NULL, /* drain */ \
- \
- netdev_vport_send, /* send */ \
-diff --git lib/netdev.c lib/netdev.c
-index be7cdd2..0c54e1e 100644
---- lib/netdev.c
-+++ lib/netdev.c
-@@ -423,6 +423,28 @@ netdev_recv_wait(struct netdev *netdev)
- }
- }
-
-+#ifdef THREADED
-+/* Attempts to receive and process 'batch' packets from 'netdev'. */
-+int
-+netdev_dispatch(struct netdev *netdev, int batch, pkt_handler h, u_char *user)
-+{
-+ int (*dispatch)(struct netdev*, int, pkt_handler, u_char *);
-+
-+ dispatch = netdev_get_dev(netdev)->netdev_class->dispatch;
-+ return dispatch ? dispatch(netdev, batch, h, user) : 0;
-+}
-+
-+/* Returns the file descriptor */
-+int
-+netdev_get_fd(struct netdev *netdev)
-+{
-+ int (*get_fd)(struct netdev *);
-+
-+ get_fd = netdev_get_dev(netdev)->netdev_class->get_fd;
-+ return get_fd ? get_fd(netdev) : 0;
-+}
-+#endif
-+
- /* Discards all packets waiting to be received from 'netdev'. */
- int
- netdev_drain(struct netdev *netdev)
-diff --git lib/netdev.h lib/netdev.h
-index 4b86c21..4fad796 100644
---- lib/netdev.h
-+++ lib/netdev.h
-@@ -21,6 +21,9 @@
- #include <stddef.h>
- #include <stdint.h>
- #include "openvswitch/types.h"
-+#ifdef THREADED
-+#include "dispatch.h"
-+#endif
-
- #ifdef __cplusplus
- extern "C" {
-@@ -107,6 +110,10 @@ int netdev_get_ifindex(const struct netdev *);
- int netdev_listen(struct netdev *);
- int netdev_recv(struct netdev *, struct ofpbuf *);
- void netdev_recv_wait(struct netdev *);
-+#ifdef THREADED
-+int netdev_dispatch(struct netdev *, int, pkt_handler, u_char *);
-+int netdev_get_fd(struct netdev *);
-+#endif
- int netdev_drain(struct netdev *);
-
- int netdev_send(struct netdev *, const struct ofpbuf *);
-diff --git lib/vlog.c lib/vlog.c
-index 899072e..b6bd4ef 100644
---- lib/vlog.c
-+++ lib/vlog.c
-@@ -34,6 +34,9 @@
- #include "timeval.h"
- #include "unixctl.h"
- #include "util.h"
-+#ifdef THREADED
-+#include <pthread.h>
-+#endif
-
- VLOG_DEFINE_THIS_MODULE(vlog);
-
-@@ -89,6 +92,10 @@ static FILE *log_file;
- /* vlog initialized? */
- static bool vlog_inited;
-
-+#ifdef THREADED
-+static pthread_mutex_t vlog_mutex;
-+#endif
-+
- static void format_log_message(const struct vlog_module *, enum vlog_level,
- enum vlog_facility, unsigned int msg_num,
- const char *message, va_list, struct ds *)
-@@ -484,6 +491,9 @@ vlog_init(void)
- return;
- }
- vlog_inited = true;
-+#ifdef THREADED
-+ pthread_mutex_init(&vlog_mutex, NULL);
-+#endif
-
- /* openlog() is allowed to keep the pointer passed in, without making a
- * copy. The daemonize code sometimes frees and replaces 'program_name',
-@@ -691,6 +701,9 @@ vlog_valist(const struct vlog_module *module, enum vlog_level level,
-
- ds_init(&s);
- ds_reserve(&s, 1024);
-+#ifdef THREADED
-+ pthread_mutex_lock(&vlog_mutex);
-+#endif
- msg_num++;
-
- if (log_to_console) {
-@@ -721,6 +734,9 @@ vlog_valist(const struct vlog_module *module, enum vlog_level level,
- fflush(log_file);
- }
-
-+#ifdef THREADED
-+ pthread_mutex_unlock(&vlog_mutex);
-+#endif
- ds_destroy(&s);
- errno = save_errno;
- }
-diff --git m4/openvswitch.m4 m4/openvswitch.m4
-index dca9f5f..084ceff 100644
---- m4/openvswitch.m4
-+++ m4/openvswitch.m4
-@@ -14,6 +14,25 @@
- # See the License for the specific language governing permissions and
- # limitations under the License.
-
-+dnl Check for --enable-threaded and updates CFLAGS.
-+AC_DEFUN([OVS_CHECK_THREADED],
-+ [AC_REQUIRE([AC_PROG_CC])
-+ AC_ARG_ENABLE(
-+ [threaded],
-+ [AC_HELP_STRING([--enable-threaded],
-+ [Enable threaded version of userspace implementation])],
-+ [case "${enableval}" in
-+ (yes) threaded=true ;;
-+ (no) threaded=false ;;
-+ (*) AC_MSG_ERROR([bad value ${enableval} for --enable-threaded]) ;;
-+ esac],
-+ [threaded=false])
-+ if $threaded; then
-+ AC_DEFINE([THREADED], [1],
-+ [Define to 1 if the threaded version of userspace
-+ implementation is enabled.])
-+ fi])
-+
- dnl Checks for --enable-coverage and updates CFLAGS and LDFLAGS appropriately.
- AC_DEFUN([OVS_CHECK_COVERAGE],
- [AC_REQUIRE([AC_PROG_CC])
diff --git a/net/openvswitch/pkg-plist b/net/openvswitch/pkg-plist
index 6df8f024d620..90603acab59f 100644
--- a/net/openvswitch/pkg-plist
+++ b/net/openvswitch/pkg-plist
@@ -1,17 +1,31 @@
bin/ovs-appctl
-bin/ovs-controller
+bin/ovs-benchmark
bin/ovs-dpctl
+bin/ovs-dpctl-top
+bin/ovs-l3ping
bin/ovs-ofctl
-bin/ovs-vsctl
-bin/ovs-benchmark
-bin/ovsdb-tool
-bin/ovsdb-client
-bin/ovs-pki
-bin/ovs-parse-leaks
+bin/ovs-parse-backtrace
bin/ovs-pcap
+bin/ovs-pki
bin/ovs-tcpundump
bin/ovs-test
bin/ovs-vlan-test
+bin/ovs-vsctl
+bin/ovsdb-client
+bin/ovsdb-tool
+bin/vtep-ctl
+lib/libofproto-2.3.1.so
+lib/libofproto.a
+lib/libofproto.so
+lib/libopenvswitch-2.3.1.so
+lib/libopenvswitch.a
+lib/libopenvswitch.so
+lib/libovsdb-2.3.1.so
+lib/libovsdb.a
+lib/libovsdb.so
+lib/libsflow-2.3.1.so
+lib/libsflow.a
+lib/libsflow.so
man/man1/ovs-benchmark.1.gz
man/man1/ovs-pcap.1.gz
man/man1/ovs-tcpundump.1.gz
@@ -19,72 +33,87 @@ man/man1/ovsdb-client.1.gz
man/man1/ovsdb-server.1.gz
man/man1/ovsdb-tool.1.gz
man/man5/ovs-vswitchd.conf.db.5.gz
+man/man5/vtep.5.gz
man/man8/ovs-appctl.8.gz
-man/man8/ovs-brcompatd.8.gz
man/man8/ovs-bugtool.8.gz
-man/man8/ovs-controller.8.gz
man/man8/ovs-ctl.8.gz
+man/man8/ovs-dpctl-top.8.gz
man/man8/ovs-dpctl.8.gz
+man/man8/ovs-l3ping.8.gz
man/man8/ovs-ofctl.8.gz
-man/man8/ovs-parse-leaks.8.gz
+man/man8/ovs-parse-backtrace.8.gz
man/man8/ovs-pki.8.gz
man/man8/ovs-test.8.gz
man/man8/ovs-vlan-bug-workaround.8.gz
man/man8/ovs-vlan-test.8.gz
man/man8/ovs-vsctl.8.gz
man/man8/ovs-vswitchd.8.gz
+man/man8/vtep-ctl.8.gz
+sbin/ovs-bugtool
sbin/ovs-vswitchd
sbin/ovsdb-server
-sbin/ovs-bugtool
-share/openvswitch/bugtool-plugins/kernel-info/openvswitch.xml
-share/openvswitch/bugtool-plugins/network-status/openvswitch.xml
-share/openvswitch/bugtool-plugins/system-configuration.xml
-share/openvswitch/bugtool-plugins/system-logs/openvswitch.xml
-share/openvswitch/bugtool-plugins/system-configuration/openvswitch.xml
-share/openvswitch/python/ovs/dirs.py
-share/openvswitch/python/ovs/db/__init__.py
-share/openvswitch/python/ovs/db/data.py
-share/openvswitch/python/ovs/db/error.py
-share/openvswitch/python/ovs/db/idl.py
-share/openvswitch/python/ovs/db/parser.py
-share/openvswitch/python/ovs/db/schema.py
-share/openvswitch/python/ovs/db/types.py
-share/openvswitch/python/ovs/__init__.py
-share/openvswitch/python/ovs/daemon.py
-share/openvswitch/python/ovs/fatal_signal.py
-share/openvswitch/python/ovs/json.py
-share/openvswitch/python/ovs/jsonrpc.py
-share/openvswitch/python/ovs/ovsuuid.py
-share/openvswitch/python/ovs/poller.py
-share/openvswitch/python/ovs/process.py
-share/openvswitch/python/ovs/reconnect.py
-share/openvswitch/python/ovs/socket_util.py
-share/openvswitch/python/ovs/stream.py
-share/openvswitch/python/ovs/timeval.py
-share/openvswitch/python/ovs/unixctl.py
-share/openvswitch/python/ovs/util.py
-share/openvswitch/python/ovs/version.py
-share/openvswitch/python/ovs/vlog.py
-share/openvswitch/python/ovstest/__init__.py
-share/openvswitch/python/ovstest/args.py
-share/openvswitch/python/ovstest/rpcserver.py
-share/openvswitch/python/ovstest/tcp.py
-share/openvswitch/python/ovstest/udp.py
-share/openvswitch/python/ovstest/util.py
-share/openvswitch/python/ovstest/vswitch.py
-share/openvswitch/vswitch.ovsschema
-share/openvswitch/scripts/ovs-lib
-share/openvswitch/scripts/ovs-ctl
-share/openvswitch/scripts/ovs-save
-share/openvswitch/scripts/ovs-bugtool-cfm-show
-share/openvswitch/scripts/ovs-bugtool-lacp-show
-share/openvswitch/scripts/ovs-bugtool-tc-class-show
-share/openvswitch/scripts/ovs-bugtool-ovsdb-dump
-share/openvswitch/scripts/ovs-check-dead-ifs
-share/openvswitch/scripts/ovs-bugtool-coverage-show
-share/openvswitch/scripts/ovs-bugtool-vsctl-show
-share/openvswitch/scripts/ovs-bugtool-daemons-ver
-share/openvswitch/scripts/ovs-bugtool-bond-show
-@dir var/lib/openvswitch/pki
-@dir var/log/openvswitch
-@dir var/run/openvswitch
+%%DATADIR%%/bugtool-plugins/kernel-info/openvswitch.xml
+%%DATADIR%%/bugtool-plugins/network-status/openvswitch.xml
+%%DATADIR%%/bugtool-plugins/system-configuration.xml
+%%DATADIR%%/bugtool-plugins/system-configuration/openvswitch.xml
+%%DATADIR%%/bugtool-plugins/system-logs/openvswitch.xml
+%%DATADIR%%/python/ovs/__init__.py
+%%DATADIR%%/python/ovs/daemon.py
+%%DATADIR%%/python/ovs/db/__init__.py
+%%DATADIR%%/python/ovs/db/data.py
+%%DATADIR%%/python/ovs/db/error.py
+%%DATADIR%%/python/ovs/db/idl.py
+%%DATADIR%%/python/ovs/db/parser.py
+%%DATADIR%%/python/ovs/db/schema.py
+%%DATADIR%%/python/ovs/db/types.py
+%%DATADIR%%/python/ovs/dirs.py
+%%DATADIR%%/python/ovs/fatal_signal.py
+%%DATADIR%%/python/ovs/json.py
+%%DATADIR%%/python/ovs/jsonrpc.py
+%%DATADIR%%/python/ovs/ovsuuid.py
+%%DATADIR%%/python/ovs/poller.py
+%%DATADIR%%/python/ovs/process.py
+%%DATADIR%%/python/ovs/reconnect.py
+%%DATADIR%%/python/ovs/socket_util.py
+%%DATADIR%%/python/ovs/stream.py
+%%DATADIR%%/python/ovs/timeval.py
+%%DATADIR%%/python/ovs/unixctl/__init__.py
+%%DATADIR%%/python/ovs/unixctl/client.py
+%%DATADIR%%/python/ovs/unixctl/server.py
+%%DATADIR%%/python/ovs/util.py
+%%DATADIR%%/python/ovs/version.py
+%%DATADIR%%/python/ovs/vlog.py
+%%DATADIR%%/python/ovstest/__init__.py
+%%DATADIR%%/python/ovstest/args.py
+%%DATADIR%%/python/ovstest/rpcserver.py
+%%DATADIR%%/python/ovstest/tcp.py
+%%DATADIR%%/python/ovstest/tests.py
+%%DATADIR%%/python/ovstest/udp.py
+%%DATADIR%%/python/ovstest/util.py
+%%DATADIR%%/python/ovstest/vswitch.py
+%%DATADIR%%/scripts/ovs-bugtool-bfd-show
+%%DATADIR%%/scripts/ovs-bugtool-bond-show
+%%DATADIR%%/scripts/ovs-bugtool-cfm-show
+%%DATADIR%%/scripts/ovs-bugtool-coverage-show
+%%DATADIR%%/scripts/ovs-bugtool-daemons-ver
+%%DATADIR%%/scripts/ovs-bugtool-lacp-show
+%%DATADIR%%/scripts/ovs-bugtool-list-dbs
+%%DATADIR%%/scripts/ovs-bugtool-memory-show
+%%DATADIR%%/scripts/ovs-bugtool-ovs-appctl-dpif
+%%DATADIR%%/scripts/ovs-bugtool-ovs-ofctl-dump-flows
+%%DATADIR%%/scripts/ovs-bugtool-ovs-ofctl-show
+%%DATADIR%%/scripts/ovs-bugtool-ovsdb-dump
+%%DATADIR%%/scripts/ovs-bugtool-tc-class-show
+%%DATADIR%%/scripts/ovs-bugtool-vsctl-show
+%%DATADIR%%/scripts/ovs-check-dead-ifs
+%%DATADIR%%/scripts/ovs-ctl
+%%DATADIR%%/scripts/ovs-lib
+%%DATADIR%%/scripts/ovs-save
+%%DATADIR%%/scripts/ovs-vtep
+%%DATADIR%%/vswitch.ovsschema
+%%DATADIR%%/vtep.ovsschema
+@dir /var/lib/openvswitch/pki
+@dir /var/lib/openvswitch
+@dir /var/lib
+@dir /var/log/openvswitch
+@dir /var/run/openvswitch