netdev: Implement an abstract interface to network devices.
[sliver-openvswitch.git] / lib / dpif-linux.c
index 8cc213d..e075c8b 100644 (file)
@@ -45,10 +45,15 @@ struct dpif_linux {
     struct dpif dpif;
     int fd;
 
+    /* Used by dpif_linux_get_all_names(). */
+    char *local_ifname;
+    int minor;
+
     /* Change notification. */
     int local_ifindex;          /* Ifindex of local port. */
     struct svec changed_ports;  /* Ports that have changed. */
     struct linux_netdev_notifier port_notifier;
+    bool change_error;
 };
 
 static struct vlog_rate_limit error_rl = VLOG_RATE_LIMIT_INIT(9999, 5);
@@ -69,16 +74,28 @@ dpif_linux_cast(const struct dpif *dpif)
     return CONTAINER_OF(dpif, struct dpif_linux, dpif);
 }
 
-static void
-dpif_linux_run(void)
-{
-    linux_netdev_notifier_run();
-}
-
-static void
-dpif_linux_wait(void)
+static int
+dpif_linux_enumerate(struct svec *all_dps)
 {
-    linux_netdev_notifier_wait();
+    int error;
+    int i;
+
+    error = 0;
+    for (i = 0; i < ODP_MAX; i++) {
+        struct dpif *dpif;
+        char devname[16];
+        int retval;
+
+        sprintf(devname, "dp%d", i);
+        retval = dpif_open(devname, &dpif);
+        if (!retval) {
+            svec_add(all_dps, devname);
+            dpif_close(dpif);
+        } else if (retval != ENODEV && !error) {
+            error = retval;
+        }
+    }
+    return error;
 }
 
 static int
@@ -146,10 +163,21 @@ dpif_linux_close(struct dpif *dpif_)
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
     linux_netdev_notifier_unregister(&dpif->port_notifier);
     svec_destroy(&dpif->changed_ports);
+    free(dpif->local_ifname);
     close(dpif->fd);
     free(dpif);
 }
 
+static int
+dpif_linux_get_all_names(const struct dpif *dpif_, struct svec *all_names)
+{
+    struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+
+    svec_add_nocopy(all_names, xasprintf("dp%d", dpif->minor));
+    svec_add(all_names, dpif->local_ifname);
+    return 0;
+}
+
 static int
 dpif_linux_delete(struct dpif *dpif_)
 {
@@ -246,26 +274,24 @@ static int
 dpif_linux_port_poll(const struct dpif *dpif_, char **devnamep)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
-    int error;
 
-    error = linux_netdev_notifier_get_error(&dpif->port_notifier);
-    if (!error) {
-        if (!dpif->changed_ports.n) {
-            return EAGAIN;
-        }
+    if (dpif->change_error) {
+        dpif->change_error = false;
+        svec_clear(&dpif->changed_ports);
+        return ENOBUFS;
+    } else if (dpif->changed_ports.n) {
         *devnamep = dpif->changed_ports.names[--dpif->changed_ports.n];
+        return 0;
     } else {
-        svec_clear(&dpif->changed_ports);
+        return EAGAIN;
     }
-    return error;
 }
 
 static void
 dpif_linux_port_poll_wait(const struct dpif *dpif_)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
-    if (dpif->changed_ports.n
-        || linux_netdev_notifier_peek_error(&dpif->port_notifier)) {
+    if (dpif->changed_ports.n || dpif->change_error) {
         poll_immediate_wake();
     } else {
         linux_netdev_notifier_wait();
@@ -413,10 +439,12 @@ dpif_linux_recv_wait(struct dpif *dpif_)
 const struct dpif_class dpif_linux_class = {
     "",                         /* This is the default class. */
     "linux",
-    dpif_linux_run,
-    dpif_linux_wait,
+    NULL,
+    NULL,
+    dpif_linux_enumerate,
     dpif_linux_open,
     dpif_linux_close,
+    dpif_linux_get_all_names,
     dpif_linux_delete,
     dpif_linux_get_stats,
     dpif_linux_get_drop_frags,
@@ -628,6 +656,7 @@ static int
 finish_open(struct dpif *dpif_, const char *local_ifname)
 {
     struct dpif_linux *dpif = dpif_linux_cast(dpif_);
+    dpif->local_ifname = strdup(local_ifname);
     dpif->local_ifindex = if_nametoindex(local_ifname);
     if (!dpif->local_ifindex) {
         int error = errno;
@@ -679,8 +708,11 @@ open_minor(int minor, struct dpif **dpifp)
             free(name);
 
             dpif->fd = fd;
+            dpif->local_ifname = NULL;
+            dpif->minor = minor;
             dpif->local_ifindex = 0;
             svec_init(&dpif->changed_ports);
+            dpif->change_error = false;
             *dpifp = &dpif->dpif;
         } else {
             free(dpif);
@@ -699,15 +731,19 @@ dpif_linux_port_changed(const struct linux_netdev_change *change, void *dpif_)
 {
     struct dpif_linux *dpif = dpif_;
 
-    if (change->master_ifindex == dpif->local_ifindex
-        && (change->nlmsg_type == RTM_NEWLINK
-            || change->nlmsg_type == RTM_DELLINK))
-    {
-        /* Our datapath changed, either adding a new port or deleting an
-         * existing one. */
-        if (!svec_contains(&dpif->changed_ports, change->ifname)) {
-            svec_add(&dpif->changed_ports, change->ifname);
-            svec_sort(&dpif->changed_ports);
+    if (change) {
+        if (change->master_ifindex == dpif->local_ifindex
+            && (change->nlmsg_type == RTM_NEWLINK
+                || change->nlmsg_type == RTM_DELLINK))
+        {
+            /* Our datapath changed, either adding a new port or deleting an
+             * existing one. */
+            if (!svec_contains(&dpif->changed_ports, change->ifname)) {
+                svec_add(&dpif->changed_ports, change->ifname);
+                svec_sort(&dpif->changed_ports);
+            }
         }
+    } else {
+        dpif->change_error = true;
     }
 }