1 /* Copyright (C) 2013-2016 B.A.T.M.A.N. contributors:
3 * Linus Lüssing, Marek Lindner
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.
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.
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/>.
21 #include <linux/atomic.h>
22 #include <linux/bug.h>
23 #include <linux/cache.h>
24 #include <linux/init.h>
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>
30 #include <linux/types.h>
31 #include <linux/workqueue.h>
33 #include "bat_v_elp.h"
34 #include "bat_v_ogm.h"
36 #include "originator.h"
39 static int batadv_v_iface_enable(struct batadv_hard_iface
*hard_iface
)
43 ret
= batadv_v_elp_iface_enable(hard_iface
);
47 ret
= batadv_v_ogm_iface_enable(hard_iface
);
49 batadv_v_elp_iface_disable(hard_iface
);
51 /* enable link throughput auto-detection by setting the throughput
54 atomic_set(&hard_iface
->bat_v
.throughput_override
, 0);
59 static void batadv_v_iface_disable(struct batadv_hard_iface
*hard_iface
)
61 batadv_v_elp_iface_disable(hard_iface
);
64 static void batadv_v_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
68 static void batadv_v_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
70 batadv_v_elp_primary_iface_set(hard_iface
);
71 batadv_v_ogm_primary_iface_set(hard_iface
);
75 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node
*hardif_neigh
)
77 ewma_throughput_init(&hardif_neigh
->bat_v
.throughput
);
78 INIT_WORK(&hardif_neigh
->bat_v
.metric_work
,
79 batadv_v_elp_throughput_metric_update
);
82 static void batadv_v_ogm_schedule(struct batadv_hard_iface
*hard_iface
)
86 static void batadv_v_ogm_emit(struct batadv_forw_packet
*forw_packet
)
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
96 * Must be called while holding an rcu lock.
99 batadv_v_orig_print_neigh(struct batadv_orig_node
*orig_node
,
100 struct batadv_hard_iface
*if_outgoing
,
101 struct seq_file
*seq
)
103 struct batadv_neigh_node
*neigh_node
;
104 struct batadv_neigh_ifinfo
*n_ifinfo
;
106 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
107 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
111 seq_printf(seq
, " %pM (%9u.%1u)",
113 n_ifinfo
->bat_v
.throughput
/ 10,
114 n_ifinfo
->bat_v
.throughput
% 10);
116 batadv_neigh_ifinfo_put(n_ifinfo
);
121 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
122 * @seq: neighbour table seq_file struct
123 * @hardif_neigh: hardif neighbour information
126 batadv_v_hardif_neigh_print(struct seq_file
*seq
,
127 struct batadv_hardif_neigh_node
*hardif_neigh
)
129 int last_secs
, last_msecs
;
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
);
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
);
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
146 static void batadv_v_neigh_print(struct batadv_priv
*bat_priv
,
147 struct seq_file
*seq
)
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;
154 seq_printf(seq
, " %-15s %s (%11s) [%10s]\n", "Neighbor",
155 "last-seen", "throughput", "IF");
158 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
159 if (hard_iface
->soft_iface
!= net_dev
)
162 hlist_for_each_entry_rcu(hardif_neigh
,
163 &hard_iface
->neigh_list
, list
) {
164 batadv_v_hardif_neigh_print(seq
, hardif_neigh
);
170 if (batman_count
== 0)
171 seq_puts(seq
, "No batman nodes in range ...\n");
175 * 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
180 static void batadv_v_orig_print(struct batadv_priv
*bat_priv
,
181 struct seq_file
*seq
,
182 struct batadv_hard_iface
*if_outgoing
)
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;
194 seq_printf(seq
, " %-15s %s (%11s) %17s [%10s]: %20s ...\n",
195 "Originator", "last-seen", "throughput", "Nexthop",
196 "outgoingIF", "Potential nexthops");
198 for (i
= 0; i
< hash
->size
; i
++) {
199 head
= &hash
->table
[i
];
202 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
203 neigh_node
= batadv_orig_router_get(orig_node
,
208 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
213 last_seen_jiffies
= jiffies
- orig_node
->last_seen
;
214 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
215 last_seen_secs
= last_seen_msecs
/ 1000;
216 last_seen_msecs
= last_seen_msecs
% 1000;
218 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
219 orig_node
->orig
, last_seen_secs
,
221 n_ifinfo
->bat_v
.throughput
/ 10,
222 n_ifinfo
->bat_v
.throughput
% 10,
224 neigh_node
->if_incoming
->net_dev
->name
);
226 batadv_v_orig_print_neigh(orig_node
, if_outgoing
, seq
);
231 batadv_neigh_node_put(neigh_node
);
233 batadv_neigh_ifinfo_put(n_ifinfo
);
238 if (batman_count
== 0)
239 seq_puts(seq
, "No batman nodes in range ...\n");
242 static int batadv_v_neigh_cmp(struct batadv_neigh_node
*neigh1
,
243 struct batadv_hard_iface
*if_outgoing1
,
244 struct batadv_neigh_node
*neigh2
,
245 struct batadv_hard_iface
*if_outgoing2
)
247 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
249 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
250 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
252 if (WARN_ON(!ifinfo1
|| !ifinfo2
))
255 return ifinfo1
->bat_v
.throughput
- ifinfo2
->bat_v
.throughput
;
258 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node
*neigh1
,
259 struct batadv_hard_iface
*if_outgoing1
,
260 struct batadv_neigh_node
*neigh2
,
261 struct batadv_hard_iface
*if_outgoing2
)
263 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
266 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
267 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
269 threshold
= ifinfo1
->bat_v
.throughput
/ 4;
270 threshold
= ifinfo1
->bat_v
.throughput
- threshold
;
272 return ifinfo2
->bat_v
.throughput
> threshold
;
275 static struct batadv_algo_ops batadv_batman_v __read_mostly
= {
277 .bat_iface_enable
= batadv_v_iface_enable
,
278 .bat_iface_disable
= batadv_v_iface_disable
,
279 .bat_iface_update_mac
= batadv_v_iface_update_mac
,
280 .bat_primary_iface_set
= batadv_v_primary_iface_set
,
281 .bat_hardif_neigh_init
= batadv_v_hardif_neigh_init
,
282 .bat_ogm_emit
= batadv_v_ogm_emit
,
283 .bat_ogm_schedule
= batadv_v_ogm_schedule
,
284 .bat_orig_print
= batadv_v_orig_print
,
285 .bat_neigh_cmp
= batadv_v_neigh_cmp
,
286 .bat_neigh_is_similar_or_better
= batadv_v_neigh_is_sob
,
287 .bat_neigh_print
= batadv_v_neigh_print
,
291 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
293 * @bat_priv: the object representing the mesh interface to initialise
295 * Return: 0 on success or a negative error code otherwise
297 int batadv_v_mesh_init(struct batadv_priv
*bat_priv
)
299 return batadv_v_ogm_init(bat_priv
);
303 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
304 * @bat_priv: the object representing the mesh interface to free
306 void batadv_v_mesh_free(struct batadv_priv
*bat_priv
)
308 batadv_v_ogm_free(bat_priv
);
312 * batadv_v_init - B.A.T.M.A.N. V initialization function
314 * Description: Takes care of initializing all the subcomponents.
315 * It is invoked upon module load only.
317 * Return: 0 on success or a negative error code otherwise
319 int __init
batadv_v_init(void)
323 /* B.A.T.M.A.N. V echo location protocol packet */
324 ret
= batadv_recv_handler_register(BATADV_ELP
,
325 batadv_v_elp_packet_recv
);
329 ret
= batadv_recv_handler_register(BATADV_OGM2
,
330 batadv_v_ogm_packet_recv
);
334 ret
= batadv_algo_register(&batadv_batman_v
);
341 batadv_recv_handler_unregister(BATADV_OGM2
);
344 batadv_recv_handler_unregister(BATADV_ELP
);