blob: cdd809836c009d29bb28156e170cc80fd21dd545 [file] [log] [blame]
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +02001/* Copied from gprs_bssgp_pcu.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +02004 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +02005 * Copyright (C) 2013 by Holger Hans Peter Freyther
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
Holger Hans Peter Freyther67ed34e2013-10-17 17:01:54 +020022#include <bts.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020023#include <tbf.h>
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +020024#include <rlc.h>
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +020025#include <encoding.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020026#include <gprs_rlcmac.h>
27#include <gprs_debug.h>
Holger Hans Peter Freyther3dc56a32013-10-26 21:38:30 +020028#include <gprs_bssgp_pcu.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020029
30extern "C" {
31#include <osmocom/core/msgb.h>
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +020032#include <osmocom/core/talloc.h>
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020033}
34
35#include <errno.h>
36#include <string.h>
37
Holger Hans Peter Freyther61a0a042013-10-26 21:48:38 +020038/* After sending these frames, we poll for ack/nack. */
39#define POLL_ACK_AFTER_FRAMES 20
40
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +020041/* If acknowledgement to downlink assignment should be polled */
42#define POLLING_ASSIGNMENT_DL 1
Holger Hans Peter Freytherae03f222013-10-26 21:20:51 +020043#define POLLING_ASSIGNMENT_UL 1
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +020044
Holger Hans Peter Freyther61a0a042013-10-26 21:48:38 +020045static const struct gprs_rlcmac_cs gprs_rlcmac_cs[] = {
46/* frame length data block max payload */
47 { 0, 0, 0 },
48 { 23, 23, 20 }, /* CS-1 */
49 { 34, 33, 30 }, /* CS-2 */
50 { 40, 39, 36 }, /* CS-3 */
51 { 54, 53, 50 }, /* CS-4 */
52};
53
Holger Hans Peter Freyther1702f102013-10-20 08:44:02 +020054extern "C" {
55int bssgp_tx_llc_discarded(struct bssgp_bvc_ctx *bctx, uint32_t tlli,
56 uint8_t num_frames, uint32_t num_octets);
57}
58
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +020059extern void *tall_pcu_ctx;
60
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +020061static void tbf_timer_cb(void *_tbf);
62
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +020063inline gprs_rlcmac_bts *gprs_rlcmac_tbf::bts_data() const
64{
65 return bts->bts_data();
66}
67
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +020068static inline void tbf_update_ms_class(struct gprs_rlcmac_tbf *tbf,
69 const uint8_t ms_class)
70{
71 if (!tbf->ms_class && ms_class)
72 tbf->ms_class = ms_class;
73}
74
Holger Hans Peter Freytherd8689282013-08-24 20:51:06 +020075static inline void tbf_assign_imsi(struct gprs_rlcmac_tbf *tbf,
76 const char *imsi)
77{
78 strncpy(tbf->meas.imsi, imsi, sizeof(tbf->meas.imsi) - 1);
79}
80
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020081static struct gprs_rlcmac_tbf *tbf_lookup_dl(BTS *bts,
82 const uint32_t tlli, const char *imsi)
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020083{
84 /* TODO: look up by IMSI first, then tlli, then old_tlli */
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +020085 return bts->tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020086}
87
88static int tbf_append_data(struct gprs_rlcmac_tbf *tbf,
89 struct gprs_rlcmac_bts *bts,
90 const uint8_t ms_class,
91 const uint16_t pdu_delay_csec,
92 const uint8_t *data, const uint16_t len)
93{
94 LOGP(DRLCMAC, LOGL_INFO, "TBF: APPEND TFI: %u TLLI: 0x%08x\n", tbf->tfi, tbf->tlli);
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +020095 if (tbf->state_is(GPRS_RLCMAC_WAIT_RELEASE)) {
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +020096 LOGP(DRLCMAC, LOGL_DEBUG, "TBF in WAIT RELEASE state "
97 "(T3193), so reuse TBF\n");
98 memcpy(tbf->llc_frame, data, len);
99 tbf->llc_length = len;
100 /* reset rlc states */
101 memset(&tbf->dir.dl, 0, sizeof(tbf->dir.dl));
102 /* keep to flags */
103 tbf->state_flags &= GPRS_RLCMAC_FLAG_TO_MASK;
104 tbf->state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +0200105 tbf_update_ms_class(tbf, ms_class);
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200106 tbf->update();
Holger Hans Peter Freyther24c1a5b2013-10-26 20:27:44 +0200107 tbf->bts->trigger_dl_ass(tbf, tbf, NULL);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200108 } else {
109 /* the TBF exists, so we must write it in the queue
110 * we prepend lifetime in front of PDU */
111 struct timeval *tv;
112 struct msgb *llc_msg = msgb_alloc(len + sizeof(*tv),
113 "llc_pdu_queue");
114 if (!llc_msg)
115 return -ENOMEM;
116 tv = (struct timeval *)msgb_put(llc_msg, sizeof(*tv));
117
118 uint16_t delay_csec;
119 if (bts->force_llc_lifetime)
120 delay_csec = bts->force_llc_lifetime;
121 else
122 delay_csec = pdu_delay_csec;
123 /* keep timestap at 0 for infinite delay */
124 if (delay_csec != 0xffff) {
125 /* calculate timestamp of timeout */
126 gettimeofday(tv, NULL);
127 tv->tv_usec += (delay_csec % 100) * 10000;
128 tv->tv_sec += delay_csec / 100;
129 if (tv->tv_usec > 999999) {
130 tv->tv_usec -= 1000000;
131 tv->tv_sec++;
132 }
133 }
134 memcpy(msgb_put(llc_msg, len), data, len);
135 msgb_enqueue(&tbf->llc_queue, llc_msg);
Holger Hans Peter Freytherd1d114f2013-08-24 20:46:18 +0200136 tbf_update_ms_class(tbf, ms_class);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200137 }
138
139 return 0;
140}
141
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200142static int tbf_new_dl_assignment(struct gprs_rlcmac_bts *bts,
143 const char *imsi,
144 const uint32_t tlli, const uint8_t ms_class,
145 const uint8_t *data, const uint16_t len)
146{
147 uint8_t trx, ta, ss;
148 int8_t use_trx;
149 struct gprs_rlcmac_tbf *old_tbf, *tbf;
150 int8_t tfi; /* must be signed */
151 int rc;
152
153 /* check for uplink data, so we copy our informations */
Holger Hans Peter Freytherbb20b2c2013-08-24 21:22:16 +0200154#warning "Do the same look up for IMSI, TLLI and OLD_TLLI"
155#warning "Refactor the below lines... into a new method"
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200156 tbf = bts->bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200157 if (tbf && tbf->dir.ul.contention_resolution_done
158 && !tbf->dir.ul.final_ack_sent) {
Holger Hans Peter Freyther96efa702013-09-29 07:44:39 +0200159 use_trx = tbf->trx_no;
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200160 ta = tbf->ta;
161 ss = 0;
162 old_tbf = tbf;
163 } else {
164 use_trx = -1;
165 /* we already have an uplink TBF, so we use that TA */
166 if (tbf)
167 ta = tbf->ta;
168 else {
169 /* recall TA */
Holger Hans Peter Freyther111614a2013-10-19 20:04:57 +0200170 rc = bts->bts->timing_advance()->recall(tlli);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200171 if (rc < 0) {
172 LOGP(DRLCMAC, LOGL_NOTICE, "TA unknown"
173 ", assuming 0\n");
174 ta = 0;
175 } else
176 ta = rc;
177 }
178 ss = 1; /* PCH assignment only allows one timeslot */
179 old_tbf = NULL;
180 }
181
182 // Create new TBF (any TRX)
Holger Hans Peter Freytherbcafdf82013-08-24 21:13:31 +0200183#warning "Copy and paste with alloc_ul_tbf"
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200184 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_DL_TBF, &trx, use_trx);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200185 if (tfi < 0) {
186 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH resource\n");
187 /* FIXME: send reject */
188 return -EBUSY;
189 }
190 /* set number of downlink slots according to multislot class */
191 tbf = tbf_alloc(bts, tbf, GPRS_RLCMAC_DL_TBF, tfi, trx, ms_class, ss);
192 if (!tbf) {
193 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
194 /* FIXME: send reject */
195 return -EBUSY;
196 }
197 tbf->tlli = tlli;
198 tbf->tlli_valid = 1;
199 tbf->ta = ta;
200
201 LOGP(DRLCMAC, LOGL_DEBUG,
202 "TBF: [DOWNLINK] START TFI: %d TLLI: 0x%08x \n",
203 tbf->tfi, tbf->tlli);
204
205 /* new TBF, so put first frame */
206 memcpy(tbf->llc_frame, data, len);
207 tbf->llc_length = len;
208
209 /* trigger downlink assignment and set state to ASSIGN.
210 * we don't use old_downlink, so the possible uplink is used
211 * to trigger downlink assignment. if there is no uplink,
212 * AGCH is used. */
Holger Hans Peter Freyther24c1a5b2013-10-26 20:27:44 +0200213 tbf->bts->trigger_dl_ass(tbf, old_tbf, imsi);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200214
215 /* store IMSI for debugging purpose. TODO: it is more than debugging */
216 tbf_assign_imsi(tbf, imsi);
217 return 0;
218}
219
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200220/**
221 * TODO: split into unit test-able parts...
222 */
223int tbf_handle(struct gprs_rlcmac_bts *bts,
224 const uint32_t tlli, const char *imsi,
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200225 const uint8_t ms_class, const uint16_t delay_csec,
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200226 const uint8_t *data, const uint16_t len)
227{
228 struct gprs_rlcmac_tbf *tbf;
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200229
230 /* check for existing TBF */
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200231 tbf = tbf_lookup_dl(bts->bts, tlli, imsi);
Holger Hans Peter Freyther31d0df92013-08-24 20:42:45 +0200232 if (tbf) {
233 int rc = tbf_append_data(tbf, bts, ms_class,
234 delay_csec, data, len);
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200235 if (rc >= 0)
236 tbf_assign_imsi(tbf, imsi);
237 return rc;
238 }
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200239
Holger Hans Peter Freyther443c8222013-08-24 20:59:46 +0200240 return tbf_new_dl_assignment(bts, imsi, tlli, ms_class, data, len);
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200241}
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +0200242
243struct gprs_rlcmac_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts,
244 int8_t use_trx, uint8_t ms_class,
245 uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf)
246{
247 uint8_t trx;
248 struct gprs_rlcmac_tbf *tbf;
249 uint8_t tfi;
250
251#warning "Copy and paste with tbf_new_dl_assignment"
252 /* create new TBF, use sme TRX as DL TBF */
Holger Hans Peter Freyther70ddde62013-10-26 19:17:58 +0200253 tfi = bts->bts->tfi_find_free(GPRS_RLCMAC_UL_TBF, &trx, use_trx);
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +0200254 if (tfi < 0) {
255 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
256 /* FIXME: send reject */
257 return NULL;
258 }
259 /* use multislot class of downlink TBF */
260 tbf = tbf_alloc(bts, dl_tbf, GPRS_RLCMAC_UL_TBF, tfi, trx, ms_class, 0);
261 if (!tbf) {
262 LOGP(DRLCMAC, LOGL_NOTICE, "No PDCH ressource\n");
263 /* FIXME: send reject */
264 return NULL;
265 }
266 tbf->tlli = tlli;
267 tbf->tlli_valid = 1; /* no contention resolution */
268 tbf->dir.ul.contention_resolution_done = 1;
269 tbf->ta = ta; /* use current TA */
270 tbf_new_state(tbf, GPRS_RLCMAC_ASSIGN);
271 tbf->state_flags |= (1 << GPRS_RLCMAC_FLAG_PACCH);
272 tbf_timer_start(tbf, 3169, bts->t3169, 0);
273
274 return tbf;
275}
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200276
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200277static void tbf_unlink_pdch(struct gprs_rlcmac_tbf *tbf)
278{
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200279 struct gprs_rlcmac_pdch *pdch;
280 int ts;
281
282 if (tbf->direction == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther173a2402013-10-16 21:47:45 +0200283 tbf->trx->ul_tbf[tbf->tfi] = NULL;
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200284 for (ts = 0; ts < 8; ts++) {
285 pdch = tbf->pdch[ts];
286 if (pdch)
287 pdch->ul_tbf[tbf->tfi] = NULL;
288 tbf->pdch[ts] = NULL;
289 }
290 } else {
Holger Hans Peter Freyther173a2402013-10-16 21:47:45 +0200291 tbf->trx->dl_tbf[tbf->tfi] = NULL;
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200292 for (ts = 0; ts < 8; ts++) {
293 pdch = tbf->pdch[ts];
294 if (pdch)
295 pdch->dl_tbf[tbf->tfi] = NULL;
296 tbf->pdch[ts] = NULL;
297 }
298 }
299}
300
301void tbf_free(struct gprs_rlcmac_tbf *tbf)
302{
303 struct msgb *msg;
304
305 /* Give final measurement report */
306 gprs_rlcmac_rssi_rep(tbf);
307 gprs_rlcmac_lost_rep(tbf);
308
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200309 debug_diagram(tbf->bts, tbf->diag, "+---------------+");
310 debug_diagram(tbf->bts, tbf->diag, "| THE END |");
311 debug_diagram(tbf->bts, tbf->diag, "+---------------+");
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200312 LOGP(DRLCMAC, LOGL_INFO, "Free %s TBF=%d with TLLI=0x%08x.\n",
313 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
314 tbf->tlli);
315 if (tbf->ul_ass_state != GPRS_RLCMAC_UL_ASS_NONE)
316 LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending uplink "
317 "assignment. This may not happen, because the "
318 "assignment message never gets transmitted. Please "
Holger Hans Peter Freyther66730052013-10-26 18:32:13 +0200319 "be sure not to free in this state. PLEASE FIX!\n");
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200320 if (tbf->dl_ass_state != GPRS_RLCMAC_DL_ASS_NONE)
321 LOGP(DRLCMAC, LOGL_ERROR, "Software error: Pending downlink "
322 "assignment. This may not happen, because the "
323 "assignment message never gets transmitted. Please "
Holger Hans Peter Freyther66730052013-10-26 18:32:13 +0200324 "be sure not to free in this state. PLEASE FIX!\n");
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200325 tbf->stop_timer();
Holger Hans Peter Freyther782f6dd2013-10-26 18:31:51 +0200326 #warning "TODO: Could/Should generate bssgp_tx_llc_discarded"
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200327 while ((msg = msgb_dequeue(&tbf->llc_queue)))
328 msgb_free(msg);
329 tbf_unlink_pdch(tbf);
330 llist_del(&tbf->list);
Holger Hans Peter Freyther15877642013-10-27 09:50:15 +0100331
332 if (tbf->direction == GPRS_RLCMAC_UL_TBF)
333 tbf->bts->tbf_ul_freed();
334 else
335 tbf->bts->tbf_dl_freed();
336
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200337 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF ends here **********\n");
338 talloc_free(tbf);
339}
340
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200341int gprs_rlcmac_tbf::update()
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200342{
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200343 struct gprs_rlcmac_tbf *ul_tbf = NULL;
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200344 struct gprs_rlcmac_bts *bts_data = bts->bts_data();
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200345 int rc;
346
347 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF update **********\n");
348
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200349 if (direction != GPRS_RLCMAC_DL_TBF)
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200350 return -EINVAL;
351
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200352 if (!ms_class) {
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200353 LOGP(DRLCMAC, LOGL_DEBUG, "- Cannot update, no class\n");
354 return -EINVAL;
355 }
356
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200357 ul_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200358
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200359 tbf_unlink_pdch(this);
360 rc = bts_data->alloc_algorithm(bts_data, ul_tbf, this, bts_data->alloc_algorithm_curst, 0);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200361 /* if no ressource */
362 if (rc < 0) {
363 LOGP(DRLCMAC, LOGL_ERROR, "No ressource after update???\n");
364 return -rc;
365 }
366
367 return 0;
368}
369
370int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf)
371{
372 if (tbf->control_ts == 0xff)
373 LOGP(DRLCMAC, LOGL_INFO, "- Setting Control TS %d\n",
374 tbf->first_common_ts);
375 else if (tbf->control_ts != tbf->first_common_ts)
376 LOGP(DRLCMAC, LOGL_INFO, "- Changing Control TS %d\n",
377 tbf->first_common_ts);
378 tbf->control_ts = tbf->first_common_ts;
379
380 return 0;
381}
382
383static const char *tbf_state_name[] = {
384 "NULL",
385 "ASSIGN",
386 "FLOW",
387 "FINISHED",
388 "WAIT RELEASE",
389 "RELEASING",
390};
391
392void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
393 enum gprs_rlcmac_tbf_state state)
394{
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200395 debug_diagram(tbf->bts, tbf->diag, "->%s", tbf_state_name[state]);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200396 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d changes state from %s to %s\n",
397 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
398 tbf_state_name[tbf->state], tbf_state_name[state]);
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200399 tbf->set_state(state);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200400}
401
402void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
403 unsigned int seconds, unsigned int microseconds)
404{
405 if (!osmo_timer_pending(&tbf->timer))
406 LOGP(DRLCMAC, LOGL_DEBUG, "Starting %s TBF=%d timer %u.\n",
407 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
408 tbf->tfi, T);
409 else
410 LOGP(DRLCMAC, LOGL_DEBUG, "Restarting %s TBF=%d timer %u "
411 "while old timer %u pending \n",
412 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
413 tbf->tfi, T, tbf->T);
414
415 tbf->T = T;
416 tbf->num_T_exp = 0;
417
418 /* Tunning timers can be safely re-scheduled. */
419 tbf->timer.data = tbf;
420 tbf->timer.cb = &tbf_timer_cb;
421
422 osmo_timer_schedule(&tbf->timer, seconds, microseconds);
423}
424
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200425void gprs_rlcmac_tbf::stop_t3191()
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200426{
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200427 return stop_timer();
428}
429
430void gprs_rlcmac_tbf::stop_timer()
431{
432 if (osmo_timer_pending(&timer)) {
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200433 LOGP(DRLCMAC, LOGL_DEBUG, "Stopping %s TBF=%d timer %u.\n",
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200434 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T);
435 osmo_timer_del(&timer);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200436 }
437}
438
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200439void gprs_rlcmac_tbf::poll_timeout()
440{
441 LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for %s TBF=%d\n",
442 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi);
443
444 poll_state = GPRS_RLCMAC_POLL_NONE;
445
446 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_WAIT_ACK) {
447 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK))) {
448 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
449 "PACKET CONTROL ACK for PACKET UPLINK ACK\n");
450 rlcmac_diag();
451 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
452 }
453 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
454 debug_diagram(bts, this->diag, "timeout UL-ACK");
455 if (state_is(GPRS_RLCMAC_FINISHED)) {
456 dir.ul.n3103++;
457 if (dir.ul.n3103 == bts->bts_data()->n3103) {
458 LOGP(DRLCMAC, LOGL_NOTICE,
459 "- N3103 exceeded\n");
460 debug_diagram(bts, diag, "N3103 exceeded");
461 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
462 tbf_timer_start(this, 3169, bts->bts_data()->t3169, 0);
463 return;
464 }
465 /* reschedule UL ack */
466 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
467 }
468 } else if (ul_ass_state == GPRS_RLCMAC_UL_ASS_WAIT_ACK) {
469 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS))) {
470 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
471 "PACKET CONTROL ACK for PACKET UPLINK "
472 "ASSIGNMENT.\n");
473 rlcmac_diag();
474 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS);
475 }
476 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
477 debug_diagram(bts, diag, "timeout UL-ASS");
478 n3105++;
479 if (n3105 == bts_data()->n3105) {
480 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
481 debug_diagram(bts, diag, "N3105 exceeded");
482 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
483 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
484 return;
485 }
486 /* reschedule UL assignment */
487 ul_ass_state = GPRS_RLCMAC_UL_ASS_SEND_ASS;
488 } else if (dl_ass_state == GPRS_RLCMAC_DL_ASS_WAIT_ACK) {
489 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS))) {
490 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
491 "PACKET CONTROL ACK for PACKET DOWNLINK "
492 "ASSIGNMENT.\n");
493 rlcmac_diag();
494 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS);
495 }
496 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
497 debug_diagram(bts, diag, "timeout DL-ASS");
498 n3105++;
499 if (n3105 == bts->bts_data()->n3105) {
500 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
501 debug_diagram(bts, diag, "N3105 exceeded");
502 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
503 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
504 return;
505 }
506 /* reschedule DL assignment */
507 dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
508 } else if (direction == GPRS_RLCMAC_DL_TBF) {
509 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK))) {
510 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
511 "PACKET DOWNLINK ACK.\n");
512 rlcmac_diag();
513 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
514 }
515 debug_diagram(bts, diag, "timeout DL-ACK");
516 n3105++;
517 if (n3105 == bts->bts_data()->n3105) {
518 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
519 debug_diagram(bts, diag, "N3105 exceeded");
520 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
521 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
522 return;
523 }
524 /* resend IMM.ASS on CCCH on timeout */
525 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))
526 && !(state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) {
527 LOGP(DRLCMAC, LOGL_DEBUG, "Re-send dowlink assignment "
528 "for TBF=%d on PCH (IMSI=%s)\n", tfi,
529 dir.dl.imsi);
530 /* send immediate assignment */
531 bts->snd_dl_ass(this, 0, dir.dl.imsi);
532 dir.dl.wait_confirm = 1;
533 }
534 } else
535 LOGP(DRLCMAC, LOGL_ERROR, "- Poll Timeout, but no event!\n");
536}
537
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200538struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
539 struct gprs_rlcmac_tbf *old_tbf, enum gprs_rlcmac_tbf_direction dir,
540 uint8_t tfi, uint8_t trx,
541 uint8_t ms_class, uint8_t single_slot)
542{
543 struct gprs_rlcmac_tbf *tbf;
544 int rc;
545
546#ifdef DEBUG_DIAGRAM
547 /* hunt for first free number in diagram */
548 int diagram_num;
549 for (diagram_num = 0; ; diagram_num++) {
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200550 llist_for_each_entry(tbf, &bts->ul_tbfs, list) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200551 if (tbf->diag == diagram_num)
552 goto next_diagram;
553 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200554 llist_for_each_entry(tbf, &bts->dl_tbfs, list) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200555 if (tbf->diag == diagram_num)
556 goto next_diagram;
557 }
558 break;
559next_diagram:
560 continue;
561 }
562#endif
563
564 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF starts here **********\n");
565 LOGP(DRLCMAC, LOGL_INFO, "Allocating %s TBF: TFI=%d TRX=%d "
566 "MS_CLASS=%d\n", (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
567 tfi, trx, ms_class);
568
569 if (trx >= 8 || tfi >= 32)
570 return NULL;
571
572 tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_tbf);
573 if (!tbf)
574 return NULL;
575
Holger Hans Peter Freyther9f0c1d22013-10-19 21:24:34 +0200576 tbf->bts = bts->bts;
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200577#ifdef DEBUG_DIAGRAM
578 tbf->diag = diagram_num;
579#endif
580 tbf->direction = dir;
581 tbf->tfi = tfi;
Holger Hans Peter Freyther96efa702013-09-29 07:44:39 +0200582 tbf->trx_no = trx;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200583 tbf->trx = &bts->trx[trx];
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200584 tbf->arfcn = bts->trx[trx].arfcn;
585 tbf->ms_class = ms_class;
586 tbf->ws = 64;
587 tbf->sns = 128;
588 /* select algorithm */
589 rc = bts->alloc_algorithm(bts, old_tbf, tbf, bts->alloc_algorithm_curst,
590 single_slot);
591 /* if no ressource */
592 if (rc < 0) {
593 talloc_free(tbf);
594 return NULL;
595 }
596 /* assign control ts */
597 tbf->control_ts = 0xff;
598 rc = tbf_assign_control_ts(tbf);
599 /* if no ressource */
600 if (rc < 0) {
601 talloc_free(tbf);
602 return NULL;
603 }
604
605 /* set timestamp */
606 gettimeofday(&tbf->meas.dl_bw_tv, NULL);
607 gettimeofday(&tbf->meas.rssi_tv, NULL);
608 gettimeofday(&tbf->meas.dl_loss_tv, NULL);
609
610 INIT_LLIST_HEAD(&tbf->llc_queue);
Holger Hans Peter Freyther15877642013-10-27 09:50:15 +0100611 if (dir == GPRS_RLCMAC_UL_TBF) {
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200612 llist_add(&tbf->list, &bts->ul_tbfs);
Holger Hans Peter Freyther15877642013-10-27 09:50:15 +0100613 tbf->bts->tbf_ul_created();
614 } else {
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200615 llist_add(&tbf->list, &bts->dl_tbfs);
Holger Hans Peter Freyther15877642013-10-27 09:50:15 +0100616 tbf->bts->tbf_dl_created();
617 }
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200618
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200619 debug_diagram(bts->bts, tbf->diag, "+-----------------+");
620 debug_diagram(bts->bts, tbf->diag, "|NEW %s TBF TFI=%2d|",
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200621 (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200622 debug_diagram(bts->bts, tbf->diag, "+-----------------+");
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200623
624 return tbf;
625}
626
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200627static void tbf_timer_cb(void *_tbf)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200628{
629 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf;
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200630 tbf->handle_timeout();
631}
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200632
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200633void gprs_rlcmac_tbf::handle_timeout()
634{
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200635 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d timer %u expired.\n",
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200636 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200637
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200638 num_T_exp++;
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200639
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200640 switch (T) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200641 case 0: /* assignment */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200642 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))) {
643 if (state_is(GPRS_RLCMAC_ASSIGN)) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200644 LOGP(DRLCMAC, LOGL_NOTICE, "Releasing due to "
645 "PACCH assignment timeout.\n");
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200646 tbf_free(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200647 } else
648 LOGP(DRLCMAC, LOGL_ERROR, "Error: TBF is not "
649 "in assign state\n");
650 }
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200651 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200652 /* change state to FLOW, so scheduler will start transmission */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200653 dir.dl.wait_confirm = 0;
654 if (state_is(GPRS_RLCMAC_ASSIGN)) {
655 tbf_new_state(this, GPRS_RLCMAC_FLOW);
656 tbf_assign_control_ts(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200657 } else
658 LOGP(DRLCMAC, LOGL_NOTICE, "Continue flow after "
659 "IMM.ASS confirm\n");
660 }
661 break;
662 case 3169:
663 case 3191:
664 case 3195:
665 LOGP(DRLCMAC, LOGL_NOTICE, "TBF T%d timeout during "
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200666 "transsmission\n", T);
667 rlcmac_diag();
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200668 /* fall through */
669 case 3193:
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200670 if (T == 3193)
671 debug_diagram(bts, diag, "T3193 timeout");
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200672 LOGP(DRLCMAC, LOGL_DEBUG, "TBF will be freed due to timeout\n");
673 /* free TBF */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200674 tbf_free(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200675 break;
676 default:
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200677 LOGP(DRLCMAC, LOGL_ERROR, "Timer expired in unknown mode: %u\n", T);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200678 }
679}
680
681int gprs_rlcmac_tbf::rlcmac_diag()
682{
683 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)))
684 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on CCCH\n");
685 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)))
686 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on PACCH\n");
687 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_UL_DATA)))
688 LOGP(DRLCMAC, LOGL_NOTICE, "- Uplink data was received\n");
689 else if (direction == GPRS_RLCMAC_UL_TBF)
690 LOGP(DRLCMAC, LOGL_NOTICE, "- No uplink data received yet\n");
691 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK)))
692 LOGP(DRLCMAC, LOGL_NOTICE, "- Downlink ACK was received\n");
693 else if (direction == GPRS_RLCMAC_DL_TBF)
694 LOGP(DRLCMAC, LOGL_NOTICE, "- No downlink ACK received yet\n");
695
696 return 0;
697}
698
Holger Hans Peter Freyther1702f102013-10-20 08:44:02 +0200699struct msgb *gprs_rlcmac_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
700{
701 struct msgb *msg;
702 struct timeval *tv, tv_now;
703 uint32_t octets = 0, frames = 0;
704
705 gettimeofday(&tv_now, NULL);
706
707 while ((msg = msgb_dequeue(&llc_queue))) {
708 tv = (struct timeval *)msg->data;
709 msgb_pull(msg, sizeof(*tv));
710 if (tv->tv_sec /* not infinite */
711 && (tv_now.tv_sec > tv->tv_sec /* and secs expired */
712 || (tv_now.tv_sec == tv->tv_sec /* .. or if secs equal .. */
713 && tv_now.tv_usec > tv->tv_usec))) { /* .. usecs expired */
714 LOGP(DRLCMACDL, LOGL_NOTICE, "Discarding LLC PDU of "
715 "DL TBF=%d, because lifetime limit reached\n",
716 tfi);
717 frames++;
718 octets += msg->len;
719 msgb_free(msg);
720 continue;
721 }
722 break;
723 }
724
725 if (frames) {
726 if (frames > 0xff)
727 frames = 0xff;
728 if (octets > 0xffffff)
729 octets = 0xffffff;
730 bssgp_tx_llc_discarded(bctx, tlli, frames, octets);
731 }
732
733 return msg;
734}
735
736void gprs_rlcmac_tbf::update_llc_frame(struct msgb *msg)
737{
738 /* TODO: bounds check */
739 memcpy(llc_frame, msg->data, msg->len);
740 llc_length = msg->len;
741}
742
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200743/*
744 * Store received block data in LLC message(s) and forward to SGSN
745 * if complete.
746 */
747int gprs_rlcmac_tbf::assemble_forward_llc(uint8_t *data, uint8_t len)
748{
749 struct rlc_ul_header *rh = (struct rlc_ul_header *)data;
750 uint8_t e, m;
751 struct rlc_li_field *li;
752 uint8_t frame_offset[16], offset = 0, chunk;
753 int i, frames = 0;
754
755 LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len);
756
757 data += 3;
758 len -= 3;
759 e = rh->e; /* if extended */
760 m = 1; /* more frames, that means: the first frame */
761
762 /* Parse frame offsets from length indicator(s), if any. */
763 while (1) {
764 if (frames == (int)sizeof(frame_offset)) {
765 LOGP(DRLCMACUL, LOGL_ERROR, "Too many frames in "
766 "block\n");
767 return -EINVAL;
768 }
769 frame_offset[frames++] = offset;
770 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset "
771 "%d\n", frames, offset);
772 if (!len)
773 break;
774 /* M == 0 and E == 0 is not allowed in this version. */
775 if (!m && !e) {
776 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d "
777 "ignored, because M='0' and E='0'.\n",
778 this->tfi);
779 return 0;
780 }
781 /* no more frames in this segment */
782 if (e) {
783 break;
784 }
785 /* There is a new frame and an LI that delimits it. */
786 if (m) {
787 li = (struct rlc_li_field *)data;
788 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Delimiter len=%d\n",
789 li->li);
790 /* Special case: LI == 0
791 * If the last segment would fit precisely into the
792 * rest of the RLC MAC block, there would be no way
793 * to delimit that this segment ends and is not
794 * continued in the next block.
795 * The special LI (0) is used to force the segment to
796 * extend into the next block, so it is delimited there.
797 * This LI must be skipped. Also it is the last LI.
798 */
799 if (li->li == 0) {
800 data++;
801 len--;
802 m = 1; /* M is ignored, we know there is more */
803 break; /* handle E as '1', so we break! */
804 }
805 e = li->e;
806 m = li->m;
807 offset += li->li;
808 data++;
809 len--;
810 continue;
811 }
812 }
813 if (!m) {
814 LOGP(DRLCMACUL, LOGL_DEBUG, "- Last frame carries spare "
815 "data\n");
816 }
817
818 LOGP(DRLCMACUL, LOGL_DEBUG, "- Data length after length fields: %d\n",
819 len);
820 /* TLLI */
821 if (rh->ti) {
822 if (len < 4) {
823 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of "
824 "frame border\n");
825 return -EINVAL;
826 }
827 data += 4;
828 len -= 4;
829 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping TLLI: "
830 "%d\n", len);
831 }
832
833 /* PFI */
834 if (rh->pi) {
835 LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, "
836 "please disable in SYSTEM INFORMATION\n");
837 if (len < 1) {
838 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA PFI out of "
839 "frame border\n");
840 return -EINVAL;
841 }
842 data++;
843 len--;
844 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping PFI: "
845 "%d\n", len);
846 }
847
848 /* Now we have:
849 * - a list of frames offsets: frame_offset[]
850 * - number of frames: i
851 * - m == 0: Last frame carries spare data (end of TBF).
852 */
853
854 /* Check if last offset would exceed frame. */
855 if (offset > len) {
856 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d ignored, "
857 "because LI delimits data that exceeds block size.\n",
858 this->tfi);
859 return -EINVAL;
860 }
861
862 /* create LLC frames */
863 for (i = 0; i < frames; i++) {
864 /* last frame ? */
865 if (i == frames - 1) {
866 /* no more data in last frame */
867 if (!m)
868 break;
869 /* data until end of frame */
870 chunk = len - frame_offset[i];
871 } else {
872 /* data until next frame */
873 chunk = frame_offset[i + 1] - frame_offset[i];
874 }
875 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Appending chunk (len=%d) to "
876 "frame at %d.\n", chunk, this->llc_index);
877 if (this->llc_index + chunk > LLC_MAX_LEN) {
878 LOGP(DRLCMACUL, LOGL_NOTICE, "LLC frame exceeds "
879 "maximum size.\n");
880 chunk = LLC_MAX_LEN - this->llc_index;
881 }
882 memcpy(this->llc_frame + this->llc_index, data + frame_offset[i],
883 chunk);
884 this->llc_index += chunk;
885 /* not last frame. */
886 if (i != frames - 1) {
887 /* send frame to SGSN */
888 LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for "
889 "TBF=%d: len=%d\n", this->tfi, this->llc_index);
890 gprs_rlcmac_tx_ul_ud(this);
891 this->llc_index = 0; /* reset frame space */
892 /* also check if CV==0, because the frame may fill up the
893 * block precisely, then it is also complete. normally the
894 * frame would be extended into the next block with a 0-length
895 * delimiter added to this block. */
896 } else if (rh->cv == 0) {
897 /* send frame to SGSN */
898 LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for "
899 "TBF=%d that fits precisely in last block: "
900 "len=%d\n", this->tfi, this->llc_index);
901 gprs_rlcmac_tx_ul_ud(this);
902 this->llc_index = 0; /* reset frame space */
903 }
904 }
905
906 return 0;
907}
908
Holger Hans Peter Freyther61a0a042013-10-26 21:48:38 +0200909/*
910 * Create DL data block
911 * The messages are fragmented and forwarded as data blocks.
912 */
913struct msgb *gprs_rlcmac_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
914{
915 struct rlc_dl_header *rh;
916 struct rlc_li_field *li;
917 uint8_t block_length; /* total length of block, including spare bits */
918 uint8_t block_data; /* usable data of block, w/o spare bits, inc. MAC */
919 struct msgb *msg, *dl_msg;
920 uint8_t bsn;
921 uint16_t mod_sns = sns - 1;
922 uint16_t mod_sns_half = (sns >> 1) - 1;
923 uint16_t index;
924 uint8_t *delimiter, *data, *e_pointer;
925 uint8_t len;
926 uint16_t space, chunk;
927 int first_fin_ack = 0;
928
929 LOGP(DRLCMACDL, LOGL_DEBUG, "DL DATA TBF=%d downlink (V(A)==%d .. "
930 "V(S)==%d)\n", tfi, dir.dl.v_a, dir.dl.v_s);
931
932do_resend:
933 /* check if there is a block with negative acknowledgement */
934 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
935 bsn = (bsn + 1) & mod_sns) {
936 index = (bsn & mod_sns_half);
937 if (dir.dl.v_b[index] == 'N'
938 || dir.dl.v_b[index] == 'X') {
939 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n",
940 bsn);
941 /* re-send block with negative aknowlegement */
942 dir.dl.v_b[index] = 'U'; /* unacked */
943 goto tx_block;
944 }
945 }
946
947 /* if the window has stalled, or transfer is complete,
948 * send an unacknowledged block */
949 if (state_is(GPRS_RLCMAC_FINISHED)
950 || ((dir.dl.v_s - dir.dl.v_a) & mod_sns) == ws) {
951 int resend = 0;
952
953 if (state_is(GPRS_RLCMAC_FINISHED))
954 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
955 "because all blocks have been transmitted.\n",
956 dir.dl.v_a);
957 else
958 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
959 "because all window is stalled.\n",
960 dir.dl.v_a);
961 /* If V(S) == V(A) and finished state, we would have received
962 * acknowledgement of all transmitted block. In this case we
963 * would have transmitted the final block, and received ack
964 * from MS. But in this case we did not receive the final ack
965 * indication from MS. This should never happen if MS works
966 * correctly. */
967 if (dir.dl.v_s == dir.dl.v_a) {
968 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
969 "so we re-transmit final block!\n");
970 /* we just send final block again */
971 index = ((dir.dl.v_s - 1) & mod_sns_half);
972 goto tx_block;
973 }
974
975 /* cycle through all unacked blocks */
976 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
977 bsn = (bsn + 1) & mod_sns) {
978 index = (bsn & mod_sns_half);
979 if (dir.dl.v_b[index] == 'U') {
980 /* mark to be re-send */
981 dir.dl.v_b[index] = 'X';
982 resend++;
983 }
984 }
985 /* At this point there should be at leasst one unacked block
986 * to be resent. If not, this is an software error. */
987 if (resend == 0) {
988 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
989 "There are no unacknowledged blocks, but V(A) "
990 " != V(S). PLEASE FIX!\n");
991 /* we just send final block again */
992 index = ((dir.dl.v_s - 1) & mod_sns_half);
993 goto tx_block;
994 }
995 goto do_resend;
996 }
997
998 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
999 dir.dl.v_s);
1000
1001 /* now we still have untransmitted LLC data, so we fill mac block */
1002 index = dir.dl.v_s & mod_sns_half;
1003 data = rlc_block[index];
1004#warning "Selection of the CS doesn't belong here"
1005 if (cs == 0) {
1006 cs = bts_data()->initial_cs_dl;
1007 if (cs < 1 || cs > 4)
1008 cs = 1;
1009 }
1010 block_length = gprs_rlcmac_cs[cs].block_length;
1011 block_data = gprs_rlcmac_cs[cs].block_data;
1012 memset(data, 0x2b, block_data); /* spare bits will be left 0 */
1013 rh = (struct rlc_dl_header *)data;
1014 rh->pt = 0; /* Data Block */
1015 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
1016 rh->usf = 7; /* will be set at scheduler */
1017 rh->pr = 0; /* FIXME: power reduction */
1018 rh->tfi = tfi; /* TFI */
1019 rh->fbi = 0; /* Final Block Indicator, set late, if true */
1020 rh->bsn = dir.dl.v_s; /* Block Sequence Number */
1021 rh->e = 0; /* Extension bit, maybe set later */
1022 e_pointer = data + 2; /* points to E of current chunk */
1023 data += 3;
1024 delimiter = data; /* where next length header would be stored */
1025 space = block_data - 3;
1026 while (1) {
1027 chunk = llc_length - llc_index;
1028 /* if chunk will exceed block limit */
1029 if (chunk > space) {
1030 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1031 "larger than space (%d) left in block: copy "
1032 "only remaining space, and we are done\n",
1033 chunk, space);
1034 /* block is filled, so there is no extension */
1035 *e_pointer |= 0x01;
1036 /* fill only space */
1037 memcpy(data, llc_frame + llc_index, space);
1038 /* incement index */
1039 llc_index += space;
1040 /* return data block as message */
1041 break;
1042 }
1043 /* if FINAL chunk would fit precisely in space left */
1044 if (chunk == space && llist_empty(&llc_queue)) {
1045 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1046 "would exactly fit into space (%d): because "
1047 "this is a final block, we don't add length "
1048 "header, and we are done\n", chunk, space);
1049 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
1050 "TBF=%d that fits precisely in last block: "
1051 "len=%d\n", tfi, llc_length);
1052 gprs_rlcmac_dl_bw(this, llc_length);
1053 /* block is filled, so there is no extension */
1054 *e_pointer |= 0x01;
1055 /* fill space */
1056 memcpy(data, llc_frame + llc_index, space);
1057 /* reset LLC frame */
1058 llc_index = llc_length = 0;
1059 /* final block */
1060 rh->fbi = 1; /* we indicate final block */
1061 tbf_new_state(this, GPRS_RLCMAC_FINISHED);
1062 /* return data block as message */
1063 break;
1064 }
1065 /* if chunk would fit exactly in space left */
1066 if (chunk == space) {
1067 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1068 "would exactly fit into space (%d): add length "
1069 "header with LI=0, to make frame extend to "
1070 "next block, and we are done\n", chunk, space);
1071 /* make space for delimiter */
1072 if (delimiter != data)
1073 memcpy(delimiter + 1, delimiter,
1074 data - delimiter);
1075 data++;
1076 space--;
1077 /* add LI with 0 length */
1078 li = (struct rlc_li_field *)delimiter;
1079 li->e = 1; /* not more extension */
1080 li->m = 0; /* shall be set to 0, in case of li = 0 */
1081 li->li = 0; /* chunk fills the complete space */
1082 // no need to set e_pointer nor increase delimiter
1083 /* fill only space, which is 1 octet less than chunk */
1084 memcpy(data, llc_frame + llc_index, space);
1085 /* incement index */
1086 llc_index += space;
1087 /* return data block as message */
1088 break;
1089 }
1090 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
1091 "than remaining space (%d): add length header to "
1092 "to delimit LLC frame\n", chunk, space);
1093 /* the LLC frame chunk ends in this block */
1094 /* make space for delimiter */
1095 if (delimiter != data)
1096 memcpy(delimiter + 1, delimiter, data - delimiter);
1097 data++;
1098 space--;
1099 /* add LI to delimit frame */
1100 li = (struct rlc_li_field *)delimiter;
1101 li->e = 0; /* Extension bit, maybe set later */
1102 li->m = 0; /* will be set later, if there is more LLC data */
1103 li->li = chunk; /* length of chunk */
1104 e_pointer = delimiter; /* points to E of current delimiter */
1105 delimiter++;
1106 /* copy (rest of) LLC frame to space */
1107 memcpy(data, llc_frame + llc_index, chunk);
1108 data += chunk;
1109 space -= chunk;
1110 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for TBF=%d: "
1111 "len=%d\n", tfi, llc_length);
1112 gprs_rlcmac_dl_bw(this, llc_length);
1113 /* reset LLC frame */
1114 llc_index = llc_length = 0;
1115 /* dequeue next LLC frame, if any */
1116 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
1117 if (msg) {
1118 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
1119 "TBF=%d (len=%d)\n", tfi, msg->len);
1120 update_llc_frame(msg);
1121 msgb_free(msg);
1122 }
1123 /* if we have more data and we have space left */
1124 if (space > 0 && llc_length) {
1125 li->m = 1; /* we indicate more frames to follow */
1126 continue;
1127 }
1128 /* if we don't have more LLC frames */
1129 if (!llc_length) {
1130 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
1131 "done.\n");
1132 li->e = 1; /* we cannot extend */
1133 rh->fbi = 1; /* we indicate final block */
1134 first_fin_ack = 1;
1135 /* + 1 indicates: first final ack */
1136 tbf_new_state(this, GPRS_RLCMAC_FINISHED);
1137 break;
1138 }
1139 /* we have no space left */
1140 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
1141 "done.\n");
1142 li->e = 1; /* we cannot extend */
1143 break;
1144 }
1145 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
1146 osmo_hexdump(rlc_block[index], block_length));
1147 rlc_block_len[index] = block_length;
1148 /* raise send state and set ack state array */
1149 dir.dl.v_b[index] = 'U'; /* unacked */
1150 dir.dl.v_s = (dir.dl.v_s + 1) & mod_sns; /* inc send state */
1151
1152tx_block:
1153 /* from this point on, new block is sent or old block is resent */
1154
1155 /* get data and header from current block */
1156 data = rlc_block[index];
1157 len = rlc_block_len[index];
1158 rh = (struct rlc_dl_header *)data;
1159
1160 /* Clear Polling, if still set in history buffer */
1161 rh->s_p = 0;
1162
1163 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
1164 */
1165 if (dir.dl.tx_counter >= POLL_ACK_AFTER_FRAMES || first_fin_ack) {
1166 if (first_fin_ack) {
1167 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
1168 "polling, because first final block sent.\n");
1169 } else {
1170 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
1171 "polling, because %d blocks sent.\n",
1172 POLL_ACK_AFTER_FRAMES);
1173 }
1174 /* scheduling not possible, because: */
1175 if (poll_state != GPRS_RLCMAC_POLL_NONE)
1176 LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already "
1177 "sheduled for TBF=%d, so we must wait for "
1178 "requesting downlink ack\n", tfi);
1179 else if (control_ts != ts)
1180 LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be "
1181 "sheduled in this TS %d, waiting for "
1182 "TS %d\n", ts, control_ts);
1183 else if (bts->sba()->find(trx_no, ts, (fn + 13) % 2715648))
1184 LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be "
1185 "sheduled, because single block alllocation "
1186 "already exists\n");
1187 else {
1188 LOGP(DRLCMAC, LOGL_DEBUG, "Polling sheduled in this "
1189 "TS %d\n", ts);
1190 dir.dl.tx_counter = 0;
1191 /* start timer whenever we send the final block */
1192 if (rh->fbi == 1)
1193 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
1194
1195 /* schedule polling */
1196 poll_state = GPRS_RLCMAC_POLL_SCHED;
1197 poll_fn = (fn + 13) % 2715648;
1198
1199#ifdef DEBUG_DIAGRAM
1200 debug_diagram(bts, diag, "poll DL-ACK");
1201 if (first_fin_ack)
1202 debug_diagram(bts, diag, "(is first FINAL)");
1203 if (rh->fbi)
1204 debug_diagram(bts, diag, "(FBI is set)");
1205#endif
1206
1207 /* set polling in header */
1208 rh->rrbp = 0; /* N+13 */
1209 rh->s_p = 1; /* Polling */
1210
1211 /* Increment TX-counter */
1212 dir.dl.tx_counter++;
1213 }
1214 } else {
1215 /* Increment TX-counter */
1216 dir.dl.tx_counter++;
1217 }
1218
1219 /* return data block as message */
1220 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
1221 if (!dl_msg)
1222 return NULL;
1223 memcpy(msgb_put(dl_msg, len), data, len);
1224
1225 return dl_msg;
1226}
1227
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +02001228struct msgb *gprs_rlcmac_tbf::create_dl_ass(uint32_t fn)
1229{
1230 struct msgb *msg;
1231 struct gprs_rlcmac_tbf *new_tbf;
1232 int poll_ass_dl = POLLING_ASSIGNMENT_DL;
1233
1234 if (poll_ass_dl && direction == GPRS_RLCMAC_DL_TBF
1235 && control_ts != first_common_ts) {
1236 LOGP(DRLCMAC, LOGL_NOTICE, "Cannot poll for downlink "
1237 "assigment, because MS cannot reply. (control TS=%d, "
1238 "first common TS=%d)\n", control_ts,
1239 first_common_ts);
1240 poll_ass_dl = 0;
1241 }
1242 if (poll_ass_dl) {
1243 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1244 LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already sheduled "
1245 "for TBF=%d, so we must wait for downlink "
1246 "assignment...\n", tfi);
1247 return NULL;
1248 }
1249 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1250 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1251 "scheduled for single block allocation...\n");
1252 return NULL;
1253 }
1254 }
1255
1256 /* on uplink TBF we get the downlink TBF to be assigned. */
1257 if (direction == GPRS_RLCMAC_UL_TBF) {
1258 /* be sure to check first, if contention resolution is done,
1259 * otherwise we cannot send the assignment yet */
1260 if (!dir.ul.contention_resolution_done) {
1261 LOGP(DRLCMAC, LOGL_DEBUG, "Cannot assign DL TBF now, "
1262 "because contention resolution is not "
1263 "finished.\n");
1264 return NULL;
1265 }
1266 #warning "THIS should probably go over the IMSI too"
1267 new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF);
1268 } else
1269 new_tbf = this;
1270 if (!new_tbf) {
1271 LOGP(DRLCMACDL, LOGL_ERROR, "We have a schedule for downlink "
1272 "assignment at uplink TBF=%d, but there is no downlink "
1273 "TBF\n", tfi);
1274 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
1275 return NULL;
1276 }
1277
1278 msg = msgb_alloc(23, "rlcmac_dl_ass");
1279 if (!msg)
1280 return NULL;
1281 bitvec *ass_vec = bitvec_alloc(23);
1282 if (!ass_vec) {
1283 msgb_free(msg);
1284 return NULL;
1285 }
1286 bitvec_unhex(ass_vec,
1287 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1288 LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Downlink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli);
1289 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1290 Encoding::write_packet_downlink_assignment(mac_control_block, tfi,
1291 (direction == GPRS_RLCMAC_DL_TBF), new_tbf,
1292 poll_ass_dl, bts_data()->alpha, bts_data()->gamma, -1, 0);
1293 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
1294 encode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
1295 LOGPC(DCSN1, LOGL_NOTICE, "\n");
1296 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
1297 bitvec_pack(ass_vec, msgb_put(msg, 23));
1298 bitvec_free(ass_vec);
1299 talloc_free(mac_control_block);
1300
1301 if (poll_ass_dl) {
1302 poll_state = GPRS_RLCMAC_POLL_SCHED;
1303 poll_fn = (fn + 13) % 2715648;
1304 dl_ass_state = GPRS_RLCMAC_DL_ASS_WAIT_ACK;
1305 } else {
1306 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
1307 tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW);
1308 tbf_assign_control_ts(new_tbf);
1309 /* stop pending assignment timer */
1310 new_tbf->stop_timer();
1311
1312 }
1313 debug_diagram(bts, diag, "send DL-ASS");
1314
1315 return msg;
1316}
1317
Holger Hans Peter Freytherae03f222013-10-26 21:20:51 +02001318struct msgb *gprs_rlcmac_tbf::create_ul_ass(uint32_t fn)
1319{
1320 struct msgb *msg;
1321 struct gprs_rlcmac_tbf *new_tbf;
1322
1323#if POLLING_ASSIGNMENT_UL == 1
1324 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1325 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1326 "sheduled for TBF=%d, so we must wait for uplink "
1327 "assignment...\n", tfi);
1328 return NULL;
1329 }
1330 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1331 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already scheduled for "
1332 "single block allocation...\n");
1333 return NULL;
1334 }
1335#endif
1336
1337 /* on down TBF we get the uplink TBF to be assigned. */
1338#warning "Probably want to find by IMSI too"
1339 if (direction == GPRS_RLCMAC_DL_TBF)
1340 new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
1341 else
1342 new_tbf = this;
1343
1344 if (!new_tbf) {
1345 LOGP(DRLCMACUL, LOGL_ERROR, "We have a schedule for uplink "
1346 "assignment at downlink TBF=%d, but there is no uplink "
1347 "TBF\n", tfi);
1348 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
1349 return NULL;
1350 }
1351
1352 msg = msgb_alloc(23, "rlcmac_ul_ass");
1353 if (!msg)
1354 return NULL;
1355 LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Uplink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli);
1356 bitvec *ass_vec = bitvec_alloc(23);
1357 if (!ass_vec) {
1358 msgb_free(msg);
1359 return NULL;
1360 }
1361 bitvec_unhex(ass_vec,
1362 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1363 Encoding::write_packet_uplink_assignment(bts_data(), ass_vec, tfi,
1364 (direction == GPRS_RLCMAC_DL_TBF), tlli,
1365 tlli_valid, new_tbf, POLLING_ASSIGNMENT_UL, bts_data()->alpha,
1366 bts_data()->gamma, -1);
1367 bitvec_pack(ass_vec, msgb_put(msg, 23));
1368 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1369 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Uplink Assignment +++++++++++++++++++++++++\n");
1370 decode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
1371 LOGPC(DCSN1, LOGL_NOTICE, "\n");
1372 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Uplink Assignment -------------------------\n");
1373 bitvec_free(ass_vec);
1374 talloc_free(mac_control_block);
1375
1376#if POLLING_ASSIGNMENT_UL == 1
1377 poll_state = GPRS_RLCMAC_POLL_SCHED;
1378 poll_fn = (fn + 13) % 2715648;
1379 ul_ass_state = GPRS_RLCMAC_UL_ASS_WAIT_ACK;
1380#else
1381 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
1382 tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW);
1383 tbf_assign_control_ts(new_tbf);
1384#endif
1385 debug_diagram(bts, diag, "send UL-ASS");
1386
1387 return msg;
1388}
1389
Holger Hans Peter Freyther46bcb8d2013-10-26 21:04:28 +02001390struct msgb *gprs_rlcmac_tbf::create_ul_ack(uint32_t fn)
1391{
1392 int final = (state_is(GPRS_RLCMAC_FINISHED));
1393 struct msgb *msg;
1394
1395 if (final) {
1396 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1397 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1398 "sheduled for TBF=%d, so we must wait for "
1399 "final uplink ack...\n", tfi);
1400 return NULL;
1401 }
1402 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1403 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1404 "scheduled for single block allocation...\n");
1405 return NULL;
1406 }
1407 }
1408
1409 msg = msgb_alloc(23, "rlcmac_ul_ack");
1410 if (!msg)
1411 return NULL;
1412 bitvec *ack_vec = bitvec_alloc(23);
1413 if (!ack_vec) {
1414 msgb_free(msg);
1415 return NULL;
1416 }
1417 bitvec_unhex(ack_vec,
1418 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1419 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1420 Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final);
1421 encode_gsm_rlcmac_downlink(ack_vec, mac_control_block);
1422 bitvec_pack(ack_vec, msgb_put(msg, 23));
1423 bitvec_free(ack_vec);
1424 talloc_free(mac_control_block);
1425
1426 /* now we must set this flag, so we are allowed to assign downlink
Holger Hans Peter Freyther22b31192013-10-26 21:07:45 +02001427 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
Holger Hans Peter Freyther46bcb8d2013-10-26 21:04:28 +02001428 dir.ul.contention_resolution_done = 1;
1429
1430 if (final) {
1431 poll_state = GPRS_RLCMAC_POLL_SCHED;
1432 poll_fn = (fn + 13) % 2715648;
1433 /* waiting for final acknowledge */
1434 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
1435 dir.ul.final_ack_sent = 1;
1436 } else
1437 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
1438 debug_diagram(bts->bts, diag, "send UL-ACK");
1439
1440 return msg;
1441}
1442
Holger Hans Peter Freyther3dc56a32013-10-26 21:38:30 +02001443int gprs_rlcmac_tbf::snd_dl_ack(uint8_t final, uint8_t ssn, uint8_t *rbb)
1444{
1445 char show_rbb[65], show_v_b[RLC_MAX_SNS + 1];
1446 uint16_t mod_sns = sns - 1;
1447 uint16_t mod_sns_half = (sns >> 1) - 1;
1448 int i; /* must be signed */
1449 int16_t dist; /* must be signed */
1450 uint8_t bit;
1451 uint16_t bsn;
1452 struct msgb *msg;
1453 uint16_t lost = 0, received = 0;
1454
1455 LOGP(DRLCMACDL, LOGL_DEBUG, "TBF=%d downlink acknowledge\n", tfi);
1456
1457 if (!final) {
1458 /* show received array in debug (bit 64..1) */
1459 for (i = 63; i >= 0; i--) {
1460 bit = (rbb[i >> 3] >> (7 - (i&7))) & 1;
1461 show_rbb[i] = (bit) ? '1' : 'o';
1462 }
1463 show_rbb[64] = '\0';
1464 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
1465 "(BSN=%d) 1=ACK o=NACK\n", (ssn - 64) & mod_sns,
1466 show_rbb, (ssn - 1) & mod_sns);
1467
1468 /* apply received array to receive state (SSN-64..SSN-1) */
1469 /* calculate distance of ssn from V(S) */
1470 dist = (dir.dl.v_s - ssn) & mod_sns;
1471 /* check if distance is less than distance V(A)..V(S) */
1472 if (dist >= ((dir.dl.v_s - dir.dl.v_a) & mod_sns)) {
1473 /* this might happpen, if the downlink assignment
1474 * was not received by ms and the ack refers
1475 * to previous TBF
1476 * FIXME: we should implement polling for
1477 * control ack!*/
1478 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
1479 "V(A)..V(S) range (DL TBF=%d) Free TFB!\n", tfi);
1480 return 1; /* indicate to free TBF */
1481 }
1482 /* SSN - 1 is in range V(A)..V(S)-1 */
1483 for (i = 63, bsn = (ssn - 1) & mod_sns;
1484 i >= 0 && bsn != ((dir.dl.v_a - 1) & mod_sns);
1485 i--, bsn = (bsn - 1) & mod_sns) {
1486 bit = (rbb[i >> 3] >> (7 - (i&7))) & 1;
1487 if (bit) {
1488 LOGP(DRLCMACDL, LOGL_DEBUG, "- got "
1489 "ack for BSN=%d\n", bsn);
1490 if (dir.dl.v_b[bsn & mod_sns_half]
1491 != 'A')
1492 received++;
1493 dir.dl.v_b[bsn & mod_sns_half] = 'A';
1494 } else {
1495 LOGP(DRLCMACDL, LOGL_DEBUG, "- got "
1496 "NACK for BSN=%d\n", bsn);
1497 dir.dl.v_b[bsn & mod_sns_half] = 'N';
1498 lost++;
1499 }
1500 }
1501 /* report lost and received packets */
1502 gprs_rlcmac_received_lost(this, received, lost);
1503
1504 /* raise V(A), if possible */
1505 for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1506 i++, bsn = (bsn + 1) & mod_sns) {
1507 if (dir.dl.v_b[bsn & mod_sns_half] == 'A') {
1508 dir.dl.v_b[bsn & mod_sns_half] = 'I';
1509 /* mark invalid */
1510 dir.dl.v_a = (dir.dl.v_a + 1)
1511 & mod_sns;
1512 } else
1513 break;
1514 }
1515
1516 /* show receive state array in debug (V(A)..V(S)-1) */
1517 for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1518 i++, bsn = (bsn + 1) & mod_sns) {
1519 show_v_b[i] = dir.dl.v_b[bsn & mod_sns_half];
1520 if (show_v_b[i] == 0)
1521 show_v_b[i] = ' ';
1522 }
1523 show_v_b[i] = '\0';
1524 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
1525 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
1526 "X=Resend-Unacked\n", dir.dl.v_a, show_v_b,
1527 (dir.dl.v_s - 1) & mod_sns);
1528
1529 if (state_is(GPRS_RLCMAC_FINISHED)
1530 && dir.dl.v_s == dir.dl.v_a) {
1531 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
1532 "all blocks, but without final ack "
1533 "inidcation (don't worry)\n");
1534 }
1535 return 0;
1536 }
1537
1538 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
1539 debug_diagram(ts, diag, "got Final ACK");
1540 /* range V(A)..V(S)-1 */
1541 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1542 bsn = (bsn + 1) & mod_sns) {
1543 if (dir.dl.v_b[bsn & mod_sns_half] != 'A')
1544 received++;
1545 }
1546
1547 /* report all outstanding packets as received */
1548 gprs_rlcmac_received_lost(this, received, lost);
1549
1550 /* check for LLC PDU in the LLC Queue */
1551 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
1552 if (!msg) {
1553 /* no message, start T3193, change state to RELEASE */
1554 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we "
1555 "release.\n");
1556 /* start T3193 */
1557 debug_diagram(bts, diag, "start T3193");
1558 tbf_timer_start(this, 3193,
1559 bts_data()->t3193_msec / 1000,
1560 (bts_data()->t3193_msec % 1000) * 1000);
1561 tbf_new_state(this, GPRS_RLCMAC_WAIT_RELEASE);
1562
1563 return 0;
1564 }
1565 #warning "Copy and paste on the sender path"
1566 update_llc_frame(msg);
1567 msgb_free(msg);
1568
1569 /* we have a message, so we trigger downlink assignment, and there
1570 * set the state to ASSIGN. also we set old_downlink, because we
1571 * re-use this tbf. */
1572 LOGP(DRLCMAC, LOGL_DEBUG, "Trigger dowlink assignment on PACCH, "
1573 "because another LLC PDU has arrived in between\n");
1574 memset(&dir.dl, 0, sizeof(dir.dl)); /* reset RLC states */
1575 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep TO flags */
1576 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
1577 update();
1578 bts->trigger_dl_ass(this, this, NULL);
1579 return 0;
1580}
1581
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +02001582void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_trx *trx)
1583{
1584 for (uint8_t tfi = 0; tfi < 32; tfi++) {
1585 struct gprs_rlcmac_tbf *tbf;
1586
1587 tbf = trx->ul_tbf[tfi];
1588 if (tbf)
1589 tbf_free(tbf);
1590 tbf = trx->dl_tbf[tfi];
1591 if (tbf)
1592 tbf_free(tbf);
1593 }
1594}
Holger Hans Peter Freyther4f6a4e5d2013-10-16 17:58:46 +02001595
1596void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_pdch *pdch)
1597{
1598 for (uint8_t tfi = 0; tfi < 32; tfi++) {
1599 struct gprs_rlcmac_tbf *tbf;
1600
1601 tbf = pdch->ul_tbf[tfi];
1602 if (tbf)
1603 tbf_free(tbf);
1604 tbf = pdch->dl_tbf[tfi];
1605 if (tbf)
1606 tbf_free(tbf);
1607 }
1608}