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