batman,rcu: convert call_rcu(neigh_node_free_rcu) to kfree()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / batman-adv / originator.c
blobed23a5895d6c416ea12e0c5bd87ee39859be0ed1
1 /*
2 * Copyright (C) 2009-2011 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
18 * 02110-1301, USA
22 /* increase the reference counter for this originator */
24 #include "main.h"
25 #include "originator.h"
26 #include "hash.h"
27 #include "translation-table.h"
28 #include "routing.h"
29 #include "gateway_client.h"
30 #include "hard-interface.h"
31 #include "unicast.h"
32 #include "soft-interface.h"
34 static void purge_orig(struct work_struct *work);
36 static void start_purge_timer(struct bat_priv *bat_priv)
38 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
39 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
42 int originator_init(struct bat_priv *bat_priv)
44 if (bat_priv->orig_hash)
45 return 1;
47 bat_priv->orig_hash = hash_new(1024);
49 if (!bat_priv->orig_hash)
50 goto err;
52 start_purge_timer(bat_priv);
53 return 1;
55 err:
56 return 0;
59 void neigh_node_free_ref(struct neigh_node *neigh_node)
61 if (atomic_dec_and_test(&neigh_node->refcount))
62 kfree_rcu(neigh_node, rcu);
65 struct neigh_node *create_neighbor(struct orig_node *orig_node,
66 struct orig_node *orig_neigh_node,
67 uint8_t *neigh,
68 struct hard_iface *if_incoming)
70 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
71 struct neigh_node *neigh_node;
73 bat_dbg(DBG_BATMAN, bat_priv,
74 "Creating new last-hop neighbor of originator\n");
76 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
77 if (!neigh_node)
78 return NULL;
80 INIT_HLIST_NODE(&neigh_node->list);
81 INIT_LIST_HEAD(&neigh_node->bonding_list);
83 memcpy(neigh_node->addr, neigh, ETH_ALEN);
84 neigh_node->orig_node = orig_neigh_node;
85 neigh_node->if_incoming = if_incoming;
87 /* extra reference for return */
88 atomic_set(&neigh_node->refcount, 2);
90 spin_lock_bh(&orig_node->neigh_list_lock);
91 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
92 spin_unlock_bh(&orig_node->neigh_list_lock);
93 return neigh_node;
96 static void orig_node_free_rcu(struct rcu_head *rcu)
98 struct hlist_node *node, *node_tmp;
99 struct neigh_node *neigh_node, *tmp_neigh_node;
100 struct orig_node *orig_node;
102 orig_node = container_of(rcu, struct orig_node, rcu);
104 spin_lock_bh(&orig_node->neigh_list_lock);
106 /* for all bonding members ... */
107 list_for_each_entry_safe(neigh_node, tmp_neigh_node,
108 &orig_node->bond_list, bonding_list) {
109 list_del_rcu(&neigh_node->bonding_list);
110 neigh_node_free_ref(neigh_node);
113 /* for all neighbors towards this originator ... */
114 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
115 &orig_node->neigh_list, list) {
116 hlist_del_rcu(&neigh_node->list);
117 neigh_node_free_ref(neigh_node);
120 spin_unlock_bh(&orig_node->neigh_list_lock);
122 frag_list_free(&orig_node->frag_list);
123 hna_global_del_orig(orig_node->bat_priv, orig_node,
124 "originator timed out");
126 kfree(orig_node->bcast_own);
127 kfree(orig_node->bcast_own_sum);
128 kfree(orig_node);
131 void orig_node_free_ref(struct orig_node *orig_node)
133 if (atomic_dec_and_test(&orig_node->refcount))
134 call_rcu(&orig_node->rcu, orig_node_free_rcu);
137 void originator_free(struct bat_priv *bat_priv)
139 struct hashtable_t *hash = bat_priv->orig_hash;
140 struct hlist_node *node, *node_tmp;
141 struct hlist_head *head;
142 spinlock_t *list_lock; /* spinlock to protect write access */
143 struct orig_node *orig_node;
144 int i;
146 if (!hash)
147 return;
149 cancel_delayed_work_sync(&bat_priv->orig_work);
151 bat_priv->orig_hash = NULL;
153 for (i = 0; i < hash->size; i++) {
154 head = &hash->table[i];
155 list_lock = &hash->list_locks[i];
157 spin_lock_bh(list_lock);
158 hlist_for_each_entry_safe(orig_node, node, node_tmp,
159 head, hash_entry) {
161 hlist_del_rcu(node);
162 orig_node_free_ref(orig_node);
164 spin_unlock_bh(list_lock);
167 hash_destroy(hash);
170 /* this function finds or creates an originator entry for the given
171 * address if it does not exits */
172 struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
174 struct orig_node *orig_node;
175 int size;
176 int hash_added;
178 orig_node = orig_hash_find(bat_priv, addr);
179 if (orig_node)
180 return orig_node;
182 bat_dbg(DBG_BATMAN, bat_priv,
183 "Creating new originator: %pM\n", addr);
185 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
186 if (!orig_node)
187 return NULL;
189 INIT_HLIST_HEAD(&orig_node->neigh_list);
190 INIT_LIST_HEAD(&orig_node->bond_list);
191 spin_lock_init(&orig_node->ogm_cnt_lock);
192 spin_lock_init(&orig_node->bcast_seqno_lock);
193 spin_lock_init(&orig_node->neigh_list_lock);
195 /* extra reference for return */
196 atomic_set(&orig_node->refcount, 2);
198 orig_node->bat_priv = bat_priv;
199 memcpy(orig_node->orig, addr, ETH_ALEN);
200 orig_node->router = NULL;
201 orig_node->hna_buff = NULL;
202 orig_node->bcast_seqno_reset = jiffies - 1
203 - msecs_to_jiffies(RESET_PROTECTION_MS);
204 orig_node->batman_seqno_reset = jiffies - 1
205 - msecs_to_jiffies(RESET_PROTECTION_MS);
207 atomic_set(&orig_node->bond_candidates, 0);
209 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
211 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
212 if (!orig_node->bcast_own)
213 goto free_orig_node;
215 size = bat_priv->num_ifaces * sizeof(uint8_t);
216 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
218 INIT_LIST_HEAD(&orig_node->frag_list);
219 orig_node->last_frag_packet = 0;
221 if (!orig_node->bcast_own_sum)
222 goto free_bcast_own;
224 hash_added = hash_add(bat_priv->orig_hash, compare_orig,
225 choose_orig, orig_node, &orig_node->hash_entry);
226 if (hash_added < 0)
227 goto free_bcast_own_sum;
229 return orig_node;
230 free_bcast_own_sum:
231 kfree(orig_node->bcast_own_sum);
232 free_bcast_own:
233 kfree(orig_node->bcast_own);
234 free_orig_node:
235 kfree(orig_node);
236 return NULL;
239 static bool purge_orig_neighbors(struct bat_priv *bat_priv,
240 struct orig_node *orig_node,
241 struct neigh_node **best_neigh_node)
243 struct hlist_node *node, *node_tmp;
244 struct neigh_node *neigh_node;
245 bool neigh_purged = false;
247 *best_neigh_node = NULL;
249 spin_lock_bh(&orig_node->neigh_list_lock);
251 /* for all neighbors towards this originator ... */
252 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
253 &orig_node->neigh_list, list) {
255 if ((time_after(jiffies,
256 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
257 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
258 (neigh_node->if_incoming->if_status == IF_NOT_IN_USE) ||
259 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
261 if ((neigh_node->if_incoming->if_status ==
262 IF_INACTIVE) ||
263 (neigh_node->if_incoming->if_status ==
264 IF_NOT_IN_USE) ||
265 (neigh_node->if_incoming->if_status ==
266 IF_TO_BE_REMOVED))
267 bat_dbg(DBG_BATMAN, bat_priv,
268 "neighbor purge: originator %pM, "
269 "neighbor: %pM, iface: %s\n",
270 orig_node->orig, neigh_node->addr,
271 neigh_node->if_incoming->net_dev->name);
272 else
273 bat_dbg(DBG_BATMAN, bat_priv,
274 "neighbor timeout: originator %pM, "
275 "neighbor: %pM, last_valid: %lu\n",
276 orig_node->orig, neigh_node->addr,
277 (neigh_node->last_valid / HZ));
279 neigh_purged = true;
281 hlist_del_rcu(&neigh_node->list);
282 bonding_candidate_del(orig_node, neigh_node);
283 neigh_node_free_ref(neigh_node);
284 } else {
285 if ((!*best_neigh_node) ||
286 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
287 *best_neigh_node = neigh_node;
291 spin_unlock_bh(&orig_node->neigh_list_lock);
292 return neigh_purged;
295 static bool purge_orig_node(struct bat_priv *bat_priv,
296 struct orig_node *orig_node)
298 struct neigh_node *best_neigh_node;
300 if (time_after(jiffies,
301 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
303 bat_dbg(DBG_BATMAN, bat_priv,
304 "Originator timeout: originator %pM, last_valid %lu\n",
305 orig_node->orig, (orig_node->last_valid / HZ));
306 return true;
307 } else {
308 if (purge_orig_neighbors(bat_priv, orig_node,
309 &best_neigh_node)) {
310 update_routes(bat_priv, orig_node,
311 best_neigh_node,
312 orig_node->hna_buff,
313 orig_node->hna_buff_len);
317 return false;
320 static void _purge_orig(struct bat_priv *bat_priv)
322 struct hashtable_t *hash = bat_priv->orig_hash;
323 struct hlist_node *node, *node_tmp;
324 struct hlist_head *head;
325 spinlock_t *list_lock; /* spinlock to protect write access */
326 struct orig_node *orig_node;
327 int i;
329 if (!hash)
330 return;
332 /* for all origins... */
333 for (i = 0; i < hash->size; i++) {
334 head = &hash->table[i];
335 list_lock = &hash->list_locks[i];
337 spin_lock_bh(list_lock);
338 hlist_for_each_entry_safe(orig_node, node, node_tmp,
339 head, hash_entry) {
340 if (purge_orig_node(bat_priv, orig_node)) {
341 if (orig_node->gw_flags)
342 gw_node_delete(bat_priv, orig_node);
343 hlist_del_rcu(node);
344 orig_node_free_ref(orig_node);
345 continue;
348 if (time_after(jiffies, orig_node->last_frag_packet +
349 msecs_to_jiffies(FRAG_TIMEOUT)))
350 frag_list_free(&orig_node->frag_list);
352 spin_unlock_bh(list_lock);
355 gw_node_purge(bat_priv);
356 gw_election(bat_priv);
358 softif_neigh_purge(bat_priv);
361 static void purge_orig(struct work_struct *work)
363 struct delayed_work *delayed_work =
364 container_of(work, struct delayed_work, work);
365 struct bat_priv *bat_priv =
366 container_of(delayed_work, struct bat_priv, orig_work);
368 _purge_orig(bat_priv);
369 start_purge_timer(bat_priv);
372 void purge_orig_ref(struct bat_priv *bat_priv)
374 _purge_orig(bat_priv);
377 int orig_seq_print_text(struct seq_file *seq, void *offset)
379 struct net_device *net_dev = (struct net_device *)seq->private;
380 struct bat_priv *bat_priv = netdev_priv(net_dev);
381 struct hashtable_t *hash = bat_priv->orig_hash;
382 struct hlist_node *node, *node_tmp;
383 struct hlist_head *head;
384 struct orig_node *orig_node;
385 struct neigh_node *neigh_node;
386 int batman_count = 0;
387 int last_seen_secs;
388 int last_seen_msecs;
389 int i;
391 if ((!bat_priv->primary_if) ||
392 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
393 if (!bat_priv->primary_if)
394 return seq_printf(seq, "BATMAN mesh %s disabled - "
395 "please specify interfaces to enable it\n",
396 net_dev->name);
398 return seq_printf(seq, "BATMAN mesh %s "
399 "disabled - primary interface not active\n",
400 net_dev->name);
403 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
404 SOURCE_VERSION, REVISION_VERSION_STR,
405 bat_priv->primary_if->net_dev->name,
406 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
407 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
408 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
409 "outgoingIF", "Potential nexthops");
411 for (i = 0; i < hash->size; i++) {
412 head = &hash->table[i];
414 rcu_read_lock();
415 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
416 if (!orig_node->router)
417 continue;
419 if (orig_node->router->tq_avg == 0)
420 continue;
422 last_seen_secs = jiffies_to_msecs(jiffies -
423 orig_node->last_valid) / 1000;
424 last_seen_msecs = jiffies_to_msecs(jiffies -
425 orig_node->last_valid) % 1000;
427 neigh_node = orig_node->router;
428 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
429 orig_node->orig, last_seen_secs,
430 last_seen_msecs, neigh_node->tq_avg,
431 neigh_node->addr,
432 neigh_node->if_incoming->net_dev->name);
434 hlist_for_each_entry_rcu(neigh_node, node_tmp,
435 &orig_node->neigh_list, list) {
436 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
437 neigh_node->tq_avg);
440 seq_printf(seq, "\n");
441 batman_count++;
443 rcu_read_unlock();
446 if ((batman_count == 0))
447 seq_printf(seq, "No batman nodes in range ...\n");
449 return 0;
452 static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
454 void *data_ptr;
456 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
457 GFP_ATOMIC);
458 if (!data_ptr) {
459 pr_err("Can't resize orig: out of memory\n");
460 return -1;
463 memcpy(data_ptr, orig_node->bcast_own,
464 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
465 kfree(orig_node->bcast_own);
466 orig_node->bcast_own = data_ptr;
468 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
469 if (!data_ptr) {
470 pr_err("Can't resize orig: out of memory\n");
471 return -1;
474 memcpy(data_ptr, orig_node->bcast_own_sum,
475 (max_if_num - 1) * sizeof(uint8_t));
476 kfree(orig_node->bcast_own_sum);
477 orig_node->bcast_own_sum = data_ptr;
479 return 0;
482 int orig_hash_add_if(struct hard_iface *hard_iface, int max_if_num)
484 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
485 struct hashtable_t *hash = bat_priv->orig_hash;
486 struct hlist_node *node;
487 struct hlist_head *head;
488 struct orig_node *orig_node;
489 int i, ret;
491 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
492 * if_num */
493 for (i = 0; i < hash->size; i++) {
494 head = &hash->table[i];
496 rcu_read_lock();
497 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
498 spin_lock_bh(&orig_node->ogm_cnt_lock);
499 ret = orig_node_add_if(orig_node, max_if_num);
500 spin_unlock_bh(&orig_node->ogm_cnt_lock);
502 if (ret == -1)
503 goto err;
505 rcu_read_unlock();
508 return 0;
510 err:
511 rcu_read_unlock();
512 return -ENOMEM;
515 static int orig_node_del_if(struct orig_node *orig_node,
516 int max_if_num, int del_if_num)
518 void *data_ptr = NULL;
519 int chunk_size;
521 /* last interface was removed */
522 if (max_if_num == 0)
523 goto free_bcast_own;
525 chunk_size = sizeof(unsigned long) * NUM_WORDS;
526 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
527 if (!data_ptr) {
528 pr_err("Can't resize orig: out of memory\n");
529 return -1;
532 /* copy first part */
533 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
535 /* copy second part */
536 memcpy(data_ptr + del_if_num * chunk_size,
537 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
538 (max_if_num - del_if_num) * chunk_size);
540 free_bcast_own:
541 kfree(orig_node->bcast_own);
542 orig_node->bcast_own = data_ptr;
544 if (max_if_num == 0)
545 goto free_own_sum;
547 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
548 if (!data_ptr) {
549 pr_err("Can't resize orig: out of memory\n");
550 return -1;
553 memcpy(data_ptr, orig_node->bcast_own_sum,
554 del_if_num * sizeof(uint8_t));
556 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
557 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
558 (max_if_num - del_if_num) * sizeof(uint8_t));
560 free_own_sum:
561 kfree(orig_node->bcast_own_sum);
562 orig_node->bcast_own_sum = data_ptr;
564 return 0;
567 int orig_hash_del_if(struct hard_iface *hard_iface, int max_if_num)
569 struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
570 struct hashtable_t *hash = bat_priv->orig_hash;
571 struct hlist_node *node;
572 struct hlist_head *head;
573 struct hard_iface *hard_iface_tmp;
574 struct orig_node *orig_node;
575 int i, ret;
577 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
578 * if_num */
579 for (i = 0; i < hash->size; i++) {
580 head = &hash->table[i];
582 rcu_read_lock();
583 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
584 spin_lock_bh(&orig_node->ogm_cnt_lock);
585 ret = orig_node_del_if(orig_node, max_if_num,
586 hard_iface->if_num);
587 spin_unlock_bh(&orig_node->ogm_cnt_lock);
589 if (ret == -1)
590 goto err;
592 rcu_read_unlock();
595 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
596 rcu_read_lock();
597 list_for_each_entry_rcu(hard_iface_tmp, &hardif_list, list) {
598 if (hard_iface_tmp->if_status == IF_NOT_IN_USE)
599 continue;
601 if (hard_iface == hard_iface_tmp)
602 continue;
604 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
605 continue;
607 if (hard_iface_tmp->if_num > hard_iface->if_num)
608 hard_iface_tmp->if_num--;
610 rcu_read_unlock();
612 hard_iface->if_num = -1;
613 return 0;
615 err:
616 rcu_read_unlock();
617 return -ENOMEM;