blob: ff8ff0e9d6600eb1a8c8cc9f0cd2cae31d07b80a [file] [log] [blame]
Stefan Sperling6442e432018-02-06 14:44:54 +01001/* (C) 2018 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 *
3 * Author: Stefan Sperling <ssperling@sysmocom.de>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include <strings.h>
23#include <errno.h>
24#include <stdbool.h>
25
26#include <osmocom/bsc/debug.h>
27#include <osmocom/bsc/acc_ramp.h>
28#include <osmocom/bsc/gsm_data.h>
Stefan Sperling4d3d2432018-04-12 09:18:36 +020029#include <osmocom/bsc/chan_alloc.h>
Stefan Sperling60ecdef2018-04-10 17:55:08 +020030#include <osmocom/bsc/signal.h>
Stefan Sperlingcda994e2018-04-12 09:18:36 +020031#include <osmocom/bsc/abis_nm.h>
Stefan Sperling6442e432018-02-06 14:44:54 +010032
33/*
34 * Check if an ACC has been permanently barred for a BTS,
35 * e.g. with the 'rach access-control-class' VTY command.
36 */
Stefan Sperling0ad90b32018-04-11 13:36:52 +020037static bool acc_is_permanently_barred(struct gsm_bts *bts, unsigned int acc)
Stefan Sperling6442e432018-02-06 14:44:54 +010038{
39 OSMO_ASSERT(acc >= 0 && acc <= 9);
40 if (acc == 8 || acc == 9)
Stefan Sperling0ad90b32018-04-11 13:36:52 +020041 return (bts->si_common.rach_control.t2 & (1 << (acc - 8)));
42 return (bts->si_common.rach_control.t3 & (1 << (acc)));
Stefan Sperling6442e432018-02-06 14:44:54 +010043}
44
45static void allow_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
46{
47 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020048 if (acc_ramp->barred_accs & (1 << acc))
49 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: allowing Access Control Class %u\n", acc_ramp->bts->nr, acc);
Stefan Sperling6442e432018-02-06 14:44:54 +010050 acc_ramp->barred_accs &= ~(1 << acc);
51}
52
53static void barr_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
54{
55 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020056 if ((acc_ramp->barred_accs & (1 << acc)) == 0)
57 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: barring Access Control Class %u\n", acc_ramp->bts->nr, acc);
Stefan Sperling6442e432018-02-06 14:44:54 +010058 acc_ramp->barred_accs |= (1 << acc);
59}
60
Stefan Sperling0ad90b32018-04-11 13:36:52 +020061static void barr_all_accs(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +010062{
63 unsigned int acc;
64 for (acc = 0; acc < 10; acc++) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +020065 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +010066 barr_one_acc(acc_ramp, acc);
67 }
68}
69
Stefan Sperling0ad90b32018-04-11 13:36:52 +020070static void allow_all_accs(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +010071{
72 unsigned int acc;
73 for (acc = 0; acc < 10; acc++) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +020074 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +010075 allow_one_acc(acc_ramp, acc);
76 }
77}
78
79static unsigned int get_next_step_interval(struct acc_ramp *acc_ramp)
80{
81 struct gsm_bts *bts = acc_ramp->bts;
82 uint64_t load;
83
84 if (acc_ramp->step_interval_is_fixed)
85 return acc_ramp->step_interval_sec;
86
87 /* Scale the step interval to current channel load average. */
88 load = (bts->chan_load_avg << 8); /* convert to fixed-point */
89 acc_ramp->step_interval_sec = ((load * ACC_RAMP_STEP_INTERVAL_MAX) / 100) >> 8;
90 if (acc_ramp->step_interval_sec < ACC_RAMP_STEP_SIZE_MIN)
91 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
92 else if (acc_ramp->step_interval_sec > ACC_RAMP_STEP_INTERVAL_MAX)
93 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MAX;
94
95 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: step interval set to %u seconds based on %u%% channel load average\n",
96 bts->nr, acc_ramp->step_interval_sec, bts->chan_load_avg);
97 return acc_ramp->step_interval_sec;
98}
99
100static void do_acc_ramping_step(void *data)
101{
102 struct acc_ramp *acc_ramp = data;
103 int i;
104
105 /* Shortcut in case we only do one ramping step. */
106 if (acc_ramp->step_size == ACC_RAMP_STEP_SIZE_MAX) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200107 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100108 gsm_bts_set_system_infos(acc_ramp->bts);
109 return;
110 }
111
112 /* Allow 'step_size' ACCs, starting from ACC0. ACC9 will be allowed last. */
113 for (i = 0; i < acc_ramp->step_size; i++) {
114 int idx = ffs(acc_ramp_get_barred_t3(acc_ramp));
115 if (idx > 0) {
116 /* One of ACC0-ACC7 is still bared. */
117 unsigned int acc = idx - 1;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200118 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +0100119 allow_one_acc(acc_ramp, acc);
120 } else {
121 idx = ffs(acc_ramp_get_barred_t2(acc_ramp));
122 if (idx == 1 || idx == 2) {
123 /* ACC8 or ACC9 is still barred. */
124 unsigned int acc = idx - 1 + 8;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200125 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +0100126 allow_one_acc(acc_ramp, acc);
127 } else {
128 /* All ACCs are now allowed. */
129 break;
130 }
131 }
132 }
133
134 gsm_bts_set_system_infos(acc_ramp->bts);
135
136 /* If we have not allowed all ACCs yet, schedule another ramping step. */
137 if (acc_ramp_get_barred_t2(acc_ramp) != 0x00 ||
138 acc_ramp_get_barred_t3(acc_ramp) != 0x00)
139 osmo_timer_schedule(&acc_ramp->step_timer, get_next_step_interval(acc_ramp), 0);
140}
141
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200142/* Implements osmo_signal_cbfn() -- trigger or abort ACC ramping upon changes RF lock state. */
143static int acc_ramp_nm_sig_cb(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data)
144{
145 struct nm_statechg_signal_data *nsd = signal_data;
146 struct acc_ramp *acc_ramp = handler_data;
147 struct gsm_bts_trx *trx = NULL;
Stefan Sperlingcda994e2018-04-12 09:18:36 +0200148 bool trigger_ramping = false, abort_ramping = false;
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200149
Stefan Sperlingb06c7a22018-04-11 20:46:38 +0200150 /* Handled signals map to an Administrative State Change ACK, or a State Changed Event Report. */
151 if (signal != S_NM_STATECHG_ADM && signal != S_NM_STATECHG_OPER)
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200152 return 0;
153
154 if (nsd->obj_class != NM_OC_RADIO_CARRIER)
155 return 0;
156
157 trx = nsd->obj;
158
Stefan Sperlingcda994e2018-04-12 09:18:36 +0200159 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: administrative state %s -> %s\n",
160 acc_ramp->bts->nr, trx->nr,
161 get_value_string(abis_nm_adm_state_names, nsd->old_state->administrative),
162 get_value_string(abis_nm_adm_state_names, nsd->new_state->administrative));
163 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: operational state %s -> %s\n",
164 acc_ramp->bts->nr, trx->nr,
165 abis_nm_opstate_name(nsd->old_state->operational),
166 abis_nm_opstate_name(nsd->new_state->operational));
167
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200168 /* We only care about state changes of the first TRX. */
169 if (trx->nr != 0)
170 return 0;
171
172 /* RSL must already be up. We cannot send RACH system information to the BTS otherwise. */
Stefan Sperlingcda994e2018-04-12 09:18:36 +0200173 if (trx->rsl_link == NULL) {
174 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: ignoring state change because RSL link is down\n",
175 acc_ramp->bts->nr, trx->nr);
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200176 return 0;
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200177 }
178
Stefan Sperlingcda994e2018-04-12 09:18:36 +0200179 /* Trigger or abort ACC ramping based on the new state of this TRX. */
180 if (nsd->old_state->administrative != nsd->new_state->administrative) {
181 switch (nsd->new_state->administrative) {
182 case NM_STATE_UNLOCKED:
183 if (nsd->old_state->operational != nsd->new_state->operational) {
184 /*
185 * Administrative and operational state have both changed.
186 * Trigger ramping only if TRX 0 will be both enabled and unlocked.
187 */
188 if (nsd->new_state->operational == NM_OPSTATE_ENABLED)
189 trigger_ramping = true;
190 else
191 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: ignoring state change "
192 "because TRX is transitioning into operational state '%s'\n",
193 acc_ramp->bts->nr, trx->nr,
194 abis_nm_opstate_name(nsd->new_state->operational));
195 } else {
196 /*
197 * Operational state has not changed.
198 * Trigger ramping only if TRX 0 is already usable.
199 */
200 if (trx_is_usable(trx))
201 trigger_ramping = true;
202 else
203 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: ignoring state change "
204 "because TRX is not usable\n", acc_ramp->bts->nr, trx->nr);
205 }
206 break;
207 case NM_STATE_LOCKED:
208 case NM_STATE_SHUTDOWN:
209 abort_ramping = true;
210 break;
211 case NM_STATE_NULL:
212 default:
213 LOGP(DRSL, LOGL_NOTICE, "(bts=%d) ACC RAMP: unrecognized administrative state '0x%x' "
214 "reported for TRX 0\n", acc_ramp->bts->nr, nsd->new_state->administrative);
215 break;
216 }
217 }
218 if (nsd->old_state->operational != nsd->new_state->operational) {
219 switch (nsd->new_state->operational) {
220 case NM_OPSTATE_ENABLED:
221 if (nsd->old_state->administrative != nsd->new_state->administrative) {
222 /*
223 * Administrative and operational state have both changed.
224 * Trigger ramping only if TRX 0 will be both enabled and unlocked.
225 */
226 if (nsd->new_state->administrative == NM_STATE_UNLOCKED)
227 trigger_ramping = true;
228 else
229 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: ignoring state change "
230 "because TRX is transitioning into administrative state '%s'\n",
231 acc_ramp->bts->nr, trx->nr,
232 get_value_string(abis_nm_adm_state_names, nsd->new_state->administrative));
233 } else {
234 /*
235 * Administrative state has not changed.
236 * Trigger ramping only if TRX 0 is already unlocked.
237 */
238 if (trx->mo.nm_state.administrative == NM_STATE_UNLOCKED)
239 trigger_ramping = true;
240 else
241 LOGP(DRSL, LOGL_DEBUG, "(bts=%d,trx=%d) ACC RAMP: ignoring state change "
242 "because TRX is in administrative state '%s'\n",
243 acc_ramp->bts->nr, trx->nr,
244 get_value_string(abis_nm_adm_state_names, trx->mo.nm_state.administrative));
245 }
246 break;
247 case NM_OPSTATE_DISABLED:
248 abort_ramping = true;
249 break;
250 case NM_OPSTATE_NULL:
251 default:
252 LOGP(DRSL, LOGL_NOTICE, "(bts=%d) ACC RAMP: unrecognized operational state '0x%x' "
253 "reported for TRX 0\n", acc_ramp->bts->nr, nsd->new_state->administrative);
254 break;
255 }
256 }
257
258 if (trigger_ramping)
259 acc_ramp_trigger(acc_ramp);
260 else if (abort_ramping)
261 acc_ramp_abort(acc_ramp);
262
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200263 return 0;
264}
265
Stefan Sperling6442e432018-02-06 14:44:54 +0100266/*!
267 * Initialize an acc_ramp data structure.
268 * Storage for this structure must be provided by the caller.
269 *
Stefan Sperlingea333412018-04-10 16:36:54 +0200270 * By default, ACC ramping is disabled and all ACCs are allowed.
Stefan Sperling6442e432018-02-06 14:44:54 +0100271 *
272 * \param[in] acc_ramp Pointer to acc_ramp structure to be initialized.
Stefan Sperling6442e432018-02-06 14:44:54 +0100273 * \param[in] bts BTS which uses this ACC ramp data structure.
274 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200275void acc_ramp_init(struct acc_ramp *acc_ramp, struct gsm_bts *bts)
Stefan Sperling6442e432018-02-06 14:44:54 +0100276{
277 acc_ramp->bts = bts;
Stefan Sperlingea333412018-04-10 16:36:54 +0200278 acc_ramp_set_enabled(acc_ramp, false);
Stefan Sperling6442e432018-02-06 14:44:54 +0100279 acc_ramp->step_size = ACC_RAMP_STEP_SIZE_DEFAULT;
280 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
281 acc_ramp->step_interval_is_fixed = false;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200282 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100283 osmo_timer_setup(&acc_ramp->step_timer, do_acc_ramping_step, acc_ramp);
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200284 osmo_signal_register_handler(SS_NM, acc_ramp_nm_sig_cb, acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100285}
286
287/*!
288 * Change the ramping step size which controls how many ACCs will be allowed per ramping step.
289 * Returns negative on error (step_size out of range), else zero.
290 * \param[in] acc_ramp Pointer to acc_ramp structure.
291 * \param[in] step_size The new step size value.
292 */
293int acc_ramp_set_step_size(struct acc_ramp *acc_ramp, unsigned int step_size)
294{
295 if (step_size < ACC_RAMP_STEP_SIZE_MIN || step_size > ACC_RAMP_STEP_SIZE_MAX)
296 return -ERANGE;
297
298 acc_ramp->step_size = step_size;
299 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step size set to %u\n", acc_ramp->bts->nr, step_size);
300 return 0;
301}
302
303/*!
304 * Change the ramping step interval to a fixed value. Unless this function is called,
305 * the interval is automatically scaled to the BTS channel load average.
306 * \param[in] acc_ramp Pointer to acc_ramp structure.
307 * \param[in] step_interval The new fixed step interval in seconds.
308 */
309int acc_ramp_set_step_interval(struct acc_ramp *acc_ramp, unsigned int step_interval)
310{
311 if (step_interval < ACC_RAMP_STEP_INTERVAL_MIN || step_interval > ACC_RAMP_STEP_INTERVAL_MAX)
312 return -ERANGE;
313
314 acc_ramp->step_interval_sec = step_interval;
315 acc_ramp->step_interval_is_fixed = true;
316 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to %u seconds\n",
317 acc_ramp->bts->nr, step_interval);
318 return 0;
319}
320
321/*!
322 * Clear a previously set fixed ramping step interval, so that the interval
323 * is again automatically scaled to the BTS channel load average.
324 * \param[in] acc_ramp Pointer to acc_ramp structure.
325 */
326void acc_ramp_set_step_interval_dynamic(struct acc_ramp *acc_ramp)
327{
328 acc_ramp->step_interval_is_fixed = false;
329 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to 'dynamic'\n",
330 acc_ramp->bts->nr);
331}
332
333/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200334 * Determine if ACC ramping should be started according to configuration, and
Stefan Sperling4d3d2432018-04-12 09:18:36 +0200335 * begin the ramping process if the necessary conditions are present.
Stefan Sperlingea333412018-04-10 16:36:54 +0200336 * Perform at least one ramping step to allow 'step_size' ACCs.
337 * If 'step_size' is ACC_RAMP_STEP_SIZE_MAX, or if ACC ramping is disabled,
338 * all ACCs will be allowed immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100339 * \param[in] acc_ramp Pointer to acc_ramp structure.
340 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200341void acc_ramp_trigger(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +0100342{
Stefan Sperlingea333412018-04-10 16:36:54 +0200343 /* Abort any previously running ramping process and allow all available ACCs. */
Stefan Sperling6442e432018-02-06 14:44:54 +0100344 acc_ramp_abort(acc_ramp);
345
Stefan Sperlingea333412018-04-10 16:36:54 +0200346 if (acc_ramp_is_enabled(acc_ramp)) {
Stefan Sperlingcda994e2018-04-12 09:18:36 +0200347 /* Set all available ACCs to barred and start ramping up. */
348 barr_all_accs(acc_ramp);
349 do_acc_ramping_step(acc_ramp);
Stefan Sperlingea333412018-04-10 16:36:54 +0200350 }
Stefan Sperling6442e432018-02-06 14:44:54 +0100351}
352
353/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200354 * Abort the ramping process and allow all available ACCs immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100355 * \param[in] acc_ramp Pointer to acc_ramp structure.
356 */
357void acc_ramp_abort(struct acc_ramp *acc_ramp)
358{
359 if (osmo_timer_pending(&acc_ramp->step_timer))
360 osmo_timer_del(&acc_ramp->step_timer);
Stefan Sperlingea333412018-04-10 16:36:54 +0200361
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200362 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100363}