virnetdevopenvswitch.c 8.5 KB
Newer Older
A
Ansis Atteka 已提交
1
/*
2
 * Copyright (C) 2013 Red Hat, Inc.
A
Ansis Atteka 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * 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
16
 * License along with this library.  If not, see
O
Osier Yang 已提交
17
 * <http://www.gnu.org/licenses/>.
A
Ansis Atteka 已提交
18 19 20 21 22 23 24 25 26 27
 *
 * Authors:
 *     Dan Wendlandt <dan@nicira.com>
 *     Kyle Mestery <kmestery@cisco.com>
 *     Ansis Atteka <aatteka@nicira.com>
 */

#include <config.h>

#include "virnetdevopenvswitch.h"
28
#include "vircommand.h"
29
#include "viralloc.h"
30
#include "virerror.h"
A
Ansis Atteka 已提交
31
#include "virmacaddr.h"
32
#include "virstring.h"
33
#include "virlog.h"
A
Ansis Atteka 已提交
34 35 36

#define VIR_FROM_THIS VIR_FROM_NONE

37 38
VIR_LOG_INIT("util.netdevopenvswitch");

A
Ansis Atteka 已提交
39 40 41 42 43
/**
 * virNetDevOpenvswitchAddPort:
 * @brname: the bridge name
 * @ifname: the network interface name
 * @macaddr: the mac address of the virtual interface
44
 * @vmuuid: the Domain UUID that has this interface
A
Ansis Atteka 已提交
45 46 47 48 49 50 51
 * @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,
52
                                   const virMacAddr *macaddr,
53
                                   const unsigned char *vmuuid,
54 55
                                   virNetDevVPortProfilePtr ovsport,
                                   virNetDevVlanPtr virtVlan)
A
Ansis Atteka 已提交
56 57
{
    int ret = -1;
58
    size_t i = 0;
A
Ansis Atteka 已提交
59 60
    virCommandPtr cmd = NULL;
    char macaddrstr[VIR_MAC_STRING_BUFLEN];
61 62
    char ifuuidstr[VIR_UUID_STRING_BUFLEN];
    char vmuuidstr[VIR_UUID_STRING_BUFLEN];
A
Ansis Atteka 已提交
63 64 65
    char *attachedmac_ex_id = NULL;
    char *ifaceid_ex_id = NULL;
    char *profile_ex_id = NULL;
66
    char *vmid_ex_id = NULL;
67
    virBuffer buf = VIR_BUFFER_INITIALIZER;
A
Ansis Atteka 已提交
68 69

    virMacAddrFormat(macaddr, macaddrstr);
70
    virUUIDFormat(ovsport->interfaceID, ifuuidstr);
71
    virUUIDFormat(vmuuid, vmuuidstr);
A
Ansis Atteka 已提交
72 73 74

    if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"",
                    macaddrstr) < 0)
75
        goto cleanup;
A
Ansis Atteka 已提交
76
    if (virAsprintf(&ifaceid_ex_id, "external-ids:iface-id=\"%s\"",
77
                    ifuuidstr) < 0)
78
        goto cleanup;
79 80
    if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
                    vmuuidstr) < 0)
81
        goto cleanup;
82
    if (ovsport->profileID[0] != '\0') {
A
Ansis Atteka 已提交
83
        if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"",
84
                        ovsport->profileID) < 0)
85
            goto cleanup;
A
Ansis Atteka 已提交
86
    }
87

L
Laine Stump 已提交
88 89
    cmd = virCommandNew(OVSVSCTL);

90 91
    virCommandAddArgList(cmd, "--timeout=5", "--", "--if-exists", "del-port",
                         ifname, "--", "add-port", brname, ifname, NULL);
L
Laine Stump 已提交
92

93
    if (virtVlan && virtVlan->nTags > 0) {
94

L
Laine Stump 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108
        switch (virtVlan->nativeMode) {
        case VIR_NATIVE_VLAN_MODE_TAGGED:
            virCommandAddArg(cmd, "vlan_mode=native-tagged");
            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
            break;
        case VIR_NATIVE_VLAN_MODE_UNTAGGED:
            virCommandAddArg(cmd, "vlan_mode=native-untagged");
            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->nativeTag);
            break;
        case VIR_NATIVE_VLAN_MODE_DEFAULT:
        default:
            break;
        }

109
        if (virtVlan->trunk) {
110
            virBufferAddLit(&buf, "trunk=");
111 112 113 114 115 116 117

            /*
             * 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.
             */
118
            virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
119 120

            for (i = 1; i < virtVlan->nTags; i++) {
121 122
                virBufferAddLit(&buf, ",");
                virBufferAsprintf(&buf, "%d", virtVlan->tag[i]);
123
            }
L
Laine Stump 已提交
124

125
            if (virBufferCheckError(&buf) < 0)
126
                goto cleanup;
L
Laine Stump 已提交
127
            virCommandAddArg(cmd, virBufferCurrentContent(&buf));
128
        } else if (virtVlan->nTags) {
L
Laine Stump 已提交
129
            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->tag[0]);
130 131
        }
    }
A
Ansis Atteka 已提交
132

133
    if (ovsport->profileID[0] == '\0') {
134
        virCommandAddArgList(cmd,
A
Ansis Atteka 已提交
135 136
                        "--", "set", "Interface", ifname, attachedmac_ex_id,
                        "--", "set", "Interface", ifname, ifaceid_ex_id,
137
                        "--", "set", "Interface", ifname, vmid_ex_id,
A
Ansis Atteka 已提交
138 139 140 141
                        "--", "set", "Interface", ifname,
                        "external-ids:iface-status=active",
                        NULL);
    } else {
142
        virCommandAddArgList(cmd,
A
Ansis Atteka 已提交
143 144
                        "--", "set", "Interface", ifname, attachedmac_ex_id,
                        "--", "set", "Interface", ifname, ifaceid_ex_id,
145
                        "--", "set", "Interface", ifname, vmid_ex_id,
A
Ansis Atteka 已提交
146 147 148 149 150 151 152
                        "--", "set", "Interface", ifname, profile_ex_id,
                        "--", "set", "Interface", ifname,
                        "external-ids:iface-status=active",
                        NULL);
    }

    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
153 154 155
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to add port %s to OVS bridge %s"),
                       ifname, brname);
A
Ansis Atteka 已提交
156 157 158
        goto cleanup;
    }

159
    ret = 0;
160
 cleanup:
161
    virBufferFreeAndReset(&buf);
162 163
    VIR_FREE(attachedmac_ex_id);
    VIR_FREE(ifaceid_ex_id);
164
    VIR_FREE(vmid_ex_id);
165 166 167
    VIR_FREE(profile_ex_id);
    virCommandFree(cmd);
    return ret;
A
Ansis Atteka 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

/**
 * 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);
184
    virCommandAddArgList(cmd, "--timeout=5", "--", "--if-exists", "del-port", ifname, NULL);
A
Ansis Atteka 已提交
185 186

    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
187 188
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to delete port %s from OVS"), ifname);
A
Ansis Atteka 已提交
189 190
        goto cleanup;
    }
191 192

    ret = 0;
193
 cleanup:
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    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;

212
    cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "--if-exists", "get", "Interface",
213 214 215 216 217 218
                               ifname, "external_ids:PortData", NULL);

    virCommandSetOutputBuffer(cmd, migrate);

    /* Run the command */
    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
219 220 221
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to run command to get OVS port data for "
                         "interface %s"), ifname);
222 223 224 225 226
        goto cleanup;
    }

    /* Wipeout the newline */
    (*migrate)[strlen(*migrate) - 1] = '\0';
A
Ansis Atteka 已提交
227
    ret = 0;
228
 cleanup:
J
John Ferlan 已提交
229
    virCommandFree(cmd);
230 231
    return ret;
}
A
Ansis Atteka 已提交
232

233 234 235 236 237 238 239 240 241 242 243 244 245 246
/**
 * 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;

247 248 249 250 251
    if (!migrate) {
        VIR_DEBUG("No OVS port data for interface %s", ifname);
        return 0;
    }

252 253 254 255 256 257
    cmd = virCommandNewArgList(OVSVSCTL, "--timeout=5", "set",
                               "Interface", ifname, NULL);
    virCommandAddArgFormat(cmd, "external_ids:PortData=%s", migrate);

    /* Run the command */
    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
258 259 260
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to run command to set OVS port data for "
                         "interface %s"), ifname);
261 262 263 264
        goto cleanup;
    }

    ret = 0;
265
 cleanup:
J
John Ferlan 已提交
266
    virCommandFree(cmd);
267
    return ret;
A
Ansis Atteka 已提交
268
}