blob: 4328e0792fa3e6c772eea46780907657ec0e8627 [file] [log] [blame]
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04001/* gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
Holger Hans Peter Freyther416ce692013-08-22 08:44:04 +02004 * Copyright (C) 2013 by Holger Hans Peter Freyther
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +04005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <gprs_rlcmac.h>
22#include <gprs_bssgp_pcu.h>
Pau Espin Pedrol3a271022021-01-21 18:44:23 +010023#include <gprs_bssgp_rim.h>
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040024#include <pcu_l1_if.h>
Max1187a772018-01-26 13:31:42 +010025#include <gprs_debug.h>
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020026#include <bts.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020027#include <tbf.h>
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +020028#include <coding_scheme.h>
Max6dc90b82018-02-19 17:17:28 +010029#include <pdch.h>
Pau Espin Pedrolde7bb752020-01-23 12:37:15 +010030#include <decoding.h>
Max136ebcc2019-03-05 14:59:03 +010031
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010032#include <osmocom/gprs/gprs_ns2.h>
33#include <osmocom/gsm/protocol/gsm_23_003.h>
34#include <osmocom/gprs/protocol/gsm_08_16.h>
35#include <osmocom/core/utils.h>
Oliver Smith97839672021-07-05 11:11:42 +020036#include <osmocom/core/stats.h>
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010037#include <osmocom/gsm/gsm48.h>
38#include "coding_scheme.h"
39#include "tbf_dl.h"
40#include "llc.h"
41#include "gprs_rlcmac.h"
Pau Espin Pedrol13961c92021-11-08 17:38:47 +010042#include "bts_pch_timer.h"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040043
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020044/* Tuning parameters for BSSGP flow control */
45#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
46#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
47#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
48#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
49
50/* Constants for BSSGP flow control */
51#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
52#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
53
Andreas Eversberg71cce912013-01-16 13:49:00 +010054extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020055extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010056extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040057
Oliver Smith97839672021-07-05 11:11:42 +020058static const struct rate_ctr_desc sgsn_ctr_description[] = {
59 [SGSN_CTR_RX_PAGING_CS] = { "rx_paging_cs", "Amount of paging CS requests received" },
60 [SGSN_CTR_RX_PAGING_PS] = { "rx_paging_ps", "Amount of paging PS requests received" },
61};
62
63static const struct rate_ctr_group_desc sgsn_ctrg_desc = {
64 .group_name_prefix = "pcu:sgsn",
65 .group_description = "SGSN Statistics",
66 .class_id = OSMO_STATS_CLASS_SUBSCRIBER,
67 .num_ctr = ARRAY_SIZE(sgsn_ctr_description),
68 .ctr_desc = sgsn_ctr_description,
69};
70
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020071static void bvc_timeout(void *_priv);
72
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010073static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020074{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010075 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020076 uint8_t cap_len;
77 uint8_t *cap;
78
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010079 memset(rac, 0, sizeof(*rac));
80
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020081 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010082 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020083
84 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
85 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
86
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010087 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
88
Alexander Couzensccde5c92017-02-04 03:10:08 +010089 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020090 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010091
92 /* TS 24.008, 10.5.5.12a */
93 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020094
95 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010096 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020097}
98
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020099static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400100{
101 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400102
Andreas Eversberge6228b32012-07-03 13:36:03 +0200103 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +0700104 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +0200105 uint8_t *data;
106 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100107 uint8_t ms_class = 0;
108 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700109 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100110 MS_Radio_Access_capability_t rac;
Neels Hofmeyr00520512020-08-21 15:44:58 +0200111 /* TODO: is it really necessary to initialize this as a "000" IMSI? It seems, the function should just return an
112 * error if no IMSI IE was found. */
113 struct osmo_mobile_identity mi_imsi = {
114 .type = GSM_MI_TYPE_TMSI,
115 };
116 OSMO_STRLCPY_ARRAY(mi_imsi.imsi, "000");
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200117
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400118 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200119 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400120
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400121 /* LLC_PDU is mandatory IE */
122 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400123 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200124 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400125 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
126 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400127
Andreas Eversberge6228b32012-07-03 13:36:03 +0200128 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
129 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700130 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400131 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200132 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 +0200133 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400134 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400135
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200136 /* read IMSI. if no IMSI exists, use first paging block (any paging),
137 * because during attachment the IMSI might not be known, so the MS
138 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700139 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
140 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200141 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
142 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200143 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700144 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
145 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
146 }
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700147 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200148
149 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100150 if (parse_ra_cap(tp, &rac) >= 0) {
151 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100152 ms_class = get_ms_class_by_capability(&rac);
153 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100154 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
155 ms_class, egprs_ms_class);
156 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200157
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200158 /* get lifetime */
159 uint16_t delay_csec = 0xffff;
160 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
161 {
162 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200163 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200164 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200165 else
166 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
167 "PDU_LIFETIME IE\n");
168 } else
169 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
170 "PDU_LIFETIME IE\n");
171
Jacob Erlbeck93990462015-05-15 15:50:43 +0200172 /* get optional TLLI old */
173 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
174 {
175 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200176 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200177 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200178 else
179 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
180 "TLLI (old) IE\n");
181 }
182
Neels Hofmeyr00520512020-08-21 15:44:58 +0200183 LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n", tlli, mi_imsi.imsi, len);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400184
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100185 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, mi_imsi.imsi,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100186 ms_class, egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400187}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400188
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200189/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
190static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
191{
192 int rc;
193
194 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
195
196 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
197 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
198 return BSSGP_CAUSE_MISSING_MAND_IE;
199 }
200
201 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
202 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
203 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
204 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
205 return BSSGP_CAUSE_INV_MAND_INF;
206 }
207 req->mi_imsi_present = true;
208
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200209 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200210 req->mi_tmsi_present = false;
211 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
212 /* Be safe against an evil SGSN - check the length */
213 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
214 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
215 return BSSGP_CAUSE_COND_IE_ERR;
216 }
217
218 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
219 req->mi_tmsi = (struct osmo_mobile_identity){
220 .type = GSM_MI_TYPE_TMSI,
221 };
222 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
223 req->mi_tmsi_present = true;
224 }
225
226 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
227 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
228 else
229 req->tlli = GSM_RESERVED_TMSI;
230
231 return 0;
232}
233
234static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
235{
236 struct paging_req_cs req;
237 struct gprs_rlcmac_bts *bts;
238 struct GprsMs *ms;
239 int rc;
240
Oliver Smith97839672021-07-05 11:11:42 +0200241 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_CS));
242
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200243 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
244 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
245
246 /* We need to page all BTSs since even if a BTS has a matching MS, it
247 * may have already moved to a newer BTS. On Each BTS, if the MS is
248 * known, then bts_add_paging() can optimize and page only on PDCHs the
249 * target MS is using. */
250 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
251 /* TODO: Match by TMSI before IMSI if present?! */
252 ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
253 if (!ms && req.mi_imsi_present)
254 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
255 bts_add_paging(bts, &req, ms);
256 }
257
258 return 0;
259}
260
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700261/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200262static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700263{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700264 /* Use TMSI (if present) or IMSI */
265 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
266 /* Be safe against an evil SGSN - check the length */
267 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
268 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
269 return BSSGP_CAUSE_COND_IE_ERR;
270 }
271
272 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200273 *mi = (struct osmo_mobile_identity){
274 .type = GSM_MI_TYPE_TMSI,
275 };
276 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700277 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200278 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
279 true);
280 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
281 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
282 return BSSGP_CAUSE_COND_IE_ERR;
283 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700284 } else {
285 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
286 return BSSGP_CAUSE_MISSING_COND_IE;
287 }
288
289 return 0;
290}
291
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700292static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200293{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200294 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200295 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100296 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100297 uint16_t pgroup;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700298 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200299
Oliver Smith97839672021-07-05 11:11:42 +0200300 rate_ctr_inc(rate_ctr_group_get_ctr(the_pcu->bssgp.ctrs, SGSN_CTR_RX_PAGING_PS));
301
Maxf3038e72019-03-19 15:39:05 +0100302 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200303 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100304 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200305 }
306
Neels Hofmeyr00520512020-08-21 15:44:58 +0200307 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
308 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700309 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100310 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
311 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200312 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100313 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200314 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100315 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700316 }
Maxf3038e72019-03-19 15:39:05 +0100317
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200318 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700319 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100320
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100321 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
322 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
Pau Espin Pedrolef8a7302021-11-08 19:19:25 +0100323 if (bts_pch_timer_get_by_imsi(bts, mi_imsi.imsi)) {
324 LOGP(DBSSGP, LOGL_INFO, "PS-Paging request already pending for IMSI=%s\n", mi_imsi.imsi);
325 bts_do_rate_ctr_inc(bts, CTR_PCH_REQUESTS_ALREADY);
326 continue;
327 }
Pau Espin Pedrol13961c92021-11-08 17:38:47 +0100328 if (gprs_rlcmac_paging_request(bts, &paging_mi, pgroup) < 0)
329 continue;
330 bts_pch_timer_start(bts, &paging_mi, mi_imsi.imsi);
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100331 }
332 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200333}
334
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200336static 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 +0400337{
338 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200339 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200340 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400341 unsigned rc = 0;
342
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200343 if (!bctx)
344 return -EINVAL;
345
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400346 /* If traffic is received on a BVC that is marked as blocked, the
347 * received PDU shall not be accepted and a STATUS PDU (Cause value:
348 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
349 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
350 {
351 uint16_t bvci = msgb_bvci(msg);
352 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
353 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
354 }
355
356 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200357 case BSSGP_PDUT_STATUS:
358 /* already handled in libosmogb */
359 OSMO_ASSERT(0);
360 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400361 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200362 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100363 if (the_pcu->bssgp.on_dl_unit_data)
364 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400365 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400366 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400367 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400368 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200369 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
370 bvci, bssgp_pdu_str(pdu_type));
371 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200372 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100373 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
374 break;
375 case BSSGP_PDUT_PAGING_PS:
376 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
377 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200378 case BSSGP_PDUT_RA_CAPABILITY:
379 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
380 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
381 bvci, bssgp_pdu_str(pdu_type));
382 break;
383 /* See TS 08.18 5.4.1 */
384 case BSSGP_PDUT_SUSPEND:
385 case BSSGP_PDUT_SUSPEND_ACK:
386 case BSSGP_PDUT_SUSPEND_NACK:
387 case BSSGP_PDUT_RESUME:
388 case BSSGP_PDUT_RESUME_ACK:
389 case BSSGP_PDUT_RESUME_NACK:
390 case BSSGP_PDUT_FLUSH_LL:
391 case BSSGP_PDUT_FLUSH_LL_ACK:
392 case BSSGP_PDUT_LLC_DISCARD:
393 case BSSGP_PDUT_BVC_BLOCK:
394 case BSSGP_PDUT_BVC_BLOCK_ACK:
395 case BSSGP_PDUT_BVC_UNBLOCK:
396 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
397 case BSSGP_PDUT_BVC_RESET:
398 case BSSGP_PDUT_BVC_RESET_ACK:
399 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
400 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
401 bctx->bvci, bssgp_pdu_str(pdu_type));
402 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400403 break;
404 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200405 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200406 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400407 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
408 break;
409 }
410 return rc;
411}
412
413/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200414static 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 +0400415{
416 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200417 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400418 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700419 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200420 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400421 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200422 /* already handled in libosmogb */
423 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400424 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200425 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200426 case BSSGP_PDUT_RESUME_ACK:
427 case BSSGP_PDUT_BVC_BLOCK_ACK:
428 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
429 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200430 break;
431 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200432 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100433 if (!the_pcu->bssgp.bvc_sig_reset)
434 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200435 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100436 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200437 bvc_timeout(NULL);
438 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100439 case BSSGP_PDUT_PAGING_CS:
440 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
441 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200442 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200443 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
444 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200445 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200446 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100447 the_pcu->bssgp.bvc_unblocked = 1;
448 if (the_pcu->bssgp.on_unblock_ack)
449 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200450 bvc_timeout(NULL);
451 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200452 case BSSGP_PDUT_SUSPEND_NACK:
453 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200454 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200455 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200456 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
457 bvci, bssgp_pdu_str(pdu_type));
458 break;
459 /* See TS 08.18 5.4.1 */
460 case BSSGP_PDUT_UL_UNITDATA:
461 case BSSGP_PDUT_DL_UNITDATA:
462 case BSSGP_PDUT_RA_CAPABILITY:
463 case BSSGP_PDUT_PTM_UNITDATA:
464 case BSSGP_PDUT_RA_CAPA_UDPATE:
465 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
466 case BSSGP_PDUT_RADIO_STATUS:
467 case BSSGP_PDUT_FLOW_CONTROL_BVC:
468 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
469 case BSSGP_PDUT_FLOW_CONTROL_MS:
470 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
471 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
472 case BSSGP_PDUT_CREATE_BSS_PFC:
473 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
474 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
475 case BSSGP_PDUT_MODIFY_BSS_PFC:
476 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
477 case BSSGP_PDUT_DELETE_BSS_PFC:
478 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
479 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
480 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200481 break;
482 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200483 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200484 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200485 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
486 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400487 }
488 return rc;
489}
490
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200491static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400492{
493 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
494 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
495 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200496 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200497 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100498 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400499 int data_len;
500 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200501 struct bssgp_bvc_ctx *bctx;
502
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100503 switch (pdu_type) {
504 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100505 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200506 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100507 case BSSGP_PDUT_RAN_INFO:
508 case BSSGP_PDUT_RAN_INFO_REQ:
509 case BSSGP_PDUT_RAN_INFO_ACK:
510 case BSSGP_PDUT_RAN_INFO_ERROR:
511 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
512 /* Also pass all RIM related messages to the generic BSSGP
513 * parser so that it can deliver primitive to the RIM SAP
514 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100515 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100516 default:
517 break;
518 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400519
520 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
521
522 /* UNITDATA BSSGP headers have TLLI in front */
523 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
524 {
525 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
526 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
527 }
528 else
529 {
530 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
531 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
532 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200533 if (rc < 0) {
534 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
535 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200536 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200537 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400538
Max2813f932017-06-27 17:18:40 +0200539 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100540 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
541 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
542 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
543 }
544
545 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
546 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
547 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
548 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
549 }
550
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100551 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200552 }
553
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400554 /* look-up or create the BTS context for this BVC */
555 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
556
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700557 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400558 {
Max2813f932017-06-27 17:18:40 +0200559 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
560 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400561 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
562 }
563
564 if (bctx)
565 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100566 log_set_context(LOG_CTX_GB_BVC, bctx);
Pau Espin Pedrol9c1db172021-06-04 17:05:51 +0200567 rate_ctr_inc(rate_ctr_group_get_ctr(bctx->ctrg, BSSGP_CTR_PKTS_IN));
568 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 +0400569 }
570
571 if (ns_bvci == BVCI_SIGNALLING)
572 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200573 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400574 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
575 }
576 else if (ns_bvci == BVCI_PTM)
577 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200578 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400579 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
580 }
581 else
582 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100583 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400584 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
585 }
586 return rc;
587}
Harald Welte477e79e2012-06-18 12:21:03 +0800588
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100589static void handle_nm_status(struct osmo_bssgp_prim *bp)
590{
591 enum gprs_bssgp_cause cause;
592
593 LOGP(DPCU, LOGL_DEBUG,
594 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
595 bp->bvci, bp->nsei);
596
597 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
598 return;
599
600 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
601
602 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
603 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
604 return;
605
606 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
607 return;
608
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100609 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100610 LOGP(DPCU, LOGL_NOTICE,
611 "Received BSSGP STATUS message for an unknown BVCI (%d), "
612 "ignored\n",
613 bp->bvci);
614 return;
615 }
616
617 switch (cause) {
618 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100619 if (the_pcu->bssgp.bvc_unblocked) {
620 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100621 bvc_timeout(NULL);
622 }
623 break;
624
625 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100626 if (the_pcu->bssgp.bvc_reset) {
627 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100628 bvc_timeout(NULL);
629 }
630 break;
631 default:
632 break;
633 }
634}
635
Harald Welte477e79e2012-06-18 12:21:03 +0800636int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
637{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100638 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100639 int rc;
640 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100641 bp = container_of(oph, struct osmo_bssgp_prim, oph);
642
643 switch (oph->sap) {
644 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100645 switch (oph->primitive) {
646 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100647 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100648 break;
649 case PRIM_NM_BVC_RESET:
650 /* received a BVC PTP reset */
651 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
652 /* Rx Reset from SGSN */
653 if (bp->bvci == BVCI_SIGNALLING) {
654 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
655 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
656 else {
657 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
658 break;
659 }
660
661 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
662 if (rc < 0) {
663 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
664 break;
665 }
666 the_pcu->bssgp.bvc_sig_reset = 1;
667 the_pcu->bssgp.bvc_reset = 0;
668 the_pcu->bssgp.bvc_unblocked = 0;
669 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
670 the_pcu->bssgp.bvc_reset = 1;
671 the_pcu->bssgp.bvc_unblocked = 0;
672 bvc_timeout(NULL);
673 }
674 break;
675 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100676 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100677 case SAP_BSSGP_RIM:
678 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100679 default:
680 break;
681 }
Harald Welte477e79e2012-06-18 12:21:03 +0800682 return 0;
683}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200684
Alexander Couzens290d9032020-09-16 21:52:02 +0200685void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200686{
Alexander Couzens290d9032020-09-16 21:52:02 +0200687 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100688 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200689 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200690 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100691 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200692 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100693 if (!the_pcu->bssgp.nsvc_unblocked) {
694 the_pcu->bssgp.bvc_sig_reset = 0;
695 the_pcu->bssgp.bvc_reset = 0;
696 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200697 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200698 }
699 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100700 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200701 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100702 if (the_pcu->bssgp.nsvc_unblocked) {
703 the_pcu->bssgp.nsvc_unblocked = 0;
704 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
705 the_pcu->bssgp.bvc_sig_reset = 0;
706 the_pcu->bssgp.bvc_reset = 0;
707 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200708 }
709 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100710 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100711 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200712 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100713 LOGP(DPCU, LOGL_DEBUG,
714 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200715 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100716 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200717 break;
718 }
719}
720
721/* called by the ns layer */
722int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
723{
724 struct osmo_gprs_ns2_prim *nsp;
725 int rc = 0;
726
727 if (oph->sap != SAP_NS)
728 return 0;
729
730 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
731
732 if (oph->operation != PRIM_OP_INDICATION) {
733 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
734 get_value_string(osmo_prim_op_names, oph->operation),
735 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100736 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200737 }
738
739 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100740 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200741 /* hand the message into the BSSGP implementation */
742 /* add required msg fields for Gb layer */
743 msgb_bssgph(oph->msg) = oph->msg->l3h;
744 msgb_bvci(oph->msg) = nsp->bvci;
745 msgb_nsei(oph->msg) = nsp->nsei;
746 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
747 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100748 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200749 gprs_ns_prim_status_cb(nsp);
750 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100751 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100752 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200753 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100754 LOGP(DPCU, LOGL_DEBUG,
755 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200756 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100757 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200758 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200759 }
760
Alexander Couzensa2848542020-10-26 00:33:31 +0100761out:
762 if (oph->msg)
763 msgb_free(oph->msg);
764
Alexander Couzens290d9032020-09-16 21:52:02 +0200765 return rc;
766}
767
768/* called by the bssgp layer to send NS PDUs */
769int gprs_gp_send_cb(void *ctx, struct msgb *msg)
770{
771 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
772 struct osmo_gprs_ns2_prim nsp = {};
773 nsp.nsei = msgb_nsei(msg);
774 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100775 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200776 PRIM_OP_REQUEST, msg);
777 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200778}
779
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200780static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
781{
782 size_t trx_no, ts_no;
783 unsigned num_pdch = 0;
784
785 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
786 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
787
788 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
789 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
790
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100791 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200792 num_pdch += 1;
793 }
794 }
795
796 return num_pdch;
797}
798
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200799static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200800{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200801 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 +0200802
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100803 /* n byte payload per 20ms */
804 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200805}
806
807static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
808 uint32_t leak_rate, uint32_t fallback)
809{
810 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100811 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200812
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200813 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100814 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200815
816 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200817 bucket_size = FC_MAX_BUCKET_SIZE;
818
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200819 if (bucket_size == 0 && bucket_time && leak_rate)
820 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200821
822 if (bucket_size == 0 && leak_rate)
823 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
824
825 if (bucket_size == 0)
826 bucket_size = fallback;
827
828 if (bucket_size > FC_MAX_BUCKET_SIZE)
829 bucket_size = FC_MAX_BUCKET_SIZE;
830
831 return bucket_size;
832}
833
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200834static uint32_t get_and_reset_avg_queue_delay(void)
835{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100836 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200837 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100838 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200839 uint32_t avg_delay_ms = 0;
840
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100841 if (the_pcu->bssgp.queue_delay_count > 0)
842 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200843
844 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100845 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100846 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200847
848 return avg_delay_ms;
849}
850
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200851static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
852{
853 int rate; /* byte per second */
854
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100855 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200856 return -1;
857
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100858 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200859 return -1;
860
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100861 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
862 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200863
864 /* 20ms/num_pdch is the average RLC block duration, so the rate is
865 * calculated as:
866 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100867 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
868 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200869
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100870 the_pcu->bssgp.queue_frames_sent = 0;
871 the_pcu->bssgp.queue_bytes_recv = 0;
872 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200873
874 return rate;
875}
876
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200877static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100878{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100879 int num = 0;
880 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100881 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100882
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100883 /* First check if we support any MCS: */
884 for (i = 8; i >= 0; i--) {
885 if (bts->mcs_mask & (1 << i)) {
886 num = i + 1;
887 mcs_any = true;
888 break;
889 }
890 }
891
892 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100893 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100894 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100895 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100896 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100897 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100898 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100899 } else if (bts_max_mcs_dl(bts)) {
900 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100901 } else {
902 num = 9;
903 }
904
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100905 if (num)
906 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100907 }
908
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100909 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100910 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100911 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100912 } else {
913 for (i = 3; i >= 0; i--) {
914 if (bts->cs_mask & (1 << i)) {
915 num = i + 1;
916 break;
917 }
918 }
919 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100920 } else if (bts_max_cs_dl(bts)) {
921 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100922 }
923
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100924 if (!num)
925 num = 4;
926
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200927 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100928}
929
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100930static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200931{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200932 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200933 uint32_t bucket_size; /* oct */
934 uint32_t ms_bucket_size; /* oct */
935 uint32_t leak_rate; /* oct/s */
936 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200937 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200938 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200939 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200940
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100941 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200942 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
943 return -EIO;
944 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100945
946 /* FIXME: This calculation needs to be redone to support multiple BTS */
947 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
948 if (!bts) {
949 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
950 return -EIO;
951 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200952
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100953 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200954
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100955 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
956 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
957 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
958 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200959
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100960 /* FIXME: This calculation is mostly wrong. It should be done based on
961 currently established TBF (and whether the related (egprs)_ms_class
962 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200963 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200964 int meas_rate;
965 int usage; /* in 0..1000 */
966
967 if (num_pdch < 0)
968 num_pdch = count_pdch(bts);
969
970 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
971 if (meas_rate > 0) {
972 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
973 leak_rate =
974 (meas_rate * usage + leak_rate * (1000 - usage)) /
975 1000;
976 LOGP(DBSSGP, LOGL_DEBUG,
977 "Estimated BVC leak rate = %d "
978 "(measured %d, usage %d%%)\n",
979 leak_rate, meas_rate, usage/10);
980 }
981 }
982
983 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200984 if (num_pdch < 0)
985 num_pdch = count_pdch(bts);
986
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200987 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200988
989 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100990 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100991 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200992 };
993
994 if (ms_leak_rate == 0) {
995 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100996 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200997
998 if (num_pdch < 0)
999 num_pdch = count_pdch(bts);
1000
1001 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +02001002 if (max_pdch > FC_MS_MAX_RX_SLOTS)
1003 max_pdch = FC_MS_MAX_RX_SLOTS;
1004 if (ms_num_pdch > max_pdch)
1005 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001006
Jacob Erlbeck6eed1912015-07-21 18:29:46 +02001007 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001008
1009 /* TODO: To properly support multiple TRX, the per MS leak rate
1010 * should be derived from the max number of PDCH TS per TRX.
1011 */
1012 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +01001013 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
1014 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +01001015 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001016 };
1017
1018 /* TODO: Force leak_rate to 0 on buffer bloat */
1019
1020 if (bucket_size == 0)
1021 bucket_size = compute_bucket_size(bts, leak_rate,
1022 FC_FALLBACK_BVC_BUCKET_SIZE);
1023
1024 if (ms_bucket_size == 0)
1025 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1026 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1027
1028 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1029 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1030
1031 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1032 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1033
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001034 /* Avg queue delay monitoring */
1035 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001036
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001037 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001038 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001039
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001040 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001041 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1042 "R_MS = %d, avg_dly = %d\n",
1043 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1044 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001045
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001046 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001047 bucket_size, leak_rate,
1048 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001049 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001050}
1051
1052static void bvc_timeout(void *_priv)
1053{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001054 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001055 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001056 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001057 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001058 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001059 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001060 return;
1061 }
1062
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001063 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001064 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001065 the_pcu->bssgp.bctx->bvci);
1066 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 +02001067 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001068 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001069 return;
1070 }
1071
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001072 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001073 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001074 the_pcu->bssgp.bctx->bvci);
1075 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001076 secs = osmo_tdef_get(the_pcu->T_defs, -101, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001077 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001078 return;
1079 }
1080
1081 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001082 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001083 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001084 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001085}
1086
Alexander Couzens13a12e22021-02-15 05:50:10 +01001087/*! configure NS layer
1088 *
1089 * \param bts pointer to the bts object
1090 * \param nsei the NSEI of the BSS
1091 * \param local pointer to an array of local address to bind on.
1092 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1093 * \param nsvci pointer to an array of nsvcis
1094 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1095 * \returns 0 if the configuration has succeeded. on error != 0
1096 */
1097static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1098 uint16_t nsei,
1099 const struct osmo_sockaddr *local,
1100 const struct osmo_sockaddr *remote,
1101 const uint16_t *nsvci,
1102 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001103{
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001104 unsigned int i;
1105 int rc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001106 uint16_t binds = 0;
1107 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001108 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001109 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001110 char name[16];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001111 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001112
1113 if (!valid)
1114 return -1;
1115
Alexander Couzens13a12e22021-02-15 05:50:10 +01001116 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1117 if (!bts->nse)
1118 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1119 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1120 if (!bts->nse) {
1121 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1122 return -1;
1123 }
1124
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001125 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1126 if (!(valid & (1 << i)))
1127 continue;
1128
Daniel Willmanndd28f822021-10-29 17:34:24 +02001129 bind[i] = gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i]);
1130 if (!bind[i]) {
Pau Espin Pedrol890de982021-07-15 14:34:35 +02001131 snprintf(name, sizeof(name), "pcu%u", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001132 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001133 if (rc < 0) {
1134 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1135 continue;
1136 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001137
1138 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1139 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1140 if (rc < 0) {
1141 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1142 continue;
1143 }
1144 }
Harald Welted9367e32021-04-29 22:02:47 +02001145
1146 if (the_pcu->vty.ns_ip_dscp != -1)
1147 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1148 if (the_pcu->vty.ns_priority != -1)
1149 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001150 }
1151
1152 binds |= 1 << i;
1153 }
1154
1155 if (!binds) {
1156 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001157 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001158 return -1;
1159 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001160
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001161 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1162 if (!(binds & (1 << i)))
1163 continue;
1164
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001165 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001166 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001167 if (rc && rc != -EALREADY) {
1168 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001169 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001170 } else {
1171 sns_configured = true;
1172 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001173 } else {
1174 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1175 if (nsvc)
1176 nsvcs = true;
1177 else
1178 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1179 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001180 }
1181
Alexander Couzens13a12e22021-02-15 05:50:10 +01001182 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1183 return sns_configured ? 0 : -1;
1184 else
1185 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001186}
1187
1188struct nsvc_cb {
1189 const struct osmo_sockaddr *local;
1190 const struct osmo_sockaddr *remote;
1191 const uint16_t *nsvci;
1192 /* [in] bitmask of valid nsvc in local/remote */
1193 uint16_t valid;
1194 /* [out] bitmask of found nsvcs */
1195 uint16_t found;
1196};
1197
1198static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1199{
1200 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001201 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001202
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001203 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001204 if (!(data->valid & (1 << i)))
1205 continue;
1206 if (data->found & (1 << i))
1207 continue;
1208
1209 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1210 &data->remote[i],
1211 data->nsvci[i])) {
1212 data->found |= 1 << i;
1213 return 0;
1214 }
1215 }
1216
1217 /* Found an extra nsvc */
1218 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1219 gprs_ns2_free_nsvc(nsvc);
1220
1221 return 0;
1222}
1223
Alexander Couzens94a367f2021-02-16 16:57:15 +01001224/* update the ns configuration if needed */
1225int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1226 const struct osmo_sockaddr *local,
1227 const struct osmo_sockaddr *remote,
1228 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001229{
1230 int rc = 0;
1231 if (!bts->nse) {
1232 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001233 gprs_ns2_free_nses(the_pcu->nsi);
1234 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001235 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001236 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1237 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001238 gprs_ns2_free_nses(the_pcu->nsi);
1239 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001240 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001241 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001242 /* SNS: check if the initial nsvc is the same, if not recreate it */
1243 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001244 unsigned int i;
1245 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001246 if (!(valid & (1 << i)))
1247 continue;
1248
1249 /* found the initial - everything should be fine */
1250 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1251 return 0;
1252 }
1253
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001254 gprs_ns2_free_nses(the_pcu->nsi);
1255 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001256 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001257 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001258 /* check if all NSVC are still the same. */
1259 struct nsvc_cb data = {
1260 .local = &local[0],
1261 .remote = &remote[0],
1262 .nsvci = &nsvci[0],
1263 .valid = valid,
1264 .found = 0,
1265 };
1266
1267 /* search the current active nsvcs */
1268 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1269
1270 /* we found all our valid nsvcs and might have removed all other nsvcs */
1271 if (valid == data.found)
1272 return 0;
1273
1274 /* remove all found nsvcs from the valid field */
1275 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001276 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001277 }
1278
1279 if (rc)
1280 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1281
1282 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001283}
1284
Alexander Couzens290d9032020-09-16 21:52:02 +02001285struct gprs_bssgp_pcu *gprs_bssgp_init(
1286 struct gprs_rlcmac_bts *bts,
1287 uint16_t nsei, uint16_t bvci,
1288 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1289 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001290{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001291
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001292 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001293 if (the_pcu->bssgp.bctx)
1294 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001295
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001296 the_pcu->bssgp.bts = bts;
1297 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1298 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001299 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001300 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001301 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001302 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001303 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001304 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001305 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001306 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1307 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001308 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001309 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1310 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001311 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001312 the_pcu->bssgp.bctx->ra_id.lac = lac;
1313 the_pcu->bssgp.bctx->ra_id.rac = rac;
1314 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001315
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001316 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001317
Oliver Smith97839672021-07-05 11:11:42 +02001318 the_pcu->bssgp.ctrs = rate_ctr_group_alloc(the_pcu, &sgsn_ctrg_desc, 0);
1319 OSMO_ASSERT(the_pcu->bssgp.ctrs)
1320
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001321 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001322}
1323
Alexander Couzens290d9032020-09-16 21:52:02 +02001324void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001325{
Oliver Smith97839672021-07-05 11:11:42 +02001326 rate_ctr_group_free(the_pcu->bssgp.ctrs);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001327 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001328
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001329 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001330 the_pcu->bssgp.nsvc_unblocked = 0;
1331 the_pcu->bssgp.bvc_sig_reset = 0;
1332 the_pcu->bssgp.bvc_reset = 0;
1333 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001334
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001335 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1336 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001337
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001338 gprs_ns2_free(the_pcu->nsi);
1339 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001340 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001341}
1342
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001343void gprs_bssgp_update_frames_sent()
1344{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001345 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001346}
1347
1348void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1349{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001350 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1351 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001352}
1353
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001354void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1355 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001356{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001357 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001358 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001359
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001360 timespecsub(tv_now, tv_recv, &tv_delay);
1361 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001362
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001363 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001364}