2
* Copyright (C) 2013 by Holger Hans Peter Freyther
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#include "gprs_rlcmac.h"
26
#include "cxx_linuxlist.h"
27
#include <gprs_debug.h>
31
#include <osmocom/core/utils.h>
44
#define Tassign_agch 0,200000 /* waiting after IMM.ASS confirm */
45
#define Tassign_pacch 2,0 /* timeout for pacch assigment */
46
#define Treject_pacch 0,2000 /* timeout for tbf reject for PRR*/
48
enum gprs_rlcmac_tbf_state {
49
GPRS_RLCMAC_NULL = 0, /* new created TBF */
50
GPRS_RLCMAC_ASSIGN, /* wait for downlink assignment */
51
GPRS_RLCMAC_FLOW, /* RLC/MAC flow, resource needed */
52
GPRS_RLCMAC_FINISHED, /* flow finished, wait for release */
53
GPRS_RLCMAC_WAIT_RELEASE,/* wait for release or restart of DL TBF */
54
GPRS_RLCMAC_RELEASING, /* releasing, wait to free TBI/USF */
57
enum gprs_rlcmac_tbf_poll_type {
58
GPRS_RLCMAC_POLL_UL_ASS,
59
GPRS_RLCMAC_POLL_DL_ASS,
60
GPRS_RLCMAC_POLL_UL_ACK,
61
GPRS_RLCMAC_POLL_DL_ACK,
64
enum gprs_rlcmac_tbf_poll_state {
65
GPRS_RLCMAC_POLL_NONE = 0,
66
GPRS_RLCMAC_POLL_SCHED, /* a polling was scheduled */
69
enum gprs_rlcmac_tbf_dl_ass_state {
70
GPRS_RLCMAC_DL_ASS_NONE = 0,
71
GPRS_RLCMAC_DL_ASS_SEND_ASS, /* send downlink assignment on next RTS */
72
GPRS_RLCMAC_DL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
75
extern const struct value_string gprs_rlcmac_tbf_dl_ass_state_names[];
77
enum gprs_rlcmac_tbf_ul_ass_state {
78
GPRS_RLCMAC_UL_ASS_NONE = 0,
79
GPRS_RLCMAC_UL_ASS_SEND_ASS, /* send uplink assignment on next RTS */
80
GPRS_RLCMAC_UL_ASS_SEND_ASS_REJ, /* send assignment reject next RTS */
81
GPRS_RLCMAC_UL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
84
extern const struct value_string gprs_rlcmac_tbf_ul_ass_state_names[];
86
enum gprs_rlcmac_tbf_ul_ack_state {
87
GPRS_RLCMAC_UL_ACK_NONE = 0,
88
GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */
89
GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */
92
enum gprs_rlcmac_tbf_direction {
101
enum tbf_gprs_counters {
108
enum tbf_egprs_counters {
109
TBF_CTR_EGPRS_DL_MCS1,
110
TBF_CTR_EGPRS_DL_MCS2,
111
TBF_CTR_EGPRS_DL_MCS3,
112
TBF_CTR_EGPRS_DL_MCS4,
113
TBF_CTR_EGPRS_DL_MCS5,
114
TBF_CTR_EGPRS_DL_MCS6,
115
TBF_CTR_EGPRS_DL_MCS7,
116
TBF_CTR_EGPRS_DL_MCS8,
117
TBF_CTR_EGPRS_DL_MCS9,
120
enum tbf_gprs_ul_counters {
127
enum tbf_egprs_ul_counters {
128
TBF_CTR_EGPRS_UL_MCS1,
129
TBF_CTR_EGPRS_UL_MCS2,
130
TBF_CTR_EGPRS_UL_MCS3,
131
TBF_CTR_EGPRS_UL_MCS4,
132
TBF_CTR_EGPRS_UL_MCS5,
133
TBF_CTR_EGPRS_UL_MCS6,
134
TBF_CTR_EGPRS_UL_MCS7,
135
TBF_CTR_EGPRS_UL_MCS8,
136
TBF_CTR_EGPRS_UL_MCS9,
139
#define GPRS_RLCMAC_FLAG_CCCH 0 /* assignment on CCCH */
140
#define GPRS_RLCMAC_FLAG_PACCH 1 /* assignment on PACCH */
141
#define GPRS_RLCMAC_FLAG_UL_DATA 2 /* uplink data received */
142
#define GPRS_RLCMAC_FLAG_DL_ACK 3 /* downlink acknowledge received */
143
#define GPRS_RLCMAC_FLAG_TO_UL_ACK 4
144
#define GPRS_RLCMAC_FLAG_TO_DL_ACK 5
145
#define GPRS_RLCMAC_FLAG_TO_UL_ASS 6
146
#define GPRS_RLCMAC_FLAG_TO_DL_ASS 7
147
#define GPRS_RLCMAC_FLAG_TO_MASK 0xf0 /* timeout bits */
149
struct gprs_rlcmac_tbf {
150
gprs_rlcmac_tbf(BTS *bts_, gprs_rlcmac_tbf_direction dir);
152
static void free_all(struct gprs_rlcmac_trx *trx);
153
static void free_all(struct gprs_rlcmac_pdch *pdch);
155
bool state_is(enum gprs_rlcmac_tbf_state rhs) const;
156
bool state_is_not(enum gprs_rlcmac_tbf_state rhs) const;
157
void set_state(enum gprs_rlcmac_tbf_state new_state);
158
const char *state_name() const;
160
const char *name() const;
162
struct msgb *create_dl_ass(uint32_t fn, uint8_t ts);
163
struct msgb *create_ul_ass(uint32_t fn, uint8_t ts);
164
struct msgb *create_packet_access_reject();
167
void set_ms(GprsMs *ms);
169
gprs_rlc_window *window();
176
void handle_timeout();
179
int establish_dl_tbf_on_pacch();
181
int check_polling(uint32_t fn, uint8_t ts,
182
uint32_t *poll_fn, unsigned int *rrbp);
183
void set_polling(uint32_t poll_fn, uint8_t ts, enum gprs_rlcmac_tbf_poll_type t);
187
uint32_t tlli() const;
188
bool is_tlli_valid() const;
191
void update_ms(uint32_t tlli, enum gprs_rlcmac_tbf_direction);
194
bool is_tfi_assigned() const;
196
const char *imsi() const;
197
void assign_imsi(const char *imsi);
199
void set_ta(uint8_t);
200
uint8_t ms_class() const;
201
void set_ms_class(uint8_t);
202
GprsCodingScheme current_cs() const;
203
int llc_queue_size() const;
205
time_t created_ts() const;
206
uint8_t dl_slots() const;
207
uint8_t ul_slots() const;
209
bool is_control_ts(uint8_t ts) const;
212
bool is_egprs_enabled() const;
214
void disable_egprs();
216
/* attempt to make things a bit more fair */
217
void rotate_in_list();
219
LListHead<gprs_rlcmac_tbf>& ms_list() {return this->m_ms_list;}
220
const LListHead<gprs_rlcmac_tbf>& ms_list() const {return this->m_ms_list;}
222
LListHead<gprs_rlcmac_tbf>& list();
223
const LListHead<gprs_rlcmac_tbf>& list() const;
225
uint32_t state_flags;
226
enum gprs_rlcmac_tbf_direction direction;
227
struct gprs_rlcmac_trx *trx;
228
uint8_t first_ts; /* first TS used by TBF */
229
uint8_t first_common_ts; /* first TS that the phone can send and
230
reveive simultaniously */
231
uint8_t control_ts; /* timeslot control messages and polling */
232
struct gprs_rlcmac_pdch *pdch[8]; /* list of PDCHs allocated to TBF */
236
enum gprs_rlcmac_tbf_dl_ass_state dl_ass_state;
237
enum gprs_rlcmac_tbf_ul_ass_state ul_ass_state;
238
enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state;
240
enum gprs_rlcmac_tbf_poll_state poll_state;
241
uint32_t poll_fn; /* frame number to poll */
242
uint8_t poll_ts; /* TS to poll */
246
uint8_t n3105; /* N3105 counter */
248
struct osmo_timer_list timer;
249
unsigned int T; /* Txxxx number */
250
unsigned int num_T_exp; /* number of consecutive T expirations */
252
struct osmo_gsm_timer_list gsm_timer;
253
unsigned int fT; /* fTxxxx number */
254
unsigned int num_fT_exp; /* number of consecutive fT expirations */
257
struct timeval rssi_tv; /* timestamp for rssi calculation */
258
int32_t rssi_sum; /* sum of rssi values */
259
int rssi_num; /* number of rssi values added since rssi_tv */
264
/* these should become protected but only after gprs_rlcmac_data.c
265
* stops to iterate over all tbf in its current form */
266
enum gprs_rlcmac_tbf_state state;
268
/* Remember if the tbf was in wait_release state when we want to
269
* schedule a new dl assignment */
270
uint8_t was_releasing;
272
/* Can/should we upgrade this tbf to use multiple slots? */
273
uint8_t upgrade_to_multislot;
275
/* store the BTS this TBF belongs to */
279
* private fields. We can't make it private as it is breaking the
285
struct rate_ctr_group *m_ctrs;
288
gprs_rlcmac_bts *bts_data() const;
290
int set_tlli_from_ul(uint32_t new_tlli);
291
void merge_and_clear_ms(GprsMs *old_ms);
293
gprs_llc_queue *llc_queue();
294
const gprs_llc_queue *llc_queue() const;
296
static const char *tbf_state_name[6];
300
/* Fields to take the TA/MS class values if no MS is associated */
305
LListHead<gprs_rlcmac_tbf> m_list;
306
LListHead<gprs_rlcmac_tbf> m_ms_list;
307
bool m_egprs_enabled;
309
mutable char m_name_buf[60];
313
struct gprs_rlcmac_ul_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts,
314
int8_t use_trx, uint8_t ms_class, uint8_t egprs_ms_class,
315
uint32_t tlli, uint8_t ta, GprsMs *ms);
317
struct gprs_rlcmac_ul_tbf *tbf_alloc_ul_tbf(struct gprs_rlcmac_bts *bts,
318
GprsMs *ms, int8_t use_trx,
319
uint8_t ms_class, uint8_t egprs_ms_class, uint8_t single_slot);
321
struct gprs_rlcmac_dl_tbf *tbf_alloc_dl_tbf(struct gprs_rlcmac_bts *bts,
322
GprsMs *ms, int8_t use_trx,
323
uint8_t ms_class, uint8_t egprs_ms_class, uint8_t single_slot);
325
void tbf_free(struct gprs_rlcmac_tbf *tbf);
327
struct gprs_rlcmac_ul_tbf *handle_tbf_reject(struct gprs_rlcmac_bts *bts,
328
GprsMs *ms, uint32_t tlli, uint8_t trx_no, uint8_t ts_no);
330
int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf);
332
void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
333
unsigned int seconds, unsigned int microseconds);
335
inline bool gprs_rlcmac_tbf::state_is(enum gprs_rlcmac_tbf_state rhs) const
340
inline bool gprs_rlcmac_tbf::state_is_not(enum gprs_rlcmac_tbf_state rhs) const
345
const char *tbf_name(gprs_rlcmac_tbf *tbf);
347
inline const char *gprs_rlcmac_tbf::state_name() const
349
return tbf_state_name[state];
352
inline void gprs_rlcmac_tbf::set_state(enum gprs_rlcmac_tbf_state new_state)
354
LOGP(DRLCMAC, LOGL_DEBUG, "%s changes state from %s to %s\n",
356
tbf_state_name[state], tbf_state_name[new_state]);
360
inline LListHead<gprs_rlcmac_tbf>& gprs_rlcmac_tbf::list()
365
inline const LListHead<gprs_rlcmac_tbf>& gprs_rlcmac_tbf::list() const
370
inline GprsMs *gprs_rlcmac_tbf::ms() const
375
inline bool gprs_rlcmac_tbf::is_tlli_valid() const
380
inline bool gprs_rlcmac_tbf::is_tfi_assigned() const
382
/* The TBF is established or has been assigned by a IMM.ASS for
384
return state > GPRS_RLCMAC_ASSIGN ||
385
(direction == GPRS_RLCMAC_DL_TBF &&
386
state == GPRS_RLCMAC_ASSIGN &&
387
(state_flags & (1 << GPRS_RLCMAC_FLAG_CCCH)));
390
inline uint8_t gprs_rlcmac_tbf::tfi() const
395
inline time_t gprs_rlcmac_tbf::created_ts() const
400
inline bool gprs_rlcmac_tbf::is_egprs_enabled() const
402
return m_egprs_enabled;
405
inline void gprs_rlcmac_tbf::enable_egprs()
407
m_egprs_enabled = true;
410
inline void gprs_rlcmac_tbf::disable_egprs()
412
m_egprs_enabled = false;
415
struct gprs_rlcmac_dl_tbf : public gprs_rlcmac_tbf {
416
gprs_rlcmac_dl_tbf(BTS *bts);
420
/* dispatch Unitdata.DL messages */
421
static int handle(struct gprs_rlcmac_bts *bts,
422
const uint32_t tlli, const uint32_t old_tlli,
423
const char *imsi, const uint8_t ms_class,
424
const uint8_t egprs_ms_class, const uint16_t delay_csec,
425
const uint8_t *data, const uint16_t len);
427
int append_data(const uint8_t ms_class,
428
const uint16_t pdu_delay_csec,
429
const uint8_t *data, const uint16_t len);
431
int rcvd_dl_ack(uint8_t final, uint8_t ssn, uint8_t *rbb);
432
int rcvd_dl_ack(uint8_t final_ack, unsigned first_bsn, struct bitvec *rbb);
433
struct msgb *create_dl_acked_block(uint32_t fn, uint8_t ts);
434
void trigger_ass(struct gprs_rlcmac_tbf *old_tbf);
435
void clear_poll_timeout_flag();
436
bool handle_ack_nack();
437
void request_dl_ack();
438
bool need_control_ts() const;
439
bool have_data() const;
440
int frames_since_last_poll(unsigned fn) const;
441
int frames_since_last_drain(unsigned fn) const;
442
bool keep_open(unsigned fn) const;
446
void egprs_calc_window_size();
447
void update_coding_scheme_counter_dl(const GprsCodingScheme cs);
449
/* TODO: add the gettimeofday as parameter */
450
struct msgb *llc_dequeue(bssgp_bvc_ctx *bctx);
452
/* Please note that all variables here will be reset when changing
453
* from WAIT RELEASE back to FLOW state (re-use of TBF).
454
* All states that need reset must be in this struct, so this is why
455
* variables are in both (dl and ul) structs and not outside union.
457
gprs_rlc_dl_window m_window;
458
int32_t m_tx_counter; /* count all transmitted blocks */
459
uint8_t m_wait_confirm; /* wait for CCCH IMM.ASS cnf */
460
bool m_dl_ack_requested;
461
int32_t m_last_dl_poll_fn;
462
int32_t m_last_dl_drained_fn;
465
struct timeval dl_bw_tv; /* timestamp for dl bw calculation */
466
uint32_t dl_bw_octets; /* number of octets since bw_tv */
467
uint32_t dl_throughput; /* throughput to be displayed in stats */
469
struct timeval dl_loss_tv; /* timestamp for loss calculation */
470
uint16_t dl_loss_lost; /* sum of lost packets */
471
uint16_t dl_loss_received; /* sum of received packets */
476
struct rate_ctr_group *m_dl_gprs_ctrs;
477
struct rate_ctr_group *m_dl_egprs_ctrs;
481
unsigned received_packets;
482
unsigned lost_packets;
483
unsigned received_bytes;
487
int take_next_bsn(uint32_t fn, int previous_bsn,
489
bool restart_bsn_cycle();
490
int create_new_bsn(const uint32_t fn, GprsCodingScheme cs);
491
struct msgb *create_dl_acked_block(const uint32_t fn, const uint8_t ts,
492
int index, int index2 = -1);
493
int update_window(const uint8_t ssn, const uint8_t *rbb);
494
int update_window(unsigned first_bsn, const struct bitvec *rbb);
495
int maybe_start_new_window();
496
bool dl_window_stalled() const;
498
void start_llc_timer();
499
int analyse_errors(char *show_rbb, uint8_t ssn, ana_result *res);
500
void schedule_next_frame();
502
enum egprs_rlc_dl_reseg_bsn_state egprs_dl_get_data
503
(int bsn, uint8_t **block_data);
504
unsigned int get_egprs_dl_spb_status(int bsn);
505
enum egprs_rlcmac_dl_spb get_egprs_dl_spb(int bsn);
507
struct osmo_timer_list m_llc_timer;
510
struct gprs_rlcmac_ul_tbf : public gprs_rlcmac_tbf {
511
gprs_rlcmac_ul_tbf(BTS *bts);
513
struct msgb *create_ul_ack(uint32_t fn, uint8_t ts);
514
bool ctrl_ack_to_toggle();
515
bool handle_ctrl_ack();
517
/* blocks were acked */
518
int rcv_data_block_acknowledged(
519
const struct gprs_rlc_data_info *rlc,
520
uint8_t *data, struct pcu_l1_meas *meas);
523
/* TODO: extract LLC class? */
524
int assemble_forward_llc(const gprs_rlc_data *data);
527
egprs_rlc_ul_reseg_bsn_state handle_egprs_ul_spb(
528
const struct gprs_rlc_data_info *rlc,
529
struct gprs_rlc_data *block,
530
uint8_t *data, const uint8_t block_idx);
532
egprs_rlc_ul_reseg_bsn_state handle_egprs_ul_first_seg(
533
const struct gprs_rlc_data_info *rlc,
534
struct gprs_rlc_data *block,
535
uint8_t *data, const uint8_t block_idx);
537
egprs_rlc_ul_reseg_bsn_state handle_egprs_ul_second_seg(
538
const struct gprs_rlc_data_info *rlc,
539
struct gprs_rlc_data *block,
540
uint8_t *data, const uint8_t block_idx);
542
void egprs_calc_ulwindow_size();
544
void update_coding_scheme_counter_ul(const GprsCodingScheme cs);
546
/* Please note that all variables here will be reset when changing
547
* from WAIT RELEASE back to FLOW state (re-use of TBF).
548
* All states that need reset must be in this struct, so this is why
549
* variables are in both (dl and ul) structs and not outside union.
551
gprs_rlc_ul_window m_window;
552
int32_t m_rx_counter; /* count all received blocks */
553
uint8_t m_n3103; /* N3103 counter */
554
uint8_t m_usf[8]; /* list USFs per PDCH (timeslot) */
555
uint8_t m_contention_resolution_done; /* set after done */
556
uint8_t m_final_ack_sent; /* set if we sent final ack */
558
struct rate_ctr_group *m_ul_gprs_ctrs;
559
struct rate_ctr_group *m_ul_egprs_ctrs;
562
void maybe_schedule_uplink_acknack(const gprs_rlc_data_info *rlc);
565
inline enum gprs_rlcmac_tbf_direction reverse(enum gprs_rlcmac_tbf_direction dir)
567
return (enum gprs_rlcmac_tbf_direction)
568
((int)GPRS_RLCMAC_UL_TBF - (int)dir + (int)GPRS_RLCMAC_DL_TBF);
571
inline gprs_rlcmac_ul_tbf *as_ul_tbf(gprs_rlcmac_tbf *tbf)
573
if (tbf && tbf->direction == GPRS_RLCMAC_UL_TBF)
574
return static_cast<gprs_rlcmac_ul_tbf *>(tbf);
579
inline gprs_rlcmac_dl_tbf *as_dl_tbf(gprs_rlcmac_tbf *tbf)
581
if (tbf && tbf->direction == GPRS_RLCMAC_DL_TBF)
582
return static_cast<gprs_rlcmac_dl_tbf *>(tbf);
587
inline gprs_rlc_window *gprs_rlcmac_tbf::window()
591
case GPRS_RLCMAC_UL_TBF: return &as_ul_tbf(this)->m_window;
592
case GPRS_RLCMAC_DL_TBF: return &as_dl_tbf(this)->m_window;