Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[cascardo/linux.git] / net / batman-adv / gateway_client.c
index 63a805d..de055d6 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <linux/atomic.h>
 #include <linux/byteorder/generic.h>
+#include <linux/errno.h>
 #include <linux/etherdevice.h>
 #include <linux/fs.h>
 #include <linux/if_ether.h>
@@ -31,6 +32,7 @@
 #include <linux/kref.h>
 #include <linux/list.h>
 #include <linux/netdevice.h>
+#include <linux/netlink.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
 #include <linux/seq_file.h>
 #include <linux/spinlock.h>
 #include <linux/stddef.h>
 #include <linux/udp.h>
+#include <net/sock.h>
+#include <uapi/linux/batman_adv.h>
 
 #include "gateway_common.h"
 #include "hard-interface.h"
 #include "log.h"
+#include "netlink.h"
 #include "originator.h"
 #include "packet.h"
 #include "routing.h"
+#include "soft-interface.h"
 #include "sysfs.h"
 #include "translation-table.h"
 
@@ -80,12 +86,12 @@ static void batadv_gw_node_release(struct kref *ref)
  * batadv_gw_node_put - decrement the gw_node refcounter and possibly release it
  * @gw_node: gateway node to free
  */
-static void batadv_gw_node_put(struct batadv_gw_node *gw_node)
+void batadv_gw_node_put(struct batadv_gw_node *gw_node)
 {
        kref_put(&gw_node->refcount, batadv_gw_node_release);
 }
 
-static struct batadv_gw_node *
+struct batadv_gw_node *
 batadv_gw_get_selected_gw_node(struct batadv_priv *bat_priv)
 {
        struct batadv_gw_node *gw_node;
@@ -164,86 +170,6 @@ void batadv_gw_reselect(struct batadv_priv *bat_priv)
        atomic_set(&bat_priv->gw.reselect, 1);
 }
 
-static struct batadv_gw_node *
-batadv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
-{
-       struct batadv_neigh_node *router;
-       struct batadv_neigh_ifinfo *router_ifinfo;
-       struct batadv_gw_node *gw_node, *curr_gw = NULL;
-       u64 max_gw_factor = 0;
-       u64 tmp_gw_factor = 0;
-       u8 max_tq = 0;
-       u8 tq_avg;
-       struct batadv_orig_node *orig_node;
-
-       rcu_read_lock();
-       hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
-               orig_node = gw_node->orig_node;
-               router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
-               if (!router)
-                       continue;
-
-               router_ifinfo = batadv_neigh_ifinfo_get(router,
-                                                       BATADV_IF_DEFAULT);
-               if (!router_ifinfo)
-                       goto next;
-
-               if (!kref_get_unless_zero(&gw_node->refcount))
-                       goto next;
-
-               tq_avg = router_ifinfo->bat_iv.tq_avg;
-
-               switch (atomic_read(&bat_priv->gw.sel_class)) {
-               case 1: /* fast connection */
-                       tmp_gw_factor = tq_avg * tq_avg;
-                       tmp_gw_factor *= gw_node->bandwidth_down;
-                       tmp_gw_factor *= 100 * 100;
-                       tmp_gw_factor >>= 18;
-
-                       if ((tmp_gw_factor > max_gw_factor) ||
-                           ((tmp_gw_factor == max_gw_factor) &&
-                            (tq_avg > max_tq))) {
-                               if (curr_gw)
-                                       batadv_gw_node_put(curr_gw);
-                               curr_gw = gw_node;
-                               kref_get(&curr_gw->refcount);
-                       }
-                       break;
-
-               default: /* 2:  stable connection (use best statistic)
-                         * 3:  fast-switch (use best statistic but change as
-                         *     soon as a better gateway appears)
-                         * XX: late-switch (use best statistic but change as
-                         *     soon as a better gateway appears which has
-                         *     $routing_class more tq points)
-                         */
-                       if (tq_avg > max_tq) {
-                               if (curr_gw)
-                                       batadv_gw_node_put(curr_gw);
-                               curr_gw = gw_node;
-                               kref_get(&curr_gw->refcount);
-                       }
-                       break;
-               }
-
-               if (tq_avg > max_tq)
-                       max_tq = tq_avg;
-
-               if (tmp_gw_factor > max_gw_factor)
-                       max_gw_factor = tmp_gw_factor;
-
-               batadv_gw_node_put(gw_node);
-
-next:
-               batadv_neigh_node_put(router);
-               if (router_ifinfo)
-                       batadv_neigh_ifinfo_put(router_ifinfo);
-       }
-       rcu_read_unlock();
-
-       return curr_gw;
-}
-
 /**
  * batadv_gw_check_client_stop - check if client mode has been switched off
  * @bat_priv: the bat priv with all the soft interface information
@@ -287,12 +213,19 @@ void batadv_gw_election(struct batadv_priv *bat_priv)
        if (atomic_read(&bat_priv->gw.mode) != BATADV_GW_MODE_CLIENT)
                goto out;
 
+       if (!bat_priv->algo_ops->gw.get_best_gw_node)
+               goto out;
+
        curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
 
        if (!batadv_atomic_dec_not_zero(&bat_priv->gw.reselect) && curr_gw)
                goto out;
 
-       next_gw = batadv_gw_get_best_gw_node(bat_priv);
+       /* if gw.reselect is set to 1 it means that a previous call to
+        * gw.is_eligible() said that we have a new best GW, therefore it can
+        * now be picked from the list and selected
+        */
+       next_gw = bat_priv->algo_ops->gw.get_best_gw_node(bat_priv);
 
        if (curr_gw == next_gw)
                goto out;
@@ -360,70 +293,31 @@ out:
 void batadv_gw_check_election(struct batadv_priv *bat_priv,
                              struct batadv_orig_node *orig_node)
 {
-       struct batadv_neigh_ifinfo *router_orig_tq = NULL;
-       struct batadv_neigh_ifinfo *router_gw_tq = NULL;
        struct batadv_orig_node *curr_gw_orig;
-       struct batadv_neigh_node *router_gw = NULL;
-       struct batadv_neigh_node *router_orig = NULL;
-       u8 gw_tq_avg, orig_tq_avg;
+
+       /* abort immediately if the routing algorithm does not support gateway
+        * election
+        */
+       if (!bat_priv->algo_ops->gw.is_eligible)
+               return;
 
        curr_gw_orig = batadv_gw_get_selected_orig(bat_priv);
        if (!curr_gw_orig)
                goto reselect;
 
-       router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
-       if (!router_gw)
-               goto reselect;
-
-       router_gw_tq = batadv_neigh_ifinfo_get(router_gw,
-                                              BATADV_IF_DEFAULT);
-       if (!router_gw_tq)
-               goto reselect;
-
        /* this node already is the gateway */
        if (curr_gw_orig == orig_node)
                goto out;
 
-       router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
-       if (!router_orig)
+       if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
+                                               orig_node))
                goto out;
 
-       router_orig_tq = batadv_neigh_ifinfo_get(router_orig,
-                                                BATADV_IF_DEFAULT);
-       if (!router_orig_tq)
-               goto out;
-
-       gw_tq_avg = router_gw_tq->bat_iv.tq_avg;
-       orig_tq_avg = router_orig_tq->bat_iv.tq_avg;
-
-       /* the TQ value has to be better */
-       if (orig_tq_avg < gw_tq_avg)
-               goto out;
-
-       /* if the routing class is greater than 3 the value tells us how much
-        * greater the TQ value of the new gateway must be
-        */
-       if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
-           (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
-               goto out;
-
-       batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
-                  "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
-                  gw_tq_avg, orig_tq_avg);
-
 reselect:
        batadv_gw_reselect(bat_priv);
 out:
        if (curr_gw_orig)
                batadv_orig_node_put(curr_gw_orig);
-       if (router_gw)
-               batadv_neigh_node_put(router_gw);
-       if (router_orig)
-               batadv_neigh_node_put(router_orig);
-       if (router_gw_tq)
-               batadv_neigh_ifinfo_put(router_gw_tq);
-       if (router_orig_tq)
-               batadv_neigh_ifinfo_put(router_orig_tq);
 }
 
 /**
@@ -445,14 +339,15 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
        if (!gw_node)
                return;
 
-       kref_get(&orig_node->refcount);
+       kref_init(&gw_node->refcount);
        INIT_HLIST_NODE(&gw_node->list);
+       kref_get(&orig_node->refcount);
        gw_node->orig_node = orig_node;
        gw_node->bandwidth_down = ntohl(gateway->bandwidth_down);
        gw_node->bandwidth_up = ntohl(gateway->bandwidth_up);
-       kref_init(&gw_node->refcount);
 
        spin_lock_bh(&bat_priv->gw.list_lock);
+       kref_get(&gw_node->refcount);
        hlist_add_head_rcu(&gw_node->list, &bat_priv->gw.list);
        spin_unlock_bh(&bat_priv->gw.list_lock);
 
@@ -463,6 +358,9 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
                   ntohl(gateway->bandwidth_down) % 10,
                   ntohl(gateway->bandwidth_up) / 10,
                   ntohl(gateway->bandwidth_up) % 10);
+
+       /* don't return reference to new gw_node */
+       batadv_gw_node_put(gw_node);
 }
 
 /**
@@ -472,9 +370,8 @@ static void batadv_gw_node_add(struct batadv_priv *bat_priv,
  *
  * Return: gateway node if found or NULL otherwise.
  */
-static struct batadv_gw_node *
-batadv_gw_node_get(struct batadv_priv *bat_priv,
-                  struct batadv_orig_node *orig_node)
+struct batadv_gw_node *batadv_gw_node_get(struct batadv_priv *bat_priv,
+                                         struct batadv_orig_node *orig_node)
 {
        struct batadv_gw_node *gw_node_tmp, *gw_node = NULL;
 
@@ -585,81 +482,87 @@ void batadv_gw_node_free(struct batadv_priv *bat_priv)
        spin_unlock_bh(&bat_priv->gw.list_lock);
 }
 
-/* fails if orig_node has no router */
-static int batadv_write_buffer_text(struct batadv_priv *bat_priv,
-                                   struct seq_file *seq,
-                                   const struct batadv_gw_node *gw_node)
+#ifdef CONFIG_BATMAN_ADV_DEBUGFS
+int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
 {
-       struct batadv_gw_node *curr_gw;
-       struct batadv_neigh_node *router;
-       struct batadv_neigh_ifinfo *router_ifinfo = NULL;
-       int ret = -1;
+       struct net_device *net_dev = (struct net_device *)seq->private;
+       struct batadv_priv *bat_priv = netdev_priv(net_dev);
+       struct batadv_hard_iface *primary_if;
 
-       router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
-       if (!router)
-               goto out;
+       primary_if = batadv_seq_print_text_primary_if_get(seq);
+       if (!primary_if)
+               return 0;
 
-       router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
-       if (!router_ifinfo)
-               goto out;
+       seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
+                  BATADV_SOURCE_VERSION, primary_if->net_dev->name,
+                  primary_if->net_dev->dev_addr, net_dev->name,
+                  bat_priv->algo_ops->name);
 
-       curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
+       batadv_hardif_put(primary_if);
 
-       seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
-                  (curr_gw == gw_node ? "=>" : "  "),
-                  gw_node->orig_node->orig,
-                  router_ifinfo->bat_iv.tq_avg, router->addr,
-                  router->if_incoming->net_dev->name,
-                  gw_node->bandwidth_down / 10,
-                  gw_node->bandwidth_down % 10,
-                  gw_node->bandwidth_up / 10,
-                  gw_node->bandwidth_up % 10);
-       ret = seq_has_overflowed(seq) ? -1 : 0;
+       if (!bat_priv->algo_ops->gw.print) {
+               seq_puts(seq,
+                        "No printing function for this routing protocol\n");
+               return 0;
+       }
 
-       if (curr_gw)
-               batadv_gw_node_put(curr_gw);
-out:
-       if (router_ifinfo)
-               batadv_neigh_ifinfo_put(router_ifinfo);
-       if (router)
-               batadv_neigh_node_put(router);
-       return ret;
+       bat_priv->algo_ops->gw.print(bat_priv, seq);
+
+       return 0;
 }
+#endif
 
-int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
+/**
+ * batadv_gw_dump - Dump gateways into a message
+ * @msg: Netlink message to dump into
+ * @cb: Control block containing additional options
+ *
+ * Return: Error code, or length of message
+ */
+int batadv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb)
 {
-       struct net_device *net_dev = (struct net_device *)seq->private;
-       struct batadv_priv *bat_priv = netdev_priv(net_dev);
-       struct batadv_hard_iface *primary_if;
-       struct batadv_gw_node *gw_node;
-       int gw_count = 0;
-
-       primary_if = batadv_seq_print_text_primary_if_get(seq);
-       if (!primary_if)
+       struct batadv_hard_iface *primary_if = NULL;
+       struct net *net = sock_net(cb->skb->sk);
+       struct net_device *soft_iface;
+       struct batadv_priv *bat_priv;
+       int ifindex;
+       int ret;
+
+       ifindex = batadv_netlink_get_ifindex(cb->nlh,
+                                            BATADV_ATTR_MESH_IFINDEX);
+       if (!ifindex)
+               return -EINVAL;
+
+       soft_iface = dev_get_by_index(net, ifindex);
+       if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
+               ret = -ENODEV;
                goto out;
+       }
 
-       seq_printf(seq,
-                  "      Gateway      (#/255)           Nexthop [outgoingIF]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
-                  BATADV_SOURCE_VERSION, primary_if->net_dev->name,
-                  primary_if->net_dev->dev_addr, net_dev->name);
+       bat_priv = netdev_priv(soft_iface);
 
-       rcu_read_lock();
-       hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.list, list) {
-               /* fails if orig_node has no router */
-               if (batadv_write_buffer_text(bat_priv, seq, gw_node) < 0)
-                       continue;
+       primary_if = batadv_primary_if_get_selected(bat_priv);
+       if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
+               ret = -ENOENT;
+               goto out;
+       }
 
-               gw_count++;
+       if (!bat_priv->algo_ops->gw.dump) {
+               ret = -EOPNOTSUPP;
+               goto out;
        }
-       rcu_read_unlock();
 
-       if (gw_count == 0)
-               seq_puts(seq, "No gateways in range ...\n");
+       bat_priv->algo_ops->gw.dump(msg, cb, bat_priv);
+
+       ret = msg->len;
 
 out:
        if (primary_if)
                batadv_hardif_put(primary_if);
-       return 0;
+       if (soft_iface)
+               dev_put(soft_iface);
+
+       return ret;
 }
 
 /**