netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / rtnetlink.c
index 1d302ea..d0c1ee7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 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 <sys/socket.h>
 #include <linux/rtnetlink.h>
 #include <net/if.h>
-#include <poll.h>
 
-#include "coverage.h"
 #include "netlink.h"
+#include "netlink-notifier.h"
 #include "ofpbuf.h"
 
-#define THIS_MODULE VLM_rtnetlink
-#include "vlog.h"
+static struct nln *nln = NULL;
+static struct rtnetlink_change rtn_change;
 
-/* rtnetlink socket. */
-static struct nl_sock *notify_sock;
-
-/* All registered notifiers. */
-static struct list all_notifiers = LIST_INITIALIZER(&all_notifiers);
-
-static void rtnetlink_report_change(const struct nlmsghdr *,
-                                    const struct ifinfomsg *,
-                                    struct nlattr *attrs[]);
-static void rtnetlink_report_notify_error(void);
-
-/* 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 that you want.  You should probably be
- * using dpif_port_poll() or netdev_monitor_create(), which unlike this
- * function are not Linux-specific.
- *
- * Returns 0 if successful, otherwise a positive errno value. */
-int
-rtnetlink_notifier_register(struct rtnetlink_notifier *notifier,
-                            rtnetlink_notify_func *cb, void *aux)
+/* Returns true if the given netlink msg type corresponds to RTNLGRP_LINK. */
+bool
+rtnetlink_type_is_rtnlgrp_link(uint16_t type)
 {
-    if (!notify_sock) {
-        int error = nl_sock_create(NETLINK_ROUTE, RTNLGRP_LINK, 0, 0,
-                                   &notify_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. */
-        rtnetlink_notifier_run();
-    }
-
-    list_push_back(&all_notifiers, &notifier->node);
-    notifier->cb = cb;
-    notifier->aux = aux;
-    return 0;
+    return type == RTM_NEWLINK || type == RTM_DELLINK;
 }
 
-/* Cancels notification on 'notifier', which must have previously been
- * registered with rtnetlink_notifier_register(). */
-void
-rtnetlink_notifier_unregister(struct rtnetlink_notifier *notifier)
+/* 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)
 {
-    list_remove(&notifier->node);
-    if (list_is_empty(&all_notifiers)) {
-        nl_sock_destroy(notify_sock);
-        notify_sock = NULL;
-    }
+    return type == RTM_NEWADDR || type == RTM_DELADDR;
 }
 
-/* Calls all of the registered notifiers, passing along any as-yet-unreported
- * netdev change events. */
-void
-rtnetlink_notifier_run(void)
+/* 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)
 {
-    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
-    if (!notify_sock) {
-        return;
-    }
+    const struct nlmsghdr *nlmsg = buf->data;
+    bool parsed = false;
 
-    for (;;) {
+    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 rtnetlink_policy[] = {
+        static const struct nl_policy policy[] = {
             [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
-            [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
+            [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(rtnetlink_policy)];
-        struct ofpbuf *buf;
-        int error;
+        struct nlattr *attrs[ARRAY_SIZE(policy)];
 
-        error = nl_sock_recv(notify_sock, &buf, false);
-        if (!error) {
-            if (nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
-                                rtnetlink_policy,
-                                attrs, ARRAY_SIZE(rtnetlink_policy))) {
-                struct ifinfomsg *ifinfo;
+        parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifinfomsg),
+                                 policy, attrs, ARRAY_SIZE(policy));
 
-                ifinfo = (void *) ((char *) buf->data + NLMSG_HDRLEN);
-                rtnetlink_report_change(buf->data, ifinfo, attrs);
-            } else {
-                VLOG_WARN_RL(&rl, "received bad rtnl message");
-                rtnetlink_report_notify_error();
-            }
-            ofpbuf_delete(buf);
-        } else if (error == EAGAIN) {
-            return;
-        } else {
-            if (error == ENOBUFS) {
-                VLOG_WARN_RL(&rl, "rtnetlink receive buffer overflowed");
+        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 {
-                VLOG_WARN_RL(&rl, "error reading rtnetlink socket: %s",
-                             strerror(error));
+                memset(&change->mac, 0, ETH_ADDR_LEN);
             }
-            rtnetlink_report_notify_error();
+        }
+    } 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 nlattr *attrs[ARRAY_SIZE(policy)];
+
+        parsed = nl_policy_parse(buf, NLMSG_HDRLEN + sizeof(struct ifaddrmsg),
+                                 policy, attrs, ARRAY_SIZE(policy));
+
+        if (parsed) {
+            const struct ifaddrmsg *ifaddr;
+
+            ifaddr = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifaddr);
+
+            change->nlmsg_type     = nlmsg->nlmsg_type;
+            change->if_index       = ifaddr->ifa_index;
+            change->ifname         = nl_attr_get_string(attrs[IFA_LABEL]);
         }
     }
+
+    return parsed;
 }
 
-/* Causes poll_block() to wake up when network device change notifications are
- * ready. */
-void
-rtnetlink_notifier_wait(void)
+static bool
+rtnetlink_parse_cb(struct ofpbuf *buf, void *change)
 {
-    if (notify_sock) {
-        nl_sock_wait(notify_sock, POLLIN);
-    }
+    return rtnetlink_parse(buf, change);
 }
 
-static void
-rtnetlink_report_change(const struct nlmsghdr *nlmsg,
-                           const struct ifinfomsg *ifinfo,
-                           struct nlattr *attrs[])
+/* 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 that you want.  You should probably be
+ * using dpif_port_poll() or netdev_change_seq(), which unlike this function
+ * are not Linux-specific.
+ *
+ * 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)
 {
-    struct rtnetlink_notifier *notifier;
-    struct rtnetlink_change change;
+    if (!nln) {
+        nln = nln_create(NETLINK_ROUTE, RTNLGRP_LINK, rtnetlink_parse_cb,
+                         &rtn_change);
+    }
 
-    COVERAGE_INC(rtnetlink_changed);
+    return nln_notifier_create(nln, (nln_notify_func *) cb, aux);
+}
 
-    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);
+/* Destroys 'notifier', which must have previously been created with
+ * rtnetlink_notifier_register(). */
+void
+rtnetlink_notifier_destroy(struct nln_notifier *notifier)
+{
+    nln_notifier_destroy(notifier);
+}
 
-    LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
-                   &all_notifiers) {
-        notifier->cb(&change, notifier->aux);
+/* Calls all of the registered notifiers, passing along any as-yet-unreported
+ * netdev change events. */
+void
+rtnetlink_run(void)
+{
+    if (nln) {
+        nln_run(nln);
     }
 }
 
-static void
-rtnetlink_report_notify_error(void)
+/* Causes poll_block() to wake up when network device change notifications are
+ * ready. */
+void
+rtnetlink_wait(void)
 {
-    struct rtnetlink_notifier *notifier;
-
-    LIST_FOR_EACH (notifier, struct rtnetlink_notifier, node,
-                   &all_notifiers) {
-        notifier->cb(NULL, notifier->aux);
+    if (nln) {
+        nln_wait(nln);
     }
 }