summaryrefslogtreecommitdiffstats
path: root/net/batman-adv/translation-table.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/batman-adv/translation-table.c')
-rw-r--r--net/batman-adv/translation-table.c208
1 files changed, 141 insertions, 67 deletions
diff --git a/net/batman-adv/translation-table.c b/net/batman-adv/translation-table.c
index a633b5a4..8d15b48 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
+ * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
*
* Marek Lindner, Simon Wunderlich
*
@@ -22,7 +22,6 @@
#include "main.h"
#include "translation-table.h"
#include "soft-interface.h"
-#include "types.h"
#include "hash.h"
#include "originator.h"
@@ -31,12 +30,85 @@ static void _hna_global_del_orig(struct bat_priv *bat_priv,
struct hna_global_entry *hna_global_entry,
char *message);
+/* returns 1 if they are the same mac addr */
+static int compare_lhna(struct hlist_node *node, void *data2)
+{
+ void *data1 = container_of(node, struct hna_local_entry, hash_entry);
+
+ return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
+}
+
+/* returns 1 if they are the same mac addr */
+static int compare_ghna(struct hlist_node *node, void *data2)
+{
+ void *data1 = container_of(node, struct hna_global_entry, hash_entry);
+
+ return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
+}
+
static void hna_local_start_timer(struct bat_priv *bat_priv)
{
INIT_DELAYED_WORK(&bat_priv->hna_work, hna_local_purge);
queue_delayed_work(bat_event_workqueue, &bat_priv->hna_work, 10 * HZ);
}
+static struct hna_local_entry *hna_local_hash_find(struct bat_priv *bat_priv,
+ void *data)
+{
+ struct hashtable_t *hash = bat_priv->hna_local_hash;
+ struct hlist_head *head;
+ struct hlist_node *node;
+ struct hna_local_entry *hna_local_entry, *hna_local_entry_tmp = NULL;
+ int index;
+
+ if (!hash)
+ return NULL;
+
+ index = choose_orig(data, hash->size);
+ head = &hash->table[index];
+
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(hna_local_entry, node, head, hash_entry) {
+ if (!compare_eth(hna_local_entry, data))
+ continue;
+
+ hna_local_entry_tmp = hna_local_entry;
+ break;
+ }
+ rcu_read_unlock();
+
+ return hna_local_entry_tmp;
+}
+
+static struct hna_global_entry *hna_global_hash_find(struct bat_priv *bat_priv,
+ void *data)
+{
+ struct hashtable_t *hash = bat_priv->hna_global_hash;
+ struct hlist_head *head;
+ struct hlist_node *node;
+ struct hna_global_entry *hna_global_entry;
+ struct hna_global_entry *hna_global_entry_tmp = NULL;
+ int index;
+
+ if (!hash)
+ return NULL;
+
+ index = choose_orig(data, hash->size);
+ head = &hash->table[index];
+
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(hna_global_entry, node, head, hash_entry) {
+ if (!compare_eth(hna_global_entry, data))
+ continue;
+
+ hna_global_entry_tmp = hna_global_entry;
+ break;
+ }
+ rcu_read_unlock();
+
+ return hna_global_entry_tmp;
+}
+
int hna_local_init(struct bat_priv *bat_priv)
{
if (bat_priv->hna_local_hash)
@@ -61,10 +133,7 @@ void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
int required_bytes;
spin_lock_bh(&bat_priv->hna_lhash_lock);
- hna_local_entry =
- ((struct hna_local_entry *)hash_find(bat_priv->hna_local_hash,
- compare_orig, choose_orig,
- addr));
+ hna_local_entry = hna_local_hash_find(bat_priv, addr);
spin_unlock_bh(&bat_priv->hna_lhash_lock);
if (hna_local_entry) {
@@ -100,15 +169,15 @@ void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
hna_local_entry->last_seen = jiffies;
/* the batman interface mac address should never be purged */
- if (compare_orig(addr, soft_iface->dev_addr))
+ if (compare_eth(addr, soft_iface->dev_addr))
hna_local_entry->never_purge = 1;
else
hna_local_entry->never_purge = 0;
spin_lock_bh(&bat_priv->hna_lhash_lock);
- hash_add(bat_priv->hna_local_hash, compare_orig, choose_orig,
- hna_local_entry);
+ hash_add(bat_priv->hna_local_hash, compare_lhna, choose_orig,
+ hna_local_entry, &hna_local_entry->hash_entry);
bat_priv->num_local_hna++;
atomic_set(&bat_priv->hna_local_changed, 1);
@@ -117,9 +186,7 @@ void hna_local_add(struct net_device *soft_iface, uint8_t *addr)
/* remove address from global hash if present */
spin_lock_bh(&bat_priv->hna_ghash_lock);
- hna_global_entry = ((struct hna_global_entry *)
- hash_find(bat_priv->hna_global_hash,
- compare_orig, choose_orig, addr));
+ hna_global_entry = hna_global_hash_find(bat_priv, addr);
if (hna_global_entry)
_hna_global_del_orig(bat_priv, hna_global_entry,
@@ -133,28 +200,27 @@ int hna_local_fill_buffer(struct bat_priv *bat_priv,
{
struct hashtable_t *hash = bat_priv->hna_local_hash;
struct hna_local_entry *hna_local_entry;
- struct element_t *bucket;
- int i;
- struct hlist_node *walk;
+ struct hlist_node *node;
struct hlist_head *head;
- int count = 0;
+ int i, count = 0;
spin_lock_bh(&bat_priv->hna_lhash_lock);
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each_entry(bucket, walk, head, hlist) {
-
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(hna_local_entry, node,
+ head, hash_entry) {
if (buff_len < (count + 1) * ETH_ALEN)
break;
- hna_local_entry = bucket->data;
memcpy(buff + (count * ETH_ALEN), hna_local_entry->addr,
ETH_ALEN);
count++;
}
+ rcu_read_unlock();
}
/* if we did not get all new local hnas see you next time ;-) */
@@ -171,12 +237,11 @@ int hna_local_seq_print_text(struct seq_file *seq, void *offset)
struct bat_priv *bat_priv = netdev_priv(net_dev);
struct hashtable_t *hash = bat_priv->hna_local_hash;
struct hna_local_entry *hna_local_entry;
- int i;
- struct hlist_node *walk;
+ struct hlist_node *node;
struct hlist_head *head;
- struct element_t *bucket;
size_t buf_size, pos;
char *buff;
+ int i;
if (!bat_priv->primary_if) {
return seq_printf(seq, "BATMAN mesh %s disabled - "
@@ -195,8 +260,10 @@ int hna_local_seq_print_text(struct seq_file *seq, void *offset)
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each(walk, head)
+ rcu_read_lock();
+ __hlist_for_each_rcu(node, head)
buf_size += 21;
+ rcu_read_unlock();
}
buff = kmalloc(buf_size, GFP_ATOMIC);
@@ -204,18 +271,20 @@ int hna_local_seq_print_text(struct seq_file *seq, void *offset)
spin_unlock_bh(&bat_priv->hna_lhash_lock);
return -ENOMEM;
}
+
buff[0] = '\0';
pos = 0;
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each_entry(bucket, walk, head, hlist) {
- hna_local_entry = bucket->data;
-
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(hna_local_entry, node,
+ head, hash_entry) {
pos += snprintf(buff + pos, 22, " * %pM\n",
hna_local_entry->addr);
}
+ rcu_read_unlock();
}
spin_unlock_bh(&bat_priv->hna_lhash_lock);
@@ -225,9 +294,10 @@ int hna_local_seq_print_text(struct seq_file *seq, void *offset)
return 0;
}
-static void _hna_local_del(void *data, void *arg)
+static void _hna_local_del(struct hlist_node *node, void *arg)
{
struct bat_priv *bat_priv = (struct bat_priv *)arg;
+ void *data = container_of(node, struct hna_local_entry, hash_entry);
kfree(data);
bat_priv->num_local_hna--;
@@ -241,9 +311,9 @@ static void hna_local_del(struct bat_priv *bat_priv,
bat_dbg(DBG_ROUTES, bat_priv, "Deleting local hna entry (%pM): %s\n",
hna_local_entry->addr, message);
- hash_remove(bat_priv->hna_local_hash, compare_orig, choose_orig,
+ hash_remove(bat_priv->hna_local_hash, compare_lhna, choose_orig,
hna_local_entry->addr);
- _hna_local_del(hna_local_entry, bat_priv);
+ _hna_local_del(&hna_local_entry->hash_entry, bat_priv);
}
void hna_local_remove(struct bat_priv *bat_priv,
@@ -253,9 +323,7 @@ void hna_local_remove(struct bat_priv *bat_priv,
spin_lock_bh(&bat_priv->hna_lhash_lock);
- hna_local_entry = (struct hna_local_entry *)
- hash_find(bat_priv->hna_local_hash, compare_orig, choose_orig,
- addr);
+ hna_local_entry = hna_local_hash_find(bat_priv, addr);
if (hna_local_entry)
hna_local_del(bat_priv, hna_local_entry, message);
@@ -271,27 +339,29 @@ static void hna_local_purge(struct work_struct *work)
container_of(delayed_work, struct bat_priv, hna_work);
struct hashtable_t *hash = bat_priv->hna_local_hash;
struct hna_local_entry *hna_local_entry;
- int i;
- struct hlist_node *walk, *safe;
+ struct hlist_node *node, *node_tmp;
struct hlist_head *head;
- struct element_t *bucket;
unsigned long timeout;
+ int i;
spin_lock_bh(&bat_priv->hna_lhash_lock);
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
- hna_local_entry = bucket->data;
+ hlist_for_each_entry_safe(hna_local_entry, node, node_tmp,
+ head, hash_entry) {
+ if (hna_local_entry->never_purge)
+ continue;
timeout = hna_local_entry->last_seen;
timeout += LOCAL_HNA_TIMEOUT * HZ;
- if ((!hna_local_entry->never_purge) &&
- time_after(jiffies, timeout))
- hna_local_del(bat_priv, hna_local_entry,
- "address timed out");
+ if (time_before(jiffies, timeout))
+ continue;
+
+ hna_local_del(bat_priv, hna_local_entry,
+ "address timed out");
}
}
@@ -335,9 +405,7 @@ void hna_global_add_orig(struct bat_priv *bat_priv,
spin_lock_bh(&bat_priv->hna_ghash_lock);
hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
- hna_global_entry = (struct hna_global_entry *)
- hash_find(bat_priv->hna_global_hash, compare_orig,
- choose_orig, hna_ptr);
+ hna_global_entry = hna_global_hash_find(bat_priv, hna_ptr);
if (!hna_global_entry) {
spin_unlock_bh(&bat_priv->hna_ghash_lock);
@@ -357,8 +425,9 @@ void hna_global_add_orig(struct bat_priv *bat_priv,
hna_global_entry->addr, orig_node->orig);
spin_lock_bh(&bat_priv->hna_ghash_lock);
- hash_add(bat_priv->hna_global_hash, compare_orig,
- choose_orig, hna_global_entry);
+ hash_add(bat_priv->hna_global_hash, compare_ghna,
+ choose_orig, hna_global_entry,
+ &hna_global_entry->hash_entry);
}
@@ -369,9 +438,7 @@ void hna_global_add_orig(struct bat_priv *bat_priv,
spin_lock_bh(&bat_priv->hna_lhash_lock);
hna_ptr = hna_buff + (hna_buff_count * ETH_ALEN);
- hna_local_entry = (struct hna_local_entry *)
- hash_find(bat_priv->hna_local_hash, compare_orig,
- choose_orig, hna_ptr);
+ hna_local_entry = hna_local_hash_find(bat_priv, hna_ptr);
if (hna_local_entry)
hna_local_del(bat_priv, hna_local_entry,
@@ -401,12 +468,11 @@ int hna_global_seq_print_text(struct seq_file *seq, void *offset)
struct bat_priv *bat_priv = netdev_priv(net_dev);
struct hashtable_t *hash = bat_priv->hna_global_hash;
struct hna_global_entry *hna_global_entry;
- int i;
- struct hlist_node *walk;
+ struct hlist_node *node;
struct hlist_head *head;
- struct element_t *bucket;
size_t buf_size, pos;
char *buff;
+ int i;
if (!bat_priv->primary_if) {
return seq_printf(seq, "BATMAN mesh %s disabled - "
@@ -424,8 +490,10 @@ int hna_global_seq_print_text(struct seq_file *seq, void *offset)
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each(walk, head)
+ rcu_read_lock();
+ __hlist_for_each_rcu(node, head)
buf_size += 43;
+ rcu_read_unlock();
}
buff = kmalloc(buf_size, GFP_ATOMIC);
@@ -439,14 +507,15 @@ int hna_global_seq_print_text(struct seq_file *seq, void *offset)
for (i = 0; i < hash->size; i++) {
head = &hash->table[i];
- hlist_for_each_entry(bucket, walk, head, hlist) {
- hna_global_entry = bucket->data;
-
+ rcu_read_lock();
+ hlist_for_each_entry_rcu(hna_global_entry, node,
+ head, hash_entry) {
pos += snprintf(buff + pos, 44,
" * %pM via %pM\n",
hna_global_entry->addr,
hna_global_entry->orig_node->orig);
}
+ rcu_read_unlock();
}
spin_unlock_bh(&bat_priv->hna_ghash_lock);
@@ -465,7 +534,7 @@ static void _hna_global_del_orig(struct bat_priv *bat_priv,
hna_global_entry->addr, hna_global_entry->orig_node->orig,
message);
- hash_remove(bat_priv->hna_global_hash, compare_orig, choose_orig,
+ hash_remove(bat_priv->hna_global_hash, compare_ghna, choose_orig,
hna_global_entry->addr);
kfree(hna_global_entry);
}
@@ -484,9 +553,7 @@ void hna_global_del_orig(struct bat_priv *bat_priv,
while ((hna_buff_count + 1) * ETH_ALEN <= orig_node->hna_buff_len) {
hna_ptr = orig_node->hna_buff + (hna_buff_count * ETH_ALEN);
- hna_global_entry = (struct hna_global_entry *)
- hash_find(bat_priv->hna_global_hash, compare_orig,
- choose_orig, hna_ptr);
+ hna_global_entry = hna_global_hash_find(bat_priv, hna_ptr);
if ((hna_global_entry) &&
(hna_global_entry->orig_node == orig_node))
@@ -503,8 +570,10 @@ void hna_global_del_orig(struct bat_priv *bat_priv,
orig_node->hna_buff = NULL;
}
-static void hna_global_del(void *data, void *arg)
+static void hna_global_del(struct hlist_node *node, void *arg)
{
+ void *data = container_of(node, struct hna_global_entry, hash_entry);
+
kfree(data);
}
@@ -520,15 +589,20 @@ void hna_global_free(struct bat_priv *bat_priv)
struct orig_node *transtable_search(struct bat_priv *bat_priv, uint8_t *addr)
{
struct hna_global_entry *hna_global_entry;
+ struct orig_node *orig_node = NULL;
spin_lock_bh(&bat_priv->hna_ghash_lock);
- hna_global_entry = (struct hna_global_entry *)
- hash_find(bat_priv->hna_global_hash,
- compare_orig, choose_orig, addr);
- spin_unlock_bh(&bat_priv->hna_ghash_lock);
+ hna_global_entry = hna_global_hash_find(bat_priv, addr);
if (!hna_global_entry)
- return NULL;
+ goto out;
- return hna_global_entry->orig_node;
+ if (!atomic_inc_not_zero(&hna_global_entry->orig_node->refcount))
+ goto out;
+
+ orig_node = hna_global_entry->orig_node;
+
+out:
+ spin_unlock_bh(&bat_priv->hna_ghash_lock);
+ return orig_node;
}
OpenPOWER on IntegriCloud