Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 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 | |
Antonio Quartulli | 0b5ecc6 | 2016-01-16 16:40:14 +0800 | [diff] [blame] | 21 | #include <linux/atomic.h> |
Antonio Quartulli | 9786906 | 2016-01-16 16:40:17 +0800 | [diff] [blame^] | 22 | #include <linux/bug.h> |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 23 | #include <linux/cache.h> |
| 24 | #include <linux/init.h> |
Antonio Quartulli | 9786906 | 2016-01-16 16:40:17 +0800 | [diff] [blame^] | 25 | #include <linux/types.h> |
Antonio Quartulli | c833484 | 2015-11-10 18:50:51 +0100 | [diff] [blame] | 26 | #include <linux/workqueue.h> |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 27 | |
| 28 | #include "bat_v_elp.h" |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 29 | #include "bat_v_ogm.h" |
Antonio Quartulli | 9786906 | 2016-01-16 16:40:17 +0800 | [diff] [blame^] | 30 | #include "originator.h" |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 31 | #include "packet.h" |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 32 | |
| 33 | static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface) |
| 34 | { |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 35 | int ret; |
| 36 | |
| 37 | ret = batadv_v_elp_iface_enable(hard_iface); |
| 38 | if (ret < 0) |
| 39 | return ret; |
| 40 | |
| 41 | ret = batadv_v_ogm_iface_enable(hard_iface); |
| 42 | if (ret < 0) |
| 43 | batadv_v_elp_iface_disable(hard_iface); |
| 44 | |
Antonio Quartulli | 0b5ecc6 | 2016-01-16 16:40:14 +0800 | [diff] [blame] | 45 | /* enable link throughput auto-detection by setting the throughput |
| 46 | * override to zero |
| 47 | */ |
| 48 | atomic_set(&hard_iface->bat_v.throughput_override, 0); |
| 49 | |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 50 | return ret; |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface) |
| 54 | { |
| 55 | batadv_v_elp_iface_disable(hard_iface); |
| 56 | } |
| 57 | |
| 58 | static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface) |
| 63 | { |
| 64 | batadv_v_elp_primary_iface_set(hard_iface); |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 65 | batadv_v_ogm_primary_iface_set(hard_iface); |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 66 | } |
| 67 | |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 68 | static void |
| 69 | batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh) |
| 70 | { |
| 71 | ewma_throughput_init(&hardif_neigh->bat_v.throughput); |
Antonio Quartulli | c833484 | 2015-11-10 18:50:51 +0100 | [diff] [blame] | 72 | INIT_WORK(&hardif_neigh->bat_v.metric_work, |
| 73 | batadv_v_elp_throughput_metric_update); |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 74 | } |
| 75 | |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 76 | static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet) |
| 81 | { |
| 82 | } |
| 83 | |
Antonio Quartulli | 9786906 | 2016-01-16 16:40:17 +0800 | [diff] [blame^] | 84 | static int batadv_v_neigh_cmp(struct batadv_neigh_node *neigh1, |
| 85 | struct batadv_hard_iface *if_outgoing1, |
| 86 | struct batadv_neigh_node *neigh2, |
| 87 | struct batadv_hard_iface *if_outgoing2) |
| 88 | { |
| 89 | struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2; |
| 90 | |
| 91 | ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); |
| 92 | ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); |
| 93 | |
| 94 | if (WARN_ON(!ifinfo1 || !ifinfo2)) |
| 95 | return 0; |
| 96 | |
| 97 | return ifinfo1->bat_v.throughput - ifinfo2->bat_v.throughput; |
| 98 | } |
| 99 | |
| 100 | static bool batadv_v_neigh_is_sob(struct batadv_neigh_node *neigh1, |
| 101 | struct batadv_hard_iface *if_outgoing1, |
| 102 | struct batadv_neigh_node *neigh2, |
| 103 | struct batadv_hard_iface *if_outgoing2) |
| 104 | { |
| 105 | struct batadv_neigh_ifinfo *ifinfo1, *ifinfo2; |
| 106 | u32 threshold; |
| 107 | |
| 108 | ifinfo1 = batadv_neigh_ifinfo_get(neigh1, if_outgoing1); |
| 109 | ifinfo2 = batadv_neigh_ifinfo_get(neigh2, if_outgoing2); |
| 110 | |
| 111 | threshold = ifinfo1->bat_v.throughput / 4; |
| 112 | threshold = ifinfo1->bat_v.throughput - threshold; |
| 113 | |
| 114 | return ifinfo2->bat_v.throughput > threshold; |
| 115 | } |
| 116 | |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 117 | static struct batadv_algo_ops batadv_batman_v __read_mostly = { |
| 118 | .name = "BATMAN_V", |
| 119 | .bat_iface_enable = batadv_v_iface_enable, |
| 120 | .bat_iface_disable = batadv_v_iface_disable, |
| 121 | .bat_iface_update_mac = batadv_v_iface_update_mac, |
| 122 | .bat_primary_iface_set = batadv_v_primary_iface_set, |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 123 | .bat_hardif_neigh_init = batadv_v_hardif_neigh_init, |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 124 | .bat_ogm_emit = batadv_v_ogm_emit, |
| 125 | .bat_ogm_schedule = batadv_v_ogm_schedule, |
Antonio Quartulli | 9786906 | 2016-01-16 16:40:17 +0800 | [diff] [blame^] | 126 | .bat_neigh_cmp = batadv_v_neigh_cmp, |
| 127 | .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob, |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /** |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 131 | * batadv_v_mesh_init - initialize the B.A.T.M.A.N. V private resources for a |
| 132 | * mesh |
| 133 | * @bat_priv: the object representing the mesh interface to initialise |
| 134 | * |
| 135 | * Return: 0 on success or a negative error code otherwise |
| 136 | */ |
| 137 | int batadv_v_mesh_init(struct batadv_priv *bat_priv) |
| 138 | { |
| 139 | return batadv_v_ogm_init(bat_priv); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * batadv_v_mesh_free - free the B.A.T.M.A.N. V private resources for a mesh |
| 144 | * @bat_priv: the object representing the mesh interface to free |
| 145 | */ |
| 146 | void batadv_v_mesh_free(struct batadv_priv *bat_priv) |
| 147 | { |
| 148 | batadv_v_ogm_free(bat_priv); |
| 149 | } |
| 150 | |
| 151 | /** |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 152 | * batadv_v_init - B.A.T.M.A.N. V initialization function |
| 153 | * |
| 154 | * Description: Takes care of initializing all the subcomponents. |
| 155 | * It is invoked upon module load only. |
| 156 | * |
| 157 | * Return: 0 on success or a negative error code otherwise |
| 158 | */ |
| 159 | int __init batadv_v_init(void) |
| 160 | { |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 161 | int ret; |
| 162 | |
| 163 | /* B.A.T.M.A.N. V echo location protocol packet */ |
| 164 | ret = batadv_recv_handler_register(BATADV_ELP, |
| 165 | batadv_v_elp_packet_recv); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 169 | ret = batadv_recv_handler_register(BATADV_OGM2, |
| 170 | batadv_v_ogm_packet_recv); |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 171 | if (ret < 0) |
Antonio Quartulli | 0da0035 | 2016-01-16 16:40:12 +0800 | [diff] [blame] | 172 | goto elp_unregister; |
| 173 | |
| 174 | ret = batadv_algo_register(&batadv_batman_v); |
| 175 | if (ret < 0) |
| 176 | goto ogm_unregister; |
| 177 | |
| 178 | return ret; |
| 179 | |
| 180 | ogm_unregister: |
| 181 | batadv_recv_handler_unregister(BATADV_OGM2); |
| 182 | |
| 183 | elp_unregister: |
| 184 | batadv_recv_handler_unregister(BATADV_ELP); |
Linus Luessing | 162bd64 | 2016-01-16 16:40:10 +0800 | [diff] [blame] | 185 | |
| 186 | return ret; |
Linus Luessing | d6f94d9 | 2016-01-16 16:40:09 +0800 | [diff] [blame] | 187 | } |