virnetdevopenvswitch.c 8.4 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"
A
Ansis Atteka 已提交
33 34 35 36 37 38 39 40

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if (virCommandRun(cmd, NULL) < 0) {
        virReportSystemError(VIR_ERR_INTERNAL_ERROR,
                             _("Unable to delete port %s from OVS"), ifname);
        goto cleanup;
    }
190 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

    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 已提交
226
    ret = 0;
227
cleanup:
J
John Ferlan 已提交
228
    virCommandFree(cmd);
229 230
    return ret;
}
A
Ansis Atteka 已提交
231

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
/**
 * 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 已提交
260
    virCommandFree(cmd);
261
    return ret;
A
Ansis Atteka 已提交
262
}