blob: eb76ab8692aeec6d504e8d319b7eb40d881df101 [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);
331 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF ends here **********\n");
332 talloc_free(tbf);
333}
334
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200335int gprs_rlcmac_tbf::update()
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200336{
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200337 struct gprs_rlcmac_tbf *ul_tbf = NULL;
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200338 struct gprs_rlcmac_bts *bts_data = bts->bts_data();
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200339 int rc;
340
341 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF update **********\n");
342
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200343 if (direction != GPRS_RLCMAC_DL_TBF)
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200344 return -EINVAL;
345
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200346 if (!ms_class) {
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200347 LOGP(DRLCMAC, LOGL_DEBUG, "- Cannot update, no class\n");
348 return -EINVAL;
349 }
350
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200351 ul_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200352
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200353 tbf_unlink_pdch(this);
354 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 +0200355 /* if no ressource */
356 if (rc < 0) {
357 LOGP(DRLCMAC, LOGL_ERROR, "No ressource after update???\n");
358 return -rc;
359 }
360
361 return 0;
362}
363
364int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf)
365{
366 if (tbf->control_ts == 0xff)
367 LOGP(DRLCMAC, LOGL_INFO, "- Setting Control TS %d\n",
368 tbf->first_common_ts);
369 else if (tbf->control_ts != tbf->first_common_ts)
370 LOGP(DRLCMAC, LOGL_INFO, "- Changing Control TS %d\n",
371 tbf->first_common_ts);
372 tbf->control_ts = tbf->first_common_ts;
373
374 return 0;
375}
376
377static const char *tbf_state_name[] = {
378 "NULL",
379 "ASSIGN",
380 "FLOW",
381 "FINISHED",
382 "WAIT RELEASE",
383 "RELEASING",
384};
385
386void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
387 enum gprs_rlcmac_tbf_state state)
388{
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200389 debug_diagram(tbf->bts, tbf->diag, "->%s", tbf_state_name[state]);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200390 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d changes state from %s to %s\n",
391 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tbf->tfi,
392 tbf_state_name[tbf->state], tbf_state_name[state]);
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200393 tbf->set_state(state);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200394}
395
396void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
397 unsigned int seconds, unsigned int microseconds)
398{
399 if (!osmo_timer_pending(&tbf->timer))
400 LOGP(DRLCMAC, LOGL_DEBUG, "Starting %s TBF=%d timer %u.\n",
401 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
402 tbf->tfi, T);
403 else
404 LOGP(DRLCMAC, LOGL_DEBUG, "Restarting %s TBF=%d timer %u "
405 "while old timer %u pending \n",
406 (tbf->direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
407 tbf->tfi, T, tbf->T);
408
409 tbf->T = T;
410 tbf->num_T_exp = 0;
411
412 /* Tunning timers can be safely re-scheduled. */
413 tbf->timer.data = tbf;
414 tbf->timer.cb = &tbf_timer_cb;
415
416 osmo_timer_schedule(&tbf->timer, seconds, microseconds);
417}
418
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200419void gprs_rlcmac_tbf::stop_t3191()
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200420{
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200421 return stop_timer();
422}
423
424void gprs_rlcmac_tbf::stop_timer()
425{
426 if (osmo_timer_pending(&timer)) {
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200427 LOGP(DRLCMAC, LOGL_DEBUG, "Stopping %s TBF=%d timer %u.\n",
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200428 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T);
429 osmo_timer_del(&timer);
Holger Hans Peter Freyther45561302013-10-16 17:55:57 +0200430 }
431}
432
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200433void gprs_rlcmac_tbf::poll_timeout()
434{
435 LOGP(DRLCMAC, LOGL_NOTICE, "Poll timeout for %s TBF=%d\n",
436 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi);
437
438 poll_state = GPRS_RLCMAC_POLL_NONE;
439
440 if (ul_ack_state == GPRS_RLCMAC_UL_ACK_WAIT_ACK) {
441 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK))) {
442 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
443 "PACKET CONTROL ACK for PACKET UPLINK ACK\n");
444 rlcmac_diag();
445 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ACK);
446 }
447 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
448 debug_diagram(bts, this->diag, "timeout UL-ACK");
449 if (state_is(GPRS_RLCMAC_FINISHED)) {
450 dir.ul.n3103++;
451 if (dir.ul.n3103 == bts->bts_data()->n3103) {
452 LOGP(DRLCMAC, LOGL_NOTICE,
453 "- N3103 exceeded\n");
454 debug_diagram(bts, diag, "N3103 exceeded");
455 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
456 tbf_timer_start(this, 3169, bts->bts_data()->t3169, 0);
457 return;
458 }
459 /* reschedule UL ack */
460 ul_ack_state = GPRS_RLCMAC_UL_ACK_SEND_ACK;
461 }
462 } else if (ul_ass_state == GPRS_RLCMAC_UL_ASS_WAIT_ACK) {
463 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS))) {
464 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
465 "PACKET CONTROL ACK for PACKET UPLINK "
466 "ASSIGNMENT.\n");
467 rlcmac_diag();
468 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_UL_ASS);
469 }
470 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
471 debug_diagram(bts, diag, "timeout UL-ASS");
472 n3105++;
473 if (n3105 == bts_data()->n3105) {
474 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
475 debug_diagram(bts, diag, "N3105 exceeded");
476 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
477 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
478 return;
479 }
480 /* reschedule UL assignment */
481 ul_ass_state = GPRS_RLCMAC_UL_ASS_SEND_ASS;
482 } else if (dl_ass_state == GPRS_RLCMAC_DL_ASS_WAIT_ACK) {
483 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS))) {
484 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
485 "PACKET CONTROL ACK for PACKET DOWNLINK "
486 "ASSIGNMENT.\n");
487 rlcmac_diag();
488 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ASS);
489 }
490 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
491 debug_diagram(bts, diag, "timeout DL-ASS");
492 n3105++;
493 if (n3105 == bts->bts_data()->n3105) {
494 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
495 debug_diagram(bts, diag, "N3105 exceeded");
496 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
497 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
498 return;
499 }
500 /* reschedule DL assignment */
501 dl_ass_state = GPRS_RLCMAC_DL_ASS_SEND_ASS;
502 } else if (direction == GPRS_RLCMAC_DL_TBF) {
503 if (!(state_flags & (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK))) {
504 LOGP(DRLCMAC, LOGL_NOTICE, "- Timeout for polling "
505 "PACKET DOWNLINK ACK.\n");
506 rlcmac_diag();
507 state_flags |= (1 << GPRS_RLCMAC_FLAG_TO_DL_ACK);
508 }
509 debug_diagram(bts, diag, "timeout DL-ACK");
510 n3105++;
511 if (n3105 == bts->bts_data()->n3105) {
512 LOGP(DRLCMAC, LOGL_NOTICE, "- N3105 exceeded\n");
513 debug_diagram(bts, diag, "N3105 exceeded");
514 tbf_new_state(this, GPRS_RLCMAC_RELEASING);
515 tbf_timer_start(this, 3195, bts_data()->t3195, 0);
516 return;
517 }
518 /* resend IMM.ASS on CCCH on timeout */
519 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))
520 && !(state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK))) {
521 LOGP(DRLCMAC, LOGL_DEBUG, "Re-send dowlink assignment "
522 "for TBF=%d on PCH (IMSI=%s)\n", tfi,
523 dir.dl.imsi);
524 /* send immediate assignment */
525 bts->snd_dl_ass(this, 0, dir.dl.imsi);
526 dir.dl.wait_confirm = 1;
527 }
528 } else
529 LOGP(DRLCMAC, LOGL_ERROR, "- Poll Timeout, but no event!\n");
530}
531
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200532struct gprs_rlcmac_tbf *tbf_alloc(struct gprs_rlcmac_bts *bts,
533 struct gprs_rlcmac_tbf *old_tbf, enum gprs_rlcmac_tbf_direction dir,
534 uint8_t tfi, uint8_t trx,
535 uint8_t ms_class, uint8_t single_slot)
536{
537 struct gprs_rlcmac_tbf *tbf;
538 int rc;
539
540#ifdef DEBUG_DIAGRAM
541 /* hunt for first free number in diagram */
542 int diagram_num;
543 for (diagram_num = 0; ; diagram_num++) {
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200544 llist_for_each_entry(tbf, &bts->ul_tbfs, list) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200545 if (tbf->diag == diagram_num)
546 goto next_diagram;
547 }
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200548 llist_for_each_entry(tbf, &bts->dl_tbfs, list) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200549 if (tbf->diag == diagram_num)
550 goto next_diagram;
551 }
552 break;
553next_diagram:
554 continue;
555 }
556#endif
557
558 LOGP(DRLCMAC, LOGL_DEBUG, "********** TBF starts here **********\n");
559 LOGP(DRLCMAC, LOGL_INFO, "Allocating %s TBF: TFI=%d TRX=%d "
560 "MS_CLASS=%d\n", (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL",
561 tfi, trx, ms_class);
562
563 if (trx >= 8 || tfi >= 32)
564 return NULL;
565
566 tbf = talloc_zero(tall_pcu_ctx, struct gprs_rlcmac_tbf);
567 if (!tbf)
568 return NULL;
569
Holger Hans Peter Freyther9f0c1d22013-10-19 21:24:34 +0200570 tbf->bts = bts->bts;
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200571#ifdef DEBUG_DIAGRAM
572 tbf->diag = diagram_num;
573#endif
574 tbf->direction = dir;
575 tbf->tfi = tfi;
Holger Hans Peter Freyther96efa702013-09-29 07:44:39 +0200576 tbf->trx_no = trx;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200577 tbf->trx = &bts->trx[trx];
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200578 tbf->arfcn = bts->trx[trx].arfcn;
579 tbf->ms_class = ms_class;
580 tbf->ws = 64;
581 tbf->sns = 128;
582 /* select algorithm */
583 rc = bts->alloc_algorithm(bts, old_tbf, tbf, bts->alloc_algorithm_curst,
584 single_slot);
585 /* if no ressource */
586 if (rc < 0) {
587 talloc_free(tbf);
588 return NULL;
589 }
590 /* assign control ts */
591 tbf->control_ts = 0xff;
592 rc = tbf_assign_control_ts(tbf);
593 /* if no ressource */
594 if (rc < 0) {
595 talloc_free(tbf);
596 return NULL;
597 }
598
599 /* set timestamp */
600 gettimeofday(&tbf->meas.dl_bw_tv, NULL);
601 gettimeofday(&tbf->meas.rssi_tv, NULL);
602 gettimeofday(&tbf->meas.dl_loss_tv, NULL);
603
604 INIT_LLIST_HEAD(&tbf->llc_queue);
605 if (dir == GPRS_RLCMAC_UL_TBF)
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200606 llist_add(&tbf->list, &bts->ul_tbfs);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200607 else
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200608 llist_add(&tbf->list, &bts->dl_tbfs);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200609
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200610 debug_diagram(bts->bts, tbf->diag, "+-----------------+");
611 debug_diagram(bts->bts, tbf->diag, "|NEW %s TBF TFI=%2d|",
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200612 (dir == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi);
Holger Hans Peter Freyther34bd8bd2013-10-19 21:10:38 +0200613 debug_diagram(bts->bts, tbf->diag, "+-----------------+");
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200614
615 return tbf;
616}
617
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200618static void tbf_timer_cb(void *_tbf)
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200619{
620 struct gprs_rlcmac_tbf *tbf = (struct gprs_rlcmac_tbf *)_tbf;
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200621 tbf->handle_timeout();
622}
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200623
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200624void gprs_rlcmac_tbf::handle_timeout()
625{
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200626 LOGP(DRLCMAC, LOGL_DEBUG, "%s TBF=%d timer %u expired.\n",
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200627 (direction == GPRS_RLCMAC_UL_TBF) ? "UL" : "DL", tfi, T);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200628
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200629 num_T_exp++;
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200630
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200631 switch (T) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200632 case 0: /* assignment */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200633 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH))) {
634 if (state_is(GPRS_RLCMAC_ASSIGN)) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200635 LOGP(DRLCMAC, LOGL_NOTICE, "Releasing due to "
636 "PACCH assignment timeout.\n");
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200637 tbf_free(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200638 } else
639 LOGP(DRLCMAC, LOGL_ERROR, "Error: TBF is not "
640 "in assign state\n");
641 }
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200642 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH))) {
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200643 /* change state to FLOW, so scheduler will start transmission */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200644 dir.dl.wait_confirm = 0;
645 if (state_is(GPRS_RLCMAC_ASSIGN)) {
646 tbf_new_state(this, GPRS_RLCMAC_FLOW);
647 tbf_assign_control_ts(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200648 } else
649 LOGP(DRLCMAC, LOGL_NOTICE, "Continue flow after "
650 "IMM.ASS confirm\n");
651 }
652 break;
653 case 3169:
654 case 3191:
655 case 3195:
656 LOGP(DRLCMAC, LOGL_NOTICE, "TBF T%d timeout during "
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200657 "transsmission\n", T);
658 rlcmac_diag();
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200659 /* fall through */
660 case 3193:
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200661 if (T == 3193)
662 debug_diagram(bts, diag, "T3193 timeout");
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200663 LOGP(DRLCMAC, LOGL_DEBUG, "TBF will be freed due to timeout\n");
664 /* free TBF */
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200665 tbf_free(this);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200666 break;
667 default:
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200668 LOGP(DRLCMAC, LOGL_ERROR, "Timer expired in unknown mode: %u\n", T);
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200669 }
670}
671
672int gprs_rlcmac_tbf::rlcmac_diag()
673{
674 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)))
675 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on CCCH\n");
676 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_PACCH)))
677 LOGP(DRLCMAC, LOGL_NOTICE, "- Assignment was on PACCH\n");
678 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_UL_DATA)))
679 LOGP(DRLCMAC, LOGL_NOTICE, "- Uplink data was received\n");
680 else if (direction == GPRS_RLCMAC_UL_TBF)
681 LOGP(DRLCMAC, LOGL_NOTICE, "- No uplink data received yet\n");
682 if ((state_flags & (1 << GPRS_RLCMAC_FLAG_DL_ACK)))
683 LOGP(DRLCMAC, LOGL_NOTICE, "- Downlink ACK was received\n");
684 else if (direction == GPRS_RLCMAC_DL_TBF)
685 LOGP(DRLCMAC, LOGL_NOTICE, "- No downlink ACK received yet\n");
686
687 return 0;
688}
689
Holger Hans Peter Freyther1702f102013-10-20 08:44:02 +0200690struct msgb *gprs_rlcmac_tbf::llc_dequeue(bssgp_bvc_ctx *bctx)
691{
692 struct msgb *msg;
693 struct timeval *tv, tv_now;
694 uint32_t octets = 0, frames = 0;
695
696 gettimeofday(&tv_now, NULL);
697
698 while ((msg = msgb_dequeue(&llc_queue))) {
699 tv = (struct timeval *)msg->data;
700 msgb_pull(msg, sizeof(*tv));
701 if (tv->tv_sec /* not infinite */
702 && (tv_now.tv_sec > tv->tv_sec /* and secs expired */
703 || (tv_now.tv_sec == tv->tv_sec /* .. or if secs equal .. */
704 && tv_now.tv_usec > tv->tv_usec))) { /* .. usecs expired */
705 LOGP(DRLCMACDL, LOGL_NOTICE, "Discarding LLC PDU of "
706 "DL TBF=%d, because lifetime limit reached\n",
707 tfi);
708 frames++;
709 octets += msg->len;
710 msgb_free(msg);
711 continue;
712 }
713 break;
714 }
715
716 if (frames) {
717 if (frames > 0xff)
718 frames = 0xff;
719 if (octets > 0xffffff)
720 octets = 0xffffff;
721 bssgp_tx_llc_discarded(bctx, tlli, frames, octets);
722 }
723
724 return msg;
725}
726
727void gprs_rlcmac_tbf::update_llc_frame(struct msgb *msg)
728{
729 /* TODO: bounds check */
730 memcpy(llc_frame, msg->data, msg->len);
731 llc_length = msg->len;
732}
733
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200734/*
735 * Store received block data in LLC message(s) and forward to SGSN
736 * if complete.
737 */
738int gprs_rlcmac_tbf::assemble_forward_llc(uint8_t *data, uint8_t len)
739{
740 struct rlc_ul_header *rh = (struct rlc_ul_header *)data;
741 uint8_t e, m;
742 struct rlc_li_field *li;
743 uint8_t frame_offset[16], offset = 0, chunk;
744 int i, frames = 0;
745
746 LOGP(DRLCMACUL, LOGL_DEBUG, "- Assembling frames: (len=%d)\n", len);
747
748 data += 3;
749 len -= 3;
750 e = rh->e; /* if extended */
751 m = 1; /* more frames, that means: the first frame */
752
753 /* Parse frame offsets from length indicator(s), if any. */
754 while (1) {
755 if (frames == (int)sizeof(frame_offset)) {
756 LOGP(DRLCMACUL, LOGL_ERROR, "Too many frames in "
757 "block\n");
758 return -EINVAL;
759 }
760 frame_offset[frames++] = offset;
761 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Frame %d starts at offset "
762 "%d\n", frames, offset);
763 if (!len)
764 break;
765 /* M == 0 and E == 0 is not allowed in this version. */
766 if (!m && !e) {
767 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d "
768 "ignored, because M='0' and E='0'.\n",
769 this->tfi);
770 return 0;
771 }
772 /* no more frames in this segment */
773 if (e) {
774 break;
775 }
776 /* There is a new frame and an LI that delimits it. */
777 if (m) {
778 li = (struct rlc_li_field *)data;
779 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Delimiter len=%d\n",
780 li->li);
781 /* Special case: LI == 0
782 * If the last segment would fit precisely into the
783 * rest of the RLC MAC block, there would be no way
784 * to delimit that this segment ends and is not
785 * continued in the next block.
786 * The special LI (0) is used to force the segment to
787 * extend into the next block, so it is delimited there.
788 * This LI must be skipped. Also it is the last LI.
789 */
790 if (li->li == 0) {
791 data++;
792 len--;
793 m = 1; /* M is ignored, we know there is more */
794 break; /* handle E as '1', so we break! */
795 }
796 e = li->e;
797 m = li->m;
798 offset += li->li;
799 data++;
800 len--;
801 continue;
802 }
803 }
804 if (!m) {
805 LOGP(DRLCMACUL, LOGL_DEBUG, "- Last frame carries spare "
806 "data\n");
807 }
808
809 LOGP(DRLCMACUL, LOGL_DEBUG, "- Data length after length fields: %d\n",
810 len);
811 /* TLLI */
812 if (rh->ti) {
813 if (len < 4) {
814 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of "
815 "frame border\n");
816 return -EINVAL;
817 }
818 data += 4;
819 len -= 4;
820 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping TLLI: "
821 "%d\n", len);
822 }
823
824 /* PFI */
825 if (rh->pi) {
826 LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, "
827 "please disable in SYSTEM INFORMATION\n");
828 if (len < 1) {
829 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA PFI out of "
830 "frame border\n");
831 return -EINVAL;
832 }
833 data++;
834 len--;
835 LOGP(DRLCMACUL, LOGL_DEBUG, "- Length after skipping PFI: "
836 "%d\n", len);
837 }
838
839 /* Now we have:
840 * - a list of frames offsets: frame_offset[]
841 * - number of frames: i
842 * - m == 0: Last frame carries spare data (end of TBF).
843 */
844
845 /* Check if last offset would exceed frame. */
846 if (offset > len) {
847 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TBF=%d ignored, "
848 "because LI delimits data that exceeds block size.\n",
849 this->tfi);
850 return -EINVAL;
851 }
852
853 /* create LLC frames */
854 for (i = 0; i < frames; i++) {
855 /* last frame ? */
856 if (i == frames - 1) {
857 /* no more data in last frame */
858 if (!m)
859 break;
860 /* data until end of frame */
861 chunk = len - frame_offset[i];
862 } else {
863 /* data until next frame */
864 chunk = frame_offset[i + 1] - frame_offset[i];
865 }
866 LOGP(DRLCMACUL, LOGL_DEBUG, "-- Appending chunk (len=%d) to "
867 "frame at %d.\n", chunk, this->llc_index);
868 if (this->llc_index + chunk > LLC_MAX_LEN) {
869 LOGP(DRLCMACUL, LOGL_NOTICE, "LLC frame exceeds "
870 "maximum size.\n");
871 chunk = LLC_MAX_LEN - this->llc_index;
872 }
873 memcpy(this->llc_frame + this->llc_index, data + frame_offset[i],
874 chunk);
875 this->llc_index += chunk;
876 /* not last frame. */
877 if (i != frames - 1) {
878 /* send frame to SGSN */
879 LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for "
880 "TBF=%d: len=%d\n", this->tfi, this->llc_index);
881 gprs_rlcmac_tx_ul_ud(this);
882 this->llc_index = 0; /* reset frame space */
883 /* also check if CV==0, because the frame may fill up the
884 * block precisely, then it is also complete. normally the
885 * frame would be extended into the next block with a 0-length
886 * delimiter added to this block. */
887 } else if (rh->cv == 0) {
888 /* send frame to SGSN */
889 LOGP(DRLCMACUL, LOGL_INFO, "Complete UL frame for "
890 "TBF=%d that fits precisely in last block: "
891 "len=%d\n", this->tfi, this->llc_index);
892 gprs_rlcmac_tx_ul_ud(this);
893 this->llc_index = 0; /* reset frame space */
894 }
895 }
896
897 return 0;
898}
899
Holger Hans Peter Freyther61a0a042013-10-26 21:48:38 +0200900/*
901 * Create DL data block
902 * The messages are fragmented and forwarded as data blocks.
903 */
904struct msgb *gprs_rlcmac_tbf::create_dl_acked_block(uint32_t fn, uint8_t ts)
905{
906 struct rlc_dl_header *rh;
907 struct rlc_li_field *li;
908 uint8_t block_length; /* total length of block, including spare bits */
909 uint8_t block_data; /* usable data of block, w/o spare bits, inc. MAC */
910 struct msgb *msg, *dl_msg;
911 uint8_t bsn;
912 uint16_t mod_sns = sns - 1;
913 uint16_t mod_sns_half = (sns >> 1) - 1;
914 uint16_t index;
915 uint8_t *delimiter, *data, *e_pointer;
916 uint8_t len;
917 uint16_t space, chunk;
918 int first_fin_ack = 0;
919
920 LOGP(DRLCMACDL, LOGL_DEBUG, "DL DATA TBF=%d downlink (V(A)==%d .. "
921 "V(S)==%d)\n", tfi, dir.dl.v_a, dir.dl.v_s);
922
923do_resend:
924 /* check if there is a block with negative acknowledgement */
925 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
926 bsn = (bsn + 1) & mod_sns) {
927 index = (bsn & mod_sns_half);
928 if (dir.dl.v_b[index] == 'N'
929 || dir.dl.v_b[index] == 'X') {
930 LOGP(DRLCMACDL, LOGL_DEBUG, "- Resending BSN %d\n",
931 bsn);
932 /* re-send block with negative aknowlegement */
933 dir.dl.v_b[index] = 'U'; /* unacked */
934 goto tx_block;
935 }
936 }
937
938 /* if the window has stalled, or transfer is complete,
939 * send an unacknowledged block */
940 if (state_is(GPRS_RLCMAC_FINISHED)
941 || ((dir.dl.v_s - dir.dl.v_a) & mod_sns) == ws) {
942 int resend = 0;
943
944 if (state_is(GPRS_RLCMAC_FINISHED))
945 LOGP(DRLCMACDL, LOGL_DEBUG, "- Restarting at BSN %d, "
946 "because all blocks have been transmitted.\n",
947 dir.dl.v_a);
948 else
949 LOGP(DRLCMACDL, LOGL_NOTICE, "- Restarting at BSN %d, "
950 "because all window is stalled.\n",
951 dir.dl.v_a);
952 /* If V(S) == V(A) and finished state, we would have received
953 * acknowledgement of all transmitted block. In this case we
954 * would have transmitted the final block, and received ack
955 * from MS. But in this case we did not receive the final ack
956 * indication from MS. This should never happen if MS works
957 * correctly. */
958 if (dir.dl.v_s == dir.dl.v_a) {
959 LOGP(DRLCMACDL, LOGL_DEBUG, "- MS acked all blocks, "
960 "so we re-transmit final block!\n");
961 /* we just send final block again */
962 index = ((dir.dl.v_s - 1) & mod_sns_half);
963 goto tx_block;
964 }
965
966 /* cycle through all unacked blocks */
967 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
968 bsn = (bsn + 1) & mod_sns) {
969 index = (bsn & mod_sns_half);
970 if (dir.dl.v_b[index] == 'U') {
971 /* mark to be re-send */
972 dir.dl.v_b[index] = 'X';
973 resend++;
974 }
975 }
976 /* At this point there should be at leasst one unacked block
977 * to be resent. If not, this is an software error. */
978 if (resend == 0) {
979 LOGP(DRLCMACDL, LOGL_ERROR, "Software error: "
980 "There are no unacknowledged blocks, but V(A) "
981 " != V(S). PLEASE FIX!\n");
982 /* we just send final block again */
983 index = ((dir.dl.v_s - 1) & mod_sns_half);
984 goto tx_block;
985 }
986 goto do_resend;
987 }
988
989 LOGP(DRLCMACDL, LOGL_DEBUG, "- Sending new block at BSN %d\n",
990 dir.dl.v_s);
991
992 /* now we still have untransmitted LLC data, so we fill mac block */
993 index = dir.dl.v_s & mod_sns_half;
994 data = rlc_block[index];
995#warning "Selection of the CS doesn't belong here"
996 if (cs == 0) {
997 cs = bts_data()->initial_cs_dl;
998 if (cs < 1 || cs > 4)
999 cs = 1;
1000 }
1001 block_length = gprs_rlcmac_cs[cs].block_length;
1002 block_data = gprs_rlcmac_cs[cs].block_data;
1003 memset(data, 0x2b, block_data); /* spare bits will be left 0 */
1004 rh = (struct rlc_dl_header *)data;
1005 rh->pt = 0; /* Data Block */
1006 rh->rrbp = rh->s_p = 0; /* Polling, set later, if required */
1007 rh->usf = 7; /* will be set at scheduler */
1008 rh->pr = 0; /* FIXME: power reduction */
1009 rh->tfi = tfi; /* TFI */
1010 rh->fbi = 0; /* Final Block Indicator, set late, if true */
1011 rh->bsn = dir.dl.v_s; /* Block Sequence Number */
1012 rh->e = 0; /* Extension bit, maybe set later */
1013 e_pointer = data + 2; /* points to E of current chunk */
1014 data += 3;
1015 delimiter = data; /* where next length header would be stored */
1016 space = block_data - 3;
1017 while (1) {
1018 chunk = llc_length - llc_index;
1019 /* if chunk will exceed block limit */
1020 if (chunk > space) {
1021 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1022 "larger than space (%d) left in block: copy "
1023 "only remaining space, and we are done\n",
1024 chunk, space);
1025 /* block is filled, so there is no extension */
1026 *e_pointer |= 0x01;
1027 /* fill only space */
1028 memcpy(data, llc_frame + llc_index, space);
1029 /* incement index */
1030 llc_index += space;
1031 /* return data block as message */
1032 break;
1033 }
1034 /* if FINAL chunk would fit precisely in space left */
1035 if (chunk == space && llist_empty(&llc_queue)) {
1036 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1037 "would exactly fit into space (%d): because "
1038 "this is a final block, we don't add length "
1039 "header, and we are done\n", chunk, space);
1040 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for "
1041 "TBF=%d that fits precisely in last block: "
1042 "len=%d\n", tfi, llc_length);
1043 gprs_rlcmac_dl_bw(this, llc_length);
1044 /* block is filled, so there is no extension */
1045 *e_pointer |= 0x01;
1046 /* fill space */
1047 memcpy(data, llc_frame + llc_index, space);
1048 /* reset LLC frame */
1049 llc_index = llc_length = 0;
1050 /* final block */
1051 rh->fbi = 1; /* we indicate final block */
1052 tbf_new_state(this, GPRS_RLCMAC_FINISHED);
1053 /* return data block as message */
1054 break;
1055 }
1056 /* if chunk would fit exactly in space left */
1057 if (chunk == space) {
1058 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d "
1059 "would exactly fit into space (%d): add length "
1060 "header with LI=0, to make frame extend to "
1061 "next block, and we are done\n", chunk, space);
1062 /* make space for delimiter */
1063 if (delimiter != data)
1064 memcpy(delimiter + 1, delimiter,
1065 data - delimiter);
1066 data++;
1067 space--;
1068 /* add LI with 0 length */
1069 li = (struct rlc_li_field *)delimiter;
1070 li->e = 1; /* not more extension */
1071 li->m = 0; /* shall be set to 0, in case of li = 0 */
1072 li->li = 0; /* chunk fills the complete space */
1073 // no need to set e_pointer nor increase delimiter
1074 /* fill only space, which is 1 octet less than chunk */
1075 memcpy(data, llc_frame + llc_index, space);
1076 /* incement index */
1077 llc_index += space;
1078 /* return data block as message */
1079 break;
1080 }
1081 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Chunk with length %d is less "
1082 "than remaining space (%d): add length header to "
1083 "to delimit LLC frame\n", chunk, space);
1084 /* the LLC frame chunk ends in this block */
1085 /* make space for delimiter */
1086 if (delimiter != data)
1087 memcpy(delimiter + 1, delimiter, data - delimiter);
1088 data++;
1089 space--;
1090 /* add LI to delimit frame */
1091 li = (struct rlc_li_field *)delimiter;
1092 li->e = 0; /* Extension bit, maybe set later */
1093 li->m = 0; /* will be set later, if there is more LLC data */
1094 li->li = chunk; /* length of chunk */
1095 e_pointer = delimiter; /* points to E of current delimiter */
1096 delimiter++;
1097 /* copy (rest of) LLC frame to space */
1098 memcpy(data, llc_frame + llc_index, chunk);
1099 data += chunk;
1100 space -= chunk;
1101 LOGP(DRLCMACDL, LOGL_INFO, "Complete DL frame for TBF=%d: "
1102 "len=%d\n", tfi, llc_length);
1103 gprs_rlcmac_dl_bw(this, llc_length);
1104 /* reset LLC frame */
1105 llc_index = llc_length = 0;
1106 /* dequeue next LLC frame, if any */
1107 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
1108 if (msg) {
1109 LOGP(DRLCMACDL, LOGL_INFO, "- Dequeue next LLC for "
1110 "TBF=%d (len=%d)\n", tfi, msg->len);
1111 update_llc_frame(msg);
1112 msgb_free(msg);
1113 }
1114 /* if we have more data and we have space left */
1115 if (space > 0 && llc_length) {
1116 li->m = 1; /* we indicate more frames to follow */
1117 continue;
1118 }
1119 /* if we don't have more LLC frames */
1120 if (!llc_length) {
1121 LOGP(DRLCMACDL, LOGL_DEBUG, "-- Final block, so we "
1122 "done.\n");
1123 li->e = 1; /* we cannot extend */
1124 rh->fbi = 1; /* we indicate final block */
1125 first_fin_ack = 1;
1126 /* + 1 indicates: first final ack */
1127 tbf_new_state(this, GPRS_RLCMAC_FINISHED);
1128 break;
1129 }
1130 /* we have no space left */
1131 LOGP(DRLCMACDL, LOGL_DEBUG, "-- No space left, so we are "
1132 "done.\n");
1133 li->e = 1; /* we cannot extend */
1134 break;
1135 }
1136 LOGP(DRLCMACDL, LOGL_DEBUG, "data block: %s\n",
1137 osmo_hexdump(rlc_block[index], block_length));
1138 rlc_block_len[index] = block_length;
1139 /* raise send state and set ack state array */
1140 dir.dl.v_b[index] = 'U'; /* unacked */
1141 dir.dl.v_s = (dir.dl.v_s + 1) & mod_sns; /* inc send state */
1142
1143tx_block:
1144 /* from this point on, new block is sent or old block is resent */
1145
1146 /* get data and header from current block */
1147 data = rlc_block[index];
1148 len = rlc_block_len[index];
1149 rh = (struct rlc_dl_header *)data;
1150
1151 /* Clear Polling, if still set in history buffer */
1152 rh->s_p = 0;
1153
1154 /* poll after POLL_ACK_AFTER_FRAMES frames, or when final block is tx.
1155 */
1156 if (dir.dl.tx_counter >= POLL_ACK_AFTER_FRAMES || first_fin_ack) {
1157 if (first_fin_ack) {
1158 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
1159 "polling, because first final block sent.\n");
1160 } else {
1161 LOGP(DRLCMACDL, LOGL_DEBUG, "- Scheduling Ack/Nack "
1162 "polling, because %d blocks sent.\n",
1163 POLL_ACK_AFTER_FRAMES);
1164 }
1165 /* scheduling not possible, because: */
1166 if (poll_state != GPRS_RLCMAC_POLL_NONE)
1167 LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already "
1168 "sheduled for TBF=%d, so we must wait for "
1169 "requesting downlink ack\n", tfi);
1170 else if (control_ts != ts)
1171 LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be "
1172 "sheduled in this TS %d, waiting for "
1173 "TS %d\n", ts, control_ts);
1174 else if (bts->sba()->find(trx_no, ts, (fn + 13) % 2715648))
1175 LOGP(DRLCMAC, LOGL_DEBUG, "Polling cannot be "
1176 "sheduled, because single block alllocation "
1177 "already exists\n");
1178 else {
1179 LOGP(DRLCMAC, LOGL_DEBUG, "Polling sheduled in this "
1180 "TS %d\n", ts);
1181 dir.dl.tx_counter = 0;
1182 /* start timer whenever we send the final block */
1183 if (rh->fbi == 1)
1184 tbf_timer_start(this, 3191, bts_data()->t3191, 0);
1185
1186 /* schedule polling */
1187 poll_state = GPRS_RLCMAC_POLL_SCHED;
1188 poll_fn = (fn + 13) % 2715648;
1189
1190#ifdef DEBUG_DIAGRAM
1191 debug_diagram(bts, diag, "poll DL-ACK");
1192 if (first_fin_ack)
1193 debug_diagram(bts, diag, "(is first FINAL)");
1194 if (rh->fbi)
1195 debug_diagram(bts, diag, "(FBI is set)");
1196#endif
1197
1198 /* set polling in header */
1199 rh->rrbp = 0; /* N+13 */
1200 rh->s_p = 1; /* Polling */
1201
1202 /* Increment TX-counter */
1203 dir.dl.tx_counter++;
1204 }
1205 } else {
1206 /* Increment TX-counter */
1207 dir.dl.tx_counter++;
1208 }
1209
1210 /* return data block as message */
1211 dl_msg = msgb_alloc(len, "rlcmac_dl_data");
1212 if (!dl_msg)
1213 return NULL;
1214 memcpy(msgb_put(dl_msg, len), data, len);
1215
1216 return dl_msg;
1217}
1218
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +02001219struct msgb *gprs_rlcmac_tbf::create_dl_ass(uint32_t fn)
1220{
1221 struct msgb *msg;
1222 struct gprs_rlcmac_tbf *new_tbf;
1223 int poll_ass_dl = POLLING_ASSIGNMENT_DL;
1224
1225 if (poll_ass_dl && direction == GPRS_RLCMAC_DL_TBF
1226 && control_ts != first_common_ts) {
1227 LOGP(DRLCMAC, LOGL_NOTICE, "Cannot poll for downlink "
1228 "assigment, because MS cannot reply. (control TS=%d, "
1229 "first common TS=%d)\n", control_ts,
1230 first_common_ts);
1231 poll_ass_dl = 0;
1232 }
1233 if (poll_ass_dl) {
1234 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1235 LOGP(DRLCMAC, LOGL_DEBUG, "Polling is already sheduled "
1236 "for TBF=%d, so we must wait for downlink "
1237 "assignment...\n", tfi);
1238 return NULL;
1239 }
1240 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1241 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1242 "scheduled for single block allocation...\n");
1243 return NULL;
1244 }
1245 }
1246
1247 /* on uplink TBF we get the downlink TBF to be assigned. */
1248 if (direction == GPRS_RLCMAC_UL_TBF) {
1249 /* be sure to check first, if contention resolution is done,
1250 * otherwise we cannot send the assignment yet */
1251 if (!dir.ul.contention_resolution_done) {
1252 LOGP(DRLCMAC, LOGL_DEBUG, "Cannot assign DL TBF now, "
1253 "because contention resolution is not "
1254 "finished.\n");
1255 return NULL;
1256 }
1257 #warning "THIS should probably go over the IMSI too"
1258 new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_DL_TBF);
1259 } else
1260 new_tbf = this;
1261 if (!new_tbf) {
1262 LOGP(DRLCMACDL, LOGL_ERROR, "We have a schedule for downlink "
1263 "assignment at uplink TBF=%d, but there is no downlink "
1264 "TBF\n", tfi);
1265 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
1266 return NULL;
1267 }
1268
1269 msg = msgb_alloc(23, "rlcmac_dl_ass");
1270 if (!msg)
1271 return NULL;
1272 bitvec *ass_vec = bitvec_alloc(23);
1273 if (!ass_vec) {
1274 msgb_free(msg);
1275 return NULL;
1276 }
1277 bitvec_unhex(ass_vec,
1278 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1279 LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Downlink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli);
1280 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1281 Encoding::write_packet_downlink_assignment(mac_control_block, tfi,
1282 (direction == GPRS_RLCMAC_DL_TBF), new_tbf,
1283 poll_ass_dl, bts_data()->alpha, bts_data()->gamma, -1, 0);
1284 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Downlink Assignment +++++++++++++++++++++++++\n");
1285 encode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
1286 LOGPC(DCSN1, LOGL_NOTICE, "\n");
1287 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Downlink Assignment -------------------------\n");
1288 bitvec_pack(ass_vec, msgb_put(msg, 23));
1289 bitvec_free(ass_vec);
1290 talloc_free(mac_control_block);
1291
1292 if (poll_ass_dl) {
1293 poll_state = GPRS_RLCMAC_POLL_SCHED;
1294 poll_fn = (fn + 13) % 2715648;
1295 dl_ass_state = GPRS_RLCMAC_DL_ASS_WAIT_ACK;
1296 } else {
1297 dl_ass_state = GPRS_RLCMAC_DL_ASS_NONE;
1298 tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW);
1299 tbf_assign_control_ts(new_tbf);
1300 /* stop pending assignment timer */
1301 new_tbf->stop_timer();
1302
1303 }
1304 debug_diagram(bts, diag, "send DL-ASS");
1305
1306 return msg;
1307}
1308
Holger Hans Peter Freytherae03f222013-10-26 21:20:51 +02001309struct msgb *gprs_rlcmac_tbf::create_ul_ass(uint32_t fn)
1310{
1311 struct msgb *msg;
1312 struct gprs_rlcmac_tbf *new_tbf;
1313
1314#if POLLING_ASSIGNMENT_UL == 1
1315 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1316 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1317 "sheduled for TBF=%d, so we must wait for uplink "
1318 "assignment...\n", tfi);
1319 return NULL;
1320 }
1321 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1322 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already scheduled for "
1323 "single block allocation...\n");
1324 return NULL;
1325 }
1326#endif
1327
1328 /* on down TBF we get the uplink TBF to be assigned. */
1329#warning "Probably want to find by IMSI too"
1330 if (direction == GPRS_RLCMAC_DL_TBF)
1331 new_tbf = bts->tbf_by_tlli(tlli, GPRS_RLCMAC_UL_TBF);
1332 else
1333 new_tbf = this;
1334
1335 if (!new_tbf) {
1336 LOGP(DRLCMACUL, LOGL_ERROR, "We have a schedule for uplink "
1337 "assignment at downlink TBF=%d, but there is no uplink "
1338 "TBF\n", tfi);
1339 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
1340 return NULL;
1341 }
1342
1343 msg = msgb_alloc(23, "rlcmac_ul_ass");
1344 if (!msg)
1345 return NULL;
1346 LOGP(DRLCMAC, LOGL_INFO, "TBF: START TFI: %u TLLI: 0x%08x Packet Uplink Assignment (PACCH)\n", new_tbf->tfi, new_tbf->tlli);
1347 bitvec *ass_vec = bitvec_alloc(23);
1348 if (!ass_vec) {
1349 msgb_free(msg);
1350 return NULL;
1351 }
1352 bitvec_unhex(ass_vec,
1353 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1354 Encoding::write_packet_uplink_assignment(bts_data(), ass_vec, tfi,
1355 (direction == GPRS_RLCMAC_DL_TBF), tlli,
1356 tlli_valid, new_tbf, POLLING_ASSIGNMENT_UL, bts_data()->alpha,
1357 bts_data()->gamma, -1);
1358 bitvec_pack(ass_vec, msgb_put(msg, 23));
1359 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1360 LOGP(DRLCMAC, LOGL_DEBUG, "+++++++++++++++++++++++++ TX : Packet Uplink Assignment +++++++++++++++++++++++++\n");
1361 decode_gsm_rlcmac_downlink(ass_vec, mac_control_block);
1362 LOGPC(DCSN1, LOGL_NOTICE, "\n");
1363 LOGP(DRLCMAC, LOGL_DEBUG, "------------------------- TX : Packet Uplink Assignment -------------------------\n");
1364 bitvec_free(ass_vec);
1365 talloc_free(mac_control_block);
1366
1367#if POLLING_ASSIGNMENT_UL == 1
1368 poll_state = GPRS_RLCMAC_POLL_SCHED;
1369 poll_fn = (fn + 13) % 2715648;
1370 ul_ass_state = GPRS_RLCMAC_UL_ASS_WAIT_ACK;
1371#else
1372 ul_ass_state = GPRS_RLCMAC_UL_ASS_NONE;
1373 tbf_new_state(new_tbf, GPRS_RLCMAC_FLOW);
1374 tbf_assign_control_ts(new_tbf);
1375#endif
1376 debug_diagram(bts, diag, "send UL-ASS");
1377
1378 return msg;
1379}
1380
Holger Hans Peter Freyther46bcb8d2013-10-26 21:04:28 +02001381struct msgb *gprs_rlcmac_tbf::create_ul_ack(uint32_t fn)
1382{
1383 int final = (state_is(GPRS_RLCMAC_FINISHED));
1384 struct msgb *msg;
1385
1386 if (final) {
1387 if (poll_state != GPRS_RLCMAC_POLL_NONE) {
1388 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1389 "sheduled for TBF=%d, so we must wait for "
1390 "final uplink ack...\n", tfi);
1391 return NULL;
1392 }
1393 if (bts->sba()->find(trx_no, control_ts, (fn + 13) % 2715648)) {
1394 LOGP(DRLCMACUL, LOGL_DEBUG, "Polling is already "
1395 "scheduled for single block allocation...\n");
1396 return NULL;
1397 }
1398 }
1399
1400 msg = msgb_alloc(23, "rlcmac_ul_ack");
1401 if (!msg)
1402 return NULL;
1403 bitvec *ack_vec = bitvec_alloc(23);
1404 if (!ack_vec) {
1405 msgb_free(msg);
1406 return NULL;
1407 }
1408 bitvec_unhex(ack_vec,
1409 "2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b");
1410 RlcMacDownlink_t * mac_control_block = (RlcMacDownlink_t *)talloc_zero(tall_pcu_ctx, RlcMacDownlink_t);
1411 Encoding::write_packet_uplink_ack(bts_data(), mac_control_block, this, final);
1412 encode_gsm_rlcmac_downlink(ack_vec, mac_control_block);
1413 bitvec_pack(ack_vec, msgb_put(msg, 23));
1414 bitvec_free(ack_vec);
1415 talloc_free(mac_control_block);
1416
1417 /* now we must set this flag, so we are allowed to assign downlink
Holger Hans Peter Freyther22b31192013-10-26 21:07:45 +02001418 * TBF on PACCH. it is only allowed when TLLI is acknowledged. */
Holger Hans Peter Freyther46bcb8d2013-10-26 21:04:28 +02001419 dir.ul.contention_resolution_done = 1;
1420
1421 if (final) {
1422 poll_state = GPRS_RLCMAC_POLL_SCHED;
1423 poll_fn = (fn + 13) % 2715648;
1424 /* waiting for final acknowledge */
1425 ul_ack_state = GPRS_RLCMAC_UL_ACK_WAIT_ACK;
1426 dir.ul.final_ack_sent = 1;
1427 } else
1428 ul_ack_state = GPRS_RLCMAC_UL_ACK_NONE;
1429 debug_diagram(bts->bts, diag, "send UL-ACK");
1430
1431 return msg;
1432}
1433
Holger Hans Peter Freyther3dc56a32013-10-26 21:38:30 +02001434int gprs_rlcmac_tbf::snd_dl_ack(uint8_t final, uint8_t ssn, uint8_t *rbb)
1435{
1436 char show_rbb[65], show_v_b[RLC_MAX_SNS + 1];
1437 uint16_t mod_sns = sns - 1;
1438 uint16_t mod_sns_half = (sns >> 1) - 1;
1439 int i; /* must be signed */
1440 int16_t dist; /* must be signed */
1441 uint8_t bit;
1442 uint16_t bsn;
1443 struct msgb *msg;
1444 uint16_t lost = 0, received = 0;
1445
1446 LOGP(DRLCMACDL, LOGL_DEBUG, "TBF=%d downlink acknowledge\n", tfi);
1447
1448 if (!final) {
1449 /* show received array in debug (bit 64..1) */
1450 for (i = 63; i >= 0; i--) {
1451 bit = (rbb[i >> 3] >> (7 - (i&7))) & 1;
1452 show_rbb[i] = (bit) ? '1' : 'o';
1453 }
1454 show_rbb[64] = '\0';
1455 LOGP(DRLCMACDL, LOGL_DEBUG, "- ack: (BSN=%d)\"%s\""
1456 "(BSN=%d) 1=ACK o=NACK\n", (ssn - 64) & mod_sns,
1457 show_rbb, (ssn - 1) & mod_sns);
1458
1459 /* apply received array to receive state (SSN-64..SSN-1) */
1460 /* calculate distance of ssn from V(S) */
1461 dist = (dir.dl.v_s - ssn) & mod_sns;
1462 /* check if distance is less than distance V(A)..V(S) */
1463 if (dist >= ((dir.dl.v_s - dir.dl.v_a) & mod_sns)) {
1464 /* this might happpen, if the downlink assignment
1465 * was not received by ms and the ack refers
1466 * to previous TBF
1467 * FIXME: we should implement polling for
1468 * control ack!*/
1469 LOGP(DRLCMACDL, LOGL_NOTICE, "- ack range is out of "
1470 "V(A)..V(S) range (DL TBF=%d) Free TFB!\n", tfi);
1471 return 1; /* indicate to free TBF */
1472 }
1473 /* SSN - 1 is in range V(A)..V(S)-1 */
1474 for (i = 63, bsn = (ssn - 1) & mod_sns;
1475 i >= 0 && bsn != ((dir.dl.v_a - 1) & mod_sns);
1476 i--, bsn = (bsn - 1) & mod_sns) {
1477 bit = (rbb[i >> 3] >> (7 - (i&7))) & 1;
1478 if (bit) {
1479 LOGP(DRLCMACDL, LOGL_DEBUG, "- got "
1480 "ack for BSN=%d\n", bsn);
1481 if (dir.dl.v_b[bsn & mod_sns_half]
1482 != 'A')
1483 received++;
1484 dir.dl.v_b[bsn & mod_sns_half] = 'A';
1485 } else {
1486 LOGP(DRLCMACDL, LOGL_DEBUG, "- got "
1487 "NACK for BSN=%d\n", bsn);
1488 dir.dl.v_b[bsn & mod_sns_half] = 'N';
1489 lost++;
1490 }
1491 }
1492 /* report lost and received packets */
1493 gprs_rlcmac_received_lost(this, received, lost);
1494
1495 /* raise V(A), if possible */
1496 for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1497 i++, bsn = (bsn + 1) & mod_sns) {
1498 if (dir.dl.v_b[bsn & mod_sns_half] == 'A') {
1499 dir.dl.v_b[bsn & mod_sns_half] = 'I';
1500 /* mark invalid */
1501 dir.dl.v_a = (dir.dl.v_a + 1)
1502 & mod_sns;
1503 } else
1504 break;
1505 }
1506
1507 /* show receive state array in debug (V(A)..V(S)-1) */
1508 for (i = 0, bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1509 i++, bsn = (bsn + 1) & mod_sns) {
1510 show_v_b[i] = dir.dl.v_b[bsn & mod_sns_half];
1511 if (show_v_b[i] == 0)
1512 show_v_b[i] = ' ';
1513 }
1514 show_v_b[i] = '\0';
1515 LOGP(DRLCMACDL, LOGL_DEBUG, "- V(B): (V(A)=%d)\"%s\""
1516 "(V(S)-1=%d) A=Acked N=Nacked U=Unacked "
1517 "X=Resend-Unacked\n", dir.dl.v_a, show_v_b,
1518 (dir.dl.v_s - 1) & mod_sns);
1519
1520 if (state_is(GPRS_RLCMAC_FINISHED)
1521 && dir.dl.v_s == dir.dl.v_a) {
1522 LOGP(DRLCMACDL, LOGL_NOTICE, "Received acknowledge of "
1523 "all blocks, but without final ack "
1524 "inidcation (don't worry)\n");
1525 }
1526 return 0;
1527 }
1528
1529 LOGP(DRLCMACDL, LOGL_DEBUG, "- Final ACK received.\n");
1530 debug_diagram(ts, diag, "got Final ACK");
1531 /* range V(A)..V(S)-1 */
1532 for (bsn = dir.dl.v_a; bsn != dir.dl.v_s;
1533 bsn = (bsn + 1) & mod_sns) {
1534 if (dir.dl.v_b[bsn & mod_sns_half] != 'A')
1535 received++;
1536 }
1537
1538 /* report all outstanding packets as received */
1539 gprs_rlcmac_received_lost(this, received, lost);
1540
1541 /* check for LLC PDU in the LLC Queue */
1542 msg = llc_dequeue(gprs_bssgp_pcu_current_bctx());
1543 if (!msg) {
1544 /* no message, start T3193, change state to RELEASE */
1545 LOGP(DRLCMACDL, LOGL_DEBUG, "- No new message, so we "
1546 "release.\n");
1547 /* start T3193 */
1548 debug_diagram(bts, diag, "start T3193");
1549 tbf_timer_start(this, 3193,
1550 bts_data()->t3193_msec / 1000,
1551 (bts_data()->t3193_msec % 1000) * 1000);
1552 tbf_new_state(this, GPRS_RLCMAC_WAIT_RELEASE);
1553
1554 return 0;
1555 }
1556 #warning "Copy and paste on the sender path"
1557 update_llc_frame(msg);
1558 msgb_free(msg);
1559
1560 /* we have a message, so we trigger downlink assignment, and there
1561 * set the state to ASSIGN. also we set old_downlink, because we
1562 * re-use this tbf. */
1563 LOGP(DRLCMAC, LOGL_DEBUG, "Trigger dowlink assignment on PACCH, "
1564 "because another LLC PDU has arrived in between\n");
1565 memset(&dir.dl, 0, sizeof(dir.dl)); /* reset RLC states */
1566 state_flags &= GPRS_RLCMAC_FLAG_TO_MASK; /* keep TO flags */
1567 state_flags &= ~(1 << GPRS_RLCMAC_FLAG_CCCH);
1568 update();
1569 bts->trigger_dl_ass(this, this, NULL);
1570 return 0;
1571}
1572
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +02001573void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_trx *trx)
1574{
1575 for (uint8_t tfi = 0; tfi < 32; tfi++) {
1576 struct gprs_rlcmac_tbf *tbf;
1577
1578 tbf = trx->ul_tbf[tfi];
1579 if (tbf)
1580 tbf_free(tbf);
1581 tbf = trx->dl_tbf[tfi];
1582 if (tbf)
1583 tbf_free(tbf);
1584 }
1585}
Holger Hans Peter Freyther4f6a4e5d2013-10-16 17:58:46 +02001586
1587void gprs_rlcmac_tbf::free_all(struct gprs_rlcmac_pdch *pdch)
1588{
1589 for (uint8_t tfi = 0; tfi < 32; tfi++) {
1590 struct gprs_rlcmac_tbf *tbf;
1591
1592 tbf = pdch->ul_tbf[tfi];
1593 if (tbf)
1594 tbf_free(tbf);
1595 tbf = pdch->dl_tbf[tfi];
1596 if (tbf)
1597 tbf_free(tbf);
1598 }
1599}