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
This commit is contained in:
Peter Krempa 2013-03-26 12:21:33 +01:00
parent ab4bf20ead
commit 6bd94a1b59
9 changed files with 41 additions and 66 deletions

View File

@ -11521,14 +11521,15 @@ static bool
virDomainNetDefCheckABIStability(virDomainNetDefPtr src, virDomainNetDefCheckABIStability(virDomainNetDefPtr src,
virDomainNetDefPtr dst) virDomainNetDefPtr dst)
{ {
char srcmac[VIR_MAC_STRING_BUFLEN];
char dstmac[VIR_MAC_STRING_BUFLEN];
if (virMacAddrCmp(&src->mac, &dst->mac) != 0) { if (virMacAddrCmp(&src->mac, &dst->mac) != 0) {
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Target network card mac %02x:%02x:%02x:%02x:%02x:%02x" _("Target network card mac %s"
" does not match source %02x:%02x:%02x:%02x:%02x:%02x"), " does not match source %s"),
dst->mac.addr[0], dst->mac.addr[1], dst->mac.addr[2], virMacAddrFormat(&dst->mac, dstmac),
dst->mac.addr[3], dst->mac.addr[4], dst->mac.addr[5], virMacAddrFormat(&src->mac, srcmac));
src->mac.addr[0], src->mac.addr[1], src->mac.addr[2],
src->mac.addr[3], src->mac.addr[4], src->mac.addr[5]);
return false; return false;
} }
@ -13389,6 +13390,7 @@ virDomainNetDefFormat(virBufferPtr buf,
unsigned int flags) unsigned int flags)
{ {
const char *type = virDomainNetTypeToString(def->type); const char *type = virDomainNetTypeToString(def->type);
char macstr[VIR_MAC_STRING_BUFLEN];
if (!type) { if (!type) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
@ -13404,10 +13406,8 @@ virDomainNetDefFormat(virBufferPtr buf,
virBufferAddLit(buf, ">\n"); virBufferAddLit(buf, ">\n");
virBufferAdjustIndent(buf, 6); virBufferAdjustIndent(buf, 6);
virBufferAsprintf(buf, virBufferAsprintf(buf, "<mac address='%s'/>\n",
"<mac address='%02x:%02x:%02x:%02x:%02x:%02x'/>\n", virMacAddrFormat(&def->mac, macstr));
def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
switch (def->type) { switch (def->type) {
case VIR_DOMAIN_NET_TYPE_NETWORK: case VIR_DOMAIN_NET_TYPE_NETWORK:

View File

@ -3638,12 +3638,12 @@ qemuBuildNicStr(virDomainNetDefPtr net,
int vlan) int vlan)
{ {
char *str; char *str;
char macaddr[VIR_MAC_STRING_BUFLEN];
if (virAsprintf(&str, 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 : "", prefix ? prefix : "",
net->mac.addr[0], net->mac.addr[1], virMacAddrFormat(&net->mac, macaddr),
net->mac.addr[2], net->mac.addr[3],
net->mac.addr[4], net->mac.addr[5],
vlan, vlan,
(net->model ? ",model=" : ""), (net->model ? ",model=" : ""),
(net->model ? net->model : ""), (net->model ? net->model : ""),
@ -3666,6 +3666,7 @@ qemuBuildNicDevStr(virDomainNetDefPtr net,
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
const char *nic; const char *nic;
bool usingVirtio = false; bool usingVirtio = false;
char macaddr[VIR_MAC_STRING_BUFLEN];
if (!net->model) { if (!net->model) {
nic = "rtl8139"; nic = "rtl8139";
@ -3722,10 +3723,8 @@ qemuBuildNicDevStr(virDomainNetDefPtr net,
else else
virBufferAsprintf(&buf, ",vlan=%d", vlan); virBufferAsprintf(&buf, ",vlan=%d", vlan);
virBufferAsprintf(&buf, ",id=%s", net->info.alias); virBufferAsprintf(&buf, ",id=%s", net->info.alias);
virBufferAsprintf(&buf, ",mac=%02x:%02x:%02x:%02x:%02x:%02x", virBufferAsprintf(&buf, ",mac=%s",
net->mac.addr[0], net->mac.addr[1], virMacAddrFormat(&net->mac, macaddr));
net->mac.addr[2], net->mac.addr[3],
net->mac.addr[4], net->mac.addr[5]);
if (qemuBuildDeviceAddressStr(&buf, &net->info, qemuCaps) < 0) if (qemuBuildDeviceAddressStr(&buf, &net->info, qemuCaps) < 0)
goto error; goto error;
if (qemuBuildRomStr(&buf, &net->info, qemuCaps) < 0) if (qemuBuildRomStr(&buf, &net->info, qemuCaps) < 0)

View File

@ -1,7 +1,7 @@
/* /*
* uml_conf.c: UML driver configuration * 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 * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -164,6 +164,7 @@ umlBuildCommandLineNet(virConnectPtr conn,
int idx) int idx)
{ {
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
char macaddr[VIR_MAC_STRING_BUFLEN];
/* General format: ethNN=type,options */ /* General format: ethNN=type,options */
@ -264,9 +265,7 @@ umlBuildCommandLineNet(virConnectPtr conn,
goto error; goto error;
} }
virBufferAsprintf(&buf, ",%02x:%02x:%02x:%02x:%02x:%02x", virBufferAsprintf(&buf, ",%s", virMacAddrFormat(&def->mac, macaddr));
def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) { if (def->type == VIR_DOMAIN_NET_TYPE_MCAST) {
virBufferAsprintf(&buf, ",%s,%d", virBufferAsprintf(&buf, ",%s,%d",

View File

@ -1,7 +1,7 @@
/* /*
* virebtables.c: Helper APIs for managing ebtables * 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. * Copyright (C) 2009 IBM Corp.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -446,15 +446,9 @@ ebtablesAddForwardAllowIn(ebtablesContext *ctx,
const char *iface, const char *iface,
const virMacAddrPtr mac) const virMacAddrPtr mac)
{ {
char *macaddr; char macaddr[VIR_MAC_STRING_BUFLEN];
if (virAsprintf(&macaddr, virMacAddrFormat(mac, 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;
}
return ebtablesForwardAllowIn(ctx, iface, macaddr, ADD); return ebtablesForwardAllowIn(ctx, iface, macaddr, ADD);
} }
@ -475,14 +469,8 @@ ebtablesRemoveForwardAllowIn(ebtablesContext *ctx,
const char *iface, const char *iface,
const virMacAddrPtr mac) const virMacAddrPtr mac)
{ {
char *macaddr; char macaddr[VIR_MAC_STRING_BUFLEN];
if (virAsprintf(&macaddr, virMacAddrFormat(mac, 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;
}
return ebtablesForwardAllowIn(ctx, iface, macaddr, REMOVE); return ebtablesForwardAllowIn(ctx, iface, macaddr, REMOVE);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2010-2012 Red Hat, Inc. * Copyright (C) 2010-2013 Red Hat, Inc.
* Copyright (C) 2010-2012 IBM Corporation * Copyright (C) 2010-2012 IBM Corporation
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -518,6 +518,7 @@ virNetDevMacVLanVPortProfileCallback(unsigned char *msg,
virNetlinkCallbackDataPtr calld = opaque; virNetlinkCallbackDataPtr calld = opaque;
pid_t lldpad_pid = 0; pid_t lldpad_pid = 0;
pid_t virip_pid = 0; pid_t virip_pid = 0;
char macaddr[VIR_MAC_STRING_BUFLEN];
hdr = (struct nlmsghdr *) msg; hdr = (struct nlmsghdr *) msg;
data = nlmsg_data(hdr); data = nlmsg_data(hdr);
@ -707,11 +708,7 @@ virNetDevMacVLanVPortProfileCallback(unsigned char *msg,
VIR_INFO("Re-send 802.1qbg associate request:"); VIR_INFO("Re-send 802.1qbg associate request:");
VIR_INFO(" if: %s", calld->cr_ifname); VIR_INFO(" if: %s", calld->cr_ifname);
VIR_INFO(" lf: %s", calld->linkdev); VIR_INFO(" lf: %s", calld->linkdev);
VIR_INFO(" mac: %02x:%02x:%02x:%02x:%02x:%02x", VIR_INFO(" mac: %s", virMacAddrFormat(&calld->macaddress, macaddr));
calld->macaddress.addr[0], calld->macaddress.addr[1],
calld->macaddress.addr[2], calld->macaddress.addr[3],
calld->macaddress.addr[4], calld->macaddress.addr[5]);
ignore_value(virNetDevVPortProfileAssociate(calld->cr_ifname, ignore_value(virNetDevVPortProfileAssociate(calld->cr_ifname,
calld->virtPortProfile, calld->virtPortProfile,
&calld->macaddress, &calld->macaddress,

View File

@ -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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -286,6 +286,7 @@ int virNetDevTapCreateInBridgePort(const char *brname,
unsigned int flags) unsigned int flags)
{ {
virMacAddr tapmac; virMacAddr tapmac;
char macaddrstr[VIR_MAC_STRING_BUFLEN];
if (virNetDevTapCreate(ifname, tapfd, flags) < 0) if (virNetDevTapCreate(ifname, tapfd, flags) < 0)
return -1; return -1;
@ -306,10 +307,8 @@ int virNetDevTapCreateInBridgePort(const char *brname,
*/ */
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
_("Unable to use MAC address starting with " _("Unable to use MAC address starting with "
"reserved value 0xFE - '%02X:%02X:%02X:%02X:%02X:%02X' - "), "reserved value 0xFE - '%s' - "),
macaddr->addr[0], macaddr->addr[1], virMacAddrFormat(macaddr, macaddrstr));
macaddr->addr[2], macaddr->addr[3],
macaddr->addr[4], macaddr->addr[5]);
goto error; goto error;
} }
tapmac.addr[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */ tapmac.addr[0] = 0xFE; /* Discourage bridge from using TAP dev MAC */

View File

@ -3730,17 +3730,14 @@ virDomainXMLDevID(virDomainPtr domain,
if (tmp == NULL) if (tmp == NULL)
return -1; return -1;
} else if (dev->type == VIR_DOMAIN_DEVICE_NET) { } else if (dev->type == VIR_DOMAIN_DEVICE_NET) {
char mac[30]; char mac[VIR_MAC_STRING_BUFLEN];
virDomainNetDefPtr def = dev->data.net; virDomainNetDefPtr def = dev->data.net;
snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x", virMacAddrFormat(&def->mac, mac);
def->mac.addr[0], def->mac.addr[1], def->mac.addr[2],
def->mac.addr[3], def->mac.addr[4], def->mac.addr[5]);
strcpy(class, "vif"); strcpy(class, "vif");
xenUnifiedLock(priv); xenUnifiedLock(priv);
xref = xenStoreDomainGetNetworkID(domain->conn, domain->id, xref = xenStoreDomainGetNetworkID(domain->conn, domain->id, mac);
mac);
xenUnifiedUnlock(priv); xenUnifiedUnlock(priv);
if (xref == NULL) if (xref == NULL)
return -1; return -1;

View File

@ -1,7 +1,7 @@
/* /*
* xen_sxpr.c: Xen SEXPR parsing functions * 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) 2011 Univention GmbH
* Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com> * Copyright (C) 2005 Anthony Liguori <aliguori@us.ibm.com>
* *
@ -1914,6 +1914,7 @@ xenFormatSxprNet(virConnectPtr conn,
int isAttach) int isAttach)
{ {
const char *script = DEFAULT_VIF_SCRIPT; const char *script = DEFAULT_VIF_SCRIPT;
char macaddr[VIR_MAC_STRING_BUFLEN];
if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE && if (def->type != VIR_DOMAIN_NET_TYPE_BRIDGE &&
def->type != VIR_DOMAIN_NET_TYPE_NETWORK && def->type != VIR_DOMAIN_NET_TYPE_NETWORK &&
@ -1936,10 +1937,7 @@ xenFormatSxprNet(virConnectPtr conn,
virBufferAddLit(buf, "(vif "); virBufferAddLit(buf, "(vif ");
virBufferAsprintf(buf, virBufferAsprintf(buf, "(mac '%s')", virMacAddrFormat(&def->mac, macaddr));
"(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]);
switch (def->type) { switch (def->type) {
case VIR_DOMAIN_NET_TYPE_BRIDGE: case VIR_DOMAIN_NET_TYPE_BRIDGE:

View File

@ -1,7 +1,7 @@
/* /*
* xen_xm.c: Xen XM parsing functions * 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) 2011 Univention GmbH
* Copyright (C) 2006 Daniel P. Berrange * Copyright (C) 2006 Daniel P. Berrange
* *
@ -1335,11 +1335,9 @@ static int xenFormatXMNet(virConnectPtr conn,
{ {
virBuffer buf = VIR_BUFFER_INITIALIZER; virBuffer buf = VIR_BUFFER_INITIALIZER;
virConfValuePtr val, tmp; virConfValuePtr val, tmp;
char macaddr[VIR_MAC_STRING_BUFLEN];
virBufferAsprintf(&buf, "mac=%02x:%02x:%02x:%02x:%02x:%02x", virBufferAsprintf(&buf, "mac=%s", virMacAddrFormat(&net->mac, macaddr));
net->mac.addr[0], net->mac.addr[1],
net->mac.addr[2], net->mac.addr[3],
net->mac.addr[4], net->mac.addr[5]);
switch (net->type) { switch (net->type) {
case VIR_DOMAIN_NET_TYPE_BRIDGE: case VIR_DOMAIN_NET_TYPE_BRIDGE: