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> |
Holger Hans Peter Freyther | 67ed34e | 2013-10-17 17:01:54 +0200 | [diff] [blame] | 24 | #include <bts.h> |
Holger Hans Peter Freyther | 17c31ce | 2013-08-24 18:31:27 +0200 | [diff] [blame] | 25 | #include <tbf.h> |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 26 | #include <decoding.h> |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 27 | |
Jacob Erlbeck | 1e96af6 | 2015-03-17 08:11:25 +0100 | [diff] [blame] | 28 | #define BSSGP_TIMER_T1 30 /* Guards the (un)blocking procedures */ |
| 29 | #define BSSGP_TIMER_T2 30 /* Guards the reset procedure */ |
| 30 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 31 | /* Tuning parameters for BSSGP flow control */ |
| 32 | #define FC_DEFAULT_LIFE_TIME_SECS 10 /* experimental value, 10s */ |
| 33 | #define FC_MS_BUCKET_SIZE_BY_BMAX(bmax) ((bmax) / 2 + 500) /* experimental */ |
| 34 | #define FC_FALLBACK_BVC_BUCKET_SIZE 2000 /* e.g. on R = 0, value taken from PCAP files */ |
| 35 | #define FC_MS_MAX_RX_SLOTS 4 /* limit MS default R to 4 TS per MS */ |
| 36 | |
| 37 | /* Constants for BSSGP flow control */ |
| 38 | #define FC_MAX_BUCKET_LEAK_RATE (6553500 / 8) /* Byte/s */ |
| 39 | #define FC_MAX_BUCKET_SIZE 6553500 /* Octets */ |
| 40 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 41 | static struct gprs_bssgp_pcu the_pcu = { 0, }; |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 42 | |
Andreas Eversberg | 71cce91 | 2013-01-16 13:49:00 +0100 | [diff] [blame] | 43 | extern void *tall_pcu_ctx; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 44 | extern uint16_t spoof_mcc, spoof_mnc; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 45 | |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 46 | static void bvc_timeout(void *_priv); |
| 47 | |
Andreas Eversberg | 0095074 | 2012-10-08 12:25:55 +0200 | [diff] [blame] | 48 | static int parse_imsi(struct tlv_parsed *tp, char *imsi) |
| 49 | { |
| 50 | uint8_t imsi_len; |
| 51 | uint8_t *bcd_imsi; |
| 52 | int i, j; |
| 53 | |
| 54 | if (!TLVP_PRESENT(tp, BSSGP_IE_IMSI)) |
| 55 | return -EINVAL; |
| 56 | |
| 57 | imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 58 | bcd_imsi = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 59 | |
| 60 | if ((bcd_imsi[0] & 0x08)) |
| 61 | imsi_len = imsi_len * 2 - 1; |
| 62 | else |
| 63 | imsi_len = (imsi_len - 1) * 2; |
| 64 | for (i = 0, j = 0; j < imsi_len && j < 15; j++) |
| 65 | { |
| 66 | if (!(j & 1)) { |
| 67 | imsi[j] = (bcd_imsi[i] >> 4) + '0'; |
| 68 | i++; |
| 69 | } else |
| 70 | imsi[j] = (bcd_imsi[i] & 0xf) + '0'; |
| 71 | } |
| 72 | imsi[j] = '\0'; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 77 | 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] | 78 | { |
| 79 | bitvec *block; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 80 | uint8_t cap_len; |
| 81 | uint8_t *cap; |
| 82 | |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 83 | memset(rac, 0, sizeof(*rac)); |
| 84 | |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 85 | if (!TLVP_PRESENT(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP)) |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 86 | return -EINVAL; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 87 | |
| 88 | cap_len = TLVP_LEN(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP); |
| 89 | cap = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_MS_RADIO_ACCESS_CAP); |
| 90 | |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 91 | LOGP(DBSSGP, LOGL_DEBUG, "Got BSSGP RA Capability of size %d\n", cap_len); |
| 92 | |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 93 | block = bitvec_alloc(cap_len); |
| 94 | bitvec_unpack(block, cap); |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 95 | |
| 96 | /* TS 24.008, 10.5.5.12a */ |
| 97 | decode_gsm_ra_cap(block, rac); |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 98 | |
| 99 | bitvec_free(block); |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 100 | return 0; |
Holger Hans Peter Freyther | 98fe945 | 2013-07-13 15:23:58 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 103 | 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] | 104 | { |
| 105 | struct bssgp_ud_hdr *budh; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 106 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 107 | uint32_t tlli; |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 108 | uint32_t tlli_old = 0; |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 109 | uint8_t *data; |
| 110 | uint16_t len; |
Andreas Eversberg | 0095074 | 2012-10-08 12:25:55 +0200 | [diff] [blame] | 111 | char imsi[16] = "000"; |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 112 | uint8_t ms_class = 0; |
| 113 | uint8_t egprs_ms_class = 0; |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 114 | #if 0 |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 115 | MS_Radio_Access_capability_t rac; |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 116 | #endif |
Andreas Eversberg | 7d7cf54 | 2012-06-25 09:26:15 +0200 | [diff] [blame] | 117 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 118 | budh = (struct bssgp_ud_hdr *)msgb_bssgph(msg); |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 119 | tlli = ntohl(budh->tlli); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 120 | |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 121 | /* LLC_PDU is mandatory IE */ |
| 122 | if (!TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 123 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 124 | 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] | 125 | return bssgp_tx_status(BSSGP_CAUSE_MISSING_MAND_IE, NULL, msg); |
| 126 | } |
Ivan Kluchnikov | b172b1b | 2012-06-07 01:51:49 +0400 | [diff] [blame] | 127 | |
Andreas Eversberg | e6228b3 | 2012-07-03 13:36:03 +0200 | [diff] [blame] | 128 | data = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 129 | len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU); |
Holger Hans Peter Freyther | 28e5378 | 2013-11-06 20:23:56 +0100 | [diff] [blame] | 130 | if (len > sizeof(gprs_llc::frame)) |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 131 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 132 | 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] | 133 | return bssgp_tx_status(BSSGP_CAUSE_COND_IE_ERR, NULL, msg); |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 134 | } |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 135 | |
Andreas Eversberg | e13fa2d | 2012-07-09 17:10:44 +0200 | [diff] [blame] | 136 | /* read IMSI. if no IMSI exists, use first paging block (any paging), |
| 137 | * because during attachment the IMSI might not be known, so the MS |
| 138 | * will listen to all paging blocks. */ |
Andreas Eversberg | 0095074 | 2012-10-08 12:25:55 +0200 | [diff] [blame] | 139 | parse_imsi(tp, imsi); |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 140 | |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 141 | #if 0 /* Do not rely on this IE. TODO: make this configurable */ |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 142 | /* parse ms radio access capability */ |
Jacob Erlbeck | 4a07a3b | 2016-01-20 12:22:07 +0100 | [diff] [blame] | 143 | if (parse_ra_cap(tp, &rac) >= 0) { |
| 144 | /* Get the EGPRS class from the RA capability */ |
| 145 | ms_class = Decoding::get_ms_class_by_capability(&rac); |
| 146 | egprs_ms_class = |
| 147 | Decoding::get_egprs_ms_class_by_capability(&rac); |
| 148 | LOGP(DBSSGP, LOGL_DEBUG, "Got downlink MS class %d/%d\n", |
| 149 | ms_class, egprs_ms_class); |
| 150 | } |
Jacob Erlbeck | 741d25c | 2016-01-20 13:38:15 +0100 | [diff] [blame] | 151 | #endif |
Andreas Eversberg | 51ab134 | 2012-07-13 14:52:50 +0200 | [diff] [blame] | 152 | |
Andreas Eversberg | 24131bf | 2012-07-21 11:09:58 +0200 | [diff] [blame] | 153 | /* get lifetime */ |
| 154 | uint16_t delay_csec = 0xffff; |
| 155 | if (TLVP_PRESENT(tp, BSSGP_IE_PDU_LIFETIME)) |
| 156 | { |
| 157 | uint8_t lt_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME); |
| 158 | uint16_t *lt = (uint16_t *) TLVP_VAL(tp, BSSGP_IE_PDU_LIFETIME); |
| 159 | if (lt_len == 2) |
| 160 | delay_csec = ntohs(*lt); |
| 161 | else |
| 162 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of " |
| 163 | "PDU_LIFETIME IE\n"); |
| 164 | } else |
| 165 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP missing mandatory " |
| 166 | "PDU_LIFETIME IE\n"); |
| 167 | |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 168 | /* get optional TLLI old */ |
| 169 | if (TLVP_PRESENT(tp, BSSGP_IE_TLLI)) |
| 170 | { |
| 171 | uint8_t tlli_len = TLVP_LEN(tp, BSSGP_IE_PDU_LIFETIME); |
| 172 | uint16_t *e_tlli_old = (uint16_t *) TLVP_VAL(tp, BSSGP_IE_TLLI); |
| 173 | if (tlli_len == 2) |
| 174 | tlli_old = ntohs(*e_tlli_old); |
| 175 | else |
| 176 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP invalid length of " |
| 177 | "TLLI (old) IE\n"); |
| 178 | } |
| 179 | |
Andreas Eversberg | e13fa2d | 2012-07-09 17:10:44 +0200 | [diff] [blame] | 180 | 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] | 181 | |
Jacob Erlbeck | 9399046 | 2015-05-15 15:50:43 +0200 | [diff] [blame] | 182 | 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] | 183 | ms_class, egprs_ms_class, delay_csec, data, len); |
Ivan Kluchnikov | c7e7f68 | 2012-06-29 22:53:15 +0400 | [diff] [blame] | 184 | } |
Ivan Kluchnikov | a9e6dc5 | 2012-06-17 08:30:06 +0400 | [diff] [blame] | 185 | |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 186 | int gprs_bssgp_pcu_rx_paging_ps(struct msgb *msg, struct tlv_parsed *tp) |
| 187 | { |
| 188 | char imsi[16]; |
| 189 | uint8_t *ptmsi = (uint8_t *) TLVP_VAL(tp, BSSGP_IE_TMSI); |
| 190 | uint16_t ptmsi_len = TLVP_LEN(tp, BSSGP_IE_TMSI); |
| 191 | |
| 192 | LOGP(DBSSGP, LOGL_NOTICE, " P-TMSI = "); |
| 193 | for (int i = 0; i < ptmsi_len; i++) |
| 194 | { |
| 195 | LOGPC(DBSSGP, LOGL_NOTICE, "%02x", ptmsi[i]); |
| 196 | } |
| 197 | LOGPC(DBSSGP, LOGL_NOTICE, "\n"); |
| 198 | |
| 199 | if (parse_imsi(tp, imsi)) |
| 200 | { |
| 201 | LOGP(DBSSGP, LOGL_ERROR, "No IMSI\n"); |
| 202 | return -EINVAL; |
| 203 | } |
| 204 | |
| 205 | return gprs_rlcmac_paging_request(ptmsi, ptmsi_len, imsi); |
| 206 | } |
| 207 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 208 | /* 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] | 209 | 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] | 210 | { |
| 211 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 212 | uint8_t pdu_type = bgph->pdu_type; |
| 213 | unsigned rc = 0; |
| 214 | |
Andreas Eversberg | ba1cd9b | 2012-07-25 09:14:09 +0200 | [diff] [blame] | 215 | if (!bctx) |
| 216 | return -EINVAL; |
| 217 | |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 218 | /* If traffic is received on a BVC that is marked as blocked, the |
| 219 | * received PDU shall not be accepted and a STATUS PDU (Cause value: |
| 220 | * BVC Blocked) shall be sent to the peer entity on the signalling BVC */ |
| 221 | if (bctx->state & BVC_S_BLOCKED && pdu_type != BSSGP_PDUT_STATUS) |
| 222 | { |
| 223 | uint16_t bvci = msgb_bvci(msg); |
| 224 | LOGP(DBSSGP, LOGL_NOTICE, "rx BVC_S_BLOCKED\n"); |
| 225 | return bssgp_tx_status(BSSGP_CAUSE_BVCI_BLOCKED, &bvci, msg); |
| 226 | } |
| 227 | |
| 228 | switch (pdu_type) { |
| 229 | case BSSGP_PDUT_DL_UNITDATA: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 230 | LOGP(DBSSGP, LOGL_DEBUG, "RX: [SGSN->PCU] BSSGP_PDUT_DL_UNITDATA\n"); |
Holger Hans Peter Freyther | 416ce69 | 2013-08-22 08:44:04 +0200 | [diff] [blame] | 231 | if (the_pcu.on_dl_unit_data) |
| 232 | the_pcu.on_dl_unit_data(&the_pcu, msg, tp); |
Ivan Kluchnikov | 5e0df93 | 2012-06-12 15:33:52 +0400 | [diff] [blame] | 233 | gprs_bssgp_pcu_rx_dl_ud(msg, tp); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 234 | break; |
| 235 | case BSSGP_PDUT_PAGING_PS: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 236 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_PAGING_PS\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 237 | break; |
| 238 | case BSSGP_PDUT_PAGING_CS: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 239 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_PAGING_CS\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 240 | break; |
| 241 | case BSSGP_PDUT_RA_CAPA_UPDATE_ACK: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 242 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_RA_CAPA_UPDATE_ACK\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 243 | break; |
| 244 | case BSSGP_PDUT_FLOW_CONTROL_BVC_ACK: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 245 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_FLOW_CONTROL_BVC_ACK\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 246 | break; |
| 247 | case BSSGP_PDUT_FLOW_CONTROL_MS_ACK: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 248 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_FLOW_CONTROL_MS_ACK\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 249 | break; |
| 250 | default: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 251 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%u PDU type 0x%02x unknown\n", bctx->bvci, pdu_type); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 252 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 253 | break; |
| 254 | } |
| 255 | return rc; |
| 256 | } |
| 257 | |
| 258 | /* 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] | 259 | 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] | 260 | { |
| 261 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 262 | int rc = 0; |
Jacob Erlbeck | 09fdf66 | 2015-06-11 11:35:58 +0200 | [diff] [blame] | 263 | int bvci = bctx ? bctx->bvci : -1; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 264 | switch (bgph->pdu_type) { |
| 265 | case BSSGP_PDUT_STATUS: |
| 266 | /* Some exception has occurred */ |
Jacob Erlbeck | 09fdf66 | 2015-06-11 11:35:58 +0200 | [diff] [blame] | 267 | DEBUGP(DBSSGP, "BSSGP BVCI=%d Rx BVC STATUS\n", bvci); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 268 | /* FIXME: send NM_STATUS.ind to NM */ |
| 269 | break; |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 270 | case BSSGP_PDUT_SUSPEND_ACK: |
| 271 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 272 | break; |
| 273 | case BSSGP_PDUT_SUSPEND_NACK: |
| 274 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_SUSPEND_NACK\n"); |
| 275 | break; |
| 276 | case BSSGP_PDUT_BVC_RESET_ACK: |
| 277 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_BVC_RESET_ACK\n"); |
| 278 | if (!the_pcu.bvc_sig_reset) |
| 279 | the_pcu.bvc_sig_reset = 1; |
| 280 | else |
| 281 | the_pcu.bvc_reset = 1; |
| 282 | bvc_timeout(NULL); |
| 283 | break; |
| 284 | case BSSGP_PDUT_PAGING_PS: |
| 285 | LOGP(DBSSGP, LOGL_NOTICE, "RX: [SGSN->PCU] BSSGP_PDUT_PAGING_PS\n"); |
| 286 | gprs_bssgp_pcu_rx_paging_ps(msg, tp); |
| 287 | break; |
| 288 | case BSSGP_PDUT_PAGING_CS: |
| 289 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_PAGING_CS\n"); |
| 290 | break; |
| 291 | case BSSGP_PDUT_RESUME_ACK: |
| 292 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_RESUME_ACK\n"); |
| 293 | break; |
| 294 | case BSSGP_PDUT_RESUME_NACK: |
| 295 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_RESUME_NACK\n"); |
| 296 | break; |
| 297 | case BSSGP_PDUT_FLUSH_LL: |
| 298 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_FLUSH_LL\n"); |
| 299 | break; |
| 300 | case BSSGP_PDUT_BVC_BLOCK_ACK: |
| 301 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_SUSPEND_ACK\n"); |
| 302 | break; |
| 303 | case BSSGP_PDUT_BVC_UNBLOCK_ACK: |
| 304 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_BVC_UNBLOCK_ACK\n"); |
| 305 | the_pcu.bvc_unblocked = 1; |
Holger Hans Peter Freyther | c0f1644 | 2013-08-22 08:40:33 +0200 | [diff] [blame] | 306 | if (the_pcu.on_unblock_ack) |
| 307 | the_pcu.on_unblock_ack(&the_pcu); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 308 | bvc_timeout(NULL); |
| 309 | break; |
| 310 | case BSSGP_PDUT_SGSN_INVOKE_TRACE: |
| 311 | LOGP(DBSSGP, LOGL_DEBUG, "rx BSSGP_PDUT_SGSN_INVOKE_TRACE\n"); |
| 312 | break; |
| 313 | default: |
Jacob Erlbeck | 09fdf66 | 2015-06-11 11:35:58 +0200 | [diff] [blame] | 314 | LOGP(DBSSGP, LOGL_NOTICE, "BSSGP BVCI=%d Rx PDU type 0x%02x unknown\n", |
| 315 | bvci, bgph->pdu_type); |
Holger Hans Peter Freyther | a9744de | 2013-07-28 19:01:20 +0200 | [diff] [blame] | 316 | rc = bssgp_tx_status(BSSGP_CAUSE_PROTO_ERR_UNSPEC, NULL, msg); |
| 317 | break; |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 318 | } |
| 319 | return rc; |
| 320 | } |
| 321 | |
Holger Hans Peter Freyther | 90d5df4 | 2013-07-28 21:14:15 +0200 | [diff] [blame] | 322 | static int gprs_bssgp_pcu_rcvmsg(struct msgb *msg) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 323 | { |
| 324 | struct bssgp_normal_hdr *bgph = (struct bssgp_normal_hdr *) msgb_bssgph(msg); |
| 325 | struct bssgp_ud_hdr *budh = (struct bssgp_ud_hdr *) msgb_bssgph(msg); |
| 326 | struct tlv_parsed tp; |
| 327 | uint8_t pdu_type = bgph->pdu_type; |
| 328 | uint16_t ns_bvci = msgb_bvci(msg); |
| 329 | int data_len; |
| 330 | int rc = 0; |
Andreas Eversberg | 3e372d5 | 2012-07-06 09:28:15 +0200 | [diff] [blame] | 331 | struct bssgp_bvc_ctx *bctx; |
| 332 | |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 333 | if (pdu_type == BSSGP_PDUT_STATUS) |
| 334 | /* Pass the message to the generic BSSGP parser, which handles |
| 335 | * STATUS message in either direction. */ |
| 336 | return bssgp_rcvmsg(msg); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 337 | |
| 338 | /* Identifiers from DOWN: NSEI, BVCI (both in msg->cb) */ |
| 339 | |
| 340 | /* UNITDATA BSSGP headers have TLLI in front */ |
| 341 | if (pdu_type != BSSGP_PDUT_UL_UNITDATA && pdu_type != BSSGP_PDUT_DL_UNITDATA) |
| 342 | { |
| 343 | data_len = msgb_bssgp_len(msg) - sizeof(*bgph); |
| 344 | rc = bssgp_tlv_parse(&tp, bgph->data, data_len); |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | data_len = msgb_bssgp_len(msg) - sizeof(*budh); |
| 349 | rc = bssgp_tlv_parse(&tp, budh->data, data_len); |
| 350 | } |
| 351 | |
| 352 | /* look-up or create the BTS context for this BVC */ |
| 353 | bctx = btsctx_by_bvci_nsei(ns_bvci, msgb_nsei(msg)); |
| 354 | |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 355 | if (!bctx |
| 356 | && pdu_type != BSSGP_PDUT_BVC_RESET_ACK |
Andreas Eversberg | 8c3680d | 2012-10-08 12:30:56 +0200 | [diff] [blame] | 357 | && pdu_type != BSSGP_PDUT_BVC_UNBLOCK_ACK |
| 358 | && pdu_type != BSSGP_PDUT_PAGING_PS) |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 359 | { |
| 360 | LOGP(DBSSGP, LOGL_NOTICE, "NSEI=%u/BVCI=%u Rejecting PDU " |
| 361 | "type %u for unknown BVCI\n", msgb_nsei(msg), ns_bvci, |
| 362 | pdu_type); |
| 363 | return bssgp_tx_status(BSSGP_CAUSE_UNKNOWN_BVCI, NULL, msg); |
| 364 | } |
| 365 | |
| 366 | if (bctx) |
| 367 | { |
| 368 | log_set_context(BSC_CTX_BVC, bctx); |
| 369 | rate_ctr_inc(&bctx->ctrg->ctr[BSSGP_CTR_PKTS_IN]); |
| 370 | rate_ctr_add(&bctx->ctrg->ctr[BSSGP_CTR_BYTES_IN], msgb_bssgp_len(msg)); |
| 371 | } |
| 372 | |
| 373 | if (ns_bvci == BVCI_SIGNALLING) |
| 374 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 375 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_SIGNALLING gprs_bssgp_rx_sign\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 376 | rc = gprs_bssgp_pcu_rx_sign(msg, &tp, bctx); |
| 377 | } |
| 378 | else if (ns_bvci == BVCI_PTM) |
| 379 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 380 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTM bssgp_tx_status\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 381 | rc = bssgp_tx_status(BSSGP_CAUSE_PDU_INCOMP_FEAT, NULL, msg); |
| 382 | } |
| 383 | else |
| 384 | { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 385 | LOGP(DBSSGP, LOGL_DEBUG, "rx BVCI_PTP gprs_bssgp_rx_ptp\n"); |
Ivan Kluchnikov | 8ee6051 | 2012-03-05 19:24:57 +0400 | [diff] [blame] | 386 | rc = gprs_bssgp_pcu_rx_ptp(msg, &tp, bctx); |
| 387 | } |
| 388 | return rc; |
| 389 | } |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 390 | |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 391 | static void handle_nm_status(struct osmo_bssgp_prim *bp) |
| 392 | { |
| 393 | enum gprs_bssgp_cause cause; |
| 394 | |
| 395 | LOGP(DPCU, LOGL_DEBUG, |
| 396 | "Got NM-STATUS.ind, BVCI=%d, NSEI=%d\n", |
| 397 | bp->bvci, bp->nsei); |
| 398 | |
| 399 | if (!TLVP_PRESENT(bp->tp, BSSGP_IE_CAUSE)) |
| 400 | return; |
| 401 | |
| 402 | cause = (enum gprs_bssgp_cause)*TLVP_VAL(bp->tp, BSSGP_IE_CAUSE); |
| 403 | |
| 404 | if (cause != BSSGP_CAUSE_BVCI_BLOCKED && |
| 405 | cause != BSSGP_CAUSE_UNKNOWN_BVCI) |
| 406 | return; |
| 407 | |
| 408 | if (!TLVP_PRESENT(bp->tp, BSSGP_IE_BVCI)) |
| 409 | return; |
| 410 | |
| 411 | if (gprs_bssgp_pcu_current_bctx()->bvci != bp->bvci) { |
| 412 | LOGP(DPCU, LOGL_NOTICE, |
| 413 | "Received BSSGP STATUS message for an unknown BVCI (%d), " |
| 414 | "ignored\n", |
| 415 | bp->bvci); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | switch (cause) { |
| 420 | case BSSGP_CAUSE_BVCI_BLOCKED: |
| 421 | if (the_pcu.bvc_unblocked) { |
| 422 | the_pcu.bvc_unblocked = 0; |
| 423 | bvc_timeout(NULL); |
| 424 | } |
| 425 | break; |
| 426 | |
| 427 | case BSSGP_CAUSE_UNKNOWN_BVCI: |
| 428 | if (the_pcu.bvc_reset) { |
| 429 | the_pcu.bvc_reset = 0; |
| 430 | bvc_timeout(NULL); |
| 431 | } |
| 432 | break; |
| 433 | default: |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 438 | int bssgp_prim_cb(struct osmo_prim_hdr *oph, void *ctx) |
| 439 | { |
Jacob Erlbeck | 39645b8 | 2015-03-16 12:27:11 +0100 | [diff] [blame] | 440 | struct osmo_bssgp_prim *bp; |
| 441 | bp = container_of(oph, struct osmo_bssgp_prim, oph); |
| 442 | |
| 443 | switch (oph->sap) { |
| 444 | case SAP_BSSGP_NM: |
| 445 | if (oph->primitive == PRIM_NM_STATUS) |
| 446 | handle_nm_status(bp); |
| 447 | break; |
| 448 | default: |
| 449 | break; |
| 450 | } |
Harald Welte | 477e79e | 2012-06-18 12:21:03 +0800 | [diff] [blame] | 451 | return 0; |
| 452 | } |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 453 | |
| 454 | static int sgsn_ns_cb(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, struct msgb *msg, uint16_t bvci) |
| 455 | { |
| 456 | int rc = 0; |
| 457 | switch (event) { |
| 458 | case GPRS_NS_EVT_UNIT_DATA: |
| 459 | /* hand the message into the BSSGP implementation */ |
| 460 | rc = gprs_bssgp_pcu_rcvmsg(msg); |
| 461 | break; |
| 462 | default: |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 463 | LOGP(DPCU, LOGL_NOTICE, "RLCMAC: Unknown event %u from NS\n", event); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 464 | rc = -EIO; |
| 465 | break; |
| 466 | } |
| 467 | return rc; |
| 468 | } |
| 469 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 470 | |
| 471 | static int nsvc_signal_cb(unsigned int subsys, unsigned int signal, |
| 472 | void *handler_data, void *signal_data) |
| 473 | { |
| 474 | struct ns_signal_data *nssd; |
| 475 | |
| 476 | if (subsys != SS_L_NS) |
| 477 | return -EINVAL; |
| 478 | |
| 479 | nssd = (struct ns_signal_data *)signal_data; |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 480 | if (nssd->nsvc != the_pcu.nsvc) { |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 481 | LOGP(DPCU, LOGL_ERROR, "Signal received of unknown NSVC\n"); |
| 482 | return -EINVAL; |
| 483 | } |
| 484 | |
| 485 | switch (signal) { |
| 486 | case S_NS_UNBLOCK: |
Holger Hans Peter Freyther | 4b984b1 | 2013-07-13 12:57:06 +0200 | [diff] [blame] | 487 | if (!the_pcu.nsvc_unblocked) { |
| 488 | the_pcu.nsvc_unblocked = 1; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 489 | LOGP(DPCU, LOGL_NOTICE, "NS-VC %d is unblocked.\n", |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 490 | the_pcu.nsvc->nsvci); |
| 491 | the_pcu.bvc_sig_reset = 0; |
| 492 | the_pcu.bvc_reset = 0; |
| 493 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 494 | bvc_timeout(NULL); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 495 | } |
| 496 | break; |
| 497 | case S_NS_BLOCK: |
Holger Hans Peter Freyther | 4b984b1 | 2013-07-13 12:57:06 +0200 | [diff] [blame] | 498 | if (the_pcu.nsvc_unblocked) { |
| 499 | the_pcu.nsvc_unblocked = 0; |
Holger Hans Peter Freyther | ed70cb7 | 2013-07-13 12:53:46 +0200 | [diff] [blame] | 500 | osmo_timer_del(&the_pcu.bvc_timer); |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 501 | the_pcu.bvc_sig_reset = 0; |
| 502 | the_pcu.bvc_reset = 0; |
| 503 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 504 | LOGP(DPCU, LOGL_NOTICE, "NS-VC is blocked.\n"); |
| 505 | } |
| 506 | break; |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 507 | case S_NS_ALIVE_EXP: |
| 508 | LOGP(DPCU, LOGL_NOTICE, "Tns alive expired too often, " |
| 509 | "re-starting RESET procedure\n"); |
| 510 | gprs_ns_reconnect(nssd->nsvc); |
| 511 | break; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 517 | static unsigned count_pdch(const struct gprs_rlcmac_bts *bts) |
| 518 | { |
| 519 | size_t trx_no, ts_no; |
| 520 | unsigned num_pdch = 0; |
| 521 | |
| 522 | for (trx_no = 0; trx_no < ARRAY_SIZE(bts->trx); ++trx_no) { |
| 523 | const struct gprs_rlcmac_trx *trx = &bts->trx[trx_no]; |
| 524 | |
| 525 | for (ts_no = 0; ts_no < ARRAY_SIZE(trx->pdch); ++ts_no) { |
| 526 | const struct gprs_rlcmac_pdch *pdch = &trx->pdch[ts_no]; |
| 527 | |
| 528 | if (pdch->is_enabled()) |
| 529 | num_pdch += 1; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | return num_pdch; |
| 534 | } |
| 535 | |
| 536 | static uint32_t gprs_bssgp_max_leak_rate(unsigned cs, int num_pdch) |
| 537 | { |
| 538 | static const uint32_t max_lr_per_ts[4] = { |
| 539 | 20 * (1000 / 20), /* CS-1: 20 byte payload per 20ms */ |
| 540 | 30 * (1000 / 20), /* CS-2: 30 byte payload per 20ms */ |
| 541 | 36 * (1000 / 20), /* CS-3: 36 byte payload per 20ms */ |
| 542 | 50 * (1000 / 20), /* CS-4: 50 byte payload per 20ms */ |
| 543 | }; |
| 544 | |
| 545 | if (cs > ARRAY_SIZE(max_lr_per_ts)) |
| 546 | cs = 1; |
| 547 | |
| 548 | return max_lr_per_ts[cs-1] * num_pdch; |
| 549 | } |
| 550 | |
| 551 | static uint32_t compute_bucket_size(struct gprs_rlcmac_bts *bts, |
| 552 | uint32_t leak_rate, uint32_t fallback) |
| 553 | { |
| 554 | uint32_t bucket_size = 0; |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 555 | uint16_t bucket_time = bts->fc_bucket_time; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 556 | |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 557 | if (bucket_time == 0) |
| 558 | bucket_time = bts->force_llc_lifetime; |
| 559 | |
| 560 | if (bucket_time == 0xffff) |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 561 | bucket_size = FC_MAX_BUCKET_SIZE; |
| 562 | |
Jacob Erlbeck | 0288cdb | 2015-05-06 10:47:30 +0200 | [diff] [blame] | 563 | if (bucket_size == 0 && bucket_time && leak_rate) |
| 564 | bucket_size = (uint64_t)leak_rate * bucket_time / 100; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 565 | |
| 566 | if (bucket_size == 0 && leak_rate) |
| 567 | bucket_size = leak_rate * FC_DEFAULT_LIFE_TIME_SECS; |
| 568 | |
| 569 | if (bucket_size == 0) |
| 570 | bucket_size = fallback; |
| 571 | |
| 572 | if (bucket_size > FC_MAX_BUCKET_SIZE) |
| 573 | bucket_size = FC_MAX_BUCKET_SIZE; |
| 574 | |
| 575 | return bucket_size; |
| 576 | } |
| 577 | |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 578 | static uint32_t get_and_reset_avg_queue_delay(void) |
| 579 | { |
| 580 | struct timeval *delay_sum = &the_pcu.queue_delay_sum; |
Jacob Erlbeck | 0ae4313 | 2015-06-11 12:03:18 +0200 | [diff] [blame] | 581 | uint32_t delay_sum_ms = delay_sum->tv_sec * 1000 + |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 582 | delay_sum->tv_usec / 1000000; |
| 583 | uint32_t avg_delay_ms = 0; |
| 584 | |
| 585 | if (the_pcu.queue_delay_count > 0) |
| 586 | avg_delay_ms = delay_sum_ms / the_pcu.queue_delay_count; |
| 587 | |
| 588 | /* Reset accumulator */ |
| 589 | delay_sum->tv_sec = delay_sum->tv_usec = 0; |
| 590 | the_pcu.queue_delay_count = 0; |
| 591 | |
| 592 | return avg_delay_ms; |
| 593 | } |
| 594 | |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 595 | static int get_and_reset_measured_leak_rate(int *usage_by_1000, unsigned num_pdch) |
| 596 | { |
| 597 | int rate; /* byte per second */ |
| 598 | |
| 599 | if (the_pcu.queue_frames_sent == 0) |
| 600 | return -1; |
| 601 | |
| 602 | if (the_pcu.queue_frames_recv == 0) |
| 603 | return -1; |
| 604 | |
| 605 | *usage_by_1000 = the_pcu.queue_frames_recv * 1000 / |
| 606 | the_pcu.queue_frames_sent; |
| 607 | |
| 608 | /* 20ms/num_pdch is the average RLC block duration, so the rate is |
| 609 | * calculated as: |
| 610 | * rate = bytes_recv / (block_dur * block_count) */ |
| 611 | rate = the_pcu.queue_bytes_recv * 1000 * num_pdch / |
| 612 | (20 * the_pcu.queue_frames_recv); |
| 613 | |
| 614 | the_pcu.queue_frames_sent = 0; |
| 615 | the_pcu.queue_bytes_recv = 0; |
| 616 | the_pcu.queue_frames_recv = 0; |
| 617 | |
| 618 | return rate; |
| 619 | } |
| 620 | |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 621 | int gprs_bssgp_tx_fc_bvc(void) |
| 622 | { |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 623 | struct gprs_rlcmac_bts *bts; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 624 | uint32_t bucket_size; /* oct */ |
| 625 | uint32_t ms_bucket_size; /* oct */ |
| 626 | uint32_t leak_rate; /* oct/s */ |
| 627 | uint32_t ms_leak_rate; /* oct/s */ |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 628 | uint32_t avg_delay_ms; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 629 | int num_pdch = -1; |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 630 | int max_cs_dl; |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 631 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 632 | if (!the_pcu.bctx) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 633 | LOGP(DBSSGP, LOGL_ERROR, "No bctx\n"); |
| 634 | return -EIO; |
| 635 | } |
Jacob Erlbeck | 87d7341 | 2015-04-21 12:56:48 +0200 | [diff] [blame] | 636 | bts = bts_main_data(); |
| 637 | |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 638 | if (bts->cs_adj_enabled) { |
| 639 | max_cs_dl = bts->max_cs_dl; |
| 640 | if (!max_cs_dl) { |
| 641 | if (bts->cs4) |
| 642 | max_cs_dl = 4; |
| 643 | else if (bts->cs3) |
| 644 | max_cs_dl = 3; |
| 645 | else if (bts->cs2) |
| 646 | max_cs_dl = 2; |
| 647 | else |
| 648 | max_cs_dl = 1; |
| 649 | } |
| 650 | } else { |
| 651 | max_cs_dl = bts->initial_cs_dl; |
| 652 | } |
| 653 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 654 | bucket_size = bts->fc_bvc_bucket_size; |
| 655 | leak_rate = bts->fc_bvc_leak_rate; |
| 656 | ms_bucket_size = bts->fc_ms_bucket_size; |
| 657 | ms_leak_rate = bts->fc_ms_leak_rate; |
| 658 | |
| 659 | if (leak_rate == 0) { |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 660 | int meas_rate; |
| 661 | int usage; /* in 0..1000 */ |
| 662 | |
| 663 | if (num_pdch < 0) |
| 664 | num_pdch = count_pdch(bts); |
| 665 | |
| 666 | meas_rate = get_and_reset_measured_leak_rate(&usage, num_pdch); |
| 667 | if (meas_rate > 0) { |
| 668 | leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch); |
| 669 | leak_rate = |
| 670 | (meas_rate * usage + leak_rate * (1000 - usage)) / |
| 671 | 1000; |
| 672 | LOGP(DBSSGP, LOGL_DEBUG, |
| 673 | "Estimated BVC leak rate = %d " |
| 674 | "(measured %d, usage %d%%)\n", |
| 675 | leak_rate, meas_rate, usage/10); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (leak_rate == 0) { |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 680 | if (num_pdch < 0) |
| 681 | num_pdch = count_pdch(bts); |
| 682 | |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 683 | leak_rate = gprs_bssgp_max_leak_rate(max_cs_dl, num_pdch); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 684 | |
| 685 | LOGP(DBSSGP, LOGL_DEBUG, |
| 686 | "Computed BVC leak rate = %d, num_pdch = %d, cs = %d\n", |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 687 | leak_rate, num_pdch, max_cs_dl); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 688 | }; |
| 689 | |
| 690 | if (ms_leak_rate == 0) { |
| 691 | int ms_num_pdch; |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 692 | int max_pdch = gprs_alloc_max_dl_slots_per_ms(bts); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 693 | |
| 694 | if (num_pdch < 0) |
| 695 | num_pdch = count_pdch(bts); |
| 696 | |
| 697 | ms_num_pdch = num_pdch; |
Jacob Erlbeck | 7f79f0d | 2015-07-17 11:38:49 +0200 | [diff] [blame] | 698 | if (max_pdch > FC_MS_MAX_RX_SLOTS) |
| 699 | max_pdch = FC_MS_MAX_RX_SLOTS; |
| 700 | if (ms_num_pdch > max_pdch) |
| 701 | ms_num_pdch = max_pdch; |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 702 | |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 703 | 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] | 704 | |
| 705 | /* TODO: To properly support multiple TRX, the per MS leak rate |
| 706 | * should be derived from the max number of PDCH TS per TRX. |
| 707 | */ |
| 708 | LOGP(DBSSGP, LOGL_DEBUG, |
| 709 | "Computed MS default leak rate = %d, ms_num_pdch = %d, cs = %d\n", |
Jacob Erlbeck | 6eed191 | 2015-07-21 18:29:46 +0200 | [diff] [blame] | 710 | ms_leak_rate, ms_num_pdch, max_cs_dl); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 711 | }; |
| 712 | |
| 713 | /* TODO: Force leak_rate to 0 on buffer bloat */ |
| 714 | |
| 715 | if (bucket_size == 0) |
| 716 | bucket_size = compute_bucket_size(bts, leak_rate, |
| 717 | FC_FALLBACK_BVC_BUCKET_SIZE); |
| 718 | |
| 719 | if (ms_bucket_size == 0) |
| 720 | ms_bucket_size = compute_bucket_size(bts, ms_leak_rate, |
| 721 | FC_MS_BUCKET_SIZE_BY_BMAX(bucket_size)); |
| 722 | |
| 723 | if (leak_rate > FC_MAX_BUCKET_LEAK_RATE) |
| 724 | leak_rate = FC_MAX_BUCKET_LEAK_RATE; |
| 725 | |
| 726 | if (ms_leak_rate > FC_MAX_BUCKET_LEAK_RATE) |
| 727 | ms_leak_rate = FC_MAX_BUCKET_LEAK_RATE; |
| 728 | |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 729 | /* Avg queue delay monitoring */ |
| 730 | avg_delay_ms = get_and_reset_avg_queue_delay(); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 731 | |
Jacob Erlbeck | 6eeb7c7 | 2015-05-06 15:26:08 +0200 | [diff] [blame] | 732 | /* Update tag */ |
| 733 | the_pcu.fc_tag += 1; |
| 734 | |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 735 | LOGP(DBSSGP, LOGL_DEBUG, |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 736 | "Sending FLOW CONTROL BVC, Bmax = %d, R = %d, Bmax_MS = %d, " |
| 737 | "R_MS = %d, avg_dly = %d\n", |
| 738 | bucket_size, leak_rate, ms_bucket_size, ms_leak_rate, |
| 739 | avg_delay_ms); |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 740 | |
Jacob Erlbeck | 6eeb7c7 | 2015-05-06 15:26:08 +0200 | [diff] [blame] | 741 | return bssgp_tx_fc_bvc(the_pcu.bctx, the_pcu.fc_tag, |
Jacob Erlbeck | 3d62fc5 | 2015-04-30 15:01:15 +0200 | [diff] [blame] | 742 | bucket_size, leak_rate, |
| 743 | ms_bucket_size, ms_leak_rate, |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 744 | NULL, &avg_delay_ms); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void bvc_timeout(void *_priv) |
| 748 | { |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 749 | if (!the_pcu.bvc_sig_reset) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 750 | LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI 0\n"); |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 751 | bssgp_tx_bvc_reset(the_pcu.bctx, 0, BSSGP_CAUSE_OML_INTERV); |
Jacob Erlbeck | 1e96af6 | 2015-03-17 08:11:25 +0100 | [diff] [blame] | 752 | osmo_timer_schedule(&the_pcu.bvc_timer, BSSGP_TIMER_T2, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 753 | return; |
| 754 | } |
| 755 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 756 | if (!the_pcu.bvc_reset) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 757 | LOGP(DBSSGP, LOGL_INFO, "Sending reset on BVCI %d\n", |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 758 | the_pcu.bctx->bvci); |
| 759 | bssgp_tx_bvc_reset(the_pcu.bctx, the_pcu.bctx->bvci, BSSGP_CAUSE_OML_INTERV); |
Jacob Erlbeck | 1e96af6 | 2015-03-17 08:11:25 +0100 | [diff] [blame] | 760 | osmo_timer_schedule(&the_pcu.bvc_timer, BSSGP_TIMER_T2, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 761 | return; |
| 762 | } |
| 763 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 764 | if (!the_pcu.bvc_unblocked) { |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 765 | LOGP(DBSSGP, LOGL_INFO, "Sending unblock on BVCI %d\n", |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 766 | the_pcu.bctx->bvci); |
| 767 | bssgp_tx_bvc_unblock(the_pcu.bctx); |
Jacob Erlbeck | 1e96af6 | 2015-03-17 08:11:25 +0100 | [diff] [blame] | 768 | osmo_timer_schedule(&the_pcu.bvc_timer, BSSGP_TIMER_T1, 0); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 769 | return; |
| 770 | } |
| 771 | |
| 772 | 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] | 773 | the_pcu.bctx->bvci); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 774 | gprs_bssgp_tx_fc_bvc(); |
Holger Hans Peter Freyther | b67a8a3 | 2013-07-28 18:55:14 +0200 | [diff] [blame] | 775 | 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] | 776 | } |
| 777 | |
Jacob Erlbeck | ebebad1 | 2015-08-17 14:43:52 +0200 | [diff] [blame] | 778 | int gprs_ns_reconnect(struct gprs_nsvc *nsvc) |
| 779 | { |
| 780 | struct gprs_nsvc *nsvc2; |
| 781 | |
| 782 | if (!bssgp_nsi) { |
| 783 | LOGP(DBSSGP, LOGL_ERROR, "NS instance does not exist\n"); |
| 784 | return -EINVAL; |
| 785 | } |
| 786 | |
| 787 | if (nsvc != the_pcu.nsvc) { |
| 788 | LOGP(DBSSGP, LOGL_ERROR, "NSVC is invalid\n"); |
| 789 | return -EBADF; |
| 790 | } |
| 791 | |
| 792 | nsvc2 = gprs_ns_nsip_connect(bssgp_nsi, &nsvc->ip.bts_addr, |
| 793 | nsvc->nsei, nsvc->nsvci); |
| 794 | |
| 795 | if (!nsvc2) { |
| 796 | LOGP(DBSSGP, LOGL_ERROR, "Failed to reconnect NSVC\n"); |
| 797 | return -EIO; |
| 798 | } |
| 799 | |
| 800 | return 0; |
| 801 | } |
| 802 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 803 | /* create BSSGP/NS layer instances */ |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 804 | 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] | 805 | uint16_t local_port, uint32_t sgsn_ip, |
Harald Welte | 30a73d8 | 2013-03-10 08:54:30 +0000 | [diff] [blame] | 806 | uint16_t sgsn_port, uint16_t nsei, uint16_t nsvci, uint16_t bvci, |
| 807 | uint16_t mcc, uint16_t mnc, uint16_t lac, uint16_t rac, |
| 808 | uint16_t cell_id) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 809 | { |
| 810 | struct sockaddr_in dest; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 811 | int rc; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 812 | |
Andreas Eversberg | 514491d | 2012-09-23 06:42:07 +0200 | [diff] [blame] | 813 | mcc = ((mcc & 0xf00) >> 8) * 100 + ((mcc & 0x0f0) >> 4) * 10 + (mcc & 0x00f); |
| 814 | mnc = ((mnc & 0xf00) >> 8) * 100 + ((mnc & 0x0f0) >> 4) * 10 + (mnc & 0x00f); |
| 815 | cell_id = ntohs(cell_id); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 816 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 817 | /* if already created... return the current address */ |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 818 | if (the_pcu.bctx) |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 819 | return &the_pcu; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 820 | |
Holger Hans Peter Freyther | b67a8a3 | 2013-07-28 18:55:14 +0200 | [diff] [blame] | 821 | the_pcu.bts = bts; |
| 822 | |
Andreas Eversberg | 71cce91 | 2013-01-16 13:49:00 +0100 | [diff] [blame] | 823 | bssgp_nsi = gprs_ns_instantiate(&sgsn_ns_cb, tall_pcu_ctx); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 824 | if (!bssgp_nsi) { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 825 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create NS instance\n"); |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 826 | return NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 827 | } |
Harald Welte | de5253a | 2013-01-11 09:45:17 +0100 | [diff] [blame] | 828 | gprs_ns_vty_init(bssgp_nsi); |
Harald Welte | 30a73d8 | 2013-03-10 08:54:30 +0000 | [diff] [blame] | 829 | bssgp_nsi->nsip.local_port = local_port; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 830 | rc = gprs_ns_nsip_listen(bssgp_nsi); |
| 831 | if (rc < 0) { |
| 832 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create socket\n"); |
| 833 | gprs_ns_destroy(bssgp_nsi); |
| 834 | bssgp_nsi = NULL; |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 835 | return NULL; |
Andreas Eversberg | a3c12fb | 2012-09-28 22:46:33 +0200 | [diff] [blame] | 836 | } |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 837 | |
| 838 | dest.sin_family = AF_INET; |
| 839 | dest.sin_port = htons(sgsn_port); |
| 840 | dest.sin_addr.s_addr = htonl(sgsn_ip); |
| 841 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 842 | the_pcu.nsvc = gprs_ns_nsip_connect(bssgp_nsi, &dest, nsei, nsvci); |
| 843 | if (!the_pcu.nsvc) { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 844 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create NSVCt\n"); |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 845 | gprs_ns_destroy(bssgp_nsi); |
| 846 | bssgp_nsi = NULL; |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 847 | return NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 848 | } |
| 849 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 850 | the_pcu.bctx = btsctx_alloc(bvci, nsei); |
| 851 | if (!the_pcu.bctx) { |
Andreas Eversberg | 0e40309 | 2012-07-06 11:04:57 +0200 | [diff] [blame] | 852 | LOGP(DBSSGP, LOGL_ERROR, "Failed to create BSSGP context\n"); |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 853 | the_pcu.nsvc = NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 854 | gprs_ns_destroy(bssgp_nsi); |
| 855 | bssgp_nsi = NULL; |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 856 | return NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 857 | } |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 858 | the_pcu.bctx->ra_id.mcc = spoof_mcc ? : mcc; |
| 859 | the_pcu.bctx->ra_id.mnc = spoof_mnc ? : mnc; |
| 860 | the_pcu.bctx->ra_id.lac = lac; |
| 861 | the_pcu.bctx->ra_id.rac = rac; |
| 862 | the_pcu.bctx->cell_id = cell_id; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 863 | |
| 864 | osmo_signal_register_handler(SS_L_NS, nsvc_signal_cb, NULL); |
| 865 | |
Holger Hans Peter Freyther | ed70cb7 | 2013-07-13 12:53:46 +0200 | [diff] [blame] | 866 | the_pcu.bvc_timer.cb = bvc_timeout; |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 867 | |
| 868 | |
Holger Hans Peter Freyther | e8d9a5f | 2013-07-28 19:11:20 +0200 | [diff] [blame] | 869 | return &the_pcu; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 870 | } |
| 871 | |
Daniel Willmann | 6d8884d | 2014-06-04 18:30:59 +0200 | [diff] [blame] | 872 | void gprs_bssgp_destroy(void) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 873 | { |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 874 | struct gprs_ns_inst *nsi = bssgp_nsi; |
| 875 | if (!nsi) |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 876 | return; |
| 877 | |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 878 | bssgp_nsi = NULL; |
| 879 | |
Holger Hans Peter Freyther | ed70cb7 | 2013-07-13 12:53:46 +0200 | [diff] [blame] | 880 | osmo_timer_del(&the_pcu.bvc_timer); |
Andreas Eversberg | cd8a83a | 2012-09-23 06:41:21 +0200 | [diff] [blame] | 881 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 882 | osmo_signal_unregister_handler(SS_L_NS, nsvc_signal_cb, NULL); |
| 883 | |
Holger Hans Peter Freyther | 90f08ef | 2013-07-13 12:45:44 +0200 | [diff] [blame] | 884 | the_pcu.nsvc = NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 885 | |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 886 | /* FIXME: blocking... */ |
Holger Hans Peter Freyther | 40bd0c4 | 2013-07-13 12:58:20 +0200 | [diff] [blame] | 887 | the_pcu.nsvc_unblocked = 0; |
| 888 | the_pcu.bvc_sig_reset = 0; |
| 889 | the_pcu.bvc_reset = 0; |
| 890 | the_pcu.bvc_unblocked = 0; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 891 | |
Jacob Erlbeck | c362df2 | 2016-01-20 22:02:19 +0100 | [diff] [blame] | 892 | gprs_ns_destroy(nsi); |
| 893 | |
| 894 | /* FIXME: move this to libgb: btsctx_free() */ |
| 895 | llist_del(&the_pcu.bctx->list); |
| 896 | #warning "This causes ASAN to complain. It is not critical for normal operation but should be fixed nevertheless" |
| 897 | #if 0 |
| 898 | talloc_free(the_pcu.bctx); |
| 899 | #endif |
| 900 | the_pcu.bctx = NULL; |
Andreas Eversberg | bf5a0f6 | 2012-07-06 08:58:22 +0200 | [diff] [blame] | 901 | } |
| 902 | |
Holger Hans Peter Freyther | 90692f9 | 2013-07-13 12:51:16 +0200 | [diff] [blame] | 903 | struct bssgp_bvc_ctx *gprs_bssgp_pcu_current_bctx(void) |
| 904 | { |
| 905 | return the_pcu.bctx; |
| 906 | } |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 907 | |
Jacob Erlbeck | 2db0f08 | 2015-09-07 18:49:00 +0200 | [diff] [blame] | 908 | void gprs_bssgp_update_frames_sent() |
| 909 | { |
| 910 | the_pcu.queue_frames_sent += 1; |
| 911 | } |
| 912 | |
| 913 | void gprs_bssgp_update_bytes_received(unsigned bytes_recv, unsigned frames_recv) |
| 914 | { |
| 915 | the_pcu.queue_bytes_recv += bytes_recv; |
| 916 | the_pcu.queue_frames_recv += frames_recv; |
| 917 | } |
| 918 | |
Jacob Erlbeck | 1e50a3d | 2015-06-16 10:18:42 +0200 | [diff] [blame] | 919 | void gprs_bssgp_update_queue_delay(const struct timeval *tv_recv, |
| 920 | const struct timeval *tv_now) |
Jacob Erlbeck | 6e4ccec | 2015-05-06 14:24:36 +0200 | [diff] [blame] | 921 | { |
| 922 | struct timeval *delay_sum = &the_pcu.queue_delay_sum; |
| 923 | struct timeval tv_delay; |
| 924 | |
| 925 | timersub(tv_now, tv_recv, &tv_delay); |
| 926 | timeradd(delay_sum, &tv_delay, delay_sum); |
| 927 | |
| 928 | the_pcu.queue_delay_count += 1; |
| 929 | } |