blob: fe8958fddf371a61beb74f15bdd6175abd63a4f2 [file] [log] [blame]
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +01001/*
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
Jacob Erlbeck1eae96c2015-06-15 11:19:13 +020021extern "C" {
22 #include <osmocom/core/linuxlist.h>
23}
24
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010025#include <stdint.h>
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010026#include <string.h>
Jacob Erlbecke500e2e2015-06-29 09:32:46 +020027#include <sys/time.h>
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010028
29#define LLC_MAX_LEN 1543
30
Jacob Erlbeck1d0a52a2015-06-02 11:28:07 +020031struct BTS;
32struct timeval;
33struct msgb;
34
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010035/**
36 * I represent the LLC data to a MS
37 */
38struct gprs_llc {
Jacob Erlbeck0c1c8772015-03-20 12:02:42 +010039 static bool is_user_data_frame(uint8_t *data, size_t len);
Holger Hans Peter Freytherfce431c2013-11-13 15:17:12 +010040
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010041 void init();
42 void reset();
43 void reset_frame_space();
44
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010045 void put_frame(const uint8_t *data, size_t len);
Jacob Erlbeck612e93e2015-03-20 13:57:27 +010046 void put_dummy_frame(size_t req_len);
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010047 void append_frame(const uint8_t *data, size_t len);
48
49 void consume(size_t len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010050 void consume(uint8_t *data, size_t len);
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010051
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010052 uint16_t chunk_size() const;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010053 uint16_t remaining_space() const;
54 uint16_t frame_length() const;
55
56 bool fits_in_current_frame(uint8_t size) const;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010057
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010058 uint8_t frame[LLC_MAX_LEN]; /* current DL or UL frame */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010059 uint16_t m_index; /* current write/read position of frame */
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010060 uint16_t m_length; /* len of current DL LLC_frame, 0 == no frame */
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020061};
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010062
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020063/**
64 * I store the LLC frames that come from the SGSN.
65 */
66struct gprs_llc_queue {
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020067 struct MetaInfo {
68 struct timeval recv_time;
69 struct timeval expire_time;
70 };
71
Jacob Erlbeck1e50a3d2015-06-16 10:18:42 +020072 static void calc_pdu_lifetime(BTS *bts, const uint16_t pdu_delay_csec,
73 struct timeval *tv);
74 static bool is_frame_expired(const struct timeval *now,
75 const struct timeval *tv);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020076 static bool is_user_data_frame(uint8_t *data, size_t len);
77
78 void init();
79
Jacob Erlbeckb671dbf2015-06-15 14:32:33 +020080 void enqueue(struct msgb *llc_msg, const MetaInfo *info = 0);
81 struct msgb *dequeue(const MetaInfo **info = 0);
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020082 void clear(BTS *bts);
83 size_t size() const;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020084 size_t octets() const;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020085
86private:
Daniel Willmann9c623892013-12-04 18:11:47 +010087 uint32_t m_avg_queue_delay; /* Average delay of data going through the queue */
Holger Hans Peter Freyther550bb882013-12-04 17:10:54 +010088 size_t m_queue_size;
Jacob Erlbeck07eb6552015-06-15 11:05:44 +020089 size_t m_queue_octets;
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020090 struct llist_head m_queue; /* queued LLC DL data */
91
Holger Hans Peter Freytherbe570812013-11-07 08:01:49 +010092};
93
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +020094
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +010095inline uint16_t gprs_llc::chunk_size() const
96{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +010097 return m_length - m_index;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +010098}
99
100inline uint16_t gprs_llc::remaining_space() const
101{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100102 return LLC_MAX_LEN - m_length;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100103}
104
105inline uint16_t gprs_llc::frame_length() const
106{
107 return m_length;
108}
109
110inline void gprs_llc::consume(size_t len)
111{
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100112 m_index += len;
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100113}
114
115inline void gprs_llc::consume(uint8_t *data, size_t len)
116{
117 /* copy and increment index */
Holger Hans Peter Freyther32f9a592013-11-13 17:14:42 +0100118 memcpy(data, frame + m_index, len);
119 consume(len);
Holger Hans Peter Freytheracb54272013-11-07 08:15:58 +0100120}
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100121
122inline bool gprs_llc::fits_in_current_frame(uint8_t chunk_size) const
123{
Holger Hans Peter Freyther60582202013-11-21 21:30:23 +0100124 return m_length + chunk_size <= LLC_MAX_LEN;
Holger Hans Peter Freythere2310262013-11-13 16:56:15 +0100125}
Jacob Erlbeck6dbe8222015-05-29 10:37:09 +0200126
127inline size_t gprs_llc_queue::size() const
128{
129 return this ? m_queue_size : 0;
130}
Jacob Erlbeck07eb6552015-06-15 11:05:44 +0200131
132inline size_t gprs_llc_queue::octets() const
133{
134 return this ? m_queue_octets : 0;
135}