0a7476
From f6e6fee82c36159f5f4b52c3926c95b1f6e40e5d Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <f6e6fee82c36159f5f4b52c3926c95b1f6e40e5d@dist-git>
0a7476
From: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
0a7476
Date: Tue, 30 Apr 2019 18:00:59 +0100
0a7476
Subject: [PATCH] nwfilter: fix adding std MAC and IP values to filter binding
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
Commit d1a7c08eb changed filter instantiation code to ignore MAC and IP
0a7476
variables explicitly specified for filter binding. It just replaces
0a7476
explicit values with values associated with the binding. Before the
0a7476
commit virNWFilterCreateVarsFrom was used so that explicit value
0a7476
take precedence. Let's bring old behavior back.
0a7476
0a7476
This is useful. For example if domain has two interfaces it makes
0a7476
sense to list both mac adresses in MAC var of every interface
0a7476
filterref. So that if guest make a bond of these interfaces
0a7476
and start sending frames with one of the mac adresses from
0a7476
both interfaces we can pass outgress traffic from both
0a7476
interfaces too.
0a7476
0a7476
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
0a7476
Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
0a7476
(cherry picked from commit 01e11ebcb6e8f24662b7c67b70134c192785691c)
0a7476
0a7476
https://bugzilla.redhat.com/show_bug.cgi?id=1691358
0a7476
0a7476
Message-Id: <20190430170059.25891-1-berrange@redhat.com>
0a7476
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
0a7476
---
0a7476
 src/nwfilter/nwfilter_gentech_driver.c | 92 +++++++++-----------------
0a7476
 1 file changed, 32 insertions(+), 60 deletions(-)
0a7476
0a7476
diff --git a/src/nwfilter/nwfilter_gentech_driver.c b/src/nwfilter/nwfilter_gentech_driver.c
0a7476
index e5dea91f83..ece5d28f41 100644
0a7476
--- a/src/nwfilter/nwfilter_gentech_driver.c
0a7476
+++ b/src/nwfilter/nwfilter_gentech_driver.c
0a7476
@@ -128,60 +128,6 @@ virNWFilterRuleInstFree(virNWFilterRuleInstPtr inst)
0a7476
 }
0a7476
 
0a7476
 
0a7476
-/**
0a7476
- * virNWFilterVarHashmapAddStdValues:
0a7476
- * @tables: pointer to hash tabel to add values to
0a7476
- * @macaddr: The string of the MAC address to add to the hash table,
0a7476
- *    may be NULL
0a7476
- * @ipaddr: The string of the IP address to add to the hash table;
0a7476
- *    may be NULL
0a7476
- *
0a7476
- * Returns 0 in case of success, -1 in case an error happened with
0a7476
- * error having been reported.
0a7476
- *
0a7476
- * Adds a couple of standard keys (MAC, IP) to the hash table.
0a7476
- */
0a7476
-static int
0a7476
-virNWFilterVarHashmapAddStdValues(virHashTablePtr table,
0a7476
-                                  const char *macaddr,
0a7476
-                                  const virNWFilterVarValue *ipaddr)
0a7476
-{
0a7476
-    virNWFilterVarValue *val;
0a7476
-
0a7476
-    if (macaddr) {
0a7476
-        val = virNWFilterVarValueCreateSimpleCopyValue(macaddr);
0a7476
-        if (!val)
0a7476
-            return -1;
0a7476
-
0a7476
-        if (virHashUpdateEntry(table,
0a7476
-                               NWFILTER_STD_VAR_MAC,
0a7476
-                               val) < 0) {
0a7476
-            virNWFilterVarValueFree(val);
0a7476
-            virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
-                           "%s", _("Could not add variable 'MAC' to hashmap"));
0a7476
-            return -1;
0a7476
-        }
0a7476
-    }
0a7476
-
0a7476
-    if (ipaddr) {
0a7476
-        val = virNWFilterVarValueCopy(ipaddr);
0a7476
-        if (!val)
0a7476
-            return -1;
0a7476
-
0a7476
-        if (virHashUpdateEntry(table,
0a7476
-                               NWFILTER_STD_VAR_IP,
0a7476
-                               val) < 0) {
0a7476
-            virNWFilterVarValueFree(val);
0a7476
-            virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
-                           "%s", _("Could not add variable 'IP' to hashmap"));
0a7476
-            return -1;
0a7476
-        }
0a7476
-    }
0a7476
-
0a7476
-    return 0;
0a7476
-}
0a7476
-
0a7476
-
0a7476
 /**
0a7476
  * Convert a virHashTable into a string of comma-separated
0a7476
  * variable names.
0a7476
@@ -707,6 +653,28 @@ virNWFilterDoInstantiate(virNWFilterTechDriverPtr techdriver,
0a7476
 }
0a7476
 
0a7476
 
0a7476
+static int
0a7476
+virNWFilterVarHashmapAddStdValue(virHashTablePtr table,
0a7476
+                                 const char *var,
0a7476
+                                 const char *value)
0a7476
+{
0a7476
+    virNWFilterVarValue *val;
0a7476
+
0a7476
+    if (virHashLookup(table, var))
0a7476
+        return 0;
0a7476
+
0a7476
+    if (!(val = virNWFilterVarValueCreateSimpleCopyValue(value)))
0a7476
+        return -1;
0a7476
+
0a7476
+    if (virHashAddEntry(table, var, val) < 0) {
0a7476
+        virNWFilterVarValueFree(val);
0a7476
+        return -1;
0a7476
+    }
0a7476
+
0a7476
+    return 0;
0a7476
+}
0a7476
+
0a7476
+
0a7476
 /*
0a7476
  * Call this function while holding the NWFilter filter update lock
0a7476
  */
0a7476
@@ -719,7 +687,7 @@ virNWFilterInstantiateFilterUpdate(virNWFilterDriverStatePtr driver,
0a7476
                                    bool forceWithPendingReq,
0a7476
                                    bool *foundNewFilter)
0a7476
 {
0a7476
-    int rc;
0a7476
+    int rc = -1;
0a7476
     const char *drvname = EBIPTABLES_DRIVER_ID;
0a7476
     virNWFilterTechDriverPtr techdriver;
0a7476
     virNWFilterObjPtr obj;
0a7476
@@ -745,14 +713,18 @@ virNWFilterInstantiateFilterUpdate(virNWFilterDriverStatePtr driver,
0a7476
         return -1;
0a7476
 
0a7476
     virMacAddrFormat(&binding->mac, vmmacaddr);
0a7476
+    if (virNWFilterVarHashmapAddStdValue(binding->filterparams,
0a7476
+                                         NWFILTER_STD_VAR_MAC,
0a7476
+                                         vmmacaddr) < 0)
0a7476
+        goto err_exit;
0a7476
 
0a7476
     ipaddr = virNWFilterIPAddrMapGetIPAddr(binding->portdevname);
0a7476
-
0a7476
-    if (virNWFilterVarHashmapAddStdValues(binding->filterparams,
0a7476
-                                          vmmacaddr, ipaddr) < 0) {
0a7476
-        rc = -1;
0a7476
+    if (ipaddr &&
0a7476
+        virNWFilterVarHashmapAddStdValue(binding->filterparams,
0a7476
+                                         NWFILTER_STD_VAR_IP,
0a7476
+                                         virNWFilterVarValueGetSimple(ipaddr)) < 0)
0a7476
         goto err_exit;
0a7476
-    }
0a7476
+
0a7476
 
0a7476
     filter = virNWFilterObjGetDef(obj);
0a7476
 
0a7476
-- 
0a7476
2.21.0
0a7476