batman-adv: make GW election code protocol specific
[cascardo/linux.git] / net / batman-adv / gateway_client.c
index 63a805d..ec363f3 100644 (file)
@@ -80,12 +80,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 +164,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 +207,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 +287,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)
-               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)
+       if (!bat_priv->algo_ops->gw.is_eligible(bat_priv, curr_gw_orig,
+                                               orig_node))
                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);
 }
 
 /**
@@ -585,80 +473,31 @@ 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)
-{
-       struct batadv_gw_node *curr_gw;
-       struct batadv_neigh_node *router;
-       struct batadv_neigh_ifinfo *router_ifinfo = NULL;
-       int ret = -1;
-
-       router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
-       if (!router)
-               goto out;
-
-       router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
-       if (!router_ifinfo)
-               goto out;
-
-       curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
-
-       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 (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;
-}
-
 int batadv_gw_client_seq_print_text(struct seq_file *seq, void *offset)
 {
        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)
-               goto out;
+               return 0;
 
-       seq_printf(seq,
-                  "      Gateway      (#/255)           Nexthop [outgoingIF]: advertised uplink bandwidth ... [B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s)]\n",
+       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);
+                  primary_if->net_dev->dev_addr, net_dev->name,
+                  bat_priv->algo_ops->name);
 
-       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;
+       batadv_hardif_put(primary_if);
 
-               gw_count++;
+       if (!bat_priv->algo_ops->gw.print) {
+               seq_puts(seq,
+                        "No printing function for this routing protocol\n");
+               return 0;
        }
-       rcu_read_unlock();
 
-       if (gw_count == 0)
-               seq_puts(seq, "No gateways in range ...\n");
+       bat_priv->algo_ops->gw.print(bat_priv, seq);
 
-out:
-       if (primary_if)
-               batadv_hardif_put(primary_if);
        return 0;
 }