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

#include <config.h>

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

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

    virMacAddrFormat(macaddr, macaddrstr);
66
    virUUIDFormat(ovsport->interfaceID, ifuuidstr);
67
    virUUIDFormat(vmuuid, vmuuidstr);
A
Ansis Atteka 已提交
68 69 70

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

L
Laine Stump 已提交
84 85 86 87 88
    cmd = virCommandNew(OVSVSCTL);

    virCommandAddArgList(cmd, "--timeout=5", "--", "--may-exist", "add-port",
                        brname, ifname, NULL);

89
    if (virtVlan && virtVlan->nTags > 0) {
90

L
Laine Stump 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
        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;
        }

105
        if (virtVlan->trunk) {
106
            virBufferAddLit(&buf, "trunk=");
107 108 109 110 111 112 113

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

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

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

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

155 156
    ret = 0;
cleanup:
157
    virBufferFreeAndReset(&buf);
158 159
    VIR_FREE(attachedmac_ex_id);
    VIR_FREE(ifaceid_ex_id);
160
    VIR_FREE(vmid_ex_id);
161 162 163
    VIR_FREE(profile_ex_id);
    virCommandFree(cmd);
    return ret;
164 165 166 167

out_of_memory:
    virReportOOMError();
    goto cleanup;
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 187 188 189 190

    if (virCommandRun(cmd, NULL) < 0) {
        virReportSystemError(VIR_ERR_INTERNAL_ERROR,
                             _("Unable to delete port %s from OVS"), ifname);
        goto cleanup;
    }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

    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';
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260
/**
 * 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:
J
John Ferlan 已提交
261
    virCommandFree(cmd);
262
    return ret;
A
Ansis Atteka 已提交
263
}