From 91779071abffe3b6d6243378ff06a179bf39d69a Mon Sep 17 00:00:00 2001
From: Ethan Jackson <ethan@nicira.com>
Date: Tue, 18 Jun 2013 16:44:23 -0700
Subject: [PATCH] lacp: Reference count 'struct lacp'.

Signed-off-by: Ethan Jackson <ethan@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
---
 lib/lacp.c             | 23 +++++++++++++++++++++--
 lib/lacp.h             |  3 ++-
 ofproto/ofproto-dpif.c |  4 ++--
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/lib/lacp.c b/lib/lacp.c
index 8bc115dd6..9daca3bd1 100644
--- a/lib/lacp.c
+++ b/lib/lacp.c
@@ -100,6 +100,8 @@ struct lacp {
     bool fast;               /* True if using fast probe interval. */
     bool negotiated;         /* True if LACP negotiations were successful. */
     bool update;             /* True if lacp_update() needs to be called. */
+
+    int ref_cnt;
 };
 
 struct slave {
@@ -197,14 +199,31 @@ lacp_create(void)
     lacp = xzalloc(sizeof *lacp);
     hmap_init(&lacp->slaves);
     list_push_back(&all_lacps, &lacp->node);
+    lacp->ref_cnt = 1;
+    return lacp;
+}
+
+struct lacp *
+lacp_ref(const struct lacp *lacp_)
+{
+    struct lacp *lacp = CONST_CAST(struct lacp *, lacp_);
+    if (lacp) {
+        ovs_assert(lacp->ref_cnt > 0);
+        lacp->ref_cnt++;
+    }
     return lacp;
 }
 
 /* Destroys 'lacp' and its slaves. Does nothing if 'lacp' is NULL. */
 void
-lacp_destroy(struct lacp *lacp)
+lacp_unref(struct lacp *lacp)
 {
-    if (lacp) {
+    if (!lacp) {
+        return;
+    }
+
+    ovs_assert(lacp->ref_cnt > 0);
+    if (!--lacp->ref_cnt) {
         struct slave *slave, *next;
 
         HMAP_FOR_EACH_SAFE (slave, next, node, &lacp->slaves) {
diff --git a/lib/lacp.h b/lib/lacp.h
index 399b39e30..89b0e0ae7 100644
--- a/lib/lacp.h
+++ b/lib/lacp.h
@@ -39,7 +39,8 @@ struct lacp_settings {
 
 void lacp_init(void);
 struct lacp *lacp_create(void);
-void lacp_destroy(struct lacp *);
+void lacp_unref(struct lacp *);
+struct lacp *lacp_ref(const struct lacp *);
 
 void lacp_configure(struct lacp *, const struct lacp_settings *);
 bool lacp_is_active(const struct lacp *);
diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c
index 4a6852b1a..a48da130c 100644
--- a/ofproto/ofproto-dpif.c
+++ b/ofproto/ofproto-dpif.c
@@ -2245,7 +2245,7 @@ bundle_destroy(struct ofbundle *bundle)
     hmap_remove(&ofproto->bundles, &bundle->hmap_node);
     free(bundle->name);
     free(bundle->trunks);
-    lacp_destroy(bundle->lacp);
+    lacp_unref(bundle->lacp);
     bond_destroy(bundle->bond);
     free(bundle);
 }
@@ -2309,7 +2309,7 @@ bundle_set(struct ofproto *ofproto_, void *aux,
         }
         lacp_configure(bundle->lacp, s->lacp);
     } else {
-        lacp_destroy(bundle->lacp);
+        lacp_unref(bundle->lacp);
         bundle->lacp = NULL;
     }
 
-- 
2.47.0