blob: 8cb53027d244dd62e1e25128c172ab45d783b958 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
Holger Hans Peter Freyther416ce692013-08-22 08:44:04 +02004 * Copyright (C) 2013 by Holger Hans Peter Freyther
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <gprs_rlcmac.h>
22#include <gprs_bssgp_pcu.h>
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010023#include <gprs_bssgp_rim.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010025#include <gprs_debug.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020026#include <bts.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020027#include <tbf.h>
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020028#include <coding_scheme.h>
Max6dc90b82018-02-19 17:17:28 +010029#include <pdch.h>
Pau Espin Pedrolde7bb752020-01-23 12:37:15 +010030#include <decoding.h>
Max136ebcc2019-03-05 14:59:03 +010031
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010032#include <osmocom/gprs/gprs_ns2.h>
33#include <osmocom/gsm/protocol/gsm_23_003.h>
34#include <osmocom/gprs/protocol/gsm_08_16.h>
35#include <osmocom/core/utils.h>
Oliver Smith97839672021-07-05 11:11:42 +020036#include <osmocom/core/stats.h>
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010037#include <osmocom/gsm/gsm48.h>
38#include "coding_scheme.h"
39#include "tbf_dl.h"
40#include "llc.h"
41#include "gprs_rlcmac.h"
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010042#include "bts_pch_timer.h"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040043
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020044/* Tuning parameters for BSSGP flow control */
45#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
46#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
47#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
48#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
49
50/* Constants for BSSGP flow control */
51#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
52#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
53
Andreas Eversberg71cce912013-01-16 13:49:00 +010054extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020055extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010056extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040057
Oliver Smith97839672021-07-05 11:11:42 +020058static const struct rate_ctr_desc sgsn_ctr_description[] = {
59 [SGSN_CTR_RX_PAGING_CS] = { "rx_paging_cs", "Amount of paging CS requests received" },
60 [SGSN_CTR_RX_PAGING_PS] = { "rx_paging_ps", "Amount of paging PS requests received" },
61};
62
63static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
64 .group_name_prefix = "pcu:sgsn",
65 .group_description = "SGSN Statistics",
66 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
67 .num_ctr = ARRAY_SIZE(sgsn_ctr_description),
68 .ctr_desc = sgsn_ctr_description,
69};
70
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020071static void bvc_timeout(void *_priv);
72
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010073static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020074{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010075 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020076 uint8_t cap_len;
77 uint8_t *cap;
78
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010079 memset(rac, 0, sizeof(*rac));
80
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020081 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010082 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020083
84 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
85 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
86
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010087 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
88
Alexander Couzensccde5c92017-02-04 03:10:08 +010089 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020090 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010091
92 /* TS 24.008, 10.5.5.12a */
93 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020094
95 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010096 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020097}
98
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020099static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400100{
101 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400102
Andreas Eversberge6228b32012-07-03 13:36:03 +0200103 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700104 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200105 uint8_t *data;
106 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100107 uint8_t ms_class = 0;
108 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700109 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100110 MS_Radio_Access_capability_t rac;
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100111 const char *imsi = NULL;
112 struct osmo_mobile_identity mi_imsi;
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200113
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400114 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200115 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400116
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400117 /* LLC_PDU is mandatory IE */
118 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400119 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200120 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400121 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
122 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400123
Andreas Eversberge6228b32012-07-03 13:36:03 +0200124 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
125 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700126 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400127 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200128 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD IE_LLC_PDU too large\n", tlli);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200129 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400130 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400131
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200132 /* read IMSI. if no IMSI exists, use first paging block (any paging),
133 * because during attachment the IMSI might not be known, so the MS
134 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700135 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
136 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200137 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
138 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200139 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700140 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
141 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
142 }
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100143 imsi = &mi_imsi.imsi[0];
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700144 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200145
146 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100147 if (parse_ra_cap(tp, &rac) >= 0) {
148 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100149 ms_class = get_ms_class_by_capability(&rac);
150 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100151 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
152 ms_class, egprs_ms_class);
153 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200154
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200155 /* get lifetime */
156 uint16_t delay_csec = 0xffff;
157 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
158 {
159 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200160 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200161 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200162 else
163 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
164 "PDU_LIFETIME IE\n");
165 } else
166 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
167 "PDU_LIFETIME IE\n");
168
Jacob Erlbeck93990462015-05-15 15:50:43 +0200169 /* get optional TLLI old */
170 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
171 {
172 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200173 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200174 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200175 else
176 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
177 "TLLI (old) IE\n");
178 }
179
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100180 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n",
181 tlli, imsi ? : "none", len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400182
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100183 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, imsi, ms_class,
184 egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400185}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400186
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200187/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
188static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
189{
190 int rc;
191
192 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
193
194 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
195 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
196 return BSSGP_CAUSE_MISSING_MAND_IE;
197 }
198
199 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
200 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
201 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
202 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
203 return BSSGP_CAUSE_INV_MAND_INF;
204 }
205 req->mi_imsi_present = true;
206
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200207 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200208 req->mi_tmsi_present = false;
209 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
210 /* Be safe against an evil SGSN - check the length */
211 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
212 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
213 return BSSGP_CAUSE_COND_IE_ERR;
214 }
215
216 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
217 req->mi_tmsi = (struct osmo_mobile_identity){
218 .type = GSM_MI_TYPE_TMSI,
219 };
220 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
221 req->mi_tmsi_present = true;
222 }
223
224 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
225 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
226 else
227 req->tlli = GSM_RESERVED_TMSI;
228
229 return 0;
230}
231
232static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
233{
234 struct paging_req_cs req;
235 struct gprs_rlcmac_bts *bts;
236 struct GprsMs *ms;
237 int rc;
238
Oliver Smith97839672021-07-05 11:11:42 +0200239 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_CS));
240
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200241 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
242 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
243
244 /* We need to page all BTSs since even if a BTS has a matching MS, it
245 * may have already moved to a newer BTS. On Each BTS, if the MS is
246 * known, then bts_add_paging() can optimize and page only on PDCHs the
247 * target MS is using. */
248 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
249 /* TODO: Match by TMSI before IMSI if present?! */
250 ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
251 if (!ms && req.mi_imsi_present)
252 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
253 bts_add_paging(bts, &req, ms);
254 }
255
256 return 0;
257}
258
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700259/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200260static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700261{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700262 /* Use TMSI (if present) or IMSI */
263 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
264 /* Be safe against an evil SGSN - check the length */
265 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
266 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
267 return BSSGP_CAUSE_COND_IE_ERR;
268 }
269
270 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200271 *mi = (struct osmo_mobile_identity){
272 .type = GSM_MI_TYPE_TMSI,
273 };
274 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700275 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200276 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
277 true);
278 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
279 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
280 return BSSGP_CAUSE_COND_IE_ERR;
281 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700282 } else {
283 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
284 return BSSGP_CAUSE_MISSING_COND_IE;
285 }
286
287 return 0;
288}
289
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700290static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200291{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200292 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200293 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100294 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100295 uint16_t pgroup;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700296 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200297
Oliver Smith97839672021-07-05 11:11:42 +0200298 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_PS));
299
Maxf3038e72019-03-19 15:39:05 +0100300 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200301 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100302 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200303 }
304
Neels Hofmeyr00520512020-08-21 15:44:58 +0200305 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
306 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700307 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100308 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
309 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200310 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100311 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200312 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100313 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700314 }
Maxf3038e72019-03-19 15:39:05 +0100315
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200316 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700317 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100318
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100319 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
320 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
Pau Espin Pedrolef8a7302021-11-08 19:19:25 +0100321 if (bts_pch_timer_get_by_imsi(bts, mi_imsi.imsi)) {
322 LOGP(DBSSGP, LOGL_INFO, "PS-Paging request already pending for IMSI=%s\n", mi_imsi.imsi);
323 bts_do_rate_ctr_inc(bts, CTR_PCH_REQUESTS_ALREADY);
324 continue;
325 }
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100326 if (gprs_rlcmac_paging_request(bts, &paging_mi, pgroup) < 0)
327 continue;
328 bts_pch_timer_start(bts, &paging_mi, mi_imsi.imsi);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100329 }
330 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200331}
332
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400333/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200334static int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335{
336 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200337 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200338 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400339 unsigned rc = 0;
340
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200341 if (!bctx)
342 return -EINVAL;
343
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400344 /* If traffic is received on a BVC that is marked as blocked, the
345 * received PDU shall not be accepted and a STATUS PDU (Cause value:
346 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
347 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
348 {
349 uint16_t bvci = msgb_bvci(msg);
350 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
351 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
352 }
353
354 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200355 case BSSGP_PDUT_STATUS:
356 /* already handled in libosmogb */
357 OSMO_ASSERT(0);
358 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400359 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200360 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100361 if (the_pcu->bssgp.on_dl_unit_data)
362 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400363 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400364 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400365 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400366 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200367 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
368 bvci, bssgp_pdu_str(pdu_type));
369 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200370 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100371 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
372 break;
373 case BSSGP_PDUT_PAGING_PS:
374 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
375 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200376 case BSSGP_PDUT_RA_CAPABILITY:
377 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
378 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
379 bvci, bssgp_pdu_str(pdu_type));
380 break;
381 /* See TS 08.18 5.4.1 */
382 case BSSGP_PDUT_SUSPEND:
383 case BSSGP_PDUT_SUSPEND_ACK:
384 case BSSGP_PDUT_SUSPEND_NACK:
385 case BSSGP_PDUT_RESUME:
386 case BSSGP_PDUT_RESUME_ACK:
387 case BSSGP_PDUT_RESUME_NACK:
388 case BSSGP_PDUT_FLUSH_LL:
389 case BSSGP_PDUT_FLUSH_LL_ACK:
390 case BSSGP_PDUT_LLC_DISCARD:
391 case BSSGP_PDUT_BVC_BLOCK:
392 case BSSGP_PDUT_BVC_BLOCK_ACK:
393 case BSSGP_PDUT_BVC_UNBLOCK:
394 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
395 case BSSGP_PDUT_BVC_RESET:
396 case BSSGP_PDUT_BVC_RESET_ACK:
397 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
398 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
399 bctx->bvci, bssgp_pdu_str(pdu_type));
400 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400401 break;
402 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200403 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200404 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400405 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
406 break;
407 }
408 return rc;
409}
410
411/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200412static int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400413{
414 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200415 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400416 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700417 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200418 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400419 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200420 /* already handled in libosmogb */
421 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400422 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200423 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200424 case BSSGP_PDUT_RESUME_ACK:
425 case BSSGP_PDUT_BVC_BLOCK_ACK:
426 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
427 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200428 break;
429 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200430 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100431 if (!the_pcu->bssgp.bvc_sig_reset)
432 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200433 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100434 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200435 bvc_timeout(NULL);
436 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100437 case BSSGP_PDUT_PAGING_CS:
438 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
439 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200440 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200441 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
442 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200443 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200444 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100445 the_pcu->bssgp.bvc_unblocked = 1;
446 if (the_pcu->bssgp.on_unblock_ack)
447 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200448 bvc_timeout(NULL);
449 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200450 case BSSGP_PDUT_SUSPEND_NACK:
451 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200452 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200453 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200454 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
455 bvci, bssgp_pdu_str(pdu_type));
456 break;
457 /* See TS 08.18 5.4.1 */
458 case BSSGP_PDUT_UL_UNITDATA:
459 case BSSGP_PDUT_DL_UNITDATA:
460 case BSSGP_PDUT_RA_CAPABILITY:
461 case BSSGP_PDUT_PTM_UNITDATA:
462 case BSSGP_PDUT_RA_CAPA_UDPATE:
463 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
464 case BSSGP_PDUT_RADIO_STATUS:
465 case BSSGP_PDUT_FLOW_CONTROL_BVC:
466 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
467 case BSSGP_PDUT_FLOW_CONTROL_MS:
468 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
469 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
470 case BSSGP_PDUT_CREATE_BSS_PFC:
471 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
472 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
473 case BSSGP_PDUT_MODIFY_BSS_PFC:
474 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
475 case BSSGP_PDUT_DELETE_BSS_PFC:
476 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
477 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
478 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200479 break;
480 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200481 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200482 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200483 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
484 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400485 }
486 return rc;
487}
488
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200489static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400490{
491 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
492 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
493 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200494 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200495 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100496 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400497 int data_len;
498 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200499 struct bssgp_bvc_ctx *bctx;
500
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100501 switch (pdu_type) {
502 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100503 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200504 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100505 case BSSGP_PDUT_RAN_INFO:
506 case BSSGP_PDUT_RAN_INFO_REQ:
507 case BSSGP_PDUT_RAN_INFO_ACK:
508 case BSSGP_PDUT_RAN_INFO_ERROR:
509 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
510 /* Also pass all RIM related messages to the generic BSSGP
511 * parser so that it can deliver primitive to the RIM SAP
512 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100513 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100514 default:
515 break;
516 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400517
518 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
519
520 /* UNITDATA BSSGP headers have TLLI in front */
521 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
522 {
523 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
524 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
525 }
526 else
527 {
528 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
529 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
530 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200531 if (rc < 0) {
532 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
533 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200534 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200535 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400536
Max2813f932017-06-27 17:18:40 +0200537 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100538 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
539 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
540 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
541 }
542
543 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
544 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
545 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
546 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
547 }
548
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100549 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200550 }
551
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400552 /* look-up or create the BTS context for this BVC */
553 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
554
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700555 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400556 {
Max2813f932017-06-27 17:18:40 +0200557 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
558 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400559 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
560 }
561
562 if (bctx)
563 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100564 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200565 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
566 rate_ctr_add(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_BYTES_IN), msgb_bssgp_len(msg));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400567 }
568
569 if (ns_bvci == BVCI_SIGNALLING)
570 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200571 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400572 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
573 }
574 else if (ns_bvci == BVCI_PTM)
575 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200576 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400577 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
578 }
579 else
580 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100581 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400582 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
583 }
584 return rc;
585}
Harald Welte477e79e2012-06-18 12:21:03 +0800586
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100587static void handle_nm_status(struct osmo_bssgp_prim *bp)
588{
589 enum gprs_bssgp_cause cause;
590
591 LOGP(DPCU, LOGL_DEBUG,
592 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
593 bp->bvci, bp->nsei);
594
595 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
596 return;
597
598 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
599
600 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
601 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
602 return;
603
604 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
605 return;
606
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100607 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100608 LOGP(DPCU, LOGL_NOTICE,
609 "Received BSSGP STATUS message for an unknown BVCI (%d), "
610 "ignored\n",
611 bp->bvci);
612 return;
613 }
614
615 switch (cause) {
616 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100617 if (the_pcu->bssgp.bvc_unblocked) {
618 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100619 bvc_timeout(NULL);
620 }
621 break;
622
623 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100624 if (the_pcu->bssgp.bvc_reset) {
625 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100626 bvc_timeout(NULL);
627 }
628 break;
629 default:
630 break;
631 }
632}
633
Harald Welte477e79e2012-06-18 12:21:03 +0800634int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
635{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100636 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100637 int rc;
638 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100639 bp = container_of(oph, struct osmo_bssgp_prim, oph);
640
641 switch (oph->sap) {
642 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100643 switch (oph->primitive) {
644 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100645 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100646 break;
647 case PRIM_NM_BVC_RESET:
648 /* received a BVC PTP reset */
649 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
650 /* Rx Reset from SGSN */
651 if (bp->bvci == BVCI_SIGNALLING) {
652 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
653 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
654 else {
655 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
656 break;
657 }
658
659 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
660 if (rc < 0) {
661 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
662 break;
663 }
664 the_pcu->bssgp.bvc_sig_reset = 1;
665 the_pcu->bssgp.bvc_reset = 0;
666 the_pcu->bssgp.bvc_unblocked = 0;
667 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
668 the_pcu->bssgp.bvc_reset = 1;
669 the_pcu->bssgp.bvc_unblocked = 0;
670 bvc_timeout(NULL);
671 }
672 break;
673 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100674 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100675 case SAP_BSSGP_RIM:
676 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100677 default:
678 break;
679 }
Harald Welte477e79e2012-06-18 12:21:03 +0800680 return 0;
681}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200682
Alexander Couzens290d9032020-09-16 21:52:02 +0200683void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200684{
Alexander Couzens290d9032020-09-16 21:52:02 +0200685 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100686 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200687 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200688 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100689 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200690 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100691 if (!the_pcu->bssgp.nsvc_unblocked) {
692 the_pcu->bssgp.bvc_sig_reset = 0;
693 the_pcu->bssgp.bvc_reset = 0;
694 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200695 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200696 }
697 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100698 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200699 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100700 if (the_pcu->bssgp.nsvc_unblocked) {
701 the_pcu->bssgp.nsvc_unblocked = 0;
702 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
703 the_pcu->bssgp.bvc_sig_reset = 0;
704 the_pcu->bssgp.bvc_reset = 0;
705 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200706 }
707 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100708 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100709 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200710 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100711 LOGP(DPCU, LOGL_DEBUG,
712 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200713 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100714 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200715 break;
716 }
717}
718
719/* called by the ns layer */
720int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
721{
722 struct osmo_gprs_ns2_prim *nsp;
723 int rc = 0;
724
725 if (oph->sap != SAP_NS)
726 return 0;
727
728 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
729
730 if (oph->operation != PRIM_OP_INDICATION) {
731 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
732 get_value_string(osmo_prim_op_names, oph->operation),
733 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100734 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200735 }
736
737 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100738 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200739 /* hand the message into the BSSGP implementation */
740 /* add required msg fields for Gb layer */
741 msgb_bssgph(oph->msg) = oph->msg->l3h;
742 msgb_bvci(oph->msg) = nsp->bvci;
743 msgb_nsei(oph->msg) = nsp->nsei;
744 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
745 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100746 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200747 gprs_ns_prim_status_cb(nsp);
748 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100749 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100750 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200751 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100752 LOGP(DPCU, LOGL_DEBUG,
753 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200754 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100755 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200756 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200757 }
758
Alexander Couzensa2848542020-10-26 00:33:31 +0100759out:
760 if (oph->msg)
761 msgb_free(oph->msg);
762
Alexander Couzens290d9032020-09-16 21:52:02 +0200763 return rc;
764}
765
766/* called by the bssgp layer to send NS PDUs */
767int gprs_gp_send_cb(void *ctx, struct msgb *msg)
768{
769 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
770 struct osmo_gprs_ns2_prim nsp = {};
771 nsp.nsei = msgb_nsei(msg);
772 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100773 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200774 PRIM_OP_REQUEST, msg);
775 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200776}
777
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200778static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
779{
780 size_t trx_no, ts_no;
781 unsigned num_pdch = 0;
782
783 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
784 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
785
786 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
787 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
788
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100789 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200790 num_pdch += 1;
791 }
792 }
793
794 return num_pdch;
795}
796
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200797static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200798{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200799 int bytes_per_rlc_block = mcs_max_data_block_bytes(cs) * num_data_blocks(mcs_header_type(cs));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200800
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100801 /* n byte payload per 20ms */
802 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200803}
804
805static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
806 uint32_t leak_rate, uint32_t fallback)
807{
808 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100809 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200810
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200811 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100812 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200813
814 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200815 bucket_size = FC_MAX_BUCKET_SIZE;
816
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200817 if (bucket_size == 0 && bucket_time && leak_rate)
818 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200819
820 if (bucket_size == 0 && leak_rate)
821 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
822
823 if (bucket_size == 0)
824 bucket_size = fallback;
825
826 if (bucket_size > FC_MAX_BUCKET_SIZE)
827 bucket_size = FC_MAX_BUCKET_SIZE;
828
829 return bucket_size;
830}
831
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200832static uint32_t get_and_reset_avg_queue_delay(void)
833{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100834 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200835 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100836 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200837 uint32_t avg_delay_ms = 0;
838
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100839 if (the_pcu->bssgp.queue_delay_count > 0)
840 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200841
842 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100843 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100844 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200845
846 return avg_delay_ms;
847}
848
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200849static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
850{
851 int rate; /* byte per second */
852
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100853 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200854 return -1;
855
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100856 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200857 return -1;
858
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100859 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
860 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200861
862 /* 20ms/num_pdch is the average RLC block duration, so the rate is
863 * calculated as:
864 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100865 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
866 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200867
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100868 the_pcu->bssgp.queue_frames_sent = 0;
869 the_pcu->bssgp.queue_bytes_recv = 0;
870 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200871
872 return rate;
873}
874
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200875static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100876{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100877 int num = 0;
878 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100879 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100880
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100881 /* First check if we support any MCS: */
882 for (i = 8; i >= 0; i--) {
883 if (bts->mcs_mask & (1 << i)) {
884 num = i + 1;
885 mcs_any = true;
886 break;
887 }
888 }
889
890 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100891 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100892 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100893 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100894 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100895 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100896 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100897 } else if (bts_max_mcs_dl(bts)) {
898 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100899 } else {
900 num = 9;
901 }
902
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100903 if (num)
904 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100905 }
906
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100907 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100908 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100909 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100910 } else {
911 for (i = 3; i >= 0; i--) {
912 if (bts->cs_mask & (1 << i)) {
913 num = i + 1;
914 break;
915 }
916 }
917 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100918 } else if (bts_max_cs_dl(bts)) {
919 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100920 }
921
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100922 if (!num)
923 num = 4;
924
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200925 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100926}
927
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100928static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200929{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200930 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200931 uint32_t bucket_size; /* oct */
932 uint32_t ms_bucket_size; /* oct */
933 uint32_t leak_rate; /* oct/s */
934 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200935 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200936 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200937 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200938
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100939 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200940 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
941 return -EIO;
942 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100943
944 /* FIXME: This calculation needs to be redone to support multiple BTS */
945 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
946 if (!bts) {
947 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
948 return -EIO;
949 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200950
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100951 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200952
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100953 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
954 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
955 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
956 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200957
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100958 /* FIXME: This calculation is mostly wrong. It should be done based on
959 currently established TBF (and whether the related (egprs)_ms_class
960 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200961 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200962 int meas_rate;
963 int usage; /* in 0..1000 */
964
965 if (num_pdch < 0)
966 num_pdch = count_pdch(bts);
967
968 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
969 if (meas_rate > 0) {
970 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
971 leak_rate =
972 (meas_rate * usage + leak_rate * (1000 - usage)) /
973 1000;
974 LOGP(DBSSGP, LOGL_DEBUG,
975 "Estimated BVC leak rate = %d "
976 "(measured %d, usage %d%%)\n",
977 leak_rate, meas_rate, usage/10);
978 }
979 }
980
981 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200982 if (num_pdch < 0)
983 num_pdch = count_pdch(bts);
984
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200985 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200986
987 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100988 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100989 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200990 };
991
992 if (ms_leak_rate == 0) {
993 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100994 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200995
996 if (num_pdch < 0)
997 num_pdch = count_pdch(bts);
998
999 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001000 if (max_pdch > FC_MS_MAX_RX_SLOTS)
1001 max_pdch = FC_MS_MAX_RX_SLOTS;
1002 if (ms_num_pdch > max_pdch)
1003 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001004
Jacob Erlbeck6eed1912015-07-21 18:29:46 +02001005 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001006
1007 /* TODO: To properly support multiple TRX, the per MS leak rate
1008 * should be derived from the max number of PDCH TS per TRX.
1009 */
1010 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +01001011 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
1012 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +01001013 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001014 };
1015
1016 /* TODO: Force leak_rate to 0 on buffer bloat */
1017
1018 if (bucket_size == 0)
1019 bucket_size = compute_bucket_size(bts, leak_rate,
1020 FC_FALLBACK_BVC_BUCKET_SIZE);
1021
1022 if (ms_bucket_size == 0)
1023 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1024 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1025
1026 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1027 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1028
1029 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1030 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1031
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001032 /* Avg queue delay monitoring */
1033 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001034
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001035 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001036 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001037
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001038 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001039 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1040 "R_MS = %d, avg_dly = %d\n",
1041 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1042 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001043
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001044 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001045 bucket_size, leak_rate,
1046 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001047 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001048}
1049
1050static void bvc_timeout(void *_priv)
1051{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001052 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001053 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001054 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001055 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001056 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001057 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001058 return;
1059 }
1060
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001061 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001062 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001063 the_pcu->bssgp.bctx->bvci);
1064 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, the_pcu->bssgp.bctx->bvci, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001065 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001066 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001067 return;
1068 }
1069
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001070 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001071 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001072 the_pcu->bssgp.bctx->bvci);
1073 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001074 secs = osmo_tdef_get(the_pcu->T_defs, -101, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001075 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001076 return;
1077 }
1078
1079 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001080 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001081 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001082 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001083}
1084
Alexander Couzens13a12e22021-02-15 05:50:10 +01001085/*! configure NS layer
1086 *
1087 * \param bts pointer to the bts object
1088 * \param nsei the NSEI of the BSS
1089 * \param local pointer to an array of local address to bind on.
1090 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1091 * \param nsvci pointer to an array of nsvcis
1092 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1093 * \returns 0 if the configuration has succeeded. on error != 0
1094 */
1095static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1096 uint16_t nsei,
1097 const struct osmo_sockaddr *local,
1098 const struct osmo_sockaddr *remote,
1099 const uint16_t *nsvci,
1100 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001101{
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001102 unsigned int i;
1103 int rc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001104 uint16_t binds = 0;
1105 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001106 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001107 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001108 char name[16];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001109 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001110
1111 if (!valid)
1112 return -1;
1113
Alexander Couzens13a12e22021-02-15 05:50:10 +01001114 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1115 if (!bts->nse)
1116 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1117 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1118 if (!bts->nse) {
1119 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1120 return -1;
1121 }
1122
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001123 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1124 if (!(valid & (1 << i)))
1125 continue;
1126
Daniel Willmanndd28f822021-10-29 17:34:24 +02001127 bind[i] = gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i]);
1128 if (!bind[i]) {
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001129 snprintf(name, sizeof(name), "pcu%u", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001130 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001131 if (rc < 0) {
1132 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1133 continue;
1134 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001135
1136 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1137 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1138 if (rc < 0) {
1139 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1140 continue;
1141 }
1142 }
Harald Welted9367e32021-04-29 22:02:47 +02001143
1144 if (the_pcu->vty.ns_ip_dscp != -1)
1145 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1146 if (the_pcu->vty.ns_priority != -1)
1147 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001148 }
1149
1150 binds |= 1 << i;
1151 }
1152
1153 if (!binds) {
1154 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001155 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001156 return -1;
1157 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001158
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001159 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1160 if (!(binds & (1 << i)))
1161 continue;
1162
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001163 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001164 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001165 if (rc && rc != -EALREADY) {
1166 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001167 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001168 } else {
1169 sns_configured = true;
1170 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001171 } else {
1172 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1173 if (nsvc)
1174 nsvcs = true;
1175 else
1176 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1177 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001178 }
1179
Alexander Couzens13a12e22021-02-15 05:50:10 +01001180 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1181 return sns_configured ? 0 : -1;
1182 else
1183 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001184}
1185
1186struct nsvc_cb {
1187 const struct osmo_sockaddr *local;
1188 const struct osmo_sockaddr *remote;
1189 const uint16_t *nsvci;
1190 /* [in] bitmask of valid nsvc in local/remote */
1191 uint16_t valid;
1192 /* [out] bitmask of found nsvcs */
1193 uint16_t found;
1194};
1195
1196static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1197{
1198 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001199 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001200
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001201 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001202 if (!(data->valid & (1 << i)))
1203 continue;
1204 if (data->found & (1 << i))
1205 continue;
1206
1207 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1208 &data->remote[i],
1209 data->nsvci[i])) {
1210 data->found |= 1 << i;
1211 return 0;
1212 }
1213 }
1214
1215 /* Found an extra nsvc */
1216 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1217 gprs_ns2_free_nsvc(nsvc);
1218
1219 return 0;
1220}
1221
Alexander Couzens94a367f2021-02-16 16:57:15 +01001222/* update the ns configuration if needed */
1223int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1224 const struct osmo_sockaddr *local,
1225 const struct osmo_sockaddr *remote,
1226 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001227{
1228 int rc = 0;
1229 if (!bts->nse) {
1230 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001231 gprs_ns2_free_nses(the_pcu->nsi);
1232 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001233 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001234 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1235 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001236 gprs_ns2_free_nses(the_pcu->nsi);
1237 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001238 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001239 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001240 /* SNS: check if the initial nsvc is the same, if not recreate it */
1241 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001242 unsigned int i;
1243 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001244 if (!(valid & (1 << i)))
1245 continue;
1246
1247 /* found the initial - everything should be fine */
1248 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1249 return 0;
1250 }
1251
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001252 gprs_ns2_free_nses(the_pcu->nsi);
1253 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001254 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001255 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001256 /* check if all NSVC are still the same. */
1257 struct nsvc_cb data = {
1258 .local = &local[0],
1259 .remote = &remote[0],
1260 .nsvci = &nsvci[0],
1261 .valid = valid,
1262 .found = 0,
1263 };
1264
1265 /* search the current active nsvcs */
1266 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1267
1268 /* we found all our valid nsvcs and might have removed all other nsvcs */
1269 if (valid == data.found)
1270 return 0;
1271
1272 /* remove all found nsvcs from the valid field */
1273 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001274 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001275 }
1276
1277 if (rc)
1278 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1279
1280 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001281}
1282
Alexander Couzens290d9032020-09-16 21:52:02 +02001283struct gprs_bssgp_pcu *gprs_bssgp_init(
1284 struct gprs_rlcmac_bts *bts,
1285 uint16_t nsei, uint16_t bvci,
1286 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1287 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001288{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001289
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001290 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001291 if (the_pcu->bssgp.bctx)
1292 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001293
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001294 the_pcu->bssgp.bts = bts;
1295 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1296 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001297 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001298 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001299 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001300 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001301 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001302 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001303 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001304 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1305 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001306 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001307 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1308 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001309 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001310 the_pcu->bssgp.bctx->ra_id.lac = lac;
1311 the_pcu->bssgp.bctx->ra_id.rac = rac;
1312 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001313
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001314 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001315
Oliver Smith97839672021-07-05 11:11:42 +02001316 the_pcu->bssgp.ctrs = rate_ctr_group_alloc(the_pcu, &sgsn_ctrg_desc, 0);
1317 OSMO_ASSERT(the_pcu->bssgp.ctrs)
1318
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001319 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001320}
1321
Alexander Couzens290d9032020-09-16 21:52:02 +02001322void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001323{
Oliver Smith97839672021-07-05 11:11:42 +02001324 rate_ctr_group_free(the_pcu->bssgp.ctrs);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001325 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001326
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001327 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001328 the_pcu->bssgp.nsvc_unblocked = 0;
1329 the_pcu->bssgp.bvc_sig_reset = 0;
1330 the_pcu->bssgp.bvc_reset = 0;
1331 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001332
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001333 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1334 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001335
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001336 gprs_ns2_free(the_pcu->nsi);
1337 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001338 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001339}
1340
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001341void gprs_bssgp_update_frames_sent()
1342{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001343 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001344}
1345
1346void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1347{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001348 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1349 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001350}
1351
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001352void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1353 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001354{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001355 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001356 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001357
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001358 timespecsub(tv_now, tv_recv, &tv_delay);
1359 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001360
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001361 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001362}