Newer
Older
/*
* cntlr_acs.c - Auto Channel Selection
*
* Copyright (C) 2021 IOPSYS Software Solutions AB. All rights reserved.
*
*/
#include <easymesh.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <easy/easy.h>
#include <libubox/list.h>
#include "wifi_dataelements.h"
#include "wifi_opclass.h"
/* Per radio ACS recalc timer */
static void cntlr_radio_acs_timer(atimer_t *t);
/* Opclass helpers - move it to other place */
int cntlr_radio_pref_opclass_reset(struct wifi_radio_element *radio)
{
/*
* Build initial preferred opclasses from supported opclasses
* we receive in basic radio capabilities.
*/
memcpy(&radio->pref_opclass, &radio->supp_opclass, sizeof(radio->pref_opclass));
wifi_opclass_set_preferences(&radio->pref_opclass, 15 << 4);
timestamp_update(&radio->pref_opclass.entry_time);
enum wifi_radio_opclass_dfs dfs_state_from_preference(uint8_t preference)
{
uint8_t reas = preference & CHANNEL_PREF_REASON;
switch(reas) {
case CHANNEL_PREF_REASON_DFS_USABLE:
return WIFI_RADIO_OPCLASS_CHANNEL_DFS_USABLE;
case CHANNEL_PREF_REASON_DFS_AVAILABLE:
return WIFI_RADIO_OPCLASS_CHANNEL_DFS_AVAILABLE;
case CHANNEL_PREF_REASON_DFS_NOP:
return WIFI_RADIO_OPCLASS_CHANNEL_DFS_NOP;
default:
break;
}
return WIFI_RADIO_OPCLASS_CHANNEL_DFS_NONE;
}
int cntlr_radio_pref_opclass_add(struct wifi_radio_element *radio, uint8_t classid,
uint8_t channel, uint8_t preference)
{
struct wifi_radio_opclass_entry *entry;
struct wifi_radio_opclass_channel *chan;
struct wifi_radio_opclass_channel new_chan = {};
struct wifi_radio_opclass *opclass;
opclass = &radio->pref_opclass;
entry = wifi_opclass_find_entry(opclass, classid);
if (!entry)
return -1;
entry->id = classid;
entry->bandwidth = wifi_opclass_get_bw(classid);
new_chan.channel = channel;
new_chan.preference = preference;
new_chan.dfs = dfs_state_from_preference(preference);
timestamp_update(&opclass->entry_time);
/* Don't clean cac_methods/cac_time */
chan = wifi_opclass_find_channel(entry, channel);
if (chan) {
chan->channel = channel;
chan->preference = preference;
chan->dfs = dfs_state_from_preference(preference);
return 0;
}
return wifi_opclass_add_channel(entry, &new_chan);
}
int cntlr_radio_pref_opclass_set_dfs_status(struct wifi_radio_element *radio, uint8_t classid,
uint8_t channel, enum wifi_radio_opclass_dfs state)
{
struct wifi_radio_opclass_channel *chan;
struct wifi_radio_opclass *opclass;
opclass = &radio->pref_opclass;
chan = wifi_opclass_get_channel(opclass, classid, channel);
if (!chan)
return -1;
chan->dfs = state;
return 0;
}
void cntlr_radio_pref_opclass_dump(struct wifi_radio_element *radio)
{
wifi_opclass_dump(&radio->pref_opclass, "dev_pref_opclass", radio->macaddr);
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
}
void cntlr_radio_cur_opclass_reset(struct wifi_radio_element *radio)
{
wifi_opclass_reset(&radio->cur_opclass);
}
int cntlr_radio_cur_opclass_add(struct wifi_radio_element *radio, uint8_t classid,
uint8_t channel, uint8_t txpower)
{
struct wifi_radio_opclass_entry *entry;
struct wifi_radio_opclass_channel chan = {};
entry = wifi_opclass_find_entry(&radio->cur_opclass, classid);
if (!entry)
entry = wifi_opclass_new_entry(&radio->cur_opclass);
if (!entry)
return -1;
entry->id = classid;
entry->bandwidth = wifi_opclass_get_bw(classid);
entry->max_txpower = txpower;
chan.channel = channel;
chan.preference = 15 << 4;
timestamp_update(&radio->cur_opclass.entry_time);
return wifi_opclass_add_channel(entry, &chan);
}
uint8_t ctrl_radio_cur_opclass_id(struct wifi_radio_element *radio)
{
uint8_t id = 0;
int ret;
ret = wifi_opclass_get_attr(&radio->cur_opclass, NULL, NULL, &id, NULL);
if (ret)
return 0;
return id;
}
uint8_t ctrl_radio_cur_opclass_ctrl_chan(struct wifi_radio_element *radio)
{
uint8_t ctrl_chan = 0;
int ret;
ret = wifi_opclass_get_attr(&radio->cur_opclass, &ctrl_chan, NULL, NULL, NULL);
if (ret)
return 0;
return ctrl_chan;
}
uint8_t ctrl_radio_cur_opclass_chan(struct wifi_radio_element *radio)
{
uint8_t chan = 0;
int ret;
ret = wifi_opclass_get_attr(&radio->cur_opclass, NULL, NULL, NULL, &chan);
if (ret)
return 0;
return chan;
}
uint16_t ctrl_radio_cur_opclass_max_bw(struct wifi_radio_element *radio)
{
uint32_t bw = 0;
int ret;
ret = wifi_opclass_get_attr(&radio->cur_opclass, NULL, &bw, NULL, NULL);
if (ret)
return 0;
return bw;
}
void cntlr_radio_pref_opclass_set_pref(struct wifi_radio_element *radio, uint8_t id, uint8_t preference)
{
wifi_opclass_id_set_preferences(&radio->pref_opclass, id, preference);
}
void cntlr_radio_cur_opclass_dump(struct wifi_radio_element *radio)
{
wifi_opclass_dump(&radio->cur_opclass, "dev_cur_opclass", radio->macaddr);
static bool cntlr_radio_pref_opclass_expired(struct wifi_radio_element *radio, int age)
return wifi_opclass_expired(&radio->pref_opclass, age);
bool cntlr_node_pref_opclass_expired(struct node *node, int age)
{
struct netif_radio *r = NULL;
bool expired = false;
list_for_each_entry(r, &node->radiolist, list) {
expired |= cntlr_radio_pref_opclass_expired(r->radio_el, age);
}
return expired;
}
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
enum acs_cleanup_status {
ACS_CLEANUP_STATUS_SKIPPED,
ACS_CLEANUP_STATUS_SELECTED,
ACS_CLEANUP_STATUS_REQUESTED,
ACS_CLEANUP_STATUS_REJECTED,
ACS_CLEANUP_STATUS_RADAR,
ACS_CLEANUP_STATUS_ACCEPTED,
ACS_CLEANUP_STATUS_DONE,
ACS_CLEANUP_STATUS_ALL_CLEAN,
ACS_CLEANUP_STATUS_NOT_SUPPORTED,
ACS_CLEANUP_STATUS_NO_APS,
ACS_CLEANUP_STATUS_ALREADY_ONGOING,
ACS_CLEANUP_STATUS_TIMEOUT,
};
enum acs_recalc_status {
ACS_RECALC_STATUS_SKIPPED,
ACS_RECALC_STATUS_SCAN_REQUESTED,
ACS_RECALC_STATUS_SCAN_DONE,
ACS_RECALC_STATUS_BEST_SELECTED,
ACS_RECALC_STATUS_BEST_REQUESTED,
ACS_RECALC_STATUS_BEST_REJECTED,
ACS_RECALC_STATUS_BEST_ACCEPTED,
ACS_RECALC_STATUS_BEST_SET,
ACS_RECALC_STATUS_BEST_ALREADY_SET,
ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT,
ACS_RECALC_STATUS_BSTA_CONNECTED,
ACS_RECALC_STATUS_INVALID_DATA,
ACS_RECALC_STATUS_BEST_SET_TIMEOUT,
ACS_RECALC_STATUS_BEST_CAC,
ACS_RECALC_STATUS_BEST_CAC_RADAR,
};
struct acs_params {
struct timespec entry_time;
/* Input params */
int opclass; /* recalc for this opclass */
int bw; /* recalc for this bandwidth */
bool skip_dfs; /* Skip all DFS channels */
bool skip_dfs_not_available; /* Prevent CAC */
bool higest_bandwidth; /* Use highest possible bandwidth */
/* Output params - for channel selection request */
int best_channel;
int best_opclass;
int best_bw;
int best_pref;
uint32_t best_cac_time;
enum acs_recalc_status status; /* current recalc state */
uint16_t scan_mid; /* scan request mid */
uint16_t mid; /* channel selection mid */
bool recalc; /* run new recalc */
};
struct acs_cac_data {
struct cac_data data; /* data for CAC CMDU */
struct timespec entry_time;
enum acs_cleanup_status status; /* current CAC state */
uint16_t mid; /* CAC request mid */
uint32_t cac_time; /* CAC time for current request */
};
struct acs_radio_metrics_entry {
struct timespec time;
uint8_t anpi; /* Noise level */
uint8_t obss; /* Other BSS busy factor */
};
struct acs_radio_metrics {
struct timespec time;
uint8_t anpi; /* EWMA value */
uint8_t obss; /* EWMA value */
/* Ring buffer for more data */
uint8_t idx;
struct acs_radio_metrics_entry entry[16];
};
/* acs per radio structure */
struct acs {
struct acs_params last_acs;
struct acs_cac_data last_cac_data;
struct acs_radio_metrics radio_metrics;
struct controller *cntlr;
struct node *node;
struct netif_radio *radio;
atimer_t acs_timer;
};
/* Called when conroller alloc radio structure */
void *cntlr_radio_acs_alloc(struct controller *c, struct node *n, struct netif_radio *r)
{
struct acs *acs;
acs = calloc(1, sizeof(struct acs));
if (!acs)
return NULL;
acs->cntlr = c;
acs->node = n;
acs->radio = r;
timer_init(&acs->acs_timer, cntlr_radio_acs_timer);
return acs;
}
/* Called before controller destroy radio structure */
void cntlr_radio_acs_free(void *acs)
{
struct acs *ptr = acs;
if (!ptr)
return;
timer_del(&ptr->acs_timer);
ptr->cntlr = NULL;
ptr->node = NULL;
ptr->radio = NULL;
/* Do required cleanup here before free memory */
free(ptr);
}
/* acs/cac/metrics - helpers */
static struct acs_params *cntlr_radio_get_last_acs(struct netif_radio *r)
{
struct acs *acs;
acs = r->radio_el->acs;
if (!acs)
return NULL;
return &acs->last_acs;
}
static struct acs_cac_data *cntlr_radio_get_last_cac(struct netif_radio *r)
{
struct acs *acs;
acs = r->radio_el->acs;
if (!acs)
return NULL;
return &acs->last_cac_data;
}
static struct acs_radio_metrics *cntlr_radio_get_metrics(struct netif_radio *r)
{
struct acs *acs;
acs = r->radio_el->acs;
if (!acs)
return NULL;
return &acs->radio_metrics;
}
static int cntlr_radio_acs_timer_set(struct netif_radio *r, uint32_t tmo_ms)
{
struct acs *acs;
acs = r->radio_el->acs;
if (!acs)
return -1;
return timer_set(&acs->acs_timer, tmo_ms);
}
/* JSON status print - for UBUS radio status and trigger_acs/trigger_channel_clearing */
static char *cntlr_acs_radio_status(enum acs_recalc_status status)
{
switch (status) {
case ACS_RECALC_STATUS_SKIPPED:
return "skipped";
case ACS_RECALC_STATUS_SCAN_REQUESTED:
return "scan requested";
case ACS_RECALC_STATUS_SCAN_DONE:
return "scan done";
case ACS_RECALC_STATUS_BEST_SELECTED:
return "best_selected";
case ACS_RECALC_STATUS_BEST_REQUESTED:
return "best_requested";
case ACS_RECALC_STATUS_BEST_ACCEPTED:
return "best_accepted";
case ACS_RECALC_STATUS_BEST_REJECTED:
return "best_rejected";
case ACS_RECALC_STATUS_BEST_SET:
return "best_set";
case ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT:
return "insufficient_data";
case ACS_RECALC_STATUS_BEST_ALREADY_SET:
return "current_best";
case ACS_RECALC_STATUS_BSTA_CONNECTED:
return "skipped - bsta connected";
case ACS_RECALC_STATUS_INVALID_DATA:
return "skipped - invalid data";
case ACS_RECALC_STATUS_BEST_SET_TIMEOUT:
return "best_set timeout";
case ACS_RECALC_STATUS_BEST_CAC:
return "best_set CAC ongoing";
case ACS_RECALC_STATUS_BEST_CAC_RADAR:
return "best_set CAC RADAR hit";
default:
break;
}
return "recalc_unknown";
}
void cntlr_acs_radio_info(struct blob_buf *bb, struct netif_radio *r)
{
struct acs_params *acs;
void *t, *a;
acs = cntlr_radio_get_last_acs(r);
if (!acs)
return;
blobmsg_add_u32(bb, "acs_request_age", (uint32_t) timestamp_elapsed_sec(&acs->entry_time));
a = blobmsg_open_array(bb, "acs_request");
t = blobmsg_open_table(bb, "");
blobmsg_add_string(bb, "status", cntlr_acs_radio_status(acs->status));
blobmsg_add_u32(bb, "channel", acs->best_channel);
blobmsg_add_u32(bb, "bandwidth", acs->best_bw);
if (acs->status == ACS_RECALC_STATUS_BEST_REQUESTED)
blobmsg_add_u32(bb, "mid", acs->mid);
blobmsg_close_table(bb, t);
blobmsg_close_array(bb, a);
}
void cntlr_acs_node_info(struct node *node, enum wifi_band band, struct blob_buf *bb)
{
struct netif_radio *r = NULL;
void *t;
list_for_each_entry(r, &node->radiolist, list) {
struct acs_params *acs;
acs = cntlr_radio_get_last_acs(r);
if (!acs)
continue;
if (band && band != BAND_ANY && band != r->radio_el->band)
continue;
if (acs->status == ACS_RECALC_STATUS_SKIPPED)
continue;
t = blobmsg_open_table(bb, "");
blobmsg_add_macaddr(bb, "agent", node->almacaddr);
blobmsg_add_macaddr(bb, "radio", r->radio_el->macaddr);
blobmsg_add_string(bb, "status", cntlr_acs_radio_status(acs->status));
blobmsg_add_u32(bb, "channel", acs->best_channel);
blobmsg_add_u32(bb, "bandwidth", acs->best_bw);
if (acs->status == ACS_RECALC_STATUS_BEST_REQUESTED)
blobmsg_add_u32(bb, "mid", acs->mid);
blobmsg_close_table(bb, t);
}
}
static char *cntlr_acs_radio_cleanup_status(enum acs_cleanup_status status)
{
switch (status) {
case ACS_CLEANUP_STATUS_SKIPPED:
return "none";
case ACS_CLEANUP_STATUS_SELECTED:
return "selected";
case ACS_CLEANUP_STATUS_REQUESTED:
return "requested";
case ACS_CLEANUP_STATUS_REJECTED:
return "rejected";
case ACS_CLEANUP_STATUS_RADAR:
return "radar";
case ACS_CLEANUP_STATUS_ACCEPTED:
return "accepted";
case ACS_CLEANUP_STATUS_DONE:
return "done";
case ACS_CLEANUP_STATUS_ALL_CLEAN:
return "all clean";
case ACS_CLEANUP_STATUS_NOT_SUPPORTED:
return "not supported";
case ACS_CLEANUP_STATUS_NO_APS:
return "no APs iface";
case ACS_CLEANUP_STATUS_ALREADY_ONGOING:
return "CAC already ongoing";
case ACS_CLEANUP_STATUS_TIMEOUT:
return "timeout";
default:
break;
}
return "unknown";
}
void cntlr_dfs_radio_cleanup_info(struct node *n, struct netif_radio *r, struct blob_buf *bb)
{
struct acs_cac_data *cac;
void *t;
cac = cntlr_radio_get_last_cac(r);
if (!cac)
return;
t = blobmsg_open_table(bb, "");
blobmsg_add_macaddr(bb, "agent", n->almacaddr);
blobmsg_add_macaddr(bb, "radio", r->radio_el->macaddr);
blobmsg_add_string(bb, "status", cntlr_acs_radio_cleanup_status(cac->status));
blobmsg_add_u32(bb, "channel", cac->data.channel);
blobmsg_add_u32(bb, "opclass", cac->data.opclass);
if (cac->status == ACS_CLEANUP_STATUS_REQUESTED)
blobmsg_add_u32(bb, "mid", cac->mid);
blobmsg_close_table(bb, t);
}
void cntlr_acs_radio_cleanup_info(struct blob_buf *bb, struct netif_radio *r)
{
struct acs_cac_data *cac;
void *t, *a;
cac = cntlr_radio_get_last_cac(r);
if (!cac)
return;
blobmsg_add_u32(bb, "cleanup_request_age", (uint32_t) timestamp_elapsed_sec(&cac->entry_time));
a = blobmsg_open_array(bb, "cleanup_request");
t = blobmsg_open_table(bb, "");
blobmsg_add_string(bb, "status", cntlr_acs_radio_cleanup_status(cac->status));
blobmsg_add_u32(bb, "channel", cac->data.channel);
blobmsg_add_u32(bb, "opclass", cac->data.opclass);
if (cac->status == ACS_CLEANUP_STATUS_REQUESTED)
blobmsg_add_u32(bb, "mid", cac->mid);
blobmsg_close_table(bb, t);
blobmsg_close_array(bb, a);
}
/* CMDU ACS builders - for scan request / preference request */
static uint16_t cntlr_acs_send_scan_request(struct node *n, struct netif_radio *r)
{
struct wifi_radio_opclass_entry *entry;
struct wifi_radio_opclass *opclass;
struct scan_req_data req = {};
struct cmdu_buff *cmdu;
uint16_t mid = 0xffff;
int num = 0;
int i;
cntlr_dbg(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " - scan request\n",
MAC2STR(n->almacaddr), MAC2STR(r->radio_el->macaddr));
opclass = &r->radio_el->pref_opclass;
if (!opclass->num_opclass)
goto out;
/* Build proper scan request - all 20MHz channels */
req.is_fresh_scan = true;
req.num_radio = 1;
memcpy(req.radios[0].radio_mac, r->radio_el->macaddr, 6);
for (i = 0; i < opclass->num_opclass; i++) {
entry = &opclass->opclass[i];
if (entry->bandwidth != 20)
continue;
if (entry->num_channel == 0)
continue;
if (num >= ARRAY_SIZE(req.radios[0].opclasses))
break;
req.radios[0].opclasses[num].classid = entry->id;
num++;
}
req.radios[0].num_opclass = num;
cmdu = cntlr_gen_channel_scan_request(n->cntlr, n->almacaddr, &req);
if (!cmdu)
goto out;
mid = send_cmdu(n->cntlr, cmdu);
cmdu_free(cmdu);
out:
cntlr_dbg(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " - scan request mid %u\n",
MAC2STR(n->almacaddr), MAC2STR(r->radio_el->macaddr), mid);
return mid;
}
static uint16_t cntlr_acs_send_pref_request(struct node *n)
{
struct cmdu_buff *cmdu;
uint16_t mid = 0xffff;
cntlr_dbg(LOG_CHANNEL, "acs node send pref request " MACFMT "\n", MAC2STR(n->almacaddr));
cmdu = cntlr_gen_channel_preference_query(n->cntlr, n->almacaddr);
if (!cmdu)
return mid;
mid = send_cmdu(n->cntlr, cmdu);
cmdu_free(cmdu);
return mid;
}
/* ACS recalc code */
uint8_t cntrl_acs_radio_ctrl_channel(struct wifi_radio_element *radio, uint8_t opclass, uint8_t channel)
{
struct wifi_radio_opclass_channel *chan;
chan = wifi_opclass_get_channel(&radio->pref_opclass, opclass, channel);
if (!chan)
return 0;
return wifi_get_best_ctrl_channel(&radio->pref_opclass,
chan->ctrl_channels,
ARRAY_SIZE(chan->ctrl_channels));
}
static bool cntlr_acs_radio_is_bsta_connected(struct controller *cntlr, struct netif_radio *radio)
{
struct netif_iface *iface = NULL;
list_for_each_entry(iface, &radio->iflist, list) {
struct node *n = NULL;
/* Check if sta iface */
if (iface->bss->is_bbss || iface->bss->is_fbss)
continue;
/* Check all nodes */
list_for_each_entry(n, &cntlr->nodelist, list) {
struct sta *s = NULL;
if (!n->sta_count)
continue;
list_for_each_entry(s, &n->stalist, list) {
if (!s->de_sta)
continue;
/* skip other band */
if (memcmp(iface->bss->bssid, s->de_sta->macaddr, sizeof(iface->bss->bssid)))
continue;
if (s->is_bsta && s->state == STA_CONNECTED)
return true;
}
}
}
return false;
}
static void cntrl_acs_radio_common_opclass(struct controller *cntlr, struct netif_radio *rd,
struct wifi_radio_opclass *opclass)
{
memcpy(opclass, &rd->radio_el->pref_opclass, sizeof(*opclass));
/*
* For future use just in case nodes using different supported
* channels. Prevent switch to channel/opclass not supported
* by leaf(s).
* We could skip this step when ethernet backhaul used.
*/
}
int cntlr_acs_radio_channel_recalc(struct node *node, struct netif_radio *rd, struct acs_params *params)
struct wifi_radio_element *radio = rd->radio_el;
struct wifi_radio_opclass common_opclass = {};
struct acs_params acs_params[64] = {};
struct wifi_radio_opclass_entry *entry;
struct wifi_radio_opclass *opclass;
struct acs_params *last_acs;
int acs_params_num = 0;
int prefered[64] = {0};
int i, j, r;
cntlr_trace(LOG_CHANNEL, "acs radio channel recalc " MACFMT " opclass %d bw %d skip_dfs %d\n",
MAC2STR(radio->macaddr),
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
last_acs = cntlr_radio_get_last_acs(rd);
if (!last_acs)
return -1;
memset(last_acs, 0, sizeof(*last_acs));
last_acs->scan_mid = 0xffff;
if (!radio->pref_opclass.num_opclass) {
/* We don't know node preferences yet */
cntlr_dbg(LOG_CHANNEL, "acs radio channel recalc " MACFMT " - no pref opclass - skip recalc\n",
MAC2STR(radio->macaddr));
params->status = ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT;
goto out;
}
if (cntlr_acs_radio_is_bsta_connected(node->cntlr, rd)) {
/*
* Node is leaf with active wifi backhaul. In such leaf should
* simple follow parent node. Skip channel switch request.
*/
cntlr_dbg(LOG_CHANNEL, "acs radio " MACFMT " - skip switch - bsta connected\n",
MAC2STR(radio->macaddr));
params->status = ACS_RECALC_STATUS_BSTA_CONNECTED;
goto out;
}
cntrl_acs_radio_common_opclass(node->cntlr, rd, &common_opclass);
opclass = &common_opclass;
for (i = 0; i < opclass->num_opclass; i++) {
entry = &opclass->opclass[i];
if (params->opclass && params->opclass != entry->id)
if (params->bw && params->bw != entry->bandwidth)
for (j = 0; j < entry->num_channel; j++) {
chan = entry->channel[j].channel;
pref = (entry->channel[j].preference & CHANNEL_PREF_MASK) >> 4;
reas = entry->channel[j].preference & CHANNEL_PREF_REASON;
cntlr_trace(LOG_CHANNEL, "\tacs check/cmp chan %d pref %d reas %d\n", chan, pref, reas);
/* Always skip disabled channels */
if (reas == CHANNEL_PREF_REASON_DFS_NOP)
continue;
if (reas == CHANNEL_PREF_REASON_REG_DISALLOWED)
continue;
/* Current channel preference */
if (chan == params->best_channel)
pref_cur = pref;
/* Skip DFS channels if requested */
if (params->skip_dfs) {
if (reas == CHANNEL_PREF_REASON_DFS_AVAILABLE ||
reas == CHANNEL_PREF_REASON_DFS_USABLE)
continue;
}
/* Skip non available DFS channels if requested */
if (params->skip_dfs_not_available && reas == CHANNEL_PREF_REASON_DFS_USABLE)
continue;
/* If background CAC supported and enabled - wait background CAC complete */
if (radio->bgcac_supported &&
reas == CHANNEL_PREF_REASON_DFS_USABLE &&
node->cntlr->cfg.dfs_cleanup)
if (WARN_ON(acs_params_num >= ARRAY_SIZE(acs_params)))
break;
/* Kick best value */
if (pref > pref_best)
acs_params[acs_params_num].best_channel = chan;
acs_params[acs_params_num].best_opclass = entry->id;
acs_params[acs_params_num].best_bw = entry->bandwidth;
acs_params[acs_params_num].best_pref = pref;
acs_params[acs_params_num].best_cac_time = cac_time;
if (!pref_best) {
cntlr_dbg(LOG_CHANNEL, "acs radio channel recalc " MACFMT " - no pref best - skip recalc\n",
MAC2STR(radio->macaddr));
params->status = ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT;
goto out;
}
cntlr_trace(LOG_CHANNEL, "acs radio " MACFMT " best pref %d vs current pref %d\n",
MAC2STR(radio->macaddr), pref_best, pref_cur);
/* If current channel equal to best don't switch */
if (pref_cur == pref_best) {
cntlr_warn(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " current channel %d is the best\n",
MAC2STR(node->almacaddr), MAC2STR(radio->macaddr), params->best_channel);
params->status = ACS_RECALC_STATUS_BEST_ALREADY_SET;
goto out;
}
/* Get random channel from best performance */
for (i = 0, j = 0; i < acs_params_num; i++) {
if (acs_params[i].best_pref != pref_best)
continue;
if (j >= ARRAY_SIZE(prefered) - 1)
break;
/* Save index in table */
prefered[j] = i;
j++;
}
if (WARN_ON(!j)) {
params->status = ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT;
goto out;
}
srandom(time(NULL));
r = random() % j;
cntlr_trace(LOG_CHANNEL, "acs radio " MACFMT " table size %d - rand %d, index %d\n",
MAC2STR(radio->macaddr), j, r, prefered[r]);
if (prefered[r] >= acs_params_num) {
params->status = ACS_RECALC_STATUS_INPUT_DATA_INSUFFICIENT;
goto out;
}
params->best_channel = acs_params[prefered[r]].best_channel;
params->best_bw = acs_params[prefered[r]].best_bw;
params->best_opclass = acs_params[prefered[r]].best_opclass;
params->best_cac_time = acs_params[prefered[r]].best_cac_time;
params->best_pref = acs_params[prefered[r]].best_pref;
params->status = ACS_RECALC_STATUS_BEST_SELECTED;
cntlr_dbg(LOG_CHANNEL, "acs radio " MACFMT " best chan %d/%d opclass %d\n",
MAC2STR(radio->macaddr),
params->best_channel, params->best_bw, params->best_opclass);
out:
memcpy(last_acs, params, sizeof(*last_acs));
timestamp_update(&last_acs->entry_time);
return params->status != ACS_RECALC_STATUS_BEST_SELECTED;
static int cntlr_get_current_acs_params(struct wifi_radio_element *radio, struct acs_params *params)
if (!radio->cur_opclass.num_opclass)
params->opclass = ctrl_radio_cur_opclass_id(radio);
params->bw = ctrl_radio_cur_opclass_max_bw(radio);
params->best_channel = ctrl_radio_cur_opclass_chan(radio);
params->best_bw = params->bw;
params->best_opclass = params->opclass;
return 0;
}
void cntlr_acs_node_channel_recalc(struct node *node, enum wifi_band band, uint8_t opclass,
uint32_t bandwidth, bool skip_dfs, bool prevent_cac,
bool highest_bandwidth)
{
struct acs_params cur_acs_params = {};
struct acs_params acs_params = {};
struct netif_radio *r = NULL;
acs_params.skip_dfs_not_available = prevent_cac;
cntlr_dbg(LOG_CHANNEL, "acs node channel recalc " MACFMT " opclass %u bandwidth %u skip_dfs %d prevent_cac %d higest_bandwidth %d\n",
MAC2STR(node->almacaddr), opclass, bandwidth, acs_params.skip_dfs, acs_params.skip_dfs_not_available, highest_bandwidth);
list_for_each_entry(r, &node->radiolist, list) {
struct wifi_radio_opclass req_opclass = {};
struct acs_params *last_acs;
uint8_t ctrl_channel;
if (band && band != BAND_ANY && band != r->radio_el->band)
continue;
last_acs = cntlr_radio_get_last_acs(r);
if (!last_acs)
continue;
memset(&acs_params, 0, sizeof(acs_params));
/* Try best bandwidth */
if (highest_bandwidth && !bandwidth && !opclass) {
bw = wifi_opclass_highest_bandwidth(&r->radio_el->pref_opclass, prevent_cac);
cntlr_dbg(LOG_CHANNEL, "acs node channel recalc " MACFMT " radio " MACFMT " highest bandwidth %d\n",
MAC2STR(node->almacaddr), MAC2STR(r->radio_el->macaddr), bw);
}
if (opclass) {
cntlr_get_current_acs_params(r->radio_el, &cur_acs_params);
acs_params.opclass = opclass;
if (cur_acs_params.opclass == opclass) {
acs_params.best_channel = cur_acs_params.best_channel;
acs_params.best_bw = cur_acs_params.best_bw;
}
} else if (bw) {
cntlr_get_current_acs_params(r->radio_el, &cur_acs_params);
acs_params.bw = bw;
if (cur_acs_params.bw == bw) {
acs_params.best_channel = cur_acs_params.best_channel;
acs_params.best_bw = cur_acs_params.best_bw;
}
} else {
if (cntlr_get_current_acs_params(r->radio_el, &cur_acs_params)) {
cntlr_dbg(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " - current channel not known\n",
MAC2STR(node->almacaddr), MAC2STR(r->radio_el->macaddr));
continue;
}
acs_params.opclass = cur_acs_params.opclass;
acs_params.best_channel = cur_acs_params.best_channel;
acs_params.best_bw = cur_acs_params.best_bw;
}
ret = cntlr_acs_radio_channel_recalc(node, r, &acs_params);
cntlr_trace(LOG_CHANNEL, "acs node " MACFMT " radio " MACFMT " new %d/%d opclass %d vs old %d/%d opclass %d cac_time %u\n",
MAC2STR(node->almacaddr), MAC2STR(r->radio_el->macaddr), acs_params.best_channel,
acs_params.best_bw, acs_params.best_opclass, cur_acs_params.best_channel,
cur_acs_params.bw, cur_acs_params.opclass, acs_params.best_cac_time);
/* Pick up control channel */
if (acs_params.best_bw > 40) {
ctrl_channel = cntrl_acs_radio_ctrl_channel(r->radio_el,
acs_params.best_opclass,
acs_params.best_channel);
} else {
ctrl_channel = acs_params.best_channel;
}
/* Build opclass we would like to send */
memcpy(&req_opclass, &r->radio_el->supp_opclass, sizeof(req_opclass));
wifi_opclass_set_preferences(&req_opclass, 0x0);
if (wifi_radio_opclass_update_channel(&req_opclass,
wifi_band_to_freqband(r->radio_el->band),
ctrl_channel, acs_params.best_bw, 15)) {
cntlr_dbg(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " - update opclass failed\n",
MAC2STR(node->almacaddr), MAC2STR(r->radio_el->macaddr));
}
wifi_opclass_dump_ex(&req_opclass, "acs_selection", r->radio_el->macaddr, false);
ret = cntlr_send_channel_selection(node->cntlr, node->almacaddr,
r->radio_el->macaddr, &req_opclass);
if (ret == 0xffff) {
cntlr_warn(LOG_CHANNEL, "acs node " MACFMT " " MACFMT " send channel switch request failed\n",
MAC2STR(node->almacaddr), MAC2STR(r->radio_el->macaddr));
continue;