2012-02-10 21:09:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Nicira, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-07-21 10:06:23 +00:00
|
|
|
* License along with this library; If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
2012-02-10 21:09:00 +00:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Dan Wendlandt <dan@nicira.com>
|
|
|
|
* Kyle Mestery <kmestery@cisco.com>
|
|
|
|
* Ansis Atteka <aatteka@nicira.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "virnetdevopenvswitch.h"
|
|
|
|
#include "command.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "virmacaddr.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevOpenvswitchAddPort:
|
|
|
|
* @brname: the bridge name
|
|
|
|
* @ifname: the network interface name
|
|
|
|
* @macaddr: the mac address of the virtual interface
|
2012-03-07 07:15:36 +00:00
|
|
|
* @vmuuid: the Domain UUID that has this interface
|
2012-02-10 21:09:00 +00:00
|
|
|
* @ovsport: the ovs specific fields
|
|
|
|
*
|
|
|
|
* Add an interface to the OVS bridge
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int virNetDevOpenvswitchAddPort(const char *brname, const char *ifname,
|
2012-07-17 12:07:59 +00:00
|
|
|
const virMacAddrPtr macaddr,
|
2012-03-07 07:15:36 +00:00
|
|
|
const unsigned char *vmuuid,
|
2012-08-17 04:04:54 +00:00
|
|
|
virNetDevVPortProfilePtr ovsport,
|
|
|
|
virNetDevVlanPtr virtVlan)
|
2012-02-10 21:09:00 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2012-08-17 04:04:54 +00:00
|
|
|
int i = 0;
|
2012-02-10 21:09:00 +00:00
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
char macaddrstr[VIR_MAC_STRING_BUFLEN];
|
2012-03-07 07:15:36 +00:00
|
|
|
char ifuuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
char vmuuidstr[VIR_UUID_STRING_BUFLEN];
|
2012-02-10 21:09:00 +00:00
|
|
|
char *attachedmac_ex_id = NULL;
|
|
|
|
char *ifaceid_ex_id = NULL;
|
|
|
|
char *profile_ex_id = NULL;
|
2012-03-07 07:15:36 +00:00
|
|
|
char *vmid_ex_id = NULL;
|
2012-08-17 04:04:54 +00:00
|
|
|
virBufferPtr buf;
|
2012-02-10 21:09:00 +00:00
|
|
|
|
|
|
|
virMacAddrFormat(macaddr, macaddrstr);
|
2012-07-25 01:14:41 +00:00
|
|
|
virUUIDFormat(ovsport->interfaceID, ifuuidstr);
|
2012-03-07 07:15:36 +00:00
|
|
|
virUUIDFormat(vmuuid, vmuuidstr);
|
2012-02-10 21:09:00 +00:00
|
|
|
|
|
|
|
if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"",
|
|
|
|
macaddrstr) < 0)
|
2012-03-08 18:48:52 +00:00
|
|
|
goto out_of_memory;
|
2012-02-10 21:09:00 +00:00
|
|
|
if (virAsprintf(&ifaceid_ex_id, "external-ids:iface-id=\"%s\"",
|
2012-03-07 07:15:36 +00:00
|
|
|
ifuuidstr) < 0)
|
2012-03-08 18:48:52 +00:00
|
|
|
goto out_of_memory;
|
2012-03-07 07:15:36 +00:00
|
|
|
if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
|
|
|
|
vmuuidstr) < 0)
|
2012-03-08 18:48:52 +00:00
|
|
|
goto out_of_memory;
|
2012-07-25 01:14:41 +00:00
|
|
|
if (ovsport->profileID[0] != '\0') {
|
2012-02-10 21:09:00 +00:00
|
|
|
if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"",
|
2012-07-25 01:14:41 +00:00
|
|
|
ovsport->profileID) < 0)
|
2012-03-08 18:48:52 +00:00
|
|
|
goto out_of_memory;
|
2012-02-10 21:09:00 +00:00
|
|
|
}
|
2012-08-17 04:04:54 +00:00
|
|
|
if (virtVlan) {
|
|
|
|
if (VIR_ALLOC(buf) < 0)
|
|
|
|
goto out_of_memory;
|
|
|
|
|
|
|
|
/* Trunk port first */
|
|
|
|
if (virtVlan->trunk) {
|
|
|
|
virBufferAddLit(buf, "trunk=");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Trunk ports have at least one VLAN. Do the first one
|
|
|
|
* outside the "for" loop so we can put a "," at the
|
|
|
|
* start of the for loop if there are more than one VLANs
|
|
|
|
* on this trunk port.
|
|
|
|
*/
|
|
|
|
virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
|
|
|
|
|
|
|
|
for (i = 1; i < virtVlan->nTags; i++) {
|
|
|
|
virBufferAddLit(buf, ",");
|
|
|
|
virBufferAsprintf(buf, "%d", virtVlan->tag[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
virBufferAsprintf(buf, "tag=%d", virtVlan->tag[0]);
|
|
|
|
}
|
|
|
|
}
|
2012-02-10 21:09:00 +00:00
|
|
|
|
|
|
|
cmd = virCommandNew(OVSVSCTL);
|
2012-07-25 01:14:41 +00:00
|
|
|
if (ovsport->profileID[0] == '\0') {
|
2012-02-10 21:09:00 +00:00
|
|
|
virCommandAddArgList(cmd, "--", "--may-exist", "add-port",
|
2012-08-17 04:04:54 +00:00
|
|
|
brname, ifname, virBufferContentAndReset(buf),
|
2012-02-10 21:09:00 +00:00
|
|
|
"--", "set", "Interface", ifname, attachedmac_ex_id,
|
|
|
|
"--", "set", "Interface", ifname, ifaceid_ex_id,
|
2012-03-07 07:15:36 +00:00
|
|
|
"--", "set", "Interface", ifname, vmid_ex_id,
|
2012-02-10 21:09:00 +00:00
|
|
|
"--", "set", "Interface", ifname,
|
|
|
|
"external-ids:iface-status=active",
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
virCommandAddArgList(cmd, "--", "--may-exist", "add-port",
|
2012-08-17 04:04:54 +00:00
|
|
|
brname, ifname, virBufferContentAndReset(buf),
|
2012-02-10 21:09:00 +00:00
|
|
|
"--", "set", "Interface", ifname, attachedmac_ex_id,
|
|
|
|
"--", "set", "Interface", ifname, ifaceid_ex_id,
|
2012-03-07 07:15:36 +00:00
|
|
|
"--", "set", "Interface", ifname, vmid_ex_id,
|
2012-02-10 21:09:00 +00:00
|
|
|
"--", "set", "Interface", ifname, profile_ex_id,
|
|
|
|
"--", "set", "Interface", ifname,
|
|
|
|
"external-ids:iface-status=active",
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virReportSystemError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to add port %s to OVS bridge %s"),
|
|
|
|
ifname, brname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2012-03-08 18:31:06 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
2012-08-17 04:04:54 +00:00
|
|
|
VIR_FREE(buf);
|
2012-03-08 18:31:06 +00:00
|
|
|
VIR_FREE(attachedmac_ex_id);
|
|
|
|
VIR_FREE(ifaceid_ex_id);
|
2012-03-07 07:15:36 +00:00
|
|
|
VIR_FREE(vmid_ex_id);
|
2012-03-08 18:31:06 +00:00
|
|
|
VIR_FREE(profile_ex_id);
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
2012-03-08 18:48:52 +00:00
|
|
|
|
|
|
|
out_of_memory:
|
|
|
|
virReportOOMError();
|
|
|
|
goto cleanup;
|
2012-02-10 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevOpenvswitchRemovePort:
|
|
|
|
* @ifname: the network interface name
|
|
|
|
*
|
|
|
|
* Deletes an interface from a OVS bridge
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or -1 in case of failure.
|
|
|
|
*/
|
|
|
|
int virNetDevOpenvswitchRemovePort(const char *brname ATTRIBUTE_UNUSED, const char *ifname)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
|
|
|
|
cmd = virCommandNew(OVSVSCTL);
|
|
|
|
virCommandAddArgList(cmd, "--", "--if-exists", "del-port", ifname, NULL);
|
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virReportSystemError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to delete port %s from OVS"), ifname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|