blob: 2c5a97a7d4c9cc66b551a00e21bd7afb07903b15 [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.
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040015 */
16
17#include <gprs_rlcmac.h>
18#include <gprs_bssgp_pcu.h>
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010019#include <gprs_bssgp_rim.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040020#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010021#include <gprs_debug.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020022#include <bts.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020023#include <tbf.h>
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020024#include <coding_scheme.h>
Max6dc90b82018-02-19 17:17:28 +010025#include <pdch.h>
Pau Espin Pedrolde7bb752020-01-23 12:37:15 +010026#include <decoding.h>
Max136ebcc2019-03-05 14:59:03 +010027
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010028#include <osmocom/gprs/gprs_ns2.h>
29#include <osmocom/gsm/protocol/gsm_23_003.h>
30#include <osmocom/gprs/protocol/gsm_08_16.h>
31#include <osmocom/core/utils.h>
Oliver Smith97839672021-07-05 11:11:42 +020032#include <osmocom/core/stats.h>
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010033#include <osmocom/gsm/gsm48.h>
34#include "coding_scheme.h"
35#include "tbf_dl.h"
36#include "llc.h"
37#include "gprs_rlcmac.h"
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010038#include "bts_pch_timer.h"
Pau Espin Pedrolff7c5812022-12-14 18:49:06 +010039#include "alloc_algo.h"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040040
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020041/* Tuning parameters for BSSGP flow control */
42#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
43#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
44#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
45#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
46
47/* Constants for BSSGP flow control */
48#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
49#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
50
Andreas Eversberg71cce912013-01-16 13:49:00 +010051extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020052extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010053extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040054
Oliver Smith97839672021-07-05 11:11:42 +020055static const struct rate_ctr_desc sgsn_ctr_description[] = {
56 [SGSN_CTR_RX_PAGING_CS] = { "rx_paging_cs", "Amount of paging CS requests received" },
57 [SGSN_CTR_RX_PAGING_PS] = { "rx_paging_ps", "Amount of paging PS requests received" },
58};
59
60static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
61 .group_name_prefix = "pcu:sgsn",
62 .group_description = "SGSN Statistics",
63 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
64 .num_ctr = ARRAY_SIZE(sgsn_ctr_description),
65 .ctr_desc = sgsn_ctr_description,
66};
67
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020068static void bvc_timeout(void *_priv);
69
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010070static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020071{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010072 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020073 uint8_t cap_len;
74 uint8_t *cap;
75
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010076 memset(rac, 0, sizeof(*rac));
77
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020078 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010079 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020080
81 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
82 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
83
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010084 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
85
Alexander Couzensccde5c92017-02-04 03:10:08 +010086 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020087 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010088
89 /* TS 24.008, 10.5.5.12a */
90 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020091
92 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010093 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020094}
95
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020096static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040097{
98 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040099
Andreas Eversberge6228b32012-07-03 13:36:03 +0200100 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700101 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200102 uint8_t *data;
103 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100104 uint8_t ms_class = 0;
105 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700106 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100107 MS_Radio_Access_capability_t rac;
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100108 const char *imsi = NULL;
109 struct osmo_mobile_identity mi_imsi;
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200110
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400111 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200112 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400113
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400114 /* LLC_PDU is mandatory IE */
115 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400116 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200117 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400118 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
119 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400120
Andreas Eversberge6228b32012-07-03 13:36:03 +0200121 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
122 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700123 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400124 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200125 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 +0200126 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400127 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400128
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200129 /* read IMSI. if no IMSI exists, use first paging block (any paging),
130 * because during attachment the IMSI might not be known, so the MS
131 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700132 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
133 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200134 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
135 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200136 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700137 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
138 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
139 }
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100140 imsi = &mi_imsi.imsi[0];
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700141 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200142
143 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100144 if (parse_ra_cap(tp, &rac) >= 0) {
145 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100146 ms_class = get_ms_class_by_capability(&rac);
147 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100148 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
149 ms_class, egprs_ms_class);
150 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200151
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200152 /* get lifetime */
153 uint16_t delay_csec = 0xffff;
154 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
155 {
156 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200157 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200158 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200159 else
160 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
161 "PDU_LIFETIME IE\n");
162 } else
163 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
164 "PDU_LIFETIME IE\n");
165
Jacob Erlbeck93990462015-05-15 15:50:43 +0200166 /* get optional TLLI old */
167 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
168 {
169 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200170 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200171 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200172 else
173 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
174 "TLLI (old) IE\n");
175 }
176
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100177 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n",
178 tlli, imsi ? : "none", len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400179
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100180 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, imsi, ms_class,
181 egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400182}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400183
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200184/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
185static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
186{
187 int rc;
188
189 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
190
191 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
192 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
193 return BSSGP_CAUSE_MISSING_MAND_IE;
194 }
195
196 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
197 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
198 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
199 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
200 return BSSGP_CAUSE_INV_MAND_INF;
201 }
202 req->mi_imsi_present = true;
203
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200204 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200205 req->mi_tmsi_present = false;
206 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
207 /* Be safe against an evil SGSN - check the length */
208 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
209 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
210 return BSSGP_CAUSE_COND_IE_ERR;
211 }
212
213 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
214 req->mi_tmsi = (struct osmo_mobile_identity){
215 .type = GSM_MI_TYPE_TMSI,
216 };
217 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
218 req->mi_tmsi_present = true;
219 }
220
221 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
222 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
223 else
224 req->tlli = GSM_RESERVED_TMSI;
225
226 return 0;
227}
228
229static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
230{
231 struct paging_req_cs req;
232 struct gprs_rlcmac_bts *bts;
233 struct GprsMs *ms;
234 int rc;
235
Oliver Smith97839672021-07-05 11:11:42 +0200236 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_CS));
237
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200238 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
239 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
240
241 /* We need to page all BTSs since even if a BTS has a matching MS, it
242 * may have already moved to a newer BTS. On Each BTS, if the MS is
243 * known, then bts_add_paging() can optimize and page only on PDCHs the
244 * target MS is using. */
245 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
246 /* TODO: Match by TMSI before IMSI if present?! */
Pau Espin Pedrolcde18c52023-04-17 18:16:48 +0200247 ms = bts_get_ms_by_tlli(bts, req.tlli, req.tlli);
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200248 if (!ms && req.mi_imsi_present)
Pau Espin Pedrolcde18c52023-04-17 18:16:48 +0200249 ms = bts_get_ms_by_imsi(bts, req.mi_imsi.imsi);
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200250 bts_add_paging(bts, &req, ms);
251 }
252
253 return 0;
254}
255
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700256/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200257static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700258{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700259 /* Use TMSI (if present) or IMSI */
260 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
261 /* Be safe against an evil SGSN - check the length */
262 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
263 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
264 return BSSGP_CAUSE_COND_IE_ERR;
265 }
266
267 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200268 *mi = (struct osmo_mobile_identity){
269 .type = GSM_MI_TYPE_TMSI,
270 };
271 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700272 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200273 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
274 true);
275 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
276 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
277 return BSSGP_CAUSE_COND_IE_ERR;
278 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700279 } else {
280 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
281 return BSSGP_CAUSE_MISSING_COND_IE;
282 }
283
284 return 0;
285}
286
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700287static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200288{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200289 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200290 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100291 struct gprs_rlcmac_bts *bts;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700292 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200293
Oliver Smith97839672021-07-05 11:11:42 +0200294 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_PS));
295
Maxf3038e72019-03-19 15:39:05 +0100296 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200297 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100298 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200299 }
300
Neels Hofmeyr00520512020-08-21 15:44:58 +0200301 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
302 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700303 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100304 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
305 }
Maxf3038e72019-03-19 15:39:05 +0100306
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200307 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700308 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100309
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100310 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
311 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
Pau Espin Pedrolef8a7302021-11-08 19:19:25 +0100312 if (bts_pch_timer_get_by_imsi(bts, mi_imsi.imsi)) {
313 LOGP(DBSSGP, LOGL_INFO, "PS-Paging request already pending for IMSI=%s\n", mi_imsi.imsi);
314 bts_do_rate_ctr_inc(bts, CTR_PCH_REQUESTS_ALREADY);
315 continue;
316 }
Philipp Maier41e38472023-03-03 16:04:26 +0100317 if (gprs_rlcmac_paging_request(bts, &paging_mi, mi_imsi.imsi) < 0)
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100318 continue;
319 bts_pch_timer_start(bts, &paging_mi, mi_imsi.imsi);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100320 }
321 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200322}
323
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400324/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200325static 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 +0400326{
327 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200328 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200329 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400330 unsigned rc = 0;
331
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200332 if (!bctx)
333 return -EINVAL;
334
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335 /* If traffic is received on a BVC that is marked as blocked, the
336 * received PDU shall not be accepted and a STATUS PDU (Cause value:
337 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
338 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
339 {
340 uint16_t bvci = msgb_bvci(msg);
341 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
342 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
343 }
344
345 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200346 case BSSGP_PDUT_STATUS:
347 /* already handled in libosmogb */
348 OSMO_ASSERT(0);
349 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400350 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200351 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100352 if (the_pcu->bssgp.on_dl_unit_data)
353 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400354 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400355 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400356 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400357 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200358 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
359 bvci, bssgp_pdu_str(pdu_type));
360 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200361 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100362 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
363 break;
364 case BSSGP_PDUT_PAGING_PS:
365 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
366 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200367 case BSSGP_PDUT_RA_CAPABILITY:
368 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
369 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
370 bvci, bssgp_pdu_str(pdu_type));
371 break;
372 /* See TS 08.18 5.4.1 */
373 case BSSGP_PDUT_SUSPEND:
374 case BSSGP_PDUT_SUSPEND_ACK:
375 case BSSGP_PDUT_SUSPEND_NACK:
376 case BSSGP_PDUT_RESUME:
377 case BSSGP_PDUT_RESUME_ACK:
378 case BSSGP_PDUT_RESUME_NACK:
379 case BSSGP_PDUT_FLUSH_LL:
380 case BSSGP_PDUT_FLUSH_LL_ACK:
381 case BSSGP_PDUT_LLC_DISCARD:
382 case BSSGP_PDUT_BVC_BLOCK:
383 case BSSGP_PDUT_BVC_BLOCK_ACK:
384 case BSSGP_PDUT_BVC_UNBLOCK:
385 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
386 case BSSGP_PDUT_BVC_RESET:
387 case BSSGP_PDUT_BVC_RESET_ACK:
388 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
389 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
390 bctx->bvci, bssgp_pdu_str(pdu_type));
391 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400392 break;
393 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200394 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200395 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400396 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
397 break;
398 }
399 return rc;
400}
401
402/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200403static 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 +0400404{
405 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200406 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400407 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700408 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200409 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400410 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200411 /* already handled in libosmogb */
412 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400413 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200414 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200415 case BSSGP_PDUT_RESUME_ACK:
416 case BSSGP_PDUT_BVC_BLOCK_ACK:
417 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
418 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200419 break;
420 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200421 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100422 if (!the_pcu->bssgp.bvc_sig_reset)
423 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200424 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100425 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200426 bvc_timeout(NULL);
427 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100428 case BSSGP_PDUT_PAGING_CS:
429 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
430 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200431 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200432 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
433 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200434 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200435 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100436 the_pcu->bssgp.bvc_unblocked = 1;
437 if (the_pcu->bssgp.on_unblock_ack)
438 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200439 bvc_timeout(NULL);
440 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200441 case BSSGP_PDUT_SUSPEND_NACK:
442 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200443 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200444 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200445 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
446 bvci, bssgp_pdu_str(pdu_type));
447 break;
448 /* See TS 08.18 5.4.1 */
449 case BSSGP_PDUT_UL_UNITDATA:
450 case BSSGP_PDUT_DL_UNITDATA:
451 case BSSGP_PDUT_RA_CAPABILITY:
452 case BSSGP_PDUT_PTM_UNITDATA:
453 case BSSGP_PDUT_RA_CAPA_UDPATE:
454 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
455 case BSSGP_PDUT_RADIO_STATUS:
456 case BSSGP_PDUT_FLOW_CONTROL_BVC:
457 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
458 case BSSGP_PDUT_FLOW_CONTROL_MS:
459 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
460 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
461 case BSSGP_PDUT_CREATE_BSS_PFC:
462 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
463 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
464 case BSSGP_PDUT_MODIFY_BSS_PFC:
465 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
466 case BSSGP_PDUT_DELETE_BSS_PFC:
467 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
468 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
469 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200470 break;
471 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200472 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200473 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200474 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
475 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400476 }
477 return rc;
478}
479
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200480static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400481{
482 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
483 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
484 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200485 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200486 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100487 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400488 int data_len;
489 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200490 struct bssgp_bvc_ctx *bctx;
491
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100492 switch (pdu_type) {
493 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100494 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200495 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100496 case BSSGP_PDUT_RAN_INFO:
497 case BSSGP_PDUT_RAN_INFO_REQ:
498 case BSSGP_PDUT_RAN_INFO_ACK:
499 case BSSGP_PDUT_RAN_INFO_ERROR:
500 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
501 /* Also pass all RIM related messages to the generic BSSGP
502 * parser so that it can deliver primitive to the RIM SAP
503 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100504 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100505 default:
506 break;
507 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400508
509 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
510
511 /* UNITDATA BSSGP headers have TLLI in front */
512 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
513 {
514 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
515 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
516 }
517 else
518 {
519 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
520 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
521 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200522 if (rc < 0) {
523 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
524 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200525 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200526 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400527
Max2813f932017-06-27 17:18:40 +0200528 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100529 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
530 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
531 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
532 }
533
534 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
535 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
536 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
537 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
538 }
539
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100540 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200541 }
542
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400543 /* look-up or create the BTS context for this BVC */
544 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
545
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700546 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400547 {
Max2813f932017-06-27 17:18:40 +0200548 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
549 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400550 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
551 }
552
553 if (bctx)
554 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100555 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200556 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
557 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 +0400558 }
559
560 if (ns_bvci == BVCI_SIGNALLING)
561 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200562 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400563 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
564 }
565 else if (ns_bvci == BVCI_PTM)
566 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200567 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400568 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
569 }
570 else
571 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100572 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400573 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
574 }
575 return rc;
576}
Harald Welte477e79e2012-06-18 12:21:03 +0800577
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100578static void handle_nm_status(struct osmo_bssgp_prim *bp)
579{
580 enum gprs_bssgp_cause cause;
581
582 LOGP(DPCU, LOGL_DEBUG,
583 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
584 bp->bvci, bp->nsei);
585
586 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
587 return;
588
589 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
590
591 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
592 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
593 return;
594
595 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
596 return;
597
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100598 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100599 LOGP(DPCU, LOGL_NOTICE,
600 "Received BSSGP STATUS message for an unknown BVCI (%d), "
601 "ignored\n",
602 bp->bvci);
603 return;
604 }
605
606 switch (cause) {
607 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100608 if (the_pcu->bssgp.bvc_unblocked) {
609 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100610 bvc_timeout(NULL);
611 }
612 break;
613
614 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100615 if (the_pcu->bssgp.bvc_reset) {
616 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100617 bvc_timeout(NULL);
618 }
619 break;
620 default:
621 break;
622 }
623}
624
Harald Welte477e79e2012-06-18 12:21:03 +0800625int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
626{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100627 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100628 int rc;
629 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100630 bp = container_of(oph, struct osmo_bssgp_prim, oph);
631
632 switch (oph->sap) {
633 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100634 switch (oph->primitive) {
635 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100636 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100637 break;
638 case PRIM_NM_BVC_RESET:
639 /* received a BVC PTP reset */
640 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
641 /* Rx Reset from SGSN */
642 if (bp->bvci == BVCI_SIGNALLING) {
643 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
644 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
645 else {
646 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
647 break;
648 }
649
650 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
651 if (rc < 0) {
652 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
653 break;
654 }
655 the_pcu->bssgp.bvc_sig_reset = 1;
656 the_pcu->bssgp.bvc_reset = 0;
657 the_pcu->bssgp.bvc_unblocked = 0;
658 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
659 the_pcu->bssgp.bvc_reset = 1;
660 the_pcu->bssgp.bvc_unblocked = 0;
661 bvc_timeout(NULL);
662 }
663 break;
664 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100665 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100666 case SAP_BSSGP_RIM:
667 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100668 default:
669 break;
670 }
Harald Welte477e79e2012-06-18 12:21:03 +0800671 return 0;
672}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200673
Alexander Couzens290d9032020-09-16 21:52:02 +0200674void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200675{
Alexander Couzens290d9032020-09-16 21:52:02 +0200676 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100677 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200678 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200679 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100680 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200681 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100682 if (!the_pcu->bssgp.nsvc_unblocked) {
683 the_pcu->bssgp.bvc_sig_reset = 0;
684 the_pcu->bssgp.bvc_reset = 0;
685 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200686 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200687 }
688 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100689 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200690 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100691 if (the_pcu->bssgp.nsvc_unblocked) {
692 the_pcu->bssgp.nsvc_unblocked = 0;
693 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
694 the_pcu->bssgp.bvc_sig_reset = 0;
695 the_pcu->bssgp.bvc_reset = 0;
696 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200697 }
698 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100699 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100700 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200701 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100702 LOGP(DPCU, LOGL_DEBUG,
703 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200704 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100705 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200706 break;
707 }
708}
709
710/* called by the ns layer */
711int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
712{
713 struct osmo_gprs_ns2_prim *nsp;
714 int rc = 0;
715
716 if (oph->sap != SAP_NS)
717 return 0;
718
719 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
720
721 if (oph->operation != PRIM_OP_INDICATION) {
722 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
723 get_value_string(osmo_prim_op_names, oph->operation),
724 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100725 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200726 }
727
728 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100729 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200730 /* hand the message into the BSSGP implementation */
731 /* add required msg fields for Gb layer */
732 msgb_bssgph(oph->msg) = oph->msg->l3h;
733 msgb_bvci(oph->msg) = nsp->bvci;
734 msgb_nsei(oph->msg) = nsp->nsei;
735 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
736 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100737 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200738 gprs_ns_prim_status_cb(nsp);
739 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100740 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100741 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200742 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100743 LOGP(DPCU, LOGL_DEBUG,
744 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200745 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100746 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200747 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200748 }
749
Alexander Couzensa2848542020-10-26 00:33:31 +0100750out:
751 if (oph->msg)
752 msgb_free(oph->msg);
753
Alexander Couzens290d9032020-09-16 21:52:02 +0200754 return rc;
755}
756
757/* called by the bssgp layer to send NS PDUs */
758int gprs_gp_send_cb(void *ctx, struct msgb *msg)
759{
760 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
761 struct osmo_gprs_ns2_prim nsp = {};
762 nsp.nsei = msgb_nsei(msg);
763 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100764 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200765 PRIM_OP_REQUEST, msg);
766 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200767}
768
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200769static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
770{
771 size_t trx_no, ts_no;
772 unsigned num_pdch = 0;
773
774 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
775 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
776
777 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
778 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
779
Pau Espin Pedrol9c2512a2022-04-01 18:27:04 +0200780 if (pdch_is_enabled(pdch))
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200781 num_pdch += 1;
782 }
783 }
784
785 return num_pdch;
786}
787
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200788static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200789{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200790 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 +0200791
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100792 /* n byte payload per 20ms */
793 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200794}
795
796static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
797 uint32_t leak_rate, uint32_t fallback)
798{
799 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100800 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200801
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200802 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100803 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200804
805 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200806 bucket_size = FC_MAX_BUCKET_SIZE;
807
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200808 if (bucket_size == 0 && bucket_time && leak_rate)
809 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200810
811 if (bucket_size == 0 && leak_rate)
812 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
813
814 if (bucket_size == 0)
815 bucket_size = fallback;
816
817 if (bucket_size > FC_MAX_BUCKET_SIZE)
818 bucket_size = FC_MAX_BUCKET_SIZE;
819
820 return bucket_size;
821}
822
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200823static uint32_t get_and_reset_avg_queue_delay(void)
824{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100825 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200826 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100827 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200828 uint32_t avg_delay_ms = 0;
829
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100830 if (the_pcu->bssgp.queue_delay_count > 0)
831 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200832
833 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100834 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100835 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200836
837 return avg_delay_ms;
838}
839
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200840static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
841{
842 int rate; /* byte per second */
843
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100844 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200845 return -1;
846
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100847 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200848 return -1;
849
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100850 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
851 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200852
853 /* 20ms/num_pdch is the average RLC block duration, so the rate is
854 * calculated as:
855 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100856 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
857 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200858
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100859 the_pcu->bssgp.queue_frames_sent = 0;
860 the_pcu->bssgp.queue_bytes_recv = 0;
861 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200862
863 return rate;
864}
865
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200866static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100867{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100868 int num = 0;
869 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100870 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100871
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100872 /* First check if we support any MCS: */
873 for (i = 8; i >= 0; i--) {
874 if (bts->mcs_mask & (1 << i)) {
875 num = i + 1;
876 mcs_any = true;
877 break;
878 }
879 }
880
881 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100882 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100883 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100884 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100885 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100886 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100887 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100888 } else if (bts_max_mcs_dl(bts)) {
889 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100890 } else {
891 num = 9;
892 }
893
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100894 if (num)
895 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100896 }
897
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100898 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100899 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100900 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100901 } else {
902 for (i = 3; i >= 0; i--) {
903 if (bts->cs_mask & (1 << i)) {
904 num = i + 1;
905 break;
906 }
907 }
908 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100909 } else if (bts_max_cs_dl(bts)) {
910 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100911 }
912
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100913 if (!num)
914 num = 4;
915
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200916 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100917}
918
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100919static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200920{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200921 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200922 uint32_t bucket_size; /* oct */
923 uint32_t ms_bucket_size; /* oct */
924 uint32_t leak_rate; /* oct/s */
925 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200926 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200927 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200928 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200929
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100930 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200931 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
932 return -EIO;
933 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100934
935 /* FIXME: This calculation needs to be redone to support multiple BTS */
936 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
937 if (!bts) {
938 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
939 return -EIO;
940 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200941
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100942 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200943
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100944 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
945 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
946 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
947 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200948
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100949 /* FIXME: This calculation is mostly wrong. It should be done based on
950 currently established TBF (and whether the related (egprs)_ms_class
951 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200952 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200953 int meas_rate;
954 int usage; /* in 0..1000 */
955
956 if (num_pdch < 0)
957 num_pdch = count_pdch(bts);
958
959 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
960 if (meas_rate > 0) {
961 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
962 leak_rate =
963 (meas_rate * usage + leak_rate * (1000 - usage)) /
964 1000;
965 LOGP(DBSSGP, LOGL_DEBUG,
966 "Estimated BVC leak rate = %d "
967 "(measured %d, usage %d%%)\n",
968 leak_rate, meas_rate, usage/10);
969 }
970 }
971
972 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200973 if (num_pdch < 0)
974 num_pdch = count_pdch(bts);
975
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200976 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200977
978 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100979 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100980 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200981 };
982
983 if (ms_leak_rate == 0) {
984 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100985 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200986
987 if (num_pdch < 0)
988 num_pdch = count_pdch(bts);
989
990 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200991 if (max_pdch > FC_MS_MAX_RX_SLOTS)
992 max_pdch = FC_MS_MAX_RX_SLOTS;
993 if (ms_num_pdch > max_pdch)
994 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200995
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200996 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200997
998 /* TODO: To properly support multiple TRX, the per MS leak rate
999 * should be derived from the max number of PDCH TS per TRX.
1000 */
1001 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +01001002 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
1003 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +01001004 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001005 };
1006
1007 /* TODO: Force leak_rate to 0 on buffer bloat */
1008
1009 if (bucket_size == 0)
1010 bucket_size = compute_bucket_size(bts, leak_rate,
1011 FC_FALLBACK_BVC_BUCKET_SIZE);
1012
1013 if (ms_bucket_size == 0)
1014 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1015 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1016
1017 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1018 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1019
1020 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1021 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1022
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001023 /* Avg queue delay monitoring */
1024 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001025
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001026 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001027 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001028
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001029 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001030 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1031 "R_MS = %d, avg_dly = %d\n",
1032 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1033 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001034
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001035 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001036 bucket_size, leak_rate,
1037 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001038 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001039}
1040
1041static void bvc_timeout(void *_priv)
1042{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001043 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001044 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001045 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001046 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001047 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001048 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001049 return;
1050 }
1051
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001052 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001053 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001054 the_pcu->bssgp.bctx->bvci);
1055 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 +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_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001062 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001063 the_pcu->bssgp.bctx->bvci);
1064 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001065 secs = osmo_tdef_get(the_pcu->T_defs, -101, 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
1070 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001071 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001072 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001073 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001074}
1075
Alexander Couzens13a12e22021-02-15 05:50:10 +01001076/*! configure NS layer
1077 *
1078 * \param bts pointer to the bts object
1079 * \param nsei the NSEI of the BSS
1080 * \param local pointer to an array of local address to bind on.
1081 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1082 * \param nsvci pointer to an array of nsvcis
1083 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1084 * \returns 0 if the configuration has succeeded. on error != 0
1085 */
1086static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1087 uint16_t nsei,
1088 const struct osmo_sockaddr *local,
1089 const struct osmo_sockaddr *remote,
1090 const uint16_t *nsvci,
1091 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001092{
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001093 unsigned int i;
1094 int rc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001095 uint16_t binds = 0;
1096 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001097 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001098 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001099 char name[16];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001100 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001101
1102 if (!valid)
1103 return -1;
1104
Alexander Couzens13a12e22021-02-15 05:50:10 +01001105 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1106 if (!bts->nse)
1107 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1108 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1109 if (!bts->nse) {
1110 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1111 return -1;
1112 }
1113
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001114 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1115 if (!(valid & (1 << i)))
1116 continue;
1117
Daniel Willmanndd28f822021-10-29 17:34:24 +02001118 bind[i] = gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i]);
1119 if (!bind[i]) {
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001120 snprintf(name, sizeof(name), "pcu%u", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001121 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001122 if (rc < 0) {
1123 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1124 continue;
1125 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001126
1127 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1128 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1129 if (rc < 0) {
1130 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1131 continue;
1132 }
1133 }
Harald Welted9367e32021-04-29 22:02:47 +02001134
1135 if (the_pcu->vty.ns_ip_dscp != -1)
1136 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1137 if (the_pcu->vty.ns_priority != -1)
1138 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001139 }
1140
1141 binds |= 1 << i;
1142 }
1143
1144 if (!binds) {
1145 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001146 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001147 return -1;
1148 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001149
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001150 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1151 if (!(binds & (1 << i)))
1152 continue;
1153
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001154 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001155 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001156 if (rc && rc != -EALREADY) {
1157 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001158 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001159 } else {
1160 sns_configured = true;
1161 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001162 } else {
1163 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1164 if (nsvc)
1165 nsvcs = true;
1166 else
1167 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1168 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001169 }
1170
Alexander Couzens13a12e22021-02-15 05:50:10 +01001171 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1172 return sns_configured ? 0 : -1;
1173 else
1174 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001175}
1176
1177struct nsvc_cb {
1178 const struct osmo_sockaddr *local;
1179 const struct osmo_sockaddr *remote;
1180 const uint16_t *nsvci;
1181 /* [in] bitmask of valid nsvc in local/remote */
1182 uint16_t valid;
1183 /* [out] bitmask of found nsvcs */
1184 uint16_t found;
1185};
1186
1187static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1188{
1189 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001190 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001191
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001192 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001193 if (!(data->valid & (1 << i)))
1194 continue;
1195 if (data->found & (1 << i))
1196 continue;
1197
1198 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1199 &data->remote[i],
1200 data->nsvci[i])) {
1201 data->found |= 1 << i;
1202 return 0;
1203 }
1204 }
1205
1206 /* Found an extra nsvc */
1207 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1208 gprs_ns2_free_nsvc(nsvc);
1209
1210 return 0;
1211}
1212
Alexander Couzens94a367f2021-02-16 16:57:15 +01001213/* update the ns configuration if needed */
1214int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1215 const struct osmo_sockaddr *local,
1216 const struct osmo_sockaddr *remote,
1217 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001218{
1219 int rc = 0;
1220 if (!bts->nse) {
1221 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001222 gprs_ns2_free_nses(the_pcu->nsi);
1223 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001224 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001225 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1226 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001227 gprs_ns2_free_nses(the_pcu->nsi);
1228 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001229 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001230 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001231 /* SNS: check if the initial nsvc is the same, if not recreate it */
1232 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001233 unsigned int i;
1234 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001235 if (!(valid & (1 << i)))
1236 continue;
1237
1238 /* found the initial - everything should be fine */
1239 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1240 return 0;
1241 }
1242
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001243 gprs_ns2_free_nses(the_pcu->nsi);
1244 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001245 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001246 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001247 /* check if all NSVC are still the same. */
1248 struct nsvc_cb data = {
1249 .local = &local[0],
1250 .remote = &remote[0],
1251 .nsvci = &nsvci[0],
1252 .valid = valid,
1253 .found = 0,
1254 };
1255
1256 /* search the current active nsvcs */
1257 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1258
1259 /* we found all our valid nsvcs and might have removed all other nsvcs */
1260 if (valid == data.found)
1261 return 0;
1262
1263 /* remove all found nsvcs from the valid field */
1264 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001265 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001266 }
1267
1268 if (rc)
1269 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1270
1271 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001272}
1273
Alexander Couzens290d9032020-09-16 21:52:02 +02001274struct gprs_bssgp_pcu *gprs_bssgp_init(
1275 struct gprs_rlcmac_bts *bts,
1276 uint16_t nsei, uint16_t bvci,
1277 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1278 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001279{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001280
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001281 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001282 if (the_pcu->bssgp.bctx)
1283 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001284
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001285 the_pcu->bssgp.bts = bts;
1286 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1287 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001288 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001289 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001290 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001291 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001292 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001293 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001294 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001295 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1296 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001297 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001298 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1299 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001300 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001301 the_pcu->bssgp.bctx->ra_id.lac = lac;
1302 the_pcu->bssgp.bctx->ra_id.rac = rac;
1303 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001304
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001305 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001306
Oliver Smith97839672021-07-05 11:11:42 +02001307 the_pcu->bssgp.ctrs = rate_ctr_group_alloc(the_pcu, &sgsn_ctrg_desc, 0);
1308 OSMO_ASSERT(the_pcu->bssgp.ctrs)
1309
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001310 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001311}
1312
Alexander Couzens290d9032020-09-16 21:52:02 +02001313void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001314{
Oliver Smith97839672021-07-05 11:11:42 +02001315 rate_ctr_group_free(the_pcu->bssgp.ctrs);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001316 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001317
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001318 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001319 the_pcu->bssgp.nsvc_unblocked = 0;
1320 the_pcu->bssgp.bvc_sig_reset = 0;
1321 the_pcu->bssgp.bvc_reset = 0;
1322 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001323
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001324 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1325 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001326
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001327 gprs_ns2_free(the_pcu->nsi);
1328 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001329 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001330}
1331
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001332void gprs_bssgp_update_frames_sent()
1333{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001334 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001335}
1336
1337void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1338{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001339 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1340 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001341}
1342
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001343void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1344 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001345{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001346 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001347 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001348
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001349 timespecsub(tv_now, tv_recv, &tv_delay);
1350 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001351
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001352 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001353}