blob: 5d358426b5748b8f7568493ec6ea300a60371650 [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>
29
30/*
31 * Check if an ACC has been permanently barred for a BTS,
32 * e.g. with the 'rach access-control-class' VTY command.
33 */
34static bool acc_is_enabled(struct gsm_bts *bts, unsigned int acc)
35{
36 OSMO_ASSERT(acc >= 0 && acc <= 9);
37 if (acc == 8 || acc == 9)
38 return (bts->si_common.rach_control.t2 & (1 << (acc - 8))) == 0;
39 return (bts->si_common.rach_control.t3 & (1 << (acc))) == 0;
40}
41
42static void allow_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
43{
44 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020045 if (acc_ramp->barred_accs & (1 << acc))
46 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 +010047 acc_ramp->barred_accs &= ~(1 << acc);
48}
49
50static void barr_one_acc(struct acc_ramp *acc_ramp, unsigned int acc)
51{
52 OSMO_ASSERT(acc >= 0 && acc <= 9);
Stefan Sperling53d40e02018-04-10 16:25:26 +020053 if ((acc_ramp->barred_accs & (1 << acc)) == 0)
54 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 +010055 acc_ramp->barred_accs |= (1 << acc);
56}
57
58static void barr_all_enabled_accs(struct acc_ramp *acc_ramp)
59{
60 unsigned int acc;
61 for (acc = 0; acc < 10; acc++) {
62 if (acc_is_enabled(acc_ramp->bts, acc))
63 barr_one_acc(acc_ramp, acc);
64 }
65}
66
67static void allow_all_enabled_accs(struct acc_ramp *acc_ramp)
68{
69 unsigned int acc;
70 for (acc = 0; acc < 10; acc++) {
71 if (acc_is_enabled(acc_ramp->bts, acc))
72 allow_one_acc(acc_ramp, acc);
73 }
74}
75
76static unsigned int get_next_step_interval(struct acc_ramp *acc_ramp)
77{
78 struct gsm_bts *bts = acc_ramp->bts;
79 uint64_t load;
80
81 if (acc_ramp->step_interval_is_fixed)
82 return acc_ramp->step_interval_sec;
83
84 /* Scale the step interval to current channel load average. */
85 load = (bts->chan_load_avg << 8); /* convert to fixed-point */
86 acc_ramp->step_interval_sec = ((load * ACC_RAMP_STEP_INTERVAL_MAX) / 100) >> 8;
87 if (acc_ramp->step_interval_sec < ACC_RAMP_STEP_SIZE_MIN)
88 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
89 else if (acc_ramp->step_interval_sec > ACC_RAMP_STEP_INTERVAL_MAX)
90 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MAX;
91
92 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: step interval set to %u seconds based on %u%% channel load average\n",
93 bts->nr, acc_ramp->step_interval_sec, bts->chan_load_avg);
94 return acc_ramp->step_interval_sec;
95}
96
97static void do_acc_ramping_step(void *data)
98{
99 struct acc_ramp *acc_ramp = data;
100 int i;
101
102 /* Shortcut in case we only do one ramping step. */
103 if (acc_ramp->step_size == ACC_RAMP_STEP_SIZE_MAX) {
104 allow_all_enabled_accs(acc_ramp);
105 gsm_bts_set_system_infos(acc_ramp->bts);
106 return;
107 }
108
109 /* Allow 'step_size' ACCs, starting from ACC0. ACC9 will be allowed last. */
110 for (i = 0; i < acc_ramp->step_size; i++) {
111 int idx = ffs(acc_ramp_get_barred_t3(acc_ramp));
112 if (idx > 0) {
113 /* One of ACC0-ACC7 is still bared. */
114 unsigned int acc = idx - 1;
115 if (acc_is_enabled(acc_ramp->bts, acc))
116 allow_one_acc(acc_ramp, acc);
117 } else {
118 idx = ffs(acc_ramp_get_barred_t2(acc_ramp));
119 if (idx == 1 || idx == 2) {
120 /* ACC8 or ACC9 is still barred. */
121 unsigned int acc = idx - 1 + 8;
122 if (acc_is_enabled(acc_ramp->bts, acc))
123 allow_one_acc(acc_ramp, acc);
124 } else {
125 /* All ACCs are now allowed. */
126 break;
127 }
128 }
129 }
130
131 gsm_bts_set_system_infos(acc_ramp->bts);
132
133 /* If we have not allowed all ACCs yet, schedule another ramping step. */
134 if (acc_ramp_get_barred_t2(acc_ramp) != 0x00 ||
135 acc_ramp_get_barred_t3(acc_ramp) != 0x00)
136 osmo_timer_schedule(&acc_ramp->step_timer, get_next_step_interval(acc_ramp), 0);
137}
138
139/*!
140 * Initialize an acc_ramp data structure.
141 * Storage for this structure must be provided by the caller.
142 *
143 * If ACC ramping is enabled, all ACCs are denied by default.
144 * A subsequent call to acc_ramp_start() will begin the ramping process.
145 * If ACC ramping is disabled, all ACCs will be allowed by default,
146 * and there is no need to do anything else.
147 *
148 * \param[in] acc_ramp Pointer to acc_ramp structure to be initialized.
149 * \param[in] enable Indicates whether ACC ramping should be enabled or disabled.
150 * \param[in] bts BTS which uses this ACC ramp data structure.
151 */
152void acc_ramp_init(struct acc_ramp *acc_ramp, bool enable, struct gsm_bts *bts)
153{
154 acc_ramp->bts = bts;
155 acc_ramp->acc_ramping_enabled = enable;
156 acc_ramp->step_size = ACC_RAMP_STEP_SIZE_DEFAULT;
157 acc_ramp->step_interval_sec = ACC_RAMP_STEP_INTERVAL_MIN;
158 acc_ramp->step_interval_is_fixed = false;
159 osmo_timer_setup(&acc_ramp->step_timer, do_acc_ramping_step, acc_ramp);
160
161 if (acc_ramp->acc_ramping_enabled)
162 barr_all_enabled_accs(acc_ramp);
163 else
164 allow_all_enabled_accs(acc_ramp);
165}
166
167/*!
168 * Change the ramping step size which controls how many ACCs will be allowed per ramping step.
169 * Returns negative on error (step_size out of range), else zero.
170 * \param[in] acc_ramp Pointer to acc_ramp structure.
171 * \param[in] step_size The new step size value.
172 */
173int acc_ramp_set_step_size(struct acc_ramp *acc_ramp, unsigned int step_size)
174{
175 if (step_size < ACC_RAMP_STEP_SIZE_MIN || step_size > ACC_RAMP_STEP_SIZE_MAX)
176 return -ERANGE;
177
178 acc_ramp->step_size = step_size;
179 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step size set to %u\n", acc_ramp->bts->nr, step_size);
180 return 0;
181}
182
183/*!
184 * Change the ramping step interval to a fixed value. Unless this function is called,
185 * the interval is automatically scaled to the BTS channel load average.
186 * \param[in] acc_ramp Pointer to acc_ramp structure.
187 * \param[in] step_interval The new fixed step interval in seconds.
188 */
189int acc_ramp_set_step_interval(struct acc_ramp *acc_ramp, unsigned int step_interval)
190{
191 if (step_interval < ACC_RAMP_STEP_INTERVAL_MIN || step_interval > ACC_RAMP_STEP_INTERVAL_MAX)
192 return -ERANGE;
193
194 acc_ramp->step_interval_sec = step_interval;
195 acc_ramp->step_interval_is_fixed = true;
196 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to %u seconds\n",
197 acc_ramp->bts->nr, step_interval);
198 return 0;
199}
200
201/*!
202 * Clear a previously set fixed ramping step interval, so that the interval
203 * is again automatically scaled to the BTS channel load average.
204 * \param[in] acc_ramp Pointer to acc_ramp structure.
205 */
206void acc_ramp_set_step_interval_dynamic(struct acc_ramp *acc_ramp)
207{
208 acc_ramp->step_interval_is_fixed = false;
209 LOGP(DRSL, LOGL_DEBUG, "(bts=%d) ACC RAMP: ramping step interval set to 'dynamic'\n",
210 acc_ramp->bts->nr);
211}
212
213/*!
214 * Begin the ramping process. Perform at least one ramping step to allow 'step_size' ACCs.
215 * If 'step_size' is ACC_RAMP_STEP_SIZE_MAX, all ACCs will be allowed immediately.
216 * \param[in] acc_ramp Pointer to acc_ramp structure.
217 */
218void acc_ramp_start(struct acc_ramp *acc_ramp)
219{
220 /* Abort any previously running ramping process. */
221 acc_ramp_abort(acc_ramp);
222
223 /* Set all availble ACCs to barred and start ramping up. */
224 barr_all_enabled_accs(acc_ramp);
225 do_acc_ramping_step(acc_ramp);
226}
227
228/*!
229 * Abort the ramping process. If ramping is disabled or has already finished,
230 * then this function has no effect.
231 * \param[in] acc_ramp Pointer to acc_ramp structure.
232 */
233void acc_ramp_abort(struct acc_ramp *acc_ramp)
234{
235 if (osmo_timer_pending(&acc_ramp->step_timer))
236 osmo_timer_del(&acc_ramp->step_timer);
237}