From efacbce62f4ca3baf305441641ee35aa5b657442 Mon Sep 17 00:00:00 2001
From: Ben Pfaff <blp@nicira.com>
Date: Mon, 23 Nov 2009 11:09:19 -0800
Subject: [PATCH] dpif: New function dpif_create_and_open().

This function combines what dpif_create() and dpif_open() do.  It allows
us to factor a tiny amount of code out of the vswitch, but more importantly
this function is also useful in the following commit.
---
 lib/dpif.c        | 24 +++++++++++++++++++++++-
 lib/dpif.h        |  1 +
 vswitchd/bridge.c | 15 +++------------
 3 files changed, 27 insertions(+), 13 deletions(-)

diff --git a/lib/dpif.c b/lib/dpif.c
index 649c2464..793eaa11 100644
--- a/lib/dpif.c
+++ b/lib/dpif.c
@@ -169,13 +169,35 @@ dpif_open(const char *name, struct dpif **dpifp)
 /* Tries to create and open a new datapath with the given 'name'.  Will fail if
  * a datapath named 'name' already exists.  Returns 0 if successful, otherwise
  * a positive errno value.  On success stores a pointer to the datapath in
- * '*dpifp', otherwise a null pointer.*/
+ * '*dpifp', otherwise a null pointer. */
 int
 dpif_create(const char *name, struct dpif **dpifp)
 {
     return do_open(name, true, dpifp);
 }
 
+/* Tries to open a datapath with the given 'name', creating it if it does not
+ * exist.  Returns 0 if successful, otherwise a positive errno value.  On
+ * success stores a pointer to the datapath in '*dpifp', otherwise a null
+ * pointer. */
+int
+dpif_create_and_open(const char *name, struct dpif **dpifp)
+{
+    int error;
+
+    error = dpif_create(name, dpifp);
+    if (error == EEXIST || error == EBUSY) {
+        error = dpif_open(name, dpifp);
+        if (error) {
+            VLOG_WARN("datapath %s already exists but cannot be opened: %s",
+                      name, strerror(error));
+        }
+    } else if (error) {
+        VLOG_WARN("failed to create datapath %s: %s", name, strerror(error));
+    }
+    return error;
+}
+
 /* Closes and frees the connection to 'dpif'.  Does not destroy the datapath
  * itself; call dpif_delete() first, instead, if that is desirable. */
 void
diff --git a/lib/dpif.h b/lib/dpif.h
index 216c0996..1d109c2d 100644
--- a/lib/dpif.h
+++ b/lib/dpif.h
@@ -37,6 +37,7 @@ int dp_enumerate(struct svec *);
 
 int dpif_open(const char *name, struct dpif **);
 int dpif_create(const char *name, struct dpif **);
+int dpif_create_and_open(const char *name, struct dpif **);
 void dpif_close(struct dpif *);
 
 const char *dpif_name(const struct dpif *);
diff --git a/vswitchd/bridge.c b/vswitchd/bridge.c
index 5494dea3..e6ea1a74 100644
--- a/vswitchd/bridge.c
+++ b/vswitchd/bridge.c
@@ -968,21 +968,12 @@ bridge_create(const char *name)
     assert(!bridge_lookup(name));
     br = xcalloc(1, sizeof *br);
 
-    error = dpif_create(name, &br->dpif);
-    if (error == EEXIST || error == EBUSY) {
-        error = dpif_open(name, &br->dpif);
-        if (error) {
-            VLOG_ERR("datapath %s already exists but cannot be opened: %s",
-                     name, strerror(error));
-            free(br);
-            return NULL;
-        }
-        dpif_flow_flush(br->dpif);
-    } else if (error) {
-        VLOG_ERR("failed to create datapath %s: %s", name, strerror(error));
+    error = dpif_create_and_open(name, &br->dpif);
+    if (error) {
         free(br);
         return NULL;
     }
+    dpif_flow_flush(br->dpif);
 
     error = ofproto_create(name, &bridge_ofhooks, br, &br->ofproto);
     if (error) {
-- 
2.30.2