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 bat_priv
*bat_priv
= netdev_priv(soft_device
);
64 struct neigh_node
*neigh_node
;
66 bat_dbg(DBG_BATMAN
, bat_priv
,
67 "Creating new last-hop neighbor of originator\n");
69 neigh_node
= kzalloc(sizeof(struct neigh_node
), GFP_ATOMIC
);
73 INIT_LIST_HEAD(&neigh_node
->list
);
75 memcpy(neigh_node
->addr
, neigh
, ETH_ALEN
);
76 neigh_node
->orig_node
= orig_neigh_node
;
77 neigh_node
->if_incoming
= if_incoming
;
79 list_add_tail(&neigh_node
->list
, &orig_node
->neigh_list
);
83 static void free_orig_node(void *data
)
85 struct list_head
*list_pos
, *list_pos_tmp
;
86 struct neigh_node
*neigh_node
;
87 struct orig_node
*orig_node
= (struct orig_node
*)data
;
89 /* for all neighbors towards this originator ... */
90 list_for_each_safe(list_pos
, list_pos_tmp
, &orig_node
->neigh_list
) {
91 neigh_node
= list_entry(list_pos
, struct neigh_node
, list
);
97 hna_global_del_orig(orig_node
, "originator timed out");
99 kfree(orig_node
->bcast_own
);
100 kfree(orig_node
->bcast_own_sum
);
104 void originator_free(void)
111 cancel_delayed_work_sync(&purge_orig_wq
);
113 spin_lock_irqsave(&orig_hash_lock
, flags
);
114 hash_delete(orig_hash
, free_orig_node
);
116 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
119 /* this function finds or creates an originator entry for the given
120 * address if it does not exits */
121 struct orig_node
*get_orig_node(uint8_t *addr
)
123 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
124 struct orig_node
*orig_node
;
125 struct hashtable_t
*swaphash
;
128 orig_node
= ((struct orig_node
*)hash_find(orig_hash
, addr
));
130 if (orig_node
!= NULL
)
133 bat_dbg(DBG_BATMAN
, bat_priv
,
134 "Creating new originator: %pM\n", addr
);
136 orig_node
= kzalloc(sizeof(struct orig_node
), GFP_ATOMIC
);
140 INIT_LIST_HEAD(&orig_node
->neigh_list
);
142 memcpy(orig_node
->orig
, addr
, ETH_ALEN
);
143 orig_node
->router
= NULL
;
144 orig_node
->hna_buff
= NULL
;
145 orig_node
->bcast_seqno_reset
= jiffies
- 1
146 - msecs_to_jiffies(RESET_PROTECTION_MS
);
147 orig_node
->batman_seqno_reset
= jiffies
- 1
148 - msecs_to_jiffies(RESET_PROTECTION_MS
);
150 size
= bat_priv
->num_ifaces
* sizeof(TYPE_OF_WORD
) * NUM_WORDS
;
152 orig_node
->bcast_own
= kzalloc(size
, GFP_ATOMIC
);
153 if (!orig_node
->bcast_own
)
156 size
= bat_priv
->num_ifaces
* sizeof(uint8_t);
157 orig_node
->bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
158 if (!orig_node
->bcast_own_sum
)
161 if (hash_add(orig_hash
, orig_node
) < 0)
162 goto free_bcast_own_sum
;
164 if (orig_hash
->elements
* 4 > orig_hash
->size
) {
165 swaphash
= hash_resize(orig_hash
, orig_hash
->size
* 2);
167 if (swaphash
== NULL
)
169 "Couldn't resize orig hash table\n");
171 orig_hash
= swaphash
;
176 kfree(orig_node
->bcast_own_sum
);
178 kfree(orig_node
->bcast_own
);
184 static bool purge_orig_neighbors(struct orig_node
*orig_node
,
185 struct neigh_node
**best_neigh_node
)
187 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
188 struct list_head
*list_pos
, *list_pos_tmp
;
189 struct neigh_node
*neigh_node
;
190 bool neigh_purged
= false;
192 *best_neigh_node
= NULL
;
194 /* for all neighbors towards this originator ... */
195 list_for_each_safe(list_pos
, list_pos_tmp
, &orig_node
->neigh_list
) {
196 neigh_node
= list_entry(list_pos
, struct neigh_node
, list
);
198 if ((time_after(jiffies
,
199 neigh_node
->last_valid
+ PURGE_TIMEOUT
* HZ
)) ||
200 (neigh_node
->if_incoming
->if_status
==
203 if (neigh_node
->if_incoming
->if_status
==
205 bat_dbg(DBG_BATMAN
, bat_priv
,
206 "neighbor purge: originator %pM, "
207 "neighbor: %pM, iface: %s\n",
208 orig_node
->orig
, neigh_node
->addr
,
209 neigh_node
->if_incoming
->dev
);
211 bat_dbg(DBG_BATMAN
, bat_priv
,
212 "neighbor timeout: originator %pM, "
213 "neighbor: %pM, last_valid: %lu\n",
214 orig_node
->orig
, neigh_node
->addr
,
215 (neigh_node
->last_valid
/ HZ
));
221 if ((*best_neigh_node
== NULL
) ||
222 (neigh_node
->tq_avg
> (*best_neigh_node
)->tq_avg
))
223 *best_neigh_node
= neigh_node
;
229 static bool purge_orig_node(struct orig_node
*orig_node
)
231 struct bat_priv
*bat_priv
= netdev_priv(soft_device
);
232 struct neigh_node
*best_neigh_node
;
234 if (time_after(jiffies
,
235 orig_node
->last_valid
+ 2 * PURGE_TIMEOUT
* HZ
)) {
237 bat_dbg(DBG_BATMAN
, bat_priv
,
238 "Originator timeout: originator %pM, last_valid %lu\n",
239 orig_node
->orig
, (orig_node
->last_valid
/ HZ
));
242 if (purge_orig_neighbors(orig_node
, &best_neigh_node
)) {
243 update_routes(orig_node
, best_neigh_node
,
245 orig_node
->hna_buff_len
);
246 /* update bonding candidates, we could have lost
247 * some candidates. */
248 update_bonding_candidates(bat_priv
, orig_node
);
255 void purge_orig(struct work_struct
*work
)
258 struct orig_node
*orig_node
;
261 spin_lock_irqsave(&orig_hash_lock
, flags
);
263 /* for all origins... */
264 while (hash_iterate(orig_hash
, &hashit
)) {
265 orig_node
= hashit
.bucket
->data
;
266 if (purge_orig_node(orig_node
)) {
267 hash_remove_bucket(orig_hash
, &hashit
);
268 free_orig_node(orig_node
);
272 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
274 /* if work == NULL we were not called by the timer
275 * and thus do not need to re-arm the timer */
280 int orig_seq_print_text(struct seq_file
*seq
, void *offset
)
283 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
284 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
285 struct orig_node
*orig_node
;
286 struct neigh_node
*neigh_node
;
287 int batman_count
= 0;
291 char orig_str
[ETH_STR_LEN
], router_str
[ETH_STR_LEN
];
293 if ((!bat_priv
->primary_if
) ||
294 (bat_priv
->primary_if
->if_status
!= IF_ACTIVE
)) {
295 if (!bat_priv
->primary_if
)
296 return seq_printf(seq
, "BATMAN mesh %s disabled - "
297 "please specify interfaces to enable it\n",
300 return seq_printf(seq
, "BATMAN mesh %s "
301 "disabled - primary interface not active\n",
306 seq_printf(seq
, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
307 SOURCE_VERSION
, REVISION_VERSION_STR
,
308 bat_priv
->primary_if
->dev
, bat_priv
->primary_if
->addr_str
,
310 seq_printf(seq
, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
311 "Originator", "last-seen", "#", TQ_MAX_VALUE
, "Nexthop",
312 "outgoingIF", "Potential nexthops");
315 spin_lock_irqsave(&orig_hash_lock
, flags
);
317 while (hash_iterate(orig_hash
, &hashit
)) {
319 orig_node
= hashit
.bucket
->data
;
321 if (!orig_node
->router
)
324 if (orig_node
->router
->tq_avg
== 0)
327 addr_to_string(orig_str
, orig_node
->orig
);
328 addr_to_string(router_str
, orig_node
->router
->addr
);
329 last_seen_secs
= jiffies_to_msecs(jiffies
-
330 orig_node
->last_valid
) / 1000;
331 last_seen_msecs
= jiffies_to_msecs(jiffies
-
332 orig_node
->last_valid
) % 1000;
334 seq_printf(seq
, "%-17s %4i.%03is (%3i) %17s [%10s]:",
335 orig_str
, last_seen_secs
, last_seen_msecs
,
336 orig_node
->router
->tq_avg
, router_str
,
337 orig_node
->router
->if_incoming
->dev
);
339 list_for_each_entry(neigh_node
, &orig_node
->neigh_list
, list
) {
340 addr_to_string(orig_str
, neigh_node
->addr
);
341 seq_printf(seq
, " %17s (%3i)", orig_str
,
345 seq_printf(seq
, "\n");
349 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
351 if ((batman_count
== 0))
352 seq_printf(seq
, "No batman nodes in range ...\n");
357 static int orig_node_add_if(struct orig_node
*orig_node
, int max_if_num
)
361 data_ptr
= kmalloc(max_if_num
* sizeof(TYPE_OF_WORD
) * NUM_WORDS
,
364 pr_err("Can't resize orig: out of memory\n");
368 memcpy(data_ptr
, orig_node
->bcast_own
,
369 (max_if_num
- 1) * sizeof(TYPE_OF_WORD
) * NUM_WORDS
);
370 kfree(orig_node
->bcast_own
);
371 orig_node
->bcast_own
= data_ptr
;
373 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
375 pr_err("Can't resize orig: out of memory\n");
379 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
380 (max_if_num
- 1) * sizeof(uint8_t));
381 kfree(orig_node
->bcast_own_sum
);
382 orig_node
->bcast_own_sum
= data_ptr
;
387 int orig_hash_add_if(struct batman_if
*batman_if
, int max_if_num
)
389 struct orig_node
*orig_node
;
393 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
395 spin_lock_irqsave(&orig_hash_lock
, flags
);
397 while (hash_iterate(orig_hash
, &hashit
)) {
398 orig_node
= hashit
.bucket
->data
;
400 if (orig_node_add_if(orig_node
, max_if_num
) == -1)
404 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
408 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
412 static int orig_node_del_if(struct orig_node
*orig_node
,
413 int max_if_num
, int del_if_num
)
415 void *data_ptr
= NULL
;
418 /* last interface was removed */
422 chunk_size
= sizeof(TYPE_OF_WORD
) * NUM_WORDS
;
423 data_ptr
= kmalloc(max_if_num
* chunk_size
, GFP_ATOMIC
);
425 pr_err("Can't resize orig: out of memory\n");
429 /* copy first part */
430 memcpy(data_ptr
, orig_node
->bcast_own
, del_if_num
* chunk_size
);
432 /* copy second part */
434 orig_node
->bcast_own
+ ((del_if_num
+ 1) * chunk_size
),
435 (max_if_num
- del_if_num
) * chunk_size
);
438 kfree(orig_node
->bcast_own
);
439 orig_node
->bcast_own
= data_ptr
;
444 data_ptr
= kmalloc(max_if_num
* sizeof(uint8_t), GFP_ATOMIC
);
446 pr_err("Can't resize orig: out of memory\n");
450 memcpy(data_ptr
, orig_node
->bcast_own_sum
,
451 del_if_num
* sizeof(uint8_t));
454 orig_node
->bcast_own_sum
+ ((del_if_num
+ 1) * sizeof(uint8_t)),
455 (max_if_num
- del_if_num
) * sizeof(uint8_t));
458 kfree(orig_node
->bcast_own_sum
);
459 orig_node
->bcast_own_sum
= data_ptr
;
464 int orig_hash_del_if(struct batman_if
*batman_if
, int max_if_num
)
466 struct batman_if
*batman_if_tmp
;
467 struct orig_node
*orig_node
;
472 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
474 spin_lock_irqsave(&orig_hash_lock
, flags
);
476 while (hash_iterate(orig_hash
, &hashit
)) {
477 orig_node
= hashit
.bucket
->data
;
479 ret
= orig_node_del_if(orig_node
, max_if_num
,
486 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
488 list_for_each_entry_rcu(batman_if_tmp
, &if_list
, list
) {
489 if (batman_if_tmp
->if_status
== IF_NOT_IN_USE
)
492 if (batman_if
== batman_if_tmp
)
495 if (batman_if_tmp
->if_num
> batman_if
->if_num
)
496 batman_if_tmp
->if_num
--;
500 batman_if
->if_num
= -1;
501 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
505 spin_unlock_irqrestore(&orig_hash_lock
, flags
);