blob: 246f9e9598490437704b1cd1a5b7c01c783859eb [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 Quartulli261e2642016-01-16 16:40:18 +080035#include "hash.h"
Antonio Quartulli97869062016-01-16 16:40:17 +080036#include "originator.h"
Linus Luessing162bd642016-01-16 16:40:10 +080037#include "packet.h"
Linus Luessingd6f94d92016-01-16 16:40:09 +080038
39static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
40{
Antonio Quartulli0da00352016-01-16 16:40:12 +080041 int ret;
42
43 ret = batadv_v_elp_iface_enable(hard_iface);
44 if (ret < 0)
45 return ret;
46
47 ret = batadv_v_ogm_iface_enable(hard_iface);
48 if (ret < 0)
49 batadv_v_elp_iface_disable(hard_iface);
50
Antonio Quartulli0b5ecc62016-01-16 16:40:14 +080051 /* enable link throughput auto-detection by setting the throughput
52 * override to zero
53 */
54 atomic_set(&hard_iface->bat_v.throughput_override, 0);
55
Antonio Quartulli0da00352016-01-16 16:40:12 +080056 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +080057}
58
59static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
60{
61 batadv_v_elp_iface_disable(hard_iface);
62}
63
64static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
65{
66}
67
68static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
69{
70 batadv_v_elp_primary_iface_set(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +080071 batadv_v_ogm_primary_iface_set(hard_iface);
Linus Luessingd6f94d92016-01-16 16:40:09 +080072}
73
Linus Luessing162bd642016-01-16 16:40:10 +080074static void
75batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
76{
77 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
Antonio Quartullic8334842015-11-10 18:50:51 +010078 INIT_WORK(&hardif_neigh->bat_v.metric_work,
79 batadv_v_elp_throughput_metric_update);
Linus Luessing162bd642016-01-16 16:40:10 +080080}
81
Linus Luessingd6f94d92016-01-16 16:40:09 +080082static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface)
83{
84}
85
86static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet)
87{
88}
89
Antonio Quartulli261e2642016-01-16 16:40:18 +080090/**
91 * batadv_v_orig_print_neigh - print neighbors for the originator table
92 * @orig_node: the orig_node for which the neighbors are printed
93 * @if_outgoing: outgoing interface for these entries
94 * @seq: debugfs table seq_file struct
95 *
96 * Must be called while holding an rcu lock.
97 */
98static void
99batadv_v_orig_print_neigh(struct batadv_orig_node *orig_node,
100 struct batadv_hard_iface *if_outgoing,
101 struct seq_file *seq)
102{
103 struct batadv_neigh_node *neigh_node;
104 struct batadv_neigh_ifinfo *n_ifinfo;
105
106 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
107 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
108 if (!n_ifinfo)
109 continue;
110
111 seq_printf(seq, " %pM (%9u.%1u)",
112 neigh_node->addr,
113 n_ifinfo->bat_v.throughput / 10,
114 n_ifinfo->bat_v.throughput % 10);
115
116 batadv_neigh_ifinfo_put(n_ifinfo);
117 }
118}
119
120/**
Linus Luessing626d23e2016-01-16 16:40:19 +0800121 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
122 * @seq: neighbour table seq_file struct
123 * @hardif_neigh: hardif neighbour information
124 */
125static void
126batadv_v_hardif_neigh_print(struct seq_file *seq,
127 struct batadv_hardif_neigh_node *hardif_neigh)
128{
129 int last_secs, last_msecs;
130 u32 throughput;
131
132 last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
133 last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
134 throughput = ewma_throughput_read(&hardif_neigh->bat_v.throughput);
135
136 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
137 hardif_neigh->addr, last_secs, last_msecs, throughput / 10,
138 throughput % 10, hardif_neigh->if_incoming->net_dev->name);
139}
140
141/**
142 * batadv_v_neigh_print - print the single hop neighbour list
143 * @bat_priv: the bat priv with all the soft interface information
144 * @seq: neighbour table seq_file struct
145 */
146static void batadv_v_neigh_print(struct batadv_priv *bat_priv,
147 struct seq_file *seq)
148{
149 struct net_device *net_dev = (struct net_device *)seq->private;
150 struct batadv_hardif_neigh_node *hardif_neigh;
151 struct batadv_hard_iface *hard_iface;
152 int batman_count = 0;
153
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100154 seq_puts(seq,
155 " Neighbor last-seen ( throughput) [ IF]\n");
Linus Luessing626d23e2016-01-16 16:40:19 +0800156
157 rcu_read_lock();
158 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
159 if (hard_iface->soft_iface != net_dev)
160 continue;
161
162 hlist_for_each_entry_rcu(hardif_neigh,
163 &hard_iface->neigh_list, list) {
164 batadv_v_hardif_neigh_print(seq, hardif_neigh);
165 batman_count++;
166 }
167 }
168 rcu_read_unlock();
169
170 if (batman_count == 0)
171 seq_puts(seq, "No batman nodes in range ...\n");
172}
173
174/**
Antonio Quartulli261e2642016-01-16 16:40:18 +0800175 * batadv_v_orig_print - print the originator table
176 * @bat_priv: the bat priv with all the soft interface information
177 * @seq: debugfs table seq_file struct
178 * @if_outgoing: the outgoing interface for which this should be printed
179 */
180static void batadv_v_orig_print(struct batadv_priv *bat_priv,
181 struct seq_file *seq,
182 struct batadv_hard_iface *if_outgoing)
183{
184 struct batadv_neigh_node *neigh_node;
185 struct batadv_hashtable *hash = bat_priv->orig_hash;
186 int last_seen_msecs, last_seen_secs;
187 struct batadv_orig_node *orig_node;
188 struct batadv_neigh_ifinfo *n_ifinfo;
189 unsigned long last_seen_jiffies;
190 struct hlist_head *head;
191 int batman_count = 0;
192 u32 i;
193
Antonio Quartulli925a6f32016-03-12 10:30:18 +0100194 seq_puts(seq,
195 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
Antonio Quartulli261e2642016-01-16 16:40:18 +0800196
197 for (i = 0; i < hash->size; i++) {
198 head = &hash->table[i];
199
200 rcu_read_lock();
201 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
202 neigh_node = batadv_orig_router_get(orig_node,
203 if_outgoing);
204 if (!neigh_node)
205 continue;
206
207 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
208 if_outgoing);
209 if (!n_ifinfo)
210 goto next;
211
212 last_seen_jiffies = jiffies - orig_node->last_seen;
213 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
214 last_seen_secs = last_seen_msecs / 1000;
215 last_seen_msecs = last_seen_msecs % 1000;
216
217 seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
218 orig_node->orig, last_seen_secs,
219 last_seen_msecs,
220 n_ifinfo->bat_v.throughput / 10,
221 n_ifinfo->bat_v.throughput % 10,
222 neigh_node->addr,
223 neigh_node->if_incoming->net_dev->name);
224
225 batadv_v_orig_print_neigh(orig_node, if_outgoing, seq);
226 seq_puts(seq, "\n");
227 batman_count++;
228
229next:
230 batadv_neigh_node_put(neigh_node);
231 if (n_ifinfo)
232 batadv_neigh_ifinfo_put(n_ifinfo);
233 }
234 rcu_read_unlock();
235 }
236
237 if (batman_count == 0)
238 seq_puts(seq, "No batman nodes in range ...\n");
239}
240
Antonio Quartulli97869062016-01-16 16:40:17 +0800241static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1,
242 struct batadv_hard_iface *if_outgoing1,
243 struct batadv_neigh_node *neigh2,
244 struct batadv_hard_iface *if_outgoing2)
245{
246 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
247
248 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
249 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
250
251 if (WARN_ON(!ifinfo1 || !ifinfo2))
252 return 0;
253
254 return ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput;
255}
256
257static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1,
258 struct batadv_hard_iface *if_outgoing1,
259 struct batadv_neigh_node *neigh2,
260 struct batadv_hard_iface *if_outgoing2)
261{
262 struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2;
263 u32 threshold;
264
265 ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
266 ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
267
268 threshold = ifinfo1->bat_v.throughput / 4;
269 threshold = ifinfo1->bat_v.throughput - threshold;
270
271 return ifinfo2->bat_v.throughput > threshold;
272}
273
Linus Luessingd6f94d92016-01-16 16:40:09 +0800274static struct batadv_algo_ops batadv_batman_v __read_mostly = {
275 .name = "BATMAN_V",
276 .bat_iface_enable = batadv_v_iface_enable,
277 .bat_iface_disable = batadv_v_iface_disable,
278 .bat_iface_update_mac = batadv_v_iface_update_mac,
279 .bat_primary_iface_set = batadv_v_primary_iface_set,
Linus Luessing162bd642016-01-16 16:40:10 +0800280 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800281 .bat_ogm_emit = batadv_v_ogm_emit,
282 .bat_ogm_schedule = batadv_v_ogm_schedule,
Antonio Quartulli261e2642016-01-16 16:40:18 +0800283 .bat_orig_print = batadv_v_orig_print,
Antonio Quartulli97869062016-01-16 16:40:17 +0800284 .bat_neigh_cmp = batadv_v_neigh_cmp,
285 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
Linus Luessing626d23e2016-01-16 16:40:19 +0800286 .bat_neigh_print = batadv_v_neigh_print,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800287};
288
289/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800290 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
291 * mesh
292 * @bat_priv: the object representing the mesh interface to initialise
293 *
294 * Return: 0 on success or a negative error code otherwise
295 */
296int batadv_v_mesh_init(struct batadv_priv *bat_priv)
297{
298 return batadv_v_ogm_init(bat_priv);
299}
300
301/**
302 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
303 * @bat_priv: the object representing the mesh interface to free
304 */
305void batadv_v_mesh_free(struct batadv_priv *bat_priv)
306{
307 batadv_v_ogm_free(bat_priv);
308}
309
310/**
Linus Luessingd6f94d92016-01-16 16:40:09 +0800311 * batadv_v_init - B.A.T.M.A.N. V initialization function
312 *
313 * Description: Takes care of initializing all the subcomponents.
314 * It is invoked upon module load only.
315 *
316 * Return: 0 on success or a negative error code otherwise
317 */
318int __init batadv_v_init(void)
319{
Linus Luessing162bd642016-01-16 16:40:10 +0800320 int ret;
321
322 /* B.A.T.M.A.N. V echo location protocol packet */
323 ret = batadv_recv_handler_register(BATADV_ELP,
324 batadv_v_elp_packet_recv);
325 if (ret < 0)
326 return ret;
327
Antonio Quartulli0da00352016-01-16 16:40:12 +0800328 ret = batadv_recv_handler_register(BATADV_OGM2,
329 batadv_v_ogm_packet_recv);
Linus Luessing162bd642016-01-16 16:40:10 +0800330 if (ret < 0)
Antonio Quartulli0da00352016-01-16 16:40:12 +0800331 goto elp_unregister;
332
333 ret = batadv_algo_register(&batadv_batman_v);
334 if (ret < 0)
335 goto ogm_unregister;
336
337 return ret;
338
339ogm_unregister:
340 batadv_recv_handler_unregister(BATADV_OGM2);
341
342elp_unregister:
343 batadv_recv_handler_unregister(BATADV_ELP);
Linus Luessing162bd642016-01-16 16:40:10 +0800344
345 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +0800346}