blob: 953c0d150231ffd7a88654b35992f5b428bc05fa [file] [log] [blame]
Linus Luessingd6f94d92016-01-16 16:40:09 +08001/* 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 Quartulli0b5ecc62016-01-16 16:40:14 +080021#include <linux/atomic.h>
Antonio Quartulli97869062016-01-16 16:40:17 +080022#include <linux/bug.h>
Linus Luessingd6f94d92016-01-16 16:40:09 +080023#include <linux/cache.h>
24#include <linux/init.h>
Antonio Quartulli97869062016-01-16 16:40:17 +080025#include <linux/types.h>
Antonio Quartullic8334842015-11-10 18:50:51 +010026#include <linux/workqueue.h>
Linus Luessingd6f94d92016-01-16 16:40:09 +080027
28#include "bat_v_elp.h"
Antonio Quartulli0da00352016-01-16 16:40:12 +080029#include "bat_v_ogm.h"
Antonio Quartulli97869062016-01-16 16:40:17 +080030#include "originator.h"
Linus Luessing162bd642016-01-16 16:40:10 +080031#include "packet.h"
Linus Luessingd6f94d92016-01-16 16:40:09 +080032
33static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
34{
Antonio Quartulli0da00352016-01-16 16:40:12 +080035 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 Quartulli0b5ecc62016-01-16 16:40:14 +080045 /* 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 Quartulli0da00352016-01-16 16:40:12 +080050 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +080051}
52
53static void batadv_v_iface_disable(struct batadv_hard_iface *hard_iface)
54{
55 batadv_v_elp_iface_disable(hard_iface);
56}
57
58static void batadv_v_iface_update_mac(struct batadv_hard_iface *hard_iface)
59{
60}
61
62static void batadv_v_primary_iface_set(struct batadv_hard_iface *hard_iface)
63{
64 batadv_v_elp_primary_iface_set(hard_iface);
Antonio Quartulli0da00352016-01-16 16:40:12 +080065 batadv_v_ogm_primary_iface_set(hard_iface);
Linus Luessingd6f94d92016-01-16 16:40:09 +080066}
67
Linus Luessing162bd642016-01-16 16:40:10 +080068static void
69batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
70{
71 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
Antonio Quartullic8334842015-11-10 18:50:51 +010072 INIT_WORK(&hardif_neigh->bat_v.metric_work,
73 batadv_v_elp_throughput_metric_update);
Linus Luessing162bd642016-01-16 16:40:10 +080074}
75
Linus Luessingd6f94d92016-01-16 16:40:09 +080076static void batadv_v_ogm_schedule(struct batadv_hard_iface *hard_iface)
77{
78}
79
80static void batadv_v_ogm_emit(struct batadv_forw_packet *forw_packet)
81{
82}
83
Antonio Quartulli97869062016-01-16 16:40:17 +080084static 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
100static 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 Luessingd6f94d92016-01-16 16:40:09 +0800117static 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 Luessing162bd642016-01-16 16:40:10 +0800123 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800124 .bat_ogm_emit = batadv_v_ogm_emit,
125 .bat_ogm_schedule = batadv_v_ogm_schedule,
Antonio Quartulli97869062016-01-16 16:40:17 +0800126 .bat_neigh_cmp = batadv_v_neigh_cmp,
127 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
Linus Luessingd6f94d92016-01-16 16:40:09 +0800128};
129
130/**
Antonio Quartulli0da00352016-01-16 16:40:12 +0800131 * 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 */
137int 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 */
146void batadv_v_mesh_free(struct batadv_priv *bat_priv)
147{
148 batadv_v_ogm_free(bat_priv);
149}
150
151/**
Linus Luessingd6f94d92016-01-16 16:40:09 +0800152 * 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 */
159int __init batadv_v_init(void)
160{
Linus Luessing162bd642016-01-16 16:40:10 +0800161 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 Quartulli0da00352016-01-16 16:40:12 +0800169 ret = batadv_recv_handler_register(BATADV_OGM2,
170 batadv_v_ogm_packet_recv);
Linus Luessing162bd642016-01-16 16:40:10 +0800171 if (ret < 0)
Antonio Quartulli0da00352016-01-16 16:40:12 +0800172 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
180ogm_unregister:
181 batadv_recv_handler_unregister(BATADV_OGM2);
182
183elp_unregister:
184 batadv_recv_handler_unregister(BATADV_ELP);
Linus Luessing162bd642016-01-16 16:40:10 +0800185
186 return ret;
Linus Luessingd6f94d92016-01-16 16:40:09 +0800187}