blob: bf82fc4fc287ccb684e501e65559dd5596f8788e [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"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040039
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020040/* Tuning parameters for BSSGP flow control */
41#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
42#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
43#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
44#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
45
46/* Constants for BSSGP flow control */
47#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
48#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
49
Andreas Eversberg71cce912013-01-16 13:49:00 +010050extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020051extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010052extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040053
Oliver Smith97839672021-07-05 11:11:42 +020054static const struct rate_ctr_desc sgsn_ctr_description[] = {
55 [SGSN_CTR_RX_PAGING_CS] = { "rx_paging_cs", "Amount of paging CS requests received" },
56 [SGSN_CTR_RX_PAGING_PS] = { "rx_paging_ps", "Amount of paging PS requests received" },
57};
58
59static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
60 .group_name_prefix = "pcu:sgsn",
61 .group_description = "SGSN Statistics",
62 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
63 .num_ctr = ARRAY_SIZE(sgsn_ctr_description),
64 .ctr_desc = sgsn_ctr_description,
65};
66
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020067static void bvc_timeout(void *_priv);
68
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010069static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020070{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010071 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020072 uint8_t cap_len;
73 uint8_t *cap;
74
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010075 memset(rac, 0, sizeof(*rac));
76
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020077 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010078 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020079
80 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
81 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
82
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010083 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
84
Alexander Couzensccde5c92017-02-04 03:10:08 +010085 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020086 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010087
88 /* TS 24.008, 10.5.5.12a */
89 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020090
91 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010092 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020093}
94
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020095static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040096{
97 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040098
Andreas Eversberge6228b32012-07-03 13:36:03 +020099 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700100 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200101 uint8_t *data;
102 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100103 uint8_t ms_class = 0;
104 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700105 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100106 MS_Radio_Access_capability_t rac;
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100107 const char *imsi = NULL;
108 struct osmo_mobile_identity mi_imsi;
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200109
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400110 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200111 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400112
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400113 /* LLC_PDU is mandatory IE */
114 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400115 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200116 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400117 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
118 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400119
Andreas Eversberge6228b32012-07-03 13:36:03 +0200120 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
121 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700122 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400123 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200124 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 +0200125 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400126 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400127
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200128 /* read IMSI. if no IMSI exists, use first paging block (any paging),
129 * because during attachment the IMSI might not be known, so the MS
130 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700131 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
132 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200133 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
134 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200135 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700136 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
137 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
138 }
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100139 imsi = &mi_imsi.imsi[0];
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700140 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200141
142 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100143 if (parse_ra_cap(tp, &rac) >= 0) {
144 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100145 ms_class = get_ms_class_by_capability(&rac);
146 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100147 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
148 ms_class, egprs_ms_class);
149 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200150
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200151 /* get lifetime */
152 uint16_t delay_csec = 0xffff;
153 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
154 {
155 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200156 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200157 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200158 else
159 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
160 "PDU_LIFETIME IE\n");
161 } else
162 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
163 "PDU_LIFETIME IE\n");
164
Jacob Erlbeck93990462015-05-15 15:50:43 +0200165 /* get optional TLLI old */
166 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
167 {
168 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200169 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200170 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200171 else
172 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
173 "TLLI (old) IE\n");
174 }
175
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100176 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n",
177 tlli, imsi ? : "none", len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400178
Pau Espin Pedrol5deac142021-11-12 18:01:50 +0100179 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, imsi, ms_class,
180 egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400181}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400182
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200183/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
184static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
185{
186 int rc;
187
188 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
189
190 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
191 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
192 return BSSGP_CAUSE_MISSING_MAND_IE;
193 }
194
195 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
196 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
197 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
198 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
199 return BSSGP_CAUSE_INV_MAND_INF;
200 }
201 req->mi_imsi_present = true;
202
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200203 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200204 req->mi_tmsi_present = false;
205 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
206 /* Be safe against an evil SGSN - check the length */
207 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
208 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
209 return BSSGP_CAUSE_COND_IE_ERR;
210 }
211
212 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
213 req->mi_tmsi = (struct osmo_mobile_identity){
214 .type = GSM_MI_TYPE_TMSI,
215 };
216 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
217 req->mi_tmsi_present = true;
218 }
219
220 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
221 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
222 else
223 req->tlli = GSM_RESERVED_TMSI;
224
225 return 0;
226}
227
228static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
229{
230 struct paging_req_cs req;
231 struct gprs_rlcmac_bts *bts;
232 struct GprsMs *ms;
233 int rc;
234
Oliver Smith97839672021-07-05 11:11:42 +0200235 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_CS));
236
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200237 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
238 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
239
240 /* We need to page all BTSs since even if a BTS has a matching MS, it
241 * may have already moved to a newer BTS. On Each BTS, if the MS is
242 * known, then bts_add_paging() can optimize and page only on PDCHs the
243 * target MS is using. */
244 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
245 /* TODO: Match by TMSI before IMSI if present?! */
246 ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
247 if (!ms && req.mi_imsi_present)
248 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
249 bts_add_paging(bts, &req, ms);
250 }
251
252 return 0;
253}
254
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700255/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200256static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700257{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700258 /* Use TMSI (if present) or IMSI */
259 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
260 /* Be safe against an evil SGSN - check the length */
261 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
262 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
263 return BSSGP_CAUSE_COND_IE_ERR;
264 }
265
266 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200267 *mi = (struct osmo_mobile_identity){
268 .type = GSM_MI_TYPE_TMSI,
269 };
270 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700271 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200272 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
273 true);
274 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
275 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
276 return BSSGP_CAUSE_COND_IE_ERR;
277 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700278 } else {
279 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
280 return BSSGP_CAUSE_MISSING_COND_IE;
281 }
282
283 return 0;
284}
285
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700286static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200287{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200288 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200289 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100290 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100291 uint16_t pgroup;
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 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200306 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100307 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200308 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100309 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700310 }
Maxf3038e72019-03-19 15:39:05 +0100311
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200312 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700313 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100314
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100315 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
316 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
Pau Espin Pedrolef8a7302021-11-08 19:19:25 +0100317 if (bts_pch_timer_get_by_imsi(bts, mi_imsi.imsi)) {
318 LOGP(DBSSGP, LOGL_INFO, "PS-Paging request already pending for IMSI=%s\n", mi_imsi.imsi);
319 bts_do_rate_ctr_inc(bts, CTR_PCH_REQUESTS_ALREADY);
320 continue;
321 }
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100322 if (gprs_rlcmac_paging_request(bts, &paging_mi, pgroup) < 0)
323 continue;
324 bts_pch_timer_start(bts, &paging_mi, mi_imsi.imsi);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100325 }
326 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200327}
328
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400329/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200330static 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 +0400331{
332 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200333 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200334 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335 unsigned rc = 0;
336
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200337 if (!bctx)
338 return -EINVAL;
339
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400340 /* If traffic is received on a BVC that is marked as blocked, the
341 * received PDU shall not be accepted and a STATUS PDU (Cause value:
342 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
343 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
344 {
345 uint16_t bvci = msgb_bvci(msg);
346 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
347 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
348 }
349
350 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200351 case BSSGP_PDUT_STATUS:
352 /* already handled in libosmogb */
353 OSMO_ASSERT(0);
354 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400355 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200356 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100357 if (the_pcu->bssgp.on_dl_unit_data)
358 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400359 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400360 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400361 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400362 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200363 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
364 bvci, bssgp_pdu_str(pdu_type));
365 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200366 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100367 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
368 break;
369 case BSSGP_PDUT_PAGING_PS:
370 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
371 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200372 case BSSGP_PDUT_RA_CAPABILITY:
373 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
374 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
375 bvci, bssgp_pdu_str(pdu_type));
376 break;
377 /* See TS 08.18 5.4.1 */
378 case BSSGP_PDUT_SUSPEND:
379 case BSSGP_PDUT_SUSPEND_ACK:
380 case BSSGP_PDUT_SUSPEND_NACK:
381 case BSSGP_PDUT_RESUME:
382 case BSSGP_PDUT_RESUME_ACK:
383 case BSSGP_PDUT_RESUME_NACK:
384 case BSSGP_PDUT_FLUSH_LL:
385 case BSSGP_PDUT_FLUSH_LL_ACK:
386 case BSSGP_PDUT_LLC_DISCARD:
387 case BSSGP_PDUT_BVC_BLOCK:
388 case BSSGP_PDUT_BVC_BLOCK_ACK:
389 case BSSGP_PDUT_BVC_UNBLOCK:
390 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
391 case BSSGP_PDUT_BVC_RESET:
392 case BSSGP_PDUT_BVC_RESET_ACK:
393 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
394 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
395 bctx->bvci, bssgp_pdu_str(pdu_type));
396 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400397 break;
398 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200399 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200400 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400401 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
402 break;
403 }
404 return rc;
405}
406
407/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200408static 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 +0400409{
410 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200411 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400412 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700413 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200414 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400415 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200416 /* already handled in libosmogb */
417 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400418 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200419 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200420 case BSSGP_PDUT_RESUME_ACK:
421 case BSSGP_PDUT_BVC_BLOCK_ACK:
422 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
423 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200424 break;
425 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200426 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100427 if (!the_pcu->bssgp.bvc_sig_reset)
428 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200429 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100430 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200431 bvc_timeout(NULL);
432 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100433 case BSSGP_PDUT_PAGING_CS:
434 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
435 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200436 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200437 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
438 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200439 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200440 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100441 the_pcu->bssgp.bvc_unblocked = 1;
442 if (the_pcu->bssgp.on_unblock_ack)
443 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200444 bvc_timeout(NULL);
445 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200446 case BSSGP_PDUT_SUSPEND_NACK:
447 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200448 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200449 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200450 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
451 bvci, bssgp_pdu_str(pdu_type));
452 break;
453 /* See TS 08.18 5.4.1 */
454 case BSSGP_PDUT_UL_UNITDATA:
455 case BSSGP_PDUT_DL_UNITDATA:
456 case BSSGP_PDUT_RA_CAPABILITY:
457 case BSSGP_PDUT_PTM_UNITDATA:
458 case BSSGP_PDUT_RA_CAPA_UDPATE:
459 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
460 case BSSGP_PDUT_RADIO_STATUS:
461 case BSSGP_PDUT_FLOW_CONTROL_BVC:
462 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
463 case BSSGP_PDUT_FLOW_CONTROL_MS:
464 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
465 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
466 case BSSGP_PDUT_CREATE_BSS_PFC:
467 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
468 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
469 case BSSGP_PDUT_MODIFY_BSS_PFC:
470 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
471 case BSSGP_PDUT_DELETE_BSS_PFC:
472 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
473 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
474 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200475 break;
476 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200477 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200478 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200479 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
480 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400481 }
482 return rc;
483}
484
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200485static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400486{
487 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
488 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
489 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200490 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200491 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100492 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400493 int data_len;
494 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200495 struct bssgp_bvc_ctx *bctx;
496
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100497 switch (pdu_type) {
498 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100499 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200500 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100501 case BSSGP_PDUT_RAN_INFO:
502 case BSSGP_PDUT_RAN_INFO_REQ:
503 case BSSGP_PDUT_RAN_INFO_ACK:
504 case BSSGP_PDUT_RAN_INFO_ERROR:
505 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
506 /* Also pass all RIM related messages to the generic BSSGP
507 * parser so that it can deliver primitive to the RIM SAP
508 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100509 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100510 default:
511 break;
512 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400513
514 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
515
516 /* UNITDATA BSSGP headers have TLLI in front */
517 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
518 {
519 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
520 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
521 }
522 else
523 {
524 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
525 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
526 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200527 if (rc < 0) {
528 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
529 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200530 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200531 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400532
Max2813f932017-06-27 17:18:40 +0200533 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100534 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
535 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
536 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
537 }
538
539 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
540 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
541 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
542 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
543 }
544
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100545 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200546 }
547
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400548 /* look-up or create the BTS context for this BVC */
549 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
550
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700551 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400552 {
Max2813f932017-06-27 17:18:40 +0200553 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
554 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400555 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
556 }
557
558 if (bctx)
559 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100560 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200561 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
562 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 +0400563 }
564
565 if (ns_bvci == BVCI_SIGNALLING)
566 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200567 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400568 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
569 }
570 else if (ns_bvci == BVCI_PTM)
571 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200572 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400573 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
574 }
575 else
576 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100577 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400578 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
579 }
580 return rc;
581}
Harald Welte477e79e2012-06-18 12:21:03 +0800582
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100583static void handle_nm_status(struct osmo_bssgp_prim *bp)
584{
585 enum gprs_bssgp_cause cause;
586
587 LOGP(DPCU, LOGL_DEBUG,
588 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
589 bp->bvci, bp->nsei);
590
591 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
592 return;
593
594 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
595
596 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
597 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
598 return;
599
600 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
601 return;
602
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100603 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100604 LOGP(DPCU, LOGL_NOTICE,
605 "Received BSSGP STATUS message for an unknown BVCI (%d), "
606 "ignored\n",
607 bp->bvci);
608 return;
609 }
610
611 switch (cause) {
612 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100613 if (the_pcu->bssgp.bvc_unblocked) {
614 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100615 bvc_timeout(NULL);
616 }
617 break;
618
619 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100620 if (the_pcu->bssgp.bvc_reset) {
621 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100622 bvc_timeout(NULL);
623 }
624 break;
625 default:
626 break;
627 }
628}
629
Harald Welte477e79e2012-06-18 12:21:03 +0800630int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
631{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100632 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100633 int rc;
634 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100635 bp = container_of(oph, struct osmo_bssgp_prim, oph);
636
637 switch (oph->sap) {
638 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100639 switch (oph->primitive) {
640 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100641 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100642 break;
643 case PRIM_NM_BVC_RESET:
644 /* received a BVC PTP reset */
645 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
646 /* Rx Reset from SGSN */
647 if (bp->bvci == BVCI_SIGNALLING) {
648 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
649 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
650 else {
651 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
652 break;
653 }
654
655 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
656 if (rc < 0) {
657 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
658 break;
659 }
660 the_pcu->bssgp.bvc_sig_reset = 1;
661 the_pcu->bssgp.bvc_reset = 0;
662 the_pcu->bssgp.bvc_unblocked = 0;
663 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
664 the_pcu->bssgp.bvc_reset = 1;
665 the_pcu->bssgp.bvc_unblocked = 0;
666 bvc_timeout(NULL);
667 }
668 break;
669 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100670 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100671 case SAP_BSSGP_RIM:
672 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100673 default:
674 break;
675 }
Harald Welte477e79e2012-06-18 12:21:03 +0800676 return 0;
677}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200678
Alexander Couzens290d9032020-09-16 21:52:02 +0200679void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200680{
Alexander Couzens290d9032020-09-16 21:52:02 +0200681 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100682 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200683 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200684 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100685 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200686 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100687 if (!the_pcu->bssgp.nsvc_unblocked) {
688 the_pcu->bssgp.bvc_sig_reset = 0;
689 the_pcu->bssgp.bvc_reset = 0;
690 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200691 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200692 }
693 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100694 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200695 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100696 if (the_pcu->bssgp.nsvc_unblocked) {
697 the_pcu->bssgp.nsvc_unblocked = 0;
698 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
699 the_pcu->bssgp.bvc_sig_reset = 0;
700 the_pcu->bssgp.bvc_reset = 0;
701 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200702 }
703 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100704 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100705 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200706 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100707 LOGP(DPCU, LOGL_DEBUG,
708 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200709 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100710 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200711 break;
712 }
713}
714
715/* called by the ns layer */
716int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
717{
718 struct osmo_gprs_ns2_prim *nsp;
719 int rc = 0;
720
721 if (oph->sap != SAP_NS)
722 return 0;
723
724 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
725
726 if (oph->operation != PRIM_OP_INDICATION) {
727 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
728 get_value_string(osmo_prim_op_names, oph->operation),
729 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100730 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200731 }
732
733 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100734 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200735 /* hand the message into the BSSGP implementation */
736 /* add required msg fields for Gb layer */
737 msgb_bssgph(oph->msg) = oph->msg->l3h;
738 msgb_bvci(oph->msg) = nsp->bvci;
739 msgb_nsei(oph->msg) = nsp->nsei;
740 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
741 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100742 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200743 gprs_ns_prim_status_cb(nsp);
744 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100745 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100746 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200747 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100748 LOGP(DPCU, LOGL_DEBUG,
749 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200750 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100751 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200752 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200753 }
754
Alexander Couzensa2848542020-10-26 00:33:31 +0100755out:
756 if (oph->msg)
757 msgb_free(oph->msg);
758
Alexander Couzens290d9032020-09-16 21:52:02 +0200759 return rc;
760}
761
762/* called by the bssgp layer to send NS PDUs */
763int gprs_gp_send_cb(void *ctx, struct msgb *msg)
764{
765 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
766 struct osmo_gprs_ns2_prim nsp = {};
767 nsp.nsei = msgb_nsei(msg);
768 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100769 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200770 PRIM_OP_REQUEST, msg);
771 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200772}
773
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200774static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
775{
776 size_t trx_no, ts_no;
777 unsigned num_pdch = 0;
778
779 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
780 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
781
782 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
783 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
784
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100785 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200786 num_pdch += 1;
787 }
788 }
789
790 return num_pdch;
791}
792
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200793static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200794{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200795 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 +0200796
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100797 /* n byte payload per 20ms */
798 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200799}
800
801static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
802 uint32_t leak_rate, uint32_t fallback)
803{
804 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100805 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200806
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200807 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100808 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200809
810 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200811 bucket_size = FC_MAX_BUCKET_SIZE;
812
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200813 if (bucket_size == 0 && bucket_time && leak_rate)
814 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200815
816 if (bucket_size == 0 && leak_rate)
817 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
818
819 if (bucket_size == 0)
820 bucket_size = fallback;
821
822 if (bucket_size > FC_MAX_BUCKET_SIZE)
823 bucket_size = FC_MAX_BUCKET_SIZE;
824
825 return bucket_size;
826}
827
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200828static uint32_t get_and_reset_avg_queue_delay(void)
829{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100830 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200831 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100832 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200833 uint32_t avg_delay_ms = 0;
834
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100835 if (the_pcu->bssgp.queue_delay_count > 0)
836 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200837
838 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100839 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100840 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200841
842 return avg_delay_ms;
843}
844
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200845static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
846{
847 int rate; /* byte per second */
848
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100849 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200850 return -1;
851
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100852 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200853 return -1;
854
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100855 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
856 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200857
858 /* 20ms/num_pdch is the average RLC block duration, so the rate is
859 * calculated as:
860 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100861 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
862 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200863
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100864 the_pcu->bssgp.queue_frames_sent = 0;
865 the_pcu->bssgp.queue_bytes_recv = 0;
866 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200867
868 return rate;
869}
870
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200871static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100872{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100873 int num = 0;
874 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100875 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100876
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100877 /* First check if we support any MCS: */
878 for (i = 8; i >= 0; i--) {
879 if (bts->mcs_mask & (1 << i)) {
880 num = i + 1;
881 mcs_any = true;
882 break;
883 }
884 }
885
886 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100887 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100888 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100889 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100890 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100891 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100892 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100893 } else if (bts_max_mcs_dl(bts)) {
894 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100895 } else {
896 num = 9;
897 }
898
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100899 if (num)
900 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100901 }
902
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100903 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100904 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100905 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100906 } else {
907 for (i = 3; i >= 0; i--) {
908 if (bts->cs_mask & (1 << i)) {
909 num = i + 1;
910 break;
911 }
912 }
913 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100914 } else if (bts_max_cs_dl(bts)) {
915 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100916 }
917
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100918 if (!num)
919 num = 4;
920
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200921 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100922}
923
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100924static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200925{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200926 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200927 uint32_t bucket_size; /* oct */
928 uint32_t ms_bucket_size; /* oct */
929 uint32_t leak_rate; /* oct/s */
930 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200931 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200932 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200933 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200934
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100935 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200936 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
937 return -EIO;
938 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100939
940 /* FIXME: This calculation needs to be redone to support multiple BTS */
941 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
942 if (!bts) {
943 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
944 return -EIO;
945 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200946
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100947 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200948
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100949 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
950 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
951 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
952 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200953
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100954 /* FIXME: This calculation is mostly wrong. It should be done based on
955 currently established TBF (and whether the related (egprs)_ms_class
956 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200957 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200958 int meas_rate;
959 int usage; /* in 0..1000 */
960
961 if (num_pdch < 0)
962 num_pdch = count_pdch(bts);
963
964 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
965 if (meas_rate > 0) {
966 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
967 leak_rate =
968 (meas_rate * usage + leak_rate * (1000 - usage)) /
969 1000;
970 LOGP(DBSSGP, LOGL_DEBUG,
971 "Estimated BVC leak rate = %d "
972 "(measured %d, usage %d%%)\n",
973 leak_rate, meas_rate, usage/10);
974 }
975 }
976
977 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200978 if (num_pdch < 0)
979 num_pdch = count_pdch(bts);
980
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200981 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200982
983 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100984 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100985 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200986 };
987
988 if (ms_leak_rate == 0) {
989 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100990 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200991
992 if (num_pdch < 0)
993 num_pdch = count_pdch(bts);
994
995 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200996 if (max_pdch > FC_MS_MAX_RX_SLOTS)
997 max_pdch = FC_MS_MAX_RX_SLOTS;
998 if (ms_num_pdch > max_pdch)
999 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001000
Jacob Erlbeck6eed1912015-07-21 18:29:46 +02001001 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001002
1003 /* TODO: To properly support multiple TRX, the per MS leak rate
1004 * should be derived from the max number of PDCH TS per TRX.
1005 */
1006 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +01001007 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
1008 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +01001009 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001010 };
1011
1012 /* TODO: Force leak_rate to 0 on buffer bloat */
1013
1014 if (bucket_size == 0)
1015 bucket_size = compute_bucket_size(bts, leak_rate,
1016 FC_FALLBACK_BVC_BUCKET_SIZE);
1017
1018 if (ms_bucket_size == 0)
1019 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1020 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1021
1022 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1023 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1024
1025 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1026 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1027
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001028 /* Avg queue delay monitoring */
1029 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001030
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001031 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001032 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001033
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001034 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001035 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1036 "R_MS = %d, avg_dly = %d\n",
1037 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1038 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001039
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001040 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001041 bucket_size, leak_rate,
1042 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001043 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001044}
1045
1046static void bvc_timeout(void *_priv)
1047{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001048 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001049 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001050 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001051 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001052 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001053 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001054 return;
1055 }
1056
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001057 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001058 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001059 the_pcu->bssgp.bctx->bvci);
1060 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 +02001061 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001062 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001063 return;
1064 }
1065
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001066 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001067 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001068 the_pcu->bssgp.bctx->bvci);
1069 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001070 secs = osmo_tdef_get(the_pcu->T_defs, -101, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001071 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001072 return;
1073 }
1074
1075 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001076 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001077 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001078 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001079}
1080
Alexander Couzens13a12e22021-02-15 05:50:10 +01001081/*! configure NS layer
1082 *
1083 * \param bts pointer to the bts object
1084 * \param nsei the NSEI of the BSS
1085 * \param local pointer to an array of local address to bind on.
1086 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1087 * \param nsvci pointer to an array of nsvcis
1088 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1089 * \returns 0 if the configuration has succeeded. on error != 0
1090 */
1091static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1092 uint16_t nsei,
1093 const struct osmo_sockaddr *local,
1094 const struct osmo_sockaddr *remote,
1095 const uint16_t *nsvci,
1096 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001097{
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001098 unsigned int i;
1099 int rc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001100 uint16_t binds = 0;
1101 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001102 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001103 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001104 char name[16];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001105 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001106
1107 if (!valid)
1108 return -1;
1109
Alexander Couzens13a12e22021-02-15 05:50:10 +01001110 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1111 if (!bts->nse)
1112 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1113 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1114 if (!bts->nse) {
1115 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1116 return -1;
1117 }
1118
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001119 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1120 if (!(valid & (1 << i)))
1121 continue;
1122
Daniel Willmanndd28f822021-10-29 17:34:24 +02001123 bind[i] = gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i]);
1124 if (!bind[i]) {
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001125 snprintf(name, sizeof(name), "pcu%u", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001126 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001127 if (rc < 0) {
1128 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1129 continue;
1130 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001131
1132 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1133 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1134 if (rc < 0) {
1135 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1136 continue;
1137 }
1138 }
Harald Welted9367e32021-04-29 22:02:47 +02001139
1140 if (the_pcu->vty.ns_ip_dscp != -1)
1141 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1142 if (the_pcu->vty.ns_priority != -1)
1143 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001144 }
1145
1146 binds |= 1 << i;
1147 }
1148
1149 if (!binds) {
1150 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001151 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001152 return -1;
1153 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001154
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001155 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1156 if (!(binds & (1 << i)))
1157 continue;
1158
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001159 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001160 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001161 if (rc && rc != -EALREADY) {
1162 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001163 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001164 } else {
1165 sns_configured = true;
1166 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001167 } else {
1168 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1169 if (nsvc)
1170 nsvcs = true;
1171 else
1172 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1173 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001174 }
1175
Alexander Couzens13a12e22021-02-15 05:50:10 +01001176 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1177 return sns_configured ? 0 : -1;
1178 else
1179 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001180}
1181
1182struct nsvc_cb {
1183 const struct osmo_sockaddr *local;
1184 const struct osmo_sockaddr *remote;
1185 const uint16_t *nsvci;
1186 /* [in] bitmask of valid nsvc in local/remote */
1187 uint16_t valid;
1188 /* [out] bitmask of found nsvcs */
1189 uint16_t found;
1190};
1191
1192static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1193{
1194 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001195 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001196
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001197 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001198 if (!(data->valid & (1 << i)))
1199 continue;
1200 if (data->found & (1 << i))
1201 continue;
1202
1203 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1204 &data->remote[i],
1205 data->nsvci[i])) {
1206 data->found |= 1 << i;
1207 return 0;
1208 }
1209 }
1210
1211 /* Found an extra nsvc */
1212 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1213 gprs_ns2_free_nsvc(nsvc);
1214
1215 return 0;
1216}
1217
Alexander Couzens94a367f2021-02-16 16:57:15 +01001218/* update the ns configuration if needed */
1219int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1220 const struct osmo_sockaddr *local,
1221 const struct osmo_sockaddr *remote,
1222 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001223{
1224 int rc = 0;
1225 if (!bts->nse) {
1226 /* there shouldn't any previous state. */
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 Couzens5bece2a2020-10-12 02:27:22 +02001230 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1231 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001232 gprs_ns2_free_nses(the_pcu->nsi);
1233 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001234 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001235 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001236 /* SNS: check if the initial nsvc is the same, if not recreate it */
1237 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001238 unsigned int i;
1239 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001240 if (!(valid & (1 << i)))
1241 continue;
1242
1243 /* found the initial - everything should be fine */
1244 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1245 return 0;
1246 }
1247
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001248 gprs_ns2_free_nses(the_pcu->nsi);
1249 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001250 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001251 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001252 /* check if all NSVC are still the same. */
1253 struct nsvc_cb data = {
1254 .local = &local[0],
1255 .remote = &remote[0],
1256 .nsvci = &nsvci[0],
1257 .valid = valid,
1258 .found = 0,
1259 };
1260
1261 /* search the current active nsvcs */
1262 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1263
1264 /* we found all our valid nsvcs and might have removed all other nsvcs */
1265 if (valid == data.found)
1266 return 0;
1267
1268 /* remove all found nsvcs from the valid field */
1269 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001270 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001271 }
1272
1273 if (rc)
1274 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1275
1276 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001277}
1278
Alexander Couzens290d9032020-09-16 21:52:02 +02001279struct gprs_bssgp_pcu *gprs_bssgp_init(
1280 struct gprs_rlcmac_bts *bts,
1281 uint16_t nsei, uint16_t bvci,
1282 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1283 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001284{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001285
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001286 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001287 if (the_pcu->bssgp.bctx)
1288 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001289
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001290 the_pcu->bssgp.bts = bts;
1291 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1292 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001293 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001294 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001295 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001296 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001297 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001298 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001299 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001300 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1301 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001302 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001303 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1304 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001305 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001306 the_pcu->bssgp.bctx->ra_id.lac = lac;
1307 the_pcu->bssgp.bctx->ra_id.rac = rac;
1308 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001309
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001310 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001311
Oliver Smith97839672021-07-05 11:11:42 +02001312 the_pcu->bssgp.ctrs = rate_ctr_group_alloc(the_pcu, &sgsn_ctrg_desc, 0);
1313 OSMO_ASSERT(the_pcu->bssgp.ctrs)
1314
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001315 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001316}
1317
Alexander Couzens290d9032020-09-16 21:52:02 +02001318void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001319{
Oliver Smith97839672021-07-05 11:11:42 +02001320 rate_ctr_group_free(the_pcu->bssgp.ctrs);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001321 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001322
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001323 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001324 the_pcu->bssgp.nsvc_unblocked = 0;
1325 the_pcu->bssgp.bvc_sig_reset = 0;
1326 the_pcu->bssgp.bvc_reset = 0;
1327 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001328
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001329 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1330 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001331
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001332 gprs_ns2_free(the_pcu->nsi);
1333 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001334 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001335}
1336
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001337void gprs_bssgp_update_frames_sent()
1338{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001339 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001340}
1341
1342void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1343{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001344 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1345 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001346}
1347
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001348void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1349 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001350{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001351 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001352 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001353
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001354 timespecsub(tv_now, tv_recv, &tv_delay);
1355 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001356
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001357 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001358}