]> git.proxmox.com Git - mirror_ovs.git/blobdiff - lib/netdev-bsd.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / netdev-bsd.c
index 65ae9f9dab8d22d20503bbdf5c13d9bf62f9b6c4..7875636cc3ccd10341bed904315f18a6e660d22b 100644 (file)
@@ -15,6 +15,7 @@
  * limitations under the License.
  */
 
+#if !defined(__MACH__)
 #include <config.h>
 
 #include "netdev-provider.h"
@@ -26,6 +27,7 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/sockio.h>
+#include <net/bpf.h>
 #include <ifaddrs.h>
 #include <pcap/pcap.h>
 #include <net/if.h>
 #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 "dp-packet.h"
 #include "dpif-netdev.h"
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
 #include "fatal-signal.h"
-#include "ofpbuf.h"
 #include "openflow/openflow.h"
 #include "ovs-thread.h"
-#include "packet-dpif.h"
 #include "packets.h"
-#include "poll-loop.h"
-#include "shash.h"
+#include "openvswitch/poll-loop.h"
+#include "openvswitch/shash.h"
 #include "socket-util.h"
 #include "svec.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
 
-\f
 struct netdev_rxq_bsd {
     struct netdev_rxq up;
 
@@ -90,10 +89,7 @@ struct netdev_bsd {
     unsigned int cache_valid;
 
     int ifindex;
-    uint8_t etheraddr[ETH_ADDR_LEN];
-    struct in_addr in4;
-    struct in_addr netmask;
-    struct in6_addr in6;
+    struct eth_addr etheraddr;
     int mtu;
     int carrier;
 
@@ -106,12 +102,11 @@ struct netdev_bsd {
 
 
 enum {
-    VALID_IFINDEX = 1 << 0,
+    VALID_IFINDEX   = 1 << 0,
     VALID_ETHERADDR = 1 << 1,
-    VALID_IN4 = 1 << 2,
-    VALID_IN6 = 1 << 3,
-    VALID_MTU = 1 << 4,
-    VALID_CARRIER = 1 << 5
+    VALID_IN        = 1 << 2,
+    VALID_MTU       = 1 << 3,
+    VALID_CARRIER   = 1 << 4
 };
 
 #define PCAP_SNAPLEN 2048
@@ -138,9 +133,9 @@ static int set_flags(const char *, int flags);
 static int do_set_addr(struct netdev *netdev,
                        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 get_etheraddr(const char *netdev_name, struct eth_addr *ea);
 static int set_etheraddr(const char *netdev_name, int hwaddr_family,
-                         int hwaddr_len, const uint8_t[ETH_ADDR_LEN]);
+                         int hwaddr_len, const struct eth_addr);
 static int get_ifindex(const struct netdev *, int *ifindexp);
 
 static int ifr_get_flags(const struct ifreq *);
@@ -150,7 +145,7 @@ static void ifr_set_flags(struct ifreq *, int flags);
 static int af_link_ioctl(unsigned long command, const void *arg);
 #endif
 
-static void netdev_bsd_run(void);
+static void netdev_bsd_run(const struct netdev_class *);
 static int netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup);
 
 static bool
@@ -184,7 +179,7 @@ netdev_get_kernel_name(const struct netdev *netdev)
  * interface status changes, and eventually calls all the user callbacks.
  */
 static void
-netdev_bsd_run(void)
+netdev_bsd_run(const struct netdev_class *netdev_class OVS_UNUSED)
 {
     rtbsd_notifier_run();
 }
@@ -194,7 +189,7 @@ netdev_bsd_run(void)
  * be called.
  */
 static void
-netdev_bsd_wait(void)
+netdev_bsd_wait(const struct netdev_class *netdev_class OVS_UNUSED)
 {
     rtbsd_notifier_wait();
 }
@@ -295,6 +290,7 @@ netdev_bsd_construct_system(struct netdev *netdev_)
     if (error == ENXIO) {
         free(netdev->kernel_name);
         cache_notifier_unref();
+        ovs_mutex_destroy(&netdev->mutex);
         return error;
     }
 
@@ -361,7 +357,7 @@ netdev_bsd_construct_tap(struct netdev *netdev_)
 
     /* Turn device UP */
     ifr_set_flags(&ifr, IFF_UP);
-    strncpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
+    ovs_strlcpy(ifr.ifr_name, kernel_name, sizeof ifr.ifr_name);
     error = af_inet_ioctl(SIOCSIFFLAGS, &ifr);
     if (error) {
         destroy_tap(netdev->tap_fd, kernel_name);
@@ -569,20 +565,20 @@ proc_pkt(u_char *args_, const struct pcap_pkthdr *hdr, const u_char *packet)
  * from rxq->pcap.
  */
 static int
-netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
+netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
 {
     struct pcap_arg arg;
     int ret;
 
     /* prepare the pcap argument to store the packet */
-    arg.size = ofpbuf_tailroom(buffer);
-    arg.data = ofpbuf_data(buffer);
+    arg.size = dp_packet_tailroom(buffer);
+    arg.data = dp_packet_data(buffer);
 
     for (;;) {
         ret = pcap_dispatch(rxq->pcap_handle, 1, proc_pkt, (u_char *) &arg);
 
         if (ret > 0) {
-            ofpbuf_set_size(buffer, ofpbuf_size(buffer) + arg.retval);
+            dp_packet_set_size(buffer, dp_packet_size(buffer) + arg.retval);
             return 0;
         }
         if (ret == -1) {
@@ -601,14 +597,14 @@ netdev_rxq_bsd_recv_pcap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
  * 'rxq->fd' is initialized with the tap file descriptor.
  */
 static int
-netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
+netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct dp_packet *buffer)
 {
-    size_t size = ofpbuf_tailroom(buffer);
+    size_t size = dp_packet_tailroom(buffer);
 
     for (;;) {
-        ssize_t retval = read(rxq->fd, ofpbuf_data(buffer), size);
+        ssize_t retval = read(rxq->fd, dp_packet_data(buffer), size);
         if (retval >= 0) {
-            ofpbuf_set_size(buffer, ofpbuf_size(buffer) + retval);
+            dp_packet_set_size(buffer, dp_packet_size(buffer) + retval);
             return 0;
         } else if (errno != EINTR) {
             if (errno != EAGAIN) {
@@ -621,13 +617,12 @@ netdev_rxq_bsd_recv_tap(struct netdev_rxq_bsd *rxq, struct ofpbuf *buffer)
 }
 
 static int
-netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
-                    int *c)
+netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dp_packet_batch *batch,
+                    int *qfill)
 {
     struct netdev_rxq_bsd *rxq = netdev_rxq_bsd_cast(rxq_);
     struct netdev *netdev = rxq->up.netdev;
-    struct dpif_packet *packet;
-    struct ofpbuf *buffer;
+    struct dp_packet *packet;
     ssize_t retval;
     int mtu;
 
@@ -635,21 +630,23 @@ netdev_bsd_rxq_recv(struct netdev_rxq *rxq_, struct dpif_packet **packets,
         mtu = ETH_PAYLOAD_MAX;
     }
 
-    packet = dpif_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
+    /* Assume Ethernet port. No need to set packet_type. */
+    packet = dp_packet_new_with_headroom(VLAN_ETH_HEADER_LEN + mtu,
                                            DP_NETDEV_HEADROOM);
-    buffer = &packet->ofpbuf;
-
     retval = (rxq->pcap_handle
-            ? netdev_rxq_bsd_recv_pcap(rxq, buffer)
-            : netdev_rxq_bsd_recv_tap(rxq, buffer));
+            ? netdev_rxq_bsd_recv_pcap(rxq, packet)
+            : netdev_rxq_bsd_recv_tap(rxq, packet));
 
     if (retval) {
-        dpif_packet_delete(packet);
+        dp_packet_delete(packet);
     } else {
-        dp_packet_pad(buffer);
-        packets[0] = packet;
-        *c = 1;
+        dp_packet_batch_init_packet(batch, packet);
     }
+
+    if (qfill) {
+        *qfill = -ENOTSUP;
+    }
+
     return retval;
 }
 
@@ -686,13 +683,14 @@ netdev_bsd_rxq_drain(struct netdev_rxq *rxq_)
  * system or a tap device.
  */
 static int
-netdev_bsd_send(struct netdev *netdev_, struct dpif_packet **pkts, int cnt,
-                bool may_steal)
+netdev_bsd_send(struct netdev *netdev_, int qid OVS_UNUSED,
+                struct dp_packet_batch *batch,
+                bool concurrent_txq OVS_UNUSED)
 {
     struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
     const char *name = netdev_get_name(netdev_);
+    struct dp_packet *packet;
     int error;
-    int i;
 
     ovs_mutex_lock(&dev->mutex);
     if (dev->tap_fd < 0 && !dev->pcap) {
@@ -701,9 +699,9 @@ netdev_bsd_send(struct netdev *netdev_, struct dpif_packet **pkts, int cnt,
         error = 0;
     }
 
-    for (i = 0; i < cnt; i++) {
-        const void *data = ofpbuf_data(&pkts[i]->ofpbuf);
-        size_t size = ofpbuf_size(&pkts[i]->ofpbuf);
+    DP_PACKET_BATCH_FOR_EACH (i, packet, batch) {
+        const void *data = dp_packet_data(packet);
+        size_t size = dp_packet_size(packet);
 
         while (!error) {
             ssize_t retval;
@@ -734,11 +732,7 @@ netdev_bsd_send(struct netdev *netdev_, struct dpif_packet **pkts, int cnt,
     }
 
     ovs_mutex_unlock(&dev->mutex);
-    if (may_steal) {
-        for (i = 0; i < cnt; i++) {
-            dpif_packet_delete(pkts[i]);
-        }
-    }
+    dp_packet_delete_batch(batch, true);
 
     return error;
 }
@@ -749,7 +743,7 @@ netdev_bsd_send(struct netdev *netdev_, struct dpif_packet **pkts, int cnt,
  * with netdev_send().
  */
 static void
-netdev_bsd_send_wait(struct netdev *netdev_)
+netdev_bsd_send_wait(struct netdev *netdev_, int qid OVS_UNUSED)
 {
     struct netdev_bsd *dev = netdev_bsd_cast(netdev_);
 
@@ -772,7 +766,7 @@ netdev_bsd_send_wait(struct netdev *netdev_)
  */
 static int
 netdev_bsd_set_etheraddr(struct netdev *netdev_,
-                         const uint8_t mac[ETH_ADDR_LEN])
+                         const struct eth_addr mac)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
     int error = 0;
@@ -784,7 +778,7 @@ netdev_bsd_set_etheraddr(struct netdev *netdev_,
                               ETH_ADDR_LEN, mac);
         if (!error) {
             netdev->cache_valid |= VALID_ETHERADDR;
-            memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
+            netdev->etheraddr = mac;
             netdev_change_seq_changed(netdev_);
         }
     }
@@ -798,8 +792,7 @@ netdev_bsd_set_etheraddr(struct netdev *netdev_,
  * free the returned buffer.
  */
 static int
-netdev_bsd_get_etheraddr(const struct netdev *netdev_,
-                         uint8_t mac[ETH_ADDR_LEN])
+netdev_bsd_get_etheraddr(const struct netdev *netdev_, struct eth_addr *mac)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
     int error = 0;
@@ -807,13 +800,13 @@ netdev_bsd_get_etheraddr(const struct netdev *netdev_,
     ovs_mutex_lock(&netdev->mutex);
     if (!(netdev->cache_valid & VALID_ETHERADDR)) {
         error = get_etheraddr(netdev_get_kernel_name(netdev_),
-                              netdev->etheraddr);
+                              &netdev->etheraddr);
         if (!error) {
             netdev->cache_valid |= VALID_ETHERADDR;
         }
     }
     if (!error) {
-        memcpy(mac, netdev->etheraddr, ETH_ADDR_LEN);
+        *mac = netdev->etheraddr;
     }
     ovs_mutex_unlock(&netdev->mutex);
 
@@ -847,7 +840,7 @@ netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
     }
     ovs_mutex_unlock(&netdev->mutex);
 
-    return 0;
+    return error;
 }
 
 static int
@@ -874,8 +867,8 @@ netdev_bsd_get_carrier(const struct netdev *netdev_, bool *carrier)
         struct ifmediareq ifmr;
 
         memset(&ifmr, 0, sizeof(ifmr));
-        strncpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
-                sizeof ifmr.ifm_name);
+        ovs_strlcpy(ifmr.ifm_name, netdev_get_kernel_name(netdev_),
+                    sizeof ifmr.ifm_name);
 
         error = af_inet_ioctl(SIOCGIFMEDIA, &ifmr);
         if (!error) {
@@ -1003,13 +996,13 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
     mib[3] = IFMIB_IFDATA;
     len = sizeof(ifmd);
     for (i = 1; i <= if_count; i++) {
-        mib[4] = i; //row
+        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_), ovs_strerror(errno));
             return errno;
         } else if (!strcmp(ifmd.ifmd_name, netdev_get_name(netdev_))) {
-            convert_stats(netdev, stats, &ifdr.ifdr_data);
+            convert_stats(netdev_, stats, &ifmd.ifmd_data);
             break;
         }
     }
@@ -1020,8 +1013,8 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
     int error;
 
     memset(&ifdr, 0, sizeof(ifdr));
-    strncpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
-            sizeof(ifdr.ifdr_name));
+    ovs_strlcpy(ifdr.ifdr_name, netdev_get_kernel_name(netdev_),
+                sizeof(ifdr.ifdr_name));
     error = af_link_ioctl(SIOCGIFDATA, &ifdr);
     if (!error) {
         convert_stats(netdev_, stats, &ifdr.ifdr_data);
@@ -1125,7 +1118,7 @@ netdev_bsd_get_features(const struct netdev *netdev,
     /* XXX Look into SIOCGIFCAP instead of SIOCGIFMEDIA */
 
     memset(&ifmr, 0, sizeof(ifmr));
-    strncpy(ifmr.ifm_name, netdev_get_name(netdev), sizeof ifmr.ifm_name);
+    ovs_strlcpy(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
@@ -1175,46 +1168,6 @@ cleanup:
     return error;
 }
 
-/*
- * If 'netdev' has an assigned IPv4 address, sets '*in4' to that address and
- * '*netmask' to its netmask 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_bsd *netdev = netdev_bsd_cast(netdev_);
-    int error = 0;
-
-    ovs_mutex_lock(&netdev->mutex);
-    if (!(netdev->cache_valid & VALID_IN4)) {
-        struct ifreq ifr;
-
-        ifr.ifr_addr.sa_family = AF_INET;
-        error = af_inet_ifreq_ioctl(netdev_get_kernel_name(netdev_), &ifr,
-                                    SIOCGIFADDR, "SIOCGIFADDR");
-        if (!error) {
-            const struct sockaddr_in *sin;
-
-            sin = ALIGNED_CAST(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;
-            }
-        }
-    }
-    if (!error) {
-        *in4 = netdev->in4;
-        *netmask = netdev->netmask;
-    }
-    ovs_mutex_unlock(&netdev->mutex);
-
-    return error ? error : 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
@@ -1233,11 +1186,6 @@ netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
         if (addr.s_addr != INADDR_ANY) {
             error = do_set_addr(netdev_, SIOCSIFNETMASK,
                                 "SIOCSIFNETMASK", mask);
-            if (!error) {
-                netdev->cache_valid |= VALID_IN4;
-                netdev->in4 = addr;
-                netdev->netmask = mask;
-            }
         }
         netdev_change_seq_changed(netdev_);
     }
@@ -1247,37 +1195,20 @@ netdev_bsd_set_in4(struct netdev *netdev_, struct in_addr addr,
 }
 
 static int
-netdev_bsd_get_in6(const struct netdev *netdev_, struct in6_addr *in6)
+netdev_bsd_get_addr_list(const struct netdev *netdev_,
+                         struct in6_addr **addr, struct in6_addr **mask, int *n_cnt)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
-    if (!(netdev->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,
-                    ovs_strerror(errno));
-            return errno;
-        }
+    int error;
 
-        for (ifa = head; ifa; ifa = ifa->ifa_next) {
-            if (ifa->ifa_addr->sa_family == AF_INET6 &&
-                    !strcmp(ifa->ifa_name, netdev_name)) {
-                sin6 = ALIGNED_CAST(struct sockaddr_in6 *, ifa->ifa_addr);
-                if (sin6) {
-                    memcpy(&netdev->in6, &sin6->sin6_addr, sin6->sin6_len);
-                    netdev->cache_valid |= VALID_IN6;
-                    *in6 = netdev->in6;
-                    freeifaddrs(head);
-                    return 0;
-                }
-            }
-        }
-        return EADDRNOTAVAIL;
+    if (!(netdev->cache_valid & VALID_IN)) {
+        netdev_get_addrs_list_flush();
     }
-    *in6 = netdev->in6;
-    return 0;
+    error = netdev_get_addrs(netdev_get_name(netdev_), addr, mask, n_cnt);
+    if (!error) {
+        netdev->cache_valid |= VALID_IN;
+    }
+    return error;
 }
 
 #if defined(__NetBSD__)
@@ -1411,7 +1342,7 @@ netdev_bsd_get_next_hop(const struct in_addr *host OVS_UNUSED,
 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)
+                      struct eth_addr *mac OVS_UNUSED)
 {
 #if defined(__NetBSD__)
     const struct rt_msghdr *rtm;
@@ -1428,7 +1359,11 @@ netdev_bsd_arp_lookup(const struct netdev *netdev OVS_UNUSED,
     mib[2] = 0;
     mib[3] = AF_INET;
     mib[4] = NET_RT_FLAGS;
+#ifdef RTF_LLINFO
     mib[5] = RTF_LLINFO;
+#else
+    mib[5] = 0;
+#endif
     if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1) {
         error = errno;
         goto error;
@@ -1543,85 +1478,45 @@ netdev_bsd_update_flags(struct netdev *netdev_, enum netdev_flags off,
     return error;
 }
 
-/* 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,                            \
-}
+#define NETDEV_BSD_CLASS_COMMON                      \
+    .run = netdev_bsd_run,                           \
+    .wait = netdev_bsd_wait,                         \
+    .alloc = netdev_bsd_alloc,                       \
+    .destruct = netdev_bsd_destruct,                 \
+    .dealloc = netdev_bsd_dealloc,                   \
+    .send = netdev_bsd_send,                         \
+    .send_wait = netdev_bsd_send_wait,               \
+    .set_etheraddr = netdev_bsd_set_etheraddr,       \
+    .get_etheraddr = netdev_bsd_get_etheraddr,       \
+    .get_mtu = netdev_bsd_get_mtu,                   \
+    .get_ifindex = netdev_bsd_get_ifindex,           \
+    .get_carrier = netdev_bsd_get_carrier,           \
+    .get_stats = netdev_bsd_get_stats,               \
+    .get_features = netdev_bsd_get_features,         \
+    .set_in4 = netdev_bsd_set_in4,                   \
+    .get_addr_list = netdev_bsd_get_addr_list,       \
+    .get_next_hop = netdev_bsd_get_next_hop,         \
+    .arp_lookup = netdev_bsd_arp_lookup,             \
+    .update_flags = netdev_bsd_update_flags,         \
+    .rxq_alloc = netdev_bsd_rxq_alloc,               \
+    .rxq_construct = netdev_bsd_rxq_construct,       \
+    .rxq_destruct = netdev_bsd_rxq_destruct,         \
+    .rxq_dealloc = netdev_bsd_rxq_dealloc,           \
+    .rxq_recv = netdev_bsd_rxq_recv,                 \
+    .rxq_wait = netdev_bsd_rxq_wait,                 \
+    .rxq_drain = netdev_bsd_rxq_drain
+
+const struct netdev_class netdev_bsd_class = {
+    NETDEV_BSD_CLASS_COMMON,
+    .type = "system",
+    .construct = netdev_bsd_construct_system,
+};
 
-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);
+const struct netdev_class netdev_tap_class = {
+    NETDEV_BSD_CLASS_COMMON,
+    .type = "tap",
+    .construct = netdev_bsd_construct_tap,
+};
 \f
 
 static void
@@ -1677,7 +1572,7 @@ get_ifindex(const struct netdev *netdev_, int *ifindexp)
 }
 
 static int
-get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
+get_etheraddr(const char *netdev_name, struct eth_addr *ea)
 {
     struct ifaddrs *head;
     struct ifaddrs *ifa;
@@ -1710,17 +1605,17 @@ get_etheraddr(const char *netdev_name, uint8_t ea[ETH_ADDR_LEN])
 static int
 set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
               int hwaddr_len OVS_UNUSED,
-              const uint8_t mac[ETH_ADDR_LEN] OVS_UNUSED)
+              const struct eth_addr mac OVS_UNUSED)
 {
 #if defined(__FreeBSD__)
     struct ifreq ifr;
     int error;
 
     memset(&ifr, 0, sizeof ifr);
-    strncpy(ifr.ifr_name, netdev_name, sizeof ifr.ifr_name);
+    ovs_strlcpy(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);
+    memcpy(ifr.ifr_addr.sa_data, &mac, hwaddr_len);
     error = af_inet_ioctl(SIOCSIFLLADDR, &ifr);
     if (error) {
         VLOG_ERR("ioctl(SIOCSIFLLADDR) on %s device failed: %s",
@@ -1743,7 +1638,7 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
         return EOPNOTSUPP;
     }
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.addr.ss_len = sizeof(req.addr);
     req.addr.ss_family = hwaddr_family;
     sdl = (struct sockaddr_dl *)&req.addr;
@@ -1753,26 +1648,26 @@ set_etheraddr(const char *netdev_name OVS_UNUSED, int hwaddr_family OVS_UNUSED,
     if (error) {
         return error;
     }
-    if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], mac, hwaddr_len)) {
+    if (!memcmp(&sdl->sdl_data[sdl->sdl_nlen], &mac, hwaddr_len)) {
         return 0;
     }
     oldaddr = req.addr;
 
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.flags = IFLR_ACTIVE;
     sdl = (struct sockaddr_dl *)&req.addr;
     sdl->sdl_len = offsetof(struct sockaddr_dl, sdl_data) + hwaddr_len;
     sdl->sdl_alen = hwaddr_len;
     sdl->sdl_family = hwaddr_family;
-    memcpy(sdl->sdl_data, mac, hwaddr_len);
+    memcpy(sdl->sdl_data, &mac, hwaddr_len);
     error = af_link_ioctl(SIOCALIFADDR, &req);
     if (error) {
         return error;
     }
 
     memset(&req, 0, sizeof(req));
-    strncpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
+    ovs_strlcpy(req.iflr_name, netdev_name, sizeof(req.iflr_name));
     req.addr = oldaddr;
     return af_link_ioctl(SIOCDLIFADDR, &req);
 #else
@@ -1784,7 +1679,7 @@ static int
 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
@@ -1793,12 +1688,15 @@ ifr_get_flags(const struct ifreq *ifr)
 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
 }
 
+#if defined(__NetBSD__)
 /* Calls ioctl() on an AF_LINK sock, passing the specified 'command' and
  * 'arg'.  Returns 0 if successful, otherwise a positive errno value. */
 int
@@ -1820,3 +1718,5 @@ af_link_ioctl(unsigned long command, const void *arg)
             : ioctl(sock, command, arg) == -1 ? errno
             : 0);
 }
+#endif
+#endif /* !defined(__MACH__) */