086da871cd59e3df79df2a06ac3dcf0d4f6b9efa
2 * Copyright (C) 2008-2009 B.A.T.M.A.N. contributors:
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
24 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
31 struct hashtable_t
*vis_hash
;
32 DEFINE_SPINLOCK(vis_hash_lock
);
33 static struct vis_info
*my_vis_info
;
34 static struct list_head send_list
; /* always locked with vis_hash_lock */
36 static void start_vis_timer(void);
39 static void free_info(void *data
)
41 struct vis_info
*info
= data
;
42 struct recvlist_node
*entry
, *tmp
;
44 list_del_init(&info
->send_list
);
45 list_for_each_entry_safe(entry
, tmp
, &info
->recv_list
, list
) {
46 list_del(&entry
->list
);
52 /* set the mode of the visualization to client or server */
53 void vis_set_mode(int mode
)
55 spin_lock(&vis_hash_lock
);
57 if (my_vis_info
!= NULL
)
58 my_vis_info
->packet
.vis_type
= mode
;
60 spin_unlock(&vis_hash_lock
);
63 /* is_vis_server(), locked outside */
64 static int is_vis_server_locked(void)
66 if (my_vis_info
!= NULL
)
67 if (my_vis_info
->packet
.vis_type
== VIS_TYPE_SERVER_SYNC
)
73 /* get the current set mode */
74 int is_vis_server(void)
78 spin_lock(&vis_hash_lock
);
79 ret
= is_vis_server_locked();
80 spin_unlock(&vis_hash_lock
);
85 /* Compare two vis packets, used by the hashing algorithm */
86 static int vis_info_cmp(void *data1
, void *data2
)
88 struct vis_info
*d1
, *d2
;
91 return compare_orig(d1
->packet
.vis_orig
, d2
->packet
.vis_orig
);
94 /* hash function to choose an entry in a hash table of given size */
95 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
96 static int vis_info_choose(void *data
, int size
)
98 struct vis_info
*vis_info
= data
;
103 key
= vis_info
->packet
.vis_orig
;
104 for (i
= 0; i
< ETH_ALEN
; i
++) {
106 hash
+= (hash
<< 10);
111 hash
^= (hash
>> 11);
112 hash
+= (hash
<< 15);
117 /* tries to add one entry to the receive list. */
118 static void recv_list_add(struct list_head
*recv_list
, char *mac
)
120 struct recvlist_node
*entry
;
121 entry
= kmalloc(sizeof(struct recvlist_node
), GFP_ATOMIC
);
125 memcpy(entry
->mac
, mac
, ETH_ALEN
);
126 list_add_tail(&entry
->list
, recv_list
);
129 /* returns 1 if this mac is in the recv_list */
130 static int recv_list_is_in(struct list_head
*recv_list
, char *mac
)
132 struct recvlist_node
*entry
;
134 list_for_each_entry(entry
, recv_list
, list
) {
135 if (memcmp(entry
->mac
, mac
, ETH_ALEN
) == 0)
142 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
143 * broken.. ). vis hash must be locked outside. is_new is set when the packet
144 * is newer than old entries in the hash. */
145 static struct vis_info
*add_packet(struct vis_packet
*vis_packet
,
146 int vis_info_len
, int *is_new
)
148 struct vis_info
*info
, *old_info
;
149 struct vis_info search_elem
;
153 if (vis_hash
== NULL
)
156 /* see if the packet is already in vis_hash */
157 memcpy(search_elem
.packet
.vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
158 old_info
= hash_find(vis_hash
, &search_elem
);
160 if (old_info
!= NULL
) {
161 if (vis_packet
->seqno
- old_info
->packet
.seqno
<= 0) {
162 if (old_info
->packet
.seqno
== vis_packet
->seqno
) {
163 recv_list_add(&old_info
->recv_list
,
164 vis_packet
->sender_orig
);
167 /* newer packet is already in hash. */
171 /* remove old entry */
172 hash_remove(vis_hash
, old_info
);
176 info
= kmalloc(sizeof(struct vis_info
) + vis_info_len
, GFP_ATOMIC
);
180 INIT_LIST_HEAD(&info
->send_list
);
181 INIT_LIST_HEAD(&info
->recv_list
);
182 info
->first_seen
= jiffies
;
183 memcpy(&info
->packet
, vis_packet
,
184 sizeof(struct vis_packet
) + vis_info_len
);
186 /* initialize and add new packet. */
189 /* repair if entries is longer than packet. */
190 if (info
->packet
.entries
* sizeof(struct vis_info_entry
) > vis_info_len
)
191 info
->packet
.entries
= vis_info_len
/ sizeof(struct vis_info_entry
);
193 recv_list_add(&info
->recv_list
, info
->packet
.sender_orig
);
196 if (hash_add(vis_hash
, info
) < 0) {
197 /* did not work (for some reason) */
205 /* handle the server sync packet, forward if needed. */
206 void receive_server_sync_packet(struct vis_packet
*vis_packet
, int vis_info_len
)
208 struct vis_info
*info
;
211 spin_lock(&vis_hash_lock
);
212 info
= add_packet(vis_packet
, vis_info_len
, &is_new
);
216 /* only if we are server ourselves and packet is newer than the one in
218 if (is_vis_server_locked() && is_new
) {
219 memcpy(info
->packet
.target_orig
, broadcastAddr
, ETH_ALEN
);
220 if (list_empty(&info
->send_list
))
221 list_add_tail(&info
->send_list
, &send_list
);
224 spin_unlock(&vis_hash_lock
);
227 /* handle an incoming client update packet and schedule forward if needed. */
228 void receive_client_update_packet(struct vis_packet
*vis_packet
,
231 struct vis_info
*info
;
234 /* clients shall not broadcast. */
235 if (is_bcast(vis_packet
->target_orig
))
238 spin_lock(&vis_hash_lock
);
239 info
= add_packet(vis_packet
, vis_info_len
, &is_new
);
242 /* note that outdated packets will be dropped at this point. */
245 /* send only if we're the target server or ... */
246 if (is_vis_server_locked() &&
247 is_my_mac(info
->packet
.target_orig
) &&
249 info
->packet
.vis_type
= VIS_TYPE_SERVER_SYNC
; /* upgrade! */
250 memcpy(info
->packet
.target_orig
, broadcastAddr
, ETH_ALEN
);
251 if (list_empty(&info
->send_list
))
252 list_add_tail(&info
->send_list
, &send_list
);
254 /* ... we're not the recipient (and thus need to forward). */
255 } else if (!is_my_mac(info
->packet
.target_orig
)) {
256 if (list_empty(&info
->send_list
))
257 list_add_tail(&info
->send_list
, &send_list
);
260 spin_unlock(&vis_hash_lock
);
263 /* Walk the originators and find the VIS server with the best tq. Set the packet
264 * address to its address and return the best_tq.
266 * Must be called with the originator hash locked */
267 static int find_best_vis_server(struct vis_info
*info
)
270 struct orig_node
*orig_node
;
273 while (hash_iterate(orig_hash
, &hashit
)) {
274 orig_node
= hashit
.bucket
->data
;
275 if ((orig_node
!= NULL
) &&
276 (orig_node
->router
!= NULL
) &&
277 (orig_node
->flags
& VIS_SERVER
) &&
278 (orig_node
->router
->tq_avg
> best_tq
)) {
279 best_tq
= orig_node
->router
->tq_avg
;
280 memcpy(info
->packet
.target_orig
, orig_node
->orig
,
287 /* Return true if the vis packet is full. */
288 static bool vis_packet_full(struct vis_info
*info
)
290 if (info
->packet
.entries
+ 1 >
291 (1000 - sizeof(struct vis_info
)) / sizeof(struct vis_info_entry
))
296 /* generates a packet of own vis data,
297 * returns 0 on success, -1 if no packet could be generated */
298 static int generate_vis_packet(void)
300 HASHIT(hashit_local
);
301 HASHIT(hashit_global
);
302 struct orig_node
*orig_node
;
303 struct vis_info
*info
= (struct vis_info
*)my_vis_info
;
304 struct vis_info_entry
*entry
, *entry_array
;
305 struct hna_local_entry
*hna_local_entry
;
309 info
->first_seen
= jiffies
;
311 spin_lock(&orig_hash_lock
);
312 memcpy(info
->packet
.target_orig
, broadcastAddr
, ETH_ALEN
);
313 info
->packet
.ttl
= TTL
;
314 info
->packet
.seqno
++;
315 info
->packet
.entries
= 0;
317 if (!is_vis_server_locked()) {
318 best_tq
= find_best_vis_server(info
);
320 spin_unlock(&orig_hash_lock
);
325 entry_array
= (struct vis_info_entry
*)
326 ((char *)info
+ sizeof(struct vis_info
));
328 while (hash_iterate(orig_hash
, &hashit_global
)) {
329 orig_node
= hashit_global
.bucket
->data
;
330 if (orig_node
->router
!= NULL
331 && compare_orig(orig_node
->router
->addr
, orig_node
->orig
)
332 && orig_node
->batman_if
333 && (orig_node
->batman_if
->if_active
== IF_ACTIVE
)
334 && orig_node
->router
->tq_avg
> 0) {
336 /* fill one entry into buffer. */
337 entry
= &entry_array
[info
->packet
.entries
];
338 memcpy(entry
->src
, orig_node
->batman_if
->net_dev
->dev_addr
, ETH_ALEN
);
339 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
340 entry
->quality
= orig_node
->router
->tq_avg
;
341 info
->packet
.entries
++;
343 if (vis_packet_full(info
)) {
344 spin_unlock(&orig_hash_lock
);
350 spin_unlock(&orig_hash_lock
);
352 spin_lock_irqsave(&hna_local_hash_lock
, flags
);
353 while (hash_iterate(hna_local_hash
, &hashit_local
)) {
354 hna_local_entry
= hashit_local
.bucket
->data
;
355 entry
= &entry_array
[info
->packet
.entries
];
356 memset(entry
->src
, 0, ETH_ALEN
);
357 memcpy(entry
->dest
, hna_local_entry
->addr
, ETH_ALEN
);
358 entry
->quality
= 0; /* 0 means HNA */
359 info
->packet
.entries
++;
361 if (vis_packet_full(info
)) {
362 spin_unlock_irqrestore(&hna_local_hash_lock
, flags
);
366 spin_unlock_irqrestore(&hna_local_hash_lock
, flags
);
370 static void purge_vis_packets(void)
373 struct vis_info
*info
;
375 while (hash_iterate(vis_hash
, &hashit
)) {
376 info
= hashit
.bucket
->data
;
377 if (info
== my_vis_info
) /* never purge own data. */
379 if (time_after(jiffies
,
380 info
->first_seen
+ (VIS_TIMEOUT
/1000)*HZ
)) {
381 hash_remove_bucket(vis_hash
, &hashit
);
387 static void broadcast_vis_packet(struct vis_info
*info
, int packet_length
)
390 struct orig_node
*orig_node
;
392 spin_lock(&orig_hash_lock
);
394 /* send to all routers in range. */
395 while (hash_iterate(orig_hash
, &hashit
)) {
396 orig_node
= hashit
.bucket
->data
;
398 /* if it's a vis server and reachable, send it. */
400 (orig_node
->flags
& VIS_SERVER
) &&
401 orig_node
->batman_if
&&
404 /* don't send it if we already received the packet from
406 if (recv_list_is_in(&info
->recv_list
, orig_node
->orig
))
409 memcpy(info
->packet
.target_orig
,
410 orig_node
->orig
, ETH_ALEN
);
412 send_raw_packet((unsigned char *) &info
->packet
,
414 orig_node
->batman_if
,
415 orig_node
->router
->addr
);
418 memcpy(info
->packet
.target_orig
, broadcastAddr
, ETH_ALEN
);
419 spin_unlock(&orig_hash_lock
);
422 static void unicast_vis_packet(struct vis_info
*info
, int packet_length
)
424 struct orig_node
*orig_node
;
426 spin_lock(&orig_hash_lock
);
427 orig_node
= ((struct orig_node
*)
428 hash_find(orig_hash
, info
->packet
.target_orig
));
430 if ((orig_node
!= NULL
) &&
431 (orig_node
->batman_if
!= NULL
) &&
432 (orig_node
->router
!= NULL
)) {
433 send_raw_packet((unsigned char *) &info
->packet
, packet_length
,
434 orig_node
->batman_if
,
435 orig_node
->router
->addr
);
437 spin_unlock(&orig_hash_lock
);
440 /* only send one vis packet. called from send_vis_packets() */
441 static void send_vis_packet(struct vis_info
*info
)
445 if (info
->packet
.ttl
< 2) {
446 printk(KERN_WARNING
"batman-adv: Error - can't send vis packet: ttl exceeded\n");
450 memcpy(info
->packet
.sender_orig
, mainIfAddr
, ETH_ALEN
);
453 packet_length
= sizeof(struct vis_packet
) +
454 info
->packet
.entries
* sizeof(struct vis_info_entry
);
456 if (is_bcast(info
->packet
.target_orig
))
457 broadcast_vis_packet(info
, packet_length
);
459 unicast_vis_packet(info
, packet_length
);
460 info
->packet
.ttl
++; /* restore TTL */
463 /* called from timer; send (and maybe generate) vis packet. */
464 static void send_vis_packets(struct work_struct
*work
)
466 struct vis_info
*info
, *temp
;
468 spin_lock(&vis_hash_lock
);
471 if (generate_vis_packet() == 0)
472 /* schedule if generation was successful */
473 list_add_tail(&my_vis_info
->send_list
, &send_list
);
475 list_for_each_entry_safe(info
, temp
, &send_list
, send_list
) {
476 list_del_init(&info
->send_list
);
477 send_vis_packet(info
);
479 spin_unlock(&vis_hash_lock
);
482 static DECLARE_DELAYED_WORK(vis_timer_wq
, send_vis_packets
);
484 /* init the vis server. this may only be called when if_list is already
485 * initialized (e.g. bat0 is initialized, interfaces have been added) */
491 spin_lock(&vis_hash_lock
);
493 vis_hash
= hash_new(256, vis_info_cmp
, vis_info_choose
);
495 printk(KERN_ERR
"batman-adv:Can't initialize vis_hash\n");
499 my_vis_info
= kmalloc(1000, GFP_ATOMIC
);
501 printk(KERN_ERR
"batman-adv:Can't initialize vis packet\n");
505 /* prefill the vis info */
506 my_vis_info
->first_seen
= jiffies
- atomic_read(&vis_interval
);
507 INIT_LIST_HEAD(&my_vis_info
->recv_list
);
508 INIT_LIST_HEAD(&my_vis_info
->send_list
);
509 my_vis_info
->packet
.version
= COMPAT_VERSION
;
510 my_vis_info
->packet
.packet_type
= BAT_VIS
;
511 my_vis_info
->packet
.vis_type
= VIS_TYPE_CLIENT_UPDATE
;
512 my_vis_info
->packet
.ttl
= TTL
;
513 my_vis_info
->packet
.seqno
= 0;
514 my_vis_info
->packet
.entries
= 0;
516 INIT_LIST_HEAD(&send_list
);
518 memcpy(my_vis_info
->packet
.vis_orig
, mainIfAddr
, ETH_ALEN
);
519 memcpy(my_vis_info
->packet
.sender_orig
, mainIfAddr
, ETH_ALEN
);
521 if (hash_add(vis_hash
, my_vis_info
) < 0) {
523 "batman-adv:Can't add own vis packet into hash\n");
524 free_info(my_vis_info
); /* not in hash, need to remove it
529 spin_unlock(&vis_hash_lock
);
534 spin_unlock(&vis_hash_lock
);
539 /* shutdown vis-server */
545 cancel_delayed_work_sync(&vis_timer_wq
);
547 spin_lock(&vis_hash_lock
);
548 /* properly remove, kill timers ... */
549 hash_delete(vis_hash
, free_info
);
552 spin_unlock(&vis_hash_lock
);
555 /* schedule packets for (re)transmission */
556 static void start_vis_timer(void)
558 queue_delayed_work(bat_event_workqueue
, &vis_timer_wq
,
559 (atomic_read(&vis_interval
)/1000) * HZ
);
This page took 0.066966 seconds and 4 git commands to generate.