datapath: Remove WARN_ON_ONCE(1) now that this code has been exercised.
[openvswitch] / lib / netdev-linux.c
index 6189bf79065a834ef0ed382671fafcc1996a4776..3e34044476be996653c16ceec841c0fc70dae9b9 100644 (file)
@@ -15,9 +15,6 @@
  */
 
 #include <config.h>
-
-#include "netdev-linux.h"
-
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
@@ -53,6 +50,7 @@
 #include "openflow/openflow.h"
 #include "packets.h"
 #include "poll-loop.h"
+#include "rtnetlink.h"
 #include "socket-util.h"
 #include "shash.h"
 #include "svec.h"
@@ -105,18 +103,7 @@ struct netdev_linux_cache {
 };
 
 static struct shash cache_map = SHASH_INITIALIZER(&cache_map);
-static struct linux_netdev_notifier netdev_linux_cache_notifier;
-
-/* Policy for RTNLGRP_LINK messages.
- *
- * There are *many* more fields in these messages, but currently we only care
- * about interface names. */
-static const struct nl_policy rtnlgrp_link_policy[] = {
-    [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
-    [IFLA_MASTER] = { .type = NL_A_U32, .optional = true },
-    [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
-                     .min_len = sizeof(struct rtnl_link_stats) },
-};
+static struct rtnetlink_notifier netdev_linux_cache_notifier;
 
 /* An AF_INET socket (used for ioctl operations). */
 static int af_inet_sock = -1;
@@ -128,7 +115,7 @@ struct netdev_linux_notifier {
 
 static struct shash netdev_linux_notifiers =
     SHASH_INITIALIZER(&netdev_linux_notifiers);
-static struct linux_netdev_notifier netdev_linux_poll_notifier;
+static struct rtnetlink_notifier netdev_linux_poll_notifier;
 
 /* This is set pretty low because we probably won't learn anything from the
  * additional log messages. */
@@ -175,17 +162,17 @@ netdev_linux_init(void)
 static void
 netdev_linux_run(void)
 {
-    linux_netdev_notifier_run();
+    rtnetlink_notifier_run();
 }
 
 static void
 netdev_linux_wait(void)
 {
-    linux_netdev_notifier_wait();
+    rtnetlink_notifier_wait();
 }
 
 static void
-netdev_linux_cache_cb(const struct linux_netdev_change *change,
+netdev_linux_cache_cb(const struct rtnetlink_change *change,
                       void *aux UNUSED)
 {
     struct netdev_linux_cache *cache;
@@ -219,7 +206,7 @@ netdev_linux_open(const char *name, char *suffix, int ethertype,
     netdev->cache = shash_find_data(&cache_map, suffix);
     if (!netdev->cache) {
         if (shash_is_empty(&cache_map)) {
-            int error = linux_netdev_notifier_register(
+            int error = rtnetlink_notifier_register(
                 &netdev_linux_cache_notifier, netdev_linux_cache_cb, NULL);
             if (error) {
                 netdev_close(&netdev->netdev);
@@ -336,7 +323,7 @@ netdev_linux_close(struct netdev *netdev_)
         free(netdev->cache);
 
         if (shash_is_empty(&cache_map)) {
-            linux_netdev_notifier_unregister(&netdev_linux_cache_notifier);
+            rtnetlink_notifier_unregister(&netdev_linux_cache_notifier);
         }
     }
     if (netdev->netdev_fd >= 0) {
@@ -1175,7 +1162,7 @@ poll_notify(struct list *list)
 }
 
 static void
-netdev_linux_poll_cb(const struct linux_netdev_change *change,
+netdev_linux_poll_cb(const struct rtnetlink_change *change,
                      void *aux UNUSED)
 {
     if (change) {
@@ -1202,7 +1189,7 @@ netdev_linux_poll_add(struct netdev *netdev,
     struct list *list;
 
     if (shash_is_empty(&netdev_linux_notifiers)) {
-        int error = linux_netdev_notifier_register(&netdev_linux_poll_notifier,
+        int error = rtnetlink_notifier_register(&netdev_linux_poll_notifier,
                                                    netdev_linux_poll_cb, NULL);
         if (error) {
             return error;
@@ -1243,7 +1230,7 @@ netdev_linux_poll_remove(struct netdev_notifier *notifier_)
 
     /* If that was the last notifier, unregister. */
     if (shash_is_empty(&netdev_linux_notifiers)) {
-        linux_netdev_notifier_unregister(&netdev_linux_poll_notifier);
+        rtnetlink_notifier_unregister(&netdev_linux_poll_notifier);
     }
 }
 
@@ -1336,6 +1323,17 @@ const struct netdev_class netdev_tap_class = {
 static int
 get_stats_via_netlink(int ifindex, struct netdev_stats *stats)
 {
+    /* 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 rtnlgrp_link_policy[] = {
+        [IFLA_IFNAME] = { .type = NL_A_STRING, .optional = false },
+        [IFLA_STATS] = { .type = NL_A_UNSPEC, .optional = true,
+                         .min_len = sizeof(struct rtnl_link_stats) },
+    };
+
+
     static struct nl_sock *rtnl_sock;
     struct ofpbuf request;
     struct ofpbuf *reply;
@@ -1593,131 +1591,3 @@ netdev_linux_do_ioctl(const struct netdev *netdev, struct ifreq *ifr,
     }
     return 0;
 }
-\f
-/* rtnetlink socket. */
-static struct nl_sock *notify_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(void);
-
-int
-linux_netdev_notifier_register(struct linux_netdev_notifier *notifier,
-                               linux_netdev_notify_func *cb, void *aux)
-{
-    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. */
-        linux_netdev_notifier_run();
-    }
-
-    list_push_back(&all_notifiers, &notifier->node);
-    notifier->cb = cb;
-    notifier->aux = aux;
-    return 0;
-}
-
-void
-linux_netdev_notifier_unregister(struct linux_netdev_notifier *notifier)
-{
-    list_remove(&notifier->node);
-    if (list_is_empty(&all_notifiers)) {
-        nl_sock_destroy(notify_sock);
-        notify_sock = NULL;
-    }
-}
-
-void
-linux_netdev_notifier_run(void)
-{
-    static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(1, 5);
-
-    if (!notify_sock) {
-        return;
-    }
-
-    for (;;) {
-        struct nlattr *attrs[ARRAY_SIZE(rtnlgrp_link_policy)];
-        struct ofpbuf *buf;
-        int error;
-
-        error = nl_sock_recv(notify_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();
-            }
-            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();
-        }
-    }
-}
-
-void
-linux_netdev_notifier_wait(void)
-{
-    if (notify_sock) {
-        nl_sock_wait(notify_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) {
-        notifier->cb(&change, notifier->aux);
-    }
-}
-
-static void
-linux_netdev_report_notify_error(void)
-{
-    struct linux_netdev_notifier *notifier;
-
-    LIST_FOR_EACH (notifier, struct linux_netdev_notifier, node,
-                   &all_notifiers) {
-        notifier->cb(NULL, notifier->aux);
-    }
-}