1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2013-2020 B.A.T.M.A.N. contributors:
4 * Linus Lüssing, Marek Lindner
10 #include <linux/atomic.h>
11 #include <linux/cache.h>
12 #include <linux/errno.h>
13 #include <linux/if_ether.h>
14 #include <linux/init.h>
15 #include <linux/jiffies.h>
16 #include <linux/kernel.h>
17 #include <linux/kref.h>
18 #include <linux/list.h>
19 #include <linux/netdevice.h>
20 #include <linux/netlink.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/seq_file.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/stddef.h>
27 #include <linux/types.h>
28 #include <linux/workqueue.h>
29 #include <net/genetlink.h>
30 #include <net/netlink.h>
31 #include <uapi/linux/batadv_packet.h>
32 #include <uapi/linux/batman_adv.h>
35 #include "bat_v_elp.h"
36 #include "bat_v_ogm.h"
37 #include "gateway_client.h"
38 #include "gateway_common.h"
39 #include "hard-interface.h"
43 #include "originator.h"
45 static void batadv_v_iface_activate(struct batadv_hard_iface
*hard_iface
)
47 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
48 struct batadv_hard_iface
*primary_if
;
50 primary_if
= batadv_primary_if_get_selected(bat_priv
);
53 batadv_v_elp_iface_activate(primary_if
, hard_iface
);
54 batadv_hardif_put(primary_if
);
57 /* B.A.T.M.A.N. V does not use any queuing mechanism, therefore it can
58 * set the interface as ACTIVE right away, without any risk of race
61 if (hard_iface
->if_status
== BATADV_IF_TO_BE_ACTIVATED
)
62 hard_iface
->if_status
= BATADV_IF_ACTIVE
;
65 static int batadv_v_iface_enable(struct batadv_hard_iface
*hard_iface
)
69 ret
= batadv_v_elp_iface_enable(hard_iface
);
73 ret
= batadv_v_ogm_iface_enable(hard_iface
);
75 batadv_v_elp_iface_disable(hard_iface
);
80 static void batadv_v_iface_disable(struct batadv_hard_iface
*hard_iface
)
82 batadv_v_ogm_iface_disable(hard_iface
);
83 batadv_v_elp_iface_disable(hard_iface
);
86 static void batadv_v_primary_iface_set(struct batadv_hard_iface
*hard_iface
)
88 batadv_v_elp_primary_iface_set(hard_iface
);
89 batadv_v_ogm_primary_iface_set(hard_iface
);
93 * batadv_v_iface_update_mac() - react to hard-interface MAC address change
94 * @hard_iface: the modified interface
96 * If the modified interface is the primary one, update the originator
97 * address in the ELP and OGM messages to reflect the new MAC address.
99 static void batadv_v_iface_update_mac(struct batadv_hard_iface
*hard_iface
)
101 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
102 struct batadv_hard_iface
*primary_if
;
104 primary_if
= batadv_primary_if_get_selected(bat_priv
);
105 if (primary_if
!= hard_iface
)
108 batadv_v_primary_iface_set(hard_iface
);
111 batadv_hardif_put(primary_if
);
115 batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node
*hardif_neigh
)
117 ewma_throughput_init(&hardif_neigh
->bat_v
.throughput
);
118 INIT_WORK(&hardif_neigh
->bat_v
.metric_work
,
119 batadv_v_elp_throughput_metric_update
);
122 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
124 * batadv_v_orig_print_neigh() - print neighbors for the originator table
125 * @orig_node: the orig_node for which the neighbors are printed
126 * @if_outgoing: outgoing interface for these entries
127 * @seq: debugfs table seq_file struct
129 * Must be called while holding an rcu lock.
132 batadv_v_orig_print_neigh(struct batadv_orig_node
*orig_node
,
133 struct batadv_hard_iface
*if_outgoing
,
134 struct seq_file
*seq
)
136 struct batadv_neigh_node
*neigh_node
;
137 struct batadv_neigh_ifinfo
*n_ifinfo
;
139 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
140 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
144 seq_printf(seq
, " %pM (%9u.%1u)",
146 n_ifinfo
->bat_v
.throughput
/ 10,
147 n_ifinfo
->bat_v
.throughput
% 10);
149 batadv_neigh_ifinfo_put(n_ifinfo
);
154 * batadv_v_hardif_neigh_print() - print a single ELP neighbour node
155 * @seq: neighbour table seq_file struct
156 * @hardif_neigh: hardif neighbour information
159 batadv_v_hardif_neigh_print(struct seq_file
*seq
,
160 struct batadv_hardif_neigh_node
*hardif_neigh
)
162 int last_secs
, last_msecs
;
165 last_secs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) / 1000;
166 last_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
) % 1000;
167 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
169 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) [%10s]\n",
170 hardif_neigh
->addr
, last_secs
, last_msecs
, throughput
/ 10,
171 throughput
% 10, hardif_neigh
->if_incoming
->net_dev
->name
);
175 * batadv_v_neigh_print() - print the single hop neighbour list
176 * @bat_priv: the bat priv with all the soft interface information
177 * @seq: neighbour table seq_file struct
179 static void batadv_v_neigh_print(struct batadv_priv
*bat_priv
,
180 struct seq_file
*seq
)
182 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
183 struct batadv_hardif_neigh_node
*hardif_neigh
;
184 struct batadv_hard_iface
*hard_iface
;
185 int batman_count
= 0;
188 " Neighbor last-seen ( throughput) [ IF]\n");
191 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
192 if (hard_iface
->soft_iface
!= net_dev
)
195 hlist_for_each_entry_rcu(hardif_neigh
,
196 &hard_iface
->neigh_list
, list
) {
197 batadv_v_hardif_neigh_print(seq
, hardif_neigh
);
203 if (batman_count
== 0)
204 seq_puts(seq
, "No batman nodes in range ...\n");
209 * batadv_v_neigh_dump_neigh() - Dump a neighbour into a message
210 * @msg: Netlink message to dump into
211 * @portid: Port making netlink request
212 * @seq: Sequence number of netlink message
213 * @hardif_neigh: Neighbour to dump
215 * Return: Error code, or 0 on success
218 batadv_v_neigh_dump_neigh(struct sk_buff
*msg
, u32 portid
, u32 seq
,
219 struct batadv_hardif_neigh_node
*hardif_neigh
)
222 unsigned int last_seen_msecs
;
225 last_seen_msecs
= jiffies_to_msecs(jiffies
- hardif_neigh
->last_seen
);
226 throughput
= ewma_throughput_read(&hardif_neigh
->bat_v
.throughput
);
227 throughput
= throughput
* 100;
229 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
230 BATADV_CMD_GET_NEIGHBORS
);
234 if (nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
235 hardif_neigh
->addr
) ||
236 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
237 hardif_neigh
->if_incoming
->net_dev
->ifindex
) ||
238 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
240 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
))
241 goto nla_put_failure
;
243 genlmsg_end(msg
, hdr
);
247 genlmsg_cancel(msg
, hdr
);
252 * batadv_v_neigh_dump_hardif() - Dump the neighbours of a hard interface into
254 * @msg: Netlink message to dump into
255 * @portid: Port making netlink request
256 * @seq: Sequence number of netlink message
257 * @bat_priv: The bat priv with all the soft interface information
258 * @hard_iface: The hard interface to be dumped
259 * @idx_s: Entries to be skipped
261 * This function assumes the caller holds rcu_read_lock().
263 * Return: Error code, or 0 on success
266 batadv_v_neigh_dump_hardif(struct sk_buff
*msg
, u32 portid
, u32 seq
,
267 struct batadv_priv
*bat_priv
,
268 struct batadv_hard_iface
*hard_iface
,
271 struct batadv_hardif_neigh_node
*hardif_neigh
;
274 hlist_for_each_entry_rcu(hardif_neigh
,
275 &hard_iface
->neigh_list
, list
) {
279 if (batadv_v_neigh_dump_neigh(msg
, portid
, seq
, hardif_neigh
)) {
290 * batadv_v_neigh_dump() - Dump the neighbours of a hard interface into a
292 * @msg: Netlink message to dump into
293 * @cb: Control block containing additional options
294 * @bat_priv: The bat priv with all the soft interface information
295 * @single_hardif: Limit dumping to this hard interface
298 batadv_v_neigh_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
299 struct batadv_priv
*bat_priv
,
300 struct batadv_hard_iface
*single_hardif
)
302 struct batadv_hard_iface
*hard_iface
;
304 int i_hardif_s
= cb
->args
[0];
305 int idx
= cb
->args
[1];
306 int portid
= NETLINK_CB(cb
->skb
).portid
;
310 if (i_hardif_s
== 0) {
311 if (batadv_v_neigh_dump_hardif(msg
, portid
,
313 bat_priv
, single_hardif
,
318 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
319 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
322 if (i_hardif
++ < i_hardif_s
)
325 if (batadv_v_neigh_dump_hardif(msg
, portid
,
327 bat_priv
, hard_iface
,
336 cb
->args
[0] = i_hardif
;
340 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
342 * batadv_v_orig_print() - print the originator table
343 * @bat_priv: the bat priv with all the soft interface information
344 * @seq: debugfs table seq_file struct
345 * @if_outgoing: the outgoing interface for which this should be printed
347 static void batadv_v_orig_print(struct batadv_priv
*bat_priv
,
348 struct seq_file
*seq
,
349 struct batadv_hard_iface
*if_outgoing
)
351 struct batadv_neigh_node
*neigh_node
;
352 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
353 int last_seen_msecs
, last_seen_secs
;
354 struct batadv_orig_node
*orig_node
;
355 struct batadv_neigh_ifinfo
*n_ifinfo
;
356 unsigned long last_seen_jiffies
;
357 struct hlist_head
*head
;
358 int batman_count
= 0;
362 " Originator last-seen ( throughput) Nexthop [outgoingIF]: Potential nexthops ...\n");
364 for (i
= 0; i
< hash
->size
; i
++) {
365 head
= &hash
->table
[i
];
368 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
369 neigh_node
= batadv_orig_router_get(orig_node
,
374 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
,
379 last_seen_jiffies
= jiffies
- orig_node
->last_seen
;
380 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
381 last_seen_secs
= last_seen_msecs
/ 1000;
382 last_seen_msecs
= last_seen_msecs
% 1000;
384 seq_printf(seq
, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:",
385 orig_node
->orig
, last_seen_secs
,
387 n_ifinfo
->bat_v
.throughput
/ 10,
388 n_ifinfo
->bat_v
.throughput
% 10,
390 neigh_node
->if_incoming
->net_dev
->name
);
392 batadv_v_orig_print_neigh(orig_node
, if_outgoing
, seq
);
397 batadv_neigh_node_put(neigh_node
);
399 batadv_neigh_ifinfo_put(n_ifinfo
);
404 if (batman_count
== 0)
405 seq_puts(seq
, "No batman nodes in range ...\n");
410 * batadv_v_orig_dump_subentry() - Dump an originator subentry into a message
411 * @msg: Netlink message to dump into
412 * @portid: Port making netlink request
413 * @seq: Sequence number of netlink message
414 * @bat_priv: The bat priv with all the soft interface information
415 * @if_outgoing: Limit dump to entries with this outgoing interface
416 * @orig_node: Originator to dump
417 * @neigh_node: Single hops neighbour
418 * @best: Is the best originator
420 * Return: Error code, or 0 on success
423 batadv_v_orig_dump_subentry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
424 struct batadv_priv
*bat_priv
,
425 struct batadv_hard_iface
*if_outgoing
,
426 struct batadv_orig_node
*orig_node
,
427 struct batadv_neigh_node
*neigh_node
,
430 struct batadv_neigh_ifinfo
*n_ifinfo
;
431 unsigned int last_seen_msecs
;
435 n_ifinfo
= batadv_neigh_ifinfo_get(neigh_node
, if_outgoing
);
439 throughput
= n_ifinfo
->bat_v
.throughput
* 100;
441 batadv_neigh_ifinfo_put(n_ifinfo
);
443 last_seen_msecs
= jiffies_to_msecs(jiffies
- orig_node
->last_seen
);
445 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
446 if_outgoing
!= neigh_node
->if_incoming
)
449 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
, NLM_F_MULTI
,
450 BATADV_CMD_GET_ORIGINATORS
);
454 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
, orig_node
->orig
) ||
455 nla_put(msg
, BATADV_ATTR_NEIGH_ADDRESS
, ETH_ALEN
,
457 nla_put_u32(msg
, BATADV_ATTR_HARD_IFINDEX
,
458 neigh_node
->if_incoming
->net_dev
->ifindex
) ||
459 nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
, throughput
) ||
460 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
,
462 goto nla_put_failure
;
464 if (best
&& nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
))
465 goto nla_put_failure
;
467 genlmsg_end(msg
, hdr
);
471 genlmsg_cancel(msg
, hdr
);
476 * batadv_v_orig_dump_entry() - Dump an originator entry into a message
477 * @msg: Netlink message to dump into
478 * @portid: Port making netlink request
479 * @seq: Sequence number of netlink message
480 * @bat_priv: The bat priv with all the soft interface information
481 * @if_outgoing: Limit dump to entries with this outgoing interface
482 * @orig_node: Originator to dump
483 * @sub_s: Number of sub entries to skip
485 * This function assumes the caller holds rcu_read_lock().
487 * Return: Error code, or 0 on success
490 batadv_v_orig_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
491 struct batadv_priv
*bat_priv
,
492 struct batadv_hard_iface
*if_outgoing
,
493 struct batadv_orig_node
*orig_node
, int *sub_s
)
495 struct batadv_neigh_node
*neigh_node_best
;
496 struct batadv_neigh_node
*neigh_node
;
500 neigh_node_best
= batadv_orig_router_get(orig_node
, if_outgoing
);
501 if (!neigh_node_best
)
504 hlist_for_each_entry_rcu(neigh_node
, &orig_node
->neigh_list
, list
) {
508 best
= (neigh_node
== neigh_node_best
);
510 if (batadv_v_orig_dump_subentry(msg
, portid
, seq
, bat_priv
,
511 if_outgoing
, orig_node
,
513 batadv_neigh_node_put(neigh_node_best
);
522 batadv_neigh_node_put(neigh_node_best
);
529 * batadv_v_orig_dump_bucket() - Dump an originator bucket into a message
530 * @msg: Netlink message to dump into
531 * @portid: Port making netlink request
532 * @seq: Sequence number of netlink message
533 * @bat_priv: The bat priv with all the soft interface information
534 * @if_outgoing: Limit dump to entries with this outgoing interface
535 * @head: Bucket to be dumped
536 * @idx_s: Number of entries to be skipped
537 * @sub: Number of sub entries to be skipped
539 * Return: Error code, or 0 on success
542 batadv_v_orig_dump_bucket(struct sk_buff
*msg
, u32 portid
, u32 seq
,
543 struct batadv_priv
*bat_priv
,
544 struct batadv_hard_iface
*if_outgoing
,
545 struct hlist_head
*head
, int *idx_s
, int *sub
)
547 struct batadv_orig_node
*orig_node
;
551 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
555 if (batadv_v_orig_dump_entry(msg
, portid
, seq
, bat_priv
,
556 if_outgoing
, orig_node
, sub
)) {
570 * batadv_v_orig_dump() - Dump the originators into a message
571 * @msg: Netlink message to dump into
572 * @cb: Control block containing additional options
573 * @bat_priv: The bat priv with all the soft interface information
574 * @if_outgoing: Limit dump to entries with this outgoing interface
577 batadv_v_orig_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
578 struct batadv_priv
*bat_priv
,
579 struct batadv_hard_iface
*if_outgoing
)
581 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
582 struct hlist_head
*head
;
583 int bucket
= cb
->args
[0];
584 int idx
= cb
->args
[1];
585 int sub
= cb
->args
[2];
586 int portid
= NETLINK_CB(cb
->skb
).portid
;
588 while (bucket
< hash
->size
) {
589 head
= &hash
->table
[bucket
];
591 if (batadv_v_orig_dump_bucket(msg
, portid
,
593 bat_priv
, if_outgoing
, head
, &idx
,
600 cb
->args
[0] = bucket
;
605 static int batadv_v_neigh_cmp(struct batadv_neigh_node
*neigh1
,
606 struct batadv_hard_iface
*if_outgoing1
,
607 struct batadv_neigh_node
*neigh2
,
608 struct batadv_hard_iface
*if_outgoing2
)
610 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
613 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
617 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
621 ret
= ifinfo1
->bat_v
.throughput
- ifinfo2
->bat_v
.throughput
;
623 batadv_neigh_ifinfo_put(ifinfo2
);
625 batadv_neigh_ifinfo_put(ifinfo1
);
630 static bool batadv_v_neigh_is_sob(struct batadv_neigh_node
*neigh1
,
631 struct batadv_hard_iface
*if_outgoing1
,
632 struct batadv_neigh_node
*neigh2
,
633 struct batadv_hard_iface
*if_outgoing2
)
635 struct batadv_neigh_ifinfo
*ifinfo1
, *ifinfo2
;
639 ifinfo1
= batadv_neigh_ifinfo_get(neigh1
, if_outgoing1
);
643 ifinfo2
= batadv_neigh_ifinfo_get(neigh2
, if_outgoing2
);
647 threshold
= ifinfo1
->bat_v
.throughput
/ 4;
648 threshold
= ifinfo1
->bat_v
.throughput
- threshold
;
650 ret
= ifinfo2
->bat_v
.throughput
> threshold
;
652 batadv_neigh_ifinfo_put(ifinfo2
);
654 batadv_neigh_ifinfo_put(ifinfo1
);
660 * batadv_v_init_sel_class() - initialize GW selection class
661 * @bat_priv: the bat priv with all the soft interface information
663 static void batadv_v_init_sel_class(struct batadv_priv
*bat_priv
)
665 /* set default throughput difference threshold to 5Mbps */
666 atomic_set(&bat_priv
->gw
.sel_class
, 50);
669 static ssize_t
batadv_v_store_sel_class(struct batadv_priv
*bat_priv
,
670 char *buff
, size_t count
)
672 u32 old_class
, class;
674 if (!batadv_parse_throughput(bat_priv
->soft_iface
, buff
,
675 "B.A.T.M.A.N. V GW selection class",
679 old_class
= atomic_read(&bat_priv
->gw
.sel_class
);
680 atomic_set(&bat_priv
->gw
.sel_class
, class);
682 if (old_class
!= class)
683 batadv_gw_reselect(bat_priv
);
688 static ssize_t
batadv_v_show_sel_class(struct batadv_priv
*bat_priv
, char *buff
)
690 u32
class = atomic_read(&bat_priv
->gw
.sel_class
);
692 return sprintf(buff
, "%u.%u MBit\n", class / 10, class % 10);
696 * batadv_v_gw_throughput_get() - retrieve the GW-bandwidth for a given GW
697 * @gw_node: the GW to retrieve the metric for
698 * @bw: the pointer where the metric will be stored. The metric is computed as
699 * the minimum between the GW advertised throughput and the path throughput to
702 * Return: 0 on success, -1 on failure
704 static int batadv_v_gw_throughput_get(struct batadv_gw_node
*gw_node
, u32
*bw
)
706 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
707 struct batadv_orig_node
*orig_node
;
708 struct batadv_neigh_node
*router
;
711 orig_node
= gw_node
->orig_node
;
712 router
= batadv_orig_router_get(orig_node
, BATADV_IF_DEFAULT
);
716 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
720 /* the GW metric is computed as the minimum between the path throughput
721 * to reach the GW itself and the advertised bandwidth.
722 * This gives us an approximation of the effective throughput that the
723 * client can expect via this particular GW node
725 *bw
= router_ifinfo
->bat_v
.throughput
;
726 *bw
= min_t(u32
, *bw
, gw_node
->bandwidth_down
);
731 batadv_neigh_node_put(router
);
733 batadv_neigh_ifinfo_put(router_ifinfo
);
739 * batadv_v_gw_get_best_gw_node() - retrieve the best GW node
740 * @bat_priv: the bat priv with all the soft interface information
742 * Return: the GW node having the best GW-metric, NULL if no GW is known
744 static struct batadv_gw_node
*
745 batadv_v_gw_get_best_gw_node(struct batadv_priv
*bat_priv
)
747 struct batadv_gw_node
*gw_node
, *curr_gw
= NULL
;
751 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
752 if (!kref_get_unless_zero(&gw_node
->refcount
))
755 if (batadv_v_gw_throughput_get(gw_node
, &bw
) < 0)
758 if (curr_gw
&& bw
<= max_bw
)
762 batadv_gw_node_put(curr_gw
);
765 kref_get(&curr_gw
->refcount
);
769 batadv_gw_node_put(gw_node
);
777 * batadv_v_gw_is_eligible() - check if a originator would be selected as GW
778 * @bat_priv: the bat priv with all the soft interface information
779 * @curr_gw_orig: originator representing the currently selected GW
780 * @orig_node: the originator representing the new candidate
782 * Return: true if orig_node can be selected as current GW, false otherwise
784 static bool batadv_v_gw_is_eligible(struct batadv_priv
*bat_priv
,
785 struct batadv_orig_node
*curr_gw_orig
,
786 struct batadv_orig_node
*orig_node
)
788 struct batadv_gw_node
*curr_gw
, *orig_gw
= NULL
;
789 u32 gw_throughput
, orig_throughput
, threshold
;
792 threshold
= atomic_read(&bat_priv
->gw
.sel_class
);
794 curr_gw
= batadv_gw_node_get(bat_priv
, curr_gw_orig
);
800 if (batadv_v_gw_throughput_get(curr_gw
, &gw_throughput
) < 0) {
805 orig_gw
= batadv_gw_node_get(bat_priv
, orig_node
);
809 if (batadv_v_gw_throughput_get(orig_gw
, &orig_throughput
) < 0)
812 if (orig_throughput
< gw_throughput
)
815 if ((orig_throughput
- gw_throughput
) < threshold
)
818 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
819 "Restarting gateway selection: better gateway found (throughput curr: %u, throughput new: %u)\n",
820 gw_throughput
, orig_throughput
);
825 batadv_gw_node_put(curr_gw
);
827 batadv_gw_node_put(orig_gw
);
832 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
833 /* fails if orig_node has no router */
834 static int batadv_v_gw_write_buffer_text(struct batadv_priv
*bat_priv
,
835 struct seq_file
*seq
,
836 const struct batadv_gw_node
*gw_node
)
838 struct batadv_gw_node
*curr_gw
;
839 struct batadv_neigh_node
*router
;
840 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
843 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
847 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
851 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
853 seq_printf(seq
, "%s %pM (%9u.%1u) %pM [%10s]: %u.%u/%u.%u MBit\n",
854 (curr_gw
== gw_node
? "=>" : " "),
855 gw_node
->orig_node
->orig
,
856 router_ifinfo
->bat_v
.throughput
/ 10,
857 router_ifinfo
->bat_v
.throughput
% 10, router
->addr
,
858 router
->if_incoming
->net_dev
->name
,
859 gw_node
->bandwidth_down
/ 10,
860 gw_node
->bandwidth_down
% 10,
861 gw_node
->bandwidth_up
/ 10,
862 gw_node
->bandwidth_up
% 10);
863 ret
= seq_has_overflowed(seq
) ? -1 : 0;
866 batadv_gw_node_put(curr_gw
);
869 batadv_neigh_ifinfo_put(router_ifinfo
);
871 batadv_neigh_node_put(router
);
876 * batadv_v_gw_print() - print the gateway list
877 * @bat_priv: the bat priv with all the soft interface information
878 * @seq: gateway table seq_file struct
880 static void batadv_v_gw_print(struct batadv_priv
*bat_priv
,
881 struct seq_file
*seq
)
883 struct batadv_gw_node
*gw_node
;
887 " Gateway ( throughput) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
890 hlist_for_each_entry_rcu(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
891 /* fails if orig_node has no router */
892 if (batadv_v_gw_write_buffer_text(bat_priv
, seq
, gw_node
) < 0)
900 seq_puts(seq
, "No gateways in range ...\n");
905 * batadv_v_gw_dump_entry() - Dump a gateway into a message
906 * @msg: Netlink message to dump into
907 * @portid: Port making netlink request
908 * @cb: Control block containing additional options
909 * @bat_priv: The bat priv with all the soft interface information
910 * @gw_node: Gateway to be dumped
912 * Return: Error code, or 0 on success
914 static int batadv_v_gw_dump_entry(struct sk_buff
*msg
, u32 portid
,
915 struct netlink_callback
*cb
,
916 struct batadv_priv
*bat_priv
,
917 struct batadv_gw_node
*gw_node
)
919 struct batadv_neigh_ifinfo
*router_ifinfo
= NULL
;
920 struct batadv_neigh_node
*router
;
921 struct batadv_gw_node
*curr_gw
= NULL
;
925 router
= batadv_orig_router_get(gw_node
->orig_node
, BATADV_IF_DEFAULT
);
929 router_ifinfo
= batadv_neigh_ifinfo_get(router
, BATADV_IF_DEFAULT
);
933 curr_gw
= batadv_gw_get_selected_gw_node(bat_priv
);
935 hdr
= genlmsg_put(msg
, portid
, cb
->nlh
->nlmsg_seq
,
936 &batadv_netlink_family
, NLM_F_MULTI
,
937 BATADV_CMD_GET_GATEWAYS
);
943 genl_dump_check_consistent(cb
, hdr
);
947 if (curr_gw
== gw_node
) {
948 if (nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
)) {
949 genlmsg_cancel(msg
, hdr
);
954 if (nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
955 gw_node
->orig_node
->orig
)) {
956 genlmsg_cancel(msg
, hdr
);
960 if (nla_put_u32(msg
, BATADV_ATTR_THROUGHPUT
,
961 router_ifinfo
->bat_v
.throughput
)) {
962 genlmsg_cancel(msg
, hdr
);
966 if (nla_put(msg
, BATADV_ATTR_ROUTER
, ETH_ALEN
, router
->addr
)) {
967 genlmsg_cancel(msg
, hdr
);
971 if (nla_put_string(msg
, BATADV_ATTR_HARD_IFNAME
,
972 router
->if_incoming
->net_dev
->name
)) {
973 genlmsg_cancel(msg
, hdr
);
977 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_DOWN
,
978 gw_node
->bandwidth_down
)) {
979 genlmsg_cancel(msg
, hdr
);
983 if (nla_put_u32(msg
, BATADV_ATTR_BANDWIDTH_UP
, gw_node
->bandwidth_up
)) {
984 genlmsg_cancel(msg
, hdr
);
988 genlmsg_end(msg
, hdr
);
993 batadv_gw_node_put(curr_gw
);
995 batadv_neigh_ifinfo_put(router_ifinfo
);
997 batadv_neigh_node_put(router
);
1002 * batadv_v_gw_dump() - Dump gateways into a message
1003 * @msg: Netlink message to dump into
1004 * @cb: Control block containing additional options
1005 * @bat_priv: The bat priv with all the soft interface information
1007 static void batadv_v_gw_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
,
1008 struct batadv_priv
*bat_priv
)
1010 int portid
= NETLINK_CB(cb
->skb
).portid
;
1011 struct batadv_gw_node
*gw_node
;
1012 int idx_skip
= cb
->args
[0];
1015 spin_lock_bh(&bat_priv
->gw
.list_lock
);
1016 cb
->seq
= bat_priv
->gw
.generation
<< 1 | 1;
1018 hlist_for_each_entry(gw_node
, &bat_priv
->gw
.gateway_list
, list
) {
1019 if (idx
++ < idx_skip
)
1022 if (batadv_v_gw_dump_entry(msg
, portid
, cb
, bat_priv
,
1031 spin_unlock_bh(&bat_priv
->gw
.list_lock
);
1033 cb
->args
[0] = idx_skip
;
1036 static struct batadv_algo_ops batadv_batman_v __read_mostly
= {
1039 .activate
= batadv_v_iface_activate
,
1040 .enable
= batadv_v_iface_enable
,
1041 .disable
= batadv_v_iface_disable
,
1042 .update_mac
= batadv_v_iface_update_mac
,
1043 .primary_set
= batadv_v_primary_iface_set
,
1046 .hardif_init
= batadv_v_hardif_neigh_init
,
1047 .cmp
= batadv_v_neigh_cmp
,
1048 .is_similar_or_better
= batadv_v_neigh_is_sob
,
1049 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1050 .print
= batadv_v_neigh_print
,
1052 .dump
= batadv_v_neigh_dump
,
1055 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1056 .print
= batadv_v_orig_print
,
1058 .dump
= batadv_v_orig_dump
,
1061 .init_sel_class
= batadv_v_init_sel_class
,
1062 .store_sel_class
= batadv_v_store_sel_class
,
1063 .show_sel_class
= batadv_v_show_sel_class
,
1064 .get_best_gw_node
= batadv_v_gw_get_best_gw_node
,
1065 .is_eligible
= batadv_v_gw_is_eligible
,
1066 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1067 .print
= batadv_v_gw_print
,
1069 .dump
= batadv_v_gw_dump
,
1074 * batadv_v_hardif_init() - initialize the algorithm specific fields in the
1075 * hard-interface object
1076 * @hard_iface: the hard-interface to initialize
1078 void batadv_v_hardif_init(struct batadv_hard_iface
*hard_iface
)
1080 /* enable link throughput auto-detection by setting the throughput
1083 atomic_set(&hard_iface
->bat_v
.throughput_override
, 0);
1084 atomic_set(&hard_iface
->bat_v
.elp_interval
, 500);
1086 hard_iface
->bat_v
.aggr_len
= 0;
1087 skb_queue_head_init(&hard_iface
->bat_v
.aggr_list
);
1088 INIT_DELAYED_WORK(&hard_iface
->bat_v
.aggr_wq
,
1089 batadv_v_ogm_aggr_work
);
1093 * batadv_v_mesh_init() - initialize the B.A.T.M.A.N. V private resources for a
1095 * @bat_priv: the object representing the mesh interface to initialise
1097 * Return: 0 on success or a negative error code otherwise
1099 int batadv_v_mesh_init(struct batadv_priv
*bat_priv
)
1103 ret
= batadv_v_ogm_init(bat_priv
);
1111 * batadv_v_mesh_free() - free the B.A.T.M.A.N. V private resources for a mesh
1112 * @bat_priv: the object representing the mesh interface to free
1114 void batadv_v_mesh_free(struct batadv_priv
*bat_priv
)
1116 batadv_v_ogm_free(bat_priv
);
1120 * batadv_v_init() - B.A.T.M.A.N. V initialization function
1122 * Description: Takes care of initializing all the subcomponents.
1123 * It is invoked upon module load only.
1125 * Return: 0 on success or a negative error code otherwise
1127 int __init
batadv_v_init(void)
1131 /* B.A.T.M.A.N. V echo location protocol packet */
1132 ret
= batadv_recv_handler_register(BATADV_ELP
,
1133 batadv_v_elp_packet_recv
);
1137 ret
= batadv_recv_handler_register(BATADV_OGM2
,
1138 batadv_v_ogm_packet_recv
);
1140 goto elp_unregister
;
1142 ret
= batadv_algo_register(&batadv_batman_v
);
1144 goto ogm_unregister
;
1149 batadv_recv_handler_unregister(BATADV_OGM2
);
1152 batadv_recv_handler_unregister(BATADV_ELP
);