Merge branch 'mainstream'
[sliver-openvswitch.git] / lib / netdev-bsd.c
index 50fb520..dd27d2d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011 Gaetano Catalli.
+ * Copyright (c) 2011, 2013 Gaetano Catalli.
  * Copyright (c) 2013 YAMAMOTO Takashi.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -42,6 +42,8 @@
 #include <sys/sysctl.h>
 #if defined(__NetBSD__)
 #include <net/route.h>
+#include <netinet/in.h>
+#include <netinet/if_inarp.h>
 #endif
 
 #include "rtbsd.h"
@@ -291,7 +293,7 @@ netdev_bsd_construct_system(struct netdev *netdev_)
         return error;
     }
 
-    ovs_mutex_init(&netdev->mutex, PTHREAD_MUTEX_NORMAL);
+    ovs_mutex_init(&netdev->mutex);
     netdev->change_seq = 1;
     netdev->tap_fd = -1;
     netdev->kernel_name = xstrdup(netdev_->name);
@@ -325,7 +327,7 @@ 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, PTHREAD_MUTEX_NORMAL);
+    ovs_mutex_init(&netdev->mutex);
     netdev->tap_fd = open("/dev/tap", O_RDWR);
     netdev->change_seq = 1;
     if (netdev->tap_fd < 0) {
@@ -700,8 +702,8 @@ netdev_bsd_send(struct netdev *netdev_, const void *data, size_t size)
                 }
             }
         } else if (retval != size) {
-            VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%zd bytes of "
-                         "%zu) on %s", retval, size, name);
+            VLOG_WARN_RL(&rl, "sent partial Ethernet packet (%"PRIuSIZE"d bytes of "
+                         "%"PRIuSIZE") on %s", retval, size, name);
             error = EMSGSIZE;
         } else {
             break;
@@ -1332,6 +1334,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)
 {
@@ -1369,6 +1428,9 @@ nd_to_iff_flags(enum netdev_flags nd)
         iff |= IFF_PPROMISC;
 #endif
     }
+    if (nd & NETDEV_LOOPBACK) {
+        iff |= IFF_LOOPBACK;
+    }
     return iff;
 }
 
@@ -1382,6 +1444,9 @@ iff_to_nd_flags(int iff)
     if (iff & IFF_PROMISC) {
         nd |= NETDEV_PROMISC;
     }
+    if (iff & IFF_LOOPBACK) {
+        nd |= NETDEV_LOOPBACK;
+    }
     return nd;
 }
 
@@ -1451,7 +1516,9 @@ const struct netdev_class netdev_bsd_class = {
     NULL, /* set_queue */
     NULL, /* delete_queue */
     NULL, /* get_queue_stats */
-    NULL, /* dump_queue */
+    NULL, /* queue_dump_start */
+    NULL, /* queue_dump_next */
+    NULL, /* queue_dump_done */
     NULL, /* dump_queue_stats */
 
     netdev_bsd_get_in4,
@@ -1460,7 +1527,7 @@ const struct netdev_class netdev_bsd_class = {
     NULL, /* add_router */
     netdev_bsd_get_next_hop,
     NULL, /* get_status */
-    NULL, /* arp_lookup */
+    netdev_bsd_arp_lookup, /* arp_lookup */
 
     netdev_bsd_update_flags,
 
@@ -1514,7 +1581,9 @@ const struct netdev_class netdev_tap_class = {
     NULL, /* set_queue */
     NULL, /* delete_queue */
     NULL, /* get_queue_stats */
-    NULL, /* dump_queue */
+    NULL, /* queue_dump_start */
+    NULL, /* queue_dump_next */
+    NULL, /* queue_dump_done */
     NULL, /* dump_queue_stats */
 
     netdev_bsd_get_in4,
@@ -1523,7 +1592,7 @@ const struct netdev_class netdev_tap_class = {
     NULL, /* add_router */
     netdev_bsd_get_next_hop,
     NULL, /* get_status */
-    NULL, /* arp_lookup */
+    netdev_bsd_arp_lookup, /* arp_lookup */
 
     netdev_bsd_update_flags,