blob: 7ee2e4550ffdd9072103f6bb24f069fc3e47479b [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>
36#include <osmocom/gsm/gsm48.h>
37#include "coding_scheme.h"
38#include "tbf_dl.h"
39#include "llc.h"
40#include "gprs_rlcmac.h"
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040041
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +020042/* Tuning parameters for BSSGP flow control */
43#define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */
44#define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */
45#define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */
46#define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */
47
48/* Constants for BSSGP flow control */
49#define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */
50#define FC_MAX_BUCKET_SIZE 6553500 /* Octets */
51
Andreas Eversberg71cce912013-01-16 13:49:00 +010052extern void *tall_pcu_ctx;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +020053extern uint16_t spoof_mcc, spoof_mnc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +010054extern bool spoof_mnc_3_digits;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040055
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +020056static void bvc_timeout(void *_priv);
57
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010058static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac)
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020059{
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +010060 struct bitvec *block;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020061 uint8_t cap_len;
62 uint8_t *cap;
63
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010064 memset(rac, 0, sizeof(*rac));
65
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020066 if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP))
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010067 return -EINVAL;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020068
69 cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
70 cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP);
71
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010072 LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len);
73
Alexander Couzensccde5c92017-02-04 03:10:08 +010074 block = bitvec_alloc(cap_len, tall_pcu_ctx);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020075 bitvec_unpack(block, cap);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010076
77 /* TS 24.008, 10.5.5.12a */
78 decode_gsm_ra_cap(block, rac);
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020079
80 bitvec_free(block);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010081 return 0;
Holger Hans Peter Freyther98fe9452013-07-13 15:23:58 +020082}
83
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +020084static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040085{
86 struct bssgp_ud_hdr *budh;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +040087
Andreas Eversberge6228b32012-07-03 13:36:03 +020088 uint32_t tlli;
Vadim Yanitskiycb988942020-11-08 13:27:35 +070089 uint32_t tlli_old = GSM_RESERVED_TMSI;
Andreas Eversberge6228b32012-07-03 13:36:03 +020090 uint8_t *data;
91 uint16_t len;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010092 uint8_t ms_class = 0;
93 uint8_t egprs_ms_class = 0;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +070094 int rc;
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +010095 MS_Radio_Access_capability_t rac;
Neels Hofmeyr00520512020-08-21 15:44:58 +020096 /* TODO: is it really necessary to initialize this as a "000" IMSI? It seems, the function should just return an
97 * error if no IMSI IE was found. */
98 struct osmo_mobile_identity mi_imsi = {
99 .type = GSM_MI_TYPE_TMSI,
100 };
101 OSMO_STRLCPY_ARRAY(mi_imsi.imsi, "000");
Andreas Eversberg7d7cf542012-06-25 09:26:15 +0200102
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400103 budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg);
Andreas Eversberge6228b32012-07-03 13:36:03 +0200104 tlli = ntohl(budh->tlli);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400105
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400106 /* LLC_PDU is mandatory IE */
107 if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU))
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400108 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200109 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400110 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
111 }
Ivan Kluchnikovb172b1b2012-06-07 01:51:49 +0400112
Andreas Eversberge6228b32012-07-03 13:36:03 +0200113 data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
114 len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
Vadim Yanitskiy3568fcf2020-02-03 16:38:51 +0700115 if (len > LLC_MAX_LEN)
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400116 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200117 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 +0200118 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400119 }
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400120
Andreas Eversberge13fa2d2012-07-09 17:10:44 +0200121 /* read IMSI. if no IMSI exists, use first paging block (any paging),
122 * because during attachment the IMSI might not be known, so the MS
123 * will listen to all paging blocks. */
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700124 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI))
125 {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200126 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
127 true);
Pau Espin Pedrol1d68cdf2020-08-26 13:41:45 +0200128 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700129 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
130 return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg);
131 }
Vadim Yanitskiyaad87b62019-07-24 16:36:45 +0700132 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200133
134 /* parse ms radio access capability */
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100135 if (parse_ra_cap(tp, &rac) >= 0) {
136 /* Get the EGPRS class from the RA capability */
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100137 ms_class = get_ms_class_by_capability(&rac);
138 egprs_ms_class = get_egprs_ms_class_by_capability(&rac);
Jacob Erlbeck4a07a3b2016-01-20 12:22:07 +0100139 LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n",
140 ms_class, egprs_ms_class);
141 }
Andreas Eversberg51ab1342012-07-13 14:52:50 +0200142
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200143 /* get lifetime */
144 uint16_t delay_csec = 0xffff;
145 if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME))
146 {
147 uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200148 if (lt_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200149 delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME);
Andreas Eversberg24131bf2012-07-21 11:09:58 +0200150 else
151 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
152 "PDU_LIFETIME IE\n");
153 } else
154 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory "
155 "PDU_LIFETIME IE\n");
156
Jacob Erlbeck93990462015-05-15 15:50:43 +0200157 /* get optional TLLI old */
158 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
159 {
160 uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200161 if (tlli_len == 2)
Harald Welte49b78222017-06-11 11:27:59 +0200162 tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI);
Jacob Erlbeck93990462015-05-15 15:50:43 +0200163 else
164 LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of "
165 "TLLI (old) IE\n");
166 }
167
Neels Hofmeyr00520512020-08-21 15:44:58 +0200168 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 +0400169
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100170 return dl_tbf_handle(the_pcu->bssgp.bts, tlli, tlli_old, mi_imsi.imsi,
Jacob Erlbeck14e00f82015-11-27 18:10:39 +0100171 ms_class, egprs_ms_class, delay_csec, data, len);
Ivan Kluchnikovc7e7f682012-06-29 22:53:15 +0400172}
Ivan Kluchnikova9e6dc52012-06-17 08:30:06 +0400173
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200174/* 3GPP TS 48.018 Table 10.3.2. Returns 0 on success, suggested BSSGP cause otherwise */
175static unsigned int get_paging_cs_mi(struct paging_req_cs *req, const struct tlv_parsed *tp)
176{
177 int rc;
178
179 req->chan_needed = tlvp_val8(tp, BSSGP_IE_CHAN_NEEDED, 0);
180
181 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
182 LOGP(DBSSGP, LOGL_ERROR, "IMSI Mobile Identity mandatory IE not found\n");
183 return BSSGP_CAUSE_MISSING_MAND_IE;
184 }
185
186 rc = osmo_mobile_identity_decode(&req->mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI),
187 TLVP_LEN(tp, BSSGP_IE_IMSI), true);
188 if (rc < 0 || req->mi_imsi.type != GSM_MI_TYPE_IMSI) {
189 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
190 return BSSGP_CAUSE_INV_MAND_INF;
191 }
192 req->mi_imsi_present = true;
193
Pau Espin Pedrol2761e572021-05-21 13:44:02 +0200194 /* TMSI is optional */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200195 req->mi_tmsi_present = false;
196 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
197 /* Be safe against an evil SGSN - check the length */
198 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
199 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
200 return BSSGP_CAUSE_COND_IE_ERR;
201 }
202
203 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
204 req->mi_tmsi = (struct osmo_mobile_identity){
205 .type = GSM_MI_TYPE_TMSI,
206 };
207 req->mi_tmsi.tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
208 req->mi_tmsi_present = true;
209 }
210
211 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI))
212 req->tlli = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TLLI));
213 else
214 req->tlli = GSM_RESERVED_TMSI;
215
216 return 0;
217}
218
219static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, const struct tlv_parsed *tp)
220{
221 struct paging_req_cs req;
222 struct gprs_rlcmac_bts *bts;
223 struct GprsMs *ms;
224 int rc;
225
226 if ((rc = get_paging_cs_mi(&req, tp)) > 0)
227 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
228
229 /* We need to page all BTSs since even if a BTS has a matching MS, it
230 * may have already moved to a newer BTS. On Each BTS, if the MS is
231 * known, then bts_add_paging() can optimize and page only on PDCHs the
232 * target MS is using. */
233 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
234 /* TODO: Match by TMSI before IMSI if present?! */
235 ms = bts_ms_by_tlli(bts, req.tlli, req.tlli);
236 if (!ms && req.mi_imsi_present)
237 ms = bts_ms_by_imsi(bts, req.mi_imsi.imsi);
238 bts_add_paging(bts, &req, ms);
239 }
240
241 return 0;
242}
243
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700244/* Returns 0 on success, suggested BSSGP cause otherwise */
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200245static unsigned int get_paging_ps_mi(struct osmo_mobile_identity *mi, const struct tlv_parsed *tp)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700246{
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700247 /* Use TMSI (if present) or IMSI */
248 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) {
249 /* Be safe against an evil SGSN - check the length */
250 if (TLVP_LEN(tp, BSSGP_IE_TMSI) != GSM23003_TMSI_NUM_BYTES) {
251 LOGP(DBSSGP, LOGL_NOTICE, "TMSI IE has odd length (!= 4)\n");
252 return BSSGP_CAUSE_COND_IE_ERR;
253 }
254
255 /* NOTE: TMSI (unlike IMSI) IE comes without MI type header */
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200256 *mi = (struct osmo_mobile_identity){
257 .type = GSM_MI_TYPE_TMSI,
258 };
259 mi->tmsi = osmo_load32be(TLVP_VAL(tp, BSSGP_IE_TMSI));
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700260 } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200261 int rc = osmo_mobile_identity_decode(mi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI),
262 true);
263 if (rc < 0 || mi->type != GSM_MI_TYPE_IMSI) {
264 LOGP(DBSSGP, LOGL_ERROR, "Invalid IMSI Mobile Identity\n");
265 return BSSGP_CAUSE_COND_IE_ERR;
266 }
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700267 } else {
268 LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n");
269 return BSSGP_CAUSE_MISSING_COND_IE;
270 }
271
272 return 0;
273}
274
Vadim Yanitskiy2c1fed22020-10-31 02:00:21 +0700275static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, const struct tlv_parsed *tp)
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200276{
Neels Hofmeyr00520512020-08-21 15:44:58 +0200277 struct osmo_mobile_identity mi_imsi;
Neels Hofmeyr59fc0bd2020-08-21 16:21:23 +0200278 struct osmo_mobile_identity paging_mi;
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100279 struct gprs_rlcmac_bts *bts;
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100280 uint16_t pgroup;
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700281 int rc;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200282
Maxf3038e72019-03-19 15:39:05 +0100283 if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200284 LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n");
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100285 return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg);
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200286 }
287
Neels Hofmeyr00520512020-08-21 15:44:58 +0200288 rc = osmo_mobile_identity_decode(&mi_imsi, TLVP_VAL(tp, BSSGP_IE_IMSI), TLVP_LEN(tp, BSSGP_IE_IMSI), true);
289 if (rc < 0 || mi_imsi.type != GSM_MI_TYPE_IMSI) {
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700290 LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100291 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
292 }
Neels Hofmeyr00520512020-08-21 15:44:58 +0200293 pgroup = imsi2paging_group(mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100294 if (pgroup > 999) {
Neels Hofmeyr00520512020-08-21 15:44:58 +0200295 LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", mi_imsi.imsi);
Pau Espin Pedrold7c32652019-12-23 12:41:34 +0100296 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Vadim Yanitskiy6170ac02019-07-24 16:44:13 +0700297 }
Maxf3038e72019-03-19 15:39:05 +0100298
Pau Espin Pedrolbd542052021-05-13 17:44:51 +0200299 if ((rc = get_paging_ps_mi(&paging_mi, tp)) > 0)
Vadim Yanitskiy0b25f692020-03-26 03:38:05 +0700300 return bssgp_tx_status((enum gprs_bssgp_cause) rc, NULL, msg);
Pau Espin Pedrol5530f122019-12-10 19:26:27 +0100301
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100302 /* FIXME: look if MS is attached a specific BTS and then only page on that one? */
303 llist_for_each_entry(bts, &the_pcu->bts_list, list) {
304 gprs_rlcmac_paging_request(bts, &paging_mi, pgroup);
305 }
306 return 0;
Andreas Eversberg8c3680d2012-10-08 12:30:56 +0200307}
308
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400309/* Receive a BSSGP PDU from a BSS on a PTP BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200310static 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 +0400311{
312 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200313 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200314 int bvci = bctx ? bctx->bvci : -1;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400315 unsigned rc = 0;
316
Andreas Eversbergba1cd9b2012-07-25 09:14:09 +0200317 if (!bctx)
318 return -EINVAL;
319
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400320 /* If traffic is received on a BVC that is marked as blocked, the
321 * received PDU shall not be accepted and a STATUS PDU (Cause value:
322 * BVC Blocked) shall be sent to the peer entity on the signalling BVC */
323 if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS)
324 {
325 uint16_t bvci = msgb_bvci(msg);
326 LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n");
327 return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg);
328 }
329
330 switch (pdu_type) {
Harald Welte05d7b5d2017-07-29 10:16:44 +0200331 case BSSGP_PDUT_STATUS:
332 /* already handled in libosmogb */
333 OSMO_ASSERT(0);
334 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400335 case BSSGP_PDUT_DL_UNITDATA:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200336 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100337 if (the_pcu->bssgp.on_dl_unit_data)
338 the_pcu->bssgp.on_dl_unit_data(&the_pcu->bssgp, msg, tp);
Ivan Kluchnikov5e0df932012-06-12 15:33:52 +0400339 gprs_bssgp_pcu_rx_dl_ud(msg, tp);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400340 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400341 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400342 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200343 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n",
344 bvci, bssgp_pdu_str(pdu_type));
345 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200346 case BSSGP_PDUT_PAGING_CS:
Pau Espin Pedrol771de1f2019-12-10 19:31:25 +0100347 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
348 break;
349 case BSSGP_PDUT_PAGING_PS:
350 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
351 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200352 case BSSGP_PDUT_RA_CAPABILITY:
353 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
354 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n",
355 bvci, bssgp_pdu_str(pdu_type));
356 break;
357 /* See TS 08.18 5.4.1 */
358 case BSSGP_PDUT_SUSPEND:
359 case BSSGP_PDUT_SUSPEND_ACK:
360 case BSSGP_PDUT_SUSPEND_NACK:
361 case BSSGP_PDUT_RESUME:
362 case BSSGP_PDUT_RESUME_ACK:
363 case BSSGP_PDUT_RESUME_NACK:
364 case BSSGP_PDUT_FLUSH_LL:
365 case BSSGP_PDUT_FLUSH_LL_ACK:
366 case BSSGP_PDUT_LLC_DISCARD:
367 case BSSGP_PDUT_BVC_BLOCK:
368 case BSSGP_PDUT_BVC_BLOCK_ACK:
369 case BSSGP_PDUT_BVC_UNBLOCK:
370 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
371 case BSSGP_PDUT_BVC_RESET:
372 case BSSGP_PDUT_BVC_RESET_ACK:
373 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
374 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n",
375 bctx->bvci, bssgp_pdu_str(pdu_type));
376 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400377 break;
378 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200379 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n",
Harald Welte356ac612017-05-25 19:05:32 +0200380 bctx->bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400381 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
382 break;
383 }
384 return rc;
385}
386
387/* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200388static 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 +0400389{
390 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
Maxa49475b2017-05-23 17:53:38 +0200391 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400392 int rc = 0;
Vadim Yanitskiyce27d1e2019-11-08 04:18:46 +0700393 int bvci = bctx ? bctx->bvci : msgb_bvci(msg);
Maxa49475b2017-05-23 17:53:38 +0200394 switch (pdu_type) {
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400395 case BSSGP_PDUT_STATUS:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200396 /* already handled in libosmogb */
397 OSMO_ASSERT(0);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400398 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200399 case BSSGP_PDUT_SUSPEND_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200400 case BSSGP_PDUT_RESUME_ACK:
401 case BSSGP_PDUT_BVC_BLOCK_ACK:
402 LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n",
403 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200404 break;
405 case BSSGP_PDUT_BVC_RESET_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200406 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100407 if (!the_pcu->bssgp.bvc_sig_reset)
408 the_pcu->bssgp.bvc_sig_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200409 else
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100410 the_pcu->bssgp.bvc_reset = 1;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200411 bvc_timeout(NULL);
412 break;
Pau Espin Pedrol65a0d1d2019-12-10 19:29:22 +0100413 case BSSGP_PDUT_PAGING_CS:
414 gprs_bssgp_pcu_rx_paging_cs(msg, tp);
415 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200416 case BSSGP_PDUT_PAGING_PS:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200417 gprs_bssgp_pcu_rx_paging_ps(msg, tp);
418 break;
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200419 case BSSGP_PDUT_BVC_UNBLOCK_ACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200420 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100421 the_pcu->bssgp.bvc_unblocked = 1;
422 if (the_pcu->bssgp.on_unblock_ack)
423 the_pcu->bssgp.on_unblock_ack(&the_pcu->bssgp);
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200424 bvc_timeout(NULL);
425 break;
Harald Welte05d7b5d2017-07-29 10:16:44 +0200426 case BSSGP_PDUT_SUSPEND_NACK:
427 case BSSGP_PDUT_RESUME_NACK:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200428 case BSSGP_PDUT_FLUSH_LL:
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200429 case BSSGP_PDUT_SGSN_INVOKE_TRACE:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200430 LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n",
431 bvci, bssgp_pdu_str(pdu_type));
432 break;
433 /* See TS 08.18 5.4.1 */
434 case BSSGP_PDUT_UL_UNITDATA:
435 case BSSGP_PDUT_DL_UNITDATA:
436 case BSSGP_PDUT_RA_CAPABILITY:
437 case BSSGP_PDUT_PTM_UNITDATA:
438 case BSSGP_PDUT_RA_CAPA_UDPATE:
439 case BSSGP_PDUT_RA_CAPA_UPDATE_ACK:
440 case BSSGP_PDUT_RADIO_STATUS:
441 case BSSGP_PDUT_FLOW_CONTROL_BVC:
442 case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK:
443 case BSSGP_PDUT_FLOW_CONTROL_MS:
444 case BSSGP_PDUT_FLOW_CONTROL_MS_ACK:
445 case BSSGP_PDUT_DOWNLOAD_BSS_PFC:
446 case BSSGP_PDUT_CREATE_BSS_PFC:
447 case BSSGP_PDUT_CREATE_BSS_PFC_ACK:
448 case BSSGP_PDUT_CREATE_BSS_PFC_NACK:
449 case BSSGP_PDUT_MODIFY_BSS_PFC:
450 case BSSGP_PDUT_MODIFY_BSS_PFC_ACK:
451 case BSSGP_PDUT_DELETE_BSS_PFC:
452 case BSSGP_PDUT_DELETE_BSS_PFC_ACK:
453 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n",
454 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200455 break;
456 default:
Harald Welte05d7b5d2017-07-29 10:16:44 +0200457 LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n",
Maxa49475b2017-05-23 17:53:38 +0200458 bvci, bssgp_pdu_str(pdu_type));
Holger Hans Peter Freythera9744de2013-07-28 19:01:20 +0200459 rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg);
460 break;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400461 }
462 return rc;
463}
464
Holger Hans Peter Freyther90d5df42013-07-28 21:14:15 +0200465static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400466{
467 struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg);
468 struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg);
469 struct tlv_parsed tp;
Maxa49475b2017-05-23 17:53:38 +0200470 enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type;
Max2813f932017-06-27 17:18:40 +0200471 uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg);
Alexander Couzens82519262021-02-22 02:08:18 +0100472 uint16_t bvci;
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400473 int data_len;
474 int rc = 0;
Andreas Eversberg3e372d52012-07-06 09:28:15 +0200475 struct bssgp_bvc_ctx *bctx;
476
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100477 switch (pdu_type) {
478 case BSSGP_PDUT_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100479 /* Pass the message to the generic BSSGP parser, which handles
Max2813f932017-06-27 17:18:40 +0200480 * STATUS and RESET messages in either direction. */
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100481 case BSSGP_PDUT_RAN_INFO:
482 case BSSGP_PDUT_RAN_INFO_REQ:
483 case BSSGP_PDUT_RAN_INFO_ACK:
484 case BSSGP_PDUT_RAN_INFO_ERROR:
485 case BSSGP_PDUT_RAN_INFO_APP_ERROR:
486 /* Also pass all RIM related messages to the generic BSSGP
487 * parser so that it can deliver primitive to the RIM SAP
488 * (SAP_BSSGP_RIM) */
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100489 return bssgp_rcvmsg(msg);
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100490 default:
491 break;
492 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400493
494 /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */
495
496 /* UNITDATA BSSGP headers have TLLI in front */
497 if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA)
498 {
499 data_len = msgb_bssgp_len(msg) - sizeof(*bgph);
500 rc = bssgp_tlv_parse(&tp, bgph->data, data_len);
501 }
502 else
503 {
504 data_len = msgb_bssgp_len(msg) - sizeof(*budh);
505 rc = bssgp_tlv_parse(&tp, budh->data, data_len);
506 }
Stefan Sperling3df15322018-06-25 12:17:39 +0200507 if (rc < 0) {
508 LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n",
509 bssgp_pdu_str(pdu_type), msgb_hexdump(msg));
Harald Welte7b7f2042019-07-21 09:26:20 +0200510 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
Stefan Sperling3df15322018-06-25 12:17:39 +0200511 }
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400512
Max2813f932017-06-27 17:18:40 +0200513 if (pdu_type == BSSGP_PDUT_BVC_RESET) {
Alexander Couzens82519262021-02-22 02:08:18 +0100514 if (ns_bvci != BVCI_SIGNALLING || !TLVP_PRESENT(&tp, BSSGP_IE_BVCI)) {
515 LOGP(DBSSGP, LOGL_ERROR, "Rx an invalid BVC-RESET %s\n", msgb_hexdump(msg));
516 return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg);
517 }
518
519 bvci = tlvp_val16be(&tp, BSSGP_IE_BVCI);
520 if (bvci != BVCI_SIGNALLING && bvci != the_pcu->bssgp.bctx->bvci) {
521 LOGP(DBSSGP, LOGL_ERROR, "Rx BVC-RESET for an unknown BVCI %d\n", bvci);
522 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, &bvci, msg);
523 }
524
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100525 return bssgp_rcvmsg(msg);
Max2813f932017-06-27 17:18:40 +0200526 }
527
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400528 /* look-up or create the BTS context for this BVC */
529 bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg));
530
Vadim Yanitskiyef444142019-11-08 04:02:31 +0700531 if (!bctx && ns_bvci != BVCI_SIGNALLING)
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400532 {
Max2813f932017-06-27 17:18:40 +0200533 LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n",
534 nsei, ns_bvci, bssgp_pdu_str(pdu_type));
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400535 return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg);
536 }
537
538 if (bctx)
539 {
Neels Hofmeyra01e2ee2017-02-23 18:10:13 +0100540 log_set_context(LOG_CTX_GB_BVC, bctx);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400541 rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]);
542 rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg));
543 }
544
545 if (ns_bvci == BVCI_SIGNALLING)
546 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200547 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400548 rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx);
549 }
550 else if (ns_bvci == BVCI_PTM)
551 {
Andreas Eversberg0e403092012-07-06 11:04:57 +0200552 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n");
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400553 rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg);
554 }
555 else
556 {
Pau Espin Pedroldb12f252019-12-23 14:27:54 +0100557 LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci);
Ivan Kluchnikov8ee60512012-03-05 19:24:57 +0400558 rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx);
559 }
560 return rc;
561}
Harald Welte477e79e2012-06-18 12:21:03 +0800562
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100563static void handle_nm_status(struct osmo_bssgp_prim *bp)
564{
565 enum gprs_bssgp_cause cause;
566
567 LOGP(DPCU, LOGL_DEBUG,
568 "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n",
569 bp->bvci, bp->nsei);
570
571 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE))
572 return;
573
574 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
575
576 if (cause != BSSGP_CAUSE_BVCI_BLOCKED &&
577 cause != BSSGP_CAUSE_UNKNOWN_BVCI)
578 return;
579
580 if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI))
581 return;
582
Pau Espin Pedroldb5e3392021-01-21 18:02:40 +0100583 if (the_pcu->bssgp.bctx->bvci != bp->bvci) {
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100584 LOGP(DPCU, LOGL_NOTICE,
585 "Received BSSGP STATUS message for an unknown BVCI (%d), "
586 "ignored\n",
587 bp->bvci);
588 return;
589 }
590
591 switch (cause) {
592 case BSSGP_CAUSE_BVCI_BLOCKED:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100593 if (the_pcu->bssgp.bvc_unblocked) {
594 the_pcu->bssgp.bvc_unblocked = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100595 bvc_timeout(NULL);
596 }
597 break;
598
599 case BSSGP_CAUSE_UNKNOWN_BVCI:
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100600 if (the_pcu->bssgp.bvc_reset) {
601 the_pcu->bssgp.bvc_reset = 0;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100602 bvc_timeout(NULL);
603 }
604 break;
605 default:
606 break;
607 }
608}
609
Harald Welte477e79e2012-06-18 12:21:03 +0800610int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
611{
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100612 struct osmo_bssgp_prim *bp;
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100613 int rc;
614 enum gprs_bssgp_cause cause;
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100615 bp = container_of(oph, struct osmo_bssgp_prim, oph);
616
617 switch (oph->sap) {
618 case SAP_BSSGP_NM:
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100619 switch (oph->primitive) {
620 case PRIM_NM_STATUS:
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100621 handle_nm_status(bp);
Alexander Couzens423bf8c2021-02-17 02:31:37 +0100622 break;
623 case PRIM_NM_BVC_RESET:
624 /* received a BVC PTP reset */
625 LOGP(DPCU, LOGL_INFO, "Rx BVC_RESET on bvci %d\n", bp->bvci);
626 /* Rx Reset from SGSN */
627 if (bp->bvci == BVCI_SIGNALLING) {
628 if (TLVP_PRES_LEN(bp->tp, BSSGP_IE_CAUSE, 1))
629 cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE);
630 else {
631 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", bp->nsei);
632 break;
633 }
634
635 rc = bssgp_tx_bvc_ptp_reset(bp->nsei, cause);
636 if (rc < 0) {
637 LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", bp->nsei, rc);
638 break;
639 }
640 the_pcu->bssgp.bvc_sig_reset = 1;
641 the_pcu->bssgp.bvc_reset = 0;
642 the_pcu->bssgp.bvc_unblocked = 0;
643 } else if (bp->bvci == the_pcu->bssgp.bctx->bvci) {
644 the_pcu->bssgp.bvc_reset = 1;
645 the_pcu->bssgp.bvc_unblocked = 0;
646 bvc_timeout(NULL);
647 }
648 break;
649 }
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100650 break;
Pau Espin Pedrol3a271022021-01-21 18:44:23 +0100651 case SAP_BSSGP_RIM:
652 return handle_rim(bp);
Jacob Erlbeck39645b82015-03-16 12:27:11 +0100653 default:
654 break;
655 }
Harald Welte477e79e2012-06-18 12:21:03 +0800656 return 0;
657}
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200658
Alexander Couzens290d9032020-09-16 21:52:02 +0200659void gprs_ns_prim_status_cb(struct osmo_gprs_ns2_prim *nsp)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200660{
Alexander Couzens290d9032020-09-16 21:52:02 +0200661 switch (nsp->u.status.cause) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100662 case GPRS_NS2_AFF_CAUSE_SNS_CONFIGURED:
Alexander Couzens290d9032020-09-16 21:52:02 +0200663 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d SNS configured.\n", nsp->nsei);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200664 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100665 case GPRS_NS2_AFF_CAUSE_RECOVERY:
Alexander Couzens290d9032020-09-16 21:52:02 +0200666 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became available\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100667 if (!the_pcu->bssgp.nsvc_unblocked) {
668 the_pcu->bssgp.bvc_sig_reset = 0;
669 the_pcu->bssgp.bvc_reset = 0;
670 the_pcu->bssgp.nsvc_unblocked = 1;
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200671 bvc_timeout(NULL);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200672 }
673 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100674 case GPRS_NS2_AFF_CAUSE_FAILURE:
Alexander Couzens290d9032020-09-16 21:52:02 +0200675 LOGP(DPCU, LOGL_NOTICE, "NS-NSE %d became unavailable\n", nsp->nsei);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100676 if (the_pcu->bssgp.nsvc_unblocked) {
677 the_pcu->bssgp.nsvc_unblocked = 0;
678 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
679 the_pcu->bssgp.bvc_sig_reset = 0;
680 the_pcu->bssgp.bvc_reset = 0;
681 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200682 }
683 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100684 case GPRS_NS2_AFF_CAUSE_SNS_FAILURE:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100685 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200686 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100687 LOGP(DPCU, LOGL_DEBUG,
688 "NS: %s Unknown affecting cause %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200689 get_value_string(osmo_prim_op_names, nsp->oph.operation),
Alexander Couzens30d9a592020-10-27 01:25:51 +0100690 gprs_ns2_aff_cause_prim_str(nsp->u.status.cause), nsp->u.status.cause);
Alexander Couzens290d9032020-09-16 21:52:02 +0200691 break;
692 }
693}
694
695/* called by the ns layer */
696int gprs_ns_prim_cb(struct osmo_prim_hdr *oph, void *ctx)
697{
698 struct osmo_gprs_ns2_prim *nsp;
699 int rc = 0;
700
701 if (oph->sap != SAP_NS)
702 return 0;
703
704 nsp = container_of(oph, struct osmo_gprs_ns2_prim, oph);
705
706 if (oph->operation != PRIM_OP_INDICATION) {
707 LOGP(DPCU, LOGL_NOTICE, "NS: %s Unknown prim %d from NS\n",
708 get_value_string(osmo_prim_op_names, oph->operation),
709 oph->operation);
Alexander Couzensa2848542020-10-26 00:33:31 +0100710 goto out;
Alexander Couzens290d9032020-09-16 21:52:02 +0200711 }
712
713 switch (oph->primitive) {
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100714 case GPRS_NS2_PRIM_UNIT_DATA:
Alexander Couzens290d9032020-09-16 21:52:02 +0200715 /* hand the message into the BSSGP implementation */
716 /* add required msg fields for Gb layer */
717 msgb_bssgph(oph->msg) = oph->msg->l3h;
718 msgb_bvci(oph->msg) = nsp->bvci;
719 msgb_nsei(oph->msg) = nsp->nsei;
720 rc = gprs_bssgp_pcu_rcvmsg(oph->msg);
721 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100722 case GPRS_NS2_PRIM_STATUS:
Alexander Couzens290d9032020-09-16 21:52:02 +0200723 gprs_ns_prim_status_cb(nsp);
724 break;
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100725 case GPRS_NS2_PRIM_CONGESTION:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100726 break;
Alexander Couzens290d9032020-09-16 21:52:02 +0200727 default:
Alexander Couzens30d9a592020-10-27 01:25:51 +0100728 LOGP(DPCU, LOGL_DEBUG,
729 "NS: %s Unknown prim %s / %d from NS\n",
Alexander Couzens290d9032020-09-16 21:52:02 +0200730 get_value_string(osmo_prim_op_names, oph->operation),
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100731 gprs_ns2_prim_str((enum gprs_ns2_prim) oph->primitive), oph->primitive);
Jacob Erlbeckebebad12015-08-17 14:43:52 +0200732 break;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200733 }
734
Alexander Couzensa2848542020-10-26 00:33:31 +0100735out:
736 if (oph->msg)
737 msgb_free(oph->msg);
738
Alexander Couzens290d9032020-09-16 21:52:02 +0200739 return rc;
740}
741
742/* called by the bssgp layer to send NS PDUs */
743int gprs_gp_send_cb(void *ctx, struct msgb *msg)
744{
745 struct gprs_ns2_inst *nsi = (struct gprs_ns2_inst *) ctx;
746 struct osmo_gprs_ns2_prim nsp = {};
747 nsp.nsei = msgb_nsei(msg);
748 nsp.bvci = msgb_bvci(msg);
Alexander Couzensf7ec5252021-01-25 20:28:38 +0100749 osmo_prim_init(&nsp.oph, SAP_NS, GPRS_NS2_PRIM_UNIT_DATA,
Alexander Couzens290d9032020-09-16 21:52:02 +0200750 PRIM_OP_REQUEST, msg);
751 return gprs_ns2_recv_prim(nsi, &nsp.oph);
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +0200752}
753
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200754static unsigned count_pdch(const struct gprs_rlcmac_bts *bts)
755{
756 size_t trx_no, ts_no;
757 unsigned num_pdch = 0;
758
759 for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) {
760 const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no];
761
762 for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) {
763 const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no];
764
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100765 if (pdch->m_is_enabled)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200766 num_pdch += 1;
767 }
768 }
769
770 return num_pdch;
771}
772
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200773static uint32_t gprs_bssgp_max_leak_rate(enum CodingScheme cs, int num_pdch)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200774{
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200775 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 +0200776
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100777 /* n byte payload per 20ms */
778 return bytes_per_rlc_block * (1000 / 20) * num_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200779}
780
781static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts,
782 uint32_t leak_rate, uint32_t fallback)
783{
784 uint32_t bucket_size = 0;
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100785 uint16_t bucket_time = the_pcu->vty.fc_bucket_time;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200786
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200787 if (bucket_time == 0)
Pau Espin Pedrolf473ec92021-01-14 14:45:14 +0100788 bucket_time = the_pcu->vty.force_llc_lifetime;
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200789
790 if (bucket_time == 0xffff)
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200791 bucket_size = FC_MAX_BUCKET_SIZE;
792
Jacob Erlbeck0288cdb2015-05-06 10:47:30 +0200793 if (bucket_size == 0 && bucket_time && leak_rate)
794 bucket_size = (uint64_t)leak_rate * bucket_time / 100;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200795
796 if (bucket_size == 0 && leak_rate)
797 bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS;
798
799 if (bucket_size == 0)
800 bucket_size = fallback;
801
802 if (bucket_size > FC_MAX_BUCKET_SIZE)
803 bucket_size = FC_MAX_BUCKET_SIZE;
804
805 return bucket_size;
806}
807
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200808static uint32_t get_and_reset_avg_queue_delay(void)
809{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100810 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Jacob Erlbeck0ae43132015-06-11 12:03:18 +0200811 uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 +
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100812 delay_sum->tv_nsec / 1000000000;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200813 uint32_t avg_delay_ms = 0;
814
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100815 if (the_pcu->bssgp.queue_delay_count > 0)
816 avg_delay_ms = delay_sum_ms / the_pcu->bssgp.queue_delay_count;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200817
818 /* Reset accumulator */
Pau Espin Pedrol1de68732020-03-11 14:04:52 +0100819 delay_sum->tv_sec = delay_sum->tv_nsec = 0;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100820 the_pcu->bssgp.queue_delay_count = 0;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200821
822 return avg_delay_ms;
823}
824
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200825static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch)
826{
827 int rate; /* byte per second */
828
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100829 if (the_pcu->bssgp.queue_frames_sent == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200830 return -1;
831
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100832 if (the_pcu->bssgp.queue_frames_recv == 0)
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200833 return -1;
834
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100835 *usage_by_1000 = the_pcu->bssgp.queue_frames_recv * 1000 /
836 the_pcu->bssgp.queue_frames_sent;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200837
838 /* 20ms/num_pdch is the average RLC block duration, so the rate is
839 * calculated as:
840 * rate = bytes_recv / (block_dur * block_count) */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100841 rate = the_pcu->bssgp.queue_bytes_recv * 1000 * num_pdch /
842 (20 * the_pcu->bssgp.queue_frames_recv);
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200843
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100844 the_pcu->bssgp.queue_frames_sent = 0;
845 the_pcu->bssgp.queue_bytes_recv = 0;
846 the_pcu->bssgp.queue_frames_recv = 0;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200847
848 return rate;
849}
850
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200851static enum CodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts)
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100852{
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100853 int num = 0;
854 int i;
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100855 bool mcs_any = false;
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100856
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100857 /* First check if we support any MCS: */
858 for (i = 8; i >= 0; i--) {
859 if (bts->mcs_mask & (1 << i)) {
860 num = i + 1;
861 mcs_any = true;
862 break;
863 }
864 }
865
866 if (mcs_any) {
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100867 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100868 if (bts->initial_mcs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100869 num = bts->initial_mcs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100870 } else {
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100871 /* We found "num" for free in the loop above */
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100872 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100873 } else if (bts_max_mcs_dl(bts)) {
874 num = bts_max_mcs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100875 } else {
876 num = 9;
877 }
878
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100879 if (num)
880 return mcs_get_egprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100881 }
882
Pau Espin Pedrole8dcf642021-01-14 13:17:01 +0100883 if (!the_pcu->vty.cs_adj_enabled) {
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100884 if (bts->initial_cs_dl) {
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100885 num = bts->initial_cs_dl;
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100886 } else {
887 for (i = 3; i >= 0; i--) {
888 if (bts->cs_mask & (1 << i)) {
889 num = i + 1;
890 break;
891 }
892 }
893 }
Pau Espin Pedrol2182e622021-01-14 16:48:38 +0100894 } else if (bts_max_cs_dl(bts)) {
895 num = bts_max_cs_dl(bts);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100896 }
897
Pau Espin Pedrold87722d2020-10-30 17:14:26 +0100898 if (!num)
899 num = 4;
900
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200901 return mcs_get_gprs_by_num(num);
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100902}
903
Pau Espin Pedrol3f064f52019-12-10 17:09:51 +0100904static int gprs_bssgp_tx_fc_bvc(void)
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200905{
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200906 struct gprs_rlcmac_bts *bts;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200907 uint32_t bucket_size; /* oct */
908 uint32_t ms_bucket_size; /* oct */
909 uint32_t leak_rate; /* oct/s */
910 uint32_t ms_leak_rate; /* oct/s */
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +0200911 uint32_t avg_delay_ms;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200912 int num_pdch = -1;
Pau Espin Pedrol2ae83372020-05-18 11:35:35 +0200913 enum CodingScheme max_cs_dl;
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200914
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +0100915 if (!the_pcu->bssgp.bctx) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +0200916 LOGP(DBSSGP, LOGL_ERROR, "No bctx\n");
917 return -EIO;
918 }
Pau Espin Pedrold1049dc2021-01-18 17:14:14 +0100919
920 /* FIXME: This calculation needs to be redone to support multiple BTS */
921 bts = llist_first_entry_or_null(&the_pcu->bts_list, struct gprs_rlcmac_bts, list);
922 if (!bts) {
923 LOGP(DBSSGP, LOGL_ERROR, "No bts\n");
924 return -EIO;
925 }
Jacob Erlbeck87d73412015-04-21 12:56:48 +0200926
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100927 max_cs_dl = max_coding_scheme_dl(bts);
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200928
Pau Espin Pedrole8912222021-01-14 14:03:17 +0100929 bucket_size = the_pcu->vty.fc_bvc_bucket_size;
930 leak_rate = the_pcu->vty.fc_bvc_leak_rate;
931 ms_bucket_size = the_pcu->vty.fc_ms_bucket_size;
932 ms_leak_rate = the_pcu->vty.fc_ms_leak_rate;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200933
Pau Espin Pedrol87eec1f2020-10-30 18:46:24 +0100934 /* FIXME: This calculation is mostly wrong. It should be done based on
935 currently established TBF (and whether the related (egprs)_ms_class
936 as per which CS/MCS they support). */
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200937 if (leak_rate == 0) {
Jacob Erlbeck2db0f082015-09-07 18:49:00 +0200938 int meas_rate;
939 int usage; /* in 0..1000 */
940
941 if (num_pdch < 0)
942 num_pdch = count_pdch(bts);
943
944 meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch);
945 if (meas_rate > 0) {
946 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
947 leak_rate =
948 (meas_rate * usage + leak_rate * (1000 - usage)) /
949 1000;
950 LOGP(DBSSGP, LOGL_DEBUG,
951 "Estimated BVC leak rate = %d "
952 "(measured %d, usage %d%%)\n",
953 leak_rate, meas_rate, usage/10);
954 }
955 }
956
957 if (leak_rate == 0) {
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200958 if (num_pdch < 0)
959 num_pdch = count_pdch(bts);
960
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200961 leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200962
963 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100964 "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100965 leak_rate, num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200966 };
967
968 if (ms_leak_rate == 0) {
969 int ms_num_pdch;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +0100970 int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts, 0);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200971
972 if (num_pdch < 0)
973 num_pdch = count_pdch(bts);
974
975 ms_num_pdch = num_pdch;
Jacob Erlbeck7f79f0d2015-07-17 11:38:49 +0200976 if (max_pdch > FC_MS_MAX_RX_SLOTS)
977 max_pdch = FC_MS_MAX_RX_SLOTS;
978 if (ms_num_pdch > max_pdch)
979 ms_num_pdch = max_pdch;
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200980
Jacob Erlbeck6eed1912015-07-21 18:29:46 +0200981 ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200982
983 /* TODO: To properly support multiple TRX, the per MS leak rate
984 * should be derived from the max number of PDCH TS per TRX.
985 */
986 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck7f28c972016-02-04 17:54:42 +0100987 "Computed MS default leak rate = %d, ms_num_pdch = %d, "
988 "cs = %s\n",
Max136ebcc2019-03-05 14:59:03 +0100989 ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl));
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +0200990 };
991
992 /* TODO: Force leak_rate to 0 on buffer bloat */
993
994 if (bucket_size == 0)
995 bucket_size = compute_bucket_size(bts, leak_rate,
996 FC_FALLBACK_BVC_BUCKET_SIZE);
997
998 if (ms_bucket_size == 0)
999 ms_bucket_size = compute_bucket_size(bts, ms_leak_rate,
1000 FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size));
1001
1002 if (leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1003 leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1004
1005 if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE)
1006 ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE;
1007
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001008 /* Avg queue delay monitoring */
1009 avg_delay_ms = get_and_reset_avg_queue_delay();
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001010
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001011 /* Update tag */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001012 the_pcu->bssgp.fc_tag += 1;
Jacob Erlbeck6eeb7c72015-05-06 15:26:08 +02001013
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001014 LOGP(DBSSGP, LOGL_DEBUG,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001015 "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, "
1016 "R_MS = %d, avg_dly = %d\n",
1017 bucket_size, leak_rate, ms_bucket_size, ms_leak_rate,
1018 avg_delay_ms);
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001019
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001020 return bssgp_tx_fc_bvc(the_pcu->bssgp.bctx, the_pcu->bssgp.fc_tag,
Jacob Erlbeck3d62fc52015-04-30 15:01:15 +02001021 bucket_size, leak_rate,
1022 ms_bucket_size, ms_leak_rate,
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001023 NULL, &avg_delay_ms);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001024}
1025
1026static void bvc_timeout(void *_priv)
1027{
Pau Espin Pedrol5211d9d2019-09-05 16:20:22 +02001028 unsigned long secs;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001029 if (!the_pcu->bssgp.bvc_sig_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001030 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001031 bssgp_tx_bvc_reset(the_pcu->bssgp.bctx, 0, BSSGP_CAUSE_OML_INTERV);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001032 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001033 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001034 return;
1035 }
1036
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001037 if (!the_pcu->bssgp.bvc_reset) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001038 LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001039 the_pcu->bssgp.bctx->bvci);
1040 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 +02001041 secs = osmo_tdef_get(the_pcu->T_defs, -102, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001042 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001043 return;
1044 }
1045
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001046 if (!the_pcu->bssgp.bvc_unblocked) {
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001047 LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001048 the_pcu->bssgp.bctx->bvci);
1049 bssgp_tx_bvc_unblock(the_pcu->bssgp.bctx);
Pau Espin Pedrol14339f62021-04-26 14:14:37 +02001050 secs = osmo_tdef_get(the_pcu->T_defs, -101, OSMO_TDEF_S, -1);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001051 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, secs, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001052 return;
1053 }
1054
1055 LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n",
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001056 the_pcu->bssgp.bctx->bvci);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001057 gprs_bssgp_tx_fc_bvc();
Pau Espin Pedrole8912222021-01-14 14:03:17 +01001058 osmo_timer_schedule(&the_pcu->bssgp.bvc_timer, the_pcu->vty.fc_interval, 0);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001059}
1060
Alexander Couzens13a12e22021-02-15 05:50:10 +01001061/*! configure NS layer
1062 *
1063 * \param bts pointer to the bts object
1064 * \param nsei the NSEI of the BSS
1065 * \param local pointer to an array of local address to bind on.
1066 * \param remote pointer to an array of remote address SGSNs. If dynamic IP-SNS is used remote is used as initial SGSN endpoints.
1067 * \param nsvci pointer to an array of nsvcis
1068 * \param valid bitmask. a 1 means the position in the array contains a valid entry for local, remote, nsvci
1069 * \returns 0 if the configuration has succeeded. on error != 0
1070 */
1071static int ns_configure_nse(struct gprs_rlcmac_bts *bts,
1072 uint16_t nsei,
1073 const struct osmo_sockaddr *local,
1074 const struct osmo_sockaddr *remote,
1075 const uint16_t *nsvci,
1076 uint16_t valid)
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001077{
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001078 int i, rc;
1079 uint16_t binds = 0;
1080 bool nsvcs = false;
Alexander Couzens290d9032020-09-16 21:52:02 +02001081 struct gprs_ns2_vc *nsvc;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001082 struct gprs_ns2_vc_bind *bind[PCU_IF_NUM_NSVC] = { };
Alexander Couzensb644fd12020-12-06 01:24:02 +01001083 char name[5];
Alexander Couzens13a12e22021-02-15 05:50:10 +01001084 bool sns_configured = false;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001085
1086 if (!valid)
1087 return -1;
1088
Alexander Couzens13a12e22021-02-15 05:50:10 +01001089 bts->nse = gprs_ns2_nse_by_nsei(the_pcu->nsi, nsei);
1090 if (!bts->nse)
1091 bts->nse = gprs_ns2_create_nse(the_pcu->nsi, nsei,
1092 GPRS_NS2_LL_UDP, the_pcu->vty.ns_dialect);
1093 if (!bts->nse) {
1094 LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSE\n");
1095 return -1;
1096 }
1097
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001098 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1099 if (!(valid & (1 << i)))
1100 continue;
1101
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001102 if (!gprs_ns2_ip_bind_by_sockaddr(the_pcu->nsi, &local[i])) {
Alexander Couzensb644fd12020-12-06 01:24:02 +01001103 snprintf(name, sizeof(name), "pcu%d", i);
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001104 rc = gprs_ns2_ip_bind(the_pcu->nsi, name, &local[i], 0, &bind[i]);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001105 if (rc < 0) {
1106 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to %s\n", osmo_sockaddr_to_str(&local[i]));
1107 continue;
1108 }
Alexander Couzens151bc5b2021-02-15 05:53:12 +01001109
1110 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
1111 rc = gprs_ns2_sns_add_bind(bts->nse, bind[i]);
1112 if (rc < 0) {
1113 LOGP(DBSSGP, LOGL_ERROR, "Failed to add bind %s to the NSE for IP-SNS\n", osmo_sockaddr_to_str(&local[i]));
1114 continue;
1115 }
1116 }
Harald Welted9367e32021-04-29 22:02:47 +02001117
1118 if (the_pcu->vty.ns_ip_dscp != -1)
1119 gprs_ns2_ip_bind_set_dscp(bind[i], the_pcu->vty.ns_ip_dscp);
1120 if (the_pcu->vty.ns_priority != -1)
1121 gprs_ns2_ip_bind_set_priority(bind[i], the_pcu->vty.ns_priority);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001122 }
1123
1124 binds |= 1 << i;
1125 }
1126
1127 if (!binds) {
1128 LOGP(DBSSGP, LOGL_ERROR, "Failed to bind to any NS-VC\n");
Alexander Couzens13a12e22021-02-15 05:50:10 +01001129 gprs_ns2_free_nses(the_pcu->nsi);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001130 return -1;
1131 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001132
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001133 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
1134 if (!(binds & (1 << i)))
1135 continue;
1136
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001137 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens1e6c3502020-12-09 01:41:24 +01001138 rc = gprs_ns2_sns_add_endpoint(bts->nse, &remote[i]);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001139 if (rc && rc != -EALREADY) {
1140 LOGP(DBSSGP, LOGL_ERROR, "Failed to add SNS endpoint %s!\n", osmo_sockaddr_to_str(&remote[i]));
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001141 return rc;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001142 } else {
1143 sns_configured = true;
1144 }
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001145 } else {
1146 nsvc = gprs_ns2_ip_connect(bind[i], &remote[i], bts->nse, nsvci[i]);
1147 if (nsvc)
1148 nsvcs = true;
1149 else
1150 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect to towards SGSN %s!\n", osmo_sockaddr_to_str(&remote[i]));
1151 }
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001152 }
1153
Alexander Couzens13a12e22021-02-15 05:50:10 +01001154 if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS)
1155 return sns_configured ? 0 : -1;
1156 else
1157 return nsvcs ? 0 : -1;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001158}
1159
1160struct nsvc_cb {
1161 const struct osmo_sockaddr *local;
1162 const struct osmo_sockaddr *remote;
1163 const uint16_t *nsvci;
1164 /* [in] bitmask of valid nsvc in local/remote */
1165 uint16_t valid;
1166 /* [out] bitmask of found nsvcs */
1167 uint16_t found;
1168};
1169
1170static int ns_conf_vc_cb(struct gprs_ns2_vc *nsvc, void *ctx)
1171{
1172 struct nsvc_cb *data = (struct nsvc_cb *) ctx;
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001173 unsigned int i;
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001174
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001175 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001176 if (!(data->valid & (1 << i)))
1177 continue;
1178 if (data->found & (1 << i))
1179 continue;
1180
1181 if (gprs_ns2_ip_vc_equal(nsvc, &data->local[i],
1182 &data->remote[i],
1183 data->nsvci[i])) {
1184 data->found |= 1 << i;
1185 return 0;
1186 }
1187 }
1188
1189 /* Found an extra nsvc */
1190 LOGP(DBSSGP, LOGL_DEBUG, " Removing NSVC %s\n", gprs_ns2_ll_str(nsvc));
1191 gprs_ns2_free_nsvc(nsvc);
1192
1193 return 0;
1194}
1195
Alexander Couzens94a367f2021-02-16 16:57:15 +01001196/* update the ns configuration if needed */
1197int gprs_ns_update_config(struct gprs_rlcmac_bts *bts, uint16_t nsei,
1198 const struct osmo_sockaddr *local,
1199 const struct osmo_sockaddr *remote,
1200 uint16_t *nsvci, uint16_t valid)
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001201{
1202 int rc = 0;
1203 if (!bts->nse) {
1204 /* there shouldn't any previous state. */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001205 gprs_ns2_free_nses(the_pcu->nsi);
1206 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001207 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001208 } else if (nsei != gprs_ns2_nse_nsei(bts->nse)) {
1209 /* the NSEI has changed */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001210 gprs_ns2_free_nses(the_pcu->nsi);
1211 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001212 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzensf7ec5252021-01-25 20:28:38 +01001213 } else if (the_pcu->vty.ns_dialect == GPRS_NS2_DIALECT_SNS) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001214 /* SNS: check if the initial nsvc is the same, if not recreate it */
1215 const struct osmo_sockaddr *initial = gprs_ns2_nse_sns_remote(bts->nse);
Pau Espin Pedrol8a35e642021-01-18 17:14:14 +01001216 unsigned int i;
1217 for (i = 0; i < PCU_IF_NUM_NSVC; i++) {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001218 if (!(valid & (1 << i)))
1219 continue;
1220
1221 /* found the initial - everything should be fine */
1222 if (!osmo_sockaddr_cmp(initial, &remote[i]))
1223 return 0;
1224 }
1225
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001226 gprs_ns2_free_nses(the_pcu->nsi);
1227 gprs_ns2_free_binds(the_pcu->nsi);
Alexander Couzens13a12e22021-02-15 05:50:10 +01001228 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001229 } else {
Alexander Couzens5bece2a2020-10-12 02:27:22 +02001230 /* check if all NSVC are still the same. */
1231 struct nsvc_cb data = {
1232 .local = &local[0],
1233 .remote = &remote[0],
1234 .nsvci = &nsvci[0],
1235 .valid = valid,
1236 .found = 0,
1237 };
1238
1239 /* search the current active nsvcs */
1240 gprs_ns2_nse_foreach_nsvc(bts->nse, &ns_conf_vc_cb, &data);
1241
1242 /* we found all our valid nsvcs and might have removed all other nsvcs */
1243 if (valid == data.found)
1244 return 0;
1245
1246 /* remove all found nsvcs from the valid field */
1247 valid &= ~data.found;
Alexander Couzens13a12e22021-02-15 05:50:10 +01001248 rc = ns_configure_nse(bts, nsei, local, remote, nsvci, valid);
Alexander Couzens290d9032020-09-16 21:52:02 +02001249 }
1250
1251 if (rc)
1252 LOGP(DBSSGP, LOGL_ERROR, "Failed to connect!\n");
1253
1254 return rc;
Jacob Erlbeckebebad12015-08-17 14:43:52 +02001255}
1256
Alexander Couzens290d9032020-09-16 21:52:02 +02001257struct gprs_bssgp_pcu *gprs_bssgp_init(
1258 struct gprs_rlcmac_bts *bts,
1259 uint16_t nsei, uint16_t bvci,
1260 uint16_t mcc, uint16_t mnc, bool mnc_3_digits,
1261 uint16_t lac, uint16_t rac, uint16_t cell_id)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001262{
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001263
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001264 /* if already created... return the current address */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001265 if (the_pcu->bssgp.bctx)
1266 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001267
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001268 the_pcu->bssgp.bts = bts;
1269 the_pcu->bssgp.bctx = btsctx_alloc(bvci, nsei);
1270 if (!the_pcu->bssgp.bctx) {
Andreas Eversberg0e403092012-07-06 11:04:57 +02001271 LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n");
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001272 the_pcu->bssgp.bts->nse = NULL;
Holger Hans Peter Freythere8d9a5f2013-07-28 19:11:20 +02001273 return NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001274 }
Alexander Couzens423bf8c2021-02-17 02:31:37 +01001275 the_pcu->bssgp.bctx->is_sgsn = false;
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001276 the_pcu->bssgp.bctx->ra_id.mcc = spoof_mcc ? : mcc;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001277 if (spoof_mnc) {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001278 the_pcu->bssgp.bctx->ra_id.mnc = spoof_mnc;
1279 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001280 } else {
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001281 the_pcu->bssgp.bctx->ra_id.mnc = mnc;
1282 the_pcu->bssgp.bctx->ra_id.mnc_3_digits = mnc_3_digits;
Neels Hofmeyrbdc55fa2018-02-21 00:39:07 +01001283 }
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001284 the_pcu->bssgp.bctx->ra_id.lac = lac;
1285 the_pcu->bssgp.bctx->ra_id.rac = rac;
1286 the_pcu->bssgp.bctx->cell_id = cell_id;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001287
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001288 osmo_timer_setup(&the_pcu->bssgp.bvc_timer, bvc_timeout, bts);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001289
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001290 return &the_pcu->bssgp;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001291}
1292
Alexander Couzens290d9032020-09-16 21:52:02 +02001293void gprs_bssgp_destroy(struct gprs_rlcmac_bts *bts)
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001294{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001295 osmo_timer_del(&the_pcu->bssgp.bvc_timer);
Andreas Eversbergcd8a83a2012-09-23 06:41:21 +02001296
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001297 /* FIXME: blocking... */
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001298 the_pcu->bssgp.nsvc_unblocked = 0;
1299 the_pcu->bssgp.bvc_sig_reset = 0;
1300 the_pcu->bssgp.bvc_reset = 0;
1301 the_pcu->bssgp.bvc_unblocked = 0;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001302
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001303 bssgp_bvc_ctx_free(the_pcu->bssgp.bctx);
1304 the_pcu->bssgp.bctx = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001305
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001306 gprs_ns2_free(the_pcu->nsi);
1307 the_pcu->nsi = NULL;
Alexander Couzens290d9032020-09-16 21:52:02 +02001308 bts->nse = NULL;
Andreas Eversbergbf5a0f62012-07-06 08:58:22 +02001309}
1310
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001311void gprs_bssgp_update_frames_sent()
1312{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001313 the_pcu->bssgp.queue_frames_sent += 1;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001314}
1315
1316void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv)
1317{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001318 the_pcu->bssgp.queue_bytes_recv += bytes_recv;
1319 the_pcu->bssgp.queue_frames_recv += frames_recv;
Jacob Erlbeck2db0f082015-09-07 18:49:00 +02001320}
1321
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001322void gprs_bssgp_update_queue_delay(const struct timespec *tv_recv,
1323 const struct timespec *tv_now)
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001324{
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001325 struct timespec *delay_sum = &the_pcu->bssgp.queue_delay_sum;
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001326 struct timespec tv_delay;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001327
Pau Espin Pedrol1de68732020-03-11 14:04:52 +01001328 timespecsub(tv_now, tv_recv, &tv_delay);
1329 timespecadd(delay_sum, &tv_delay, delay_sum);
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001330
Pau Espin Pedrolac3fd122021-01-13 18:54:38 +01001331 the_pcu->bssgp.queue_delay_count += 1;
Jacob Erlbeck6e4ccec2015-05-06 14:24:36 +02001332}