[v5,3/9] batman-adv: Distributed ARP Table - create DHT helper functions
Commit Message
Add all the relevant functions in order to manage a Distributed Hash Table over
the B.A.T.M.A.N.-adv network. It will later be used to store several ARP entries
and implement DAT (Distributed ARP Table)
Signed-off-by: Antonio Quartulli <ordex@autistici.org>
---
Makefile.kbuild | 1 +
distributed-arp-table.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++
distributed-arp-table.h | 55 ++++++++++++++
hard-interface.c | 5 ++
main.h | 7 ++
originator.c | 2 +
soft-interface.c | 2 +
types.h | 8 ++
8 files changed, 262 insertions(+), 0 deletions(-)
create mode 100644 distributed-arp-table.c
create mode 100644 distributed-arp-table.h
Comments
On Friday, February 10, 2012 07:41:36 Antonio Quartulli wrote:
> + for (select = 0; select < DHT_CANDIDATES_NUM; select++) {
> + max = 0;
> + max_orig_node = NULL;
> + if (!chosen_me) {
> + /* if true, wrap around the key space */
> + if (bat_priv->dht_hash > ip_key)
> + max = DAT_ADDR_MAX - bat_priv->dht_hash +
> + ip_key;
> + else
> + max = ip_key - bat_priv->dht_hash;
> + max = bat_priv->dht_hash;
Somehow this does not make sense to me. Why do we calculate a magic value for
"max" if we set it to bat_priv->dht_hash afterwards ?
Moreover, the name dht_hash is a bit confusing. At first I thought it is a
pointer to a hash (read: orig_hash/tt_local_hash/tt_global_hash/etc). Perhaps
the comment should explain what and why the magic is supposed to achieve
instead of stating what the c code is obviously doing ?
Cheers,
Marek
On Sat, Feb 11, 2012 at 09:59:57PM +0800, Marek Lindner wrote:
> On Friday, February 10, 2012 07:41:36 Antonio Quartulli wrote:
> > + for (select = 0; select < DHT_CANDIDATES_NUM; select++) {
> > + max = 0;
> > + max_orig_node = NULL;
> > + if (!chosen_me) {
> > + /* if true, wrap around the key space */
> > + if (bat_priv->dht_hash > ip_key)
> > + max = DAT_ADDR_MAX - bat_priv->dht_hash +
> > + ip_key;
> > + else
> > + max = ip_key - bat_priv->dht_hash;
> > + max = bat_priv->dht_hash;
>
> Somehow this does not make sense to me. Why do we calculate a magic value for
> "max" if we set it to bat_priv->dht_hash afterwards ?
Thank you, actually I didn't spot it in the tests because of the limited number
of nodes involved. I thought they were enough.
> Moreover, the name dht_hash is a bit confusing. At first I thought it is a
> pointer to a hash (read: orig_hash/tt_local_hash/tt_global_hash/etc).
ok, I'll rename it to dht_id (or dht_addr ?)
> Perhaps
> the comment should explain what and why the magic is supposed to achieve
> instead of stating what the c code is obviously doing ?
>
I agree :-)
Thanks!
Cheers,
On Tuesday, February 14, 2012 04:33:52 Antonio Quartulli wrote:
> > Moreover, the name dht_hash is a bit confusing. At first I thought it is
> > a pointer to a hash (read: orig_hash/tt_local_hash/tt_global_hash/etc).
>
> ok, I'll rename it to dht_id (or dht_addr ?)
It is difficult for me to suggest a better name because I don't understand
what this variable is used for.
Regards,
Marek
On Tue, Feb 14, 2012 at 02:07:57PM +0800, Marek Lindner wrote:
> On Tuesday, February 14, 2012 04:33:52 Antonio Quartulli wrote:
> > > Moreover, the name dht_hash is a bit confusing. At first I thought it is
> > > a pointer to a hash (read: orig_hash/tt_local_hash/tt_global_hash/etc).
> >
> > ok, I'll rename it to dht_id (or dht_addr ?)
>
> It is difficult for me to suggest a better name because I don't understand
> what this variable is used for.
Ops sorry! Actually this variable represent the address of the node (me or the
others, depending on the struct it belongs to - bat_priv or orig_node) in the
dht space. In the dht space every object has an id, either mesh_node and stored
keys.
I'd say that dht_addr better represent this concept.
Cheers,
@@ -24,6 +24,7 @@ batman-adv-y += bat_iv_ogm.o
batman-adv-y += bat_sysfs.o
batman-adv-y += bitarray.o
batman-adv-$(CONFIG_BATMAN_ADV_BLA) += bridge_loop_avoidance.o
+batman-adv-y += distributed-arp-table.o
batman-adv-y += gateway_client.o
batman-adv-y += gateway_common.o
batman-adv-y += hard-interface.o
new file mode 100644
@@ -0,0 +1,182 @@
+/*
+ * Copyright (C) 2011 B.A.T.M.A.N. contributors:
+ *
+ * Antonio Quartulli
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA
+ *
+ */
+
+#include <linux/if_ether.h>
+#include <linux/if_arp.h>
+
+#include "main.h"
+#include "distributed-arp-table.h"
+#include "hard-interface.h"
+#include "originator.h"
+#include "send.h"
+#include "types.h"
+#include "unicast.h"
+
+/* Given a key, selects the candidates which the DHT message has to be sent to.
+ * An originator O is selected if and only if its DHT_ID value is one of three
+ * closest values (but not greater) then the hash value of the key.
+ * ip_dst is the key.
+ *
+ * return an array of size DHT_CANDIDATES_NUM */
+static struct dht_candidate *dht_select_candidates(struct bat_priv *bat_priv,
+ uint32_t ip_dst)
+{
+ struct hashtable_t *hash = bat_priv->orig_hash;
+ struct hlist_node *node;
+ struct hlist_head *head;
+ struct orig_node *orig_node, *max_orig_node = NULL;
+ int select, i, j;
+ dat_addr_t last_max = DAT_ADDR_MAX, max, tmp_max, ip_key;
+ struct dht_candidate *res;
+ bool chosen_me = false;
+
+ if (!hash)
+ return NULL;
+
+ res = kmalloc(DHT_CANDIDATES_NUM * sizeof(*res), GFP_ATOMIC);
+ if (!res)
+ return NULL;
+
+ ip_key = (dat_addr_t)hash_ipv4(&ip_dst, DAT_ADDR_MAX);
+
+ bat_dbg(DBG_ARP, bat_priv, "DHT_SEL_CAND key: %pI4 %u\n", &ip_dst,
+ ip_key);
+
+ for (select = 0; select < DHT_CANDIDATES_NUM; select++) {
+ max = 0;
+ max_orig_node = NULL;
+ if (!chosen_me) {
+ /* if true, wrap around the key space */
+ if (bat_priv->dht_hash > ip_key)
+ max = DAT_ADDR_MAX - bat_priv->dht_hash +
+ ip_key;
+ else
+ max = ip_key - bat_priv->dht_hash;
+ max = bat_priv->dht_hash;
+ res[select].type = DHT_CANDIDATE_ME;
+ } else
+ res[select].type = DHT_CANDIDATE_NULL;
+ /* for all origins... */
+ for (i = 0; i < hash->size; i++) {
+ head = &hash->table[i];
+
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(orig_node, node, head,
+ hash_entry) {
+ if (orig_node->dht_hash > ip_key)
+ tmp_max = DAT_ADDR_MAX -
+ orig_node->dht_hash + ip_key;
+ else
+ tmp_max = ip_key - orig_node->dht_hash;
+
+ /* this is closest! */
+ if (tmp_max <= max)
+ continue;
+
+ /* this has already been selected */
+ if (tmp_max > last_max)
+ continue;
+
+ /* In case of hash collision we can select two
+ * nodes with the same hash, but we have ensure
+ * they are different */
+ if (tmp_max == last_max) {
+ for (j = 0; j < select; j++)
+ if (res[j].orig_node ==
+ orig_node)
+ break;
+ if (j < select)
+ continue;
+ }
+
+ if (!atomic_inc_not_zero(&orig_node->refcount))
+ continue;
+
+ max = tmp_max;
+ if (max_orig_node)
+ orig_node_free_ref(max_orig_node);
+ max_orig_node = orig_node;
+ }
+ rcu_read_unlock();
+ }
+ last_max = max;
+ if (max_orig_node) {
+ res[select].type = DHT_CANDIDATE_ORIG;
+ res[select].orig_node = max_orig_node;
+ bat_dbg(DBG_ARP, bat_priv, "DHT_SEL_CAND %d: %pM %u\n",
+ select, max_orig_node->orig, max);
+ }
+ if (res[select].type == DHT_CANDIDATE_ME) {
+ chosen_me = true;
+ bat_dbg(DBG_ARP, bat_priv, "DHT_SEL_CAND %d: ME %u\n",
+ select, bat_priv->dht_hash);
+ }
+
+ max_orig_node = NULL;
+ }
+
+ return res;
+}
+
+/*
+ * Sends the skb payload passed as argument to the candidates selected for
+ * 'ip'. The skb is copied by means of pskb_copy() and is sent as unicast packet
+ * to each of the selected candidate. */
+static bool dht_send_data(struct bat_priv *bat_priv, struct sk_buff *skb,
+ uint32_t ip)
+{
+ int i;
+ bool ret = false;
+ struct neigh_node *neigh_node = NULL;
+ struct sk_buff *tmp_skb;
+ struct dht_candidate *cand = dht_select_candidates(bat_priv, ip);
+
+ if (!cand)
+ goto out;
+
+ bat_dbg(DBG_ARP, bat_priv, "DHT_SEND for %pI4\n", &ip);
+
+ for (i = 0; i < DHT_CANDIDATES_NUM; i++) {
+ if (cand[i].type == DHT_CANDIDATE_ME ||
+ cand[i].type == DHT_CANDIDATE_NULL)
+ continue;
+
+ neigh_node = orig_node_get_router(cand[i].orig_node);
+ if (!neigh_node)
+ goto free_orig;
+
+ tmp_skb = pskb_copy(skb, GFP_ATOMIC);
+ if (prepare_unicast_packet(tmp_skb, cand[i].orig_node))
+ send_skb_packet(tmp_skb, neigh_node->if_incoming,
+ neigh_node->addr);
+ else
+ kfree_skb(tmp_skb);
+ /* set ret to true only if we send at least one request */
+ ret = true;
+ neigh_node_free_ref(neigh_node);
+free_orig:
+ orig_node_free_ref(cand[i].orig_node);
+ }
+
+out:
+ kfree(cand);
+ return ret;
+}
new file mode 100644
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2011 B.A.T.M.A.N. contributors:
+ *
+ * Antonio Quartulli
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public
+ * License as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA
+ *
+ */
+
+#ifndef _NET_BATMAN_ADV_ARP_H_
+#define _NET_BATMAN_ADV_ARP_H_
+
+/*
+ * dat_addr_t is the type used for all DHT indexes. If it is changed,
+ * DAT_ADDR_MAX is changed as well.
+ *
+ * *Please be careful: dat_addr_t must be UNSIGNED*
+ */
+#define dat_addr_t uint16_t
+#define DAT_ADDR_MAX biggest_unsigned_int(dat_addr_t)
+
+/* hash function to choose an entry in a hash table of given size */
+/* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
+static inline uint32_t hash_ipv4(const void *data, uint32_t size)
+{
+ const unsigned char *key = data;
+ uint32_t hash = 0;
+ size_t i;
+
+ for (i = 0; i < 4; i++) {
+ hash += key[i];
+ hash += (hash << 10);
+ hash ^= (hash >> 6);
+ }
+
+ hash += (hash << 3);
+ hash ^= (hash >> 11);
+ hash += (hash << 15);
+
+ return hash % size;
+}
+
+#endif /* _NET_BATMAN_ADV_ARP_H_ */
@@ -20,6 +20,7 @@
*/
#include "main.h"
+#include "distributed-arp-table.h"
#include "hard-interface.h"
#include "soft-interface.h"
#include "send.h"
@@ -118,6 +119,10 @@ static void primary_if_update_addr(struct bat_priv *bat_priv,
if (!primary_if)
goto out;
+ bat_priv->dht_hash = (dat_addr_t)
+ choose_orig(primary_if->net_dev->dev_addr,
+ DAT_ADDR_MAX);
+
vis_packet = (struct vis_packet *)
bat_priv->my_vis_info->skb_packet->data;
memcpy(vis_packet->vis_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
@@ -67,6 +67,9 @@
#define NUM_WORDS BITS_TO_LONGS(TQ_LOCAL_WINDOW_SIZE)
+/* numbers of originator to contact for any STORE/GET DHT operation */
+#define DHT_CANDIDATES_NUM 3
+
#define LOG_BUF_LEN 8192 /* has to be a power of 2 */
#define VIS_INTERVAL 5000 /* 5 seconds */
@@ -111,6 +114,10 @@ enum uev_type {
#define GW_THRESHOLD 50
+#define DHT_CANDIDATE_NULL 0
+#define DHT_CANDIDATE_ME 1
+#define DHT_CANDIDATE_ORIG 2
+
/*
* Debug Messages
*/
@@ -20,6 +20,7 @@
*/
#include "main.h"
+#include "distributed-arp-table.h"
#include "originator.h"
#include "hash.h"
#include "translation-table.h"
@@ -224,6 +225,7 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, const uint8_t *addr)
orig_node->tt_poss_change = false;
orig_node->bat_priv = bat_priv;
memcpy(orig_node->orig, addr, ETH_ALEN);
+ orig_node->dht_hash = (dat_addr_t)choose_orig(addr, DAT_ADDR_MAX);
orig_node->router = NULL;
orig_node->tt_crc = 0;
atomic_set(&orig_node->last_ttvn, 0);
@@ -401,6 +401,8 @@ struct net_device *softif_create(const char *name)
if (ret < 0)
goto unreg_soft_iface;
+ bat_priv->dht_hash = 0;
+
ret = sysfs_add_meshif(soft_iface);
if (ret < 0)
goto unreg_soft_iface;
@@ -24,6 +24,7 @@
#ifndef _NET_BATMAN_ADV_TYPES_H_
#define _NET_BATMAN_ADV_TYPES_H_
+#include "distributed-arp-table.h"
#include "packet.h"
#include "bitarray.h"
@@ -67,6 +68,7 @@ struct hard_iface {
struct orig_node {
uint8_t orig[ETH_ALEN];
uint8_t primary_addr[ETH_ALEN];
+ dat_addr_t dht_hash;
struct neigh_node __rcu *router; /* rcu protected pointer */
unsigned long *bcast_own;
uint8_t *bcast_own_sum;
@@ -215,6 +217,7 @@ struct bat_priv {
struct gw_node __rcu *curr_gw; /* rcu protected pointer */
atomic_t gw_reselect;
struct hard_iface __rcu *primary_if; /* rcu protected pointer */
+ dat_addr_t dht_hash;
struct vis_info *my_vis_info;
struct bat_algo_ops *bat_algo_ops;
};
@@ -385,4 +388,9 @@ struct bat_algo_ops {
struct sk_buff *skb);
};
+struct dht_candidate {
+ int type;
+ struct orig_node *orig_node;
+};
+
#endif /* _NET_BATMAN_ADV_TYPES_H_ */