-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcandidate.c
462 lines (363 loc) · 11.7 KB
/
candidate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2020 embedd.ch
* Copyright (C) 2020 Felix Fietkau <[email protected]>
* Copyright (C) 2020 John Crispin <[email protected]>
* Copyright (C) 2022 David Bauer <[email protected]>
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "element.h"
#include "node.h"
#include "remote.h"
#include "usteer.h"
#include "neighbor_report.h"
struct usteer_candidate_list *
usteer_candidate_list_get_empty(int max_length)
{
struct usteer_candidate_list *cl;
cl = calloc(1, sizeof(*cl));
if (!cl)
return NULL;
INIT_LIST_HEAD(&cl->candidates);
cl->max_length = max_length;
return cl;
}
void
usteer_candidate_list_free(struct usteer_candidate_list *cl)
{
struct usteer_candidate *c, *tmp;
if (!cl)
return;
list_for_each_entry_safe(c, tmp, &cl->candidates, list) {
free(c);
}
free(cl);
}
static bool
usteer_candidate_contains_node(struct usteer_candidate_list *cl, struct usteer_node *node)
{
struct usteer_candidate *c;
list_for_each_entry(c, &cl->candidates, list) {
if (c->node == node)
return true;
}
return false;
}
int
usteer_candidate_list_len(struct usteer_candidate_list *cl)
{
struct usteer_candidate *c;
int i = 0;
/* libubox does not offer this. Why? */
list_for_each_entry(c, &cl->candidates, list) {
i++;
}
return i;
}
static struct usteer_candidate *
usteer_candidate_list_get_idx(struct usteer_candidate_list *cl, int idx)
{
struct usteer_candidate *c;
int i = 0;
list_for_each_entry(c, &cl->candidates, list) {
if (i == idx)
return c;
i++;
}
return NULL;
}
static bool
usteer_candidate_list_can_insert(struct usteer_candidate_list *cl)
{
return !cl->max_length || usteer_candidate_list_len(cl) < cl->max_length;
}
static bool
usteer_candidate_list_can_insert_node(struct usteer_candidate_list *cl, struct usteer_node *node)
{
if (!usteer_candidate_list_can_insert(cl))
return false;
return !usteer_candidate_contains_node(cl, node);
}
static int
usteer_candidate_classify_load(struct usteer_node *node)
{
return (node->load / 10) * 10;
}
static bool
cl_sort_has_lower_load(struct usteer_candidate *ref, struct usteer_candidate *candidate)
{
struct usteer_node *ref_node = ref->node;
struct usteer_node *candidate_node = candidate->node;
if (candidate == NULL || candidate_node == NULL) {
return false;
} else if (ref == NULL || ref_node == NULL) {
return true;
}
int ref_load = usteer_candidate_classify_load(ref_node);
int candidate_load = usteer_candidate_classify_load(candidate_node);
return ref_load > candidate_load || (ref_load == candidate_load && candidate_node->freq > 4000 && ref_node->freq < 4000);
}
static bool
cl_sort_has_higher_priority(struct usteer_candidate *ref, struct usteer_candidate *candidate)
{
return ref->priority < candidate->priority;
}
static void
usteer_candidate_list_swap(struct list_head *elem1, struct list_head *elem2)
{
struct list_head *elem1_prev = elem1->prev;
struct list_head *elem1_next = elem1->next;
struct list_head *elem2_prev = elem2->prev;
struct list_head *elem2_next = elem2->next;
if ((elem1_next == elem2 && elem2_prev ==elem1) || (elem2_next == elem1 && elem1_prev == elem2)) {
/* Adjacent */
elem1->prev = elem1_next;
elem1->next = elem2_next;
elem2->prev = elem1_prev;
elem2->next = elem2_prev;
} else {
elem1->prev = elem2_prev;
elem1->next = elem2_next;
elem2->prev = elem1_prev;
elem2->next = elem1_next;
}
/* Update adjacent heads */
elem1->prev->next = elem1;
elem1->next->prev = elem1;
elem2->prev->next = elem2;
elem2->next->prev = elem2;
}
static void
usteer_candidate_list_sort(struct usteer_candidate_list *cl, bool (*sort_fun)(struct usteer_candidate *, struct usteer_candidate *))
{
struct usteer_candidate *ref, *candidate;
int cl_len = usteer_candidate_list_len(cl);
int i, j;
for (i = 0; i < cl_len - 1; i++) {
for (j = 0; j < cl_len - i - 1; j++) {
ref = usteer_candidate_list_get_idx(cl, j);
candidate = usteer_candidate_list_get_idx(cl, j + 1);
if (sort_fun(ref, candidate)) {
usteer_candidate_list_swap(&ref->list, &candidate->list);
}
}
}
}
#define NR_MAX_PREFERENCE 255
#define NR_MIN_PREFERENCE 0
static void
usteer_candidate_list_add_load_preference(struct usteer_candidate_list *cl,
struct usteer_node *node_ref,
enum usteer_reference_node_rating node_ref_pref)
{
struct usteer_candidate *c;
uint8_t pref = NR_MAX_PREFERENCE;
int last_load = -1;
if (node_ref_pref == RN_RATING_PREFER)
pref--;
for_each_candidate(cl, c) {
if (last_load > -1 && last_load < c->node->load)
pref--;
c->priority = pref;
last_load = c->node->load;
if (c->node == node_ref) {
if (node_ref_pref == RN_RATING_PREFER) {
c->priority = NR_MAX_PREFERENCE;
continue;
} else if (node_ref_pref == RN_RATING_FORBID) {
c->priority = NR_MIN_PREFERENCE;
continue;
}
}
}
}
static bool
usteer_candidate_list_add(struct usteer_candidate_list *cl, struct usteer_candidate *c)
{
if (!usteer_candidate_list_can_insert_node(cl, c->node))
return false;
list_add_tail(&c->list, &cl->candidates);
return true;
}
static bool
usteer_candidate_list_add_node(struct usteer_candidate_list *cl, struct usteer_node *n, int signal, uint32_t reasons)
{
struct usteer_candidate *c;
if (!usteer_candidate_list_can_insert_node(cl, n))
return false;
c = calloc(1, sizeof(*c));
if (!c)
return false;
c->node = n;
c->signal = signal;
c->reasons = reasons;
usteer_candidate_list_add(cl, c);
return true;
}
static bool
usteer_candidate_list_add_better_node(struct usteer_candidate_list *cl, struct usteer_node *n, int signal, uint32_t reasons)
{
struct usteer_candidate *c, *worst_candidate = NULL;
/* Check if node is already in candidate list */
if (usteer_candidate_contains_node(cl, n))
return false;
/* Check if max candidate-list size is not exceeded */
if (usteer_candidate_list_add_node(cl, n, signal, reasons))
return true;
/* Find the candidate with the worst signal */
for_each_candidate(cl, c) {
if (!worst_candidate || c->signal < worst_candidate->signal)
worst_candidate = c;
}
if (!worst_candidate || worst_candidate->signal >= signal)
return false;
/* Delete worst candidate from list */
list_del(&worst_candidate->list);
free(worst_candidate);
/* Add candidate to list */
return usteer_candidate_list_add_node(cl, n, signal, reasons);
}
static int
usteer_candidate_list_add_local_nodes(struct usteer_candidate_list *cl, struct usteer_node *node_ref,
enum usteer_reference_node_rating node_ref_pref)
{
struct usteer_node *node;
int inserted = 0;
for_each_local_node(node) {
if (node_ref == node && node_ref_pref == RN_RATING_EXCLUDE) {
continue;
}
if (strcmp(node->ssid, node_ref->ssid)) {
continue;
}
if (usteer_candidate_list_add_node(cl, node, 0, 0)) {
inserted++;
}
}
return inserted;
}
static int
usteer_candidate_list_add_remote_nodes(struct usteer_candidate_list *cl, struct usteer_node *node_ref)
{
struct usteer_node *node, *last_remote_neighbor = NULL;;
int inserted = 0;
while (inserted < usteer_candidate_list_len(cl)) {
node = usteer_node_get_next_neighbor(node_ref, last_remote_neighbor);
if (!node) {
/* No more nodes available */
break;
}
if (usteer_candidate_list_add_node(cl, node, 0, 0)) {
inserted++;
}
last_remote_neighbor = node;
}
return inserted;
}
int
usteer_candidate_list_add_for_node(struct usteer_candidate_list *cl, struct usteer_node *node_ref,
enum usteer_reference_node_rating node_ref_rating)
{
int inserted = 0;
/* Add local nodes */
inserted += usteer_candidate_list_add_local_nodes(cl, node_ref, node_ref_rating);
/* Add remote nodes */
inserted += usteer_candidate_list_add_remote_nodes(cl, node_ref);
/* Sort list order based on load */
usteer_candidate_list_sort(cl, &cl_sort_has_lower_load);
/* Add preferences */
usteer_candidate_list_add_load_preference(cl, node_ref, node_ref_rating);
/* Sort by preference */
usteer_candidate_list_sort(cl, &cl_sort_has_higher_priority);
return inserted;
}
static uint32_t
usteer_candidate_list_should_add_node(struct usteer_node *current_node, int current_signal,
struct usteer_node *new_node, int new_signal,
enum usteer_reference_node_rating node_ref_rating,
uint32_t required_criteria)
{
uint32_t reasons;
if (node_ref_rating == RN_RATING_EXCLUDE && current_node == new_node)
return 0;
reasons = usteer_policy_is_better_candidate(current_node, current_signal,
new_node, new_signal);
if (!reasons || (required_criteria && !(reasons & required_criteria)))
return 0;
return reasons;
}
static void
usteer_candidate_list_add_sta_reported(struct usteer_candidate_list *cl, struct sta_info *si,
enum usteer_reference_node_rating node_ref_rating,
uint32_t required_criteria, uint64_t signal_max_age)
{
struct usteer_measurement_report *mr, *own_mr;
int current_signal;
uint32_t reasons;
int signal;
/* Check if we have a measurement of the current connection, */
own_mr = usteer_measurement_report_get(si->sta, si->node, false);
if (!own_mr)
return;
current_signal = usteer_rcpi_to_rssi(own_mr->beacon_report.rcpi);
list_for_each_entry(mr, &si->sta->measurements, sta_list) {
if (!usteer_policy_node_selectable_by_sta_measurement(own_mr, mr, signal_max_age))
continue;
signal = usteer_rcpi_to_rssi(mr->beacon_report.rcpi);
reasons = usteer_candidate_list_should_add_node(si->node, current_signal, mr->node,
signal,
node_ref_rating, required_criteria);
if (!reasons)
continue;
usteer_candidate_list_add_better_node(cl, mr->node, signal, reasons);
}
}
static void
usteer_candidate_list_add_sta_seen(struct usteer_candidate_list *cl, struct sta_info *si,
enum usteer_reference_node_rating node_ref_rating,
uint32_t required_criteria, uint64_t signal_max_age)
{
struct sta_info *foreign_si;
uint32_t reasons;
list_for_each_entry(foreign_si, &si->sta->nodes, list) {
if (!usteer_policy_node_selectable_by_sta(si, foreign_si, signal_max_age))
continue;
reasons = usteer_candidate_list_should_add_node(si->node, si->signal, foreign_si->node,
foreign_si->signal,
node_ref_rating, required_criteria);
if (!reasons)
continue;
usteer_candidate_list_add_better_node(cl, foreign_si->node, foreign_si->signal, reasons);
}
}
int
usteer_candidate_list_add_for_sta(struct usteer_candidate_list *cl, struct sta_info *si,
enum usteer_reference_node_rating node_ref_rating,
uint32_t required_criteria, uint64_t signal_max_age)
{
/* First add all nodes which the STA has reported measurements of */
usteer_candidate_list_add_sta_reported(cl, si, node_ref_rating, required_criteria, signal_max_age);
/* Add all nodes we have seen the STA on */
usteer_candidate_list_add_sta_seen(cl, si, node_ref_rating, required_criteria, signal_max_age);
/* Sort list order based on load */
usteer_candidate_list_sort(cl, &cl_sort_has_lower_load);
/* Add preferences */
usteer_candidate_list_add_load_preference(cl, si->node, node_ref_rating);
/* Sort by preference */
usteer_candidate_list_sort(cl, &cl_sort_has_higher_priority);
return 0;
}