batman-adv: Consolidate logging related functions
[deliverable/linux.git] / net / batman-adv / bat_v.c
CommitLineData
d6f94d91
LL
1/* 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
0b5ecc68 21#include <linux/atomic.h>
97869060 22#include <linux/bug.h>
d6f94d91
LL
23#include <linux/cache.h>
24#include <linux/init.h>
261e264d
AQ
25#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>
a45e932a 30#include <linux/stddef.h>
97869060 31#include <linux/types.h>
c833484e 32#include <linux/workqueue.h>
d6f94d91
LL
33
34#include "bat_v_elp.h"
0da00359 35#include "bat_v_ogm.h"
b6cf5d49 36#include "hard-interface.h"
261e264d 37#include "hash.h"
97869060 38#include "originator.h"
162bd64c 39#include "packet.h"
d6f94d91 40
b6cf5d49
AQ
41static void batadv_v_iface_activate(struct batadv_hard_iface *hard_iface)
42{
ebe24cea
ML
43 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
44 struct batadv_hard_iface *primary_if;
45
46 primary_if = batadv_primary_if_get_selected(bat_priv);
47
48 if (primary_if) {
49 batadv_v_elp_iface_activate(primary_if, hard_iface);
50 batadv_hardif_put(primary_if);
51 }
52
b6cf5d49
AQ
53 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
54 * set the interface as ACTIVE right away, without any risk of race
55 * condition
56 */
57 if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
58 hard_iface->if_status = BATADV_IF_ACTIVE;
59}
60
d6f94d91
LL
61static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
62{
0da00359
AQ
63 int ret;
64
65 ret = batadv_v_elp_iface_enable(hard_iface);
66 if (ret < 0)
67 return ret;
68
69 ret = batadv_v_ogm_iface_enable(hard_iface);
70 if (ret < 0)
71 batadv_v_elp_iface_disable(hard_iface);
72
73 return ret;
d6f94d91
LL
74}
75
76static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
77{
78 batadv_v_elp_iface_disable(hard_iface);
79}
80
d6f94d91
LL
81static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
82{
83 batadv_v_elp_primary_iface_set(hard_iface);
0da00359 84 batadv_v_ogm_primary_iface_set(hard_iface);
d6f94d91
LL
85}
86
1653f61d
AQ
87/**
88 * batadv_v_iface_update_mac - react to hard-interface MAC address change
89 * @hard_iface: the modified interface
90 *
91 * If the modified interface is the primary one, update the originator
92 * address in the ELP and OGM messages to reflect the new MAC address.
93 */
94static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
95{
96 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
97 struct batadv_hard_iface *primary_if;
98
99 primary_if = batadv_primary_if_get_selected(bat_priv);
100 if (primary_if != hard_iface)
101 goto out;
102
103 batadv_v_primary_iface_set(hard_iface);
104out:
105 if (primary_if)
106 batadv_hardif_put(primary_if);
107}
108
162bd64c
LL
109static void
110batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
111{
112 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
c833484e
AQ
113 INIT_WORK(&hardif_neigh->bat_v.metric_work,
114 batadv_v_elp_throughput_metric_update);
162bd64c
LL
115}
116
261e264d
AQ
117/**
118 * batadv_v_orig_print_neigh - print neighbors for the originator table
119 * @orig_node: the orig_node for which the neighbors are printed
120 * @if_outgoing: outgoing interface for these entries
121 * @seq: debugfs table seq_file struct
122 *
123 * Must be called while holding an rcu lock.
124 */
125static void
126batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
127 struct batadv_hard_iface *if_outgoing,
128 struct seq_file *seq)
129{
130 struct batadv_neigh_node *neigh_node;
131 struct batadv_neigh_ifinfo *n_ifinfo;
132
133 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
134 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
135 if (!n_ifinfo)
136 continue;
137
138 seq_printf(seq, " %pM (%9u.%1u)",
139 neigh_node->addr,
140 n_ifinfo->bat_v.throughput / 10,
141 n_ifinfo->bat_v.throughput % 10);
142
143 batadv_neigh_ifinfo_put(n_ifinfo);
144 }
145}
146
626d23e8
LL
147/**
148 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
149 * @seq: neighbour table seq_file struct
150 * @hardif_neigh: hardif neighbour information
151 */
152static void
153batadv_v_hardif_neigh_print(struct seq_file *seq,
154 struct batadv_hardif_neigh_node *hardif_neigh)
155{
156 int last_secs, last_msecs;
157 u32 throughput;
158
159 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
160 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
161 throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
162
163 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
164 hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
165 throughput % 10, hardif_neigh->if_incoming->net_dev->name);
166}
167
168/**
169 * batadv_v_neigh_print - print the single hop neighbour list
170 * @bat_priv: the bat priv with all the soft interface information
171 * @seq: neighbour table seq_file struct
172 */
173static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
174 struct seq_file *seq)
175{
176 struct net_device *net_dev = (struct net_device *)seq->private;
177 struct batadv_hardif_neigh_node *hardif_neigh;
178 struct batadv_hard_iface *hard_iface;
179 int batman_count = 0;
180
925a6f37
AQ
181 seq_puts(seq,
182 " Neighbor last-seen ( throughput) [ IF]\n");
626d23e8
LL
183
184 rcu_read_lock();
185 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
186 if (hard_iface->soft_iface != net_dev)
187 continue;
188
189 hlist_for_each_entry_rcu(hardif_neigh,
190 &hard_iface->neigh_list, list) {
191 batadv_v_hardif_neigh_print(seq, hardif_neigh);
192 batman_count++;
193 }
194 }
195 rcu_read_unlock();
196
197 if (batman_count == 0)
198 seq_puts(seq, "No batman nodes in range ...\n");
199}
200
261e264d
AQ
201/**
202 * batadv_v_orig_print - print the originator table
203 * @bat_priv: the bat priv with all the soft interface information
204 * @seq: debugfs table seq_file struct
205 * @if_outgoing: the outgoing interface for which this should be printed
206 */
207static void batadv_v_orig_print(struct batadv_priv *bat_priv,
208 struct seq_file *seq,
209 struct batadv_hard_iface *if_outgoing)
210{
211 struct batadv_neigh_node *neigh_node;
212 struct batadv_hashtable *hash = bat_priv->orig_hash;
213 int last_seen_msecs, last_seen_secs;
214 struct batadv_orig_node *orig_node;
215 struct batadv_neigh_ifinfo *n_ifinfo;
216 unsigned long last_seen_jiffies;
217 struct hlist_head *head;
218 int batman_count = 0;
219 u32 i;
220
925a6f37
AQ
221 seq_puts(seq,
222 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
261e264d
AQ
223
224 for (i = 0; i < hash->size; i++) {
225 head = &hash->table[i];
226
227 rcu_read_lock();
228 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
229 neigh_node = batadv_orig_router_get(orig_node,
230 if_outgoing);
231 if (!neigh_node)
232 continue;
233
234 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
235 if_outgoing);
236 if (!n_ifinfo)
237 goto next;
238
239 last_seen_jiffies = jiffies - orig_node->last_seen;
240 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
241 last_seen_secs = last_seen_msecs / 1000;
242 last_seen_msecs = last_seen_msecs % 1000;
243
244 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
245 orig_node->orig, last_seen_secs,
246 last_seen_msecs,
247 n_ifinfo->bat_v.throughput / 10,
248 n_ifinfo->bat_v.throughput % 10,
249 neigh_node->addr,
250 neigh_node->if_incoming->net_dev->name);
251
252 batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
253 seq_puts(seq, "\n");
254 batman_count++;
255
256next:
257 batadv_neigh_node_put(neigh_node);
258 if (n_ifinfo)
259 batadv_neigh_ifinfo_put(n_ifinfo);
260 }
261 rcu_read_unlock();
262 }
263
264 if (batman_count == 0)
265 seq_puts(seq, "No batman nodes in range ...\n");
266}
267
97869060
AQ
268static int batadv_v_neigh_cmp(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;
71f9d27d 274 int ret = 0;
97869060
AQ
275
276 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
71f9d27d
SE
277 if (WARN_ON(!ifinfo1))
278 goto err_ifinfo1;
279
97869060 280 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
71f9d27d
SE
281 if (WARN_ON(!ifinfo2))
282 goto err_ifinfo2;
97869060 283
71f9d27d 284 ret = ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
97869060 285
71f9d27d
SE
286 batadv_neigh_ifinfo_put(ifinfo2);
287err_ifinfo2:
288 batadv_neigh_ifinfo_put(ifinfo1);
289err_ifinfo1:
290 return ret;
97869060
AQ
291}
292
293static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
294 struct batadv_hard_iface *if_outgoing1,
295 struct batadv_neigh_node *neigh2,
296 struct batadv_hard_iface *if_outgoing2)
297{
298 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
299 u32 threshold;
71f9d27d 300 bool ret = false;
97869060
AQ
301
302 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
71f9d27d
SE
303 if (WARN_ON(!ifinfo1))
304 goto err_ifinfo1;
97869060 305
71f9d27d
SE
306 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
307 if (WARN_ON(!ifinfo2))
308 goto err_ifinfo2;
a45e932a 309
97869060
AQ
310 threshold = ifinfo1->bat_v.throughput / 4;
311 threshold = ifinfo1->bat_v.throughput - threshold;
312
71f9d27d
SE
313 ret = ifinfo2->bat_v.throughput > threshold;
314
315 batadv_neigh_ifinfo_put(ifinfo2);
316err_ifinfo2:
317 batadv_neigh_ifinfo_put(ifinfo1);
318err_ifinfo1:
319 return ret;
97869060
AQ
320}
321
d6f94d91
LL
322static struct batadv_algo_ops batadv_batman_v __read_mostly = {
323 .name = "BATMAN_V",
b6cf5d49 324 .bat_iface_activate = batadv_v_iface_activate,
d6f94d91
LL
325 .bat_iface_enable = batadv_v_iface_enable,
326 .bat_iface_disable = batadv_v_iface_disable,
327 .bat_iface_update_mac = batadv_v_iface_update_mac,
328 .bat_primary_iface_set = batadv_v_primary_iface_set,
162bd64c 329 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
261e264d 330 .bat_orig_print = batadv_v_orig_print,
97869060
AQ
331 .bat_neigh_cmp = batadv_v_neigh_cmp,
332 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
626d23e8 333 .bat_neigh_print = batadv_v_neigh_print,
d6f94d91
LL
334};
335
7db682d1
ML
336/**
337 * batadv_v_hardif_init - initialize the algorithm specific fields in the
338 * hard-interface object
339 * @hard_iface: the hard-interface to initialize
340 */
341void batadv_v_hardif_init(struct batadv_hard_iface *hard_iface)
342{
343 /* enable link throughput auto-detection by setting the throughput
344 * override to zero
345 */
346 atomic_set(&hard_iface->bat_v.throughput_override, 0);
347 atomic_set(&hard_iface->bat_v.elp_interval, 500);
348}
349
0da00359
AQ
350/**
351 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
352 * mesh
353 * @bat_priv: the object representing the mesh interface to initialise
354 *
355 * Return: 0 on success or a negative error code otherwise
356 */
357int batadv_v_mesh_init(struct batadv_priv *bat_priv)
358{
359 return batadv_v_ogm_init(bat_priv);
360}
361
362/**
363 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
364 * @bat_priv: the object representing the mesh interface to free
365 */
366void batadv_v_mesh_free(struct batadv_priv *bat_priv)
367{
368 batadv_v_ogm_free(bat_priv);
369}
370
d6f94d91
LL
371/**
372 * batadv_v_init - B.A.T.M.A.N. V initialization function
373 *
374 * Description: Takes care of initializing all the subcomponents.
375 * It is invoked upon module load only.
376 *
377 * Return: 0 on success or a negative error code otherwise
378 */
379int __init batadv_v_init(void)
380{
162bd64c
LL
381 int ret;
382
383 /* B.A.T.M.A.N. V echo location protocol packet */
384 ret = batadv_recv_handler_register(BATADV_ELP,
385 batadv_v_elp_packet_recv);
386 if (ret < 0)
387 return ret;
388
0da00359
AQ
389 ret = batadv_recv_handler_register(BATADV_OGM2,
390 batadv_v_ogm_packet_recv);
391 if (ret < 0)
392 goto elp_unregister;
162bd64c 393
0da00359 394 ret = batadv_algo_register(&batadv_batman_v);
162bd64c 395 if (ret < 0)
0da00359
AQ
396 goto ogm_unregister;
397
398 return ret;
399
400ogm_unregister:
401 batadv_recv_handler_unregister(BATADV_OGM2);
402
403elp_unregister:
404 batadv_recv_handler_unregister(BATADV_ELP);
162bd64c
LL
405
406 return ret;
d6f94d91 407}
This page took 0.06149 seconds and 5 git commands to generate.