blob: 3ff8bd1b7bdcf6fa9704a5d73461354b7b22a883 [file] [log] [blame]
Linus Luessingd6f94d92016-01-16 16:40:09 +08001/* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
2 *
3 * Linus Lüssing, Marek Lindner
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "bat_algo.h"
19#include "main.h"
20
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +080021#include <linux/atomic.h>
Antonio Quartulli97869062016-01-16 16:40:17 +080022#include <linux/bug.h>
Linus Luessingd6f94d92016-01-16 16:40:09 +080023#include <linux/cache.h>
24#include <linux/init.h>
Antonio Quartulli261e2642016-01-16 16:40:18 +080025#include <linux/jiffies.h>
26#include <linux/netdevice.h>
27#include <linux/rculist.h>
28#include <linux/rcupdate.h>
29#include <linux/seq_file.h>
Antonio Quartulli97869062016-01-16 16:40:17 +080030#include <linux/types.h>
Antonio Quartullic8334842015-11-10 18:50:51 +010031#include <linux/workqueue.h>
Linus Luessingd6f94d92016-01-16 16:40:09 +080032
33#include "bat_v_elp.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080034#include "bat_v_ogm.h"
Antonio Quartullib6cf5d42016-04-14 09:37:05 +080035#include "hard-interface.h"
Antonio Quartulli261e2642016-01-16 16:40:18 +080036#include "hash.h"
Antonio Quartulli97869062016-01-16 16:40:17 +080037#include "originator.h"
Linus Luessing162bd642016-01-16 16:40:10 +080038#include "packet.h"
Linus Luessingd6f94d92016-01-16 16:40:09 +080039
Antonio Quartullib6cf5d42016-04-14 09:37:05 +080040static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
41{
42 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
43 * set the interface as ACTIVE right away, without any risk of race
44 * condition
45 */
46 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
47 hard_iface->if_status = BATADV_IF_ACTIVE;
48}
49
Linus Luessingd6f94d92016-01-16 16:40:09 +080050static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
51{
Antonio Quartulli0da00352016-01-16 16:40:12 +080052 int ret;
53
54 ret = batadv_v_elp_iface_enable(hard_iface);
55 if (ret < 0)
56 return ret;
57
58 ret = batadv_v_ogm_iface_enable(hard_iface);
59 if (ret < 0)
60 batadv_v_elp_iface_disable(hard_iface);
61
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +080062 /* enable link throughput auto-detection by setting the throughput
63 * override to zero
64 */
65 atomic_set(&hard_iface->bat_v.throughput_override, 0);
66
Antonio Quartulli0da00352016-01-16 16:40:12 +080067 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +080068}
69
70static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
71{
72 batadv_v_elp_iface_disable(hard_iface);
73}
74
75static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
76{
77}
78
79static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
80{
81 batadv_v_elp_primary_iface_set(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +080082 batadv_v_ogm_primary_iface_set(hard_iface);
Linus Luessingd6f94d92016-01-16 16:40:09 +080083}
84
Linus Luessing162bd642016-01-16 16:40:10 +080085static void
86batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
87{
88 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
Antonio Quartullic8334842015-11-10 18:50:51 +010089 INIT_WORK(&hardif_neigh->bat_v.metric_work,
90 batadv_v_elp_throughput_metric_update);
Linus Luessing162bd642016-01-16 16:40:10 +080091}
92
Linus Luessingd6f94d92016-01-16 16:40:09 +080093static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface)
94{
95}
96
97static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet)
98{
99}
100
Antonio Quartulli261e2642016-01-16 16:40:18 +0800101/**
102 * batadv_v_orig_print_neigh - print neighbors for the originator table
103 * @orig_node: the orig_node for which the neighbors are printed
104 * @if_outgoing: outgoing interface for these entries
105 * @seq: debugfs table seq_file struct
106 *
107 * Must be called while holding an rcu lock.
108 */
109static void
110batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
111 struct batadv_hard_iface *if_outgoing,
112 struct seq_file *seq)
113{
114 struct batadv_neigh_node *neigh_node;
115 struct batadv_neigh_ifinfo *n_ifinfo;
116
117 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
118 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
119 if (!n_ifinfo)
120 continue;
121
122 seq_printf(seq, " %pM (%9u.%1u)",
123 neigh_node->addr,
124 n_ifinfo->bat_v.throughput / 10,
125 n_ifinfo->bat_v.throughput % 10);
126
127 batadv_neigh_ifinfo_put(n_ifinfo);
128 }
129}
130
131/**
Linus Luessing626d23e2016-01-16 16:40:19 +0800132 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
133 * @seq: neighbour table seq_file struct
134 * @hardif_neigh: hardif neighbour information
135 */
136static void
137batadv_v_hardif_neigh_print(struct seq_file *seq,
138 struct batadv_hardif_neigh_node *hardif_neigh)
139{
140 int last_secs, last_msecs;
141 u32 throughput;
142
143 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
144 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
145 throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
146
147 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
148 hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
149 throughput % 10, hardif_neigh->if_incoming->net_dev->name);
150}
151
152/**
153 * batadv_v_neigh_print - print the single hop neighbour list
154 * @bat_priv: the bat priv with all the soft interface information
155 * @seq: neighbour table seq_file struct
156 */
157static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
158 struct seq_file *seq)
159{
160 struct net_device *net_dev = (struct net_device *)seq->private;
161 struct batadv_hardif_neigh_node *hardif_neigh;
162 struct batadv_hard_iface *hard_iface;
163 int batman_count = 0;
164
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100165 seq_puts(seq,
166 " Neighbor last-seen ( throughput) [ IF]\n");
Linus Luessing626d23e2016-01-16 16:40:19 +0800167
168 rcu_read_lock();
169 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
170 if (hard_iface->soft_iface != net_dev)
171 continue;
172
173 hlist_for_each_entry_rcu(hardif_neigh,
174 &hard_iface->neigh_list, list) {
175 batadv_v_hardif_neigh_print(seq, hardif_neigh);
176 batman_count++;
177 }
178 }
179 rcu_read_unlock();
180
181 if (batman_count == 0)
182 seq_puts(seq, "No batman nodes in range ...\n");
183}
184
185/**
Antonio Quartulli261e2642016-01-16 16:40:18 +0800186 * batadv_v_orig_print - print the originator table
187 * @bat_priv: the bat priv with all the soft interface information
188 * @seq: debugfs table seq_file struct
189 * @if_outgoing: the outgoing interface for which this should be printed
190 */
191static void batadv_v_orig_print(struct batadv_priv *bat_priv,
192 struct seq_file *seq,
193 struct batadv_hard_iface *if_outgoing)
194{
195 struct batadv_neigh_node *neigh_node;
196 struct batadv_hashtable *hash = bat_priv->orig_hash;
197 int last_seen_msecs, last_seen_secs;
198 struct batadv_orig_node *orig_node;
199 struct batadv_neigh_ifinfo *n_ifinfo;
200 unsigned long last_seen_jiffies;
201 struct hlist_head *head;
202 int batman_count = 0;
203 u32 i;
204
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100205 seq_puts(seq,
206 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli261e2642016-01-16 16:40:18 +0800207
208 for (i = 0; i < hash->size; i++) {
209 head = &hash->table[i];
210
211 rcu_read_lock();
212 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
213 neigh_node = batadv_orig_router_get(orig_node,
214 if_outgoing);
215 if (!neigh_node)
216 continue;
217
218 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
219 if_outgoing);
220 if (!n_ifinfo)
221 goto next;
222
223 last_seen_jiffies = jiffies - orig_node->last_seen;
224 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
225 last_seen_secs = last_seen_msecs / 1000;
226 last_seen_msecs = last_seen_msecs % 1000;
227
228 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
229 orig_node->orig, last_seen_secs,
230 last_seen_msecs,
231 n_ifinfo->bat_v.throughput / 10,
232 n_ifinfo->bat_v.throughput % 10,
233 neigh_node->addr,
234 neigh_node->if_incoming->net_dev->name);
235
236 batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
237 seq_puts(seq, "\n");
238 batman_count++;
239
240next:
241 batadv_neigh_node_put(neigh_node);
242 if (n_ifinfo)
243 batadv_neigh_ifinfo_put(n_ifinfo);
244 }
245 rcu_read_unlock();
246 }
247
248 if (batman_count == 0)
249 seq_puts(seq, "No batman nodes in range ...\n");
250}
251
Antonio Quartulli97869062016-01-16 16:40:17 +0800252static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
253 struct batadv_hard_iface *if_outgoing1,
254 struct batadv_neigh_node *neigh2,
255 struct batadv_hard_iface *if_outgoing2)
256{
257 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
258
259 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
260 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
261
262 if (WARN_ON(!ifinfo1 || !ifinfo2))
263 return 0;
264
265 return ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
266}
267
268static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
269 struct batadv_hard_iface *if_outgoing1,
270 struct batadv_neigh_node *neigh2,
271 struct batadv_hard_iface *if_outgoing2)
272{
273 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
274 u32 threshold;
275
276 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
277 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
278
279 threshold = ifinfo1->bat_v.throughput / 4;
280 threshold = ifinfo1->bat_v.throughput - threshold;
281
282 return ifinfo2->bat_v.throughput > threshold;
283}
284
Linus Luessingd6f94d92016-01-16 16:40:09 +0800285static struct batadv_algo_ops batadv_batman_v __read_mostly = {
286 .name = "BATMAN_V",
Antonio Quartullib6cf5d42016-04-14 09:37:05 +0800287 .bat_iface_activate = batadv_v_iface_activate,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800288 .bat_iface_enable = batadv_v_iface_enable,
289 .bat_iface_disable = batadv_v_iface_disable,
290 .bat_iface_update_mac = batadv_v_iface_update_mac,
291 .bat_primary_iface_set = batadv_v_primary_iface_set,
Linus Luessing162bd642016-01-16 16:40:10 +0800292 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800293 .bat_ogm_emit = batadv_v_ogm_emit,
294 .bat_ogm_schedule = batadv_v_ogm_schedule,
Antonio Quartulli261e2642016-01-16 16:40:18 +0800295 .bat_orig_print = batadv_v_orig_print,
Antonio Quartulli97869062016-01-16 16:40:17 +0800296 .bat_neigh_cmp = batadv_v_neigh_cmp,
297 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
Linus Luessing626d23e2016-01-16 16:40:19 +0800298 .bat_neigh_print = batadv_v_neigh_print,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800299};
300
301/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800302 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
303 * mesh
304 * @bat_priv: the object representing the mesh interface to initialise
305 *
306 * Return: 0 on success or a negative error code otherwise
307 */
308int batadv_v_mesh_init(struct batadv_priv *bat_priv)
309{
310 return batadv_v_ogm_init(bat_priv);
311}
312
313/**
314 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
315 * @bat_priv: the object representing the mesh interface to free
316 */
317void batadv_v_mesh_free(struct batadv_priv *bat_priv)
318{
319 batadv_v_ogm_free(bat_priv);
320}
321
322/**
Linus Luessingd6f94d92016-01-16 16:40:09 +0800323 * batadv_v_init - B.A.T.M.A.N. V initialization function
324 *
325 * Description: Takes care of initializing all the subcomponents.
326 * It is invoked upon module load only.
327 *
328 * Return: 0 on success or a negative error code otherwise
329 */
330int __init batadv_v_init(void)
331{
Linus Luessing162bd642016-01-16 16:40:10 +0800332 int ret;
333
334 /* B.A.T.M.A.N. V echo location protocol packet */
335 ret = batadv_recv_handler_register(BATADV_ELP,
336 batadv_v_elp_packet_recv);
337 if (ret < 0)
338 return ret;
339
Antonio Quartulli0da00352016-01-16 16:40:12 +0800340 ret = batadv_recv_handler_register(BATADV_OGM2,
341 batadv_v_ogm_packet_recv);
Linus Luessing162bd642016-01-16 16:40:10 +0800342 if (ret < 0)
Antonio Quartulli0da00352016-01-16 16:40:12 +0800343 goto elp_unregister;
344
345 ret = batadv_algo_register(&batadv_batman_v);
346 if (ret < 0)
347 goto ogm_unregister;
348
349 return ret;
350
351ogm_unregister:
352 batadv_recv_handler_unregister(BATADV_OGM2);
353
354elp_unregister:
355 batadv_recv_handler_unregister(BATADV_ELP);
Linus Luessing162bd642016-01-16 16:40:10 +0800356
357 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +0800358}