[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 2/3] Use virMacAddrFormat instead of manual mac address formatting



Format the address using the helper instead of having similar code in
multiple places.

This patch also fixes leak of the MAC address string in
ebtablesRemoveForwardAllowIn() and ebtablesAddForwardAllowIn() in
src/util/virebtables.c
---
 src/conf/domain_conf.c      | 20 ++++++++++----------
 src/qemu/qemu_command.c     | 15 +++++++--------
 src/uml/uml_conf.c          |  7 +++----
 src/util/virebtables.c      | 26 +++++++-------------------
 src/util/virnetdevmacvlan.c |  9 +++------
 src/util/virnetdevtap.c     |  9 ++++-----
 src/xen/xend_internal.c     |  9 +++------
 src/xenxs/xen_sxpr.c        |  8 +++-----
 src/xenxs/xen_xm.c          |  8 +++-----
 9 files changed, 43 insertions(+), 68 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 371d80c..cc26f21 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -11521,14 +11521,15 @@ static bool
 virDomainNetDefCheckABIStability(virDomainNetDefPtr src,
                                  virDomainNetDefPtr dst)
 {
+    char srcmac[VIR_MAC_STRING_BUFLEN];
+    char dstmac[VIR_MAC_STRING_BUFLEN];
+
     if (virMacAddrCmp(&src->mac, &dst->mac) != 0) {
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                       _("Target network card mac %02x:%02x:%02x:%02x:%02x:%02x"
-                         " does not match source %02x:%02x:%02x:%02x:%02x:%02x"),
-                       dst->mac.addr[0], dst->mac.addr[1], dst->mac.addr[2],
-                       dst->mac.addr[3], dst->mac.addr[4], dst->mac.addr[5],
-                       src->mac.addr[0], src->mac.addr[1], src->mac.addr[2],
-                       src->mac.addr[3], src->mac.addr[4], src->mac.addr[5]);
+                       _("Target network card mac %s"
+                         " does not match source %s"),
+                       virMacAddrFormat(&dst->mac, dstmac),
+                       virMacAddrFormat(&src->mac, srcmac));
         return false;
     }

@@ -13389,6 +13390,7 @@ virDomainNetDefFormat(virBufferPtr buf,
                       unsigned int flags)
 {
     const char *type = virDomainNetTypeToString(def->type);
+    char macstr[VIR_MAC_STRING_BUFLEN];

     if (!type) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
@@ -13404,10 +13406,8 @@ virDomainNetDefFormat(virBufferPtr buf,
     virBufferAddLit(buf, ">\n");

     virBufferAdjustIndent(buf, 6);
-    virBufferAsprintf(buf,
-                      "<mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n",
-                      def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
-                      def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
+    virBufferAsprintf(buf, "<mac address='%s'/>\n",
+                      virMacAddrFormat(&def->mac, macstr));

     switch (def->type) {
     case VIR_DOMAIN_NET_TYPE_NETWORK:
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index c80218d..17b0a9f 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -3638,12 +3638,12 @@ qemuBuildNicStr(virDomainNetDefPtr net,
                 int vlan)
 {
     char *str;
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+
     if (virAsprintf(&str,
-                    "%smacaddr=%02x:%02x:%02x:%02x:%02x:%02x,vlan=%d%s%s%s%s",
+                    "%smacaddr=%s,vlan=%d%s%s%s%s",
                     prefix ? prefix : "",
-                    net->mac.addr[0], net->mac.addr[1],
-                    net->mac.addr[2], net->mac.addr[3],
-                    net->mac.addr[4], net->mac.addr[5],
+                    virMacAddrFormat(&net->mac, macaddr),
                     vlan,
                     (net->model ? ",model=" : ""),
                     (net->model ? net->model : ""),
@@ -3666,6 +3666,7 @@ qemuBuildNicDevStr(virDomainNetDefPtr net,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     const char *nic;
     bool usingVirtio = false;
+    char macaddr[VIR_MAC_STRING_BUFLEN];

     if (!net->model) {
         nic = "rtl8139";
@@ -3722,10 +3723,8 @@ qemuBuildNicDevStr(virDomainNetDefPtr net,
     else
         virBufferAsprintf(&buf, ",vlan=%d", vlan);
     virBufferAsprintf(&buf, ",id=%s", net->info.alias);
-    virBufferAsprintf(&buf, ",mac=%02x:%02x:%02x:%02x:%02x:%02x",
-                      net->mac.addr[0], net->mac.addr[1],
-                      net->mac.addr[2], net->mac.addr[3],
-                      net->mac.addr[4], net->mac.addr[5]);
+    virBufferAsprintf(&buf, ",mac=%s",
+                      virMacAddrFormat(&net->mac, macaddr));
     if (qemuBuildDeviceAddressStr(&buf, &net->info, qemuCaps) < 0)
         goto error;
     if (qemuBuildRomStr(&buf, &net->info, qemuCaps) < 0)
diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c
index b3ac326..0fe59fb 100644
--- a/src/uml/uml_conf.c
+++ b/src/uml/uml_conf.c
@@ -1,7 +1,7 @@
 /*
  * uml_conf.c: UML driver configuration
  *
- * Copyright (C) 2006-2012 Red Hat, Inc.
+ * Copyright (C) 2006-2013 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -164,6 +164,7 @@ umlBuildCommandLineNet(virConnectPtr conn,
                        int idx)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char macaddr[VIR_MAC_STRING_BUFLEN];

     /* General format:  ethNN=type,options */

@@ -264,9 +265,7 @@ umlBuildCommandLineNet(virConnectPtr conn,
         goto error;
     }

-    virBufferAsprintf(&buf, ",%02x:%02x:%02x:%02x:%02x:%02x",
-                      def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
-                      def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
+    virBufferAsprintf(&buf, ",%s", virMacAddrFormat(&def->mac, macaddr));

     if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
         virBufferAsprintf(&buf, ",%s,%d",
diff --git a/src/util/virebtables.c b/src/util/virebtables.c
index ded62d7..65c0ac3 100644
--- a/src/util/virebtables.c
+++ b/src/util/virebtables.c
@@ -1,7 +1,7 @@
 /*
  * virebtables.c: Helper APIs for managing ebtables
  *
- * Copyright (C) 2007-2012 Red Hat, Inc.
+ * Copyright (C) 2007-2013 Red Hat, Inc.
  * Copyright (C) 2009 IBM Corp.
  *
  * This library is free software; you can redistribute it and/or
@@ -446,15 +446,9 @@ ebtablesAddForwardAllowIn(ebtablesContext *ctx,
                           const char *iface,
                           const virMacAddrPtr mac)
 {
-    char *macaddr;
-
-    if (virAsprintf(&macaddr,
-                    "%02x:%02x:%02x:%02x:%02x:%02x",
-                    mac->addr[0], mac->addr[1],
-                    mac->addr[2], mac->addr[3],
-                    mac->addr[4], mac->addr[5]) < 0) {
-        return -1;
-    }
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+
+    virMacAddrFormat(mac, macaddr);
     return ebtablesForwardAllowIn(ctx, iface, macaddr, ADD);
 }

@@ -475,14 +469,8 @@ ebtablesRemoveForwardAllowIn(ebtablesContext *ctx,
                              const char *iface,
                              const virMacAddrPtr mac)
 {
-    char *macaddr;
-
-    if (virAsprintf(&macaddr,
-                    "%02x:%02x:%02x:%02x:%02x:%02x",
-                    mac->addr[0], mac->addr[1],
-                    mac->addr[2], mac->addr[3],
-                    mac->addr[4], mac->addr[5]) < 0) {
-       return -1;
-    }
+    char macaddr[VIR_MAC_STRING_BUFLEN];
+
+    virMacAddrFormat(mac, macaddr);
     return ebtablesForwardAllowIn(ctx, iface, macaddr, REMOVE);
 }
diff --git a/src/util/virnetdevmacvlan.c b/src/util/virnetdevmacvlan.c
index ddea11f..2578ff0 100644
--- a/src/util/virnetdevmacvlan.c
+++ b/src/util/virnetdevmacvlan.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 Red Hat, Inc.
+ * Copyright (C) 2010-2013 Red Hat, Inc.
  * Copyright (C) 2010-2012 IBM Corporation
  *
  * This library is free software; you can redistribute it and/or
@@ -518,6 +518,7 @@ virNetDevMacVLanVPortProfileCallback(unsigned char *msg,
     virNetlinkCallbackDataPtr calld = opaque;
     pid_t lldpad_pid = 0;
     pid_t virip_pid = 0;
+    char macaddr[VIR_MAC_STRING_BUFLEN];

     hdr = (struct nlmsghdr *) msg;
     data = nlmsg_data(hdr);
@@ -707,11 +708,7 @@ virNetDevMacVLanVPortProfileCallback(unsigned char *msg,
     VIR_INFO("Re-send 802.1qbg associate request:");
     VIR_INFO("  if: %s", calld->cr_ifname);
     VIR_INFO("  lf: %s", calld->linkdev);
-    VIR_INFO(" mac: %02x:%02x:%02x:%02x:%02x:%02x",
-             calld->macaddress.addr[0], calld->macaddress.addr[1],
-             calld->macaddress.addr[2], calld->macaddress.addr[3],
-             calld->macaddress.addr[4], calld->macaddress.addr[5]);
-
+    VIR_INFO(" mac: %s", virMacAddrFormat(&calld->macaddress, macaddr));
     ignore_value(virNetDevVPortProfileAssociate(calld->cr_ifname,
                                                 calld->virtPortProfile,
                                                 &calld->macaddress,
diff --git a/src/util/virnetdevtap.c b/src/util/virnetdevtap.c
index a884de1..871376e 100644
--- a/src/util/virnetdevtap.c
+++ b/src/util/virnetdevtap.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2007-2012 Red Hat, Inc.
+ * Copyright (C) 2007-2013 Red Hat, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -286,6 +286,7 @@ int virNetDevTapCreateInBridgePort(const char *brname,
                                    unsigned int flags)
 {
     virMacAddr tapmac;
+    char macaddrstr[VIR_MAC_STRING_BUFLEN];

     if (virNetDevTapCreate(ifname, tapfd, flags) < 0)
         return -1;
@@ -306,10 +307,8 @@ int virNetDevTapCreateInBridgePort(const char *brname,
              */
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                            _("Unable to use MAC address starting with "
-                             "reserved value 0xFE - '%02X:%02X:%02X:%02X:%02X:%02X' - "),
-                           macaddr->addr[0], macaddr->addr[1],
-                           macaddr->addr[2], macaddr->addr[3],
-                           macaddr->addr[4], macaddr->addr[5]);
+                             "reserved value 0xFE - '%s' - "),
+                           virMacAddrFormat(macaddr, macaddrstr));
             goto error;
         }
         tapmac.addr[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */
diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c
index 398da0d..7abc030 100644
--- a/src/xen/xend_internal.c
+++ b/src/xen/xend_internal.c
@@ -3730,17 +3730,14 @@ virDomainXMLDevID(virDomainPtr domain,
         if (tmp == NULL)
             return -1;
     } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
-        char mac[30];
+        char mac[VIR_MAC_STRING_BUFLEN];
         virDomainNetDefPtr def = dev->data.net;
-        snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
-                 def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
-                 def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
+        virMacAddrFormat(&def->mac, mac);

         strcpy(class, "vif");

         xenUnifiedLock(priv);
-        xref = xenStoreDomainGetNetworkID(domain->conn, domain->id,
-                                          mac);
+        xref = xenStoreDomainGetNetworkID(domain->conn, domain->id, mac);
         xenUnifiedUnlock(priv);
         if (xref == NULL)
             return -1;
diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c
index 83b7c74..cc4225b 100644
--- a/src/xenxs/xen_sxpr.c
+++ b/src/xenxs/xen_sxpr.c
@@ -1,7 +1,7 @@
 /*
  * xen_sxpr.c: Xen SEXPR parsing functions
  *
- * Copyright (C) 2010-2012 Red Hat, Inc.
+ * Copyright (C) 2010-2013 Red Hat, Inc.
  * Copyright (C) 2011 Univention GmbH
  * Copyright (C) 2005 Anthony Liguori <aliguori us ibm com>
  *
@@ -1914,6 +1914,7 @@ xenFormatSxprNet(virConnectPtr conn,
                  int isAttach)
 {
     const char *script = DEFAULT_VIF_SCRIPT;
+    char macaddr[VIR_MAC_STRING_BUFLEN];

     if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
         def->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
@@ -1936,10 +1937,7 @@ xenFormatSxprNet(virConnectPtr conn,

     virBufferAddLit(buf, "(vif ");

-    virBufferAsprintf(buf,
-                      "(mac '%02x:%02x:%02x:%02x:%02x:%02x')",
-                      def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
-                      def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
+    virBufferAsprintf(buf, "(mac '%s')", virMacAddrFormat(&def->mac, macaddr));

     switch (def->type) {
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
index 73ba06b..405ebf3 100644
--- a/src/xenxs/xen_xm.c
+++ b/src/xenxs/xen_xm.c
@@ -1,7 +1,7 @@
 /*
  * xen_xm.c: Xen XM parsing functions
  *
- * Copyright (C) 2006-2007, 2009-2010, 2012 Red Hat, Inc.
+ * Copyright (C) 2006-2007, 2009-2010, 2012, 2013 Red Hat, Inc.
  * Copyright (C) 2011 Univention GmbH
  * Copyright (C) 2006 Daniel P. Berrange
  *
@@ -1335,11 +1335,9 @@ static int xenFormatXMNet(virConnectPtr conn,
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     virConfValuePtr val, tmp;
+    char macaddr[VIR_MAC_STRING_BUFLEN];

-    virBufferAsprintf(&buf, "mac=%02x:%02x:%02x:%02x:%02x:%02x",
-                      net->mac.addr[0], net->mac.addr[1],
-                      net->mac.addr[2], net->mac.addr[3],
-                      net->mac.addr[4], net->mac.addr[5]);
+    virBufferAsprintf(&buf, "mac=%s", virMacAddrFormat(&net->mac, macaddr));

     switch (net->type) {
     case VIR_DOMAIN_NET_TYPE_BRIDGE:
-- 
1.8.1.5


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]