Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 1 | /* gprs_bssgp_pcu.cpp |
| 2 | * |
| 3 | * Copyright (C) 2012 Ivan Klyuchnikov |
Holger Hans Peter Freyther | 416ce69 | 2013-08-22 08:44:04 +0200 | [diff] [blame] | 4 | * Copyright (C) 2013 by Holger Hans Peter Freyther |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 5 | * |
| 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> |
| 23 | #include <pcu_l1_if.h> |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 24 | #include <gprs_debug.h> |
Holger Hans Peter Freyther | 67ed34e | 2013-10-17 17:01:54 +0200 | [diff] [blame] | 25 | #include <bts.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 26 | #include <tbf.h> |
Max | 1187a77 | 2018-01-26 13:31:42 +0100 | [diff] [blame] | 27 | #include <gprs_coding_scheme.h> |
Max | 6dc90b8 | 2018-02-19 17:17:28 +0100 | [diff] [blame] | 28 | #include <pdch.h> |
Max | 136ebcc | 2019-03-05 14:59:03 +0100 | [diff] [blame] | 29 | |
| 30 | extern "C" { |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 31 | #include <osmocom/gsm/protocol/gsm_23_003.h> |
Max | 136ebcc | 2019-03-05 14:59:03 +0100 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
| 33 | #include "coding_scheme.h" |
| 34 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 35 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 36 | /* Tuning parameters for BSSGP flow control */ |
| 37 | #define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */ |
| 38 | #define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */ |
| 39 | #define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */ |
| 40 | #define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */ |
| 41 | |
| 42 | /* Constants for BSSGP flow control */ |
| 43 | #define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */ |
| 44 | #define FC_MAX_BUCKET_SIZE 6553500 /* Octets */ |
| 45 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 46 | static struct gprs_bssgp_pcu the_pcu = { 0, }; |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 47 | |
Andreas Eversberg | 71cce91 | 2013-01-16 13:49:00 +0100 | [diff] [blame] | 48 | extern void *tall_pcu_ctx; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 49 | extern uint16_t spoof_mcc, spoof_mnc; |
Neels Hofmeyr | bdc55fa | 2018-02-21 00:39:07 +0100 | [diff] [blame] | 50 | extern bool spoof_mnc_3_digits; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 51 | |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 52 | static void bvc_timeout(void *_priv); |
Harald Welte | b26854c | 2019-02-21 17:17:07 +0100 | [diff] [blame] | 53 | static int gprs_ns_reconnect(struct gprs_nsvc *nsvc); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 54 | |
Pau Espin Pedrol | 7490622 | 2018-03-17 01:43:12 +0100 | [diff] [blame] | 55 | #if 0 |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 56 | static int parse_ra_cap(struct tlv_parsed *tp, MS_Radio_Access_capability_t *rac) |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 57 | { |
| 58 | bitvec *block; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 59 | uint8_t cap_len; |
| 60 | uint8_t *cap; |
| 61 | |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 62 | memset(rac, 0, sizeof(*rac)); |
| 63 | |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 64 | if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP)) |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 65 | return -EINVAL; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 66 | |
| 67 | cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP); |
| 68 | cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP); |
| 69 | |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 70 | LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len); |
| 71 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 72 | block = bitvec_alloc(cap_len, tall_pcu_ctx); |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 73 | bitvec_unpack(block, cap); |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 74 | |
| 75 | /* TS 24.008, 10.5.5.12a */ |
| 76 | decode_gsm_ra_cap(block, rac); |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 77 | |
| 78 | bitvec_free(block); |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 79 | return 0; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 80 | } |
Pau Espin Pedrol | 7490622 | 2018-03-17 01:43:12 +0100 | [diff] [blame] | 81 | #endif |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 82 | |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 83 | static int gprs_bssgp_pcu_rx_dl_ud(struct msgb *msg, struct tlv_parsed *tp) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 84 | { |
| 85 | struct bssgp_ud_hdr *budh; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 86 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 87 | uint32_t tlli; |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 88 | uint32_t tlli_old = 0; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 89 | uint8_t *data; |
| 90 | uint16_t len; |
Vadim Yanitskiy | 2c076bc | 2019-07-24 18:31:19 +0700 | [diff] [blame] | 91 | char imsi[OSMO_IMSI_BUF_SIZE] = "000"; |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 92 | uint8_t ms_class = 0; |
| 93 | uint8_t egprs_ms_class = 0; |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 94 | int rc; |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 95 | #if 0 |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 96 | MS_Radio_Access_capability_t rac; |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 97 | #endif |
Andreas Eversberg | 7d7cf54 | 2012-06-25 09:26:15 +0200 | [diff] [blame] | 98 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 99 | budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 100 | tlli = ntohl(budh->tlli); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 101 | |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 102 | /* LLC_PDU is mandatory IE */ |
| 103 | if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 104 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 105 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD missing mandatory IE\n", tlli); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 106 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 107 | } |
Ivan Kluchnikov | b172b1b | 2012-06-07 01:51:49 +0400 | [diff] [blame] | 108 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 109 | data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 110 | len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU); |
Holger Hans Peter Freyther | 28e5378 | 2013-11-06 20:23:56 +0100 | [diff] [blame] | 111 | if (len > sizeof(gprs_llc::frame)) |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 112 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 113 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP TLLI=0x%08x Rx UL-UD IE_LLC_PDU too large\n", tlli); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 114 | return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg); |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 115 | } |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 116 | |
Andreas Eversberg | e13fa2d | 2012-07-09 17:10:44 +0200 | [diff] [blame] | 117 | /* read IMSI. if no IMSI exists, use first paging block (any paging), |
| 118 | * because during attachment the IMSI might not be known, so the MS |
| 119 | * will listen to all paging blocks. */ |
Vadim Yanitskiy | aad87b6 | 2019-07-24 16:36:45 +0700 | [diff] [blame] | 120 | if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) |
| 121 | { |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 122 | /* gsm48_mi_to_string() returns number of bytes written, including '\0' */ |
| 123 | rc = gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(tp, BSSGP_IE_IMSI), |
| 124 | TLVP_LEN(tp, BSSGP_IE_IMSI)); |
| 125 | if (rc != GSM23003_IMSI_MAX_DIGITS + 1) { |
| 126 | LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc); |
| 127 | return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg); |
| 128 | } |
Vadim Yanitskiy | aad87b6 | 2019-07-24 16:36:45 +0700 | [diff] [blame] | 129 | } |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 130 | |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 131 | #if 0 /* Do not rely on this IE. TODO: make this configurable */ |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 132 | /* parse ms radio access capability */ |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 133 | if (parse_ra_cap(tp, &rac) >= 0) { |
| 134 | /* Get the EGPRS class from the RA capability */ |
| 135 | ms_class = Decoding::get_ms_class_by_capability(&rac); |
| 136 | egprs_ms_class = |
| 137 | Decoding::get_egprs_ms_class_by_capability(&rac); |
| 138 | LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n", |
| 139 | ms_class, egprs_ms_class); |
| 140 | } |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 141 | #endif |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 142 | |
Andreas Eversberg | 24131bf | 2012-07-21 11:09:58 +0200 | [diff] [blame] | 143 | /* 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 Eversberg | 24131bf | 2012-07-21 11:09:58 +0200 | [diff] [blame] | 148 | if (lt_len == 2) |
Harald Welte | 49b7822 | 2017-06-11 11:27:59 +0200 | [diff] [blame] | 149 | delay_csec = tlvp_val16be(tp, BSSGP_IE_PDU_LIFETIME); |
Andreas Eversberg | 24131bf | 2012-07-21 11:09:58 +0200 | [diff] [blame] | 150 | 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 Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 157 | /* 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 Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 161 | if (tlli_len == 2) |
Harald Welte | 49b7822 | 2017-06-11 11:27:59 +0200 | [diff] [blame] | 162 | tlli_old = tlvp_val16be(tp, BSSGP_IE_TLLI); |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 163 | else |
| 164 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of " |
| 165 | "TLLI (old) IE\n"); |
| 166 | } |
| 167 | |
Andreas Eversberg | e13fa2d | 2012-07-09 17:10:44 +0200 | [diff] [blame] | 168 | LOGP(DBSSGP, LOGL_INFO, "LLC [SGSN -> PCU] = TLLI: 0x%08x IMSI: %s len: %d\n", tlli, imsi, len); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 169 | |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 170 | return gprs_rlcmac_dl_tbf::handle(the_pcu.bts, tlli, tlli_old, imsi, |
Jacob Erlbeck | 14e00f8 | 2015-11-27 18:10:39 +0100 | [diff] [blame] | 171 | ms_class, egprs_ms_class, delay_csec, data, len); |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 172 | } |
Ivan Kluchnikov | a9e6dc5 | 2012-06-17 08:30:06 +0400 | [diff] [blame] | 173 | |
Pau Espin Pedrol | 65a0d1d | 2019-12-10 19:29:22 +0100 | [diff] [blame^] | 174 | static int gprs_bssgp_pcu_rx_paging_cs(struct msgb *msg, struct tlv_parsed *tp) |
| 175 | { |
| 176 | const uint8_t *mi; |
| 177 | uint8_t mi_len; |
| 178 | uint8_t *chan_needed = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_CHAN_NEEDED); |
| 179 | |
| 180 | if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) { |
| 181 | mi_len = TLVP_LEN(tp, BSSGP_IE_TMSI); |
| 182 | mi = TLVP_VAL(tp, BSSGP_IE_TMSI); |
| 183 | } else if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) { /* Use IMSI if TMSI not available: */ |
| 184 | mi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 185 | mi = TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 186 | } else { |
| 187 | LOGP(DBSSGP, LOGL_ERROR, "Neither TMSI IE nor IMSI IE is present\n"); |
| 188 | return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg); |
| 189 | } |
| 190 | |
| 191 | return BTS::main_bts()->add_paging(chan_needed ? *chan_needed : 0, mi, mi_len); |
| 192 | } |
| 193 | |
Pau Espin Pedrol | 3f064f5 | 2019-12-10 17:09:51 +0100 | [diff] [blame] | 194 | static int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, struct tlv_parsed *tp) |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 195 | { |
| 196 | char imsi[16]; |
Pau Espin Pedrol | d7c3265 | 2019-12-23 12:41:34 +0100 | [diff] [blame] | 197 | uint16_t pgroup; |
Pau Espin Pedrol | 5530f12 | 2019-12-10 19:26:27 +0100 | [diff] [blame] | 198 | const uint8_t *mi; |
| 199 | uint8_t mi_len; |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 200 | int rc; |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 201 | |
Max | f3038e7 | 2019-03-19 15:39:05 +0100 | [diff] [blame] | 202 | if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) { |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 203 | LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n"); |
Pau Espin Pedrol | 5530f12 | 2019-12-10 19:26:27 +0100 | [diff] [blame] | 204 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 207 | /* gsm48_mi_to_string() returns number of bytes written, including '\0' */ |
| 208 | rc = gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(tp, BSSGP_IE_IMSI), |
Pau Espin Pedrol | 5530f12 | 2019-12-10 19:26:27 +0100 | [diff] [blame] | 209 | TLVP_LEN(tp, BSSGP_IE_IMSI)); |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 210 | if (rc != GSM23003_IMSI_MAX_DIGITS + 1) { |
| 211 | LOGP(DBSSGP, LOGL_NOTICE, "Failed to parse IMSI IE (rc=%d)\n", rc); |
Pau Espin Pedrol | d7c3265 | 2019-12-23 12:41:34 +0100 | [diff] [blame] | 212 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
| 213 | } |
| 214 | pgroup = imsi2paging_group(imsi); |
| 215 | if (pgroup > 999) { |
| 216 | LOGP(DBSSGP, LOGL_NOTICE, "Failed to compute IMSI %s paging group\n", imsi); |
| 217 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Vadim Yanitskiy | 6170ac0 | 2019-07-24 16:44:13 +0700 | [diff] [blame] | 218 | } |
Max | f3038e7 | 2019-03-19 15:39:05 +0100 | [diff] [blame] | 219 | |
Pau Espin Pedrol | 5530f12 | 2019-12-10 19:26:27 +0100 | [diff] [blame] | 220 | if (TLVP_PRESENT(tp, BSSGP_IE_TMSI)) { |
| 221 | mi_len = TLVP_LEN(tp, BSSGP_IE_TMSI); |
| 222 | mi = TLVP_VAL(tp, BSSGP_IE_TMSI); |
| 223 | } else { /* Use IMSI if TMSI not available: */ |
| 224 | mi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 225 | mi = TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 226 | } |
| 227 | |
| 228 | return gprs_rlcmac_paging_request(mi, mi_len, pgroup); |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 229 | } |
| 230 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 231 | /* Receive a BSSGP PDU from a BSS on a PTP BVCI */ |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 232 | static int gprs_bssgp_pcu_rx_ptp(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 233 | { |
| 234 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Max | a49475b | 2017-05-23 17:53:38 +0200 | [diff] [blame] | 235 | enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type; |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 236 | int bvci = bctx ? bctx->bvci : -1; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 237 | unsigned rc = 0; |
| 238 | |
Andreas Eversberg | ba1cd9b | 2012-07-25 09:14:09 +0200 | [diff] [blame] | 239 | if (!bctx) |
| 240 | return -EINVAL; |
| 241 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 242 | /* If traffic is received on a BVC that is marked as blocked, the |
| 243 | * received PDU shall not be accepted and a STATUS PDU (Cause value: |
| 244 | * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ |
| 245 | if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) |
| 246 | { |
| 247 | uint16_t bvci = msgb_bvci(msg); |
| 248 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n"); |
| 249 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); |
| 250 | } |
| 251 | |
| 252 | switch (pdu_type) { |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 253 | case BSSGP_PDUT_STATUS: |
| 254 | /* already handled in libosmogb */ |
| 255 | OSMO_ASSERT(0); |
| 256 | break; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 257 | case BSSGP_PDUT_DL_UNITDATA: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 258 | LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) DL_UNITDATA\n", bvci); |
Holger Hans Peter Freyther | 416ce69 | 2013-08-22 08:44:04 +0200 | [diff] [blame] | 259 | if (the_pcu.on_dl_unit_data) |
| 260 | the_pcu.on_dl_unit_data(&the_pcu, msg, tp); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 261 | gprs_bssgp_pcu_rx_dl_ud(msg, tp); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 262 | break; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 263 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 264 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 265 | LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (PTP) %s\n", |
| 266 | bvci, bssgp_pdu_str(pdu_type)); |
| 267 | break; |
| 268 | case BSSGP_PDUT_PAGING_PS: |
| 269 | case BSSGP_PDUT_PAGING_CS: |
| 270 | case BSSGP_PDUT_RA_CAPABILITY: |
| 271 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
| 272 | LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (PTP) PDU type %s not implemented\n", |
| 273 | bvci, bssgp_pdu_str(pdu_type)); |
| 274 | break; |
| 275 | /* See TS 08.18 5.4.1 */ |
| 276 | case BSSGP_PDUT_SUSPEND: |
| 277 | case BSSGP_PDUT_SUSPEND_ACK: |
| 278 | case BSSGP_PDUT_SUSPEND_NACK: |
| 279 | case BSSGP_PDUT_RESUME: |
| 280 | case BSSGP_PDUT_RESUME_ACK: |
| 281 | case BSSGP_PDUT_RESUME_NACK: |
| 282 | case BSSGP_PDUT_FLUSH_LL: |
| 283 | case BSSGP_PDUT_FLUSH_LL_ACK: |
| 284 | case BSSGP_PDUT_LLC_DISCARD: |
| 285 | case BSSGP_PDUT_BVC_BLOCK: |
| 286 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 287 | case BSSGP_PDUT_BVC_UNBLOCK: |
| 288 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 289 | case BSSGP_PDUT_BVC_RESET: |
| 290 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 291 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 292 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unexpected at PTP\n", |
| 293 | bctx->bvci, bssgp_pdu_str(pdu_type)); |
| 294 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 295 | break; |
| 296 | default: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 297 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%u (PTP) PDU type %s unknown\n", |
Harald Welte | 356ac61 | 2017-05-25 19:05:32 +0200 | [diff] [blame] | 298 | bctx->bvci, bssgp_pdu_str(pdu_type)); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 299 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 300 | break; |
| 301 | } |
| 302 | return rc; |
| 303 | } |
| 304 | |
| 305 | /* Receive a BSSGP PDU from a SGSN on a SIGNALLING BVCI */ |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 306 | static int gprs_bssgp_pcu_rx_sign(struct msgb *msg, struct tlv_parsed *tp, struct bssgp_bvc_ctx *bctx) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 307 | { |
| 308 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
Max | a49475b | 2017-05-23 17:53:38 +0200 | [diff] [blame] | 309 | enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 310 | int rc = 0; |
Vadim Yanitskiy | ce27d1e | 2019-11-08 04:18:46 +0700 | [diff] [blame] | 311 | int bvci = bctx ? bctx->bvci : msgb_bvci(msg); |
Max | a49475b | 2017-05-23 17:53:38 +0200 | [diff] [blame] | 312 | switch (pdu_type) { |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 313 | case BSSGP_PDUT_STATUS: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 314 | /* already handled in libosmogb */ |
| 315 | OSMO_ASSERT(0); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 316 | break; |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 317 | case BSSGP_PDUT_SUSPEND_ACK: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 318 | case BSSGP_PDUT_RESUME_ACK: |
| 319 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 320 | LOGP(DBSSGP, LOGL_DEBUG, "Rx BSSGP BVCI=%d (SIGN) %s\n", |
| 321 | bvci, bssgp_pdu_str(pdu_type)); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 322 | break; |
| 323 | case BSSGP_PDUT_BVC_RESET_ACK: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 324 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_RESET_ACK\n", bvci); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 325 | if (!the_pcu.bvc_sig_reset) |
| 326 | the_pcu.bvc_sig_reset = 1; |
| 327 | else |
| 328 | the_pcu.bvc_reset = 1; |
| 329 | bvc_timeout(NULL); |
| 330 | break; |
Pau Espin Pedrol | 65a0d1d | 2019-12-10 19:29:22 +0100 | [diff] [blame^] | 331 | case BSSGP_PDUT_PAGING_CS: |
| 332 | gprs_bssgp_pcu_rx_paging_cs(msg, tp); |
| 333 | break; |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 334 | case BSSGP_PDUT_PAGING_PS: |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 335 | gprs_bssgp_pcu_rx_paging_ps(msg, tp); |
| 336 | break; |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 337 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 338 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) BVC_UNBLOCK_ACK\n", bvci); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 339 | the_pcu.bvc_unblocked = 1; |
Holger Hans Peter Freyther | c0f1644 | 2013-08-22 08:40:33 +0200 | [diff] [blame] | 340 | if (the_pcu.on_unblock_ack) |
| 341 | the_pcu.on_unblock_ack(&the_pcu); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 342 | bvc_timeout(NULL); |
| 343 | break; |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 344 | case BSSGP_PDUT_SUSPEND_NACK: |
| 345 | case BSSGP_PDUT_RESUME_NACK: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 346 | case BSSGP_PDUT_FLUSH_LL: |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 347 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 348 | LOGP(DBSSGP, LOGL_INFO, "Rx BSSGP BVCI=%d (SIGN) PDU type %s not implemented\n", |
| 349 | bvci, bssgp_pdu_str(pdu_type)); |
| 350 | break; |
| 351 | /* See TS 08.18 5.4.1 */ |
| 352 | case BSSGP_PDUT_UL_UNITDATA: |
| 353 | case BSSGP_PDUT_DL_UNITDATA: |
| 354 | case BSSGP_PDUT_RA_CAPABILITY: |
| 355 | case BSSGP_PDUT_PTM_UNITDATA: |
| 356 | case BSSGP_PDUT_RA_CAPA_UDPATE: |
| 357 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
| 358 | case BSSGP_PDUT_RADIO_STATUS: |
| 359 | case BSSGP_PDUT_FLOW_CONTROL_BVC: |
| 360 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
| 361 | case BSSGP_PDUT_FLOW_CONTROL_MS: |
| 362 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
| 363 | case BSSGP_PDUT_DOWNLOAD_BSS_PFC: |
| 364 | case BSSGP_PDUT_CREATE_BSS_PFC: |
| 365 | case BSSGP_PDUT_CREATE_BSS_PFC_ACK: |
| 366 | case BSSGP_PDUT_CREATE_BSS_PFC_NACK: |
| 367 | case BSSGP_PDUT_MODIFY_BSS_PFC: |
| 368 | case BSSGP_PDUT_MODIFY_BSS_PFC_ACK: |
| 369 | case BSSGP_PDUT_DELETE_BSS_PFC: |
| 370 | case BSSGP_PDUT_DELETE_BSS_PFC_ACK: |
| 371 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unexpected at SIGN\n", |
| 372 | bvci, bssgp_pdu_str(pdu_type)); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 373 | break; |
| 374 | default: |
Harald Welte | 05d7b5d | 2017-07-29 10:16:44 +0200 | [diff] [blame] | 375 | LOGP(DBSSGP, LOGL_NOTICE, "Rx BSSGP BVCI=%d (SIGN) PDU type %s unknown\n", |
Max | a49475b | 2017-05-23 17:53:38 +0200 | [diff] [blame] | 376 | bvci, bssgp_pdu_str(pdu_type)); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 377 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 378 | break; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 379 | } |
| 380 | return rc; |
| 381 | } |
| 382 | |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 383 | static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 384 | { |
| 385 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 386 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 387 | struct tlv_parsed tp; |
Max | a49475b | 2017-05-23 17:53:38 +0200 | [diff] [blame] | 388 | enum bssgp_pdu_type pdu_type = (enum bssgp_pdu_type) bgph->pdu_type; |
Max | 2813f93 | 2017-06-27 17:18:40 +0200 | [diff] [blame] | 389 | enum gprs_bssgp_cause cause = BSSGP_CAUSE_OML_INTERV; |
| 390 | uint16_t ns_bvci = msgb_bvci(msg), nsei = msgb_nsei(msg); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 391 | int data_len; |
| 392 | int rc = 0; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 393 | struct bssgp_bvc_ctx *bctx; |
| 394 | |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 395 | if (pdu_type == BSSGP_PDUT_STATUS) |
| 396 | /* Pass the message to the generic BSSGP parser, which handles |
Max | 2813f93 | 2017-06-27 17:18:40 +0200 | [diff] [blame] | 397 | * STATUS and RESET messages in either direction. */ |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 398 | return bssgp_rcvmsg(msg); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 399 | |
| 400 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 401 | |
| 402 | /* UNITDATA BSSGP headers have TLLI in front */ |
| 403 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA) |
| 404 | { |
| 405 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 406 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 411 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 412 | } |
Stefan Sperling | 3df1532 | 2018-06-25 12:17:39 +0200 | [diff] [blame] | 413 | if (rc < 0) { |
| 414 | LOGP(DBSSGP, LOGL_ERROR, "Failed to parse BSSGP %s message. Invalid message was: %s\n", |
| 415 | bssgp_pdu_str(pdu_type), msgb_hexdump(msg)); |
Harald Welte | 7b7f204 | 2019-07-21 09:26:20 +0200 | [diff] [blame] | 416 | return bssgp_tx_status(BSSGP_CAUSE_INV_MAND_INF, NULL, msg); |
Stefan Sperling | 3df1532 | 2018-06-25 12:17:39 +0200 | [diff] [blame] | 417 | } |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 418 | |
Max | 2813f93 | 2017-06-27 17:18:40 +0200 | [diff] [blame] | 419 | if (pdu_type == BSSGP_PDUT_BVC_RESET) { |
| 420 | rc = bssgp_rcvmsg(msg); |
| 421 | if (ns_bvci != BVCI_SIGNALLING) |
| 422 | return rc; |
| 423 | |
| 424 | if (TLVP_PRES_LEN(&tp, BSSGP_IE_CAUSE, 1)) |
| 425 | cause = (enum gprs_bssgp_cause)*TLVP_VAL(&tp, BSSGP_IE_CAUSE); |
| 426 | else |
| 427 | LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC RESET without cause?!\n", nsei); |
| 428 | |
| 429 | rc = bssgp_tx_bvc_ptp_reset(nsei, cause); |
| 430 | if (rc < 0) |
| 431 | LOGP(DBSSGP, LOGL_ERROR, "NSEI=%u BVC PTP reset procedure failed: %d\n", nsei, rc); |
| 432 | |
| 433 | return rc; |
| 434 | } |
| 435 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 436 | /* look-up or create the BTS context for this BVC */ |
| 437 | bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg)); |
| 438 | |
Vadim Yanitskiy | ef44414 | 2019-11-08 04:02:31 +0700 | [diff] [blame] | 439 | if (!bctx && ns_bvci != BVCI_SIGNALLING) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 440 | { |
Max | 2813f93 | 2017-06-27 17:18:40 +0200 | [diff] [blame] | 441 | LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU type %s for unknown BVCI\n", |
| 442 | nsei, ns_bvci, bssgp_pdu_str(pdu_type)); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 443 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
| 444 | } |
| 445 | |
| 446 | if (bctx) |
| 447 | { |
Neels Hofmeyr | a01e2ee | 2017-02-23 18:10:13 +0100 | [diff] [blame] | 448 | log_set_context(LOG_CTX_GB_BVC, bctx); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 449 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); |
| 450 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg)); |
| 451 | } |
| 452 | |
| 453 | if (ns_bvci == BVCI_SIGNALLING) |
| 454 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 455 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 456 | rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx); |
| 457 | } |
| 458 | else if (ns_bvci == BVCI_PTM) |
| 459 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 460 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 461 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
| 462 | } |
| 463 | else |
| 464 | { |
Pau Espin Pedrol | db12f25 | 2019-12-23 14:27:54 +0100 | [diff] [blame] | 465 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP=%u gprs_bssgp_rx_ptp\n", ns_bvci); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 466 | rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx); |
| 467 | } |
| 468 | return rc; |
| 469 | } |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 470 | |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 471 | static void handle_nm_status(struct osmo_bssgp_prim *bp) |
| 472 | { |
| 473 | enum gprs_bssgp_cause cause; |
| 474 | |
| 475 | LOGP(DPCU, LOGL_DEBUG, |
| 476 | "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n", |
| 477 | bp->bvci, bp->nsei); |
| 478 | |
| 479 | if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE)) |
| 480 | return; |
| 481 | |
| 482 | cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE); |
| 483 | |
| 484 | if (cause != BSSGP_CAUSE_BVCI_BLOCKED && |
| 485 | cause != BSSGP_CAUSE_UNKNOWN_BVCI) |
| 486 | return; |
| 487 | |
| 488 | if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI)) |
| 489 | return; |
| 490 | |
| 491 | if (gprs_bssgp_pcu_current_bctx()->bvci != bp->bvci) { |
| 492 | LOGP(DPCU, LOGL_NOTICE, |
| 493 | "Received BSSGP STATUS message for an unknown BVCI (%d), " |
| 494 | "ignored\n", |
| 495 | bp->bvci); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | switch (cause) { |
| 500 | case BSSGP_CAUSE_BVCI_BLOCKED: |
| 501 | if (the_pcu.bvc_unblocked) { |
| 502 | the_pcu.bvc_unblocked = 0; |
| 503 | bvc_timeout(NULL); |
| 504 | } |
| 505 | break; |
| 506 | |
| 507 | case BSSGP_CAUSE_UNKNOWN_BVCI: |
| 508 | if (the_pcu.bvc_reset) { |
| 509 | the_pcu.bvc_reset = 0; |
| 510 | bvc_timeout(NULL); |
| 511 | } |
| 512 | break; |
| 513 | default: |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 518 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 519 | { |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 520 | struct osmo_bssgp_prim *bp; |
| 521 | bp = container_of(oph, struct osmo_bssgp_prim, oph); |
| 522 | |
| 523 | switch (oph->sap) { |
| 524 | case SAP_BSSGP_NM: |
| 525 | if (oph->primitive == PRIM_NM_STATUS) |
| 526 | handle_nm_status(bp); |
| 527 | break; |
| 528 | default: |
| 529 | break; |
| 530 | } |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 533 | |
Alexander Couzens | eb64d43 | 2019-05-25 03:10:51 +0200 | [diff] [blame] | 534 | int gprs_bssgp_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 535 | { |
| 536 | int rc = 0; |
| 537 | switch (event) { |
| 538 | case GPRS_NS_EVT_UNIT_DATA: |
| 539 | /* hand the message into the BSSGP implementation */ |
| 540 | rc = gprs_bssgp_pcu_rcvmsg(msg); |
| 541 | break; |
| 542 | default: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 543 | LOGP(DPCU, LOGL_NOTICE, "RLCMAC: Unknown event %u from NS\n", event); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 544 | rc = -EIO; |
| 545 | break; |
| 546 | } |
| 547 | return rc; |
| 548 | } |
| 549 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 550 | |
| 551 | static int nsvc_signal_cb(unsigned int subsys, unsigned int signal, |
| 552 | void *handler_data, void *signal_data) |
| 553 | { |
| 554 | struct ns_signal_data *nssd; |
| 555 | |
| 556 | if (subsys != SS_L_NS) |
| 557 | return -EINVAL; |
| 558 | |
| 559 | nssd = (struct ns_signal_data *)signal_data; |
Harald Welte | 57d3515 | 2018-07-05 03:11:17 +0200 | [diff] [blame] | 560 | if (signal != S_SNS_CONFIGURED && nssd->nsvc != the_pcu.nsvc) { |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 561 | LOGP(DPCU, LOGL_ERROR, "Signal received of unknown NSVC\n"); |
| 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | switch (signal) { |
Harald Welte | 57d3515 | 2018-07-05 03:11:17 +0200 | [diff] [blame] | 566 | case S_SNS_CONFIGURED: |
| 567 | the_pcu.bvc_sig_reset = 0; |
| 568 | the_pcu.bvc_reset = 0; |
| 569 | /* There's no NS-RESET / NS-UNBLOCK procedure on IP SNS based NS-VCs */ |
| 570 | the_pcu.nsvc_unblocked = 1; |
| 571 | LOGP(DPCU, LOGL_NOTICE, "NS-VC %d is unblocked.\n", the_pcu.nsvc->nsvci); |
| 572 | bvc_timeout(NULL); |
| 573 | break; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 574 | case S_NS_UNBLOCK: |
Holger Hans Peter Freyther | 4b984b1 | 2013-07-13 12:57:06 +0200 | [diff] [blame] | 575 | if (!the_pcu.nsvc_unblocked) { |
| 576 | the_pcu.nsvc_unblocked = 1; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 577 | LOGP(DPCU, LOGL_NOTICE, "NS-VC %d is unblocked.\n", |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 578 | the_pcu.nsvc->nsvci); |
| 579 | the_pcu.bvc_sig_reset = 0; |
| 580 | the_pcu.bvc_reset = 0; |
| 581 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 582 | bvc_timeout(NULL); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 583 | } |
| 584 | break; |
| 585 | case S_NS_BLOCK: |
Holger Hans Peter Freyther | 4b984b1 | 2013-07-13 12:57:06 +0200 | [diff] [blame] | 586 | if (the_pcu.nsvc_unblocked) { |
| 587 | the_pcu.nsvc_unblocked = 0; |
Holger Hans Peter Freyther | ed70cb7 | 2013-07-13 12:53:46 +0200 | [diff] [blame] | 588 | osmo_timer_del(&the_pcu.bvc_timer); |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 589 | the_pcu.bvc_sig_reset = 0; |
| 590 | the_pcu.bvc_reset = 0; |
| 591 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 592 | LOGP(DPCU, LOGL_NOTICE, "NS-VC is blocked.\n"); |
| 593 | } |
| 594 | break; |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 595 | case S_NS_ALIVE_EXP: |
| 596 | LOGP(DPCU, LOGL_NOTICE, "Tns alive expired too often, " |
| 597 | "re-starting RESET procedure\n"); |
| 598 | gprs_ns_reconnect(nssd->nsvc); |
| 599 | break; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | return 0; |
| 603 | } |
| 604 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 605 | static unsigned count_pdch(const struct gprs_rlcmac_bts *bts) |
| 606 | { |
| 607 | size_t trx_no, ts_no; |
| 608 | unsigned num_pdch = 0; |
| 609 | |
| 610 | for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) { |
| 611 | const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no]; |
| 612 | |
| 613 | for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) { |
| 614 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no]; |
| 615 | |
| 616 | if (pdch->is_enabled()) |
| 617 | num_pdch += 1; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | return num_pdch; |
| 622 | } |
| 623 | |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 624 | static uint32_t gprs_bssgp_max_leak_rate(GprsCodingScheme cs, int num_pdch) |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 625 | { |
Max | 51754b6 | 2019-03-13 17:14:13 +0100 | [diff] [blame] | 626 | int bytes_per_rlc_block = cs.maxDataBlockBytes() * num_data_blocks(cs.headerTypeData()); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 627 | |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 628 | /* n byte payload per 20ms */ |
| 629 | return bytes_per_rlc_block * (1000 / 20) * num_pdch; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts, |
| 633 | uint32_t leak_rate, uint32_t fallback) |
| 634 | { |
| 635 | uint32_t bucket_size = 0; |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 636 | uint16_t bucket_time = bts->fc_bucket_time; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 637 | |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 638 | if (bucket_time == 0) |
| 639 | bucket_time = bts->force_llc_lifetime; |
| 640 | |
| 641 | if (bucket_time == 0xffff) |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 642 | bucket_size = FC_MAX_BUCKET_SIZE; |
| 643 | |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 644 | if (bucket_size == 0 && bucket_time && leak_rate) |
| 645 | bucket_size = (uint64_t)leak_rate * bucket_time / 100; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 646 | |
| 647 | if (bucket_size == 0 && leak_rate) |
| 648 | bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS; |
| 649 | |
| 650 | if (bucket_size == 0) |
| 651 | bucket_size = fallback; |
| 652 | |
| 653 | if (bucket_size > FC_MAX_BUCKET_SIZE) |
| 654 | bucket_size = FC_MAX_BUCKET_SIZE; |
| 655 | |
| 656 | return bucket_size; |
| 657 | } |
| 658 | |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 659 | static uint32_t get_and_reset_avg_queue_delay(void) |
| 660 | { |
| 661 | struct timeval *delay_sum = &the_pcu.queue_delay_sum; |
Jacob Erlbeck | 0ae4313 | 2015-06-11 12:03:18 +0200 | [diff] [blame] | 662 | uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 + |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 663 | delay_sum->tv_usec / 1000000; |
| 664 | uint32_t avg_delay_ms = 0; |
| 665 | |
| 666 | if (the_pcu.queue_delay_count > 0) |
| 667 | avg_delay_ms = delay_sum_ms / the_pcu.queue_delay_count; |
| 668 | |
| 669 | /* Reset accumulator */ |
| 670 | delay_sum->tv_sec = delay_sum->tv_usec = 0; |
| 671 | the_pcu.queue_delay_count = 0; |
| 672 | |
| 673 | return avg_delay_ms; |
| 674 | } |
| 675 | |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 676 | static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch) |
| 677 | { |
| 678 | int rate; /* byte per second */ |
| 679 | |
| 680 | if (the_pcu.queue_frames_sent == 0) |
| 681 | return -1; |
| 682 | |
| 683 | if (the_pcu.queue_frames_recv == 0) |
| 684 | return -1; |
| 685 | |
| 686 | *usage_by_1000 = the_pcu.queue_frames_recv * 1000 / |
| 687 | the_pcu.queue_frames_sent; |
| 688 | |
| 689 | /* 20ms/num_pdch is the average RLC block duration, so the rate is |
| 690 | * calculated as: |
| 691 | * rate = bytes_recv / (block_dur * block_count) */ |
| 692 | rate = the_pcu.queue_bytes_recv * 1000 * num_pdch / |
| 693 | (20 * the_pcu.queue_frames_recv); |
| 694 | |
| 695 | the_pcu.queue_frames_sent = 0; |
| 696 | the_pcu.queue_bytes_recv = 0; |
| 697 | the_pcu.queue_frames_recv = 0; |
| 698 | |
| 699 | return rate; |
| 700 | } |
| 701 | |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 702 | static GprsCodingScheme max_coding_scheme_dl(struct gprs_rlcmac_bts *bts) |
| 703 | { |
| 704 | int num; |
| 705 | |
| 706 | if (bts->egprs_enabled) { |
| 707 | if (!bts->cs_adj_enabled) { |
| 708 | if (bts->initial_mcs_dl) |
| 709 | num = bts->initial_mcs_dl; |
| 710 | else |
| 711 | num = 1; |
| 712 | } else if (bts->max_mcs_dl) { |
| 713 | num = bts->max_mcs_dl; |
| 714 | } else { |
| 715 | num = 9; |
| 716 | } |
| 717 | |
| 718 | return GprsCodingScheme::getEgprsByNum(num); |
| 719 | } |
| 720 | |
| 721 | if (!bts->cs_adj_enabled) { |
| 722 | if (bts->initial_cs_dl) |
| 723 | num = bts->initial_cs_dl; |
| 724 | else if (bts->cs4) |
| 725 | num = 4; |
| 726 | else if (bts->cs3) |
| 727 | num = 3; |
| 728 | else if (bts->cs2) |
| 729 | num = 2; |
| 730 | else |
| 731 | num = 1; |
| 732 | } else if (bts->max_cs_dl) { |
| 733 | num = bts->max_cs_dl; |
| 734 | } else { |
| 735 | num = 4; |
| 736 | } |
| 737 | |
| 738 | return GprsCodingScheme::getGprsByNum(num); |
| 739 | } |
| 740 | |
Pau Espin Pedrol | 3f064f5 | 2019-12-10 17:09:51 +0100 | [diff] [blame] | 741 | static int gprs_bssgp_tx_fc_bvc(void) |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 742 | { |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 743 | struct gprs_rlcmac_bts *bts; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 744 | uint32_t bucket_size; /* oct */ |
| 745 | uint32_t ms_bucket_size; /* oct */ |
| 746 | uint32_t leak_rate; /* oct/s */ |
| 747 | uint32_t ms_leak_rate; /* oct/s */ |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 748 | uint32_t avg_delay_ms; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 749 | int num_pdch = -1; |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 750 | GprsCodingScheme max_cs_dl; |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 751 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 752 | if (!the_pcu.bctx) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 753 | LOGP(DBSSGP, LOGL_ERROR, "No bctx\n"); |
| 754 | return -EIO; |
| 755 | } |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 756 | bts = bts_main_data(); |
| 757 | |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 758 | max_cs_dl = max_coding_scheme_dl(bts); |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 759 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 760 | bucket_size = bts->fc_bvc_bucket_size; |
| 761 | leak_rate = bts->fc_bvc_leak_rate; |
| 762 | ms_bucket_size = bts->fc_ms_bucket_size; |
| 763 | ms_leak_rate = bts->fc_ms_leak_rate; |
| 764 | |
| 765 | if (leak_rate == 0) { |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 766 | int meas_rate; |
| 767 | int usage; /* in 0..1000 */ |
| 768 | |
| 769 | if (num_pdch < 0) |
| 770 | num_pdch = count_pdch(bts); |
| 771 | |
| 772 | meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch); |
| 773 | if (meas_rate > 0) { |
| 774 | leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch); |
| 775 | leak_rate = |
| 776 | (meas_rate * usage + leak_rate * (1000 - usage)) / |
| 777 | 1000; |
| 778 | LOGP(DBSSGP, LOGL_DEBUG, |
| 779 | "Estimated BVC leak rate = %d " |
| 780 | "(measured %d, usage %d%%)\n", |
| 781 | leak_rate, meas_rate, usage/10); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | if (leak_rate == 0) { |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 786 | if (num_pdch < 0) |
| 787 | num_pdch = count_pdch(bts); |
| 788 | |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 789 | leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 790 | |
| 791 | LOGP(DBSSGP, LOGL_DEBUG, |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 792 | "Computed BVC leak rate = %d, num_pdch = %d, cs = %s\n", |
Max | 136ebcc | 2019-03-05 14:59:03 +0100 | [diff] [blame] | 793 | leak_rate, num_pdch, mcs_name(max_cs_dl)); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 794 | }; |
| 795 | |
| 796 | if (ms_leak_rate == 0) { |
| 797 | int ms_num_pdch; |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 798 | int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 799 | |
| 800 | if (num_pdch < 0) |
| 801 | num_pdch = count_pdch(bts); |
| 802 | |
| 803 | ms_num_pdch = num_pdch; |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 804 | if (max_pdch > FC_MS_MAX_RX_SLOTS) |
| 805 | max_pdch = FC_MS_MAX_RX_SLOTS; |
| 806 | if (ms_num_pdch > max_pdch) |
| 807 | ms_num_pdch = max_pdch; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 808 | |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 809 | ms_leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, ms_num_pdch); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 810 | |
| 811 | /* TODO: To properly support multiple TRX, the per MS leak rate |
| 812 | * should be derived from the max number of PDCH TS per TRX. |
| 813 | */ |
| 814 | LOGP(DBSSGP, LOGL_DEBUG, |
Jacob Erlbeck | 7f28c97 | 2016-02-04 17:54:42 +0100 | [diff] [blame] | 815 | "Computed MS default leak rate = %d, ms_num_pdch = %d, " |
| 816 | "cs = %s\n", |
Max | 136ebcc | 2019-03-05 14:59:03 +0100 | [diff] [blame] | 817 | ms_leak_rate, ms_num_pdch, mcs_name(max_cs_dl)); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 818 | }; |
| 819 | |
| 820 | /* TODO: Force leak_rate to 0 on buffer bloat */ |
| 821 | |
| 822 | if (bucket_size == 0) |
| 823 | bucket_size = compute_bucket_size(bts, leak_rate, |
| 824 | FC_FALLBACK_BVC_BUCKET_SIZE); |
| 825 | |
| 826 | if (ms_bucket_size == 0) |
| 827 | ms_bucket_size = compute_bucket_size(bts, ms_leak_rate, |
| 828 | FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size)); |
| 829 | |
| 830 | if (leak_rate > FC_MAX_BUCKET_LEAK_RATE) |
| 831 | leak_rate = FC_MAX_BUCKET_LEAK_RATE; |
| 832 | |
| 833 | if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE) |
| 834 | ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE; |
| 835 | |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 836 | /* Avg queue delay monitoring */ |
| 837 | avg_delay_ms = get_and_reset_avg_queue_delay(); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 838 | |
Jacob Erlbeck | 6eeb7c7 | 2015-05-06 15:26:08 +0200 | [diff] [blame] | 839 | /* Update tag */ |
| 840 | the_pcu.fc_tag += 1; |
| 841 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 842 | LOGP(DBSSGP, LOGL_DEBUG, |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 843 | "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, " |
| 844 | "R_MS = %d, avg_dly = %d\n", |
| 845 | bucket_size, leak_rate, ms_bucket_size, ms_leak_rate, |
| 846 | avg_delay_ms); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 847 | |
Jacob Erlbeck | 6eeb7c7 | 2015-05-06 15:26:08 +0200 | [diff] [blame] | 848 | return bssgp_tx_fc_bvc(the_pcu.bctx, the_pcu.fc_tag, |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 849 | bucket_size, leak_rate, |
| 850 | ms_bucket_size, ms_leak_rate, |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 851 | NULL, &avg_delay_ms); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | static void bvc_timeout(void *_priv) |
| 855 | { |
Pau Espin Pedrol | 5211d9d | 2019-09-05 16:20:22 +0200 | [diff] [blame] | 856 | unsigned long secs; |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 857 | if (!the_pcu.bvc_sig_reset) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 858 | LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n"); |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 859 | bssgp_tx_bvc_reset(the_pcu.bctx, 0, BSSGP_CAUSE_OML_INTERV); |
Pau Espin Pedrol | 5211d9d | 2019-09-05 16:20:22 +0200 | [diff] [blame] | 860 | secs = osmo_tdef_get(the_pcu.bts->T_defs_pcu, 2, OSMO_TDEF_S, -1); |
| 861 | osmo_timer_schedule(&the_pcu.bvc_timer, secs, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 862 | return; |
| 863 | } |
| 864 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 865 | if (!the_pcu.bvc_reset) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 866 | LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n", |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 867 | the_pcu.bctx->bvci); |
| 868 | bssgp_tx_bvc_reset(the_pcu.bctx, the_pcu.bctx->bvci, BSSGP_CAUSE_OML_INTERV); |
Pau Espin Pedrol | 5211d9d | 2019-09-05 16:20:22 +0200 | [diff] [blame] | 869 | secs = osmo_tdef_get(the_pcu.bts->T_defs_pcu, 2, OSMO_TDEF_S, -1); |
| 870 | osmo_timer_schedule(&the_pcu.bvc_timer, secs, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 871 | return; |
| 872 | } |
| 873 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 874 | if (!the_pcu.bvc_unblocked) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 875 | LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n", |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 876 | the_pcu.bctx->bvci); |
| 877 | bssgp_tx_bvc_unblock(the_pcu.bctx); |
Pau Espin Pedrol | 5211d9d | 2019-09-05 16:20:22 +0200 | [diff] [blame] | 878 | secs = osmo_tdef_get(the_pcu.bts->T_defs_pcu, 1, OSMO_TDEF_S, -1); |
| 879 | osmo_timer_schedule(&the_pcu.bvc_timer, secs, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 880 | return; |
| 881 | } |
| 882 | |
| 883 | LOGP(DBSSGP, LOGL_DEBUG, "Sending flow control info on BVCI %d\n", |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 884 | the_pcu.bctx->bvci); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 885 | gprs_bssgp_tx_fc_bvc(); |
Holger Hans Peter Freyther | b67a8a3 | 2013-07-28 18:55:14 +0200 | [diff] [blame] | 886 | osmo_timer_schedule(&the_pcu.bvc_timer, the_pcu.bts->fc_interval, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 887 | } |
| 888 | |
Harald Welte | b26854c | 2019-02-21 17:17:07 +0100 | [diff] [blame] | 889 | static int gprs_ns_reconnect(struct gprs_nsvc *nsvc) |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 890 | { |
| 891 | struct gprs_nsvc *nsvc2; |
| 892 | |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 893 | if (nsvc != the_pcu.nsvc) { |
| 894 | LOGP(DBSSGP, LOGL_ERROR, "NSVC is invalid\n"); |
| 895 | return -EBADF; |
| 896 | } |
| 897 | |
Harald Welte | 57d3515 | 2018-07-05 03:11:17 +0200 | [diff] [blame] | 898 | if (the_pcu.bts->gb_dialect_sns) |
| 899 | nsvc2 = gprs_ns_nsip_connect_sns(bssgp_nsi, &nsvc->ip.bts_addr, nsvc->nsei, nsvc->nsvci); |
| 900 | else |
| 901 | nsvc2 = gprs_ns_nsip_connect(bssgp_nsi, &nsvc->ip.bts_addr, nsvc->nsei, nsvc->nsvci); |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 902 | if (!nsvc2) { |
| 903 | LOGP(DBSSGP, LOGL_ERROR, "Failed to reconnect NSVC\n"); |
| 904 | return -EIO; |
| 905 | } |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 910 | /* create BSSGP/NS layer instances */ |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 911 | struct gprs_bssgp_pcu *gprs_bssgp_create_and_connect(struct gprs_rlcmac_bts *bts, |
Holger Hans Peter Freyther | b67a8a3 | 2013-07-28 18:55:14 +0200 | [diff] [blame] | 912 | uint16_t local_port, uint32_t sgsn_ip, |
Harald Welte | 30a73d8 | 2013-03-10 08:54:30 +0000 | [diff] [blame] | 913 | uint16_t sgsn_port, uint16_t nsei, uint16_t nsvci, uint16_t bvci, |
Neels Hofmeyr | bdc55fa | 2018-02-21 00:39:07 +0100 | [diff] [blame] | 914 | uint16_t mcc, uint16_t mnc, bool mnc_3_digits, uint16_t lac, uint16_t rac, |
Harald Welte | 30a73d8 | 2013-03-10 08:54:30 +0000 | [diff] [blame] | 915 | uint16_t cell_id) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 916 | { |
| 917 | struct sockaddr_in dest; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 918 | int rc; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 919 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 920 | /* if already created... return the current address */ |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 921 | if (the_pcu.bctx) |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 922 | return &the_pcu; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 923 | |
Holger Hans Peter Freyther | b67a8a3 | 2013-07-28 18:55:14 +0200 | [diff] [blame] | 924 | the_pcu.bts = bts; |
| 925 | |
Harald Welte | 57d3515 | 2018-07-05 03:11:17 +0200 | [diff] [blame] | 926 | /* don't specify remote IP/port if SNS dialect is in use; Doing so would |
| 927 | * issue a connect() on the socket, which prevents us to dynamically communicate |
| 928 | * with any number of IP-SNS endpoints on the SGSN side */ |
| 929 | if (!bts->gb_dialect_sns) { |
| 930 | bssgp_nsi->nsip.remote_port = sgsn_port; |
| 931 | bssgp_nsi->nsip.remote_ip = sgsn_ip; |
| 932 | } |
Harald Welte | 30a73d8 | 2013-03-10 08:54:30 +0000 | [diff] [blame] | 933 | bssgp_nsi->nsip.local_port = local_port; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 934 | rc = gprs_ns_nsip_listen(bssgp_nsi); |
| 935 | if (rc < 0) { |
| 936 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create socket\n"); |
Alexander Couzens | 5c3783b | 2019-05-25 05:41:18 +0200 | [diff] [blame] | 937 | gprs_ns_close(bssgp_nsi); |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 938 | return NULL; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 939 | } |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 940 | |
| 941 | dest.sin_family = AF_INET; |
| 942 | dest.sin_port = htons(sgsn_port); |
| 943 | dest.sin_addr.s_addr = htonl(sgsn_ip); |
| 944 | |
Harald Welte | 57d3515 | 2018-07-05 03:11:17 +0200 | [diff] [blame] | 945 | if (bts->gb_dialect_sns) |
| 946 | the_pcu.nsvc = gprs_ns_nsip_connect_sns(bssgp_nsi, &dest, nsei, nsvci); |
| 947 | else |
| 948 | the_pcu.nsvc = gprs_ns_nsip_connect(bssgp_nsi, &dest, nsei, nsvci); |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 949 | if (!the_pcu.nsvc) { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 950 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSVCt\n"); |
Alexander Couzens | 5c3783b | 2019-05-25 05:41:18 +0200 | [diff] [blame] | 951 | gprs_ns_close(bssgp_nsi); |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 952 | return NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 953 | } |
| 954 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 955 | the_pcu.bctx = btsctx_alloc(bvci, nsei); |
| 956 | if (!the_pcu.bctx) { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 957 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n"); |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 958 | the_pcu.nsvc = NULL; |
Alexander Couzens | 5c3783b | 2019-05-25 05:41:18 +0200 | [diff] [blame] | 959 | gprs_ns_close(bssgp_nsi); |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 960 | return NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 961 | } |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 962 | the_pcu.bctx->ra_id.mcc = spoof_mcc ? : mcc; |
Neels Hofmeyr | bdc55fa | 2018-02-21 00:39:07 +0100 | [diff] [blame] | 963 | if (spoof_mnc) { |
| 964 | the_pcu.bctx->ra_id.mnc = spoof_mnc; |
| 965 | the_pcu.bctx->ra_id.mnc_3_digits = spoof_mnc_3_digits; |
| 966 | } else { |
| 967 | the_pcu.bctx->ra_id.mnc = mnc; |
| 968 | the_pcu.bctx->ra_id.mnc_3_digits = mnc_3_digits; |
| 969 | } |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 970 | the_pcu.bctx->ra_id.lac = lac; |
| 971 | the_pcu.bctx->ra_id.rac = rac; |
| 972 | the_pcu.bctx->cell_id = cell_id; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 973 | |
| 974 | osmo_signal_register_handler(SS_L_NS, nsvc_signal_cb, NULL); |
| 975 | |
Pau Espin Pedrol | a8892a6 | 2019-09-05 16:21:04 +0200 | [diff] [blame] | 976 | osmo_timer_setup(&the_pcu.bvc_timer, bvc_timeout, NULL); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 977 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 978 | return &the_pcu; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 979 | } |
| 980 | |
Daniel Willmann | 6d8884d | 2014-06-04 18:30:59 +0200 | [diff] [blame] | 981 | void gprs_bssgp_destroy(void) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 982 | { |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 983 | struct gprs_ns_inst *nsi = bssgp_nsi; |
| 984 | if (!nsi) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 985 | return; |
| 986 | |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 987 | bssgp_nsi = NULL; |
| 988 | |
Holger Hans Peter Freyther | ed70cb7 | 2013-07-13 12:53:46 +0200 | [diff] [blame] | 989 | osmo_timer_del(&the_pcu.bvc_timer); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 990 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 991 | osmo_signal_unregister_handler(SS_L_NS, nsvc_signal_cb, NULL); |
| 992 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 993 | the_pcu.nsvc = NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 994 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 995 | /* FIXME: blocking... */ |
Holger Hans Peter Freyther | 40bd0c4 | 2013-07-13 12:58:20 +0200 | [diff] [blame] | 996 | the_pcu.nsvc_unblocked = 0; |
| 997 | the_pcu.bvc_sig_reset = 0; |
| 998 | the_pcu.bvc_reset = 0; |
Pau Espin Pedrol | 3f064f5 | 2019-12-10 17:09:51 +0100 | [diff] [blame] | 999 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 1000 | |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 1001 | gprs_ns_destroy(nsi); |
| 1002 | |
Vadim Yanitskiy | 0bf622e | 2019-11-09 01:57:17 +0700 | [diff] [blame] | 1003 | bssgp_bvc_ctx_free(the_pcu.bctx); |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 1004 | the_pcu.bctx = NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 1005 | } |
| 1006 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 1007 | struct bssgp_bvc_ctx *gprs_bssgp_pcu_current_bctx(void) |
| 1008 | { |
| 1009 | return the_pcu.bctx; |
| 1010 | } |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 1011 | |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 1012 | void gprs_bssgp_update_frames_sent() |
| 1013 | { |
| 1014 | the_pcu.queue_frames_sent += 1; |
| 1015 | } |
| 1016 | |
| 1017 | void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv) |
| 1018 | { |
| 1019 | the_pcu.queue_bytes_recv += bytes_recv; |
| 1020 | the_pcu.queue_frames_recv += frames_recv; |
| 1021 | } |
| 1022 | |
Jacob Erlbeck | 1e50a3d | 2015-06-16 10:18:42 +0200 | [diff] [blame] | 1023 | void gprs_bssgp_update_queue_delay(const struct timeval *tv_recv, |
| 1024 | const struct timeval *tv_now) |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 1025 | { |
| 1026 | struct timeval *delay_sum = &the_pcu.queue_delay_sum; |
| 1027 | struct timeval tv_delay; |
| 1028 | |
| 1029 | timersub(tv_now, tv_recv, &tv_delay); |
| 1030 | timeradd(delay_sum, &tv_delay, delay_sum); |
| 1031 | |
| 1032 | the_pcu.queue_delay_count += 1; |
| 1033 | } |