On Sun, Oct 30, 2011 at 09:55:59AM +0100, Antonio Quartulli wrote:
> [...]
> + 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 = USHRT_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;
> +
> + if (!atomic_inc_not_zero(&orig_node->refcount))
> + 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;
Why do you increase the refcount two times? If it is really required, please
add a comment.
> +
> + 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;
> + }
no braces are required here.
> +
> + neigh_node = orig_node_get_router(cand[i].orig_node);
> + if (!neigh_node)
> + goto free_orig;
> +
> + tmp_skb = pskb_copy(skb, GFP_ATOMIC);
> + tmp_skb = prepare_unicast_packet(tmp_skb, cand[i].orig_node);
> + if (tmp_skb)
> + send_skb_packet(tmp_skb, neigh_node->if_incoming,
> + neigh_node->addr);
> + /* 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;
> +}
@@ -32,6 +32,7 @@ ccflags-y += -DSOURCE_VERSION=\"$(REVISION)\"
endif
obj-m += batman-adv.o
+batman-adv-y += arp.o
batman-adv-y += bat_debugfs.o
batman-adv-y += bat_iv_ogm.o
batman-adv-y += bat_sysfs.o
new file mode 100644
@@ -0,0 +1,185 @@
+/*
+ * 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 "arp.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;
+ uint16_t last_max = USHRT_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 = hash_ipv4(&ip_dst, USHRT_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 = USHRT_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 = USHRT_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;
+
+ if (!atomic_inc_not_zero(&orig_node->refcount))
+ 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);
+ tmp_skb = prepare_unicast_packet(tmp_skb, cand[i].orig_node);
+ if (tmp_skb)
+ send_skb_packet(tmp_skb, neigh_node->if_incoming,
+ neigh_node->addr);
+ /* 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,46 @@
+/*
+ * 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_
+
+/* 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_ */
@@ -117,6 +117,8 @@ static void primary_if_update_addr(struct bat_priv *bat_priv)
if (!primary_if)
goto out;
+ bat_priv->dht_hash = choose_orig(bat_priv, USHRT_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);
@@ -64,6 +64,9 @@
#define NULL_IFINDEX 0 /* dummy ifindex used to avoid iface checks */
+/* numbers of originator to contact for any STORE/GET DHT operation */
+#define DHT_CANDIDATES_NUM 3
+
#define NUM_WORDS (TQ_LOCAL_WINDOW_SIZE / WORD_BIT_SIZE)
#define LOG_BUF_LEN 8192 /* has to be a power of 2 */
@@ -106,6 +109,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
*/
@@ -222,6 +222,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 = choose_orig(addr, USHRT_MAX);
orig_node->router = NULL;
orig_node->tt_crc = 0;
atomic_set(&orig_node->last_ttvn, 0);
@@ -20,6 +20,7 @@
*/
#include "main.h"
+#include "arp.h"
#include "soft-interface.h"
#include "hard-interface.h"
#include "routing.h"
@@ -855,6 +856,8 @@ struct net_device *softif_create(const char *name)
bat_priv->primary_if = NULL;
bat_priv->num_ifaces = 0;
+ bat_priv->dht_hash = 0;
+
ret = sysfs_add_meshif(soft_iface);
if (ret < 0)
goto unreg_soft_iface;
@@ -67,6 +67,7 @@ struct hard_iface {
struct orig_node {
uint8_t orig[ETH_ALEN];
uint8_t primary_addr[ETH_ALEN];
+ uint16_t dht_hash;
struct neigh_node __rcu *router; /* rcu protected pointer */
unsigned long *bcast_own;
uint8_t *bcast_own_sum;
@@ -204,6 +205,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 */
+ uint16_t dht_hash;
struct vis_info *my_vis_info;
};
@@ -343,4 +345,9 @@ struct softif_neigh {
struct rcu_head rcu;
};
+struct dht_candidate {
+ int type;
+ struct orig_node *orig_node;
+};
+
#endif /* _NET_BATMAN_ADV_TYPES_H_ */