2 * Copyright (C) 2009 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"
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);
59 void originator_free(void)
66 cancel_delayed_work_sync(&purge_orig_wq);
68 spin_lock_irqsave(&orig_hash_lock, flags);
69 hash_delete(orig_hash, free_orig_node);
71 spin_unlock_irqrestore(&orig_hash_lock, flags);
75 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
76 uint8_t *neigh, struct batman_if *if_incoming)
78 struct neigh_node *neigh_node;
80 bat_dbg(DBG_BATMAN, "Creating new last-hop neighbor of originator\n");
82 neigh_node = kmalloc(sizeof(struct neigh_node), GFP_ATOMIC);
86 memset(neigh_node, 0, sizeof(struct neigh_node));
87 INIT_LIST_HEAD(&neigh_node->list);
89 memcpy(neigh_node->addr, neigh, ETH_ALEN);
90 neigh_node->orig_node = orig_neigh_node;
91 neigh_node->if_incoming = if_incoming;
93 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
97 void free_orig_node(void *data)
99 struct list_head *list_pos, *list_pos_tmp;
100 struct neigh_node *neigh_node;
101 struct orig_node *orig_node = (struct orig_node *)data;
103 /* for all neighbors towards this originator ... */
104 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
105 neigh_node = list_entry(list_pos, struct neigh_node, list);
111 hna_global_del_orig(orig_node, "originator timed out");
113 kfree(orig_node->bcast_own);
114 kfree(orig_node->bcast_own_sum);
118 /* this function finds or creates an originator entry for the given
119 * address if it does not exits */
120 struct orig_node *get_orig_node(uint8_t *addr)
122 struct orig_node *orig_node;
123 struct hashtable_t *swaphash;
124 char orig_str[ETH_STR_LEN];
127 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
129 if (orig_node != NULL)
132 addr_to_string(orig_str, addr);
133 bat_dbg(DBG_BATMAN, "Creating new originator: %s \n", orig_str);
135 orig_node = kmalloc(sizeof(struct orig_node), GFP_ATOMIC);
139 memset(orig_node, 0, sizeof(struct orig_node));
140 INIT_LIST_HEAD(&orig_node->neigh_list);
142 memcpy(orig_node->orig, addr, ETH_ALEN);
143 orig_node->router = NULL;
144 orig_node->batman_if = NULL;
145 orig_node->hna_buff = NULL;
147 size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
149 orig_node->bcast_own = kmalloc(size, GFP_ATOMIC);
150 if (!orig_node->bcast_own)
153 memset(orig_node->bcast_own, 0, size);
155 size = num_ifs * sizeof(uint8_t);
156 orig_node->bcast_own_sum = kmalloc(size, GFP_ATOMIC);
157 if (!orig_node->bcast_own_sum)
160 memset(orig_node->bcast_own_sum, 0, size);
162 if (hash_add(orig_hash, orig_node) < 0)
163 goto free_bcast_own_sum;
165 if (orig_hash->elements * 4 > orig_hash->size) {
166 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
168 if (swaphash == NULL)
170 "batman-adv:Couldn't resize orig hash table \n");
172 orig_hash = swaphash;
177 kfree(orig_node->bcast_own_sum);
179 kfree(orig_node->bcast_own);
185 static bool purge_orig_neighbors(struct orig_node *orig_node,
186 struct neigh_node **best_neigh_node)
188 struct list_head *list_pos, *list_pos_tmp;
189 char neigh_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
190 struct neigh_node *neigh_node;
191 bool neigh_purged = false;
193 *best_neigh_node = NULL;
196 /* for all neighbors towards this originator ... */
197 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
198 neigh_node = list_entry(list_pos, struct neigh_node, list);
200 if (time_after(jiffies,
201 (neigh_node->last_valid +
202 ((PURGE_TIMEOUT * HZ) / 1000)))) {
204 addr_to_string(neigh_str, neigh_node->addr);
205 addr_to_string(orig_str, orig_node->orig);
206 bat_dbg(DBG_BATMAN, "neighbor timeout: originator %s, neighbor: %s, last_valid %lu\n", orig_str, neigh_str, (neigh_node->last_valid / HZ));
212 if ((*best_neigh_node == NULL) ||
213 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
214 *best_neigh_node = neigh_node;
221 static bool purge_orig_node(struct orig_node *orig_node)
223 struct neigh_node *best_neigh_node;
224 char orig_str[ETH_STR_LEN];
226 addr_to_string(orig_str, orig_node->orig);
228 if (time_after(jiffies,
229 (orig_node->last_valid +
230 ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
233 "Originator timeout: originator %s, last_valid %lu\n",
234 orig_str, (orig_node->last_valid / HZ));
237 if (purge_orig_neighbors(orig_node, &best_neigh_node))
238 update_routes(orig_node, best_neigh_node,
240 orig_node->hna_buff_len);
245 void purge_orig(struct work_struct *work)
248 struct orig_node *orig_node;
251 spin_lock_irqsave(&orig_hash_lock, flags);
253 /* for all origins... */
254 while (hash_iterate(orig_hash, &hashit)) {
255 orig_node = hashit.bucket->data;
256 if (purge_orig_node(orig_node)) {
257 hash_remove_bucket(orig_hash, &hashit);
258 free_orig_node(orig_node);
262 spin_unlock_irqrestore(&orig_hash_lock, flags);