2 * Copyright (C) 2008-2011 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 /* Returns the smallest signed integer in two's complement with the sizeof x */
34 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
36 /* Checks if a sequence number x is a predecessor/successor of y.
37 * they handle overflows/underflows and can correctly check for a
38 * predecessor/successor unless the variable sequence number has grown by
39 * more then 2**(bitwidth(x)-1)-1.
40 * This means that for a uint8_t with the maximum value 255, it would think:
41 * - when adding nothing - it is neither a predecessor nor a successor
42 * - before adding more than 127 to the starting value - it is a predecessor,
43 * - when adding 128 - it is neither a predecessor nor a successor,
44 * - after adding more than 127 to the starting value - it is a successor */
45 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
46 _dummy > smallest_signed_int(_dummy); })
47 #define seq_after(x, y) seq_before(y, x)
49 static void start_vis_timer(struct bat_priv *bat_priv);
52 static void free_info(struct kref *ref)
54 struct vis_info *info = container_of(ref, struct vis_info, refcount);
55 struct bat_priv *bat_priv = info->bat_priv;
56 struct recvlist_node *entry, *tmp;
58 list_del_init(&info->send_list);
59 spin_lock_bh(&bat_priv->vis_list_lock);
60 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
61 list_del(&entry->list);
65 spin_unlock_bh(&bat_priv->vis_list_lock);
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_eth(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_eth(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_eth(entry->src, src))
169 return sprintf(buff, "TQ %pM %d, ", entry->dest,
175 int vis_seq_print_text(struct seq_file *seq, void *offset)
177 struct hlist_node *walk;
178 struct hlist_head *head;
179 struct element_t *bucket;
180 struct vis_info *info;
181 struct vis_packet *packet;
182 struct vis_info_entry *entries;
183 struct net_device *net_dev = (struct net_device *)seq->private;
184 struct bat_priv *bat_priv = netdev_priv(net_dev);
185 struct hashtable_t *hash = bat_priv->vis_hash;
186 HLIST_HEAD(vis_if_list);
187 struct if_list_entry *entry;
188 struct hlist_node *pos, *n;
190 int vis_server = atomic_read(&bat_priv->vis_mode);
191 size_t buff_pos, buf_size;
195 if ((!bat_priv->primary_if) ||
196 (vis_server == VIS_TYPE_CLIENT_UPDATE))
200 /* Estimate length */
201 spin_lock_bh(&bat_priv->vis_hash_lock);
202 for (i = 0; i < hash->size; i++) {
203 head = &hash->table[i];
205 hlist_for_each_entry(bucket, walk, head, hlist) {
207 packet = (struct vis_packet *)info->skb_packet->data;
208 entries = (struct vis_info_entry *)
209 ((char *)packet + sizeof(struct vis_packet));
211 for (j = 0; j < packet->entries; j++) {
212 if (entries[j].quality == 0)
215 compare_eth(entries[j].src, packet->vis_orig);
216 vis_data_insert_interface(entries[j].src,
221 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
222 buf_size += 18 + 26 * packet->entries;
224 /* add primary/secondary records */
225 if (compare_eth(entry->addr, packet->vis_orig))
227 vis_data_count_prim_sec(&vis_if_list);
232 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
234 hlist_del(&entry->list);
240 buff = kmalloc(buf_size, GFP_ATOMIC);
242 spin_unlock_bh(&bat_priv->vis_hash_lock);
248 for (i = 0; i < hash->size; i++) {
249 head = &hash->table[i];
251 hlist_for_each_entry(bucket, walk, head, hlist) {
253 packet = (struct vis_packet *)info->skb_packet->data;
254 entries = (struct vis_info_entry *)
255 ((char *)packet + sizeof(struct vis_packet));
257 for (j = 0; j < packet->entries; j++) {
258 if (entries[j].quality == 0)
261 compare_eth(entries[j].src, packet->vis_orig);
262 vis_data_insert_interface(entries[j].src,
267 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
268 buff_pos += sprintf(buff + buff_pos, "%pM,",
271 for (i = 0; i < packet->entries; i++)
272 buff_pos += vis_data_read_entry(
278 /* add primary/secondary records */
279 if (compare_eth(entry->addr, packet->vis_orig))
281 vis_data_read_prim_sec(buff + buff_pos,
284 buff_pos += sprintf(buff + buff_pos, "\n");
287 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
289 hlist_del(&entry->list);
295 spin_unlock_bh(&bat_priv->vis_hash_lock);
297 seq_printf(seq, "%s", buff);
303 /* add the info packet to the send list, if it was not
304 * already linked in. */
305 static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
307 if (list_empty(&info->send_list)) {
308 kref_get(&info->refcount);
309 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
313 /* delete the info packet from the send list, if it was
315 static void send_list_del(struct vis_info *info)
317 if (!list_empty(&info->send_list)) {
318 list_del_init(&info->send_list);
319 kref_put(&info->refcount, free_info);
323 /* tries to add one entry to the receive list. */
324 static void recv_list_add(struct bat_priv *bat_priv,
325 struct list_head *recv_list, char *mac)
327 struct recvlist_node *entry;
329 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
333 memcpy(entry->mac, mac, ETH_ALEN);
334 spin_lock_bh(&bat_priv->vis_list_lock);
335 list_add_tail(&entry->list, recv_list);
336 spin_unlock_bh(&bat_priv->vis_list_lock);
339 /* returns 1 if this mac is in the recv_list */
340 static int recv_list_is_in(struct bat_priv *bat_priv,
341 struct list_head *recv_list, char *mac)
343 struct recvlist_node *entry;
345 spin_lock_bh(&bat_priv->vis_list_lock);
346 list_for_each_entry(entry, recv_list, list) {
347 if (compare_eth(entry->mac, mac)) {
348 spin_unlock_bh(&bat_priv->vis_list_lock);
352 spin_unlock_bh(&bat_priv->vis_list_lock);
356 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
357 * broken.. ). vis hash must be locked outside. is_new is set when the packet
358 * is newer than old entries in the hash. */
359 static struct vis_info *add_packet(struct bat_priv *bat_priv,
360 struct vis_packet *vis_packet,
361 int vis_info_len, int *is_new,
364 struct vis_info *info, *old_info;
365 struct vis_packet *search_packet, *old_packet;
366 struct vis_info search_elem;
367 struct vis_packet *packet;
372 if (!bat_priv->vis_hash)
375 /* see if the packet is already in vis_hash */
376 search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet));
377 if (!search_elem.skb_packet)
379 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
380 sizeof(struct vis_packet));
382 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
384 old_info = hash_find(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
387 kfree_skb(search_elem.skb_packet);
390 old_packet = (struct vis_packet *)old_info->skb_packet->data;
391 if (!seq_after(ntohl(vis_packet->seqno),
392 ntohl(old_packet->seqno))) {
393 if (old_packet->seqno == vis_packet->seqno) {
394 recv_list_add(bat_priv, &old_info->recv_list,
395 vis_packet->sender_orig);
398 /* newer packet is already in hash. */
402 /* remove old entry */
403 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
405 send_list_del(old_info);
406 kref_put(&old_info->refcount, free_info);
409 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
413 info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
414 vis_info_len + sizeof(struct ethhdr));
415 if (!info->skb_packet) {
419 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
420 packet = (struct vis_packet *)skb_put(info->skb_packet,
421 sizeof(struct vis_packet) +
424 kref_init(&info->refcount);
425 INIT_LIST_HEAD(&info->send_list);
426 INIT_LIST_HEAD(&info->recv_list);
427 info->first_seen = jiffies;
428 info->bat_priv = bat_priv;
429 memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len);
431 /* initialize and add new packet. */
434 /* Make it a broadcast packet, if required */
436 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
438 /* repair if entries is longer than packet. */
439 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
440 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
442 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
445 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
447 if (hash_added < 0) {
448 /* did not work (for some reason) */
449 kref_put(&old_info->refcount, free_info);
456 /* handle the server sync packet, forward if needed. */
457 void receive_server_sync_packet(struct bat_priv *bat_priv,
458 struct vis_packet *vis_packet,
461 struct vis_info *info;
462 int is_new, make_broadcast;
463 int vis_server = atomic_read(&bat_priv->vis_mode);
465 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
467 spin_lock_bh(&bat_priv->vis_hash_lock);
468 info = add_packet(bat_priv, vis_packet, vis_info_len,
469 &is_new, make_broadcast);
473 /* only if we are server ourselves and packet is newer than the one in
475 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
476 send_list_add(bat_priv, info);
478 spin_unlock_bh(&bat_priv->vis_hash_lock);
481 /* handle an incoming client update packet and schedule forward if needed. */
482 void receive_client_update_packet(struct bat_priv *bat_priv,
483 struct vis_packet *vis_packet,
486 struct vis_info *info;
487 struct vis_packet *packet;
489 int vis_server = atomic_read(&bat_priv->vis_mode);
492 /* clients shall not broadcast. */
493 if (is_broadcast_ether_addr(vis_packet->target_orig))
496 /* Are we the target for this VIS packet? */
497 if (vis_server == VIS_TYPE_SERVER_SYNC &&
498 is_my_mac(vis_packet->target_orig))
501 spin_lock_bh(&bat_priv->vis_hash_lock);
502 info = add_packet(bat_priv, vis_packet, vis_info_len,
503 &is_new, are_target);
507 /* note that outdated packets will be dropped at this point. */
509 packet = (struct vis_packet *)info->skb_packet->data;
511 /* send only if we're the target server or ... */
512 if (are_target && is_new) {
513 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
514 send_list_add(bat_priv, info);
516 /* ... we're not the recipient (and thus need to forward). */
517 } else if (!is_my_mac(packet->target_orig)) {
518 send_list_add(bat_priv, info);
522 spin_unlock_bh(&bat_priv->vis_hash_lock);
525 /* Walk the originators and find the VIS server with the best tq. Set the packet
526 * address to its address and return the best_tq.
528 * Must be called with the originator hash locked */
529 static int find_best_vis_server(struct bat_priv *bat_priv,
530 struct vis_info *info)
532 struct hashtable_t *hash = bat_priv->orig_hash;
533 struct hlist_node *walk;
534 struct hlist_head *head;
535 struct element_t *bucket;
536 struct orig_node *orig_node;
537 struct vis_packet *packet;
540 packet = (struct vis_packet *)info->skb_packet->data;
542 for (i = 0; i < hash->size; i++) {
543 head = &hash->table[i];
546 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
547 orig_node = bucket->data;
548 if ((orig_node) && (orig_node->router) &&
549 (orig_node->flags & VIS_SERVER) &&
550 (orig_node->router->tq_avg > best_tq)) {
551 best_tq = orig_node->router->tq_avg;
552 memcpy(packet->target_orig, orig_node->orig,
562 /* Return true if the vis packet is full. */
563 static bool vis_packet_full(struct vis_info *info)
565 struct vis_packet *packet;
566 packet = (struct vis_packet *)info->skb_packet->data;
568 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
569 < packet->entries + 1)
574 /* generates a packet of own vis data,
575 * returns 0 on success, -1 if no packet could be generated */
576 static int generate_vis_packet(struct bat_priv *bat_priv)
578 struct hashtable_t *hash = bat_priv->orig_hash;
579 struct hlist_node *walk;
580 struct hlist_head *head;
581 struct element_t *bucket;
582 struct orig_node *orig_node;
583 struct neigh_node *neigh_node;
584 struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info;
585 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
586 struct vis_info_entry *entry;
587 struct hna_local_entry *hna_local_entry;
590 info->first_seen = jiffies;
591 packet->vis_type = atomic_read(&bat_priv->vis_mode);
593 spin_lock_bh(&bat_priv->orig_hash_lock);
594 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
596 packet->seqno = htonl(ntohl(packet->seqno) + 1);
598 skb_trim(info->skb_packet, sizeof(struct vis_packet));
600 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
601 best_tq = find_best_vis_server(bat_priv, info);
604 spin_unlock_bh(&bat_priv->orig_hash_lock);
609 for (i = 0; i < hash->size; i++) {
610 head = &hash->table[i];
613 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
614 orig_node = bucket->data;
615 neigh_node = orig_node->router;
620 if (!compare_eth(neigh_node->addr, orig_node->orig))
623 if (neigh_node->if_incoming->if_status != IF_ACTIVE)
626 if (neigh_node->tq_avg < 1)
629 /* fill one entry into buffer. */
630 entry = (struct vis_info_entry *)
631 skb_put(info->skb_packet, sizeof(*entry));
633 neigh_node->if_incoming->net_dev->dev_addr,
635 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
636 entry->quality = neigh_node->tq_avg;
639 if (vis_packet_full(info)) {
641 spin_unlock_bh(&bat_priv->orig_hash_lock);
648 spin_unlock_bh(&bat_priv->orig_hash_lock);
650 hash = bat_priv->hna_local_hash;
652 spin_lock_bh(&bat_priv->hna_lhash_lock);
653 for (i = 0; i < hash->size; i++) {
654 head = &hash->table[i];
656 hlist_for_each_entry(bucket, walk, head, hlist) {
657 hna_local_entry = bucket->data;
658 entry = (struct vis_info_entry *)
659 skb_put(info->skb_packet,
661 memset(entry->src, 0, ETH_ALEN);
662 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
663 entry->quality = 0; /* 0 means HNA */
666 if (vis_packet_full(info)) {
667 spin_unlock_bh(&bat_priv->hna_lhash_lock);
673 spin_unlock_bh(&bat_priv->hna_lhash_lock);
677 /* free old vis packets. Must be called with this vis_hash_lock
679 static void purge_vis_packets(struct bat_priv *bat_priv)
682 struct hashtable_t *hash = bat_priv->vis_hash;
683 struct hlist_node *walk, *safe;
684 struct hlist_head *head;
685 struct element_t *bucket;
686 struct vis_info *info;
688 for (i = 0; i < hash->size; i++) {
689 head = &hash->table[i];
691 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
694 /* never purge own data. */
695 if (info == bat_priv->my_vis_info)
698 if (time_after(jiffies,
699 info->first_seen + VIS_TIMEOUT * HZ)) {
703 kref_put(&info->refcount, free_info);
709 static void broadcast_vis_packet(struct bat_priv *bat_priv,
710 struct vis_info *info)
712 struct hashtable_t *hash = bat_priv->orig_hash;
713 struct hlist_node *walk;
714 struct hlist_head *head;
715 struct element_t *bucket;
716 struct orig_node *orig_node;
717 struct vis_packet *packet;
719 struct batman_if *batman_if;
720 uint8_t dstaddr[ETH_ALEN];
724 spin_lock_bh(&bat_priv->orig_hash_lock);
725 packet = (struct vis_packet *)info->skb_packet->data;
727 /* send to all routers in range. */
728 for (i = 0; i < hash->size; i++) {
729 head = &hash->table[i];
732 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
733 orig_node = bucket->data;
735 /* if it's a vis server and reachable, send it. */
736 if ((!orig_node) || (!orig_node->router))
738 if (!(orig_node->flags & VIS_SERVER))
740 /* don't send it if we already received the packet from
742 if (recv_list_is_in(bat_priv, &info->recv_list,
746 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
747 batman_if = orig_node->router->if_incoming;
748 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
749 spin_unlock_bh(&bat_priv->orig_hash_lock);
751 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
753 send_skb_packet(skb, batman_if, dstaddr);
755 spin_lock_bh(&bat_priv->orig_hash_lock);
760 spin_unlock_bh(&bat_priv->orig_hash_lock);
763 static void unicast_vis_packet(struct bat_priv *bat_priv,
764 struct vis_info *info)
766 struct orig_node *orig_node;
767 struct neigh_node *neigh_node = NULL;
769 struct vis_packet *packet;
770 struct batman_if *batman_if;
771 uint8_t dstaddr[ETH_ALEN];
773 packet = (struct vis_packet *)info->skb_packet->data;
775 spin_lock_bh(&bat_priv->orig_hash_lock);
777 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
778 compare_orig, choose_orig,
779 packet->target_orig));
784 kref_get(&orig_node->refcount);
785 neigh_node = orig_node->router;
790 if (!atomic_inc_not_zero(&neigh_node->refcount)) {
797 /* don't lock while sending the packets ... we therefore
798 * copy the required data before sending */
799 batman_if = orig_node->router->if_incoming;
800 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
801 spin_unlock_bh(&bat_priv->orig_hash_lock);
803 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
805 send_skb_packet(skb, batman_if, dstaddr);
811 spin_unlock_bh(&bat_priv->orig_hash_lock);
814 neigh_node_free_ref(neigh_node);
816 kref_put(&orig_node->refcount, orig_node_free_ref);
820 /* only send one vis packet. called from send_vis_packets() */
821 static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
823 struct vis_packet *packet;
825 packet = (struct vis_packet *)info->skb_packet->data;
826 if (packet->ttl < 2) {
827 pr_debug("Error - can't send vis packet: ttl exceeded\n");
831 memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
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->ttl++; /* restore TTL */
842 /* called from timer; send (and maybe generate) vis packet. */
843 static void send_vis_packets(struct work_struct *work)
845 struct delayed_work *delayed_work =
846 container_of(work, struct delayed_work, work);
847 struct bat_priv *bat_priv =
848 container_of(delayed_work, struct bat_priv, vis_work);
849 struct vis_info *info, *temp;
851 spin_lock_bh(&bat_priv->vis_hash_lock);
852 purge_vis_packets(bat_priv);
854 if (generate_vis_packet(bat_priv) == 0) {
855 /* schedule if generation was successful */
856 send_list_add(bat_priv, bat_priv->my_vis_info);
859 list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list,
862 kref_get(&info->refcount);
863 spin_unlock_bh(&bat_priv->vis_hash_lock);
865 if (bat_priv->primary_if)
866 send_vis_packet(bat_priv, info);
868 spin_lock_bh(&bat_priv->vis_hash_lock);
870 kref_put(&info->refcount, free_info);
872 spin_unlock_bh(&bat_priv->vis_hash_lock);
873 start_vis_timer(bat_priv);
876 /* init the vis server. this may only be called when if_list is already
877 * initialized (e.g. bat0 is initialized, interfaces have been added) */
878 int vis_init(struct bat_priv *bat_priv)
880 struct vis_packet *packet;
883 if (bat_priv->vis_hash)
886 spin_lock_bh(&bat_priv->vis_hash_lock);
888 bat_priv->vis_hash = hash_new(256);
889 if (!bat_priv->vis_hash) {
890 pr_err("Can't initialize vis_hash\n");
894 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
895 if (!bat_priv->my_vis_info) {
896 pr_err("Can't initialize vis packet\n");
900 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
901 sizeof(struct vis_packet) +
902 MAX_VIS_PACKET_SIZE +
903 sizeof(struct ethhdr));
904 if (!bat_priv->my_vis_info->skb_packet)
907 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
908 packet = (struct vis_packet *)skb_put(
909 bat_priv->my_vis_info->skb_packet,
910 sizeof(struct vis_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->version = COMPAT_VERSION;
920 packet->packet_type = BAT_VIS;
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 if (hash_added < 0) {
930 pr_err("Can't add own vis packet into hash\n");
931 /* not in hash, need to remove it manually. */
932 kref_put(&bat_priv->my_vis_info->refcount, free_info);
936 spin_unlock_bh(&bat_priv->vis_hash_lock);
937 start_vis_timer(bat_priv);
941 kfree(bat_priv->my_vis_info);
942 bat_priv->my_vis_info = NULL;
944 spin_unlock_bh(&bat_priv->vis_hash_lock);
949 /* Decrease the reference count on a hash item info */
950 static void free_info_ref(void *data, void *arg)
952 struct vis_info *info = data;
955 kref_put(&info->refcount, free_info);
958 /* shutdown vis-server */
959 void vis_quit(struct bat_priv *bat_priv)
961 if (!bat_priv->vis_hash)
964 cancel_delayed_work_sync(&bat_priv->vis_work);
966 spin_lock_bh(&bat_priv->vis_hash_lock);
967 /* properly remove, kill timers ... */
968 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
969 bat_priv->vis_hash = NULL;
970 bat_priv->my_vis_info = NULL;
971 spin_unlock_bh(&bat_priv->vis_hash_lock);
974 /* schedule packets for (re)transmission */
975 static void start_vis_timer(struct bat_priv *bat_priv)
977 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
978 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
979 msecs_to_jiffies(VIS_INTERVAL));