Fix a crash when using Open vSwitch virtual ports

Fixup buffer usage when handling VLANs. Also fix the logic
used to determine if the virNetDevVlanPtr is valid or not.
Fixes crashes in the latest code when using Open vSwitch
virtualports.

Signed-off-by: Kyle Mestery <kmestery@cisco.com>
This commit is contained in:
Kyle Mestery 2012-08-29 14:44:36 -04:00 committed by Daniel Veillard
parent 20b780aec9
commit 5e465df6be

View File

@ -59,7 +59,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
char *ifaceid_ex_id = NULL; char *ifaceid_ex_id = NULL;
char *profile_ex_id = NULL; char *profile_ex_id = NULL;
char *vmid_ex_id = NULL; char *vmid_ex_id = NULL;
virBufferPtr buf; virBuffer buf = VIR_BUFFER_INITIALIZER;
virMacAddrFormat(macaddr, macaddrstr); virMacAddrFormat(macaddr, macaddrstr);
virUUIDFormat(ovsport->interfaceID, ifuuidstr); virUUIDFormat(ovsport->interfaceID, ifuuidstr);
@ -79,13 +79,12 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
ovsport->profileID) < 0) ovsport->profileID) < 0)
goto out_of_memory; goto out_of_memory;
} }
if (virtVlan) {
if (VIR_ALLOC(buf) < 0) if (virtVlan && virtVlan->nTags > 0) {
goto out_of_memory;
/* Trunk port first */ /* Trunk port first */
if (virtVlan->trunk) { if (virtVlan->trunk == true) {
virBufferAddLit(buf, "trunk="); virBufferAddLit(&buf, "trunk=");
/* /*
* Trunk ports have at least one VLAN. Do the first one * Trunk ports have at least one VLAN. Do the first one
@ -93,21 +92,21 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
* start of the for loop if there are more than one VLANs * start of the for loop if there are more than one VLANs
* on this trunk port. * on this trunk port.
*/ */
virBufferAsprintf(buf, "%d", virtVlan->tag[i]); virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
for (i = 1; i < virtVlan->nTags; i++) { for (i = 1; i < virtVlan->nTags; i++) {
virBufferAddLit(buf, ","); virBufferAddLit(&buf, ",");
virBufferAsprintf(buf, "%d", virtVlan->tag[i]); virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
} }
} else if (virtVlan->nTags) { } else if (virtVlan->nTags) {
virBufferAsprintf(buf, "tag=%d", virtVlan->tag[0]); virBufferAsprintf(&buf, "tag=%d", virtVlan->tag[0]);
} }
} }
cmd = virCommandNew(OVSVSCTL); cmd = virCommandNew(OVSVSCTL);
if (ovsport->profileID[0] == '\0') { if (ovsport->profileID[0] == '\0') {
virCommandAddArgList(cmd, "--", "--may-exist", "add-port", virCommandAddArgList(cmd, "--", "--may-exist", "add-port",
brname, ifname, virBufferContentAndReset(buf), brname, ifname, virBufferCurrentContent(&buf),
"--", "set", "Interface", ifname, attachedmac_ex_id, "--", "set", "Interface", ifname, attachedmac_ex_id,
"--", "set", "Interface", ifname, ifaceid_ex_id, "--", "set", "Interface", ifname, ifaceid_ex_id,
"--", "set", "Interface", ifname, vmid_ex_id, "--", "set", "Interface", ifname, vmid_ex_id,
@ -116,7 +115,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
NULL); NULL);
} else { } else {
virCommandAddArgList(cmd, "--", "--may-exist", "add-port", virCommandAddArgList(cmd, "--", "--may-exist", "add-port",
brname, ifname, virBufferContentAndReset(buf), brname, ifname, virBufferCurrentContent(&buf),
"--", "set", "Interface", ifname, attachedmac_ex_id, "--", "set", "Interface", ifname, attachedmac_ex_id,
"--", "set", "Interface", ifname, ifaceid_ex_id, "--", "set", "Interface", ifname, ifaceid_ex_id,
"--", "set", "Interface", ifname, vmid_ex_id, "--", "set", "Interface", ifname, vmid_ex_id,
@ -135,7 +134,7 @@ int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(buf); virBufferFreeAndReset(&buf);
VIR_FREE(attachedmac_ex_id); VIR_FREE(attachedmac_ex_id);
VIR_FREE(ifaceid_ex_id); VIR_FREE(ifaceid_ex_id);
VIR_FREE(vmid_ex_id); VIR_FREE(vmid_ex_id);