| .. | .. |
|---|
| 1 | 1 | // 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: |
|---|
| 3 | 3 | * |
|---|
| 4 | 4 | * 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/>. |
|---|
| 17 | 5 | */ |
|---|
| 18 | 6 | |
|---|
| 19 | 7 | #include "bat_v_ogm.h" |
|---|
| .. | .. |
|---|
| 32 | 20 | #include <linux/lockdep.h> |
|---|
| 33 | 21 | #include <linux/mutex.h> |
|---|
| 34 | 22 | #include <linux/netdevice.h> |
|---|
| 23 | +#include <linux/prandom.h> |
|---|
| 35 | 24 | #include <linux/random.h> |
|---|
| 36 | 25 | #include <linux/rculist.h> |
|---|
| 37 | 26 | #include <linux/rcupdate.h> |
|---|
| 38 | 27 | #include <linux/skbuff.h> |
|---|
| 39 | 28 | #include <linux/slab.h> |
|---|
| 29 | +#include <linux/spinlock.h> |
|---|
| 40 | 30 | #include <linux/stddef.h> |
|---|
| 41 | 31 | #include <linux/string.h> |
|---|
| 42 | 32 | #include <linux/types.h> |
|---|
| .. | .. |
|---|
| 58 | 48 | * @bat_priv: the bat priv with all the soft interface information |
|---|
| 59 | 49 | * @addr: the address of the originator |
|---|
| 60 | 50 | * |
|---|
| 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. |
|---|
| 64 | 54 | */ |
|---|
| 65 | 55 | struct batadv_orig_node *batadv_v_ogm_orig_get(struct batadv_priv *bat_priv, |
|---|
| 66 | 56 | const u8 *addr) |
|---|
| .. | .. |
|---|
| 91 | 81 | } |
|---|
| 92 | 82 | |
|---|
| 93 | 83 | /** |
|---|
| 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 | +/** |
|---|
| 94 | 98 | * batadv_v_ogm_start_timer() - restart the OGM sending timer |
|---|
| 95 | 99 | * @bat_priv: the bat priv with all the soft interface information |
|---|
| 96 | 100 | */ |
|---|
| .. | .. |
|---|
| 104 | 108 | return; |
|---|
| 105 | 109 | |
|---|
| 106 | 110 | msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER; |
|---|
| 107 | | - msecs += prandom_u32() % (2 * BATADV_JITTER); |
|---|
| 111 | + msecs += prandom_u32_max(2 * BATADV_JITTER); |
|---|
| 108 | 112 | queue_delayed_work(batadv_event_workqueue, &bat_priv->bat_v.ogm_wq, |
|---|
| 109 | 113 | msecs_to_jiffies(msecs)); |
|---|
| 110 | 114 | } |
|---|
| .. | .. |
|---|
| 127 | 131 | skb->len + ETH_HLEN); |
|---|
| 128 | 132 | |
|---|
| 129 | 133 | 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); |
|---|
| 130 | 254 | } |
|---|
| 131 | 255 | |
|---|
| 132 | 256 | /** |
|---|
| .. | .. |
|---|
| 221 | 345 | break; |
|---|
| 222 | 346 | } |
|---|
| 223 | 347 | |
|---|
| 224 | | - batadv_v_ogm_send_to_if(skb_tmp, hard_iface); |
|---|
| 348 | + batadv_v_ogm_queue_on_if(skb_tmp, hard_iface); |
|---|
| 225 | 349 | batadv_hardif_put(hard_iface); |
|---|
| 226 | 350 | } |
|---|
| 227 | 351 | rcu_read_unlock(); |
|---|
| .. | .. |
|---|
| 252 | 376 | } |
|---|
| 253 | 377 | |
|---|
| 254 | 378 | /** |
|---|
| 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 | +/** |
|---|
| 255 | 400 | * batadv_v_ogm_iface_enable() - prepare an interface for B.A.T.M.A.N. V |
|---|
| 256 | 401 | * @hard_iface: the interface to prepare |
|---|
| 257 | 402 | * |
|---|
| 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. |
|---|
| 259 | 404 | * |
|---|
| 260 | 405 | * Return: 0 on success or a negative error code otherwise |
|---|
| 261 | 406 | */ |
|---|
| .. | .. |
|---|
| 263 | 408 | { |
|---|
| 264 | 409 | struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface); |
|---|
| 265 | 410 | |
|---|
| 411 | + batadv_v_ogm_start_queue_timer(hard_iface); |
|---|
| 266 | 412 | batadv_v_ogm_start_timer(bat_priv); |
|---|
| 267 | 413 | |
|---|
| 268 | 414 | 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); |
|---|
| 269 | 428 | } |
|---|
| 270 | 429 | |
|---|
| 271 | 430 | /** |
|---|
| .. | .. |
|---|
| 297 | 456 | * @throughput: the current throughput |
|---|
| 298 | 457 | * |
|---|
| 299 | 458 | * 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: |
|---|
| 302 | 463 | * - throughput * 50% if the incoming and outgoing interface are the |
|---|
| 303 | 464 | * same WiFi interface and the throughput is above |
|---|
| 304 | 465 | * 1MBit/s |
|---|
| 305 | 466 | * - throughput if the outgoing interface is the default |
|---|
| 306 | 467 | * interface (i.e. this OGM is processed for the |
|---|
| 307 | 468 | * internal table and not forwarded) |
|---|
| 308 | | - * - throughput * hop penalty otherwise |
|---|
| 469 | + * - throughput * node hop penalty otherwise |
|---|
| 309 | 470 | * |
|---|
| 310 | 471 | * Return: the penalised throughput metric. |
|---|
| 311 | 472 | */ |
|---|
| .. | .. |
|---|
| 314 | 475 | struct batadv_hard_iface *if_outgoing, |
|---|
| 315 | 476 | u32 throughput) |
|---|
| 316 | 477 | { |
|---|
| 478 | + int if_hop_penalty = atomic_read(&if_incoming->hop_penalty); |
|---|
| 317 | 479 | int hop_penalty = atomic_read(&bat_priv->hop_penalty); |
|---|
| 318 | 480 | 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; |
|---|
| 319 | 485 | |
|---|
| 320 | 486 | /* Don't apply hop penalty in default originator table. */ |
|---|
| 321 | 487 | if (if_outgoing == BATADV_IF_DEFAULT) |
|---|
| .. | .. |
|---|
| 414 | 580 | if_outgoing->net_dev->name, ntohl(ogm_forward->throughput), |
|---|
| 415 | 581 | ogm_forward->ttl, if_incoming->net_dev->name); |
|---|
| 416 | 582 | |
|---|
| 417 | | - batadv_v_ogm_send_to_if(skb, if_outgoing); |
|---|
| 583 | + batadv_v_ogm_queue_on_if(skb, if_outgoing); |
|---|
| 418 | 584 | |
|---|
| 419 | 585 | out: |
|---|
| 420 | 586 | if (orig_ifinfo) |
|---|
| .. | .. |
|---|
| 689 | 855 | * batadv_v_ogm_process() - process an incoming batman v OGM |
|---|
| 690 | 856 | * @skb: the skb containing the OGM |
|---|
| 691 | 857 | * @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 |
|---|
| 693 | 859 | */ |
|---|
| 694 | 860 | static void batadv_v_ogm_process(const struct sk_buff *skb, int ogm_offset, |
|---|
| 695 | 861 | struct batadv_hard_iface *if_incoming) |
|---|