Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[pandora-kernel.git] / drivers / staging / batman-adv / originator.c
1 /*
2  * Copyright (C) 2009 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
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.
9  *
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.
14  *
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
19  *
20  */
21
22 /* increase the reference counter for this originator */
23
24 #include "main.h"
25 #include "originator.h"
26 #include "hash.h"
27 #include "translation-table.h"
28 #include "routing.h"
29
30 static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
31
32 static void start_purge_timer(void)
33 {
34         queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
35 }
36
37 int originator_init(void)
38 {
39         unsigned long flags;
40         if (orig_hash)
41                 return 1;
42
43         spin_lock_irqsave(&orig_hash_lock, flags);
44         orig_hash = hash_new(128, compare_orig, choose_orig);
45
46         if (!orig_hash)
47                 goto err;
48
49         spin_unlock_irqrestore(&orig_hash_lock, flags);
50         start_purge_timer();
51         return 1;
52
53 err:
54         spin_unlock_irqrestore(&orig_hash_lock, flags);
55         return 0;
56 }
57
58 void originator_free(void)
59 {
60         unsigned long flags;
61
62         if (!orig_hash)
63                 return;
64
65         cancel_delayed_work_sync(&purge_orig_wq);
66
67         spin_lock_irqsave(&orig_hash_lock, flags);
68         hash_delete(orig_hash, free_orig_node);
69         orig_hash = NULL;
70         spin_unlock_irqrestore(&orig_hash_lock, flags);
71 }
72
73 struct neigh_node *
74 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
75                 uint8_t *neigh, struct batman_if *if_incoming)
76 {
77         struct neigh_node *neigh_node;
78
79         bat_dbg(DBG_BATMAN, "Creating new last-hop neighbor of originator\n");
80
81         neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
82         if (!neigh_node)
83                 return NULL;
84
85         INIT_LIST_HEAD(&neigh_node->list);
86
87         memcpy(neigh_node->addr, neigh, ETH_ALEN);
88         neigh_node->orig_node = orig_neigh_node;
89         neigh_node->if_incoming = if_incoming;
90
91         list_add_tail(&neigh_node->list, &orig_node->neigh_list);
92         return neigh_node;
93 }
94
95 void free_orig_node(void *data)
96 {
97         struct list_head *list_pos, *list_pos_tmp;
98         struct neigh_node *neigh_node;
99         struct orig_node *orig_node = (struct orig_node *)data;
100
101         /* for all neighbors towards this originator ... */
102         list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
103                 neigh_node = list_entry(list_pos, struct neigh_node, list);
104
105                 list_del(list_pos);
106                 kfree(neigh_node);
107         }
108
109         hna_global_del_orig(orig_node, "originator timed out");
110
111         kfree(orig_node->bcast_own);
112         kfree(orig_node->bcast_own_sum);
113         kfree(orig_node);
114 }
115
116 /* this function finds or creates an originator entry for the given
117  * address if it does not exits */
118 struct orig_node *get_orig_node(uint8_t *addr)
119 {
120         struct orig_node *orig_node;
121         struct hashtable_t *swaphash;
122         int size;
123
124         orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
125
126         if (orig_node != NULL)
127                 return orig_node;
128
129         bat_dbg(DBG_BATMAN, "Creating new originator: %pM \n", addr);
130
131         orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
132         if (!orig_node)
133                 return NULL;
134
135         INIT_LIST_HEAD(&orig_node->neigh_list);
136
137         memcpy(orig_node->orig, addr, ETH_ALEN);
138         orig_node->router = NULL;
139         orig_node->batman_if = NULL;
140         orig_node->hna_buff = NULL;
141
142         size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
143
144         orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
145         if (!orig_node->bcast_own)
146                 goto free_orig_node;
147
148         size = num_ifs * sizeof(uint8_t);
149         orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
150         if (!orig_node->bcast_own_sum)
151                 goto free_bcast_own;
152
153         if (hash_add(orig_hash, orig_node) < 0)
154                 goto free_bcast_own_sum;
155
156         if (orig_hash->elements * 4 > orig_hash->size) {
157                 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
158
159                 if (swaphash == NULL)
160                         printk(KERN_ERR
161                                "batman-adv:Couldn't resize orig hash table \n");
162                 else
163                         orig_hash = swaphash;
164         }
165
166         return orig_node;
167 free_bcast_own_sum:
168         kfree(orig_node->bcast_own_sum);
169 free_bcast_own:
170         kfree(orig_node->bcast_own);
171 free_orig_node:
172         kfree(orig_node);
173         return NULL;
174 }
175
176 static bool purge_orig_neighbors(struct orig_node *orig_node,
177                                  struct neigh_node **best_neigh_node)
178 {
179         struct list_head *list_pos, *list_pos_tmp;
180         struct neigh_node *neigh_node;
181         bool neigh_purged = false;
182
183         *best_neigh_node = NULL;
184
185
186         /* for all neighbors towards this originator ... */
187         list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
188                 neigh_node = list_entry(list_pos, struct neigh_node, list);
189
190                 if (time_after(jiffies,
191                                (neigh_node->last_valid +
192                                 ((PURGE_TIMEOUT * HZ) / 1000)))) {
193
194                         bat_dbg(DBG_BATMAN, "neighbor timeout: originator %pM, neighbor: %pM, last_valid %lu\n", orig_node->orig, neigh_node->addr, (neigh_node->last_valid / HZ));
195
196                         neigh_purged = true;
197                         list_del(list_pos);
198                         kfree(neigh_node);
199                 } else {
200                         if ((*best_neigh_node == NULL) ||
201                             (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
202                                 *best_neigh_node = neigh_node;
203                 }
204         }
205         return neigh_purged;
206 }
207
208
209 static bool purge_orig_node(struct orig_node *orig_node)
210 {
211         struct neigh_node *best_neigh_node;
212
213         if (time_after(jiffies,
214                        (orig_node->last_valid +
215                         ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
216
217                 bat_dbg(DBG_BATMAN,
218                         "Originator timeout: originator %pM, last_valid %lu\n",
219                         orig_node->orig, (orig_node->last_valid / HZ));
220                 return true;
221         } else {
222                 if (purge_orig_neighbors(orig_node, &best_neigh_node))
223                         update_routes(orig_node, best_neigh_node,
224                                       orig_node->hna_buff,
225                                       orig_node->hna_buff_len);
226         }
227         return false;
228 }
229
230 void purge_orig(struct work_struct *work)
231 {
232         HASHIT(hashit);
233         struct orig_node *orig_node;
234         unsigned long flags;
235
236         spin_lock_irqsave(&orig_hash_lock, flags);
237
238         /* for all origins... */
239         while (hash_iterate(orig_hash, &hashit)) {
240                 orig_node = hashit.bucket->data;
241                 if (purge_orig_node(orig_node)) {
242                         hash_remove_bucket(orig_hash, &hashit);
243                         free_orig_node(orig_node);
244                 }
245         }
246
247         spin_unlock_irqrestore(&orig_hash_lock, flags);
248
249         start_purge_timer();
250 }
251
252