[ovs-dev] [PATCH 06/16] netdev: Add function netdev_is_open().

Jesse Gross jesse at nicira.com
Tue Apr 13 14:41:08 UTC 2010


Add netdev_is_open(), which checks to see if a given netdev is
currently open.  It will be used to assist in cleaning up old ports
that are no longer in use.
---
 lib/netdev-provider.h |    2 +-
 lib/netdev.c          |   19 ++++++++++++++-----
 lib/netdev.h          |    1 +
 3 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/netdev-provider.h b/lib/netdev-provider.h
index e1c18cc..3cd144f 100644
--- a/lib/netdev-provider.h
+++ b/lib/netdev-provider.h
@@ -69,7 +69,7 @@ static inline void netdev_dev_assert_class(const struct netdev_dev *netdev_dev,
  * implementations. */
 struct netdev {
     struct netdev_dev *netdev_dev;   /* Parent netdev_dev. */
-    struct list node;                /* Element in global list. */
+    struct shash_node *node;         /* Pointer to element in global map. */
 
     enum netdev_flags save_flags;    /* Initial device flags. */
     enum netdev_flags changed_flags; /* Flags that we changed. */
diff --git a/lib/netdev.c b/lib/netdev.c
index 99b5d24..9e442e2 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -53,7 +53,7 @@ static struct shash netdev_classes = SHASH_INITIALIZER(&netdev_classes);
 static struct shash netdev_dev_shash = SHASH_INITIALIZER(&netdev_dev_shash);
 
 /* All open network devices. */
-static struct list netdev_list = LIST_INITIALIZER(&netdev_list);
+static struct shash netdev_shash = SHASH_INITIALIZER(&netdev_shash);
 
 /* This is set pretty low because we probably won't learn anything from the
  * additional log messages. */
@@ -434,6 +434,14 @@ netdev_exists(const char *name)
     }
 }
 
+/* Returns true if a network device named 'name' is currently opened,
+ * otherwise false. */
+bool
+netdev_is_open(const char *name)
+{
+    return !!shash_find_data(&netdev_shash, name);
+}
+
 /*  Clears 'svec' and enumerates the names of all known network devices. */
 int
 netdev_enumerate(struct svec *svec)
@@ -1087,7 +1095,7 @@ netdev_init(struct netdev *netdev, struct netdev_dev *netdev_dev)
 {
     memset(netdev, 0, sizeof *netdev);
     netdev->netdev_dev = netdev_dev;
-    list_push_back(&netdev_list, &netdev->node);
+    netdev->node = shash_add(&netdev_shash, netdev_get_name(netdev), netdev);
 }
 
 /* Undoes the results of initialization.
@@ -1101,11 +1109,11 @@ netdev_uninit(struct netdev *netdev, bool close)
 {
     /* Restore flags that we changed, if any. */
     int error = restore_flags(netdev);
-    list_remove(&netdev->node);
     if (error) {
         VLOG_WARN("failed to restore network device flags on %s: %s",
                   netdev_get_name(netdev), strerror(error));
     }
+    shash_delete(&netdev_shash, netdev->node);
 
     if (close) {
         netdev_get_dev(netdev)->netdev_class->close(netdev);
@@ -1292,8 +1300,9 @@ restore_flags(struct netdev *netdev)
 static void
 close_all_netdevs(void *aux OVS_UNUSED)
 {
-    struct netdev *netdev, *next;
-    LIST_FOR_EACH_SAFE(netdev, next, struct netdev, node, &netdev_list) {
+    struct shash_node *node, *next;
+    SHASH_FOR_EACH_SAFE(node, next, &netdev_shash) {
+        struct netdev *netdev = node->data;
         netdev_close(netdev);
     }
 }
diff --git a/lib/netdev.h b/lib/netdev.h
index 27eb82e..63414ae 100644
--- a/lib/netdev.h
+++ b/lib/netdev.h
@@ -105,6 +105,7 @@ int netdev_reconfigure(struct netdev *, const struct shash *args);
 void netdev_close(struct netdev *);
 
 bool netdev_exists(const char *name);
+bool netdev_is_open(const char *name);
 
 int netdev_enumerate(struct svec *);
 
-- 
1.6.3.3





More information about the dev mailing list