blob: 54781c0f37b9e1e236a3637d89ddbf362c0bce16 [file] [log] [blame]
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +02001/*
2 * Copyright (C) 2013 by Holger Hans Peter Freyther
3 *
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.
8 *
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.
13 *
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.
17 */
18
19#pragma once
20
Daniel Willmannafa72f52014-01-15 17:06:19 +010021#ifdef __cplusplus
22
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +020023#include "gprs_rlcmac.h"
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010024#include "llc.h"
Holger Hans Peter Freyther6b5660c2013-11-23 16:10:48 +010025#include "rlc.h"
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +020026
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +020027#include <stdint.h>
28
Holger Hans Peter Freyther1702f102013-10-20 08:44:02 +020029struct bssgp_bvc_ctx;
Holger Hans Peter Freyther77e05972013-11-06 19:16:43 +010030struct rlc_ul_header;
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010031struct msgb;
Holger Hans Peter Freyther1702f102013-10-20 08:44:02 +020032
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +020033/*
34 * TBF instance
35 */
36
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +020037#define Tassign_agch 0,200000 /* waiting after IMM.ASS confirm */
38#define Tassign_pacch 2,0 /* timeout for pacch assigment */
39
40enum gprs_rlcmac_tbf_state {
41 GPRS_RLCMAC_NULL = 0, /* new created TBF */
42 GPRS_RLCMAC_ASSIGN, /* wait for downlink assignment */
43 GPRS_RLCMAC_FLOW, /* RLC/MAC flow, resource needed */
44 GPRS_RLCMAC_FINISHED, /* flow finished, wait for release */
45 GPRS_RLCMAC_WAIT_RELEASE,/* wait for release or restart of DL TBF */
46 GPRS_RLCMAC_RELEASING, /* releasing, wait to free TBI/USF */
47};
48
49enum gprs_rlcmac_tbf_poll_state {
50 GPRS_RLCMAC_POLL_NONE = 0,
51 GPRS_RLCMAC_POLL_SCHED, /* a polling was scheduled */
52};
53
54enum gprs_rlcmac_tbf_dl_ass_state {
55 GPRS_RLCMAC_DL_ASS_NONE = 0,
56 GPRS_RLCMAC_DL_ASS_SEND_ASS, /* send downlink assignment on next RTS */
57 GPRS_RLCMAC_DL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
58};
59
60enum gprs_rlcmac_tbf_ul_ass_state {
61 GPRS_RLCMAC_UL_ASS_NONE = 0,
62 GPRS_RLCMAC_UL_ASS_SEND_ASS, /* send uplink assignment on next RTS */
63 GPRS_RLCMAC_UL_ASS_WAIT_ACK, /* wait for PACKET CONTROL ACK */
64};
65
66enum gprs_rlcmac_tbf_ul_ack_state {
67 GPRS_RLCMAC_UL_ACK_NONE = 0,
68 GPRS_RLCMAC_UL_ACK_SEND_ACK, /* send acknowledge on next RTS */
69 GPRS_RLCMAC_UL_ACK_WAIT_ACK, /* wait for PACKET CONTROL ACK */
70};
71
72enum gprs_rlcmac_tbf_direction {
73 GPRS_RLCMAC_DL_TBF,
74 GPRS_RLCMAC_UL_TBF
75};
76
77#define GPRS_RLCMAC_FLAG_CCCH 0 /* assignment on CCCH */
78#define GPRS_RLCMAC_FLAG_PACCH 1 /* assignment on PACCH */
79#define GPRS_RLCMAC_FLAG_UL_DATA 2 /* uplink data received */
80#define GPRS_RLCMAC_FLAG_DL_ACK 3 /* downlink acknowledge received */
81#define GPRS_RLCMAC_FLAG_TO_UL_ACK 4
82#define GPRS_RLCMAC_FLAG_TO_DL_ACK 5
83#define GPRS_RLCMAC_FLAG_TO_UL_ASS 6
84#define GPRS_RLCMAC_FLAG_TO_DL_ASS 7
85#define GPRS_RLCMAC_FLAG_TO_MASK 0xf0 /* timeout bits */
86
Daniel Willmann1b3864f2014-07-30 13:25:19 +020087struct llist_pods {
88 struct llist_head list;
89 void *back;
90};
91
92#define llist_pods_entry(ptr, type) \
93 ((type *)(container_of(ptr, struct llist_pods, list)->back))
94
95/**
96 * llist_pods_for_each_entry - like llist_for_each_entry, but uses
97 * struct llist_pods ->back to access the entry.
98 * This is necessary for non-PODS classes because container_of is
99 * not guaranteed to work anymore. */
100#define llist_pods_for_each_entry(pos, head, member, lpods) \
101 for (lpods = llist_entry((head)->next, typeof(struct llist_pods), list), \
102 pos = ((typeof(pos))lpods->back), \
103 prefetch(pos->member.list.next); \
104 &lpods->list != (head); \
105 lpods = llist_entry(lpods->list.next, typeof(struct llist_pods), list), \
106 pos = ((typeof(pos))lpods->back),\
107 prefetch(pos->member.list.next))
108
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200109struct gprs_rlcmac_tbf {
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200110
111 static void free_all(struct gprs_rlcmac_trx *trx);
Holger Hans Peter Freyther4f6a4e5d2013-10-16 17:58:46 +0200112 static void free_all(struct gprs_rlcmac_pdch *pdch);
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200113
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200114 bool state_is(enum gprs_rlcmac_tbf_state rhs) const;
115 bool state_is_not(enum gprs_rlcmac_tbf_state rhs) const;
116 void set_state(enum gprs_rlcmac_tbf_state new_state);
117
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +0200118 struct msgb *create_dl_ass(uint32_t fn);
Holger Hans Peter Freytherae03f222013-10-26 21:20:51 +0200119 struct msgb *create_ul_ass(uint32_t fn);
Holger Hans Peter Freyther2db7e7e2013-10-26 20:45:35 +0200120
Holger Hans Peter Freyther8f399de2013-12-25 20:22:35 +0100121 uint8_t tsc() const;
Holger Hans Peter Freythera1da2512013-11-07 07:32:51 +0100122
Holger Hans Peter Freyther7380bab2013-10-16 18:09:19 +0200123 int rlcmac_diag();
Holger Hans Peter Freyther964ddb62013-10-16 17:53:23 +0200124
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200125 int update();
Holger Hans Peter Freytheraf8094d2013-10-26 17:56:15 +0200126 void handle_timeout();
Holger Hans Peter Freyther86300bb2013-10-26 18:01:35 +0200127 void stop_timer();
128 void stop_t3191();
Holger Hans Peter Freytheraa9c3262013-10-26 17:49:36 +0200129
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200130 void poll_timeout();
131
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100132 /** tlli handling */
133 void update_tlli(uint32_t tlli);
134 uint32_t tlli() const;
135 bool is_tlli_valid() const;
136 void tlli_mark_valid();
137
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100138 uint8_t tfi() const;
139
Holger Hans Peter Freyther5464c9b2013-10-27 20:57:35 +0100140 const char *imsi() const;
141 void assign_imsi(const char *imsi);
142
Holger Hans Peter Freythera0047992014-01-16 10:07:20 +0100143 time_t created_ts() const;
Daniel Willmann80367aa2014-01-15 17:40:28 +0100144
Holger Hans Peter Freyther7a5f3c22013-11-26 13:08:12 +0100145 /* attempt to make things a bit more fair */
146 void rotate_in_list();
147
Daniel Willmann1b3864f2014-07-30 13:25:19 +0200148 struct llist_pods list;
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200149 uint32_t state_flags;
150 enum gprs_rlcmac_tbf_direction direction;
Holger Hans Peter Freyther743bafa2013-09-29 07:50:50 +0200151 struct gprs_rlcmac_trx *trx;
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200152 uint8_t first_ts; /* first TS used by TBF */
153 uint8_t first_common_ts; /* first TS that the phone can send and
154 reveive simultaniously */
155 uint8_t control_ts; /* timeslot control messages and polling */
156 uint8_t ms_class;
157 struct gprs_rlcmac_pdch *pdch[8]; /* list of PDCHs allocated to TBF */
158 uint16_t ta;
Holger Hans Peter Freyther28e53782013-11-06 20:23:56 +0100159
160 gprs_llc m_llc;
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200161
162 enum gprs_rlcmac_tbf_dl_ass_state dl_ass_state;
163 enum gprs_rlcmac_tbf_ul_ass_state ul_ass_state;
164 enum gprs_rlcmac_tbf_ul_ack_state ul_ack_state;
165
166 enum gprs_rlcmac_tbf_poll_state poll_state;
167 uint32_t poll_fn; /* frame number to poll */
168
Holger Hans Peter Freyther9241fd02013-11-13 19:51:55 +0100169 gprs_rlc m_rlc;
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200170
171 uint8_t n3105; /* N3105 counter */
172
173 struct osmo_timer_list timer;
174 unsigned int T; /* Txxxx number */
175 unsigned int num_T_exp; /* number of consecutive T expirations */
176
177 struct osmo_gsm_timer_list gsm_timer;
178 unsigned int fT; /* fTxxxx number */
179 unsigned int num_fT_exp; /* number of consecutive fT expirations */
180
181 struct {
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200182 struct timeval rssi_tv; /* timestamp for rssi calculation */
183 int32_t rssi_sum; /* sum of rssi values */
184 int rssi_num; /* number of rssi values added since rssi_tv */
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200185 } meas;
186
187 uint8_t cs; /* current coding scheme */
188
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200189 /* these should become protected but only after gprs_rlcmac_data.c
190 * stops to iterate over all tbf in its current form */
191 enum gprs_rlcmac_tbf_state state;
Holger Hans Peter Freyther9f0c1d22013-10-19 21:24:34 +0200192
Daniel Willmann73191a42014-05-30 17:58:00 +0200193 /* Remember if the tbf was in wait_release state when we want to
194 * schedule a new dl assignment */
195 uint8_t was_releasing;
196
Daniel Willmanncf1fae72014-05-30 17:58:01 +0200197 /* Can/should we upgrade this tbf to use multiple slots? */
198 uint8_t upgrade_to_multislot;
199
Holger Hans Peter Freyther9f0c1d22013-10-19 21:24:34 +0200200 /* store the BTS this TBF belongs to */
201 BTS *bts;
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200202
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100203 /*
204 * private fields. We can't make it private as it is breaking the
205 * llist macros.
206 */
207 uint32_t m_tlli;
208 uint8_t m_tlli_valid;
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100209 uint8_t m_tfi;
Holger Hans Peter Freythera0047992014-01-16 10:07:20 +0100210 time_t m_created_ts;
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100211
Holger Hans Peter Freyther5464c9b2013-10-27 20:57:35 +0100212 /* store IMSI for look-up and PCH retransmission */
213 char m_imsi[16];
214
Holger Hans Peter Freytherd9262b32013-10-26 20:12:59 +0200215protected:
216 gprs_rlcmac_bts *bts_data() const;
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100217
Holger Hans Peter Freyther4c06d912013-11-25 23:05:26 +0100218 int extract_tlli(const uint8_t *data, const size_t len);
219
Holger Hans Peter Freyther099535a2013-10-16 17:42:31 +0200220};
221
Holger Hans Peter Freyther17c31ce2013-08-24 18:31:27 +0200222
Daniel Willmann0d12a2f2014-07-10 17:44:07 +0200223struct gprs_rlcmac_ul_tbf *tbf_alloc_ul(struct gprs_rlcmac_bts *bts,
Holger Hans Peter Freyther86921282013-08-24 21:26:42 +0200224 int8_t use_trx, uint8_t ms_class,
225 uint32_t tlli, uint8_t ta, struct gprs_rlcmac_tbf *dl_tbf);
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +0200226
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200227struct gprs_rlcmac_ul_tbf *tbf_alloc_ul_tbf(struct gprs_rlcmac_bts *bts,
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +0200228 struct gprs_rlcmac_tbf *old_tbf,
Daniel Willmann48aa0b02014-07-16 18:54:10 +0200229 uint8_t tfi, uint8_t trx,
230 uint8_t ms_class, uint8_t single_slot);
231
232struct gprs_rlcmac_dl_tbf *tbf_alloc_dl_tbf(struct gprs_rlcmac_bts *bts,
233 struct gprs_rlcmac_tbf *old_tbf,
234 uint8_t tfi, uint8_t trx,
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +0200235 uint8_t ms_class, uint8_t single_slot);
236
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +0200237void tbf_free(struct gprs_rlcmac_tbf *tbf);
238
Holger Hans Peter Freyther9e21d842013-10-16 17:48:12 +0200239int tbf_assign_control_ts(struct gprs_rlcmac_tbf *tbf);
240
241void tbf_new_state(struct gprs_rlcmac_tbf *tbf,
242 enum gprs_rlcmac_tbf_state state);
243
244void tbf_timer_start(struct gprs_rlcmac_tbf *tbf, unsigned int T,
245 unsigned int seconds, unsigned int microseconds);
246
Holger Hans Peter Freyther1c344e22013-10-16 18:33:18 +0200247inline bool gprs_rlcmac_tbf::state_is(enum gprs_rlcmac_tbf_state rhs) const
248{
249 return state == rhs;
250}
251
252inline bool gprs_rlcmac_tbf::state_is_not(enum gprs_rlcmac_tbf_state rhs) const
253{
254 return state != rhs;
255}
256
257inline void gprs_rlcmac_tbf::set_state(enum gprs_rlcmac_tbf_state new_state)
258{
259 state = new_state;
260}
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100261
Holger Hans Peter Freyther474685e2013-10-27 17:01:14 +0100262inline uint32_t gprs_rlcmac_tbf::tlli() const
263{
264 return m_tlli;
265}
266
267inline bool gprs_rlcmac_tbf::is_tlli_valid() const
268{
269 return m_tlli_valid;
270}
271
Holger Hans Peter Freyther34f6e5e2013-10-27 20:31:47 +0100272inline uint8_t gprs_rlcmac_tbf::tfi() const
273{
274 return m_tfi;
275}
276
Holger Hans Peter Freyther5464c9b2013-10-27 20:57:35 +0100277inline const char *gprs_rlcmac_tbf::imsi() const
278{
279 return m_imsi;
280}
281
Holger Hans Peter Freytherbd449f52013-10-27 16:39:36 +0100282const char *tbf_name(gprs_rlcmac_tbf *tbf);
Daniel Willmann80367aa2014-01-15 17:40:28 +0100283
Holger Hans Peter Freythera0047992014-01-16 10:07:20 +0100284inline time_t gprs_rlcmac_tbf::created_ts() const
Daniel Willmann80367aa2014-01-15 17:40:28 +0100285{
Holger Hans Peter Freythera0047992014-01-16 10:07:20 +0100286 return m_created_ts;
Daniel Willmann80367aa2014-01-15 17:40:28 +0100287}
288
Daniel Willmann078bb712014-07-10 17:44:06 +0200289struct gprs_rlcmac_dl_tbf : public gprs_rlcmac_tbf {
Daniel Willmann30168882014-08-07 12:48:29 +0200290 /* dispatch Unitdata.DL messages */
291 static int handle(struct gprs_rlcmac_bts *bts,
292 const uint32_t tlli, const char *imsi, const uint8_t ms_class,
293 const uint16_t delay_csec, const uint8_t *data, const uint16_t len);
294
Daniel Willmann538ac5b2014-07-30 19:12:27 +0200295 int append_data(const uint8_t ms_class,
296 const uint16_t pdu_delay_csec,
297 const uint8_t *data, const uint16_t len);
298
Daniel Willmann6c813fc2014-07-30 19:12:27 +0200299 int rcvd_dl_ack(uint8_t final, uint8_t ssn, uint8_t *rbb);
Daniel Willmannb8f26012014-07-30 19:12:28 +0200300 struct msgb *create_dl_acked_block(uint32_t fn, uint8_t ts);
301
Daniel Willmanncf706b02014-08-07 17:35:22 +0200302 /* TODO: add the gettimeofday as parameter */
303 struct msgb *llc_dequeue(bssgp_bvc_ctx *bctx);
304
Daniel Willmann6a8a1dc2014-08-07 15:14:08 +0200305 /* Please note that all variables here will be reset when changing
306 * from WAIT RELEASE back to FLOW state (re-use of TBF).
307 * All states that need reset must be in this struct, so this is why
308 * variables are in both (dl and ul) structs and not outside union.
309 */
Daniel Willmann7e994e32014-08-07 15:49:21 +0200310 gprs_rlc_dl_window m_window;
311 int32_t m_tx_counter; /* count all transmitted blocks */
312 uint8_t m_wait_confirm; /* wait for CCCH IMM.ASS cnf */
Daniel Willmann6a8a1dc2014-08-07 15:14:08 +0200313
Daniel Willmann418a4232014-08-08 11:21:04 +0200314 struct {
315 struct timeval dl_bw_tv; /* timestamp for dl bw calculation */
316 uint32_t dl_bw_octets; /* number of octets since bw_tv */
317
318 struct timeval dl_loss_tv; /* timestamp for loss calculation */
319 uint16_t dl_loss_lost; /* sum of lost packets */
320 uint16_t dl_loss_received; /* sum of received packets */
321 } m_bw;
322
Daniel Willmannb8f26012014-07-30 19:12:28 +0200323protected:
324 struct msgb *create_new_bsn(const uint32_t fn, const uint8_t ts);
325 struct msgb *create_dl_acked_block(const uint32_t fn, const uint8_t ts,
326 const int index, const bool fin_first_ack);
Daniel Willmann30168882014-08-07 12:48:29 +0200327 int update_window(const uint8_t ssn, const uint8_t *rbb);
328 int maybe_start_new_window();
329 bool dl_window_stalled() const;
330 void reuse_tbf(const uint8_t *data, const uint16_t len);
Daniel Willmann078bb712014-07-10 17:44:06 +0200331};
332
333struct gprs_rlcmac_ul_tbf : public gprs_rlcmac_tbf {
Daniel Willmann350f64d2014-08-07 14:54:11 +0200334 struct msgb *create_ul_ack(uint32_t fn);
335
336 /* blocks were acked */
337 int rcv_data_block_acknowledged(const uint8_t *data, size_t len, int8_t rssi);
338
Daniel Willmanne2732e22014-08-07 17:32:01 +0200339 /* TODO: extract LLC class? */
340 int assemble_forward_llc(const gprs_rlc_data *data);
341 int snd_ul_ud();
342
Daniel Willmann6a8a1dc2014-08-07 15:14:08 +0200343 /* Please note that all variables here will be reset when changing
344 * from WAIT RELEASE back to FLOW state (re-use of TBF).
345 * All states that need reset must be in this struct, so this is why
346 * variables are in both (dl and ul) structs and not outside union.
347 */
Daniel Willmann7e994e32014-08-07 15:49:21 +0200348 gprs_rlc_ul_window m_window;
349 int32_t m_rx_counter; /* count all received blocks */
350 uint8_t m_n3103; /* N3103 counter */
351 uint8_t m_usf[8]; /* list USFs per PDCH (timeslot) */
352 uint8_t m_contention_resolution_done; /* set after done */
353 uint8_t m_final_ack_sent; /* set if we sent final ack */
Daniel Willmann6a8a1dc2014-08-07 15:14:08 +0200354
Daniel Willmann350f64d2014-08-07 14:54:11 +0200355protected:
356 void maybe_schedule_uplink_acknack(const rlc_ul_header *rh);
Daniel Willmann078bb712014-07-10 17:44:06 +0200357};
358
Daniel Willmannafa72f52014-01-15 17:06:19 +0100359#endif
Holger Hans Peter Freyther321f3c32013-11-23 16:06:54 +0100360
Daniel Willmannafa72f52014-01-15 17:06:19 +0100361#ifdef __cplusplus
362extern "C" {
363#endif
364#include <osmocom/vty/command.h>
365#include <osmocom/vty/vty.h>
366
367
368 void tbf_print_vty_info(struct vty *vty, llist_head *tbf);
369#ifdef __cplusplus
370}
371#endif