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 neighbour of originator\n");
79 neigh_node = kmalloc(sizeof(struct neigh_node), GFP_ATOMIC);
80 memset(neigh_node, 0, sizeof(struct neigh_node));
81 INIT_LIST_HEAD(&neigh_node->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 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
91 void free_orig_node(void *data)
93 struct list_head *list_pos, *list_pos_tmp;
94 struct neigh_node *neigh_node;
95 struct orig_node *orig_node = (struct orig_node *)data;
97 /* for all neighbours towards this originator ... */
98 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
99 neigh_node = list_entry(list_pos, struct neigh_node, list);
105 hna_global_del_orig(orig_node, "originator timed out");
107 kfree(orig_node->bcast_own);
108 kfree(orig_node->bcast_own_sum);
112 /* this function finds or creates an originator entry for the given
113 * address if it does not exits */
114 struct orig_node *get_orig_node(uint8_t *addr)
116 struct orig_node *orig_node;
117 struct hashtable_t *swaphash;
118 char orig_str[ETH_STR_LEN];
121 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
123 if (orig_node != NULL)
126 addr_to_string(orig_str, addr);
127 bat_dbg(DBG_BATMAN, "Creating new originator: %s \n", orig_str);
129 orig_node = kmalloc(sizeof(struct orig_node), GFP_ATOMIC);
130 memset(orig_node, 0, sizeof(struct orig_node));
131 INIT_LIST_HEAD(&orig_node->neigh_list);
133 memcpy(orig_node->orig, addr, ETH_ALEN);
134 orig_node->router = NULL;
135 orig_node->batman_if = NULL;
136 orig_node->hna_buff = NULL;
138 size = num_ifs * sizeof(TYPE_OF_WORD) * NUM_WORDS;
140 orig_node->bcast_own = kmalloc(size, GFP_ATOMIC);
141 memset(orig_node->bcast_own, 0, size);
143 size = num_ifs * sizeof(uint8_t);
144 orig_node->bcast_own_sum = kmalloc(size, GFP_ATOMIC);
145 memset(orig_node->bcast_own_sum, 0, size);
147 hash_add(orig_hash, orig_node);
149 if (orig_hash->elements * 4 > orig_hash->size) {
150 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
152 if (swaphash == NULL)
154 "batman-adv:Couldn't resize orig hash table \n");
156 orig_hash = swaphash;
162 static bool purge_orig_neigbours(struct orig_node *orig_node,
163 struct neigh_node **best_neigh_node)
165 struct list_head *list_pos, *list_pos_tmp;
166 char neigh_str[ETH_STR_LEN], orig_str[ETH_STR_LEN];
167 struct neigh_node *neigh_node;
168 bool neigh_purged = false;
170 *best_neigh_node = NULL;
173 /* for all neighbours towards this originator ... */
174 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
175 neigh_node = list_entry(list_pos, struct neigh_node, list);
177 if (time_after(jiffies,
178 (neigh_node->last_valid +
179 ((PURGE_TIMEOUT * HZ) / 1000)))) {
181 addr_to_string(neigh_str, neigh_node->addr);
182 addr_to_string(orig_str, orig_node->orig);
183 bat_dbg(DBG_BATMAN, "Neighbour timeout: originator %s, neighbour: %s, last_valid %lu\n", orig_str, neigh_str, (neigh_node->last_valid / HZ));
189 if ((*best_neigh_node == NULL) ||
190 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
191 *best_neigh_node = neigh_node;
198 static bool purge_orig_node(struct orig_node *orig_node)
200 struct neigh_node *best_neigh_node;
201 char orig_str[ETH_STR_LEN];
203 addr_to_string(orig_str, orig_node->orig);
205 if (time_after(jiffies,
206 (orig_node->last_valid +
207 ((2 * PURGE_TIMEOUT * HZ) / 1000)))) {
210 "Originator timeout: originator %s, last_valid %lu\n",
211 orig_str, (orig_node->last_valid / HZ));
214 if (purge_orig_neigbours(orig_node, &best_neigh_node))
215 update_routes(orig_node, best_neigh_node,
217 orig_node->hna_buff_len);
222 void purge_orig(struct work_struct *work)
225 struct orig_node *orig_node;
227 spin_lock(&orig_hash_lock);
229 /* for all origins... */
230 while (hash_iterate(orig_hash, &hashit)) {
231 orig_node = hashit.bucket->data;
232 if (purge_orig_node(orig_node)) {
233 hash_remove_bucket(orig_hash, &hashit);
234 free_orig_node(orig_node);
238 spin_unlock(&orig_hash_lock);