virnetlink.c 21.9 KB
Newer Older
1
/*
S
Serge Hallyn 已提交
2
 * Copyright (C) 2010-2012 Red Hat, Inc.
3
 * Copyright (C) 2010-2012 IBM Corporation
4 5 6 7 8 9 10 11 12 13 14 15
 *
 * 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
O
Osier Yang 已提交
16 17
 * License along with this library;  If not, see
 * <http://www.gnu.org/licenses/>.
18 19 20
 *
 * Authors:
 *     Stefan Berger <stefanb@us.ibm.com>
21
 *     Dirk Herrendoerfer <herrend[at]de[dot]ibm[dot]com>
22 23 24 25 26
 *
 * Notes:
 * netlink: http://lovezutto.googlepages.com/netlink.pdf
 *          iproute2 package
 *
27 28
 * 2012/02: Renamed from netlink.[ch] to virnetlink.[ch]
 *
29 30 31 32 33 34 35
 */

#include <config.h>

#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
36
#include <sys/socket.h>
37

38
#include "virnetlink.h"
39
#include "logging.h"
40
#include "memory.h"
41 42
#include "threads.h"
#include "virmacaddr.h"
43 44
#include "virterror_internal.h"

45 46 47 48
#ifndef SOL_NETLINK
# define SOL_NETLINK 270
#endif

49 50 51 52
#define VIR_FROM_THIS VIR_FROM_NET

#define NETLINK_ACK_TIMEOUT_S  2

53 54 55 56 57 58 59
#if defined(__linux__) && defined(HAVE_LIBNL)
/* State for a single netlink event handle */
struct virNetlinkEventHandle {
    int watch;
    virNetlinkEventHandleCallback handleCB;
    virNetlinkEventRemoveCallback removeCB;
    void *opaque;
60
    virMacAddr macaddr;
61 62 63
    int deleted;
};

S
Serge Hallyn 已提交
64 65 66 67 68 69 70 71 72 73
# ifdef HAVE_LIBNL1
#  define virNetlinkAlloc nl_handle_alloc
#  define virNetlinkFree nl_handle_destroy
typedef struct nl_handle virNetlinkHandle;
# else
#  define virNetlinkAlloc nl_socket_alloc
#  define virNetlinkFree nl_socket_free
typedef struct nl_sock virNetlinkHandle;
# endif

74 75 76 77 78 79 80
typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate;
typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr;
struct _virNetlinkEventSrvPrivate {
    /*Server*/
    virMutex lock;
    int eventwatch;
    int netlinkfd;
S
Serge Hallyn 已提交
81
    virNetlinkHandle *netlinknh;
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    /*Events*/
    int handled;
    size_t handlesCount;
    size_t handlesAlloc;
    struct virNetlinkEventHandle *handles;
};

enum virNetlinkDeleteMode {
    VIR_NETLINK_HANDLE_VALID,
    VIR_NETLINK_HANDLE_DELETED,
};

/* Unique ID for the next netlink watch to be registered */
static int nextWatch = 1;

/* Allocate extra slots for virEventPollHandle/virEventPollTimeout
 records in this multiple */
# define NETLINK_EVENT_ALLOC_EXTENT 10

101 102 103
/* Linux kernel supports up to MAX_LINKS (32 at the time) individual
 * netlink protocols. */
static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
J
Jiri Denemark 已提交
104
static virNetlinkHandle *placeholder_nlhandle = NULL;
105 106 107

/* Function definitions */

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/**
 * virNetlinkStartup:
 *
 * Perform any initialization that needs to take place before the
 * program starts up worker threads. This is currently used to assure
 * that an nl_handle is allocated prior to any attempts to bind a
 * netlink socket. For a discussion of why this is necessary, please
 * see the following email message:
 *
 *   https://www.redhat.com/archives/libvir-list/2012-May/msg00202.html
 *
 * The short version is that, without this placeholder allocation of
 * an nl_handle that is never used, it is possible for nl_connect() in
 * one thread to collide with a direct bind() of a netlink socket in
 * another thread, leading to failure of the operation (which could
 * lead to failure of libvirtd to start). Since getaddrinfo() (used by
 * libvirtd in virSocketAddrParse, which is called quite frequently
 * during startup) directly calls bind() on a netlink socket, this is
 * actually a very common occurrence (15-20% failure rate on some
 * hardware).
 *
 * Returns 0 on success, -1 on failure.
 */
int
virNetlinkStartup(void)
{
    if (placeholder_nlhandle)
        return 0;
136
    VIR_DEBUG("Running global netlink initialization");
J
Jiri Denemark 已提交
137
    placeholder_nlhandle = virNetlinkAlloc();
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
    if (!placeholder_nlhandle) {
        virReportSystemError(errno, "%s",
                             _("cannot allocate placeholder nlhandle for netlink"));
        return -1;
    }
    return 0;
}

/**
 * virNetlinkShutdown:
 *
 * Undo any initialization done by virNetlinkStartup. This currently
 * destroys the placeholder nl_handle.
 */
void
virNetlinkShutdown(void)
{
    if (placeholder_nlhandle) {
J
Jiri Denemark 已提交
156
        virNetlinkFree(placeholder_nlhandle);
157 158 159 160
        placeholder_nlhandle = NULL;
    }
}

161
/**
162
 * virNetlinkCommand:
163 164 165 166
 * @nlmsg: pointer to netlink message
 * @respbuf: pointer to pointer where response buffer will be allocated
 * @respbuflen: pointer to integer holding the size of the response buffer
 *      on return of the function.
167 168 169 170
 * @src_pid: the pid of the process to send a message
 * @dst_pid: the pid of the process to talk to, i.e., pid = 0 for kernel
 * @protocol: netlink protocol
 * @groups: the group identifier
171 172 173 174 175
 *
 * Send the given message to the netlink layer and receive response.
 * Returns 0 on success, -1 on error. In case of error, no response
 * buffer will be returned.
 */
176 177
int virNetlinkCommand(struct nl_msg *nl_msg,
                      unsigned char **respbuf, unsigned int *respbuflen,
178 179
                      uint32_t src_pid, uint32_t dst_pid,
                      unsigned int protocol, unsigned int groups)
180 181 182 183
{
    int rc = 0;
    struct sockaddr_nl nladdr = {
            .nl_family = AF_NETLINK,
184
            .nl_pid    = dst_pid,
185 186 187 188 189 190 191 192 193 194
            .nl_groups = 0,
    };
    ssize_t nbytes;
    struct timeval tv = {
        .tv_sec = NETLINK_ACK_TIMEOUT_S,
    };
    fd_set readfds;
    int fd;
    int n;
    struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
195 196 197 198 199 200 201
    virNetlinkHandle *nlhandle = NULL;

    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
        return -EINVAL;
    }
202

203
    nlhandle = virNetlinkAlloc();
204 205 206 207 208 209
    if (!nlhandle) {
        virReportSystemError(errno,
                             "%s", _("cannot allocate nlhandle for netlink"));
        return -1;
    }

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    if (nl_connect(nlhandle, protocol) < 0) {
        virReportSystemError(errno,
                        _("cannot connect to netlink socket with protocol %d"),
                             protocol);
        rc = -1;
        goto error;
    }

    fd = nl_socket_get_fd(nlhandle);
    if (fd < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot get netlink socket fd"));
        rc = -1;
        goto error;
    }

    if (groups && nl_socket_add_membership(nlhandle, groups) < 0) {
227
        virReportSystemError(errno,
228
                             "%s", _("cannot add netlink membership"));
229
        rc = -1;
230
        goto error;
231 232 233 234
    }

    nlmsg_set_dst(nl_msg, &nladdr);

235
    nlmsg->nlmsg_pid = src_pid ? src_pid : getpid();
236 237 238 239 240 241

    nbytes = nl_send_auto_complete(nlhandle, nl_msg);
    if (nbytes < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot send to netlink socket"));
        rc = -1;
242
        goto error;
243 244 245 246 247 248 249 250 251 252 253 254 255 256
    }

    FD_ZERO(&readfds);
    FD_SET(fd, &readfds);

    n = select(fd + 1, &readfds, NULL, NULL, &tv);
    if (n <= 0) {
        if (n < 0)
            virReportSystemError(errno, "%s",
                                 _("error in select call"));
        if (n == 0)
            virReportSystemError(ETIMEDOUT, "%s",
                                 _("no valid netlink response was received"));
        rc = -1;
257
        goto error;
258 259 260 261 262 263 264 265
    }

    *respbuflen = nl_recv(nlhandle, &nladdr, respbuf, NULL);
    if (*respbuflen <= 0) {
        virReportSystemError(errno,
                             "%s", _("nl_recv failed"));
        rc = -1;
    }
266
error:
267 268 269 270 271 272
    if (rc == -1) {
        VIR_FREE(*respbuf);
        *respbuf = NULL;
        *respbuflen = 0;
    }

S
Serge Hallyn 已提交
273
    virNetlinkFree(nlhandle);
274 275 276
    return rc;
}

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static void
virNetlinkEventServerLock(virNetlinkEventSrvPrivatePtr driver)
{
    virMutexLock(&driver->lock);
}

static void
virNetlinkEventServerUnlock(virNetlinkEventSrvPrivatePtr driver)
{
    virMutexUnlock(&driver->lock);
}

/**
 * virNetlinkEventRemoveClientPrimitive:
 *
 * @i: index of the client to remove from the table
293
 * @protocol: netlink protocol
294 295 296 297 298 299 300 301 302
 *
 * This static function does the low level removal of a client from
 * the table once its index is known, including calling the remove
 * callback (which usually will free resources required by the
 * handler). The event server lock *must* be locked before calling
 * this function.
 *
 * assumes success, returns nothing.
 */
303 304
static int
virNetlinkEventRemoveClientPrimitive(size_t i, unsigned int protocol)
305
{
306 307 308 309
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    virNetlinkEventRemoveCallback removeCB = server[protocol]->handles[i].removeCB;
310 311

    if (removeCB) {
312 313 314
        (removeCB)(server[protocol]->handles[i].watch,
                   &server[protocol]->handles[i].macaddr,
                   server[protocol]->handles[i].opaque);
315
    }
316 317
    server[protocol]->handles[i].deleted = VIR_NETLINK_HANDLE_DELETED;
    return 0;
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
}

static void
virNetlinkEventCallback(int watch,
                        int fd ATTRIBUTE_UNUSED,
                        int events ATTRIBUTE_UNUSED,
                        void *opaque)
{
    virNetlinkEventSrvPrivatePtr srv = opaque;
    unsigned char *msg;
    struct sockaddr_nl peer;
    struct ucred *creds = NULL;
    int i, length;
    bool handled = false;

    length = nl_recv(srv->netlinknh, &peer, &msg, &creds);

    if (length == 0)
        return;
    if (length < 0) {
338 339
        virReportSystemError(errno,
                             "%s", _("nl_recv returned with error"));
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
        return;
    }

    virNetlinkEventServerLock(srv);

    VIR_DEBUG("dispatching to max %d clients, called from event watch %d",
            (int)srv->handlesCount, watch);

    for (i = 0; i < srv->handlesCount; i++) {
        if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
            continue;

        VIR_DEBUG("dispatching client %d.", i);

        (srv->handles[i].handleCB)(msg, length, &peer, &handled,
                                   srv->handles[i].opaque);
    }

    if (!handled)
        VIR_DEBUG("event not handled.");
    VIR_FREE(msg);
    virNetlinkEventServerUnlock(srv);
}

/**
 * virNetlinkEventServiceStop:
 *
 * stop the monitor to receive netlink messages for libvirtd.
 * This removes the netlink socket fd from the event handler.
 *
370 371
 * @protocol: netlink protocol
 *
372
 * Returns -1 if the monitor cannot be unregistered, 0 upon success
373 374
 */
int
375
virNetlinkEventServiceStop(unsigned int protocol)
376
{
377 378 379 380
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    virNetlinkEventSrvPrivatePtr srv = server[protocol];
381 382 383 384
    int i;

    VIR_INFO("stopping netlink event service");

385
    if (!server[protocol])
386 387 388 389
        return 0;

    virNetlinkEventServerLock(srv);
    nl_close(srv->netlinknh);
S
Serge Hallyn 已提交
390
    virNetlinkFree(srv->netlinknh);
391 392 393 394 395
    virEventRemoveHandle(srv->eventwatch);

    /* free any remaining clients on the list */
    for (i = 0; i < srv->handlesCount; i++) {
        if (srv->handles[i].deleted == VIR_NETLINK_HANDLE_VALID)
396
            virNetlinkEventRemoveClientPrimitive(i, protocol);
397 398
    }

399
    server[protocol] = NULL;
400 401 402 403 404 405 406 407 408 409
    virNetlinkEventServerUnlock(srv);

    virMutexDestroy(&srv->lock);
    VIR_FREE(srv);
    return 0;
}

/**
 * virNetlinkEventServiceIsRunning:
 *
410
 * Returns if the netlink event service is running.
411
 *
412 413
 * @protocol: netlink protocol
 *
414
 * Returns 'true' if the service is running, 'false' if stopped.
415 416
 */
bool
417
virNetlinkEventServiceIsRunning(unsigned int protocol)
418
{
419 420 421 422 423 424 425
    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
        return false;
    }

    return server[protocol] != NULL;
426 427
}

428 429 430
/**
 * virNetlinkEventServiceLocalPid:
 *
431 432
 * @protocol: netlink protocol
 *
433 434 435 436
 * Returns the nl_pid value that was used to bind() the netlink socket
 * used by the netlink event service, or -1 on error (netlink
 * guarantees that this value will always be > 0).
 */
437
int virNetlinkEventServiceLocalPid(unsigned int protocol)
438
{
439 440 441 442
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    if (!(server[protocol] && server[protocol]->netlinknh)) {
443 444
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("netlink event service not running"));
445 446
        return -1;
    }
447
    return (int)nl_socket_get_local_port(server[protocol]->netlinknh);
448 449 450
}


451 452 453 454 455 456
/**
 * virNetlinkEventServiceStart:
 *
 * start a monitor to receive netlink messages for libvirtd.
 * This registers a netlink socket with the event interface.
 *
457 458
 * @protocol: netlink protocol
 * @groups: broadcast groups to join in
459
 * Returns -1 if the monitor cannot be registered, 0 upon success
460 461
 */
int
462
virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
463 464 465 466 467
{
    virNetlinkEventSrvPrivatePtr srv;
    int fd;
    int ret = -1;

468 469 470 471 472 473 474
    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
        return -EINVAL;
    }

    if (server[protocol])
475 476
        return 0;

477
    VIR_INFO("starting netlink event service with protocol %d", protocol);
478 479 480

    if (VIR_ALLOC(srv) < 0) {
        virReportOOMError();
481
        return -1;
482 483
    }

484 485 486 487
    if (virMutexInit(&srv->lock) < 0) {
        VIR_FREE(srv);
        return -1;
    }
488 489 490 491

    virNetlinkEventServerLock(srv);

    /* Allocate a new socket and get fd */
S
Serge Hallyn 已提交
492
    srv->netlinknh = virNetlinkAlloc();
493 494

    if (!srv->netlinknh) {
495 496
        virReportSystemError(errno,
                             "%s", _("cannot allocate nlhandle for virNetlinkEvent server"));
497 498 499
        goto error_locked;
    }

500
    if (nl_connect(srv->netlinknh, protocol) < 0) {
501
        virReportSystemError(errno,
502
                             _("cannot connect to netlink socket with protocol %d"), protocol);
503 504 505 506 507
        goto error_server;
    }

    fd = nl_socket_get_fd(srv->netlinknh);
    if (fd < 0) {
508 509
        virReportSystemError(errno,
                             "%s", _("cannot get netlink socket fd"));
510 511 512
        goto error_server;
    }

513 514 515 516 517 518
    if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot add netlink membership"));
        goto error_server;
    }

519
    if (nl_socket_set_nonblocking(srv->netlinknh)) {
520 521
        virReportSystemError(errno, "%s",
                             _("cannot set netlink socket nonblocking"));
522 523 524 525 526 527 528
        goto error_server;
    }

    if ((srv->eventwatch = virEventAddHandle(fd,
                                             VIR_EVENT_HANDLE_READABLE,
                                             virNetlinkEventCallback,
                                             srv, NULL)) < 0) {
529 530
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to add netlink event handle watch"));
531 532 533 534 535 536 537
        goto error_server;
    }

    srv->netlinkfd = fd;
    VIR_DEBUG("netlink event listener on fd: %i running", fd);

    ret = 0;
538
    server[protocol] = srv;
539 540 541 542

error_server:
    if (ret < 0) {
        nl_close(srv->netlinknh);
S
Serge Hallyn 已提交
543
        virNetlinkFree(srv->netlinknh);
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
    }
error_locked:
    virNetlinkEventServerUnlock(srv);
    if (ret < 0) {
        virMutexDestroy(&srv->lock);
        VIR_FREE(srv);
    }
    return ret;
}

/**
 * virNetlinkEventAddClient:
 *
 * @handleCB: callback to invoke when an event occurs
 * @removeCB: callback to invoke when removing a client
 * @opaque: user data to pass to callback
560 561
 * @macaddr: macaddr to store with the data. Used to identify callers.
 *           May be null.
562
 * @protocol: netlink protocol
563 564 565 566 567
 *
 * register a callback for handling of netlink messages. The
 * registered function receives the entire netlink message and
 * may choose to act upon it.
 *
568 569
 * Returns -1 if the file handle cannot be registered, number of
 * monitor upon success.
570 571 572 573
 */
int
virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
                         virNetlinkEventRemoveCallback removeCB,
574 575
                         void *opaque, const virMacAddrPtr macaddr,
                         unsigned int protocol)
576 577
{
    int i, r, ret = -1;
578 579 580 581 582 583
    virNetlinkEventSrvPrivatePtr srv = NULL;

    if (protocol >= MAX_LINKS)
        return -EINVAL;

    srv = server[protocol];
584

585
    if (handleCB == NULL) {
586 587
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Invalid NULL callback provided"));
588
        return -1;
589
    }
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

    virNetlinkEventServerLock(srv);

    VIR_DEBUG("adding client: %d.", nextWatch);

    r = 0;
    /* first try to re-use deleted free slots */
    for (i = 0; i < srv->handlesCount; i++) {
        if (srv->handles[i].deleted == VIR_NETLINK_HANDLE_DELETED) {
            r = i;
            goto addentry;
        }
    }
    /* Resize the eventLoop array if needed */
    if (srv->handlesCount == srv->handlesAlloc) {
        VIR_DEBUG("Used %zu handle slots, adding at least %d more",
                  srv->handlesAlloc, NETLINK_EVENT_ALLOC_EXTENT);
        if (VIR_RESIZE_N(srv->handles, srv->handlesAlloc,
                        srv->handlesCount, NETLINK_EVENT_ALLOC_EXTENT) < 0) {
609
            virReportOOMError();
610 611 612 613 614 615 616 617 618 619 620 621
            goto error;
        }
    }
    r = srv->handlesCount++;

addentry:
    srv->handles[r].watch    = nextWatch;
    srv->handles[r].handleCB = handleCB;
    srv->handles[r].removeCB = removeCB;
    srv->handles[r].opaque   = opaque;
    srv->handles[r].deleted  = VIR_NETLINK_HANDLE_VALID;
    if (macaddr)
622
        virMacAddrSet(&srv->handles[r].macaddr, macaddr);
623
    else
624 625
        virMacAddrSetRaw(&srv->handles[r].macaddr,
                         (unsigned char[VIR_MAC_BUFLEN]){0,0,0,0,0,0});
626 627 628 629 630 631 632 633 634 635 636 637 638 639

    VIR_DEBUG("added client to loop slot: %d. with macaddr ptr=%p", r, macaddr);

    ret = nextWatch++;
error:
    virNetlinkEventServerUnlock(srv);
    return ret;
}

/**
 * virNetlinkEventRemoveClient:
 *
 * @watch: watch whose handle to remove
 * @macaddr: macaddr whose handle to remove
640
 * @protocol: netlink protocol
641 642 643 644 645
 *
 * Unregister a callback from a netlink monitor.
 * The handler function referenced will no longer receive netlink messages.
 * Either watch or macaddr may be used, the other should be null.
 *
646
 * Returns -1 if the file handle was not registered, 0 upon success
647 648
 */
int
649 650
virNetlinkEventRemoveClient(int watch, const virMacAddrPtr macaddr,
                            unsigned int protocol)
651 652 653
{
    int i;
    int ret = -1;
654 655 656 657 658 659
    virNetlinkEventSrvPrivatePtr srv = NULL;

    if (protocol >= MAX_LINKS)
        return -EINVAL;

    srv = server[protocol];
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675

    VIR_DEBUG("removing client watch=%d, mac=%p.", watch, macaddr);

    if (watch <= 0 && !macaddr) {
        VIR_WARN("Ignoring invalid netlink client id: %d", watch);
        return -1;
    }

    virNetlinkEventServerLock(srv);

    for (i = 0; i < srv->handlesCount; i++) {
        if (srv->handles[i].deleted != VIR_NETLINK_HANDLE_VALID)
            continue;

        if ((watch && srv->handles[i].watch == watch) ||
            (!watch &&
676
             virMacAddrCmp(macaddr, &srv->handles[i].macaddr) == 0)) {
677 678 679

            VIR_DEBUG("removed client: %d by %s.",
                      srv->handles[i].watch, watch ? "index" : "mac");
680
            virNetlinkEventRemoveClientPrimitive(i, protocol);
681 682 683 684 685 686 687 688 689 690 691
            ret = 0;
            goto cleanup;
        }
    }
    VIR_DEBUG("no client found to remove.");

cleanup:
    virNetlinkEventServerUnlock(srv);
    return ret;
}

692 693
#else

694 695 696 697 698 699
# if defined(__linux)
static const char *unsupported = N_("libnl was not available at build time");
# else
static const char *unsupported = N_("not supported on non-linux platforms");
# endif

700 701 702 703 704 705 706 707 708 709 710 711
int
virNetlinkStartup(void)
{
    return 0;
}

void
virNetlinkShutdown(void)
{
    return;
}

712
int virNetlinkCommand(struct nl_msg *nl_msg ATTRIBUTE_UNUSED,
713 714 715
                      unsigned char **respbuf ATTRIBUTE_UNUSED,
                      unsigned int *respbuflen ATTRIBUTE_UNUSED,
                      uint32_t src_pid ATTRIBUTE_UNUSED,
716 717 718
                      uint32_t dst_pid ATTRIBUTE_UNUSED,
                      unsigned int protocol ATTRIBUTE_UNUSED,
                      unsigned int groups ATTRIBUTE_UNUSED)
719
{
720
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
721 722 723
    return -1;
}

724
/**
725 726
 * stopNetlinkEventServer: stop the monitor to receive netlink
 * messages for libvirtd
727
 */
728
int virNetlinkEventServiceStop(unsigned int protocol ATTRIBUTE_UNUSED)
729
{
730
    VIR_DEBUG("%s", _(unsupported));
731 732 733 734
    return 0;
}

/**
735 736
 * startNetlinkEventServer: start a monitor to receive netlink
 * messages for libvirtd
737
 */
738 739
int virNetlinkEventServiceStart(unsigned int protocol ATTRIBUTE_UNUSED,
                                unsigned int groups ATTRIBUTE_UNUSED)
740
{
741
    VIR_DEBUG("%s", _(unsupported));
742 743 744 745
    return 0;
}

/**
746 747
 * virNetlinkEventServiceIsRunning: returns if the netlink event
 * service is running.
748
 */
749
bool virNetlinkEventServiceIsRunning(unsigned int protocol ATTRIBUTE_UNUSED)
750
{
751
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
752 753 754
    return 0;
}

755
int virNetlinkEventServiceLocalPid(unsigned int protocol ATTRIBUTE_UNUSED)
756
{
757
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
758 759 760
    return -1;
}

761
/**
762 763
 * virNetlinkEventAddClient: register a callback for handling of
 * netlink messages
764
 */
J
Jim Fehlig 已提交
765 766 767
int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB ATTRIBUTE_UNUSED,
                             virNetlinkEventRemoveCallback removeCB ATTRIBUTE_UNUSED,
                             void *opaque ATTRIBUTE_UNUSED,
768
                             const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
769
                             unsigned int protocol ATTRIBUTE_UNUSED)
770
{
771
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
772 773 774 775 776 777
    return -1;
}

/**
 * virNetlinkEventRemoveClient: unregister a callback from a netlink monitor
 */
J
Jim Fehlig 已提交
778
int virNetlinkEventRemoveClient(int watch ATTRIBUTE_UNUSED,
779
                                const virMacAddrPtr macaddr ATTRIBUTE_UNUSED)
780
                                unsigned int protocol ATTRIBUTE_UNUSED)
781
{
782
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
783 784 785
    return -1;
}

786
#endif /* __linux__ */