2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* increase the reference counter for this originator */
25 #include "originator.h"
27 #include "translation-table.h"
29 #include "hard-interface.h"
31 static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
33 static void start_purge_timer(void)
35 queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
38 int originator_init(void)
44 spin_lock_irqsave(&orig_hash_lock, flags);
45 orig_hash = hash_new(128, compare_orig, choose_orig);
50 spin_unlock_irqrestore(&orig_hash_lock, flags);
55 spin_unlock_irqrestore(&orig_hash_lock, flags);
60 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
61 uint8_t *neigh, struct batman_if *if_incoming)
63 /* FIXME: each orig_node->batman_if will be attached to a softif */
64 struct bat_priv *bat_priv = netdev_priv(soft_device);
65 struct neigh_node *neigh_node;
67 bat_dbg(DBG_BATMAN, bat_priv,
68 "Creating new last-hop neighbor of originator\n");
70 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
74 INIT_LIST_HEAD(&neigh_node->list);
76 memcpy(neigh_node->addr, neigh, ETH_ALEN);
77 neigh_node->orig_node = orig_neigh_node;
78 neigh_node->if_incoming = if_incoming;
80 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
84 static void free_orig_node(void *data)
86 struct list_head *list_pos, *list_pos_tmp;
87 struct neigh_node *neigh_node;
88 struct orig_node *orig_node = (struct orig_node *)data;
90 /* for all neighbors towards this originator ... */
91 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
92 neigh_node = list_entry(list_pos, struct neigh_node, list);
98 hna_global_del_orig(orig_node, "originator timed out");
100 kfree(orig_node->bcast_own);
101 kfree(orig_node->bcast_own_sum);
105 void originator_free(void)
112 cancel_delayed_work_sync(&purge_orig_wq);
114 spin_lock_irqsave(&orig_hash_lock, flags);
115 hash_delete(orig_hash, free_orig_node);
117 spin_unlock_irqrestore(&orig_hash_lock, flags);
120 /* this function finds or creates an originator entry for the given
121 * address if it does not exits */
122 struct orig_node *get_orig_node(uint8_t *addr)
124 /* FIXME: each batman_if will be attached to a softif */
125 struct bat_priv *bat_priv = netdev_priv(soft_device);
126 struct orig_node *orig_node;
127 struct hashtable_t *swaphash;
130 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
132 if (orig_node != NULL)
135 bat_dbg(DBG_BATMAN, bat_priv,
136 "Creating new originator: %pM\n", addr);
138 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
142 INIT_LIST_HEAD(&orig_node->neigh_list);
144 memcpy(orig_node->orig, addr, ETH_ALEN);
145 orig_node->router = NULL;
146 orig_node->hna_buff = NULL;
147 orig_node->bcast_seqno_reset = jiffies - 1
148 - msecs_to_jiffies(RESET_PROTECTION_MS);
149 orig_node->batman_seqno_reset = jiffies - 1
150 - msecs_to_jiffies(RESET_PROTECTION_MS);
152 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
154 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
155 if (!orig_node->bcast_own)
158 size = bat_priv->num_ifaces * sizeof(uint8_t);
159 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
160 if (!orig_node->bcast_own_sum)
163 if (hash_add(orig_hash, orig_node) < 0)
164 goto free_bcast_own_sum;
166 if (orig_hash->elements * 4 > orig_hash->size) {
167 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
169 if (swaphash == NULL)
171 "Couldn't resize orig hash table\n");
173 orig_hash = swaphash;
178 kfree(orig_node->bcast_own_sum);
180 kfree(orig_node->bcast_own);
186 static bool purge_orig_neighbors(struct orig_node *orig_node,
187 struct neigh_node **best_neigh_node)
189 /* FIXME: each orig_node->batman_if will be attached to a softif */
190 struct bat_priv *bat_priv = netdev_priv(soft_device);
191 struct list_head *list_pos, *list_pos_tmp;
192 struct neigh_node *neigh_node;
193 bool neigh_purged = false;
195 *best_neigh_node = NULL;
197 /* for all neighbors towards this originator ... */
198 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
199 neigh_node = list_entry(list_pos, struct neigh_node, list);
201 if ((time_after(jiffies,
202 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
203 (neigh_node->if_incoming->if_status ==
206 if (neigh_node->if_incoming->if_status ==
208 bat_dbg(DBG_BATMAN, bat_priv,
209 "neighbor purge: originator %pM, "
210 "neighbor: %pM, iface: %s\n",
211 orig_node->orig, neigh_node->addr,
212 neigh_node->if_incoming->dev);
214 bat_dbg(DBG_BATMAN, bat_priv,
215 "neighbor timeout: originator %pM, "
216 "neighbor: %pM, last_valid: %lu\n",
217 orig_node->orig, neigh_node->addr,
218 (neigh_node->last_valid / HZ));
224 if ((*best_neigh_node == NULL) ||
225 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
226 *best_neigh_node = neigh_node;
232 static bool purge_orig_node(struct orig_node *orig_node)
234 /* FIXME: each batman_if will be attached to a softif */
235 struct bat_priv *bat_priv = netdev_priv(soft_device);
236 struct neigh_node *best_neigh_node;
238 if (time_after(jiffies,
239 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
241 bat_dbg(DBG_BATMAN, bat_priv,
242 "Originator timeout: originator %pM, last_valid %lu\n",
243 orig_node->orig, (orig_node->last_valid / HZ));
246 if (purge_orig_neighbors(orig_node, &best_neigh_node)) {
247 update_routes(orig_node, best_neigh_node,
249 orig_node->hna_buff_len);
250 /* update bonding candidates, we could have lost
251 * some candidates. */
252 update_bonding_candidates(bat_priv, orig_node);
259 void purge_orig(struct work_struct *work)
262 struct orig_node *orig_node;
265 spin_lock_irqsave(&orig_hash_lock, flags);
267 /* for all origins... */
268 while (hash_iterate(orig_hash, &hashit)) {
269 orig_node = hashit.bucket->data;
270 if (purge_orig_node(orig_node)) {
271 hash_remove_bucket(orig_hash, &hashit);
272 free_orig_node(orig_node);
276 spin_unlock_irqrestore(&orig_hash_lock, flags);
278 /* if work == NULL we were not called by the timer
279 * and thus do not need to re-arm the timer */
284 int orig_seq_print_text(struct seq_file *seq, void *offset)
287 struct net_device *net_dev = (struct net_device *)seq->private;
288 struct bat_priv *bat_priv = netdev_priv(net_dev);
289 struct orig_node *orig_node;
290 struct neigh_node *neigh_node;
291 int batman_count = 0;
295 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
297 if ((!bat_priv->primary_if) ||
298 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
299 if (!bat_priv->primary_if)
300 return seq_printf(seq, "BATMAN mesh %s disabled - "
301 "please specify interfaces to enable it\n",
304 return seq_printf(seq, "BATMAN mesh %s "
305 "disabled - primary interface not active\n",
310 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
311 SOURCE_VERSION, REVISION_VERSION_STR,
312 bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
314 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
315 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
316 "outgoingIF", "Potential nexthops");
319 spin_lock_irqsave(&orig_hash_lock, flags);
321 while (hash_iterate(orig_hash, &hashit)) {
323 orig_node = hashit.bucket->data;
325 if (!orig_node->router)
328 if (orig_node->router->tq_avg == 0)
331 addr_to_string(orig_str, orig_node->orig);
332 addr_to_string(router_str, orig_node->router->addr);
333 last_seen_secs = jiffies_to_msecs(jiffies -
334 orig_node->last_valid) / 1000;
335 last_seen_msecs = jiffies_to_msecs(jiffies -
336 orig_node->last_valid) % 1000;
338 seq_printf(seq, "%-17s %4i.%03is (%3i) %17s [%10s]:",
339 orig_str, last_seen_secs, last_seen_msecs,
340 orig_node->router->tq_avg, router_str,
341 orig_node->router->if_incoming->dev);
343 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
344 addr_to_string(orig_str, neigh_node->addr);
345 seq_printf(seq, " %17s (%3i)", orig_str,
349 seq_printf(seq, "\n");
353 spin_unlock_irqrestore(&orig_hash_lock, flags);
355 if ((batman_count == 0))
356 seq_printf(seq, "No batman nodes in range ...\n");
361 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
365 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
368 pr_err("Can't resize orig: out of memory\n");
372 memcpy(data_ptr, orig_node->bcast_own,
373 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
374 kfree(orig_node->bcast_own);
375 orig_node->bcast_own = data_ptr;
377 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
379 pr_err("Can't resize orig: out of memory\n");
383 memcpy(data_ptr, orig_node->bcast_own_sum,
384 (max_if_num - 1) * sizeof(uint8_t));
385 kfree(orig_node->bcast_own_sum);
386 orig_node->bcast_own_sum = data_ptr;
391 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
393 struct orig_node *orig_node;
396 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
398 spin_lock(&orig_hash_lock);
400 while (hash_iterate(orig_hash, &hashit)) {
401 orig_node = hashit.bucket->data;
403 if (orig_node_add_if(orig_node, max_if_num) == -1)
407 spin_unlock(&orig_hash_lock);
411 spin_unlock(&orig_hash_lock);
415 static int orig_node_del_if(struct orig_node *orig_node,
416 int max_if_num, int del_if_num)
418 void *data_ptr = NULL;
421 /* last interface was removed */
425 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
426 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
428 pr_err("Can't resize orig: out of memory\n");
432 /* copy first part */
433 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
435 /* copy second part */
437 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
438 (max_if_num - del_if_num) * chunk_size);
441 kfree(orig_node->bcast_own);
442 orig_node->bcast_own = data_ptr;
447 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
449 pr_err("Can't resize orig: out of memory\n");
453 memcpy(data_ptr, orig_node->bcast_own_sum,
454 del_if_num * sizeof(uint8_t));
457 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
458 (max_if_num - del_if_num) * sizeof(uint8_t));
461 kfree(orig_node->bcast_own_sum);
462 orig_node->bcast_own_sum = data_ptr;
467 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
469 struct batman_if *batman_if_tmp;
470 struct orig_node *orig_node;
474 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
476 spin_lock(&orig_hash_lock);
478 while (hash_iterate(orig_hash, &hashit)) {
479 orig_node = hashit.bucket->data;
481 ret = orig_node_del_if(orig_node, max_if_num,
488 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
490 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
491 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
494 if (batman_if == batman_if_tmp)
497 if (batman_if_tmp->if_num > batman_if->if_num)
498 batman_if_tmp->if_num--;
502 batman_if->if_num = -1;
503 spin_unlock(&orig_hash_lock);
507 spin_unlock(&orig_hash_lock);