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"
30 static DECLARE_DELAYED_WORK(purge_orig_wq
, purge_orig
);
32 static void start_purge_timer(void)
34 queue_delayed_work(bat_event_workqueue
, &purge_orig_wq
, 1 * HZ
);
37 int originator_init(void)
43 spin_lock_irqsave(&orig_hash_lock
, flags
);
44 orig_hash
= hash_new(128, compare_orig
, choose_orig
);
49 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
54 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
58 void originator_free(void)
65 cancel_delayed_work_sync(&purge_orig_wq
);
67 spin_lock_irqsave(&orig_hash_lock
, flags
);
68 hash_delete(orig_hash
, free_orig_node
);
70 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
74 create_neighbor(struct orig_node
*orig_node
, struct orig_node
*orig_neigh_node
,
75 uint8_t *neigh
, struct batman_if
*if_incoming
)
77 struct neigh_node
*neigh_node
;
79 bat_dbg(DBG_BATMAN
, "Creating new last-hop neighbor of originator\n");
81 neigh_node
= kzalloc(sizeof(struct neigh_node
), GFP_ATOMIC
);
85 INIT_LIST_HEAD(&neigh_node
->list
);
87 memcpy(neigh_node
->addr
, neigh
, ETH_ALEN
);
88 neigh_node
->orig_node
= orig_neigh_node
;
89 neigh_node
->if_incoming
= if_incoming
;
91 list_add_tail(&neigh_node
->list
, &orig_node
->neigh_list
);
95 void free_orig_node(void *data
)
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
;
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
);
109 hna_global_del_orig(orig_node
, "originator timed out");
111 kfree(orig_node
->bcast_own
);
112 kfree(orig_node
->bcast_own_sum
);
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
)
120 struct orig_node
*orig_node
;
121 struct hashtable_t
*swaphash
;
124 orig_node
= ((struct orig_node
*)hash_find(orig_hash
, addr
));
126 if (orig_node
!= NULL
)
129 bat_dbg(DBG_BATMAN
, "Creating new originator: %pM \n", addr
);
131 orig_node
= kzalloc(sizeof(struct orig_node
), GFP_ATOMIC
);
135 INIT_LIST_HEAD(&orig_node
->neigh_list
);
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
;
142 size
= num_ifs
* sizeof(TYPE_OF_WORD
) * NUM_WORDS
;
144 orig_node
->bcast_own
= kzalloc(size
, GFP_ATOMIC
);
145 if (!orig_node
->bcast_own
)
148 size
= num_ifs
* sizeof(uint8_t);
149 orig_node
->bcast_own_sum
= kzalloc(size
, GFP_ATOMIC
);
150 if (!orig_node
->bcast_own_sum
)
153 if (hash_add(orig_hash
, orig_node
) < 0)
154 goto free_bcast_own_sum
;
156 if (orig_hash
->elements
* 4 > orig_hash
->size
) {
157 swaphash
= hash_resize(orig_hash
, orig_hash
->size
* 2);
159 if (swaphash
== NULL
)
161 "batman-adv:Couldn't resize orig hash table \n");
163 orig_hash
= swaphash
;
168 kfree(orig_node
->bcast_own_sum
);
170 kfree(orig_node
->bcast_own
);
176 static bool purge_orig_neighbors(struct orig_node
*orig_node
,
177 struct neigh_node
**best_neigh_node
)
179 struct list_head
*list_pos
, *list_pos_tmp
;
180 struct neigh_node
*neigh_node
;
181 bool neigh_purged
= false;
183 *best_neigh_node
= NULL
;
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
);
190 if (time_after(jiffies
,
191 (neigh_node
->last_valid
+
192 ((PURGE_TIMEOUT
* HZ
) / 1000)))) {
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
));
200 if ((*best_neigh_node
== NULL
) ||
201 (neigh_node
->tq_avg
> (*best_neigh_node
)->tq_avg
))
202 *best_neigh_node
= neigh_node
;
209 static bool purge_orig_node(struct orig_node
*orig_node
)
211 struct neigh_node
*best_neigh_node
;
213 if (time_after(jiffies
,
214 (orig_node
->last_valid
+
215 ((2 * PURGE_TIMEOUT
* HZ
) / 1000)))) {
218 "Originator timeout: originator %pM, last_valid %lu\n",
219 orig_node
->orig
, (orig_node
->last_valid
/ HZ
));
222 if (purge_orig_neighbors(orig_node
, &best_neigh_node
))
223 update_routes(orig_node
, best_neigh_node
,
225 orig_node
->hna_buff_len
);
230 void purge_orig(struct work_struct
*work
)
233 struct orig_node
*orig_node
;
236 spin_lock_irqsave(&orig_hash_lock
, flags
);
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
);
247 spin_unlock_irqrestore(&orig_hash_lock
, flags
);