parallels_network.c 16.6 KB
Newer Older
D
Dmitry Guryanov 已提交
1
/*
2
 * parallels_network.c: core privconn functions for managing
D
Dmitry Guryanov 已提交
3 4
 * Parallels Cloud Server hosts
 *
N
Nehal J Wani 已提交
5
 * Copyright (C) 2013-2014 Red Hat, Inc.
D
Dmitry Guryanov 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 * Copyright (C) 2012 Parallels, 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
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include "datatypes.h"
27
#include "dirname.h"
28
#include "viralloc.h"
29
#include "virerror.h"
30
#include "virfile.h"
D
Dmitry Guryanov 已提交
31 32
#include "md5.h"
#include "parallels_utils.h"
33
#include "virstring.h"
D
Dmitry Guryanov 已提交
34 35

#define VIR_FROM_THIS VIR_FROM_PARALLELS
36
#define PARALLELS_ROUTED_NETWORK_UUID   "eb593dd1-6846-45b0-84a0-de0729286982"
D
Dmitry Guryanov 已提交
37 38 39 40 41

#define parallelsParseError()                                                  \
    virReportErrorHelper(VIR_FROM_TEST, VIR_ERR_OPERATION_FAILED, __FILE__,    \
                     __FUNCTION__, __LINE__, _("Can't parse prlctl output"))

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#define SYSFS_NET_DIR "/sys/class/net"

static int parallelsGetBridgedNetInfo(virNetworkDefPtr def, virJSONValuePtr jobj)
{
    const char *ifname;
    char *bridgeLink = NULL;
    char *bridgePath = NULL;
    char *bridgeAddressPath = NULL;
    char *bridgeAddress = NULL;
    int len = 0;
    int ret = -1;

    if (!(ifname = virJSONValueObjectGetString(jobj, "Bound To"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virAsprintf(&bridgeLink, "%s/%s/brport/bridge",
60
                    SYSFS_NET_DIR, ifname) < 0)
61 62 63 64 65 66 67
        goto cleanup;

    if (virFileResolveLink(bridgeLink, &bridgePath) < 0) {
        virReportSystemError(errno, _("cannot read link '%s'"), bridgeLink);
        goto cleanup;
    }

68
    if (VIR_STRDUP(def->bridge, last_component(bridgePath)) < 0)
69 70 71
        goto cleanup;

    if (virAsprintf(&bridgeAddressPath, "%s/%s/brport/bridge/address",
72
                    SYSFS_NET_DIR, ifname) < 0)
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        goto cleanup;

    if ((len = virFileReadAll(bridgeAddressPath, 18, &bridgeAddress)) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error reading file '%s'"), bridgeAddressPath);

        goto cleanup;
    }

    if (len < VIR_MAC_STRING_BUFLEN) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Error reading MAC from '%s'"), bridgeAddressPath);
    }

    bridgeAddress[VIR_MAC_STRING_BUFLEN - 1] = '\0';
    if (virMacAddrParse(bridgeAddress, &def->mac) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Can't parse MAC '%s'"), bridgeAddress);
        goto cleanup;
    }
    def->mac_specified = 1;

    ret = 0;

97
 cleanup:
98 99 100 101 102 103 104 105 106 107 108 109 110
    VIR_FREE(bridgeLink);
    VIR_FREE(bridgePath);
    VIR_FREE(bridgeAddress);
    VIR_FREE(bridgeAddressPath);
    return ret;
}

static int parallelsGetHostOnlyNetInfo(virNetworkDefPtr def, const char *name)
{
    const char *tmp;
    virJSONValuePtr jobj = NULL, jobj2;
    int ret = -1;

111
    if (VIR_EXPAND_N(def->ips, def->nips, 1) < 0)
112 113 114 115 116 117 118 119 120 121 122 123 124 125
        goto cleanup;

    jobj = parallelsParseOutput("prlsrvctl", "net", "info", "-j", name, NULL);

    if (!jobj) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(jobj2 = virJSONValueObjectGet(jobj, "Parallels adapter"))) {
        parallelsParseError();
        goto cleanup;
    }

126
    if (VIR_STRDUP(def->ips[0].family, "ipv4") < 0)
127
        goto cleanup;
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    if (!(tmp = virJSONValueObjectGetString(jobj2, "IP address"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virSocketAddrParseIPv4(&def->ips[0].address, tmp) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(tmp = virJSONValueObjectGetString(jobj2, "Subnet mask"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virSocketAddrParseIPv4(&def->ips[0].netmask, tmp) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(jobj2 = virJSONValueObjectGet(jobj, "DHCPv4 server"))) {
        parallelsParseError();
        goto cleanup;
    }

154
    if (VIR_EXPAND_N(def->ips[0].ranges, def->ips[0].nranges, 1) < 0)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        goto cleanup;

    if (!(tmp = virJSONValueObjectGetString(jobj2, "IP scope start address"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virSocketAddrParseIPv4(&def->ips[0].ranges[0].start, tmp) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    if (!(tmp = virJSONValueObjectGetString(jobj2, "IP scope end address"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (virSocketAddrParseIPv4(&def->ips[0].ranges[0].end, tmp) < 0) {
        parallelsParseError();
        goto cleanup;
    }

    ret = 0;
178
 cleanup:
179 180 181 182
    virJSONValueFree(jobj);
    return ret;
}

D
Dmitry Guryanov 已提交
183 184 185 186 187 188 189 190 191 192
static virNetworkObjPtr
parallelsLoadNetwork(parallelsConnPtr privconn, virJSONValuePtr jobj)
{
    virNetworkObjPtr net;
    virNetworkDefPtr def;
    const char *tmp;
    /* MD5_DIGEST_SIZE = VIR_UUID_BUFLEN = 16 */
    unsigned char md5[MD5_DIGEST_SIZE];

    if (VIR_ALLOC(def) < 0)
193
        goto cleanup;
D
Dmitry Guryanov 已提交
194 195 196 197 198 199

    if (!(tmp = virJSONValueObjectGetString(jobj, "Network ID"))) {
        parallelsParseError();
        goto cleanup;
    }

200 201
    if (VIR_STRDUP(def->name, tmp) < 0)
        goto cleanup;
D
Dmitry Guryanov 已提交
202 203

    /* Network names are unique in Parallels Cloud Server, so we can make
N
Nehal J Wani 已提交
204
     * a UUID from it */
D
Dmitry Guryanov 已提交
205 206 207 208
    md5_buffer(tmp, strlen(tmp), md5);
    memcpy(def->uuid, md5, VIR_UUID_BUFLEN);
    def->uuid_specified = 1;

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    if (!(tmp = virJSONValueObjectGetString(jobj, "Type"))) {
        parallelsParseError();
        goto cleanup;
    }

    if (STREQ(tmp, "bridged")) {
        def->forward.type = VIR_NETWORK_FORWARD_BRIDGE;

        if (parallelsGetBridgedNetInfo(def, jobj) < 0)
            goto cleanup;
    } else if (STREQ(tmp, "host-only")) {
        def->forward.type = VIR_NETWORK_FORWARD_NONE;

        if (parallelsGetHostOnlyNetInfo(def, def->name) < 0)
            goto cleanup;
    } else {
        parallelsParseError();
        goto cleanup;
    }

229
    if (!(net = virNetworkAssignDef(&privconn->networks, def, false)))
D
Dmitry Guryanov 已提交
230 231 232 233 234 235
        goto cleanup;
    net->active = 1;
    net->autostart = 1;
    virNetworkObjUnlock(net);
    return net;

236
 cleanup:
D
Dmitry Guryanov 已提交
237 238 239 240
    virNetworkDefFree(def);
    return NULL;
}

241 242 243 244 245 246 247
static virNetworkObjPtr
parallelsAddRoutedNetwork(parallelsConnPtr privconn)
{
    virNetworkObjPtr net;
    virNetworkDefPtr def;

    if (VIR_ALLOC(def) < 0)
248
        goto cleanup;
249 250 251

    def->forward.type = VIR_NETWORK_FORWARD_ROUTE;

252 253
    if (VIR_STRDUP(def->name, PARALLELS_ROUTED_NETWORK_NAME) < 0)
        goto cleanup;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    if (virUUIDParse(PARALLELS_ROUTED_NETWORK_UUID, def->uuid) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Can't parse UUID"));
        goto cleanup;
    }
    def->uuid_specified = 1;

    if (!(net = virNetworkAssignDef(&privconn->networks, def, false))) {
        virNetworkDefFree(def);
        goto cleanup;
    }
    net->active = 1;
    net->autostart = 1;
    virNetworkObjUnlock(net);

    return net;

272
 cleanup:
273 274 275 276
    virNetworkDefFree(def);
    return NULL;
}

D
Dmitry Guryanov 已提交
277 278 279 280 281
static int parallelsLoadNetworks(parallelsConnPtr privconn)
{
    virJSONValuePtr jobj, jobj2;
    virNetworkObjPtr net;
    int ret = -1;
282 283
    int count;
    size_t i;
D
Dmitry Guryanov 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

    jobj = parallelsParseOutput("prlsrvctl", "net", "list", "-j", NULL);

    if (!jobj) {
        parallelsParseError();
        goto cleanup;
    }

    count = virJSONValueArraySize(jobj);
    if (count < 0) {
        parallelsParseError();
        goto cleanup;
    }

    for (i = 0; i < count; i++) {
        jobj2 = virJSONValueArrayGet(jobj, i);
        if (!jobj2) {
            parallelsParseError();
            goto cleanup;
        }

        net = parallelsLoadNetwork(privconn, jobj2);
        if (!net)
            goto cleanup;

    }

311 312 313
    if (!parallelsAddRoutedNetwork(privconn))
        goto cleanup;

D
Dmitry Guryanov 已提交
314 315
    ret = 0;

316
 cleanup:
D
Dmitry Guryanov 已提交
317 318 319 320
    virJSONValueFree(jobj);
    return ret;
}

321
virDrvOpenStatus
322
parallelsNetworkOpen(virConnectPtr conn,
D
Dmitry Guryanov 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335
                     unsigned int flags)
{
    virCheckFlags(VIR_CONNECT_RO, VIR_DRV_OPEN_ERROR);

    if (STRNEQ(conn->driver->name, "Parallels"))
        return VIR_DRV_OPEN_DECLINED;

    if (parallelsLoadNetworks(conn->privateData) < 0)
        return VIR_DRV_OPEN_DECLINED;

    return VIR_DRV_OPEN_SUCCESS;
}

336
int parallelsNetworkClose(virConnectPtr conn)
D
Dmitry Guryanov 已提交
337 338 339 340 341 342 343 344
{
    parallelsConnPtr privconn = conn->privateData;
    parallelsDriverLock(privconn);
    virNetworkObjListFree(&privconn->networks);
    parallelsDriverUnlock(privconn);
    return 0;
}

345
static int parallelsConnectNumOfNetworks(virConnectPtr conn)
D
Dmitry Guryanov 已提交
346
{
347 348
    int nactive = 0;
    size_t i;
D
Dmitry Guryanov 已提交
349 350 351
    parallelsConnPtr privconn = conn->privateData;

    parallelsDriverLock(privconn);
352
    for (i = 0; i < privconn->networks.count; i++) {
D
Dmitry Guryanov 已提交
353 354 355 356 357 358 359 360 361 362
        virNetworkObjLock(privconn->networks.objs[i]);
        if (virNetworkObjIsActive(privconn->networks.objs[i]))
            nactive++;
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    parallelsDriverUnlock(privconn);

    return nactive;
}

363 364 365
static int parallelsConnectListNetworks(virConnectPtr conn,
                                        char **const names,
                                        int nnames)
D
Dmitry Guryanov 已提交
366 367
{
    parallelsConnPtr privconn = conn->privateData;
368 369
    int got = 0;
    size_t i;
D
Dmitry Guryanov 已提交
370 371

    parallelsDriverLock(privconn);
372
    for (i = 0; i < privconn->networks.count && got < nnames; i++) {
D
Dmitry Guryanov 已提交
373 374
        virNetworkObjLock(privconn->networks.objs[i]);
        if (virNetworkObjIsActive(privconn->networks.objs[i])) {
375
            if (VIR_STRDUP(names[got], privconn->networks.objs[i]->def->name) < 0) {
D
Dmitry Guryanov 已提交
376 377 378 379 380 381 382 383 384 385 386 387 388
                virNetworkObjUnlock(privconn->networks.objs[i]);
                goto cleanup;
            }
            got++;
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    parallelsDriverUnlock(privconn);

    return got;

 cleanup:
    parallelsDriverUnlock(privconn);
389
    for (i = 0; i < got; i++)
D
Dmitry Guryanov 已提交
390 391 392 393
        VIR_FREE(names[i]);
    return -1;
}

394
static int parallelsConnectNumOfDefinedNetworks(virConnectPtr conn)
D
Dmitry Guryanov 已提交
395
{
396 397
    int ninactive = 0;
    size_t i;
D
Dmitry Guryanov 已提交
398 399 400
    parallelsConnPtr privconn = conn->privateData;

    parallelsDriverLock(privconn);
401
    for (i = 0; i < privconn->networks.count; i++) {
D
Dmitry Guryanov 已提交
402 403 404 405 406 407 408 409 410 411
        virNetworkObjLock(privconn->networks.objs[i]);
        if (!virNetworkObjIsActive(privconn->networks.objs[i]))
            ninactive++;
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    parallelsDriverUnlock(privconn);

    return ninactive;
}

412 413 414
static int parallelsConnectListDefinedNetworks(virConnectPtr conn,
                                               char **const names,
                                               int nnames)
D
Dmitry Guryanov 已提交
415 416
{
    parallelsConnPtr privconn = conn->privateData;
417 418
    int got = 0;
    size_t i;
D
Dmitry Guryanov 已提交
419 420

    parallelsDriverLock(privconn);
421
    for (i = 0; i < privconn->networks.count && got < nnames; i++) {
D
Dmitry Guryanov 已提交
422 423
        virNetworkObjLock(privconn->networks.objs[i]);
        if (!virNetworkObjIsActive(privconn->networks.objs[i])) {
424
            if (VIR_STRDUP(names[got], privconn->networks.objs[i]->def->name) < 0) {
D
Dmitry Guryanov 已提交
425 426 427 428 429 430 431 432 433 434 435 436
                virNetworkObjUnlock(privconn->networks.objs[i]);
                goto cleanup;
            }
            got++;
        }
        virNetworkObjUnlock(privconn->networks.objs[i]);
    }
    parallelsDriverUnlock(privconn);
    return got;

 cleanup:
    parallelsDriverUnlock(privconn);
437
    for (i = 0; i < got; i++)
D
Dmitry Guryanov 已提交
438 439 440 441
        VIR_FREE(names[i]);
    return -1;
}

442 443 444
static int parallelsConnectListAllNetworks(virConnectPtr conn,
                                           virNetworkPtr **nets,
                                           unsigned int flags)
D
Dmitry Guryanov 已提交
445 446 447 448 449 450 451
{
    parallelsConnPtr privconn = conn->privateData;
    int ret = -1;

    virCheckFlags(VIR_CONNECT_LIST_NETWORKS_FILTERS_ALL, -1);

    parallelsDriverLock(privconn);
452
    ret = virNetworkObjListExport(conn, &privconn->networks, nets, NULL, flags);
D
Dmitry Guryanov 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
    parallelsDriverUnlock(privconn);

    return ret;
}

static virNetworkPtr parallelsNetworkLookupByUUID(virConnectPtr conn,
                                                  const unsigned char *uuid)
{
    parallelsConnPtr privconn = conn->privateData;
    virNetworkObjPtr network;
    virNetworkPtr ret = NULL;

    parallelsDriverLock(privconn);
    network = virNetworkFindByUUID(&privconn->networks, uuid);
    parallelsDriverUnlock(privconn);
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        goto cleanup;
    }

    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

476
 cleanup:
D
Dmitry Guryanov 已提交
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
    if (network)
        virNetworkObjUnlock(network);
    return ret;
}

static virNetworkPtr parallelsNetworkLookupByName(virConnectPtr conn,
                                                  const char *name)
{
    parallelsConnPtr privconn = conn->privateData;
    virNetworkObjPtr network;
    virNetworkPtr ret = NULL;

    parallelsDriverLock(privconn);
    network = virNetworkFindByName(&privconn->networks, name);
    parallelsDriverUnlock(privconn);
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       _("no network with matching name '%s'"), name);
        goto cleanup;
    }

    ret = virGetNetwork(conn, network->def->name, network->def->uuid);

500
 cleanup:
D
Dmitry Guryanov 已提交
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
    if (network)
        virNetworkObjUnlock(network);
    return ret;
}

static char *parallelsNetworkGetXMLDesc(virNetworkPtr net,
                                        unsigned int flags)
{
    parallelsConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr network;
    char *ret = NULL;

    virCheckFlags(VIR_NETWORK_XML_INACTIVE, NULL);

    parallelsDriverLock(privconn);
    network = virNetworkFindByUUID(&privconn->networks, net->uuid);
    parallelsDriverUnlock(privconn);

    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        goto cleanup;
    }

    ret = virNetworkDefFormat(network->def, flags);

527
 cleanup:
D
Dmitry Guryanov 已提交
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
    if (network)
        virNetworkObjUnlock(network);
    return ret;
}

static int parallelsNetworkIsActive(virNetworkPtr net)
{
    parallelsConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    parallelsDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    parallelsDriverUnlock(privconn);
    if (!obj) {
        virReportError(VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = virNetworkObjIsActive(obj);

548
 cleanup:
D
Dmitry Guryanov 已提交
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}

static int parallelsNetworkIsPersistent(virNetworkPtr net)
{
    parallelsConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr obj;
    int ret = -1;

    parallelsDriverLock(privconn);
    obj = virNetworkFindByUUID(&privconn->networks, net->uuid);
    parallelsDriverUnlock(privconn);
    if (!obj) {
        virReportError(VIR_ERR_NO_NETWORK, NULL);
        goto cleanup;
    }
    ret = obj->persistent;

569
 cleanup:
D
Dmitry Guryanov 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    if (obj)
        virNetworkObjUnlock(obj);
    return ret;
}

static int parallelsNetworkGetAutostart(virNetworkPtr net,
                                 int *autostart)
{
    parallelsConnPtr privconn = net->conn->privateData;
    virNetworkObjPtr network;
    int ret = -1;

    parallelsDriverLock(privconn);
    network = virNetworkFindByUUID(&privconn->networks, net->uuid);
    parallelsDriverUnlock(privconn);
    if (!network) {
        virReportError(VIR_ERR_NO_NETWORK,
                       "%s", _("no network with matching uuid"));
        goto cleanup;
    }

    *autostart = network->autostart;
    ret = 0;

594
 cleanup:
D
Dmitry Guryanov 已提交
595 596 597 598
    if (network)
        virNetworkObjUnlock(network);
    return ret;
}
599 600 601

virNetworkDriver parallelsNetworkDriver = {
    .name = "Parallels",
602 603 604 605 606
    .connectNumOfNetworks = parallelsConnectNumOfNetworks, /* 1.0.1 */
    .connectListNetworks = parallelsConnectListNetworks, /* 1.0.1 */
    .connectNumOfDefinedNetworks = parallelsConnectNumOfDefinedNetworks, /* 1.0.1 */
    .connectListDefinedNetworks = parallelsConnectListDefinedNetworks, /* 1.0.1 */
    .connectListAllNetworks = parallelsConnectListAllNetworks, /* 1.0.1 */
D
Dmitry Guryanov 已提交
607 608 609 610 611 612 613
    .networkLookupByUUID = parallelsNetworkLookupByUUID, /* 1.0.1 */
    .networkLookupByName = parallelsNetworkLookupByName, /* 1.0.1 */
    .networkGetXMLDesc = parallelsNetworkGetXMLDesc, /* 1.0.1 */
    .networkGetAutostart = parallelsNetworkGetAutostart, /* 1.0.1 */
    .networkIsActive = parallelsNetworkIsActive, /* 1.0.1 */
    .networkIsPersistent = parallelsNetworkIsPersistent, /* 1.0.1 */
};