2 * Copyright (C) 2008-2012 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"
29 #include "originator.h"
31 #define MAX_VIS_PACKET_SIZE 1000
33 static void start_vis_timer(struct bat_priv
*bat_priv
);
36 static void free_info(struct kref
*ref
)
38 struct vis_info
*info
= container_of(ref
, struct vis_info
, refcount
);
39 struct bat_priv
*bat_priv
= info
->bat_priv
;
40 struct recvlist_node
*entry
, *tmp
;
42 list_del_init(&info
->send_list
);
43 spin_lock_bh(&bat_priv
->vis_list_lock
);
44 list_for_each_entry_safe(entry
, tmp
, &info
->recv_list
, list
) {
45 list_del(&entry
->list
);
49 spin_unlock_bh(&bat_priv
->vis_list_lock
);
50 kfree_skb(info
->skb_packet
);
54 /* Compare two vis packets, used by the hashing algorithm */
55 static int vis_info_cmp(const struct hlist_node
*node
, const void *data2
)
57 const struct vis_info
*d1
, *d2
;
58 const struct vis_packet
*p1
, *p2
;
60 d1
= container_of(node
, struct vis_info
, hash_entry
);
62 p1
= (struct vis_packet
*)d1
->skb_packet
->data
;
63 p2
= (struct vis_packet
*)d2
->skb_packet
->data
;
64 return compare_eth(p1
->vis_orig
, p2
->vis_orig
);
67 /* hash function to choose an entry in a hash table of given size */
68 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
69 static uint32_t vis_info_choose(const void *data
, uint32_t size
)
71 const struct vis_info
*vis_info
= data
;
72 const struct vis_packet
*packet
;
73 const unsigned char *key
;
77 packet
= (struct vis_packet
*)vis_info
->skb_packet
->data
;
78 key
= packet
->vis_orig
;
79 for (i
= 0; i
< ETH_ALEN
; i
++) {
92 static struct vis_info
*vis_hash_find(struct bat_priv
*bat_priv
,
95 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
96 struct hlist_head
*head
;
97 struct hlist_node
*node
;
98 struct vis_info
*vis_info
, *vis_info_tmp
= NULL
;
104 index
= vis_info_choose(data
, hash
->size
);
105 head
= &hash
->table
[index
];
108 hlist_for_each_entry_rcu(vis_info
, node
, head
, hash_entry
) {
109 if (!vis_info_cmp(node
, data
))
112 vis_info_tmp
= vis_info
;
120 /* insert interface to the list of interfaces of one originator, if it
121 * does not already exist in the list */
122 static void vis_data_insert_interface(const uint8_t *interface
,
123 struct hlist_head
*if_list
,
126 struct if_list_entry
*entry
;
127 struct hlist_node
*pos
;
129 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
130 if (compare_eth(entry
->addr
, interface
))
134 /* it's a new address, add it to the list */
135 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
138 memcpy(entry
->addr
, interface
, ETH_ALEN
);
139 entry
->primary
= primary
;
140 hlist_add_head(&entry
->list
, if_list
);
143 static ssize_t
vis_data_read_prim_sec(char *buff
,
144 const struct hlist_head
*if_list
)
146 struct if_list_entry
*entry
;
147 struct hlist_node
*pos
;
150 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
152 len
+= sprintf(buff
+ len
, "PRIMARY, ");
154 len
+= sprintf(buff
+ len
, "SEC %pM, ", entry
->addr
);
160 static size_t vis_data_count_prim_sec(struct hlist_head
*if_list
)
162 struct if_list_entry
*entry
;
163 struct hlist_node
*pos
;
166 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
177 static ssize_t
vis_data_read_entry(char *buff
,
178 const struct vis_info_entry
*entry
,
179 const uint8_t *src
, bool primary
)
181 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
182 if (primary
&& entry
->quality
== 0)
183 return sprintf(buff
, "TT %pM, ", entry
->dest
);
184 else if (compare_eth(entry
->src
, src
))
185 return sprintf(buff
, "TQ %pM %d, ", entry
->dest
,
191 int vis_seq_print_text(struct seq_file
*seq
, void *offset
)
193 struct hard_iface
*primary_if
;
194 struct hlist_node
*node
;
195 struct hlist_head
*head
;
196 struct vis_info
*info
;
197 struct vis_packet
*packet
;
198 struct vis_info_entry
*entries
;
199 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
200 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
201 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
202 HLIST_HEAD(vis_if_list
);
203 struct if_list_entry
*entry
;
204 struct hlist_node
*pos
, *n
;
207 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
208 size_t buff_pos
, buf_size
;
211 primary_if
= primary_if_get_selected(bat_priv
);
215 if (vis_server
== VIS_TYPE_CLIENT_UPDATE
)
219 /* Estimate length */
220 spin_lock_bh(&bat_priv
->vis_hash_lock
);
221 for (i
= 0; i
< hash
->size
; i
++) {
222 head
= &hash
->table
[i
];
225 hlist_for_each_entry_rcu(info
, node
, head
, hash_entry
) {
226 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
227 entries
= (struct vis_info_entry
*)
228 ((char *)packet
+ sizeof(*packet
));
230 vis_data_insert_interface(packet
->vis_orig
,
233 for (j
= 0; j
< packet
->entries
; j
++) {
234 if (entries
[j
].quality
== 0)
236 if (compare_eth(entries
[j
].src
,
239 vis_data_insert_interface(entries
[j
].src
,
244 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
245 buf_size
+= 18 + 26 * packet
->entries
;
247 /* add primary/secondary records */
248 if (compare_eth(entry
->addr
, packet
->vis_orig
))
250 vis_data_count_prim_sec(&vis_if_list
);
255 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
,
257 hlist_del(&entry
->list
);
264 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
266 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
273 for (i
= 0; i
< hash
->size
; i
++) {
274 head
= &hash
->table
[i
];
277 hlist_for_each_entry_rcu(info
, node
, head
, hash_entry
) {
278 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
279 entries
= (struct vis_info_entry
*)
280 ((char *)packet
+ sizeof(*packet
));
282 vis_data_insert_interface(packet
->vis_orig
,
285 for (j
= 0; j
< packet
->entries
; j
++) {
286 if (entries
[j
].quality
== 0)
288 if (compare_eth(entries
[j
].src
,
291 vis_data_insert_interface(entries
[j
].src
,
296 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
297 buff_pos
+= sprintf(buff
+ buff_pos
, "%pM,",
300 for (j
= 0; j
< packet
->entries
; j
++)
301 buff_pos
+= vis_data_read_entry(
307 /* add primary/secondary records */
308 if (compare_eth(entry
->addr
, packet
->vis_orig
))
310 vis_data_read_prim_sec(buff
+ buff_pos
,
313 buff_pos
+= sprintf(buff
+ buff_pos
, "\n");
316 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
,
318 hlist_del(&entry
->list
);
325 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
327 seq_printf(seq
, "%s", buff
);
332 hardif_free_ref(primary_if
);
336 /* add the info packet to the send list, if it was not
337 * already linked in. */
338 static void send_list_add(struct bat_priv
*bat_priv
, struct vis_info
*info
)
340 if (list_empty(&info
->send_list
)) {
341 kref_get(&info
->refcount
);
342 list_add_tail(&info
->send_list
, &bat_priv
->vis_send_list
);
346 /* delete the info packet from the send list, if it was
348 static void send_list_del(struct vis_info
*info
)
350 if (!list_empty(&info
->send_list
)) {
351 list_del_init(&info
->send_list
);
352 kref_put(&info
->refcount
, free_info
);
356 /* tries to add one entry to the receive list. */
357 static void recv_list_add(struct bat_priv
*bat_priv
,
358 struct list_head
*recv_list
, const char *mac
)
360 struct recvlist_node
*entry
;
362 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
366 memcpy(entry
->mac
, mac
, ETH_ALEN
);
367 spin_lock_bh(&bat_priv
->vis_list_lock
);
368 list_add_tail(&entry
->list
, recv_list
);
369 spin_unlock_bh(&bat_priv
->vis_list_lock
);
372 /* returns 1 if this mac is in the recv_list */
373 static int recv_list_is_in(struct bat_priv
*bat_priv
,
374 const struct list_head
*recv_list
, const char *mac
)
376 const struct recvlist_node
*entry
;
378 spin_lock_bh(&bat_priv
->vis_list_lock
);
379 list_for_each_entry(entry
, recv_list
, list
) {
380 if (compare_eth(entry
->mac
, mac
)) {
381 spin_unlock_bh(&bat_priv
->vis_list_lock
);
385 spin_unlock_bh(&bat_priv
->vis_list_lock
);
389 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
390 * broken.. ). vis hash must be locked outside. is_new is set when the packet
391 * is newer than old entries in the hash. */
392 static struct vis_info
*add_packet(struct bat_priv
*bat_priv
,
393 struct vis_packet
*vis_packet
,
394 int vis_info_len
, int *is_new
,
397 struct vis_info
*info
, *old_info
;
398 struct vis_packet
*search_packet
, *old_packet
;
399 struct vis_info search_elem
;
400 struct vis_packet
*packet
;
405 if (!bat_priv
->vis_hash
)
408 /* see if the packet is already in vis_hash */
409 search_elem
.skb_packet
= dev_alloc_skb(sizeof(*search_packet
));
410 if (!search_elem
.skb_packet
)
412 search_packet
= (struct vis_packet
*)skb_put(search_elem
.skb_packet
,
413 sizeof(*search_packet
));
415 memcpy(search_packet
->vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
416 old_info
= vis_hash_find(bat_priv
, &search_elem
);
417 kfree_skb(search_elem
.skb_packet
);
420 old_packet
= (struct vis_packet
*)old_info
->skb_packet
->data
;
421 if (!seq_after(ntohl(vis_packet
->seqno
),
422 ntohl(old_packet
->seqno
))) {
423 if (old_packet
->seqno
== vis_packet
->seqno
) {
424 recv_list_add(bat_priv
, &old_info
->recv_list
,
425 vis_packet
->sender_orig
);
428 /* newer packet is already in hash. */
432 /* remove old entry */
433 hash_remove(bat_priv
->vis_hash
, vis_info_cmp
, vis_info_choose
,
435 send_list_del(old_info
);
436 kref_put(&old_info
->refcount
, free_info
);
439 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
443 info
->skb_packet
= dev_alloc_skb(sizeof(*packet
) + vis_info_len
+
445 if (!info
->skb_packet
) {
449 skb_reserve(info
->skb_packet
, ETH_HLEN
);
450 packet
= (struct vis_packet
*)skb_put(info
->skb_packet
, sizeof(*packet
)
453 kref_init(&info
->refcount
);
454 INIT_LIST_HEAD(&info
->send_list
);
455 INIT_LIST_HEAD(&info
->recv_list
);
456 info
->first_seen
= jiffies
;
457 info
->bat_priv
= bat_priv
;
458 memcpy(packet
, vis_packet
, sizeof(*packet
) + vis_info_len
);
460 /* initialize and add new packet. */
463 /* Make it a broadcast packet, if required */
465 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
467 /* repair if entries is longer than packet. */
468 if (packet
->entries
* sizeof(struct vis_info_entry
) > vis_info_len
)
469 packet
->entries
= vis_info_len
/ sizeof(struct vis_info_entry
);
471 recv_list_add(bat_priv
, &info
->recv_list
, packet
->sender_orig
);
474 hash_added
= hash_add(bat_priv
->vis_hash
, vis_info_cmp
, vis_info_choose
,
475 info
, &info
->hash_entry
);
476 if (hash_added
!= 0) {
477 /* did not work (for some reason) */
478 kref_put(&info
->refcount
, free_info
);
485 /* handle the server sync packet, forward if needed. */
486 void receive_server_sync_packet(struct bat_priv
*bat_priv
,
487 struct vis_packet
*vis_packet
,
490 struct vis_info
*info
;
491 int is_new
, make_broadcast
;
492 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
494 make_broadcast
= (vis_server
== VIS_TYPE_SERVER_SYNC
);
496 spin_lock_bh(&bat_priv
->vis_hash_lock
);
497 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
498 &is_new
, make_broadcast
);
502 /* only if we are server ourselves and packet is newer than the one in
504 if (vis_server
== VIS_TYPE_SERVER_SYNC
&& is_new
)
505 send_list_add(bat_priv
, info
);
507 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
510 /* handle an incoming client update packet and schedule forward if needed. */
511 void receive_client_update_packet(struct bat_priv
*bat_priv
,
512 struct vis_packet
*vis_packet
,
515 struct vis_info
*info
;
516 struct vis_packet
*packet
;
518 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
521 /* clients shall not broadcast. */
522 if (is_broadcast_ether_addr(vis_packet
->target_orig
))
525 /* Are we the target for this VIS packet? */
526 if (vis_server
== VIS_TYPE_SERVER_SYNC
&&
527 is_my_mac(vis_packet
->target_orig
))
530 spin_lock_bh(&bat_priv
->vis_hash_lock
);
531 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
532 &is_new
, are_target
);
536 /* note that outdated packets will be dropped at this point. */
538 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
540 /* send only if we're the target server or ... */
541 if (are_target
&& is_new
) {
542 packet
->vis_type
= VIS_TYPE_SERVER_SYNC
; /* upgrade! */
543 send_list_add(bat_priv
, info
);
545 /* ... we're not the recipient (and thus need to forward). */
546 } else if (!is_my_mac(packet
->target_orig
)) {
547 send_list_add(bat_priv
, info
);
551 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
554 /* Walk the originators and find the VIS server with the best tq. Set the packet
555 * address to its address and return the best_tq.
557 * Must be called with the originator hash locked */
558 static int find_best_vis_server(struct bat_priv
*bat_priv
,
559 struct vis_info
*info
)
561 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
562 struct neigh_node
*router
;
563 struct hlist_node
*node
;
564 struct hlist_head
*head
;
565 struct orig_node
*orig_node
;
566 struct vis_packet
*packet
;
570 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
572 for (i
= 0; i
< hash
->size
; i
++) {
573 head
= &hash
->table
[i
];
576 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
577 router
= batadv_orig_node_get_router(orig_node
);
581 if ((orig_node
->flags
& VIS_SERVER
) &&
582 (router
->tq_avg
> best_tq
)) {
583 best_tq
= router
->tq_avg
;
584 memcpy(packet
->target_orig
, orig_node
->orig
,
587 batadv_neigh_node_free_ref(router
);
595 /* Return true if the vis packet is full. */
596 static bool vis_packet_full(const struct vis_info
*info
)
598 const struct vis_packet
*packet
;
599 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
601 if (MAX_VIS_PACKET_SIZE
/ sizeof(struct vis_info_entry
)
602 < packet
->entries
+ 1)
607 /* generates a packet of own vis data,
608 * returns 0 on success, -1 if no packet could be generated */
609 static int generate_vis_packet(struct bat_priv
*bat_priv
)
611 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
612 struct hlist_node
*node
;
613 struct hlist_head
*head
;
614 struct orig_node
*orig_node
;
615 struct neigh_node
*router
;
616 struct vis_info
*info
= bat_priv
->my_vis_info
;
617 struct vis_packet
*packet
= (struct vis_packet
*)info
->skb_packet
->data
;
618 struct vis_info_entry
*entry
;
619 struct tt_common_entry
*tt_common_entry
;
623 info
->first_seen
= jiffies
;
624 packet
->vis_type
= atomic_read(&bat_priv
->vis_mode
);
626 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
627 packet
->header
.ttl
= TTL
;
628 packet
->seqno
= htonl(ntohl(packet
->seqno
) + 1);
630 skb_trim(info
->skb_packet
, sizeof(*packet
));
632 if (packet
->vis_type
== VIS_TYPE_CLIENT_UPDATE
) {
633 best_tq
= find_best_vis_server(bat_priv
, info
);
639 for (i
= 0; i
< hash
->size
; i
++) {
640 head
= &hash
->table
[i
];
643 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
644 router
= batadv_orig_node_get_router(orig_node
);
648 if (!compare_eth(router
->addr
, orig_node
->orig
))
651 if (router
->if_incoming
->if_status
!= IF_ACTIVE
)
654 if (router
->tq_avg
< 1)
657 /* fill one entry into buffer. */
658 entry
= (struct vis_info_entry
*)
659 skb_put(info
->skb_packet
, sizeof(*entry
));
661 router
->if_incoming
->net_dev
->dev_addr
,
663 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
664 entry
->quality
= router
->tq_avg
;
668 batadv_neigh_node_free_ref(router
);
670 if (vis_packet_full(info
))
676 hash
= bat_priv
->tt_local_hash
;
678 for (i
= 0; i
< hash
->size
; i
++) {
679 head
= &hash
->table
[i
];
682 hlist_for_each_entry_rcu(tt_common_entry
, node
, head
,
684 entry
= (struct vis_info_entry
*)
685 skb_put(info
->skb_packet
,
687 memset(entry
->src
, 0, ETH_ALEN
);
688 memcpy(entry
->dest
, tt_common_entry
->addr
, ETH_ALEN
);
689 entry
->quality
= 0; /* 0 means TT */
692 if (vis_packet_full(info
))
705 /* free old vis packets. Must be called with this vis_hash_lock
707 static void purge_vis_packets(struct bat_priv
*bat_priv
)
710 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
711 struct hlist_node
*node
, *node_tmp
;
712 struct hlist_head
*head
;
713 struct vis_info
*info
;
715 for (i
= 0; i
< hash
->size
; i
++) {
716 head
= &hash
->table
[i
];
718 hlist_for_each_entry_safe(info
, node
, node_tmp
,
720 /* never purge own data. */
721 if (info
== bat_priv
->my_vis_info
)
724 if (has_timed_out(info
->first_seen
, VIS_TIMEOUT
)) {
727 kref_put(&info
->refcount
, free_info
);
733 static void broadcast_vis_packet(struct bat_priv
*bat_priv
,
734 struct vis_info
*info
)
736 struct neigh_node
*router
;
737 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
738 struct hlist_node
*node
;
739 struct hlist_head
*head
;
740 struct orig_node
*orig_node
;
741 struct vis_packet
*packet
;
743 struct hard_iface
*hard_iface
;
744 uint8_t dstaddr
[ETH_ALEN
];
748 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
750 /* send to all routers in range. */
751 for (i
= 0; i
< hash
->size
; i
++) {
752 head
= &hash
->table
[i
];
755 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
756 /* if it's a vis server and reachable, send it. */
757 if (!(orig_node
->flags
& VIS_SERVER
))
760 router
= batadv_orig_node_get_router(orig_node
);
764 /* don't send it if we already received the packet from
766 if (recv_list_is_in(bat_priv
, &info
->recv_list
,
768 batadv_neigh_node_free_ref(router
);
772 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
773 hard_iface
= router
->if_incoming
;
774 memcpy(dstaddr
, router
->addr
, ETH_ALEN
);
776 batadv_neigh_node_free_ref(router
);
778 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
780 send_skb_packet(skb
, hard_iface
, dstaddr
);
787 static void unicast_vis_packet(struct bat_priv
*bat_priv
,
788 struct vis_info
*info
)
790 struct orig_node
*orig_node
;
791 struct neigh_node
*router
= NULL
;
793 struct vis_packet
*packet
;
795 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
797 orig_node
= orig_hash_find(bat_priv
, packet
->target_orig
);
801 router
= batadv_orig_node_get_router(orig_node
);
805 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
807 send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
811 batadv_neigh_node_free_ref(router
);
813 batadv_orig_node_free_ref(orig_node
);
816 /* only send one vis packet. called from send_vis_packets() */
817 static void send_vis_packet(struct bat_priv
*bat_priv
, struct vis_info
*info
)
819 struct hard_iface
*primary_if
;
820 struct vis_packet
*packet
;
822 primary_if
= primary_if_get_selected(bat_priv
);
826 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
827 if (packet
->header
.ttl
< 2) {
828 pr_debug("Error - can't send vis packet: ttl exceeded\n");
832 memcpy(packet
->sender_orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
833 packet
->header
.ttl
--;
835 if (is_broadcast_ether_addr(packet
->target_orig
))
836 broadcast_vis_packet(bat_priv
, info
);
838 unicast_vis_packet(bat_priv
, info
);
839 packet
->header
.ttl
++; /* restore TTL */
843 hardif_free_ref(primary_if
);
846 /* called from timer; send (and maybe generate) vis packet. */
847 static void send_vis_packets(struct work_struct
*work
)
849 struct delayed_work
*delayed_work
=
850 container_of(work
, struct delayed_work
, work
);
851 struct bat_priv
*bat_priv
=
852 container_of(delayed_work
, struct bat_priv
, vis_work
);
853 struct vis_info
*info
;
855 spin_lock_bh(&bat_priv
->vis_hash_lock
);
856 purge_vis_packets(bat_priv
);
858 if (generate_vis_packet(bat_priv
) == 0) {
859 /* schedule if generation was successful */
860 send_list_add(bat_priv
, bat_priv
->my_vis_info
);
863 while (!list_empty(&bat_priv
->vis_send_list
)) {
864 info
= list_first_entry(&bat_priv
->vis_send_list
,
865 typeof(*info
), send_list
);
867 kref_get(&info
->refcount
);
868 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
870 send_vis_packet(bat_priv
, info
);
872 spin_lock_bh(&bat_priv
->vis_hash_lock
);
874 kref_put(&info
->refcount
, free_info
);
876 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
877 start_vis_timer(bat_priv
);
880 /* init the vis server. this may only be called when if_list is already
881 * initialized (e.g. bat0 is initialized, interfaces have been added) */
882 int vis_init(struct bat_priv
*bat_priv
)
884 struct vis_packet
*packet
;
887 if (bat_priv
->vis_hash
)
890 spin_lock_bh(&bat_priv
->vis_hash_lock
);
892 bat_priv
->vis_hash
= batadv_hash_new(256);
893 if (!bat_priv
->vis_hash
) {
894 pr_err("Can't initialize vis_hash\n");
898 bat_priv
->my_vis_info
= kmalloc(MAX_VIS_PACKET_SIZE
, GFP_ATOMIC
);
899 if (!bat_priv
->my_vis_info
)
902 bat_priv
->my_vis_info
->skb_packet
= dev_alloc_skb(sizeof(*packet
) +
903 MAX_VIS_PACKET_SIZE
+
905 if (!bat_priv
->my_vis_info
->skb_packet
)
908 skb_reserve(bat_priv
->my_vis_info
->skb_packet
, ETH_HLEN
);
909 packet
= (struct vis_packet
*)skb_put(bat_priv
->my_vis_info
->skb_packet
,
912 /* prefill the vis info */
913 bat_priv
->my_vis_info
->first_seen
= jiffies
-
914 msecs_to_jiffies(VIS_INTERVAL
);
915 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->recv_list
);
916 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->send_list
);
917 kref_init(&bat_priv
->my_vis_info
->refcount
);
918 bat_priv
->my_vis_info
->bat_priv
= bat_priv
;
919 packet
->header
.version
= COMPAT_VERSION
;
920 packet
->header
.packet_type
= BAT_VIS
;
921 packet
->header
.ttl
= TTL
;
925 INIT_LIST_HEAD(&bat_priv
->vis_send_list
);
927 hash_added
= hash_add(bat_priv
->vis_hash
, vis_info_cmp
, vis_info_choose
,
928 bat_priv
->my_vis_info
,
929 &bat_priv
->my_vis_info
->hash_entry
);
930 if (hash_added
!= 0) {
931 pr_err("Can't add own vis packet into hash\n");
932 /* not in hash, need to remove it manually. */
933 kref_put(&bat_priv
->my_vis_info
->refcount
, free_info
);
937 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
938 start_vis_timer(bat_priv
);
942 kfree(bat_priv
->my_vis_info
);
943 bat_priv
->my_vis_info
= NULL
;
945 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
950 /* Decrease the reference count on a hash item info */
951 static void free_info_ref(struct hlist_node
*node
, void *arg
)
953 struct vis_info
*info
;
955 info
= container_of(node
, struct vis_info
, hash_entry
);
957 kref_put(&info
->refcount
, free_info
);
960 /* shutdown vis-server */
961 void vis_quit(struct bat_priv
*bat_priv
)
963 if (!bat_priv
->vis_hash
)
966 cancel_delayed_work_sync(&bat_priv
->vis_work
);
968 spin_lock_bh(&bat_priv
->vis_hash_lock
);
969 /* properly remove, kill timers ... */
970 hash_delete(bat_priv
->vis_hash
, free_info_ref
, NULL
);
971 bat_priv
->vis_hash
= NULL
;
972 bat_priv
->my_vis_info
= NULL
;
973 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
976 /* schedule packets for (re)transmission */
977 static void start_vis_timer(struct bat_priv
*bat_priv
)
979 INIT_DELAYED_WORK(&bat_priv
->vis_work
, send_vis_packets
);
980 queue_delayed_work(bat_event_workqueue
, &bat_priv
->vis_work
,
981 msecs_to_jiffies(VIS_INTERVAL
));
This page took 0.051267 seconds and 6 git commands to generate.