Commit | Line | Data |
---|---|---|
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> | |
97869060 | 30 | #include <linux/types.h> |
c833484e | 31 | #include <linux/workqueue.h> |
d6f94d91 LL |
32 | |
33 | #include "bat_v_elp.h" | |
0da00359 | 34 | #include "bat_v_ogm.h" |
261e264d | 35 | #include "hash.h" |
97869060 | 36 | #include "originator.h" |
162bd64c | 37 | #include "packet.h" |
d6f94d91 LL |
38 | |
39 | static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface) | |
40 | { | |
0da00359 AQ |
41 | 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 | ||
0b5ecc68 AQ |
51 | /* 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 | ||
0da00359 | 56 | return ret; |
d6f94d91 LL |
57 | } |
58 | ||
59 | static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface) | |
60 | { | |
61 | batadv_v_elp_iface_disable(hard_iface); | |
62 | } | |
63 | ||
64 | static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface) | |
65 | { | |
66 | } | |
67 | ||
68 | static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface) | |
69 | { | |
70 | batadv_v_elp_primary_iface_set(hard_iface); | |
0da00359 | 71 | batadv_v_ogm_primary_iface_set(hard_iface); |
d6f94d91 LL |
72 | } |
73 | ||
162bd64c LL |
74 | static void |
75 | batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh) | |
76 | { | |
77 | ewma_throughput_init(&hardif_neigh->bat_v.throughput); | |
c833484e AQ |
78 | INIT_WORK(&hardif_neigh->bat_v.metric_work, |
79 | batadv_v_elp_throughput_metric_update); | |
162bd64c LL |
80 | } |
81 | ||
d6f94d91 LL |
82 | static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface) |
83 | { | |
84 | } | |
85 | ||
86 | static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet) | |
87 | { | |
88 | } | |
89 | ||
261e264d AQ |
90 | /** |
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 | */ | |
98 | static void | |
99 | batadv_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 | /** | |
121 | * batadv_v_orig_print - print the originator table | |
122 | * @bat_priv: the bat priv with all the soft interface information | |
123 | * @seq: debugfs table seq_file struct | |
124 | * @if_outgoing: the outgoing interface for which this should be printed | |
125 | */ | |
126 | static void batadv_v_orig_print(struct batadv_priv *bat_priv, | |
127 | struct seq_file *seq, | |
128 | struct batadv_hard_iface *if_outgoing) | |
129 | { | |
130 | struct batadv_neigh_node *neigh_node; | |
131 | struct batadv_hashtable *hash = bat_priv->orig_hash; | |
132 | int last_seen_msecs, last_seen_secs; | |
133 | struct batadv_orig_node *orig_node; | |
134 | struct batadv_neigh_ifinfo *n_ifinfo; | |
135 | unsigned long last_seen_jiffies; | |
136 | struct hlist_head *head; | |
137 | int batman_count = 0; | |
138 | u32 i; | |
139 | ||
140 | seq_printf(seq, " %-15s %s (%11s) %17s [%10s]: %20s ...\n", | |
141 | "Originator", "last-seen", "throughput", "Nexthop", | |
142 | "outgoingIF", "Potential nexthops"); | |
143 | ||
144 | for (i = 0; i < hash->size; i++) { | |
145 | head = &hash->table[i]; | |
146 | ||
147 | rcu_read_lock(); | |
148 | hlist_for_each_entry_rcu(orig_node, head, hash_entry) { | |
149 | neigh_node = batadv_orig_router_get(orig_node, | |
150 | if_outgoing); | |
151 | if (!neigh_node) | |
152 | continue; | |
153 | ||
154 | n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, | |
155 | if_outgoing); | |
156 | if (!n_ifinfo) | |
157 | goto next; | |
158 | ||
159 | last_seen_jiffies = jiffies - orig_node->last_seen; | |
160 | last_seen_msecs = jiffies_to_msecs(last_seen_jiffies); | |
161 | last_seen_secs = last_seen_msecs / 1000; | |
162 | last_seen_msecs = last_seen_msecs % 1000; | |
163 | ||
164 | seq_printf(seq, "%pM %4i.%03is (%9u.%1u) %pM [%10s]:", | |
165 | orig_node->orig, last_seen_secs, | |
166 | last_seen_msecs, | |
167 | n_ifinfo->bat_v.throughput / 10, | |
168 | n_ifinfo->bat_v.throughput % 10, | |
169 | neigh_node->addr, | |
170 | neigh_node->if_incoming->net_dev->name); | |
171 | ||
172 | batadv_v_orig_print_neigh(orig_node, if_outgoing, seq); | |
173 | seq_puts(seq, "\n"); | |
174 | batman_count++; | |
175 | ||
176 | next: | |
177 | batadv_neigh_node_put(neigh_node); | |
178 | if (n_ifinfo) | |
179 | batadv_neigh_ifinfo_put(n_ifinfo); | |
180 | } | |
181 | rcu_read_unlock(); | |
182 | } | |
183 | ||
184 | if (batman_count == 0) | |
185 | seq_puts(seq, "No batman nodes in range ...\n"); | |
186 | } | |
187 | ||
97869060 AQ |
188 | static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1, |
189 | struct batadv_hard_iface *if_outgoing1, | |
190 | struct batadv_neigh_node *neigh2, | |
191 | struct batadv_hard_iface *if_outgoing2) | |
192 | { | |
193 | struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2; | |
194 | ||
195 | ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); | |
196 | ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); | |
197 | ||
198 | if (WARN_ON(!ifinfo1 || !ifinfo2)) | |
199 | return 0; | |
200 | ||
201 | return ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput; | |
202 | } | |
203 | ||
204 | static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1, | |
205 | struct batadv_hard_iface *if_outgoing1, | |
206 | struct batadv_neigh_node *neigh2, | |
207 | struct batadv_hard_iface *if_outgoing2) | |
208 | { | |
209 | struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2; | |
210 | u32 threshold; | |
211 | ||
212 | ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); | |
213 | ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); | |
214 | ||
215 | threshold = ifinfo1->bat_v.throughput / 4; | |
216 | threshold = ifinfo1->bat_v.throughput - threshold; | |
217 | ||
218 | return ifinfo2->bat_v.throughput > threshold; | |
219 | } | |
220 | ||
d6f94d91 LL |
221 | static struct batadv_algo_ops batadv_batman_v __read_mostly = { |
222 | .name = "BATMAN_V", | |
223 | .bat_iface_enable = batadv_v_iface_enable, | |
224 | .bat_iface_disable = batadv_v_iface_disable, | |
225 | .bat_iface_update_mac = batadv_v_iface_update_mac, | |
226 | .bat_primary_iface_set = batadv_v_primary_iface_set, | |
162bd64c | 227 | .bat_hardif_neigh_init = batadv_v_hardif_neigh_init, |
d6f94d91 LL |
228 | .bat_ogm_emit = batadv_v_ogm_emit, |
229 | .bat_ogm_schedule = batadv_v_ogm_schedule, | |
261e264d | 230 | .bat_orig_print = batadv_v_orig_print, |
97869060 AQ |
231 | .bat_neigh_cmp = batadv_v_neigh_cmp, |
232 | .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob, | |
d6f94d91 LL |
233 | }; |
234 | ||
0da00359 AQ |
235 | /** |
236 | * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a | |
237 | * mesh | |
238 | * @bat_priv: the object representing the mesh interface to initialise | |
239 | * | |
240 | * Return: 0 on success or a negative error code otherwise | |
241 | */ | |
242 | int batadv_v_mesh_init(struct batadv_priv *bat_priv) | |
243 | { | |
244 | return batadv_v_ogm_init(bat_priv); | |
245 | } | |
246 | ||
247 | /** | |
248 | * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh | |
249 | * @bat_priv: the object representing the mesh interface to free | |
250 | */ | |
251 | void batadv_v_mesh_free(struct batadv_priv *bat_priv) | |
252 | { | |
253 | batadv_v_ogm_free(bat_priv); | |
254 | } | |
255 | ||
d6f94d91 LL |
256 | /** |
257 | * batadv_v_init - B.A.T.M.A.N. V initialization function | |
258 | * | |
259 | * Description: Takes care of initializing all the subcomponents. | |
260 | * It is invoked upon module load only. | |
261 | * | |
262 | * Return: 0 on success or a negative error code otherwise | |
263 | */ | |
264 | int __init batadv_v_init(void) | |
265 | { | |
162bd64c LL |
266 | int ret; |
267 | ||
268 | /* B.A.T.M.A.N. V echo location protocol packet */ | |
269 | ret = batadv_recv_handler_register(BATADV_ELP, | |
270 | batadv_v_elp_packet_recv); | |
271 | if (ret < 0) | |
272 | return ret; | |
273 | ||
0da00359 AQ |
274 | ret = batadv_recv_handler_register(BATADV_OGM2, |
275 | batadv_v_ogm_packet_recv); | |
276 | if (ret < 0) | |
277 | goto elp_unregister; | |
162bd64c | 278 | |
0da00359 | 279 | ret = batadv_algo_register(&batadv_batman_v); |
162bd64c | 280 | if (ret < 0) |
0da00359 AQ |
281 | goto ogm_unregister; |
282 | ||
283 | return ret; | |
284 | ||
285 | ogm_unregister: | |
286 | batadv_recv_handler_unregister(BATADV_OGM2); | |
287 | ||
288 | elp_unregister: | |
289 | batadv_recv_handler_unregister(BATADV_ELP); | |
162bd64c LL |
290 | |
291 | return ret; | |
d6f94d91 | 292 | } |