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,
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:

View File

@ -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)

View File

@ -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",

View File

@ -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;
char macaddr[VIR_MAC_STRING_BUFLEN];
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;
}
virMacAddrFormat(mac, macaddr);
return ebtablesForwardAllowIn(ctx, iface, macaddr, ADD);
}
@ -475,14 +469,8 @@ ebtablesRemoveForwardAllowIn(ebtablesContext *ctx,
const char *iface,
const virMacAddrPtr mac)
{
char *macaddr;
char macaddr[VIR_MAC_STRING_BUFLEN];
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;
}
virMacAddrFormat(mac, macaddr);
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
*
* 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,

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
* 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 */

View File

@ -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;

View File

@ -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:

View File

@ -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: