blob: 4cd0cc45122a764a74a5adcd4b86b75f9f2f3e22 [file] [log] [blame]
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +01001/* Copied from tbf.cpp
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 * 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 Freyther096f6f92013-11-07 07:21:06 +010022#include <bts.h>
23
Holger Hans Peter Freyther857281f2013-11-13 14:56:55 +010024#include <stdio.h>
25
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010026extern "C" {
27#include <osmocom/core/msgb.h>
28}
29
30/* reset LLC frame */
31void gprs_llc::reset()
32{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010033 m_index = 0;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010034 m_length = 0;
Holger Hans Peter Freythera42b2ad2013-11-26 16:39:47 +010035
36 memset(frame, 0x42, sizeof(frame));
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010037}
38
39void gprs_llc::reset_frame_space()
40{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010041 m_index = 0;
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010042}
43
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010044/* Put an Unconfirmed Information (UI) Dummy command, see GSM 44.064, 6.4.2.2 */
45void gprs_llc::put_dummy_frame(size_t req_len)
46{
47 /* The shortest dummy command (the spec requests at least 6 octets) */
48 static const uint8_t llc_dummy_command[] = {
49 0x43, 0xc0, 0x01, 0x2b, 0x2b, 0x2b
50 };
Jacob Erlbeck3db617f2015-07-07 13:43:44 +020051 static const size_t max_dummy_command_len = 79;
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010052
53 put_frame(llc_dummy_command, sizeof(llc_dummy_command));
54
55 if (req_len > max_dummy_command_len)
56 req_len = max_dummy_command_len;
57
58 /* Add further stuffing, if the requested length exceeds the minimum
59 * dummy command length */
60 while (m_length < req_len)
61 frame[m_length++] = 0x2b;
62}
63
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010064void gprs_llc::put_frame(const uint8_t *data, size_t len)
65{
Holger Hans Peter Freyther857281f2013-11-13 14:56:55 +010066 /* only put frames when we are empty */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010067 OSMO_ASSERT(m_index == 0 && m_length == 0);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010068 append_frame(data, len);
69}
Holger Hans Peter Freyther857281f2013-11-13 14:56:55 +010070
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010071void gprs_llc::append_frame(const uint8_t *data, size_t len)
72{
Holger Hans Peter Freyther857281f2013-11-13 14:56:55 +010073 /* TODO: bounds check */
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +010074 memcpy(frame + m_length, data, len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010075 m_length += len;
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +010076}
77
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020078void gprs_llc::init()
79{
80 reset();
81}
82
83bool gprs_llc::is_user_data_frame(uint8_t *data, size_t len)
84{
85 if (len < 2)
86 return false;
87
88 if ((data[0] & 0x0f) == 1 /* GPRS_SAPI_GMM */)
89 return false;
90
Jacob Erlbeckd0aee852015-06-11 11:47:06 +020091 if ((data[0] & 0xe0) != 0xc0 /* LLC UI */)
92 /* It is not an LLC UI frame, see TS 44.064, 6.3 */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020093 return false;
94
95 return true;
96}
97
98void gprs_llc_queue::init()
99{
100 INIT_LLIST_HEAD(&m_queue);
101 m_queue_size = 0;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200102 m_queue_octets = 0;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200103 m_avg_queue_delay = 0;
104}
105
Pau Espin Pedrol5fc6e012020-02-26 20:05:33 +0100106
107void gprs_llc_queue::enqueue(struct msgb *llc_msg, const struct timeval *expire_time)
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200108{
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200109 MetaInfo *meta_storage;
110
Pau Espin Pedrol5fc6e012020-02-26 20:05:33 +0100111 osmo_static_assert(sizeof(*meta_storage) <= sizeof(llc_msg->cb), info_does_not_fit);
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200112
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200113 m_queue_size += 1;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200114 m_queue_octets += msgb_length(llc_msg);
115
116 meta_storage = (MetaInfo *)&llc_msg->cb[0];
Pau Espin Pedrol5fc6e012020-02-26 20:05:33 +0100117 osmo_gettimeofday(&meta_storage->recv_time, NULL);
118 meta_storage->expire_time = *expire_time;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200119
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200120 msgb_enqueue(&m_queue, llc_msg);
121}
122
123void gprs_llc_queue::clear(BTS *bts)
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +0100124{
125 struct msgb *msg;
126
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200127 while ((msg = msgb_dequeue(&m_queue))) {
128 if (bts)
129 bts->llc_dropped_frame();
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +0100130 msgb_free(msg);
131 }
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +0100132
133 m_queue_size = 0;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200134 m_queue_octets = 0;
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +0100135}
136
Jacob Erlbeck257b6302015-08-21 18:07:47 +0200137void gprs_llc_queue::move_and_merge(gprs_llc_queue *o)
138{
139 struct msgb *msg, *msg1 = NULL, *msg2 = NULL;
140 struct llist_head new_queue;
141 size_t queue_size = 0;
142 size_t queue_octets = 0;
143 INIT_LLIST_HEAD(&new_queue);
144
145 while (1) {
146 if (msg1 == NULL)
147 msg1 = msgb_dequeue(&m_queue);
148
149 if (msg2 == NULL)
150 msg2 = msgb_dequeue(&o->m_queue);
151
152 if (msg1 == NULL && msg2 == NULL)
153 break;
154
155 if (msg1 == NULL) {
156 msg = msg2;
157 msg2 = NULL;
158 } else if (msg2 == NULL) {
159 msg = msg1;
160 msg1 = NULL;
161 } else {
162 const MetaInfo *mi1 = (MetaInfo *)&msg1->cb[0];
163 const MetaInfo *mi2 = (MetaInfo *)&msg2->cb[0];
164
165 if (timercmp(&mi2->recv_time, &mi1->recv_time, >)) {
166 msg = msg1;
167 msg1 = NULL;
168 } else {
169 msg = msg2;
170 msg2 = NULL;
171 }
172 }
173
174 msgb_enqueue(&new_queue, msg);
175 queue_size += 1;
176 queue_octets += msgb_length(msg);
177 }
178
179 OSMO_ASSERT(llist_empty(&m_queue));
180 OSMO_ASSERT(llist_empty(&o->m_queue));
181
182 o->m_queue_size = 0;
183 o->m_queue_octets = 0;
184
185 llist_splice_init(&new_queue, &m_queue);
186 m_queue_size = queue_size;
187 m_queue_octets = queue_octets;
188}
189
Daniel Willmann9c623892013-12-04 18:11:47 +0100190#define ALPHA 0.5f
191
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200192struct msgb *gprs_llc_queue::dequeue(const MetaInfo **info)
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +0100193{
Daniel Willmann9c623892013-12-04 18:11:47 +0100194 struct msgb *msg;
195 struct timeval *tv, tv_now, tv_result;
196 uint32_t lifetime;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200197 const MetaInfo *meta_storage;
Daniel Willmann9c623892013-12-04 18:11:47 +0100198
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200199 msg = msgb_dequeue(&m_queue);
Daniel Willmann9c623892013-12-04 18:11:47 +0100200 if (!msg)
201 return NULL;
202
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200203 meta_storage = (MetaInfo *)&msg->cb[0];
204
205 if (info)
206 *info = meta_storage;
207
Daniel Willmann9c623892013-12-04 18:11:47 +0100208 m_queue_size -= 1;
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200209 m_queue_octets -= msgb_length(msg);
Daniel Willmann9c623892013-12-04 18:11:47 +0100210
211 /* take the second time */
Pau Espin Pedrol5fc6e012020-02-26 20:05:33 +0100212 osmo_gettimeofday(&tv_now, NULL);
Daniel Willmann9c623892013-12-04 18:11:47 +0100213 tv = (struct timeval *)&msg->data[sizeof(*tv)];
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +0200214 timersub(&tv_now, &meta_storage->recv_time, &tv_result);
Daniel Willmann9c623892013-12-04 18:11:47 +0100215
216 lifetime = tv_result.tv_sec*1000 + tv_result.tv_usec/1000;
217 m_avg_queue_delay = m_avg_queue_delay * ALPHA + lifetime * (1-ALPHA);
218
219 return msg;
Holger Hans Peter Freyther096f6f92013-11-07 07:21:06 +0100220}
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +0100221
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200222void gprs_llc_queue::calc_pdu_lifetime(BTS *bts, const uint16_t pdu_delay_csec, struct timeval *tv)
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +0100223{
224 uint16_t delay_csec;
225 if (bts->bts_data()->force_llc_lifetime)
226 delay_csec = bts->bts_data()->force_llc_lifetime;
227 else
228 delay_csec = pdu_delay_csec;
229
Holger Hans Peter Freyther5a7f6362013-11-21 21:59:32 +0100230 /* keep timestamp at 0 for infinite delay */
Holger Hans Peter Freyther985fd112013-11-13 15:19:39 +0100231 if (delay_csec == 0xffff) {
232 memset(tv, 0, sizeof(*tv));
233 return;
234 }
235
236 /* calculate timestamp of timeout */
Holger Hans Peter Freyther51e093b2013-11-13 15:35:45 +0100237 struct timeval now, csec;
Pau Espin Pedrol5fc6e012020-02-26 20:05:33 +0100238 osmo_gettimeofday(&now, NULL);
Holger Hans Peter Freyther51e093b2013-11-13 15:35:45 +0100239 csec.tv_usec = (delay_csec % 100) * 10000;
240 csec.tv_sec = delay_csec / 100;
241
242 timeradd(&now, &csec, tv);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +0100243}
Holger Hans Peter Freytherb1302b02013-11-13 17:15:26 +0100244
Jacob Erlbeck1e50a3d2015-06-16 10:18:42 +0200245bool gprs_llc_queue::is_frame_expired(const struct timeval *tv_now,
246 const struct timeval *tv)
Holger Hans Peter Freytherb1302b02013-11-13 17:15:26 +0100247{
248 /* Timeout is infinite */
249 if (tv->tv_sec == 0 && tv->tv_usec == 0)
250 return false;
251
252 return timercmp(tv_now, tv, >);
253}