For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / netdev.c
index 7b55aec..9a91e2f 100644 (file)
@@ -17,7 +17,7 @@
  * The above copyright notice and this permission notice shall be
  * included in all copies or substantial portions of the Software.
  * 
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * THE SOFTWRE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <arpa/inet.h>
 #include <inttypes.h>
+#include <linux/if_tun.h>
 #include <linux/types.h>
 #include <linux/ethtool.h>
 #include <linux/sockios.h>
+#include <linux/version.h>
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include "fatal-signal.h"
 #include "list.h"
 #include "ofpbuf.h"
-#include "openflow.h"
+#include "openflow/openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "socket-util.h"
+#include "svec.h"
 
 #define THIS_MODULE VLM_netdev
 #include "vlog.h"
 struct netdev {
     struct list node;
     char *name;
+
+    /* File descriptors.  For ordinary network devices, the two fds below are
+     * the same; for tap devices, they differ. */
+    int netdev_fd;              /* Network device. */
+    int tap_fd;                 /* TAP character device, if any, otherwise the
+                                 * network device. */
+
+    /* Cached network device information. */
     int ifindex;
-    int fd;
     uint8_t etheraddr[ETH_ADDR_LEN];
+    struct in6_addr in6;
     int speed;
     int mtu;
-    uint32_t features;
-    struct in6_addr in6;
+    int txqlen;
+    int hwaddr_family;
+
+    /* Bitmaps of OFPPF_* that describe features.  All bits disabled if
+     * unsupported or unavailable. */
+    uint32_t curr;              /* Current features. */
+    uint32_t advertised;        /* Features being advertised by the port. */
+    uint32_t supported;         /* Features supported by the port. */
+    uint32_t peer;              /* Features advertised by the peer. */
+
     int save_flags;             /* Initial device flags. */
     int changed_flags;          /* Flags that we changed. */
 };
@@ -91,9 +111,11 @@ static int af_inet_sock = -1;
 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(5, 20);
 
 static void init_netdev(void);
+static int do_open_netdev(const char *name, int ethertype, int tap_fd,
+                          struct netdev **netdev_);
 static int restore_flags(struct netdev *netdev);
-static int get_flags(const struct netdev *, int *flagsp);
-static int set_flags(struct netdev *, int flags);
+static int get_flags(const struct netdev *, int fd, int *flagsp);
+static int set_flags(struct netdev *, int fd, int flags);
 
 /* Obtains the IPv6 address for 'name' into 'in6'. */
 static void
@@ -124,6 +146,7 @@ get_ipv6_address(const char *name, struct in6_addr *in6)
                    ifname) == 17
             && !strcmp(name, ifname))
         {
+            fclose(file);
             return;
         }
     }
@@ -138,8 +161,10 @@ do_ethtool(struct netdev *netdev)
     struct ifreq ifr;
     struct ethtool_cmd ecmd;
 
-    netdev->speed = 0;
-    netdev->features = 0;
+    netdev->curr = 0;
+    netdev->supported = 0;
+    netdev->advertised = 0;
+    netdev->peer = 0;
 
     memset(&ifr, 0, sizeof ifr);
     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
@@ -147,50 +172,109 @@ do_ethtool(struct netdev *netdev)
 
     memset(&ecmd, 0, sizeof ecmd);
     ecmd.cmd = ETHTOOL_GSET;
-    if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) {
+    if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
         if (ecmd.supported & SUPPORTED_10baseT_Half) {
-            netdev->features |= OFPPF_10MB_HD;
+            netdev->supported |= OFPPF_10MB_HD;
         }
         if (ecmd.supported & SUPPORTED_10baseT_Full) {
-            netdev->features |= OFPPF_10MB_FD;
+            netdev->supported |= OFPPF_10MB_FD;
         }
         if (ecmd.supported & SUPPORTED_100baseT_Half)  {
-            netdev->features |= OFPPF_100MB_HD;
+            netdev->supported |= OFPPF_100MB_HD;
         }
         if (ecmd.supported & SUPPORTED_100baseT_Full) {
-            netdev->features |= OFPPF_100MB_FD;
+            netdev->supported |= OFPPF_100MB_FD;
         }
         if (ecmd.supported & SUPPORTED_1000baseT_Half) {
-            netdev->features |= OFPPF_1GB_HD;
+            netdev->supported |= OFPPF_1GB_HD;
         }
         if (ecmd.supported & SUPPORTED_1000baseT_Full) {
-            netdev->features |= OFPPF_1GB_FD;
+            netdev->supported |= OFPPF_1GB_FD;
         }
-        /* 10Gbps half-duplex doesn't exist... */
         if (ecmd.supported & SUPPORTED_10000baseT_Full) {
-            netdev->features |= OFPPF_10GB_FD;
+            netdev->supported |= OFPPF_10GB_FD;
         }
+        if (ecmd.supported & SUPPORTED_TP) {
+            netdev->supported |= OFPPF_COPPER;
+        }
+        if (ecmd.supported & SUPPORTED_FIBRE) {
+            netdev->supported |= OFPPF_FIBER;
+        }
+        if (ecmd.supported & SUPPORTED_Autoneg) {
+            netdev->supported |= OFPPF_AUTONEG;
+        }
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
+        if (ecmd.supported & SUPPORTED_Pause) {
+            netdev->supported |= OFPPF_PAUSE;
+        }
+        if (ecmd.supported & SUPPORTED_Asym_Pause) {
+            netdev->supported |= OFPPF_PAUSE_ASYM;
+        }
+#endif /* kernel >= 2.6.14 */
 
-        switch (ecmd.speed) {
-        case SPEED_10:
-            netdev->speed = 10;
-            break;
-
-        case SPEED_100:
-            netdev->speed = 100;
-            break;
+        /* Set the advertised features */
+        if (ecmd.advertising & ADVERTISED_10baseT_Half) {
+            netdev->advertised |= OFPPF_10MB_HD;
+        }
+        if (ecmd.advertising & ADVERTISED_10baseT_Full) {
+            netdev->advertised |= OFPPF_10MB_FD;
+        }
+        if (ecmd.advertising & ADVERTISED_100baseT_Half) {
+            netdev->advertised |= OFPPF_100MB_HD;
+        }
+        if (ecmd.advertising & ADVERTISED_100baseT_Full) {
+            netdev->advertised |= OFPPF_100MB_FD;
+        }
+        if (ecmd.advertising & ADVERTISED_1000baseT_Half) {
+            netdev->advertised |= OFPPF_1GB_HD;
+        }
+        if (ecmd.advertising & ADVERTISED_1000baseT_Full) {
+            netdev->advertised |= OFPPF_1GB_FD;
+        }
+        if (ecmd.advertising & ADVERTISED_10000baseT_Full) {
+            netdev->advertised |= OFPPF_10GB_FD;
+        }
+        if (ecmd.advertising & ADVERTISED_TP) {
+            netdev->advertised |= OFPPF_COPPER;
+        }
+        if (ecmd.advertising & ADVERTISED_FIBRE) {
+            netdev->advertised |= OFPPF_FIBER;
+        }
+        if (ecmd.advertising & ADVERTISED_Autoneg) {
+            netdev->advertised |= OFPPF_AUTONEG;
+        }
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
+        if (ecmd.advertising & ADVERTISED_Pause) {
+            netdev->advertised |= OFPPF_PAUSE;
+        }
+        if (ecmd.advertising & ADVERTISED_Asym_Pause) {
+            netdev->advertised |= OFPPF_PAUSE_ASYM;
+        }
+#endif /* kernel >= 2.6.14 */
 
-        case SPEED_1000:
-            netdev->speed = 1000;
-            break;
+        /* Set the current features */
+        if (ecmd.speed == SPEED_10) {
+            netdev->curr = (ecmd.duplex) ? OFPPF_10MB_FD : OFPPF_10MB_HD;
+        }
+        else if (ecmd.speed == SPEED_100) {
+            netdev->curr = (ecmd.duplex) ? OFPPF_100MB_FD : OFPPF_100MB_HD;
+        }
+        else if (ecmd.speed == SPEED_1000) {
+            netdev->curr = (ecmd.duplex) ? OFPPF_1GB_FD : OFPPF_1GB_HD;
+        }
+        else if (ecmd.speed == SPEED_10000) {
+            netdev->curr = OFPPF_10GB_FD;
+        }
 
-        case SPEED_2500:
-            netdev->speed = 2500;
-            break;
+        if (ecmd.port == PORT_TP) {
+            netdev->curr |= OFPPF_COPPER;
+        }
+        else if (ecmd.port == PORT_FIBRE) {
+            netdev->curr |= OFPPF_FIBER;
+        }
 
-        case SPEED_10000:
-            netdev->speed = 10000;
-            break;
+        if (ecmd.autoneg) {
+            netdev->curr |= OFPPF_AUTONEG;
         }
     } else {
         VLOG_DBG("ioctl(SIOCETHTOOL) failed: %s", strerror(errno));
@@ -198,7 +282,7 @@ do_ethtool(struct netdev *netdev)
 }
 
 /* Opens the network device named 'name' (e.g. "eth0") and returns zero if
- * successful, otherwise a positive errno value.  On success, sets '*netdev'
+ * successful, otherwise a positive errno value.  On success, sets '*netdevp'
  * to the new network device, otherwise to null.
  *
  * 'ethertype' may be a 16-bit Ethernet protocol value in host byte order to
@@ -206,34 +290,99 @@ do_ethtool(struct netdev *netdev)
  * the 'enum netdev_pseudo_ethertype' values to receive frames in one of those
  * categories. */
 int
-netdev_open(const char *name, int ethertype, struct netdev **netdev_)
+netdev_open(const char *name, int ethertype, struct netdev **netdevp) 
+{
+    if (!strncmp(name, "tap:", 4)) {
+        return netdev_open_tap(name + 4, netdevp);
+    } else {
+        return do_open_netdev(name, ethertype, -1, netdevp); 
+    }
+}
+
+/* Opens a TAP virtual network device.  If 'name' is a nonnull, non-empty
+ * string, attempts to assign that name to the TAP device (failing if the name
+ * is already in use); otherwise, a name is automatically assigned.  Returns
+ * zero if successful, otherwise a positive errno value.  On success, sets
+ * '*netdevp' to the new network device, otherwise to null.  */
+int
+netdev_open_tap(const char *name, struct netdev **netdevp)
 {
-    int fd;
+    static const char tap_dev[] = "/dev/net/tun";
+    struct ifreq ifr;
+    int error;
+    int tap_fd;
+
+    tap_fd = open(tap_dev, O_RDWR);
+    if (tap_fd < 0) {
+        ofp_error(errno, "opening \"%s\" failed", tap_dev);
+        return errno;
+    }
+
+    memset(&ifr, 0, sizeof ifr);
+    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
+    if (name) {
+        strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
+    }
+    if (ioctl(tap_fd, TUNSETIFF, &ifr) < 0) {
+        int error = errno;
+        ofp_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
+        close(tap_fd);
+        return error;
+    }
+
+    error = set_nonblocking(tap_fd);
+    if (error) {
+        ofp_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
+        close(tap_fd);
+        return error;
+    }
+
+    error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
+                           netdevp);
+    if (error) {
+        close(tap_fd);
+    }
+    return error;
+}
+
+static int
+do_open_netdev(const char *name, int ethertype, int tap_fd,
+               struct netdev **netdev_)
+{
+    int netdev_fd;
     struct sockaddr_ll sll;
     struct ifreq ifr;
     unsigned int ifindex;
     uint8_t etheraddr[ETH_ADDR_LEN];
     struct in6_addr in6;
     int mtu;
+    int txqlen;
+    int hwaddr_family;
     int error;
     struct netdev *netdev;
 
-    *netdev_ = NULL;
     init_netdev();
+    *netdev_ = NULL;
 
     /* Create raw socket. */
-    fd = socket(PF_PACKET, SOCK_RAW,
-                htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
-                      : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
-                      : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
-                      : ethertype));
-    if (fd < 0) {
+    netdev_fd = socket(PF_PACKET, SOCK_RAW,
+                       htons(ethertype == NETDEV_ETH_TYPE_NONE ? 0
+                             : ethertype == NETDEV_ETH_TYPE_ANY ? ETH_P_ALL
+                             : ethertype == NETDEV_ETH_TYPE_802_2 ? ETH_P_802_2
+                             : ethertype));
+    if (netdev_fd < 0) {
         return errno;
     }
 
+    /* Set non-blocking mode. */
+    error = set_nonblocking(netdev_fd);
+    if (error) {
+        goto error_already_set;
+    }
+
     /* Get ethernet device index. */
     strncpy(ifr.ifr_name, name, sizeof ifr.ifr_name);
-    if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
+    if (ioctl(netdev_fd, SIOCGIFINDEX, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCGIFINDEX) on %s device failed: %s",
                  name, strerror(errno));
         goto error;
@@ -244,7 +393,7 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     memset(&sll, 0, sizeof sll);
     sll.sll_family = AF_PACKET;
     sll.sll_ifindex = ifindex;
-    if (bind(fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
+    if (bind(netdev_fd, (struct sockaddr *) &sll, sizeof sll) < 0) {
         VLOG_ERR("bind to %s failed: %s", name, strerror(errno));
         goto error;
     }
@@ -254,40 +403,51 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
          * packets of the requested type on all system interfaces.  We do not
          * want to receive that data, but there is no way to avoid it.  So we
          * must now drain out the receive queue. */
-        error = drain_rcvbuf(fd);
+        error = drain_rcvbuf(netdev_fd);
         if (error) {
             goto error;
         }
     }
 
     /* Get MAC address. */
-    if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) {
+    if (ioctl(netdev_fd, SIOCGIFHWADDR, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCGIFHWADDR) on %s device failed: %s",
                  name, strerror(errno));
         goto error;
     }
-    if (ifr.ifr_hwaddr.sa_family != AF_UNSPEC
-        && ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER) {
+    hwaddr_family = ifr.ifr_hwaddr.sa_family;
+    if (hwaddr_family != AF_UNSPEC && hwaddr_family != ARPHRD_ETHER) {
         VLOG_WARN("%s device has unknown hardware address family %d",
-                  name, (int) ifr.ifr_hwaddr.sa_family);
+                  name, hwaddr_family);
     }
     memcpy(etheraddr, ifr.ifr_hwaddr.sa_data, sizeof etheraddr);
 
     /* Get MTU. */
-    if (ioctl(fd, SIOCGIFMTU, &ifr) < 0) {
+    if (ioctl(netdev_fd, SIOCGIFMTU, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCGIFMTU) on %s device failed: %s",
                  name, strerror(errno));
         goto error;
     }
     mtu = ifr.ifr_mtu;
 
+    /* Get TX queue length. */
+    if (ioctl(netdev_fd, SIOCGIFTXQLEN, &ifr) < 0) {
+        VLOG_ERR("ioctl(SIOCGIFTXQLEN) on %s device failed: %s",
+                 name, strerror(errno));
+        goto error;
+    }
+    txqlen = ifr.ifr_qlen;
+
     get_ipv6_address(name, &in6);
 
     /* Allocate network device. */
     netdev = xmalloc(sizeof *netdev);
     netdev->name = xstrdup(name);
     netdev->ifindex = ifindex;
-    netdev->fd = fd;
+    netdev->txqlen = txqlen;
+    netdev->hwaddr_family = hwaddr_family;
+    netdev->netdev_fd = netdev_fd;
+    netdev->tap_fd = tap_fd < 0 ? netdev_fd : tap_fd;
     memcpy(netdev->etheraddr, etheraddr, sizeof etheraddr);
     netdev->mtu = mtu;
     netdev->in6 = in6;
@@ -296,9 +456,9 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
     do_ethtool(netdev);
 
     /* Save flags to restore at close or exit. */
-    error = get_flags(netdev, &netdev->save_flags);
+    error = get_flags(netdev, netdev_fd, &netdev->save_flags);
     if (error) {
-        goto preset_error;
+        goto error_already_set;
     }
     netdev->changed_flags = 0;
     fatal_signal_block();
@@ -311,8 +471,11 @@ netdev_open(const char *name, int ethertype, struct netdev **netdev_)
 
 error:
     error = errno;
-preset_error:
-    close(fd);
+error_already_set:
+    close(netdev_fd);
+    if (tap_fd >= 0) {
+        close(tap_fd);
+    }
     return error;
 }
 
@@ -335,7 +498,10 @@ netdev_close(struct netdev *netdev)
 
         /* Free. */
         free(netdev->name);
-        close(netdev->fd);
+        close(netdev->netdev_fd);
+        if (netdev->netdev_fd != netdev->tap_fd) {
+            close(netdev->tap_fd);
+        }
         free(netdev);
     }
 }
@@ -346,8 +512,7 @@ static void
 pad_to_minimum_length(struct ofpbuf *buffer)
 {
     if (buffer->size < ETH_TOTAL_MIN) {
-        size_t shortage = ETH_TOTAL_MIN - buffer->size;
-        memset(ofpbuf_put_uninit(buffer, shortage), 0, shortage);
+        ofpbuf_put_zeros(buffer, ETH_TOTAL_MIN - buffer->size);
     }
 }
 
@@ -371,9 +536,8 @@ netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
     assert(buffer->size == 0);
     assert(ofpbuf_tailroom(buffer) >= ETH_TOTAL_MIN);
     do {
-        n_bytes = recv(netdev->fd,
-                       ofpbuf_tail(buffer), ofpbuf_tailroom(buffer),
-                       MSG_DONTWAIT);
+        n_bytes = read(netdev->tap_fd,
+                       ofpbuf_tail(buffer), ofpbuf_tailroom(buffer));
     } while (n_bytes < 0 && errno == EINTR);
     if (n_bytes < 0) {
         if (errno != EAGAIN) {
@@ -399,14 +563,19 @@ netdev_recv(struct netdev *netdev, struct ofpbuf *buffer)
 void
 netdev_recv_wait(struct netdev *netdev)
 {
-    poll_fd_wait(netdev->fd, POLLIN);
+    poll_fd_wait(netdev->tap_fd, POLLIN);
 }
 
 /* Discards all packets waiting to be received from 'netdev'. */
-void
+int
 netdev_drain(struct netdev *netdev)
 {
-    drain_rcvbuf(netdev->fd);
+    if (netdev->tap_fd != netdev->netdev_fd) {
+        drain_fd(netdev->tap_fd, netdev->txqlen);
+        return 0;
+    } else {
+        return drain_rcvbuf(netdev->netdev_fd);
+    }
 }
 
 /* Sends 'buffer' on 'netdev'.  Returns 0 if successful, otherwise a positive
@@ -422,18 +591,9 @@ int
 netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
 {
     ssize_t n_bytes;
-    const struct eth_header *eh;
-
-    /* Pull out the Ethernet header. */
-    if (buffer->size < ETH_HEADER_LEN) {
-        VLOG_WARN_RL(&rl, "cannot send %zu-byte frame on %s",
-                     buffer->size, netdev->name);
-        return EMSGSIZE;
-    }
-    eh = ofpbuf_at_assert(buffer, 0, sizeof *eh);
 
     do {
-        n_bytes = sendto(netdev->fd, buffer->data, buffer->size, 0, NULL, 0);
+        n_bytes = write(netdev->tap_fd, buffer->data, buffer->size);
     } while (n_bytes < 0 && errno == EINTR);
 
     if (n_bytes < 0) {
@@ -467,7 +627,32 @@ netdev_send(struct netdev *netdev, const struct ofpbuf *buffer)
 void
 netdev_send_wait(struct netdev *netdev)
 {
-    poll_fd_wait(netdev->fd, POLLOUT);
+    if (netdev->tap_fd == netdev->netdev_fd) {
+        poll_fd_wait(netdev->tap_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. */
+int
+netdev_set_etheraddr(struct netdev *netdev, 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_hwaddr.sa_family = netdev->hwaddr_family;
+    memcpy(ifr.ifr_hwaddr.sa_data, mac, ETH_ADDR_LEN);
+    if (ioctl(netdev->netdev_fd, SIOCSIFHWADDR, &ifr) < 0) {
+        VLOG_ERR("ioctl(SIOCSIFHWADDR) on %s device failed: %s",
+                 netdev->name, strerror(errno));
+        return errno;
+    }
+    memcpy(netdev->etheraddr, mac, ETH_ADDR_LEN);
+    return 0;
 }
 
 /* Returns a pointer to 'netdev''s MAC address.  The caller must not modify or
@@ -495,14 +680,6 @@ netdev_get_mtu(const struct netdev *netdev)
     return netdev->mtu;
 }
 
-/* Returns the current speed of the network device that 'netdev' represents, in
- * megabits per second, or 0 if the speed is unknown. */
-int
-netdev_get_speed(const struct netdev *netdev) 
-{
-    return netdev->speed;
-}
-
 /* Checks the link status.  Returns 1 or 0 to indicate the link is active 
  * or not, respectively.  Any other return value indicates an error. */
 int
@@ -517,7 +694,7 @@ netdev_get_link_status(const struct netdev *netdev)
 
     memset(&edata, 0, sizeof edata);
     edata.cmd = ETHTOOL_GLINK;
-    if (ioctl(netdev->fd, SIOCETHTOOL, &ifr) == 0) {
+    if (ioctl(netdev->netdev_fd, SIOCETHTOOL, &ifr) == 0) {
         if (edata.data) {
             return 1;
         } else {
@@ -528,12 +705,25 @@ netdev_get_link_status(const struct netdev *netdev)
     return -1;
 }
 
-/* Returns the features supported by 'netdev', as a bitmap of bits from enum
- * ofp_phy_port, in host byte order. */
+/* Returns the features supported by 'netdev' of type 'type', as a bitmap 
+ * of bits from enum ofp_phy_features, in host byte order. */
 uint32_t
-netdev_get_features(const struct netdev *netdev
+netdev_get_features(struct netdev *netdev, int type
 {
-    return netdev->features;
+    do_ethtool(netdev);
+    switch (type) {
+    case NETDEV_FEAT_CURRENT:
+        return netdev->curr;
+    case NETDEV_FEAT_ADVERTISED:
+        return netdev->advertised;
+    case NETDEV_FEAT_SUPPORTED:
+        return netdev->supported;
+    case NETDEV_FEAT_PEER:
+        return netdev->peer;
+    default:
+        VLOG_WARN("Unknown feature type: %d\n", type);
+        return 0;
+    }
 }
 
 /* If 'netdev' has an assigned IPv4 address, sets '*in4' to that address (if
@@ -643,7 +833,7 @@ netdev_get_flags(const struct netdev *netdev, enum netdev_flags *flagsp)
 {
     int error, flags;
 
-    error = get_flags(netdev, &flags);
+    error = get_flags(netdev, netdev->netdev_fd, &flags);
     if (error) {
         return error;
     }
@@ -676,13 +866,13 @@ nd_to_iff_flags(enum netdev_flags nd)
  * will be reverted when 'netdev' is closed or the program exits.  Returns 0 if
  * successful, otherwise a positive errno value. */
 static int
-do_update_flags(struct netdev *netdev, enum netdev_flags off,
+do_update_flags(struct netdev *netdev, int fd, enum netdev_flags off,
                 enum netdev_flags on, bool permanent)
 {
     int old_flags, new_flags;
     int error;
 
-    error = get_flags(netdev, &old_flags);
+    error = get_flags(netdev, fd, &old_flags);
     if (error) {
         return error;
     }
@@ -692,7 +882,7 @@ do_update_flags(struct netdev *netdev, enum netdev_flags off,
         netdev->changed_flags |= new_flags ^ old_flags; 
     }
     if (new_flags != old_flags) {
-        error = set_flags(netdev, new_flags);
+        error = set_flags(netdev, fd, new_flags);
     }
     return error;
 }
@@ -705,7 +895,7 @@ int
 netdev_set_flags(struct netdev *netdev, enum netdev_flags flags,
                  bool permanent)
 {
-    return do_update_flags(netdev, -1, flags, permanent);
+    return do_update_flags(netdev, netdev->netdev_fd, -1, flags, permanent);
 }
 
 /* Turns on the specified 'flags' on 'netdev'.
@@ -716,7 +906,7 @@ int
 netdev_turn_flags_on(struct netdev *netdev, enum netdev_flags flags,
                      bool permanent)
 {
-    return do_update_flags(netdev, 0, flags, permanent);
+    return do_update_flags(netdev, netdev->netdev_fd, 0, flags, permanent);
 }
 
 /* Turns off the specified 'flags' on 'netdev'.
@@ -727,7 +917,7 @@ int
 netdev_turn_flags_off(struct netdev *netdev, enum netdev_flags flags,
                       bool permanent)
 {
-    return do_update_flags(netdev, flags, 0, permanent);
+    return do_update_flags(netdev, netdev->netdev_fd, flags, 0, permanent);
 }
 
 /* Looks up the ARP table entry for 'ip' on 'netdev'.  If one exists and can be
@@ -759,6 +949,27 @@ netdev_arp_lookup(const struct netdev *netdev,
     }
     return retval;
 }
+
+/* Initializes 'svec' with a list of the names of all known network devices. */
+void
+netdev_enumerate(struct svec *svec)
+{
+    struct if_nameindex *names;
+
+    svec_init(svec);
+    names = if_nameindex();
+    if (names) {
+        size_t i;
+
+        for (i = 0; names[i].if_name != NULL; i++) {
+            svec_add(svec, names[i].if_name);
+        }
+        if_freenameindex(names);
+    } else {
+        VLOG_WARN("could not obtain list of network device names: %s",
+                  strerror(errno));
+    }
+}
 \f
 static void restore_all_flags(void *aux);
 
@@ -791,7 +1002,7 @@ restore_flags(struct netdev *netdev)
 
     /* Get current flags. */
     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
-    if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
+    if (ioctl(netdev->netdev_fd, SIOCGIFFLAGS, &ifr) < 0) {
         return errno;
     }
 
@@ -800,7 +1011,7 @@ restore_flags(struct netdev *netdev)
     if ((ifr.ifr_flags ^ netdev->save_flags) & restore_flags) {
         ifr.ifr_flags &= ~restore_flags;
         ifr.ifr_flags |= netdev->save_flags & restore_flags;
-        if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
+        if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
             return errno;
         }
     }
@@ -820,11 +1031,11 @@ restore_all_flags(void *aux UNUSED)
 }
 
 static int
-get_flags(const struct netdev *netdev, int *flags)
+get_flags(const struct netdev *netdev, int fd, int *flags)
 {
     struct ifreq ifr;
     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
-    if (ioctl(netdev->fd, SIOCGIFFLAGS, &ifr) < 0) {
+    if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCGIFFLAGS) on %s device failed: %s",
                  netdev->name, strerror(errno));
         return errno;
@@ -834,12 +1045,12 @@ get_flags(const struct netdev *netdev, int *flags)
 }
 
 static int
-set_flags(struct netdev *netdev, int flags)
+set_flags(struct netdev *netdev, int fd, int flags)
 {
     struct ifreq ifr;
     strncpy(ifr.ifr_name, netdev->name, sizeof ifr.ifr_name);
     ifr.ifr_flags = flags;
-    if (ioctl(netdev->fd, SIOCSIFFLAGS, &ifr) < 0) {
+    if (ioctl(netdev->netdev_fd, SIOCSIFFLAGS, &ifr) < 0) {
         VLOG_ERR("ioctl(SIOCSIFFLAGS) on %s device failed: %s",
                  netdev->name, strerror(errno));
         return errno;