[ovs-dev] [PATCH 12/16] netdev-linux: Check notifications are for netdev-linux device.

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


When receiving a change notification from rtnetlink we checked whether
a netdev of that name existed and if so tried to handle it.  This also
checks that the type of the device is one handled by netdev-linux.
---
 lib/netdev-linux.c    |   27 +++++++++++++++++++--------
 lib/netdev-provider.h |    1 +
 lib/netdev.c          |    7 +++++++
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c
index 736b588..2fc5b09 100644
--- a/lib/netdev-linux.c
+++ b/lib/netdev-linux.c
@@ -175,18 +175,21 @@ static int get_stats_via_proc(const char *netdev_name, struct netdev_stats *stat
 static struct netdev_dev_linux *
 netdev_dev_linux_cast(const struct netdev_dev *netdev_dev)
 {
-    const char *type = netdev_dev_get_type(netdev_dev);
-    assert(!strcmp(type, "system") || !strcmp(type, "tap")
-            || !strcmp(type, "gre"));
+    const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
+    assert(netdev_class == &netdev_linux_class
+            || netdev_class == &netdev_tap_class
+            || netdev_class == &netdev_gre_class);
     return CONTAINER_OF(netdev_dev, struct netdev_dev_linux, netdev_dev);
 }
 
 static struct netdev_linux *
 netdev_linux_cast(const struct netdev *netdev)
 {
-    const char *type = netdev_get_type(netdev);
-    assert(!strcmp(type, "system") || !strcmp(type, "tap")
-            || !strcmp(type, "gre"));
+    struct netdev_dev *netdev_dev = netdev_get_dev(netdev);
+    const struct netdev_class *netdev_class = netdev_dev_get_class(netdev_dev);
+    assert(netdev_class == &netdev_linux_class
+            || netdev_class == &netdev_tap_class
+            || netdev_class == &netdev_gre_class);
     return CONTAINER_OF(netdev, struct netdev_linux, netdev);
 }
 
@@ -224,8 +227,16 @@ netdev_linux_cache_cb(const struct rtnetlink_change *change,
     if (change) {
         struct netdev_dev *base_dev = netdev_dev_from_name(change->ifname);
         if (base_dev) {
-            dev = netdev_dev_linux_cast(base_dev);
-            dev->cache_valid = 0;
+            const struct netdev_class *netdev_class =
+                                                netdev_dev_get_class(base_dev);
+
+            if (netdev_class == &netdev_linux_class
+                || netdev_class == &netdev_tap_class
+                || netdev_class == &netdev_gre_class) {
+
+                dev = netdev_dev_linux_cast(base_dev);
+                dev->cache_valid = 0;
+            }
         }
     } else {
         struct shash device_shash;
diff --git a/lib/netdev-provider.h b/lib/netdev-provider.h
index 3cd144f..466afdd 100644
--- a/lib/netdev-provider.h
+++ b/lib/netdev-provider.h
@@ -52,6 +52,7 @@ void netdev_dev_init(struct netdev_dev *, const char *name,
                      const struct netdev_class *);
 void netdev_dev_uninit(struct netdev_dev *, bool destroy);
 const char *netdev_dev_get_type(const struct netdev_dev *);
+const struct netdev_class *netdev_dev_get_class(const struct netdev_dev *);
 const char *netdev_dev_get_name(const struct netdev_dev *);
 struct netdev_dev *netdev_dev_from_name(const char *name);
 void netdev_dev_get_devices(const struct netdev_class *,
diff --git a/lib/netdev.c b/lib/netdev.c
index 9e442e2..7913fbd 100644
--- a/lib/netdev.c
+++ b/lib/netdev.c
@@ -1050,6 +1050,13 @@ netdev_dev_get_type(const struct netdev_dev *netdev_dev)
     return netdev_dev->netdev_class->type;
 }
 
+/* Returns the class associated with 'netdev_dev'. */
+const struct netdev_class *
+netdev_dev_get_class(const struct netdev_dev *netdev_dev)
+{
+    return netdev_dev->netdev_class;
+}
+
 /* Returns the name of 'netdev_dev'.
  *
  * The caller must not free the returned value. */
-- 
1.6.3.3





More information about the dev mailing list