hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/net/batman-adv/bat_v_ogm.c
....@@ -1,19 +1,7 @@
11 // SPDX-License-Identifier: GPL-2.0
2
-/* Copyright (C) 2013-2018 B.A.T.M.A.N. contributors:
2
+/* Copyright (C) 2013-2020 B.A.T.M.A.N. contributors:
33 *
44 * Antonio Quartulli
5
- *
6
- * This program is free software; you can redistribute it and/or
7
- * modify it under the terms of version 2 of the GNU General Public
8
- * License as published by the Free Software Foundation.
9
- *
10
- * This program is distributed in the hope that it will be useful, but
11
- * WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- * General Public License for more details.
14
- *
15
- * You should have received a copy of the GNU General Public License
16
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
175 */
186
197 #include "bat_v_ogm.h"
....@@ -32,11 +20,13 @@
3220 #include <linux/lockdep.h>
3321 #include <linux/mutex.h>
3422 #include <linux/netdevice.h>
23
+#include <linux/prandom.h>
3524 #include <linux/random.h>
3625 #include <linux/rculist.h>
3726 #include <linux/rcupdate.h>
3827 #include <linux/skbuff.h>
3928 #include <linux/slab.h>
29
+#include <linux/spinlock.h>
4030 #include <linux/stddef.h>
4131 #include <linux/string.h>
4232 #include <linux/types.h>
....@@ -58,9 +48,9 @@
5848 * @bat_priv: the bat priv with all the soft interface information
5949 * @addr: the address of the originator
6050 *
61
- * Return: the orig_node corresponding to the specified address. If such object
62
- * does not exist it is allocated here. In case of allocation failure returns
63
- * NULL.
51
+ * Return: the orig_node corresponding to the specified address. If such an
52
+ * object does not exist, it is allocated here. In case of allocation failure
53
+ * returns NULL.
6454 */
6555 struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv,
6656 const u8 *addr)
....@@ -91,6 +81,20 @@
9181 }
9282
9383 /**
84
+ * batadv_v_ogm_start_queue_timer() - restart the OGM aggregation timer
85
+ * @hard_iface: the interface to use to send the OGM
86
+ */
87
+static void batadv_v_ogm_start_queue_timer(struct batadv_hard_iface *hard_iface)
88
+{
89
+ unsigned int msecs = BATADV_MAX_AGGREGATION_MS * 1000;
90
+
91
+ /* msecs * [0.9, 1.1] */
92
+ msecs += prandom_u32_max(msecs / 5) - (msecs / 10);
93
+ queue_delayed_work(batadv_event_workqueue, &hard_iface->bat_v.aggr_wq,
94
+ msecs_to_jiffies(msecs / 1000));
95
+}
96
+
97
+/**
9498 * batadv_v_ogm_start_timer() - restart the OGM sending timer
9599 * @bat_priv: the bat priv with all the soft interface information
96100 */
....@@ -104,7 +108,7 @@
104108 return;
105109
106110 msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
107
- msecs += prandom_u32() % (2 * BATADV_JITTER);
111
+ msecs += prandom_u32_max(2 * BATADV_JITTER);
108112 queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq,
109113 msecs_to_jiffies(msecs));
110114 }
....@@ -127,6 +131,126 @@
127131 skb->len + ETH_HLEN);
128132
129133 batadv_send_broadcast_skb(skb, hard_iface);
134
+}
135
+
136
+/**
137
+ * batadv_v_ogm_len() - OGMv2 packet length
138
+ * @skb: the OGM to check
139
+ *
140
+ * Return: Length of the given OGMv2 packet, including tvlv length, excluding
141
+ * ethernet header length.
142
+ */
143
+static unsigned int batadv_v_ogm_len(struct sk_buff *skb)
144
+{
145
+ struct batadv_ogm2_packet *ogm_packet;
146
+
147
+ ogm_packet = (struct batadv_ogm2_packet *)skb->data;
148
+ return BATADV_OGM2_HLEN + ntohs(ogm_packet->tvlv_len);
149
+}
150
+
151
+/**
152
+ * batadv_v_ogm_queue_left() - check if given OGM still fits aggregation queue
153
+ * @skb: the OGM to check
154
+ * @hard_iface: the interface to use to send the OGM
155
+ *
156
+ * Caller needs to hold the hard_iface->bat_v.aggr_list.lock.
157
+ *
158
+ * Return: True, if the given OGMv2 packet still fits, false otherwise.
159
+ */
160
+static bool batadv_v_ogm_queue_left(struct sk_buff *skb,
161
+ struct batadv_hard_iface *hard_iface)
162
+{
163
+ unsigned int max = min_t(unsigned int, hard_iface->net_dev->mtu,
164
+ BATADV_MAX_AGGREGATION_BYTES);
165
+ unsigned int ogm_len = batadv_v_ogm_len(skb);
166
+
167
+ lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
168
+
169
+ return hard_iface->bat_v.aggr_len + ogm_len <= max;
170
+}
171
+
172
+/**
173
+ * batadv_v_ogm_aggr_list_free - free all elements in an aggregation queue
174
+ * @hard_iface: the interface holding the aggregation queue
175
+ *
176
+ * Empties the OGMv2 aggregation queue and frees all the skbs it contains.
177
+ *
178
+ * Caller needs to hold the hard_iface->bat_v.aggr_list.lock.
179
+ */
180
+static void batadv_v_ogm_aggr_list_free(struct batadv_hard_iface *hard_iface)
181
+{
182
+ lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
183
+
184
+ __skb_queue_purge(&hard_iface->bat_v.aggr_list);
185
+ hard_iface->bat_v.aggr_len = 0;
186
+}
187
+
188
+/**
189
+ * batadv_v_ogm_aggr_send() - flush & send aggregation queue
190
+ * @hard_iface: the interface with the aggregation queue to flush
191
+ *
192
+ * Aggregates all OGMv2 packets currently in the aggregation queue into a
193
+ * single OGMv2 packet and transmits this aggregate.
194
+ *
195
+ * The aggregation queue is empty after this call.
196
+ *
197
+ * Caller needs to hold the hard_iface->bat_v.aggr_list.lock.
198
+ */
199
+static void batadv_v_ogm_aggr_send(struct batadv_hard_iface *hard_iface)
200
+{
201
+ unsigned int aggr_len = hard_iface->bat_v.aggr_len;
202
+ struct sk_buff *skb_aggr;
203
+ unsigned int ogm_len;
204
+ struct sk_buff *skb;
205
+
206
+ lockdep_assert_held(&hard_iface->bat_v.aggr_list.lock);
207
+
208
+ if (!aggr_len)
209
+ return;
210
+
211
+ skb_aggr = dev_alloc_skb(aggr_len + ETH_HLEN + NET_IP_ALIGN);
212
+ if (!skb_aggr) {
213
+ batadv_v_ogm_aggr_list_free(hard_iface);
214
+ return;
215
+ }
216
+
217
+ skb_reserve(skb_aggr, ETH_HLEN + NET_IP_ALIGN);
218
+ skb_reset_network_header(skb_aggr);
219
+
220
+ while ((skb = __skb_dequeue(&hard_iface->bat_v.aggr_list))) {
221
+ hard_iface->bat_v.aggr_len -= batadv_v_ogm_len(skb);
222
+
223
+ ogm_len = batadv_v_ogm_len(skb);
224
+ skb_put_data(skb_aggr, skb->data, ogm_len);
225
+
226
+ consume_skb(skb);
227
+ }
228
+
229
+ batadv_v_ogm_send_to_if(skb_aggr, hard_iface);
230
+}
231
+
232
+/**
233
+ * batadv_v_ogm_queue_on_if() - queue a batman ogm on a given interface
234
+ * @skb: the OGM to queue
235
+ * @hard_iface: the interface to queue the OGM on
236
+ */
237
+static void batadv_v_ogm_queue_on_if(struct sk_buff *skb,
238
+ struct batadv_hard_iface *hard_iface)
239
+{
240
+ struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
241
+
242
+ if (!atomic_read(&bat_priv->aggregated_ogms)) {
243
+ batadv_v_ogm_send_to_if(skb, hard_iface);
244
+ return;
245
+ }
246
+
247
+ spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
248
+ if (!batadv_v_ogm_queue_left(skb, hard_iface))
249
+ batadv_v_ogm_aggr_send(hard_iface);
250
+
251
+ hard_iface->bat_v.aggr_len += batadv_v_ogm_len(skb);
252
+ __skb_queue_tail(&hard_iface->bat_v.aggr_list, skb);
253
+ spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
130254 }
131255
132256 /**
....@@ -221,7 +345,7 @@
221345 break;
222346 }
223347
224
- batadv_v_ogm_send_to_if(skb_tmp, hard_iface);
348
+ batadv_v_ogm_queue_on_if(skb_tmp, hard_iface);
225349 batadv_hardif_put(hard_iface);
226350 }
227351 rcu_read_unlock();
....@@ -252,10 +376,31 @@
252376 }
253377
254378 /**
379
+ * batadv_v_ogm_aggr_work() - OGM queue periodic task per interface
380
+ * @work: work queue item
381
+ *
382
+ * Emits aggregated OGM messages in regular intervals.
383
+ */
384
+void batadv_v_ogm_aggr_work(struct work_struct *work)
385
+{
386
+ struct batadv_hard_iface_bat_v *batv;
387
+ struct batadv_hard_iface *hard_iface;
388
+
389
+ batv = container_of(work, struct batadv_hard_iface_bat_v, aggr_wq.work);
390
+ hard_iface = container_of(batv, struct batadv_hard_iface, bat_v);
391
+
392
+ spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
393
+ batadv_v_ogm_aggr_send(hard_iface);
394
+ spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
395
+
396
+ batadv_v_ogm_start_queue_timer(hard_iface);
397
+}
398
+
399
+/**
255400 * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V
256401 * @hard_iface: the interface to prepare
257402 *
258
- * Takes care of scheduling own OGM sending routine for this interface.
403
+ * Takes care of scheduling its own OGM sending routine for this interface.
259404 *
260405 * Return: 0 on success or a negative error code otherwise
261406 */
....@@ -263,9 +408,23 @@
263408 {
264409 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
265410
411
+ batadv_v_ogm_start_queue_timer(hard_iface);
266412 batadv_v_ogm_start_timer(bat_priv);
267413
268414 return 0;
415
+}
416
+
417
+/**
418
+ * batadv_v_ogm_iface_disable() - release OGM interface private resources
419
+ * @hard_iface: interface for which the resources have to be released
420
+ */
421
+void batadv_v_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
422
+{
423
+ cancel_delayed_work_sync(&hard_iface->bat_v.aggr_wq);
424
+
425
+ spin_lock_bh(&hard_iface->bat_v.aggr_list.lock);
426
+ batadv_v_ogm_aggr_list_free(hard_iface);
427
+ spin_unlock_bh(&hard_iface->bat_v.aggr_list.lock);
269428 }
270429
271430 /**
....@@ -297,15 +456,17 @@
297456 * @throughput: the current throughput
298457 *
299458 * Apply a penalty on the current throughput metric value based on the
300
- * characteristic of the interface where the OGM has been received. The return
301
- * value is computed as follows:
459
+ * characteristic of the interface where the OGM has been received.
460
+ *
461
+ * Initially the per hardif hop penalty is applied to the throughput. After
462
+ * that the return value is then computed as follows:
302463 * - throughput * 50% if the incoming and outgoing interface are the
303464 * same WiFi interface and the throughput is above
304465 * 1MBit/s
305466 * - throughput if the outgoing interface is the default
306467 * interface (i.e. this OGM is processed for the
307468 * internal table and not forwarded)
308
- * - throughput * hop penalty otherwise
469
+ * - throughput * node hop penalty otherwise
309470 *
310471 * Return: the penalised throughput metric.
311472 */
....@@ -314,8 +475,13 @@
314475 struct batadv_hard_iface *if_outgoing,
315476 u32 throughput)
316477 {
478
+ int if_hop_penalty = atomic_read(&if_incoming->hop_penalty);
317479 int hop_penalty = atomic_read(&bat_priv->hop_penalty);
318480 int hop_penalty_max = BATADV_TQ_MAX_VALUE;
481
+
482
+ /* Apply per hardif hop penalty */
483
+ throughput = throughput * (hop_penalty_max - if_hop_penalty) /
484
+ hop_penalty_max;
319485
320486 /* Don't apply hop penalty in default originator table. */
321487 if (if_outgoing == BATADV_IF_DEFAULT)
....@@ -414,7 +580,7 @@
414580 if_outgoing->net_dev->name, ntohl(ogm_forward->throughput),
415581 ogm_forward->ttl, if_incoming->net_dev->name);
416582
417
- batadv_v_ogm_send_to_if(skb, if_outgoing);
583
+ batadv_v_ogm_queue_on_if(skb, if_outgoing);
418584
419585 out:
420586 if (orig_ifinfo)
....@@ -689,7 +855,7 @@
689855 * batadv_v_ogm_process() - process an incoming batman v OGM
690856 * @skb: the skb containing the OGM
691857 * @ogm_offset: offset to the OGM which should be processed (for aggregates)
692
- * @if_incoming: the interface where this packet was receved
858
+ * @if_incoming: the interface where this packet was received
693859 */
694860 static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset,
695861 struct batadv_hard_iface *if_incoming)