/*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2013, 2015 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "rtnetlink.h"
-#include <errno.h>
-#include <poll.h>
-#include <stdlib.h>
+#include <sys/socket.h>
+#include <linux/rtnetlink.h>
+#include <net/if.h>
-#include "coverage.h"
#include "netlink.h"
-#include "netlink-socket.h"
+#include "netlink-notifier.h"
#include "ofpbuf.h"
-#include "vlog.h"
-VLOG_DEFINE_THIS_MODULE(rtnetlink);
+static struct nln *nln = NULL;
+static struct rtnetlink_change rtn_change;
-COVERAGE_DEFINE(rtnetlink_changed);
+/* Returns true if the given netlink msg type corresponds to RTNLGRP_LINK. */
+bool
+rtnetlink_type_is_rtnlgrp_link(uint16_t type)
+{
+ return type == RTM_NEWLINK || type == RTM_DELLINK;
+}
+
+/* Returns true if the given netlink msg type corresponds to
+ * RTNLGRP_IPV4_IFADDR or RTNLGRP_IPV6_IFADDR. */
+bool
+rtnetlink_type_is_rtnlgrp_addr(uint16_t type)
+{
+ return type == RTM_NEWADDR || type == RTM_DELADDR;
+}
-static void rtnetlink_report(struct rtnetlink *rtn, void *change);
+/* Parses a rtnetlink message 'buf' into 'change'. If 'buf' is unparseable,
+ * leaves 'change' untouched and returns false. Otherwise, populates 'change'
+ * and returns true. */
+bool
+rtnetlink_parse(struct ofpbuf *buf, struct rtnetlink_change *change)
+{
+ const struct nlmsghdr *nlmsg = buf->data;
+ bool parsed = false;
+
+ if (rtnetlink_type_is_rtnlgrp_link(nlmsg->nlmsg_type)) {
+ /* Policy for RTNLGRP_LINK messages.
+ *
+ * There are *many* more fields in these messages, but currently we
+ * only care about these fields. */
+ static const struct nl_policy policy[] = {
+ [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
+ [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
+ [IFLA_MTU] = { .type = NL_A_U32, .optional = true },
+ [IFLA_ADDRESS] = { .type = NL_A_UNSPEC, .optional = true },
+ };
+
+ struct nlattr *attrs[ARRAY_SIZE(policy)];
+
+ parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
+ policy, attrs, ARRAY_SIZE(policy));
+
+ if (parsed) {
+ const struct ifinfomsg *ifinfo;
+
+ ifinfo = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifinfo);
+
+ change->nlmsg_type = nlmsg->nlmsg_type;
+ change->if_index = ifinfo->ifi_index;
+ change->ifname = nl_attr_get_string(attrs[IFLA_IFNAME]);
+ change->ifi_flags = ifinfo->ifi_flags;
+ change->master_ifindex = (attrs[IFLA_MASTER]
+ ? nl_attr_get_u32(attrs[IFLA_MASTER])
+ : 0);
+ change->mtu = (attrs[IFLA_MTU]
+ ? nl_attr_get_u32(attrs[IFLA_MTU])
+ : 0);
+
+ if (attrs[IFLA_ADDRESS] &&
+ nl_attr_get_size(attrs[IFLA_ADDRESS]) == ETH_ADDR_LEN) {
+ memcpy(&change->mac, nl_attr_get(attrs[IFLA_ADDRESS]),
+ ETH_ADDR_LEN);
+ } else {
+ memset(&change->mac, 0, ETH_ADDR_LEN);
+ }
+ }
+ } else if (rtnetlink_type_is_rtnlgrp_addr(nlmsg->nlmsg_type)) {
+ /* Policy for RTNLGRP_IPV4_IFADDR/RTNLGRP_IPV6_IFADDR messages.
+ *
+ * There are *many* more fields in these messages, but currently we
+ * only care about these fields. */
+ static const struct nl_policy policy[] = {
+ [IFA_LABEL] = { .type = NL_A_STRING, .optional = false },
+ };
-struct rtnetlink {
- struct nl_sock *notify_sock; /* Rtnetlink socket. */
- struct list all_notifiers; /* All rtnetlink notifiers. */
- bool has_run; /* Guard for run and wait functions. */
+ struct nlattr *attrs[ARRAY_SIZE(policy)];
- /* Passed in by rtnetlink_create(). */
- int multicast_group; /* Multicast group we listen on. */
- rtnetlink_parse_func *parse; /* Message parsing function. */
- void *change; /* Change passed to parse. */
-};
+ parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifaddrmsg),
+ policy, attrs, ARRAY_SIZE(policy));
-/* Creates an rtnetlink handle which may be used to manage change
- * notifications. The created handle will listen for rtnetlink messages on
- * 'multicast_group'. Incoming messages will be parsed with 'parse' which will
- * be passed 'change' as an argument. */
-struct rtnetlink *
-rtnetlink_create(int multicast_group, rtnetlink_parse_func *parse,
- void *change)
-{
- struct rtnetlink *rtn;
+ if (parsed) {
+ const struct ifaddrmsg *ifaddr;
- rtn = xzalloc(sizeof *rtn);
- rtn->notify_sock = NULL;
- rtn->multicast_group = multicast_group;
- rtn->parse = parse;
- rtn->change = change;
- rtn->has_run = false;
+ ifaddr = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifaddr);
- list_init(&rtn->all_notifiers);
- return rtn;
+ change->nlmsg_type = nlmsg->nlmsg_type;
+ change->if_index = ifaddr->ifa_index;
+ change->ifname = nl_attr_get_string(attrs[IFA_LABEL]);
+ }
+ }
+
+ return parsed;
}
-/* Destroys 'rtn' by freeing any memory it has reserved and closing any sockets
- * it has opened. */
-void
-rtnetlink_destroy(struct rtnetlink *rtn)
+static bool
+rtnetlink_parse_cb(struct ofpbuf *buf, void *change)
{
- if (rtn) {
- nl_sock_destroy(rtn->notify_sock);
- free(rtn);
- }
+ return rtnetlink_parse(buf, change);
}
-/* Registers 'cb' to be called with auxiliary data 'aux' with change
- * notifications. The notifier is stored in 'notifier', which the caller must
- * not modify or free.
+/* Registers 'cb' to be called with auxiliary data 'aux' with network device
+ * change notifications. The notifier is stored in 'notifier', which the
+ * caller must not modify or free.
*
- * This is probably not the function you want. You should probably be using
- * message specific notifiers like rtnetlink_link_notifier_register().
+ * This is probably not the function that you want. You should probably be
+ * using dpif_port_poll() or netdev_change_seq(), which unlike this function
+ * are not Linux-specific.
*
- * Returns 0 if successful, otherwise a positive errno value. */
-int
-rtnetlink_notifier_register(struct rtnetlink *rtn,
- struct rtnetlink_notifier *notifier,
- rtnetlink_notify_func *cb, void *aux)
+ * xxx Joins more multicast groups when needed.
+ *
+ * Returns an initialized nln_notifier if successful, NULL otherwise. */
+struct nln_notifier *
+rtnetlink_notifier_create(rtnetlink_notify_func *cb, void *aux)
{
- if (!rtn->notify_sock) {
- struct nl_sock *sock;
- int error;
-
- error = nl_sock_create(NETLINK_ROUTE, &sock);
- if (!error) {
- error = nl_sock_join_mcgroup(sock, rtn->multicast_group);
- }
- if (error) {
- nl_sock_destroy(sock);
- VLOG_WARN("could not create rtnetlink socket: %s",
- strerror(error));
- return error;
- }
- rtn->notify_sock = sock;
- } else {
- /* Catch up on notification work so that the new notifier won't
- * receive any stale notifications. */
- rtnetlink_notifier_run(rtn);
+ if (!nln) {
+ nln = nln_create(NETLINK_ROUTE, RTNLGRP_LINK, rtnetlink_parse_cb,
+ &rtn_change);
}
- list_push_back(&rtn->all_notifiers, ¬ifier->node);
- notifier->cb = cb;
- notifier->aux = aux;
- return 0;
+ return nln_notifier_create(nln, (nln_notify_func *) cb, aux);
}
-/* Cancels notification on 'notifier', which must have previously been
- * registered with rtnetlink_notifier_register(). */
+/* Destroys 'notifier', which must have previously been created with
+ * rtnetlink_notifier_register(). */
void
-rtnetlink_notifier_unregister(struct rtnetlink *rtn,
- struct rtnetlink_notifier *notifier)
+rtnetlink_notifier_destroy(struct nln_notifier *notifier)
{
- list_remove(¬ifier->node);
- if (list_is_empty(&rtn->all_notifiers)) {
- nl_sock_destroy(rtn->notify_sock);
- rtn->notify_sock = NULL;
- }
+ nln_notifier_destroy(notifier);
}
/* Calls all of the registered notifiers, passing along any as-yet-unreported
- * change events. */
+ * netdev change events. */
void
-rtnetlink_notifier_run(struct rtnetlink *rtn)
+rtnetlink_run(void)
{
- static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
- if (!rtn->notify_sock || rtn->has_run) {
- return;
- }
-
- rtn->has_run = true;
- for (;;) {
- struct ofpbuf *buf;
- int error;
-
- error = nl_sock_recv(rtn->notify_sock, &buf, false);
- if (!error) {
- if (rtn->parse(buf, rtn->change)) {
- rtnetlink_report(rtn, rtn->change);
- } else {
- VLOG_WARN_RL(&rl, "received bad rtnl message");
- rtnetlink_report(rtn, NULL);
- }
- 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));
- }
- rtnetlink_report(rtn, NULL);
- }
+ if (nln) {
+ nln_run(nln);
}
}
-/* Causes poll_block() to wake up when change notifications are ready. */
+/* Causes poll_block() to wake up when network device change notifications are
+ * ready. */
void
-rtnetlink_notifier_wait(struct rtnetlink *rtn)
+rtnetlink_wait(void)
{
- rtn->has_run = false;
- if (rtn->notify_sock) {
- nl_sock_wait(rtn->notify_sock, POLLIN);
+ if (nln) {
+ nln_wait(nln);
}
}
-
-static void
-rtnetlink_report(struct rtnetlink *rtn, void *change)
-{
- struct rtnetlink_notifier *notifier;
-
- if (change) {
- COVERAGE_INC(rtnetlink_changed);
- }
-
- LIST_FOR_EACH (notifier, node, &rtn->all_notifiers) {
- notifier->cb(change, notifier->aux);
- }
-}
-