batman-adv: B.A.T.M.A.N. V - implement neighbor comparison API calls
[deliverable/linux.git] / net / batman-adv / bat_v.c
CommitLineData
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>
97869060 25#include <linux/types.h>
c833484e 26#include <linux/workqueue.h>
d6f94d91
LL
27
28#include "bat_v_elp.h"
0da00359 29#include "bat_v_ogm.h"
97869060 30#include "originator.h"
162bd64c 31#include "packet.h"
d6f94d91
LL
32
33static int batadv_v_iface_enable(struct batadv_hard_iface *hard_iface)
34{
0da00359
AQ
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
0b5ecc68
AQ
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
0da00359 50 return ret;
d6f94d91
LL
51}
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);
0da00359 65 batadv_v_ogm_primary_iface_set(hard_iface);
d6f94d91
LL
66}
67
162bd64c
LL
68static void
69batadv_v_hardif_neigh_init(struct batadv_hardif_neigh_node *hardif_neigh)
70{
71 ewma_throughput_init(&hardif_neigh->bat_v.throughput);
c833484e
AQ
72 INIT_WORK(&hardif_neigh->bat_v.metric_work,
73 batadv_v_elp_throughput_metric_update);
162bd64c
LL
74}
75
d6f94d91
LL
76static 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
97869060
AQ
84static 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
d6f94d91
LL
117static 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,
162bd64c 123 .bat_hardif_neigh_init = batadv_v_hardif_neigh_init,
d6f94d91
LL
124 .bat_ogm_emit = batadv_v_ogm_emit,
125 .bat_ogm_schedule = batadv_v_ogm_schedule,
97869060
AQ
126 .bat_neigh_cmp = batadv_v_neigh_cmp,
127 .bat_neigh_is_similar_or_better = batadv_v_neigh_is_sob,
d6f94d91
LL
128};
129
0da00359
AQ
130/**
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 */
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
d6f94d91
LL
151/**
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 */
159int __init batadv_v_init(void)
160{
162bd64c
LL
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
0da00359
AQ
169 ret = batadv_recv_handler_register(BATADV_OGM2,
170 batadv_v_ogm_packet_recv);
171 if (ret < 0)
172 goto elp_unregister;
162bd64c 173
0da00359 174 ret = batadv_algo_register(&batadv_batman_v);
162bd64c 175 if (ret < 0)
0da00359
AQ
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);
162bd64c
LL
185
186 return ret;
d6f94d91 187}
This page took 0.042654 seconds and 5 git commands to generate.