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-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <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"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-02-10 21:09:00 +00:00
|
|
|
#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-29 18:44:36 +00:00
|
|
|
virBuffer buf = VIR_BUFFER_INITIALIZER;
|
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-29 18:44:36 +00:00
|
|
|
|
|
|
|
if (virtVlan && virtVlan->nTags > 0) {
|
2012-08-17 04:04:54 +00:00
|
|
|
|
|
|
|
/* Trunk port first */
|
2012-08-30 07:18:02 +00:00
|
|
|
if (virtVlan->trunk) {
|
2012-08-29 18:44:36 +00:00
|
|
|
virBufferAddLit(&buf, "trunk=");
|
2012-08-17 04:04:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2012-08-29 18:44:36 +00:00
|
|
|
virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
|
2012-08-17 04:04:54 +00:00
|
|
|
|
|
|
|
for (i = 1; i < virtVlan->nTags; i++) {
|
2012-08-29 18:44:36 +00:00
|
|
|
virBufferAddLit(&buf, ",");
|
|
|
|
virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
|
2012-08-17 04:04:54 +00:00
|
|
|
}
|
2012-08-29 02:56:04 +00:00
|
|
|
} else if (virtVlan->nTags) {
|
2012-08-29 18:44:36 +00:00
|
|
|
virBufferAsprintf(&buf, "tag=%d", virtVlan->tag[0]);
|
2012-08-17 04:04:54 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-10 21:09:00 +00:00
|
|
|
|
|
|
|
cmd = virCommandNew(OVSVSCTL);
|
2012-08-30 20:38:06 +00:00
|
|
|
|
2012-09-04 19:26:29 +00:00
|
|
|
virCommandAddArgList(cmd, "--timeout=5", "--", "--may-exist", "add-port",
|
2012-08-30 20:38:06 +00:00
|
|
|
brname, ifname, NULL);
|
|
|
|
|
|
|
|
if (virBufferUse(&buf) != 0)
|
|
|
|
virCommandAddArgList(cmd, virBufferCurrentContent(&buf), NULL);
|
|
|
|
|
2012-07-25 01:14:41 +00:00
|
|
|
if (ovsport->profileID[0] == '\0') {
|
2012-08-30 20:38:06 +00:00
|
|
|
virCommandAddArgList(cmd,
|
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 {
|
2012-08-30 20:38:06 +00:00
|
|
|
virCommandAddArgList(cmd,
|
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-29 18:44:36 +00:00
|
|
|
virBufferFreeAndReset(&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);
|
2012-09-04 19:26:29 +00:00
|
|
|
virCommandAddArgList(cmd, "--timeout=5", "--", "--if-exists", "del-port", ifname, NULL);
|
2012-02-10 21:09:00 +00:00
|
|
|
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virReportSystemError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to delete port %s from OVS"), ifname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-10-01 15:18:21 +00:00
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virCommandFree(cmd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virNetDevOpenvswitchGetMigrateData:
|
|
|
|
* @migrate: a pointer to store the data into, allocated by this function
|
|
|
|
* @ifname: name of the interface for which data is being migrated
|
|
|
|
*
|
|
|
|
* Allocates data to be migrated specific to Open vSwitch
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or -1 in case of failure
|
|
|
|
*/
|
|
|
|
int virNetDevOpenvswitchGetMigrateData(char **migrate, const char *ifname)
|
|
|
|
{
|
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "get", "Interface",
|
|
|
|
ifname, "external_ids:PortData", NULL);
|
|
|
|
|
|
|
|
virCommandSetOutputBuffer(cmd, migrate);
|
|
|
|
|
|
|
|
/* Run the command */
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virReportSystemError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to run command to get OVS port data for "
|
|
|
|
"interface %s"), ifname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wipeout the newline */
|
|
|
|
(*migrate)[strlen(*migrate) - 1] = '\0';
|
2012-02-10 21:09:00 +00:00
|
|
|
ret = 0;
|
2012-10-01 15:18:21 +00:00
|
|
|
cleanup:
|
|
|
|
return ret;
|
|
|
|
}
|
2012-02-10 21:09:00 +00:00
|
|
|
|
2012-10-01 15:18:21 +00:00
|
|
|
/**
|
|
|
|
* virNetDevOpenvswitchSetMigrateData:
|
|
|
|
* @migrate: the data which was transferred during migration
|
|
|
|
* @ifname: the name of the interface the data is associated with
|
|
|
|
*
|
|
|
|
* Repopulates OVS per-port data on destination host
|
|
|
|
*
|
|
|
|
* Returns 0 in case of success or -1 in case of failure
|
|
|
|
*/
|
|
|
|
int virNetDevOpenvswitchSetMigrateData(char *migrate, const char *ifname)
|
|
|
|
{
|
|
|
|
virCommandPtr cmd = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "set",
|
|
|
|
"Interface", ifname, NULL);
|
|
|
|
virCommandAddArgFormat(cmd, "external_ids:PortData=%s", migrate);
|
|
|
|
|
|
|
|
/* Run the command */
|
|
|
|
if (virCommandRun(cmd, NULL) < 0) {
|
|
|
|
virReportSystemError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to run command to set OVS port data for "
|
|
|
|
"interface %s"), ifname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
return ret;
|
2012-02-10 21:09:00 +00:00
|
|
|
}
|