netdev: Add 'change_seq' back to netdev.
[sliver-openvswitch.git] / lib / netdev-bsd.c
index 0f625af..ef1e649 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Copyright (c) 2011 Gaetano Catalli.
- * Copyright (c) 2013 YAMAMOTO Takashi.
+ * Copyright (c) 2011, 2013 Gaetano Catalli.
+ * Copyright (c) 2013, 2014 YAMAMOTO Takashi.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #include <sys/sysctl.h>
 #if defined(__NetBSD__)
 #include <net/route.h>
+#include <netinet/in.h>
+#include <netinet/if_inarp.h>
 #endif
 
 #include "rtbsd.h"
 #include "coverage.h"
+#include "dpif-netdev.h"
 #include "dynamic-string.h"
 #include "fatal-signal.h"
 #include "ofpbuf.h"
@@ -53,8 +56,8 @@
 #include "ovs-thread.h"
 #include "packets.h"
 #include "poll-loop.h"
-#include "socket-util.h"
 #include "shash.h"
+#include "socket-util.h"
 #include "svec.h"
 #include "util.h"
 #include "vlog.h"
@@ -62,8 +65,8 @@
 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
 
 \f
-struct netdev_rx_bsd {
-    struct netdev_rx up;
+struct netdev_rxq_bsd {
+    struct netdev_rxq up;
 
     /* Packet capture descriptor for a system network device.
      * For a tap device this is NULL. */
@@ -76,8 +79,14 @@ struct netdev_rx_bsd {
 
 struct netdev_bsd {
     struct netdev up;
+
+    /* Never changes after initialization. */
+    char *kernel_name;
+
+    /* Protects all members below. */
+    struct ovs_mutex mutex;
+
     unsigned int cache_valid;
-    unsigned int change_seq;
 
     int ifindex;
     uint8_t etheraddr[ETH_ADDR_LEN];
@@ -92,8 +101,6 @@ struct netdev_bsd {
     /* Used for sending packets on non-tap devices. */
     pcap_t *pcap;
     int fd;
-
-    char *kernel_name;
 };
 
 
@@ -128,7 +135,7 @@ static void destroy_tap(int fd, const char *name);
 static int get_flags(const struct netdev *, int *flagsp);
 static int set_flags(const char *, int flags);
 static int do_set_addr(struct netdev *netdev,
-                       int ioctl_nr, const char *ioctl_name,
+                       unsigned long 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,
@@ -139,10 +146,11 @@ static int ifr_get_flags(const struct ifreq *);
 static void ifr_set_flags(struct ifreq *, int flags);
 
 #ifdef __NetBSD__
-static int af_link_ioctl(int command, const void *arg);
+static int af_link_ioctl(unsigned long command, const void *arg);
 #endif
 
 static void netdev_bsd_run(void);
+static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);
 
 static bool
 is_netdev_bsd_class(const struct netdev_class *netdev_class)
@@ -157,11 +165,11 @@ netdev_bsd_cast(const struct netdev *netdev)
     return CONTAINER_OF(netdev, struct netdev_bsd, up);
 }
 
-static struct netdev_rx_bsd *
-netdev_rx_bsd_cast(const struct netdev_rx *rx)
+static struct netdev_rxq_bsd *
+netdev_rxq_bsd_cast(const struct netdev_rxq *rxq)
 {
-    ovs_assert(is_netdev_bsd_class(netdev_get_class(rx->netdev)));
-    return CONTAINER_OF(rx, struct netdev_rx_bsd, up);
+    ovs_assert(is_netdev_bsd_class(netdev_get_class(rxq->netdev)));
+    return CONTAINER_OF(rxq, struct netdev_rxq_bsd, up);
 }
 
 static const char *
@@ -190,15 +198,6 @@ netdev_bsd_wait(void)
     rtbsd_notifier_wait();
 }
 
-static void
-netdev_bsd_changed(struct netdev_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,
@@ -216,7 +215,7 @@ netdev_bsd_cache_cb(const struct rtbsd_change *change,
             if (is_netdev_bsd_class(netdev_class)) {
                 dev = netdev_bsd_cast(base_dev);
                 dev->cache_valid = 0;
-                netdev_bsd_changed(dev);
+                netdev_change_seq_changed(base_dev);
             }
             netdev_close(base_dev);
         }
@@ -234,7 +233,7 @@ netdev_bsd_cache_cb(const struct rtbsd_change *change,
             struct netdev *netdev = node->data;
             dev = netdev_bsd_cast(netdev);
             dev->cache_valid = 0;
-            netdev_bsd_changed(dev);
+            netdev_change_seq_changed(netdev);
             netdev_close(netdev);
         }
         shash_destroy(&device_shash);
@@ -286,7 +285,7 @@ netdev_bsd_construct_system(struct netdev *netdev_)
         return error;
     }
 
-    netdev->change_seq = 1;
+    ovs_mutex_init(&netdev->mutex);
     netdev->tap_fd = -1;
     netdev->kernel_name = xstrdup(netdev_->name);
 
@@ -319,8 +318,8 @@ netdev_bsd_construct_tap(struct netdev *netdev_)
 
     /* Create a tap device by opening /dev/tap.  The TAPGIFNAME ioctl is used
      * to retrieve the name of the tap device. */
+    ovs_mutex_init(&netdev->mutex);
     netdev->tap_fd = open("/dev/tap", O_RDWR);
-    netdev->change_seq = 1;
     if (netdev->tap_fd < 0) {
         error = errno;
         VLOG_WARN("opening \"/dev/tap\" failed: %s", ovs_strerror(error));
@@ -373,6 +372,7 @@ netdev_bsd_construct_tap(struct netdev *netdev_)
     return 0;
 
 error_unref_notifier:
+    ovs_mutex_destroy(&netdev->mutex);
     cache_notifier_unref();
 error:
     free(kernel_name);
@@ -393,6 +393,7 @@ netdev_bsd_destruct(struct netdev *netdev_)
         pcap_close(netdev->pcap);
     }
     free(netdev->kernel_name);
+    ovs_mutex_destroy(&netdev->mutex);
 }
 
 static void
@@ -472,52 +473,51 @@ error:
     return error;
 }
 
-static struct netdev_rx *
-netdev_bsd_rx_alloc(void)
+static struct netdev_rxq *
+netdev_bsd_rxq_alloc(void)
 {
-    struct netdev_rx_bsd *rx = xzalloc(sizeof *rx);
-    return &rx->up;
+    struct netdev_rxq_bsd *rxq = xzalloc(sizeof *rxq);
+    return &rxq->up;
 }
 
 static int
-netdev_bsd_rx_construct(struct netdev_rx *rx_)
+netdev_bsd_rxq_construct(struct netdev_rxq *rxq_)
 {
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
-    struct netdev *netdev_ = rx->up.netdev;
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
+    struct netdev *netdev_ = rxq->up.netdev;
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    int error;
 
     if (!strcmp(netdev_get_type(netdev_), "tap")) {
-        rx->pcap_handle = NULL;
-        rx->fd = netdev->tap_fd;
+        rxq->pcap_handle = NULL;
+        rxq->fd = netdev->tap_fd;
+        error = 0;
     } else {
-        int error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
-                                         &rx->pcap_handle, &rx->fd);
-        if (error) {
-            return error;
-        }
-
-        netdev_bsd_changed(netdev);
+        ovs_mutex_lock(&netdev->mutex);
+        error = netdev_bsd_open_pcap(netdev_get_kernel_name(netdev_),
+                                     &rxq->pcap_handle, &rxq->fd);
+        ovs_mutex_unlock(&netdev->mutex);
     }
 
-    return 0;
+    return error;
 }
 
 static void
-netdev_bsd_rx_destruct(struct netdev_rx *rx_)
+netdev_bsd_rxq_destruct(struct netdev_rxq *rxq_)
 {
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
 
-    if (rx->pcap_handle) {
-        pcap_close(rx->pcap_handle);
+    if (rxq->pcap_handle) {
+        pcap_close(rxq->pcap_handle);
     }
 }
 
 static void
-netdev_bsd_rx_dealloc(struct netdev_rx *rx_)
+netdev_bsd_rxq_dealloc(struct netdev_rxq *rxq_)
 {
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
 
-    free(rx);
+    free(rxq);
 }
 
 /* The recv callback of the netdev class returns the number of bytes of the
@@ -565,23 +565,24 @@ proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
  * 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
- * from rx->pcap.
+ * from rxq->pcap.
  */
 static int
-netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
+netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
 {
     struct pcap_arg arg;
     int ret;
 
     /* prepare the pcap argument to store the packet */
-    arg.size = size;
-    arg.data = data;
+    arg.size = ofpbuf_tailroom(buffer);
+    arg.data = ofpbuf_data(buffer);
 
     for (;;) {
-        ret = pcap_dispatch(rx->pcap_handle, 1, proc_pkt, (u_char *) &arg);
+        ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);
 
         if (ret > 0) {
-            return arg.retval; /* arg.retval < 0 is handled in the caller */
+            ofpbuf_set_size(buffer, ofpbuf_size(buffer) + arg.retval);
+            return 0;
         }
         if (ret == -1) {
             if (errno == EINTR) {
@@ -589,65 +590,87 @@ netdev_rx_bsd_recv_pcap(struct netdev_rx_bsd *rx, void *data, size_t size)
             }
         }
 
-        return -EAGAIN;
+        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
- * 'rx->fd' is initialized with the tap file descriptor.
+ * 'rxq->fd' is initialized with the tap file descriptor.
  */
 static int
-netdev_rx_bsd_recv_tap(struct netdev_rx_bsd *rx, void *data, size_t size)
+netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
 {
+    size_t size = ofpbuf_tailroom(buffer);
+
     for (;;) {
-        ssize_t retval = read(rx->fd, data, size);
+        ssize_t retval = read(rxq->fd, ofpbuf_data(buffer), size);
         if (retval >= 0) {
-            return retval;
+            ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
+            return 0;
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
                 VLOG_WARN_RL(&rl, "error receiving Ethernet packet on %s: %s",
-                             ovs_strerror(errno), netdev_rx_get_name(&rx->up));
+                             ovs_strerror(errno), netdev_rxq_get_name(&rxq->up));
             }
-            return -errno;
+            return errno;
         }
     }
 }
 
 static int
-netdev_bsd_rx_recv(struct netdev_rx *rx_, void *data, size_t size)
+netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct ofpbuf **packet, int *c)
 {
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
+    struct netdev *netdev = rxq->up.netdev;
+    struct ofpbuf *buffer;
+    ssize_t retval;
+    int mtu;
+
+    if (netdev_bsd_get_mtu(netdev, &mtu)) {
+        mtu = ETH_PAYLOAD_MAX;
+    }
 
-    return (rx->pcap_handle
-            ? netdev_rx_bsd_recv_pcap(rx, data, size)
-            : netdev_rx_bsd_recv_tap(rx, data, size));
+    buffer = ofpbuf_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu, DP_NETDEV_HEADROOM);
+
+    retval = (rxq->pcap_handle
+            ? netdev_rxq_bsd_recv_pcap(rxq, buffer)
+            : netdev_rxq_bsd_recv_tap(rxq, buffer));
+
+    if (retval) {
+        ofpbuf_delete(buffer);
+    } else {
+        dp_packet_pad(buffer);
+        packet[0] = buffer;
+        *c = 1;
+    }
+    return retval;
 }
 
 /*
  * 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_rx_recv() on 'rx'.
+ * when a packet is ready to be received with netdev_rxq_recv() on 'rxq'.
  */
 static void
-netdev_bsd_rx_wait(struct netdev_rx *rx_)
+netdev_bsd_rxq_wait(struct netdev_rxq *rxq_)
 {
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
 
-    poll_fd_wait(rx->fd, POLLIN);
+    poll_fd_wait(rxq->fd, POLLIN);
 }
 
-/* Discards all packets waiting to be received from 'rx'. */
+/* Discards all packets waiting to be received from 'rxq'. */
 static int
-netdev_bsd_rx_drain(struct netdev_rx *rx_)
+netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
 {
     struct ifreq ifr;
-    struct netdev_rx_bsd *rx = netdev_rx_bsd_cast(rx_);
+    struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
 
-    strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rx_get_netdev(rx_)));
-    if (ioctl(rx->fd, BIOCFLUSH, &ifr) == -1) {
+    strcpy(ifr.ifr_name, netdev_get_kernel_name(netdev_rxq_get_netdev(rxq_)));
+    if (ioctl(rxq->fd, BIOCFLUSH, &ifr) == -1) {
         VLOG_DBG_RL(&rl, "%s: ioctl(BIOCFLUSH) failed: %s",
-                    netdev_rx_get_name(rx_), ovs_strerror(errno));
+                    netdev_rxq_get_name(rxq_), ovs_strerror(errno));
         return errno;
     }
     return 0;
@@ -658,19 +681,22 @@ netdev_bsd_rx_drain(struct netdev_rx *rx_)
  * system or a tap device.
  */
 static int
-netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
+netdev_bsd_send(struct netdev *netdev_, struct ofpbuf *pkt, bool may_steal)
 {
     struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
     const char *name = netdev_get_name(netdev_);
+    const void *data = ofpbuf_data(pkt);
+    size_t size = ofpbuf_size(pkt);
+    int error;
 
+    ovs_mutex_lock(&dev->mutex);
     if (dev->tap_fd < 0 && !dev->pcap) {
-        int error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
-        if (error) {
-            return error;
-        }
+        error = netdev_bsd_open_pcap(name, &dev->pcap, &dev->fd);
+    } else {
+        error = 0;
     }
 
-    for (;;) {
+    while (!error) {
         ssize_t retval;
         if (dev->tap_fd >= 0) {
             retval = write(dev->tap_fd, data, size);
@@ -680,19 +706,28 @@ netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
         if (retval < 0) {
             if (errno == EINTR) {
                 continue;
-            } else if (errno != EAGAIN) {
-                VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: %s",
-                             name, ovs_strerror(errno));
+            } else {
+                error = errno;
+                if (error != EAGAIN) {
+                    VLOG_WARN_RL(&rl, "error sending Ethernet packet on %s: "
+                                 "%s", name, ovs_strerror(error));
+                }
             }
-            return errno;
         } else if (retval != size) {
-            VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
-                         "%zu) on %s", retval, size, name);
-           return EMSGSIZE;
+            VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
+                         "%"PRIuSIZE") on %s", retval, size, name);
+            error = EMSGSIZE;
         } else {
-            return 0;
+            break;
         }
     }
+
+    ovs_mutex_unlock(&dev->mutex);
+    if (may_steal) {
+        ofpbuf_delete(pkt);
+    }
+
+    return error;
 }
 
 /*
@@ -705,6 +740,7 @@ netdev_bsd_send_wait(struct netdev *netdev_)
 {
     struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
 
+    ovs_mutex_lock(&dev->mutex);
     if (dev->tap_fd >= 0) {
         /* TAP device always accepts packets. */
         poll_immediate_wake();
@@ -714,6 +750,7 @@ netdev_bsd_send_wait(struct netdev *netdev_)
         /* We haven't even tried to send a packet yet. */
         poll_immediate_wake();
     }
+    ovs_mutex_unlock(&dev->mutex);
 }
 
 /*
@@ -725,8 +762,9 @@ netdev_bsd_set_etheraddr(struct netdev *netdev_,
                          const uint8_t mac[ETH_ADDR_LEN])
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-    int error;
+    int error = 0;
 
+    ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_ETHERADDR)
         || !eth_addr_equals(netdev->etheraddr, mac)) {
         error = set_etheraddr(netdev_get_kernel_name(netdev_), AF_LINK,
@@ -734,11 +772,11 @@ netdev_bsd_set_etheraddr(struct netdev *netdev_,
         if (!error) {
             netdev->cache_valid |= VALID_ETHERADDR;
             memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
-            netdev_bsd_changed(netdev);
+            netdev_change_seq_changed(netdev_);
         }
-    } else {
-        error = 0;
     }
+    ovs_mutex_unlock(&netdev->mutex);
+
     return error;
 }
 
@@ -751,18 +789,22 @@ netdev_bsd_get_etheraddr(const struct netdev *netdev_,
                          uint8_t mac[ETH_ADDR_LEN])
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    int error = 0;
 
+    ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_ETHERADDR)) {
-        int error = get_etheraddr(netdev_get_kernel_name(netdev_),
-                                  netdev->etheraddr);
-        if (error) {
-            return error;
+        error = get_etheraddr(netdev_get_kernel_name(netdev_),
+                              netdev->etheraddr);
+        if (!error) {
+            netdev->cache_valid |= VALID_ETHERADDR;
         }
-        netdev->cache_valid |= VALID_ETHERADDR;
     }
-    memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
+    if (!error) {
+        memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
+    }
+    ovs_mutex_unlock(&netdev->mutex);
 
-    return 0;
+    return error;
 }
 
 /*
@@ -774,30 +816,37 @@ static int
 netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    int error = 0;
 
+    ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_MTU)) {
         struct ifreq ifr;
-        int error;
 
         error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
                                     SIOCGIFMTU, "SIOCGIFMTU");
-        if (error) {
-            return error;
+        if (!error) {
+            netdev->mtu = ifr.ifr_mtu;
+            netdev->cache_valid |= VALID_MTU;
         }
-        netdev->mtu = ifr.ifr_mtu;
-        netdev->cache_valid |= VALID_MTU;
     }
+    if (!error) {
+        *mtup = netdev->mtu;
+    }
+    ovs_mutex_unlock(&netdev->mutex);
 
-    *mtup = netdev->mtu;
     return 0;
 }
 
 static int
-netdev_bsd_get_ifindex(const struct netdev *netdev)
+netdev_bsd_get_ifindex(const struct netdev *netdev_)
 {
+    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
     int ifindex, error;
 
-    error = get_ifindex(netdev, &ifindex);
+    ovs_mutex_lock(&netdev->mutex);
+    error = get_ifindex(netdev_, &ifindex);
+    ovs_mutex_unlock(&netdev->mutex);
+
     return error ? -error : ifindex;
 }
 
@@ -805,38 +854,41 @@ static int
 netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    int error = 0;
 
+    ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_CARRIER)) {
         struct ifmediareq ifmr;
-        int error;
 
         memset(&ifmr, 0, sizeof(ifmr));
         strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
                 sizeof ifmr.ifm_name);
 
         error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
-        if (error) {
+        if (!error) {
+            netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
+            netdev->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->carrier = true;
+            }
+        } else {
             VLOG_DBG_RL(&rl, "%s: ioctl(SIOCGIFMEDIA) failed: %s",
                         netdev_get_name(netdev_), ovs_strerror(error));
-            return error;
-        }
-
-        netdev->carrier = (ifmr.ifm_status & IFM_ACTIVE) == IFM_ACTIVE;
-        netdev->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->carrier = true;
         }
     }
-    *carrier = netdev->carrier;
+    if (!error) {
+        *carrier = netdev->carrier;
+    }
+    ovs_mutex_unlock(&netdev->mutex);
 
-    return 0;
+    return error;
 }
 
 static void
-convert_stats(struct netdev_stats *stats, const struct if_data *ifd)
+convert_stats_system(struct netdev_stats *stats, const struct if_data *ifd)
 {
     /*
      * note: UINT64_MAX means unsupported
@@ -864,6 +916,51 @@ convert_stats(struct netdev_stats *stats, const struct if_data *ifd)
     stats->tx_window_errors = UINT64_MAX;
 }
 
+static void
+convert_stats_tap(struct netdev_stats *stats, const struct if_data *ifd)
+{
+    /*
+     * Similar to convert_stats_system but swapping rxq and tx
+     * because 'ifd' is stats for the network interface side of the
+     * tap device and what the caller wants is one for the character
+     * device side.
+     *
+     * note: UINT64_MAX means unsupported
+     */
+    stats->rx_packets = ifd->ifi_opackets;
+    stats->tx_packets = ifd->ifi_ipackets;
+    stats->rx_bytes = ifd->ifi_ibytes;
+    stats->tx_bytes = ifd->ifi_obytes;
+    stats->rx_errors = ifd->ifi_oerrors;
+    stats->tx_errors = ifd->ifi_ierrors;
+    stats->rx_dropped = UINT64_MAX;
+    stats->tx_dropped = ifd->ifi_iqdrops;
+    stats->multicast = ifd->ifi_omcasts;
+    stats->collisions = UINT64_MAX;
+    stats->rx_length_errors = UINT64_MAX;
+    stats->rx_over_errors = UINT64_MAX;
+    stats->rx_crc_errors = UINT64_MAX;
+    stats->rx_frame_errors = UINT64_MAX;
+    stats->rx_fifo_errors = UINT64_MAX;
+    stats->rx_missed_errors = UINT64_MAX;
+    stats->tx_aborted_errors = UINT64_MAX;
+    stats->tx_carrier_errors = UINT64_MAX;
+    stats->tx_fifo_errors = UINT64_MAX;
+    stats->tx_heartbeat_errors = UINT64_MAX;
+    stats->tx_window_errors = UINT64_MAX;
+}
+
+static void
+convert_stats(const struct netdev *netdev, struct netdev_stats *stats,
+              const struct if_data *ifd)
+{
+    if (netdev_bsd_cast(netdev)->tap_fd == -1) {
+        convert_stats_system(stats, ifd);
+    } else {
+        convert_stats_tap(stats, ifd);
+    }
+}
+
 /* Retrieves current device stats for 'netdev'. */
 static int
 netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
@@ -899,7 +996,7 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
                         netdev_get_name(netdev_), ovs_strerror(errno));
             return errno;
         } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
-            convert_stats(stats, &ifmd.ifmd_data);
+            convert_stats(netdev, stats, &ifdr.ifdr_data);
             break;
         }
     }
@@ -914,7 +1011,7 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
             sizeof(ifdr.ifdr_name));
     error = af_link_ioctl(SIOCGIFDATA, &ifdr);
     if (!error) {
-        convert_stats(stats, &ifdr.ifdr_data);
+        convert_stats(netdev_, stats, &ifdr.ifdr_data);
     }
     return error;
 #else
@@ -1074,33 +1171,35 @@ netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
                    struct in_addr *netmask)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    int error = 0;
 
+    ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_IN4)) {
-        const struct sockaddr_in *sin;
         struct ifreq ifr;
-        int error;
 
         ifr.ifr_addr.sa_family = AF_INET;
         error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
                                     SIOCGIFADDR, "SIOCGIFADDR");
-        if (error) {
-            return error;
-        }
+        if (!error) {
+            const struct sockaddr_in *sin;
 
-        sin = (struct sockaddr_in *) &ifr.ifr_addr;
-        netdev->in4 = sin->sin_addr;
-        error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
-                                    SIOCGIFNETMASK, "SIOCGIFNETMASK");
-        if (error) {
-            return error;
+            sin = (struct sockaddr_in *) &ifr.ifr_addr;
+            netdev->in4 = sin->sin_addr;
+            netdev->cache_valid |= VALID_IN4;
+            error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
+                                        SIOCGIFNETMASK, "SIOCGIFNETMASK");
+            if (!error) {
+                *netmask = sin->sin_addr;
+            }
         }
-        netdev->netmask = sin->sin_addr;
-        netdev->cache_valid |= VALID_IN4;
     }
-    *in4 = netdev->in4;
-    *netmask = netdev->netmask;
+    if (!error) {
+        *in4 = netdev->in4;
+        *netmask = netdev->netmask;
+    }
+    ovs_mutex_unlock(&netdev->mutex);
 
-    return in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
+    return error ? error : in4->s_addr == INADDR_ANY ? EADDRNOTAVAIL : 0;
 }
 
 /*
@@ -1115,6 +1214,7 @@ netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
     int error;
 
+    ovs_mutex_lock(&netdev->mutex);
     error = do_set_addr(netdev_, SIOCSIFADDR, "SIOCSIFADDR", addr);
     if (!error) {
         if (addr.s_addr != INADDR_ANY) {
@@ -1126,8 +1226,10 @@ netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
                 netdev->netmask = mask;
             }
         }
-        netdev_bsd_changed(netdev);
+        netdev_change_seq_changed(netdev_);
     }
+    ovs_mutex_unlock(&netdev->mutex);
+
     return error;
 }
 
@@ -1293,6 +1395,63 @@ netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
 #endif
 }
 
+static int
+netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
+                      ovs_be32 ip OVS_UNUSED,
+                     uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
+{
+#if defined(__NetBSD__)
+    const struct rt_msghdr *rtm;
+    size_t needed;
+    char *buf;
+    const char *cp;
+    const char *ep;
+    int mib[6];
+    int error;
+
+    buf = NULL;
+    mib[0] = CTL_NET;
+    mib[1] = PF_ROUTE;
+    mib[2] = 0;
+    mib[3] = AF_INET;
+    mib[4] = NET_RT_FLAGS;
+    mib[5] = RTF_LLINFO;
+    if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
+        error = errno;
+        goto error;
+    }
+    buf = xmalloc(needed);
+    if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1) {
+        error = errno;
+        goto error;
+    }
+    ep = buf + needed;
+    for (cp = buf; cp < ep; cp += rtm->rtm_msglen) {
+        const struct sockaddr_inarp *sina;
+        const struct sockaddr_dl *sdl;
+
+        rtm = (const void *)cp;
+        sina = (const void *)(rtm + 1);
+        if (ip != sina->sin_addr.s_addr) {
+            continue;
+        }
+        sdl = (const void *)
+           ((const char *)(const void *)sina + RT_ROUNDUP(sina->sin_len));
+        if (sdl->sdl_alen == ETH_ADDR_LEN) {
+            memcpy(mac, &sdl->sdl_data[sdl->sdl_nlen], ETH_ADDR_LEN);
+            error = 0;
+            goto error;
+        }
+    }
+    error = ENXIO;
+error:
+    free(buf);
+    return error;
+#else
+    return EOPNOTSUPP;
+#endif
+}
+
 static void
 make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
 {
@@ -1308,7 +1467,8 @@ make_in4_sockaddr(struct sockaddr *sa, struct in_addr addr)
 
 static int
 do_set_addr(struct netdev *netdev,
-            int ioctl_nr, const char *ioctl_name, struct in_addr addr)
+            unsigned long ioctl_nr, const char *ioctl_name,
+            struct in_addr addr)
 {
     struct ifreq ifr;
     make_in4_sockaddr(&ifr.ifr_addr, addr);
@@ -1329,6 +1489,9 @@ nd_to_iff_flags(enum netdev_flags nd)
         iff |= IFF_PPROMISC;
 #endif
     }
+    if (nd & NETDEV_LOOPBACK) {
+        iff |= IFF_LOOPBACK;
+    }
     return iff;
 }
 
@@ -1342,6 +1505,9 @@ iff_to_nd_flags(int iff)
     if (iff & IFF_PROMISC) {
         nd |= NETDEV_PROMISC;
     }
+    if (iff & IFF_LOOPBACK) {
+        nd |= NETDEV_LOOPBACK;
+    }
     return nd;
 }
 
@@ -1349,7 +1515,6 @@ static int
 netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
                         enum netdev_flags on, enum netdev_flags *old_flagsp)
 {
-    struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
     int old_flags, new_flags;
     int error;
 
@@ -1359,144 +1524,91 @@ netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
         new_flags = (old_flags & ~nd_to_iff_flags(off)) | nd_to_iff_flags(on);
         if (new_flags != old_flags) {
             error = set_flags(netdev_get_kernel_name(netdev_), new_flags);
-            netdev_bsd_changed(netdev);
+            netdev_change_seq_changed(netdev_);
         }
     }
     return error;
 }
 
-static unsigned int
-netdev_bsd_change_seq(const struct netdev *netdev)
-{
-    return netdev_bsd_cast(netdev)->change_seq;
+/* Linux has also different GET_STATS, SET_STATS,
+ * GET_STATUS)
+ */
+#define NETDEV_BSD_CLASS(NAME, CONSTRUCT,            \
+                         GET_FEATURES)               \
+{                                                    \
+    NAME,                                            \
+                                                     \
+    NULL, /* init */                                 \
+    netdev_bsd_run,                                  \
+    netdev_bsd_wait,                                 \
+    netdev_bsd_alloc,                                \
+    CONSTRUCT,                                       \
+    netdev_bsd_destruct,                             \
+    netdev_bsd_dealloc,                              \
+    NULL, /* get_config */                           \
+    NULL, /* set_config */                           \
+    NULL, /* get_tunnel_config */                    \
+                                                     \
+    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 */                            \
+                                                     \
+    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, /* queue_dump_start */                     \
+    NULL, /* queue_dump_next */                      \
+    NULL, /* queue_dump_done */                      \
+    NULL, /* dump_queue_stats */                     \
+                                                     \
+    netdev_bsd_get_in4,                              \
+    netdev_bsd_set_in4,                              \
+    netdev_bsd_get_in6,                              \
+    NULL, /* add_router */                           \
+    netdev_bsd_get_next_hop,                         \
+    NULL, /* get_status */                           \
+    netdev_bsd_arp_lookup, /* arp_lookup */          \
+                                                     \
+    netdev_bsd_update_flags,                         \
+                                                     \
+    netdev_bsd_rxq_alloc,                            \
+    netdev_bsd_rxq_construct,                        \
+    netdev_bsd_rxq_destruct,                         \
+    netdev_bsd_rxq_dealloc,                          \
+    netdev_bsd_rxq_recv,                             \
+    netdev_bsd_rxq_wait,                             \
+    netdev_bsd_rxq_drain,                            \
 }
 
-
-const struct netdev_class netdev_bsd_class = {
-    "system",
-
-    NULL, /* init */
-    netdev_bsd_run,
-    netdev_bsd_wait,
-    netdev_bsd_alloc,
-    netdev_bsd_construct_system,
-    netdev_bsd_destruct,
-    netdev_bsd_dealloc,
-    NULL, /* get_config */
-    NULL, /* set_config */
-    NULL, /* get_tunnel_config */
-
-    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 */
-    netdev_bsd_get_next_hop,
-    NULL, /* get_status */
-    NULL, /* arp_lookup */
-
-    netdev_bsd_update_flags,
-
-    netdev_bsd_change_seq,
-
-    netdev_bsd_rx_alloc,
-    netdev_bsd_rx_construct,
-    netdev_bsd_rx_destruct,
-    netdev_bsd_rx_dealloc,
-    netdev_bsd_rx_recv,
-    netdev_bsd_rx_wait,
-    netdev_bsd_rx_drain,
-};
-
-const struct netdev_class netdev_tap_class = {
-    "tap",
-
-    NULL, /* init */
-    netdev_bsd_run,
-    netdev_bsd_wait,
-    netdev_bsd_alloc,
-    netdev_bsd_construct_tap,
-    netdev_bsd_destruct,
-    netdev_bsd_dealloc,
-    NULL, /* get_config */
-    NULL, /* set_config */
-    NULL, /* get_tunnel_config */
-
-    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 */
-    netdev_bsd_get_next_hop,
-    NULL, /* get_status */
-    NULL, /* arp_lookup */
-
-    netdev_bsd_update_flags,
-
-    netdev_bsd_change_seq,
-
-    netdev_bsd_rx_alloc,
-    netdev_bsd_rx_construct,
-    netdev_bsd_rx_destruct,
-    netdev_bsd_rx_dealloc,
-    netdev_bsd_rx_recv,
-    netdev_bsd_rx_wait,
-    netdev_bsd_rx_drain,
-};
+const struct netdev_class netdev_bsd_class =
+    NETDEV_BSD_CLASS(
+        "system",
+        netdev_bsd_construct_system,
+        netdev_bsd_get_features);
+
+const struct netdev_class netdev_tap_class =
+    NETDEV_BSD_CLASS(
+        "tap",
+        netdev_bsd_construct_tap,
+        netdev_bsd_get_features);
 \f
 
 static void
@@ -1677,7 +1789,7 @@ ifr_set_flags(struct ifreq *ifr, int flags)
 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
  * 'arg'.  Returns 0 if successful, otherwise a positive errno value. */
 int
-af_link_ioctl(int command, const void *arg)
+af_link_ioctl(unsigned long command, const void *arg)
 {
     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
     static int sock;