>From bc14ecde8258332f627308e3bc688c62ae434229 Mon Sep 17 00:00:00 2001
From: Laine Stump <laine@laine.org>
Date: Sun, 26 Oct 2014 12:08:23 -0400
Subject: [PATCH] changes to squash into 'util: Functions to update host
 network device's multicast filter'

This patch addresses all issues I raised in my review. Please test it
and let me know if you approve. If so, I will squash this patch in and
push the result.
---
 src/util/virmacaddr.h |  2 +-
 src/util/virnetdev.c  | 73 +++++++++++++++++++--------------------------------
 2 files changed, 28 insertions(+), 47 deletions(-)

diff --git a/src/util/virmacaddr.h b/src/util/virmacaddr.h
index 72a285a..ae26867 100644
--- a/src/util/virmacaddr.h
+++ b/src/util/virmacaddr.h
@@ -27,7 +27,7 @@
 # include "internal.h"
 
 # define VIR_MAC_BUFLEN 6
-#define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)
+# define VIR_MAC_HEXLEN (VIR_MAC_BUFLEN * 2)
 # define VIR_MAC_PREFIX_BUFLEN 3
 # define VIR_MAC_STRING_BUFLEN (VIR_MAC_BUFLEN * 3)
 
diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c
index 1410cfe..cd564d8 100644
--- a/src/util/virnetdev.c
+++ b/src/util/virnetdev.c
@@ -56,17 +56,17 @@
 
 VIR_LOG_INIT("util.netdev");
 
-# define PROC_NET_DEV_MCAST "/proc/net/dev_mcast"
-# define MAX_MCAST_SIZE 50*14336
-# define VIR_MCAST_NAME_LEN (IFNAMSIZ + 1)
-# define VIR_MCAST_INDEX_TOKEN_IDX 0
-# define VIR_MCAST_NAME_TOKEN_IDX 1
-# define VIR_MCAST_USERS_TOKEN_IDX 2
-# define VIR_MCAST_GLOBAL_TOKEN_IDX 3
-# define VIR_MCAST_ADDR_TOKEN_IDX 4
-# define VIR_MCAST_NUM_TOKENS 5
-# define VIR_MCAST_TOKEN_DELIMS " \n"
-# define VIR_MCAST_ADDR_LEN (VIR_MAC_HEXLEN + 1)
+#define PROC_NET_DEV_MCAST "/proc/net/dev_mcast"
+#define MAX_MCAST_SIZE 50*14336
+#define VIR_MCAST_NAME_LEN (IFNAMSIZ + 1)
+#define VIR_MCAST_INDEX_TOKEN_IDX 0
+#define VIR_MCAST_NAME_TOKEN_IDX 1
+#define VIR_MCAST_USERS_TOKEN_IDX 2
+#define VIR_MCAST_GLOBAL_TOKEN_IDX 3
+#define VIR_MCAST_ADDR_TOKEN_IDX 4
+#define VIR_MCAST_NUM_TOKENS 5
+#define VIR_MCAST_TOKEN_DELIMS " \n"
+#define VIR_MCAST_ADDR_LEN (VIR_MAC_HEXLEN + 1)
 
 typedef struct _virNetDevMcastEntry virNetDevMcastEntry;
 typedef virNetDevMcastEntry *virNetDevMcastEntryPtr;
@@ -78,9 +78,9 @@ struct _virNetDevMcastEntry  {
         virMacAddr macaddr;
 };
 
-typedef struct _virNetDevMcast virNetDevMcast;
-typedef virNetDevMcast *virNetDevMcastPtr;
-struct _virNetDevMcast {
+typedef struct _virNetDevMcastList virNetDevMcastList;
+typedef virNetDevMcastList *virNetDevMcastListPtr;
+struct _virNetDevMcastList {
     size_t nentries;
     virNetDevMcastEntryPtr *entries;
 };
@@ -1996,7 +1996,6 @@ int virNetDevAddMulti(const char *ifname,
     }
 
     ret = 0;
-
  cleanup:
     VIR_FORCE_CLOSE(fd);
     return ret;
@@ -2046,7 +2045,6 @@ int virNetDevDelMulti(const char *ifname,
     }
 
     ret = 0;
-
  cleanup:
     VIR_FORCE_CLOSE(fd);
     return ret;
@@ -2092,9 +2090,7 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
                     return -1;
 
                 }
-
                 mcast->index = num;
-
                 break;
             case VIR_MCAST_NAME_TOKEN_IDX:
                 if (virStrncpy(mcast->name, token, strlen(token),
@@ -2104,7 +2100,6 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
                                          buf);
                     return -1;
                 }
-
                 break;
             case VIR_MCAST_USERS_TOKEN_IDX:
                 if (virStrToLong_i(token, &endptr, 10, &num) < 0) {
@@ -2114,9 +2109,7 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
                     return -1;
 
                 }
-
                 mcast->users = num;
-
                 break;
             case VIR_MCAST_GLOBAL_TOKEN_IDX:
                 if (virStrToLong_i(token, &endptr, 10, &num) < 0) {
@@ -2126,9 +2119,7 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
                     return -1;
 
                 }
-
                 mcast->global = num;
-
                 break;
             case VIR_MCAST_ADDR_TOKEN_IDX:
                 if (virMacAddrParseHex((const char*)token,
@@ -2137,41 +2128,35 @@ static int virNetDevParseMcast(char *buf, virNetDevMcastEntryPtr mcast)
                                          _("Failed to parse MAC address from '%s'"),
                                          buf);
                 }
-
                 break;
             default:
                 break;
         }
     }
-
     return 0;
 }
 
 
-static void virNetDevMcastEntryListFree(size_t nentries,
-                                        virNetDevMcastEntryPtr *entries)
+static void virNetDevMcastListClear(virNetDevMcastListPtr mcast)
 {
     size_t i;
 
-    if (entries) {
-        for (i = 0; i < nentries; i++)
-            VIR_FREE(entries[i]);
-
-        VIR_FREE(entries);
-    }
+    for (i = 0; i < mcast->nentries; i++)
+       VIR_FREE(mcast->entries[i]);
+    VIR_FREE(mcast->entries);
+    mcast->nentries = 0;
 }
 
 
-static int virNetDevGetMcast(const char *ifname,
-                             virNetDevMcastPtr mcast)
+static int virNetDevGetMcastList(const char *ifname,
+                                 virNetDevMcastListPtr mcast)
 {
     char *cur = NULL;
     char *buf = NULL;
     char *next = NULL;
     int ret = -1, len;
     virNetDevMcastEntryPtr entry = NULL;
-    virNetDevMcastEntryPtr *entries = NULL;
-    size_t nentries = 0;
+
     mcast->entries = NULL;
     mcast->nentries = 0;
 
@@ -2180,7 +2165,6 @@ static int virNetDevGetMcast(const char *ifname,
         goto cleanup;
 
     cur = buf;
-
     while (cur) {
         if (!entry) {
             if (VIR_ALLOC(entry))
@@ -2199,7 +2183,7 @@ static int virNetDevGetMcast(const char *ifname,
         /* Only return global multicast MAC addresses for
          * specified interface */
         if (entry->global && STREQ(ifname, entry->name)) {
-            if (VIR_APPEND_ELEMENT(entries, nentries, entry))
+            if (VIR_APPEND_ELEMENT(mcast->entries, mcast->nentries, entry))
                  goto cleanup;
 
             entry = NULL;
@@ -2210,12 +2194,10 @@ static int virNetDevGetMcast(const char *ifname,
         cur = next && ((next - buf) < len) ? next : NULL;
     }
 
-    mcast->nentries = nentries;
-    mcast->entries = entries;
     ret = 0;
  cleanup:
     if (ret < 0)
-        virNetDevMcastEntryListFree(nentries, entries);
+        virNetDevMcastListClear(mcast);
 
     VIR_FREE(entry);
 
@@ -2235,11 +2217,11 @@ static int virNetDevGetMulticastTable(const char *ifname,
 {
     size_t i;
     int ret = -1;
-    virNetDevMcast mcast;
+    virNetDevMcastList mcast;
     filter->multicast.nTable = 0;
     filter->multicast.table = NULL;
 
-    if (virNetDevGetMcast(ifname, &mcast) < 0)
+    if (virNetDevGetMcastList(ifname, &mcast) < 0)
         goto cleanup;
 
     if (mcast.nentries > 0) {
@@ -2256,7 +2238,7 @@ static int virNetDevGetMulticastTable(const char *ifname,
 
     ret = 0;
  cleanup:
-    virNetDevMcastEntryListFree(mcast.nentries, mcast.entries);
+    virNetDevMcastListClear(&mcast);
 
     return ret;
 }
@@ -2318,6 +2300,5 @@ int virNetDevGetRxFilter(const char *ifname,
     }
 
     *filter = fil;
-
     return ret;
 }
-- 
1.9.3

