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

#include <config.h>

29 30
#include <stdio.h>

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

#define VIR_FROM_THIS VIR_FROM_NONE

41 42
VIR_LOG_INIT("util.netdevopenvswitch");

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
/*
 * Set openvswitch default timout
 */
static unsigned int virNetDevOpenvswitchTimeout = VIR_NETDEV_OVS_DEFAULT_TIMEOUT;

/**
 * virNetDevOpenvswitchSetTimeout:
 * @timeout: the timeout in seconds
 *
 * Set the openvswitch timeout
 */
void
virNetDevOpenvswitchSetTimeout(unsigned int timeout)
{
    virNetDevOpenvswitchTimeout = timeout;
}

static void
virNetDevOpenvswitchAddTimeout(virCommandPtr cmd)
{
    virCommandAddArgFormat(cmd, "--timeout=%u", virNetDevOpenvswitchTimeout);
}

A
Ansis Atteka 已提交
66 67 68 69 70
/**
 * virNetDevOpenvswitchAddPort:
 * @brname: the bridge name
 * @ifname: the network interface name
 * @macaddr: the mac address of the virtual interface
71
 * @vmuuid: the Domain UUID that has this interface
A
Ansis Atteka 已提交
72 73 74 75 76 77 78
 * @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,
79
                                   const virMacAddr *macaddr,
80
                                   const unsigned char *vmuuid,
81 82
                                   virNetDevVPortProfilePtr ovsport,
                                   virNetDevVlanPtr virtVlan)
A
Ansis Atteka 已提交
83 84
{
    int ret = -1;
85
    size_t i = 0;
A
Ansis Atteka 已提交
86 87
    virCommandPtr cmd = NULL;
    char macaddrstr[VIR_MAC_STRING_BUFLEN];
88 89
    char ifuuidstr[VIR_UUID_STRING_BUFLEN];
    char vmuuidstr[VIR_UUID_STRING_BUFLEN];
A
Ansis Atteka 已提交
90 91 92
    char *attachedmac_ex_id = NULL;
    char *ifaceid_ex_id = NULL;
    char *profile_ex_id = NULL;
93
    char *vmid_ex_id = NULL;
94
    virBuffer buf = VIR_BUFFER_INITIALIZER;
A
Ansis Atteka 已提交
95 96

    virMacAddrFormat(macaddr, macaddrstr);
97
    virUUIDFormat(ovsport->interfaceID, ifuuidstr);
98
    virUUIDFormat(vmuuid, vmuuidstr);
A
Ansis Atteka 已提交
99 100 101

    if (virAsprintf(&attachedmac_ex_id, "external-ids:attached-mac=\"%s\"",
                    macaddrstr) < 0)
102
        goto cleanup;
A
Ansis Atteka 已提交
103
    if (virAsprintf(&ifaceid_ex_id, "external-ids:iface-id=\"%s\"",
104
                    ifuuidstr) < 0)
105
        goto cleanup;
106 107
    if (virAsprintf(&vmid_ex_id, "external-ids:vm-id=\"%s\"",
                    vmuuidstr) < 0)
108
        goto cleanup;
109
    if (ovsport->profileID[0] != '\0') {
A
Ansis Atteka 已提交
110
        if (virAsprintf(&profile_ex_id, "external-ids:port-profile=\"%s\"",
111
                        ovsport->profileID) < 0)
112
            goto cleanup;
A
Ansis Atteka 已提交
113
    }
114

L
Laine Stump 已提交
115
    cmd = virCommandNew(OVSVSCTL);
116 117
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "--", "--if-exists", "del-port",
118
                         ifname, "--", "add-port", brname, ifname, NULL);
L
Laine Stump 已提交
119

120
    if (virtVlan && virtVlan->nTags > 0) {
121

L
Laine Stump 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135
        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;
        }

136
        if (virtVlan->trunk) {
137
            virBufferAddLit(&buf, "trunk=");
138 139 140 141 142 143 144

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

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

152
            if (virBufferCheckError(&buf) < 0)
153
                goto cleanup;
L
Laine Stump 已提交
154
            virCommandAddArg(cmd, virBufferCurrentContent(&buf));
155
        } else if (virtVlan->nTags) {
L
Laine Stump 已提交
156
            virCommandAddArgFormat(cmd, "tag=%d", virtVlan->tag[0]);
157 158
        }
    }
A
Ansis Atteka 已提交
159

160
    if (ovsport->profileID[0] == '\0') {
161
        virCommandAddArgList(cmd,
A
Ansis Atteka 已提交
162 163
                        "--", "set", "Interface", ifname, attachedmac_ex_id,
                        "--", "set", "Interface", ifname, ifaceid_ex_id,
164
                        "--", "set", "Interface", ifname, vmid_ex_id,
A
Ansis Atteka 已提交
165 166 167 168
                        "--", "set", "Interface", ifname,
                        "external-ids:iface-status=active",
                        NULL);
    } else {
169
        virCommandAddArgList(cmd,
A
Ansis Atteka 已提交
170 171
                        "--", "set", "Interface", ifname, attachedmac_ex_id,
                        "--", "set", "Interface", ifname, ifaceid_ex_id,
172
                        "--", "set", "Interface", ifname, vmid_ex_id,
A
Ansis Atteka 已提交
173 174 175 176 177 178 179
                        "--", "set", "Interface", ifname, profile_ex_id,
                        "--", "set", "Interface", ifname,
                        "external-ids:iface-status=active",
                        NULL);
    }

    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
180 181 182
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to add port %s to OVS bridge %s"),
                       ifname, brname);
A
Ansis Atteka 已提交
183 184 185
        goto cleanup;
    }

186
    ret = 0;
187
 cleanup:
188
    virBufferFreeAndReset(&buf);
189 190
    VIR_FREE(attachedmac_ex_id);
    VIR_FREE(ifaceid_ex_id);
191
    VIR_FREE(vmid_ex_id);
192 193 194
    VIR_FREE(profile_ex_id);
    virCommandFree(cmd);
    return ret;
A
Ansis Atteka 已提交
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
}

/**
 * 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);
211 212
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "--", "--if-exists", "del-port", ifname, NULL);
A
Ansis Atteka 已提交
213 214

    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
215 216
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to delete port %s from OVS"), ifname);
A
Ansis Atteka 已提交
217 218
        goto cleanup;
    }
219 220

    ret = 0;
221
 cleanup:
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    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;
238
    size_t len;
239 240
    int ret = -1;

241 242 243 244
    cmd = virCommandNew(OVSVSCTL);
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "--if-exists", "get", "Interface",
                         ifname, "external_ids:PortData", NULL);
245 246 247 248 249

    virCommandSetOutputBuffer(cmd, migrate);

    /* Run the command */
    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
250 251 252
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to run command to get OVS port data for "
                         "interface %s"), ifname);
253 254 255
        goto cleanup;
    }

256
    /* Wipeout the newline, if it exists */
257 258 259
    len = strlen(*migrate);
    if (len > 0)
        (*migrate)[len - 1] = '\0';
260

A
Ansis Atteka 已提交
261
    ret = 0;
262
 cleanup:
J
John Ferlan 已提交
263
    virCommandFree(cmd);
264 265
    return ret;
}
A
Ansis Atteka 已提交
266

267 268 269 270 271 272 273 274 275 276 277 278 279 280
/**
 * 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;

281 282 283 284 285
    if (!migrate) {
        VIR_DEBUG("No OVS port data for interface %s", ifname);
        return 0;
    }

286 287 288
    cmd = virCommandNew(OVSVSCTL);
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "set", "Interface", ifname, NULL);
289 290 291 292
    virCommandAddArgFormat(cmd, "external_ids:PortData=%s", migrate);

    /* Run the command */
    if (virCommandRun(cmd, NULL) < 0) {
J
Jiri Denemark 已提交
293 294 295
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to run command to set OVS port data for "
                         "interface %s"), ifname);
296 297 298 299
        goto cleanup;
    }

    ret = 0;
300
 cleanup:
J
John Ferlan 已提交
301
    virCommandFree(cmd);
302
    return ret;
A
Ansis Atteka 已提交
303
}
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

/**
 * virNetDevOpenvswitchInterfaceStats:
 * @ifname: the name of the interface
 * @stats: the retreived domain interface stat
 *
 * Retrieves the OVS interfaces stats
 *
 * Returns 0 in case of success or -1 in case of failure
 */
int
virNetDevOpenvswitchInterfaceStats(const char *ifname,
                                   virDomainInterfaceStatsPtr stats)
{
    virCommandPtr cmd = NULL;
    char *output;
320 321
    char *tmp;
    bool gotStats = false;
322 323 324
    int ret = -1;

    /* Just ensure the interface exists in ovs */
325 326 327
    cmd = virCommandNew(OVSVSCTL);
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "get", "Interface", ifname, "name", NULL);
328 329 330 331 332 333 334 335 336
    virCommandSetOutputBuffer(cmd, &output);

    if (virCommandRun(cmd, NULL) < 0) {
        /* no ovs-vsctl or interface 'ifname' doesn't exists in ovs */
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Interface not found"));
        goto cleanup;
    }

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
#define GET_STAT(name, member)                                              \
    do {                                                                    \
        VIR_FREE(output);                                                   \
        virCommandFree(cmd);                                                \
        cmd = virCommandNew(OVSVSCTL);                                      \
        virNetDevOpenvswitchAddTimeout(cmd);                                \
        virCommandAddArgList(cmd, "get", "Interface", ifname,               \
                             "statistics:" name, NULL);                     \
        virCommandSetOutputBuffer(cmd, &output);                            \
        if (virCommandRun(cmd, NULL) < 0) {                                 \
            stats->member = -1;                                             \
        } else {                                                            \
            if (virStrToLong_ll(output, &tmp, 10, &stats->member) < 0 ||    \
                *tmp != '\n') {                                             \
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",                \
                               _("Fail to parse ovs-vsctl output"));        \
                goto cleanup;                                               \
            }                                                               \
            gotStats = true;                                                \
        }                                                                   \
    } while (0)
358 359 360

    /* The TX/RX fields appear to be swapped here
     * because this is the host view. */
361 362 363 364 365 366 367 368 369 370
    GET_STAT("rx_bytes", tx_bytes);
    GET_STAT("rx_packets", tx_packets);
    GET_STAT("rx_errors", tx_errs);
    GET_STAT("rx_dropped", tx_drop);
    GET_STAT("tx_bytes", rx_bytes);
    GET_STAT("tx_packets", rx_packets);
    GET_STAT("tx_errors", rx_errs);
    GET_STAT("tx_dropped", rx_drop);

    if (!gotStats) {
371
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
372
                       _("Interface doesn't have any statistics"));
373 374 375 376 377 378 379 380 381 382
        goto cleanup;
    }

    ret = 0;

 cleanup:
    VIR_FREE(output);
    virCommandFree(cmd);
    return ret;
}
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

/**
 * virNetDevOpenvswitchVhostuserGetIfname:
 * @path: the path of the unix socket
 * @ifname: the retrieved name of the interface
 *
 * Retreives the ovs ifname from vhostuser unix socket path.
 *
 * Returns: 1 if interface is an openvswitch interface,
 *          0 if it is not, but no other error occurred,
 *         -1 otherwise.
 */
int
virNetDevOpenvswitchGetVhostuserIfname(const char *path,
                                       char **ifname)
{
    virCommandPtr cmd = NULL;
    char *tmpIfname = NULL;
    char **tokens = NULL;
    size_t ntokens = 0;
    int status;
    int ret = -1;
405
    char *ovs_timeout = NULL;
406 407 408 409 410 411 412 413 414 415 416 417 418

    /* Openvswitch vhostuser path are hardcoded to
     * /<runstatedir>/openvswitch/<ifname>
     * for example: /var/run/openvswitch/dpdkvhostuser0
     *
     * so we pick the filename and check it's a openvswitch interface
     */
    if (!path ||
        !(tmpIfname = strrchr(path, '/'))) {
        ret = 0;
        goto cleanup;
    }

419
    tmpIfname++;
420 421 422
    cmd = virCommandNew(OVSVSCTL);
    virNetDevOpenvswitchAddTimeout(cmd);
    virCommandAddArgList(cmd, "get", "Interface", tmpIfname, "name", NULL);
423 424 425 426 427 428 429 430 431 432 433 434 435 436
    if (virCommandRun(cmd, &status) < 0 ||
        status) {
        /* it's not a openvswitch vhostuser interface. */
        ret = 0;
        goto cleanup;
    }

    if (VIR_STRDUP(*ifname, tmpIfname) < 0)
        goto cleanup;
    ret = 1;

 cleanup:
    virStringListFreeCount(tokens, ntokens);
    virCommandFree(cmd);
437
    VIR_FREE(ovs_timeout);
438 439
    return ret;
}