1 /* Copyright (C) 2013-2017 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/errno.h>
25 #include <linux/if_ether.h>
26 #include <linux/init.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/kref.h>
30 #include <linux/netdevice.h>
31 #include <linux/netlink.h>
32 #include <linux/rculist.h>
33 #include <linux/rcupdate.h>
34 #include <linux/seq_file.h>
35 #include <linux/stddef.h>
36 #include <linux/types.h>
37 #include <linux/workqueue.h>
38 #include <net/genetlink.h>
39 #include <net/netlink.h>
40 #include <uapi/linux/batman_adv.h>
43 #include "bat_v_elp.h"
44 #include "bat_v_ogm.h"
45 #include "gateway_client.h"
46 #include "gateway_common.h"
47 #include "hard-interface.h"
51 #include "originator.h"
56 static void batadv_v_iface_activate(struct batadv_hard_iface
*hard_iface
)
58 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
59 struct batadv_hard_iface
*primary_if
;
61 primary_if
= batadv_primary_if_get_selected(bat_priv
);
64 batadv_v_elp_iface_activate(primary_if
, hard_iface
);
65 batadv_hardif_put(primary_if
);
68 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
69 * set the interface as ACTIVE right away, without any risk of race
72 if (hard_iface
->if_status
== BATADV_IF_TO_BE_ACTIVATED
)
73 hard_iface
->if_status
= BATADV_IF_ACTIVE
;
76 static int batadv_v_iface_enable(struct batadv_hard_iface
*hard_iface
)
80 ret
= batadv_v_elp_iface_enable(hard_iface
);
84 ret
= batadv_v_ogm_iface_enable(hard_iface
);
86 batadv_v_elp_iface_disable(hard_iface
);
91 static void batadv_v_iface_disable(struct batadv_hard_iface
*hard_iface
)
93 batadv_v_elp_iface_disable(hard_iface
);
96 static void batadv_v_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
98 batadv_v_elp_primary_iface_set(hard_iface
);
99 batadv_v_ogm_primary_iface_set(hard_iface
);
103 * batadv_v_iface_update_mac - react to hard-interface MAC address change
104 * @hard_iface: the modified interface
106 * If the modified interface is the primary one, update the originator
107 * address in the ELP and OGM messages to reflect the new MAC address.
109 static void batadv_v_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
111 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
112 struct batadv_hard_iface
*primary_if
;
114 primary_if
= batadv_primary_if_get_selected(bat_priv
);
115 if (primary_if
!= hard_iface
)
118 batadv_v_primary_iface_set(hard_iface
);
121 batadv_hardif_put(primary_if
);
125 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node
*hardif_neigh
)
127 ewma_throughput_init(&hardif_neigh
->bat_v
.throughput
);
128 INIT_WORK(&hardif_neigh
->bat_v
.metric_work
,
129 batadv_v_elp_throughput_metric_update
);
132 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
134 * batadv_v_orig_print_neigh - print neighbors for the originator table
135 * @orig_node: the orig_node for which the neighbors are printed
136 * @if_outgoing: outgoing interface for these entries
137 * @seq: debugfs table seq_file struct
139 * Must be called while holding an rcu lock.
142 batadv_v_orig_print_neigh(struct batadv_orig_node
*orig_node
,
143 struct batadv_hard_iface
*if_outgoing
,
144 struct seq_file
*seq
)
146 struct batadv_neigh_node
*neigh_node
;
147 struct batadv_neigh_ifinfo
*n_ifinfo
;
149 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
150 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
154 seq_printf(seq
, " %pM (%9u.%1u)",
156 n_ifinfo
->bat_v
.throughput
/ 10,
157 n_ifinfo
->bat_v
.throughput
% 10);
159 batadv_neigh_ifinfo_put(n_ifinfo
);
164 * batadv_v_hardif_neigh_print - print a single ELP neighbour node
165 * @seq: neighbour table seq_file struct
166 * @hardif_neigh: hardif neighbour information
169 batadv_v_hardif_neigh_print(struct seq_file
*seq
,
170 struct batadv_hardif_neigh_node
*hardif_neigh
)
172 int last_secs
, last_msecs
;
175 last_secs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) / 1000;
176 last_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) % 1000;
177 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
179 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
180 hardif_neigh
->addr
, last_secs
, last_msecs
, throughput
/ 10,
181 throughput
% 10, hardif_neigh
->if_incoming
->net_dev
->name
);
185 * batadv_v_neigh_print - print the single hop neighbour list
186 * @bat_priv: the bat priv with all the soft interface information
187 * @seq: neighbour table seq_file struct
189 static void batadv_v_neigh_print(struct batadv_priv
*bat_priv
,
190 struct seq_file
*seq
)
192 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
193 struct batadv_hardif_neigh_node
*hardif_neigh
;
194 struct batadv_hard_iface
*hard_iface
;
195 int batman_count
= 0;
198 " Neighbor last-seen ( throughput) [ IF]\n");
201 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
202 if (hard_iface
->soft_iface
!= net_dev
)
205 hlist_for_each_entry_rcu(hardif_neigh
,
206 &hard_iface
->neigh_list
, list
) {
207 batadv_v_hardif_neigh_print(seq
, hardif_neigh
);
213 if (batman_count
== 0)
214 seq_puts(seq
, "No batman nodes in range ...\n");
219 * batadv_v_neigh_dump_neigh - Dump a neighbour into a message
220 * @msg: Netlink message to dump into
221 * @portid: Port making netlink request
222 * @seq: Sequence number of netlink message
223 * @hardif_neigh: Neighbour to dump
225 * Return: Error code, or 0 on success
228 batadv_v_neigh_dump_neigh(struct sk_buff
*msg
, u32 portid
, u32 seq
,
229 struct batadv_hardif_neigh_node
*hardif_neigh
)
232 unsigned int last_seen_msecs
;
235 last_seen_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
);
236 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
237 throughput
= throughput
* 100;
239 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
240 BATADV_CMD_GET_NEIGHBORS
);
244 if (nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
245 hardif_neigh
->addr
) ||
246 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
247 hardif_neigh
->if_incoming
->net_dev
->ifindex
) ||
248 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
250 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
))
251 goto nla_put_failure
;
253 genlmsg_end(msg
, hdr
);
257 genlmsg_cancel(msg
, hdr
);
262 * batadv_v_neigh_dump_hardif - Dump the neighbours of a hard interface into
264 * @msg: Netlink message to dump into
265 * @portid: Port making netlink request
266 * @seq: Sequence number of netlink message
267 * @bat_priv: The bat priv with all the soft interface information
268 * @hard_iface: The hard interface to be dumped
269 * @idx_s: Entries to be skipped
271 * This function assumes the caller holds rcu_read_lock().
273 * Return: Error code, or 0 on success
276 batadv_v_neigh_dump_hardif(struct sk_buff
*msg
, u32 portid
, u32 seq
,
277 struct batadv_priv
*bat_priv
,
278 struct batadv_hard_iface
*hard_iface
,
281 struct batadv_hardif_neigh_node
*hardif_neigh
;
284 hlist_for_each_entry_rcu(hardif_neigh
,
285 &hard_iface
->neigh_list
, list
) {
289 if (batadv_v_neigh_dump_neigh(msg
, portid
, seq
, hardif_neigh
)) {
300 * batadv_v_neigh_dump - Dump the neighbours of a hard interface into a
302 * @msg: Netlink message to dump into
303 * @cb: Control block containing additional options
304 * @bat_priv: The bat priv with all the soft interface information
305 * @single_hardif: Limit dumping to this hard interface
308 batadv_v_neigh_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
309 struct batadv_priv
*bat_priv
,
310 struct batadv_hard_iface
*single_hardif
)
312 struct batadv_hard_iface
*hard_iface
;
314 int i_hardif_s
= cb
->args
[0];
315 int idx
= cb
->args
[1];
316 int portid
= NETLINK_CB(cb
->skb
).portid
;
320 if (i_hardif_s
== 0) {
321 if (batadv_v_neigh_dump_hardif(msg
, portid
,
323 bat_priv
, single_hardif
,
328 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
329 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
332 if (i_hardif
++ < i_hardif_s
)
335 if (batadv_v_neigh_dump_hardif(msg
, portid
,
337 bat_priv
, hard_iface
,
346 cb
->args
[0] = i_hardif
;
350 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
352 * batadv_v_orig_print - print the originator table
353 * @bat_priv: the bat priv with all the soft interface information
354 * @seq: debugfs table seq_file struct
355 * @if_outgoing: the outgoing interface for which this should be printed
357 static void batadv_v_orig_print(struct batadv_priv
*bat_priv
,
358 struct seq_file
*seq
,
359 struct batadv_hard_iface
*if_outgoing
)
361 struct batadv_neigh_node
*neigh_node
;
362 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
363 int last_seen_msecs
, last_seen_secs
;
364 struct batadv_orig_node
*orig_node
;
365 struct batadv_neigh_ifinfo
*n_ifinfo
;
366 unsigned long last_seen_jiffies
;
367 struct hlist_head
*head
;
368 int batman_count
= 0;
372 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
374 for (i
= 0; i
< hash
->size
; i
++) {
375 head
= &hash
->table
[i
];
378 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
379 neigh_node
= batadv_orig_router_get(orig_node
,
384 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
389 last_seen_jiffies
= jiffies
- orig_node
->last_seen
;
390 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
391 last_seen_secs
= last_seen_msecs
/ 1000;
392 last_seen_msecs
= last_seen_msecs
% 1000;
394 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
395 orig_node
->orig
, last_seen_secs
,
397 n_ifinfo
->bat_v
.throughput
/ 10,
398 n_ifinfo
->bat_v
.throughput
% 10,
400 neigh_node
->if_incoming
->net_dev
->name
);
402 batadv_v_orig_print_neigh(orig_node
, if_outgoing
, seq
);
407 batadv_neigh_node_put(neigh_node
);
409 batadv_neigh_ifinfo_put(n_ifinfo
);
414 if (batman_count
== 0)
415 seq_puts(seq
, "No batman nodes in range ...\n");
420 * batadv_v_orig_dump_subentry - Dump an originator subentry into a
422 * @msg: Netlink message to dump into
423 * @portid: Port making netlink request
424 * @seq: Sequence number of netlink message
425 * @bat_priv: The bat priv with all the soft interface information
426 * @if_outgoing: Limit dump to entries with this outgoing interface
427 * @orig_node: Originator to dump
428 * @neigh_node: Single hops neighbour
429 * @best: Is the best originator
431 * Return: Error code, or 0 on success
434 batadv_v_orig_dump_subentry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
435 struct batadv_priv
*bat_priv
,
436 struct batadv_hard_iface
*if_outgoing
,
437 struct batadv_orig_node
*orig_node
,
438 struct batadv_neigh_node
*neigh_node
,
441 struct batadv_neigh_ifinfo
*n_ifinfo
;
442 unsigned int last_seen_msecs
;
446 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
450 throughput
= n_ifinfo
->bat_v
.throughput
* 100;
452 batadv_neigh_ifinfo_put(n_ifinfo
);
454 last_seen_msecs
= jiffies_to_msecs(jiffies
- orig_node
->last_seen
);
456 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
457 if_outgoing
!= neigh_node
->if_incoming
)
460 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
461 BATADV_CMD_GET_ORIGINATORS
);
465 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
, orig_node
->orig
) ||
466 nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
468 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
469 neigh_node
->if_incoming
->net_dev
->ifindex
) ||
470 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
) ||
471 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
473 goto nla_put_failure
;
475 if (best
&& nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
))
476 goto nla_put_failure
;
478 genlmsg_end(msg
, hdr
);
482 genlmsg_cancel(msg
, hdr
);
487 * batadv_v_orig_dump_entry - Dump an originator entry into a message
488 * @msg: Netlink message to dump into
489 * @portid: Port making netlink request
490 * @seq: Sequence number of netlink message
491 * @bat_priv: The bat priv with all the soft interface information
492 * @if_outgoing: Limit dump to entries with this outgoing interface
493 * @orig_node: Originator to dump
494 * @sub_s: Number of sub entries to skip
496 * This function assumes the caller holds rcu_read_lock().
498 * Return: Error code, or 0 on success
501 batadv_v_orig_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
502 struct batadv_priv
*bat_priv
,
503 struct batadv_hard_iface
*if_outgoing
,
504 struct batadv_orig_node
*orig_node
, int *sub_s
)
506 struct batadv_neigh_node
*neigh_node_best
;
507 struct batadv_neigh_node
*neigh_node
;
511 neigh_node_best
= batadv_orig_router_get(orig_node
, if_outgoing
);
512 if (!neigh_node_best
)
515 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
519 best
= (neigh_node
== neigh_node_best
);
521 if (batadv_v_orig_dump_subentry(msg
, portid
, seq
, bat_priv
,
522 if_outgoing
, orig_node
,
524 batadv_neigh_node_put(neigh_node_best
);
533 batadv_neigh_node_put(neigh_node_best
);
540 * batadv_v_orig_dump_bucket - Dump an originator bucket into a
542 * @msg: Netlink message to dump into
543 * @portid: Port making netlink request
544 * @seq: Sequence number of netlink message
545 * @bat_priv: The bat priv with all the soft interface information
546 * @if_outgoing: Limit dump to entries with this outgoing interface
547 * @head: Bucket to be dumped
548 * @idx_s: Number of entries to be skipped
549 * @sub: Number of sub entries to be skipped
551 * Return: Error code, or 0 on success
554 batadv_v_orig_dump_bucket(struct sk_buff
*msg
, u32 portid
, u32 seq
,
555 struct batadv_priv
*bat_priv
,
556 struct batadv_hard_iface
*if_outgoing
,
557 struct hlist_head
*head
, int *idx_s
, int *sub
)
559 struct batadv_orig_node
*orig_node
;
563 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
567 if (batadv_v_orig_dump_entry(msg
, portid
, seq
, bat_priv
,
568 if_outgoing
, orig_node
, sub
)) {
582 * batadv_v_orig_dump - Dump the originators into a message
583 * @msg: Netlink message to dump into
584 * @cb: Control block containing additional options
585 * @bat_priv: The bat priv with all the soft interface information
586 * @if_outgoing: Limit dump to entries with this outgoing interface
589 batadv_v_orig_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
590 struct batadv_priv
*bat_priv
,
591 struct batadv_hard_iface
*if_outgoing
)
593 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
594 struct hlist_head
*head
;
595 int bucket
= cb
->args
[0];
596 int idx
= cb
->args
[1];
597 int sub
= cb
->args
[2];
598 int portid
= NETLINK_CB(cb
->skb
).portid
;
600 while (bucket
< hash
->size
) {
601 head
= &hash
->table
[bucket
];
603 if (batadv_v_orig_dump_bucket(msg
, portid
,
605 bat_priv
, if_outgoing
, head
, &idx
,
612 cb
->args
[0] = bucket
;
617 static int batadv_v_neigh_cmp(struct batadv_neigh_node
*neigh1
,
618 struct batadv_hard_iface
*if_outgoing1
,
619 struct batadv_neigh_node
*neigh2
,
620 struct batadv_hard_iface
*if_outgoing2
)
622 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
625 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
626 if (WARN_ON(!ifinfo1
))
629 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
630 if (WARN_ON(!ifinfo2
))
633 ret
= ifinfo1
->bat_v
.throughput
- ifinfo2
->bat_v
.throughput
;
635 batadv_neigh_ifinfo_put(ifinfo2
);
637 batadv_neigh_ifinfo_put(ifinfo1
);
642 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node
*neigh1
,
643 struct batadv_hard_iface
*if_outgoing1
,
644 struct batadv_neigh_node
*neigh2
,
645 struct batadv_hard_iface
*if_outgoing2
)
647 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
651 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
652 if (WARN_ON(!ifinfo1
))
655 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
656 if (WARN_ON(!ifinfo2
))
659 threshold
= ifinfo1
->bat_v
.throughput
/ 4;
660 threshold
= ifinfo1
->bat_v
.throughput
- threshold
;
662 ret
= ifinfo2
->bat_v
.throughput
> threshold
;
664 batadv_neigh_ifinfo_put(ifinfo2
);
666 batadv_neigh_ifinfo_put(ifinfo1
);
672 * batadv_v_init_sel_class - initialize GW selection class
673 * @bat_priv: the bat priv with all the soft interface information
675 static void batadv_v_init_sel_class(struct batadv_priv
*bat_priv
)
677 /* set default throughput difference threshold to 5Mbps */
678 atomic_set(&bat_priv
->gw
.sel_class
, 50);
681 static ssize_t
batadv_v_store_sel_class(struct batadv_priv
*bat_priv
,
682 char *buff
, size_t count
)
684 u32 old_class
, class;
686 if (!batadv_parse_throughput(bat_priv
->soft_iface
, buff
,
687 "B.A.T.M.A.N. V GW selection class",
691 old_class
= atomic_read(&bat_priv
->gw
.sel_class
);
692 atomic_set(&bat_priv
->gw
.sel_class
, class);
694 if (old_class
!= class)
695 batadv_gw_reselect(bat_priv
);
700 static ssize_t
batadv_v_show_sel_class(struct batadv_priv
*bat_priv
, char *buff
)
702 u32
class = atomic_read(&bat_priv
->gw
.sel_class
);
704 return sprintf(buff
, "%u.%u MBit\n", class / 10, class % 10);
708 * batadv_v_gw_throughput_get - retrieve the GW-bandwidth for a given GW
709 * @gw_node: the GW to retrieve the metric for
710 * @bw: the pointer where the metric will be stored. The metric is computed as
711 * the minimum between the GW advertised throughput and the path throughput to
714 * Return: 0 on success, -1 on failure
716 static int batadv_v_gw_throughput_get(struct batadv_gw_node
*gw_node
, u32
*bw
)
718 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
719 struct batadv_orig_node
*orig_node
;
720 struct batadv_neigh_node
*router
;
723 orig_node
= gw_node
->orig_node
;
724 router
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
728 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
732 /* the GW metric is computed as the minimum between the path throughput
733 * to reach the GW itself and the advertised bandwidth.
734 * This gives us an approximation of the effective throughput that the
735 * client can expect via this particular GW node
737 *bw
= router_ifinfo
->bat_v
.throughput
;
738 *bw
= min_t(u32
, *bw
, gw_node
->bandwidth_down
);
743 batadv_neigh_node_put(router
);
745 batadv_neigh_ifinfo_put(router_ifinfo
);
751 * batadv_v_gw_get_best_gw_node - retrieve the best GW node
752 * @bat_priv: the bat priv with all the soft interface information
754 * Return: the GW node having the best GW-metric, NULL if no GW is known
756 static struct batadv_gw_node
*
757 batadv_v_gw_get_best_gw_node(struct batadv_priv
*bat_priv
)
759 struct batadv_gw_node
*gw_node
, *curr_gw
= NULL
;
763 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
764 if (!kref_get_unless_zero(&gw_node
->refcount
))
767 if (batadv_v_gw_throughput_get(gw_node
, &bw
) < 0)
770 if (curr_gw
&& (bw
<= max_bw
))
774 batadv_gw_node_put(curr_gw
);
777 kref_get(&curr_gw
->refcount
);
781 batadv_gw_node_put(gw_node
);
789 * batadv_v_gw_is_eligible - check if a originator would be selected as GW
790 * @bat_priv: the bat priv with all the soft interface information
791 * @curr_gw_orig: originator representing the currently selected GW
792 * @orig_node: the originator representing the new candidate
794 * Return: true if orig_node can be selected as current GW, false otherwise
796 static bool batadv_v_gw_is_eligible(struct batadv_priv
*bat_priv
,
797 struct batadv_orig_node
*curr_gw_orig
,
798 struct batadv_orig_node
*orig_node
)
800 struct batadv_gw_node
*curr_gw
, *orig_gw
= NULL
;
801 u32 gw_throughput
, orig_throughput
, threshold
;
804 threshold
= atomic_read(&bat_priv
->gw
.sel_class
);
806 curr_gw
= batadv_gw_node_get(bat_priv
, curr_gw_orig
);
812 if (batadv_v_gw_throughput_get(curr_gw
, &gw_throughput
) < 0) {
817 orig_gw
= batadv_gw_node_get(bat_priv
, orig_node
);
821 if (batadv_v_gw_throughput_get(orig_gw
, &orig_throughput
) < 0)
824 if (orig_throughput
< gw_throughput
)
827 if ((orig_throughput
- gw_throughput
) < threshold
)
830 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
831 "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
832 gw_throughput
, orig_throughput
);
837 batadv_gw_node_put(curr_gw
);
839 batadv_gw_node_put(orig_gw
);
844 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
845 /* fails if orig_node has no router */
846 static int batadv_v_gw_write_buffer_text(struct batadv_priv
*bat_priv
,
847 struct seq_file
*seq
,
848 const struct batadv_gw_node
*gw_node
)
850 struct batadv_gw_node
*curr_gw
;
851 struct batadv_neigh_node
*router
;
852 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
855 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
859 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
863 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
865 seq_printf(seq
, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
866 (curr_gw
== gw_node
? "=>" : " "),
867 gw_node
->orig_node
->orig
,
868 router_ifinfo
->bat_v
.throughput
/ 10,
869 router_ifinfo
->bat_v
.throughput
% 10, router
->addr
,
870 router
->if_incoming
->net_dev
->name
,
871 gw_node
->bandwidth_down
/ 10,
872 gw_node
->bandwidth_down
% 10,
873 gw_node
->bandwidth_up
/ 10,
874 gw_node
->bandwidth_up
% 10);
875 ret
= seq_has_overflowed(seq
) ? -1 : 0;
878 batadv_gw_node_put(curr_gw
);
881 batadv_neigh_ifinfo_put(router_ifinfo
);
883 batadv_neigh_node_put(router
);
888 * batadv_v_gw_print - print the gateway list
889 * @bat_priv: the bat priv with all the soft interface information
890 * @seq: gateway table seq_file struct
892 static void batadv_v_gw_print(struct batadv_priv
*bat_priv
,
893 struct seq_file
*seq
)
895 struct batadv_gw_node
*gw_node
;
899 " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
902 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
903 /* fails if orig_node has no router */
904 if (batadv_v_gw_write_buffer_text(bat_priv
, seq
, gw_node
) < 0)
912 seq_puts(seq
, "No gateways in range ...\n");
917 * batadv_v_gw_dump_entry - Dump a gateway into a message
918 * @msg: Netlink message to dump into
919 * @portid: Port making netlink request
920 * @seq: Sequence number of netlink message
921 * @bat_priv: The bat priv with all the soft interface information
922 * @gw_node: Gateway to be dumped
924 * Return: Error code, or 0 on success
926 static int batadv_v_gw_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
927 struct batadv_priv
*bat_priv
,
928 struct batadv_gw_node
*gw_node
)
930 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
931 struct batadv_neigh_node
*router
;
932 struct batadv_gw_node
*curr_gw
;
936 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
940 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
944 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
946 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
,
947 NLM_F_MULTI
, BATADV_CMD_GET_GATEWAYS
);
955 if (curr_gw
== gw_node
) {
956 if (nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
)) {
957 genlmsg_cancel(msg
, hdr
);
962 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
963 gw_node
->orig_node
->orig
)) {
964 genlmsg_cancel(msg
, hdr
);
968 if (nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
,
969 router_ifinfo
->bat_v
.throughput
)) {
970 genlmsg_cancel(msg
, hdr
);
974 if (nla_put(msg
, BATADV_ATTR_ROUTER
, ETH_ALEN
, router
->addr
)) {
975 genlmsg_cancel(msg
, hdr
);
979 if (nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
980 router
->if_incoming
->net_dev
->name
)) {
981 genlmsg_cancel(msg
, hdr
);
985 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_DOWN
,
986 gw_node
->bandwidth_down
)) {
987 genlmsg_cancel(msg
, hdr
);
991 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_UP
, gw_node
->bandwidth_up
)) {
992 genlmsg_cancel(msg
, hdr
);
996 genlmsg_end(msg
, hdr
);
1001 batadv_neigh_ifinfo_put(router_ifinfo
);
1003 batadv_neigh_node_put(router
);
1008 * batadv_v_gw_dump - Dump gateways into a message
1009 * @msg: Netlink message to dump into
1010 * @cb: Control block containing additional options
1011 * @bat_priv: The bat priv with all the soft interface information
1013 static void batadv_v_gw_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
1014 struct batadv_priv
*bat_priv
)
1016 int portid
= NETLINK_CB(cb
->skb
).portid
;
1017 struct batadv_gw_node
*gw_node
;
1018 int idx_skip
= cb
->args
[0];
1022 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
1023 if (idx
++ < idx_skip
)
1026 if (batadv_v_gw_dump_entry(msg
, portid
, cb
->nlh
->nlmsg_seq
,
1027 bat_priv
, gw_node
)) {
1037 cb
->args
[0] = idx_skip
;
1040 static struct batadv_algo_ops batadv_batman_v __read_mostly
= {
1043 .activate
= batadv_v_iface_activate
,
1044 .enable
= batadv_v_iface_enable
,
1045 .disable
= batadv_v_iface_disable
,
1046 .update_mac
= batadv_v_iface_update_mac
,
1047 .primary_set
= batadv_v_primary_iface_set
,
1050 .hardif_init
= batadv_v_hardif_neigh_init
,
1051 .cmp
= batadv_v_neigh_cmp
,
1052 .is_similar_or_better
= batadv_v_neigh_is_sob
,
1053 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1054 .print
= batadv_v_neigh_print
,
1056 .dump
= batadv_v_neigh_dump
,
1059 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1060 .print
= batadv_v_orig_print
,
1062 .dump
= batadv_v_orig_dump
,
1065 .init_sel_class
= batadv_v_init_sel_class
,
1066 .store_sel_class
= batadv_v_store_sel_class
,
1067 .show_sel_class
= batadv_v_show_sel_class
,
1068 .get_best_gw_node
= batadv_v_gw_get_best_gw_node
,
1069 .is_eligible
= batadv_v_gw_is_eligible
,
1070 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1071 .print
= batadv_v_gw_print
,
1073 .dump
= batadv_v_gw_dump
,
1078 * batadv_v_hardif_init - initialize the algorithm specific fields in the
1079 * hard-interface object
1080 * @hard_iface: the hard-interface to initialize
1082 void batadv_v_hardif_init(struct batadv_hard_iface
*hard_iface
)
1084 /* enable link throughput auto-detection by setting the throughput
1087 atomic_set(&hard_iface
->bat_v
.throughput_override
, 0);
1088 atomic_set(&hard_iface
->bat_v
.elp_interval
, 500);
1092 * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a
1094 * @bat_priv: the object representing the mesh interface to initialise
1096 * Return: 0 on success or a negative error code otherwise
1098 int batadv_v_mesh_init(struct batadv_priv
*bat_priv
)
1102 ret
= batadv_v_ogm_init(bat_priv
);
1110 * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh
1111 * @bat_priv: the object representing the mesh interface to free
1113 void batadv_v_mesh_free(struct batadv_priv
*bat_priv
)
1115 batadv_v_ogm_free(bat_priv
);
1119 * batadv_v_init - B.A.T.M.A.N. V initialization function
1121 * Description: Takes care of initializing all the subcomponents.
1122 * It is invoked upon module load only.
1124 * Return: 0 on success or a negative error code otherwise
1126 int __init
batadv_v_init(void)
1130 /* B.A.T.M.A.N. V echo location protocol packet */
1131 ret
= batadv_recv_handler_register(BATADV_ELP
,
1132 batadv_v_elp_packet_recv
);
1136 ret
= batadv_recv_handler_register(BATADV_OGM2
,
1137 batadv_v_ogm_packet_recv
);
1139 goto elp_unregister
;
1141 ret
= batadv_algo_register(&batadv_batman_v
);
1143 goto ogm_unregister
;
1148 batadv_recv_handler_unregister(BATADV_OGM2
);
1151 batadv_recv_handler_unregister(BATADV_ELP
);