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 struct neigh_node *neigh_node;
65 bat_dbg(DBG_BATMAN, "Creating new last-hop neighbor of originator\n");
67 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
71 INIT_LIST_HEAD(&neigh_node->list);
73 memcpy(neigh_node->addr, neigh, ETH_ALEN);
74 neigh_node->orig_node = orig_neigh_node;
75 neigh_node->if_incoming = if_incoming;
77 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
81 static void free_orig_node(void *data)
83 struct list_head *list_pos, *list_pos_tmp;
84 struct neigh_node *neigh_node;
85 struct orig_node *orig_node = (struct orig_node *)data;
87 /* for all neighbors towards this originator ... */
88 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
89 neigh_node = list_entry(list_pos, struct neigh_node, list);
95 hna_global_del_orig(orig_node, "originator timed out");
97 kfree(orig_node->bcast_own);
98 kfree(orig_node->bcast_own_sum);
102 void originator_free(void)
109 cancel_delayed_work_sync(&purge_orig_wq);
111 spin_lock_irqsave(&orig_hash_lock, flags);
112 hash_delete(orig_hash, free_orig_node);
114 spin_unlock_irqrestore(&orig_hash_lock, flags);
117 /* this function finds or creates an originator entry for the given
118 * address if it does not exits */
119 struct orig_node *get_orig_node(uint8_t *addr)
121 /* FIXME: each batman_if will be attached to a softif */
122 struct bat_priv *bat_priv = netdev_priv(soft_device);
123 struct orig_node *orig_node;
124 struct hashtable_t *swaphash;
127 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
129 if (orig_node != NULL)
132 bat_dbg(DBG_BATMAN, "Creating new originator: %pM\n", addr);
134 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
138 INIT_LIST_HEAD(&orig_node->neigh_list);
140 memcpy(orig_node->orig, addr, ETH_ALEN);
141 orig_node->router = NULL;
142 orig_node->hna_buff = NULL;
143 orig_node->bcast_seqno_reset = jiffies - 1
144 - msecs_to_jiffies(RESET_PROTECTION_MS);
145 orig_node->batman_seqno_reset = jiffies - 1
146 - msecs_to_jiffies(RESET_PROTECTION_MS);
148 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
150 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
151 if (!orig_node->bcast_own)
154 size = bat_priv->num_ifaces * sizeof(uint8_t);
155 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
156 if (!orig_node->bcast_own_sum)
159 if (hash_add(orig_hash, orig_node) < 0)
160 goto free_bcast_own_sum;
162 if (orig_hash->elements * 4 > orig_hash->size) {
163 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
165 if (swaphash == NULL)
167 "batman-adv:Couldn't resize orig hash table\n");
169 orig_hash = swaphash;
174 kfree(orig_node->bcast_own_sum);
176 kfree(orig_node->bcast_own);
182 static bool purge_orig_neighbors(struct orig_node *orig_node,
183 struct neigh_node **best_neigh_node)
185 struct list_head *list_pos, *list_pos_tmp;
186 struct neigh_node *neigh_node;
187 bool neigh_purged = false;
189 *best_neigh_node = NULL;
191 /* for all neighbors towards this originator ... */
192 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
193 neigh_node = list_entry(list_pos, struct neigh_node, list);
195 if ((time_after(jiffies,
196 (neigh_node->last_valid +
197 ((PURGE_TIMEOUT * HZ) / 1000)))) ||
198 (neigh_node->if_incoming->if_status ==
201 if (neigh_node->if_incoming->if_status ==
204 "neighbor purge: originator %pM, "
205 "neighbor: %pM, iface: %s\n",
206 orig_node->orig, neigh_node->addr,
207 neigh_node->if_incoming->dev);
210 "neighbor timeout: originator %pM, "
211 "neighbor: %pM, last_valid: %lu\n",
212 orig_node->orig, neigh_node->addr,
213 (neigh_node->last_valid / HZ));
219 if ((*best_neigh_node == NULL) ||
220 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
221 *best_neigh_node = neigh_node;
227 static bool purge_orig_node(struct orig_node *orig_node)
229 struct neigh_node *best_neigh_node;
231 if (time_after(jiffies,
232 (orig_node->last_valid +
233 ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
236 "Originator timeout: originator %pM, last_valid %lu\n",
237 orig_node->orig, (orig_node->last_valid / HZ));
240 if (purge_orig_neighbors(orig_node, &best_neigh_node))
241 update_routes(orig_node, best_neigh_node,
243 orig_node->hna_buff_len);
249 void purge_orig(struct work_struct *work)
252 struct orig_node *orig_node;
255 spin_lock_irqsave(&orig_hash_lock, flags);
257 /* for all origins... */
258 while (hash_iterate(orig_hash, &hashit)) {
259 orig_node = hashit.bucket->data;
260 if (purge_orig_node(orig_node)) {
261 hash_remove_bucket(orig_hash, &hashit);
262 free_orig_node(orig_node);
266 spin_unlock_irqrestore(&orig_hash_lock, flags);
268 /* if work == NULL we were not called by the timer
269 * and thus do not need to re-arm the timer */
274 int orig_seq_print_text(struct seq_file *seq, void *offset)
277 struct net_device *net_dev = (struct net_device *)seq->private;
278 struct bat_priv *bat_priv = netdev_priv(net_dev);
279 struct orig_node *orig_node;
280 struct neigh_node *neigh_node;
281 int batman_count = 0;
283 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
285 if ((!bat_priv->primary_if) ||
286 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
287 if (!bat_priv->primary_if)
288 return seq_printf(seq, "BATMAN mesh %s disabled - "
289 "please specify interfaces to enable it\n",
292 return seq_printf(seq, "BATMAN mesh %s "
293 "disabled - primary interface not active\n",
298 seq_printf(seq, " %-14s (%s/%i) %17s [%10s]: %20s "
299 "... [B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
300 "Originator", "#", TQ_MAX_VALUE, "Nexthop", "outgoingIF",
301 "Potential nexthops", SOURCE_VERSION, REVISION_VERSION_STR,
302 bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
306 spin_lock_irqsave(&orig_hash_lock, flags);
308 while (hash_iterate(orig_hash, &hashit)) {
310 orig_node = hashit.bucket->data;
312 if (!orig_node->router)
315 if (orig_node->router->tq_avg == 0)
318 addr_to_string(orig_str, orig_node->orig);
319 addr_to_string(router_str, orig_node->router->addr);
321 seq_printf(seq, "%-17s (%3i) %17s [%10s]:",
322 orig_str, orig_node->router->tq_avg, router_str,
323 orig_node->router->if_incoming->dev);
325 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
326 addr_to_string(orig_str, neigh_node->addr);
327 seq_printf(seq, " %17s (%3i)", orig_str,
331 seq_printf(seq, "\n");
335 spin_unlock_irqrestore(&orig_hash_lock, flags);
337 if ((batman_count == 0))
338 seq_printf(seq, "No batman nodes in range ...\n");
343 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
347 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
351 "batman-adv:Can't resize orig: out of memory\n");
355 memcpy(data_ptr, orig_node->bcast_own,
356 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
357 kfree(orig_node->bcast_own);
358 orig_node->bcast_own = data_ptr;
360 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
363 "batman-adv:Can't resize orig: out of memory\n");
367 memcpy(data_ptr, orig_node->bcast_own_sum,
368 (max_if_num - 1) * sizeof(uint8_t));
369 kfree(orig_node->bcast_own_sum);
370 orig_node->bcast_own_sum = data_ptr;
375 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
377 struct orig_node *orig_node;
380 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
382 spin_lock(&orig_hash_lock);
384 while (hash_iterate(orig_hash, &hashit)) {
385 orig_node = hashit.bucket->data;
387 if (orig_node_add_if(orig_node, max_if_num) == -1)
391 spin_unlock(&orig_hash_lock);
395 spin_unlock(&orig_hash_lock);
399 static int orig_node_del_if(struct orig_node *orig_node,
400 int max_if_num, int del_if_num)
402 void *data_ptr = NULL;
405 /* last interface was removed */
409 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
410 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
413 "batman-adv:Can't resize orig: out of memory\n");
417 /* copy first part */
418 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
420 /* copy second part */
422 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
423 (max_if_num - del_if_num) * chunk_size);
426 kfree(orig_node->bcast_own);
427 orig_node->bcast_own = data_ptr;
432 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
435 "batman-adv:Can't resize orig: out of memory\n");
439 memcpy(data_ptr, orig_node->bcast_own_sum,
440 del_if_num * sizeof(uint8_t));
443 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
444 (max_if_num - del_if_num) * sizeof(uint8_t));
447 kfree(orig_node->bcast_own_sum);
448 orig_node->bcast_own_sum = data_ptr;
453 int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
455 struct batman_if *batman_if_tmp;
456 struct orig_node *orig_node;
460 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
462 spin_lock(&orig_hash_lock);
464 while (hash_iterate(orig_hash, &hashit)) {
465 orig_node = hashit.bucket->data;
467 ret = orig_node_del_if(orig_node, max_if_num,
474 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
476 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
477 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
480 if (batman_if == batman_if_tmp)
483 if (batman_if_tmp->if_num > batman_if->if_num)
484 batman_if_tmp->if_num--;
488 batman_if->if_num = -1;
489 spin_unlock(&orig_hash_lock);
493 spin_unlock(&orig_hash_lock);