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)
43 spin_lock(&orig_hash_lock);
44 orig_hash = hash_new(128, compare_orig, choose_orig);
49 spin_unlock(&orig_hash_lock);
54 spin_unlock(&orig_hash_lock);
58 void originator_free(void)
63 cancel_delayed_work_sync(&purge_orig_wq);
65 spin_lock(&orig_hash_lock);
66 hash_delete(orig_hash, free_orig_node);
68 spin_unlock(&orig_hash_lock);
72 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
73 uint8_t *neigh, struct batman_if *if_incoming)
75 struct neigh_node *neigh_node;
77 bat_dbg(DBG_BATMAN, "Creating new last-hop neighbor of originator\n");
79 neigh_node = kmalloc(sizeof(struct neigh_node), GFP_ATOMIC);
83 memset(neigh_node, 0, sizeof(struct neigh_node));
84 INIT_LIST_HEAD(&neigh_node->list);
86 memcpy(neigh_node->addr, neigh, ETH_ALEN);
87 neigh_node->orig_node = orig_neigh_node;
88 neigh_node->if_incoming = if_incoming;
90 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
94 void free_orig_node(void *data)
96 struct list_head *list_pos, *list_pos_tmp;
97 struct neigh_node *neigh_node;
98 struct orig_node *orig_node = (struct orig_node *)data;
100 /* for all neighbors towards this originator ... */
101 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
102 neigh_node = list_entry(list_pos, struct neigh_node, list);
108 hna_global_del_orig(orig_node, "originator timed out");
110 kfree(orig_node->bcast_own);
111 kfree(orig_node->bcast_own_sum);
115 /* this function finds or creates an originator entry for the given
116 * address if it does not exits */
117 struct orig_node *get_orig_node(uint8_t *addr)
119 struct orig_node *orig_node;
120 struct hashtable_t *swaphash;
121 char orig_str[ETH_STR_LEN];
124 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
126 if (orig_node != NULL)
129 addr_to_string(orig_str, addr);
130 bat_dbg(DBG_BATMAN, "Creating new originator: %s \n", orig_str);
132 orig_node = kmalloc(sizeof(struct orig_node), GFP_ATOMIC);
136 memset(orig_node, 0, sizeof(struct orig_node));
137 INIT_LIST_HEAD(&orig_node->neigh_list);
139 memcpy(orig_node->orig, addr, ETH_ALEN);
140 orig_node->router = NULL;
141 orig_node->batman_if = NULL;
142 orig_node->hna_buff = NULL;
144 size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
146 orig_node->bcast_own = kmalloc(size, GFP_ATOMIC);
147 if (!orig_node->bcast_own)
150 memset(orig_node->bcast_own, 0, size);
152 size = num_ifs * sizeof(uint8_t);
153 orig_node->bcast_own_sum = kmalloc(size, GFP_ATOMIC);
154 if (!orig_node->bcast_own_sum)
157 memset(orig_node->bcast_own_sum, 0, size);
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 char neigh_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
187 struct neigh_node *neigh_node;
188 bool neigh_purged = false;
190 *best_neigh_node = NULL;
193 /* for all neighbors towards this originator ... */
194 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
195 neigh_node = list_entry(list_pos, struct neigh_node, list);
197 if (time_after(jiffies,
198 (neigh_node->last_valid +
199 ((PURGE_TIMEOUT * HZ) / 1000)))) {
201 addr_to_string(neigh_str, neigh_node->addr);
202 addr_to_string(orig_str, orig_node->orig);
203 bat_dbg(DBG_BATMAN, "neighbor timeout: originator %s, neighbor: %s, last_valid %lu\n", orig_str, neigh_str, (neigh_node->last_valid / HZ));
209 if ((*best_neigh_node == NULL) ||
210 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
211 *best_neigh_node = neigh_node;
218 static bool purge_orig_node(struct orig_node *orig_node)
220 struct neigh_node *best_neigh_node;
221 char orig_str[ETH_STR_LEN];
223 addr_to_string(orig_str, orig_node->orig);
225 if (time_after(jiffies,
226 (orig_node->last_valid +
227 ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
230 "Originator timeout: originator %s, last_valid %lu\n",
231 orig_str, (orig_node->last_valid / HZ));
234 if (purge_orig_neighbors(orig_node, &best_neigh_node))
235 update_routes(orig_node, best_neigh_node,
237 orig_node->hna_buff_len);
242 void purge_orig(struct work_struct *work)
245 struct orig_node *orig_node;
247 spin_lock(&orig_hash_lock);
249 /* for all origins... */
250 while (hash_iterate(orig_hash, &hashit)) {
251 orig_node = hashit.bucket->data;
252 if (purge_orig_node(orig_node)) {
253 hash_remove_bucket(orig_hash, &hashit);
254 free_orig_node(orig_node);
258 spin_unlock(&orig_hash_lock);