virnetdevvportprofile.c 46.2 KB
Newer Older
1
/*
L
Laine Stump 已提交
2
 * Copyright (C) 2009-2016 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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/>.
17 18 19 20 21
 */

#include <config.h>

#include "virnetdevvportprofile.h"
22
#include "virerror.h"
23
#include "viralloc.h"
24
#include "virstring.h"
25 26 27

#define VIR_FROM_THIS VIR_FROM_NET

28 29
VIR_ENUM_IMPL(virNetDevVPort,
              VIR_NETDEV_VPORT_PROFILE_LAST,
30 31 32
              "none",
              "802.1Qbg",
              "802.1Qbh",
33
              "openvswitch",
34 35
              "midonet",
);
36

37 38
VIR_ENUM_IMPL(virNetDevVPortProfileOp,
              VIR_NETDEV_VPORT_PROFILE_OP_LAST,
39 40 41 42 43 44 45
              "create",
              "save",
              "restore",
              "destroy",
              "migrate out",
              "migrate in start",
              "migrate in finish",
46 47
              "no-op",
);
48 49 50 51 52 53 54 55

#if WITH_VIRTUALPORT

# include <fcntl.h>
# include <c-ctype.h>
# include <sys/socket.h>
# include <sys/ioctl.h>

56
# include <net/if.h>
57 58
# include <linux/if_tun.h>

59
# include "virnetlink.h"
60
# include "virfile.h"
61
# include "virlog.h"
62 63
# include "virnetdev.h"

64 65
VIR_LOG_INIT("util.netdevvportprofile");

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
# define MICROSEC_PER_SEC       (1000 * 1000)

# define NLMSGBUF_SIZE  256
# define RATTBUF_SIZE   64


# define STATUS_POLL_TIMEOUT_USEC (10 * MICROSEC_PER_SEC)
# define STATUS_POLL_INTERVL_USEC (MICROSEC_PER_SEC / 8)

# define LLDPAD_PID_FILE  "/var/run/lldpad.pid"


enum virNetDevVPortProfileLinkOp {
    VIR_NETDEV_VPORT_PROFILE_LINK_OP_ASSOCIATE = 0x1,
    VIR_NETDEV_VPORT_PROFILE_LINK_OP_DISASSOCIATE = 0x2,
    VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE = 0x3,
    VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE_RR = 0x4,
};

#endif
86 87

bool
88
virNetDevVPortProfileEqual(const virNetDevVPortProfile *a, const virNetDevVPortProfile *b)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
    /* NULL resistant */
    if (!a && !b)
        return true;

    if (!a || !b)
        return false;

    if (a->virtPortType != b->virtPortType)
        return false;

    switch (a->virtPortType) {
    case VIR_NETDEV_VPORT_PROFILE_NONE:
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBG:
105 106 107 108
        if (a->managerID != b->managerID ||
            a->typeID != b->typeID ||
            a->typeIDVersion != b->typeIDVersion ||
            memcmp(a->instanceID, b->instanceID, VIR_UUID_BUFLEN) != 0)
109 110 111 112
            return false;
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBH:
113
        if (STRNEQ(a->profileID, b->profileID))
114 115 116
            return false;
        break;

117 118 119 120 121 122
    case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
        if (STRNEQ(a->profileID, b->profileID) ||
            memcmp(a->interfaceID, b->interfaceID, VIR_UUID_BUFLEN) != 0)
            return false;
        break;

123 124 125 126 127 128
    default:
        break;
    }

    return true;
}
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

int virNetDevVPortProfileCopy(virNetDevVPortProfilePtr *dst, const virNetDevVPortProfile *src)
{
    if (!src) {
        *dst = NULL;
        return 0;
    }

    if (VIR_ALLOC(*dst) < 0)
        return -1;

    memcpy(*dst, src, sizeof(*src));
    return 0;
}


146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/* virNetDevVPortProfileCheckComplete() checks that all attributes
 * required for the type of virtport are specified. When
 * generateMissing is true, any missing attribute that can be
 * autogenerated, will be (instanceid, interfaceid). If virtport ==
 * NULL or virtPortType == NONE, then the result is always 0
 * (success). If a required attribute is missing, an error is logged
 * and -1 is returned.
 */
int
virNetDevVPortProfileCheckComplete(virNetDevVPortProfilePtr virtport,
                                   bool generateMissing)
{
    const char *missing = NULL;

    if (!virtport || virtport->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)
        return 0;

    switch (virtport->virtPortType) {
    case VIR_NETDEV_VPORT_PROFILE_8021QBG:
        if (!virtport->managerID_specified) {
            missing = "managerid";
        } else if (!virtport->typeID_specified) {
            missing = "typeid";
        } else if (!virtport->typeIDVersion_specified) {
            missing = "typeidversion";
        } else if (!virtport->instanceID_specified) {
            if (generateMissing) {
                if (virUUIDGenerate(virtport->instanceID) < 0) {
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                   _("cannot generate a random uuid for instanceid"));
                    return -1;
                }
                virtport->instanceID_specified = true;
            } else {
                missing = "instanceid";
            }
        }
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBH:
        if (!virtport->profileID[0])
            missing = "profileid";
        break;

    case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
        /* profileid is optional for openvswitch */
        if (!virtport->interfaceID_specified) {
            if (generateMissing) {
                if (virUUIDGenerate(virtport->interfaceID) < 0) {
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                   _("cannot generate a random uuid for interfaceid"));
                    return -1;
                }
                virtport->interfaceID_specified = true;
            } else {
                missing = "interfaceid";
            }
        }
        break;
205 206 207 208 209

    case VIR_NETDEV_VPORT_PROFILE_MIDONET:
       if (!virtport->interfaceID_specified)
          missing = "interfaceid";
       break;
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    }

    if (missing) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("missing %s in <virtualport type='%s'>"), missing,
                       virNetDevVPortTypeToString(virtport->virtPortType));
        return -1;
    }

    return 0;
}

/* virNetDevVPortProfileCheckNoExtras() checks that there are no
 * attributes specified in this virtport that are inappropriate for
 * the type. if virtport == NULL or virtPortType == NONE, then the
 * result is always 0 (success). If an extra attribute is present,
 * an error is logged and -1 is returned.
 */
int
229
virNetDevVPortProfileCheckNoExtras(const virNetDevVPortProfile *virtport)
230 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 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
{
    const char *extra = NULL;

    if (!virtport || virtport->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)
        return 0;

    switch (virtport->virtPortType) {
    case VIR_NETDEV_VPORT_PROFILE_8021QBG:
        if (virtport->profileID[0])
            extra = "profileid";
        else if (virtport->interfaceID_specified)
            extra = "interfaceid";
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBH:
        if (virtport->managerID_specified)
            extra = "managerid";
        else if (virtport->typeID_specified)
            extra = "typeid";
        else if (virtport->typeIDVersion_specified)
            extra = "typeidversion";
        else if (virtport->instanceID_specified)
            extra = "instanceid";
        else if (virtport->interfaceID_specified)
            extra = "interfaceid";
        break;

    case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
        if (virtport->managerID_specified)
            extra = "managerid";
        else if (virtport->typeID_specified)
            extra = "typeid";
        else if (virtport->typeIDVersion_specified)
            extra = "typeidversion";
        else if (virtport->instanceID_specified)
            extra = "instanceid";
        break;
    }

    if (extra) {
        virReportError(VIR_ERR_XML_ERROR,
                       _("extra %s unsupported in <virtualport type='%s'>"),
                       extra,
                       virNetDevVPortTypeToString(virtport->virtPortType));
        return -1;
    }

    return 0;
}

/* virNetDevVPortProfileMerge() - merge the attributes in mods into
 * orig. If anything that is set in mods has already been set in orig
 * *and doesn't match*, log an error and return -1, otherwise return 0.
 */
static int
virNetDevVPortProfileMerge(virNetDevVPortProfilePtr orig,
286
                           const virNetDevVPortProfile *mods)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
{
    enum virNetDevVPortProfile otype;

    if (!orig || !mods)
        return 0;

    otype = orig->virtPortType;

    if (mods->virtPortType != VIR_NETDEV_VPORT_PROFILE_NONE) {
        if (otype != VIR_NETDEV_VPORT_PROFILE_NONE &&
            otype != mods->virtPortType) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports "
                             "with mismatched types (%s and %s)"),
                           virNetDevVPortTypeToString(otype),
                           virNetDevVPortTypeToString(mods->virtPortType));
            return -1;
        }
        otype = orig->virtPortType = mods->virtPortType;
    }

    if (mods->managerID_specified &&
        (otype == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->managerID_specified &&
            (orig->managerID != mods->managerID)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports "
                             "with mismatched managerids (%d and %d)"),
                           orig->managerID, mods->managerID);
            return -1;
        }
        orig->managerID = mods->managerID;
        orig->managerID_specified = true;
    }

    if (mods->typeID_specified &&
        (otype == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->typeID_specified &&
            (orig->typeID != mods->typeID)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports "
                             "with mismatched typeids (%d and %d)"),
                           orig->typeID, mods->typeID);
            return -1;
        }
        orig->typeID = mods->typeID;
        orig->typeID_specified = true;
    }

    if (mods->typeIDVersion_specified &&
        (otype == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->typeIDVersion_specified &&
            (orig->typeIDVersion != mods->typeIDVersion)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports with "
                             "mismatched typeidversions (%d and %d)"),
                           orig->typeIDVersion, mods->typeIDVersion);
            return -1;
        }
        orig->typeIDVersion = mods->typeIDVersion;
        orig->typeIDVersion_specified = true;
    }

    if (mods->instanceID_specified &&
        (otype == VIR_NETDEV_VPORT_PROFILE_8021QBG ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->instanceID_specified &&
            memcmp(orig->instanceID, mods->instanceID,
                   sizeof(orig->instanceID))) {
            char origuuid[VIR_UUID_STRING_BUFLEN];
            char modsuuid[VIR_UUID_STRING_BUFLEN];

            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports with "
                             "mismatched instanceids ('%s' and '%s')"),
                           virUUIDFormat(orig->instanceID, origuuid),
                           virUUIDFormat(mods->instanceID, modsuuid));
            return -1;
        }
        memcpy(orig->instanceID, mods->instanceID, sizeof(orig->instanceID));
        orig->instanceID_specified = true;
    }

    if (mods->interfaceID_specified &&
        (otype == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->interfaceID_specified &&
            memcmp(orig->interfaceID, mods->interfaceID,
                   sizeof(orig->interfaceID))) {
            char origuuid[VIR_UUID_STRING_BUFLEN];
            char modsuuid[VIR_UUID_STRING_BUFLEN];

            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports with "
                             "mismatched interfaceids ('%s' and '%s')"),
                           virUUIDFormat(orig->interfaceID, origuuid),
                           virUUIDFormat(mods->interfaceID, modsuuid));
            return -1;
        }
        memcpy(orig->interfaceID, mods->interfaceID, sizeof(orig->interfaceID));
        orig->interfaceID_specified = true;
    }

    if (mods->profileID[0] &&
        (otype == VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH ||
         otype == VIR_NETDEV_VPORT_PROFILE_8021QBH ||
         otype == VIR_NETDEV_VPORT_PROFILE_NONE)) {
        if (orig->profileID[0] &&
            STRNEQ(orig->profileID, mods->profileID)) {
            virReportError(VIR_ERR_XML_ERROR,
                           _("attempt to merge virtualports with "
                             "mismatched profileids ('%s' and '%s')"),
                           orig->profileID, mods->profileID);
            return -1;
        }
405
        if (virStrcpyStatic(orig->profileID, mods->profileID) < 0) {
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
            /* this should never happen - it indicates mods->profileID
             * isn't properly null terminated. */
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                           _("corrupted profileid string"));
            return -1;
        }
    }
    return 0;
}

/* virNetDevVPortProfileMerge3() - create a new virNetDevVPortProfile
 * that is a combination of the three input profiles. fromInterface is
 * highest priority and fromPortgroup is lowest. As lower priority
 * objects' attributes are merged in, if the attribute is unset in the
 * result object, it is set from the lower priority object, but if it
 * is already set in the result and the lower priority object wants to
 * change it, that is an error.
 */

int virNetDevVPortProfileMerge3(virNetDevVPortProfilePtr *result,
426 427 428
                                const virNetDevVPortProfile *fromInterface,
                                const virNetDevVPortProfile *fromNetwork,
                                const virNetDevVPortProfile *fromPortgroup)
429 430 431 432 433 434 435 436 437 438 439
{
    int ret = -1;
    *result = NULL;

    if ((!fromInterface || (fromInterface->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE)) &&
        (!fromNetwork   || (fromNetwork->virtPortType   == VIR_NETDEV_VPORT_PROFILE_NONE)) &&
        (!fromPortgroup || (fromPortgroup->virtPortType == VIR_NETDEV_VPORT_PROFILE_NONE))) {
        return 0;
    }

    /* at least one of the source profiles is non-empty */
440
    if (VIR_ALLOC(*result) < 0)
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
        return ret;

    /* start with the interface's profile. There are no pointers in a
     * virtualPortProfile, so a shallow copy is sufficient.
     */
    if (fromInterface)
        **result = *fromInterface;

    if (virNetDevVPortProfileMerge(*result, fromNetwork) < 0)
        goto error;
    if (virNetDevVPortProfileMerge(*result, fromPortgroup) < 0)
        goto error;

    ret = 0;

456
 error:
457 458 459 460 461
    if (ret < 0)
        VIR_FREE(*result);
    return ret;
}

462

463
#if WITH_VIRTUALPORT
464 465 466

static struct nla_policy ifla_port_policy[IFLA_PORT_MAX + 1] =
{
467
    [IFLA_PORT_RESPONSE] = { .type = NLA_U16 },
468 469 470
};

static uint32_t
471 472
virNetDevVPortProfileGetLldpadPid(void)
{
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
    int fd;
    uint32_t pid = 0;

    fd = open(LLDPAD_PID_FILE, O_RDONLY);
    if (fd >= 0) {
        char buffer[10];

        if (saferead(fd, buffer, sizeof(buffer)) <= sizeof(buffer)) {
            unsigned int res;
            char *endptr;

            if (virStrToLong_ui(buffer, &endptr, 10, &res) == 0
                && (*endptr == '\0' || c_isspace(*endptr))
                && res != 0) {
                pid = res;
            } else {
489
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
                               _("error parsing pid of lldpad"));
            }
        }
    } else {
        virReportSystemError(errno,
                             _("Error opening file %s"), LLDPAD_PID_FILE);
    }

    VIR_FORCE_CLOSE(fd);

    return pid;
}

/**
 * virNetDevVPortProfileGetStatus:
 *
 * tb: top level netlink response attributes + values
 * vf: The virtual function used in the request
 * instanceId: instanceId of the interface (vm uuid in case of 802.1Qbh)
 * is8021Qbg: whether this function is call for 8021Qbg
 * status: pointer to a uint16 where the status will be written into
 *
 * Get the status from the IFLA_PORT_RESPONSE field; Returns 0 in
 * case of success, < 0 otherwise with error having been reported
 */
static int
virNetDevVPortProfileGetStatus(struct nlattr **tb, int32_t vf,
                               const unsigned char *instanceId,
                               bool nltarget_kernel,
                               bool is8021Qbg,
                               uint16_t *status)
{
    int rc = -1;
    struct nlattr *tb_port[IFLA_PORT_MAX + 1] = { NULL, };

    if (vf == PORT_SELF_VF && nltarget_kernel) {
        if (tb[IFLA_PORT_SELF]) {
            if (nla_parse_nested(tb_port, IFLA_PORT_MAX, tb[IFLA_PORT_SELF],
                                 ifla_port_policy)) {
529
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
530 531
                               _("error parsing IFLA_PORT_SELF part"));
                goto cleanup;
532 533
            }
        } else {
534
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
535 536
                           _("IFLA_PORT_SELF is missing"));
            goto cleanup;
537 538 539 540 541 542 543 544 545 546
        }
    } else {
        if (tb[IFLA_VF_PORTS]) {
            int rem;
            bool found = false;
            struct nlattr *tb_vf_ports = { NULL, };

            nla_for_each_nested(tb_vf_ports, tb[IFLA_VF_PORTS], rem) {

                if (nla_type(tb_vf_ports) != IFLA_VF_PORT) {
547
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
548 549 550
                                   _("error while iterating over "
                                     "IFLA_VF_PORTS part"));
                    goto cleanup;
551 552 553 554
                }

                if (nla_parse_nested(tb_port, IFLA_PORT_MAX, tb_vf_ports,
                                     ifla_port_policy)) {
555
                    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
556 557
                                   _("error parsing IFLA_VF_PORT part"));
                    goto cleanup;
558 559
                }

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
                /* This ensures that the given VF is present in the
                 * IFLA_VF_PORTS list, and that its uuid matches the
                 * instanceId (in case we've associated it). If no
                 * instanceId is sent from the caller, that means we've
                 * disassociated it from this instanceId, and the uuid
                 * will either be unset (if still not associated with
                 * anything) or will be set to a new and different uuid
                 */
                if ((tb_port[IFLA_PORT_VF] &&
                     vf == *(uint32_t *)RTA_DATA(tb_port[IFLA_PORT_VF])) &&
                    (!instanceId ||
                     (tb_port[IFLA_PORT_INSTANCE_UUID] &&
                      !memcmp(instanceId,
                              (unsigned char *)
                              RTA_DATA(tb_port[IFLA_PORT_INSTANCE_UUID]),
                              VIR_UUID_BUFLEN)))) {
576 577 578 579 580 581
                        found = true;
                        break;
                }
            }

            if (!found) {
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
                char instanceIdStr[VIR_UUID_STRING_BUFLEN] = "(none)";

                if (instanceId)
                   virUUIDFormat(instanceId, instanceIdStr);

                virReportError(VIR_ERR_INTERNAL_ERROR,
                               _("Could not find vf/instanceId %u/%s "
                                 " in netlink response"),
                               vf, instanceIdStr);

                /* go through all the entries again. This seems tedious,
                 * but experience has shown the resulting log to be
                 * very useful.
                 */
                VIR_WARN("IFLA_VF_PORTS entries that were returned:");
                nla_for_each_nested(tb_vf_ports, tb[IFLA_VF_PORTS], rem) {
                    char uuidstr[VIR_UUID_STRING_BUFLEN] = "(none)";

                    if (nla_parse_nested(tb_port, IFLA_PORT_MAX, tb_vf_ports,
                                         ifla_port_policy)) {
                        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                                       _("error parsing IFLA_VF_PORT "
                                         "during error reporting"));
                        goto cleanup;
                    }
                    if (tb_port[IFLA_PORT_INSTANCE_UUID]) {
                        virUUIDFormat((unsigned char *)
                                      RTA_DATA(tb_port[IFLA_PORT_INSTANCE_UUID]),
                                      uuidstr);
                    }
                    VIR_WARN("  vf: %d uuid: %s",
                             tb_port[IFLA_PORT_VF] ?
                             *(uint32_t *)RTA_DATA(tb_port[IFLA_PORT_VF]) : -1,
                             uuidstr);
L
Laine Stump 已提交
616 617
                }
                goto cleanup;
618 619
            }
        } else {
620
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
621 622
                           _("IFLA_VF_PORTS is missing"));
            goto cleanup;
623 624 625 626 627 628 629 630 631 632 633 634
        }
    }

    if (tb_port[IFLA_PORT_RESPONSE]) {
        *status = *(uint16_t *)RTA_DATA(tb_port[IFLA_PORT_RESPONSE]);
        rc = 0;
    } else {
        if (is8021Qbg) {
            /* no in-progress here; may be missing */
            *status = PORT_PROFILE_RESPONSE_INPROGRESS;
            rc = 0;
        } else {
635
            virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
636 637
                           _("no IFLA_PORT_RESPONSE found in netlink message"));
            goto cleanup;
638 639
        }
    }
640
 cleanup:
641 642 643 644 645 646 647
    return rc;
}


static int
virNetDevVPortProfileOpSetLink(const char *ifname, int ifindex,
                               bool nltarget_kernel,
648
                               const virMacAddr *macaddr,
649 650 651 652 653 654 655 656 657
                               int vlanid,
                               const char *profileId,
                               struct ifla_port_vsi *portVsi,
                               const unsigned char *instanceId,
                               const unsigned char *hostUUID,
                               int32_t vf,
                               uint8_t op)
{
    int rc = -1;
658
    struct nlmsghdr *resp = NULL;
659 660 661 662 663 664
    struct nlmsgerr *err;
    struct ifinfomsg ifinfo = {
        .ifi_family = AF_UNSPEC,
        .ifi_index  = ifindex,
    };
    unsigned int recvbuflen = 0;
665 666
    int src_pid = 0;
    uint32_t dst_pid = 0;
667 668
    struct nl_msg *nl_msg;
    struct nlattr *vfports = NULL, *vfport;
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
    char macStr[VIR_MAC_STRING_BUFLEN];
    char hostUUIDStr[VIR_UUID_STRING_BUFLEN];
    char instanceUUIDStr[VIR_UUID_STRING_BUFLEN];
    const char *opName;

    switch (op) {
    case PORT_REQUEST_PREASSOCIATE:
        opName = "PREASSOCIATE";
        break;
    case PORT_REQUEST_PREASSOCIATE_RR:
        opName = "PREASSOCIATE_RR";
        break;
    case PORT_REQUEST_ASSOCIATE:
        opName = "ASSOCIATE";
        break;
    case PORT_REQUEST_DISASSOCIATE:
        opName = "DISASSOCIATE";
        break;
    default:
        opName = "(unknown)";
        break;
    }
    VIR_INFO("%s: ifname: %s ifindex: %d vf: %d vlanid: %d mac: %s "
             "profileId: %s instanceId: %s hostUUID: %s",
             opName, ifname ? ifname : "(unspecified)",
             ifindex, vf, vlanid,
             macaddr ? virMacAddrFormat(macaddr, macStr) : "(unspecified)",
             profileId ? profileId : "(unspecified)",
             (instanceId
              ? virUUIDFormat(instanceId, instanceUUIDStr)
              : "(unspecified)"),
             (hostUUID
              ? virUUIDFormat(hostUUID, hostUUIDStr)
              : "(unspecified)"));
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731

    nl_msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_REQUEST);
    if (!nl_msg) {
        virReportOOMError();
        return rc;
    }

    if (nlmsg_append(nl_msg,  &ifinfo, sizeof(ifinfo), NLMSG_ALIGNTO) < 0)
        goto buffer_too_small;

    if (ifname &&
        nla_put(nl_msg, IFLA_IFNAME, strlen(ifname)+1, ifname) < 0)
        goto buffer_too_small;

    if (macaddr || vlanid >= 0) {
        struct nlattr *vfinfolist, *vfinfo;

        if (!(vfinfolist = nla_nest_start(nl_msg, IFLA_VFINFO_LIST)))
            goto buffer_too_small;

        if (!(vfinfo = nla_nest_start(nl_msg, IFLA_VF_INFO)))
            goto buffer_too_small;

        if (macaddr) {
            struct ifla_vf_mac ifla_vf_mac = {
                .vf = vf,
                .mac = { 0, },
            };

732
            virMacAddrGetRaw(macaddr, ifla_vf_mac.mac);
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

            if (nla_put(nl_msg, IFLA_VF_MAC, sizeof(ifla_vf_mac),
                        &ifla_vf_mac) < 0)
                goto buffer_too_small;
        }

        if (vlanid >= 0) {
            struct ifla_vf_vlan ifla_vf_vlan = {
                .vf = vf,
                .vlan = vlanid,
                .qos = 0,
            };

            if (nla_put(nl_msg, IFLA_VF_VLAN, sizeof(ifla_vf_vlan),
                        &ifla_vf_vlan) < 0)
                goto buffer_too_small;
        }

        nla_nest_end(nl_msg, vfinfo);
        nla_nest_end(nl_msg, vfinfolist);
    }

    if (vf == PORT_SELF_VF && nltarget_kernel) {
        if (!(vfport = nla_nest_start(nl_msg, IFLA_PORT_SELF)))
            goto buffer_too_small;
    } else {
        if (!(vfports = nla_nest_start(nl_msg, IFLA_VF_PORTS)))
            goto buffer_too_small;

        /* begin nesting vfports */
        if (!(vfport = nla_nest_start(nl_msg, IFLA_VF_PORT)))
            goto buffer_too_small;
    }

    if (profileId) {
        if (nla_put(nl_msg, IFLA_PORT_PROFILE, strlen(profileId) + 1,
                    profileId) < 0)
            goto buffer_too_small;
    }

    if (portVsi) {
        if (nla_put(nl_msg, IFLA_PORT_VSI_TYPE, sizeof(*portVsi),
                    portVsi) < 0)
            goto buffer_too_small;
    }

    if (instanceId) {
        if (nla_put(nl_msg, IFLA_PORT_INSTANCE_UUID, VIR_UUID_BUFLEN,
                    instanceId) < 0)
            goto buffer_too_small;
    }

    if (hostUUID) {
        if (nla_put(nl_msg, IFLA_PORT_HOST_UUID, VIR_UUID_BUFLEN,
                    hostUUID) < 0)
            goto buffer_too_small;
    }

    if (vf != PORT_SELF_VF) {
        if (nla_put(nl_msg, IFLA_PORT_VF, sizeof(vf), &vf) < 0)
            goto buffer_too_small;
    }

    if (nla_put(nl_msg, IFLA_PORT_REQUEST, sizeof(op), &op) < 0)
        goto buffer_too_small;

    /* end nesting of vport */
    nla_nest_end(nl_msg, vfport);

    if (vfports) {
        /* end nesting of vfports */
        nla_nest_end(nl_msg, vfports);
    }

    if (!nltarget_kernel) {
808
        if ((src_pid = virNetlinkEventServiceLocalPid(NETLINK_ROUTE)) < 0)
809 810
            goto cleanup;
        if ((dst_pid = virNetDevVPortProfileGetLldpadPid()) == 0)
811
            goto cleanup;
812 813
    }

814
    if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
815
                          src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
816
        goto cleanup;
817

818
    if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
819 820 821 822 823 824 825 826 827 828
        goto malformed_resp;

    switch (resp->nlmsg_type) {
    case NLMSG_ERROR:
        err = (struct nlmsgerr *)NLMSG_DATA(resp);
        if (resp->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
            goto malformed_resp;

        if (err->error) {
            virReportSystemError(-err->error,
829 830
                                 _("error during virtual port configuration of ifindex %d"),
                                 ifindex);
831
            goto cleanup;
832 833 834 835 836 837 838 839 840 841 842
        }
        break;

    case NLMSG_DONE:
        break;

    default:
        goto malformed_resp;
    }

    rc = 0;
843
 cleanup:
844
    nlmsg_free(nl_msg);
845
    VIR_FREE(resp);
846 847
    return rc;

848
 malformed_resp:
849
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
850
                   _("malformed netlink response message"));
851
    goto cleanup;
852

853
 buffer_too_small:
854
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
855
                   _("allocated netlink buffer is too small"));
856
    goto cleanup;
857 858 859
}


860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
/**
 * virNetDevVPortProfileGetNthParent
 *
 * @ifname : the name of the interface; ignored if ifindex is valid
 * @ifindex : the index of the interface or -1 if ifname is given
 * @nthParent : the nth parent interface to get
 * @parent_ifindex : pointer to int
 * @parent_ifname : pointer to buffer of size IFNAMSIZ
 * @nth : the nth parent that is actually returned; if for example eth0.100
 *        was given and the 100th parent is to be returned, then eth0 will
 *        most likely be returned with nth set to 1 since the chain does
 *        not have more interfaces
 *
 * Get the nth parent interface of the given interface. 0 is the interface
 * itself.
 *
 * Return 0 on success, < 0 otherwise
 */
static int
virNetDevVPortProfileGetNthParent(const char *ifname, int ifindex, unsigned int nthParent,
                                  int *parent_ifindex, char *parent_ifname,
                                  unsigned int *nth)
{
883 884
    int rc = -1;
    void *nlData = NULL;
885 886
    struct nlattr *tb[IFLA_MAX + 1] = { NULL, };
    bool end = false;
887
    size_t i = 0;
888 889 890 891 892 893 894

    *nth = 0;

    if (ifindex <= 0 && virNetDevGetIndex(ifname, &ifindex) < 0)
        return -1;

    while (!end && i <= nthParent) {
895
        VIR_FREE(nlData);
896
        rc = virNetlinkDumpLink(ifname, ifindex, &nlData, tb, 0, 0);
897 898 899 900
        if (rc < 0)
            break;

        if (tb[IFLA_IFNAME]) {
901 902
            if (virStrcpy(parent_ifname, (char*)RTA_DATA(tb[IFLA_IFNAME]),
                          IFNAMSIZ) < 0) {
903
                virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
904
                               _("buffer for root interface name is too small"));
905 906
                rc = -1;
                goto cleanup;
907 908 909 910 911 912 913
            }
            *parent_ifindex = ifindex;
        }

        if (tb[IFLA_LINK]) {
            ifindex = *(int *)RTA_DATA(tb[IFLA_LINK]);
            ifname = NULL;
914
        } else {
915
            end = true;
916
        }
917 918 919 920 921 922

        i++;
    }

    *nth = i - 1;

923 924
 cleanup:
    VIR_FREE(nlData);
925 926 927 928
    return rc;
}


929 930 931 932
/* Returns 0 on success, -1 on general failure, and -2 on timeout */
static int
virNetDevVPortProfileOpCommon(const char *ifname, int ifindex,
                              bool nltarget_kernel,
933
                              const virMacAddr *macaddr,
934 935 936 937 938 939
                              int vlanid,
                              const char *profileId,
                              struct ifla_port_vsi *portVsi,
                              const unsigned char *instanceId,
                              const unsigned char *hostUUID,
                              int32_t vf,
940 941
                              uint8_t op,
                              bool setlink_only)
942 943
{
    int rc;
944 945
    int src_pid = 0;
    uint32_t dst_pid = 0;
946
    void *nlData = NULL;
E
Eric Blake 已提交
947
    struct nlattr *tb[IFLA_MAX + 1] = { NULL, };
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
    int repeats = STATUS_POLL_TIMEOUT_USEC / STATUS_POLL_INTERVL_USEC;
    uint16_t status = 0;
    bool is8021Qbg = (profileId == NULL);

    rc = virNetDevVPortProfileOpSetLink(ifname, ifindex,
                                        nltarget_kernel,
                                        macaddr,
                                        vlanid,
                                        profileId,
                                        portVsi,
                                        instanceId,
                                        hostUUID,
                                        vf,
                                        op);
    if (rc < 0) {
963
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
964 965 966 967
                       _("sending of PortProfileRequest failed."));
        return rc;
    }

968 969 970
    if (setlink_only) /*for re-associations on existing links*/
        return 0;

971
    if (!nltarget_kernel &&
972
        (((src_pid = virNetlinkEventServiceLocalPid(NETLINK_ROUTE)) < 0) ||
973 974 975 976 977
         ((dst_pid = virNetDevVPortProfileGetLldpadPid()) == 0))) {
        rc = -1;
        goto cleanup;
    }

978
    while (--repeats >= 0) {
979
        VIR_FREE(nlData);
980
        rc = virNetlinkDumpLink(NULL, ifindex, &nlData, tb, src_pid, dst_pid);
981
        if (rc < 0)
982
            goto cleanup;
983 984 985 986

        rc = virNetDevVPortProfileGetStatus(tb, vf, instanceId, nltarget_kernel,
                                            is8021Qbg, &status);
        if (rc < 0)
987
            goto cleanup;
988 989 990 991 992 993 994
        if (status == PORT_PROFILE_RESPONSE_SUCCESS ||
            status == PORT_VDP_RESPONSE_SUCCESS) {
            break;
        } else if (status == PORT_PROFILE_RESPONSE_INPROGRESS) {
            /* keep trying... */
        } else {
            virReportSystemError(EINVAL,
995 996 997
                                 _("error %d during port-profile setlink on "
                                   "interface %s (%d)"),
                                 status, ifname, ifindex);
998 999 1000 1001
            rc = -1;
            break;
        }

1002
        g_usleep(STATUS_POLL_INTERVL_USEC);
1003 1004 1005
    }

    if (status == PORT_PROFILE_RESPONSE_INPROGRESS) {
1006
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
1007 1008 1009 1010
                       _("port-profile setlink timed out"));
        rc = -2;
    }

1011
 cleanup:
1012
    VIR_FREE(nlData);
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
    return rc;
}


static int
virNetDevVPortProfileGetPhysdevAndVlan(const char *ifname, int *root_ifindex, char *root_ifname,
                                       int *vlanid)
{
    int ret;
    unsigned int nth;
    int ifindex = -1;

    *vlanid = -1;
    while (1) {
1027 1028
        if ((ret = virNetDevVPortProfileGetNthParent(ifname, ifindex, 1,
                                                     root_ifindex, root_ifname, &nth)) < 0)
1029 1030 1031 1032
            return ret;
        if (nth == 0)
            break;
        if (*vlanid == -1) {
1033 1034
            if (virNetDevGetVLanID(root_ifname, vlanid) < 0) {
                virResetLastError();
1035
                *vlanid = -1;
1036
            }
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
        }

        ifindex = *root_ifindex;
        ifname = NULL;
    }

    return 0;
}

/* Returns 0 on success, -1 on general failure, and -2 on timeout */
static int
virNetDevVPortProfileOp8021Qbg(const char *ifname,
1049
                               const virMacAddr *macaddr,
1050
                               int vf,
1051
                               const virNetDevVPortProfile *virtPort,
1052 1053
                               enum virNetDevVPortProfileLinkOp virtPortOp,
                               bool setlink_only)
1054
{
1055
    int rc = -1;
1056 1057
    int op = PORT_REQUEST_ASSOCIATE;
    struct ifla_port_vsi portVsi = {
1058 1059
        .vsi_mgr_id       = virtPort->managerID,
        .vsi_type_version = virtPort->typeIDVersion,
1060 1061 1062 1063 1064
    };
    bool nltarget_kernel = false;
    int vlanid;
    int physdev_ifindex = 0;
    char physdev_ifname[IFNAMSIZ] = { 0, };
1065 1066 1067 1068 1069

    if (!ifname)
        return -1;

    vf = PORT_SELF_VF;
1070

1071 1072 1073
    if (virNetDevVPortProfileGetPhysdevAndVlan(ifname, &physdev_ifindex,
                                               physdev_ifname, &vlanid) < 0) {
        goto cleanup;
1074 1075 1076 1077 1078
    }

    if (vlanid < 0)
        vlanid = 0;

1079 1080 1081
    portVsi.vsi_type_id[2] = virtPort->typeID >> 16;
    portVsi.vsi_type_id[1] = virtPort->typeID >> 8;
    portVsi.vsi_type_id[0] = virtPort->typeID;
1082 1083 1084 1085 1086

    switch (virtPortOp) {
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE:
        op = PORT_REQUEST_PREASSOCIATE;
        break;
1087 1088 1089
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE_RR:
        op = PORT_REQUEST_PREASSOCIATE_RR;
        break;
1090 1091 1092 1093 1094 1095 1096
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_ASSOCIATE:
        op = PORT_REQUEST_ASSOCIATE;
        break;
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_DISASSOCIATE:
        op = PORT_REQUEST_DISASSOCIATE;
        break;
    default:
1097
        virReportError(VIR_ERR_INTERNAL_ERROR,
1098
                       _("operation type %d not supported"), virtPortOp);
1099
        goto cleanup;
1100 1101 1102 1103 1104 1105 1106 1107
    }

    rc = virNetDevVPortProfileOpCommon(physdev_ifname, physdev_ifindex,
                                       nltarget_kernel,
                                       macaddr,
                                       vlanid,
                                       NULL,
                                       &portVsi,
1108
                                       virtPort->instanceID,
1109 1110
                                       NULL,
                                       vf,
1111 1112
                                       op,
                                       setlink_only);
1113
 cleanup:
1114 1115 1116 1117 1118 1119
    return rc;
}

/* Returns 0 on success, -1 on general failure, and -2 on timeout */
static int
virNetDevVPortProfileOp8021Qbh(const char *ifname,
1120
                               const virMacAddr *macaddr,
1121
                               int32_t vf,
1122
                               const virNetDevVPortProfile *virtPort,
1123 1124 1125 1126 1127 1128 1129 1130 1131
                               const unsigned char *vm_uuid,
                               enum virNetDevVPortProfileLinkOp virtPortOp)
{
    int rc = 0;
    char *physfndev = NULL;
    unsigned char hostuuid[VIR_UUID_BUFLEN];
    bool nltarget_kernel = true;
    int ifindex;
    int vlanid = -1;
1132
    bool is_vf = false;
1133

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
    if (vf == -1) {
        int isvf_ret = virNetDevIsVirtualFunction(ifname);

        if (isvf_ret == -1)
            goto cleanup;
        is_vf = !!isvf_ret;
    }

    if (is_vf) {
        if (virNetDevGetVirtualFunctionInfo(ifname, &physfndev, &vf) < 0) {
            rc = -1;
            goto cleanup;
        }
    } else {
1148
        physfndev = g_strdup(ifname);
1149
    }
1150

1151
    rc = virNetDevGetIndex(physfndev, &ifindex);
1152
    if (rc < 0)
1153
        goto cleanup;
1154 1155 1156 1157 1158 1159 1160

    switch (virtPortOp) {
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE_RR:
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_ASSOCIATE:
        errno = virGetHostUUID(hostuuid);
        if (errno) {
            rc = -1;
1161
            goto cleanup;
1162 1163 1164 1165 1166 1167
        }

        rc = virNetDevVPortProfileOpCommon(NULL, ifindex,
                                           nltarget_kernel,
                                           macaddr,
                                           vlanid,
1168
                                           virtPort->profileID,
1169 1170 1171 1172 1173 1174 1175
                                           NULL,
                                           vm_uuid,
                                           hostuuid,
                                           vf,
                                           (virtPortOp ==
                                            VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE_RR) ?
                                           PORT_REQUEST_PREASSOCIATE_RR
1176 1177
                                           : PORT_REQUEST_ASSOCIATE,
                                           false);
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
        if (rc == -2)
            /* Association timed out, disassociate */
            virNetDevVPortProfileOpCommon(NULL, ifindex,
                                          nltarget_kernel,
                                          NULL,
                                          vlanid,
                                          NULL,
                                          NULL,
                                          NULL,
                                          NULL,
                                          vf,
1189 1190
                                          PORT_REQUEST_DISASSOCIATE,
                                          false);
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
        break;

    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_DISASSOCIATE:
        rc = virNetDevVPortProfileOpCommon(NULL, ifindex,
                                           nltarget_kernel,
                                           NULL,
                                           vlanid,
                                           NULL,
                                           NULL,
                                           NULL,
                                           NULL,
                                           vf,
1203 1204
                                           PORT_REQUEST_DISASSOCIATE,
                                           false);
1205 1206
        break;

1207
    case VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE:
1208
        virReportError(VIR_ERR_INTERNAL_ERROR,
1209 1210
                       _("operation type %d not supported"), virtPortOp);
        rc = -1;
1211 1212 1213 1214 1215
        break;
    default:
        virReportEnumRangeError(virNetDevVPortProfileType, virtPortOp);
        rc = -1;
        break;
1216 1217
    }

1218
 cleanup:
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
    VIR_FREE(physfndev);
    return rc;
}

/**
 * virNetDevVPortProfileAssociate:
 *
 * @macvtap_ifname: The name of the macvtap device
 * @virtPort: pointer to the object holding port profile parameters
 * @vmuuid : the UUID of the virtual machine
 * @vmOp : The VM operation (i.e., create, no-op)
1230
 * @setlink_only : Only set the link - dont wait for the link to come up
1231
 *
E
Eric Blake 已提交
1232
 * Associate a port on a switch with a profile. This function
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
 * may notify a kernel driver or an external daemon to run
 * the setup protocol. If profile parameters were not supplied
 * by the user, then this function returns without doing
 * anything.
 *
 * Returns 0 in case of success, < 0 otherwise with error
 * having been reported.
 */
int
virNetDevVPortProfileAssociate(const char *macvtap_ifname,
1243 1244
                               const virNetDevVPortProfile *virtPort,
                               const virMacAddr *macvtap_macaddr,
1245
                               const char *linkdev,
1246
                               int vf,
1247
                               const unsigned char *vmuuid,
1248
                               virNetDevVPortProfileOp vmOp,
1249
                               bool setlink_only)
1250 1251
{
    int rc = 0;
1252 1253
    char uuidStr[VIR_UUID_STRING_BUFLEN];
    char macStr[VIR_MAC_STRING_BUFLEN];
1254

1255 1256
    VIR_DEBUG("profile:'%p' vmOp: %s device: %s@%s mac: %s uuid: %s",
              virtPort, virNetDevVPortProfileOpTypeToString(vmOp),
J
Ján Tomko 已提交
1257
              NULLSTR_EMPTY(macvtap_ifname), linkdev,
1258 1259 1260 1261
              (macvtap_macaddr
               ? virMacAddrFormat(macvtap_macaddr, macStr)
               : "(unspecified)"),
              vmuuid ? virUUIDFormat(vmuuid, uuidStr) : "(unspecified)");
1262 1263 1264 1265 1266 1267

    if (!virtPort || vmOp == VIR_NETDEV_VPORT_PROFILE_OP_NO_OP)
        return 0;

    switch (virtPort->virtPortType) {
    case VIR_NETDEV_VPORT_PROFILE_NONE:
A
Ansis Atteka 已提交
1268
    case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
1269 1270 1271 1272 1273
    case VIR_NETDEV_VPORT_PROFILE_LAST:
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBG:
        rc = virNetDevVPortProfileOp8021Qbg(macvtap_ifname, macvtap_macaddr,
1274
                                            vf, virtPort,
1275 1276
                                            (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_MIGRATE_IN_START)
                                            ? VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE
1277 1278
                                            : VIR_NETDEV_VPORT_PROFILE_LINK_OP_ASSOCIATE,
                                            setlink_only);
1279 1280 1281
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBH:
1282
        rc = virNetDevVPortProfileOp8021Qbh(linkdev, macvtap_macaddr, vf,
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
                                            virtPort, vmuuid,
                                            (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_MIGRATE_IN_START)
                                            ? VIR_NETDEV_VPORT_PROFILE_LINK_OP_PREASSOCIATE_RR
                                            : VIR_NETDEV_VPORT_PROFILE_LINK_OP_ASSOCIATE);
        if (vmOp != VIR_NETDEV_VPORT_PROFILE_OP_MIGRATE_IN_START && !rc) {
            /* XXX bogus error handling */
            ignore_value(virNetDevSetOnline(linkdev, true));
        }

        break;
    }

    return rc;
}


/**
 * virNetDevVPortProfileDisassociate:
 *
 * @macvtap_ifname: The name of the macvtap device
 * @macvtap_macaddr : The MAC address of the macvtap
 * @linkdev: The link device in case of macvtap
 * @virtPort: point to object holding port profile parameters
 *
 * Returns 0 in case of success, != 0 otherwise with error
 * having been reported.
 */
int
virNetDevVPortProfileDisassociate(const char *macvtap_ifname,
1312 1313
                                  const virNetDevVPortProfile *virtPort,
                                  const virMacAddr *macvtap_macaddr,
1314
                                  const char *linkdev,
1315
                                  int vf,
1316
                                  virNetDevVPortProfileOp vmOp)
1317 1318
{
    int rc = 0;
1319 1320 1321 1322
    char macStr[VIR_MAC_STRING_BUFLEN];

    VIR_DEBUG("profile:'%p' vmOp: %s device: %s@%s mac: %s",
              virtPort, virNetDevVPortProfileOpTypeToString(vmOp),
J
Ján Tomko 已提交
1323
              NULLSTR_EMPTY(macvtap_ifname), linkdev,
1324 1325 1326
              (macvtap_macaddr
               ? virMacAddrFormat(macvtap_macaddr, macStr)
               : "(unspecified)"));
1327 1328 1329 1330 1331 1332

    if (!virtPort)
       return 0;

    switch (virtPort->virtPortType) {
    case VIR_NETDEV_VPORT_PROFILE_NONE:
A
Ansis Atteka 已提交
1333
    case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
1334 1335 1336 1337
    case VIR_NETDEV_VPORT_PROFILE_LAST:
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBG:
1338
        rc = virNetDevVPortProfileOp8021Qbg(macvtap_ifname, macvtap_macaddr, vf,
1339
                                            virtPort,
1340
                                            VIR_NETDEV_VPORT_PROFILE_LINK_OP_DISASSOCIATE, false);
1341 1342 1343 1344 1345 1346
        break;

    case VIR_NETDEV_VPORT_PROFILE_8021QBH:
        /* avoid disassociating twice */
        if (vmOp == VIR_NETDEV_VPORT_PROFILE_OP_MIGRATE_IN_FINISH)
            break;
1347 1348
        if (vf < 0)
            ignore_value(virNetDevSetOnline(linkdev, false));
1349
        rc = virNetDevVPortProfileOp8021Qbh(linkdev, macvtap_macaddr, vf,
1350 1351 1352 1353 1354 1355 1356 1357 1358
                                            virtPort, NULL,
                                            VIR_NETDEV_VPORT_PROFILE_LINK_OP_DISASSOCIATE);
        break;
    }

    return rc;
}

#else /* ! WITH_VIRTUALPORT */
J
Ján Tomko 已提交
1359 1360 1361 1362 1363 1364 1365 1366
int virNetDevVPortProfileAssociate(const char *macvtap_ifname G_GNUC_UNUSED,
                                   const virNetDevVPortProfile *virtPort G_GNUC_UNUSED,
                                   const virMacAddr *macvtap_macaddr G_GNUC_UNUSED,
                                   const char *linkdev G_GNUC_UNUSED,
                                   int vf G_GNUC_UNUSED,
                                   const unsigned char *vmuuid G_GNUC_UNUSED,
                                   virNetDevVPortProfileOp vmOp G_GNUC_UNUSED,
                                   bool setlink_only G_GNUC_UNUSED)
1367 1368 1369 1370 1371 1372
{
    virReportSystemError(ENOSYS, "%s",
                         _("Virtual port profile association not supported on this platform"));
    return -1;
}

J
Ján Tomko 已提交
1373 1374 1375 1376 1377 1378
int virNetDevVPortProfileDisassociate(const char *macvtap_ifname G_GNUC_UNUSED,
                                      const virNetDevVPortProfile *virtPort G_GNUC_UNUSED,
                                      const virMacAddr *macvtap_macaddr G_GNUC_UNUSED,
                                      const char *linkdev G_GNUC_UNUSED,
                                      int vf G_GNUC_UNUSED,
                                      virNetDevVPortProfileOp vmOp G_GNUC_UNUSED)
1379 1380 1381 1382 1383 1384
{
    virReportSystemError(ENOSYS, "%s",
                         _("Virtual port profile association not supported on this platform"));
    return -1;
}
#endif /* ! WITH_VIRTUALPORT */