blob: 16bce3f4b3a71fa9f866ebf4fc28700beac54abd [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 */
Stefan Sperling0ad90b32018-04-11 13:36:52 +020035static bool acc_is_permanently_barred(struct gsm_bts *bts, unsigned int acc)
Stefan Sperling6442e432018-02-06 14:44:54 +010036{
37 OSMO_ASSERT(acc >= 0 && acc <= 9);
38 if (acc == 8 || acc == 9)
Stefan Sperling0ad90b32018-04-11 13:36:52 +020039 return (bts->si_common.rach_control.t2 & (1 << (acc - 8)));
40 return (bts->si_common.rach_control.t3 & (1 << (acc)));
Stefan Sperling6442e432018-02-06 14:44:54 +010041}
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
Stefan Sperling0ad90b32018-04-11 13:36:52 +020059static void barr_all_accs(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +010060{
61 unsigned int acc;
62 for (acc = 0; acc < 10; acc++) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +020063 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +010064 barr_one_acc(acc_ramp, acc);
65 }
66}
67
Stefan Sperling0ad90b32018-04-11 13:36:52 +020068static void allow_all_accs(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +010069{
70 unsigned int acc;
71 for (acc = 0; acc < 10; acc++) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +020072 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +010073 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) {
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200105 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100106 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;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200116 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +0100117 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;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200123 if (!acc_is_permanently_barred(acc_ramp->bts, acc))
Stefan Sperling6442e432018-02-06 14:44:54 +0100124 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
Stefan Sperlingb06c7a22018-04-11 20:46:38 +0200147 /* Handled signals map to an Administrative State Change ACK, or a State Changed Event Report. */
148 if (signal != S_NM_STATECHG_ADM && signal != S_NM_STATECHG_OPER)
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200149 return 0;
150
151 if (nsd->obj_class != NM_OC_RADIO_CARRIER)
152 return 0;
153
154 trx = nsd->obj;
155
156 /* We only care about state changes of the first TRX. */
157 if (trx->nr != 0)
158 return 0;
159
160 /* RSL must already be up. We cannot send RACH system information to the BTS otherwise. */
161 if (trx->rsl_link == NULL)
162 return 0;
163
164 /* Trigger or abort ACC ramping based on the new 'RF lock' state of this TRX. */
165 switch (nsd->new_state->administrative) {
166 case NM_STATE_UNLOCKED:
Stefan Sperlingb06c7a22018-04-11 20:46:38 +0200167 /*
168 * Do not re-trigger ACC ramping if ramping is already in progress.
169 * A BTS might send several "unlock" change events: One in the Administrative
170 * State Change ACK, and/or another in a State Changed Event Report.
171 * For instance, the nanobts is known to send both.
172 */
173 if (!osmo_timer_pending(&acc_ramp->step_timer))
174 acc_ramp_trigger(acc_ramp);
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200175 break;
176 case NM_STATE_LOCKED:
177 case NM_STATE_SHUTDOWN:
178 acc_ramp_abort(acc_ramp);
179 break;
180 case NM_STATE_NULL:
181 break;
182 default:
183 LOGP(DRSL, LOGL_NOTICE, "(bts=%d) ACC RAMP: unrecognized administrative state '0x%x' reported for TRX 0\n",
184 acc_ramp->bts->nr, nsd->new_state->administrative);
185 break;
186 }
187
188 return 0;
189}
190
Stefan Sperling6442e432018-02-06 14:44:54 +0100191/*!
192 * Initialize an acc_ramp data structure.
193 * Storage for this structure must be provided by the caller.
194 *
Stefan Sperlingea333412018-04-10 16:36:54 +0200195 * By default, ACC ramping is disabled and all ACCs are allowed.
Stefan Sperling6442e432018-02-06 14:44:54 +0100196 *
197 * \param[in] acc_ramp Pointer to acc_ramp structure to be initialized.
Stefan Sperling6442e432018-02-06 14:44:54 +0100198 * \param[in] bts BTS which uses this ACC ramp data structure.
199 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200200void acc_ramp_init(struct acc_ramp *acc_ramp, struct gsm_bts *bts)
Stefan Sperling6442e432018-02-06 14:44:54 +0100201{
202 acc_ramp->bts = bts;
Stefan Sperlingea333412018-04-10 16:36:54 +0200203 acc_ramp_set_enabled(acc_ramp, false);
Stefan Sperling6442e432018-02-06 14:44:54 +0100204 acc_ramp->step_size = ACC_RAMP_STEP_SIZE_DEFAULT;
205 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
206 acc_ramp->step_interval_is_fixed = false;
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200207 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100208 osmo_timer_setup(&acc_ramp->step_timer, do_acc_ramping_step, acc_ramp);
Stefan Sperling60ecdef2018-04-10 17:55:08 +0200209 osmo_signal_register_handler(SS_NM, acc_ramp_nm_sig_cb, acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100210}
211
212/*!
213 * Change the ramping step size which controls how many ACCs will be allowed per ramping step.
214 * Returns negative on error (step_size out of range), else zero.
215 * \param[in] acc_ramp Pointer to acc_ramp structure.
216 * \param[in] step_size The new step size value.
217 */
218int acc_ramp_set_step_size(struct acc_ramp *acc_ramp, unsigned int step_size)
219{
220 if (step_size < ACC_RAMP_STEP_SIZE_MIN || step_size > ACC_RAMP_STEP_SIZE_MAX)
221 return -ERANGE;
222
223 acc_ramp->step_size = step_size;
224 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step size set to %u\n", acc_ramp->bts->nr, step_size);
225 return 0;
226}
227
228/*!
229 * Change the ramping step interval to a fixed value. Unless this function is called,
230 * the interval is automatically scaled to the BTS channel load average.
231 * \param[in] acc_ramp Pointer to acc_ramp structure.
232 * \param[in] step_interval The new fixed step interval in seconds.
233 */
234int acc_ramp_set_step_interval(struct acc_ramp *acc_ramp, unsigned int step_interval)
235{
236 if (step_interval < ACC_RAMP_STEP_INTERVAL_MIN || step_interval > ACC_RAMP_STEP_INTERVAL_MAX)
237 return -ERANGE;
238
239 acc_ramp->step_interval_sec = step_interval;
240 acc_ramp->step_interval_is_fixed = true;
241 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to %u seconds\n",
242 acc_ramp->bts->nr, step_interval);
243 return 0;
244}
245
246/*!
247 * Clear a previously set fixed ramping step interval, so that the interval
248 * is again automatically scaled to the BTS channel load average.
249 * \param[in] acc_ramp Pointer to acc_ramp structure.
250 */
251void acc_ramp_set_step_interval_dynamic(struct acc_ramp *acc_ramp)
252{
253 acc_ramp->step_interval_is_fixed = false;
254 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to 'dynamic'\n",
255 acc_ramp->bts->nr);
256}
257
258/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200259 * Determine if ACC ramping should be started according to configuration, and
260 * if ACC ramping is enabled, begin the ramping process.
261 * Perform at least one ramping step to allow 'step_size' ACCs.
262 * If 'step_size' is ACC_RAMP_STEP_SIZE_MAX, or if ACC ramping is disabled,
263 * all ACCs will be allowed immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100264 * \param[in] acc_ramp Pointer to acc_ramp structure.
265 */
Stefan Sperlingea333412018-04-10 16:36:54 +0200266void acc_ramp_trigger(struct acc_ramp *acc_ramp)
Stefan Sperling6442e432018-02-06 14:44:54 +0100267{
Stefan Sperlingea333412018-04-10 16:36:54 +0200268 /* Abort any previously running ramping process and allow all available ACCs. */
Stefan Sperling6442e432018-02-06 14:44:54 +0100269 acc_ramp_abort(acc_ramp);
270
Stefan Sperlingea333412018-04-10 16:36:54 +0200271 if (acc_ramp_is_enabled(acc_ramp)) {
272 /* Set all available ACCs to barred and start ramping up. */
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200273 barr_all_accs(acc_ramp);
Stefan Sperlingea333412018-04-10 16:36:54 +0200274 do_acc_ramping_step(acc_ramp);
275 }
Stefan Sperling6442e432018-02-06 14:44:54 +0100276}
277
278/*!
Stefan Sperlingea333412018-04-10 16:36:54 +0200279 * Abort the ramping process and allow all available ACCs immediately.
Stefan Sperling6442e432018-02-06 14:44:54 +0100280 * \param[in] acc_ramp Pointer to acc_ramp structure.
281 */
282void acc_ramp_abort(struct acc_ramp *acc_ramp)
283{
284 if (osmo_timer_pending(&acc_ramp->step_timer))
285 osmo_timer_del(&acc_ramp->step_timer);
Stefan Sperlingea333412018-04-10 16:36:54 +0200286
Stefan Sperling0ad90b32018-04-11 13:36:52 +0200287 allow_all_accs(acc_ramp);
Stefan Sperling6442e432018-02-06 14:44:54 +0100288}