X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib%2Fnetlink-notifier.c;h=9aa185d5b1a3149611af747a8c25621c57562cf6;hb=cfc50ae514f805dcd9c14589f21158185424daf6;hp=1b7529dd7726ce9f2c358d780f536c994aa80dd4;hpb=0a811051ffd7f836f66ef770d7a3dcc92ec7d51a;p=sliver-openvswitch.git diff --git a/lib/netlink-notifier.c b/lib/netlink-notifier.c index 1b7529dd7..9aa185d5b 100644 --- a/lib/netlink-notifier.c +++ b/lib/netlink-notifier.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010, 2011 Nicira Networks. + * Copyright (c) 2009, 2010, 2011, 2012, 2013 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -41,11 +41,19 @@ struct nln { /* Passed in by nln_create(). */ int multicast_group; /* Multicast group we listen on. */ - int protocol; /* Protocal passed to nl_sock_create(). */ + int protocol; /* Protocol passed to nl_sock_create(). */ nln_parse_func *parse; /* Message parsing function. */ void *change; /* Change passed to parse. */ }; +struct nln_notifier { + struct nln *nln; /* Parent nln. */ + + struct list node; + nln_notify_func *cb; + void *aux; +}; + /* Creates an nln handle which may be used to manage change notifications. The * created handle will listen for netlink messages on 'multicast_group' using * netlink protocol 'protocol' (e.g. NETLINK_ROUTE, NETLINK_GENERIC, ...). @@ -70,11 +78,15 @@ nln_create(int protocol, int multicast_group, nln_parse_func *parse, } /* Destroys 'nln' by freeing any memory it has reserved and closing any sockets - * it has opened. */ + * it has opened. + * + * The caller is responsible for destroying any notifiers created by this + * 'nln' before destroying 'nln'. */ void nln_destroy(struct nln *nln) { if (nln) { + ovs_assert(list_is_empty(&nln->all_notifiers)); nl_sock_destroy(nln->notify_sock); free(nln); } @@ -87,11 +99,12 @@ nln_destroy(struct nln *nln) * This is probably not the function you want. You should probably be using * message specific notifiers like rtnetlink_link_notifier_register(). * - * Returns 0 if successful, otherwise a positive errno value. */ -int -nln_notifier_register(struct nln *nln, struct nln_notifier *notifier, - nln_notify_func *cb, void *aux) + * Returns an initialized nln_notifier if successful, otherwise NULL. */ +struct nln_notifier * +nln_notifier_create(struct nln *nln, nln_notify_func *cb, void *aux) { + struct nln_notifier *notifier; + if (!nln->notify_sock) { struct nl_sock *sock; int error; @@ -102,38 +115,46 @@ nln_notifier_register(struct nln *nln, struct nln_notifier *notifier, } if (error) { nl_sock_destroy(sock); - VLOG_WARN("could not create netlink socket: %s", strerror(error)); - return error; + VLOG_WARN("could not create netlink socket: %s", + ovs_strerror(error)); + return NULL; } nln->notify_sock = sock; } else { /* Catch up on notification work so that the new notifier won't * receive any stale notifications. */ - nln_notifier_run(nln); + nln_run(nln); } + notifier = xmalloc(sizeof *notifier); list_push_back(&nln->all_notifiers, ¬ifier->node); notifier->cb = cb; notifier->aux = aux; - return 0; + notifier->nln = nln; + return notifier; } -/* Cancels notification on 'notifier', which must have previously been - * registered with nln_notifier_register(). */ +/* Destroys 'notifier', which must have previously been created with + * nln_notifier_register(). */ void -nln_notifier_unregister(struct nln *nln, struct nln_notifier *notifier) +nln_notifier_destroy(struct nln_notifier *notifier) { - list_remove(¬ifier->node); - if (list_is_empty(&nln->all_notifiers)) { - nl_sock_destroy(nln->notify_sock); - nln->notify_sock = NULL; + if (notifier) { + struct nln *nln = notifier->nln; + + list_remove(¬ifier->node); + if (list_is_empty(&nln->all_notifiers)) { + nl_sock_destroy(nln->notify_sock); + nln->notify_sock = NULL; + } + free(notifier); } } /* Calls all of the registered notifiers, passing along any as-yet-unreported * change events. */ void -nln_notifier_run(struct nln *nln) +nln_run(struct nln *nln) { static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); @@ -143,18 +164,20 @@ nln_notifier_run(struct nln *nln) nln->has_run = true; for (;;) { - struct ofpbuf *buf; + uint64_t buf_stub[4096 / 8]; + struct ofpbuf buf; int error; + ofpbuf_use_stub(&buf, buf_stub, sizeof buf_stub); error = nl_sock_recv(nln->notify_sock, &buf, false); if (!error) { - if (nln->parse(buf, nln->change)) { + if (nln->parse(&buf, nln->change)) { nln_report(nln, nln->change); } else { VLOG_WARN_RL(&rl, "received bad netlink message"); nln_report(nln, NULL); } - ofpbuf_delete(buf); + ofpbuf_uninit(&buf); } else if (error == EAGAIN) { return; } else { @@ -162,7 +185,7 @@ nln_notifier_run(struct nln *nln) VLOG_WARN_RL(&rl, "netlink receive buffer overflowed"); } else { VLOG_WARN_RL(&rl, "error reading netlink socket: %s", - strerror(error)); + ovs_strerror(error)); } nln_report(nln, NULL); } @@ -171,7 +194,7 @@ nln_notifier_run(struct nln *nln) /* Causes poll_block() to wake up when change notifications are ready. */ void -nln_notifier_wait(struct nln *nln) +nln_wait(struct nln *nln) { nln->has_run = false; if (nln->notify_sock) {