diff options
| author | Ben Pfaff <blp@nicira.com> | 2009-06-24 10:24:09 -0700 |
|---|---|---|
| committer | Ben Pfaff <blp@nicira.com> | 2009-07-06 09:07:24 -0700 |
| commit | e9e28be359f64c03944c0a282c17e714fcb0afc4 (patch) | |
| tree | c7bc0bbb944be3bcdc36402f1b55805b1c21f5f0 /lib/netdev-linux.c | |
| parent | 5792c5c64a194304b554e2c6b9a9a4c0216c08a4 (diff) | |
| download | openvswitch-e9e28be359f64c03944c0a282c17e714fcb0afc4.tar.gz | |
Introduce general-purpose ways to wait for dpif and netdev changes.
The dpif and netdev code has had various ways to check for changes to
dpifs and netdevs over the course of Open vSwitch development. All of
these have been thus far fairly specific to the Linux implementation. This
commit is the start of a more general API for watching for such changes.
The dpif-related parts seem fairly mature and so they are documented,
the netdev parts will probably need to change somewhat and so they are
not documented yet.
Diffstat (limited to 'lib/netdev-linux.c')
| -rw-r--r-- | lib/netdev-linux.c | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c new file mode 100644 index 000000000..c753e28a2 --- /dev/null +++ b/lib/netdev-linux.c @@ -0,0 +1,183 @@ +/* + * Copyright (c) 2009 Nicira Networks. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <config.h> + +#include "netdev-linux.h" + +#include <errno.h> +#include <sys/socket.h> +#include <linux/rtnetlink.h> +#include <poll.h> + +#include "coverage.h" +#include "netlink.h" +#include "ofpbuf.h" + +#define THIS_MODULE VLM_netdev_linux +#include "vlog.h" + +/* rtnetlink socket. */ +static struct nl_sock *rtnl_sock; + +/* All registered notifiers. */ +static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers); + +static void linux_netdev_report_change(const struct nlmsghdr *, + const struct ifinfomsg *, + struct nlattr *attrs[]); +static void linux_netdev_report_notify_error(int error); + +int +linux_netdev_notifier_register(struct linux_netdev_notifier *notifier, + linux_netdev_notify_func *cb, void *aux) +{ + if (!rtnl_sock) { + int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0, + &rtnl_sock); + if (error) { + VLOG_WARN("could not create rtnetlink socket: %s", + strerror(error)); + return error; + } + } else { + /* Catch up on notification work so that the new notifier won't + * receive any stale notifications. */ + linux_netdev_notifier_run(); + } + + list_push_back(&all_notifiers, ¬ifier->node); + notifier->error = 0; + notifier->cb = cb; + notifier->aux = aux; + return 0; +} + +void +linux_netdev_notifier_unregister(struct linux_netdev_notifier *notifier) +{ + list_remove(¬ifier->node); + if (list_is_empty(&all_notifiers)) { + nl_sock_destroy(rtnl_sock); + rtnl_sock = NULL; + } +} + +int +linux_netdev_notifier_get_error(struct linux_netdev_notifier *notifier) +{ + int error = notifier->error; + notifier->error = 0; + return error; +} + +int +linux_netdev_notifier_peek_error(const struct linux_netdev_notifier *notifier) +{ + return notifier->error; +} + +static const struct nl_policy rtnlgrp_link_policy[] = { + [IFLA_IFNAME] = { .type = NL_A_STRING }, + [IFLA_MASTER] = { .type = NL_A_U32, .optional = true }, +}; + +void +linux_netdev_notifier_run(void) +{ + static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5); + + if (!rtnl_sock) { + return; + } + + for (;;) { + struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)]; + struct ofpbuf *buf; + int error; + + error = nl_sock_recv(rtnl_sock, &buf, false); + if (!error) { + if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg), + rtnlgrp_link_policy, + attrs, ARRAY_SIZE(rtnlgrp_link_policy))) { + struct ifinfomsg *ifinfo; + + ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN); + linux_netdev_report_change(buf->data, ifinfo, attrs); + } else { + VLOG_WARN_RL(&rl, "received bad rtnl message"); + linux_netdev_report_notify_error(ENOBUFS); + } + ofpbuf_delete(buf); + } else if (error == EAGAIN) { + return; + } else { + if (error == ENOBUFS) { + VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed"); + } else { + VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s", + strerror(error)); + } + linux_netdev_report_notify_error(error); + } + } +} + +void +linux_netdev_notifier_wait(void) +{ + if (rtnl_sock) { + nl_sock_wait(rtnl_sock, POLLIN); + } +} + +static void +linux_netdev_report_change(const struct nlmsghdr *nlmsg, + const struct ifinfomsg *ifinfo, + struct nlattr *attrs[]) +{ + struct linux_netdev_notifier *notifier; + struct linux_netdev_change change; + + COVERAGE_INC(linux_netdev_changed); + + change.nlmsg_type = nlmsg->nlmsg_type; + change.ifi_index = ifinfo->ifi_index; + change.ifname = nl_attr_get_string(attrs[IFLA_IFNAME]); + change.master_ifindex = (attrs[IFLA_MASTER] + ? nl_attr_get_u32(attrs[IFLA_MASTER]) : 0); + + LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node, + &all_notifiers) { + if (!notifier->error) { + notifier->cb(&change, notifier->aux); + } + } +} + +static void +linux_netdev_report_notify_error(int error) +{ + struct linux_netdev_notifier *notifier; + + LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node, + &all_notifiers) { + if (error != ENOBUFS || !notifier->error) { + notifier->error = error; + } + } +} |
