blob: 54626ad10ad20c3298d5cd7c0dedd4d1c9e4c505 [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 Sperling60ecdef2018-04-10 17:55:08 +020029#include <osmocom/bsc/signal.h>
Stefan Sperling6442e432018-02-06 14:44:54 +010030
31/*
32 * Check if an ACC has been permanently barred for a BTS,
33 * e.g. with the 'rach access-control-class' VTY command.
34 */
35static bool acc_is_enabled(struct gsm_bts *bts, unsigned int acc)
36{
37 OSMO_ASSERT(acc >= 0 && acc <= 9);
38 if (acc == 8 || acc == 9)
39 return (bts->si_common.rach_control.t2 & (1 << (acc - 8))) == 0;
40 return (bts->si_common.rach_control.t3 & (1 << (acc))) == 0;
41}
42
43static void allow_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
44{
45 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020046 if (acc_ramp->barred_accs & (1 << acc))
47 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 +010048 acc_ramp->barred_accs &= ~(1 << acc);
49}
50
51static void barr_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
52{
53 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020054 if ((acc_ramp->barred_accs & (1 << acc)) == 0)
55 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 +010056 acc_ramp->barred_accs |= (1 << acc);
57}
58
59static void barr_all_enabled_accs(struct acc_ramp *acc_ramp)
60{
61 unsigned int acc;
62 for (acc = 0; acc < 10; acc++) {
63 if (acc_is_enabled(acc_ramp->bts, acc))
64 barr_one_acc(acc_ramp, acc);
65 }
66}
67
68static void allow_all_enabled_accs(struct acc_ramp *acc_ramp)
69{
70 unsigned int acc;
71 for (acc = 0; acc < 10; acc++) {
72 if (acc_is_enabled(acc_ramp->bts, acc))
73 allow_one_acc(acc_ramp, acc);
74 }
75}
76
77static unsigned int get_next_step_interval(struct acc_ramp *acc_ramp)
78{
79 struct gsm_bts *bts = acc_ramp->bts;
80 uint64_t load;
81
82 if (acc_ramp->step_interval_is_fixed)
83 return acc_ramp->step_interval_sec;
84
85 /* Scale the step interval to current channel load average. */
86 load = (bts->chan_load_avg << 8); /* convert to fixed-point */
87 acc_ramp->step_interval_sec = ((load * ACC_RAMP_STEP_INTERVAL_MAX) / 100) >> 8;
88 if (acc_ramp->step_interval_sec < ACC_RAMP_STEP_SIZE_MIN)
89 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
90 else if (acc_ramp->step_interval_sec > ACC_RAMP_STEP_INTERVAL_MAX)
91 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MAX;
92
93 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: step interval set to %u seconds based on %u%% channel load average\n",
94 bts->nr, acc_ramp->step_interval_sec, bts->chan_load_avg);
95 return acc_ramp->step_interval_sec;
96}
97
98static void do_acc_ramping_step(void *data)
99{
100 struct acc_ramp *acc_ramp = data;
101 int i;
102
103 /* Shortcut in case we only do one ramping step. */
104 if (acc_ramp->step_size == ACC_RAMP_STEP_SIZE_MAX) {
105 allow_all_enabled_accs(acc_ramp);
106 gsm_bts_set_system_infos(acc_ramp->bts);
107 return;
108 }
109
110 /* Allow 'step_size' ACCs, starting from ACC0. ACC9 will be allowed last. */
111 for (i = 0; i < acc_ramp->step_size; i++) {
112 int idx = ffs(acc_ramp_get_barred_t3(acc_ramp));
113 if (idx > 0) {
114 /* One of ACC0-ACC7 is still bared. */
115 unsigned int acc = idx - 1;
116 if (acc_is_enabled(acc_ramp->bts, acc))
117 allow_one_acc(acc_ramp, acc);
118 } else {
119 idx = ffs(acc_ramp_get_barred_t2(acc_ramp));
120 if (idx == 1 || idx == 2) {
121 /* ACC8 or ACC9 is still barred. */
122 unsigned int acc = idx - 1 + 8;
123 if (acc_is_enabled(acc_ramp->bts, acc))
124 allow_one_acc(acc_ramp, acc);
125 } else {
126 /* All ACCs are now allowed. */
127 break;
128 }
129 }
130 }
131
132 gsm_bts_set_system_infos(acc_ramp->bts);
133
134 /* If we have not allowed all ACCs yet, schedule another ramping step. */
135 if (acc_ramp_get_barred_t2(acc_ramp) != 0x00 ||
136 acc_ramp_get_barred_t3(acc_ramp) != 0x00)
137 osmo_timer_schedule(&acc_ramp->step_timer, get_next_step_interval(acc_ramp), 0);
138}
139
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200140/* Implements osmo_signal_cbfn() -- trigger or abort ACC ramping upon changes RF lock state. */
141static int acc_ramp_nm_sig_cb(unsigned int subsys, unsigned int signal, void *handler_data, void *signal_data)
142{
143 struct nm_statechg_signal_data *nsd = signal_data;
144 struct acc_ramp *acc_ramp = handler_data;
145 struct gsm_bts_trx *trx = NULL;
146
147 if (signal != S_NM_STATECHG_ADM)
148 return 0;
149
150 if (nsd->obj_class != NM_OC_RADIO_CARRIER)
151 return 0;
152
153 trx = nsd->obj;
154
155 /* We only care about state changes of the first TRX. */
156 if (trx->nr != 0)
157 return 0;
158
159 /* RSL must already be up. We cannot send RACH system information to the BTS otherwise. */
160 if (trx->rsl_link == NULL)
161 return 0;
162
163 /* Trigger or abort ACC ramping based on the new 'RF lock' state of this TRX. */
164 switch (nsd->new_state->administrative) {
165 case NM_STATE_UNLOCKED:
166 acc_ramp_trigger(acc_ramp);
167 break;
168 case NM_STATE_LOCKED:
169 case NM_STATE_SHUTDOWN:
170 acc_ramp_abort(acc_ramp);
171 break;
172 case NM_STATE_NULL:
173 break;
174 default:
175 LOGP(DRSL, LOGL_NOTICE, "(bts=%d) ACC RAMP: unrecognized administrative state '0x%x' reported for TRX 0\n",
176 acc_ramp->bts->nr, nsd->new_state->administrative);
177 break;
178 }
179
180 return 0;
181}
182
Stefan Sperling6442e432018-02-06 14:44:54 +0100183/*!
184 * Initialize an acc_ramp data structure.
185 * Storage for this structure must be provided by the caller.
186 *
Stefan Sperlingea333412018-04-10 16:36:54 +0200187 * By default, ACC ramping is disabled and all ACCs are allowed.
Stefan Sperling6442e432018-02-06 14:44:54 +0100188 *
189 * \param[in] acc_ramp Pointer to acc_ramp structure to be initialized.
Stefan Sperling6442e432018-02-06 14:44:54 +0100190 * \param[in] bts BTS which uses this ACC ramp data structure.
191 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200192void acc_ramp_init(struct acc_ramp *acc_ramp, struct gsm_bts *bts)
Stefan Sperling6442e432018-02-06 14:44:54 +0100193{
194 acc_ramp->bts = bts;
Stefan Sperlingea333412018-04-10 16:36:54 +0200195 acc_ramp_set_enabled(acc_ramp, false);
Stefan Sperling6442e432018-02-06 14:44:54 +0100196 acc_ramp->step_size = ACC_RAMP_STEP_SIZE_DEFAULT;
197 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
198 acc_ramp->step_interval_is_fixed = false;
Stefan Sperlingea333412018-04-10 16:36:54 +0200199 allow_all_enabled_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100200 osmo_timer_setup(&acc_ramp->step_timer, do_acc_ramping_step, acc_ramp);
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200201 osmo_signal_register_handler(SS_NM, acc_ramp_nm_sig_cb, acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100202}
203
204/*!
205 * Change the ramping step size which controls how many ACCs will be allowed per ramping step.
206 * Returns negative on error (step_size out of range), else zero.
207 * \param[in] acc_ramp Pointer to acc_ramp structure.
208 * \param[in] step_size The new step size value.
209 */
210int acc_ramp_set_step_size(struct acc_ramp *acc_ramp, unsigned int step_size)
211{
212 if (step_size < ACC_RAMP_STEP_SIZE_MIN || step_size > ACC_RAMP_STEP_SIZE_MAX)
213 return -ERANGE;
214
215 acc_ramp->step_size = step_size;
216 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step size set to %u\n", acc_ramp->bts->nr, step_size);
217 return 0;
218}
219
220/*!
221 * Change the ramping step interval to a fixed value. Unless this function is called,
222 * the interval is automatically scaled to the BTS channel load average.
223 * \param[in] acc_ramp Pointer to acc_ramp structure.
224 * \param[in] step_interval The new fixed step interval in seconds.
225 */
226int acc_ramp_set_step_interval(struct acc_ramp *acc_ramp, unsigned int step_interval)
227{
228 if (step_interval < ACC_RAMP_STEP_INTERVAL_MIN || step_interval > ACC_RAMP_STEP_INTERVAL_MAX)
229 return -ERANGE;
230
231 acc_ramp->step_interval_sec = step_interval;
232 acc_ramp->step_interval_is_fixed = true;
233 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to %u seconds\n",
234 acc_ramp->bts->nr, step_interval);
235 return 0;
236}
237
238/*!
239 * Clear a previously set fixed ramping step interval, so that the interval
240 * is again automatically scaled to the BTS channel load average.
241 * \param[in] acc_ramp Pointer to acc_ramp structure.
242 */
243void acc_ramp_set_step_interval_dynamic(struct acc_ramp *acc_ramp)
244{
245 acc_ramp->step_interval_is_fixed = false;
246 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to 'dynamic'\n",
247 acc_ramp->bts->nr);
248}
249
250/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200251 * Determine if ACC ramping should be started according to configuration, and
252 * if ACC ramping is enabled, begin the ramping process.
253 * Perform at least one ramping step to allow 'step_size' ACCs.
254 * If 'step_size' is ACC_RAMP_STEP_SIZE_MAX, or if ACC ramping is disabled,
255 * all ACCs will be allowed immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100256 * \param[in] acc_ramp Pointer to acc_ramp structure.
257 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200258void acc_ramp_trigger(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +0100259{
Stefan Sperlingea333412018-04-10 16:36:54 +0200260 /* Abort any previously running ramping process and allow all available ACCs. */
Stefan Sperling6442e432018-02-06 14:44:54 +0100261 acc_ramp_abort(acc_ramp);
262
Stefan Sperlingea333412018-04-10 16:36:54 +0200263 if (acc_ramp_is_enabled(acc_ramp)) {
264 /* Set all available ACCs to barred and start ramping up. */
265 barr_all_enabled_accs(acc_ramp);
266 do_acc_ramping_step(acc_ramp);
267 }
Stefan Sperling6442e432018-02-06 14:44:54 +0100268}
269
270/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200271 * Abort the ramping process and allow all available ACCs immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100272 * \param[in] acc_ramp Pointer to acc_ramp structure.
273 */
274void acc_ramp_abort(struct acc_ramp *acc_ramp)
275{
276 if (osmo_timer_pending(&acc_ramp->step_timer))
277 osmo_timer_del(&acc_ramp->step_timer);
Stefan Sperlingea333412018-04-10 16:36:54 +0200278
279 allow_all_enabled_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100280}