upgrade to new signature of update_flags
[sliver-openvswitch.git] / lib / netdev-bsd.c
index f8b1188..8b384ba 100644 (file)
@@ -17,8 +17,6 @@
 #include <config.h>
 
 #include <stdlib.h>
-#include <config.h>
-#include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/types.h>
@@ -51,6 +49,7 @@
 #include "socket-util.h"
 #include "shash.h"
 #include "svec.h"
+#include "util.h"
 #include "vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(netdev_bsd);
@@ -134,11 +133,11 @@ 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 int netdev_bsd_do_ioctl(const char *, 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 get_flags(const struct netdev_dev *, 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,
                        struct in_addr addr);
@@ -158,14 +157,15 @@ is_netdev_bsd_class(const struct netdev_class *netdev_class)
 static struct netdev_bsd *
 netdev_bsd_cast(const struct netdev *netdev)
 {
-    assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_get_dev(netdev))));
+    ovs_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)));
+    ovs_assert(is_netdev_bsd_class(netdev_dev_get_class(netdev_dev)));
     return CONTAINER_OF(netdev_dev, struct netdev_dev_bsd, netdev_dev);
 }
 
@@ -399,7 +399,6 @@ netdev_bsd_destroy(struct netdev_dev *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;
@@ -415,15 +414,6 @@ netdev_bsd_open_system(struct netdev_dev *netdev_dev_, struct netdev **netdevp)
         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;
 
@@ -451,6 +441,9 @@ static int
 netdev_bsd_listen(struct netdev *netdev_)
 {
     struct netdev_bsd *netdev = netdev_bsd_cast(netdev_);
+    struct netdev_dev_bsd *netdev_dev =
+                              netdev_dev_bsd_cast(netdev_get_dev(netdev_));
+
     char errbuf[PCAP_ERRBUF_SIZE];
     int error;
     int fd = -1;
@@ -460,6 +453,13 @@ netdev_bsd_listen(struct netdev *netdev_)
         return 0;
     }
 
+    if (!strcmp(netdev_get_type(netdev_), "tap") &&
+            !netdev_dev->tap_opened) {
+        netdev->netdev_fd = netdev_dev->tap_fd;
+        netdev_dev->tap_opened = true;
+        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';
@@ -813,7 +813,8 @@ netdev_bsd_get_mtu(const struct netdev *netdev_, int *mtup)
         struct ifreq ifr;
         int error;
 
-        error = netdev_bsd_do_ioctl(netdev_, &ifr, SIOCGIFMTU, "SIOCGIFMTU");
+        error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr, SIOCGIFMTU,
+                                    "SIOCGIFMTU");
         if (error) {
             return error;
         }
@@ -907,22 +908,22 @@ netdev_bsd_get_stats(const struct netdev *netdev_, struct netdev_stats *stats)
             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->tx_dropped = UINT64_MAX;
             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;
+            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;
             break;
         }
     }
@@ -1037,7 +1038,7 @@ netdev_bsd_get_features(const struct netdev *netdev,
     media_list = xcalloc(ifmr.ifm_count, sizeof(int));
     ifmr.ifm_ulist = media_list;
 
-    if (!IFM_TYPE(ifmr.ifm_current) & IFM_ETHER) {
+    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;
@@ -1089,7 +1090,7 @@ netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
         int error;
 
         ifr.ifr_addr.sa_family = AF_INET;
-        error = netdev_bsd_do_ioctl(netdev_, &ifr,
+        error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
                                       SIOCGIFADDR, "SIOCGIFADDR");
         if (error) {
             return error;
@@ -1098,7 +1099,7 @@ netdev_bsd_get_in4(const struct netdev *netdev_, struct in_addr *in4,
         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,
+        error = netdev_bsd_do_ioctl(netdev_get_name(netdev_), &ifr,
                                       SIOCGIFNETMASK, "SIOCGIFNETMASK");
         if (error) {
             return error;
@@ -1221,19 +1222,21 @@ iff_to_nd_flags(int iff)
 }
 
 static int
-netdev_bsd_update_flags(struct netdev *netdev, enum netdev_flags off,
+netdev_bsd_update_flags(struct netdev_dev *dev_, enum netdev_flags off,
                           enum netdev_flags on, enum netdev_flags *old_flagsp)
 {
+    struct netdev_dev_bsd *netdev_dev;
     int old_flags, new_flags;
     int error;
 
-    error = get_flags(netdev, &old_flags);
+    netdev_dev = netdev_dev_bsd_cast(dev_);
+    error = get_flags(dev_, &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)));
+            error = set_flags(netdev_dev_get_name(dev_), new_flags);
+            netdev_dev_bsd_changed(netdev_dev);
         }
     }
     return error;
@@ -1256,6 +1259,7 @@ const struct netdev_class netdev_bsd_class = {
     netdev_bsd_destroy,
     NULL, /* get_config */
     NULL, /* set_config */
+    NULL, /* get_tunnel_config */
     netdev_bsd_open_system,
     netdev_bsd_close,
 
@@ -1298,7 +1302,7 @@ const struct netdev_class netdev_bsd_class = {
     netdev_bsd_get_in6,
     NULL, /* add_router */
     NULL, /* get_next_hop */
-    NULL, /* get_drv_info */
+    NULL, /* get_status */
     NULL, /* arp_lookup */
 
     netdev_bsd_update_flags,
@@ -1316,6 +1320,7 @@ const struct netdev_class netdev_tap_class = {
     netdev_bsd_destroy,
     NULL, /* get_config */
     NULL, /* set_config */
+    NULL, /* get_tunnel_config */
     netdev_bsd_open_system,
     netdev_bsd_close,
 
@@ -1358,7 +1363,7 @@ const struct netdev_class netdev_tap_class = {
     netdev_bsd_get_in6,
     NULL, /* add_router */
     NULL, /* get_next_hop */
-    NULL, /* get_drv_info */
+    NULL, /* get_status */
     NULL, /* arp_lookup */
 
     netdev_bsd_update_flags,
@@ -1379,12 +1384,12 @@ destroy_tap(int fd, const char *name)
 }
 
 static int
-get_flags(const struct netdev *netdev, int *flags)
+get_flags(const struct netdev_dev *dev, int *flags)
 {
     struct ifreq ifr;
     int error;
 
-    error = netdev_bsd_do_ioctl(netdev, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
+    error = netdev_bsd_do_ioctl(dev->name, &ifr, SIOCGIFFLAGS, "SIOCGIFFLAGS");
 
     *flags = 0xFFFF0000 & (ifr.ifr_flagshigh << 16);
     *flags |= 0x0000FFFF & ifr.ifr_flags;
@@ -1393,14 +1398,14 @@ get_flags(const struct netdev *netdev, int *flags)
 }
 
 static int
-set_flags(struct netdev *netdev, int flags)
+set_flags(const char *name, 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");
+    return netdev_bsd_do_ioctl(name, &ifr, SIOCSIFFLAGS, "SIOCSIFFLAGS");
 }
 
 static int
@@ -1472,13 +1477,13 @@ set_etheraddr(const char *netdev_name, int hwaddr_family,
 }
 
 static int
-netdev_bsd_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
-                    unsigned long cmd, const char *cmd_name)
+netdev_bsd_do_ioctl(const char *name, struct ifreq *ifr, unsigned long cmd,
+                    const char *cmd_name)
 {
-    strncpy(ifr->ifr_name, netdev_get_name(netdev), sizeof ifr->ifr_name);
+    strncpy(ifr->ifr_name, name, 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));
+        VLOG_DBG_RL(&rl, "%s: ioctl(%s) failed: %s", name, cmd_name,
+                    strerror(errno));
         return errno;
     }
     return 0;