virnetlink.c 38.3 KB
Newer Older
1
/*
L
Leno Hou 已提交
2 3
 * Copyright (C) 2010-2016 Red Hat, Inc.
 * Copyright (C) 2010-2012, 2016 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
16
 * License along with this library.  If not, see
O
Osier Yang 已提交
17
 * <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25 26 27
 *
 * Notes:
 * netlink: http://lovezutto.googlepages.com/netlink.pdf
 *          iproute2 package
 */

#include <config.h>

#include <unistd.h>
#include <sys/types.h>
28
#include <sys/socket.h>
29

30
#include "virnetlink.h"
31
#include "virnetdev.h"
32
#include "virlog.h"
33
#include "virthread.h"
34
#include "virmacaddr.h"
35
#include "virerror.h"
36
#include "viralloc.h"
37 38 39

#define VIR_FROM_THIS VIR_FROM_NET

40 41
VIR_LOG_INIT("util.netlink");

42
#define NETLINK_ACK_TIMEOUT_S  (2*1000)
43

44 45 46 47 48 49 50
#if defined(__linux__) && defined(HAVE_LIBNL)
/* State for a single netlink event handle */
struct virNetlinkEventHandle {
    int watch;
    virNetlinkEventHandleCallback handleCB;
    virNetlinkEventRemoveCallback removeCB;
    void *opaque;
51
    virMacAddr macaddr;
52 53 54
    int deleted;
};

55 56 57
# define virNetlinkAlloc nl_socket_alloc
# define virNetlinkSetBufferSize nl_socket_set_buffer_size
# define virNetlinkFree nl_socket_free
S
Serge Hallyn 已提交
58 59
typedef struct nl_sock virNetlinkHandle;

60
G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetlinkHandle, virNetlinkFree);
61

62 63 64 65 66 67 68
typedef struct _virNetlinkEventSrvPrivate virNetlinkEventSrvPrivate;
typedef virNetlinkEventSrvPrivate *virNetlinkEventSrvPrivatePtr;
struct _virNetlinkEventSrvPrivate {
    /*Server*/
    virMutex lock;
    int eventwatch;
    int netlinkfd;
S
Serge Hallyn 已提交
69
    virNetlinkHandle *netlinknh;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
    /*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

89 90 91
/* Linux kernel supports up to MAX_LINKS (32 at the time) individual
 * netlink protocols. */
static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL};
92
static virNetlinkHandle *placeholder_nlhandle;
93 94 95

/* Function definitions */

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/**
 * 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;
124
    VIR_DEBUG("Running global netlink initialization");
J
Jiri Denemark 已提交
125
    placeholder_nlhandle = virNetlinkAlloc();
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    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 已提交
144
        virNetlinkFree(placeholder_nlhandle);
145 146 147 148
        placeholder_nlhandle = NULL;
    }
}

L
Leno Hou 已提交
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

/**
 * virNetLinkCreateSocket:
 *
 * @protocol: which protocol to connect to (e.g. NETLINK_ROUTE,
 *
 * Create a netlink socket, set its buffer size, and turn on message
 * peeking (so the buffer size can be dynamically increased if
 * needed).
 *
 * Returns a handle to the new netlink socket, or 0 if there was a failure.
 *
 */
static virNetlinkHandle *
virNetlinkCreateSocket(int protocol)
{
    virNetlinkHandle *nlhandle = NULL;

    if (!(nlhandle = virNetlinkAlloc())) {
        virReportSystemError(errno, "%s",
                             _("cannot allocate nlhandle for netlink"));
        goto error;
    }
    if (nl_connect(nlhandle, protocol) < 0) {
        virReportSystemError(errno,
                             _("cannot connect to netlink socket "
                               "with protocol %d"), protocol);
        goto error;
    }

    if (virNetlinkSetBufferSize(nlhandle, 131702, 0) < 0) {
        virReportSystemError(errno, "%s",
                             _("cannot set netlink socket buffer "
                               "size to 128k"));
        goto error;
    }
    nl_socket_enable_msg_peek(nlhandle);

 cleanup:
    return nlhandle;

 error:
    if (nlhandle) {
        nl_close(nlhandle);
        virNetlinkFree(nlhandle);
        nlhandle = NULL;
    }
    goto cleanup;
}

199 200 201
static virNetlinkHandle *
virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t src_pid,
                      struct sockaddr_nl nladdr,
202
                      unsigned int protocol, unsigned int groups)
203 204 205 206
{
    ssize_t nbytes;
    int fd;
    int n;
207
    virNetlinkHandle *nlhandle = NULL;
208 209
    struct pollfd fds[1];
    struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg);
210 211 212 213

    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
214
        goto error;
215
    }
216

L
Leno Hou 已提交
217
    if (!(nlhandle = virNetlinkCreateSocket(protocol)))
218
        goto error;
219 220 221 222 223

    fd = nl_socket_get_fd(nlhandle);
    if (fd < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot get netlink socket fd"));
224
        goto error;
225 226 227
    }

    if (groups && nl_socket_add_membership(nlhandle, groups) < 0) {
228
        virReportSystemError(errno,
229
                             "%s", _("cannot add netlink membership"));
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

    nbytes = nl_send_auto_complete(nlhandle, nl_msg);
    if (nbytes < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot send to netlink socket"));
241
        goto error;
242 243
    }

244
    memset(fds, 0, sizeof(fds));
245

246 247
    fds[0].fd = fd;
    fds[0].events = POLLIN;
248

249
    n = poll(fds, G_N_ELEMENTS(fds), NETLINK_ACK_TIMEOUT_S);
250 251 252
    if (n <= 0) {
        if (n < 0)
            virReportSystemError(errno, "%s",
253
                                 _("error in poll call"));
254 255 256 257 258
        if (n == 0)
            virReportSystemError(ETIMEDOUT, "%s",
                                 _("no valid netlink response was received"));
    }

259 260 261 262 263 264 265 266 267
    return nlhandle;

 error:
    virNetlinkFree(nlhandle);
    return NULL;
}

/**
 * virNetlinkCommand:
268 269
 * @nl_msg:     pointer to netlink message
 * @resp:       pointer to pointer where response buffer will be allocated
270
 * @respbuflen: pointer to integer holding the size of the response buffer
271 272 273 274 275
 *              on return of the function.
 * @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
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
 *
 * 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.
 */
int virNetlinkCommand(struct nl_msg *nl_msg,
                      struct nlmsghdr **resp, unsigned int *respbuflen,
                      uint32_t src_pid, uint32_t dst_pid,
                      unsigned int protocol, unsigned int groups)
{
    struct sockaddr_nl nladdr = {
            .nl_family = AF_NETLINK,
            .nl_pid    = dst_pid,
            .nl_groups = 0,
    };
    struct pollfd fds[1];
292
    g_autofree struct nlmsghdr *temp_resp = NULL;
J
Ján Tomko 已提交
293
    g_autoptr(virNetlinkHandle) nlhandle = NULL;
294 295 296 297 298 299
    int len = 0;

    memset(fds, 0, sizeof(fds));

    if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
                                           protocol, groups)))
300
        return -1;
301

302
    len = nl_recv(nlhandle, &nladdr, (unsigned char **)&temp_resp, NULL);
303 304 305
    if (len == 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("nl_recv failed - returned 0 bytes"));
306
        return -1;
307
    }
308 309
    if (len < 0) {
        virReportSystemError(errno, "%s", _("nl_recv failed"));
310
        return -1;
311 312
    }

313
    *resp = g_steal_pointer(&temp_resp);
314
    *respbuflen = len;
315
    return 0;
316 317
}

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
int
virNetlinkDumpCommand(struct nl_msg *nl_msg,
                      virNetlinkDumpCallback callback,
                      uint32_t src_pid, uint32_t dst_pid,
                      unsigned int protocol, unsigned int groups,
                      void *opaque)
{
    bool end = false;
    int len = 0;
    struct nlmsghdr *msg = NULL;

    struct sockaddr_nl nladdr = {
            .nl_family = AF_NETLINK,
            .nl_pid    = dst_pid,
            .nl_groups = 0,
    };
J
Ján Tomko 已提交
334
    g_autoptr(virNetlinkHandle) nlhandle = NULL;
335 336 337

    if (!(nlhandle = virNetlinkSendRequest(nl_msg, src_pid, nladdr,
                                           protocol, groups)))
338
        return -1;
339 340

    while (!end) {
341
        g_autofree struct nlmsghdr *resp = NULL;
342

343
        len = nl_recv(nlhandle, &nladdr, (unsigned char **)&resp, NULL);
344
        VIR_WARNINGS_NO_CAST_ALIGN
345
        for (msg = resp; NLMSG_OK(msg, len); msg = NLMSG_NEXT(msg, len)) {
346
            VIR_WARNINGS_RESET
347 348 349 350
            if (msg->nlmsg_type == NLMSG_DONE)
                end = true;

            if (virNetlinkGetErrorCode(msg, len) < 0)
351
                return -1;
352 353

            if (callback(msg, opaque) < 0)
354
                return -1;
355 356 357
        }
    }

358
    return 0;
359 360
}

361 362 363 364 365
/**
 * virNetlinkDumpLink:
 *
 * @ifname:  The name of the interface; only use if ifindex <= 0
 * @ifindex: The interface index; may be <= 0 if ifname is given
366
 * @nlData:  Gets a pointer to the raw data from netlink.
367
             MUST BE FREED BY CALLER!
368
 * @tb:      Pointer to a pointer of netlink attributes that will contain
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
 *           the results
 * @src_pid: pid used for nl_pid of the local end of the netlink message
 *           (0 == "use getpid()")
 * @dst_pid: pid of destination nl_pid if the kernel
 *           is not the target of the netlink message but it is to be
 *           sent to another process (0 if sending to the kernel)
 *
 * Get information from netlink about an interface given its name or index.
 *
 * Returns 0 on success, -1 on fatal error.
 */
int
virNetlinkDumpLink(const char *ifname, int ifindex,
                   void **nlData, struct nlattr **tb,
                   uint32_t src_pid, uint32_t dst_pid)
{
    int rc = -1;
    struct nlmsgerr *err;
    struct ifinfomsg ifinfo = {
        .ifi_family = AF_UNSPEC,
        .ifi_index  = ifindex
    };
    unsigned int recvbuflen;
J
Ján Tomko 已提交
392
    g_autoptr(virNetlinkMsg) nl_msg = NULL;
393
    g_autofree struct nlmsghdr *resp = NULL;
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430

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

    ifinfo.ifi_index = ifindex;

    nl_msg = nlmsg_alloc_simple(RTM_GETLINK, NLM_F_REQUEST);
    if (!nl_msg) {
        virReportOOMError();
        return -1;
    }

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

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

# ifdef RTEXT_FILTER_VF
    /* if this filter exists in the kernel's netlink implementation,
     * we need to set it, otherwise the response message will not
     * contain the IFLA_VFINFO_LIST that we're looking for.
     */
    {
        uint32_t ifla_ext_mask = RTEXT_FILTER_VF;

        if (nla_put(nl_msg, IFLA_EXT_MASK,
                    sizeof(ifla_ext_mask), &ifla_ext_mask) < 0) {
            goto buffer_too_small;
        }
    }
# endif

    if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
                          src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
431
        return -1;
432 433 434 435 436 437 438 439 440 441 442 443 444 445

    if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
        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,
                                 _("error dumping %s (%d) interface"),
                                 ifname, ifindex);
446
            return -1;
447 448 449 450 451 452 453 454 455 456 457 458 459 460
        }
        break;

    case GENL_ID_CTRL:
    case NLMSG_DONE:
        rc = nlmsg_parse(resp, sizeof(struct ifinfomsg),
                         tb, IFLA_MAX, NULL);
        if (rc < 0)
            goto malformed_resp;
        break;

    default:
        goto malformed_resp;
    }
461

462
    *nlData = g_steal_pointer(&resp);
463
    return 0;
464 465 466 467

 malformed_resp:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("malformed netlink response message"));
468
    return rc;
469 470 471 472

 buffer_too_small:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("allocated netlink buffer is too small"));
473
    return rc;
474 475 476
}


477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
/**
 * virNetlinkNewLink:
 *
 * @ifname: name of the link
 * @type: the type of the device, i.e. "bridge", "macvtap", "macvlan"
 * @extra_args: the extra args for creating the netlink interface
 * @error: netlink error code
 *
 * A generic wrapper to create a network link.
 *
 * Returns 0 on success, -1 on error. Additionally, if the @error is
 * non-zero, then a failure occurred during virNetlinkCommand, but
 * no error message is generated leaving it up to the caller to handle
 * the condition.
 */
int
virNetlinkNewLink(const char *ifname,
                  const char *type,
                  virNetlinkNewLinkDataPtr extra_args,
                  int *error)
{
    struct nlmsgerr *err;
    struct nlattr *linkinfo = NULL;
    struct nlattr *infodata = NULL;
    unsigned int buflen;
    struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
J
Ján Tomko 已提交
503
    g_autoptr(virNetlinkMsg) nl_msg = NULL;
504
    g_autofree struct nlmsghdr *resp = NULL;
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525

    *error = 0;

    VIR_DEBUG("Creating %s interface '%s'", type, ifname);

    if (!ifname || !type) {
        virReportError(VIR_ERR_INVALID_ARG, "%s",
                       _("both interface name and type must not be NULL"));
        return -1;
    }

    nl_msg = nlmsg_alloc_simple(RTM_NEWLINK,
                                NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
    if (!nl_msg) {
        virReportOOMError();
        return -1;
    }

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

526
    NETLINK_MSG_PUT(nl_msg, IFLA_IFNAME, (strlen(ifname) + 1), ifname);
527

528 529
    NETLINK_MSG_NEST_START(nl_msg, linkinfo, IFLA_LINKINFO);
    NETLINK_MSG_PUT(nl_msg, IFLA_INFO_KIND, (strlen(type) + 1), type);
530 531

    if ((STREQ(type, "macvtap") || STREQ(type, "macvlan")) &&
532 533 534
        extra_args &&
        extra_args->macvlan_mode &&
        *extra_args->macvlan_mode > 0) {
535 536 537 538
        NETLINK_MSG_NEST_START(nl_msg, infodata, IFLA_INFO_DATA);
        NETLINK_MSG_PUT(nl_msg, IFLA_MACVLAN_MODE,
                        sizeof(uint32_t), extra_args->macvlan_mode);
        NETLINK_MSG_NEST_END(nl_msg, infodata);
539 540
    }

541
    NETLINK_MSG_NEST_END(nl_msg, linkinfo);
542 543

    if (extra_args) {
544 545
        if (extra_args->ifindex) {
            NETLINK_MSG_PUT(nl_msg, IFLA_LINK,
546
                        sizeof(uint32_t), extra_args->ifindex);
547 548 549 550 551
        }
        if (extra_args->mac) {
            NETLINK_MSG_PUT(nl_msg, IFLA_ADDRESS,
                            VIR_MAC_BUFLEN, extra_args->mac);
        }
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
    }

    if (virNetlinkCommand(nl_msg, &resp, &buflen, 0, 0, NETLINK_ROUTE, 0) < 0)
        return -1;

    if (buflen < NLMSG_LENGTH(0) || resp == NULL)
        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 < 0) {
            *error = err->error;
            return -1;
        }
        break;

    case NLMSG_DONE:
        break;

    default:
        goto malformed_resp;
    }

    return 0;

 malformed_resp:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("malformed netlink response message"));
    return -1;

 buffer_too_small:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("allocated netlink buffer is too small"));
    return -1;
}


593 594 595
/**
 * virNetlinkDelLink:
 *
596
 * @ifname:   Name of the link
597 598 599 600
 * @fallback: pointer to an alternate function that will
 *            be called to perform the delete if RTM_DELLINK fails
 *            with EOPNOTSUPP (any other error will simply be treated
 *            as an error).
601 602 603 604 605 606 607 608
 *
 * delete a network "link" (aka interface aka device) with the given
 * name. This works for many different types of network devices,
 * including macvtap and bridges.
 *
 * Returns 0 on success, -1 on fatal error.
 */
int
609
virNetlinkDelLink(const char *ifname, virNetlinkDelLinkFallback fallback)
610 611 612 613
{
    struct nlmsgerr *err;
    struct ifinfomsg ifinfo = { .ifi_family = AF_UNSPEC };
    unsigned int recvbuflen;
J
Ján Tomko 已提交
614
    g_autoptr(virNetlinkMsg) nl_msg = NULL;
615
    g_autofree struct nlmsghdr *resp = NULL;
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

    nl_msg = nlmsg_alloc_simple(RTM_DELLINK,
                                NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL);
    if (!nl_msg) {
        virReportOOMError();
        return -1;
    }

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

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

    if (virNetlinkCommand(nl_msg, &resp, &recvbuflen, 0, 0,
                          NETLINK_ROUTE, 0) < 0) {
632
        return -1;
633 634 635 636 637 638 639 640 641 642 643
    }

    if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
        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;

644 645 646
        if (-err->error == EOPNOTSUPP && fallback)
            return fallback(ifname);

647 648 649 650
        if (err->error) {
            virReportSystemError(-err->error,
                                 _("error destroying network device %s"),
                                 ifname);
651
            return -1;
652 653 654 655 656 657 658 659 660 661
        }
        break;

    case NLMSG_DONE:
        break;

    default:
        goto malformed_resp;
    }

662
    return 0;
663 664 665 666

 malformed_resp:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("malformed netlink response message"));
667
    return -1;
668 669 670 671

 buffer_too_small:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("allocated netlink buffer is too small"));
672
    return -1;
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
/**
 * virNetlinkGetNeighbor:
 *
 * @nlData:  Gets a pointer to the raw data from netlink.
             MUST BE FREED BY CALLER!
 * @src_pid: pid used for nl_pid of the local end of the netlink message
 *           (0 == "use getpid()")
 * @dst_pid: pid of destination nl_pid if the kernel
 *           is not the target of the netlink message but it is to be
 *           sent to another process (0 if sending to the kernel)
 *
 * Get neighbor table entry from netlink.
 *
 * Returns 0 on success, -1 on fatal error.
 */
int
virNetlinkGetNeighbor(void **nlData, uint32_t src_pid, uint32_t dst_pid)
{
    struct nlmsgerr *err;
    struct ndmsg ndinfo = {
        .ndm_family = AF_UNSPEC,
    };
    unsigned int recvbuflen;
J
Ján Tomko 已提交
698
    g_autoptr(virNetlinkMsg) nl_msg = NULL;
699
    g_autofree struct nlmsghdr *resp = NULL;
700 701 702 703 704 705 706 707 708 709 710 711 712

    nl_msg = nlmsg_alloc_simple(RTM_GETNEIGH, NLM_F_DUMP | NLM_F_REQUEST);
    if (!nl_msg) {
        virReportOOMError();
        return -1;
    }

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


    if (virNetlinkCommand(nl_msg, &resp, &recvbuflen,
                          src_pid, dst_pid, NETLINK_ROUTE, 0) < 0)
713
        return -1;
714 715 716 717 718 719 720 721 722 723 724 725 726

    if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
        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,
                                 "%s", _("error dumping"));
727
            return -1;
728 729 730 731 732 733 734 735 736
        }
        break;

    case RTM_NEWNEIGH:
        break;

    default:
        goto malformed_resp;
    }
737

738
    *nlData = g_steal_pointer(&resp);
739
    return recvbuflen;
740 741 742 743

 malformed_resp:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("malformed netlink response message"));
744
    return -1;
745 746 747 748

 buffer_too_small:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("allocated netlink buffer is too small"));
749
    return -1;
750
}
751

E
Eric Blake 已提交
752 753
int
virNetlinkGetErrorCode(struct nlmsghdr *resp, unsigned int recvbuflen)
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
{
    struct nlmsgerr *err;
    int result = 0;

    if (recvbuflen < NLMSG_LENGTH(0) || resp == NULL)
        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;

        switch (err->error) {
        case 0: /* ACK */
            break;

        default:
            result = err->error;
        }
        break;

    case NLMSG_DONE:
        break;

    default:
780 781 782
        /* We allow multipart messages. */
        if (!(resp->nlmsg_flags & NLM_F_MULTI))
            goto malformed_resp;
783 784 785 786 787 788 789 790 791 792
    }

    return result;

 malformed_resp:
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                   _("malformed netlink response message"));
    return -EINVAL;
}

E
Eric Blake 已提交
793

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
static void
virNetlinkEventServerLock(virNetlinkEventSrvPrivatePtr driver)
{
    virMutexLock(&driver->lock);
}

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

/**
 * virNetlinkEventRemoveClientPrimitive:
 *
809
 * @i:        index of the client to remove from the table
810
 * @protocol: netlink protocol
811 812 813 814 815 816 817 818 819
 *
 * 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.
 */
820 821
static int
virNetlinkEventRemoveClientPrimitive(size_t i, unsigned int protocol)
822
{
823 824 825 826
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    virNetlinkEventRemoveCallback removeCB = server[protocol]->handles[i].removeCB;
827 828

    if (removeCB) {
829 830 831
        (removeCB)(server[protocol]->handles[i].watch,
                   &server[protocol]->handles[i].macaddr,
                   server[protocol]->handles[i].opaque);
832
    }
833 834
    server[protocol]->handles[i].deleted = VIR_NETLINK_HANDLE_DELETED;
    return 0;
835 836 837 838
}

static void
virNetlinkEventCallback(int watch,
J
Ján Tomko 已提交
839 840
                        int fd G_GNUC_UNUSED,
                        int events G_GNUC_UNUSED,
841 842 843 844 845
                        void *opaque)
{
    virNetlinkEventSrvPrivatePtr srv = opaque;
    struct sockaddr_nl peer;
    struct ucred *creds = NULL;
846 847
    size_t i;
    int length;
848
    bool handled = false;
849
    g_autofree struct nlmsghdr *msg = NULL;
850

851 852
    length = nl_recv(srv->netlinknh, &peer,
                     (unsigned char **)&msg, &creds);
853 854 855 856

    if (length == 0)
        return;
    if (length < 0) {
857 858
        virReportSystemError(errno,
                             "%s", _("nl_recv returned with error"));
859 860 861 862 863 864
        return;
    }

    virNetlinkEventServerLock(srv);

    VIR_DEBUG("dispatching to max %d clients, called from event watch %d",
865
              (int)srv->handlesCount, watch);
866 867 868 869 870

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

871
        VIR_DEBUG("dispatching client %zu.", i);
872 873 874 875 876 877 878

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

    if (!handled)
        VIR_DEBUG("event not handled.");
879

880 881 882 883 884 885 886 887 888
    virNetlinkEventServerUnlock(srv);
}

/**
 * virNetlinkEventServiceStop:
 *
 * stop the monitor to receive netlink messages for libvirtd.
 * This removes the netlink socket fd from the event handler.
 *
889 890
 * @protocol: netlink protocol
 *
891
 * Returns -1 if the monitor cannot be unregistered, 0 upon success
892 893
 */
int
894
virNetlinkEventServiceStop(unsigned int protocol)
895
{
896 897 898 899
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    virNetlinkEventSrvPrivatePtr srv = server[protocol];
900
    size_t i;
901 902 903

    VIR_INFO("stopping netlink event service");

904
    if (!server[protocol])
905 906 907 908
        return 0;

    virNetlinkEventServerLock(srv);
    nl_close(srv->netlinknh);
S
Serge Hallyn 已提交
909
    virNetlinkFree(srv->netlinknh);
910 911 912 913 914
    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)
915
            virNetlinkEventRemoveClientPrimitive(i, protocol);
916 917
    }

918
    server[protocol] = NULL;
919
    VIR_FREE(srv->handles);
920 921 922 923 924 925 926
    virNetlinkEventServerUnlock(srv);

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

927 928 929 930 931 932 933 934 935 936
/**
 * virNetlinkEventServiceStopAll:
 *
 * Stop all the monitors to receive netlink messages for libvirtd.
 *
 * Returns -1 if any monitor cannot be unregistered, 0 upon success
 */
int
virNetlinkEventServiceStopAll(void)
{
937
    size_t i;
938 939 940

    VIR_INFO("stopping all netlink event services");

941 942
    for (i = 0; i < MAX_LINKS; i++)
        virNetlinkEventServiceStop(i);
943 944 945 946

    return 0;
}

947 948 949
/**
 * virNetlinkEventServiceIsRunning:
 *
950
 * Returns if the netlink event service is running.
951
 *
952 953
 * @protocol: netlink protocol
 *
954
 * Returns 'true' if the service is running, 'false' if stopped.
955 956
 */
bool
957
virNetlinkEventServiceIsRunning(unsigned int protocol)
958
{
959 960 961 962 963 964 965
    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
        return false;
    }

    return server[protocol] != NULL;
966 967
}

968 969 970
/**
 * virNetlinkEventServiceLocalPid:
 *
971 972
 * @protocol: netlink protocol
 *
973 974 975 976
 * 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).
 */
977
int virNetlinkEventServiceLocalPid(unsigned int protocol)
978
{
979 980 981 982
    if (protocol >= MAX_LINKS)
        return -EINVAL;

    if (!(server[protocol] && server[protocol]->netlinknh)) {
983 984
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("netlink event service not running"));
985 986
        return -1;
    }
987
    return (int)nl_socket_get_local_port(server[protocol]->netlinknh);
988 989 990
}


991 992 993 994 995 996
/**
 * virNetlinkEventServiceStart:
 *
 * start a monitor to receive netlink messages for libvirtd.
 * This registers a netlink socket with the event interface.
 *
997
 * @protocol: netlink protocol
998 999
 * @groups:   broadcast groups to join in
 *
1000
 * Returns -1 if the monitor cannot be registered, 0 upon success
1001 1002
 */
int
1003
virNetlinkEventServiceStart(unsigned int protocol, unsigned int groups)
1004 1005 1006 1007 1008
{
    virNetlinkEventSrvPrivatePtr srv;
    int fd;
    int ret = -1;

1009 1010 1011 1012 1013 1014 1015
    if (protocol >= MAX_LINKS) {
        virReportSystemError(EINVAL,
                             _("invalid protocol argument: %d"), protocol);
        return -EINVAL;
    }

    if (server[protocol])
1016 1017
        return 0;

1018
    VIR_INFO("starting netlink event service with protocol %d", protocol);
1019

1020
    if (VIR_ALLOC(srv) < 0)
1021
        return -1;
1022

1023 1024 1025 1026
    if (virMutexInit(&srv->lock) < 0) {
        VIR_FREE(srv);
        return -1;
    }
1027 1028 1029 1030

    virNetlinkEventServerLock(srv);

    /* Allocate a new socket and get fd */
L
Leno Hou 已提交
1031
    if (!(srv->netlinknh = virNetlinkCreateSocket(protocol)))
1032 1033 1034 1035
        goto error_locked;

    fd = nl_socket_get_fd(srv->netlinknh);
    if (fd < 0) {
1036 1037
        virReportSystemError(errno,
                             "%s", _("cannot get netlink socket fd"));
1038 1039 1040
        goto error_server;
    }

1041 1042 1043 1044 1045 1046
    if (groups && nl_socket_add_membership(srv->netlinknh, groups) < 0) {
        virReportSystemError(errno,
                             "%s", _("cannot add netlink membership"));
        goto error_server;
    }

1047
    if (nl_socket_set_nonblocking(srv->netlinknh)) {
1048 1049
        virReportSystemError(errno, "%s",
                             _("cannot set netlink socket nonblocking"));
1050 1051 1052 1053 1054 1055 1056
        goto error_server;
    }

    if ((srv->eventwatch = virEventAddHandle(fd,
                                             VIR_EVENT_HANDLE_READABLE,
                                             virNetlinkEventCallback,
                                             srv, NULL)) < 0) {
1057 1058
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Failed to add netlink event handle watch"));
1059 1060 1061 1062 1063 1064 1065
        goto error_server;
    }

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

    ret = 0;
1066
    server[protocol] = srv;
1067

1068
 error_server:
1069 1070
    if (ret < 0) {
        nl_close(srv->netlinknh);
S
Serge Hallyn 已提交
1071
        virNetlinkFree(srv->netlinknh);
1072
    }
1073
 error_locked:
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
    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
1087 1088 1089
 * @opaque:   user data to pass to callback
 * @macaddr:  macaddr to store with the data. Used to identify callers.
 *            May be null.
1090
 * @protocol: netlink protocol
1091 1092 1093 1094 1095
 *
 * register a callback for handling of netlink messages. The
 * registered function receives the entire netlink message and
 * may choose to act upon it.
 *
1096 1097
 * Returns -1 if the file handle cannot be registered, number of
 * monitor upon success.
1098 1099 1100 1101
 */
int
virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB,
                         virNetlinkEventRemoveCallback removeCB,
1102
                         void *opaque, const virMacAddr *macaddr,
1103
                         unsigned int protocol)
1104
{
1105 1106
    size_t i;
    int r, ret = -1;
1107 1108 1109 1110 1111 1112
    virNetlinkEventSrvPrivatePtr srv = NULL;

    if (protocol >= MAX_LINKS)
        return -EINVAL;

    srv = server[protocol];
1113

1114
    if (handleCB == NULL) {
1115 1116
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("Invalid NULL callback provided"));
1117
        return -1;
1118
    }
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

    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,
1137
                         srv->handlesCount, NETLINK_EVENT_ALLOC_EXTENT) < 0)
1138 1139 1140 1141
            goto error;
    }
    r = srv->handlesCount++;

1142
 addentry:
1143 1144 1145 1146 1147 1148
    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)
1149
        virMacAddrSet(&srv->handles[r].macaddr, macaddr);
1150
    else
1151
        virMacAddrSetRaw(&srv->handles[r].macaddr,
E
Eric Blake 已提交
1152
                         (unsigned char[VIR_MAC_BUFLEN]){0, 0, 0, 0, 0, 0});
1153 1154 1155 1156

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

    ret = nextWatch++;
1157
 error:
1158 1159 1160 1161 1162 1163 1164
    virNetlinkEventServerUnlock(srv);
    return ret;
}

/**
 * virNetlinkEventRemoveClient:
 *
1165 1166
 * @watch:    watch whose handle to remove
 * @macaddr:  macaddr whose handle to remove
1167
 * @protocol: netlink protocol
1168 1169 1170 1171 1172
 *
 * 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.
 *
1173
 * Returns -1 if the file handle was not registered, 0 upon success
1174 1175
 */
int
1176
virNetlinkEventRemoveClient(int watch, const virMacAddr *macaddr,
1177
                            unsigned int protocol)
1178
{
1179
    size_t i;
1180
    int ret = -1;
1181 1182 1183 1184 1185 1186
    virNetlinkEventSrvPrivatePtr srv = NULL;

    if (protocol >= MAX_LINKS)
        return -EINVAL;

    srv = server[protocol];
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202

    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 &&
1203
             virMacAddrCmp(macaddr, &srv->handles[i].macaddr) == 0)) {
1204 1205 1206

            VIR_DEBUG("removed client: %d by %s.",
                      srv->handles[i].watch, watch ? "index" : "mac");
1207
            virNetlinkEventRemoveClientPrimitive(i, protocol);
1208 1209 1210 1211 1212 1213
            ret = 0;
            goto cleanup;
        }
    }
    VIR_DEBUG("no client found to remove.");

1214
 cleanup:
1215 1216 1217 1218
    virNetlinkEventServerUnlock(srv);
    return ret;
}

1219 1220
#else

1221 1222 1223 1224 1225 1226
# 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

1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
int
virNetlinkStartup(void)
{
    return 0;
}

void
virNetlinkShutdown(void)
{
    return;
}

J
Ján Tomko 已提交
1239 1240 1241 1242 1243 1244 1245
int virNetlinkCommand(struct nl_msg *nl_msg G_GNUC_UNUSED,
                      struct nlmsghdr **resp G_GNUC_UNUSED,
                      unsigned int *respbuflen G_GNUC_UNUSED,
                      uint32_t src_pid G_GNUC_UNUSED,
                      uint32_t dst_pid G_GNUC_UNUSED,
                      unsigned int protocol G_GNUC_UNUSED,
                      unsigned int groups G_GNUC_UNUSED)
1246
{
1247
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
1248 1249 1250
    return -1;
}

1251
int
J
Ján Tomko 已提交
1252 1253 1254 1255 1256 1257 1258
virNetlinkDumpCommand(struct nl_msg *nl_msg G_GNUC_UNUSED,
                      virNetlinkDumpCallback callback G_GNUC_UNUSED,
                      uint32_t src_pid G_GNUC_UNUSED,
                      uint32_t dst_pid G_GNUC_UNUSED,
                      unsigned int protocol G_GNUC_UNUSED,
                      unsigned int groups G_GNUC_UNUSED,
                      void *opaque G_GNUC_UNUSED)
1259 1260 1261 1262
{
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
    return -1;
}
1263

1264
int
J
Ján Tomko 已提交
1265 1266 1267 1268 1269 1270
virNetlinkDumpLink(const char *ifname G_GNUC_UNUSED,
                   int ifindex G_GNUC_UNUSED,
                   void **nlData G_GNUC_UNUSED,
                   struct nlattr **tb G_GNUC_UNUSED,
                   uint32_t src_pid G_GNUC_UNUSED,
                   uint32_t dst_pid G_GNUC_UNUSED)
1271 1272 1273 1274 1275 1276 1277
{
    virReportSystemError(ENOSYS, "%s",
                         _("Unable to dump link info on this platform"));
    return -1;
}


1278
int
J
Ján Tomko 已提交
1279 1280
virNetlinkDelLink(const char *ifname G_GNUC_UNUSED,
                  virNetlinkDelLinkFallback fallback G_GNUC_UNUSED)
1281 1282 1283 1284 1285
{
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
    return -1;
}

1286

1287
int
J
Ján Tomko 已提交
1288 1289 1290 1291
virNetlinkNewLink(const char *ifname G_GNUC_UNUSED,
                  const char *type G_GNUC_UNUSED,
                  virNetlinkNewLinkDataPtr extra_args G_GNUC_UNUSED,
                  int *error G_GNUC_UNUSED)
1292 1293 1294 1295 1296 1297
{
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
    return -1;
}


1298
int
J
Ján Tomko 已提交
1299 1300 1301
virNetlinkGetNeighbor(void **nlData G_GNUC_UNUSED,
                      uint32_t src_pid G_GNUC_UNUSED,
                      uint32_t dst_pid G_GNUC_UNUSED)
1302 1303 1304 1305 1306 1307
{
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
    return -1;
}


1308
/**
1309 1310
 * stopNetlinkEventServer: stop the monitor to receive netlink
 * messages for libvirtd
1311
 */
J
Ján Tomko 已提交
1312
int virNetlinkEventServiceStop(unsigned int protocol G_GNUC_UNUSED)
1313
{
1314
    VIR_DEBUG("%s", _(unsupported));
1315 1316 1317
    return 0;
}

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
/**
 * stopNetlinkEventServerAll: stop all the monitors to receive netlink
 * messages for libvirtd
 */
int virNetlinkEventServiceStopAll(void)
{
    VIR_DEBUG("%s", _(unsupported));
    return 0;
}

1328
/**
1329 1330
 * startNetlinkEventServer: start a monitor to receive netlink
 * messages for libvirtd
1331
 */
J
Ján Tomko 已提交
1332 1333
int virNetlinkEventServiceStart(unsigned int protocol G_GNUC_UNUSED,
                                unsigned int groups G_GNUC_UNUSED)
1334
{
1335
    VIR_DEBUG("%s", _(unsupported));
1336 1337 1338 1339
    return 0;
}

/**
1340 1341
 * virNetlinkEventServiceIsRunning: returns if the netlink event
 * service is running.
1342
 */
J
Ján Tomko 已提交
1343
bool virNetlinkEventServiceIsRunning(unsigned int protocol G_GNUC_UNUSED)
1344
{
1345
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
1346 1347 1348
    return 0;
}

J
Ján Tomko 已提交
1349
int virNetlinkEventServiceLocalPid(unsigned int protocol G_GNUC_UNUSED)
1350
{
1351
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
1352 1353 1354
    return -1;
}

1355
/**
1356 1357
 * virNetlinkEventAddClient: register a callback for handling of
 * netlink messages
1358
 */
J
Ján Tomko 已提交
1359 1360 1361 1362 1363
int virNetlinkEventAddClient(virNetlinkEventHandleCallback handleCB G_GNUC_UNUSED,
                             virNetlinkEventRemoveCallback removeCB G_GNUC_UNUSED,
                             void *opaque G_GNUC_UNUSED,
                             const virMacAddr *macaddr G_GNUC_UNUSED,
                             unsigned int protocol G_GNUC_UNUSED)
1364
{
1365
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
1366 1367 1368 1369 1370 1371
    return -1;
}

/**
 * virNetlinkEventRemoveClient: unregister a callback from a netlink monitor
 */
J
Ján Tomko 已提交
1372 1373 1374
int virNetlinkEventRemoveClient(int watch G_GNUC_UNUSED,
                                const virMacAddr *macaddr G_GNUC_UNUSED,
                                unsigned int protocol G_GNUC_UNUSED)
1375
{
1376
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
1377 1378 1379
    return -1;
}

E
Eric Blake 已提交
1380 1381

int
J
Ján Tomko 已提交
1382 1383
virNetlinkGetErrorCode(struct nlmsghdr *resp G_GNUC_UNUSED,
                       unsigned int recvbuflen G_GNUC_UNUSED)
E
Eric Blake 已提交
1384 1385 1386 1387 1388
{
    virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
    return -EINVAL;
}

1389
#endif /* __linux__ */