blob: 1fcacdb126744b6eb62445d16e7d2b418aa723f4 [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"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040042
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020043/* Tuning parameters for BSSGP flow control */
44#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
45#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
46#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
47#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
48
49/* Constants for BSSGP flow control */
50#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
51#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
52
Andreas Eversberg71cce912013-01-16 13:49:00 +010053extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020054extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010055extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040056
Oliver Smith97839672021-07-05 11:11:42 +020057static const struct rate_ctr_desc sgsn_ctr_description[] = {
58 [SGSN_CTR_RX_PAGING_CS] = { "rx_paging_cs", "Amount of paging CS requests received" },
59 [SGSN_CTR_RX_PAGING_PS] = { "rx_paging_ps", "Amount of paging PS requests received" },
60};
61
62static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
63 .group_name_prefix = "pcu:sgsn",
64 .group_description = "SGSN Statistics",
65 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
66 .num_ctr = ARRAY_SIZE(sgsn_ctr_description),
67 .ctr_desc = sgsn_ctr_description,
68};
69
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020070static void bvc_timeout(void *_priv);
71
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010072static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020073{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010074 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020075 uint8_t cap_len;
76 uint8_t *cap;
77
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010078 memset(rac, 0, sizeof(*rac));
79
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020080 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010081 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020082
83 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
84 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
85
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010086 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
87
Alexander Couzensccde5c92017-02-04 03:10:08 +010088 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020089 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010090
91 /* TS 24.008, 10.5.5.12a */
92 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020093
94 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010095 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020096}
97
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020098static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040099{
100 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400101
Andreas Eversberge6228b32012-07-03 13:36:03 +0200102 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700103 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200104 uint8_t *data;
105 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100106 uint8_t ms_class = 0;
107 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700108 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100109 MS_Radio_Access_capability_t rac;
Neels Hofmeyr00520512020-08-21 15:44:58 +0200110 /* TODO: is it really necessary to initialize this as a "000" IMSI? It seems, the function should just return an
111 * error if no IMSI IE was found. */
112 struct osmo_mobile_identity mi_imsi = {
113 .type = GSM_MI_TYPE_TMSI,
114 };
115 OSMO_STRLCPY_ARRAY(mi_imsi.imsi, "000");
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200116
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400117 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200118 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400119
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400120 /* LLC_PDU is mandatory IE */
121 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400122 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200123 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400124 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
125 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400126
Andreas Eversberge6228b32012-07-03 13:36:03 +0200127 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
128 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700129 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400130 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200131 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 +0200132 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400133 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400134
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200135 /* read IMSI. if no IMSI exists, use first paging block (any paging),
136 * because during attachment the IMSI might not be known, so the MS
137 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700138 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
139 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200140 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
141 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200142 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700143 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
144 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
145 }
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700146 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200147
148 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100149 if (parse_ra_cap(tp, &rac) >= 0) {
150 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100151 ms_class = get_ms_class_by_capability(&rac);
152 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100153 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
154 ms_class, egprs_ms_class);
155 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200156
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200157 /* get lifetime */
158 uint16_t delay_csec = 0xffff;
159 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
160 {
161 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200162 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200163 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200164 else
165 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
166 "PDU_LIFETIME IE\n");
167 } else
168 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
169 "PDU_LIFETIME IE\n");
170
Jacob Erlbeck93990462015-05-15 15:50:43 +0200171 /* get optional TLLI old */
172 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
173 {
174 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200175 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200176 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200177 else
178 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
179 "TLLI (old) IE\n");
180 }
181
Neels Hofmeyr00520512020-08-21 15:44:58 +0200182 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n", tlli, mi_imsi.imsi, len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400183
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100184 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, mi_imsi.imsi,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100185 ms_class, egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400186}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400187
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200188/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
189static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
190{
191 int rc;
192
193 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
194
195 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
196 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
197 return BSSGP_CAUSE_MISSING_MAND_IE;
198 }
199
200 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
201 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
202 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
203 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
204 return BSSGP_CAUSE_INV_MAND_INF;
205 }
206 req->mi_imsi_present = true;
207
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200208 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200209 req->mi_tmsi_present = false;
210 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
211 /* Be safe against an evil SGSN - check the length */
212 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
213 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
214 return BSSGP_CAUSE_COND_IE_ERR;
215 }
216
217 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
218 req->mi_tmsi = (struct osmo_mobile_identity){
219 .type = GSM_MI_TYPE_TMSI,
220 };
221 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
222 req->mi_tmsi_present = true;
223 }
224
225 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
226 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
227 else
228 req->tlli = GSM_RESERVED_TMSI;
229
230 return 0;
231}
232
233static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
234{
235 struct paging_req_cs req;
236 struct gprs_rlcmac_bts *bts;
237 struct GprsMs *ms;
238 int rc;
239
Oliver Smith97839672021-07-05 11:11:42 +0200240 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_CS));
241
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200242 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
243 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
244
245 /* We need to page all BTSs since even if a BTS has a matching MS, it
246 * may have already moved to a newer BTS. On Each BTS, if the MS is
247 * known, then bts_add_paging() can optimize and page only on PDCHs the
248 * target MS is using. */
249 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
250 /* TODO: Match by TMSI before IMSI if present?! */
251 ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
252 if (!ms && req.mi_imsi_present)
253 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
254 bts_add_paging(bts, &req, ms);
255 }
256
257 return 0;
258}
259
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700260/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200261static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700262{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700263 /* Use TMSI (if present) or IMSI */
264 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
265 /* Be safe against an evil SGSN - check the length */
266 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
267 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
268 return BSSGP_CAUSE_COND_IE_ERR;
269 }
270
271 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200272 *mi = (struct osmo_mobile_identity){
273 .type = GSM_MI_TYPE_TMSI,
274 };
275 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700276 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200277 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
278 true);
279 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
280 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
281 return BSSGP_CAUSE_COND_IE_ERR;
282 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700283 } else {
284 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
285 return BSSGP_CAUSE_MISSING_COND_IE;
286 }
287
288 return 0;
289}
290
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700291static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200292{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200293 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200294 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100295 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100296 uint16_t pgroup;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700297 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200298
Oliver Smith97839672021-07-05 11:11:42 +0200299 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_PS));
300
Maxf3038e72019-03-19 15:39:05 +0100301 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200302 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100303 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200304 }
305
Neels Hofmeyr00520512020-08-21 15:44:58 +0200306 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
307 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700308 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100309 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
310 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200311 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100312 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200313 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100314 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700315 }
Maxf3038e72019-03-19 15:39:05 +0100316
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200317 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700318 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100319
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100320 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
321 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
322 gprs_rlcmac_paging_request(bts, &paging_mi, pgroup);
323 }
324 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200325}
326
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400327/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200328static 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 +0400329{
330 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200331 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200332 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400333 unsigned rc = 0;
334
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200335 if (!bctx)
336 return -EINVAL;
337
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400338 /* If traffic is received on a BVC that is marked as blocked, the
339 * received PDU shall not be accepted and a STATUS PDU (Cause value:
340 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
341 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
342 {
343 uint16_t bvci = msgb_bvci(msg);
344 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
345 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
346 }
347
348 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200349 case BSSGP_PDUT_STATUS:
350 /* already handled in libosmogb */
351 OSMO_ASSERT(0);
352 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400353 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200354 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100355 if (the_pcu->bssgp.on_dl_unit_data)
356 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400357 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400358 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400359 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400360 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200361 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
362 bvci, bssgp_pdu_str(pdu_type));
363 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200364 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100365 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
366 break;
367 case BSSGP_PDUT_PAGING_PS:
368 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
369 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200370 case BSSGP_PDUT_RA_CAPABILITY:
371 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
372 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
373 bvci, bssgp_pdu_str(pdu_type));
374 break;
375 /* See TS 08.18 5.4.1 */
376 case BSSGP_PDUT_SUSPEND:
377 case BSSGP_PDUT_SUSPEND_ACK:
378 case BSSGP_PDUT_SUSPEND_NACK:
379 case BSSGP_PDUT_RESUME:
380 case BSSGP_PDUT_RESUME_ACK:
381 case BSSGP_PDUT_RESUME_NACK:
382 case BSSGP_PDUT_FLUSH_LL:
383 case BSSGP_PDUT_FLUSH_LL_ACK:
384 case BSSGP_PDUT_LLC_DISCARD:
385 case BSSGP_PDUT_BVC_BLOCK:
386 case BSSGP_PDUT_BVC_BLOCK_ACK:
387 case BSSGP_PDUT_BVC_UNBLOCK:
388 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
389 case BSSGP_PDUT_BVC_RESET:
390 case BSSGP_PDUT_BVC_RESET_ACK:
391 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
392 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
393 bctx->bvci, bssgp_pdu_str(pdu_type));
394 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400395 break;
396 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200397 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200398 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400399 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
400 break;
401 }
402 return rc;
403}
404
405/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200406static 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 +0400407{
408 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200409 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400410 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700411 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200412 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400413 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200414 /* already handled in libosmogb */
415 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400416 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200417 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200418 case BSSGP_PDUT_RESUME_ACK:
419 case BSSGP_PDUT_BVC_BLOCK_ACK:
420 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
421 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200422 break;
423 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200424 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100425 if (!the_pcu->bssgp.bvc_sig_reset)
426 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200427 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100428 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200429 bvc_timeout(NULL);
430 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100431 case BSSGP_PDUT_PAGING_CS:
432 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
433 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200434 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200435 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
436 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200437 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200438 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100439 the_pcu->bssgp.bvc_unblocked = 1;
440 if (the_pcu->bssgp.on_unblock_ack)
441 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200442 bvc_timeout(NULL);
443 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200444 case BSSGP_PDUT_SUSPEND_NACK:
445 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200446 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200447 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200448 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
449 bvci, bssgp_pdu_str(pdu_type));
450 break;
451 /* See TS 08.18 5.4.1 */
452 case BSSGP_PDUT_UL_UNITDATA:
453 case BSSGP_PDUT_DL_UNITDATA:
454 case BSSGP_PDUT_RA_CAPABILITY:
455 case BSSGP_PDUT_PTM_UNITDATA:
456 case BSSGP_PDUT_RA_CAPA_UDPATE:
457 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
458 case BSSGP_PDUT_RADIO_STATUS:
459 case BSSGP_PDUT_FLOW_CONTROL_BVC:
460 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
461 case BSSGP_PDUT_FLOW_CONTROL_MS:
462 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
463 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
464 case BSSGP_PDUT_CREATE_BSS_PFC:
465 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
466 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
467 case BSSGP_PDUT_MODIFY_BSS_PFC:
468 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
469 case BSSGP_PDUT_DELETE_BSS_PFC:
470 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
471 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
472 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200473 break;
474 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200475 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200476 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200477 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
478 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400479 }
480 return rc;
481}
482
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200483static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400484{
485 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
486 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
487 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200488 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200489 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100490 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400491 int data_len;
492 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200493 struct bssgp_bvc_ctx *bctx;
494
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100495 switch (pdu_type) {
496 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100497 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200498 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100499 case BSSGP_PDUT_RAN_INFO:
500 case BSSGP_PDUT_RAN_INFO_REQ:
501 case BSSGP_PDUT_RAN_INFO_ACK:
502 case BSSGP_PDUT_RAN_INFO_ERROR:
503 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
504 /* Also pass all RIM related messages to the generic BSSGP
505 * parser so that it can deliver primitive to the RIM SAP
506 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100507 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100508 default:
509 break;
510 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400511
512 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
513
514 /* UNITDATA BSSGP headers have TLLI in front */
515 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
516 {
517 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
518 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
519 }
520 else
521 {
522 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
523 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
524 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200525 if (rc < 0) {
526 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
527 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200528 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200529 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400530
Max2813f932017-06-27 17:18:40 +0200531 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100532 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
533 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
534 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
535 }
536
537 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
538 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
539 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
540 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
541 }
542
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100543 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200544 }
545
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400546 /* look-up or create the BTS context for this BVC */
547 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
548
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700549 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400550 {
Max2813f932017-06-27 17:18:40 +0200551 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
552 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400553 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
554 }
555
556 if (bctx)
557 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100558 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200559 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
560 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 +0400561 }
562
563 if (ns_bvci == BVCI_SIGNALLING)
564 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200565 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400566 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
567 }
568 else if (ns_bvci == BVCI_PTM)
569 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200570 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400571 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
572 }
573 else
574 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100575 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400576 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
577 }
578 return rc;
579}
Harald Welte477e79e2012-06-18 12:21:03 +0800580
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100581static void handle_nm_status(struct osmo_bssgp_prim *bp)
582{
583 enum gprs_bssgp_cause cause;
584
585 LOGP(DPCU, LOGL_DEBUG,
586 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
587 bp->bvci, bp->nsei);
588
589 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
590 return;
591
592 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
593
594 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
595 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
596 return;
597
598 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
599 return;
600
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100601 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100602 LOGP(DPCU, LOGL_NOTICE,
603 "Received BSSGP STATUS message for an unknown BVCI (%d), "
604 "ignored\n",
605 bp->bvci);
606 return;
607 }
608
609 switch (cause) {
610 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100611 if (the_pcu->bssgp.bvc_unblocked) {
612 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100613 bvc_timeout(NULL);
614 }
615 break;
616
617 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100618 if (the_pcu->bssgp.bvc_reset) {
619 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100620 bvc_timeout(NULL);
621 }
622 break;
623 default:
624 break;
625 }
626}
627
Harald Welte477e79e2012-06-18 12:21:03 +0800628int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
629{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100630 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100631 int rc;
632 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100633 bp = container_of(oph, struct osmo_bssgp_prim, oph);
634
635 switch (oph->sap) {
636 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100637 switch (oph->primitive) {
638 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100639 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100640 break;
641 case PRIM_NM_BVC_RESET:
642 /* received a BVC PTP reset */
643 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
644 /* Rx Reset from SGSN */
645 if (bp->bvci == BVCI_SIGNALLING) {
646 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
647 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
648 else {
649 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
650 break;
651 }
652
653 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
654 if (rc < 0) {
655 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
656 break;
657 }
658 the_pcu->bssgp.bvc_sig_reset = 1;
659 the_pcu->bssgp.bvc_reset = 0;
660 the_pcu->bssgp.bvc_unblocked = 0;
661 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
662 the_pcu->bssgp.bvc_reset = 1;
663 the_pcu->bssgp.bvc_unblocked = 0;
664 bvc_timeout(NULL);
665 }
666 break;
667 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100668 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100669 case SAP_BSSGP_RIM:
670 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100671 default:
672 break;
673 }
Harald Welte477e79e2012-06-18 12:21:03 +0800674 return 0;
675}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200676
Alexander Couzens290d9032020-09-16 21:52:02 +0200677void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200678{
Alexander Couzens290d9032020-09-16 21:52:02 +0200679 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100680 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200681 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200682 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100683 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200684 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100685 if (!the_pcu->bssgp.nsvc_unblocked) {
686 the_pcu->bssgp.bvc_sig_reset = 0;
687 the_pcu->bssgp.bvc_reset = 0;
688 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200689 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200690 }
691 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100692 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200693 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100694 if (the_pcu->bssgp.nsvc_unblocked) {
695 the_pcu->bssgp.nsvc_unblocked = 0;
696 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
697 the_pcu->bssgp.bvc_sig_reset = 0;
698 the_pcu->bssgp.bvc_reset = 0;
699 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200700 }
701 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100702 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100703 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200704 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100705 LOGP(DPCU, LOGL_DEBUG,
706 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200707 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100708 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200709 break;
710 }
711}
712
713/* called by the ns layer */
714int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
715{
716 struct osmo_gprs_ns2_prim *nsp;
717 int rc = 0;
718
719 if (oph->sap != SAP_NS)
720 return 0;
721
722 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
723
724 if (oph->operation != PRIM_OP_INDICATION) {
725 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
726 get_value_string(osmo_prim_op_names, oph->operation),
727 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100728 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200729 }
730
731 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100732 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200733 /* hand the message into the BSSGP implementation */
734 /* add required msg fields for Gb layer */
735 msgb_bssgph(oph->msg) = oph->msg->l3h;
736 msgb_bvci(oph->msg) = nsp->bvci;
737 msgb_nsei(oph->msg) = nsp->nsei;
738 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
739 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100740 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200741 gprs_ns_prim_status_cb(nsp);
742 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100743 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100744 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200745 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100746 LOGP(DPCU, LOGL_DEBUG,
747 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200748 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100749 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200750 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200751 }
752
Alexander Couzensa2848542020-10-26 00:33:31 +0100753out:
754 if (oph->msg)
755 msgb_free(oph->msg);
756
Alexander Couzens290d9032020-09-16 21:52:02 +0200757 return rc;
758}
759
760/* called by the bssgp layer to send NS PDUs */
761int gprs_gp_send_cb(void *ctx, struct msgb *msg)
762{
763 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
764 struct osmo_gprs_ns2_prim nsp = {};
765 nsp.nsei = msgb_nsei(msg);
766 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100767 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200768 PRIM_OP_REQUEST, msg);
769 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200770}
771
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200772static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
773{
774 size_t trx_no, ts_no;
775 unsigned num_pdch = 0;
776
777 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
778 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
779
780 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
781 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
782
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100783 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200784 num_pdch += 1;
785 }
786 }
787
788 return num_pdch;
789}
790
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200791static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200792{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200793 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 +0200794
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100795 /* n byte payload per 20ms */
796 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200797}
798
799static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
800 uint32_t leak_rate, uint32_t fallback)
801{
802 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100803 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200804
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200805 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100806 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200807
808 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200809 bucket_size = FC_MAX_BUCKET_SIZE;
810
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200811 if (bucket_size == 0 && bucket_time && leak_rate)
812 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200813
814 if (bucket_size == 0 && leak_rate)
815 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
816
817 if (bucket_size == 0)
818 bucket_size = fallback;
819
820 if (bucket_size > FC_MAX_BUCKET_SIZE)
821 bucket_size = FC_MAX_BUCKET_SIZE;
822
823 return bucket_size;
824}
825
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200826static uint32_t get_and_reset_avg_queue_delay(void)
827{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100828 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200829 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100830 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200831 uint32_t avg_delay_ms = 0;
832
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100833 if (the_pcu->bssgp.queue_delay_count > 0)
834 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200835
836 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100837 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100838 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200839
840 return avg_delay_ms;
841}
842
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200843static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
844{
845 int rate; /* byte per second */
846
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100847 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200848 return -1;
849
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100850 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200851 return -1;
852
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100853 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
854 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200855
856 /* 20ms/num_pdch is the average RLC block duration, so the rate is
857 * calculated as:
858 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100859 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
860 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200861
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100862 the_pcu->bssgp.queue_frames_sent = 0;
863 the_pcu->bssgp.queue_bytes_recv = 0;
864 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200865
866 return rate;
867}
868
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200869static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100870{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100871 int num = 0;
872 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100873 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100874
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100875 /* First check if we support any MCS: */
876 for (i = 8; i >= 0; i--) {
877 if (bts->mcs_mask & (1 << i)) {
878 num = i + 1;
879 mcs_any = true;
880 break;
881 }
882 }
883
884 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100885 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100886 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100887 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100888 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100889 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100890 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100891 } else if (bts_max_mcs_dl(bts)) {
892 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100893 } else {
894 num = 9;
895 }
896
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100897 if (num)
898 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100899 }
900
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100901 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100902 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100903 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100904 } else {
905 for (i = 3; i >= 0; i--) {
906 if (bts->cs_mask & (1 << i)) {
907 num = i + 1;
908 break;
909 }
910 }
911 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100912 } else if (bts_max_cs_dl(bts)) {
913 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100914 }
915
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100916 if (!num)
917 num = 4;
918
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200919 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100920}
921
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100922static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200923{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200924 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200925 uint32_t bucket_size; /* oct */
926 uint32_t ms_bucket_size; /* oct */
927 uint32_t leak_rate; /* oct/s */
928 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200929 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200930 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200931 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200932
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100933 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200934 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
935 return -EIO;
936 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100937
938 /* FIXME: This calculation needs to be redone to support multiple BTS */
939 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
940 if (!bts) {
941 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
942 return -EIO;
943 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200944
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100945 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200946
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100947 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
948 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
949 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
950 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200951
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100952 /* FIXME: This calculation is mostly wrong. It should be done based on
953 currently established TBF (and whether the related (egprs)_ms_class
954 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200955 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200956 int meas_rate;
957 int usage; /* in 0..1000 */
958
959 if (num_pdch < 0)
960 num_pdch = count_pdch(bts);
961
962 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
963 if (meas_rate > 0) {
964 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
965 leak_rate =
966 (meas_rate * usage + leak_rate * (1000 - usage)) /
967 1000;
968 LOGP(DBSSGP, LOGL_DEBUG,
969 "Estimated BVC leak rate = %d "
970 "(measured %d, usage %d%%)\n",
971 leak_rate, meas_rate, usage/10);
972 }
973 }
974
975 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200976 if (num_pdch < 0)
977 num_pdch = count_pdch(bts);
978
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200979 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200980
981 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100982 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100983 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200984 };
985
986 if (ms_leak_rate == 0) {
987 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100988 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200989
990 if (num_pdch < 0)
991 num_pdch = count_pdch(bts);
992
993 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200994 if (max_pdch > FC_MS_MAX_RX_SLOTS)
995 max_pdch = FC_MS_MAX_RX_SLOTS;
996 if (ms_num_pdch > max_pdch)
997 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200998
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200999 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001000
1001 /* TODO: To properly support multiple TRX, the per MS leak rate
1002 * should be derived from the max number of PDCH TS per TRX.
1003 */
1004 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +01001005 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
1006 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +01001007 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001008 };
1009
1010 /* TODO: Force leak_rate to 0 on buffer bloat */
1011
1012 if (bucket_size == 0)
1013 bucket_size = compute_bucket_size(bts, leak_rate,
1014 FC_FALLBACK_BVC_BUCKET_SIZE);
1015
1016 if (ms_bucket_size == 0)
1017 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1018 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1019
1020 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1021 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1022
1023 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1024 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1025
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001026 /* Avg queue delay monitoring */
1027 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001028
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001029 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001030 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001031
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001032 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001033 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1034 "R_MS = %d, avg_dly = %d\n",
1035 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1036 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001037
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001038 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001039 bucket_size, leak_rate,
1040 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001041 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001042}
1043
1044static void bvc_timeout(void *_priv)
1045{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001046 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001047 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001048 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001049 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001050 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001051 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001052 return;
1053 }
1054
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001055 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001056 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001057 the_pcu->bssgp.bctx->bvci);
1058 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 +02001059 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001060 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001061 return;
1062 }
1063
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001064 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001065 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001066 the_pcu->bssgp.bctx->bvci);
1067 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001068 secs = osmo_tdef_get(the_pcu->T_defs, -101, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001069 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001070 return;
1071 }
1072
1073 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001074 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001075 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001076 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001077}
1078
Alexander Couzens13a12e22021-02-15 05:50:10 +01001079/*! configure NS layer
1080 *
1081 * \param bts pointer to the bts object
1082 * \param nsei the NSEI of the BSS
1083 * \param local pointer to an array of local address to bind on.
1084 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1085 * \param nsvci pointer to an array of nsvcis
1086 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1087 * \returns 0 if the configuration has succeeded. on error != 0
1088 */
1089static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1090 uint16_t nsei,
1091 const struct osmo_sockaddr *local,
1092 const struct osmo_sockaddr *remote,
1093 const uint16_t *nsvci,
1094 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001095{
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001096 unsigned int i;
1097 int rc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001098 uint16_t binds = 0;
1099 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001100 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001101 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001102 char name[16];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001103 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001104
1105 if (!valid)
1106 return -1;
1107
Alexander Couzens13a12e22021-02-15 05:50:10 +01001108 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1109 if (!bts->nse)
1110 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1111 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1112 if (!bts->nse) {
1113 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1114 return -1;
1115 }
1116
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001117 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1118 if (!(valid & (1 << i)))
1119 continue;
1120
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001121 if (!gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i])) {
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001122 snprintf(name, sizeof(name), "pcu%u", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001123 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001124 if (rc < 0) {
1125 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1126 continue;
1127 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001128
1129 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1130 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1131 if (rc < 0) {
1132 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1133 continue;
1134 }
1135 }
Harald Welted9367e32021-04-29 22:02:47 +02001136
1137 if (the_pcu->vty.ns_ip_dscp != -1)
1138 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1139 if (the_pcu->vty.ns_priority != -1)
1140 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001141 }
1142
1143 binds |= 1 << i;
1144 }
1145
1146 if (!binds) {
1147 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001148 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001149 return -1;
1150 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001151
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001152 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1153 if (!(binds & (1 << i)))
1154 continue;
1155
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001156 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001157 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001158 if (rc && rc != -EALREADY) {
1159 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001160 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001161 } else {
1162 sns_configured = true;
1163 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001164 } else {
1165 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1166 if (nsvc)
1167 nsvcs = true;
1168 else
1169 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1170 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001171 }
1172
Alexander Couzens13a12e22021-02-15 05:50:10 +01001173 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1174 return sns_configured ? 0 : -1;
1175 else
1176 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001177}
1178
1179struct nsvc_cb {
1180 const struct osmo_sockaddr *local;
1181 const struct osmo_sockaddr *remote;
1182 const uint16_t *nsvci;
1183 /* [in] bitmask of valid nsvc in local/remote */
1184 uint16_t valid;
1185 /* [out] bitmask of found nsvcs */
1186 uint16_t found;
1187};
1188
1189static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1190{
1191 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001192 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001193
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001194 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001195 if (!(data->valid & (1 << i)))
1196 continue;
1197 if (data->found & (1 << i))
1198 continue;
1199
1200 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1201 &data->remote[i],
1202 data->nsvci[i])) {
1203 data->found |= 1 << i;
1204 return 0;
1205 }
1206 }
1207
1208 /* Found an extra nsvc */
1209 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1210 gprs_ns2_free_nsvc(nsvc);
1211
1212 return 0;
1213}
1214
Alexander Couzens94a367f2021-02-16 16:57:15 +01001215/* update the ns configuration if needed */
1216int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1217 const struct osmo_sockaddr *local,
1218 const struct osmo_sockaddr *remote,
1219 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001220{
1221 int rc = 0;
1222 if (!bts->nse) {
1223 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001224 gprs_ns2_free_nses(the_pcu->nsi);
1225 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001226 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001227 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1228 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001229 gprs_ns2_free_nses(the_pcu->nsi);
1230 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001231 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001232 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001233 /* SNS: check if the initial nsvc is the same, if not recreate it */
1234 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001235 unsigned int i;
1236 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001237 if (!(valid & (1 << i)))
1238 continue;
1239
1240 /* found the initial - everything should be fine */
1241 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1242 return 0;
1243 }
1244
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001245 gprs_ns2_free_nses(the_pcu->nsi);
1246 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001247 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001248 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001249 /* check if all NSVC are still the same. */
1250 struct nsvc_cb data = {
1251 .local = &local[0],
1252 .remote = &remote[0],
1253 .nsvci = &nsvci[0],
1254 .valid = valid,
1255 .found = 0,
1256 };
1257
1258 /* search the current active nsvcs */
1259 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1260
1261 /* we found all our valid nsvcs and might have removed all other nsvcs */
1262 if (valid == data.found)
1263 return 0;
1264
1265 /* remove all found nsvcs from the valid field */
1266 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001267 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001268 }
1269
1270 if (rc)
1271 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1272
1273 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001274}
1275
Alexander Couzens290d9032020-09-16 21:52:02 +02001276struct gprs_bssgp_pcu *gprs_bssgp_init(
1277 struct gprs_rlcmac_bts *bts,
1278 uint16_t nsei, uint16_t bvci,
1279 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1280 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001281{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001282
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001283 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001284 if (the_pcu->bssgp.bctx)
1285 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001286
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001287 the_pcu->bssgp.bts = bts;
1288 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1289 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001290 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001291 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001292 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001293 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001294 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001295 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001296 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001297 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1298 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001299 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001300 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1301 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001302 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001303 the_pcu->bssgp.bctx->ra_id.lac = lac;
1304 the_pcu->bssgp.bctx->ra_id.rac = rac;
1305 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001306
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001307 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001308
Oliver Smith97839672021-07-05 11:11:42 +02001309 the_pcu->bssgp.ctrs = rate_ctr_group_alloc(the_pcu, &sgsn_ctrg_desc, 0);
1310 OSMO_ASSERT(the_pcu->bssgp.ctrs)
1311
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001312 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001313}
1314
Alexander Couzens290d9032020-09-16 21:52:02 +02001315void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001316{
Oliver Smith97839672021-07-05 11:11:42 +02001317 rate_ctr_group_free(the_pcu->bssgp.ctrs);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001318 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001319
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001320 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001321 the_pcu->bssgp.nsvc_unblocked = 0;
1322 the_pcu->bssgp.bvc_sig_reset = 0;
1323 the_pcu->bssgp.bvc_reset = 0;
1324 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001325
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001326 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1327 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001328
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001329 gprs_ns2_free(the_pcu->nsi);
1330 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001331 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001332}
1333
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001334void gprs_bssgp_update_frames_sent()
1335{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001336 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001337}
1338
1339void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1340{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001341 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1342 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001343}
1344
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001345void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1346 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001347{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001348 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001349 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001350
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001351 timespecsub(tv_now, tv_recv, &tv_delay);
1352 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001353
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001354 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001355}