2 * Copyright (C) 2008-2010 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"
30 #define MAX_VIS_PACKET_SIZE 1000
32 /* Returns the smallest signed integer in two's complement with the sizeof x */
33 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
35 /* Checks if a sequence number x is a predecessor/successor of y.
36 * they handle overflows/underflows and can correctly check for a
37 * predecessor/successor unless the variable sequence number has grown by
38 * more then 2**(bitwidth(x)-1)-1.
39 * This means that for a uint8_t with the maximum value 255, it would think:
40 * - when adding nothing - it is neither a predecessor nor a successor
41 * - before adding more than 127 to the starting value - it is a predecessor,
42 * - when adding 128 - it is neither a predecessor nor a successor,
43 * - after adding more than 127 to the starting value - it is a successor */
44 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
45 _dummy > smallest_signed_int(_dummy); })
46 #define seq_after(x, y) seq_before(y, x)
48 static void start_vis_timer(struct bat_priv
*bat_priv
);
51 static void free_info(struct kref
*ref
)
53 struct vis_info
*info
= container_of(ref
, struct vis_info
, refcount
);
54 struct bat_priv
*bat_priv
= info
->bat_priv
;
55 struct recvlist_node
*entry
, *tmp
;
58 list_del_init(&info
->send_list
);
59 spin_lock_irqsave(&bat_priv
->vis_list_lock
, flags
);
60 list_for_each_entry_safe(entry
, tmp
, &info
->recv_list
, list
) {
61 list_del(&entry
->list
);
65 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
, flags
);
66 kfree_skb(info
->skb_packet
);
69 /* Compare two vis packets, used by the hashing algorithm */
70 static int vis_info_cmp(void *data1
, void *data2
)
72 struct vis_info
*d1
, *d2
;
73 struct vis_packet
*p1
, *p2
;
76 p1
= (struct vis_packet
*)d1
->skb_packet
->data
;
77 p2
= (struct vis_packet
*)d2
->skb_packet
->data
;
78 return compare_orig(p1
->vis_orig
, p2
->vis_orig
);
81 /* hash function to choose an entry in a hash table of given size */
82 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
83 static int vis_info_choose(void *data
, int size
)
85 struct vis_info
*vis_info
= data
;
86 struct vis_packet
*packet
;
91 packet
= (struct vis_packet
*)vis_info
->skb_packet
->data
;
92 key
= packet
->vis_orig
;
93 for (i
= 0; i
< ETH_ALEN
; i
++) {
100 hash
^= (hash
>> 11);
101 hash
+= (hash
<< 15);
106 /* insert interface to the list of interfaces of one originator, if it
107 * does not already exist in the list */
108 static void vis_data_insert_interface(const uint8_t *interface
,
109 struct hlist_head
*if_list
,
112 struct if_list_entry
*entry
;
113 struct hlist_node
*pos
;
115 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
116 if (compare_orig(entry
->addr
, (void *)interface
))
120 /* its a new address, add it to the list */
121 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
124 memcpy(entry
->addr
, interface
, ETH_ALEN
);
125 entry
->primary
= primary
;
126 hlist_add_head(&entry
->list
, if_list
);
129 static ssize_t
vis_data_read_prim_sec(char *buff
, struct hlist_head
*if_list
)
131 struct if_list_entry
*entry
;
132 struct hlist_node
*pos
;
135 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
137 len
+= sprintf(buff
+ len
, "PRIMARY, ");
139 len
+= sprintf(buff
+ len
, "SEC %pM, ", entry
->addr
);
145 static size_t vis_data_count_prim_sec(struct hlist_head
*if_list
)
147 struct if_list_entry
*entry
;
148 struct hlist_node
*pos
;
151 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
162 static ssize_t
vis_data_read_entry(char *buff
, struct vis_info_entry
*entry
,
163 uint8_t *src
, bool primary
)
165 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
166 if (primary
&& entry
->quality
== 0)
167 return sprintf(buff
, "HNA %pM, ", entry
->dest
);
168 else if (compare_orig(entry
->src
, src
))
169 return sprintf(buff
, "TQ %pM %d, ", entry
->dest
,
175 int vis_seq_print_text(struct seq_file
*seq
, void *offset
)
178 HASHIT(hashit_count
);
179 struct vis_info
*info
;
180 struct vis_packet
*packet
;
181 struct vis_info_entry
*entries
;
182 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
183 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
184 HLIST_HEAD(vis_if_list
);
185 struct if_list_entry
*entry
;
186 struct hlist_node
*pos
, *n
;
189 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
190 size_t buff_pos
, buf_size
;
193 if ((!bat_priv
->primary_if
) ||
194 (vis_server
== VIS_TYPE_CLIENT_UPDATE
))
198 /* Estimate length */
199 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
200 while (hash_iterate(bat_priv
->vis_hash
, &hashit_count
)) {
201 info
= hashit_count
.bucket
->data
;
202 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
203 entries
= (struct vis_info_entry
*)
204 ((char *)packet
+ sizeof(struct vis_packet
));
206 for (i
= 0; i
< packet
->entries
; i
++) {
207 if (entries
[i
].quality
== 0)
209 vis_data_insert_interface(entries
[i
].src
, &vis_if_list
,
210 compare_orig(entries
[i
].src
, packet
->vis_orig
));
213 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
214 buf_size
+= 18 + 26 * packet
->entries
;
216 /* add primary/secondary records */
217 if (compare_orig(entry
->addr
, packet
->vis_orig
))
219 vis_data_count_prim_sec(&vis_if_list
);
224 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
225 hlist_del(&entry
->list
);
230 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
232 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
238 while (hash_iterate(bat_priv
->vis_hash
, &hashit
)) {
239 info
= hashit
.bucket
->data
;
240 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
241 entries
= (struct vis_info_entry
*)
242 ((char *)packet
+ sizeof(struct vis_packet
));
244 for (i
= 0; i
< packet
->entries
; i
++) {
245 if (entries
[i
].quality
== 0)
247 vis_data_insert_interface(entries
[i
].src
, &vis_if_list
,
248 compare_orig(entries
[i
].src
, packet
->vis_orig
));
251 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
252 buff_pos
+= sprintf(buff
+ buff_pos
, "%pM,",
255 for (i
= 0; i
< packet
->entries
; i
++)
256 buff_pos
+= vis_data_read_entry(buff
+ buff_pos
,
261 /* add primary/secondary records */
262 if (compare_orig(entry
->addr
, packet
->vis_orig
))
264 vis_data_read_prim_sec(buff
+ buff_pos
,
267 buff_pos
+= sprintf(buff
+ buff_pos
, "\n");
270 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
271 hlist_del(&entry
->list
);
276 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
278 seq_printf(seq
, "%s", buff
);
284 /* add the info packet to the send list, if it was not
285 * already linked in. */
286 static void send_list_add(struct bat_priv
*bat_priv
, struct vis_info
*info
)
288 if (list_empty(&info
->send_list
)) {
289 kref_get(&info
->refcount
);
290 list_add_tail(&info
->send_list
, &bat_priv
->vis_send_list
);
294 /* delete the info packet from the send list, if it was
296 static void send_list_del(struct vis_info
*info
)
298 if (!list_empty(&info
->send_list
)) {
299 list_del_init(&info
->send_list
);
300 kref_put(&info
->refcount
, free_info
);
304 /* tries to add one entry to the receive list. */
305 static void recv_list_add(struct bat_priv
*bat_priv
,
306 struct list_head
*recv_list
, char *mac
)
308 struct recvlist_node
*entry
;
311 entry
= kmalloc(sizeof(struct recvlist_node
), GFP_ATOMIC
);
315 memcpy(entry
->mac
, mac
, ETH_ALEN
);
316 spin_lock_irqsave(&bat_priv
->vis_list_lock
, flags
);
317 list_add_tail(&entry
->list
, recv_list
);
318 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
, flags
);
321 /* returns 1 if this mac is in the recv_list */
322 static int recv_list_is_in(struct bat_priv
*bat_priv
,
323 struct list_head
*recv_list
, char *mac
)
325 struct recvlist_node
*entry
;
328 spin_lock_irqsave(&bat_priv
->vis_list_lock
, flags
);
329 list_for_each_entry(entry
, recv_list
, list
) {
330 if (memcmp(entry
->mac
, mac
, ETH_ALEN
) == 0) {
331 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
,
336 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
, flags
);
340 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
341 * broken.. ). vis hash must be locked outside. is_new is set when the packet
342 * is newer than old entries in the hash. */
343 static struct vis_info
*add_packet(struct bat_priv
*bat_priv
,
344 struct vis_packet
*vis_packet
,
345 int vis_info_len
, int *is_new
,
348 struct vis_info
*info
, *old_info
;
349 struct vis_packet
*search_packet
, *old_packet
;
350 struct vis_info search_elem
;
351 struct vis_packet
*packet
;
355 if (!bat_priv
->vis_hash
)
358 /* see if the packet is already in vis_hash */
359 search_elem
.skb_packet
= dev_alloc_skb(sizeof(struct vis_packet
));
360 if (!search_elem
.skb_packet
)
362 search_packet
= (struct vis_packet
*)skb_put(search_elem
.skb_packet
,
363 sizeof(struct vis_packet
));
365 memcpy(search_packet
->vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
366 old_info
= hash_find(bat_priv
->vis_hash
, &search_elem
);
367 kfree_skb(search_elem
.skb_packet
);
369 if (old_info
!= NULL
) {
370 old_packet
= (struct vis_packet
*)old_info
->skb_packet
->data
;
371 if (!seq_after(ntohl(vis_packet
->seqno
),
372 ntohl(old_packet
->seqno
))) {
373 if (old_packet
->seqno
== vis_packet
->seqno
) {
374 recv_list_add(bat_priv
, &old_info
->recv_list
,
375 vis_packet
->sender_orig
);
378 /* newer packet is already in hash. */
382 /* remove old entry */
383 hash_remove(bat_priv
->vis_hash
, old_info
);
384 send_list_del(old_info
);
385 kref_put(&old_info
->refcount
, free_info
);
388 info
= kmalloc(sizeof(struct vis_info
), GFP_ATOMIC
);
392 info
->skb_packet
= dev_alloc_skb(sizeof(struct vis_packet
) +
393 vis_info_len
+ sizeof(struct ethhdr
));
394 if (!info
->skb_packet
) {
398 skb_reserve(info
->skb_packet
, sizeof(struct ethhdr
));
399 packet
= (struct vis_packet
*)skb_put(info
->skb_packet
,
400 sizeof(struct vis_packet
) +
403 kref_init(&info
->refcount
);
404 INIT_LIST_HEAD(&info
->send_list
);
405 INIT_LIST_HEAD(&info
->recv_list
);
406 info
->first_seen
= jiffies
;
407 info
->bat_priv
= bat_priv
;
408 memcpy(packet
, vis_packet
, sizeof(struct vis_packet
) + vis_info_len
);
410 /* initialize and add new packet. */
413 /* Make it a broadcast packet, if required */
415 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
417 /* repair if entries is longer than packet. */
418 if (packet
->entries
* sizeof(struct vis_info_entry
) > vis_info_len
)
419 packet
->entries
= vis_info_len
/ sizeof(struct vis_info_entry
);
421 recv_list_add(bat_priv
, &info
->recv_list
, packet
->sender_orig
);
424 if (hash_add(bat_priv
->vis_hash
, info
) < 0) {
425 /* did not work (for some reason) */
426 kref_put(&old_info
->refcount
, free_info
);
433 /* handle the server sync packet, forward if needed. */
434 void receive_server_sync_packet(struct bat_priv
*bat_priv
,
435 struct vis_packet
*vis_packet
,
438 struct vis_info
*info
;
439 int is_new
, make_broadcast
;
441 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
443 make_broadcast
= (vis_server
== VIS_TYPE_SERVER_SYNC
);
445 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
446 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
447 &is_new
, make_broadcast
);
451 /* only if we are server ourselves and packet is newer than the one in
453 if (vis_server
== VIS_TYPE_SERVER_SYNC
&& is_new
)
454 send_list_add(bat_priv
, info
);
456 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
459 /* handle an incoming client update packet and schedule forward if needed. */
460 void receive_client_update_packet(struct bat_priv
*bat_priv
,
461 struct vis_packet
*vis_packet
,
464 struct vis_info
*info
;
465 struct vis_packet
*packet
;
468 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
471 /* clients shall not broadcast. */
472 if (is_bcast(vis_packet
->target_orig
))
475 /* Are we the target for this VIS packet? */
476 if (vis_server
== VIS_TYPE_SERVER_SYNC
&&
477 is_my_mac(vis_packet
->target_orig
))
480 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
481 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
482 &is_new
, are_target
);
486 /* note that outdated packets will be dropped at this point. */
488 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
490 /* send only if we're the target server or ... */
491 if (are_target
&& is_new
) {
492 packet
->vis_type
= VIS_TYPE_SERVER_SYNC
; /* upgrade! */
493 send_list_add(bat_priv
, info
);
495 /* ... we're not the recipient (and thus need to forward). */
496 } else if (!is_my_mac(packet
->target_orig
)) {
497 send_list_add(bat_priv
, info
);
501 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
504 /* Walk the originators and find the VIS server with the best tq. Set the packet
505 * address to its address and return the best_tq.
507 * Must be called with the originator hash locked */
508 static int find_best_vis_server(struct bat_priv
*bat_priv
,
509 struct vis_info
*info
)
512 struct orig_node
*orig_node
;
513 struct vis_packet
*packet
;
516 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
518 while (hash_iterate(bat_priv
->orig_hash
, &hashit
)) {
519 orig_node
= hashit
.bucket
->data
;
520 if ((orig_node
) && (orig_node
->router
) &&
521 (orig_node
->flags
& VIS_SERVER
) &&
522 (orig_node
->router
->tq_avg
> best_tq
)) {
523 best_tq
= orig_node
->router
->tq_avg
;
524 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
530 /* Return true if the vis packet is full. */
531 static bool vis_packet_full(struct vis_info
*info
)
533 struct vis_packet
*packet
;
534 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
536 if (MAX_VIS_PACKET_SIZE
/ sizeof(struct vis_info_entry
)
537 < packet
->entries
+ 1)
542 /* generates a packet of own vis data,
543 * returns 0 on success, -1 if no packet could be generated */
544 static int generate_vis_packet(struct bat_priv
*bat_priv
)
546 HASHIT(hashit_local
);
547 HASHIT(hashit_global
);
548 struct orig_node
*orig_node
;
549 struct vis_info
*info
= (struct vis_info
*)bat_priv
->my_vis_info
;
550 struct vis_packet
*packet
= (struct vis_packet
*)info
->skb_packet
->data
;
551 struct vis_info_entry
*entry
;
552 struct hna_local_entry
*hna_local_entry
;
556 info
->first_seen
= jiffies
;
557 packet
->vis_type
= atomic_read(&bat_priv
->vis_mode
);
559 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
560 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
562 packet
->seqno
= htonl(ntohl(packet
->seqno
) + 1);
564 skb_trim(info
->skb_packet
, sizeof(struct vis_packet
));
566 if (packet
->vis_type
== VIS_TYPE_CLIENT_UPDATE
) {
567 best_tq
= find_best_vis_server(bat_priv
, info
);
570 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
,
576 while (hash_iterate(bat_priv
->orig_hash
, &hashit_global
)) {
577 orig_node
= hashit_global
.bucket
->data
;
579 if (!orig_node
->router
)
582 if (!compare_orig(orig_node
->router
->addr
, orig_node
->orig
))
585 if (orig_node
->router
->if_incoming
->if_status
!= IF_ACTIVE
)
588 if (orig_node
->router
->tq_avg
< 1)
591 /* fill one entry into buffer. */
592 entry
= (struct vis_info_entry
*)
593 skb_put(info
->skb_packet
, sizeof(*entry
));
595 orig_node
->router
->if_incoming
->net_dev
->dev_addr
,
597 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
598 entry
->quality
= orig_node
->router
->tq_avg
;
601 if (vis_packet_full(info
)) {
602 spin_unlock_irqrestore(
603 &bat_priv
->orig_hash_lock
, flags
);
608 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
610 spin_lock_irqsave(&bat_priv
->hna_lhash_lock
, flags
);
611 while (hash_iterate(bat_priv
->hna_local_hash
, &hashit_local
)) {
612 hna_local_entry
= hashit_local
.bucket
->data
;
613 entry
= (struct vis_info_entry
*)skb_put(info
->skb_packet
,
615 memset(entry
->src
, 0, ETH_ALEN
);
616 memcpy(entry
->dest
, hna_local_entry
->addr
, ETH_ALEN
);
617 entry
->quality
= 0; /* 0 means HNA */
620 if (vis_packet_full(info
)) {
621 spin_unlock_irqrestore(&bat_priv
->hna_lhash_lock
,
627 spin_unlock_irqrestore(&bat_priv
->hna_lhash_lock
, flags
);
631 /* free old vis packets. Must be called with this vis_hash_lock
633 static void purge_vis_packets(struct bat_priv
*bat_priv
)
636 struct vis_info
*info
;
638 while (hash_iterate(bat_priv
->vis_hash
, &hashit
)) {
639 info
= hashit
.bucket
->data
;
641 /* never purge own data. */
642 if (info
== bat_priv
->my_vis_info
)
645 if (time_after(jiffies
,
646 info
->first_seen
+ VIS_TIMEOUT
* HZ
)) {
647 hash_remove_bucket(bat_priv
->vis_hash
, &hashit
);
649 kref_put(&info
->refcount
, free_info
);
654 static void broadcast_vis_packet(struct bat_priv
*bat_priv
,
655 struct vis_info
*info
)
658 struct orig_node
*orig_node
;
659 struct vis_packet
*packet
;
662 struct batman_if
*batman_if
;
663 uint8_t dstaddr
[ETH_ALEN
];
666 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
667 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
669 /* send to all routers in range. */
670 while (hash_iterate(bat_priv
->orig_hash
, &hashit
)) {
671 orig_node
= hashit
.bucket
->data
;
673 /* if it's a vis server and reachable, send it. */
674 if ((!orig_node
) || (!orig_node
->router
))
676 if (!(orig_node
->flags
& VIS_SERVER
))
678 /* don't send it if we already received the packet from
680 if (recv_list_is_in(bat_priv
, &info
->recv_list
,
684 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
685 batman_if
= orig_node
->router
->if_incoming
;
686 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
687 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
689 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
691 send_skb_packet(skb
, batman_if
, dstaddr
);
693 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
697 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
700 static void unicast_vis_packet(struct bat_priv
*bat_priv
,
701 struct vis_info
*info
)
703 struct orig_node
*orig_node
;
705 struct vis_packet
*packet
;
707 struct batman_if
*batman_if
;
708 uint8_t dstaddr
[ETH_ALEN
];
710 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
711 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
712 orig_node
= ((struct orig_node
*)hash_find(bat_priv
->orig_hash
,
713 packet
->target_orig
));
715 if ((!orig_node
) || (!orig_node
->router
))
718 /* don't lock while sending the packets ... we therefore
719 * copy the required data before sending */
720 batman_if
= orig_node
->router
->if_incoming
;
721 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
722 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
724 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
726 send_skb_packet(skb
, batman_if
, dstaddr
);
731 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
734 /* only send one vis packet. called from send_vis_packets() */
735 static void send_vis_packet(struct bat_priv
*bat_priv
, struct vis_info
*info
)
737 struct vis_packet
*packet
;
739 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
740 if (packet
->ttl
< 2) {
741 pr_debug("Error - can't send vis packet: ttl exceeded\n");
745 memcpy(packet
->sender_orig
, bat_priv
->primary_if
->net_dev
->dev_addr
,
749 if (is_bcast(packet
->target_orig
))
750 broadcast_vis_packet(bat_priv
, info
);
752 unicast_vis_packet(bat_priv
, info
);
753 packet
->ttl
++; /* restore TTL */
756 /* called from timer; send (and maybe generate) vis packet. */
757 static void send_vis_packets(struct work_struct
*work
)
759 struct delayed_work
*delayed_work
=
760 container_of(work
, struct delayed_work
, work
);
761 struct bat_priv
*bat_priv
=
762 container_of(delayed_work
, struct bat_priv
, vis_work
);
763 struct vis_info
*info
, *temp
;
766 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
767 purge_vis_packets(bat_priv
);
769 if (generate_vis_packet(bat_priv
) == 0) {
770 /* schedule if generation was successful */
771 send_list_add(bat_priv
, bat_priv
->my_vis_info
);
774 list_for_each_entry_safe(info
, temp
, &bat_priv
->vis_send_list
,
777 kref_get(&info
->refcount
);
778 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
780 if (bat_priv
->primary_if
)
781 send_vis_packet(bat_priv
, info
);
783 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
785 kref_put(&info
->refcount
, free_info
);
787 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
788 start_vis_timer(bat_priv
);
791 /* init the vis server. this may only be called when if_list is already
792 * initialized (e.g. bat0 is initialized, interfaces have been added) */
793 int vis_init(struct bat_priv
*bat_priv
)
795 struct vis_packet
*packet
;
798 if (bat_priv
->vis_hash
)
801 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
803 bat_priv
->vis_hash
= hash_new(256, vis_info_cmp
, vis_info_choose
);
804 if (!bat_priv
->vis_hash
) {
805 pr_err("Can't initialize vis_hash\n");
809 bat_priv
->my_vis_info
= kmalloc(MAX_VIS_PACKET_SIZE
, GFP_ATOMIC
);
810 if (!bat_priv
->my_vis_info
) {
811 pr_err("Can't initialize vis packet\n");
815 bat_priv
->my_vis_info
->skb_packet
= dev_alloc_skb(
816 sizeof(struct vis_packet
) +
817 MAX_VIS_PACKET_SIZE
+
818 sizeof(struct ethhdr
));
819 if (!bat_priv
->my_vis_info
->skb_packet
)
822 skb_reserve(bat_priv
->my_vis_info
->skb_packet
, sizeof(struct ethhdr
));
823 packet
= (struct vis_packet
*)skb_put(
824 bat_priv
->my_vis_info
->skb_packet
,
825 sizeof(struct vis_packet
));
827 /* prefill the vis info */
828 bat_priv
->my_vis_info
->first_seen
= jiffies
-
829 msecs_to_jiffies(VIS_INTERVAL
);
830 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->recv_list
);
831 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->send_list
);
832 kref_init(&bat_priv
->my_vis_info
->refcount
);
833 bat_priv
->my_vis_info
->bat_priv
= bat_priv
;
834 packet
->version
= COMPAT_VERSION
;
835 packet
->packet_type
= BAT_VIS
;
840 INIT_LIST_HEAD(&bat_priv
->vis_send_list
);
842 if (hash_add(bat_priv
->vis_hash
, bat_priv
->my_vis_info
) < 0) {
843 pr_err("Can't add own vis packet into hash\n");
844 /* not in hash, need to remove it manually. */
845 kref_put(&bat_priv
->my_vis_info
->refcount
, free_info
);
849 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
850 start_vis_timer(bat_priv
);
854 kfree(bat_priv
->my_vis_info
);
855 bat_priv
->my_vis_info
= NULL
;
857 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
862 /* Decrease the reference count on a hash item info */
863 static void free_info_ref(void *data
, void *arg
)
865 struct vis_info
*info
= data
;
868 kref_put(&info
->refcount
, free_info
);
871 /* shutdown vis-server */
872 void vis_quit(struct bat_priv
*bat_priv
)
875 if (!bat_priv
->vis_hash
)
878 cancel_delayed_work_sync(&bat_priv
->vis_work
);
880 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
881 /* properly remove, kill timers ... */
882 hash_delete(bat_priv
->vis_hash
, free_info_ref
, NULL
);
883 bat_priv
->vis_hash
= NULL
;
884 bat_priv
->my_vis_info
= NULL
;
885 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
888 /* schedule packets for (re)transmission */
889 static void start_vis_timer(struct bat_priv
*bat_priv
)
891 INIT_DELAYED_WORK(&bat_priv
->vis_work
, send_vis_packets
);
892 queue_delayed_work(bat_event_workqueue
, &bat_priv
->vis_work
,
893 msecs_to_jiffies(VIS_INTERVAL
));