[ovs-discuss] [PATCH 06/20] netdev: Get rid of netdev_open_tap().

Ben Pfaff blp at nicira.com
Fri Jul 24 21:19:49 UTC 2009


netdev_open() can always be used in place of netdev_open_tap().  The
former is going to be generalized to support pluggable network device
types, so it makes sense to use it everywhere.
---
 lib/dpif-netdev.c |    4 ++-
 lib/netdev.c      |   80 +++++++++++++++++++++++-----------------------------
 lib/netdev.h      |    1 -
 3 files changed, 38 insertions(+), 47 deletions(-)

diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c
index 6bf92f2..4ff1a42 100644
--- a/lib/dpif-netdev.c
+++ b/lib/dpif-netdev.c
@@ -377,7 +377,9 @@ do_add_port(struct dp_netdev *dp, const char *devname, uint16_t flags,
     if (!internal) {
         error = netdev_open(devname, NETDEV_ETH_TYPE_ANY, &netdev);
     } else {
-        error = netdev_open_tap(devname, &netdev);
+        char *tapname = xasprintf("tap:%s", devname);
+        error = netdev_open(tapname, NETDEV_ETH_TYPE_ANY, &netdev);
+        free(tapname);
     }
     if (error) {
         return error;
diff --git a/lib/netdev.c b/lib/netdev.c
index b8e936d..8be1c80 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -335,59 +335,49 @@ do_get_features(struct netdev *netdev,
 int
 netdev_open(const char *name, int ethertype, struct netdev **netdevp) 
 {
-    if (!strncmp(name, "tap:", 4)) {
-        return netdev_open_tap(name + 4, netdevp);
-    } else {
+    if (strncmp(name, "tap:", 4)) {
         return do_open_netdev(name, ethertype, -1, netdevp); 
-    }
-}
+    } else {
+        static const char tap_dev[] = "/dev/net/tun";
+        struct ifreq ifr;
+        int error;
+        int tap_fd;
 
-/* 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)
-{
-    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) {
+            ovs_error(errno, "opening \"%s\" failed", tap_dev);
+            return errno;
+        }
 
-    tap_fd = open(tap_dev, O_RDWR);
-    if (tap_fd < 0) {
-        ovs_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;
+            ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
+            close(tap_fd);
+            return error;
+        }
 
-    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;
-        ovs_error(error, "ioctl(TUNSETIFF) on \"%s\" failed", tap_dev);
-        close(tap_fd);
-        return error;
-    }
+        error = set_nonblocking(tap_fd);
+        if (error) {
+            ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
+            close(tap_fd);
+            return error;
+        }
 
-    error = set_nonblocking(tap_fd);
-    if (error) {
-        ovs_error(error, "set_nonblocking on \"%s\" failed", tap_dev);
-        close(tap_fd);
+        error = do_open_netdev(ifr.ifr_name, NETDEV_ETH_TYPE_NONE, tap_fd,
+                               netdevp);
+        if (error) {
+            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_)
diff --git a/lib/netdev.h b/lib/netdev.h
index 0c7f1cc..b37d0af 100644
--- a/lib/netdev.h
+++ b/lib/netdev.h
@@ -75,7 +75,6 @@ struct netdev_stats {
 struct netdev;
 
 int netdev_open(const char *name, int ethertype, struct netdev **);
-int netdev_open_tap(const char *name, struct netdev **);
 void netdev_close(struct netdev *);
 
 int netdev_recv(struct netdev *, struct ofpbuf *);
-- 
1.6.3.3





More information about the discuss mailing list