Staging: batman-adv: convert vis_interval into define
[linux-2.6/kvm.git] / drivers / staging / batman-adv / main.c
blob14ab4a0a88050cc258f5647b447472990968e280
1 /*
2 * Copyright (C) 2007-2010 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
18 * 02110-1301, USA
22 #include "main.h"
23 #include "bat_sysfs.h"
24 #include "routing.h"
25 #include "send.h"
26 #include "originator.h"
27 #include "soft-interface.h"
28 #include "device.h"
29 #include "translation-table.h"
30 #include "hard-interface.h"
31 #include "types.h"
32 #include "vis.h"
33 #include "hash.h"
35 struct list_head if_list;
36 struct hlist_head forw_bat_list;
37 struct hlist_head forw_bcast_list;
38 struct hashtable_t *orig_hash;
40 DEFINE_SPINLOCK(orig_hash_lock);
41 DEFINE_SPINLOCK(forw_bat_list_lock);
42 DEFINE_SPINLOCK(forw_bcast_list_lock);
44 atomic_t bcast_queue_left;
45 atomic_t batman_queue_left;
47 int16_t num_hna;
49 struct net_device *soft_device;
51 unsigned char broadcastAddr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
52 atomic_t module_state;
54 static struct packet_type batman_adv_packet_type __read_mostly = {
55 .type = __constant_htons(ETH_P_BATMAN),
56 .func = batman_skb_recv,
59 struct workqueue_struct *bat_event_workqueue;
61 #ifdef CONFIG_BATMAN_ADV_DEBUG
62 int debug;
64 module_param(debug, int, 0644);
66 int bat_debug_type(int type)
68 return debug & type;
70 #endif
72 int init_module(void)
74 int retval;
76 INIT_LIST_HEAD(&if_list);
77 INIT_HLIST_HEAD(&forw_bat_list);
78 INIT_HLIST_HEAD(&forw_bcast_list);
80 atomic_set(&module_state, MODULE_INACTIVE);
82 atomic_set(&bcast_queue_left, BCAST_QUEUE_LEN);
83 atomic_set(&batman_queue_left, BATMAN_QUEUE_LEN);
85 /* the name should not be longer than 10 chars - see
86 * http://lwn.net/Articles/23634/ */
87 bat_event_workqueue = create_singlethread_workqueue("bat_events");
89 if (!bat_event_workqueue)
90 return -ENOMEM;
92 bat_device_init();
94 /* initialize layer 2 interface */
95 soft_device = alloc_netdev(sizeof(struct bat_priv) , "bat%d",
96 interface_setup);
98 if (!soft_device) {
99 printk(KERN_ERR "batman-adv:"
100 "Unable to allocate the batman interface\n");
101 goto end;
104 retval = register_netdev(soft_device);
106 if (retval < 0) {
107 printk(KERN_ERR "batman-adv:"
108 "Unable to register the batman interface: %i\n", retval);
109 goto free_soft_device;
112 retval = sysfs_add_meshif(soft_device);
114 if (retval < 0)
115 goto unreg_soft_device;
117 register_netdevice_notifier(&hard_if_notifier);
118 dev_add_pack(&batman_adv_packet_type);
120 printk(KERN_INFO "batman-adv:"
121 "B.A.T.M.A.N. advanced %s%s (compatibility version %i) loaded\n",
122 SOURCE_VERSION, REVISION_VERSION_STR, COMPAT_VERSION);
124 return 0;
126 unreg_soft_device:
127 unregister_netdev(soft_device);
128 soft_device = NULL;
129 return -ENOMEM;
131 free_soft_device:
132 free_netdev(soft_device);
133 soft_device = NULL;
134 end:
135 return -ENOMEM;
138 void cleanup_module(void)
140 deactivate_module();
142 unregister_netdevice_notifier(&hard_if_notifier);
143 hardif_remove_interfaces();
145 if (soft_device) {
146 sysfs_del_meshif(soft_device);
147 unregister_netdev(soft_device);
148 soft_device = NULL;
151 dev_remove_pack(&batman_adv_packet_type);
153 destroy_workqueue(bat_event_workqueue);
154 bat_event_workqueue = NULL;
157 /* activates the module, creates bat device, starts timer ... */
158 void activate_module(void)
160 if (originator_init() < 1)
161 goto err;
163 if (hna_local_init() < 1)
164 goto err;
166 if (hna_global_init() < 1)
167 goto err;
169 hna_local_add(soft_device->dev_addr);
171 if (bat_device_setup() < 1)
172 goto end;
174 if (vis_init() < 1)
175 goto err;
177 update_min_mtu();
178 atomic_set(&module_state, MODULE_ACTIVE);
179 goto end;
181 err:
182 printk(KERN_ERR "batman-adv:"
183 "Unable to allocate memory for mesh information structures: "
184 "out of mem ?\n");
185 deactivate_module();
186 end:
187 return;
190 /* shuts down the whole module.*/
191 void deactivate_module(void)
193 atomic_set(&module_state, MODULE_DEACTIVATING);
195 purge_outstanding_packets(NULL);
196 flush_workqueue(bat_event_workqueue);
198 vis_quit();
200 /* TODO: unregister BATMAN pack */
202 originator_free();
204 hna_local_free();
205 hna_global_free();
207 synchronize_net();
208 bat_device_destroy();
210 synchronize_rcu();
211 atomic_set(&module_state, MODULE_INACTIVE);
214 void inc_module_count(void)
216 try_module_get(THIS_MODULE);
219 void dec_module_count(void)
221 module_put(THIS_MODULE);
224 int addr_to_string(char *buff, uint8_t *addr)
226 return sprintf(buff, MAC_FMT,
227 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
230 /* returns 1 if they are the same originator */
232 int compare_orig(void *data1, void *data2)
234 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
237 /* hashfunction to choose an entry in a hash table of given size */
238 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
239 int choose_orig(void *data, int32_t size)
241 unsigned char *key = data;
242 uint32_t hash = 0;
243 size_t i;
245 for (i = 0; i < 6; i++) {
246 hash += key[i];
247 hash += (hash << 10);
248 hash ^= (hash >> 6);
251 hash += (hash << 3);
252 hash ^= (hash >> 11);
253 hash += (hash << 15);
255 return hash % size;
258 int is_my_mac(uint8_t *addr)
260 struct batman_if *batman_if;
261 rcu_read_lock();
262 list_for_each_entry_rcu(batman_if, &if_list, list) {
263 if ((batman_if->net_dev) &&
264 (compare_orig(batman_if->net_dev->dev_addr, addr))) {
265 rcu_read_unlock();
266 return 1;
269 rcu_read_unlock();
270 return 0;
274 int is_bcast(uint8_t *addr)
276 return (addr[0] == (uint8_t)0xff) && (addr[1] == (uint8_t)0xff);
279 int is_mcast(uint8_t *addr)
281 return *addr & 0x01;
284 MODULE_LICENSE("GPL");
286 MODULE_AUTHOR(DRIVER_AUTHOR);
287 MODULE_DESCRIPTION(DRIVER_DESC);
288 MODULE_SUPPORTED_DEVICE(DRIVER_DEVICE);
289 #ifdef REVISION_VERSION
290 MODULE_VERSION(SOURCE_VERSION "-" REVISION_VERSION);
291 #else
292 MODULE_VERSION(SOURCE_VERSION);
293 #endif